Register keyword removal
[dragonfly.git] / sys / emulation / svr4 / i386 / svr4_machdep.c
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: 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 $
30  */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <sys/exec.h>
37 #include <sys/lock.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 #include <sys/filedesc.h>
41 #include <sys/signal.h>
42 #include <sys/signalvar.h>
43
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>
52
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>
60
61 #undef sigcode
62 #undef szsigcode
63
64 extern int svr4_szsigcode;
65 extern char svr4_sigcode[];
66 extern int _udatasel, _ucodesel;
67
68 static void svr4_getsiginfo __P((union svr4_siginfo *, int, u_long, caddr_t));
69
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)
75 #endif
76
77 #if defined(__NetBSD__)
78 void
79 svr4_setregs(p, epp, stack)
80         struct proc *p;
81         struct exec_package *epp;
82         u_long stack;
83 {
84         struct pcb *pcb = p->p_thread->td_pcb;
85
86         pcb->pcb_savefpu.sv_env.en_cw = __SVR4_NPXCW__;
87         setregs(p, epp, stack, 0UL);
88 }
89 #endif /* __NetBSD__ */
90
91 void
92 svr4_getcontext(p, uc, mask, oonstack)
93         struct proc *p;
94         struct svr4_ucontext *uc;
95         sigset_t *mask;
96         int oonstack;
97 {
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;
104 #endif
105
106         memset(uc, 0, sizeof(struct svr4_ucontext));
107
108         uc->uc_link = p->p_emuldata;
109         /*
110          * Set the general purpose registers
111          */
112 #ifdef VM86
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);
119         } else
120 #endif
121         {
122 #if defined(__NetBSD__)
123                 __asm("movl %%gs,%w0" : "=r" (r[SVR4_X86_GS]));
124                 __asm("movl %%fs,%w0" : "=r" (r[SVR4_X86_FS]));
125 #else
126                 r[SVR4_X86_GS] = rgs();
127                 r[SVR4_X86_FS] = tf->tf_fs;
128 #endif
129                 r[SVR4_X86_ES] = tf->tf_es;
130                 r[SVR4_X86_DS] = tf->tf_ds;
131                 r[SVR4_X86_EFL] = tf->tf_eflags;
132         }
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;
147
148         /*
149          * Set the signal stack
150          */
151 #if defined(DONE_MORE_SIGALTSTACK_WORK)
152         bsd_to_svr4_sigaltstack(sf, s);
153 #else
154         s->ss_sp = (void *)(((u_long) tf->tf_esp) & ~(16384 - 1));
155         s->ss_size = 16384;
156         s->ss_flags = 0;
157 #endif
158
159         /*
160          * Set the signal mask
161          */
162         bsd_to_svr4_sigset(mask, &uc->uc_sigmask);
163
164         /*
165          * Set the flags
166          */
167         uc->uc_flags = SVR4_UC_SIGMASK|SVR4_UC_CPU|SVR4_UC_STACK;
168 }
169
170
171 /*
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
178  * a machine fault.
179  */
180 int
181 svr4_setcontext(p, uc)
182         struct proc *p;
183         struct svr4_ucontext *uc;
184 {
185 #if defined(DONE_MORE_SIGALTSTACK_WORK)
186         struct sigacts *psp = p->p_sigacts;
187 #endif
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;
192         sigset_t mask;
193
194         /*
195          * XXX:
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?
201          */
202
203         if ((uc->uc_flags & SVR4_UC_CPU) == 0)
204                 return 0;
205
206         DPRINTF(("svr4_setcontext(%d)\n", p->p_pid));
207
208         tf = p->p_md.md_regs;
209
210         /*
211          * Restore register context.
212          */
213 #ifdef VM86
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]);
221         } else
222 #endif
223         {
224                 /*
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.
229                  */
230                 if (((r[SVR4_X86_EFL] ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
231                     !USERMODE(r[SVR4_X86_CS], r[SVR4_X86_EFL]))
232                         return (EINVAL);
233
234 #if defined(__NetBSD__)
235                 /* %fs and %gs were restored by the trampoline. */
236 #else
237                 /* %gs was restored by the trampoline. */
238                 tf->tf_fs = r[SVR4_X86_FS];
239 #endif
240                 tf->tf_es = r[SVR4_X86_ES];
241                 tf->tf_ds = r[SVR4_X86_DS];
242                 tf->tf_eflags = r[SVR4_X86_EFL];
243         }
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];
257
258         p->p_emuldata = uc->uc_link;
259         /*
260          * restore signal stack
261          */
262         if (uc->uc_flags & SVR4_UC_STACK) {
263                 svr4_to_bsd_sigaltstack(s, sf);
264         }
265
266         /*
267          * restore signal mask
268          */
269         if (uc->uc_flags & SVR4_UC_SIGMASK) {
270 #if defined(DEBUG_SVR4)
271                 {
272                         int i;
273                         for (i = 0; i < 4; i++)
274                                 DPRINTF(("\tuc_sigmask[%d] = %lx\n", i,
275                                                 uc->uc_sigmask.bits[i]));
276                 }
277 #endif
278                 svr4_to_bsd_sigset(&uc->uc_sigmask, &mask);
279                 SIG_CANTMASK(mask);
280                 p->p_sigmask = mask;
281         }
282
283         return 0; /*EJUSTRETURN;*/
284 }
285
286
287 static void
288 svr4_getsiginfo(si, sig, code, addr)
289         union svr4_siginfo      *si;
290         int                      sig;
291         u_long                   code;
292         caddr_t                  addr;
293 {
294         si->si_signo = bsd_to_svr4_sig[sig];
295         si->si_errno = 0;
296         si->si_addr  = addr;
297
298         switch (code) {
299         case T_PRIVINFLT:
300                 si->si_code = SVR4_ILL_PRVOPC;
301                 si->si_trap = SVR4_T_PRIVINFLT;
302                 break;
303
304         case T_BPTFLT:
305                 si->si_code = SVR4_TRAP_BRKPT;
306                 si->si_trap = SVR4_T_BPTFLT;
307                 break;
308
309         case T_ARITHTRAP:
310                 si->si_code = SVR4_FPE_INTOVF;
311                 si->si_trap = SVR4_T_DIVIDE;
312                 break;
313
314         case T_PROTFLT:
315                 si->si_code = SVR4_SEGV_ACCERR;
316                 si->si_trap = SVR4_T_PROTFLT;
317                 break;
318
319         case T_TRCTRAP:
320                 si->si_code = SVR4_TRAP_TRACE;
321                 si->si_trap = SVR4_T_TRCTRAP;
322                 break;
323
324         case T_PAGEFLT:
325                 si->si_code = SVR4_SEGV_ACCERR;
326                 si->si_trap = SVR4_T_PAGEFLT;
327                 break;
328
329         case T_ALIGNFLT:
330                 si->si_code = SVR4_BUS_ADRALN;
331                 si->si_trap = SVR4_T_ALIGNFLT;
332                 break;
333
334         case T_DIVIDE:
335                 si->si_code = SVR4_FPE_FLTDIV;
336                 si->si_trap = SVR4_T_DIVIDE;
337                 break;
338
339         case T_OFLOW:
340                 si->si_code = SVR4_FPE_FLTOVF;
341                 si->si_trap = SVR4_T_DIVIDE;
342                 break;
343
344         case T_BOUND:
345                 si->si_code = SVR4_FPE_FLTSUB;
346                 si->si_trap = SVR4_T_BOUND;
347                 break;
348
349         case T_DNA:
350                 si->si_code = SVR4_FPE_FLTINV;
351                 si->si_trap = SVR4_T_DNA;
352                 break;
353
354         case T_FPOPFLT:
355                 si->si_code = SVR4_FPE_FLTINV;
356                 si->si_trap = SVR4_T_FPOPFLT;
357                 break;
358
359         case T_SEGNPFLT:
360                 si->si_code = SVR4_SEGV_MAPERR;
361                 si->si_trap = SVR4_T_SEGNPFLT;
362                 break;
363
364         case T_STKFLT:
365                 si->si_code = SVR4_ILL_BADSTK;
366                 si->si_trap = SVR4_T_STKFLT;
367                 break;
368
369         default:
370                 si->si_code = 0;
371                 si->si_trap = 0;
372 #if defined(DEBUG_SVR4)
373                 printf("sig %d code %ld\n", sig, code);
374 /*              panic("svr4_getsiginfo");*/
375 #endif
376                 break;
377         }
378 }
379
380
381 /*
382  * Send an interrupt to process.
383  *
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.
389  */
390 void
391 svr4_sendsig(catcher, sig, mask, code)
392         sig_t catcher;
393         int sig;
394         sigset_t *mask;
395         u_long code;
396 {
397         struct proc *p = curproc;
398         struct trapframe *tf;
399         struct svr4_sigframe *fp, frame;
400         struct sigacts *psp = p->p_sigacts;
401         int oonstack;
402
403 #if defined(DEBUG_SVR4)
404         printf("svr4_sendsig(%d)\n", sig);
405 #endif
406
407         tf = p->p_md.md_regs;
408         oonstack = p->p_sigstk.ss_flags & SS_ONSTACK;
409
410         /*
411          * Allocate space for the signal handler context.
412          */
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;
418         } else {
419                 fp = (struct svr4_sigframe *)tf->tf_esp - 1;
420         }
421
422         /* 
423          * Build the argument list for the signal handler.
424          * Notes:
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]
430          */
431
432         svr4_getcontext(p, &frame.sf_uc, mask, oonstack);
433 #if defined(DEBUG_SVR4)
434         printf("obtained ucontext\n");
435 #endif
436         svr4_getsiginfo(&frame.sf_si, sig, code, (caddr_t) tf->tf_eip);
437 #if defined(DEBUG_SVR4)
438         printf("obtained siginfo\n");
439 #endif
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);
447 #endif
448
449         if (copyout(&frame, fp, sizeof(frame)) != 0) {
450                 /*
451                  * Process has trashed its stack; give it an illegal
452                  * instruction to halt it in its tracks.
453                  */
454                 sigexit(p, SIGILL);
455                 /* NOTREACHED */
456         }
457 #if defined(__NetBSD__)
458         /*
459          * Build context to run handler in.
460          */
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) -
464              svr4_szsigcode);
465         tf->tf_cs = GSEL(GUSERLDT_SEL, SEL_UPL);
466
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);
470 #else
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;
478         load_gs(_udatasel);
479         tf->tf_ss = _udatasel;
480 #endif
481 }
482
483
484
485 int
486 svr4_sys_sysarch(struct svr4_sys_sysarch_args *v)
487 {
488         struct svr4_sys_sysarch_args *uap = v;
489 #ifdef USER_LDT
490         caddr_t sg = stackgap_init(p->p_emul);
491         int error;
492 #endif
493         switch (uap->op) {
494         case SVR4_SYSARCH_FPHW:
495                 return 0;
496
497         case SVR4_SYSARCH_DSCR:
498 #ifdef USER_LDT
499 #warning "USER_LDT doesn't work - are you sure you want this?"
500                 {
501                         struct i386_set_ldt_args sa, *sap;
502                         struct sys_sysarch_args ua;
503
504                         struct svr4_ssd ssd;
505                         union descriptor bsd;
506
507                         if ((error = copyin(SCARG(uap, a1), &ssd,
508                                             sizeof(ssd))) != 0) {
509                                 printf("Cannot copy arg1\n");
510                                 return error;
511                         }
512
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);
516
517                         /* We can only set ldt's for now. */
518                         if (!ISLDT(ssd.selector)) {
519                                 printf("Not an ldt\n");
520                                 return EPERM;
521                         }
522
523                         /* Oh, well we don't cleanup either */
524                         if (ssd.access1 == 0)
525                                 return 0;
526
527                         bsd.sd.sd_lobase = ssd.base & 0xffffff;
528                         bsd.sd.sd_hibase = (ssd.base >> 24) & 0xff;
529
530                         bsd.sd.sd_lolimit = ssd.limit & 0xffff;
531                         bsd.sd.sd_hilimit = (ssd.limit >> 16) & 0xf;
532
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;
536
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;
540
541                         sa.start = IDXSEL(ssd.selector);
542                         sa.desc = stackgap_alloc(&sg, sizeof(union descriptor));
543                         sa.num = 1;
544                         sap = stackgap_alloc(&sg,
545                                              sizeof(struct i386_set_ldt_args));
546
547                         if ((error = copyout(&sa, sap, sizeof(sa))) != 0) {
548                                 printf("Cannot copyout args\n");
549                                 return error;
550                         }
551
552                         SCARG(&ua, op) = I386_SET_LDT;
553                         SCARG(&ua, parms) = (char *) sap;
554
555                         if ((error = copyout(&bsd, sa.desc, sizeof(bsd))) != 0) {
556                                 printf("Cannot copyout desc\n");
557                                 return error;
558                         }
559
560                         return sys_sysarch(p, &ua, retval);
561                 }
562 #endif
563
564         default:
565                 printf("svr4_sysarch(%d), a1 %p\n", uap->op,
566                        uap->a1);
567                 return 0;
568         }
569 }