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

TOMOYO Linux Cross Reference
Linux/arch/unicore32/kernel/signal.c

Version: ~ [ linux-5.19-rc8 ] ~ [ linux-5.18.14 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.57 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.133 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.207 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.253 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.289 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.324 ] ~ [ 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  * linux/arch/unicore32/kernel/signal.c
  3  *
  4  * Code specific to PKUnity SoC and UniCore ISA
  5  *
  6  * Copyright (C) 2001-2010 GUAN Xue-tao
  7  *
  8  * This program is free software; you can redistribute it and/or modify
  9  * it under the terms of the GNU General Public License version 2 as
 10  * published by the Free Software Foundation.
 11  */
 12 #include <linux/errno.h>
 13 #include <linux/signal.h>
 14 #include <linux/personality.h>
 15 #include <linux/uaccess.h>
 16 #include <linux/tracehook.h>
 17 #include <linux/elf.h>
 18 #include <linux/unistd.h>
 19 
 20 #include <asm/cacheflush.h>
 21 #include <asm/ucontext.h>
 22 
 23 /*
 24  * For UniCore syscalls, we encode the syscall number into the instruction.
 25  */
 26 #define SWI_SYS_SIGRETURN       (0xff000000) /* error number for new abi */
 27 #define SWI_SYS_RT_SIGRETURN    (0xff000000 | (__NR_rt_sigreturn))
 28 #define SWI_SYS_RESTART         (0xff000000 | (__NR_restart_syscall))
 29 
 30 #define KERN_SIGRETURN_CODE     (KUSER_VECPAGE_BASE + 0x00000500)
 31 #define KERN_RESTART_CODE       (KERN_SIGRETURN_CODE + sizeof(sigreturn_codes))
 32 
 33 const unsigned long sigreturn_codes[3] = {
 34         SWI_SYS_SIGRETURN, SWI_SYS_RT_SIGRETURN,
 35 };
 36 
 37 const unsigned long syscall_restart_code[2] = {
 38         SWI_SYS_RESTART,        /* swi  __NR_restart_syscall */
 39         0x69efc004,             /* ldr  pc, [sp], #4 */
 40 };
 41 
 42 /*
 43  * Do a signal return; undo the signal stack.  These are aligned to 64-bit.
 44  */
 45 struct sigframe {
 46         struct ucontext uc;
 47         unsigned long retcode[2];
 48 };
 49 
 50 struct rt_sigframe {
 51         struct siginfo info;
 52         struct sigframe sig;
 53 };
 54 
 55 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
 56 {
 57         sigset_t set;
 58         int err;
 59 
 60         err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
 61         if (err == 0)
 62                 set_current_blocked(&set);
 63 
 64         err |= __get_user(regs->UCreg_00, &sf->uc.uc_mcontext.regs.UCreg_00);
 65         err |= __get_user(regs->UCreg_01, &sf->uc.uc_mcontext.regs.UCreg_01);
 66         err |= __get_user(regs->UCreg_02, &sf->uc.uc_mcontext.regs.UCreg_02);
 67         err |= __get_user(regs->UCreg_03, &sf->uc.uc_mcontext.regs.UCreg_03);
 68         err |= __get_user(regs->UCreg_04, &sf->uc.uc_mcontext.regs.UCreg_04);
 69         err |= __get_user(regs->UCreg_05, &sf->uc.uc_mcontext.regs.UCreg_05);
 70         err |= __get_user(regs->UCreg_06, &sf->uc.uc_mcontext.regs.UCreg_06);
 71         err |= __get_user(regs->UCreg_07, &sf->uc.uc_mcontext.regs.UCreg_07);
 72         err |= __get_user(regs->UCreg_08, &sf->uc.uc_mcontext.regs.UCreg_08);
 73         err |= __get_user(regs->UCreg_09, &sf->uc.uc_mcontext.regs.UCreg_09);
 74         err |= __get_user(regs->UCreg_10, &sf->uc.uc_mcontext.regs.UCreg_10);
 75         err |= __get_user(regs->UCreg_11, &sf->uc.uc_mcontext.regs.UCreg_11);
 76         err |= __get_user(regs->UCreg_12, &sf->uc.uc_mcontext.regs.UCreg_12);
 77         err |= __get_user(regs->UCreg_13, &sf->uc.uc_mcontext.regs.UCreg_13);
 78         err |= __get_user(regs->UCreg_14, &sf->uc.uc_mcontext.regs.UCreg_14);
 79         err |= __get_user(regs->UCreg_15, &sf->uc.uc_mcontext.regs.UCreg_15);
 80         err |= __get_user(regs->UCreg_16, &sf->uc.uc_mcontext.regs.UCreg_16);
 81         err |= __get_user(regs->UCreg_17, &sf->uc.uc_mcontext.regs.UCreg_17);
 82         err |= __get_user(regs->UCreg_18, &sf->uc.uc_mcontext.regs.UCreg_18);
 83         err |= __get_user(regs->UCreg_19, &sf->uc.uc_mcontext.regs.UCreg_19);
 84         err |= __get_user(regs->UCreg_20, &sf->uc.uc_mcontext.regs.UCreg_20);
 85         err |= __get_user(regs->UCreg_21, &sf->uc.uc_mcontext.regs.UCreg_21);
 86         err |= __get_user(regs->UCreg_22, &sf->uc.uc_mcontext.regs.UCreg_22);
 87         err |= __get_user(regs->UCreg_23, &sf->uc.uc_mcontext.regs.UCreg_23);
 88         err |= __get_user(regs->UCreg_24, &sf->uc.uc_mcontext.regs.UCreg_24);
 89         err |= __get_user(regs->UCreg_25, &sf->uc.uc_mcontext.regs.UCreg_25);
 90         err |= __get_user(regs->UCreg_26, &sf->uc.uc_mcontext.regs.UCreg_26);
 91         err |= __get_user(regs->UCreg_fp, &sf->uc.uc_mcontext.regs.UCreg_fp);
 92         err |= __get_user(regs->UCreg_ip, &sf->uc.uc_mcontext.regs.UCreg_ip);
 93         err |= __get_user(regs->UCreg_sp, &sf->uc.uc_mcontext.regs.UCreg_sp);
 94         err |= __get_user(regs->UCreg_lr, &sf->uc.uc_mcontext.regs.UCreg_lr);
 95         err |= __get_user(regs->UCreg_pc, &sf->uc.uc_mcontext.regs.UCreg_pc);
 96         err |= __get_user(regs->UCreg_asr, &sf->uc.uc_mcontext.regs.UCreg_asr);
 97 
 98         err |= !valid_user_regs(regs);
 99 
100         return err;
101 }
102 
103 asmlinkage int __sys_rt_sigreturn(struct pt_regs *regs)
104 {
105         struct rt_sigframe __user *frame;
106 
107         /* Always make any pending restarted system calls return -EINTR */
108         current->restart_block.fn = do_no_restart_syscall;
109 
110         /*
111          * Since we stacked the signal on a 64-bit boundary,
112          * then 'sp' should be word aligned here.  If it's
113          * not, then the user is trying to mess with us.
114          */
115         if (regs->UCreg_sp & 7)
116                 goto badframe;
117 
118         frame = (struct rt_sigframe __user *)regs->UCreg_sp;
119 
120         if (!access_ok(frame, sizeof(*frame)))
121                 goto badframe;
122 
123         if (restore_sigframe(regs, &frame->sig))
124                 goto badframe;
125 
126         if (restore_altstack(&frame->sig.uc.uc_stack))
127                 goto badframe;
128 
129         return regs->UCreg_00;
130 
131 badframe:
132         force_sig(SIGSEGV, current);
133         return 0;
134 }
135 
136 static int setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs,
137                 sigset_t *set)
138 {
139         int err = 0;
140 
141         err |= __put_user(regs->UCreg_00, &sf->uc.uc_mcontext.regs.UCreg_00);
142         err |= __put_user(regs->UCreg_01, &sf->uc.uc_mcontext.regs.UCreg_01);
143         err |= __put_user(regs->UCreg_02, &sf->uc.uc_mcontext.regs.UCreg_02);
144         err |= __put_user(regs->UCreg_03, &sf->uc.uc_mcontext.regs.UCreg_03);
145         err |= __put_user(regs->UCreg_04, &sf->uc.uc_mcontext.regs.UCreg_04);
146         err |= __put_user(regs->UCreg_05, &sf->uc.uc_mcontext.regs.UCreg_05);
147         err |= __put_user(regs->UCreg_06, &sf->uc.uc_mcontext.regs.UCreg_06);
148         err |= __put_user(regs->UCreg_07, &sf->uc.uc_mcontext.regs.UCreg_07);
149         err |= __put_user(regs->UCreg_08, &sf->uc.uc_mcontext.regs.UCreg_08);
150         err |= __put_user(regs->UCreg_09, &sf->uc.uc_mcontext.regs.UCreg_09);
151         err |= __put_user(regs->UCreg_10, &sf->uc.uc_mcontext.regs.UCreg_10);
152         err |= __put_user(regs->UCreg_11, &sf->uc.uc_mcontext.regs.UCreg_11);
153         err |= __put_user(regs->UCreg_12, &sf->uc.uc_mcontext.regs.UCreg_12);
154         err |= __put_user(regs->UCreg_13, &sf->uc.uc_mcontext.regs.UCreg_13);
155         err |= __put_user(regs->UCreg_14, &sf->uc.uc_mcontext.regs.UCreg_14);
156         err |= __put_user(regs->UCreg_15, &sf->uc.uc_mcontext.regs.UCreg_15);
157         err |= __put_user(regs->UCreg_16, &sf->uc.uc_mcontext.regs.UCreg_16);
158         err |= __put_user(regs->UCreg_17, &sf->uc.uc_mcontext.regs.UCreg_17);
159         err |= __put_user(regs->UCreg_18, &sf->uc.uc_mcontext.regs.UCreg_18);
160         err |= __put_user(regs->UCreg_19, &sf->uc.uc_mcontext.regs.UCreg_19);
161         err |= __put_user(regs->UCreg_20, &sf->uc.uc_mcontext.regs.UCreg_20);
162         err |= __put_user(regs->UCreg_21, &sf->uc.uc_mcontext.regs.UCreg_21);
163         err |= __put_user(regs->UCreg_22, &sf->uc.uc_mcontext.regs.UCreg_22);
164         err |= __put_user(regs->UCreg_23, &sf->uc.uc_mcontext.regs.UCreg_23);
165         err |= __put_user(regs->UCreg_24, &sf->uc.uc_mcontext.regs.UCreg_24);
166         err |= __put_user(regs->UCreg_25, &sf->uc.uc_mcontext.regs.UCreg_25);
167         err |= __put_user(regs->UCreg_26, &sf->uc.uc_mcontext.regs.UCreg_26);
168         err |= __put_user(regs->UCreg_fp, &sf->uc.uc_mcontext.regs.UCreg_fp);
169         err |= __put_user(regs->UCreg_ip, &sf->uc.uc_mcontext.regs.UCreg_ip);
170         err |= __put_user(regs->UCreg_sp, &sf->uc.uc_mcontext.regs.UCreg_sp);
171         err |= __put_user(regs->UCreg_lr, &sf->uc.uc_mcontext.regs.UCreg_lr);
172         err |= __put_user(regs->UCreg_pc, &sf->uc.uc_mcontext.regs.UCreg_pc);
173         err |= __put_user(regs->UCreg_asr, &sf->uc.uc_mcontext.regs.UCreg_asr);
174 
175         err |= __put_user(current->thread.trap_no,
176                         &sf->uc.uc_mcontext.trap_no);
177         err |= __put_user(current->thread.error_code,
178                         &sf->uc.uc_mcontext.error_code);
179         err |= __put_user(current->thread.address,
180                         &sf->uc.uc_mcontext.fault_address);
181         err |= __put_user(set->sig[0], &sf->uc.uc_mcontext.oldmask);
182 
183         err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
184 
185         return err;
186 }
187 
188 static inline void __user *get_sigframe(struct k_sigaction *ka,
189                 struct pt_regs *regs, int framesize)
190 {
191         unsigned long sp = regs->UCreg_sp;
192         void __user *frame;
193 
194         /*
195          * This is the X/Open sanctioned signal stack switching.
196          */
197         if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
198                 sp = current->sas_ss_sp + current->sas_ss_size;
199 
200         /*
201          * ATPCS B01 mandates 8-byte alignment
202          */
203         frame = (void __user *)((sp - framesize) & ~7);
204 
205         /*
206          * Check that we can actually write to the signal frame.
207          */
208         if (!access_ok(frame, framesize))
209                 frame = NULL;
210 
211         return frame;
212 }
213 
214 static int setup_return(struct pt_regs *regs, struct k_sigaction *ka,
215              unsigned long __user *rc, void __user *frame, int usig)
216 {
217         unsigned long handler = (unsigned long)ka->sa.sa_handler;
218         unsigned long retcode;
219         unsigned long asr = regs->UCreg_asr & ~PSR_f;
220 
221         unsigned int idx = 0;
222 
223         if (ka->sa.sa_flags & SA_SIGINFO)
224                 idx += 1;
225 
226         if (__put_user(sigreturn_codes[idx],   rc) ||
227             __put_user(sigreturn_codes[idx+1], rc+1))
228                 return 1;
229 
230         retcode = KERN_SIGRETURN_CODE + (idx << 2);
231 
232         regs->UCreg_00 = usig;
233         regs->UCreg_sp = (unsigned long)frame;
234         regs->UCreg_lr = retcode;
235         regs->UCreg_pc = handler;
236         regs->UCreg_asr = asr;
237 
238         return 0;
239 }
240 
241 static int setup_frame(struct ksignal *ksig, sigset_t *set,
242                        struct pt_regs *regs)
243 {
244         struct sigframe __user *frame = get_sigframe(&ksig->ka, regs, sizeof(*frame));
245         int err = 0;
246 
247         if (!frame)
248                 return 1;
249 
250         /*
251          * Set uc.uc_flags to a value which sc.trap_no would never have.
252          */
253         err |= __put_user(0x5ac3c35a, &frame->uc.uc_flags);
254 
255         err |= setup_sigframe(frame, regs, set);
256         if (err == 0)
257                 err |= setup_return(regs, &ksig->ka, frame->retcode, frame,
258                                     ksig->sig);
259 
260         return err;
261 }
262 
263 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
264                           struct pt_regs *regs)
265 {
266         struct rt_sigframe __user *frame =
267                         get_sigframe(&ksig->ka, regs, sizeof(*frame));
268         int err = 0;
269 
270         if (!frame)
271                 return 1;
272 
273         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
274 
275         err |= __put_user(0, &frame->sig.uc.uc_flags);
276         err |= __put_user(NULL, &frame->sig.uc.uc_link);
277         err |= __save_altstack(&frame->sig.uc.uc_stack, regs->UCreg_sp);
278         err |= setup_sigframe(&frame->sig, regs, set);
279         if (err == 0)
280                 err |= setup_return(regs, &ksig->ka, frame->sig.retcode, frame,
281                                     ksig->sig);
282 
283         if (err == 0) {
284                 /*
285                  * For realtime signals we must also set the second and third
286                  * arguments for the signal handler.
287                  */
288                 regs->UCreg_01 = (unsigned long)&frame->info;
289                 regs->UCreg_02 = (unsigned long)&frame->sig.uc;
290         }
291 
292         return err;
293 }
294 
295 static inline void setup_syscall_restart(struct pt_regs *regs)
296 {
297         regs->UCreg_00 = regs->UCreg_ORIG_00;
298         regs->UCreg_pc -= 4;
299 }
300 
301 /*
302  * OK, we're invoking a handler
303  */
304 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs,
305                           int syscall)
306 {
307         struct thread_info *thread = current_thread_info();
308         sigset_t *oldset = sigmask_to_save();
309         int usig = ksig->sig;
310         int ret;
311 
312         /*
313          * If we were from a system call, check for system call restarting...
314          */
315         if (syscall) {
316                 switch (regs->UCreg_00) {
317                 case -ERESTART_RESTARTBLOCK:
318                 case -ERESTARTNOHAND:
319                         regs->UCreg_00 = -EINTR;
320                         break;
321                 case -ERESTARTSYS:
322                         if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
323                                 regs->UCreg_00 = -EINTR;
324                                 break;
325                         }
326                         /* fallthrough */
327                 case -ERESTARTNOINTR:
328                         setup_syscall_restart(regs);
329                 }
330         }
331 
332         /*
333          * Set up the stack frame
334          */
335         if (ksig->ka.sa.sa_flags & SA_SIGINFO)
336                 ret = setup_rt_frame(ksig, oldset, regs);
337         else
338                 ret = setup_frame(ksig, oldset, regs);
339 
340         /*
341          * Check that the resulting registers are actually sane.
342          */
343         ret |= !valid_user_regs(regs);
344 
345         signal_setup_done(ret, ksig, 0);
346 }
347 
348 /*
349  * Note that 'init' is a special process: it doesn't get signals it doesn't
350  * want to handle. Thus you cannot kill init even with a SIGKILL even by
351  * mistake.
352  *
353  * Note that we go through the signals twice: once to check the signals that
354  * the kernel can handle, and then we build all the user-level signal handling
355  * stack-frames in one go after that.
356  */
357 static void do_signal(struct pt_regs *regs, int syscall)
358 {
359         struct ksignal ksig;
360 
361         /*
362          * We want the common case to go fast, which
363          * is why we may in certain cases get here from
364          * kernel mode. Just return without doing anything
365          * if so.
366          */
367         if (!user_mode(regs))
368                 return;
369 
370         if (get_signal(&ksig)) {
371                 handle_signal(&ksig, regs, syscall);
372                 return;
373         }
374 
375         /*
376          * No signal to deliver to the process - restart the syscall.
377          */
378         if (syscall) {
379                 if (regs->UCreg_00 == -ERESTART_RESTARTBLOCK) {
380                                 u32 __user *usp;
381 
382                                 regs->UCreg_sp -= 4;
383                                 usp = (u32 __user *)regs->UCreg_sp;
384 
385                                 if (put_user(regs->UCreg_pc, usp) == 0) {
386                                         regs->UCreg_pc = KERN_RESTART_CODE;
387                                 } else {
388                                         regs->UCreg_sp += 4;
389                                         force_sigsegv(0, current);
390                                 }
391                 }
392                 if (regs->UCreg_00 == -ERESTARTNOHAND ||
393                     regs->UCreg_00 == -ERESTARTSYS ||
394                     regs->UCreg_00 == -ERESTARTNOINTR) {
395                         setup_syscall_restart(regs);
396                 }
397         }
398         /* If there's no signal to deliver, we just put the saved
399          * sigmask back.
400          */
401         restore_saved_sigmask();
402 }
403 
404 asmlinkage void do_notify_resume(struct pt_regs *regs,
405                 unsigned int thread_flags, int syscall)
406 {
407         if (thread_flags & _TIF_SIGPENDING)
408                 do_signal(regs, syscall);
409 
410         if (thread_flags & _TIF_NOTIFY_RESUME) {
411                 clear_thread_flag(TIF_NOTIFY_RESUME);
412                 tracehook_notify_resume(regs);
413         }
414 }
415 
416 /*
417  * Copy signal return handlers into the vector page, and
418  * set sigreturn to be a pointer to these.
419  */
420 void __init early_signal_init(void)
421 {
422         memcpy((void *)kuser_vecpage_to_vectors(KERN_SIGRETURN_CODE),
423                         sigreturn_codes, sizeof(sigreturn_codes));
424         memcpy((void *)kuser_vecpage_to_vectors(KERN_RESTART_CODE),
425                         syscall_restart_code, sizeof(syscall_restart_code));
426         /* Need not to flush icache, since early_trap_init will do it last. */
427 }
428 

~ [ 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