2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994 Christos Zoulas
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
28 * $FreeBSD: src/sys/i386/svr4/svr4_machdep.c,v 1.13.2.1 2002/01/12 11:03:30 bde Exp $
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
39 #include <sys/filedesc.h>
40 #include <sys/signal.h>
41 #include <sys/signalvar.h>
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>
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>
63 extern int svr4_szsigcode;
64 extern char svr4_sigcode[];
65 extern int _udatasel, _ucodesel;
67 static void svr4_getsiginfo __P((union svr4_siginfo *, int, u_long, caddr_t));
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)
76 #if defined(__NetBSD__)
78 svr4_setregs(p, epp, stack)
80 struct exec_package *epp;
83 register struct pcb *pcb = &p->p_addr->u_pcb;
85 pcb->pcb_savefpu.sv_env.en_cw = __SVR4_NPXCW__;
86 setregs(p, epp, stack, 0UL);
88 #endif /* __NetBSD__ */
91 svr4_getcontext(p, uc, mask, oonstack)
93 struct svr4_ucontext *uc;
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;
105 memset(uc, 0, sizeof(struct svr4_ucontext));
107 uc->uc_link = p->p_emuldata;
109 * Set the general purpose registers
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);
121 #if defined(__NetBSD__)
122 __asm("movl %%gs,%w0" : "=r" (r[SVR4_X86_GS]));
123 __asm("movl %%fs,%w0" : "=r" (r[SVR4_X86_FS]));
125 r[SVR4_X86_GS] = rgs();
126 r[SVR4_X86_FS] = tf->tf_fs;
128 r[SVR4_X86_ES] = tf->tf_es;
129 r[SVR4_X86_DS] = tf->tf_ds;
130 r[SVR4_X86_EFL] = tf->tf_eflags;
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;
148 * Set the signal stack
150 #if defined(DONE_MORE_SIGALTSTACK_WORK)
151 bsd_to_svr4_sigaltstack(sf, s);
153 s->ss_sp = (void *)(((u_long) tf->tf_esp) & ~(16384 - 1));
159 * Set the signal mask
161 bsd_to_svr4_sigset(mask, &uc->uc_sigmask);
166 uc->uc_flags = SVR4_UC_SIGMASK|SVR4_UC_CPU|SVR4_UC_STACK;
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
180 svr4_setcontext(p, uc)
182 struct svr4_ucontext *uc;
184 #if defined(DONE_MORE_SIGALTSTACK_WORK)
185 struct sigacts *psp = p->p_sigacts;
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;
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?
202 if ((uc->uc_flags & SVR4_UC_CPU) == 0)
205 DPRINTF(("svr4_setcontext(%d)\n", p->p_pid));
207 tf = p->p_md.md_regs;
210 * Restore register context.
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]);
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.
229 if (((r[SVR4_X86_EFL] ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
230 !USERMODE(r[SVR4_X86_CS], r[SVR4_X86_EFL]))
233 #if defined(__NetBSD__)
234 /* %fs and %gs were restored by the trampoline. */
236 /* %gs was restored by the trampoline. */
237 tf->tf_fs = r[SVR4_X86_FS];
239 tf->tf_es = r[SVR4_X86_ES];
240 tf->tf_ds = r[SVR4_X86_DS];
241 tf->tf_eflags = r[SVR4_X86_EFL];
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];
257 p->p_emuldata = uc->uc_link;
259 * restore signal stack
261 if (uc->uc_flags & SVR4_UC_STACK) {
262 svr4_to_bsd_sigaltstack(s, sf);
266 * restore signal mask
268 if (uc->uc_flags & SVR4_UC_SIGMASK) {
269 #if defined(DEBUG_SVR4)
272 for (i = 0; i < 4; i++)
273 DPRINTF(("\tuc_sigmask[%d] = %lx\n", i,
274 uc->uc_sigmask.bits[i]));
277 svr4_to_bsd_sigset(&uc->uc_sigmask, &mask);
282 return 0; /*EJUSTRETURN;*/
287 svr4_getsiginfo(si, sig, code, addr)
288 union svr4_siginfo *si;
293 si->si_signo = bsd_to_svr4_sig[sig];
299 si->si_code = SVR4_ILL_PRVOPC;
300 si->si_trap = SVR4_T_PRIVINFLT;
304 si->si_code = SVR4_TRAP_BRKPT;
305 si->si_trap = SVR4_T_BPTFLT;
309 si->si_code = SVR4_FPE_INTOVF;
310 si->si_trap = SVR4_T_DIVIDE;
314 si->si_code = SVR4_SEGV_ACCERR;
315 si->si_trap = SVR4_T_PROTFLT;
319 si->si_code = SVR4_TRAP_TRACE;
320 si->si_trap = SVR4_T_TRCTRAP;
324 si->si_code = SVR4_SEGV_ACCERR;
325 si->si_trap = SVR4_T_PAGEFLT;
329 si->si_code = SVR4_BUS_ADRALN;
330 si->si_trap = SVR4_T_ALIGNFLT;
334 si->si_code = SVR4_FPE_FLTDIV;
335 si->si_trap = SVR4_T_DIVIDE;
339 si->si_code = SVR4_FPE_FLTOVF;
340 si->si_trap = SVR4_T_DIVIDE;
344 si->si_code = SVR4_FPE_FLTSUB;
345 si->si_trap = SVR4_T_BOUND;
349 si->si_code = SVR4_FPE_FLTINV;
350 si->si_trap = SVR4_T_DNA;
354 si->si_code = SVR4_FPE_FLTINV;
355 si->si_trap = SVR4_T_FPOPFLT;
359 si->si_code = SVR4_SEGV_MAPERR;
360 si->si_trap = SVR4_T_SEGNPFLT;
364 si->si_code = SVR4_ILL_BADSTK;
365 si->si_trap = SVR4_T_STKFLT;
371 #if defined(DEBUG_SVR4)
372 printf("sig %d code %ld\n", sig, code);
373 /* panic("svr4_getsiginfo");*/
381 * Send an interrupt to process.
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.
390 svr4_sendsig(catcher, sig, mask, code)
396 register struct proc *p = curproc;
397 register struct trapframe *tf;
398 struct svr4_sigframe *fp, frame;
399 struct sigacts *psp = p->p_sigacts;
402 #if defined(DEBUG_SVR4)
403 printf("svr4_sendsig(%d)\n", sig);
406 tf = p->p_md.md_regs;
407 oonstack = p->p_sigstk.ss_flags & SS_ONSTACK;
410 * Allocate space for the signal handler context.
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;
418 fp = (struct svr4_sigframe *)tf->tf_esp - 1;
422 * Build the argument list for the signal handler.
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]
431 svr4_getcontext(p, &frame.sf_uc, mask, oonstack);
432 #if defined(DEBUG_SVR4)
433 printf("obtained ucontext\n");
435 svr4_getsiginfo(&frame.sf_si, sig, code, (caddr_t) tf->tf_eip);
436 #if defined(DEBUG_SVR4)
437 printf("obtained siginfo\n");
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);
448 if (copyout(&frame, fp, sizeof(frame)) != 0) {
450 * Process has trashed its stack; give it an illegal
451 * instruction to halt it in its tracks.
456 #if defined(__NetBSD__)
458 * Build context to run handler in.
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) -
464 tf->tf_cs = GSEL(GUSERLDT_SEL, SEL_UPL);
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);
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;
478 tf->tf_ss = _udatasel;
485 svr4_sys_sysarch(p, v)
487 struct svr4_sys_sysarch_args *v;
489 struct svr4_sys_sysarch_args *uap = v;
491 caddr_t sg = stackgap_init(p->p_emul);
495 case SVR4_SYSARCH_FPHW:
498 case SVR4_SYSARCH_DSCR:
500 #warning "USER_LDT doesn't work - are you sure you want this?"
502 struct i386_set_ldt_args sa, *sap;
503 struct sys_sysarch_args ua;
506 union descriptor bsd;
508 if ((error = copyin(SCARG(uap, a1), &ssd,
509 sizeof(ssd))) != 0) {
510 printf("Cannot copy arg1\n");
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);
518 /* We can only set ldt's for now. */
519 if (!ISLDT(ssd.selector)) {
520 printf("Not an ldt\n");
524 /* Oh, well we don't cleanup either */
525 if (ssd.access1 == 0)
528 bsd.sd.sd_lobase = ssd.base & 0xffffff;
529 bsd.sd.sd_hibase = (ssd.base >> 24) & 0xff;
531 bsd.sd.sd_lolimit = ssd.limit & 0xffff;
532 bsd.sd.sd_hilimit = (ssd.limit >> 16) & 0xf;
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;
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;
542 sa.start = IDXSEL(ssd.selector);
543 sa.desc = stackgap_alloc(&sg, sizeof(union descriptor));
545 sap = stackgap_alloc(&sg,
546 sizeof(struct i386_set_ldt_args));
548 if ((error = copyout(&sa, sap, sizeof(sa))) != 0) {
549 printf("Cannot copyout args\n");
553 SCARG(&ua, op) = I386_SET_LDT;
554 SCARG(&ua, parms) = (char *) sap;
556 if ((error = copyout(&bsd, sa.desc, sizeof(bsd))) != 0) {
557 printf("Cannot copyout desc\n");
561 return sys_sysarch(p, &ua, retval);
566 printf("svr4_sysarch(%d), a1 %p\n", uap->op,