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 $
29 * $DragonFly: src/sys/emulation/svr4/i386/Attic/svr4_machdep.c,v 1.5 2003/07/26 19:07:50 rob Exp $
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
40 #include <sys/filedesc.h>
41 #include <sys/signal.h>
42 #include <sys/signalvar.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/psl.h>
47 #include <machine/reg.h>
48 #include <machine/specialreg.h>
49 #include <machine/sysarch.h>
50 #include <machine/vm86.h>
51 #include <machine/vmparam.h>
53 #include <svr4/svr4.h>
54 #include <svr4/svr4_types.h>
55 #include <svr4/svr4_signal.h>
56 #include <i386/svr4/svr4_machdep.h>
57 #include <svr4/svr4_ucontext.h>
58 #include <svr4/svr4_proto.h>
59 #include <svr4/svr4_util.h>
64 extern int svr4_szsigcode;
65 extern char svr4_sigcode[];
66 extern int _udatasel, _ucodesel;
68 static void svr4_getsiginfo __P((union svr4_siginfo *, int, u_long, caddr_t));
70 #if !defined(__NetBSD__)
71 /* taken from /sys/arch/i386/include/psl.h on NetBSD-1.3 */
72 # define PSL_MBZ 0xffc08028
73 # define PSL_USERSTATIC (PSL_USER | PSL_MBZ | PSL_IOPL | PSL_NT | PSL_VM | PSL_VIF | PSL_VIP)
74 # define USERMODE(c, f) (ISPL(c) == SEL_UPL)
77 #if defined(__NetBSD__)
79 svr4_setregs(p, epp, stack)
81 struct exec_package *epp;
84 struct pcb *pcb = p->p_thread->td_pcb;
86 pcb->pcb_savefpu.sv_env.en_cw = __SVR4_NPXCW__;
87 setregs(p, epp, stack, 0UL);
89 #endif /* __NetBSD__ */
92 svr4_getcontext(p, uc, mask, oonstack)
94 struct svr4_ucontext *uc;
98 struct trapframe *tf = p->p_md.md_regs;
99 svr4_greg_t *r = uc->uc_mcontext.greg;
100 struct svr4_sigaltstack *s = &uc->uc_stack;
101 #if defined(DONE_MORE_SIGALTSTACK_WORK)
102 struct sigacts *psp = p->p_sigacts;
103 struct sigaltstack *sf = &p->p_sigstk;
106 memset(uc, 0, sizeof(struct svr4_ucontext));
108 uc->uc_link = p->p_emuldata;
110 * Set the general purpose registers
113 if (tf->tf_eflags & PSL_VM) {
114 r[SVR4_X86_GS] = tf->tf_vm86_gs;
115 r[SVR4_X86_FS] = tf->tf_vm86_fs;
116 r[SVR4_X86_ES] = tf->tf_vm86_es;
117 r[SVR4_X86_DS] = tf->tf_vm86_ds;
118 r[SVR4_X86_EFL] = get_vflags(p);
122 #if defined(__NetBSD__)
123 __asm("movl %%gs,%w0" : "=r" (r[SVR4_X86_GS]));
124 __asm("movl %%fs,%w0" : "=r" (r[SVR4_X86_FS]));
126 r[SVR4_X86_GS] = rgs();
127 r[SVR4_X86_FS] = tf->tf_fs;
129 r[SVR4_X86_ES] = tf->tf_es;
130 r[SVR4_X86_DS] = tf->tf_ds;
131 r[SVR4_X86_EFL] = tf->tf_eflags;
133 r[SVR4_X86_EDI] = tf->tf_edi;
134 r[SVR4_X86_ESI] = tf->tf_esi;
135 r[SVR4_X86_EBP] = tf->tf_ebp;
136 r[SVR4_X86_ESP] = tf->tf_esp;
137 r[SVR4_X86_EBX] = tf->tf_ebx;
138 r[SVR4_X86_EDX] = tf->tf_edx;
139 r[SVR4_X86_ECX] = tf->tf_ecx;
140 r[SVR4_X86_EAX] = tf->tf_eax;
141 r[SVR4_X86_TRAPNO] = tf->tf_trapno;
142 r[SVR4_X86_ERR] = tf->tf_err;
143 r[SVR4_X86_EIP] = tf->tf_eip;
144 r[SVR4_X86_CS] = tf->tf_cs;
145 r[SVR4_X86_UESP] = 0;
146 r[SVR4_X86_SS] = tf->tf_ss;
149 * Set the signal stack
151 #if defined(DONE_MORE_SIGALTSTACK_WORK)
152 bsd_to_svr4_sigaltstack(sf, s);
154 s->ss_sp = (void *)(((u_long) tf->tf_esp) & ~(16384 - 1));
160 * Set the signal mask
162 bsd_to_svr4_sigset(mask, &uc->uc_sigmask);
167 uc->uc_flags = SVR4_UC_SIGMASK|SVR4_UC_CPU|SVR4_UC_STACK;
172 * Set to ucontext specified. Reset signal mask and
173 * stack state from context.
174 * Return to previous pc and psl as specified by
175 * context left by sendsig. Check carefully to
176 * make sure that the user has not modified the
177 * psl to gain improper privileges or to cause
181 svr4_setcontext(p, uc)
183 struct svr4_ucontext *uc;
185 #if defined(DONE_MORE_SIGALTSTACK_WORK)
186 struct sigacts *psp = p->p_sigacts;
188 struct trapframe *tf;
189 svr4_greg_t *r = uc->uc_mcontext.greg;
190 struct svr4_sigaltstack *s = &uc->uc_stack;
191 struct sigaltstack *sf = &p->p_sigstk;
196 * Should we check the value of flags to determine what to restore?
197 * What to do with uc_link?
198 * What to do with floating point stuff?
199 * Should we bother with the rest of the registers that we
200 * set to 0 right now?
203 if ((uc->uc_flags & SVR4_UC_CPU) == 0)
206 DPRINTF(("svr4_setcontext(%d)\n", p->p_pid));
208 tf = p->p_md.md_regs;
211 * Restore register context.
214 #warning "VM86 doesn't work yet, please don't try to use it."
215 if (r[SVR4_X86_EFL] & PSL_VM) {
216 tf->tf_vm86_gs = r[SVR4_X86_GS];
217 tf->tf_vm86_fs = r[SVR4_X86_FS];
218 tf->tf_vm86_es = r[SVR4_X86_ES];
219 tf->tf_vm86_ds = r[SVR4_X86_DS];
220 set_vflags(p, r[SVR4_X86_EFL]);
225 * Check for security violations. If we're returning to
226 * protected mode, the CPU will validate the segment registers
227 * automatically and generate a trap on violations. We handle
228 * the trap, rather than doing all of the checking here.
230 if (((r[SVR4_X86_EFL] ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
231 !USERMODE(r[SVR4_X86_CS], r[SVR4_X86_EFL]))
234 #if defined(__NetBSD__)
235 /* %fs and %gs were restored by the trampoline. */
237 /* %gs was restored by the trampoline. */
238 tf->tf_fs = r[SVR4_X86_FS];
240 tf->tf_es = r[SVR4_X86_ES];
241 tf->tf_ds = r[SVR4_X86_DS];
242 tf->tf_eflags = r[SVR4_X86_EFL];
244 tf->tf_edi = r[SVR4_X86_EDI];
245 tf->tf_esi = r[SVR4_X86_ESI];
246 tf->tf_ebp = r[SVR4_X86_EBP];
247 tf->tf_ebx = r[SVR4_X86_EBX];
248 tf->tf_edx = r[SVR4_X86_EDX];
249 tf->tf_ecx = r[SVR4_X86_ECX];
250 tf->tf_eax = r[SVR4_X86_EAX];
251 tf->tf_trapno = r[SVR4_X86_TRAPNO];
252 tf->tf_err = r[SVR4_X86_ERR];
253 tf->tf_eip = r[SVR4_X86_EIP];
254 tf->tf_cs = r[SVR4_X86_CS];
255 tf->tf_ss = r[SVR4_X86_SS];
256 tf->tf_esp = r[SVR4_X86_ESP];
258 p->p_emuldata = uc->uc_link;
260 * restore signal stack
262 if (uc->uc_flags & SVR4_UC_STACK) {
263 svr4_to_bsd_sigaltstack(s, sf);
267 * restore signal mask
269 if (uc->uc_flags & SVR4_UC_SIGMASK) {
270 #if defined(DEBUG_SVR4)
273 for (i = 0; i < 4; i++)
274 DPRINTF(("\tuc_sigmask[%d] = %lx\n", i,
275 uc->uc_sigmask.bits[i]));
278 svr4_to_bsd_sigset(&uc->uc_sigmask, &mask);
283 return 0; /*EJUSTRETURN;*/
288 svr4_getsiginfo(si, sig, code, addr)
289 union svr4_siginfo *si;
294 si->si_signo = bsd_to_svr4_sig[sig];
300 si->si_code = SVR4_ILL_PRVOPC;
301 si->si_trap = SVR4_T_PRIVINFLT;
305 si->si_code = SVR4_TRAP_BRKPT;
306 si->si_trap = SVR4_T_BPTFLT;
310 si->si_code = SVR4_FPE_INTOVF;
311 si->si_trap = SVR4_T_DIVIDE;
315 si->si_code = SVR4_SEGV_ACCERR;
316 si->si_trap = SVR4_T_PROTFLT;
320 si->si_code = SVR4_TRAP_TRACE;
321 si->si_trap = SVR4_T_TRCTRAP;
325 si->si_code = SVR4_SEGV_ACCERR;
326 si->si_trap = SVR4_T_PAGEFLT;
330 si->si_code = SVR4_BUS_ADRALN;
331 si->si_trap = SVR4_T_ALIGNFLT;
335 si->si_code = SVR4_FPE_FLTDIV;
336 si->si_trap = SVR4_T_DIVIDE;
340 si->si_code = SVR4_FPE_FLTOVF;
341 si->si_trap = SVR4_T_DIVIDE;
345 si->si_code = SVR4_FPE_FLTSUB;
346 si->si_trap = SVR4_T_BOUND;
350 si->si_code = SVR4_FPE_FLTINV;
351 si->si_trap = SVR4_T_DNA;
355 si->si_code = SVR4_FPE_FLTINV;
356 si->si_trap = SVR4_T_FPOPFLT;
360 si->si_code = SVR4_SEGV_MAPERR;
361 si->si_trap = SVR4_T_SEGNPFLT;
365 si->si_code = SVR4_ILL_BADSTK;
366 si->si_trap = SVR4_T_STKFLT;
372 #if defined(DEBUG_SVR4)
373 printf("sig %d code %ld\n", sig, code);
374 /* panic("svr4_getsiginfo");*/
382 * Send an interrupt to process.
384 * Stack is set up to allow sigcode stored
385 * in u. to call routine. After the handler is
386 * done svr4 will call setcontext for us
387 * with the user context we just set up, and we
388 * will return to the user pc, psl.
391 svr4_sendsig(catcher, sig, mask, code)
397 struct proc *p = curproc;
398 struct trapframe *tf;
399 struct svr4_sigframe *fp, frame;
400 struct sigacts *psp = p->p_sigacts;
403 #if defined(DEBUG_SVR4)
404 printf("svr4_sendsig(%d)\n", sig);
407 tf = p->p_md.md_regs;
408 oonstack = p->p_sigstk.ss_flags & SS_ONSTACK;
411 * Allocate space for the signal handler context.
413 if ((p->p_flag & P_ALTSTACK) && !oonstack &&
414 SIGISMEMBER(psp->ps_sigonstack, sig)) {
415 fp = (struct svr4_sigframe *)(p->p_sigstk.ss_sp +
416 p->p_sigstk.ss_size - sizeof(struct svr4_sigframe));
417 p->p_sigstk.ss_flags |= SS_ONSTACK;
419 fp = (struct svr4_sigframe *)tf->tf_esp - 1;
423 * Build the argument list for the signal handler.
425 * - we always build the whole argument list, even when we
426 * don't need to [when SA_SIGINFO is not set, we don't need
427 * to pass all sf_si and sf_uc]
428 * - we don't pass the correct signal address [we need to
429 * modify many kernel files to enable that]
432 svr4_getcontext(p, &frame.sf_uc, mask, oonstack);
433 #if defined(DEBUG_SVR4)
434 printf("obtained ucontext\n");
436 svr4_getsiginfo(&frame.sf_si, sig, code, (caddr_t) tf->tf_eip);
437 #if defined(DEBUG_SVR4)
438 printf("obtained siginfo\n");
440 frame.sf_signum = frame.sf_si.si_signo;
441 frame.sf_sip = &fp->sf_si;
442 frame.sf_ucp = &fp->sf_uc;
443 frame.sf_handler = catcher;
444 #if defined(DEBUG_SVR4)
445 printf("sig = %d, sip %p, ucp = %p, handler = %p\n",
446 frame.sf_signum, frame.sf_sip, frame.sf_ucp, frame.sf_handler);
449 if (copyout(&frame, fp, sizeof(frame)) != 0) {
451 * Process has trashed its stack; give it an illegal
452 * instruction to halt it in its tracks.
457 #if defined(__NetBSD__)
459 * Build context to run handler in.
461 tf->tf_es = GSEL(GUSERLDT_SEL, SEL_UPL);
462 tf->tf_ds = GSEL(GUSERLDT_SEL, SEL_UPL);
463 tf->tf_eip = (int)(((char *)PS_STRINGS) -
465 tf->tf_cs = GSEL(GUSERLDT_SEL, SEL_UPL);
467 tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
468 tf->tf_esp = (int)fp;
469 tf->tf_ss = GSEL(GUSERLDT_SEL, SEL_UPL);
471 tf->tf_esp = (int)fp;
472 tf->tf_eip = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
473 tf->tf_eflags &= ~PSL_T;
474 tf->tf_cs = _ucodesel;
475 tf->tf_ds = _udatasel;
476 tf->tf_es = _udatasel;
477 tf->tf_fs = _udatasel;
479 tf->tf_ss = _udatasel;
486 svr4_sys_sysarch(struct svr4_sys_sysarch_args *v)
488 struct svr4_sys_sysarch_args *uap = v;
490 caddr_t sg = stackgap_init(p->p_emul);
494 case SVR4_SYSARCH_FPHW:
497 case SVR4_SYSARCH_DSCR:
499 #warning "USER_LDT doesn't work - are you sure you want this?"
501 struct i386_set_ldt_args sa, *sap;
502 struct sys_sysarch_args ua;
505 union descriptor bsd;
507 if ((error = copyin(SCARG(uap, a1), &ssd,
508 sizeof(ssd))) != 0) {
509 printf("Cannot copy arg1\n");
513 printf("s=%x, b=%x, l=%x, a1=%x a2=%x\n",
514 ssd.selector, ssd.base, ssd.limit,
515 ssd.access1, ssd.access2);
517 /* We can only set ldt's for now. */
518 if (!ISLDT(ssd.selector)) {
519 printf("Not an ldt\n");
523 /* Oh, well we don't cleanup either */
524 if (ssd.access1 == 0)
527 bsd.sd.sd_lobase = ssd.base & 0xffffff;
528 bsd.sd.sd_hibase = (ssd.base >> 24) & 0xff;
530 bsd.sd.sd_lolimit = ssd.limit & 0xffff;
531 bsd.sd.sd_hilimit = (ssd.limit >> 16) & 0xf;
533 bsd.sd.sd_type = ssd.access1 & 0x1f;
534 bsd.sd.sd_dpl = (ssd.access1 >> 5) & 0x3;
535 bsd.sd.sd_p = (ssd.access1 >> 7) & 0x1;
537 bsd.sd.sd_xx = ssd.access2 & 0x3;
538 bsd.sd.sd_def32 = (ssd.access2 >> 2) & 0x1;
539 bsd.sd.sd_gran = (ssd.access2 >> 3)& 0x1;
541 sa.start = IDXSEL(ssd.selector);
542 sa.desc = stackgap_alloc(&sg, sizeof(union descriptor));
544 sap = stackgap_alloc(&sg,
545 sizeof(struct i386_set_ldt_args));
547 if ((error = copyout(&sa, sap, sizeof(sa))) != 0) {
548 printf("Cannot copyout args\n");
552 SCARG(&ua, op) = I386_SET_LDT;
553 SCARG(&ua, parms) = (char *) sap;
555 if ((error = copyout(&bsd, sa.desc, sizeof(bsd))) != 0) {
556 printf("Cannot copyout desc\n");
560 return sys_sysarch(p, &ua, retval);
565 printf("svr4_sysarch(%d), a1 %p\n", uap->op,