1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/sched.h>
22#include <linux/mm.h>
23#include <linux/smp.h>
24#include <linux/kernel.h>
25#include <linux/signal.h>
26#include <linux/errno.h>
27#include <linux/wait.h>
28#include <linux/ptrace.h>
29#include <linux/unistd.h>
30#include <linux/stddef.h>
31#include <linux/personality.h>
32#include <linux/percpu.h>
33#include <linux/linkage.h>
34#include <linux/tracehook.h>
35#include <asm/entry.h>
36#include <asm/ucontext.h>
37#include <linux/uaccess.h>
38#include <asm/pgtable.h>
39#include <asm/pgalloc.h>
40#include <linux/syscalls.h>
41#include <asm/cacheflush.h>
42#include <asm/syscalls.h>
43
44
45
46
47struct sigframe {
48 struct sigcontext sc;
49 unsigned long extramask[_NSIG_WORDS-1];
50 unsigned long tramp[2];
51};
52
53struct rt_sigframe {
54 struct siginfo info;
55 struct ucontext uc;
56 unsigned long tramp[2];
57};
58
59static int restore_sigcontext(struct pt_regs *regs,
60 struct sigcontext __user *sc, int *rval_p)
61{
62 unsigned int err = 0;
63
64#define COPY(x) {err |= __get_user(regs->x, &sc->regs.x); }
65 COPY(r0);
66 COPY(r1);
67 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
68 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
69 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
70 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
71 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
72 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
73 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
74 COPY(r30); COPY(r31);
75 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
76#undef COPY
77
78 *rval_p = regs->r3;
79
80 return err;
81}
82
83asmlinkage long sys_rt_sigreturn(struct pt_regs *regs)
84{
85 struct rt_sigframe __user *frame =
86 (struct rt_sigframe __user *)(regs->r1);
87
88 sigset_t set;
89 int rval;
90
91
92 current->restart_block.fn = do_no_restart_syscall;
93
94 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
95 goto badframe;
96
97 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
98 goto badframe;
99
100 set_current_blocked(&set);
101
102 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &rval))
103 goto badframe;
104
105 if (restore_altstack(&frame->uc.uc_stack))
106 goto badframe;
107
108 return rval;
109
110badframe:
111 force_sig(SIGSEGV, current);
112 return 0;
113}
114
115
116
117
118
119static int
120setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
121 unsigned long mask)
122{
123 int err = 0;
124
125#define COPY(x) {err |= __put_user(regs->x, &sc->regs.x); }
126 COPY(r0);
127 COPY(r1);
128 COPY(r2); COPY(r3); COPY(r4); COPY(r5);
129 COPY(r6); COPY(r7); COPY(r8); COPY(r9);
130 COPY(r10); COPY(r11); COPY(r12); COPY(r13);
131 COPY(r14); COPY(r15); COPY(r16); COPY(r17);
132 COPY(r18); COPY(r19); COPY(r20); COPY(r21);
133 COPY(r22); COPY(r23); COPY(r24); COPY(r25);
134 COPY(r26); COPY(r27); COPY(r28); COPY(r29);
135 COPY(r30); COPY(r31);
136 COPY(pc); COPY(ear); COPY(esr); COPY(fsr);
137#undef COPY
138
139 err |= __put_user(mask, &sc->oldmask);
140
141 return err;
142}
143
144
145
146
147static inline void __user *
148get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size)
149{
150
151 unsigned long sp = sigsp(regs->r1, ksig);
152
153 return (void __user *)((sp - frame_size) & -8UL);
154}
155
156static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
157 struct pt_regs *regs)
158{
159 struct rt_sigframe __user *frame;
160 int err = 0, sig = ksig->sig;
161 unsigned long signal;
162 unsigned long address = 0;
163#ifdef CONFIG_MMU
164 pmd_t *pmdp;
165 pte_t *ptep;
166#endif
167
168 frame = get_sigframe(ksig, regs, sizeof(*frame));
169
170 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
171 return -EFAULT;
172
173 signal = current_thread_info()->exec_domain
174 && current_thread_info()->exec_domain->signal_invmap
175 && sig < 32
176 ? current_thread_info()->exec_domain->signal_invmap[sig]
177 : (unsigned long)sig;
178
179 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
180 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
181
182
183 err |= __put_user(0, &frame->uc.uc_flags);
184 err |= __put_user(NULL, &frame->uc.uc_link);
185 err |= __save_altstack(&frame->uc.uc_stack, regs->r1);
186 err |= setup_sigcontext(&frame->uc.uc_mcontext,
187 regs, set->sig[0]);
188 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
189
190
191
192
193
194 err |= __put_user(0x31800000 | __NR_rt_sigreturn ,
195 frame->tramp + 0);
196
197 err |= __put_user(0xb9cc0008, frame->tramp + 1);
198
199
200
201 regs->r15 = ((unsigned long)frame->tramp)-8;
202
203 address = ((unsigned long)frame->tramp);
204#ifdef CONFIG_MMU
205 pmdp = pmd_offset(pud_offset(
206 pgd_offset(current->mm, address),
207 address), address);
208
209 preempt_disable();
210 ptep = pte_offset_map(pmdp, address);
211 if (pte_present(*ptep)) {
212 address = (unsigned long) page_address(pte_page(*ptep));
213
214 address += ((unsigned long)frame->tramp) & ~PAGE_MASK;
215
216 address = __virt_to_phys(address);
217 invalidate_icache_range(address, address + 8);
218 flush_dcache_range(address, address + 8);
219 }
220 pte_unmap(ptep);
221 preempt_enable();
222#else
223 flush_icache_range(address, address + 8);
224 flush_dcache_range(address, address + 8);
225#endif
226 if (err)
227 return -EFAULT;
228
229
230 regs->r1 = (unsigned long) frame;
231
232
233 regs->r5 = signal;
234 regs->r6 = (unsigned long) &frame->info;
235 regs->r7 = (unsigned long) &frame->uc;
236
237 regs->pc = (unsigned long)ksig->ka.sa.sa_handler;
238
239 set_fs(USER_DS);
240
241#ifdef DEBUG_SIG
242 pr_info("SIG deliver (%s:%d): sp=%p pc=%08lx\n",
243 current->comm, current->pid, frame, regs->pc);
244#endif
245
246 return 0;
247}
248
249
250static inline void
251handle_restart(struct pt_regs *regs, struct k_sigaction *ka, int has_handler)
252{
253 switch (regs->r3) {
254 case -ERESTART_RESTARTBLOCK:
255 case -ERESTARTNOHAND:
256 if (!has_handler)
257 goto do_restart;
258 regs->r3 = -EINTR;
259 break;
260 case -ERESTARTSYS:
261 if (has_handler && !(ka->sa.sa_flags & SA_RESTART)) {
262 regs->r3 = -EINTR;
263 break;
264 }
265
266 case -ERESTARTNOINTR:
267do_restart:
268
269 regs->pc -= 4;
270 break;
271 }
272}
273
274
275
276
277
278static void
279handle_signal(struct ksignal *ksig, struct pt_regs *regs)
280{
281 sigset_t *oldset = sigmask_to_save();
282 int ret;
283
284
285 ret = setup_rt_frame(ksig, oldset, regs);
286
287 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
288}
289
290
291
292
293
294
295
296
297
298
299static void do_signal(struct pt_regs *regs, int in_syscall)
300{
301 struct ksignal ksig;
302
303#ifdef DEBUG_SIG
304 pr_info("do signal: %p %d\n", regs, in_syscall);
305 pr_info("do signal2: %lx %lx %ld [%lx]\n", regs->pc, regs->r1,
306 regs->r12, current_thread_info()->flags);
307#endif
308
309 if (get_signal(&ksig)) {
310
311 if (in_syscall)
312 handle_restart(regs, &ksig.ka, 1);
313 handle_signal(&ksig, regs);
314 return;
315 }
316
317 if (in_syscall)
318 handle_restart(regs, NULL, 0);
319
320
321
322
323
324 restore_saved_sigmask();
325}
326
327asmlinkage void do_notify_resume(struct pt_regs *regs, int in_syscall)
328{
329 if (test_thread_flag(TIF_SIGPENDING))
330 do_signal(regs, in_syscall);
331
332 if (test_and_clear_thread_flag(TIF_NOTIFY_RESUME))
333 tracehook_notify_resume(regs);
334}
335