1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Joyent, Inc.
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/vmparam.h>
#include <sys/systm.h>
#include <sys/signal.h>
#include <sys/stack.h>
#include <sys/regset.h>
#include <sys/privregs.h>
#include <sys/frame.h>
#include <sys/proc.h>
#include <sys/brand.h>
#include <sys/psw.h>
#include <sys/ucontext.h>
#include <sys/asm_linkage.h>
#include <sys/errno.h>
#include <sys/archsystm.h>
#include <sys/schedctl.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/sdt.h>
/*
* Save user context.
*/
void
savecontext(ucontext_t *ucp, const k_sigset_t *mask)
{
proc_t *p = ttoproc(curthread);
klwp_t *lwp = ttolwp(curthread);
struct regs *rp = lwptoregs(lwp);
/*
* We unconditionally assign to every field through the end
* of the gregs, but we need to bzero() everything -after- that
* to avoid having any kernel stack garbage escape to userland.
*/
bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext_t) -
offsetof(ucontext_t, uc_mcontext.fpregs));
ucp->uc_flags = UC_ALL;
ucp->uc_link = (struct ucontext *)lwp->lwp_oldcontext;
/*
* Try to copyin() the ustack if one is registered. If the stack
* has zero size, this indicates that stack bounds checking has
* been disabled for this LWP. If stack bounds checking is disabled
* or the copyin() fails, we fall back to the legacy behavior.
*/
if (lwp->lwp_ustack == (uintptr_t)NULL ||
copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
sizeof (ucp->uc_stack)) != 0 ||
ucp->uc_stack.ss_size == 0) {
if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
ucp->uc_stack = lwp->lwp_sigaltstack;
} else {
ucp->uc_stack.ss_sp = p->p_usrstack - p->p_stksize;
ucp->uc_stack.ss_size = p->p_stksize;
ucp->uc_stack.ss_flags = 0;
}
}
/*
* If either the trace flag or REQUEST_STEP is set,
* arrange for single-stepping and turn off the trace flag.
*/
if ((rp->r_ps & PS_T) || (lwp->lwp_pcb.pcb_flags & REQUEST_STEP)) {
/*
* Clear PS_T so that saved user context won't have trace
* flag set.
*/
rp->r_ps &= ~PS_T;
if (!(lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP)) {
lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
/*
* trap() always checks DEBUG_PENDING before
* checking for any pending signal. This at times
* can potentially lead to DEBUG_PENDING not being
* honoured. (for eg: the lwp is stopped by
* stop_on_fault() called from trap(), after being
* awakened it might see a pending signal and call
* savecontext(), however on the way back to userland
* there is no place it can be detected). Hence in
* anticipation of such occassions, set AST flag for
* the thread which will make the thread take an
* excursion through trap() where it will be handled
* appropriately.
*/
aston(curthread);
}
}
getgregs(lwp, ucp->uc_mcontext.gregs);
if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN)
getfpregs(lwp, &ucp->uc_mcontext.fpregs);
else
ucp->uc_flags &= ~UC_FPU;
if (mask != NULL) {
/*
* Save signal mask.
*/
sigktou(mask, &ucp->uc_sigmask);
} else {
ucp->uc_flags &= ~UC_SIGMASK;
bzero(&ucp->uc_sigmask, sizeof (ucp->uc_sigmask));
}
if (PROC_IS_BRANDED(p) && BROP(p)->b_savecontext != NULL) {
/*
* Allow the brand the chance to modify the context we
* saved:
*/
BROP(p)->b_savecontext(ucp);
}
}
/*
* Restore user context.
*/
void
restorecontext(ucontext_t *ucp)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = lwptoproc(lwp);
if (PROC_IS_BRANDED(p) && BROP(p)->b_restorecontext != NULL) {
/*
* Allow the brand the chance to modify the context before
* we restore it:
*/
BROP(p)->b_restorecontext(ucp);
}
DTRACE_PROBE3(oldcontext__set, klwp_t *, lwp,
uintptr_t, lwp->lwp_oldcontext,
uintptr_t, (uintptr_t)ucp->uc_link);
lwp->lwp_oldcontext = (uintptr_t)ucp->uc_link;
if (ucp->uc_flags & UC_STACK) {
if (ucp->uc_stack.ss_flags == SS_ONSTACK)
lwp->lwp_sigaltstack = ucp->uc_stack;
else
lwp->lwp_sigaltstack.ss_flags &= ~SS_ONSTACK;
}
if (ucp->uc_flags & UC_CPU) {
/*
* If the trace flag is set, mark the lwp to take a
* single-step trap on return to user level (below).
* The x86 lcall interface and sysenter has already done this,
* and turned off the flag, but amd64 syscall interface has not.
*/
if (lwptoregs(lwp)->r_ps & PS_T)
lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
setgregs(lwp, ucp->uc_mcontext.gregs);
lwp->lwp_eosys = JUSTRETURN;
t->t_post_sys = 1;
aston(curthread);
}
if (ucp->uc_flags & UC_FPU)
setfpregs(lwp, &ucp->uc_mcontext.fpregs);
if (ucp->uc_flags & UC_SIGMASK) {
/*
* We don't need to acquire p->p_lock here;
* we are manipulating thread-private data.
*/
schedctl_finish_sigblock(t);
sigutok(&ucp->uc_sigmask, &t->t_hold);
if (sigcheck(ttoproc(t), t))
t->t_sig_check = 1;
}
}
int
getsetcontext(int flag, void *arg)
{
ucontext_t uc;
ucontext_t *ucp;
klwp_t *lwp = ttolwp(curthread);
stack_t dummy_stk;
proc_t *p = lwptoproc(lwp);
/*
* In future releases, when the ucontext structure grows,
* getcontext should be modified to only return the fields
* specified in the uc_flags. That way, the structure can grow
* and still be binary compatible will all .o's which will only
* have old fields defined in uc_flags
*/
switch (flag) {
default:
return (set_errno(EINVAL));
case GETCONTEXT:
schedctl_finish_sigblock(curthread);
savecontext(&uc, &curthread->t_hold);
if (uc.uc_flags & UC_SIGMASK)
SIGSET_NATIVE_TO_BRAND(&uc.uc_sigmask);
if (copyout(&uc, arg, sizeof (uc)))
return (set_errno(EFAULT));
return (0);
case SETCONTEXT:
ucp = arg;
if (ucp == NULL)
exit(CLD_EXITED, 0);
/*
* Don't copyin filler or floating state unless we need it.
* The ucontext_t struct and fields are specified in the ABI.
*/
if (copyin(ucp, &uc, sizeof (ucontext_t) -
sizeof (uc.uc_filler) -
sizeof (uc.uc_mcontext.fpregs))) {
return (set_errno(EFAULT));
}
if (uc.uc_flags & UC_SIGMASK)
SIGSET_BRAND_TO_NATIVE(&uc.uc_sigmask);
if ((uc.uc_flags & UC_FPU) &&
copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs,
sizeof (uc.uc_mcontext.fpregs))) {
return (set_errno(EFAULT));
}
/*
* If this is a branded process, copy in the brand-private
* data:
*/
if (PROC_IS_BRANDED(p) && copyin(&ucp->uc_brand_data,
&uc.uc_brand_data, sizeof (uc.uc_brand_data)) != 0) {
return (set_errno(EFAULT));
}
restorecontext(&uc);
if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0))
(void) copyout(&uc.uc_stack, (stack_t *)lwp->lwp_ustack,
sizeof (uc.uc_stack));
return (0);
case GETUSTACK:
if (copyout(&lwp->lwp_ustack, arg, sizeof (caddr_t)))
return (set_errno(EFAULT));
return (0);
case SETUSTACK:
if (copyin(arg, &dummy_stk, sizeof (dummy_stk)))
return (set_errno(EFAULT));
lwp->lwp_ustack = (uintptr_t)arg;
return (0);
}
}
#ifdef _SYSCALL32_IMPL
/*
* Save user context for 32-bit processes.
*/
void
savecontext32(ucontext32_t *ucp, const k_sigset_t *mask)
{
proc_t *p = ttoproc(curthread);
klwp_t *lwp = ttolwp(curthread);
struct regs *rp = lwptoregs(lwp);
bzero(&ucp->uc_mcontext.fpregs, sizeof (ucontext32_t) -
offsetof(ucontext32_t, uc_mcontext.fpregs));
ucp->uc_flags = UC_ALL;
ucp->uc_link = (caddr32_t)lwp->lwp_oldcontext;
if (lwp->lwp_ustack == (uintptr_t)NULL ||
copyin((void *)lwp->lwp_ustack, &ucp->uc_stack,
sizeof (ucp->uc_stack)) != 0 ||
ucp->uc_stack.ss_size == 0) {
if (lwp->lwp_sigaltstack.ss_flags == SS_ONSTACK) {
ucp->uc_stack.ss_sp =
(caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
ucp->uc_stack.ss_size =
(size32_t)lwp->lwp_sigaltstack.ss_size;
ucp->uc_stack.ss_flags = SS_ONSTACK;
} else {
ucp->uc_stack.ss_sp = (caddr32_t)(uintptr_t)
(p->p_usrstack - p->p_stksize);
ucp->uc_stack.ss_size = (size32_t)p->p_stksize;
ucp->uc_stack.ss_flags = 0;
}
}
/*
* If either the trace flag or REQUEST_STEP is set, arrange
* for single-stepping and turn off the trace flag.
*/
if ((rp->r_ps & PS_T) || (lwp->lwp_pcb.pcb_flags & REQUEST_STEP)) {
/*
* Clear PS_T so that saved user context won't have trace
* flag set.
*/
rp->r_ps &= ~PS_T;
if (!(lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP)) {
lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
/*
* See comments in savecontext().
*/
aston(curthread);
}
}
getgregs32(lwp, ucp->uc_mcontext.gregs);
if (lwp->lwp_pcb.pcb_fpu.fpu_flags & FPU_EN)
getfpregs32(lwp, &ucp->uc_mcontext.fpregs);
else
ucp->uc_flags &= ~UC_FPU;
if (mask != NULL) {
/*
* Save signal mask.
*/
sigktou(mask, &ucp->uc_sigmask);
} else {
ucp->uc_flags &= ~UC_SIGMASK;
bzero(&ucp->uc_sigmask, sizeof (ucp->uc_sigmask));
}
if (PROC_IS_BRANDED(p) && BROP(p)->b_savecontext32 != NULL) {
/*
* Allow the brand the chance to modify the context we
* saved:
*/
BROP(p)->b_savecontext32(ucp);
}
}
int
getsetcontext32(int flag, void *arg)
{
ucontext32_t uc;
ucontext_t ucnat;
ucontext32_t *ucp;
klwp_t *lwp = ttolwp(curthread);
caddr32_t ustack32;
stack32_t dummy_stk32;
proc_t *p = lwptoproc(lwp);
switch (flag) {
default:
return (set_errno(EINVAL));
case GETCONTEXT:
schedctl_finish_sigblock(curthread);
savecontext32(&uc, &curthread->t_hold);
if (uc.uc_flags & UC_SIGMASK)
SIGSET_NATIVE_TO_BRAND(&uc.uc_sigmask);
if (copyout(&uc, arg, sizeof (uc)))
return (set_errno(EFAULT));
return (0);
case SETCONTEXT:
ucp = arg;
if (ucp == NULL)
exit(CLD_EXITED, 0);
if (copyin(ucp, &uc, sizeof (uc) -
sizeof (uc.uc_filler) -
sizeof (uc.uc_mcontext.fpregs))) {
return (set_errno(EFAULT));
}
if (uc.uc_flags & UC_SIGMASK)
SIGSET_BRAND_TO_NATIVE(&uc.uc_sigmask);
if ((uc.uc_flags & UC_FPU) &&
copyin(&ucp->uc_mcontext.fpregs, &uc.uc_mcontext.fpregs,
sizeof (uc.uc_mcontext.fpregs))) {
return (set_errno(EFAULT));
}
/*
* If this is a branded process, copy in the brand-private
* data:
*/
if (PROC_IS_BRANDED(p) && copyin(&ucp->uc_brand_data,
&uc.uc_brand_data, sizeof (uc.uc_brand_data)) != 0) {
return (set_errno(EFAULT));
}
ucontext_32ton(&uc, &ucnat);
restorecontext(&ucnat);
if ((uc.uc_flags & UC_STACK) && (lwp->lwp_ustack != 0))
(void) copyout(&uc.uc_stack,
(stack32_t *)lwp->lwp_ustack, sizeof (uc.uc_stack));
return (0);
case GETUSTACK:
ustack32 = (caddr32_t)lwp->lwp_ustack;
if (copyout(&ustack32, arg, sizeof (ustack32)))
return (set_errno(EFAULT));
return (0);
case SETUSTACK:
if (copyin(arg, &dummy_stk32, sizeof (dummy_stk32)))
return (set_errno(EFAULT));
lwp->lwp_ustack = (uintptr_t)arg;
return (0);
}
}
#endif /* _SYSCALL32_IMPL */
|