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