1 // SPDX-License-Identifier: GPL-2.0-only
3 * Port on Texas Instruments TMS320C6x architecture
5 * Copyright (C) 2004, 2006, 2009, 2010, 2011 Texas Instruments Incorporated
6 * Author: Aurelien Jacquiot (aurelien.jacquiot@jaluna.com)
8 * Updated for 2.6.34: Mark Salter <msalter@redhat.com>
11 #include <linux/kernel.h>
12 #include <linux/uaccess.h>
13 #include <linux/syscalls.h>
14 #include <linux/tracehook.h>
16 #include <asm/asm-offsets.h>
17 #include <asm/ucontext.h>
18 #include <asm/cacheflush.h>
22 * Do a signal return, undo the signal stack.
25 #define RETCODE_SIZE (9 << 2) /* 9 instructions = 36 bytes */
28 struct siginfo __user *pinfo;
32 unsigned long retcode[RETCODE_SIZE >> 2];
35 static int restore_sigcontext(struct pt_regs *regs,
36 struct sigcontext __user *sc)
40 /* The access_ok check was done by caller, so use __get_user here */
41 #define COPY(x) (err |= __get_user(regs->x, &sc->sc_##x))
43 COPY(sp); COPY(a4); COPY(b4); COPY(a6); COPY(b6); COPY(a8); COPY(b8);
44 COPY(a0); COPY(a1); COPY(a2); COPY(a3); COPY(a5); COPY(a7); COPY(a9);
45 COPY(b0); COPY(b1); COPY(b2); COPY(b3); COPY(b5); COPY(b7); COPY(b9);
47 COPY(a16); COPY(a17); COPY(a18); COPY(a19);
48 COPY(a20); COPY(a21); COPY(a22); COPY(a23);
49 COPY(a24); COPY(a25); COPY(a26); COPY(a27);
50 COPY(a28); COPY(a29); COPY(a30); COPY(a31);
51 COPY(b16); COPY(b17); COPY(b18); COPY(b19);
52 COPY(b20); COPY(b21); COPY(b22); COPY(b23);
53 COPY(b24); COPY(b25); COPY(b26); COPY(b27);
54 COPY(b28); COPY(b29); COPY(b30); COPY(b31);
63 asmlinkage int do_rt_sigreturn(struct pt_regs *regs)
65 struct rt_sigframe __user *frame;
68 /* Always make any pending restarted system calls return -EINTR */
69 current->restart_block.fn = do_no_restart_syscall;
72 * Since we stacked the signal on a dword boundary,
73 * 'sp' should be dword aligned here. If it's
74 * not, then the user is trying to mess with us.
79 frame = (struct rt_sigframe __user *) ((unsigned long) regs->sp + 8);
81 if (!access_ok(frame, sizeof(*frame)))
83 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
86 set_current_blocked(&set);
88 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
98 static int setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
103 err |= __put_user(mask, &sc->sc_mask);
105 /* The access_ok check was done by caller, so use __put_user here */
106 #define COPY(x) (err |= __put_user(regs->x, &sc->sc_##x))
108 COPY(sp); COPY(a4); COPY(b4); COPY(a6); COPY(b6); COPY(a8); COPY(b8);
109 COPY(a0); COPY(a1); COPY(a2); COPY(a3); COPY(a5); COPY(a7); COPY(a9);
110 COPY(b0); COPY(b1); COPY(b2); COPY(b3); COPY(b5); COPY(b7); COPY(b9);
112 COPY(a16); COPY(a17); COPY(a18); COPY(a19);
113 COPY(a20); COPY(a21); COPY(a22); COPY(a23);
114 COPY(a24); COPY(a25); COPY(a26); COPY(a27);
115 COPY(a28); COPY(a29); COPY(a30); COPY(a31);
116 COPY(b16); COPY(b17); COPY(b18); COPY(b19);
117 COPY(b20); COPY(b21); COPY(b22); COPY(b23);
118 COPY(b24); COPY(b25); COPY(b26); COPY(b27);
119 COPY(b28); COPY(b29); COPY(b30); COPY(b31);
128 static inline void __user *get_sigframe(struct ksignal *ksig,
129 struct pt_regs *regs,
130 unsigned long framesize)
132 unsigned long sp = sigsp(regs->sp, ksig);
135 * No matter what happens, 'sp' must be dword
136 * aligned. Otherwise, nasty things will happen
138 return (void __user *)((sp - framesize) & ~7);
141 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
142 struct pt_regs *regs)
144 struct rt_sigframe __user *frame;
145 unsigned long __user *retcode;
148 frame = get_sigframe(ksig, regs, sizeof(*frame));
150 if (!access_ok(frame, sizeof(*frame)))
153 err |= __put_user(&frame->info, &frame->pinfo);
154 err |= __put_user(&frame->uc, &frame->puc);
155 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
157 /* Clear all the bits of the ucontext we don't use. */
158 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
160 err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
161 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
163 /* Set up to return from userspace */
164 retcode = (unsigned long __user *) &frame->retcode;
166 /* The access_ok check was done above, so use __put_user here */
167 #define COPY(x) (err |= __put_user(x, retcode++))
169 COPY(0x0000002AUL | (__NR_rt_sigreturn << 7));
170 /* MVK __NR_rt_sigreturn,B0 */
171 COPY(0x10000000UL); /* SWE */
172 COPY(0x00006000UL); /* NOP 4 */
173 COPY(0x00006000UL); /* NOP 4 */
174 COPY(0x00006000UL); /* NOP 4 */
175 COPY(0x00006000UL); /* NOP 4 */
176 COPY(0x00006000UL); /* NOP 4 */
177 COPY(0x00006000UL); /* NOP 4 */
178 COPY(0x00006000UL); /* NOP 4 */
185 flush_icache_range((unsigned long) &frame->retcode,
186 (unsigned long) &frame->retcode + RETCODE_SIZE);
188 retcode = (unsigned long __user *) &frame->retcode;
190 /* Change user context to branch to signal handler */
191 regs->sp = (unsigned long) frame - 8;
192 regs->b3 = (unsigned long) retcode;
193 regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
195 /* Give the signal number to the handler */
196 regs->a4 = ksig->sig;
199 * For realtime signals we must also set the second and third
200 * arguments for the signal handler.
201 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
203 regs->b4 = (unsigned long)&frame->info;
204 regs->a6 = (unsigned long)&frame->uc;
210 handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
213 case -ERESTARTNOHAND:
220 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
225 case -ERESTARTNOINTR:
227 regs->a4 = regs->orig_a4;
234 * handle the actual delivery of a signal to userspace
236 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs,
241 /* Are we from a system call? */
243 /* If so, check system call restarting.. */
245 case -ERESTART_RESTARTBLOCK:
246 case -ERESTARTNOHAND:
251 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
257 case -ERESTARTNOINTR:
258 regs->a4 = regs->orig_a4;
263 /* Set up the stack frame */
264 ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
265 signal_setup_done(ret, ksig, 0);
269 * handle a potential signal
271 static void do_signal(struct pt_regs *regs, int syscall)
275 /* we want the common case to go fast, which is why we may in certain
276 * cases get here from kernel mode */
277 if (!user_mode(regs))
280 if (get_signal(&ksig)) {
281 handle_signal(&ksig, regs, syscall);
285 /* did we come from a system call? */
287 /* restart the system call - no handlers present */
289 case -ERESTARTNOHAND:
291 case -ERESTARTNOINTR:
292 regs->a4 = regs->orig_a4;
296 case -ERESTART_RESTARTBLOCK:
297 regs->a4 = regs->orig_a4;
298 regs->b0 = __NR_restart_syscall;
304 /* if there's no signal to deliver, we just put the saved sigmask
306 restore_saved_sigmask();
310 * notification of userspace execution resumption
311 * - triggered by current->work.notify_resume
313 asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags,
316 /* deal with pending signal delivery */
317 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
318 do_signal(regs, syscall);
320 if (thread_info_flags & (1 << TIF_NOTIFY_RESUME))
321 tracehook_notify_resume(regs);