]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/pc98/i386/machdep.c
This commit was generated by cvs2svn to compensate for changes in r115990,
[FreeBSD/FreeBSD.git] / sys / pc98 / i386 / machdep.c
1 /*-
2  * Copyright (c) 1992 Terrence R. Lambert.
3  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      from: @(#)machdep.c     7.4 (Berkeley) 6/3/91
38  * $FreeBSD$
39  */
40
41 #include "opt_atalk.h"
42 #include "opt_compat.h"
43 #include "opt_cpu.h"
44 #include "opt_ddb.h"
45 #include "opt_inet.h"
46 #include "opt_ipx.h"
47 #include "opt_isa.h"
48 #include "opt_maxmem.h"
49 #include "opt_msgbuf.h"
50 #include "opt_npx.h"
51 #include "opt_perfmon.h"
52 #include "opt_swtch.h"
53 #include "opt_kstack_pages.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/signalvar.h>
59 #include <sys/imgact.h>
60 #include <sys/kernel.h>
61 #include <sys/ktr.h>
62 #include <sys/linker.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
66 #include <sys/pcpu.h>
67 #include <sys/proc.h>
68 #include <sys/bio.h>
69 #include <sys/buf.h>
70 #include <sys/reboot.h>
71 #include <sys/callout.h>
72 #include <sys/msgbuf.h>
73 #include <sys/sched.h>
74 #include <sys/sysent.h>
75 #include <sys/sysctl.h>
76 #include <sys/ucontext.h>
77 #include <sys/vmmeter.h>
78 #include <sys/bus.h>
79 #include <sys/eventhandler.h>
80
81 #include <vm/vm.h>
82 #include <vm/vm_param.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_map.h>
87 #include <vm/vm_pager.h>
88 #include <vm/vm_extern.h>
89
90 #include <sys/user.h>
91 #include <sys/exec.h>
92 #include <sys/cons.h>
93
94 #include <ddb/ddb.h>
95
96 #include <net/netisr.h>
97
98 #include <machine/cpu.h>
99 #include <machine/cputypes.h>
100 #include <machine/reg.h>
101 #include <machine/clock.h>
102 #include <machine/specialreg.h>
103 #include <machine/bootinfo.h>
104 #include <machine/md_var.h>
105 #include <machine/pc/bios.h>
106 #include <machine/pcb_ext.h>            /* pcb.h included via sys/user.h */
107 #include <machine/proc.h>
108 #ifdef PERFMON
109 #include <machine/perfmon.h>
110 #endif
111 #ifdef SMP
112 #include <machine/privatespace.h>
113 #include <machine/smp.h>
114 #endif
115
116 #include <i386/isa/icu.h>
117 #include <i386/isa/intr_machdep.h>
118 #ifdef PC98
119 #include <pc98/pc98/pc98_machdep.h>
120 #include <pc98/pc98/pc98.h>
121 #else
122 #include <isa/rtc.h>
123 #endif
124 #include <machine/vm86.h>
125 #include <sys/ptrace.h>
126 #include <machine/sigframe.h>
127
128 extern void init386(int first);
129 extern void dblfault_handler(void);
130
131 extern void printcpuinfo(void); /* XXX header file */
132 extern void finishidentcpu(void);
133 extern void panicifcpuunsupported(void);
134 extern void initializecpu(void);
135
136 #define CS_SECURE(cs)           (ISPL(cs) == SEL_UPL)
137 #define EFL_SECURE(ef, oef)     ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
138
139 #if !defined(CPU_ENABLE_SSE) && defined(I686_CPU)
140 #define CPU_ENABLE_SSE
141 #endif
142 #if defined(CPU_DISABLE_SSE)
143 #undef CPU_ENABLE_SSE
144 #endif
145
146 static void cpu_startup(void *);
147 static void fpstate_drop(struct thread *td);
148 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
149 static int  set_fpcontext(struct thread *td, const mcontext_t *mcp);
150 #ifdef CPU_ENABLE_SSE
151 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
152 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
153 #endif /* CPU_ENABLE_SSE */
154 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
155
156 #ifdef PC98
157 int     need_pre_dma_flush;     /* If 1, use wbinvd befor DMA transfer. */
158 int     need_post_dma_flush;    /* If 1, use invd after DMA transfer. */
159 #endif
160
161 int     _udatasel, _ucodesel;
162 u_int   atdevbase;
163
164 #if defined(SWTCH_OPTIM_STATS)
165 int stupid_switch;
166 SYSCTL_INT(_debug, OID_AUTO, stupid_switch,
167         CTLFLAG_RW, &stupid_switch, 0, "");
168 int swtch_optim_stats;
169 SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
170         CTLFLAG_RW, &swtch_optim_stats, 0, "");
171 int tlb_flush_count;
172 SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
173         CTLFLAG_RW, &tlb_flush_count, 0, "");
174 int lazy_flush_count;
175 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_count,
176         CTLFLAG_RW, &lazy_flush_count, 0, "");
177 int lazy_flush_fixup;
178 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_fixup,
179         CTLFLAG_RW, &lazy_flush_fixup, 0, "");
180 #ifdef SMP
181 int lazy_flush_smpfixup;
182 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_smpfixup,
183         CTLFLAG_RW, &lazy_flush_smpfixup, 0, "");
184 int lazy_flush_smpipi;
185 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_smpipi,
186         CTLFLAG_RW, &lazy_flush_smpipi, 0, "");
187 int lazy_flush_smpbadcr3;
188 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_smpbadcr3,
189         CTLFLAG_RW, &lazy_flush_smpbadcr3, 0, "");
190 int lazy_flush_smpmiss;
191 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_smpmiss,
192         CTLFLAG_RW, &lazy_flush_smpmiss, 0, "");
193 #endif
194 #endif
195 #ifdef LAZY_SWITCH
196 int lazy_flush_enable = 1;
197 SYSCTL_INT(_debug, OID_AUTO, lazy_flush_enable,
198         CTLFLAG_RW, &lazy_flush_enable, 0, "");
199 #endif
200
201 #ifdef PC98
202 static int      ispc98 = 1;
203 #else
204 static int      ispc98 = 0;
205 #endif
206 SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
207
208 int cold = 1;
209
210 #ifdef COMPAT_43
211 static void osendsig(sig_t catcher, int sig, sigset_t *mask, u_long code);
212 #endif
213 #ifdef COMPAT_FREEBSD4
214 static void freebsd4_sendsig(sig_t catcher, int sig, sigset_t *mask,
215     u_long code);
216 #endif
217
218 long Maxmem = 0;
219 #ifdef PC98
220 int Maxmem_under16M = 0;
221 #endif
222
223 vm_paddr_t phys_avail[10];
224
225 /* must be 2 less so 0 0 can signal end of chunks */
226 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
227
228 struct kva_md_info kmi;
229
230 static struct trapframe proc0_tf;
231 #ifndef SMP
232 static struct pcpu __pcpu;
233 #endif
234
235 struct mtx icu_lock;
236
237 static void
238 cpu_startup(dummy)
239         void *dummy;
240 {
241         /*
242          * Good {morning,afternoon,evening,night}.
243          */
244         startrtclock();
245         printcpuinfo();
246         panicifcpuunsupported();
247 #ifdef PERFMON
248         perfmon_init();
249 #endif
250         printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)Maxmem),
251             ptoa((uintmax_t)Maxmem) / 1048576);
252         /*
253          * Display any holes after the first chunk of extended memory.
254          */
255         if (bootverbose) {
256                 int indx;
257
258                 printf("Physical memory chunk(s):\n");
259                 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
260                         vm_paddr_t size;
261
262                         size = phys_avail[indx + 1] - phys_avail[indx];
263                         printf(
264                             "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
265                             (uintmax_t)phys_avail[indx],
266                             (uintmax_t)phys_avail[indx + 1] - 1,
267                             (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
268                 }
269         }
270
271         vm_ksubmap_init(&kmi);
272
273         printf("avail memory = %ju (%ju MB)\n",
274             ptoa((uintmax_t)cnt.v_free_count),
275             ptoa((uintmax_t)cnt.v_free_count) / 1048576);
276
277         /*
278          * Set up buffers, so they can be used to read disk labels.
279          */
280         bufinit();
281         vm_pager_bufferinit();
282
283 #ifndef SMP
284         /* For SMP, we delay the cpu_setregs() until after SMP startup. */
285         cpu_setregs();
286 #endif
287 }
288
289 /*
290  * Send an interrupt to process.
291  *
292  * Stack is set up to allow sigcode stored
293  * at top to call routine, followed by kcall
294  * to sigreturn routine below.  After sigreturn
295  * resets the signal mask, the stack, and the
296  * frame pointer, it returns to the user
297  * specified pc, psl.
298  */
299 #ifdef COMPAT_43
300 static void
301 osendsig(catcher, sig, mask, code)
302         sig_t catcher;
303         int sig;
304         sigset_t *mask;
305         u_long code;
306 {
307         struct osigframe sf, *fp;
308         struct proc *p;
309         struct thread *td;
310         struct sigacts *psp;
311         struct trapframe *regs;
312         int oonstack;
313
314         td = curthread;
315         p = td->td_proc;
316         PROC_LOCK_ASSERT(p, MA_OWNED);
317         psp = p->p_sigacts;
318         mtx_assert(&psp->ps_mtx, MA_OWNED);
319         regs = td->td_frame;
320         oonstack = sigonstack(regs->tf_esp);
321
322         /* Allocate space for the signal handler context. */
323         if ((p->p_flag & P_ALTSTACK) && !oonstack &&
324             SIGISMEMBER(psp->ps_sigonstack, sig)) {
325                 fp = (struct osigframe *)(p->p_sigstk.ss_sp +
326                     p->p_sigstk.ss_size - sizeof(struct osigframe));
327 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
328                 p->p_sigstk.ss_flags |= SS_ONSTACK;
329 #endif
330         } else
331                 fp = (struct osigframe *)regs->tf_esp - 1;
332
333         /* Translate the signal if appropriate. */
334         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
335                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
336
337         /* Build the argument list for the signal handler. */
338         sf.sf_signum = sig;
339         sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
340         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
341                 /* Signal handler installed with SA_SIGINFO. */
342                 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
343                 sf.sf_siginfo.si_signo = sig;
344                 sf.sf_siginfo.si_code = code;
345                 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
346         } else {
347                 /* Old FreeBSD-style arguments. */
348                 sf.sf_arg2 = code;
349                 sf.sf_addr = regs->tf_err;
350                 sf.sf_ahu.sf_handler = catcher;
351         }
352         mtx_unlock(&psp->ps_mtx);
353         PROC_UNLOCK(p);
354
355         /* Save most if not all of trap frame. */
356         sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
357         sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
358         sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
359         sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
360         sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
361         sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
362         sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
363         sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
364         sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
365         sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
366         sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
367         sf.sf_siginfo.si_sc.sc_gs = rgs();
368         sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
369
370         /* Build the signal context to be used by osigreturn(). */
371         sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
372         SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
373         sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
374         sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
375         sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
376         sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
377         sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
378         sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
379
380         /*
381          * If we're a vm86 process, we want to save the segment registers.
382          * We also change eflags to be our emulated eflags, not the actual
383          * eflags.
384          */
385         if (regs->tf_eflags & PSL_VM) {
386                 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
387                 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
388                 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
389
390                 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
391                 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
392                 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
393                 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
394
395                 if (vm86->vm86_has_vme == 0)
396                         sf.sf_siginfo.si_sc.sc_ps =
397                             (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
398                             (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
399
400                 /* See sendsig() for comments. */
401                 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
402         }
403
404         /*
405          * Copy the sigframe out to the user's stack.
406          */
407         if (copyout(&sf, fp, sizeof(*fp)) != 0) {
408 #ifdef DEBUG
409                 printf("process %ld has trashed its stack\n", (long)p->p_pid);
410 #endif
411                 PROC_LOCK(p);
412                 sigexit(td, SIGILL);
413         }
414
415         regs->tf_esp = (int)fp;
416         regs->tf_eip = PS_STRINGS - szosigcode;
417         regs->tf_eflags &= ~PSL_T;
418         regs->tf_cs = _ucodesel;
419         regs->tf_ds = _udatasel;
420         regs->tf_es = _udatasel;
421         regs->tf_fs = _udatasel;
422         load_gs(_udatasel);
423         regs->tf_ss = _udatasel;
424         PROC_LOCK(p);
425         mtx_lock(&psp->ps_mtx);
426 }
427 #endif /* COMPAT_43 */
428
429 #ifdef COMPAT_FREEBSD4
430 static void
431 freebsd4_sendsig(catcher, sig, mask, code)
432         sig_t catcher;
433         int sig;
434         sigset_t *mask;
435         u_long code;
436 {
437         struct sigframe4 sf, *sfp;
438         struct proc *p;
439         struct thread *td;
440         struct sigacts *psp;
441         struct trapframe *regs;
442         int oonstack;
443
444         td = curthread;
445         p = td->td_proc;
446         PROC_LOCK_ASSERT(p, MA_OWNED);
447         psp = p->p_sigacts;
448         mtx_assert(&psp->ps_mtx, MA_OWNED);
449         regs = td->td_frame;
450         oonstack = sigonstack(regs->tf_esp);
451
452         /* Save user context. */
453         bzero(&sf, sizeof(sf));
454         sf.sf_uc.uc_sigmask = *mask;
455         sf.sf_uc.uc_stack = p->p_sigstk;
456         sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
457             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
458         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
459         sf.sf_uc.uc_mcontext.mc_gs = rgs();
460         bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
461
462         /* Allocate space for the signal handler context. */
463         if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
464             SIGISMEMBER(psp->ps_sigonstack, sig)) {
465                 sfp = (struct sigframe4 *)(p->p_sigstk.ss_sp +
466                     p->p_sigstk.ss_size - sizeof(struct sigframe4));
467 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
468                 p->p_sigstk.ss_flags |= SS_ONSTACK;
469 #endif
470         } else
471                 sfp = (struct sigframe4 *)regs->tf_esp - 1;
472
473         /* Translate the signal if appropriate. */
474         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
475                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
476
477         /* Build the argument list for the signal handler. */
478         sf.sf_signum = sig;
479         sf.sf_ucontext = (register_t)&sfp->sf_uc;
480         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
481                 /* Signal handler installed with SA_SIGINFO. */
482                 sf.sf_siginfo = (register_t)&sfp->sf_si;
483                 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
484
485                 /* Fill in POSIX parts */
486                 sf.sf_si.si_signo = sig;
487                 sf.sf_si.si_code = code;
488                 sf.sf_si.si_addr = (void *)regs->tf_err;
489         } else {
490                 /* Old FreeBSD-style arguments. */
491                 sf.sf_siginfo = code;
492                 sf.sf_addr = regs->tf_err;
493                 sf.sf_ahu.sf_handler = catcher;
494         }
495         mtx_unlock(&psp->ps_mtx);
496         PROC_UNLOCK(p);
497
498         /*
499          * If we're a vm86 process, we want to save the segment registers.
500          * We also change eflags to be our emulated eflags, not the actual
501          * eflags.
502          */
503         if (regs->tf_eflags & PSL_VM) {
504                 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
505                 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
506
507                 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
508                 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
509                 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
510                 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
511
512                 if (vm86->vm86_has_vme == 0)
513                         sf.sf_uc.uc_mcontext.mc_eflags =
514                             (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
515                             (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
516
517                 /*
518                  * Clear PSL_NT to inhibit T_TSSFLT faults on return from
519                  * syscalls made by the signal handler.  This just avoids
520                  * wasting time for our lazy fixup of such faults.  PSL_NT
521                  * does nothing in vm86 mode, but vm86 programs can set it
522                  * almost legitimately in probes for old cpu types.
523                  */
524                 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
525         }
526
527         /*
528          * Copy the sigframe out to the user's stack.
529          */
530         if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
531 #ifdef DEBUG
532                 printf("process %ld has trashed its stack\n", (long)p->p_pid);
533 #endif
534                 PROC_LOCK(p);
535                 sigexit(td, SIGILL);
536         }
537
538         regs->tf_esp = (int)sfp;
539         regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
540         regs->tf_eflags &= ~PSL_T;
541         regs->tf_cs = _ucodesel;
542         regs->tf_ds = _udatasel;
543         regs->tf_es = _udatasel;
544         regs->tf_fs = _udatasel;
545         regs->tf_ss = _udatasel;
546         PROC_LOCK(p);
547         mtx_lock(&psp->ps_mtx);
548 }
549 #endif  /* COMPAT_FREEBSD4 */
550
551 void
552 sendsig(catcher, sig, mask, code)
553         sig_t catcher;
554         int sig;
555         sigset_t *mask;
556         u_long code;
557 {
558         struct sigframe sf, *sfp;
559         struct proc *p;
560         struct thread *td;
561         struct sigacts *psp;
562         char *sp;
563         struct trapframe *regs;
564         int oonstack;
565
566         td = curthread;
567         p = td->td_proc;
568         PROC_LOCK_ASSERT(p, MA_OWNED);
569         psp = p->p_sigacts;
570         mtx_assert(&psp->ps_mtx, MA_OWNED);
571 #ifdef COMPAT_FREEBSD4
572         if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
573                 freebsd4_sendsig(catcher, sig, mask, code);
574                 return;
575         }
576 #endif
577 #ifdef COMPAT_43
578         if (SIGISMEMBER(psp->ps_osigset, sig)) {
579                 osendsig(catcher, sig, mask, code);
580                 return;
581         }
582 #endif
583         regs = td->td_frame;
584         oonstack = sigonstack(regs->tf_esp);
585
586         /* Save user context. */
587         bzero(&sf, sizeof(sf));
588         sf.sf_uc.uc_sigmask = *mask;
589         sf.sf_uc.uc_stack = p->p_sigstk;
590         sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
591             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
592         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
593         sf.sf_uc.uc_mcontext.mc_gs = rgs();
594         bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
595         sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
596         get_fpcontext(td, &sf.sf_uc.uc_mcontext);
597         fpstate_drop(td);
598
599         /* Allocate space for the signal handler context. */
600         if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
601             SIGISMEMBER(psp->ps_sigonstack, sig)) {
602                 sp = p->p_sigstk.ss_sp +
603                     p->p_sigstk.ss_size - sizeof(struct sigframe);
604 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
605                 p->p_sigstk.ss_flags |= SS_ONSTACK;
606 #endif
607         } else
608                 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
609         /* Align to 16 bytes. */
610         sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
611
612         /* Translate the signal if appropriate. */
613         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
614                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
615
616         /* Build the argument list for the signal handler. */
617         sf.sf_signum = sig;
618         sf.sf_ucontext = (register_t)&sfp->sf_uc;
619         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
620                 /* Signal handler installed with SA_SIGINFO. */
621                 sf.sf_siginfo = (register_t)&sfp->sf_si;
622                 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
623
624                 /* Fill in POSIX parts */
625                 sf.sf_si.si_signo = sig;
626                 sf.sf_si.si_code = code;
627                 sf.sf_si.si_addr = (void *)regs->tf_err;
628         } else {
629                 /* Old FreeBSD-style arguments. */
630                 sf.sf_siginfo = code;
631                 sf.sf_addr = regs->tf_err;
632                 sf.sf_ahu.sf_handler = catcher;
633         }
634         mtx_unlock(&psp->ps_mtx);
635         PROC_UNLOCK(p);
636
637         /*
638          * If we're a vm86 process, we want to save the segment registers.
639          * We also change eflags to be our emulated eflags, not the actual
640          * eflags.
641          */
642         if (regs->tf_eflags & PSL_VM) {
643                 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
644                 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
645
646                 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
647                 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
648                 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
649                 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
650
651                 if (vm86->vm86_has_vme == 0)
652                         sf.sf_uc.uc_mcontext.mc_eflags =
653                             (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
654                             (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
655
656                 /*
657                  * Clear PSL_NT to inhibit T_TSSFLT faults on return from
658                  * syscalls made by the signal handler.  This just avoids
659                  * wasting time for our lazy fixup of such faults.  PSL_NT
660                  * does nothing in vm86 mode, but vm86 programs can set it
661                  * almost legitimately in probes for old cpu types.
662                  */
663                 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
664         }
665
666         /*
667          * Copy the sigframe out to the user's stack.
668          */
669         if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
670 #ifdef DEBUG
671                 printf("process %ld has trashed its stack\n", (long)p->p_pid);
672 #endif
673                 PROC_LOCK(p);
674                 sigexit(td, SIGILL);
675         }
676
677         regs->tf_esp = (int)sfp;
678         regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
679         regs->tf_eflags &= ~PSL_T;
680         regs->tf_cs = _ucodesel;
681         regs->tf_ds = _udatasel;
682         regs->tf_es = _udatasel;
683         regs->tf_fs = _udatasel;
684         regs->tf_ss = _udatasel;
685         PROC_LOCK(p);
686         mtx_lock(&psp->ps_mtx);
687 }
688
689 /*
690  * System call to cleanup state after a signal
691  * has been taken.  Reset signal mask and
692  * stack state from context left by sendsig (above).
693  * Return to previous pc and psl as specified by
694  * context left by sendsig. Check carefully to
695  * make sure that the user has not modified the
696  * state to gain improper privileges.
697  *
698  * MPSAFE
699  */
700 #ifdef COMPAT_43
701 int
702 osigreturn(td, uap)
703         struct thread *td;
704         struct osigreturn_args /* {
705                 struct osigcontext *sigcntxp;
706         } */ *uap;
707 {
708         struct osigcontext sc;
709         struct trapframe *regs;
710         struct osigcontext *scp;
711         struct proc *p = td->td_proc;
712         int eflags, error;
713
714         regs = td->td_frame;
715         error = copyin(uap->sigcntxp, &sc, sizeof(sc));
716         if (error != 0)
717                 return (error);
718         scp = &sc;
719         eflags = scp->sc_ps;
720         if (eflags & PSL_VM) {
721                 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
722                 struct vm86_kernel *vm86;
723
724                 /*
725                  * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
726                  * set up the vm86 area, and we can't enter vm86 mode.
727                  */
728                 if (td->td_pcb->pcb_ext == 0)
729                         return (EINVAL);
730                 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
731                 if (vm86->vm86_inited == 0)
732                         return (EINVAL);
733
734                 /* Go back to user mode if both flags are set. */
735                 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
736                         trapsignal(td, SIGBUS, 0);
737
738                 if (vm86->vm86_has_vme) {
739                         eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
740                             (eflags & VME_USERCHANGE) | PSL_VM;
741                 } else {
742                         vm86->vm86_eflags = eflags;     /* save VIF, VIP */
743                         eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
744                             (eflags & VM_USERCHANGE) | PSL_VM;
745                 }
746                 tf->tf_vm86_ds = scp->sc_ds;
747                 tf->tf_vm86_es = scp->sc_es;
748                 tf->tf_vm86_fs = scp->sc_fs;
749                 tf->tf_vm86_gs = scp->sc_gs;
750                 tf->tf_ds = _udatasel;
751                 tf->tf_es = _udatasel;
752                 tf->tf_fs = _udatasel;
753         } else {
754                 /*
755                  * Don't allow users to change privileged or reserved flags.
756                  */
757                 /*
758                  * XXX do allow users to change the privileged flag PSL_RF.
759                  * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
760                  * should sometimes set it there too.  tf_eflags is kept in
761                  * the signal context during signal handling and there is no
762                  * other place to remember it, so the PSL_RF bit may be
763                  * corrupted by the signal handler without us knowing.
764                  * Corruption of the PSL_RF bit at worst causes one more or
765                  * one less debugger trap, so allowing it is fairly harmless.
766                  */
767                 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
768                         return (EINVAL);
769                 }
770
771                 /*
772                  * Don't allow users to load a valid privileged %cs.  Let the
773                  * hardware check for invalid selectors, excess privilege in
774                  * other selectors, invalid %eip's and invalid %esp's.
775                  */
776                 if (!CS_SECURE(scp->sc_cs)) {
777                         trapsignal(td, SIGBUS, T_PROTFLT);
778                         return (EINVAL);
779                 }
780                 regs->tf_ds = scp->sc_ds;
781                 regs->tf_es = scp->sc_es;
782                 regs->tf_fs = scp->sc_fs;
783         }
784
785         /* Restore remaining registers. */
786         regs->tf_eax = scp->sc_eax;
787         regs->tf_ebx = scp->sc_ebx;
788         regs->tf_ecx = scp->sc_ecx;
789         regs->tf_edx = scp->sc_edx;
790         regs->tf_esi = scp->sc_esi;
791         regs->tf_edi = scp->sc_edi;
792         regs->tf_cs = scp->sc_cs;
793         regs->tf_ss = scp->sc_ss;
794         regs->tf_isp = scp->sc_isp;
795         regs->tf_ebp = scp->sc_fp;
796         regs->tf_esp = scp->sc_sp;
797         regs->tf_eip = scp->sc_pc;
798         regs->tf_eflags = eflags;
799
800         PROC_LOCK(p);
801 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
802         if (scp->sc_onstack & 1)
803                 p->p_sigstk.ss_flags |= SS_ONSTACK;
804         else
805                 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
806 #endif
807         SIGSETOLD(td->td_sigmask, scp->sc_mask);
808         SIG_CANTMASK(td->td_sigmask);
809         signotify(td);
810         PROC_UNLOCK(p);
811         return (EJUSTRETURN);
812 }
813 #endif /* COMPAT_43 */
814
815 #ifdef COMPAT_FREEBSD4
816 /*
817  * MPSAFE
818  */
819 int
820 freebsd4_sigreturn(td, uap)
821         struct thread *td;
822         struct freebsd4_sigreturn_args /* {
823                 const ucontext4 *sigcntxp;
824         } */ *uap;
825 {
826         struct ucontext4 uc;
827         struct proc *p = td->td_proc;
828         struct trapframe *regs;
829         const struct ucontext4 *ucp;
830         int cs, eflags, error;
831
832         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
833         if (error != 0)
834                 return (error);
835         ucp = &uc;
836         regs = td->td_frame;
837         eflags = ucp->uc_mcontext.mc_eflags;
838         if (eflags & PSL_VM) {
839                 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
840                 struct vm86_kernel *vm86;
841
842                 /*
843                  * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
844                  * set up the vm86 area, and we can't enter vm86 mode.
845                  */
846                 if (td->td_pcb->pcb_ext == 0)
847                         return (EINVAL);
848                 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
849                 if (vm86->vm86_inited == 0)
850                         return (EINVAL);
851
852                 /* Go back to user mode if both flags are set. */
853                 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
854                         trapsignal(td, SIGBUS, 0);
855
856                 if (vm86->vm86_has_vme) {
857                         eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
858                             (eflags & VME_USERCHANGE) | PSL_VM;
859                 } else {
860                         vm86->vm86_eflags = eflags;     /* save VIF, VIP */
861                         eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
862                             (eflags & VM_USERCHANGE) | PSL_VM;
863                 }
864                 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
865                 tf->tf_eflags = eflags;
866                 tf->tf_vm86_ds = tf->tf_ds;
867                 tf->tf_vm86_es = tf->tf_es;
868                 tf->tf_vm86_fs = tf->tf_fs;
869                 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
870                 tf->tf_ds = _udatasel;
871                 tf->tf_es = _udatasel;
872                 tf->tf_fs = _udatasel;
873         } else {
874                 /*
875                  * Don't allow users to change privileged or reserved flags.
876                  */
877                 /*
878                  * XXX do allow users to change the privileged flag PSL_RF.
879                  * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
880                  * should sometimes set it there too.  tf_eflags is kept in
881                  * the signal context during signal handling and there is no
882                  * other place to remember it, so the PSL_RF bit may be
883                  * corrupted by the signal handler without us knowing.
884                  * Corruption of the PSL_RF bit at worst causes one more or
885                  * one less debugger trap, so allowing it is fairly harmless.
886                  */
887                 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
888                         printf("freebsd4_sigreturn: eflags = 0x%x\n", eflags);
889                         return (EINVAL);
890                 }
891
892                 /*
893                  * Don't allow users to load a valid privileged %cs.  Let the
894                  * hardware check for invalid selectors, excess privilege in
895                  * other selectors, invalid %eip's and invalid %esp's.
896                  */
897                 cs = ucp->uc_mcontext.mc_cs;
898                 if (!CS_SECURE(cs)) {
899                         printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
900                         trapsignal(td, SIGBUS, T_PROTFLT);
901                         return (EINVAL);
902                 }
903
904                 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
905         }
906
907         PROC_LOCK(p);
908 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
909         if (ucp->uc_mcontext.mc_onstack & 1)
910                 p->p_sigstk.ss_flags |= SS_ONSTACK;
911         else
912                 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
913 #endif
914
915         td->td_sigmask = ucp->uc_sigmask;
916         SIG_CANTMASK(td->td_sigmask);
917         signotify(td);
918         PROC_UNLOCK(p);
919         return (EJUSTRETURN);
920 }
921 #endif  /* COMPAT_FREEBSD4 */
922
923 /*
924  * MPSAFE
925  */
926 int
927 sigreturn(td, uap)
928         struct thread *td;
929         struct sigreturn_args /* {
930                 const __ucontext *sigcntxp;
931         } */ *uap;
932 {
933         ucontext_t uc;
934         struct proc *p = td->td_proc;
935         struct trapframe *regs;
936         const ucontext_t *ucp;
937         int cs, eflags, error, ret;
938
939         error = copyin(uap->sigcntxp, &uc, sizeof(uc));
940         if (error != 0)
941                 return (error);
942         ucp = &uc;
943         regs = td->td_frame;
944         eflags = ucp->uc_mcontext.mc_eflags;
945         if (eflags & PSL_VM) {
946                 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
947                 struct vm86_kernel *vm86;
948
949                 /*
950                  * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
951                  * set up the vm86 area, and we can't enter vm86 mode.
952                  */
953                 if (td->td_pcb->pcb_ext == 0)
954                         return (EINVAL);
955                 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
956                 if (vm86->vm86_inited == 0)
957                         return (EINVAL);
958
959                 /* Go back to user mode if both flags are set. */
960                 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
961                         trapsignal(td, SIGBUS, 0);
962
963                 if (vm86->vm86_has_vme) {
964                         eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
965                             (eflags & VME_USERCHANGE) | PSL_VM;
966                 } else {
967                         vm86->vm86_eflags = eflags;     /* save VIF, VIP */
968                         eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
969                             (eflags & VM_USERCHANGE) | PSL_VM;
970                 }
971                 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
972                 tf->tf_eflags = eflags;
973                 tf->tf_vm86_ds = tf->tf_ds;
974                 tf->tf_vm86_es = tf->tf_es;
975                 tf->tf_vm86_fs = tf->tf_fs;
976                 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
977                 tf->tf_ds = _udatasel;
978                 tf->tf_es = _udatasel;
979                 tf->tf_fs = _udatasel;
980         } else {
981                 /*
982                  * Don't allow users to change privileged or reserved flags.
983                  */
984                 /*
985                  * XXX do allow users to change the privileged flag PSL_RF.
986                  * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
987                  * should sometimes set it there too.  tf_eflags is kept in
988                  * the signal context during signal handling and there is no
989                  * other place to remember it, so the PSL_RF bit may be
990                  * corrupted by the signal handler without us knowing.
991                  * Corruption of the PSL_RF bit at worst causes one more or
992                  * one less debugger trap, so allowing it is fairly harmless.
993                  */
994                 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
995                         printf("sigreturn: eflags = 0x%x\n", eflags);
996                         return (EINVAL);
997                 }
998
999                 /*
1000                  * Don't allow users to load a valid privileged %cs.  Let the
1001                  * hardware check for invalid selectors, excess privilege in
1002                  * other selectors, invalid %eip's and invalid %esp's.
1003                  */
1004                 cs = ucp->uc_mcontext.mc_cs;
1005                 if (!CS_SECURE(cs)) {
1006                         printf("sigreturn: cs = 0x%x\n", cs);
1007                         trapsignal(td, SIGBUS, T_PROTFLT);
1008                         return (EINVAL);
1009                 }
1010
1011                 ret = set_fpcontext(td, &ucp->uc_mcontext);
1012                 if (ret != 0)
1013                         return (ret);
1014                 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1015         }
1016
1017         PROC_LOCK(p);
1018 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1019         if (ucp->uc_mcontext.mc_onstack & 1)
1020                 p->p_sigstk.ss_flags |= SS_ONSTACK;
1021         else
1022                 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
1023 #endif
1024
1025         td->td_sigmask = ucp->uc_sigmask;
1026         SIG_CANTMASK(td->td_sigmask);
1027         signotify(td);
1028         PROC_UNLOCK(p);
1029         return (EJUSTRETURN);
1030 }
1031
1032 /*
1033  * Machine dependent boot() routine
1034  *
1035  * I haven't seen anything to put here yet
1036  * Possibly some stuff might be grafted back here from boot()
1037  */
1038 void
1039 cpu_boot(int howto)
1040 {
1041 }
1042
1043 /*
1044  * Shutdown the CPU as much as possible
1045  */
1046 void
1047 cpu_halt(void)
1048 {
1049         for (;;)
1050                 __asm__ ("hlt");
1051 }
1052
1053 /*
1054  * Hook to idle the CPU when possible.  In the SMP case we default to
1055  * off because a halted cpu will not currently pick up a new thread in the
1056  * run queue until the next timer tick.  If turned on this will result in
1057  * approximately a 4.2% loss in real time performance in buildworld tests
1058  * (but improves user and sys times oddly enough), and saves approximately
1059  * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3).
1060  *
1061  * XXX we need to have a cpu mask of idle cpus and generate an IPI or
1062  * otherwise generate some sort of interrupt to wake up cpus sitting in HLT.
1063  * Then we can have our cake and eat it too.
1064  *
1065  * XXX I'm turning it on for SMP as well by default for now.  It seems to
1066  * help lock contention somewhat, and this is critical for HTT. -Peter
1067  */
1068 static int      cpu_idle_hlt = 1;
1069 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
1070     &cpu_idle_hlt, 0, "Idle loop HLT enable");
1071
1072 /*
1073  * Note that we have to be careful here to avoid a race between checking
1074  * sched_runnable() and actually halting.  If we don't do this, we may waste
1075  * the time between calling hlt and the next interrupt even though there
1076  * is a runnable process.
1077  */
1078 void
1079 cpu_idle(void)
1080 {
1081
1082 #ifdef SMP
1083         if (mp_grab_cpu_hlt())
1084                 return;
1085 #endif
1086
1087         if (cpu_idle_hlt) {
1088                 disable_intr();
1089                 if (sched_runnable()) {
1090                         enable_intr();
1091                 } else {
1092                         /*
1093                          * we must absolutely guarentee that hlt is the
1094                          * absolute next instruction after sti or we
1095                          * introduce a timing window.
1096                          */
1097                         __asm __volatile("sti; hlt");
1098                 }
1099         }
1100 }
1101
1102 /*
1103  * Clear registers on exec
1104  */
1105 void
1106 exec_setregs(td, entry, stack, ps_strings)
1107         struct thread *td;
1108         u_long entry;
1109         u_long stack;
1110         u_long ps_strings;
1111 {
1112         struct trapframe *regs = td->td_frame;
1113         struct pcb *pcb = td->td_pcb;
1114
1115         /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1116         pcb->pcb_gs = _udatasel;
1117         load_gs(_udatasel);
1118
1119         if (td->td_proc->p_md.md_ldt)
1120                 user_ldt_free(td);
1121   
1122         bzero((char *)regs, sizeof(struct trapframe));
1123         regs->tf_eip = entry;
1124         regs->tf_esp = stack;
1125         regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1126         regs->tf_ss = _udatasel;
1127         regs->tf_ds = _udatasel;
1128         regs->tf_es = _udatasel;
1129         regs->tf_fs = _udatasel;
1130         regs->tf_cs = _ucodesel;
1131
1132         /* PS_STRINGS value for BSD/OS binaries.  It is 0 for non-BSD/OS. */
1133         regs->tf_ebx = ps_strings;
1134
1135         /*
1136          * Reset the hardware debug registers if they were in use.
1137          * They won't have any meaning for the newly exec'd process.  
1138          */
1139         if (pcb->pcb_flags & PCB_DBREGS) {
1140                 pcb->pcb_dr0 = 0;
1141                 pcb->pcb_dr1 = 0;
1142                 pcb->pcb_dr2 = 0;
1143                 pcb->pcb_dr3 = 0;
1144                 pcb->pcb_dr6 = 0;
1145                 pcb->pcb_dr7 = 0;
1146                 if (pcb == PCPU_GET(curpcb)) {
1147                         /*
1148                          * Clear the debug registers on the running
1149                          * CPU, otherwise they will end up affecting
1150                          * the next process we switch to.
1151                          */
1152                         reset_dbregs();
1153                 }
1154                 pcb->pcb_flags &= ~PCB_DBREGS;
1155         }
1156
1157         /*
1158          * Initialize the math emulator (if any) for the current process.
1159          * Actually, just clear the bit that says that the emulator has
1160          * been initialized.  Initialization is delayed until the process
1161          * traps to the emulator (if it is done at all) mainly because
1162          * emulators don't provide an entry point for initialization.
1163          */
1164         td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1165
1166         /*
1167          * Arrange to trap the next npx or `fwait' instruction (see npx.c
1168          * for why fwait must be trapped at least if there is an npx or an
1169          * emulator).  This is mainly to handle the case where npx0 is not
1170          * configured, since the npx routines normally set up the trap
1171          * otherwise.  It should be done only at boot time, but doing it
1172          * here allows modifying `npx_exists' for testing the emulator on
1173          * systems with an npx.
1174          */
1175         load_cr0(rcr0() | CR0_MP | CR0_TS);
1176
1177         /* Initialize the npx (if any) for the current process. */
1178         /*
1179          * XXX the above load_cr0() also initializes it and is a layering
1180          * violation if NPX is configured.  It drops the npx partially
1181          * and this would be fatal if we were interrupted now, and decided
1182          * to force the state to the pcb, and checked the invariant
1183          * (CR0_TS clear) if and only if PCPU_GET(fpcurthread) != NULL).
1184          * ALL of this can happen except the check.  The check used to
1185          * happen and be fatal later when we didn't complete the drop
1186          * before returning to user mode.  This should be fixed properly
1187          * soon.
1188          */
1189         fpstate_drop(td);
1190
1191         /*
1192          * XXX - Linux emulator
1193          * Make sure sure edx is 0x0 on entry. Linux binaries depend
1194          * on it.
1195          */
1196         td->td_retval[1] = 0;
1197 }
1198
1199 void
1200 cpu_setregs(void)
1201 {
1202         unsigned int cr0;
1203
1204         cr0 = rcr0();
1205 #ifdef SMP
1206         cr0 |= CR0_NE;                  /* Done by npxinit() */
1207 #endif
1208         cr0 |= CR0_MP | CR0_TS;         /* Done at every execve() too. */
1209 #ifndef I386_CPU
1210         cr0 |= CR0_WP | CR0_AM;
1211 #endif
1212         load_cr0(cr0);
1213         load_gs(_udatasel);
1214 }
1215
1216 static int
1217 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
1218 {
1219         int error;
1220         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1221                 req);
1222         if (!error && req->newptr)
1223                 resettodr();
1224         return (error);
1225 }
1226
1227 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1228         &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1229
1230 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1231         CTLFLAG_RW, &disable_rtc_set, 0, "");
1232
1233 SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo, 
1234         CTLFLAG_RD, &bootinfo, bootinfo, "");
1235
1236 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1237         CTLFLAG_RW, &wall_cmos_clock, 0, "");
1238
1239 u_long bootdev;         /* not a dev_t - encoding is different */
1240 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1241         CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in dev_t format)");
1242
1243 /*
1244  * Initialize 386 and configure to run kernel
1245  */
1246
1247 /*
1248  * Initialize segments & interrupt table
1249  */
1250
1251 int _default_ldt;
1252 union descriptor gdt[NGDT * MAXCPU];    /* global descriptor table */
1253 static struct gate_descriptor idt0[NIDT];
1254 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1255 union descriptor ldt[NLDT];             /* local descriptor table */
1256 #ifdef SMP
1257 /* table descriptors - used to load tables by microp */
1258 struct region_descriptor r_gdt, r_idt;
1259 #endif
1260
1261 int private_tss;                        /* flag indicating private tss */
1262
1263 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1264 extern int has_f00f_bug;
1265 #endif
1266
1267 static struct i386tss dblfault_tss;
1268 static char dblfault_stack[PAGE_SIZE];
1269
1270 extern  struct user     *proc0uarea;
1271 extern  vm_offset_t     proc0kstack;
1272
1273
1274 /* software prototypes -- in more palatable form */
1275 struct soft_segment_descriptor gdt_segs[] = {
1276 /* GNULL_SEL    0 Null Descriptor */
1277 {       0x0,                    /* segment base address  */
1278         0x0,                    /* length */
1279         0,                      /* segment type */
1280         0,                      /* segment descriptor priority level */
1281         0,                      /* segment descriptor present */
1282         0, 0,
1283         0,                      /* default 32 vs 16 bit size */
1284         0                       /* limit granularity (byte/page units)*/ },
1285 /* GCODE_SEL    1 Code Descriptor for kernel */
1286 {       0x0,                    /* segment base address  */
1287         0xfffff,                /* length - all address space */
1288         SDT_MEMERA,             /* segment type */
1289         0,                      /* segment descriptor priority level */
1290         1,                      /* segment descriptor present */
1291         0, 0,
1292         1,                      /* default 32 vs 16 bit size */
1293         1                       /* limit granularity (byte/page units)*/ },
1294 /* GDATA_SEL    2 Data Descriptor for kernel */
1295 {       0x0,                    /* segment base address  */
1296         0xfffff,                /* length - all address space */
1297         SDT_MEMRWA,             /* segment type */
1298         0,                      /* segment descriptor priority level */
1299         1,                      /* segment descriptor present */
1300         0, 0,
1301         1,                      /* default 32 vs 16 bit size */
1302         1                       /* limit granularity (byte/page units)*/ },
1303 /* GPRIV_SEL    3 SMP Per-Processor Private Data Descriptor */
1304 {       0x0,                    /* segment base address  */
1305         0xfffff,                /* length - all address space */
1306         SDT_MEMRWA,             /* segment type */
1307         0,                      /* segment descriptor priority level */
1308         1,                      /* segment descriptor present */
1309         0, 0,
1310         1,                      /* default 32 vs 16 bit size */
1311         1                       /* limit granularity (byte/page units)*/ },
1312 /* GPROC0_SEL   4 Proc 0 Tss Descriptor */
1313 {
1314         0x0,                    /* segment base address */
1315         sizeof(struct i386tss)-1,/* length - all address space */
1316         SDT_SYS386TSS,          /* segment type */
1317         0,                      /* segment descriptor priority level */
1318         1,                      /* segment descriptor present */
1319         0, 0,
1320         0,                      /* unused - default 32 vs 16 bit size */
1321         0                       /* limit granularity (byte/page units)*/ },
1322 /* GLDT_SEL     5 LDT Descriptor */
1323 {       (int) ldt,              /* segment base address  */
1324         sizeof(ldt)-1,          /* length - all address space */
1325         SDT_SYSLDT,             /* segment type */
1326         SEL_UPL,                /* segment descriptor priority level */
1327         1,                      /* segment descriptor present */
1328         0, 0,
1329         0,                      /* unused - default 32 vs 16 bit size */
1330         0                       /* limit granularity (byte/page units)*/ },
1331 /* GUSERLDT_SEL 6 User LDT Descriptor per process */
1332 {       (int) ldt,              /* segment base address  */
1333         (512 * sizeof(union descriptor)-1),             /* length */
1334         SDT_SYSLDT,             /* segment type */
1335         0,                      /* segment descriptor priority level */
1336         1,                      /* segment descriptor present */
1337         0, 0,
1338         0,                      /* unused - default 32 vs 16 bit size */
1339         0                       /* limit granularity (byte/page units)*/ },
1340 /* GTGATE_SEL   7 Null Descriptor - Placeholder */
1341 {       0x0,                    /* segment base address  */
1342         0x0,                    /* length - all address space */
1343         0,                      /* segment type */
1344         0,                      /* segment descriptor priority level */
1345         0,                      /* segment descriptor present */
1346         0, 0,
1347         0,                      /* default 32 vs 16 bit size */
1348         0                       /* limit granularity (byte/page units)*/ },
1349 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1350 {       0x400,                  /* segment base address */
1351         0xfffff,                /* length */
1352         SDT_MEMRWA,             /* segment type */
1353         0,                      /* segment descriptor priority level */
1354         1,                      /* segment descriptor present */
1355         0, 0,
1356         1,                      /* default 32 vs 16 bit size */
1357         1                       /* limit granularity (byte/page units)*/ },
1358 /* GPANIC_SEL   9 Panic Tss Descriptor */
1359 {       (int) &dblfault_tss,    /* segment base address  */
1360         sizeof(struct i386tss)-1,/* length - all address space */
1361         SDT_SYS386TSS,          /* segment type */
1362         0,                      /* segment descriptor priority level */
1363         1,                      /* segment descriptor present */
1364         0, 0,
1365         0,                      /* unused - default 32 vs 16 bit size */
1366         0                       /* limit granularity (byte/page units)*/ },
1367 /* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1368 {       0,                      /* segment base address (overwritten)  */
1369         0xfffff,                /* length */
1370         SDT_MEMERA,             /* segment type */
1371         0,                      /* segment descriptor priority level */
1372         1,                      /* segment descriptor present */
1373         0, 0,
1374         0,                      /* default 32 vs 16 bit size */
1375         1                       /* limit granularity (byte/page units)*/ },
1376 /* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1377 {       0,                      /* segment base address (overwritten)  */
1378         0xfffff,                /* length */
1379         SDT_MEMERA,             /* segment type */
1380         0,                      /* segment descriptor priority level */
1381         1,                      /* segment descriptor present */
1382         0, 0,
1383         0,                      /* default 32 vs 16 bit size */
1384         1                       /* limit granularity (byte/page units)*/ },
1385 /* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1386 {       0,                      /* segment base address (overwritten) */
1387         0xfffff,                /* length */
1388         SDT_MEMRWA,             /* segment type */
1389         0,                      /* segment descriptor priority level */
1390         1,                      /* segment descriptor present */
1391         0, 0,
1392         1,                      /* default 32 vs 16 bit size */
1393         1                       /* limit granularity (byte/page units)*/ },
1394 /* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1395 {       0,                      /* segment base address (overwritten) */
1396         0xfffff,                /* length */
1397         SDT_MEMRWA,             /* segment type */
1398         0,                      /* segment descriptor priority level */
1399         1,                      /* segment descriptor present */
1400         0, 0,
1401         0,                      /* default 32 vs 16 bit size */
1402         1                       /* limit granularity (byte/page units)*/ },
1403 /* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1404 {       0,                      /* segment base address (overwritten) */
1405         0xfffff,                /* length */
1406         SDT_MEMRWA,             /* segment type */
1407         0,                      /* segment descriptor priority level */
1408         1,                      /* segment descriptor present */
1409         0, 0,
1410         0,                      /* default 32 vs 16 bit size */
1411         1                       /* limit granularity (byte/page units)*/ },
1412 };
1413
1414 static struct soft_segment_descriptor ldt_segs[] = {
1415         /* Null Descriptor - overwritten by call gate */
1416 {       0x0,                    /* segment base address  */
1417         0x0,                    /* length - all address space */
1418         0,                      /* segment type */
1419         0,                      /* segment descriptor priority level */
1420         0,                      /* segment descriptor present */
1421         0, 0,
1422         0,                      /* default 32 vs 16 bit size */
1423         0                       /* limit granularity (byte/page units)*/ },
1424         /* Null Descriptor - overwritten by call gate */
1425 {       0x0,                    /* segment base address  */
1426         0x0,                    /* length - all address space */
1427         0,                      /* segment type */
1428         0,                      /* segment descriptor priority level */
1429         0,                      /* segment descriptor present */
1430         0, 0,
1431         0,                      /* default 32 vs 16 bit size */
1432         0                       /* limit granularity (byte/page units)*/ },
1433         /* Null Descriptor - overwritten by call gate */
1434 {       0x0,                    /* segment base address  */
1435         0x0,                    /* length - all address space */
1436         0,                      /* segment type */
1437         0,                      /* segment descriptor priority level */
1438         0,                      /* segment descriptor present */
1439         0, 0,
1440         0,                      /* default 32 vs 16 bit size */
1441         0                       /* limit granularity (byte/page units)*/ },
1442         /* Code Descriptor for user */
1443 {       0x0,                    /* segment base address  */
1444         0xfffff,                /* length - all address space */
1445         SDT_MEMERA,             /* segment type */
1446         SEL_UPL,                /* segment descriptor priority level */
1447         1,                      /* segment descriptor present */
1448         0, 0,
1449         1,                      /* default 32 vs 16 bit size */
1450         1                       /* limit granularity (byte/page units)*/ },
1451         /* Null Descriptor - overwritten by call gate */
1452 {       0x0,                    /* segment base address  */
1453         0x0,                    /* length - all address space */
1454         0,                      /* segment type */
1455         0,                      /* segment descriptor priority level */
1456         0,                      /* segment descriptor present */
1457         0, 0,
1458         0,                      /* default 32 vs 16 bit size */
1459         0                       /* limit granularity (byte/page units)*/ },
1460         /* Data Descriptor for user */
1461 {       0x0,                    /* segment base address  */
1462         0xfffff,                /* length - all address space */
1463         SDT_MEMRWA,             /* segment type */
1464         SEL_UPL,                /* segment descriptor priority level */
1465         1,                      /* segment descriptor present */
1466         0, 0,
1467         1,                      /* default 32 vs 16 bit size */
1468         1                       /* limit granularity (byte/page units)*/ },
1469 };
1470
1471 void
1472 setidt(idx, func, typ, dpl, selec)
1473         int idx;
1474         inthand_t *func;
1475         int typ;
1476         int dpl;
1477         int selec;
1478 {
1479         struct gate_descriptor *ip;
1480
1481         ip = idt + idx;
1482         ip->gd_looffset = (int)func;
1483         ip->gd_selector = selec;
1484         ip->gd_stkcpy = 0;
1485         ip->gd_xx = 0;
1486         ip->gd_type = typ;
1487         ip->gd_dpl = dpl;
1488         ip->gd_p = 1;
1489         ip->gd_hioffset = ((int)func)>>16 ;
1490 }
1491
1492 #define IDTVEC(name)    __CONCAT(X,name)
1493
1494 extern inthand_t
1495         IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1496         IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1497         IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1498         IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1499         IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1500
1501 void
1502 sdtossd(sd, ssd)
1503         struct segment_descriptor *sd;
1504         struct soft_segment_descriptor *ssd;
1505 {
1506         ssd->ssd_base  = (sd->sd_hibase << 24) | sd->sd_lobase;
1507         ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1508         ssd->ssd_type  = sd->sd_type;
1509         ssd->ssd_dpl   = sd->sd_dpl;
1510         ssd->ssd_p     = sd->sd_p;
1511         ssd->ssd_def32 = sd->sd_def32;
1512         ssd->ssd_gran  = sd->sd_gran;
1513 }
1514
1515 #define PHYSMAP_SIZE    (2 * 8)
1516
1517 /*
1518  * Populate the (physmap) array with base/bound pairs describing the
1519  * available physical memory in the system, then test this memory and
1520  * build the phys_avail array describing the actually-available memory.
1521  *
1522  * If we cannot accurately determine the physical memory map, then use
1523  * value from the 0xE801 call, and failing that, the RTC.
1524  *
1525  * Total memory size may be set by the kernel environment variable
1526  * hw.physmem or the compile-time define MAXMEM.
1527  *
1528  * XXX first should be vm_paddr_t.
1529  */
1530 static void
1531 getmemsize(int first)
1532 {
1533 #ifdef PC98
1534         int i, physmap_idx, pa_indx, pg_n;
1535         u_int basemem, extmem, under16;
1536         vm_offset_t pa, physmap[PHYSMAP_SIZE];
1537         pt_entry_t *pte;
1538         char *cp;
1539 #else
1540         int i, physmap_idx, pa_indx;
1541         u_int basemem, extmem;
1542         struct vm86frame vmf;
1543         struct vm86context vmc;
1544         vm_paddr_t pa, physmap[PHYSMAP_SIZE];
1545         pt_entry_t *pte;
1546         char *cp;
1547         struct bios_smap *smap;
1548 #endif
1549
1550 #ifdef PC98
1551         /* XXX - some of EPSON machines can't use PG_N */
1552         pg_n = PG_N;
1553         if (pc98_machine_type & M_EPSON_PC98) {
1554                 switch (epson_machine_id) {
1555 #ifdef WB_CACHE
1556                 default:
1557 #endif
1558                 case 0x34:              /* PC-486HX */
1559                 case 0x35:              /* PC-486HG */
1560                 case 0x3B:              /* PC-486HA */
1561                         pg_n = 0;
1562                         break;
1563                 }
1564         }
1565         bzero(physmap, sizeof(physmap));
1566
1567         /*
1568          * Perform "base memory" related probes & setup
1569          */
1570         under16 = pc98_getmemsize(&basemem, &extmem);
1571         if (basemem > 640) {
1572                 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1573                         basemem);
1574                 basemem = 640;
1575         }
1576
1577         /*
1578          * XXX if biosbasemem is now < 640, there is a `hole'
1579          * between the end of base memory and the start of
1580          * ISA memory.  The hole may be empty or it may
1581          * contain BIOS code or data.  Map it read/write so
1582          * that the BIOS can write to it.  (Memory from 0 to
1583          * the physical end of the kernel is mapped read-only
1584          * to begin with and then parts of it are remapped.
1585          * The parts that aren't remapped form holes that
1586          * remain read-only and are unused by the kernel.
1587          * The base memory area is below the physical end of
1588          * the kernel and right now forms a read-only hole.
1589          * The part of it from PAGE_SIZE to
1590          * (trunc_page(biosbasemem * 1024) - 1) will be
1591          * remapped and used by the kernel later.)
1592          *
1593          * This code is similar to the code used in
1594          * pmap_mapdev, but since no memory needs to be
1595          * allocated we simply change the mapping.
1596          */
1597         for (pa = trunc_page(basemem * 1024);
1598              pa < ISA_HOLE_START; pa += PAGE_SIZE)
1599                 pmap_kenter(KERNBASE + pa, pa);
1600
1601         /*
1602          * if basemem != 640, map pages r/w into vm86 page table so 
1603          * that the bios can scribble on it.
1604          */
1605         pte = (pt_entry_t *)vm86paddr;
1606         for (i = basemem / 4; i < 160; i++)
1607                 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1608
1609 #else /* PC98 */
1610
1611         bzero(&vmf, sizeof(struct vm86frame));
1612         bzero(physmap, sizeof(physmap));
1613         basemem = 0;
1614
1615         /*
1616          * map page 1 R/W into the kernel page table so we can use it
1617          * as a buffer.  The kernel will unmap this page later.
1618          */
1619         pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
1620
1621         /*
1622          * get memory map with INT 15:E820
1623          */
1624         vmc.npages = 0;
1625         smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1626         vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1627
1628         physmap_idx = 0;
1629         vmf.vmf_ebx = 0;
1630         do {
1631                 vmf.vmf_eax = 0xE820;
1632                 vmf.vmf_edx = SMAP_SIG;
1633                 vmf.vmf_ecx = sizeof(struct bios_smap);
1634                 i = vm86_datacall(0x15, &vmf, &vmc);
1635                 if (i || vmf.vmf_eax != SMAP_SIG)
1636                         break;
1637                 if (boothowto & RB_VERBOSE)
1638                         printf("SMAP type=%02x base=%016llx len=%016llx\n",
1639                             smap->type, smap->base, smap->length);
1640
1641                 if (smap->type != 0x01)
1642                         goto next_run;
1643
1644                 if (smap->length == 0)
1645                         goto next_run;
1646
1647                 if (smap->base >= 0xffffffff) {
1648                         printf("%uK of memory above 4GB ignored\n",
1649                             (u_int)(smap->length / 1024));
1650                         goto next_run;
1651                 }
1652
1653                 for (i = 0; i <= physmap_idx; i += 2) {
1654                         if (smap->base < physmap[i + 1]) {
1655                                 if (boothowto & RB_VERBOSE)
1656                                         printf(
1657         "Overlapping or non-montonic memory region, ignoring second region\n");
1658                                 goto next_run;
1659                         }
1660                 }
1661
1662                 if (smap->base == physmap[physmap_idx + 1]) {
1663                         physmap[physmap_idx + 1] += smap->length;
1664                         goto next_run;
1665                 }
1666
1667                 physmap_idx += 2;
1668                 if (physmap_idx == PHYSMAP_SIZE) {
1669                         printf(
1670                 "Too many segments in the physical address map, giving up\n");
1671                         break;
1672                 }
1673                 physmap[physmap_idx] = smap->base;
1674                 physmap[physmap_idx + 1] = smap->base + smap->length;
1675 next_run: ;
1676         } while (vmf.vmf_ebx != 0);
1677
1678         /*
1679          * Perform "base memory" related probes & setup
1680          */
1681         for (i = 0; i <= physmap_idx; i += 2) {
1682                 if (physmap[i] == 0x00000000) {
1683                         basemem = physmap[i + 1] / 1024;
1684                         break;
1685                 }
1686         }
1687
1688         /* Fall back to the old compatibility function for base memory */
1689         if (basemem == 0) {
1690                 vm86_intcall(0x12, &vmf);
1691                 basemem = vmf.vmf_ax;
1692         }
1693
1694         if (basemem > 640) {
1695                 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1696                         basemem);
1697                 basemem = 640;
1698         }
1699
1700         /*
1701          * XXX if biosbasemem is now < 640, there is a `hole'
1702          * between the end of base memory and the start of
1703          * ISA memory.  The hole may be empty or it may
1704          * contain BIOS code or data.  Map it read/write so
1705          * that the BIOS can write to it.  (Memory from 0 to
1706          * the physical end of the kernel is mapped read-only
1707          * to begin with and then parts of it are remapped.
1708          * The parts that aren't remapped form holes that
1709          * remain read-only and are unused by the kernel.
1710          * The base memory area is below the physical end of
1711          * the kernel and right now forms a read-only hole.
1712          * The part of it from PAGE_SIZE to
1713          * (trunc_page(biosbasemem * 1024) - 1) will be
1714          * remapped and used by the kernel later.)
1715          *
1716          * This code is similar to the code used in
1717          * pmap_mapdev, but since no memory needs to be
1718          * allocated we simply change the mapping.
1719          */
1720         for (pa = trunc_page(basemem * 1024);
1721              pa < ISA_HOLE_START; pa += PAGE_SIZE)
1722                 pmap_kenter(KERNBASE + pa, pa);
1723
1724         /*
1725          * if basemem != 640, map pages r/w into vm86 page table so
1726          * that the bios can scribble on it.
1727          */
1728         pte = (pt_entry_t *)vm86paddr;
1729         for (i = basemem / 4; i < 160; i++)
1730                 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1731
1732         if (physmap[1] != 0)
1733                 goto physmap_done;
1734
1735         /*
1736          * If we failed above, try memory map with INT 15:E801
1737          */
1738         vmf.vmf_ax = 0xE801;
1739         if (vm86_intcall(0x15, &vmf) == 0) {
1740                 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1741         } else {
1742 #if 0
1743                 vmf.vmf_ah = 0x88;
1744                 vm86_intcall(0x15, &vmf);
1745                 extmem = vmf.vmf_ax;
1746 #else
1747                 /*
1748                  * Prefer the RTC value for extended memory.
1749                  */
1750                 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1751 #endif
1752         }
1753
1754         /*
1755          * Special hack for chipsets that still remap the 384k hole when
1756          * there's 16MB of memory - this really confuses people that
1757          * are trying to use bus mastering ISA controllers with the
1758          * "16MB limit"; they only have 16MB, but the remapping puts
1759          * them beyond the limit.
1760          *
1761          * If extended memory is between 15-16MB (16-17MB phys address range),
1762          *      chop it to 15MB.
1763          */
1764         if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1765                 extmem = 15 * 1024;
1766 #endif /* PC98 */
1767
1768         physmap[0] = 0;
1769         physmap[1] = basemem * 1024;
1770         physmap_idx = 2;
1771         physmap[physmap_idx] = 0x100000;
1772         physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1773
1774 #ifdef PC98
1775         if ((under16 != 16 * 1024) && (extmem > 15 * 1024)) {
1776                 /* 15M - 16M region is cut off, so need to divide chunk */
1777                 physmap[physmap_idx + 1] = under16 * 1024;
1778                 physmap_idx += 2;
1779                 physmap[physmap_idx] = 0x1000000;
1780                 physmap[physmap_idx + 1] = physmap[2] + extmem * 1024;
1781         }
1782 #else
1783 physmap_done:
1784 #endif
1785         /*
1786          * Now, physmap contains a map of physical memory.
1787          */
1788
1789 #ifdef SMP
1790         /* make hole for AP bootstrap code */
1791         physmap[1] = mp_bootaddress(physmap[1] / 1024);
1792
1793         /* look for the MP hardware - needed for apic addresses */
1794         i386_mp_probe();
1795 #endif
1796
1797         /*
1798          * Maxmem isn't the "maximum memory", it's one larger than the
1799          * highest page of the physical address space.  It should be
1800          * called something like "Maxphyspage".  We may adjust this 
1801          * based on ``hw.physmem'' and the results of the memory test.
1802          */
1803         Maxmem = atop(physmap[physmap_idx + 1]);
1804
1805 #ifdef MAXMEM
1806         Maxmem = MAXMEM / 4;
1807 #endif
1808
1809         /*
1810          * hw.physmem is a size in bytes; we also allow k, m, and g suffixes
1811          * for the appropriate modifiers.  This overrides MAXMEM.
1812          */
1813         if ((cp = getenv("hw.physmem")) != NULL) {
1814                 u_int64_t AllowMem, sanity;
1815                 char *ep;
1816
1817                 sanity = AllowMem = strtouq(cp, &ep, 0);
1818                 if ((ep != cp) && (*ep != 0)) {
1819                         switch(*ep) {
1820                         case 'g':
1821                         case 'G':
1822                                 AllowMem <<= 10;
1823                         case 'm':
1824                         case 'M':
1825                                 AllowMem <<= 10;
1826                         case 'k':
1827                         case 'K':
1828                                 AllowMem <<= 10;
1829                                 break;
1830                         default:
1831                                 AllowMem = sanity = 0;
1832                         }
1833                         if (AllowMem < sanity)
1834                                 AllowMem = 0;
1835                 }
1836                 if (AllowMem == 0)
1837                         printf("Ignoring invalid memory size of '%s'\n", cp);
1838                 else
1839                         Maxmem = atop(AllowMem);
1840                 freeenv(cp);
1841         }
1842
1843         if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1844             (boothowto & RB_VERBOSE))
1845                 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1846
1847         /*
1848          * If Maxmem has been increased beyond what the system has detected,
1849          * extend the last memory segment to the new limit.
1850          */ 
1851         if (atop(physmap[physmap_idx + 1]) < Maxmem)
1852                 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1853
1854         /* call pmap initialization to make new kernel address space */
1855         pmap_bootstrap(first, 0);
1856
1857         /*
1858          * Size up each available chunk of physical memory.
1859          */
1860         physmap[0] = PAGE_SIZE;         /* mask off page 0 */
1861         pa_indx = 0;
1862         phys_avail[pa_indx++] = physmap[0];
1863         phys_avail[pa_indx] = physmap[0];
1864         pte = CMAP1;
1865
1866         /*
1867          * physmap is in bytes, so when converting to page boundaries,
1868          * round up the start address and round down the end address.
1869          */
1870         for (i = 0; i <= physmap_idx; i += 2) {
1871                 vm_paddr_t end;
1872
1873                 end = ptoa((vm_paddr_t)Maxmem);
1874                 if (physmap[i + 1] < end)
1875                         end = trunc_page(physmap[i + 1]);
1876                 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1877                         int tmp, page_bad;
1878                         int *ptr = (int *)CADDR1;
1879
1880                         /*
1881                          * block out kernel memory as not available.
1882                          */
1883                         if (pa >= 0x100000 && pa < first)
1884                                 continue;
1885         
1886                         page_bad = FALSE;
1887
1888                         /*
1889                          * map page into kernel: valid, read/write,non-cacheable
1890                          */
1891 #ifdef PC98
1892                         *pte = pa | PG_V | PG_RW | pg_n;
1893 #else
1894                         *pte = pa | PG_V | PG_RW | PG_N;
1895 #endif
1896                         invltlb();
1897
1898                         tmp = *(int *)ptr;
1899                         /*
1900                          * Test for alternating 1's and 0's
1901                          */
1902                         *(volatile int *)ptr = 0xaaaaaaaa;
1903                         if (*(volatile int *)ptr != 0xaaaaaaaa) {
1904                                 page_bad = TRUE;
1905                         }
1906                         /*
1907                          * Test for alternating 0's and 1's
1908                          */
1909                         *(volatile int *)ptr = 0x55555555;
1910                         if (*(volatile int *)ptr != 0x55555555) {
1911                         page_bad = TRUE;
1912                         }
1913                         /*
1914                          * Test for all 1's
1915                          */
1916                         *(volatile int *)ptr = 0xffffffff;
1917                         if (*(volatile int *)ptr != 0xffffffff) {
1918                                 page_bad = TRUE;
1919                         }
1920                         /*
1921                          * Test for all 0's
1922                          */
1923                         *(volatile int *)ptr = 0x0;
1924                         if (*(volatile int *)ptr != 0x0) {
1925                                 page_bad = TRUE;
1926                         }
1927                         /*
1928                          * Restore original value.
1929                          */
1930                         *(int *)ptr = tmp;
1931
1932                         /*
1933                          * Adjust array of valid/good pages.
1934                          */
1935                         if (page_bad == TRUE) {
1936                                 continue;
1937                         }
1938                         /*
1939                          * If this good page is a continuation of the
1940                          * previous set of good pages, then just increase
1941                          * the end pointer. Otherwise start a new chunk.
1942                          * Note that "end" points one higher than end,
1943                          * making the range >= start and < end.
1944                          * If we're also doing a speculative memory
1945                          * test and we at or past the end, bump up Maxmem
1946                          * so that we keep going. The first bad page
1947                          * will terminate the loop.
1948                          */
1949                         if (phys_avail[pa_indx] == pa) {
1950                                 phys_avail[pa_indx] += PAGE_SIZE;
1951                         } else {
1952                                 pa_indx++;
1953                                 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1954                                         printf(
1955                 "Too many holes in the physical address space, giving up\n");
1956                                         pa_indx--;
1957                                         break;
1958                                 }
1959                                 phys_avail[pa_indx++] = pa;     /* start */
1960                                 phys_avail[pa_indx] = pa + PAGE_SIZE;   /* end */
1961                         }
1962                         physmem++;
1963                 }
1964         }
1965         *pte = 0;
1966         invltlb();
1967
1968         /*
1969          * XXX
1970          * The last chunk must contain at least one page plus the message
1971          * buffer to avoid complicating other code (message buffer address
1972          * calculation, etc.).
1973          */
1974         while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1975             round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1976                 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1977                 phys_avail[pa_indx--] = 0;
1978                 phys_avail[pa_indx--] = 0;
1979         }
1980
1981         Maxmem = atop(phys_avail[pa_indx]);
1982
1983         /* Trim off space for the message buffer. */
1984         phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1985
1986         avail_end = phys_avail[pa_indx];
1987 }
1988
1989 void
1990 init386(first)
1991         int first;
1992 {
1993         struct gate_descriptor *gdp;
1994         int gsel_tss, metadata_missing, off, x;
1995 #ifndef SMP
1996         /* table descriptors - used to load tables by microp */
1997         struct region_descriptor r_gdt, r_idt;
1998 #endif
1999         struct pcpu *pc;
2000
2001         proc0.p_uarea = proc0uarea;
2002         thread0.td_kstack = proc0kstack;
2003         thread0.td_pcb = (struct pcb *)
2004            (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
2005         atdevbase = ISA_HOLE_START + KERNBASE;
2006
2007         /*
2008          * This may be done better later if it gets more high level
2009          * components in it. If so just link td->td_proc here.
2010          */
2011         proc_linkup(&proc0, &ksegrp0, &kse0, &thread0);
2012
2013 #ifdef PC98
2014         /*
2015          * Initialize DMAC
2016          */
2017         pc98_init_dmac();
2018 #endif
2019
2020         metadata_missing = 0;
2021         if (bootinfo.bi_modulep) {
2022                 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2023                 preload_bootstrap_relocate(KERNBASE);
2024         } else {
2025                 metadata_missing = 1;
2026         }
2027         if (envmode == 1)
2028                 kern_envp = static_env;
2029         else if (bootinfo.bi_envp)
2030                 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
2031
2032         /* Init basic tunables, hz etc */
2033         init_param1();
2034
2035         /*
2036          * make gdt memory segments, the code segment goes up to end of the
2037          * page with etext in it, the data segment goes to the end of
2038          * the address space
2039          */
2040         /*
2041          * XXX text protection is temporarily (?) disabled.  The limit was
2042          * i386_btop(round_page(etext)) - 1.
2043          */
2044         gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2045         gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2046 #ifdef SMP
2047         pc = &SMP_prvspace[0].pcpu;
2048         gdt_segs[GPRIV_SEL].ssd_limit =
2049                 atop(sizeof(struct privatespace) - 1);
2050 #else
2051         pc = &__pcpu;
2052         gdt_segs[GPRIV_SEL].ssd_limit =
2053                 atop(sizeof(struct pcpu) - 1);
2054 #endif
2055         gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2056         gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2057
2058         for (x = 0; x < NGDT; x++)
2059                 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2060
2061         r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2062         r_gdt.rd_base =  (int) gdt;
2063         lgdt(&r_gdt);
2064
2065         pcpu_init(pc, 0, sizeof(struct pcpu));
2066         PCPU_SET(prvspace, pc);
2067         PCPU_SET(curthread, &thread0);
2068
2069         /*
2070          * Initialize mutexes.
2071          *
2072          * icu_lock: in order to allow an interrupt to occur in a critical
2073          *           section, to set pcpu->ipending (etc...) properly, we
2074          *           must be able to get the icu lock, so it can't be
2075          *           under witness.
2076          */
2077         mutex_init();
2078         mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_RECURSE);
2079         mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS);
2080
2081         /* make ldt memory segments */
2082         /*
2083          * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  And it
2084          * should be spelled ...MAX_USER...
2085          */
2086         ldt_segs[LUCODE_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
2087         ldt_segs[LUDATA_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
2088         for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2089                 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2090
2091         _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2092         lldt(_default_ldt);
2093         PCPU_SET(currentldt, _default_ldt);
2094
2095         /* exceptions */
2096         for (x = 0; x < NIDT; x++)
2097                 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2098                     GSEL(GCODE_SEL, SEL_KPL));
2099         setidt(0, &IDTVEC(div),  SDT_SYS386TGT, SEL_KPL,
2100             GSEL(GCODE_SEL, SEL_KPL));
2101         setidt(1, &IDTVEC(dbg),  SDT_SYS386IGT, SEL_KPL,
2102             GSEL(GCODE_SEL, SEL_KPL));
2103         setidt(2, &IDTVEC(nmi),  SDT_SYS386TGT, SEL_KPL,
2104             GSEL(GCODE_SEL, SEL_KPL));
2105         setidt(3, &IDTVEC(bpt),  SDT_SYS386IGT, SEL_UPL,
2106             GSEL(GCODE_SEL, SEL_KPL));
2107         setidt(4, &IDTVEC(ofl),  SDT_SYS386TGT, SEL_UPL,
2108             GSEL(GCODE_SEL, SEL_KPL));
2109         setidt(5, &IDTVEC(bnd),  SDT_SYS386TGT, SEL_KPL,
2110             GSEL(GCODE_SEL, SEL_KPL));
2111         setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL,
2112             GSEL(GCODE_SEL, SEL_KPL));
2113         setidt(7, &IDTVEC(dna),  SDT_SYS386TGT, SEL_KPL
2114             , GSEL(GCODE_SEL, SEL_KPL));
2115         setidt(8, 0,  SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2116         setidt(9, &IDTVEC(fpusegm),  SDT_SYS386TGT, SEL_KPL,
2117             GSEL(GCODE_SEL, SEL_KPL));
2118         setidt(10, &IDTVEC(tss),  SDT_SYS386TGT, SEL_KPL,
2119             GSEL(GCODE_SEL, SEL_KPL));
2120         setidt(11, &IDTVEC(missing),  SDT_SYS386TGT, SEL_KPL,
2121             GSEL(GCODE_SEL, SEL_KPL));
2122         setidt(12, &IDTVEC(stk),  SDT_SYS386TGT, SEL_KPL,
2123             GSEL(GCODE_SEL, SEL_KPL));
2124         setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL,
2125             GSEL(GCODE_SEL, SEL_KPL));
2126         setidt(14, &IDTVEC(page),  SDT_SYS386IGT, SEL_KPL,
2127             GSEL(GCODE_SEL, SEL_KPL));
2128         setidt(15, &IDTVEC(rsvd),  SDT_SYS386TGT, SEL_KPL,
2129             GSEL(GCODE_SEL, SEL_KPL));
2130         setidt(16, &IDTVEC(fpu),  SDT_SYS386TGT, SEL_KPL,
2131             GSEL(GCODE_SEL, SEL_KPL));
2132         setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2133             GSEL(GCODE_SEL, SEL_KPL));
2134         setidt(18, &IDTVEC(mchk),  SDT_SYS386TGT, SEL_KPL,
2135             GSEL(GCODE_SEL, SEL_KPL));
2136         setidt(19, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2137             GSEL(GCODE_SEL, SEL_KPL));
2138         setidt(0x80, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2139             GSEL(GCODE_SEL, SEL_KPL));
2140
2141         r_idt.rd_limit = sizeof(idt0) - 1;
2142         r_idt.rd_base = (int) idt;
2143         lidt(&r_idt);
2144
2145         /*
2146          * Initialize the console before we print anything out.
2147          */
2148         cninit();
2149
2150         if (metadata_missing)
2151                 printf("WARNING: loader(8) metadata is missing!\n");
2152
2153 #ifdef DEV_ISA
2154         isa_defaultirq();
2155 #endif
2156
2157 #ifdef DDB
2158         kdb_init();
2159         if (boothowto & RB_KDB)
2160                 Debugger("Boot flags requested debugger");
2161 #endif
2162
2163         finishidentcpu();       /* Final stage of CPU initialization */
2164         setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL,
2165             GSEL(GCODE_SEL, SEL_KPL));
2166         setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL,
2167             GSEL(GCODE_SEL, SEL_KPL));
2168         initializecpu();        /* Initialize CPU registers */
2169
2170         /* make an initial tss so cpu can get interrupt stack on syscall! */
2171         /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2172         PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2173             KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2174         PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2175         gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2176         private_tss = 0;
2177         PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2178         PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2179         PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2180         ltr(gsel_tss);
2181
2182         dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2183             dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2184         dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2185             dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2186         dblfault_tss.tss_cr3 = (int)IdlePTD;
2187         dblfault_tss.tss_eip = (int)dblfault_handler;
2188         dblfault_tss.tss_eflags = PSL_KERNEL;
2189         dblfault_tss.tss_ds = dblfault_tss.tss_es =
2190             dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2191         dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2192         dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2193         dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2194
2195         vm86_initialize();
2196         getmemsize(first);
2197         init_param2(physmem);
2198
2199         /* now running on new page tables, configured,and u/iom is accessible */
2200
2201         /* Map the message buffer. */
2202         for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
2203                 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
2204
2205         msgbufinit(msgbufp, MSGBUF_SIZE);
2206
2207         /* make a call gate to reenter kernel with */
2208         gdp = &ldt[LSYS5CALLS_SEL].gd;
2209
2210         x = (int) &IDTVEC(lcall_syscall);
2211         gdp->gd_looffset = x;
2212         gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2213         gdp->gd_stkcpy = 1;
2214         gdp->gd_type = SDT_SYS386CGT;
2215         gdp->gd_dpl = SEL_UPL;
2216         gdp->gd_p = 1;
2217         gdp->gd_hioffset = x >> 16;
2218
2219         /* XXX does this work? */
2220         ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2221         ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2222
2223         /* transfer to user mode */
2224
2225         _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
2226         _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
2227
2228         /* setup proc 0's pcb */
2229         thread0.td_pcb->pcb_flags = 0; /* XXXKSE */
2230         thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2231         thread0.td_pcb->pcb_ext = 0;
2232         thread0.td_frame = &proc0_tf;
2233 }
2234
2235 void
2236 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2237 {
2238 }
2239
2240 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2241 static void f00f_hack(void *unused);
2242 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2243
2244 static void
2245 f00f_hack(void *unused) {
2246         struct gate_descriptor *new_idt;
2247 #ifndef SMP
2248         struct region_descriptor r_idt;
2249 #endif
2250         vm_offset_t tmp;
2251
2252         if (!has_f00f_bug)
2253                 return;
2254
2255         GIANT_REQUIRED;
2256
2257         printf("Intel Pentium detected, installing workaround for F00F bug\n");
2258
2259         r_idt.rd_limit = sizeof(idt0) - 1;
2260
2261         tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
2262         if (tmp == 0)
2263                 panic("kmem_alloc returned 0");
2264         if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0)
2265                 panic("kmem_alloc returned non-page-aligned memory");
2266         /* Put the first seven entries in the lower page */
2267         new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8));
2268         bcopy(idt, new_idt, sizeof(idt0));
2269         r_idt.rd_base = (int)new_idt;
2270         lidt(&r_idt);
2271         idt = new_idt;
2272         if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2273                            VM_PROT_READ, FALSE) != KERN_SUCCESS)
2274                 panic("vm_map_protect failed");
2275         return;
2276 }
2277 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2278
2279 int
2280 ptrace_set_pc(struct thread *td, unsigned long addr)
2281 {
2282         td->td_frame->tf_eip = addr;
2283         return (0);
2284 }
2285
2286 int
2287 ptrace_single_step(struct thread *td)
2288 {
2289         td->td_frame->tf_eflags |= PSL_T;
2290         return (0);
2291 }
2292
2293 int
2294 fill_regs(struct thread *td, struct reg *regs)
2295 {
2296         struct pcb *pcb;
2297         struct trapframe *tp;
2298
2299         tp = td->td_frame;
2300         regs->r_fs = tp->tf_fs;
2301         regs->r_es = tp->tf_es;
2302         regs->r_ds = tp->tf_ds;
2303         regs->r_edi = tp->tf_edi;
2304         regs->r_esi = tp->tf_esi;
2305         regs->r_ebp = tp->tf_ebp;
2306         regs->r_ebx = tp->tf_ebx;
2307         regs->r_edx = tp->tf_edx;
2308         regs->r_ecx = tp->tf_ecx;
2309         regs->r_eax = tp->tf_eax;
2310         regs->r_eip = tp->tf_eip;
2311         regs->r_cs = tp->tf_cs;
2312         regs->r_eflags = tp->tf_eflags;
2313         regs->r_esp = tp->tf_esp;
2314         regs->r_ss = tp->tf_ss;
2315         pcb = td->td_pcb;
2316         regs->r_gs = pcb->pcb_gs;
2317         return (0);
2318 }
2319
2320 int
2321 set_regs(struct thread *td, struct reg *regs)
2322 {
2323         struct pcb *pcb;
2324         struct trapframe *tp;
2325
2326         tp = td->td_frame;
2327         if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2328             !CS_SECURE(regs->r_cs))
2329                 return (EINVAL);
2330         tp->tf_fs = regs->r_fs;
2331         tp->tf_es = regs->r_es;
2332         tp->tf_ds = regs->r_ds;
2333         tp->tf_edi = regs->r_edi;
2334         tp->tf_esi = regs->r_esi;
2335         tp->tf_ebp = regs->r_ebp;
2336         tp->tf_ebx = regs->r_ebx;
2337         tp->tf_edx = regs->r_edx;
2338         tp->tf_ecx = regs->r_ecx;
2339         tp->tf_eax = regs->r_eax;
2340         tp->tf_eip = regs->r_eip;
2341         tp->tf_cs = regs->r_cs;
2342         tp->tf_eflags = regs->r_eflags;
2343         tp->tf_esp = regs->r_esp;
2344         tp->tf_ss = regs->r_ss;
2345         pcb = td->td_pcb;
2346         pcb->pcb_gs = regs->r_gs;
2347         return (0);
2348 }
2349
2350 #ifdef CPU_ENABLE_SSE
2351 static void
2352 fill_fpregs_xmm(sv_xmm, sv_87)
2353         struct savexmm *sv_xmm;
2354         struct save87 *sv_87;
2355 {
2356         register struct env87 *penv_87 = &sv_87->sv_env;
2357         register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2358         int i;
2359
2360         bzero(sv_87, sizeof(*sv_87));
2361
2362         /* FPU control/status */
2363         penv_87->en_cw = penv_xmm->en_cw;
2364         penv_87->en_sw = penv_xmm->en_sw;
2365         penv_87->en_tw = penv_xmm->en_tw;
2366         penv_87->en_fip = penv_xmm->en_fip;
2367         penv_87->en_fcs = penv_xmm->en_fcs;
2368         penv_87->en_opcode = penv_xmm->en_opcode;
2369         penv_87->en_foo = penv_xmm->en_foo;
2370         penv_87->en_fos = penv_xmm->en_fos;
2371
2372         /* FPU registers */
2373         for (i = 0; i < 8; ++i)
2374                 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2375 }
2376
2377 static void
2378 set_fpregs_xmm(sv_87, sv_xmm)
2379         struct save87 *sv_87;
2380         struct savexmm *sv_xmm;
2381 {
2382         register struct env87 *penv_87 = &sv_87->sv_env;
2383         register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2384         int i;
2385
2386         /* FPU control/status */
2387         penv_xmm->en_cw = penv_87->en_cw;
2388         penv_xmm->en_sw = penv_87->en_sw;
2389         penv_xmm->en_tw = penv_87->en_tw;
2390         penv_xmm->en_fip = penv_87->en_fip;
2391         penv_xmm->en_fcs = penv_87->en_fcs;
2392         penv_xmm->en_opcode = penv_87->en_opcode;
2393         penv_xmm->en_foo = penv_87->en_foo;
2394         penv_xmm->en_fos = penv_87->en_fos;
2395
2396         /* FPU registers */
2397         for (i = 0; i < 8; ++i)
2398                 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2399 }
2400 #endif /* CPU_ENABLE_SSE */
2401
2402 int
2403 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2404 {
2405 #ifdef CPU_ENABLE_SSE
2406         if (cpu_fxsr) {
2407                 fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
2408                                                 (struct save87 *)fpregs);
2409                 return (0);
2410         }
2411 #endif /* CPU_ENABLE_SSE */
2412         bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
2413         return (0);
2414 }
2415
2416 int
2417 set_fpregs(struct thread *td, struct fpreg *fpregs)
2418 {
2419 #ifdef CPU_ENABLE_SSE
2420         if (cpu_fxsr) {
2421                 set_fpregs_xmm((struct save87 *)fpregs,
2422                                            &td->td_pcb->pcb_save.sv_xmm);
2423                 return (0);
2424         }
2425 #endif /* CPU_ENABLE_SSE */
2426         bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
2427         return (0);
2428 }
2429
2430 /*
2431  * Get machine context.
2432  */
2433 int
2434 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
2435 {
2436         struct trapframe *tp;
2437
2438         tp = td->td_frame;
2439
2440         PROC_LOCK(curthread->td_proc);
2441         mcp->mc_onstack = sigonstack(tp->tf_esp);
2442         PROC_UNLOCK(curthread->td_proc);
2443         mcp->mc_gs = td->td_pcb->pcb_gs;
2444         mcp->mc_fs = tp->tf_fs;
2445         mcp->mc_es = tp->tf_es;
2446         mcp->mc_ds = tp->tf_ds;
2447         mcp->mc_edi = tp->tf_edi;
2448         mcp->mc_esi = tp->tf_esi;
2449         mcp->mc_ebp = tp->tf_ebp;
2450         mcp->mc_isp = tp->tf_isp;
2451         mcp->mc_ebx = tp->tf_ebx;
2452         if (clear_ret != 0) {
2453                 mcp->mc_eax = 0;
2454                 mcp->mc_edx = 0;
2455         } else {
2456                 mcp->mc_eax = tp->tf_eax;
2457                 mcp->mc_edx = tp->tf_edx;
2458         }
2459         mcp->mc_ecx = tp->tf_ecx;
2460         mcp->mc_eip = tp->tf_eip;
2461         mcp->mc_cs = tp->tf_cs;
2462         mcp->mc_eflags = tp->tf_eflags;
2463         mcp->mc_esp = tp->tf_esp;
2464         mcp->mc_ss = tp->tf_ss;
2465         mcp->mc_len = sizeof(*mcp);
2466         get_fpcontext(td, mcp);
2467         return (0);
2468 }
2469
2470 /*
2471  * Set machine context.
2472  *
2473  * However, we don't set any but the user modifiable flags, and we won't
2474  * touch the cs selector.
2475  */
2476 int
2477 set_mcontext(struct thread *td, const mcontext_t *mcp)
2478 {
2479         struct trapframe *tp;
2480         int eflags, ret;
2481
2482         tp = td->td_frame;
2483         if (mcp->mc_len != sizeof(*mcp))
2484                 return (EINVAL);
2485         eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2486             (tp->tf_eflags & ~PSL_USERCHANGE);
2487         if ((ret = set_fpcontext(td, mcp)) == 0) {
2488                 tp->tf_fs = mcp->mc_fs;
2489                 tp->tf_es = mcp->mc_es;
2490                 tp->tf_ds = mcp->mc_ds;
2491                 tp->tf_edi = mcp->mc_edi;
2492                 tp->tf_esi = mcp->mc_esi;
2493                 tp->tf_ebp = mcp->mc_ebp;
2494                 tp->tf_ebx = mcp->mc_ebx;
2495                 tp->tf_edx = mcp->mc_edx;
2496                 tp->tf_ecx = mcp->mc_ecx;
2497                 tp->tf_eax = mcp->mc_eax;
2498                 tp->tf_eip = mcp->mc_eip;
2499                 tp->tf_eflags = eflags;
2500                 tp->tf_esp = mcp->mc_esp;
2501                 tp->tf_ss = mcp->mc_ss;
2502                 td->td_pcb->pcb_gs = mcp->mc_gs;
2503                 ret = 0;
2504         }
2505         return (ret);
2506 }
2507
2508 static void
2509 get_fpcontext(struct thread *td, mcontext_t *mcp)
2510 {
2511 #ifndef DEV_NPX
2512         mcp->mc_fpformat = _MC_FPFMT_NODEV;
2513         mcp->mc_ownedfp = _MC_FPOWNED_NONE;
2514 #else
2515         union savefpu *addr;
2516
2517         /*
2518          * XXX mc_fpstate might be misaligned, since its declaration is not
2519          * unportabilized using __attribute__((aligned(16))) like the
2520          * declaration of struct savemm, and anyway, alignment doesn't work
2521          * for auto variables since we don't use gcc's pessimal stack
2522          * alignment.  Work around this by abusing the spare fields after
2523          * mcp->mc_fpstate.
2524          *
2525          * XXX unpessimize most cases by only aligning when fxsave might be
2526          * called, although this requires knowing too much about
2527          * npxgetregs()'s internals.
2528          */
2529         addr = (union savefpu *)&mcp->mc_fpstate;
2530         if (td == PCPU_GET(fpcurthread) &&
2531 #ifdef CPU_ENABLE_SSE
2532             cpu_fxsr &&
2533 #endif
2534             ((uintptr_t)(void *)addr & 0xF)) {
2535                 do
2536                         addr = (void *)((char *)addr + 4);
2537                 while ((uintptr_t)(void *)addr & 0xF);
2538         }
2539         mcp->mc_ownedfp = npxgetregs(td, addr);
2540         if (addr != (union savefpu *)&mcp->mc_fpstate) {
2541                 bcopy(addr, &mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
2542                 bzero(&mcp->mc_spare2, sizeof(mcp->mc_spare2));
2543         }
2544         mcp->mc_fpformat = npxformat();
2545 #endif
2546 }
2547
2548 static int
2549 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2550 {
2551         union savefpu *addr;
2552
2553         if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2554                 return (0);
2555         else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2556             mcp->mc_fpformat != _MC_FPFMT_XMM)
2557                 return (EINVAL);
2558         else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2559                 /* We don't care what state is left in the FPU or PCB. */
2560                 fpstate_drop(td);
2561         else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2562             mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2563                 /* XXX align as above. */
2564                 addr = (union savefpu *)&mcp->mc_fpstate;
2565                 if (td == PCPU_GET(fpcurthread) &&
2566 #ifdef CPU_ENABLE_SSE
2567                     cpu_fxsr &&
2568 #endif
2569                     ((uintptr_t)(void *)addr & 0xF)) {
2570                         do
2571                                 addr = (void *)((char *)addr + 4);
2572                         while ((uintptr_t)(void *)addr & 0xF);
2573                         bcopy(&mcp->mc_fpstate, addr, sizeof(mcp->mc_fpstate));
2574                 }
2575 #ifdef DEV_NPX
2576                 /*
2577                  * XXX we violate the dubious requirement that npxsetregs()
2578                  * be called with interrupts disabled.
2579                  */
2580                 npxsetregs(td, addr);
2581 #endif
2582                 /*
2583                  * Don't bother putting things back where they were in the
2584                  * misaligned case, since we know that the caller won't use
2585                  * them again.
2586                  */
2587         } else
2588                 return (EINVAL);
2589         return (0);
2590 }
2591
2592 static void
2593 fpstate_drop(struct thread *td)
2594 {
2595         register_t s;
2596
2597         s = intr_disable();
2598 #ifdef DEV_NPX
2599         if (PCPU_GET(fpcurthread) == td)
2600                 npxdrop();
2601 #endif
2602         /*
2603          * XXX force a full drop of the npx.  The above only drops it if we
2604          * owned it.  npxgetregs() has the same bug in the !cpu_fxsr case.
2605          *
2606          * XXX I don't much like npxgetregs()'s semantics of doing a full
2607          * drop.  Dropping only to the pcb matches fnsave's behaviour.
2608          * We only need to drop to !PCB_INITDONE in sendsig().  But
2609          * sendsig() is the only caller of npxgetregs()... perhaps we just
2610          * have too many layers.
2611          */
2612         curthread->td_pcb->pcb_flags &= ~PCB_NPXINITDONE;
2613         intr_restore(s);
2614 }
2615
2616 int
2617 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2618 {
2619         struct pcb *pcb;
2620
2621         if (td == NULL) {
2622                 dbregs->dr[0] = rdr0();
2623                 dbregs->dr[1] = rdr1();
2624                 dbregs->dr[2] = rdr2();
2625                 dbregs->dr[3] = rdr3();
2626                 dbregs->dr[4] = rdr4();
2627                 dbregs->dr[5] = rdr5();
2628                 dbregs->dr[6] = rdr6();
2629                 dbregs->dr[7] = rdr7();
2630         } else {
2631                 pcb = td->td_pcb;
2632                 dbregs->dr[0] = pcb->pcb_dr0;
2633                 dbregs->dr[1] = pcb->pcb_dr1;
2634                 dbregs->dr[2] = pcb->pcb_dr2;
2635                 dbregs->dr[3] = pcb->pcb_dr3;
2636                 dbregs->dr[4] = 0;
2637                 dbregs->dr[5] = 0;
2638                 dbregs->dr[6] = pcb->pcb_dr6;
2639                 dbregs->dr[7] = pcb->pcb_dr7;
2640         }
2641         return (0);
2642 }
2643
2644 int
2645 set_dbregs(struct thread *td, struct dbreg *dbregs)
2646 {
2647         struct pcb *pcb;
2648         int i;
2649         u_int32_t mask1, mask2;
2650
2651         if (td == NULL) {
2652                 load_dr0(dbregs->dr[0]);
2653                 load_dr1(dbregs->dr[1]);
2654                 load_dr2(dbregs->dr[2]);
2655                 load_dr3(dbregs->dr[3]);
2656                 load_dr4(dbregs->dr[4]);
2657                 load_dr5(dbregs->dr[5]);
2658                 load_dr6(dbregs->dr[6]);
2659                 load_dr7(dbregs->dr[7]);
2660         } else {
2661                 /*
2662                  * Don't let an illegal value for dr7 get set.  Specifically,
2663                  * check for undefined settings.  Setting these bit patterns
2664                  * result in undefined behaviour and can lead to an unexpected
2665                  * TRCTRAP.
2666                  */
2667                 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8; 
2668                      i++, mask1 <<= 2, mask2 <<= 2)
2669                         if ((dbregs->dr[7] & mask1) == mask2)
2670                                 return (EINVAL);
2671                 
2672                 pcb = td->td_pcb;
2673                 
2674                 /*
2675                  * Don't let a process set a breakpoint that is not within the
2676                  * process's address space.  If a process could do this, it
2677                  * could halt the system by setting a breakpoint in the kernel
2678                  * (if ddb was enabled).  Thus, we need to check to make sure
2679                  * that no breakpoints are being enabled for addresses outside
2680                  * process's address space, unless, perhaps, we were called by
2681                  * uid 0.
2682                  *
2683                  * XXX - what about when the watched area of the user's
2684                  * address space is written into from within the kernel
2685                  * ... wouldn't that still cause a breakpoint to be generated
2686                  * from within kernel mode?
2687                  */
2688
2689                 if (suser(td) != 0) {
2690                         if (dbregs->dr[7] & 0x3) {
2691                                 /* dr0 is enabled */
2692                                 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2693                                         return (EINVAL);
2694                         }
2695                         
2696                         if (dbregs->dr[7] & (0x3<<2)) {
2697                                 /* dr1 is enabled */
2698                                 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2699                                         return (EINVAL);
2700                         }
2701                         
2702                         if (dbregs->dr[7] & (0x3<<4)) {
2703                                 /* dr2 is enabled */
2704                                 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2705                                         return (EINVAL);
2706                         }
2707                         
2708                         if (dbregs->dr[7] & (0x3<<6)) {
2709                                 /* dr3 is enabled */
2710                                 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2711                                         return (EINVAL);
2712                         }
2713                 }
2714
2715                 pcb->pcb_dr0 = dbregs->dr[0];
2716                 pcb->pcb_dr1 = dbregs->dr[1];
2717                 pcb->pcb_dr2 = dbregs->dr[2];
2718                 pcb->pcb_dr3 = dbregs->dr[3];
2719                 pcb->pcb_dr6 = dbregs->dr[6];
2720                 pcb->pcb_dr7 = dbregs->dr[7];
2721
2722                 pcb->pcb_flags |= PCB_DBREGS;
2723         }
2724
2725         return (0);
2726 }
2727
2728 /*
2729  * Return > 0 if a hardware breakpoint has been hit, and the
2730  * breakpoint was in user space.  Return 0, otherwise.
2731  */
2732 int
2733 user_dbreg_trap(void)
2734 {
2735         u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2736         u_int32_t bp;       /* breakpoint bits extracted from dr6 */
2737         int nbp;            /* number of breakpoints that triggered */
2738         caddr_t addr[4];    /* breakpoint addresses */
2739         int i;
2740         
2741         dr7 = rdr7();
2742         if ((dr7 & 0x000000ff) == 0) {
2743                 /*
2744                  * all GE and LE bits in the dr7 register are zero,
2745                  * thus the trap couldn't have been caused by the
2746                  * hardware debug registers
2747                  */
2748                 return 0;
2749         }
2750
2751         nbp = 0;
2752         dr6 = rdr6();
2753         bp = dr6 & 0x0000000f;
2754
2755         if (!bp) {
2756                 /*
2757                  * None of the breakpoint bits are set meaning this
2758                  * trap was not caused by any of the debug registers
2759                  */
2760                 return 0;
2761         }
2762
2763         /*
2764          * at least one of the breakpoints were hit, check to see
2765          * which ones and if any of them are user space addresses
2766          */
2767
2768         if (bp & 0x01) {
2769                 addr[nbp++] = (caddr_t)rdr0();
2770         }
2771         if (bp & 0x02) {
2772                 addr[nbp++] = (caddr_t)rdr1();
2773         }
2774         if (bp & 0x04) {
2775                 addr[nbp++] = (caddr_t)rdr2();
2776         }
2777         if (bp & 0x08) {
2778                 addr[nbp++] = (caddr_t)rdr3();
2779         }
2780
2781         for (i=0; i<nbp; i++) {
2782                 if (addr[i] <
2783                     (caddr_t)VM_MAXUSER_ADDRESS) {
2784                         /*
2785                          * addr[i] is in user space
2786                          */
2787                         return nbp;
2788                 }
2789         }
2790
2791         /*
2792          * None of the breakpoints are in user space.
2793          */
2794         return 0;
2795 }
2796
2797
2798 #ifndef DDB
2799 void
2800 Debugger(const char *msg)
2801 {
2802         printf("Debugger(\"%s\") called.\n", msg);
2803 }
2804 #endif /* no DDB */
2805
2806 #ifdef DDB
2807
2808 /*
2809  * Provide inb() and outb() as functions.  They are normally only
2810  * available as macros calling inlined functions, thus cannot be
2811  * called inside DDB.
2812  *
2813  * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2814  */
2815
2816 #undef inb
2817 #undef outb
2818
2819 /* silence compiler warnings */
2820 u_char inb(u_int);
2821 void outb(u_int, u_char);
2822
2823 u_char
2824 inb(u_int port)
2825 {
2826         u_char  data;
2827         /*
2828          * We use %%dx and not %1 here because i/o is done at %dx and not at
2829          * %edx, while gcc generates inferior code (movw instead of movl)
2830          * if we tell it to load (u_short) port.
2831          */
2832         __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2833         return (data);
2834 }
2835
2836 void
2837 outb(u_int port, u_char data)
2838 {
2839         u_char  al;
2840         /*
2841          * Use an unnecessary assignment to help gcc's register allocator.
2842          * This make a large difference for gcc-1.40 and a tiny difference
2843          * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
2844          * best results.  gcc-2.6.0 can't handle this.
2845          */
2846         al = data;
2847         __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
2848 }
2849
2850 #endif /* DDB */