machdep.c revision 83366
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: head/sys/amd64/amd64/machdep.c 83366 2001-09-12 08:38:13Z julian $
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_kstack_pages.h"
53/* #include "opt_userconfig.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/kernel.h>
60#include <sys/ktr.h>
61#include <sys/linker.h>
62#include <sys/lock.h>
63#include <sys/malloc.h>
64#include <sys/mutex.h>
65#include <sys/pcpu.h>
66#include <sys/proc.h>
67#include <sys/bio.h>
68#include <sys/buf.h>
69#include <sys/reboot.h>
70#include <sys/smp.h>
71#include <sys/callout.h>
72#include <sys/msgbuf.h>
73#include <sys/sysent.h>
74#include <sys/sysctl.h>
75#include <sys/vmmeter.h>
76#include <sys/bus.h>
77#include <sys/eventhandler.h>
78
79#include <vm/vm.h>
80#include <vm/vm_param.h>
81#include <sys/lock.h>
82#include <vm/vm_kern.h>
83#include <vm/vm_object.h>
84#include <vm/vm_page.h>
85#include <vm/vm_map.h>
86#include <vm/vm_pager.h>
87#include <vm/vm_extern.h>
88
89#include <sys/user.h>
90#include <sys/exec.h>
91#include <sys/cons.h>
92
93#include <ddb/ddb.h>
94
95#include <net/netisr.h>
96
97#include <machine/cpu.h>
98#include <machine/cputypes.h>
99#include <machine/reg.h>
100#include <machine/clock.h>
101#include <machine/specialreg.h>
102#include <machine/bootinfo.h>
103#include <machine/md_var.h>
104#include <machine/pc/bios.h>
105#include <machine/pcb_ext.h>		/* pcb.h included via sys/user.h */
106#include <machine/globals.h>
107#ifdef PERFMON
108#include <machine/perfmon.h>
109#endif
110#ifdef SMP
111#include <machine/privatespace.h>
112#endif
113
114#include <i386/isa/icu.h>
115#include <i386/isa/intr_machdep.h>
116#include <isa/rtc.h>
117#include <machine/vm86.h>
118#include <sys/ptrace.h>
119#include <machine/sigframe.h>
120
121extern void init386 __P((int first));
122extern void dblfault_handler __P((void));
123
124extern void printcpuinfo(void);	/* XXX header file */
125extern void earlysetcpuclass(void);	/* same header file */
126extern void finishidentcpu(void);
127extern void panicifcpuunsupported(void);
128extern void initializecpu(void);
129
130#define	CS_SECURE(cs)		(ISPL(cs) == SEL_UPL)
131#define	EFL_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
132
133static void cpu_startup __P((void *));
134#ifdef CPU_ENABLE_SSE
135static void set_fpregs_xmm __P((struct save87 *, struct savexmm *));
136static void fill_fpregs_xmm __P((struct savexmm *, struct save87 *));
137#endif /* CPU_ENABLE_SSE */
138SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
139
140int	_udatasel, _ucodesel;
141u_int	atdevbase;
142
143#if defined(SWTCH_OPTIM_STATS)
144extern int swtch_optim_stats;
145SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
146	CTLFLAG_RD, &swtch_optim_stats, 0, "");
147SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
148	CTLFLAG_RD, &tlb_flush_count, 0, "");
149#endif
150
151#ifdef PC98
152static int	ispc98 = 1;
153#else
154static int	ispc98 = 0;
155#endif
156SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
157
158int physmem = 0;
159int cold = 1;
160
161#ifdef COMPAT_43
162static void osendsig __P((sig_t catcher, int sig, sigset_t *mask, u_long code));
163#endif
164
165static int
166sysctl_hw_physmem(SYSCTL_HANDLER_ARGS)
167{
168	int error = sysctl_handle_int(oidp, 0, ctob(physmem), req);
169	return (error);
170}
171
172SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
173	0, 0, sysctl_hw_physmem, "IU", "");
174
175static int
176sysctl_hw_usermem(SYSCTL_HANDLER_ARGS)
177{
178	int error = sysctl_handle_int(oidp, 0,
179		ctob(physmem - cnt.v_wire_count), req);
180	return (error);
181}
182
183SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
184	0, 0, sysctl_hw_usermem, "IU", "");
185
186static int
187sysctl_hw_availpages(SYSCTL_HANDLER_ARGS)
188{
189	int error = sysctl_handle_int(oidp, 0,
190		i386_btop(avail_end - avail_start), req);
191	return (error);
192}
193
194SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD,
195	0, 0, sysctl_hw_availpages, "I", "");
196
197int Maxmem = 0;
198long dumplo;
199
200vm_offset_t phys_avail[10];
201
202/* must be 2 less so 0 0 can signal end of chunks */
203#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
204
205struct kva_md_info kmi;
206
207static struct trapframe proc0_tf;
208#ifndef SMP
209static struct globaldata __globaldata;
210#endif
211
212struct mtx sched_lock;
213struct mtx Giant;
214
215static void
216cpu_startup(dummy)
217	void *dummy;
218{
219	/*
220	 * Good {morning,afternoon,evening,night}.
221	 */
222	earlysetcpuclass();
223	startrtclock();
224	printcpuinfo();
225	panicifcpuunsupported();
226#ifdef PERFMON
227	perfmon_init();
228#endif
229	printf("real memory  = %u (%uK bytes)\n", ptoa(Maxmem),
230	    ptoa(Maxmem) / 1024);
231	/*
232	 * Display any holes after the first chunk of extended memory.
233	 */
234	if (bootverbose) {
235		int indx;
236
237		printf("Physical memory chunk(s):\n");
238		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
239			unsigned int size1;
240
241			size1 = phys_avail[indx + 1] - phys_avail[indx];
242			printf("0x%08x - 0x%08x, %u bytes (%u pages)\n",
243			    phys_avail[indx], phys_avail[indx + 1] - 1, size1,
244			    size1 / PAGE_SIZE);
245		}
246	}
247
248	vm_ksubmap_init(&kmi);
249
250#if defined(USERCONFIG)
251	userconfig();
252	cninit();		/* the preferred console may have changed */
253#endif
254
255	printf("avail memory = %u (%uK bytes)\n", ptoa(cnt.v_free_count),
256	    ptoa(cnt.v_free_count) / 1024);
257
258	/*
259	 * Set up buffers, so they can be used to read disk labels.
260	 */
261	bufinit();
262	vm_pager_bufferinit();
263
264	globaldata_register(GLOBALDATA);
265#ifndef SMP
266	/* For SMP, we delay the cpu_setregs() until after SMP startup. */
267	cpu_setregs();
268#endif
269}
270
271/*
272 * Send an interrupt to process.
273 *
274 * Stack is set up to allow sigcode stored
275 * at top to call routine, followed by kcall
276 * to sigreturn routine below.  After sigreturn
277 * resets the signal mask, the stack, and the
278 * frame pointer, it returns to the user
279 * specified pc, psl.
280 */
281#ifdef COMPAT_43
282static void
283osendsig(catcher, sig, mask, code)
284	sig_t catcher;
285	int sig;
286	sigset_t *mask;
287	u_long code;
288{
289	struct osigframe sf;
290	struct osigframe *fp;
291	struct proc *p;
292	struct thread *td;
293	struct sigacts *psp;
294	struct trapframe *regs;
295	int oonstack;
296
297	td = curthread;
298	p = td->td_proc;
299	PROC_LOCK_ASSERT(p, MA_OWNED);
300	psp = p->p_sigacts;
301	regs = td->td_frame;
302	oonstack = sigonstack(regs->tf_esp);
303
304	/* Allocate and validate space for the signal handler context. */
305	if ((p->p_flag & P_ALTSTACK) && !oonstack &&
306	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
307		fp = (struct osigframe *)(p->p_sigstk.ss_sp +
308		    p->p_sigstk.ss_size - sizeof(struct osigframe));
309#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
310		p->p_sigstk.ss_flags |= SS_ONSTACK;
311#endif
312	} else
313		fp = (struct osigframe *)regs->tf_esp - 1;
314	PROC_UNLOCK(p);
315
316	/*
317	 * grow_stack() will return 0 if *fp does not fit inside the stack
318	 * and the stack can not be grown.
319	 * useracc() will return FALSE if access is denied.
320	 */
321	if (grow_stack(p, (int)fp) == 0 ||
322	    !useracc((caddr_t)fp, sizeof(*fp), VM_PROT_WRITE)) {
323		/*
324		 * Process has trashed its stack; give it an illegal
325		 * instruction to halt it in its tracks.
326		 */
327		PROC_LOCK(p);
328		SIGACTION(p, SIGILL) = SIG_DFL;
329		SIGDELSET(p->p_sigignore, SIGILL);
330		SIGDELSET(p->p_sigcatch, SIGILL);
331		SIGDELSET(p->p_sigmask, SIGILL);
332		psignal(p, SIGILL);
333		return;
334	}
335
336	/* Translate the signal if appropriate. */
337	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
338		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
339
340	/* Build the argument list for the signal handler. */
341	sf.sf_signum = sig;
342	sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
343	PROC_LOCK(p);
344	if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
345		/* Signal handler installed with SA_SIGINFO. */
346		sf.sf_arg2 = (register_t)&fp->sf_siginfo;
347		sf.sf_siginfo.si_signo = sig;
348		sf.sf_siginfo.si_code = code;
349		sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
350	} else {
351		/* Old FreeBSD-style arguments. */
352		sf.sf_arg2 = code;
353		sf.sf_addr = regs->tf_err;
354		sf.sf_ahu.sf_handler = catcher;
355	}
356	PROC_UNLOCK(p);
357
358	/* Save most if not all of trap frame. */
359	sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
360	sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
361	sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
362	sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
363	sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
364	sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
365	sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
366	sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
367	sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
368	sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
369	sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
370	sf.sf_siginfo.si_sc.sc_gs = rgs();
371	sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
372
373	/* Build the signal context to be used by osigreturn(). */
374	sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
375	SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
376	sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
377	sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
378	sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
379	sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
380	sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
381	sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
382
383	/*
384	 * If we're a vm86 process, we want to save the segment registers.
385	 * We also change eflags to be our emulated eflags, not the actual
386	 * eflags.
387	 */
388	if (regs->tf_eflags & PSL_VM) {
389		/* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
390		struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
391		struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
392
393		sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
394		sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
395		sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
396		sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
397
398		if (vm86->vm86_has_vme == 0)
399			sf.sf_siginfo.si_sc.sc_ps =
400			    (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
401			    (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
402
403		/* See sendsig() for comments. */
404		tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_T | PSL_VIF | PSL_VIP);
405	}
406
407	/* Copy the sigframe out to the user's stack. */
408	if (copyout(&sf, fp, sizeof(*fp)) != 0) {
409		/*
410		 * Something is wrong with the stack pointer.
411		 * ...Kill the process.
412		 */
413		PROC_LOCK(p);
414		sigexit(td, SIGILL);
415		/* NOTREACHED */
416	}
417
418	regs->tf_esp = (int)fp;
419	regs->tf_eip = PS_STRINGS - szosigcode;
420	regs->tf_cs = _ucodesel;
421	regs->tf_ds = _udatasel;
422	regs->tf_es = _udatasel;
423	regs->tf_fs = _udatasel;
424	load_gs(_udatasel);
425	regs->tf_ss = _udatasel;
426	PROC_LOCK(p);
427}
428#endif
429
430void
431sendsig(catcher, sig, mask, code)
432	sig_t catcher;
433	int sig;
434	sigset_t *mask;
435	u_long code;
436{
437	struct sigframe sf;
438	struct proc *p;
439	struct thread *td;
440	struct sigacts *psp;
441	struct trapframe *regs;
442	struct sigframe *sfp;
443	int oonstack;
444
445	td = curthread;
446	p = td->td_proc;
447	PROC_LOCK_ASSERT(p, MA_OWNED);
448	psp = p->p_sigacts;
449#ifdef COMPAT_43
450	if (SIGISMEMBER(psp->ps_osigset, sig)) {
451		osendsig(catcher, sig, mask, code);
452		return;
453	}
454#endif
455	regs = td->td_frame;
456	oonstack = sigonstack(regs->tf_esp);
457
458	/* Save user context. */
459	bzero(&sf, sizeof(sf));
460	sf.sf_uc.uc_sigmask = *mask;
461	sf.sf_uc.uc_stack = p->p_sigstk;
462	sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
463	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
464	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
465	sf.sf_uc.uc_mcontext.mc_gs = rgs();
466	bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
467
468	/* Allocate and validate space for the signal handler context. */
469	if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
470	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
471		sfp = (struct sigframe *)(p->p_sigstk.ss_sp +
472		    p->p_sigstk.ss_size - sizeof(struct sigframe));
473#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
474		p->p_sigstk.ss_flags |= SS_ONSTACK;
475#endif
476	} else
477		sfp = (struct sigframe *)regs->tf_esp - 1;
478	PROC_UNLOCK(p);
479
480	/*
481	 * grow_stack() will return 0 if *sfp does not fit inside the stack
482	 * and the stack can not be grown.
483	 * useracc() will return FALSE if access is denied.
484	 */
485	if (grow_stack(p, (int)sfp) == 0 ||
486	    !useracc((caddr_t)sfp, sizeof(*sfp), VM_PROT_WRITE)) {
487		/*
488		 * Process has trashed its stack; give it an illegal
489		 * instruction to halt it in its tracks.
490		 */
491#ifdef DEBUG
492		printf("process %d has trashed its stack\n", p->p_pid);
493#endif
494		PROC_LOCK(p);
495		SIGACTION(p, SIGILL) = SIG_DFL;
496		SIGDELSET(p->p_sigignore, SIGILL);
497		SIGDELSET(p->p_sigcatch, SIGILL);
498		SIGDELSET(p->p_sigmask, SIGILL);
499		psignal(p, SIGILL);
500		return;
501	}
502
503	/* Translate the signal if appropriate. */
504	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
505		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
506
507	/* Build the argument list for the signal handler. */
508	sf.sf_signum = sig;
509	sf.sf_ucontext = (register_t)&sfp->sf_uc;
510	PROC_LOCK(p);
511	if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
512		/* Signal handler installed with SA_SIGINFO. */
513		sf.sf_siginfo = (register_t)&sfp->sf_si;
514		sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
515
516		/* Fill siginfo structure. */
517		sf.sf_si.si_signo = sig;
518		sf.sf_si.si_code = code;
519		sf.sf_si.si_addr = (void *)regs->tf_err;
520	} else {
521		/* Old FreeBSD-style arguments. */
522		sf.sf_siginfo = code;
523		sf.sf_addr = regs->tf_err;
524		sf.sf_ahu.sf_handler = catcher;
525	}
526	PROC_UNLOCK(p);
527
528	/*
529	 * If we're a vm86 process, we want to save the segment registers.
530	 * We also change eflags to be our emulated eflags, not the actual
531	 * eflags.
532	 */
533	if (regs->tf_eflags & PSL_VM) {
534		struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
535		struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
536
537		sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
538		sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
539		sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
540		sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
541
542		if (vm86->vm86_has_vme == 0)
543			sf.sf_uc.uc_mcontext.mc_eflags =
544			    (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
545			    (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
546
547		/*
548		 * We should never have PSL_T set when returning from vm86
549		 * mode.  It may be set here if we deliver a signal before
550		 * getting to vm86 mode, so turn it off.
551		 *
552		 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
553		 * syscalls made by the signal handler.  This just avoids
554		 * wasting time for our lazy fixup of such faults.  PSL_NT
555		 * does nothing in vm86 mode, but vm86 programs can set it
556		 * almost legitimately in probes for old cpu types.
557		 */
558		tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_T | PSL_VIF | PSL_VIP);
559	}
560
561	/* Copy the sigframe out to the user's stack. */
562	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
563		/*
564		 * Something is wrong with the stack pointer.
565		 * ...Kill the process.
566		 */
567		PROC_LOCK(p);
568		sigexit(td, SIGILL);
569		/* NOTREACHED */
570	}
571
572	regs->tf_esp = (int)sfp;
573	regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
574	regs->tf_cs = _ucodesel;
575	regs->tf_ds = _udatasel;
576	regs->tf_es = _udatasel;
577	regs->tf_fs = _udatasel;
578	regs->tf_ss = _udatasel;
579	PROC_LOCK(p);
580}
581
582/*
583 * System call to cleanup state after a signal
584 * has been taken.  Reset signal mask and
585 * stack state from context left by sendsig (above).
586 * Return to previous pc and psl as specified by
587 * context left by sendsig. Check carefully to
588 * make sure that the user has not modified the
589 * state to gain improper privileges.
590 */
591#ifdef COMPAT_43
592int
593osigreturn(td, uap)
594	struct thread *td;
595	struct osigreturn_args /* {
596		struct osigcontext *sigcntxp;
597	} */ *uap;
598{
599	struct trapframe *regs;
600	struct osigcontext *scp;
601	struct proc *p = td->td_proc;
602	int eflags;
603
604	regs = td->td_frame;
605	scp = uap->sigcntxp;
606	if (!useracc((caddr_t)scp, sizeof(*scp), VM_PROT_READ))
607		return (EFAULT);
608	eflags = scp->sc_ps;
609	if (eflags & PSL_VM) {
610		struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
611		struct vm86_kernel *vm86;
612
613		/*
614		 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
615		 * set up the vm86 area, and we can't enter vm86 mode.
616		 */
617		if (td->td_pcb->pcb_ext == 0)
618			return (EINVAL);
619		vm86 = &td->td_pcb->pcb_ext->ext_vm86;
620		if (vm86->vm86_inited == 0)
621			return (EINVAL);
622
623		/* Go back to user mode if both flags are set. */
624		if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
625			trapsignal(p, SIGBUS, 0);
626
627		if (vm86->vm86_has_vme) {
628			eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
629			    (eflags & VME_USERCHANGE) | PSL_VM;
630		} else {
631			vm86->vm86_eflags = eflags;	/* save VIF, VIP */
632			eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
633			    (eflags & VM_USERCHANGE) | PSL_VM;
634		}
635		tf->tf_vm86_ds = scp->sc_ds;
636		tf->tf_vm86_es = scp->sc_es;
637		tf->tf_vm86_fs = scp->sc_fs;
638		tf->tf_vm86_gs = scp->sc_gs;
639		tf->tf_ds = _udatasel;
640		tf->tf_es = _udatasel;
641		tf->tf_fs = _udatasel;
642	} else {
643		/*
644		 * Don't allow users to change privileged or reserved flags.
645		 */
646		/*
647		 * XXX do allow users to change the privileged flag PSL_RF.
648		 * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
649		 * should sometimes set it there too.  tf_eflags is kept in
650		 * the signal context during signal handling and there is no
651		 * other place to remember it, so the PSL_RF bit may be
652		 * corrupted by the signal handler without us knowing.
653		 * Corruption of the PSL_RF bit at worst causes one more or
654		 * one less debugger trap, so allowing it is fairly harmless.
655		 */
656		if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
657	    		return (EINVAL);
658		}
659
660		/*
661		 * Don't allow users to load a valid privileged %cs.  Let the
662		 * hardware check for invalid selectors, excess privilege in
663		 * other selectors, invalid %eip's and invalid %esp's.
664		 */
665		if (!CS_SECURE(scp->sc_cs)) {
666			trapsignal(p, SIGBUS, T_PROTFLT);
667			return (EINVAL);
668		}
669		regs->tf_ds = scp->sc_ds;
670		regs->tf_es = scp->sc_es;
671		regs->tf_fs = scp->sc_fs;
672	}
673
674	/* Restore remaining registers. */
675	regs->tf_eax = scp->sc_eax;
676	regs->tf_ebx = scp->sc_ebx;
677	regs->tf_ecx = scp->sc_ecx;
678	regs->tf_edx = scp->sc_edx;
679	regs->tf_esi = scp->sc_esi;
680	regs->tf_edi = scp->sc_edi;
681	regs->tf_cs = scp->sc_cs;
682	regs->tf_ss = scp->sc_ss;
683	regs->tf_isp = scp->sc_isp;
684
685	PROC_LOCK(p);
686#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
687	if (scp->sc_onstack & 1)
688		p->p_sigstk.ss_flags |= SS_ONSTACK;
689	else
690		p->p_sigstk.ss_flags &= ~SS_ONSTACK;
691#endif
692
693	SIGSETOLD(p->p_sigmask, scp->sc_mask);
694	SIG_CANTMASK(p->p_sigmask);
695	PROC_UNLOCK(p);
696	regs->tf_ebp = scp->sc_fp;
697	regs->tf_esp = scp->sc_sp;
698	regs->tf_eip = scp->sc_pc;
699	regs->tf_eflags = eflags;
700	return (EJUSTRETURN);
701}
702#endif
703
704int
705sigreturn(td, uap)
706	struct thread *td;
707	struct sigreturn_args /* {
708		ucontext_t *sigcntxp;
709	} */ *uap;
710{
711	struct proc *p = td->td_proc;
712	struct trapframe *regs;
713	ucontext_t *ucp;
714	int cs, eflags;
715
716	ucp = uap->sigcntxp;
717#ifdef COMPAT_43
718	if (!useracc((caddr_t)ucp, sizeof(struct osigcontext), VM_PROT_READ))
719		return (EFAULT);
720	if (((struct osigcontext *)ucp)->sc_trapno == 0x01d516)
721		return (osigreturn(td, (struct osigreturn_args *)uap));
722	/*
723	 * Since ucp is not an osigcontext but a ucontext_t, we have to
724	 * check again if all of it is accessible.  A ucontext_t is
725	 * much larger, so instead of just checking for the pointer
726	 * being valid for the size of an osigcontext, now check for
727	 * it being valid for a whole, new-style ucontext_t.
728	 */
729#endif
730	if (!useracc((caddr_t)ucp, sizeof(*ucp), VM_PROT_READ))
731		return (EFAULT);
732
733	regs = td->td_frame;
734	eflags = ucp->uc_mcontext.mc_eflags;
735	if (eflags & PSL_VM) {
736		struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
737		struct vm86_kernel *vm86;
738
739		/*
740		 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
741		 * set up the vm86 area, and we can't enter vm86 mode.
742		 */
743		if (td->td_pcb->pcb_ext == 0)
744			return (EINVAL);
745		vm86 = &td->td_pcb->pcb_ext->ext_vm86;
746		if (vm86->vm86_inited == 0)
747			return (EINVAL);
748
749		/* Go back to user mode if both flags are set. */
750		if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
751			trapsignal(p, SIGBUS, 0);
752
753		if (vm86->vm86_has_vme) {
754			eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
755			    (eflags & VME_USERCHANGE) | PSL_VM;
756		} else {
757			vm86->vm86_eflags = eflags;	/* save VIF, VIP */
758			eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
759			    (eflags & VM_USERCHANGE) | PSL_VM;
760		}
761		bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
762		tf->tf_eflags = eflags;
763		tf->tf_vm86_ds = tf->tf_ds;
764		tf->tf_vm86_es = tf->tf_es;
765		tf->tf_vm86_fs = tf->tf_fs;
766		tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
767		tf->tf_ds = _udatasel;
768		tf->tf_es = _udatasel;
769		tf->tf_fs = _udatasel;
770	} else {
771		/*
772		 * Don't allow users to change privileged or reserved flags.
773		 */
774		/*
775		 * XXX do allow users to change the privileged flag PSL_RF.
776		 * The cpu sets PSL_RF in tf_eflags for faults.  Debuggers
777		 * should sometimes set it there too.  tf_eflags is kept in
778		 * the signal context during signal handling and there is no
779		 * other place to remember it, so the PSL_RF bit may be
780		 * corrupted by the signal handler without us knowing.
781		 * Corruption of the PSL_RF bit at worst causes one more or
782		 * one less debugger trap, so allowing it is fairly harmless.
783		 */
784		if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
785			printf("sigreturn: eflags = 0x%x\n", eflags);
786	    		return (EINVAL);
787		}
788
789		/*
790		 * Don't allow users to load a valid privileged %cs.  Let the
791		 * hardware check for invalid selectors, excess privilege in
792		 * other selectors, invalid %eip's and invalid %esp's.
793		 */
794		cs = ucp->uc_mcontext.mc_cs;
795		if (!CS_SECURE(cs)) {
796			printf("sigreturn: cs = 0x%x\n", cs);
797			trapsignal(p, SIGBUS, T_PROTFLT);
798			return (EINVAL);
799		}
800
801		bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
802	}
803
804	PROC_LOCK(p);
805#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
806	if (ucp->uc_mcontext.mc_onstack & 1)
807		p->p_sigstk.ss_flags |= SS_ONSTACK;
808	else
809		p->p_sigstk.ss_flags &= ~SS_ONSTACK;
810#endif
811
812	p->p_sigmask = ucp->uc_sigmask;
813	SIG_CANTMASK(p->p_sigmask);
814	PROC_UNLOCK(p);
815	return (EJUSTRETURN);
816}
817
818/*
819 * Machine dependent boot() routine
820 *
821 * I haven't seen anything to put here yet
822 * Possibly some stuff might be grafted back here from boot()
823 */
824void
825cpu_boot(int howto)
826{
827}
828
829/*
830 * Shutdown the CPU as much as possible
831 */
832void
833cpu_halt(void)
834{
835	for (;;)
836		__asm__ ("hlt");
837}
838
839/*
840 * Hook to idle the CPU when possible.  This currently only works in
841 * the !SMP case, as there is no clean way to ensure that a CPU will be
842 * woken when there is work available for it.
843 */
844static int	cpu_idle_hlt = 1;
845SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
846    &cpu_idle_hlt, 0, "Idle loop HLT enable");
847
848/*
849 * Note that we have to be careful here to avoid a race between checking
850 * procrunnable() and actually halting.  If we don't do this, we may waste
851 * the time between calling hlt and the next interrupt even though there
852 * is a runnable process.
853 */
854void
855cpu_idle(void)
856{
857#ifndef SMP
858	if (cpu_idle_hlt) {
859		disable_intr();
860  		if (procrunnable())
861			enable_intr();
862		else {
863			enable_intr();
864			__asm __volatile("hlt");
865		}
866	}
867#endif
868}
869
870/*
871 * Clear registers on exec
872 */
873void
874setregs(td, entry, stack, ps_strings)
875	struct thread *td;
876	u_long entry;
877	u_long stack;
878	u_long ps_strings;
879{
880	struct trapframe *regs = td->td_frame;
881	struct pcb *pcb = td->td_pcb;
882
883	if (pcb->pcb_ldt)
884		user_ldt_free(pcb);
885
886	bzero((char *)regs, sizeof(struct trapframe));
887	regs->tf_eip = entry;
888	regs->tf_esp = stack;
889	regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
890	regs->tf_ss = _udatasel;
891	regs->tf_ds = _udatasel;
892	regs->tf_es = _udatasel;
893	regs->tf_fs = _udatasel;
894	regs->tf_cs = _ucodesel;
895
896	/* PS_STRINGS value for BSD/OS binaries.  It is 0 for non-BSD/OS. */
897	regs->tf_ebx = ps_strings;
898
899	/* reset %gs as well */
900	if (pcb == PCPU_GET(curpcb))
901		load_gs(_udatasel);
902	else
903		pcb->pcb_gs = _udatasel;
904
905        /*
906         * Reset the hardware debug registers if they were in use.
907         * They won't have any meaning for the newly exec'd process.
908         */
909        if (pcb->pcb_flags & PCB_DBREGS) {
910                pcb->pcb_dr0 = 0;
911                pcb->pcb_dr1 = 0;
912                pcb->pcb_dr2 = 0;
913                pcb->pcb_dr3 = 0;
914                pcb->pcb_dr6 = 0;
915                pcb->pcb_dr7 = 0;
916                if (pcb == PCPU_GET(curpcb)) {
917		        /*
918			 * Clear the debug registers on the running
919			 * CPU, otherwise they will end up affecting
920			 * the next process we switch to.
921			 */
922		        reset_dbregs();
923                }
924                pcb->pcb_flags &= ~PCB_DBREGS;
925        }
926
927	/*
928	 * Initialize the math emulator (if any) for the current process.
929	 * Actually, just clear the bit that says that the emulator has
930	 * been initialized.  Initialization is delayed until the process
931	 * traps to the emulator (if it is done at all) mainly because
932	 * emulators don't provide an entry point for initialization.
933	 */
934	td->td_pcb->pcb_flags &= ~FP_SOFTFP;
935
936	/*
937	 * Arrange to trap the next npx or `fwait' instruction (see npx.c
938	 * for why fwait must be trapped at least if there is an npx or an
939	 * emulator).  This is mainly to handle the case where npx0 is not
940	 * configured, since the npx routines normally set up the trap
941	 * otherwise.  It should be done only at boot time, but doing it
942	 * here allows modifying `npx_exists' for testing the emulator on
943	 * systems with an npx.
944	 */
945	load_cr0(rcr0() | CR0_MP | CR0_TS);
946
947#ifdef DEV_NPX
948	/* Initialize the npx (if any) for the current process. */
949	npxinit(__INITIAL_NPXCW__);
950#endif
951
952	/*
953	 * XXX - Linux emulator
954	 * Make sure sure edx is 0x0 on entry. Linux binaries depend
955	 * on it.
956	 */
957	td->td_retval[1] = 0;
958}
959
960void
961cpu_setregs(void)
962{
963	unsigned int cr0;
964
965	cr0 = rcr0();
966	cr0 |= CR0_NE;			/* Done by npxinit() */
967	cr0 |= CR0_MP | CR0_TS;		/* Done at every execve() too. */
968#ifndef I386_CPU
969	cr0 |= CR0_WP | CR0_AM;
970#endif
971	load_cr0(cr0);
972	load_gs(_udatasel);
973}
974
975static int
976sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
977{
978	int error;
979	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
980		req);
981	if (!error && req->newptr)
982		resettodr();
983	return (error);
984}
985
986SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
987	&adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
988
989SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
990	CTLFLAG_RW, &disable_rtc_set, 0, "");
991
992SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
993	CTLFLAG_RD, &bootinfo, bootinfo, "");
994
995SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
996	CTLFLAG_RW, &wall_cmos_clock, 0, "");
997
998/*
999 * Initialize 386 and configure to run kernel
1000 */
1001
1002/*
1003 * Initialize segments & interrupt table
1004 */
1005
1006int _default_ldt;
1007union descriptor gdt[NGDT * MAXCPU];	/* global descriptor table */
1008static struct gate_descriptor idt0[NIDT];
1009struct gate_descriptor *idt = &idt0[0];	/* interrupt descriptor table */
1010union descriptor ldt[NLDT];		/* local descriptor table */
1011#ifdef SMP
1012/* table descriptors - used to load tables by microp */
1013struct region_descriptor r_gdt, r_idt;
1014#endif
1015
1016int private_tss;			/* flag indicating private tss */
1017
1018#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1019extern int has_f00f_bug;
1020#endif
1021
1022static struct i386tss dblfault_tss;
1023static char dblfault_stack[PAGE_SIZE];
1024
1025extern  struct user	*proc0uarea;
1026extern  vm_offset_t	proc0kstack;
1027
1028
1029/* software prototypes -- in more palatable form */
1030struct soft_segment_descriptor gdt_segs[] = {
1031/* GNULL_SEL	0 Null Descriptor */
1032{	0x0,			/* segment base address  */
1033	0x0,			/* length */
1034	0,			/* segment type */
1035	0,			/* segment descriptor priority level */
1036	0,			/* segment descriptor present */
1037	0, 0,
1038	0,			/* default 32 vs 16 bit size */
1039	0  			/* limit granularity (byte/page units)*/ },
1040/* GCODE_SEL	1 Code Descriptor for kernel */
1041{	0x0,			/* segment base address  */
1042	0xfffff,		/* length - all address space */
1043	SDT_MEMERA,		/* segment type */
1044	0,			/* segment descriptor priority level */
1045	1,			/* segment descriptor present */
1046	0, 0,
1047	1,			/* default 32 vs 16 bit size */
1048	1  			/* limit granularity (byte/page units)*/ },
1049/* GDATA_SEL	2 Data Descriptor for kernel */
1050{	0x0,			/* segment base address  */
1051	0xfffff,		/* length - all address space */
1052	SDT_MEMRWA,		/* segment type */
1053	0,			/* segment descriptor priority level */
1054	1,			/* segment descriptor present */
1055	0, 0,
1056	1,			/* default 32 vs 16 bit size */
1057	1  			/* limit granularity (byte/page units)*/ },
1058/* GPRIV_SEL	3 SMP Per-Processor Private Data Descriptor */
1059{	0x0,			/* segment base address  */
1060	0xfffff,		/* length - all address space */
1061	SDT_MEMRWA,		/* segment type */
1062	0,			/* segment descriptor priority level */
1063	1,			/* segment descriptor present */
1064	0, 0,
1065	1,			/* default 32 vs 16 bit size */
1066	1  			/* limit granularity (byte/page units)*/ },
1067/* GPROC0_SEL	4 Proc 0 Tss Descriptor */
1068{
1069	0x0,			/* segment base address */
1070	sizeof(struct i386tss)-1,/* length - all address space */
1071	SDT_SYS386TSS,		/* segment type */
1072	0,			/* segment descriptor priority level */
1073	1,			/* segment descriptor present */
1074	0, 0,
1075	0,			/* unused - default 32 vs 16 bit size */
1076	0  			/* limit granularity (byte/page units)*/ },
1077/* GLDT_SEL	5 LDT Descriptor */
1078{	(int) ldt,		/* segment base address  */
1079	sizeof(ldt)-1,		/* length - all address space */
1080	SDT_SYSLDT,		/* segment type */
1081	SEL_UPL,		/* segment descriptor priority level */
1082	1,			/* segment descriptor present */
1083	0, 0,
1084	0,			/* unused - default 32 vs 16 bit size */
1085	0  			/* limit granularity (byte/page units)*/ },
1086/* GUSERLDT_SEL	6 User LDT Descriptor per process */
1087{	(int) ldt,		/* segment base address  */
1088	(512 * sizeof(union descriptor)-1),		/* length */
1089	SDT_SYSLDT,		/* segment type */
1090	0,			/* segment descriptor priority level */
1091	1,			/* segment descriptor present */
1092	0, 0,
1093	0,			/* unused - default 32 vs 16 bit size */
1094	0  			/* limit granularity (byte/page units)*/ },
1095/* GTGATE_SEL	7 Null Descriptor - Placeholder */
1096{	0x0,			/* segment base address  */
1097	0x0,			/* length - all address space */
1098	0,			/* segment type */
1099	0,			/* segment descriptor priority level */
1100	0,			/* segment descriptor present */
1101	0, 0,
1102	0,			/* default 32 vs 16 bit size */
1103	0  			/* limit granularity (byte/page units)*/ },
1104/* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1105{	0x400,			/* segment base address */
1106	0xfffff,		/* length */
1107	SDT_MEMRWA,		/* segment type */
1108	0,			/* segment descriptor priority level */
1109	1,			/* segment descriptor present */
1110	0, 0,
1111	1,			/* default 32 vs 16 bit size */
1112	1  			/* limit granularity (byte/page units)*/ },
1113/* GPANIC_SEL	9 Panic Tss Descriptor */
1114{	(int) &dblfault_tss,	/* segment base address  */
1115	sizeof(struct i386tss)-1,/* length - all address space */
1116	SDT_SYS386TSS,		/* segment type */
1117	0,			/* segment descriptor priority level */
1118	1,			/* segment descriptor present */
1119	0, 0,
1120	0,			/* unused - default 32 vs 16 bit size */
1121	0  			/* limit granularity (byte/page units)*/ },
1122/* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1123{	0,			/* segment base address (overwritten)  */
1124	0xfffff,		/* length */
1125	SDT_MEMERA,		/* segment type */
1126	0,			/* segment descriptor priority level */
1127	1,			/* segment descriptor present */
1128	0, 0,
1129	0,			/* default 32 vs 16 bit size */
1130	1  			/* limit granularity (byte/page units)*/ },
1131/* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1132{	0,			/* segment base address (overwritten)  */
1133	0xfffff,		/* length */
1134	SDT_MEMERA,		/* segment type */
1135	0,			/* segment descriptor priority level */
1136	1,			/* segment descriptor present */
1137	0, 0,
1138	0,			/* default 32 vs 16 bit size */
1139	1  			/* limit granularity (byte/page units)*/ },
1140/* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1141{	0,			/* segment base address (overwritten) */
1142	0xfffff,		/* length */
1143	SDT_MEMRWA,		/* segment type */
1144	0,			/* segment descriptor priority level */
1145	1,			/* segment descriptor present */
1146	0, 0,
1147	1,			/* default 32 vs 16 bit size */
1148	1  			/* limit granularity (byte/page units)*/ },
1149/* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1150{	0,			/* segment base address (overwritten) */
1151	0xfffff,		/* length */
1152	SDT_MEMRWA,		/* segment type */
1153	0,			/* segment descriptor priority level */
1154	1,			/* segment descriptor present */
1155	0, 0,
1156	0,			/* default 32 vs 16 bit size */
1157	1  			/* limit granularity (byte/page units)*/ },
1158/* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1159{	0,			/* segment base address (overwritten) */
1160	0xfffff,		/* length */
1161	SDT_MEMRWA,		/* segment type */
1162	0,			/* segment descriptor priority level */
1163	1,			/* segment descriptor present */
1164	0, 0,
1165	0,			/* default 32 vs 16 bit size */
1166	1  			/* limit granularity (byte/page units)*/ },
1167};
1168
1169static struct soft_segment_descriptor ldt_segs[] = {
1170	/* Null Descriptor - overwritten by call gate */
1171{	0x0,			/* segment base address  */
1172	0x0,			/* length - all address space */
1173	0,			/* segment type */
1174	0,			/* segment descriptor priority level */
1175	0,			/* segment descriptor present */
1176	0, 0,
1177	0,			/* default 32 vs 16 bit size */
1178	0  			/* limit granularity (byte/page units)*/ },
1179	/* Null Descriptor - overwritten by call gate */
1180{	0x0,			/* segment base address  */
1181	0x0,			/* length - all address space */
1182	0,			/* segment type */
1183	0,			/* segment descriptor priority level */
1184	0,			/* segment descriptor present */
1185	0, 0,
1186	0,			/* default 32 vs 16 bit size */
1187	0  			/* limit granularity (byte/page units)*/ },
1188	/* Null Descriptor - overwritten by call gate */
1189{	0x0,			/* segment base address  */
1190	0x0,			/* length - all address space */
1191	0,			/* segment type */
1192	0,			/* segment descriptor priority level */
1193	0,			/* segment descriptor present */
1194	0, 0,
1195	0,			/* default 32 vs 16 bit size */
1196	0  			/* limit granularity (byte/page units)*/ },
1197	/* Code Descriptor for user */
1198{	0x0,			/* segment base address  */
1199	0xfffff,		/* length - all address space */
1200	SDT_MEMERA,		/* segment type */
1201	SEL_UPL,		/* segment descriptor priority level */
1202	1,			/* segment descriptor present */
1203	0, 0,
1204	1,			/* default 32 vs 16 bit size */
1205	1  			/* limit granularity (byte/page units)*/ },
1206	/* Null Descriptor - overwritten by call gate */
1207{	0x0,			/* segment base address  */
1208	0x0,			/* length - all address space */
1209	0,			/* segment type */
1210	0,			/* segment descriptor priority level */
1211	0,			/* segment descriptor present */
1212	0, 0,
1213	0,			/* default 32 vs 16 bit size */
1214	0  			/* limit granularity (byte/page units)*/ },
1215	/* Data Descriptor for user */
1216{	0x0,			/* segment base address  */
1217	0xfffff,		/* length - all address space */
1218	SDT_MEMRWA,		/* segment type */
1219	SEL_UPL,		/* segment descriptor priority level */
1220	1,			/* segment descriptor present */
1221	0, 0,
1222	1,			/* default 32 vs 16 bit size */
1223	1  			/* limit granularity (byte/page units)*/ },
1224};
1225
1226void
1227setidt(idx, func, typ, dpl, selec)
1228	int idx;
1229	inthand_t *func;
1230	int typ;
1231	int dpl;
1232	int selec;
1233{
1234	struct gate_descriptor *ip;
1235
1236	ip = idt + idx;
1237	ip->gd_looffset = (int)func;
1238	ip->gd_selector = selec;
1239	ip->gd_stkcpy = 0;
1240	ip->gd_xx = 0;
1241	ip->gd_type = typ;
1242	ip->gd_dpl = dpl;
1243	ip->gd_p = 1;
1244	ip->gd_hioffset = ((int)func)>>16 ;
1245}
1246
1247#define	IDTVEC(name)	__CONCAT(X,name)
1248
1249extern inthand_t
1250	IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1251	IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1252	IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1253	IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1254	IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1255
1256void
1257sdtossd(sd, ssd)
1258	struct segment_descriptor *sd;
1259	struct soft_segment_descriptor *ssd;
1260{
1261	ssd->ssd_base  = (sd->sd_hibase << 24) | sd->sd_lobase;
1262	ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1263	ssd->ssd_type  = sd->sd_type;
1264	ssd->ssd_dpl   = sd->sd_dpl;
1265	ssd->ssd_p     = sd->sd_p;
1266	ssd->ssd_def32 = sd->sd_def32;
1267	ssd->ssd_gran  = sd->sd_gran;
1268}
1269
1270#define PHYSMAP_SIZE	(2 * 8)
1271
1272/*
1273 * Populate the (physmap) array with base/bound pairs describing the
1274 * available physical memory in the system, then test this memory and
1275 * build the phys_avail array describing the actually-available memory.
1276 *
1277 * If we cannot accurately determine the physical memory map, then use
1278 * value from the 0xE801 call, and failing that, the RTC.
1279 *
1280 * Total memory size may be set by the kernel environment variable
1281 * hw.physmem or the compile-time define MAXMEM.
1282 */
1283static void
1284getmemsize(int first)
1285{
1286	int i, physmap_idx, pa_indx;
1287	u_int basemem, extmem;
1288	struct vm86frame vmf;
1289	struct vm86context vmc;
1290	vm_offset_t pa, physmap[PHYSMAP_SIZE];
1291	pt_entry_t pte;
1292	const char *cp;
1293	struct bios_smap *smap;
1294
1295	bzero(&vmf, sizeof(struct vm86frame));
1296	bzero(physmap, sizeof(physmap));
1297
1298	/*
1299	 * Perform "base memory" related probes & setup
1300	 */
1301	vm86_intcall(0x12, &vmf);
1302	basemem = vmf.vmf_ax;
1303	if (basemem > 640) {
1304		printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1305			basemem);
1306		basemem = 640;
1307	}
1308
1309	/*
1310	 * XXX if biosbasemem is now < 640, there is a `hole'
1311	 * between the end of base memory and the start of
1312	 * ISA memory.  The hole may be empty or it may
1313	 * contain BIOS code or data.  Map it read/write so
1314	 * that the BIOS can write to it.  (Memory from 0 to
1315	 * the physical end of the kernel is mapped read-only
1316	 * to begin with and then parts of it are remapped.
1317	 * The parts that aren't remapped form holes that
1318	 * remain read-only and are unused by the kernel.
1319	 * The base memory area is below the physical end of
1320	 * the kernel and right now forms a read-only hole.
1321	 * The part of it from PAGE_SIZE to
1322	 * (trunc_page(biosbasemem * 1024) - 1) will be
1323	 * remapped and used by the kernel later.)
1324	 *
1325	 * This code is similar to the code used in
1326	 * pmap_mapdev, but since no memory needs to be
1327	 * allocated we simply change the mapping.
1328	 */
1329	for (pa = trunc_page(basemem * 1024);
1330	     pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1331		pte = (pt_entry_t)vtopte(pa + KERNBASE);
1332		*pte = pa | PG_RW | PG_V;
1333	}
1334
1335	/*
1336	 * if basemem != 640, map pages r/w into vm86 page table so
1337	 * that the bios can scribble on it.
1338	 */
1339	pte = (pt_entry_t)vm86paddr;
1340	for (i = basemem / 4; i < 160; i++)
1341		pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1342
1343	/*
1344	 * map page 1 R/W into the kernel page table so we can use it
1345	 * as a buffer.  The kernel will unmap this page later.
1346	 */
1347	pte = (pt_entry_t)vtopte(KERNBASE + (1 << PAGE_SHIFT));
1348	*pte = (1 << PAGE_SHIFT) | PG_RW | PG_V;
1349
1350	/*
1351	 * get memory map with INT 15:E820
1352	 */
1353	vmc.npages = 0;
1354	smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1355	vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1356
1357	physmap_idx = 0;
1358	vmf.vmf_ebx = 0;
1359	do {
1360		vmf.vmf_eax = 0xE820;
1361		vmf.vmf_edx = SMAP_SIG;
1362		vmf.vmf_ecx = sizeof(struct bios_smap);
1363		i = vm86_datacall(0x15, &vmf, &vmc);
1364		if (i || vmf.vmf_eax != SMAP_SIG)
1365			break;
1366		if (boothowto & RB_VERBOSE)
1367			printf("SMAP type=%02x base=%08x %08x len=%08x %08x\n",
1368				smap->type,
1369				*(u_int32_t *)((char *)&smap->base + 4),
1370				(u_int32_t)smap->base,
1371				*(u_int32_t *)((char *)&smap->length + 4),
1372				(u_int32_t)smap->length);
1373
1374		if (smap->type != 0x01)
1375			goto next_run;
1376
1377		if (smap->length == 0)
1378			goto next_run;
1379
1380		if (smap->base >= 0xffffffff) {
1381			printf("%uK of memory above 4GB ignored\n",
1382			    (u_int)(smap->length / 1024));
1383			goto next_run;
1384		}
1385
1386		for (i = 0; i <= physmap_idx; i += 2) {
1387			if (smap->base < physmap[i + 1]) {
1388				if (boothowto & RB_VERBOSE)
1389					printf(
1390	"Overlapping or non-montonic memory region, ignoring second region\n");
1391				goto next_run;
1392			}
1393		}
1394
1395		if (smap->base == physmap[physmap_idx + 1]) {
1396			physmap[physmap_idx + 1] += smap->length;
1397			goto next_run;
1398		}
1399
1400		physmap_idx += 2;
1401		if (physmap_idx == PHYSMAP_SIZE) {
1402			printf(
1403		"Too many segments in the physical address map, giving up\n");
1404			break;
1405		}
1406		physmap[physmap_idx] = smap->base;
1407		physmap[physmap_idx + 1] = smap->base + smap->length;
1408next_run:
1409	} while (vmf.vmf_ebx != 0);
1410
1411	if (physmap[1] != 0)
1412		goto physmap_done;
1413
1414	/*
1415	 * If we failed above, try memory map with INT 15:E801
1416	 */
1417	vmf.vmf_ax = 0xE801;
1418	if (vm86_intcall(0x15, &vmf) == 0) {
1419		extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1420	} else {
1421#if 0
1422		vmf.vmf_ah = 0x88;
1423		vm86_intcall(0x15, &vmf);
1424		extmem = vmf.vmf_ax;
1425#else
1426		/*
1427		 * Prefer the RTC value for extended memory.
1428		 */
1429		extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1430#endif
1431	}
1432
1433	/*
1434	 * Special hack for chipsets that still remap the 384k hole when
1435	 * there's 16MB of memory - this really confuses people that
1436	 * are trying to use bus mastering ISA controllers with the
1437	 * "16MB limit"; they only have 16MB, but the remapping puts
1438	 * them beyond the limit.
1439	 *
1440	 * If extended memory is between 15-16MB (16-17MB phys address range),
1441	 *	chop it to 15MB.
1442	 */
1443	if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1444		extmem = 15 * 1024;
1445
1446	physmap[0] = 0;
1447	physmap[1] = basemem * 1024;
1448	physmap_idx = 2;
1449	physmap[physmap_idx] = 0x100000;
1450	physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1451
1452physmap_done:
1453	/*
1454	 * Now, physmap contains a map of physical memory.
1455	 */
1456
1457#ifdef SMP
1458	/* make hole for AP bootstrap code */
1459	physmap[1] = mp_bootaddress(physmap[1] / 1024);
1460
1461	/* look for the MP hardware - needed for apic addresses */
1462	i386_mp_probe();
1463#endif
1464
1465	/*
1466	 * Maxmem isn't the "maximum memory", it's one larger than the
1467	 * highest page of the physical address space.  It should be
1468	 * called something like "Maxphyspage".  We may adjust this
1469	 * based on ``hw.physmem'' and the results of the memory test.
1470	 */
1471	Maxmem = atop(physmap[physmap_idx + 1]);
1472
1473#ifdef MAXMEM
1474	Maxmem = MAXMEM / 4;
1475#endif
1476
1477	/*
1478	 * hw.physmem is a size in bytes; we also allow k, m, and g suffixes
1479	 * for the appropriate modifiers.  This overrides MAXMEM.
1480	 */
1481	if ((cp = getenv("hw.physmem")) != NULL) {
1482		u_int64_t AllowMem, sanity;
1483		char *ep;
1484
1485		sanity = AllowMem = strtouq(cp, &ep, 0);
1486		if ((ep != cp) && (*ep != 0)) {
1487			switch(*ep) {
1488			case 'g':
1489			case 'G':
1490				AllowMem <<= 10;
1491			case 'm':
1492			case 'M':
1493				AllowMem <<= 10;
1494			case 'k':
1495			case 'K':
1496				AllowMem <<= 10;
1497				break;
1498			default:
1499				AllowMem = sanity = 0;
1500			}
1501			if (AllowMem < sanity)
1502				AllowMem = 0;
1503		}
1504		if (AllowMem == 0)
1505			printf("Ignoring invalid memory size of '%s'\n", cp);
1506		else
1507			Maxmem = atop(AllowMem);
1508	}
1509
1510	if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1511	    (boothowto & RB_VERBOSE))
1512		printf("Physical memory use set to %uK\n", Maxmem * 4);
1513
1514	/*
1515	 * If Maxmem has been increased beyond what the system has detected,
1516	 * extend the last memory segment to the new limit.
1517	 */
1518	if (atop(physmap[physmap_idx + 1]) < Maxmem)
1519		physmap[physmap_idx + 1] = ptoa(Maxmem);
1520
1521	/* call pmap initialization to make new kernel address space */
1522	pmap_bootstrap(first, 0);
1523
1524	/*
1525	 * Size up each available chunk of physical memory.
1526	 */
1527	physmap[0] = PAGE_SIZE;		/* mask off page 0 */
1528	pa_indx = 0;
1529	phys_avail[pa_indx++] = physmap[0];
1530	phys_avail[pa_indx] = physmap[0];
1531#if 0
1532	pte = (pt_entry_t)vtopte(KERNBASE);
1533#else
1534	pte = (pt_entry_t)CMAP1;
1535#endif
1536
1537	/*
1538	 * physmap is in bytes, so when converting to page boundaries,
1539	 * round up the start address and round down the end address.
1540	 */
1541	for (i = 0; i <= physmap_idx; i += 2) {
1542		vm_offset_t end;
1543
1544		end = ptoa(Maxmem);
1545		if (physmap[i + 1] < end)
1546			end = trunc_page(physmap[i + 1]);
1547		for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1548			int tmp, page_bad;
1549#if 0
1550			int *ptr = 0;
1551#else
1552			int *ptr = (int *)CADDR1;
1553#endif
1554
1555			/*
1556			 * block out kernel memory as not available.
1557			 */
1558			if (pa >= 0x100000 && pa < first)
1559				continue;
1560
1561			page_bad = FALSE;
1562
1563			/*
1564			 * map page into kernel: valid, read/write,non-cacheable
1565			 */
1566			*pte = pa | PG_V | PG_RW | PG_N;
1567			invltlb();
1568
1569			tmp = *(int *)ptr;
1570			/*
1571			 * Test for alternating 1's and 0's
1572			 */
1573			*(volatile int *)ptr = 0xaaaaaaaa;
1574			if (*(volatile int *)ptr != 0xaaaaaaaa) {
1575				page_bad = TRUE;
1576			}
1577			/*
1578			 * Test for alternating 0's and 1's
1579			 */
1580			*(volatile int *)ptr = 0x55555555;
1581			if (*(volatile int *)ptr != 0x55555555) {
1582			page_bad = TRUE;
1583			}
1584			/*
1585			 * Test for all 1's
1586			 */
1587			*(volatile int *)ptr = 0xffffffff;
1588			if (*(volatile int *)ptr != 0xffffffff) {
1589				page_bad = TRUE;
1590			}
1591			/*
1592			 * Test for all 0's
1593			 */
1594			*(volatile int *)ptr = 0x0;
1595			if (*(volatile int *)ptr != 0x0) {
1596				page_bad = TRUE;
1597			}
1598			/*
1599			 * Restore original value.
1600			 */
1601			*(int *)ptr = tmp;
1602
1603			/*
1604			 * Adjust array of valid/good pages.
1605			 */
1606			if (page_bad == TRUE) {
1607				continue;
1608			}
1609			/*
1610			 * If this good page is a continuation of the
1611			 * previous set of good pages, then just increase
1612			 * the end pointer. Otherwise start a new chunk.
1613			 * Note that "end" points one higher than end,
1614			 * making the range >= start and < end.
1615			 * If we're also doing a speculative memory
1616			 * test and we at or past the end, bump up Maxmem
1617			 * so that we keep going. The first bad page
1618			 * will terminate the loop.
1619			 */
1620			if (phys_avail[pa_indx] == pa) {
1621				phys_avail[pa_indx] += PAGE_SIZE;
1622			} else {
1623				pa_indx++;
1624				if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1625					printf(
1626		"Too many holes in the physical address space, giving up\n");
1627					pa_indx--;
1628					break;
1629				}
1630				phys_avail[pa_indx++] = pa;	/* start */
1631				phys_avail[pa_indx] = pa + PAGE_SIZE;	/* end */
1632			}
1633			physmem++;
1634		}
1635	}
1636	*pte = 0;
1637	invltlb();
1638
1639	/*
1640	 * XXX
1641	 * The last chunk must contain at least one page plus the message
1642	 * buffer to avoid complicating other code (message buffer address
1643	 * calculation, etc.).
1644	 */
1645	while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1646	    round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1647		physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1648		phys_avail[pa_indx--] = 0;
1649		phys_avail[pa_indx--] = 0;
1650	}
1651
1652	Maxmem = atop(phys_avail[pa_indx]);
1653
1654	/* Trim off space for the message buffer. */
1655	phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1656
1657	avail_end = phys_avail[pa_indx];
1658}
1659
1660void
1661init386(first)
1662	int first;
1663{
1664	struct gate_descriptor *gdp;
1665	int gsel_tss, metadata_missing, off, x;
1666#ifndef SMP
1667	/* table descriptors - used to load tables by microp */
1668	struct region_descriptor r_gdt, r_idt;
1669#endif
1670
1671	proc_linkup(&proc0);
1672	proc0.p_uarea = proc0uarea;
1673	thread0 = &proc0.p_thread;
1674	thread0->td_kstack = proc0kstack;
1675	thread0->td_pcb = (struct pcb *)
1676	   (thread0->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1677	atdevbase = ISA_HOLE_START + KERNBASE;
1678
1679	metadata_missing = 0;
1680	if (bootinfo.bi_modulep) {
1681		preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1682		preload_bootstrap_relocate(KERNBASE);
1683	} else {
1684		metadata_missing = 1;
1685	}
1686	if (envmode == 1)
1687		kern_envp = static_env;
1688	else if (bootinfo.bi_envp)
1689		kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1690
1691	/* Init basic tunables, hz etc */
1692	init_param();
1693
1694	/*
1695	 * make gdt memory segments, the code segment goes up to end of the
1696	 * page with etext in it, the data segment goes to the end of
1697	 * the address space
1698	 */
1699	/*
1700	 * XXX text protection is temporarily (?) disabled.  The limit was
1701	 * i386_btop(round_page(etext)) - 1.
1702	 */
1703	gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
1704	gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
1705#ifdef SMP
1706	gdt_segs[GPRIV_SEL].ssd_limit =
1707		atop(sizeof(struct privatespace) - 1);
1708	gdt_segs[GPRIV_SEL].ssd_base = (int) &SMP_prvspace[0];
1709	gdt_segs[GPROC0_SEL].ssd_base =
1710		(int) &SMP_prvspace[0].globaldata.gd_common_tss;
1711	SMP_prvspace[0].globaldata.gd_prvspace = &SMP_prvspace[0].globaldata;
1712#else
1713	gdt_segs[GPRIV_SEL].ssd_limit =
1714		atop(sizeof(struct globaldata) - 1);
1715	gdt_segs[GPRIV_SEL].ssd_base = (int) &__globaldata;
1716	gdt_segs[GPROC0_SEL].ssd_base =
1717		(int) &__globaldata.gd_common_tss;
1718	__globaldata.gd_prvspace = &__globaldata;
1719#endif
1720
1721	for (x = 0; x < NGDT; x++) {
1722#ifdef BDE_DEBUGGER
1723		/* avoid overwriting db entries with APM ones */
1724		if (x >= GAPMCODE32_SEL && x <= GAPMDATA_SEL)
1725			continue;
1726#endif
1727		ssdtosd(&gdt_segs[x], &gdt[x].sd);
1728	}
1729
1730	r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1731	r_gdt.rd_base =  (int) gdt;
1732	lgdt(&r_gdt);
1733
1734	/* setup curproc so that mutexes work */
1735
1736	PCPU_SET(curthread, thread0);
1737	PCPU_SET(spinlocks, NULL);
1738
1739	LIST_INIT(&thread0->td_contested);
1740
1741	/*
1742	 * Initialize mutexes.
1743	 */
1744	mtx_init(&Giant, "Giant", MTX_DEF | MTX_RECURSE);
1745	mtx_init(&sched_lock, "sched lock", MTX_SPIN | MTX_RECURSE);
1746	mtx_init(&proc0.p_mtx, "process lock", MTX_DEF);
1747	mtx_init(&clock_lock, "clk", MTX_SPIN | MTX_RECURSE);
1748#ifdef SMP
1749	mtx_init(&imen_mtx, "imen", MTX_SPIN);
1750#endif
1751	mtx_lock(&Giant);
1752
1753	/* make ldt memory segments */
1754	/*
1755	 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  And it
1756	 * should be spelled ...MAX_USER...
1757	 */
1758	ldt_segs[LUCODE_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
1759	ldt_segs[LUDATA_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
1760	for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
1761		ssdtosd(&ldt_segs[x], &ldt[x].sd);
1762
1763	_default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1764	lldt(_default_ldt);
1765	PCPU_SET(currentldt, _default_ldt);
1766
1767	/* exceptions */
1768	for (x = 0; x < NIDT; x++)
1769		setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
1770		    GSEL(GCODE_SEL, SEL_KPL));
1771	setidt(0, &IDTVEC(div),  SDT_SYS386TGT, SEL_KPL,
1772	    GSEL(GCODE_SEL, SEL_KPL));
1773	setidt(1, &IDTVEC(dbg),  SDT_SYS386IGT, SEL_KPL,
1774	    GSEL(GCODE_SEL, SEL_KPL));
1775	setidt(2, &IDTVEC(nmi),  SDT_SYS386TGT, SEL_KPL,
1776	    GSEL(GCODE_SEL, SEL_KPL));
1777 	setidt(3, &IDTVEC(bpt),  SDT_SYS386IGT, SEL_UPL,
1778	    GSEL(GCODE_SEL, SEL_KPL));
1779	setidt(4, &IDTVEC(ofl),  SDT_SYS386TGT, SEL_UPL,
1780	    GSEL(GCODE_SEL, SEL_KPL));
1781	setidt(5, &IDTVEC(bnd),  SDT_SYS386TGT, SEL_KPL,
1782	    GSEL(GCODE_SEL, SEL_KPL));
1783	setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL,
1784	    GSEL(GCODE_SEL, SEL_KPL));
1785	setidt(7, &IDTVEC(dna),  SDT_SYS386TGT, SEL_KPL
1786	    , GSEL(GCODE_SEL, SEL_KPL));
1787	setidt(8, 0,  SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1788	setidt(9, &IDTVEC(fpusegm),  SDT_SYS386TGT, SEL_KPL,
1789	    GSEL(GCODE_SEL, SEL_KPL));
1790	setidt(10, &IDTVEC(tss),  SDT_SYS386TGT, SEL_KPL,
1791	    GSEL(GCODE_SEL, SEL_KPL));
1792	setidt(11, &IDTVEC(missing),  SDT_SYS386TGT, SEL_KPL,
1793	    GSEL(GCODE_SEL, SEL_KPL));
1794	setidt(12, &IDTVEC(stk),  SDT_SYS386TGT, SEL_KPL,
1795	    GSEL(GCODE_SEL, SEL_KPL));
1796	setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL,
1797	    GSEL(GCODE_SEL, SEL_KPL));
1798	setidt(14, &IDTVEC(page),  SDT_SYS386IGT, SEL_KPL,
1799	    GSEL(GCODE_SEL, SEL_KPL));
1800	setidt(15, &IDTVEC(rsvd),  SDT_SYS386TGT, SEL_KPL,
1801	    GSEL(GCODE_SEL, SEL_KPL));
1802	setidt(16, &IDTVEC(fpu),  SDT_SYS386TGT, SEL_KPL,
1803	    GSEL(GCODE_SEL, SEL_KPL));
1804	setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
1805	    GSEL(GCODE_SEL, SEL_KPL));
1806	setidt(18, &IDTVEC(mchk),  SDT_SYS386TGT, SEL_KPL,
1807	    GSEL(GCODE_SEL, SEL_KPL));
1808	setidt(19, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
1809	    GSEL(GCODE_SEL, SEL_KPL));
1810 	setidt(0x80, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
1811	    GSEL(GCODE_SEL, SEL_KPL));
1812
1813	r_idt.rd_limit = sizeof(idt0) - 1;
1814	r_idt.rd_base = (int) idt;
1815	lidt(&r_idt);
1816
1817	/*
1818	 * Initialize the console before we print anything out.
1819	 */
1820	cninit();
1821
1822	if (metadata_missing)
1823		printf("WARNING: loader(8) metadata is missing!\n");
1824
1825#ifdef DEV_ISA
1826	isa_defaultirq();
1827#endif
1828
1829#ifdef DDB
1830	kdb_init();
1831	if (boothowto & RB_KDB)
1832		Debugger("Boot flags requested debugger");
1833#endif
1834
1835	finishidentcpu();	/* Final stage of CPU initialization */
1836	setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL,
1837	    GSEL(GCODE_SEL, SEL_KPL));
1838	setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL,
1839	    GSEL(GCODE_SEL, SEL_KPL));
1840	initializecpu();	/* Initialize CPU registers */
1841
1842	/* make an initial tss so cpu can get interrupt stack on syscall! */
1843	/* Note: -16 is so we can grow the trapframe if we came from vm86 */
1844	PCPU_SET(common_tss.tss_esp0, thread0->td_kstack +
1845	    KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
1846	PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
1847	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1848	private_tss = 0;
1849	PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
1850	PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
1851	PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
1852	ltr(gsel_tss);
1853
1854	dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1855	    dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
1856	dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1857	    dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1858	dblfault_tss.tss_cr3 = (int)IdlePTD;
1859	dblfault_tss.tss_eip = (int)dblfault_handler;
1860	dblfault_tss.tss_eflags = PSL_KERNEL;
1861	dblfault_tss.tss_ds = dblfault_tss.tss_es =
1862	    dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1863	dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1864	dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1865	dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1866
1867	vm86_initialize();
1868	getmemsize(first);
1869
1870	/* now running on new page tables, configured,and u/iom is accessible */
1871
1872	/* Map the message buffer. */
1873	for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1874		pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1875
1876	msgbufinit(msgbufp, MSGBUF_SIZE);
1877
1878	/* make a call gate to reenter kernel with */
1879	gdp = &ldt[LSYS5CALLS_SEL].gd;
1880
1881	x = (int) &IDTVEC(lcall_syscall);
1882	gdp->gd_looffset = x;
1883	gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1884	gdp->gd_stkcpy = 1;
1885	gdp->gd_type = SDT_SYS386CGT;
1886	gdp->gd_dpl = SEL_UPL;
1887	gdp->gd_p = 1;
1888	gdp->gd_hioffset = x >> 16;
1889
1890	/* XXX does this work? */
1891	ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1892	ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
1893
1894	/* transfer to user mode */
1895
1896	_ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1897	_udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1898
1899	/* setup proc 0's pcb */
1900	thread0->td_pcb->pcb_flags = 0; /* XXXKSE */
1901	thread0->td_pcb->pcb_cr3 = (int)IdlePTD;
1902	thread0->td_pcb->pcb_ext = 0;
1903	thread0->td_frame = &proc0_tf;
1904}
1905
1906#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1907static void f00f_hack(void *unused);
1908SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
1909
1910static void
1911f00f_hack(void *unused) {
1912	struct gate_descriptor *new_idt;
1913#ifndef SMP
1914	struct region_descriptor r_idt;
1915#endif
1916	vm_offset_t tmp;
1917
1918	if (!has_f00f_bug)
1919		return;
1920
1921	GIANT_REQUIRED;
1922
1923	printf("Intel Pentium detected, installing workaround for F00F bug\n");
1924
1925	r_idt.rd_limit = sizeof(idt0) - 1;
1926
1927	tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
1928	if (tmp == 0)
1929		panic("kmem_alloc returned 0");
1930	if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0)
1931		panic("kmem_alloc returned non-page-aligned memory");
1932	/* Put the first seven entries in the lower page */
1933	new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8));
1934	bcopy(idt, new_idt, sizeof(idt0));
1935	r_idt.rd_base = (int)new_idt;
1936	lidt(&r_idt);
1937	idt = new_idt;
1938	if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
1939			   VM_PROT_READ, FALSE) != KERN_SUCCESS)
1940		panic("vm_map_protect failed");
1941	return;
1942}
1943#endif /* defined(I586_CPU) && !NO_F00F_HACK */
1944
1945int
1946ptrace_set_pc(struct thread *td, unsigned long addr)
1947{
1948	td->td_frame->tf_eip = addr;
1949	return (0);
1950}
1951
1952int
1953ptrace_single_step(struct thread *td)
1954{
1955	td->td_frame->tf_eflags |= PSL_T;
1956	return (0);
1957}
1958
1959int
1960fill_regs(struct thread *td, struct reg *regs)
1961{
1962	struct pcb *pcb;
1963	struct trapframe *tp;
1964
1965	tp = td->td_frame;
1966	regs->r_fs = tp->tf_fs;
1967	regs->r_es = tp->tf_es;
1968	regs->r_ds = tp->tf_ds;
1969	regs->r_edi = tp->tf_edi;
1970	regs->r_esi = tp->tf_esi;
1971	regs->r_ebp = tp->tf_ebp;
1972	regs->r_ebx = tp->tf_ebx;
1973	regs->r_edx = tp->tf_edx;
1974	regs->r_ecx = tp->tf_ecx;
1975	regs->r_eax = tp->tf_eax;
1976	regs->r_eip = tp->tf_eip;
1977	regs->r_cs = tp->tf_cs;
1978	regs->r_eflags = tp->tf_eflags;
1979	regs->r_esp = tp->tf_esp;
1980	regs->r_ss = tp->tf_ss;
1981	pcb = td->td_pcb;
1982	regs->r_gs = pcb->pcb_gs;
1983	return (0);
1984}
1985
1986int
1987set_regs(struct thread *td, struct reg *regs)
1988{
1989	struct pcb *pcb;
1990	struct trapframe *tp;
1991
1992	tp = td->td_frame;
1993	if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
1994	    !CS_SECURE(regs->r_cs))
1995		return (EINVAL);
1996	tp->tf_fs = regs->r_fs;
1997	tp->tf_es = regs->r_es;
1998	tp->tf_ds = regs->r_ds;
1999	tp->tf_edi = regs->r_edi;
2000	tp->tf_esi = regs->r_esi;
2001	tp->tf_ebp = regs->r_ebp;
2002	tp->tf_ebx = regs->r_ebx;
2003	tp->tf_edx = regs->r_edx;
2004	tp->tf_ecx = regs->r_ecx;
2005	tp->tf_eax = regs->r_eax;
2006	tp->tf_eip = regs->r_eip;
2007	tp->tf_cs = regs->r_cs;
2008	tp->tf_eflags = regs->r_eflags;
2009	tp->tf_esp = regs->r_esp;
2010	tp->tf_ss = regs->r_ss;
2011	pcb = td->td_pcb;
2012	pcb->pcb_gs = regs->r_gs;
2013	return (0);
2014}
2015
2016#ifdef CPU_ENABLE_SSE
2017static void
2018fill_fpregs_xmm(sv_xmm, sv_87)
2019	struct savexmm *sv_xmm;
2020	struct save87 *sv_87;
2021{
2022	register struct env87 *penv_87 = &sv_87->sv_env;
2023	register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2024	int i;
2025
2026	/* FPU control/status */
2027	penv_87->en_cw = penv_xmm->en_cw;
2028	penv_87->en_sw = penv_xmm->en_sw;
2029	penv_87->en_tw = penv_xmm->en_tw;
2030	penv_87->en_fip = penv_xmm->en_fip;
2031	penv_87->en_fcs = penv_xmm->en_fcs;
2032	penv_87->en_opcode = penv_xmm->en_opcode;
2033	penv_87->en_foo = penv_xmm->en_foo;
2034	penv_87->en_fos = penv_xmm->en_fos;
2035
2036	/* FPU registers */
2037	for (i = 0; i < 8; ++i)
2038		sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2039
2040	sv_87->sv_ex_sw = sv_xmm->sv_ex_sw;
2041}
2042
2043static void
2044set_fpregs_xmm(sv_87, sv_xmm)
2045	struct save87 *sv_87;
2046	struct savexmm *sv_xmm;
2047{
2048	register struct env87 *penv_87 = &sv_87->sv_env;
2049	register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2050	int i;
2051
2052	/* FPU control/status */
2053	penv_xmm->en_cw = penv_87->en_cw;
2054	penv_xmm->en_sw = penv_87->en_sw;
2055	penv_xmm->en_tw = penv_87->en_tw;
2056	penv_xmm->en_fip = penv_87->en_fip;
2057	penv_xmm->en_fcs = penv_87->en_fcs;
2058	penv_xmm->en_opcode = penv_87->en_opcode;
2059	penv_xmm->en_foo = penv_87->en_foo;
2060	penv_xmm->en_fos = penv_87->en_fos;
2061
2062	/* FPU registers */
2063	for (i = 0; i < 8; ++i)
2064		sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2065
2066	sv_xmm->sv_ex_sw = sv_87->sv_ex_sw;
2067}
2068#endif /* CPU_ENABLE_SSE */
2069
2070int
2071fill_fpregs(struct thread *td, struct fpreg *fpregs)
2072{
2073#ifdef CPU_ENABLE_SSE
2074	if (cpu_fxsr) {
2075		fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
2076						(struct save87 *)fpregs);
2077		return (0);
2078	}
2079#endif /* CPU_ENABLE_SSE */
2080	bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
2081	return (0);
2082}
2083
2084int
2085set_fpregs(struct thread *td, struct fpreg *fpregs)
2086{
2087#ifdef CPU_ENABLE_SSE
2088	if (cpu_fxsr) {
2089		set_fpregs_xmm((struct save87 *)fpregs,
2090					   &td->td_pcb->pcb_save.sv_xmm);
2091		return (0);
2092	}
2093#endif /* CPU_ENABLE_SSE */
2094	bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
2095	return (0);
2096}
2097
2098int
2099fill_dbregs(struct thread *td, struct dbreg *dbregs)
2100{
2101	struct pcb *pcb;
2102
2103	if (td == NULL) {
2104		dbregs->dr0 = rdr0();
2105		dbregs->dr1 = rdr1();
2106		dbregs->dr2 = rdr2();
2107		dbregs->dr3 = rdr3();
2108		dbregs->dr4 = rdr4();
2109		dbregs->dr5 = rdr5();
2110		dbregs->dr6 = rdr6();
2111		dbregs->dr7 = rdr7();
2112	} else {
2113		pcb = td->td_pcb;
2114		dbregs->dr0 = pcb->pcb_dr0;
2115		dbregs->dr1 = pcb->pcb_dr1;
2116		dbregs->dr2 = pcb->pcb_dr2;
2117		dbregs->dr3 = pcb->pcb_dr3;
2118		dbregs->dr4 = 0;
2119		dbregs->dr5 = 0;
2120		dbregs->dr6 = pcb->pcb_dr6;
2121		dbregs->dr7 = pcb->pcb_dr7;
2122	}
2123	return (0);
2124}
2125
2126int
2127set_dbregs(struct thread *td, struct dbreg *dbregs)
2128{
2129	struct pcb *pcb;
2130	int i;
2131	u_int32_t mask1, mask2;
2132
2133	if (td == NULL) {
2134		load_dr0(dbregs->dr0);
2135		load_dr1(dbregs->dr1);
2136		load_dr2(dbregs->dr2);
2137		load_dr3(dbregs->dr3);
2138		load_dr4(dbregs->dr4);
2139		load_dr5(dbregs->dr5);
2140		load_dr6(dbregs->dr6);
2141		load_dr7(dbregs->dr7);
2142	} else {
2143		/*
2144		 * Don't let an illegal value for dr7 get set.	Specifically,
2145		 * check for undefined settings.  Setting these bit patterns
2146		 * result in undefined behaviour and can lead to an unexpected
2147		 * TRCTRAP.
2148		 */
2149		for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8;
2150		     i++, mask1 <<= 2, mask2 <<= 2)
2151			if ((dbregs->dr7 & mask1) == mask2)
2152				return (EINVAL);
2153
2154		pcb = td->td_pcb;
2155
2156		/*
2157		 * Don't let a process set a breakpoint that is not within the
2158		 * process's address space.  If a process could do this, it
2159		 * could halt the system by setting a breakpoint in the kernel
2160		 * (if ddb was enabled).  Thus, we need to check to make sure
2161		 * that no breakpoints are being enabled for addresses outside
2162		 * process's address space, unless, perhaps, we were called by
2163		 * uid 0.
2164		 *
2165		 * XXX - what about when the watched area of the user's
2166		 * address space is written into from within the kernel
2167		 * ... wouldn't that still cause a breakpoint to be generated
2168		 * from within kernel mode?
2169		 */
2170
2171		if (suser_td(td) != 0) {
2172			if (dbregs->dr7 & 0x3) {
2173				/* dr0 is enabled */
2174				if (dbregs->dr0 >= VM_MAXUSER_ADDRESS)
2175					return (EINVAL);
2176			}
2177
2178			if (dbregs->dr7 & (0x3<<2)) {
2179				/* dr1 is enabled */
2180				if (dbregs->dr1 >= VM_MAXUSER_ADDRESS)
2181					return (EINVAL);
2182			}
2183
2184			if (dbregs->dr7 & (0x3<<4)) {
2185				/* dr2 is enabled */
2186				if (dbregs->dr2 >= VM_MAXUSER_ADDRESS)
2187					return (EINVAL);
2188			}
2189
2190			if (dbregs->dr7 & (0x3<<6)) {
2191				/* dr3 is enabled */
2192				if (dbregs->dr3 >= VM_MAXUSER_ADDRESS)
2193					return (EINVAL);
2194			}
2195		}
2196
2197		pcb->pcb_dr0 = dbregs->dr0;
2198		pcb->pcb_dr1 = dbregs->dr1;
2199		pcb->pcb_dr2 = dbregs->dr2;
2200		pcb->pcb_dr3 = dbregs->dr3;
2201		pcb->pcb_dr6 = dbregs->dr6;
2202		pcb->pcb_dr7 = dbregs->dr7;
2203
2204		pcb->pcb_flags |= PCB_DBREGS;
2205	}
2206
2207	return (0);
2208}
2209
2210/*
2211 * Return > 0 if a hardware breakpoint has been hit, and the
2212 * breakpoint was in user space.  Return 0, otherwise.
2213 */
2214int
2215user_dbreg_trap(void)
2216{
2217        u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2218        u_int32_t bp;       /* breakpoint bits extracted from dr6 */
2219        int nbp;            /* number of breakpoints that triggered */
2220        caddr_t addr[4];    /* breakpoint addresses */
2221        int i;
2222
2223        dr7 = rdr7();
2224        if ((dr7 & 0x000000ff) == 0) {
2225                /*
2226                 * all GE and LE bits in the dr7 register are zero,
2227                 * thus the trap couldn't have been caused by the
2228                 * hardware debug registers
2229                 */
2230                return 0;
2231        }
2232
2233        nbp = 0;
2234        dr6 = rdr6();
2235        bp = dr6 & 0x0000000f;
2236
2237        if (!bp) {
2238                /*
2239                 * None of the breakpoint bits are set meaning this
2240                 * trap was not caused by any of the debug registers
2241                 */
2242                return 0;
2243        }
2244
2245        /*
2246         * at least one of the breakpoints were hit, check to see
2247         * which ones and if any of them are user space addresses
2248         */
2249
2250        if (bp & 0x01) {
2251                addr[nbp++] = (caddr_t)rdr0();
2252        }
2253        if (bp & 0x02) {
2254                addr[nbp++] = (caddr_t)rdr1();
2255        }
2256        if (bp & 0x04) {
2257                addr[nbp++] = (caddr_t)rdr2();
2258        }
2259        if (bp & 0x08) {
2260                addr[nbp++] = (caddr_t)rdr3();
2261        }
2262
2263        for (i=0; i<nbp; i++) {
2264                if (addr[i] <
2265                    (caddr_t)VM_MAXUSER_ADDRESS) {
2266                        /*
2267                         * addr[i] is in user space
2268                         */
2269                        return nbp;
2270                }
2271        }
2272
2273        /*
2274         * None of the breakpoints are in user space.
2275         */
2276        return 0;
2277}
2278
2279
2280#ifndef DDB
2281void
2282Debugger(const char *msg)
2283{
2284	printf("Debugger(\"%s\") called.\n", msg);
2285}
2286#endif /* no DDB */
2287
2288#include <sys/disklabel.h>
2289
2290/*
2291 * Determine the size of the transfer, and make sure it is
2292 * within the boundaries of the partition. Adjust transfer
2293 * if needed, and signal errors or early completion.
2294 */
2295int
2296bounds_check_with_label(struct bio *bp, struct disklabel *lp, int wlabel)
2297{
2298        struct partition *p = lp->d_partitions + dkpart(bp->bio_dev);
2299        int labelsect = lp->d_partitions[0].p_offset;
2300        int maxsz = p->p_size,
2301                sz = (bp->bio_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
2302
2303        /* overwriting disk label ? */
2304        /* XXX should also protect bootstrap in first 8K */
2305        if (bp->bio_blkno + p->p_offset <= LABELSECTOR + labelsect &&
2306#if LABELSECTOR != 0
2307            bp->bio_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
2308#endif
2309            (bp->bio_cmd == BIO_WRITE) && wlabel == 0) {
2310                bp->bio_error = EROFS;
2311                goto bad;
2312        }
2313
2314#if     defined(DOSBBSECTOR) && defined(notyet)
2315        /* overwriting master boot record? */
2316        if (bp->bio_blkno + p->p_offset <= DOSBBSECTOR &&
2317            (bp->bio_cmd == BIO_WRITE) && wlabel == 0) {
2318                bp->bio_error = EROFS;
2319                goto bad;
2320        }
2321#endif
2322
2323        /* beyond partition? */
2324        if (bp->bio_blkno < 0 || bp->bio_blkno + sz > maxsz) {
2325                /* if exactly at end of disk, return an EOF */
2326                if (bp->bio_blkno == maxsz) {
2327                        bp->bio_resid = bp->bio_bcount;
2328                        return(0);
2329                }
2330                /* or truncate if part of it fits */
2331                sz = maxsz - bp->bio_blkno;
2332                if (sz <= 0) {
2333                        bp->bio_error = EINVAL;
2334                        goto bad;
2335                }
2336                bp->bio_bcount = sz << DEV_BSHIFT;
2337        }
2338
2339        bp->bio_pblkno = bp->bio_blkno + p->p_offset;
2340        return(1);
2341
2342bad:
2343        bp->bio_flags |= BIO_ERROR;
2344        return(-1);
2345}
2346
2347#ifdef DDB
2348
2349/*
2350 * Provide inb() and outb() as functions.  They are normally only
2351 * available as macros calling inlined functions, thus cannot be
2352 * called inside DDB.
2353 *
2354 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2355 */
2356
2357#undef inb
2358#undef outb
2359
2360/* silence compiler warnings */
2361u_char inb(u_int);
2362void outb(u_int, u_char);
2363
2364u_char
2365inb(u_int port)
2366{
2367	u_char	data;
2368	/*
2369	 * We use %%dx and not %1 here because i/o is done at %dx and not at
2370	 * %edx, while gcc generates inferior code (movw instead of movl)
2371	 * if we tell it to load (u_short) port.
2372	 */
2373	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2374	return (data);
2375}
2376
2377void
2378outb(u_int port, u_char data)
2379{
2380	u_char	al;
2381	/*
2382	 * Use an unnecessary assignment to help gcc's register allocator.
2383	 * This make a large difference for gcc-1.40 and a tiny difference
2384	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
2385	 * best results.  gcc-2.6.0 can't handle this.
2386	 */
2387	al = data;
2388	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
2389}
2390
2391#endif /* DDB */
2392