machdep.c revision 144637
1/*-
2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1992 Terrence R. Lambert.
4 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	from: @(#)machdep.c	7.4 (Berkeley) 6/3/91
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sys/amd64/amd64/machdep.c 144637 2005-04-04 21:53:56Z jhb $");
43
44#include "opt_atalk.h"
45#include "opt_atpic.h"
46#include "opt_compat.h"
47#include "opt_cpu.h"
48#include "opt_ddb.h"
49#include "opt_inet.h"
50#include "opt_ipx.h"
51#include "opt_isa.h"
52#include "opt_kstack_pages.h"
53#include "opt_maxmem.h"
54#include "opt_msgbuf.h"
55#include "opt_perfmon.h"
56
57#include <sys/param.h>
58#include <sys/proc.h>
59#include <sys/systm.h>
60#include <sys/bio.h>
61#include <sys/buf.h>
62#include <sys/bus.h>
63#include <sys/callout.h>
64#include <sys/cons.h>
65#include <sys/cpu.h>
66#include <sys/eventhandler.h>
67#include <sys/exec.h>
68#include <sys/imgact.h>
69#include <sys/kdb.h>
70#include <sys/kernel.h>
71#include <sys/ktr.h>
72#include <sys/linker.h>
73#include <sys/lock.h>
74#include <sys/malloc.h>
75#include <sys/memrange.h>
76#include <sys/msgbuf.h>
77#include <sys/mutex.h>
78#include <sys/pcpu.h>
79#include <sys/ptrace.h>
80#include <sys/reboot.h>
81#include <sys/sched.h>
82#include <sys/signalvar.h>
83#include <sys/sysctl.h>
84#include <sys/sysent.h>
85#include <sys/sysproto.h>
86#include <sys/ucontext.h>
87#include <sys/vmmeter.h>
88
89#include <vm/vm.h>
90#include <vm/vm_extern.h>
91#include <vm/vm_kern.h>
92#include <vm/vm_page.h>
93#include <vm/vm_map.h>
94#include <vm/vm_object.h>
95#include <vm/vm_pager.h>
96#include <vm/vm_param.h>
97
98#ifdef DDB
99#ifndef KDB
100#error KDB must be enabled in order for DDB to work!
101#endif
102#endif
103#include <ddb/ddb.h>
104
105#include <net/netisr.h>
106
107#include <machine/clock.h>
108#include <machine/cpu.h>
109#include <machine/cputypes.h>
110#include <machine/intr_machdep.h>
111#include <machine/md_var.h>
112#include <machine/metadata.h>
113#include <machine/pc/bios.h>
114#include <machine/pcb.h>
115#include <machine/proc.h>
116#include <machine/reg.h>
117#include <machine/sigframe.h>
118#include <machine/specialreg.h>
119#ifdef PERFMON
120#include <machine/perfmon.h>
121#endif
122#include <machine/tss.h>
123#ifdef SMP
124#include <machine/smp.h>
125#endif
126
127#include <amd64/isa/icu.h>
128
129#include <isa/isareg.h>
130#include <isa/rtc.h>
131
132/* Sanity check for __curthread() */
133CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
134
135extern u_int64_t hammer_time(u_int64_t, u_int64_t);
136extern void dblfault_handler(void);
137
138extern void printcpuinfo(void);	/* XXX header file */
139extern void identify_cpu(void);
140extern void panicifcpuunsupported(void);
141
142#define	CS_SECURE(cs)		(ISPL(cs) == SEL_UPL)
143#define	EFL_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
144
145static void cpu_startup(void *);
146static void get_fpcontext(struct thread *td, mcontext_t *mcp);
147static int  set_fpcontext(struct thread *td, const mcontext_t *mcp);
148SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
149
150#ifdef DDB
151extern vm_offset_t ksym_start, ksym_end;
152#endif
153
154int	_udatasel, _ucodesel, _ucode32sel;
155
156int cold = 1;
157
158long Maxmem = 0;
159long realmem = 0;
160
161vm_paddr_t phys_avail[20];
162
163/* must be 2 less so 0 0 can signal end of chunks */
164#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
165
166struct kva_md_info kmi;
167
168static struct trapframe proc0_tf;
169struct region_descriptor r_gdt, r_idt;
170
171struct pcpu __pcpu[MAXCPU];
172
173struct mtx icu_lock;
174
175struct mem_range_softc mem_range_softc;
176
177static void
178cpu_startup(dummy)
179	void *dummy;
180{
181	/*
182	 * Good {morning,afternoon,evening,night}.
183	 */
184	startrtclock();
185	printcpuinfo();
186	panicifcpuunsupported();
187#ifdef PERFMON
188	perfmon_init();
189#endif
190	printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)Maxmem),
191	    ptoa((uintmax_t)Maxmem) / 1048576);
192	realmem = Maxmem;
193	/*
194	 * Display any holes after the first chunk of extended memory.
195	 */
196	if (bootverbose) {
197		int indx;
198
199		printf("Physical memory chunk(s):\n");
200		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
201			vm_paddr_t size;
202
203			size = phys_avail[indx + 1] - phys_avail[indx];
204			printf(
205			    "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
206			    (uintmax_t)phys_avail[indx],
207			    (uintmax_t)phys_avail[indx + 1] - 1,
208			    (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
209		}
210	}
211
212	vm_ksubmap_init(&kmi);
213
214	printf("avail memory = %ju (%ju MB)\n",
215	    ptoa((uintmax_t)cnt.v_free_count),
216	    ptoa((uintmax_t)cnt.v_free_count) / 1048576);
217
218	/*
219	 * Set up buffers, so they can be used to read disk labels.
220	 */
221	bufinit();
222	vm_pager_bufferinit();
223
224	cpu_setregs();
225}
226
227/*
228 * Send an interrupt to process.
229 *
230 * Stack is set up to allow sigcode stored
231 * at top to call routine, followed by kcall
232 * to sigreturn routine below.  After sigreturn
233 * resets the signal mask, the stack, and the
234 * frame pointer, it returns to the user
235 * specified pc, psl.
236 */
237void
238sendsig(catcher, sig, mask, code)
239	sig_t catcher;
240	int sig;
241	sigset_t *mask;
242	u_long code;
243{
244	struct sigframe sf, *sfp;
245	struct proc *p;
246	struct thread *td;
247	struct sigacts *psp;
248	char *sp;
249	struct trapframe *regs;
250	int oonstack;
251
252	td = curthread;
253	p = td->td_proc;
254	PROC_LOCK_ASSERT(p, MA_OWNED);
255	psp = p->p_sigacts;
256	mtx_assert(&psp->ps_mtx, MA_OWNED);
257	regs = td->td_frame;
258	oonstack = sigonstack(regs->tf_rsp);
259
260	/* Save user context. */
261	bzero(&sf, sizeof(sf));
262	sf.sf_uc.uc_sigmask = *mask;
263	sf.sf_uc.uc_stack = td->td_sigstk;
264	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
265	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
266	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
267	bcopy(regs, &sf.sf_uc.uc_mcontext.mc_rdi, sizeof(*regs));
268	sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
269	get_fpcontext(td, &sf.sf_uc.uc_mcontext);
270	fpstate_drop(td);
271
272	/* Allocate space for the signal handler context. */
273	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
274	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
275		sp = td->td_sigstk.ss_sp +
276		    td->td_sigstk.ss_size - sizeof(struct sigframe);
277#if defined(COMPAT_43)
278		td->td_sigstk.ss_flags |= SS_ONSTACK;
279#endif
280	} else
281		sp = (char *)regs->tf_rsp - sizeof(struct sigframe) - 128;
282	/* Align to 16 bytes. */
283	sfp = (struct sigframe *)((unsigned long)sp & ~0xFul);
284
285	/* Translate the signal if appropriate. */
286	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
287		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
288
289	/* Build the argument list for the signal handler. */
290	regs->tf_rdi = sig;			/* arg 1 in %rdi */
291	regs->tf_rdx = (register_t)&sfp->sf_uc;	/* arg 3 in %rdx */
292	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
293		/* Signal handler installed with SA_SIGINFO. */
294		regs->tf_rsi = (register_t)&sfp->sf_si;	/* arg 2 in %rsi */
295		sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
296
297		/* Fill in POSIX parts */
298		sf.sf_si.si_signo = sig;
299		sf.sf_si.si_code = code;
300		regs->tf_rcx = regs->tf_addr;	/* arg 4 in %rcx */
301	} else {
302		/* Old FreeBSD-style arguments. */
303		regs->tf_rsi = code;		/* arg 2 in %rsi */
304		regs->tf_rcx = regs->tf_addr;	/* arg 4 in %rcx */
305		sf.sf_ahu.sf_handler = catcher;
306	}
307	mtx_unlock(&psp->ps_mtx);
308	PROC_UNLOCK(p);
309
310	/*
311	 * Copy the sigframe out to the user's stack.
312	 */
313	if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
314#ifdef DEBUG
315		printf("process %ld has trashed its stack\n", (long)p->p_pid);
316#endif
317		PROC_LOCK(p);
318		sigexit(td, SIGILL);
319	}
320
321	regs->tf_rsp = (long)sfp;
322	regs->tf_rip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
323	regs->tf_rflags &= ~PSL_T;
324	regs->tf_cs = _ucodesel;
325	PROC_LOCK(p);
326	mtx_lock(&psp->ps_mtx);
327}
328
329/*
330 * Build siginfo_t for SA thread
331 */
332void
333cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
334{
335	struct proc *p;
336	struct thread *td;
337	struct trapframe *regs;
338
339	td = curthread;
340	p = td->td_proc;
341	regs = td->td_frame;
342	PROC_LOCK_ASSERT(p, MA_OWNED);
343
344	bzero(si, sizeof(*si));
345	si->si_signo = sig;
346	si->si_code = code;
347	si->si_addr = (void *)regs->tf_addr;
348	/* XXXKSE fill other fields */
349}
350
351/*
352 * System call to cleanup state after a signal
353 * has been taken.  Reset signal mask and
354 * stack state from context left by sendsig (above).
355 * Return to previous pc and psl as specified by
356 * context left by sendsig. Check carefully to
357 * make sure that the user has not modified the
358 * state to gain improper privileges.
359 *
360 * MPSAFE
361 */
362int
363sigreturn(td, uap)
364	struct thread *td;
365	struct sigreturn_args /* {
366		const __ucontext *sigcntxp;
367	} */ *uap;
368{
369	ucontext_t uc;
370	struct proc *p = td->td_proc;
371	struct trapframe *regs;
372	const ucontext_t *ucp;
373	long rflags;
374	int cs, error, ret;
375
376	error = copyin(uap->sigcntxp, &uc, sizeof(uc));
377	if (error != 0)
378		return (error);
379	ucp = &uc;
380	regs = td->td_frame;
381	rflags = ucp->uc_mcontext.mc_rflags;
382	/*
383	 * Don't allow users to change privileged or reserved flags.
384	 */
385	/*
386	 * XXX do allow users to change the privileged flag PSL_RF.
387	 * The cpu sets PSL_RF in tf_rflags for faults.  Debuggers
388	 * should sometimes set it there too.  tf_rflags is kept in
389	 * the signal context during signal handling and there is no
390	 * other place to remember it, so the PSL_RF bit may be
391	 * corrupted by the signal handler without us knowing.
392	 * Corruption of the PSL_RF bit at worst causes one more or
393	 * one less debugger trap, so allowing it is fairly harmless.
394	 */
395	if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
396		printf("sigreturn: rflags = 0x%lx\n", rflags);
397		return (EINVAL);
398	}
399
400	/*
401	 * Don't allow users to load a valid privileged %cs.  Let the
402	 * hardware check for invalid selectors, excess privilege in
403	 * other selectors, invalid %eip's and invalid %esp's.
404	 */
405	cs = ucp->uc_mcontext.mc_cs;
406	if (!CS_SECURE(cs)) {
407		printf("sigreturn: cs = 0x%x\n", cs);
408		trapsignal(td, SIGBUS, T_PROTFLT);
409		return (EINVAL);
410	}
411
412	ret = set_fpcontext(td, &ucp->uc_mcontext);
413	if (ret != 0)
414		return (ret);
415	bcopy(&ucp->uc_mcontext.mc_rdi, regs, sizeof(*regs));
416
417	PROC_LOCK(p);
418#if defined(COMPAT_43)
419	if (ucp->uc_mcontext.mc_onstack & 1)
420		td->td_sigstk.ss_flags |= SS_ONSTACK;
421	else
422		td->td_sigstk.ss_flags &= ~SS_ONSTACK;
423#endif
424
425	td->td_sigmask = ucp->uc_sigmask;
426	SIG_CANTMASK(td->td_sigmask);
427	signotify(td);
428	PROC_UNLOCK(p);
429	td->td_pcb->pcb_flags |= PCB_FULLCTX;
430	return (EJUSTRETURN);
431}
432
433#ifdef COMPAT_FREEBSD4
434int
435freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
436{
437
438	return sigreturn(td, (struct sigreturn_args *)uap);
439}
440#endif
441
442
443/*
444 * Machine dependent boot() routine
445 *
446 * I haven't seen anything to put here yet
447 * Possibly some stuff might be grafted back here from boot()
448 */
449void
450cpu_boot(int howto)
451{
452}
453
454/* Get current clock frequency for the given cpu id. */
455int
456cpu_est_clockrate(int cpu_id, uint64_t *rate)
457{
458	register_t reg;
459	uint64_t tsc1, tsc2;
460
461	if (pcpu_find(cpu_id) == NULL || rate == NULL)
462		return (EINVAL);
463
464	/* If we're booting, trust the rate calibrated moments ago. */
465	if (cold) {
466		*rate = tsc_freq;
467		return (0);
468	}
469
470#ifdef SMP
471	/* Schedule ourselves on the indicated cpu. */
472	mtx_lock_spin(&sched_lock);
473	sched_bind(curthread, cpu_id);
474	mtx_unlock_spin(&sched_lock);
475#endif
476
477	/* Calibrate by measuring a short delay. */
478	reg = intr_disable();
479	tsc1 = rdtsc();
480	DELAY(1000);
481	tsc2 = rdtsc();
482	intr_restore(reg);
483
484#ifdef SMP
485	mtx_lock_spin(&sched_lock);
486	sched_unbind(curthread);
487	mtx_unlock_spin(&sched_lock);
488#endif
489
490	/*
491	 * Calculate the difference in readings, convert to Mhz, and
492	 * subtract 0.5% of the total.  Empirical testing has shown that
493	 * overhead in DELAY() works out to approximately this value.
494	 */
495	tsc2 -= tsc1;
496	*rate = tsc2 * 1000 - tsc2 * 5;
497	return (0);
498}
499
500/*
501 * Shutdown the CPU as much as possible
502 */
503void
504cpu_halt(void)
505{
506	for (;;)
507		__asm__ ("hlt");
508}
509
510/*
511 * Hook to idle the CPU when possible.  In the SMP case we default to
512 * off because a halted cpu will not currently pick up a new thread in the
513 * run queue until the next timer tick.  If turned on this will result in
514 * approximately a 4.2% loss in real time performance in buildworld tests
515 * (but improves user and sys times oddly enough), and saves approximately
516 * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3).
517 *
518 * XXX we need to have a cpu mask of idle cpus and generate an IPI or
519 * otherwise generate some sort of interrupt to wake up cpus sitting in HLT.
520 * Then we can have our cake and eat it too.
521 *
522 * XXX I'm turning it on for SMP as well by default for now.  It seems to
523 * help lock contention somewhat, and this is critical for HTT. -Peter
524 */
525static int	cpu_idle_hlt = 1;
526SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
527    &cpu_idle_hlt, 0, "Idle loop HLT enable");
528
529static void
530cpu_idle_default(void)
531{
532	/*
533	 * we must absolutely guarentee that hlt is the
534	 * absolute next instruction after sti or we
535	 * introduce a timing window.
536	 */
537	__asm __volatile("sti; hlt");
538}
539
540/*
541 * Note that we have to be careful here to avoid a race between checking
542 * sched_runnable() and actually halting.  If we don't do this, we may waste
543 * the time between calling hlt and the next interrupt even though there
544 * is a runnable process.
545 */
546void
547cpu_idle(void)
548{
549
550#ifdef SMP
551	if (mp_grab_cpu_hlt())
552		return;
553#endif
554	if (cpu_idle_hlt) {
555		disable_intr();
556  		if (sched_runnable())
557			enable_intr();
558		else
559			(*cpu_idle_hook)();
560	}
561}
562
563/* Other subsystems (e.g., ACPI) can hook this later. */
564void (*cpu_idle_hook)(void) = cpu_idle_default;
565
566/*
567 * Clear registers on exec
568 */
569void
570exec_setregs(td, entry, stack, ps_strings)
571	struct thread *td;
572	u_long entry;
573	u_long stack;
574	u_long ps_strings;
575{
576	struct trapframe *regs = td->td_frame;
577	struct pcb *pcb = td->td_pcb;
578
579	wrmsr(MSR_FSBASE, 0);
580	wrmsr(MSR_KGSBASE, 0);	/* User value while we're in the kernel */
581	pcb->pcb_fsbase = 0;
582	pcb->pcb_gsbase = 0;
583	load_ds(_udatasel);
584	load_es(_udatasel);
585	load_fs(_udatasel);
586	load_gs(_udatasel);
587	pcb->pcb_ds = _udatasel;
588	pcb->pcb_es = _udatasel;
589	pcb->pcb_fs = _udatasel;
590	pcb->pcb_gs = _udatasel;
591
592	bzero((char *)regs, sizeof(struct trapframe));
593	regs->tf_rip = entry;
594	regs->tf_rsp = ((stack - 8) & ~0xFul) + 8;
595	regs->tf_rdi = stack;		/* argv */
596	regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T);
597	regs->tf_ss = _udatasel;
598	regs->tf_cs = _ucodesel;
599
600	/*
601	 * Reset the hardware debug registers if they were in use.
602	 * They won't have any meaning for the newly exec'd process.
603	 */
604	if (pcb->pcb_flags & PCB_DBREGS) {
605		pcb->pcb_dr0 = 0;
606		pcb->pcb_dr1 = 0;
607		pcb->pcb_dr2 = 0;
608		pcb->pcb_dr3 = 0;
609		pcb->pcb_dr6 = 0;
610		pcb->pcb_dr7 = 0;
611		if (pcb == PCPU_GET(curpcb)) {
612			/*
613			 * Clear the debug registers on the running
614			 * CPU, otherwise they will end up affecting
615			 * the next process we switch to.
616			 */
617			reset_dbregs();
618		}
619		pcb->pcb_flags &= ~PCB_DBREGS;
620	}
621
622	/*
623	 * Drop the FP state if we hold it, so that the process gets a
624	 * clean FP state if it uses the FPU again.
625	 */
626	fpstate_drop(td);
627}
628
629void
630cpu_setregs(void)
631{
632	register_t cr0;
633
634	cr0 = rcr0();
635	/*
636	 * CR0_MP, CR0_NE and CR0_TS are also set by npx_probe() for the
637	 * BSP.  See the comments there about why we set them.
638	 */
639	cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
640	load_cr0(cr0);
641}
642
643static int
644sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
645{
646	int error;
647	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
648		req);
649	if (!error && req->newptr)
650		resettodr();
651	return (error);
652}
653
654SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
655	&adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
656
657SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
658	CTLFLAG_RW, &disable_rtc_set, 0, "");
659
660SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
661	CTLFLAG_RW, &wall_cmos_clock, 0, "");
662
663/*
664 * Initialize 386 and configure to run kernel
665 */
666
667/*
668 * Initialize segments & interrupt table
669 */
670
671struct user_segment_descriptor gdt[NGDT * MAXCPU];/* global descriptor table */
672static struct gate_descriptor idt0[NIDT];
673struct gate_descriptor *idt = &idt0[0];	/* interrupt descriptor table */
674
675static char dblfault_stack[PAGE_SIZE] __aligned(16);
676
677struct amd64tss common_tss[MAXCPU];
678
679/* software prototypes -- in more palatable form */
680struct soft_segment_descriptor gdt_segs[] = {
681/* GNULL_SEL	0 Null Descriptor */
682{	0x0,			/* segment base address  */
683	0x0,			/* length */
684	0,			/* segment type */
685	0,			/* segment descriptor priority level */
686	0,			/* segment descriptor present */
687	0,			/* long */
688	0,			/* default 32 vs 16 bit size */
689	0  			/* limit granularity (byte/page units)*/ },
690/* GCODE_SEL	1 Code Descriptor for kernel */
691{	0x0,			/* segment base address  */
692	0xfffff,		/* length - all address space */
693	SDT_MEMERA,		/* segment type */
694	SEL_KPL,		/* segment descriptor priority level */
695	1,			/* segment descriptor present */
696	1,			/* long */
697	0,			/* default 32 vs 16 bit size */
698	1  			/* limit granularity (byte/page units)*/ },
699/* GDATA_SEL	2 Data Descriptor for kernel */
700{	0x0,			/* segment base address  */
701	0xfffff,		/* length - all address space */
702	SDT_MEMRWA,		/* segment type */
703	SEL_KPL,		/* segment descriptor priority level */
704	1,			/* segment descriptor present */
705	1,			/* long */
706	0,			/* default 32 vs 16 bit size */
707	1  			/* limit granularity (byte/page units)*/ },
708/* GUCODE32_SEL	3 32 bit Code Descriptor for user */
709{	0x0,			/* segment base address  */
710	0xfffff,		/* length - all address space */
711	SDT_MEMERA,		/* segment type */
712	SEL_UPL,		/* segment descriptor priority level */
713	1,			/* segment descriptor present */
714	0,			/* long */
715	1,			/* default 32 vs 16 bit size */
716	1  			/* limit granularity (byte/page units)*/ },
717/* GUDATA_SEL	4 32/64 bit Data Descriptor for user */
718{	0x0,			/* segment base address  */
719	0xfffff,		/* length - all address space */
720	SDT_MEMRWA,		/* segment type */
721	SEL_UPL,		/* segment descriptor priority level */
722	1,			/* segment descriptor present */
723	0,			/* long */
724	1,			/* default 32 vs 16 bit size */
725	1  			/* limit granularity (byte/page units)*/ },
726/* GUCODE_SEL	5 64 bit Code Descriptor for user */
727{	0x0,			/* segment base address  */
728	0xfffff,		/* length - all address space */
729	SDT_MEMERA,		/* segment type */
730	SEL_UPL,		/* segment descriptor priority level */
731	1,			/* segment descriptor present */
732	1,			/* long */
733	0,			/* default 32 vs 16 bit size */
734	1  			/* limit granularity (byte/page units)*/ },
735/* GPROC0_SEL	6 Proc 0 Tss Descriptor */
736{
737	0x0,			/* segment base address */
738	sizeof(struct amd64tss)-1,/* length - all address space */
739	SDT_SYSTSS,		/* segment type */
740	SEL_KPL,		/* segment descriptor priority level */
741	1,			/* segment descriptor present */
742	0,			/* long */
743	0,			/* unused - default 32 vs 16 bit size */
744	0  			/* limit granularity (byte/page units)*/ },
745/* Actually, the TSS is a system descriptor which is double size */
746{	0x0,			/* segment base address  */
747	0x0,			/* length */
748	0,			/* segment type */
749	0,			/* segment descriptor priority level */
750	0,			/* segment descriptor present */
751	0,			/* long */
752	0,			/* default 32 vs 16 bit size */
753	0  			/* limit granularity (byte/page units)*/ },
754};
755
756void
757setidt(idx, func, typ, dpl, ist)
758	int idx;
759	inthand_t *func;
760	int typ;
761	int dpl;
762	int ist;
763{
764	struct gate_descriptor *ip;
765
766	ip = idt + idx;
767	ip->gd_looffset = (uintptr_t)func;
768	ip->gd_selector = GSEL(GCODE_SEL, SEL_KPL);
769	ip->gd_ist = ist;
770	ip->gd_xx = 0;
771	ip->gd_type = typ;
772	ip->gd_dpl = dpl;
773	ip->gd_p = 1;
774	ip->gd_hioffset = ((uintptr_t)func)>>16 ;
775}
776
777#define	IDTVEC(name)	__CONCAT(X,name)
778
779extern inthand_t
780	IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
781	IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
782	IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
783	IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
784	IDTVEC(xmm), IDTVEC(dblfault),
785	IDTVEC(fast_syscall), IDTVEC(fast_syscall32);
786
787void
788sdtossd(sd, ssd)
789	struct user_segment_descriptor *sd;
790	struct soft_segment_descriptor *ssd;
791{
792
793	ssd->ssd_base  = (sd->sd_hibase << 24) | sd->sd_lobase;
794	ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
795	ssd->ssd_type  = sd->sd_type;
796	ssd->ssd_dpl   = sd->sd_dpl;
797	ssd->ssd_p     = sd->sd_p;
798	ssd->ssd_long  = sd->sd_long;
799	ssd->ssd_def32 = sd->sd_def32;
800	ssd->ssd_gran  = sd->sd_gran;
801}
802
803void
804ssdtosd(ssd, sd)
805	struct soft_segment_descriptor *ssd;
806	struct user_segment_descriptor *sd;
807{
808
809	sd->sd_lobase = (ssd->ssd_base) & 0xffffff;
810	sd->sd_hibase = (ssd->ssd_base >> 24) & 0xff;
811	sd->sd_lolimit = (ssd->ssd_limit) & 0xffff;
812	sd->sd_hilimit = (ssd->ssd_limit >> 16) & 0xf;
813	sd->sd_type  = ssd->ssd_type;
814	sd->sd_dpl   = ssd->ssd_dpl;
815	sd->sd_p     = ssd->ssd_p;
816	sd->sd_long  = ssd->ssd_long;
817	sd->sd_def32 = ssd->ssd_def32;
818	sd->sd_gran  = ssd->ssd_gran;
819}
820
821void
822ssdtosyssd(ssd, sd)
823	struct soft_segment_descriptor *ssd;
824	struct system_segment_descriptor *sd;
825{
826
827	sd->sd_lobase = (ssd->ssd_base) & 0xffffff;
828	sd->sd_hibase = (ssd->ssd_base >> 24) & 0xfffffffffful;
829	sd->sd_lolimit = (ssd->ssd_limit) & 0xffff;
830	sd->sd_hilimit = (ssd->ssd_limit >> 16) & 0xf;
831	sd->sd_type  = ssd->ssd_type;
832	sd->sd_dpl   = ssd->ssd_dpl;
833	sd->sd_p     = ssd->ssd_p;
834	sd->sd_gran  = ssd->ssd_gran;
835}
836
837#if !defined(DEV_ATPIC) && defined(DEV_ISA)
838#include <isa/isavar.h>
839u_int
840isa_irq_pending(void)
841{
842
843	return (0);
844}
845#endif
846
847#define PHYSMAP_SIZE	(2 * 8)
848
849u_int basemem;
850
851/*
852 * Populate the (physmap) array with base/bound pairs describing the
853 * available physical memory in the system, then test this memory and
854 * build the phys_avail array describing the actually-available memory.
855 *
856 * If we cannot accurately determine the physical memory map, then use
857 * value from the 0xE801 call, and failing that, the RTC.
858 *
859 * Total memory size may be set by the kernel environment variable
860 * hw.physmem or the compile-time define MAXMEM.
861 *
862 * XXX first should be vm_paddr_t.
863 */
864static void
865getmemsize(caddr_t kmdp, u_int64_t first)
866{
867	int i, physmap_idx, pa_indx;
868	vm_paddr_t pa, physmap[PHYSMAP_SIZE];
869	u_long physmem_tunable;
870	pt_entry_t *pte;
871	struct bios_smap *smapbase, *smap, *smapend;
872	u_int32_t smapsize;
873	quad_t dcons_addr, dcons_size;
874
875	bzero(physmap, sizeof(physmap));
876	basemem = 0;
877	physmap_idx = 0;
878
879	/*
880	 * get memory map from INT 15:E820, kindly supplied by the loader.
881	 *
882	 * subr_module.c says:
883	 * "Consumer may safely assume that size value precedes data."
884	 * ie: an int32_t immediately precedes smap.
885	 */
886	smapbase = (struct bios_smap *)preload_search_info(kmdp,
887	    MODINFO_METADATA | MODINFOMD_SMAP);
888	if (smapbase == NULL)
889		panic("No BIOS smap info from loader!");
890
891	smapsize = *((u_int32_t *)smapbase - 1);
892	smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
893
894	for (smap = smapbase; smap < smapend; smap++) {
895		if (boothowto & RB_VERBOSE)
896			printf("SMAP type=%02x base=%016lx len=%016lx\n",
897			    smap->type, smap->base, smap->length);
898
899		if (smap->type != 0x01)
900			continue;
901
902		if (smap->length == 0)
903			continue;
904
905		for (i = 0; i <= physmap_idx; i += 2) {
906			if (smap->base < physmap[i + 1]) {
907				if (boothowto & RB_VERBOSE)
908					printf(
909	"Overlapping or non-montonic memory region, ignoring second region\n");
910				continue;
911			}
912		}
913
914		if (smap->base == physmap[physmap_idx + 1]) {
915			physmap[physmap_idx + 1] += smap->length;
916			continue;
917		}
918
919		physmap_idx += 2;
920		if (physmap_idx == PHYSMAP_SIZE) {
921			printf(
922		"Too many segments in the physical address map, giving up\n");
923			break;
924		}
925		physmap[physmap_idx] = smap->base;
926		physmap[physmap_idx + 1] = smap->base + smap->length;
927	}
928
929	/*
930	 * Find the 'base memory' segment for SMP
931	 */
932	basemem = 0;
933	for (i = 0; i <= physmap_idx; i += 2) {
934		if (physmap[i] == 0x00000000) {
935			basemem = physmap[i + 1] / 1024;
936			break;
937		}
938	}
939	if (basemem == 0)
940		panic("BIOS smap did not include a basemem segment!");
941
942#ifdef SMP
943	/* make hole for AP bootstrap code */
944	physmap[1] = mp_bootaddress(physmap[1] / 1024);
945#endif
946
947	/*
948	 * Maxmem isn't the "maximum memory", it's one larger than the
949	 * highest page of the physical address space.  It should be
950	 * called something like "Maxphyspage".  We may adjust this
951	 * based on ``hw.physmem'' and the results of the memory test.
952	 */
953	Maxmem = atop(physmap[physmap_idx + 1]);
954
955#ifdef MAXMEM
956	Maxmem = MAXMEM / 4;
957#endif
958
959	if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
960		Maxmem = atop(physmem_tunable);
961
962	if (atop(physmap[physmap_idx + 1]) != Maxmem &&
963	    (boothowto & RB_VERBOSE))
964		printf("Physical memory use set to %ldK\n", Maxmem * 4);
965
966	/*
967	 * If Maxmem has been increased beyond what the system has detected,
968	 * extend the last memory segment to the new limit.
969	 */
970	if (atop(physmap[physmap_idx + 1]) < Maxmem)
971		physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
972
973	/* call pmap initialization to make new kernel address space */
974	pmap_bootstrap(&first);
975
976	/*
977	 * Size up each available chunk of physical memory.
978	 */
979	physmap[0] = PAGE_SIZE;		/* mask off page 0 */
980	pa_indx = 0;
981	phys_avail[pa_indx++] = physmap[0];
982	phys_avail[pa_indx] = physmap[0];
983	pte = CMAP1;
984
985	/*
986	 * Get dcons buffer address
987	 */
988	if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
989	    getenv_quad("dcons.size", &dcons_size) == 0)
990		dcons_addr = 0;
991
992	/*
993	 * physmap is in bytes, so when converting to page boundaries,
994	 * round up the start address and round down the end address.
995	 */
996	for (i = 0; i <= physmap_idx; i += 2) {
997		vm_paddr_t end;
998
999		end = ptoa((vm_paddr_t)Maxmem);
1000		if (physmap[i + 1] < end)
1001			end = trunc_page(physmap[i + 1]);
1002		for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1003			int tmp, page_bad;
1004			int *ptr = (int *)CADDR1;
1005
1006			/*
1007			 * block out kernel memory as not available.
1008			 */
1009			if (pa >= 0x100000 && pa < first)
1010				continue;
1011
1012 			/*
1013 			 * block out dcons buffer
1014 			 */
1015 			if (dcons_addr > 0
1016 			    && pa >= trunc_page(dcons_addr)
1017 			    && pa < dcons_addr + dcons_size)
1018 				continue;
1019
1020			page_bad = FALSE;
1021
1022			/*
1023			 * map page into kernel: valid, read/write,non-cacheable
1024			 */
1025			*pte = pa | PG_V | PG_RW | PG_N;
1026			invltlb();
1027
1028			tmp = *(int *)ptr;
1029			/*
1030			 * Test for alternating 1's and 0's
1031			 */
1032			*(volatile int *)ptr = 0xaaaaaaaa;
1033			if (*(volatile int *)ptr != 0xaaaaaaaa)
1034				page_bad = TRUE;
1035			/*
1036			 * Test for alternating 0's and 1's
1037			 */
1038			*(volatile int *)ptr = 0x55555555;
1039			if (*(volatile int *)ptr != 0x55555555)
1040				page_bad = TRUE;
1041			/*
1042			 * Test for all 1's
1043			 */
1044			*(volatile int *)ptr = 0xffffffff;
1045			if (*(volatile int *)ptr != 0xffffffff)
1046				page_bad = TRUE;
1047			/*
1048			 * Test for all 0's
1049			 */
1050			*(volatile int *)ptr = 0x0;
1051			if (*(volatile int *)ptr != 0x0)
1052				page_bad = TRUE;
1053			/*
1054			 * Restore original value.
1055			 */
1056			*(int *)ptr = tmp;
1057
1058			/*
1059			 * Adjust array of valid/good pages.
1060			 */
1061			if (page_bad == TRUE)
1062				continue;
1063			/*
1064			 * If this good page is a continuation of the
1065			 * previous set of good pages, then just increase
1066			 * the end pointer. Otherwise start a new chunk.
1067			 * Note that "end" points one higher than end,
1068			 * making the range >= start and < end.
1069			 * If we're also doing a speculative memory
1070			 * test and we at or past the end, bump up Maxmem
1071			 * so that we keep going. The first bad page
1072			 * will terminate the loop.
1073			 */
1074			if (phys_avail[pa_indx] == pa) {
1075				phys_avail[pa_indx] += PAGE_SIZE;
1076			} else {
1077				pa_indx++;
1078				if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1079					printf(
1080		"Too many holes in the physical address space, giving up\n");
1081					pa_indx--;
1082					break;
1083				}
1084				phys_avail[pa_indx++] = pa;	/* start */
1085				phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1086			}
1087			physmem++;
1088		}
1089	}
1090	*pte = 0;
1091	invltlb();
1092
1093	/*
1094	 * XXX
1095	 * The last chunk must contain at least one page plus the message
1096	 * buffer to avoid complicating other code (message buffer address
1097	 * calculation, etc.).
1098	 */
1099	while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1100	    round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1101		physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1102		phys_avail[pa_indx--] = 0;
1103		phys_avail[pa_indx--] = 0;
1104	}
1105
1106	Maxmem = atop(phys_avail[pa_indx]);
1107
1108	/* Trim off space for the message buffer. */
1109	phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1110
1111	avail_end = phys_avail[pa_indx];
1112}
1113
1114u_int64_t
1115hammer_time(u_int64_t modulep, u_int64_t physfree)
1116{
1117	caddr_t kmdp;
1118	int gsel_tss, off, x;
1119	struct pcpu *pc;
1120	u_int64_t msr;
1121	char *env;
1122
1123#ifdef DEV_ISA
1124	/* Preemptively mask the atpics and leave them shut down */
1125	outb(IO_ICU1 + ICU_IMR_OFFSET, 0xff);
1126	outb(IO_ICU2 + ICU_IMR_OFFSET, 0xff);
1127#else
1128#error "have you forgotten the isa device?";
1129#endif
1130
1131	thread0.td_kstack = physfree + KERNBASE;
1132	bzero((void *)thread0.td_kstack, KSTACK_PAGES * PAGE_SIZE);
1133	physfree += KSTACK_PAGES * PAGE_SIZE;
1134	thread0.td_pcb = (struct pcb *)
1135	   (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1136
1137	/*
1138 	 * This may be done better later if it gets more high level
1139 	 * components in it. If so just link td->td_proc here.
1140	 */
1141	proc_linkup(&proc0, &ksegrp0, &thread0);
1142
1143	preload_metadata = (caddr_t)(uintptr_t)(modulep + KERNBASE);
1144	preload_bootstrap_relocate(KERNBASE);
1145	kmdp = preload_search_by_type("elf kernel");
1146	if (kmdp == NULL)
1147		kmdp = preload_search_by_type("elf64 kernel");
1148	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
1149	kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *) + KERNBASE;
1150#ifdef DDB
1151	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
1152	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
1153#endif
1154
1155	/* Init basic tunables, hz etc */
1156	init_param1();
1157
1158	/*
1159	 * make gdt memory segments
1160	 */
1161	gdt_segs[GPROC0_SEL].ssd_base = (uintptr_t)&common_tss[0];
1162
1163	for (x = 0; x < NGDT; x++) {
1164		if (x != GPROC0_SEL && x != (GPROC0_SEL + 1))
1165			ssdtosd(&gdt_segs[x], &gdt[x]);
1166	}
1167	ssdtosyssd(&gdt_segs[GPROC0_SEL],
1168	    (struct system_segment_descriptor *)&gdt[GPROC0_SEL]);
1169
1170	r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1171	r_gdt.rd_base =  (long) gdt;
1172	lgdt(&r_gdt);
1173	pc = &__pcpu[0];
1174
1175	wrmsr(MSR_FSBASE, 0);		/* User value */
1176	wrmsr(MSR_GSBASE, (u_int64_t)pc);
1177	wrmsr(MSR_KGSBASE, 0);		/* User value while in the kernel */
1178
1179	pcpu_init(pc, 0, sizeof(struct pcpu));
1180	PCPU_SET(prvspace, pc);
1181	PCPU_SET(curthread, &thread0);
1182	PCPU_SET(curpcb, thread0.td_pcb);
1183	PCPU_SET(tssp, &common_tss[0]);
1184
1185	/*
1186	 * Initialize mutexes.
1187	 *
1188	 * icu_lock: in order to allow an interrupt to occur in a critical
1189	 * 	     section, to set pcpu->ipending (etc...) properly, we
1190	 *	     must be able to get the icu lock, so it can't be
1191	 *	     under witness.
1192	 */
1193	mutex_init();
1194	mtx_init(&clock_lock, "clk", NULL, MTX_SPIN);
1195	mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS);
1196
1197	/* exceptions */
1198	for (x = 0; x < NIDT; x++)
1199		setidt(x, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
1200	setidt(IDT_DE, &IDTVEC(div),  SDT_SYSIGT, SEL_KPL, 0);
1201	setidt(IDT_DB, &IDTVEC(dbg),  SDT_SYSIGT, SEL_KPL, 0);
1202	setidt(IDT_NMI, &IDTVEC(nmi),  SDT_SYSIGT, SEL_KPL, 0);
1203 	setidt(IDT_BP, &IDTVEC(bpt),  SDT_SYSIGT, SEL_UPL, 0);
1204	setidt(IDT_OF, &IDTVEC(ofl),  SDT_SYSIGT, SEL_KPL, 0);
1205	setidt(IDT_BR, &IDTVEC(bnd),  SDT_SYSIGT, SEL_KPL, 0);
1206	setidt(IDT_UD, &IDTVEC(ill),  SDT_SYSIGT, SEL_KPL, 0);
1207	setidt(IDT_NM, &IDTVEC(dna),  SDT_SYSIGT, SEL_KPL, 0);
1208	setidt(IDT_DF, &IDTVEC(dblfault), SDT_SYSIGT, SEL_KPL, 1);
1209	setidt(IDT_FPUGP, &IDTVEC(fpusegm),  SDT_SYSIGT, SEL_KPL, 0);
1210	setidt(IDT_TS, &IDTVEC(tss),  SDT_SYSIGT, SEL_KPL, 0);
1211	setidt(IDT_NP, &IDTVEC(missing),  SDT_SYSIGT, SEL_KPL, 0);
1212	setidt(IDT_SS, &IDTVEC(stk),  SDT_SYSIGT, SEL_KPL, 0);
1213	setidt(IDT_GP, &IDTVEC(prot),  SDT_SYSIGT, SEL_KPL, 0);
1214	setidt(IDT_PF, &IDTVEC(page),  SDT_SYSIGT, SEL_KPL, 0);
1215	setidt(IDT_MF, &IDTVEC(fpu),  SDT_SYSIGT, SEL_KPL, 0);
1216	setidt(IDT_AC, &IDTVEC(align), SDT_SYSIGT, SEL_KPL, 0);
1217	setidt(IDT_MC, &IDTVEC(mchk),  SDT_SYSIGT, SEL_KPL, 0);
1218	setidt(IDT_XF, &IDTVEC(xmm), SDT_SYSIGT, SEL_KPL, 0);
1219
1220	r_idt.rd_limit = sizeof(idt0) - 1;
1221	r_idt.rd_base = (long) idt;
1222	lidt(&r_idt);
1223
1224	/*
1225	 * Initialize the console before we print anything out.
1226	 */
1227	cninit();
1228
1229#ifdef DEV_ATPIC
1230	elcr_probe();
1231	atpic_startup();
1232#endif
1233
1234	kdb_init();
1235
1236#ifdef KDB
1237	if (boothowto & RB_KDB)
1238		kdb_enter("Boot flags requested debugger");
1239#endif
1240
1241	identify_cpu();		/* Final stage of CPU initialization */
1242	initializecpu();	/* Initialize CPU registers */
1243
1244	/* make an initial tss so cpu can get interrupt stack on syscall! */
1245	common_tss[0].tss_rsp0 = thread0.td_kstack + \
1246	    KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb);
1247	/* Ensure the stack is aligned to 16 bytes */
1248	common_tss[0].tss_rsp0 &= ~0xFul;
1249	PCPU_SET(rsp0, common_tss[0].tss_rsp0);
1250
1251	/* doublefault stack space, runs on ist1 */
1252	common_tss[0].tss_ist1 = (long)&dblfault_stack[sizeof(dblfault_stack)];
1253
1254	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1255	ltr(gsel_tss);
1256
1257	/* Set up the fast syscall stuff */
1258	msr = rdmsr(MSR_EFER) | EFER_SCE;
1259	wrmsr(MSR_EFER, msr);
1260	wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall));
1261	wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32));
1262	msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) |
1263	      ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48);
1264	wrmsr(MSR_STAR, msr);
1265	wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D);
1266
1267	getmemsize(kmdp, physfree);
1268	init_param2(physmem);
1269
1270	/* now running on new page tables, configured,and u/iom is accessible */
1271
1272	/* Map the message buffer. */
1273	for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1274		pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1275
1276	msgbufinit(msgbufp, MSGBUF_SIZE);
1277	fpuinit();
1278
1279	/* transfer to user mode */
1280
1281	_ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
1282	_udatasel = GSEL(GUDATA_SEL, SEL_UPL);
1283	_ucode32sel = GSEL(GUCODE32_SEL, SEL_UPL);
1284
1285	/* setup proc 0's pcb */
1286	thread0.td_pcb->pcb_flags = 0; /* XXXKSE */
1287	thread0.td_pcb->pcb_cr3 = KPML4phys;
1288	thread0.td_frame = &proc0_tf;
1289
1290        env = getenv("kernelname");
1291	if (env != NULL)
1292		strlcpy(kernelname, env, sizeof(kernelname));
1293
1294	/* Location of kernel stack for locore */
1295	return ((u_int64_t)thread0.td_pcb);
1296}
1297
1298void
1299cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
1300{
1301
1302	pcpu->pc_acpi_id = 0xffffffff;
1303}
1304
1305void
1306spinlock_enter(void)
1307{
1308	struct thread *td;
1309
1310	td = curthread;
1311	if (td->td_md.md_spinlock_count == 0)
1312		td->td_md.md_saved_flags = intr_disable();
1313	td->td_md.md_spinlock_count++;
1314	critical_enter();
1315}
1316
1317void
1318spinlock_exit(void)
1319{
1320	struct thread *td;
1321
1322	td = curthread;
1323	critical_exit();
1324	td->td_md.md_spinlock_count--;
1325	if (td->td_md.md_spinlock_count == 0)
1326		intr_restore(td->td_md.md_saved_flags);
1327}
1328
1329/*
1330 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1331 * we want to start a backtrace from the function that caused us to enter
1332 * the debugger. We have the context in the trapframe, but base the trace
1333 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1334 * enough for a backtrace.
1335 */
1336void
1337makectx(struct trapframe *tf, struct pcb *pcb)
1338{
1339
1340	pcb->pcb_r12 = tf->tf_r12;
1341	pcb->pcb_r13 = tf->tf_r13;
1342	pcb->pcb_r14 = tf->tf_r14;
1343	pcb->pcb_r15 = tf->tf_r15;
1344	pcb->pcb_rbp = tf->tf_rbp;
1345	pcb->pcb_rbx = tf->tf_rbx;
1346	pcb->pcb_rip = tf->tf_rip;
1347	pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8;
1348}
1349
1350int
1351ptrace_set_pc(struct thread *td, unsigned long addr)
1352{
1353	td->td_frame->tf_rip = addr;
1354	return (0);
1355}
1356
1357int
1358ptrace_single_step(struct thread *td)
1359{
1360	td->td_frame->tf_rflags |= PSL_T;
1361	return (0);
1362}
1363
1364int
1365ptrace_clear_single_step(struct thread *td)
1366{
1367	td->td_frame->tf_rflags &= ~PSL_T;
1368	return (0);
1369}
1370
1371int
1372fill_regs(struct thread *td, struct reg *regs)
1373{
1374	struct pcb *pcb;
1375	struct trapframe *tp;
1376
1377	tp = td->td_frame;
1378	regs->r_r15 = tp->tf_r15;
1379	regs->r_r14 = tp->tf_r14;
1380	regs->r_r13 = tp->tf_r13;
1381	regs->r_r12 = tp->tf_r12;
1382	regs->r_r11 = tp->tf_r11;
1383	regs->r_r10 = tp->tf_r10;
1384	regs->r_r9  = tp->tf_r9;
1385	regs->r_r8  = tp->tf_r8;
1386	regs->r_rdi = tp->tf_rdi;
1387	regs->r_rsi = tp->tf_rsi;
1388	regs->r_rbp = tp->tf_rbp;
1389	regs->r_rbx = tp->tf_rbx;
1390	regs->r_rdx = tp->tf_rdx;
1391	regs->r_rcx = tp->tf_rcx;
1392	regs->r_rax = tp->tf_rax;
1393	regs->r_rip = tp->tf_rip;
1394	regs->r_cs = tp->tf_cs;
1395	regs->r_rflags = tp->tf_rflags;
1396	regs->r_rsp = tp->tf_rsp;
1397	regs->r_ss = tp->tf_ss;
1398	pcb = td->td_pcb;
1399	return (0);
1400}
1401
1402int
1403set_regs(struct thread *td, struct reg *regs)
1404{
1405	struct pcb *pcb;
1406	struct trapframe *tp;
1407	register_t rflags;
1408
1409	tp = td->td_frame;
1410	rflags = regs->r_rflags & 0xffffffff;
1411	if (!EFL_SECURE(rflags, tp->tf_rflags) || !CS_SECURE(regs->r_cs))
1412		return (EINVAL);
1413	tp->tf_r15 = regs->r_r15;
1414	tp->tf_r14 = regs->r_r14;
1415	tp->tf_r13 = regs->r_r13;
1416	tp->tf_r12 = regs->r_r12;
1417	tp->tf_r11 = regs->r_r11;
1418	tp->tf_r10 = regs->r_r10;
1419	tp->tf_r9  = regs->r_r9;
1420	tp->tf_r8  = regs->r_r8;
1421	tp->tf_rdi = regs->r_rdi;
1422	tp->tf_rsi = regs->r_rsi;
1423	tp->tf_rbp = regs->r_rbp;
1424	tp->tf_rbx = regs->r_rbx;
1425	tp->tf_rdx = regs->r_rdx;
1426	tp->tf_rcx = regs->r_rcx;
1427	tp->tf_rax = regs->r_rax;
1428	tp->tf_rip = regs->r_rip;
1429	tp->tf_cs = regs->r_cs;
1430	tp->tf_rflags = rflags;
1431	tp->tf_rsp = regs->r_rsp;
1432	tp->tf_ss = regs->r_ss;
1433	pcb = td->td_pcb;
1434	return (0);
1435}
1436
1437/* XXX check all this stuff! */
1438/* externalize from sv_xmm */
1439static void
1440fill_fpregs_xmm(struct savefpu *sv_xmm, struct fpreg *fpregs)
1441{
1442	struct envxmm *penv_fpreg = (struct envxmm *)&fpregs->fpr_env;
1443	struct envxmm *penv_xmm = &sv_xmm->sv_env;
1444	int i;
1445
1446	/* pcb -> fpregs */
1447	bzero(fpregs, sizeof(*fpregs));
1448
1449	/* FPU control/status */
1450	penv_fpreg->en_cw = penv_xmm->en_cw;
1451	penv_fpreg->en_sw = penv_xmm->en_sw;
1452	penv_fpreg->en_tw = penv_xmm->en_tw;
1453	penv_fpreg->en_opcode = penv_xmm->en_opcode;
1454	penv_fpreg->en_rip = penv_xmm->en_rip;
1455	penv_fpreg->en_rdp = penv_xmm->en_rdp;
1456	penv_fpreg->en_mxcsr = penv_xmm->en_mxcsr;
1457	penv_fpreg->en_mxcsr_mask = penv_xmm->en_mxcsr_mask;
1458
1459	/* FPU registers */
1460	for (i = 0; i < 8; ++i)
1461		bcopy(sv_xmm->sv_fp[i].fp_acc.fp_bytes, fpregs->fpr_acc[i], 10);
1462
1463	/* SSE registers */
1464	for (i = 0; i < 16; ++i)
1465		bcopy(sv_xmm->sv_xmm[i].xmm_bytes, fpregs->fpr_xacc[i], 16);
1466}
1467
1468/* internalize from fpregs into sv_xmm */
1469static void
1470set_fpregs_xmm(struct fpreg *fpregs, struct savefpu *sv_xmm)
1471{
1472	struct envxmm *penv_xmm = &sv_xmm->sv_env;
1473	struct envxmm *penv_fpreg = (struct envxmm *)&fpregs->fpr_env;
1474	int i;
1475
1476	/* fpregs -> pcb */
1477	/* FPU control/status */
1478	penv_xmm->en_cw = penv_fpreg->en_cw;
1479	penv_xmm->en_sw = penv_fpreg->en_sw;
1480	penv_xmm->en_tw = penv_fpreg->en_tw;
1481	penv_xmm->en_opcode = penv_fpreg->en_opcode;
1482	penv_xmm->en_rip = penv_fpreg->en_rip;
1483	penv_xmm->en_rdp = penv_fpreg->en_rdp;
1484	penv_xmm->en_mxcsr = penv_fpreg->en_mxcsr;
1485	penv_xmm->en_mxcsr_mask = penv_fpreg->en_mxcsr_mask;
1486
1487	/* FPU registers */
1488	for (i = 0; i < 8; ++i)
1489		bcopy(fpregs->fpr_acc[i], sv_xmm->sv_fp[i].fp_acc.fp_bytes, 10);
1490
1491	/* SSE registers */
1492	for (i = 0; i < 16; ++i)
1493		bcopy(fpregs->fpr_xacc[i], sv_xmm->sv_xmm[i].xmm_bytes, 16);
1494}
1495
1496/* externalize from td->pcb */
1497int
1498fill_fpregs(struct thread *td, struct fpreg *fpregs)
1499{
1500
1501	fill_fpregs_xmm(&td->td_pcb->pcb_save, fpregs);
1502	return (0);
1503}
1504
1505/* internalize to td->pcb */
1506int
1507set_fpregs(struct thread *td, struct fpreg *fpregs)
1508{
1509
1510	set_fpregs_xmm(fpregs, &td->td_pcb->pcb_save);
1511	return (0);
1512}
1513
1514/*
1515 * Get machine context.
1516 */
1517int
1518get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
1519{
1520	struct trapframe *tp;
1521
1522	tp = td->td_frame;
1523	PROC_LOCK(curthread->td_proc);
1524	mcp->mc_onstack = sigonstack(tp->tf_rsp);
1525	PROC_UNLOCK(curthread->td_proc);
1526	mcp->mc_r15 = tp->tf_r15;
1527	mcp->mc_r14 = tp->tf_r14;
1528	mcp->mc_r13 = tp->tf_r13;
1529	mcp->mc_r12 = tp->tf_r12;
1530	mcp->mc_r11 = tp->tf_r11;
1531	mcp->mc_r10 = tp->tf_r10;
1532	mcp->mc_r9  = tp->tf_r9;
1533	mcp->mc_r8  = tp->tf_r8;
1534	mcp->mc_rdi = tp->tf_rdi;
1535	mcp->mc_rsi = tp->tf_rsi;
1536	mcp->mc_rbp = tp->tf_rbp;
1537	mcp->mc_rbx = tp->tf_rbx;
1538	mcp->mc_rcx = tp->tf_rcx;
1539	if (flags & GET_MC_CLEAR_RET) {
1540		mcp->mc_rax = 0;
1541		mcp->mc_rdx = 0;
1542	} else {
1543		mcp->mc_rax = tp->tf_rax;
1544		mcp->mc_rdx = tp->tf_rdx;
1545	}
1546	mcp->mc_rip = tp->tf_rip;
1547	mcp->mc_cs = tp->tf_cs;
1548	mcp->mc_rflags = tp->tf_rflags;
1549	mcp->mc_rsp = tp->tf_rsp;
1550	mcp->mc_ss = tp->tf_ss;
1551	mcp->mc_len = sizeof(*mcp);
1552	get_fpcontext(td, mcp);
1553	return (0);
1554}
1555
1556/*
1557 * Set machine context.
1558 *
1559 * However, we don't set any but the user modifiable flags, and we won't
1560 * touch the cs selector.
1561 */
1562int
1563set_mcontext(struct thread *td, const mcontext_t *mcp)
1564{
1565	struct trapframe *tp;
1566	long rflags;
1567	int ret;
1568
1569	tp = td->td_frame;
1570	if (mcp->mc_len != sizeof(*mcp))
1571		return (EINVAL);
1572	rflags = (mcp->mc_rflags & PSL_USERCHANGE) |
1573	    (tp->tf_rflags & ~PSL_USERCHANGE);
1574	ret = set_fpcontext(td, mcp);
1575	if (ret != 0)
1576		return (ret);
1577	tp->tf_r15 = mcp->mc_r15;
1578	tp->tf_r14 = mcp->mc_r14;
1579	tp->tf_r13 = mcp->mc_r13;
1580	tp->tf_r12 = mcp->mc_r12;
1581	tp->tf_r11 = mcp->mc_r11;
1582	tp->tf_r10 = mcp->mc_r10;
1583	tp->tf_r9  = mcp->mc_r9;
1584	tp->tf_r8  = mcp->mc_r8;
1585	tp->tf_rdi = mcp->mc_rdi;
1586	tp->tf_rsi = mcp->mc_rsi;
1587	tp->tf_rbp = mcp->mc_rbp;
1588	tp->tf_rbx = mcp->mc_rbx;
1589	tp->tf_rdx = mcp->mc_rdx;
1590	tp->tf_rcx = mcp->mc_rcx;
1591	tp->tf_rax = mcp->mc_rax;
1592	tp->tf_rip = mcp->mc_rip;
1593	tp->tf_rflags = rflags;
1594	tp->tf_rsp = mcp->mc_rsp;
1595	tp->tf_ss = mcp->mc_ss;
1596	td->td_pcb->pcb_flags |= PCB_FULLCTX;
1597	return (0);
1598}
1599
1600static void
1601get_fpcontext(struct thread *td, mcontext_t *mcp)
1602{
1603
1604	mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate);
1605	mcp->mc_fpformat = fpuformat();
1606}
1607
1608static int
1609set_fpcontext(struct thread *td, const mcontext_t *mcp)
1610{
1611
1612	if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
1613		return (0);
1614	else if (mcp->mc_fpformat != _MC_FPFMT_XMM)
1615		return (EINVAL);
1616	else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
1617		/* We don't care what state is left in the FPU or PCB. */
1618		fpstate_drop(td);
1619	else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
1620	    mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
1621		/*
1622		 * XXX we violate the dubious requirement that fpusetregs()
1623		 * be called with interrupts disabled.
1624		 * XXX obsolete on trap-16 systems?
1625		 */
1626		fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate);
1627	} else
1628		return (EINVAL);
1629	return (0);
1630}
1631
1632void
1633fpstate_drop(struct thread *td)
1634{
1635	register_t s;
1636
1637	s = intr_disable();
1638	if (PCPU_GET(fpcurthread) == td)
1639		fpudrop();
1640	/*
1641	 * XXX force a full drop of the fpu.  The above only drops it if we
1642	 * owned it.
1643	 *
1644	 * XXX I don't much like fpugetregs()'s semantics of doing a full
1645	 * drop.  Dropping only to the pcb matches fnsave's behaviour.
1646	 * We only need to drop to !PCB_INITDONE in sendsig().  But
1647	 * sendsig() is the only caller of fpugetregs()... perhaps we just
1648	 * have too many layers.
1649	 */
1650	curthread->td_pcb->pcb_flags &= ~PCB_FPUINITDONE;
1651	intr_restore(s);
1652}
1653
1654int
1655fill_dbregs(struct thread *td, struct dbreg *dbregs)
1656{
1657	struct pcb *pcb;
1658
1659	if (td == NULL) {
1660		dbregs->dr[0] = rdr0();
1661		dbregs->dr[1] = rdr1();
1662		dbregs->dr[2] = rdr2();
1663		dbregs->dr[3] = rdr3();
1664		dbregs->dr[6] = rdr6();
1665		dbregs->dr[7] = rdr7();
1666	} else {
1667		pcb = td->td_pcb;
1668		dbregs->dr[0] = pcb->pcb_dr0;
1669		dbregs->dr[1] = pcb->pcb_dr1;
1670		dbregs->dr[2] = pcb->pcb_dr2;
1671		dbregs->dr[3] = pcb->pcb_dr3;
1672		dbregs->dr[6] = pcb->pcb_dr6;
1673		dbregs->dr[7] = pcb->pcb_dr7;
1674	}
1675	dbregs->dr[4] = 0;
1676	dbregs->dr[5] = 0;
1677	dbregs->dr[8] = 0;
1678	dbregs->dr[9] = 0;
1679	dbregs->dr[10] = 0;
1680	dbregs->dr[11] = 0;
1681	dbregs->dr[12] = 0;
1682	dbregs->dr[13] = 0;
1683	dbregs->dr[14] = 0;
1684	dbregs->dr[15] = 0;
1685	return (0);
1686}
1687
1688int
1689set_dbregs(struct thread *td, struct dbreg *dbregs)
1690{
1691	struct pcb *pcb;
1692	int i;
1693	u_int64_t mask1, mask2;
1694
1695	if (td == NULL) {
1696		load_dr0(dbregs->dr[0]);
1697		load_dr1(dbregs->dr[1]);
1698		load_dr2(dbregs->dr[2]);
1699		load_dr3(dbregs->dr[3]);
1700		load_dr6(dbregs->dr[6]);
1701		load_dr7(dbregs->dr[7]);
1702	} else {
1703		/*
1704		 * Don't let an illegal value for dr7 get set.  Specifically,
1705		 * check for undefined settings.  Setting these bit patterns
1706		 * result in undefined behaviour and can lead to an unexpected
1707		 * TRCTRAP or a general protection fault right here.
1708		 */
1709		for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8;
1710		     i++, mask1 <<= 2, mask2 <<= 2)
1711			if ((dbregs->dr[7] & mask1) == mask2)
1712				return (EINVAL);
1713
1714		pcb = td->td_pcb;
1715
1716		/*
1717		 * Don't let a process set a breakpoint that is not within the
1718		 * process's address space.  If a process could do this, it
1719		 * could halt the system by setting a breakpoint in the kernel
1720		 * (if ddb was enabled).  Thus, we need to check to make sure
1721		 * that no breakpoints are being enabled for addresses outside
1722		 * process's address space, unless, perhaps, we were called by
1723		 * uid 0.
1724		 *
1725		 * XXX - what about when the watched area of the user's
1726		 * address space is written into from within the kernel
1727		 * ... wouldn't that still cause a breakpoint to be generated
1728		 * from within kernel mode?
1729		 */
1730
1731		if (suser(td) != 0) {
1732			if (dbregs->dr[7] & 0x3) {
1733				/* dr0 is enabled */
1734				if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
1735					return (EINVAL);
1736			}
1737			if (dbregs->dr[7] & 0x3<<2) {
1738				/* dr1 is enabled */
1739				if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
1740					return (EINVAL);
1741			}
1742			if (dbregs->dr[7] & 0x3<<4) {
1743				/* dr2 is enabled */
1744				if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
1745					return (EINVAL);
1746			}
1747			if (dbregs->dr[7] & 0x3<<6) {
1748				/* dr3 is enabled */
1749				if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
1750					return (EINVAL);
1751			}
1752		}
1753
1754		pcb->pcb_dr0 = dbregs->dr[0];
1755		pcb->pcb_dr1 = dbregs->dr[1];
1756		pcb->pcb_dr2 = dbregs->dr[2];
1757		pcb->pcb_dr3 = dbregs->dr[3];
1758		pcb->pcb_dr6 = dbregs->dr[6];
1759		pcb->pcb_dr7 = dbregs->dr[7];
1760
1761		pcb->pcb_flags |= PCB_DBREGS;
1762	}
1763
1764	return (0);
1765}
1766
1767void
1768reset_dbregs(void)
1769{
1770
1771	load_dr7(0);	/* Turn off the control bits first */
1772	load_dr0(0);
1773	load_dr1(0);
1774	load_dr2(0);
1775	load_dr3(0);
1776	load_dr6(0);
1777}
1778
1779/*
1780 * Return > 0 if a hardware breakpoint has been hit, and the
1781 * breakpoint was in user space.  Return 0, otherwise.
1782 */
1783int
1784user_dbreg_trap(void)
1785{
1786        u_int64_t dr7, dr6; /* debug registers dr6 and dr7 */
1787        u_int64_t bp;       /* breakpoint bits extracted from dr6 */
1788        int nbp;            /* number of breakpoints that triggered */
1789        caddr_t addr[4];    /* breakpoint addresses */
1790        int i;
1791
1792        dr7 = rdr7();
1793        if ((dr7 & 0x000000ff) == 0) {
1794                /*
1795                 * all GE and LE bits in the dr7 register are zero,
1796                 * thus the trap couldn't have been caused by the
1797                 * hardware debug registers
1798                 */
1799                return 0;
1800        }
1801
1802        nbp = 0;
1803        dr6 = rdr6();
1804        bp = dr6 & 0x0000000f;
1805
1806        if (!bp) {
1807                /*
1808                 * None of the breakpoint bits are set meaning this
1809                 * trap was not caused by any of the debug registers
1810                 */
1811                return 0;
1812        }
1813
1814        /*
1815         * at least one of the breakpoints were hit, check to see
1816         * which ones and if any of them are user space addresses
1817         */
1818
1819        if (bp & 0x01) {
1820                addr[nbp++] = (caddr_t)rdr0();
1821        }
1822        if (bp & 0x02) {
1823                addr[nbp++] = (caddr_t)rdr1();
1824        }
1825        if (bp & 0x04) {
1826                addr[nbp++] = (caddr_t)rdr2();
1827        }
1828        if (bp & 0x08) {
1829                addr[nbp++] = (caddr_t)rdr3();
1830        }
1831
1832        for (i=0; i<nbp; i++) {
1833                if (addr[i] <
1834                    (caddr_t)VM_MAXUSER_ADDRESS) {
1835                        /*
1836                         * addr[i] is in user space
1837                         */
1838                        return nbp;
1839                }
1840        }
1841
1842        /*
1843         * None of the breakpoints are in user space.
1844         */
1845        return 0;
1846}
1847
1848#ifdef KDB
1849
1850/*
1851 * Provide inb() and outb() as functions.  They are normally only
1852 * available as macros calling inlined functions, thus cannot be
1853 * called from the debugger.
1854 *
1855 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
1856 */
1857
1858#undef inb
1859#undef outb
1860
1861/* silence compiler warnings */
1862u_char inb(u_int);
1863void outb(u_int, u_char);
1864
1865u_char
1866inb(u_int port)
1867{
1868	u_char	data;
1869	/*
1870	 * We use %%dx and not %1 here because i/o is done at %dx and not at
1871	 * %edx, while gcc generates inferior code (movw instead of movl)
1872	 * if we tell it to load (u_short) port.
1873	 */
1874	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
1875	return (data);
1876}
1877
1878void
1879outb(u_int port, u_char data)
1880{
1881	u_char	al;
1882	/*
1883	 * Use an unnecessary assignment to help gcc's register allocator.
1884	 * This make a large difference for gcc-1.40 and a tiny difference
1885	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
1886	 * best results.  gcc-2.6.0 can't handle this.
1887	 */
1888	al = data;
1889	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
1890}
1891
1892#endif /* KDB */
1893