~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/cris/arch-v32/kernel/signal.c

Version: ~ [ linux-6.1-rc7 ] ~ [ linux-6.0.10 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.80 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.156 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.225 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.267 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.300 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.334 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2003, Axis Communications AB.
  3  */
  4 
  5 #include <linux/sched.h>
  6 #include <linux/sched/task_stack.h>
  7 #include <linux/mm.h>
  8 #include <linux/slab.h>
  9 #include <linux/kernel.h>
 10 #include <linux/signal.h>
 11 #include <linux/errno.h>
 12 #include <linux/wait.h>
 13 #include <linux/ptrace.h>
 14 #include <linux/unistd.h>
 15 #include <linux/stddef.h>
 16 #include <linux/syscalls.h>
 17 #include <linux/vmalloc.h>
 18 
 19 #include <asm/io.h>
 20 #include <asm/processor.h>
 21 #include <asm/ucontext.h>
 22 #include <linux/uaccess.h>
 23 #include <arch/hwregs/cpu_vect.h>
 24 
 25 extern unsigned long cris_signal_return_page;
 26 
 27 /*
 28  * A syscall in CRIS is really a "break 13" instruction, which is 2
 29  * bytes. The registers is manipulated so upon return the instruction
 30  * will be executed again.
 31  *
 32  * This relies on that PC points to the instruction after the break call.
 33  */
 34 #define RESTART_CRIS_SYS(regs) regs->r10 = regs->orig_r10; regs->erp -= 2;
 35 
 36 /* Signal frames. */
 37 struct signal_frame {
 38         struct sigcontext sc;
 39         unsigned long extramask[_NSIG_WORDS - 1];
 40         unsigned char retcode[8];       /* Trampoline code. */
 41 };
 42 
 43 struct rt_signal_frame {
 44         struct siginfo *pinfo;
 45         void *puc;
 46         struct siginfo info;
 47         struct ucontext uc;
 48         unsigned char retcode[8];       /* Trampoline code. */
 49 };
 50 
 51 void do_signal(int restart, struct pt_regs *regs);
 52 void keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
 53                       struct pt_regs *regs);
 54 
 55 static int
 56 restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
 57 {
 58         unsigned int err = 0;
 59         unsigned long old_usp;
 60 
 61         /* Always make any pending restarted system calls return -EINTR */
 62         current->restart_block.fn = do_no_restart_syscall;
 63 
 64         /*
 65          * Restore the registers from &sc->regs. sc is already checked
 66          * for VERIFY_READ since the signal_frame was previously
 67          * checked in sys_sigreturn().
 68          */
 69         if (__copy_from_user(regs, sc, sizeof(struct pt_regs)))
 70                 goto badframe;
 71 
 72         /* Make that the user-mode flag is set. */
 73         regs->ccs |= (1 << (U_CCS_BITNR + CCS_SHIFT));
 74 
 75         /* Don't perform syscall restarting */
 76         regs->exs = -1;
 77 
 78         /* Restore the old USP. */
 79         err |= __get_user(old_usp, &sc->usp);
 80         wrusp(old_usp);
 81 
 82         return err;
 83 
 84 badframe:
 85         return 1;
 86 }
 87 
 88 asmlinkage int sys_sigreturn(void)
 89 {
 90         struct pt_regs *regs = current_pt_regs();
 91         sigset_t set;
 92         struct signal_frame __user *frame;
 93         unsigned long oldspc = regs->spc;
 94         unsigned long oldccs = regs->ccs;
 95 
 96         frame = (struct signal_frame *) rdusp();
 97 
 98         /*
 99          * Since the signal is stacked on a dword boundary, the frame
100          * should be dword aligned here as well. It it's not, then the
101          * user is trying some funny business.
102          */
103         if (((long)frame) & 3)
104                 goto badframe;
105 
106         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
107                 goto badframe;
108 
109         if (__get_user(set.sig[0], &frame->sc.oldmask) ||
110             (_NSIG_WORDS > 1 && __copy_from_user(&set.sig[1],
111                                                  frame->extramask,
112                                                  sizeof(frame->extramask))))
113                 goto badframe;
114 
115         set_current_blocked(&set);
116 
117         if (restore_sigcontext(regs, &frame->sc))
118                 goto badframe;
119 
120         keep_debug_flags(oldccs, oldspc, regs);
121 
122         return regs->r10;
123 
124 badframe:
125         force_sig(SIGSEGV, current);
126         return 0;
127 }
128 
129 asmlinkage int sys_rt_sigreturn(void)
130 {
131         struct pt_regs *regs = current_pt_regs();
132         sigset_t set;
133         struct rt_signal_frame __user *frame;
134         unsigned long oldspc = regs->spc;
135         unsigned long oldccs = regs->ccs;
136 
137         frame = (struct rt_signal_frame *) rdusp();
138 
139         /*
140          * Since the signal is stacked on a dword boundary, the frame
141          * should be dword aligned here as well. It it's not, then the
142          * user is trying some funny business.
143          */
144         if (((long)frame) & 3)
145                 goto badframe;
146 
147         if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
148                 goto badframe;
149 
150         if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
151                 goto badframe;
152 
153         set_current_blocked(&set);
154 
155         if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
156                 goto badframe;
157 
158         if (restore_altstack(&frame->uc.uc_stack))
159                 goto badframe;
160 
161         keep_debug_flags(oldccs, oldspc, regs);
162 
163         return regs->r10;
164 
165 badframe:
166         force_sig(SIGSEGV, current);
167         return 0;
168 }
169 
170 /* Setup a signal frame. */
171 static int
172 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
173                  unsigned long mask)
174 {
175         int err;
176         unsigned long usp;
177 
178         err = 0;
179         usp = rdusp();
180 
181         /*
182          * Copy the registers. They are located first in sc, so it's
183          * possible to use sc directly.
184          */
185         err |= __copy_to_user(sc, regs, sizeof(struct pt_regs));
186 
187         err |= __put_user(mask, &sc->oldmask);
188         err |= __put_user(usp, &sc->usp);
189 
190         return err;
191 }
192 
193 /* Figure out where to put the new signal frame - usually on the stack. */
194 static inline void __user *
195 get_sigframe(struct ksignal *ksig, size_t frame_size)
196 {
197         unsigned long sp = sigsp(rdusp(), ksig);
198 
199         /* Make sure the frame is dword-aligned. */
200         sp &= ~3;
201 
202         return (void __user *)(sp - frame_size);
203 }
204 
205 /* Grab and setup a signal frame.
206  *
207  * Basically a lot of state-info is stacked, and arranged for the
208  * user-mode program to return to the kernel using either a trampiline
209  * which performs the syscall sigreturn(), or a provided user-mode
210  * trampoline.
211   */
212 static int
213 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
214 {
215         int err;
216         unsigned long return_ip;
217         struct signal_frame __user *frame;
218 
219         err = 0;
220         frame = get_sigframe(ksig, sizeof(*frame));
221 
222         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
223                 return -EFAULT;
224 
225         err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
226 
227         if (err)
228                 return -EFAULT;
229 
230         if (_NSIG_WORDS > 1) {
231                 err |= __copy_to_user(frame->extramask, &set->sig[1],
232                                       sizeof(frame->extramask));
233         }
234 
235         if (err)
236                 return -EFAULT;
237 
238         /*
239          * Set up to return from user-space. If provided, use a stub
240          * already located in user-space.
241          */
242         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
243                 return_ip = (unsigned long)ksig->ka.sa.sa_restorer;
244         } else {
245                 /* Trampoline - the desired return ip is in the signal return page. */
246                 return_ip = cris_signal_return_page;
247 
248                 /*
249                  * This is movu.w __NR_sigreturn, r9; break 13;
250                  *
251                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
252                  * reasons and because gdb uses it as a signature to notice
253                  * signal handler stack frames.
254                  */
255                 err |= __put_user(0x9c5f,         (short __user*)(frame->retcode+0));
256                 err |= __put_user(__NR_sigreturn, (short __user*)(frame->retcode+2));
257                 err |= __put_user(0xe93d,         (short __user*)(frame->retcode+4));
258         }
259 
260         if (err)
261                 return -EFAULT;
262 
263         /*
264          * Set up registers for signal handler.
265          *
266          * Where the code enters now.
267          * Where the code enter later.
268          * First argument, signo.
269          */
270         regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
271         regs->srp = return_ip;
272         regs->r10 = ksig->sig;
273 
274         /* Actually move the USP to reflect the stacked frame. */
275         wrusp((unsigned long)frame);
276 
277         return 0;
278 }
279 
280 static int
281 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
282 {
283         int err;
284         unsigned long return_ip;
285         struct rt_signal_frame __user *frame;
286 
287         err = 0;
288         frame = get_sigframe(ksig, sizeof(*frame));
289 
290         if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
291                 return -EFAULT;
292 
293         err |= __put_user(&frame->info, &frame->pinfo);
294         err |= __put_user(&frame->uc, &frame->puc);
295         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
296 
297         if (err)
298                 return -EFAULT;
299 
300         /* Clear all the bits of the ucontext we don't use.  */
301         err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
302         err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, set->sig[0]);
303         err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
304         err |= __save_altstack(&frame->uc.uc_stack, rdusp());
305 
306         if (err)
307                 return -EFAULT;
308 
309         /*
310          * Set up to return from user-space. If provided, use a stub
311          * already located in user-space.
312          */
313         if (ksig->ka.sa.sa_flags & SA_RESTORER) {
314                 return_ip = (unsigned long) ksig->ka.sa.sa_restorer;
315         } else {
316                 /* Trampoline - the desired return ip is in the signal return page. */
317                 return_ip = cris_signal_return_page + 6;
318 
319                 /*
320                  * This is movu.w __NR_rt_sigreturn, r9; break 13;
321                  *
322                  * WE DO NOT USE IT ANY MORE! It's only left here for historical
323                  * reasons and because gdb uses it as a signature to notice
324                  * signal handler stack frames.
325                  */
326                 err |= __put_user(0x9c5f, (short __user*)(frame->retcode+0));
327 
328                 err |= __put_user(__NR_rt_sigreturn,
329                                   (short __user*)(frame->retcode+2));
330 
331                 err |= __put_user(0xe93d, (short __user*)(frame->retcode+4));
332         }
333 
334         if (err)
335                 return -EFAULT;
336 
337         /*
338          * Set up registers for signal handler.
339          *
340          * Where the code enters now.
341          * Where the code enters later.
342          * First argument is signo.
343          * Second argument is (siginfo_t *).
344          * Third argument is unused.
345          */
346         regs->erp = (unsigned long) ksig->ka.sa.sa_handler;
347         regs->srp = return_ip;
348         regs->r10 = ksig->sig;
349         regs->r11 = (unsigned long) &frame->info;
350         regs->r12 = 0;
351 
352         /* Actually move the usp to reflect the stacked frame. */
353         wrusp((unsigned long)frame);
354 
355         return 0;
356 }
357 
358 /* Invoke a signal handler to, well, handle the signal. */
359 static inline void
360 handle_signal(int canrestart, struct ksignal *ksig, struct pt_regs *regs)
361 {
362         sigset_t *oldset = sigmask_to_save();
363         int ret;
364 
365         /* Check if this got called from a system call. */
366         if (canrestart) {
367                 /* If so, check system call restarting. */
368                 switch (regs->r10) {
369                         case -ERESTART_RESTARTBLOCK:
370                         case -ERESTARTNOHAND:
371                                 /*
372                                  * This means that the syscall should
373                                  * only be restarted if there was no
374                                  * handler for the signal, and since
375                                  * this point isn't reached unless
376                                  * there is a handler, there's no need
377                                  * to restart.
378                                  */
379                                 regs->r10 = -EINTR;
380                                 break;
381 
382                         case -ERESTARTSYS:
383                                 /*
384                                  * This means restart the syscall if
385                                  * there is no handler, or the handler
386                                  * was registered with SA_RESTART.
387                                  */
388                                 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
389                                         regs->r10 = -EINTR;
390                                         break;
391                                 }
392 
393                                 /* Fall through. */
394 
395                         case -ERESTARTNOINTR:
396                                 /*
397                                  * This means that the syscall should
398                                  * be called again after the signal
399                                  * handler returns.
400                                  */
401                                 RESTART_CRIS_SYS(regs);
402                                 break;
403                 }
404         }
405 
406         /* Set up the stack frame. */
407         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
408                 ret = setup_rt_frame(ksig, oldset, regs);
409         else
410                 ret = setup_frame(ksig, oldset, regs);
411 
412         signal_setup_done(ret, ksig, 0);
413 }
414 
415 /*
416  * Note that 'init' is a special process: it doesn't get signals it doesn't
417  * want to handle. Thus you cannot kill init even with a SIGKILL even by
418  * mistake.
419  *
420  * Also note that the regs structure given here as an argument, is the latest
421  * pushed pt_regs. It may or may not be the same as the first pushed registers
422  * when the initial usermode->kernelmode transition took place. Therefore
423  * we can use user_mode(regs) to see if we came directly from kernel or user
424  * mode below.
425  */
426 void
427 do_signal(int canrestart, struct pt_regs *regs)
428 {
429         struct ksignal ksig;
430 
431         canrestart = canrestart && ((int)regs->exs >= 0);
432 
433         /*
434          * The common case should go fast, which is why this point is
435          * reached from kernel-mode. If that's the case, just return
436          * without doing anything.
437          */
438         if (!user_mode(regs))
439                 return;
440 
441         if (get_signal(&ksig)) {
442                 /* Whee!  Actually deliver the signal.  */
443                 handle_signal(canrestart, &ksig, regs);
444                 return;
445         }
446 
447         /* Got here from a system call? */
448         if (canrestart) {
449                 /* Restart the system call - no handlers present. */
450                 if (regs->r10 == -ERESTARTNOHAND ||
451                     regs->r10 == -ERESTARTSYS ||
452                     regs->r10 == -ERESTARTNOINTR) {
453                         RESTART_CRIS_SYS(regs);
454                 }
455 
456                 if (regs->r10 == -ERESTART_RESTARTBLOCK){
457                         regs->r9 = __NR_restart_syscall;
458                         regs->erp -= 2;
459                 }
460         }
461 
462         /* if there's no signal to deliver, we just put the saved sigmask
463          * back */
464         restore_saved_sigmask();
465 }
466 
467 asmlinkage void
468 ugdb_trap_user(struct thread_info *ti, int sig)
469 {
470         if (((user_regs(ti)->exs & 0xff00) >> 8) != SINGLE_STEP_INTR_VECT) {
471                 /* Zero single-step PC if the reason we stopped wasn't a single
472                    step exception. This is to avoid relying on it when it isn't
473                    reliable. */
474                 user_regs(ti)->spc = 0;
475         }
476         /* FIXME: Filter out false h/w breakpoint hits (i.e. EDA
477            not within any configured h/w breakpoint range). Synchronize with
478            what already exists for kernel debugging.  */
479         if (((user_regs(ti)->exs & 0xff00) >> 8) == BREAK_8_INTR_VECT) {
480                 /* Break 8: subtract 2 from ERP unless in a delay slot. */
481                 if (!(user_regs(ti)->erp & 0x1))
482                         user_regs(ti)->erp -= 2;
483         }
484         sys_kill(ti->task->pid, sig);
485 }
486 
487 void
488 keep_debug_flags(unsigned long oldccs, unsigned long oldspc,
489                  struct pt_regs *regs)
490 {
491         if (oldccs & (1 << Q_CCS_BITNR)) {
492                 /* Pending single step due to single-stepping the break 13
493                    in the signal trampoline: keep the Q flag. */
494                 regs->ccs |= (1 << Q_CCS_BITNR);
495                 /* S flag should be set - complain if it's not. */
496                 if (!(oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT)))) {
497                         printk("Q flag but no S flag?");
498                 }
499                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
500                 /* Assume the SPC is valid and interesting. */
501                 regs->spc = oldspc;
502 
503         } else if (oldccs & (1 << (S_CCS_BITNR + CCS_SHIFT))) {
504                 /* If a h/w bp was set in the signal handler we need
505                    to keep the S flag. */
506                 regs->ccs |= (1 << (S_CCS_BITNR + CCS_SHIFT));
507                 /* Don't keep the old SPC though; if we got here due to
508                    a single-step, the Q flag should have been set. */
509         } else if (regs->spc) {
510                 /* If we were single-stepping *before* the signal was taken,
511                    we don't want to restore that state now, because GDB will
512                    have forgotten all about it. */
513                 regs->spc = 0;
514                 regs->ccs &= ~(1 << (S_CCS_BITNR + CCS_SHIFT));
515         }
516 }
517 
518 /* Set up the trampolines on the signal return page. */
519 int __init
520 cris_init_signal(void)
521 {
522         u16* data = kmalloc(PAGE_SIZE, GFP_KERNEL);
523 
524         /* This is movu.w __NR_sigreturn, r9; break 13; */
525         data[0] = 0x9c5f;
526         data[1] = __NR_sigreturn;
527         data[2] = 0xe93d;
528         /* This is movu.w __NR_rt_sigreturn, r9; break 13; */
529         data[3] = 0x9c5f;
530         data[4] = __NR_rt_sigreturn;
531         data[5] = 0xe93d;
532 
533         /* Map to userspace with appropriate permissions (no write access...) */
534         cris_signal_return_page = (unsigned long)
535           __ioremap_prot(virt_to_phys(data), PAGE_SIZE, PAGE_SIGNAL_TRAMPOLINE);
536 
537         return 0;
538 }
539 
540 __initcall(cris_init_signal);
541 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp