1 /*
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD$
29 */
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
35 #include <sys/exec.h>
36 #include <sys/lock.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <sys/filedesc.h>
40 #include <sys/signal.h>
41 #include <sys/signalvar.h>
42
43 #include <machine/cpu.h>
44 #include <machine/cpufunc.h>
45 #include <machine/psl.h>
46 #include <machine/reg.h>
47 #include <machine/specialreg.h>
48 #include <machine/sysarch.h>
49 #include <machine/vm86.h>
50 #include <machine/vmparam.h>
51
52 #include <svr4/svr4.h>
53 #include <svr4/svr4_types.h>
54 #include <svr4/svr4_signal.h>
55 #include <i386/svr4/svr4_machdep.h>
56 #include <svr4/svr4_ucontext.h>
57 #include <svr4/svr4_proto.h>
58 #include <svr4/svr4_util.h>
59
60 #undef sigcode
61 #undef szsigcode
62
63 extern int svr4_szsigcode;
64 extern char svr4_sigcode[];
65 extern int _udatasel, _ucodesel;
66
67 static void svr4_getsiginfo __P((union svr4_siginfo *, int, u_long, caddr_t));
68
69 #if !defined(__NetBSD__)
70 /* taken from /sys/arch/i386/include/psl.h on NetBSD-1.3 */
71 # define PSL_MBZ 0xffc08028
72 # define PSL_USERSTATIC (PSL_USER | PSL_MBZ | PSL_IOPL | PSL_NT | PSL_VM | PSL_VIF | PSL_VIP)
73 # define USERMODE(c, f) (ISPL(c) == SEL_UPL)
74 #endif
75
76 #if defined(__NetBSD__)
77 void
78 svr4_setregs(p, epp, stack)
79 struct proc *p;
80 struct exec_package *epp;
81 u_long stack;
82 {
83 register struct pcb *pcb = &p->p_addr->u_pcb;
84
85 pcb->pcb_savefpu.sv_env.en_cw = __SVR4_NPXCW__;
86 setregs(p, epp, stack, 0UL);
87 }
88 #endif /* __NetBSD__ */
89
90 void
91 svr4_getcontext(p, uc, mask, oonstack)
92 struct proc *p;
93 struct svr4_ucontext *uc;
94 sigset_t *mask;
95 int oonstack;
96 {
97 struct trapframe *tf = p->p_md.md_regs;
98 svr4_greg_t *r = uc->uc_mcontext.greg;
99 struct svr4_sigaltstack *s = &uc->uc_stack;
100 #if defined(DONE_MORE_SIGALTSTACK_WORK)
101 struct sigacts *psp = p->p_sigacts;
102 struct sigaltstack *sf = &p->p_sigstk;
103 #endif
104
105 memset(uc, 0, sizeof(struct svr4_ucontext));
106
107 uc->uc_link = p->p_emuldata;
108 /*
109 * Set the general purpose registers
110 */
111 #ifdef VM86
112 if (tf->tf_eflags & PSL_VM) {
113 r[SVR4_X86_GS] = tf->tf_vm86_gs;
114 r[SVR4_X86_FS] = tf->tf_vm86_fs;
115 r[SVR4_X86_ES] = tf->tf_vm86_es;
116 r[SVR4_X86_DS] = tf->tf_vm86_ds;
117 r[SVR4_X86_EFL] = get_vflags(p);
118 } else
119 #endif
120 {
121 #if defined(__NetBSD__)
122 __asm("movl %%gs,%w0" : "=r" (r[SVR4_X86_GS]));
123 __asm("movl %%fs,%w0" : "=r" (r[SVR4_X86_FS]));
124 #else
125 r[SVR4_X86_GS] = rgs();
126 r[SVR4_X86_FS] = tf->tf_fs;
127 #endif
128 r[SVR4_X86_ES] = tf->tf_es;
129 r[SVR4_X86_DS] = tf->tf_ds;
130 r[SVR4_X86_EFL] = tf->tf_eflags;
131 }
132 r[SVR4_X86_EDI] = tf->tf_edi;
133 r[SVR4_X86_ESI] = tf->tf_esi;
134 r[SVR4_X86_EBP] = tf->tf_ebp;
135 r[SVR4_X86_ESP] = tf->tf_esp;
136 r[SVR4_X86_EBX] = tf->tf_ebx;
137 r[SVR4_X86_EDX] = tf->tf_edx;
138 r[SVR4_X86_ECX] = tf->tf_ecx;
139 r[SVR4_X86_EAX] = tf->tf_eax;
140 r[SVR4_X86_TRAPNO] = tf->tf_trapno;
141 r[SVR4_X86_ERR] = tf->tf_err;
142 r[SVR4_X86_EIP] = tf->tf_eip;
143 r[SVR4_X86_CS] = tf->tf_cs;
144 r[SVR4_X86_UESP] = 0;
145 r[SVR4_X86_SS] = tf->tf_ss;
146
147 /*
148 * Set the signal stack
149 */
150 #if defined(DONE_MORE_SIGALTSTACK_WORK)
151 bsd_to_svr4_sigaltstack(sf, s);
152 #else
153 s->ss_sp = (void *)(((u_long) tf->tf_esp) & ~(16384 - 1));
154 s->ss_size = 16384;
155 s->ss_flags = 0;
156 #endif
157
158 /*
159 * Set the signal mask
160 */
161 bsd_to_svr4_sigset(mask, &uc->uc_sigmask);
162
163 /*
164 * Set the flags
165 */
166 uc->uc_flags = SVR4_UC_SIGMASK|SVR4_UC_CPU|SVR4_UC_STACK;
167 }
168
169
170 /*
171 * Set to ucontext specified. Reset signal mask and
172 * stack state from context.
173 * Return to previous pc and psl as specified by
174 * context left by sendsig. Check carefully to
175 * make sure that the user has not modified the
176 * psl to gain improper privileges or to cause
177 * a machine fault.
178 */
179 int
180 svr4_setcontext(p, uc)
181 struct proc *p;
182 struct svr4_ucontext *uc;
183 {
184 #if defined(DONE_MORE_SIGALTSTACK_WORK)
185 struct sigacts *psp = p->p_sigacts;
186 #endif
187 register struct trapframe *tf;
188 svr4_greg_t *r = uc->uc_mcontext.greg;
189 struct svr4_sigaltstack *s = &uc->uc_stack;
190 struct sigaltstack *sf = &p->p_sigstk;
191 sigset_t mask;
192
193 /*
194 * XXX:
195 * Should we check the value of flags to determine what to restore?
196 * What to do with uc_link?
197 * What to do with floating point stuff?
198 * Should we bother with the rest of the registers that we
199 * set to 0 right now?
200 */
201
202 if ((uc->uc_flags & SVR4_UC_CPU) == 0)
203 return 0;
204
205 DPRINTF(("svr4_setcontext(%d)\n", p->p_pid));
206
207 tf = p->p_md.md_regs;
208
209 /*
210 * Restore register context.
211 */
212 #ifdef VM86
213 #warning "VM86 doesn't work yet, please don't try to use it."
214 if (r[SVR4_X86_EFL] & PSL_VM) {
215 tf->tf_vm86_gs = r[SVR4_X86_GS];
216 tf->tf_vm86_fs = r[SVR4_X86_FS];
217 tf->tf_vm86_es = r[SVR4_X86_ES];
218 tf->tf_vm86_ds = r[SVR4_X86_DS];
219 set_vflags(p, r[SVR4_X86_EFL]);
220 } else
221 #endif
222 {
223 /*
224 * Check for security violations. If we're returning to
225 * protected mode, the CPU will validate the segment registers
226 * automatically and generate a trap on violations. We handle
227 * the trap, rather than doing all of the checking here.
228 */
229 if (((r[SVR4_X86_EFL] ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
230 !USERMODE(r[SVR4_X86_CS], r[SVR4_X86_EFL]))
231 return (EINVAL);
232
233 #if defined(__NetBSD__)
234 /* %fs and %gs were restored by the trampoline. */
235 #else
236 /* %gs was restored by the trampoline. */
237 tf->tf_fs = r[SVR4_X86_FS];
238 #endif
239 tf->tf_es = r[SVR4_X86_ES];
240 tf->tf_ds = r[SVR4_X86_DS];
241 tf->tf_eflags = r[SVR4_X86_EFL];
242 }
243 tf->tf_edi = r[SVR4_X86_EDI];
244 tf->tf_esi = r[SVR4_X86_ESI];
245 tf->tf_ebp = r[SVR4_X86_EBP];
246 tf->tf_ebx = r[SVR4_X86_EBX];
247 tf->tf_edx = r[SVR4_X86_EDX];
248 tf->tf_ecx = r[SVR4_X86_ECX];
249 tf->tf_eax = r[SVR4_X86_EAX];
250 tf->tf_trapno = r[SVR4_X86_TRAPNO];
251 tf->tf_err = r[SVR4_X86_ERR];
252 tf->tf_eip = r[SVR4_X86_EIP];
253 tf->tf_cs = r[SVR4_X86_CS];
254 tf->tf_ss = r[SVR4_X86_SS];
255 tf->tf_esp = r[SVR4_X86_ESP];
256
257 p->p_emuldata = uc->uc_link;
258 /*
259 * restore signal stack
260 */
261 if (uc->uc_flags & SVR4_UC_STACK) {
262 svr4_to_bsd_sigaltstack(s, sf);
263 }
264
265 /*
266 * restore signal mask
267 */
268 if (uc->uc_flags & SVR4_UC_SIGMASK) {
269 #if defined(DEBUG_SVR4)
270 {
271 int i;
272 for (i = 0; i < 4; i++)
273 DPRINTF(("\tuc_sigmask[%d] = %lx\n", i,
274 uc->uc_sigmask.bits[i]));
275 }
276 #endif
277 svr4_to_bsd_sigset(&uc->uc_sigmask, &mask);
278 SIG_CANTMASK(mask);
279 p->p_sigmask = mask;
280 }
281
282 return 0; /*EJUSTRETURN;*/
283 }
284
285
286 static void
287 svr4_getsiginfo(si, sig, code, addr)
288 union svr4_siginfo *si;
289 int sig;
290 u_long code;
291 caddr_t addr;
292 {
293 si->si_signo = bsd_to_svr4_sig[sig];
294 si->si_errno = 0;
295 si->si_addr = addr;
296
297 switch (code) {
298 case T_PRIVINFLT:
299 si->si_code = SVR4_ILL_PRVOPC;
300 si->si_trap = SVR4_T_PRIVINFLT;
301 break;
302
303 case T_BPTFLT:
304 si->si_code = SVR4_TRAP_BRKPT;
305 si->si_trap = SVR4_T_BPTFLT;
306 break;
307
308 case T_ARITHTRAP:
309 si->si_code = SVR4_FPE_INTOVF;
310 si->si_trap = SVR4_T_DIVIDE;
311 break;
312
313 case T_PROTFLT:
314 si->si_code = SVR4_SEGV_ACCERR;
315 si->si_trap = SVR4_T_PROTFLT;
316 break;
317
318 case T_TRCTRAP:
319 si->si_code = SVR4_TRAP_TRACE;
320 si->si_trap = SVR4_T_TRCTRAP;
321 break;
322
323 case T_PAGEFLT:
324 si->si_code = SVR4_SEGV_ACCERR;
325 si->si_trap = SVR4_T_PAGEFLT;
326 break;
327
328 case T_ALIGNFLT:
329 si->si_code = SVR4_BUS_ADRALN;
330 si->si_trap = SVR4_T_ALIGNFLT;
331 break;
332
333 case T_DIVIDE:
334 si->si_code = SVR4_FPE_FLTDIV;
335 si->si_trap = SVR4_T_DIVIDE;
336 break;
337
338 case T_OFLOW:
339 si->si_code = SVR4_FPE_FLTOVF;
340 si->si_trap = SVR4_T_DIVIDE;
341 break;
342
343 case T_BOUND:
344 si->si_code = SVR4_FPE_FLTSUB;
345 si->si_trap = SVR4_T_BOUND;
346 break;
347
348 case T_DNA:
349 si->si_code = SVR4_FPE_FLTINV;
350 si->si_trap = SVR4_T_DNA;
351 break;
352
353 case T_FPOPFLT:
354 si->si_code = SVR4_FPE_FLTINV;
355 si->si_trap = SVR4_T_FPOPFLT;
356 break;
357
358 case T_SEGNPFLT:
359 si->si_code = SVR4_SEGV_MAPERR;
360 si->si_trap = SVR4_T_SEGNPFLT;
361 break;
362
363 case T_STKFLT:
364 si->si_code = SVR4_ILL_BADSTK;
365 si->si_trap = SVR4_T_STKFLT;
366 break;
367
368 default:
369 si->si_code = 0;
370 si->si_trap = 0;
371 #if defined(DEBUG_SVR4)
372 printf("sig %d code %ld\n", sig, code);
373 /* panic("svr4_getsiginfo");*/
374 #endif
375 break;
376 }
377 }
378
379
380 /*
381 * Send an interrupt to process.
382 *
383 * Stack is set up to allow sigcode stored
384 * in u. to call routine. After the handler is
385 * done svr4 will call setcontext for us
386 * with the user context we just set up, and we
387 * will return to the user pc, psl.
388 */
389 void
390 svr4_sendsig(catcher, sig, mask, code)
391 sig_t catcher;
392 int sig;
393 sigset_t *mask;
394 u_long code;
395 {
396 register struct proc *p = curproc;
397 register struct trapframe *tf;
398 struct svr4_sigframe *fp, frame;
399 struct sigacts *psp = p->p_sigacts;
400 int oonstack;
401
402 #if defined(DEBUG_SVR4)
403 printf("svr4_sendsig(%d)\n", sig);
404 #endif
405
406 tf = p->p_md.md_regs;
407 oonstack = p->p_sigstk.ss_flags & SS_ONSTACK;
408
409 /*
410 * Allocate space for the signal handler context.
411 */
412 if ((p->p_flag & P_ALTSTACK) && !oonstack &&
413 SIGISMEMBER(psp->ps_sigonstack, sig)) {
414 fp = (struct svr4_sigframe *)(p->p_sigstk.ss_sp +
415 p->p_sigstk.ss_size - sizeof(struct svr4_sigframe));
416 p->p_sigstk.ss_flags |= SS_ONSTACK;
417 } else {
418 fp = (struct svr4_sigframe *)tf->tf_esp - 1;
419 }
420
421 /*
422 * Build the argument list for the signal handler.
423 * Notes:
424 * - we always build the whole argument list, even when we
425 * don't need to [when SA_SIGINFO is not set, we don't need
426 * to pass all sf_si and sf_uc]
427 * - we don't pass the correct signal address [we need to
428 * modify many kernel files to enable that]
429 */
430
431 svr4_getcontext(p, &frame.sf_uc, mask, oonstack);
432 #if defined(DEBUG_SVR4)
433 printf("obtained ucontext\n");
434 #endif
435 svr4_getsiginfo(&frame.sf_si, sig, code, (caddr_t) tf->tf_eip);
436 #if defined(DEBUG_SVR4)
437 printf("obtained siginfo\n");
438 #endif
439 frame.sf_signum = frame.sf_si.si_signo;
440 frame.sf_sip = &fp->sf_si;
441 frame.sf_ucp = &fp->sf_uc;
442 frame.sf_handler = catcher;
443 #if defined(DEBUG_SVR4)
444 printf("sig = %d, sip %p, ucp = %p, handler = %p\n",
445 frame.sf_signum, frame.sf_sip, frame.sf_ucp, frame.sf_handler);
446 #endif
447
448 if (copyout(&frame, fp, sizeof(frame)) != 0) {
449 /*
450 * Process has trashed its stack; give it an illegal
451 * instruction to halt it in its tracks.
452 */
453 sigexit(p, SIGILL);
454 /* NOTREACHED */
455 }
456 #if defined(__NetBSD__)
457 /*
458 * Build context to run handler in.
459 */
460 tf->tf_es = GSEL(GUSERLDT_SEL, SEL_UPL);
461 tf->tf_ds = GSEL(GUSERLDT_SEL, SEL_UPL);
462 tf->tf_eip = (int)(((char *)PS_STRINGS) -
463 svr4_szsigcode);
464 tf->tf_cs = GSEL(GUSERLDT_SEL, SEL_UPL);
465
466 tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
467 tf->tf_esp = (int)fp;
468 tf->tf_ss = GSEL(GUSERLDT_SEL, SEL_UPL);
469 #else
470 tf->tf_esp = (int)fp;
471 tf->tf_eip = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
472 tf->tf_eflags &= ~PSL_T;
473 tf->tf_cs = _ucodesel;
474 tf->tf_ds = _udatasel;
475 tf->tf_es = _udatasel;
476 tf->tf_fs = _udatasel;
477 load_gs(_udatasel);
478 tf->tf_ss = _udatasel;
479 #endif
480 }
481
482
483
484 int
485 svr4_sys_sysarch(p, v)
486 struct proc *p;
487 struct svr4_sys_sysarch_args *v;
488 {
489 struct svr4_sys_sysarch_args *uap = v;
490 #ifdef USER_LDT
491 caddr_t sg = stackgap_init(p->p_emul);
492 int error;
493 #endif
494 switch (uap->op) {
495 case SVR4_SYSARCH_FPHW:
496 return 0;
497
498 case SVR4_SYSARCH_DSCR:
499 #ifdef USER_LDT
500 #warning "USER_LDT doesn't work - are you sure you want this?"
501 {
502 struct i386_set_ldt_args sa, *sap;
503 struct sys_sysarch_args ua;
504
505 struct svr4_ssd ssd;
506 union descriptor bsd;
507
508 if ((error = copyin(SCARG(uap, a1), &ssd,
509 sizeof(ssd))) != 0) {
510 printf("Cannot copy arg1\n");
511 return error;
512 }
513
514 printf("s=%x, b=%x, l=%x, a1=%x a2=%x\n",
515 ssd.selector, ssd.base, ssd.limit,
516 ssd.access1, ssd.access2);
517
518 /* We can only set ldt's for now. */
519 if (!ISLDT(ssd.selector)) {
520 printf("Not an ldt\n");
521 return EPERM;
522 }
523
524 /* Oh, well we don't cleanup either */
525 if (ssd.access1 == 0)
526 return 0;
527
528 bsd.sd.sd_lobase = ssd.base & 0xffffff;
529 bsd.sd.sd_hibase = (ssd.base >> 24) & 0xff;
530
531 bsd.sd.sd_lolimit = ssd.limit & 0xffff;
532 bsd.sd.sd_hilimit = (ssd.limit >> 16) & 0xf;
533
534 bsd.sd.sd_type = ssd.access1 & 0x1f;
535 bsd.sd.sd_dpl = (ssd.access1 >> 5) & 0x3;
536 bsd.sd.sd_p = (ssd.access1 >> 7) & 0x1;
537
538 bsd.sd.sd_xx = ssd.access2 & 0x3;
539 bsd.sd.sd_def32 = (ssd.access2 >> 2) & 0x1;
540 bsd.sd.sd_gran = (ssd.access2 >> 3)& 0x1;
541
542 sa.start = IDXSEL(ssd.selector);
543 sa.desc = stackgap_alloc(&sg, sizeof(union descriptor));
544 sa.num = 1;
545 sap = stackgap_alloc(&sg,
546 sizeof(struct i386_set_ldt_args));
547
548 if ((error = copyout(&sa, sap, sizeof(sa))) != 0) {
549 printf("Cannot copyout args\n");
550 return error;
551 }
552
553 SCARG(&ua, op) = I386_SET_LDT;
554 SCARG(&ua, parms) = (char *) sap;
555
556 if ((error = copyout(&bsd, sa.desc, sizeof(bsd))) != 0) {
557 printf("Cannot copyout desc\n");
558 return error;
559 }
560
561 return sys_sysarch(p, &ua, retval);
562 }
563 #endif
564
565 default:
566 printf("svr4_sysarch(%d), a1 %p\n", uap->op,
567 uap->a1);
568 return 0;
569 }
570 }
Cache object: 582743aee1ab9608be022af8c6ba0e3a
|