machdep.c revision 26657
1276789Sdim/*-
2276789Sdim * Copyright (c) 1992 Terrence R. Lambert.
3276789Sdim * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4276789Sdim * All rights reserved.
5276789Sdim *
6276789Sdim * This code is derived from software contributed to Berkeley by
7276789Sdim * William Jolitz.
8276789Sdim *
9276789Sdim * Redistribution and use in source and binary forms, with or without
10276789Sdim * modification, are permitted provided that the following conditions
11276789Sdim * are met:
12276789Sdim * 1. Redistributions of source code must retain the above copyright
13276789Sdim *    notice, this list of conditions and the following disclaimer.
14276789Sdim * 2. Redistributions in binary form must reproduce the above copyright
15276789Sdim *    notice, this list of conditions and the following disclaimer in the
16276789Sdim *    documentation and/or other materials provided with the distribution.
17276789Sdim * 3. All advertising materials mentioning features or use of this software
18276789Sdim *    must display the following acknowledgement:
19276789Sdim *	This product includes software developed by the University of
20276789Sdim *	California, Berkeley and its contributors.
21276789Sdim * 4. Neither the name of the University nor the names of its contributors
22276789Sdim *    may be used to endorse or promote products derived from this software
23276789Sdim *    without specific prior written permission.
24276789Sdim *
25276789Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26276789Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27276789Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28276789Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29276789Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30327952Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31276789Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32276789Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33276789Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34276789Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35276789Sdim * SUCH DAMAGE.
36276789Sdim *
37276789Sdim *	from: @(#)machdep.c	7.4 (Berkeley) 6/3/91
38276789Sdim *	$Id: machdep.c,v 1.247 1997/06/07 04:36:09 bde Exp $
39276789Sdim */
40276789Sdim
41276789Sdim#include "apm.h"
42276789Sdim#include "npx.h"
43276789Sdim#include "opt_sysvipc.h"
44276789Sdim#include "opt_ddb.h"
45276789Sdim#include "opt_bounce.h"
46276789Sdim#include "opt_machdep.h"
47276789Sdim#include "opt_perfmon.h"
48276789Sdim#include "opt_smp.h"
49276789Sdim#include "opt_userconfig.h"
50276789Sdim
51276789Sdim#include <sys/param.h>
52276789Sdim#include <sys/systm.h>
53276789Sdim#include <sys/sysproto.h>
54276789Sdim#include <sys/signalvar.h>
55276789Sdim#include <sys/kernel.h>
56276789Sdim#include <sys/proc.h>
57276789Sdim#include <sys/buf.h>
58276789Sdim#include <sys/reboot.h>
59276789Sdim#include <sys/conf.h>
60276789Sdim#include <sys/file.h>
61276789Sdim#include <sys/callout.h>
62276789Sdim#include <sys/malloc.h>
63276789Sdim#include <sys/mbuf.h>
64276789Sdim#include <sys/mount.h>
65276789Sdim#include <sys/msgbuf.h>
66276789Sdim#include <sys/sysent.h>
67276789Sdim#include <sys/tty.h>
68276789Sdim#include <sys/sysctl.h>
69276789Sdim#include <sys/vmmeter.h>
70276789Sdim
71276789Sdim#ifdef SYSVSHM
72276789Sdim#include <sys/shm.h>
73276789Sdim#endif
74276789Sdim
75276789Sdim#ifdef SYSVMSG
76276789Sdim#include <sys/msg.h>
77276789Sdim#endif
78276789Sdim
79276789Sdim#ifdef SYSVSEM
80276789Sdim#include <sys/sem.h>
81276789Sdim#endif
82276789Sdim
83276789Sdim#include <vm/vm.h>
84276789Sdim#include <vm/vm_param.h>
85276789Sdim#include <vm/vm_prot.h>
86276789Sdim#include <sys/lock.h>
87276789Sdim#include <vm/vm_kern.h>
88276789Sdim#include <vm/vm_object.h>
89276789Sdim#include <vm/vm_page.h>
90276789Sdim#include <vm/vm_map.h>
91276789Sdim#include <vm/vm_pager.h>
92276789Sdim#include <vm/vm_extern.h>
93276789Sdim
94276789Sdim#include <sys/user.h>
95276789Sdim#include <sys/exec.h>
96276789Sdim#include <sys/vnode.h>
97276789Sdim
98276789Sdim#include <ddb/ddb.h>
99276789Sdim
100276789Sdim#include <net/netisr.h>
101276789Sdim
102276789Sdim#if NAPM > 0
103276789Sdim#include <machine/apm_bios.h>
104276789Sdim#endif
105276789Sdim#include <machine/cpu.h>
106276789Sdim#include <machine/npx.h>
107276789Sdim#include <machine/reg.h>
108276789Sdim#include <machine/psl.h>
109276789Sdim#include <machine/clock.h>
110276789Sdim#include <machine/specialreg.h>
111276789Sdim#include <machine/sysarch.h>
112276789Sdim#include <machine/cons.h>
113276789Sdim#include <machine/bootinfo.h>
114276789Sdim#include <machine/md_var.h>
115276789Sdim#ifdef SMP
116276789Sdim#include <machine/smp.h>
117276789Sdim#include <machine/smptests.h>	/** LATE_START */
118276789Sdim#endif
119276789Sdim#ifdef PERFMON
120276789Sdim#include <machine/perfmon.h>
121276789Sdim#endif
122276789Sdim
123276789Sdim#include <i386/isa/isa_device.h>
124276789Sdim#include <i386/isa/intr_machdep.h>
125276789Sdim#include <i386/isa/rtc.h>
126276789Sdim#include <machine/random.h>
127276789Sdim
128276789Sdimextern void init386 __P((int first));
129276789Sdimextern int ptrace_set_pc __P((struct proc *p, unsigned int addr));
130276789Sdimextern int ptrace_single_step __P((struct proc *p));
131276789Sdimextern int ptrace_write_u __P((struct proc *p, vm_offset_t off, int data));
132276789Sdimextern void dblfault_handler __P((void));
133276789Sdim
134276789Sdimextern void printcpuinfo(void);	/* XXX header file */
135276789Sdimextern void earlysetcpuclass(void);	/* same header file */
136276789Sdimextern void finishidentcpu(void);
137276789Sdimextern void panicifcpuunsupported(void);
138276789Sdimextern void initializecpu(void);
139276789Sdim
140276789Sdimstatic void cpu_startup __P((void *));
141276789SdimSYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
142276789Sdim
143276789Sdim
144276789Sdim#ifdef BOUNCE_BUFFERS
145276789Sdimextern char *bouncememory;
146276789Sdimextern int maxbkva;
147276789Sdim#ifdef BOUNCEPAGES
148276789Sdimint	bouncepages = BOUNCEPAGES;
149276789Sdim#else
150276789Sdimint	bouncepages = 0;
151276789Sdim#endif
152276789Sdim#endif	/* BOUNCE_BUFFERS */
153276789Sdim
154276789Sdimextern int freebufspace;
155276789Sdimint	msgbufmapped = 0;		/* set when safe to use msgbuf */
156276789Sdimint _udatasel, _ucodesel;
157276789Sdimu_int	atdevbase;
158276789Sdim
159276789Sdim
160276789Sdimint physmem = 0;
161276789Sdimint cold = 1;
162276789Sdim
163276789Sdimstatic int
164276789Sdimsysctl_hw_physmem SYSCTL_HANDLER_ARGS
165276789Sdim{
166276789Sdim	int error = sysctl_handle_int(oidp, 0, ctob(physmem), req);
167276789Sdim	return (error);
168276789Sdim}
169276789Sdim
170276789SdimSYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
171276789Sdim	0, 0, sysctl_hw_physmem, "I", "");
172276789Sdim
173276789Sdimstatic int
174276789Sdimsysctl_hw_usermem SYSCTL_HANDLER_ARGS
175276789Sdim{
176276789Sdim	int error = sysctl_handle_int(oidp, 0,
177276789Sdim		ctob(physmem - cnt.v_wire_count), req);
178276789Sdim	return (error);
179276789Sdim}
180
181SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
182	0, 0, sysctl_hw_usermem, "I", "");
183
184int boothowto = 0, bootverbose = 0, Maxmem = 0;
185static int	badpages = 0;
186long dumplo;
187extern int bootdev;
188
189vm_offset_t phys_avail[10];
190
191/* must be 2 less so 0 0 can signal end of chunks */
192#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
193
194static void setup_netisrs __P((struct linker_set *)); /* XXX declare elsewhere */
195
196static vm_offset_t buffer_sva, buffer_eva;
197vm_offset_t clean_sva, clean_eva;
198static vm_offset_t pager_sva, pager_eva;
199extern struct linker_set netisr_set;
200
201#define offsetof(type, member)	((size_t)(&((type *)0)->member))
202
203static void
204cpu_startup(dummy)
205	void *dummy;
206{
207	register unsigned i;
208	register caddr_t v;
209	vm_offset_t maxaddr;
210	vm_size_t size = 0;
211	int firstaddr;
212	vm_offset_t minaddr;
213
214	if (boothowto & RB_VERBOSE)
215		bootverbose++;
216
217	/*
218	 * Good {morning,afternoon,evening,night}.
219	 */
220	printf(version);
221#ifdef SMP
222#if defined(LATE_START)
223	mp_start();			/* fire up the APs and APICs */
224#endif  /* LATE_START */
225	mp_announce();
226#endif  /* SMP */
227	earlysetcpuclass();
228	startrtclock();
229	printcpuinfo();
230	panicifcpuunsupported();
231#ifdef PERFMON
232	perfmon_init();
233#endif
234	printf("real memory  = %d (%dK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024);
235	/*
236	 * Display any holes after the first chunk of extended memory.
237	 */
238	if (badpages != 0) {
239		int indx = 1;
240
241		/*
242		 * XXX skip reporting ISA hole & unmanaged kernel memory
243		 */
244		if (phys_avail[0] == PAGE_SIZE)
245			indx += 2;
246
247		printf("Physical memory hole(s):\n");
248		for (; phys_avail[indx + 1] != 0; indx += 2) {
249			int size = phys_avail[indx + 1] - phys_avail[indx];
250
251			printf("0x%08lx - 0x%08lx, %d bytes (%d pages)\n", phys_avail[indx],
252			    phys_avail[indx + 1] - 1, size, size / PAGE_SIZE);
253		}
254	}
255
256	/*
257	 * Quickly wire in netisrs.
258	 */
259	setup_netisrs(&netisr_set);
260
261	/*
262	 * Allocate space for system data structures.
263	 * The first available kernel virtual address is in "v".
264	 * As pages of kernel virtual memory are allocated, "v" is incremented.
265	 * As pages of memory are allocated and cleared,
266	 * "firstaddr" is incremented.
267	 * An index into the kernel page table corresponding to the
268	 * virtual memory address maintained in "v" is kept in "mapaddr".
269	 */
270
271	/*
272	 * Make two passes.  The first pass calculates how much memory is
273	 * needed and allocates it.  The second pass assigns virtual
274	 * addresses to the various data structures.
275	 */
276	firstaddr = 0;
277again:
278	v = (caddr_t)firstaddr;
279
280#define	valloc(name, type, num) \
281	    (name) = (type *)v; v = (caddr_t)((name)+(num))
282#define	valloclim(name, type, num, lim) \
283	    (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
284	valloc(callout, struct callout, ncallout);
285#ifdef SYSVSHM
286	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
287#endif
288#ifdef SYSVSEM
289	valloc(sema, struct semid_ds, seminfo.semmni);
290	valloc(sem, struct sem, seminfo.semmns);
291	/* This is pretty disgusting! */
292	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
293#endif
294#ifdef SYSVMSG
295	valloc(msgpool, char, msginfo.msgmax);
296	valloc(msgmaps, struct msgmap, msginfo.msgseg);
297	valloc(msghdrs, struct msg, msginfo.msgtql);
298	valloc(msqids, struct msqid_ds, msginfo.msgmni);
299#endif
300
301	if (nbuf == 0) {
302		nbuf = 30;
303		if( physmem > 1024)
304			nbuf += min((physmem - 1024) / 8, 2048);
305	}
306	nswbuf = max(min(nbuf/4, 128), 16);
307
308	valloc(swbuf, struct buf, nswbuf);
309	valloc(buf, struct buf, nbuf);
310
311#ifdef BOUNCE_BUFFERS
312	/*
313	 * If there is more than 16MB of memory, allocate some bounce buffers
314	 */
315	if (Maxmem > 4096) {
316		if (bouncepages == 0) {
317			bouncepages = 64;
318			bouncepages += ((Maxmem - 4096) / 2048) * 32;
319			if (bouncepages > 128)
320				bouncepages = 128;
321		}
322		v = (caddr_t)((vm_offset_t)round_page(v));
323		valloc(bouncememory, char, bouncepages * PAGE_SIZE);
324	}
325#endif
326
327	/*
328	 * End of first pass, size has been calculated so allocate memory
329	 */
330	if (firstaddr == 0) {
331		size = (vm_size_t)(v - firstaddr);
332		firstaddr = (int)kmem_alloc(kernel_map, round_page(size));
333		if (firstaddr == 0)
334			panic("startup: no room for tables");
335		goto again;
336	}
337
338	/*
339	 * End of second pass, addresses have been assigned
340	 */
341	if ((vm_size_t)(v - firstaddr) != size)
342		panic("startup: table size inconsistency");
343
344#ifdef BOUNCE_BUFFERS
345	clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
346			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) +
347				maxbkva + pager_map_size, TRUE);
348	io_map = kmem_suballoc(clean_map, &minaddr, &maxaddr, maxbkva, FALSE);
349#else
350	clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
351			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size, TRUE);
352#endif
353	buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
354				(nbuf*BKVASIZE), TRUE);
355	pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva,
356				(nswbuf*MAXPHYS) + pager_map_size, TRUE);
357	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
358				(16*ARG_MAX), TRUE);
359	u_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
360				(maxproc*UPAGES*PAGE_SIZE), FALSE);
361
362#if defined(SMP) && defined(SMP_PRIVPAGES)
363	/* Per-cpu pages.. (the story so far is... subject to change)
364	 * ========= For the per-cpu data page ========
365	 * 1 private data page
366	 * 1 PDE	(per-cpu PTD entry page)
367	 * 1 PT		(per-cpu page table page)
368	 * ============ For the idle loop =============
369	 * 2 UPAGEs	(per-cpu idle procs)
370	 * 1 PTD	(for per-cpu equiv of IdlePTD)
371	 * ============================================
372	 * = total of 6 pages per cpu.  The BSP reuses the ones allocated
373	 * by locore.s during boot to remove special cases at runtime.
374	 */
375	ppage_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
376				(NCPU*6*PAGE_SIZE), FALSE);
377#endif
378
379	/*
380	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
381	 * we use the more space efficient malloc in place of kmem_alloc.
382	 */
383	{
384		vm_offset_t mb_map_size;
385
386		mb_map_size = nmbufs * MSIZE + nmbclusters * MCLBYTES;
387		mb_map_size = roundup2(mb_map_size, max(MCLBYTES, PAGE_SIZE));
388		mclrefcnt = malloc(mb_map_size / MCLBYTES, M_MBUF, M_NOWAIT);
389		bzero(mclrefcnt, mb_map_size / MCLBYTES);
390		mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr,
391			mb_map_size, FALSE);
392	}
393
394	/*
395	 * Initialize callouts
396	 */
397	callfree = callout;
398	for (i = 1; i < ncallout; i++)
399		callout[i-1].c_next = &callout[i];
400
401#if defined(USERCONFIG)
402#if defined(USERCONFIG_BOOT)
403	if (1) {
404#else
405        if (boothowto & RB_CONFIG) {
406#endif
407		userconfig();
408		cninit();	/* the preferred console may have changed */
409	}
410#endif
411
412#ifdef BOUNCE_BUFFERS
413	/*
414	 * init bounce buffers
415	 */
416	vm_bounce_init();
417#endif
418
419	printf("avail memory = %d (%dK bytes)\n", ptoa(cnt.v_free_count),
420	    ptoa(cnt.v_free_count) / 1024);
421
422	/*
423	 * Set up buffers, so they can be used to read disk labels.
424	 */
425	bufinit();
426	vm_pager_bufferinit();
427}
428
429int
430register_netisr(num, handler)
431	int num;
432	netisr_t *handler;
433{
434
435	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
436		printf("register_netisr: bad isr number: %d\n", num);
437		return (EINVAL);
438	}
439	netisrs[num] = handler;
440	return (0);
441}
442
443static void
444setup_netisrs(ls)
445	struct linker_set *ls;
446{
447	int i;
448	const struct netisrtab *nit;
449
450	for(i = 0; ls->ls_items[i]; i++) {
451		nit = (const struct netisrtab *)ls->ls_items[i];
452		register_netisr(nit->nit_num, nit->nit_isr);
453	}
454}
455
456/*
457 * Send an interrupt to process.
458 *
459 * Stack is set up to allow sigcode stored
460 * at top to call routine, followed by kcall
461 * to sigreturn routine below.  After sigreturn
462 * resets the signal mask, the stack, and the
463 * frame pointer, it returns to the user
464 * specified pc, psl.
465 */
466void
467sendsig(catcher, sig, mask, code)
468	sig_t catcher;
469	int sig, mask;
470	u_long code;
471{
472	register struct proc *p = curproc;
473	register struct trapframe *regs;
474	register struct sigframe *fp;
475	struct sigframe sf;
476	struct sigacts *psp = p->p_sigacts;
477	int oonstack;
478
479	regs = p->p_md.md_regs;
480        oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
481	/*
482	 * Allocate and validate space for the signal handler context.
483	 */
484        if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
485	    (psp->ps_sigonstack & sigmask(sig))) {
486		fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
487		    psp->ps_sigstk.ss_size - sizeof(struct sigframe));
488		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
489	} else {
490		fp = (struct sigframe *)regs->tf_esp - 1;
491	}
492
493	/*
494	 * grow() will return FALSE if the fp will not fit inside the stack
495	 *	and the stack can not be grown. useracc will return FALSE
496	 *	if access is denied.
497	 */
498	if ((grow(p, (int)fp) == FALSE) ||
499	    (useracc((caddr_t)fp, sizeof (struct sigframe), B_WRITE) == FALSE)) {
500		/*
501		 * Process has trashed its stack; give it an illegal
502		 * instruction to halt it in its tracks.
503		 */
504		SIGACTION(p, SIGILL) = SIG_DFL;
505		sig = sigmask(SIGILL);
506		p->p_sigignore &= ~sig;
507		p->p_sigcatch &= ~sig;
508		p->p_sigmask &= ~sig;
509		psignal(p, SIGILL);
510		return;
511	}
512
513	/*
514	 * Build the argument list for the signal handler.
515	 */
516	if (p->p_sysent->sv_sigtbl) {
517		if (sig < p->p_sysent->sv_sigsize)
518			sig = p->p_sysent->sv_sigtbl[sig];
519		else
520			sig = p->p_sysent->sv_sigsize + 1;
521	}
522	sf.sf_signum = sig;
523	sf.sf_code = code;
524	sf.sf_scp = &fp->sf_sc;
525	sf.sf_addr = (char *) regs->tf_err;
526	sf.sf_handler = catcher;
527
528	/* save scratch registers */
529	sf.sf_sc.sc_eax = regs->tf_eax;
530	sf.sf_sc.sc_ebx = regs->tf_ebx;
531	sf.sf_sc.sc_ecx = regs->tf_ecx;
532	sf.sf_sc.sc_edx = regs->tf_edx;
533	sf.sf_sc.sc_esi = regs->tf_esi;
534	sf.sf_sc.sc_edi = regs->tf_edi;
535	sf.sf_sc.sc_cs = regs->tf_cs;
536	sf.sf_sc.sc_ds = regs->tf_ds;
537	sf.sf_sc.sc_ss = regs->tf_ss;
538	sf.sf_sc.sc_es = regs->tf_es;
539	sf.sf_sc.sc_isp = regs->tf_isp;
540
541	/*
542	 * Build the signal context to be used by sigreturn.
543	 */
544	sf.sf_sc.sc_onstack = oonstack;
545	sf.sf_sc.sc_mask = mask;
546	sf.sf_sc.sc_sp = regs->tf_esp;
547	sf.sf_sc.sc_fp = regs->tf_ebp;
548	sf.sf_sc.sc_pc = regs->tf_eip;
549	sf.sf_sc.sc_ps = regs->tf_eflags;
550
551	/*
552	 * Copy the sigframe out to the user's stack.
553	 */
554	if (copyout(&sf, fp, sizeof(struct sigframe)) != 0) {
555		/*
556		 * Something is wrong with the stack pointer.
557		 * ...Kill the process.
558		 */
559		sigexit(p, SIGILL);
560	};
561
562	regs->tf_esp = (int)fp;
563	regs->tf_eip = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
564	regs->tf_eflags &= ~PSL_VM;
565	regs->tf_cs = _ucodesel;
566	regs->tf_ds = _udatasel;
567	regs->tf_es = _udatasel;
568	regs->tf_ss = _udatasel;
569}
570
571/*
572 * System call to cleanup state after a signal
573 * has been taken.  Reset signal mask and
574 * stack state from context left by sendsig (above).
575 * Return to previous pc and psl as specified by
576 * context left by sendsig. Check carefully to
577 * make sure that the user has not modified the
578 * state to gain improper privileges.
579 */
580int
581sigreturn(p, uap, retval)
582	struct proc *p;
583	struct sigreturn_args /* {
584		struct sigcontext *sigcntxp;
585	} */ *uap;
586	int *retval;
587{
588	register struct sigcontext *scp;
589	register struct sigframe *fp;
590	register struct trapframe *regs = p->p_md.md_regs;
591	int eflags;
592
593	/*
594	 * (XXX old comment) regs->tf_esp points to the return address.
595	 * The user scp pointer is above that.
596	 * The return address is faked in the signal trampoline code
597	 * for consistency.
598	 */
599	scp = uap->sigcntxp;
600	fp = (struct sigframe *)
601	     ((caddr_t)scp - offsetof(struct sigframe, sf_sc));
602
603	if (useracc((caddr_t)fp, sizeof (*fp), B_WRITE) == 0)
604		return(EFAULT);
605
606	/*
607	 * Don't allow users to change privileged or reserved flags.
608	 */
609#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
610	eflags = scp->sc_ps;
611	/*
612	 * XXX do allow users to change the privileged flag PSL_RF.  The
613	 * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
614	 * sometimes set it there too.  tf_eflags is kept in the signal
615	 * context during signal handling and there is no other place
616	 * to remember it, so the PSL_RF bit may be corrupted by the
617	 * signal handler without us knowing.  Corruption of the PSL_RF
618	 * bit at worst causes one more or one less debugger trap, so
619	 * allowing it is fairly harmless.
620	 */
621	if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
622#ifdef DEBUG
623    		printf("sigreturn: eflags = 0x%x\n", eflags);
624#endif
625    		return(EINVAL);
626	}
627
628	/*
629	 * Don't allow users to load a valid privileged %cs.  Let the
630	 * hardware check for invalid selectors, excess privilege in
631	 * other selectors, invalid %eip's and invalid %esp's.
632	 */
633#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
634	if (!CS_SECURE(scp->sc_cs)) {
635#ifdef DEBUG
636    		printf("sigreturn: cs = 0x%x\n", scp->sc_cs);
637#endif
638		trapsignal(p, SIGBUS, T_PROTFLT);
639		return(EINVAL);
640	}
641
642	/* restore scratch registers */
643	regs->tf_eax = scp->sc_eax;
644	regs->tf_ebx = scp->sc_ebx;
645	regs->tf_ecx = scp->sc_ecx;
646	regs->tf_edx = scp->sc_edx;
647	regs->tf_esi = scp->sc_esi;
648	regs->tf_edi = scp->sc_edi;
649	regs->tf_cs = scp->sc_cs;
650	regs->tf_ds = scp->sc_ds;
651	regs->tf_es = scp->sc_es;
652	regs->tf_ss = scp->sc_ss;
653	regs->tf_isp = scp->sc_isp;
654
655	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0)
656		return(EINVAL);
657
658	if (scp->sc_onstack & 01)
659		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
660	else
661		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
662	p->p_sigmask = scp->sc_mask & ~sigcantmask;
663	regs->tf_ebp = scp->sc_fp;
664	regs->tf_esp = scp->sc_sp;
665	regs->tf_eip = scp->sc_pc;
666	regs->tf_eflags = eflags;
667	return(EJUSTRETURN);
668}
669
670/*
671 * Machine dependent boot() routine
672 *
673 * I haven't seen anything to put here yet
674 * Possibly some stuff might be grafted back here from boot()
675 */
676void
677cpu_boot(int howto)
678{
679}
680
681/*
682 * Shutdown the CPU as much as possible
683 */
684void
685cpu_halt(void)
686{
687	for (;;)
688		__asm__ ("hlt");
689}
690
691/*
692 * Turn the power off.
693 */
694void
695cpu_power_off(void)
696{
697#if NAPM > 0
698	apm_power_off();
699#endif
700}
701
702/*
703 * Clear registers on exec
704 */
705void
706setregs(p, entry, stack)
707	struct proc *p;
708	u_long entry;
709	u_long stack;
710{
711	struct trapframe *regs = p->p_md.md_regs;
712
713#ifdef USER_LDT
714	struct pcb *pcb = &p->p_addr->u_pcb;
715
716	/* was i386_user_cleanup() in NetBSD */
717	if (pcb->pcb_ldt) {
718		if (pcb == curpcb)
719			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
720		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
721			pcb->pcb_ldt_len * sizeof(union descriptor));
722		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
723 	}
724#endif
725
726	bzero((char *)regs, sizeof(struct trapframe));
727	regs->tf_eip = entry;
728	regs->tf_esp = stack;
729	regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
730	regs->tf_ss = _udatasel;
731	regs->tf_ds = _udatasel;
732	regs->tf_es = _udatasel;
733	regs->tf_cs = _ucodesel;
734
735	/*
736	 * Initialize the math emulator (if any) for the current process.
737	 * Actually, just clear the bit that says that the emulator has
738	 * been initialized.  Initialization is delayed until the process
739	 * traps to the emulator (if it is done at all) mainly because
740	 * emulators don't provide an entry point for initialization.
741	 */
742	p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP;
743
744	/*
745	 * Arrange to trap the next npx or `fwait' instruction (see npx.c
746	 * for why fwait must be trapped at least if there is an npx or an
747	 * emulator).  This is mainly to handle the case where npx0 is not
748	 * configured, since the npx routines normally set up the trap
749	 * otherwise.  It should be done only at boot time, but doing it
750	 * here allows modifying `npx_exists' for testing the emulator on
751	 * systems with an npx.
752	 */
753	load_cr0(rcr0() | CR0_MP | CR0_TS);
754
755#if NNPX > 0
756	/* Initialize the npx (if any) for the current process. */
757	npxinit(__INITIAL_NPXCW__);
758#endif
759}
760
761static int
762sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS
763{
764	int error;
765	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
766		req);
767	if (!error && req->newptr)
768		resettodr();
769	return (error);
770}
771
772SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
773	&adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
774
775SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
776	CTLFLAG_RW, &disable_rtc_set, 0, "");
777
778SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
779	CTLFLAG_RD, &bootinfo, bootinfo, "");
780
781SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
782	CTLFLAG_RW, &wall_cmos_clock, 0, "");
783
784/*
785 * Initialize 386 and configure to run kernel
786 */
787
788/*
789 * Initialize segments & interrupt table
790 */
791
792int currentldt;
793int _default_ldt;
794#ifdef SMP
795union descriptor gdt[NGDT + NCPU];		/* global descriptor table */
796#else
797union descriptor gdt[NGDT];		/* global descriptor table */
798#endif
799struct gate_descriptor idt[NIDT];	/* interrupt descriptor table */
800union descriptor ldt[NLDT];		/* local descriptor table */
801#ifdef SMP
802/* table descriptors - used to load tables by microp */
803struct region_descriptor r_gdt, r_idt;
804#endif
805
806#ifdef SMP
807struct i386tss SMPcommon_tss[NCPU];	/* One tss per cpu */
808struct i386tss *SMPcommon_tss_ptr[NCPU]; /* for the benefit of asmp code */
809#else
810struct i386tss common_tss;
811#endif
812
813static struct i386tss dblfault_tss;
814static char dblfault_stack[PAGE_SIZE];
815
816extern  struct user *proc0paddr;
817
818#ifdef TSS_IS_CACHED			/* cpu_switch helper */
819struct segment_descriptor *tssptr;
820int gsel_tss;
821#endif
822
823/* software prototypes -- in more palatable form */
824struct soft_segment_descriptor gdt_segs[
825#ifdef SMP
826					NGDT + NCPU
827#endif
828						   ] = {
829/* GNULL_SEL	0 Null Descriptor */
830{	0x0,			/* segment base address  */
831	0x0,			/* length */
832	0,			/* segment type */
833	0,			/* segment descriptor priority level */
834	0,			/* segment descriptor present */
835	0, 0,
836	0,			/* default 32 vs 16 bit size */
837	0  			/* limit granularity (byte/page units)*/ },
838/* GCODE_SEL	1 Code Descriptor for kernel */
839{	0x0,			/* segment base address  */
840	0xfffff,		/* length - all address space */
841	SDT_MEMERA,		/* segment type */
842	0,			/* segment descriptor priority level */
843	1,			/* segment descriptor present */
844	0, 0,
845	1,			/* default 32 vs 16 bit size */
846	1  			/* limit granularity (byte/page units)*/ },
847/* GDATA_SEL	2 Data Descriptor for kernel */
848{	0x0,			/* segment base address  */
849	0xfffff,		/* length - all address space */
850	SDT_MEMRWA,		/* segment type */
851	0,			/* segment descriptor priority level */
852	1,			/* segment descriptor present */
853	0, 0,
854	1,			/* default 32 vs 16 bit size */
855	1  			/* limit granularity (byte/page units)*/ },
856/* GLDT_SEL	3 LDT Descriptor */
857{	(int) ldt,		/* segment base address  */
858	sizeof(ldt)-1,		/* length - all address space */
859	SDT_SYSLDT,		/* segment type */
860	SEL_UPL,		/* segment descriptor priority level */
861	1,			/* segment descriptor present */
862	0, 0,
863	0,			/* unused - default 32 vs 16 bit size */
864	0  			/* limit granularity (byte/page units)*/ },
865/* GTGATE_SEL	4 Null Descriptor - Placeholder */
866{	0x0,			/* segment base address  */
867	0x0,			/* length - all address space */
868	0,			/* segment type */
869	0,			/* segment descriptor priority level */
870	0,			/* segment descriptor present */
871	0, 0,
872	0,			/* default 32 vs 16 bit size */
873	0  			/* limit granularity (byte/page units)*/ },
874/* GPANIC_SEL	5 Panic Tss Descriptor */
875{	(int) &dblfault_tss,	/* segment base address  */
876	sizeof(struct i386tss)-1,/* length - all address space */
877	SDT_SYS386TSS,		/* segment type */
878	0,			/* segment descriptor priority level */
879	1,			/* segment descriptor present */
880	0, 0,
881	0,			/* unused - default 32 vs 16 bit size */
882	0  			/* limit granularity (byte/page units)*/ },
883/* GPROC0_SEL	6 Proc 0 Tss Descriptor */
884{
885#ifdef SMP
886	(int) &SMPcommon_tss[0],/* segment base address */
887#else
888	(int) &common_tss,	/* segment base address */
889#endif
890	sizeof(struct i386tss)-1,/* length - all address space */
891	SDT_SYS386TSS,		/* segment type */
892	0,			/* segment descriptor priority level */
893	1,			/* segment descriptor present */
894	0, 0,
895	0,			/* unused - default 32 vs 16 bit size */
896	0  			/* limit granularity (byte/page units)*/ },
897/* GUSERLDT_SEL	7 User LDT Descriptor per process */
898{	(int) ldt,		/* segment base address  */
899	(512 * sizeof(union descriptor)-1),		/* length */
900	SDT_SYSLDT,		/* segment type */
901	0,			/* segment descriptor priority level */
902	1,			/* segment descriptor present */
903	0, 0,
904	0,			/* unused - default 32 vs 16 bit size */
905	0  			/* limit granularity (byte/page units)*/ },
906/* GAPMCODE32_SEL 8 APM BIOS 32-bit interface (32bit Code) */
907{	0,			/* segment base address (overwritten by APM)  */
908	0xfffff,		/* length */
909	SDT_MEMERA,		/* segment type */
910	0,			/* segment descriptor priority level */
911	1,			/* segment descriptor present */
912	0, 0,
913	1,			/* default 32 vs 16 bit size */
914	1  			/* limit granularity (byte/page units)*/ },
915/* GAPMCODE16_SEL 9 APM BIOS 32-bit interface (16bit Code) */
916{	0,			/* segment base address (overwritten by APM)  */
917	0xfffff,		/* length */
918	SDT_MEMERA,		/* segment type */
919	0,			/* segment descriptor priority level */
920	1,			/* segment descriptor present */
921	0, 0,
922	0,			/* default 32 vs 16 bit size */
923	1  			/* limit granularity (byte/page units)*/ },
924/* GAPMDATA_SEL	10 APM BIOS 32-bit interface (Data) */
925{	0,			/* segment base address (overwritten by APM) */
926	0xfffff,		/* length */
927	SDT_MEMRWA,		/* segment type */
928	0,			/* segment descriptor priority level */
929	1,			/* segment descriptor present */
930	0, 0,
931	1,			/* default 32 vs 16 bit size */
932	1  			/* limit granularity (byte/page units)*/ },
933};
934
935static struct soft_segment_descriptor ldt_segs[] = {
936	/* Null Descriptor - overwritten by call gate */
937{	0x0,			/* segment base address  */
938	0x0,			/* length - all address space */
939	0,			/* segment type */
940	0,			/* segment descriptor priority level */
941	0,			/* segment descriptor present */
942	0, 0,
943	0,			/* default 32 vs 16 bit size */
944	0  			/* limit granularity (byte/page units)*/ },
945	/* Null Descriptor - overwritten by call gate */
946{	0x0,			/* segment base address  */
947	0x0,			/* length - all address space */
948	0,			/* segment type */
949	0,			/* segment descriptor priority level */
950	0,			/* segment descriptor present */
951	0, 0,
952	0,			/* default 32 vs 16 bit size */
953	0  			/* limit granularity (byte/page units)*/ },
954	/* Null Descriptor - overwritten by call gate */
955{	0x0,			/* segment base address  */
956	0x0,			/* length - all address space */
957	0,			/* segment type */
958	0,			/* segment descriptor priority level */
959	0,			/* segment descriptor present */
960	0, 0,
961	0,			/* default 32 vs 16 bit size */
962	0  			/* limit granularity (byte/page units)*/ },
963	/* Code Descriptor for user */
964{	0x0,			/* segment base address  */
965	0xfffff,		/* length - all address space */
966	SDT_MEMERA,		/* segment type */
967	SEL_UPL,		/* segment descriptor priority level */
968	1,			/* segment descriptor present */
969	0, 0,
970	1,			/* default 32 vs 16 bit size */
971	1  			/* limit granularity (byte/page units)*/ },
972	/* Data Descriptor for user */
973{	0x0,			/* segment base address  */
974	0xfffff,		/* length - all address space */
975	SDT_MEMRWA,		/* segment type */
976	SEL_UPL,		/* segment descriptor priority level */
977	1,			/* segment descriptor present */
978	0, 0,
979	1,			/* default 32 vs 16 bit size */
980	1  			/* limit granularity (byte/page units)*/ },
981};
982
983void
984setidt(idx, func, typ, dpl, selec)
985	int idx;
986	inthand_t *func;
987	int typ;
988	int dpl;
989	int selec;
990{
991	struct gate_descriptor *ip = idt + idx;
992
993	ip->gd_looffset = (int)func;
994	ip->gd_selector = selec;
995	ip->gd_stkcpy = 0;
996	ip->gd_xx = 0;
997	ip->gd_type = typ;
998	ip->gd_dpl = dpl;
999	ip->gd_p = 1;
1000	ip->gd_hioffset = ((int)func)>>16 ;
1001}
1002
1003#define	IDTVEC(name)	__CONCAT(X,name)
1004
1005extern inthand_t
1006	IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1007	IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1008	IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1009	IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1010	IDTVEC(syscall), IDTVEC(int0x80_syscall);
1011
1012void
1013sdtossd(sd, ssd)
1014	struct segment_descriptor *sd;
1015	struct soft_segment_descriptor *ssd;
1016{
1017	ssd->ssd_base  = (sd->sd_hibase << 24) | sd->sd_lobase;
1018	ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1019	ssd->ssd_type  = sd->sd_type;
1020	ssd->ssd_dpl   = sd->sd_dpl;
1021	ssd->ssd_p     = sd->sd_p;
1022	ssd->ssd_def32 = sd->sd_def32;
1023	ssd->ssd_gran  = sd->sd_gran;
1024}
1025
1026void
1027init386(first)
1028	int first;
1029{
1030	int x;
1031	unsigned biosbasemem, biosextmem;
1032	struct gate_descriptor *gdp;
1033#ifndef TSS_IS_CACHED
1034	int gsel_tss;
1035#endif
1036	struct isa_device *idp;
1037#ifndef SMP
1038	/* table descriptors - used to load tables by microp */
1039	struct region_descriptor r_gdt, r_idt;
1040#endif
1041	int	pagesinbase, pagesinext;
1042	int	target_page, pa_indx;
1043	int	off;
1044
1045	proc0.p_addr = proc0paddr;
1046
1047	atdevbase = ISA_HOLE_START + KERNBASE;
1048
1049	/*
1050	 * Initialize the console before we print anything out.
1051	 */
1052	cninit();
1053
1054	/*
1055	 * make gdt memory segments, the code segment goes up to end of the
1056	 * page with etext in it, the data segment goes to the end of
1057	 * the address space
1058	 */
1059	/*
1060	 * XXX text protection is temporarily (?) disabled.  The limit was
1061	 * i386_btop(round_page(etext)) - 1.
1062	 */
1063	gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1;
1064	gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1;
1065#ifdef BDE_DEBUGGER
1066#define	NGDT1	8		/* avoid overwriting db entries with APM ones */
1067#else
1068#define	NGDT1	(sizeof gdt_segs / sizeof gdt_segs[0])
1069#endif
1070	for (x = 0; x < NGDT1; x++)
1071		ssdtosd(&gdt_segs[x], &gdt[x].sd);
1072
1073#ifdef SMP
1074	/*
1075	 * Oh puke!
1076	 */
1077	for (x = 0; x < NCPU; x++) {
1078		SMPcommon_tss_ptr[x] = &SMPcommon_tss[x];
1079		gdt_segs[NGDT + x] = gdt_segs[GPROC0_SEL];
1080		gdt_segs[NGDT + x].ssd_base = (int) SMPcommon_tss_ptr[x];
1081		ssdtosd(&gdt_segs[NGDT + x], &gdt[NGDT + x].sd);
1082	}
1083#endif
1084
1085	/* make ldt memory segments */
1086	/*
1087	 * The data segment limit must not cover the user area because we
1088	 * don't want the user area to be writable in copyout() etc. (page
1089	 * level protection is lost in kernel mode on 386's).  Also, we
1090	 * don't want the user area to be writable directly (page level
1091	 * protection of the user area is not available on 486's with
1092	 * CR0_WP set, because there is no user-read/kernel-write mode).
1093	 *
1094	 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  And it
1095	 * should be spelled ...MAX_USER...
1096	 */
1097#define VM_END_USER_RW_ADDRESS	VM_MAXUSER_ADDRESS
1098	/*
1099	 * The code segment limit has to cover the user area until we move
1100	 * the signal trampoline out of the user area.  This is safe because
1101	 * the code segment cannot be written to directly.
1102	 */
1103#define VM_END_USER_R_ADDRESS	(VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE)
1104	ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1;
1105	ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1;
1106	for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
1107		ssdtosd(&ldt_segs[x], &ldt[x].sd);
1108
1109	/* exceptions */
1110	for (x = 0; x < NIDT; x++)
1111		setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1112	setidt(0, &IDTVEC(div),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1113	setidt(1, &IDTVEC(dbg),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1114	setidt(2, &IDTVEC(nmi),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1115 	setidt(3, &IDTVEC(bpt),  SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1116	setidt(4, &IDTVEC(ofl),  SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1117	setidt(5, &IDTVEC(bnd),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1118	setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1119	setidt(7, &IDTVEC(dna),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1120	setidt(8, 0,  SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1121	setidt(9, &IDTVEC(fpusegm),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1122	setidt(10, &IDTVEC(tss),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1123	setidt(11, &IDTVEC(missing),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1124	setidt(12, &IDTVEC(stk),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1125	setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1126	setidt(14, &IDTVEC(page),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1127	setidt(15, &IDTVEC(rsvd),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1128	setidt(16, &IDTVEC(fpu),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1129	setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1130	setidt(18, &IDTVEC(mchk),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1131 	setidt(0x80, &IDTVEC(int0x80_syscall),
1132			SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1133
1134#include	"isa.h"
1135#if	NISA >0
1136	isa_defaultirq();
1137#endif
1138	rand_initialize();
1139
1140	r_gdt.rd_limit = sizeof(gdt) - 1;
1141	r_gdt.rd_base =  (int) gdt;
1142	lgdt(&r_gdt);
1143
1144	r_idt.rd_limit = sizeof(idt) - 1;
1145	r_idt.rd_base = (int) idt;
1146	lidt(&r_idt);
1147
1148	_default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1149	lldt(_default_ldt);
1150	currentldt = _default_ldt;
1151
1152#ifdef DDB
1153	kdb_init();
1154	if (boothowto & RB_KDB)
1155		Debugger("Boot flags requested debugger");
1156#endif
1157
1158	finishidentcpu();	/* Final stage of CPU initialization */
1159	setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1160	initializecpu();	/* Initialize CPU registers */
1161
1162	/* Use BIOS values stored in RTC CMOS RAM, since probing
1163	 * breaks certain 386 AT relics.
1164	 */
1165	biosbasemem = rtcin(RTC_BASELO)+ (rtcin(RTC_BASEHI)<<8);
1166	biosextmem = rtcin(RTC_EXTLO)+ (rtcin(RTC_EXTHI)<<8);
1167
1168	/*
1169	 * If BIOS tells us that it has more than 640k in the basemem,
1170	 *	don't believe it - set it to 640k.
1171	 */
1172	if (biosbasemem > 640) {
1173		printf("Preposterous RTC basemem of %dK, truncating to 640K\n",
1174		       biosbasemem);
1175		biosbasemem = 640;
1176	}
1177	if (bootinfo.bi_memsizes_valid && bootinfo.bi_basemem > 640) {
1178		printf("Preposterous BIOS basemem of %dK, truncating to 640K\n",
1179		       bootinfo.bi_basemem);
1180		bootinfo.bi_basemem = 640;
1181	}
1182
1183	/*
1184	 * Warn if the official BIOS interface disagrees with the RTC
1185	 * interface used above about the amount of base memory or the
1186	 * amount of extended memory.  Prefer the BIOS value for the base
1187	 * memory.  This is necessary for machines that `steal' base
1188	 * memory for use as BIOS memory, at least if we are going to use
1189	 * the BIOS for apm.  Prefer the RTC value for extended memory.
1190	 * Eventually the hackish interface shouldn't even be looked at.
1191	 */
1192	if (bootinfo.bi_memsizes_valid) {
1193		if (bootinfo.bi_basemem != biosbasemem) {
1194			vm_offset_t pa;
1195
1196			printf(
1197	"BIOS basemem (%ldK) != RTC basemem (%dK), setting to BIOS value\n",
1198			       bootinfo.bi_basemem, biosbasemem);
1199			biosbasemem = bootinfo.bi_basemem;
1200
1201			/*
1202			 * XXX if biosbasemem is now < 640, there is `hole'
1203			 * between the end of base memory and the start of
1204			 * ISA memory.  The hole may be empty or it may
1205			 * contain BIOS code or data.  Map it read/write so
1206			 * that the BIOS can write to it.  (Memory from 0 to
1207			 * the physical end of the kernel is mapped read-only
1208			 * to begin with and then parts of it are remapped.
1209			 * The parts that aren't remapped form holes that
1210			 * remain read-only and are unused by the kernel.
1211			 * The base memory area is below the physical end of
1212			 * the kernel and right now forms a read-only hole.
1213			 * The part of it from 0 to
1214			 * (trunc_page(biosbasemem * 1024) - 1) will be
1215			 * remapped and used by the kernel later.)
1216			 *
1217			 * This code is similar to the code used in
1218			 * pmap_mapdev, but since no memory needs to be
1219			 * allocated we simply change the mapping.
1220			 */
1221			for (pa = trunc_page(biosbasemem * 1024);
1222			     pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1223				unsigned *pte;
1224
1225				pte = (unsigned *)vtopte(pa + KERNBASE);
1226				*pte = pa | PG_RW | PG_V;
1227			}
1228		}
1229		if (bootinfo.bi_extmem != biosextmem)
1230			printf("BIOS extmem (%ldK) != RTC extmem (%dK)\n",
1231			       bootinfo.bi_extmem, biosextmem);
1232	}
1233
1234#ifdef SMP
1235	/* make hole for AP bootstrap code */
1236	pagesinbase = mp_bootaddress(biosbasemem) / PAGE_SIZE;
1237#else
1238	pagesinbase = biosbasemem * 1024 / PAGE_SIZE;
1239#endif
1240
1241	pagesinext = biosextmem * 1024 / PAGE_SIZE;
1242
1243	/*
1244	 * Special hack for chipsets that still remap the 384k hole when
1245	 *	there's 16MB of memory - this really confuses people that
1246	 *	are trying to use bus mastering ISA controllers with the
1247	 *	"16MB limit"; they only have 16MB, but the remapping puts
1248	 *	them beyond the limit.
1249	 */
1250	/*
1251	 * If extended memory is between 15-16MB (16-17MB phys address range),
1252	 *	chop it to 15MB.
1253	 */
1254	if ((pagesinext > 3840) && (pagesinext < 4096))
1255		pagesinext = 3840;
1256
1257	/*
1258	 * Maxmem isn't the "maximum memory", it's one larger than the
1259	 * highest page of the physical address space.  It should be
1260	 * called something like "Maxphyspage".
1261	 */
1262	Maxmem = pagesinext + 0x100000/PAGE_SIZE;
1263
1264#ifdef MAXMEM
1265	Maxmem = MAXMEM/4;
1266#endif
1267
1268#if NNPX > 0
1269	idp = find_isadev(isa_devtab_null, &npxdriver, 0);
1270	if (idp != NULL && idp->id_msize != 0)
1271		Maxmem = idp->id_msize / 4;
1272#endif
1273
1274	/* call pmap initialization to make new kernel address space */
1275	pmap_bootstrap (first, 0);
1276
1277	/*
1278	 * Size up each available chunk of physical memory.
1279	 */
1280
1281	/*
1282	 * We currently don't bother testing base memory.
1283	 * XXX  ...but we probably should.
1284	 */
1285	pa_indx = 0;
1286	badpages = 0;
1287	if (pagesinbase > 1) {
1288		phys_avail[pa_indx++] = PAGE_SIZE;	/* skip first page of memory */
1289		phys_avail[pa_indx] = ptoa(pagesinbase);/* memory up to the ISA hole */
1290		physmem = pagesinbase - 1;
1291	} else {
1292		/* point at first chunk end */
1293		pa_indx++;
1294	}
1295
1296	for (target_page = avail_start; target_page < ptoa(Maxmem); target_page += PAGE_SIZE) {
1297		int tmp, page_bad = FALSE;
1298
1299		/*
1300		 * map page into kernel: valid, read/write, non-cacheable
1301		 */
1302		*(int *)CMAP1 = PG_V | PG_RW | PG_N | target_page;
1303		invltlb();
1304
1305		tmp = *(int *)CADDR1;
1306		/*
1307		 * Test for alternating 1's and 0's
1308		 */
1309		*(volatile int *)CADDR1 = 0xaaaaaaaa;
1310		if (*(volatile int *)CADDR1 != 0xaaaaaaaa) {
1311			page_bad = TRUE;
1312		}
1313		/*
1314		 * Test for alternating 0's and 1's
1315		 */
1316		*(volatile int *)CADDR1 = 0x55555555;
1317		if (*(volatile int *)CADDR1 != 0x55555555) {
1318			page_bad = TRUE;
1319		}
1320		/*
1321		 * Test for all 1's
1322		 */
1323		*(volatile int *)CADDR1 = 0xffffffff;
1324		if (*(volatile int *)CADDR1 != 0xffffffff) {
1325			page_bad = TRUE;
1326		}
1327		/*
1328		 * Test for all 0's
1329		 */
1330		*(volatile int *)CADDR1 = 0x0;
1331		if (*(volatile int *)CADDR1 != 0x0) {
1332			/*
1333			 * test of page failed
1334			 */
1335			page_bad = TRUE;
1336		}
1337		/*
1338		 * Restore original value.
1339		 */
1340		*(int *)CADDR1 = tmp;
1341
1342		/*
1343		 * Adjust array of valid/good pages.
1344		 */
1345		if (page_bad == FALSE) {
1346			/*
1347			 * If this good page is a continuation of the
1348			 * previous set of good pages, then just increase
1349			 * the end pointer. Otherwise start a new chunk.
1350			 * Note that "end" points one higher than end,
1351			 * making the range >= start and < end.
1352			 */
1353			if (phys_avail[pa_indx] == target_page) {
1354				phys_avail[pa_indx] += PAGE_SIZE;
1355			} else {
1356				pa_indx++;
1357				if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1358					printf("Too many holes in the physical address space, giving up\n");
1359					pa_indx--;
1360					break;
1361				}
1362				phys_avail[pa_indx++] = target_page;	/* start */
1363				phys_avail[pa_indx] = target_page + PAGE_SIZE;	/* end */
1364			}
1365			physmem++;
1366		} else {
1367			badpages++;
1368			page_bad = FALSE;
1369		}
1370	}
1371
1372	*(int *)CMAP1 = 0;
1373	invltlb();
1374
1375	/*
1376	 * XXX
1377	 * The last chunk must contain at least one page plus the message
1378	 * buffer to avoid complicating other code (message buffer address
1379	 * calculation, etc.).
1380	 */
1381	while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1382	    round_page(sizeof(struct msgbuf)) >= phys_avail[pa_indx]) {
1383		physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1384		phys_avail[pa_indx--] = 0;
1385		phys_avail[pa_indx--] = 0;
1386	}
1387
1388	Maxmem = atop(phys_avail[pa_indx]);
1389
1390	/* Trim off space for the message buffer. */
1391	phys_avail[pa_indx] -= round_page(sizeof(struct msgbuf));
1392
1393	avail_end = phys_avail[pa_indx];
1394
1395	/* now running on new page tables, configured,and u/iom is accessible */
1396
1397	/* Map the message buffer. */
1398	for (off = 0; off < round_page(sizeof(struct msgbuf)); off += PAGE_SIZE)
1399		pmap_enter(kernel_pmap, (vm_offset_t)msgbufp + off,
1400			   avail_end + off, VM_PROT_ALL, TRUE);
1401	msgbufmapped = 1;
1402
1403#ifdef SMP
1404	/* look for the MP hardware */
1405	mp_probe();
1406
1407	/* make the initial tss so cpu can get interrupt stack on syscall! */
1408	for(x = 0; x < NCPU; x++) {
1409		SMPcommon_tss[x].tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE;
1410		SMPcommon_tss[x].tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1411		SMPcommon_tss[x].tss_ioopt = (sizeof SMPcommon_tss[x]) << 16;
1412	}
1413#if 0
1414	/** XXX FIXME:
1415	 *   We can't access the LOCAL APIC till mp_enable() runs.  Since
1416	 *   this is run by the BSP, cpunumber() should always equal 0 anyway.
1417	 */
1418	gsel_tss = GSEL(NGDT + cpunumber(), SEL_KPL);
1419#else
1420	gsel_tss = GSEL(NGDT /** + 0 */, SEL_KPL);
1421#endif  /** 0 */
1422	ltr(gsel_tss);
1423#else
1424	/* make an initial tss so cpu can get interrupt stack on syscall! */
1425	common_tss.tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE;
1426	common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1427	common_tss.tss_ioopt = (sizeof common_tss) << 16;
1428	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1429	ltr(gsel_tss);
1430#endif  /* SMP */
1431
1432	dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1433	    dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1434	dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1435	    dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1436	dblfault_tss.tss_cr3 = IdlePTD;
1437	dblfault_tss.tss_eip = (int) dblfault_handler;
1438	dblfault_tss.tss_eflags = PSL_KERNEL;
1439	dblfault_tss.tss_ds = dblfault_tss.tss_es = dblfault_tss.tss_fs =
1440	    dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1441	dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1442	dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1443
1444#ifdef TSS_IS_CACHED			/* cpu_switch helper */
1445	tssptr = &gdt[GPROC0_SEL].sd;
1446#endif
1447
1448	/* make a call gate to reenter kernel with */
1449	gdp = &ldt[LSYS5CALLS_SEL].gd;
1450
1451	x = (int) &IDTVEC(syscall);
1452	gdp->gd_looffset = x++;
1453	gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1454	gdp->gd_stkcpy = 1;
1455	gdp->gd_type = SDT_SYS386CGT;
1456	gdp->gd_dpl = SEL_UPL;
1457	gdp->gd_p = 1;
1458	gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
1459
1460	/* XXX does this work? */
1461	ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1462
1463	/* transfer to user mode */
1464
1465	_ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1466	_udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1467
1468	/* setup proc 0's pcb */
1469	proc0.p_addr->u_pcb.pcb_flags = 0;
1470	proc0.p_addr->u_pcb.pcb_cr3 = IdlePTD;
1471	proc0.p_addr->u_pcb.pcb_mpnest = 1;
1472
1473#ifdef SMP
1474#if !defined(LATE_START)
1475	mp_start();			/* fire up the APs and APICs */
1476#endif  /* LATE_START */
1477#endif  /* SMP */
1478}
1479
1480int
1481ptrace_set_pc(p, addr)
1482	struct proc *p;
1483	unsigned int addr;
1484{
1485	p->p_md.md_regs->tf_eip = addr;
1486	return (0);
1487}
1488
1489int
1490ptrace_single_step(p)
1491	struct proc *p;
1492{
1493	p->p_md.md_regs->tf_eflags |= PSL_T;
1494	return (0);
1495}
1496
1497int ptrace_write_u(p, off, data)
1498	struct proc *p;
1499	vm_offset_t off;
1500	int data;
1501{
1502	struct trapframe frame_copy;
1503	vm_offset_t min;
1504	struct trapframe *tp;
1505
1506	/*
1507	 * Privileged kernel state is scattered all over the user area.
1508	 * Only allow write access to parts of regs and to fpregs.
1509	 */
1510	min = (char *)p->p_md.md_regs - (char *)p->p_addr;
1511	if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) {
1512		tp = p->p_md.md_regs;
1513		frame_copy = *tp;
1514		*(int *)((char *)&frame_copy + (off - min)) = data;
1515		if (!EFLAGS_SECURE(frame_copy.tf_eflags, tp->tf_eflags) ||
1516		    !CS_SECURE(frame_copy.tf_cs))
1517			return (EINVAL);
1518		*(int*)((char *)p->p_addr + off) = data;
1519		return (0);
1520	}
1521	min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu);
1522	if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) {
1523		*(int*)((char *)p->p_addr + off) = data;
1524		return (0);
1525	}
1526	return (EFAULT);
1527}
1528
1529int
1530fill_regs(p, regs)
1531	struct proc *p;
1532	struct reg *regs;
1533{
1534	struct pcb *pcb;
1535	struct trapframe *tp;
1536
1537	tp = p->p_md.md_regs;
1538	regs->r_es = tp->tf_es;
1539	regs->r_ds = tp->tf_ds;
1540	regs->r_edi = tp->tf_edi;
1541	regs->r_esi = tp->tf_esi;
1542	regs->r_ebp = tp->tf_ebp;
1543	regs->r_ebx = tp->tf_ebx;
1544	regs->r_edx = tp->tf_edx;
1545	regs->r_ecx = tp->tf_ecx;
1546	regs->r_eax = tp->tf_eax;
1547	regs->r_eip = tp->tf_eip;
1548	regs->r_cs = tp->tf_cs;
1549	regs->r_eflags = tp->tf_eflags;
1550	regs->r_esp = tp->tf_esp;
1551	regs->r_ss = tp->tf_ss;
1552	pcb = &p->p_addr->u_pcb;
1553	regs->r_fs = pcb->pcb_fs;
1554	regs->r_gs = pcb->pcb_gs;
1555	return (0);
1556}
1557
1558int
1559set_regs(p, regs)
1560	struct proc *p;
1561	struct reg *regs;
1562{
1563	struct pcb *pcb;
1564	struct trapframe *tp;
1565
1566	tp = p->p_md.md_regs;
1567	if (!EFLAGS_SECURE(regs->r_eflags, tp->tf_eflags) ||
1568	    !CS_SECURE(regs->r_cs))
1569		return (EINVAL);
1570	tp->tf_es = regs->r_es;
1571	tp->tf_ds = regs->r_ds;
1572	tp->tf_edi = regs->r_edi;
1573	tp->tf_esi = regs->r_esi;
1574	tp->tf_ebp = regs->r_ebp;
1575	tp->tf_ebx = regs->r_ebx;
1576	tp->tf_edx = regs->r_edx;
1577	tp->tf_ecx = regs->r_ecx;
1578	tp->tf_eax = regs->r_eax;
1579	tp->tf_eip = regs->r_eip;
1580	tp->tf_cs = regs->r_cs;
1581	tp->tf_eflags = regs->r_eflags;
1582	tp->tf_esp = regs->r_esp;
1583	tp->tf_ss = regs->r_ss;
1584	pcb = &p->p_addr->u_pcb;
1585	pcb->pcb_fs = regs->r_fs;
1586	pcb->pcb_gs = regs->r_gs;
1587	return (0);
1588}
1589
1590#ifndef DDB
1591void
1592Debugger(const char *msg)
1593{
1594	printf("Debugger(\"%s\") called.\n", msg);
1595}
1596#endif /* no DDB */
1597
1598#include <sys/disklabel.h>
1599
1600/*
1601 * Determine the size of the transfer, and make sure it is
1602 * within the boundaries of the partition. Adjust transfer
1603 * if needed, and signal errors or early completion.
1604 */
1605int
1606bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
1607{
1608        struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
1609        int labelsect = lp->d_partitions[0].p_offset;
1610        int maxsz = p->p_size,
1611                sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1612
1613        /* overwriting disk label ? */
1614        /* XXX should also protect bootstrap in first 8K */
1615        if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
1616#if LABELSECTOR != 0
1617            bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
1618#endif
1619            (bp->b_flags & B_READ) == 0 && wlabel == 0) {
1620                bp->b_error = EROFS;
1621                goto bad;
1622        }
1623
1624#if     defined(DOSBBSECTOR) && defined(notyet)
1625        /* overwriting master boot record? */
1626        if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
1627            (bp->b_flags & B_READ) == 0 && wlabel == 0) {
1628                bp->b_error = EROFS;
1629                goto bad;
1630        }
1631#endif
1632
1633        /* beyond partition? */
1634        if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
1635                /* if exactly at end of disk, return an EOF */
1636                if (bp->b_blkno == maxsz) {
1637                        bp->b_resid = bp->b_bcount;
1638                        return(0);
1639                }
1640                /* or truncate if part of it fits */
1641                sz = maxsz - bp->b_blkno;
1642                if (sz <= 0) {
1643                        bp->b_error = EINVAL;
1644                        goto bad;
1645                }
1646                bp->b_bcount = sz << DEV_BSHIFT;
1647        }
1648
1649        bp->b_pblkno = bp->b_blkno + p->p_offset;
1650        return(1);
1651
1652bad:
1653        bp->b_flags |= B_ERROR;
1654        return(-1);
1655}
1656
1657#ifdef DDB
1658
1659/*
1660 * Provide inb() and outb() as functions.  They are normally only
1661 * available as macros calling inlined functions, thus cannot be
1662 * called inside DDB.
1663 *
1664 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
1665 */
1666
1667#undef inb
1668#undef outb
1669
1670/* silence compiler warnings */
1671u_char inb(u_int);
1672void outb(u_int, u_char);
1673
1674u_char
1675inb(u_int port)
1676{
1677	u_char	data;
1678	/*
1679	 * We use %%dx and not %1 here because i/o is done at %dx and not at
1680	 * %edx, while gcc generates inferior code (movw instead of movl)
1681	 * if we tell it to load (u_short) port.
1682	 */
1683	__asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
1684	return (data);
1685}
1686
1687void
1688outb(u_int port, u_char data)
1689{
1690	u_char	al;
1691	/*
1692	 * Use an unnecessary assignment to help gcc's register allocator.
1693	 * This make a large difference for gcc-1.40 and a tiny difference
1694	 * for gcc-2.6.0.  For gcc-1.40, al had to be ``asm("ax")'' for
1695	 * best results.  gcc-2.6.0 can't handle this.
1696	 */
1697	al = data;
1698	__asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
1699}
1700
1701#endif /* DDB */
1702