machdep.c revision 23070
1/*-
2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	from: @(#)machdep.c	7.4 (Berkeley) 6/3/91
38 *	$Id: machdep.c,v 1.228 1997/02/22 09:32:26 peter Exp $
39 */
40
41#include "npx.h"
42#include "opt_sysvipc.h"
43#include "opt_ddb.h"
44#include "opt_bounce.h"
45#include "opt_machdep.h"
46#include "opt_perfmon.h"
47#include "opt_userconfig.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/sysproto.h>
52#include <sys/signalvar.h>
53#include <sys/kernel.h>
54#include <sys/proc.h>
55#include <sys/buf.h>
56#include <sys/reboot.h>
57#include <sys/conf.h>
58#include <sys/file.h>
59#include <sys/callout.h>
60#include <sys/malloc.h>
61#include <sys/mbuf.h>
62#include <sys/mount.h>
63#include <sys/msgbuf.h>
64#include <sys/ioctl.h>
65#include <sys/sysent.h>
66#include <sys/tty.h>
67#include <sys/sysctl.h>
68#include <sys/vmmeter.h>
69
70#ifdef SYSVSHM
71#include <sys/shm.h>
72#endif
73
74#ifdef SYSVMSG
75#include <sys/msg.h>
76#endif
77
78#ifdef SYSVSEM
79#include <sys/sem.h>
80#endif
81
82#include <vm/vm.h>
83#include <vm/vm_param.h>
84#include <vm/vm_prot.h>
85#include <sys/lock.h>
86#include <vm/vm_kern.h>
87#include <vm/vm_object.h>
88#include <vm/vm_page.h>
89#include <vm/vm_map.h>
90#include <vm/vm_pager.h>
91#include <vm/vm_extern.h>
92
93#include <sys/user.h>
94#include <sys/exec.h>
95#include <sys/vnode.h>
96
97#include <ddb/ddb.h>
98
99#include <net/netisr.h>
100
101#include <machine/cpu.h>
102#include <machine/npx.h>
103#include <machine/reg.h>
104#include <machine/psl.h>
105#include <machine/clock.h>
106#include <machine/specialreg.h>
107#include <machine/sysarch.h>
108#include <machine/cons.h>
109#include <machine/bootinfo.h>
110#include <machine/md_var.h>
111#ifdef PERFMON
112#include <machine/perfmon.h>
113#endif
114
115#include <i386/isa/isa_device.h>
116#include <i386/isa/rtc.h>
117#include <machine/random.h>
118
119extern void init386 __P((int first));
120extern int ptrace_set_pc __P((struct proc *p, unsigned int addr));
121extern int ptrace_single_step __P((struct proc *p));
122extern int ptrace_write_u __P((struct proc *p, vm_offset_t off, int data));
123extern void dblfault_handler __P((void));
124
125extern void identifycpu(void);	/* XXX header file */
126extern void earlysetcpuclass(void);	/* same header file */
127
128static void cpu_startup __P((void *));
129SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
130
131
132#ifdef BOUNCE_BUFFERS
133extern char *bouncememory;
134extern int maxbkva;
135#ifdef BOUNCEPAGES
136int	bouncepages = BOUNCEPAGES;
137#else
138int	bouncepages = 0;
139#endif
140#endif	/* BOUNCE_BUFFERS */
141
142extern int freebufspace;
143int	msgbufmapped = 0;		/* set when safe to use msgbuf */
144int _udatasel, _ucodesel;
145u_int	atdevbase;
146
147
148int physmem = 0;
149int cold = 1;
150
151static int
152sysctl_hw_physmem SYSCTL_HANDLER_ARGS
153{
154	int error = sysctl_handle_int(oidp, 0, ctob(physmem), req);
155	return (error);
156}
157
158SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
159	0, 0, sysctl_hw_physmem, "I", "");
160
161static int
162sysctl_hw_usermem SYSCTL_HANDLER_ARGS
163{
164	int error = sysctl_handle_int(oidp, 0,
165		ctob(physmem - cnt.v_wire_count), req);
166	return (error);
167}
168
169SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
170	0, 0, sysctl_hw_usermem, "I", "");
171
172int boothowto = 0, bootverbose = 0, Maxmem = 0;
173static int	badpages = 0;
174long dumplo;
175extern int bootdev;
176
177vm_offset_t phys_avail[10];
178
179/* must be 2 less so 0 0 can signal end of chunks */
180#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
181
182static void setup_netisrs __P((struct linker_set *)); /* XXX declare elsewhere */
183
184static vm_offset_t buffer_sva, buffer_eva;
185vm_offset_t clean_sva, clean_eva;
186static vm_offset_t pager_sva, pager_eva;
187extern struct linker_set netisr_set;
188
189#define offsetof(type, member)	((size_t)(&((type *)0)->member))
190
191static void
192cpu_startup(dummy)
193	void *dummy;
194{
195	register unsigned i;
196	register caddr_t v;
197	vm_offset_t maxaddr;
198	vm_size_t size = 0;
199	int firstaddr;
200	vm_offset_t minaddr;
201
202	if (boothowto & RB_VERBOSE)
203		bootverbose++;
204
205	/*
206	 * Good {morning,afternoon,evening,night}.
207	 */
208	printf(version);
209	earlysetcpuclass();
210	startrtclock();
211	identifycpu();
212#ifdef PERFMON
213	perfmon_init();
214#endif
215	printf("real memory  = %d (%dK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024);
216	/*
217	 * Display any holes after the first chunk of extended memory.
218	 */
219	if (badpages != 0) {
220		int indx = 1;
221
222		/*
223		 * XXX skip reporting ISA hole & unmanaged kernel memory
224		 */
225		if (phys_avail[0] == PAGE_SIZE)
226			indx += 2;
227
228		printf("Physical memory hole(s):\n");
229		for (; phys_avail[indx + 1] != 0; indx += 2) {
230			int size = phys_avail[indx + 1] - phys_avail[indx];
231
232			printf("0x%08lx - 0x%08lx, %d bytes (%d pages)\n", phys_avail[indx],
233			    phys_avail[indx + 1] - 1, size, size / PAGE_SIZE);
234		}
235	}
236
237	/*
238	 * Quickly wire in netisrs.
239	 */
240	setup_netisrs(&netisr_set);
241
242	/*
243	 * Allocate space for system data structures.
244	 * The first available kernel virtual address is in "v".
245	 * As pages of kernel virtual memory are allocated, "v" is incremented.
246	 * As pages of memory are allocated and cleared,
247	 * "firstaddr" is incremented.
248	 * An index into the kernel page table corresponding to the
249	 * virtual memory address maintained in "v" is kept in "mapaddr".
250	 */
251
252	/*
253	 * Make two passes.  The first pass calculates how much memory is
254	 * needed and allocates it.  The second pass assigns virtual
255	 * addresses to the various data structures.
256	 */
257	firstaddr = 0;
258again:
259	v = (caddr_t)firstaddr;
260
261#define	valloc(name, type, num) \
262	    (name) = (type *)v; v = (caddr_t)((name)+(num))
263#define	valloclim(name, type, num, lim) \
264	    (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
265	valloc(callout, struct callout, ncallout);
266#ifdef SYSVSHM
267	valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
268#endif
269#ifdef SYSVSEM
270	valloc(sema, struct semid_ds, seminfo.semmni);
271	valloc(sem, struct sem, seminfo.semmns);
272	/* This is pretty disgusting! */
273	valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
274#endif
275#ifdef SYSVMSG
276	valloc(msgpool, char, msginfo.msgmax);
277	valloc(msgmaps, struct msgmap, msginfo.msgseg);
278	valloc(msghdrs, struct msg, msginfo.msgtql);
279	valloc(msqids, struct msqid_ds, msginfo.msgmni);
280#endif
281
282	if (nbuf == 0) {
283		nbuf = 30;
284		if( physmem > 1024)
285			nbuf += min((physmem - 1024) / 8, 2048);
286	}
287	nswbuf = max(min(nbuf/4, 128), 16);
288
289	valloc(swbuf, struct buf, nswbuf);
290	valloc(buf, struct buf, nbuf);
291
292#ifdef BOUNCE_BUFFERS
293	/*
294	 * If there is more than 16MB of memory, allocate some bounce buffers
295	 */
296	if (Maxmem > 4096) {
297		if (bouncepages == 0) {
298			bouncepages = 64;
299			bouncepages += ((Maxmem - 4096) / 2048) * 32;
300		}
301		v = (caddr_t)((vm_offset_t)round_page(v));
302		valloc(bouncememory, char, bouncepages * PAGE_SIZE);
303	}
304#endif
305
306	/*
307	 * End of first pass, size has been calculated so allocate memory
308	 */
309	if (firstaddr == 0) {
310		size = (vm_size_t)(v - firstaddr);
311		firstaddr = (int)kmem_alloc(kernel_map, round_page(size));
312		if (firstaddr == 0)
313			panic("startup: no room for tables");
314		goto again;
315	}
316
317	/*
318	 * End of second pass, addresses have been assigned
319	 */
320	if ((vm_size_t)(v - firstaddr) != size)
321		panic("startup: table size inconsistency");
322
323#ifdef BOUNCE_BUFFERS
324	clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
325			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) +
326				maxbkva + pager_map_size, TRUE);
327	io_map = kmem_suballoc(clean_map, &minaddr, &maxaddr, maxbkva, FALSE);
328#else
329	clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
330			(nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size, TRUE);
331#endif
332	buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
333				(nbuf*BKVASIZE), TRUE);
334	pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva,
335				(nswbuf*MAXPHYS) + pager_map_size, TRUE);
336	exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
337				(16*ARG_MAX), TRUE);
338	exech_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
339				(16*PAGE_SIZE), TRUE);
340	u_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
341				(maxproc*UPAGES*PAGE_SIZE), FALSE);
342
343	/*
344	 * Finally, allocate mbuf pool.  Since mclrefcnt is an off-size
345	 * we use the more space efficient malloc in place of kmem_alloc.
346	 */
347	{
348		vm_offset_t mb_map_size;
349
350		mb_map_size = nmbufs * MSIZE + nmbclusters * MCLBYTES;
351		mb_map_size = roundup2(mb_map_size, max(MCLBYTES, PAGE_SIZE));
352		mclrefcnt = malloc(mb_map_size / MCLBYTES, M_MBUF, M_NOWAIT);
353		bzero(mclrefcnt, mb_map_size / MCLBYTES);
354		mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr,
355			mb_map_size, FALSE);
356	}
357
358	/*
359	 * Initialize callouts
360	 */
361	callfree = callout;
362	for (i = 1; i < ncallout; i++)
363		callout[i-1].c_next = &callout[i];
364
365#if defined(USERCONFIG)
366#if defined(USERCONFIG_BOOT)
367	if (1) {
368#else
369        if (boothowto & RB_CONFIG) {
370#endif
371		userconfig();
372		cninit();	/* the preferred console may have changed */
373	}
374#endif
375
376#ifdef BOUNCE_BUFFERS
377	/*
378	 * init bounce buffers
379	 */
380	vm_bounce_init();
381#endif
382
383	printf("avail memory = %d (%dK bytes)\n", ptoa(cnt.v_free_count),
384	    ptoa(cnt.v_free_count) / 1024);
385
386	/*
387	 * Set up buffers, so they can be used to read disk labels.
388	 */
389	bufinit();
390	vm_pager_bufferinit();
391}
392
393int
394register_netisr(num, handler)
395	int num;
396	netisr_t *handler;
397{
398
399	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
400		printf("register_netisr: bad isr number: %d\n", num);
401		return (EINVAL);
402	}
403	netisrs[num] = handler;
404	return (0);
405}
406
407static void
408setup_netisrs(ls)
409	struct linker_set *ls;
410{
411	int i;
412	const struct netisrtab *nit;
413
414	for(i = 0; ls->ls_items[i]; i++) {
415		nit = (const struct netisrtab *)ls->ls_items[i];
416		register_netisr(nit->nit_num, nit->nit_isr);
417	}
418}
419
420/*
421 * Send an interrupt to process.
422 *
423 * Stack is set up to allow sigcode stored
424 * at top to call routine, followed by kcall
425 * to sigreturn routine below.  After sigreturn
426 * resets the signal mask, the stack, and the
427 * frame pointer, it returns to the user
428 * specified pc, psl.
429 */
430void
431sendsig(catcher, sig, mask, code)
432	sig_t catcher;
433	int sig, mask;
434	u_long code;
435{
436	register struct proc *p = curproc;
437	register int *regs;
438	register struct sigframe *fp;
439	struct sigframe sf;
440	struct sigacts *psp = p->p_sigacts;
441	int oonstack;
442
443	regs = p->p_md.md_regs;
444        oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
445	/*
446	 * Allocate and validate space for the signal handler context.
447	 */
448        if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
449	    (psp->ps_sigonstack & sigmask(sig))) {
450		fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
451		    psp->ps_sigstk.ss_size - sizeof(struct sigframe));
452		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
453	} else {
454		fp = (struct sigframe *)regs[tESP] - 1;
455	}
456
457	/*
458	 * grow() will return FALSE if the fp will not fit inside the stack
459	 *	and the stack can not be grown. useracc will return FALSE
460	 *	if access is denied.
461	 */
462	if ((grow(p, (int)fp) == FALSE) ||
463	    (useracc((caddr_t)fp, sizeof (struct sigframe), B_WRITE) == FALSE)) {
464		/*
465		 * Process has trashed its stack; give it an illegal
466		 * instruction to halt it in its tracks.
467		 */
468		SIGACTION(p, SIGILL) = SIG_DFL;
469		sig = sigmask(SIGILL);
470		p->p_sigignore &= ~sig;
471		p->p_sigcatch &= ~sig;
472		p->p_sigmask &= ~sig;
473		psignal(p, SIGILL);
474		return;
475	}
476
477	/*
478	 * Build the argument list for the signal handler.
479	 */
480	if (p->p_sysent->sv_sigtbl) {
481		if (sig < p->p_sysent->sv_sigsize)
482			sig = p->p_sysent->sv_sigtbl[sig];
483		else
484			sig = p->p_sysent->sv_sigsize + 1;
485	}
486	sf.sf_signum = sig;
487	sf.sf_code = code;
488	sf.sf_scp = &fp->sf_sc;
489	sf.sf_addr = (char *) regs[tERR];
490	sf.sf_handler = catcher;
491
492	/* save scratch registers */
493	sf.sf_sc.sc_eax = regs[tEAX];
494	sf.sf_sc.sc_ebx = regs[tEBX];
495	sf.sf_sc.sc_ecx = regs[tECX];
496	sf.sf_sc.sc_edx = regs[tEDX];
497	sf.sf_sc.sc_esi = regs[tESI];
498	sf.sf_sc.sc_edi = regs[tEDI];
499	sf.sf_sc.sc_cs = regs[tCS];
500	sf.sf_sc.sc_ds = regs[tDS];
501	sf.sf_sc.sc_ss = regs[tSS];
502	sf.sf_sc.sc_es = regs[tES];
503	sf.sf_sc.sc_isp = regs[tISP];
504
505	/*
506	 * Build the signal context to be used by sigreturn.
507	 */
508	sf.sf_sc.sc_onstack = oonstack;
509	sf.sf_sc.sc_mask = mask;
510	sf.sf_sc.sc_sp = regs[tESP];
511	sf.sf_sc.sc_fp = regs[tEBP];
512	sf.sf_sc.sc_pc = regs[tEIP];
513	sf.sf_sc.sc_ps = regs[tEFLAGS];
514
515	/*
516	 * Copy the sigframe out to the user's stack.
517	 */
518	if (copyout(&sf, fp, sizeof(struct sigframe)) != 0) {
519		/*
520		 * Something is wrong with the stack pointer.
521		 * ...Kill the process.
522		 */
523		sigexit(p, SIGILL);
524	};
525
526	regs[tESP] = (int)fp;
527	regs[tEIP] = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
528	regs[tEFLAGS] &= ~PSL_VM;
529	regs[tCS] = _ucodesel;
530	regs[tDS] = _udatasel;
531	regs[tES] = _udatasel;
532	regs[tSS] = _udatasel;
533}
534
535/*
536 * System call to cleanup state after a signal
537 * has been taken.  Reset signal mask and
538 * stack state from context left by sendsig (above).
539 * Return to previous pc and psl as specified by
540 * context left by sendsig. Check carefully to
541 * make sure that the user has not modified the
542 * state to gain improper privileges.
543 */
544int
545sigreturn(p, uap, retval)
546	struct proc *p;
547	struct sigreturn_args /* {
548		struct sigcontext *sigcntxp;
549	} */ *uap;
550	int *retval;
551{
552	register struct sigcontext *scp;
553	register struct sigframe *fp;
554	register int *regs = p->p_md.md_regs;
555	int eflags;
556
557	/*
558	 * (XXX old comment) regs[tESP] points to the return address.
559	 * The user scp pointer is above that.
560	 * The return address is faked in the signal trampoline code
561	 * for consistency.
562	 */
563	scp = uap->sigcntxp;
564	fp = (struct sigframe *)
565	     ((caddr_t)scp - offsetof(struct sigframe, sf_sc));
566
567	if (useracc((caddr_t)fp, sizeof (*fp), B_WRITE) == 0)
568		return(EINVAL);
569
570	/*
571	 * Don't allow users to change privileged or reserved flags.
572	 */
573#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
574	eflags = scp->sc_ps;
575	/*
576	 * XXX do allow users to change the privileged flag PSL_RF.  The
577	 * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
578	 * sometimes set it there too.  tf_eflags is kept in the signal
579	 * context during signal handling and there is no other place
580	 * to remember it, so the PSL_RF bit may be corrupted by the
581	 * signal handler without us knowing.  Corruption of the PSL_RF
582	 * bit at worst causes one more or one less debugger trap, so
583	 * allowing it is fairly harmless.
584	 */
585	if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs[tEFLAGS] & ~PSL_RF)) {
586#ifdef DEBUG
587    		printf("sigreturn: eflags = 0x%x\n", eflags);
588#endif
589    		return(EINVAL);
590	}
591
592	/*
593	 * Don't allow users to load a valid privileged %cs.  Let the
594	 * hardware check for invalid selectors, excess privilege in
595	 * other selectors, invalid %eip's and invalid %esp's.
596	 */
597#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
598	if (!CS_SECURE(scp->sc_cs)) {
599#ifdef DEBUG
600    		printf("sigreturn: cs = 0x%x\n", scp->sc_cs);
601#endif
602		trapsignal(p, SIGBUS, T_PROTFLT);
603		return(EINVAL);
604	}
605
606	/* restore scratch registers */
607	regs[tEAX] = scp->sc_eax;
608	regs[tEBX] = scp->sc_ebx;
609	regs[tECX] = scp->sc_ecx;
610	regs[tEDX] = scp->sc_edx;
611	regs[tESI] = scp->sc_esi;
612	regs[tEDI] = scp->sc_edi;
613	regs[tCS] = scp->sc_cs;
614	regs[tDS] = scp->sc_ds;
615	regs[tES] = scp->sc_es;
616	regs[tSS] = scp->sc_ss;
617	regs[tISP] = scp->sc_isp;
618
619	if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0)
620		return(EINVAL);
621
622	if (scp->sc_onstack & 01)
623		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
624	else
625		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
626	p->p_sigmask = scp->sc_mask & ~sigcantmask;
627	regs[tEBP] = scp->sc_fp;
628	regs[tESP] = scp->sc_sp;
629	regs[tEIP] = scp->sc_pc;
630	regs[tEFLAGS] = eflags;
631	return(EJUSTRETURN);
632}
633
634/*
635 * Machine dependent boot() routine
636 *
637 * I haven't seen anything to put here yet
638 * Possibly some stuff might be grafted back here from boot()
639 */
640void
641cpu_boot(int howto)
642{
643}
644
645/*
646 * Shutdown the CPU as much as possible
647 */
648void
649cpu_halt(void)
650{
651	for (;;)
652		__asm__ ("hlt");
653}
654
655/*
656 * Clear registers on exec
657 */
658void
659setregs(p, entry, stack)
660	struct proc *p;
661	u_long entry;
662	u_long stack;
663{
664	int *regs = p->p_md.md_regs;
665
666#ifdef USER_LDT
667	struct pcb *pcb = &p->p_addr->u_pcb;
668
669	/* was i386_user_cleanup() in NetBSD */
670	if (pcb->pcb_ldt) {
671		if (pcb == curpcb)
672			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
673		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
674			pcb->pcb_ldt_len * sizeof(union descriptor));
675		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
676 	}
677#endif
678
679	bzero(regs, sizeof(struct trapframe));
680	regs[tEIP] = entry;
681	regs[tESP] = stack;
682	regs[tEFLAGS] = PSL_USER | (regs[tEFLAGS] & PSL_T);
683	regs[tSS] = _udatasel;
684	regs[tDS] = _udatasel;
685	regs[tES] = _udatasel;
686	regs[tCS] = _ucodesel;
687
688	/*
689	 * Initialize the math emulator (if any) for the current process.
690	 * Actually, just clear the bit that says that the emulator has
691	 * been initialized.  Initialization is delayed until the process
692	 * traps to the emulator (if it is done at all) mainly because
693	 * emulators don't provide an entry point for initialization.
694	 */
695	p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP;
696
697	/*
698	 * Arrange to trap the next npx or `fwait' instruction (see npx.c
699	 * for why fwait must be trapped at least if there is an npx or an
700	 * emulator).  This is mainly to handle the case where npx0 is not
701	 * configured, since the npx routines normally set up the trap
702	 * otherwise.  It should be done only at boot time, but doing it
703	 * here allows modifying `npx_exists' for testing the emulator on
704	 * systems with an npx.
705	 */
706	load_cr0(rcr0() | CR0_MP | CR0_TS);
707
708#if NNPX > 0
709	/* Initialize the npx (if any) for the current process. */
710	npxinit(__INITIAL_NPXCW__);
711#endif
712}
713
714static int
715sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS
716{
717	int error;
718	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
719		req);
720	if (!error && req->newptr)
721		resettodr();
722	return (error);
723}
724
725SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
726	&adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
727
728SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
729	CTLFLAG_RW, &disable_rtc_set, 0, "");
730
731SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
732	CTLFLAG_RD, &bootinfo, bootinfo, "");
733
734SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
735	CTLFLAG_RW, &wall_cmos_clock, 0, "");
736
737/*
738 * Initialize 386 and configure to run kernel
739 */
740
741/*
742 * Initialize segments & interrupt table
743 */
744
745int currentldt;
746int _default_ldt;
747union descriptor gdt[NGDT];		/* global descriptor table */
748struct gate_descriptor idt[NIDT];	/* interrupt descriptor table */
749union descriptor ldt[NLDT];		/* local descriptor table */
750
751static struct i386tss dblfault_tss;
752static char dblfault_stack[PAGE_SIZE];
753
754extern  struct user *proc0paddr;
755
756/* software prototypes -- in more palatable form */
757struct soft_segment_descriptor gdt_segs[] = {
758/* GNULL_SEL	0 Null Descriptor */
759{	0x0,			/* segment base address  */
760	0x0,			/* length */
761	0,			/* segment type */
762	0,			/* segment descriptor priority level */
763	0,			/* segment descriptor present */
764	0, 0,
765	0,			/* default 32 vs 16 bit size */
766	0  			/* limit granularity (byte/page units)*/ },
767/* GCODE_SEL	1 Code Descriptor for kernel */
768{	0x0,			/* segment base address  */
769	0xfffff,		/* length - all address space */
770	SDT_MEMERA,		/* segment type */
771	0,			/* segment descriptor priority level */
772	1,			/* segment descriptor present */
773	0, 0,
774	1,			/* default 32 vs 16 bit size */
775	1  			/* limit granularity (byte/page units)*/ },
776/* GDATA_SEL	2 Data Descriptor for kernel */
777{	0x0,			/* segment base address  */
778	0xfffff,		/* length - all address space */
779	SDT_MEMRWA,		/* segment type */
780	0,			/* segment descriptor priority level */
781	1,			/* segment descriptor present */
782	0, 0,
783	1,			/* default 32 vs 16 bit size */
784	1  			/* limit granularity (byte/page units)*/ },
785/* GLDT_SEL	3 LDT Descriptor */
786{	(int) ldt,		/* segment base address  */
787	sizeof(ldt)-1,		/* length - all address space */
788	SDT_SYSLDT,		/* segment type */
789	0,			/* segment descriptor priority level */
790	1,			/* segment descriptor present */
791	0, 0,
792	0,			/* unused - default 32 vs 16 bit size */
793	0  			/* limit granularity (byte/page units)*/ },
794/* GTGATE_SEL	4 Null Descriptor - Placeholder */
795{	0x0,			/* segment base address  */
796	0x0,			/* length - all address space */
797	0,			/* segment type */
798	0,			/* segment descriptor priority level */
799	0,			/* segment descriptor present */
800	0, 0,
801	0,			/* default 32 vs 16 bit size */
802	0  			/* limit granularity (byte/page units)*/ },
803/* GPANIC_SEL	5 Panic Tss Descriptor */
804{	(int) &dblfault_tss,	/* segment base address  */
805	sizeof(struct i386tss)-1,/* length - all address space */
806	SDT_SYS386TSS,		/* segment type */
807	0,			/* segment descriptor priority level */
808	1,			/* segment descriptor present */
809	0, 0,
810	0,			/* unused - default 32 vs 16 bit size */
811	0  			/* limit granularity (byte/page units)*/ },
812/* GPROC0_SEL	6 Proc 0 Tss Descriptor */
813{	(int) kstack,		/* segment base address  */
814	sizeof(struct i386tss)-1,/* length - all address space */
815	SDT_SYS386TSS,		/* segment type */
816	0,			/* segment descriptor priority level */
817	1,			/* segment descriptor present */
818	0, 0,
819	0,			/* unused - default 32 vs 16 bit size */
820	0  			/* limit granularity (byte/page units)*/ },
821/* GUSERLDT_SEL	7 User LDT Descriptor per process */
822{	(int) ldt,		/* segment base address  */
823	(512 * sizeof(union descriptor)-1),		/* length */
824	SDT_SYSLDT,		/* segment type */
825	0,			/* segment descriptor priority level */
826	1,			/* segment descriptor present */
827	0, 0,
828	0,			/* unused - default 32 vs 16 bit size */
829	0  			/* limit granularity (byte/page units)*/ },
830/* GAPMCODE32_SEL 8 APM BIOS 32-bit interface (32bit Code) */
831{	0,			/* segment base address (overwritten by APM)  */
832	0xfffff,		/* length */
833	SDT_MEMERA,		/* segment type */
834	0,			/* segment descriptor priority level */
835	1,			/* segment descriptor present */
836	0, 0,
837	1,			/* default 32 vs 16 bit size */
838	1  			/* limit granularity (byte/page units)*/ },
839/* GAPMCODE16_SEL 9 APM BIOS 32-bit interface (16bit Code) */
840{	0,			/* segment base address (overwritten by APM)  */
841	0xfffff,		/* length */
842	SDT_MEMERA,		/* segment type */
843	0,			/* segment descriptor priority level */
844	1,			/* segment descriptor present */
845	0, 0,
846	0,			/* default 32 vs 16 bit size */
847	1  			/* limit granularity (byte/page units)*/ },
848/* GAPMDATA_SEL	10 APM BIOS 32-bit interface (Data) */
849{	0,			/* segment base address (overwritten by APM) */
850	0xfffff,		/* length */
851	SDT_MEMRWA,		/* segment type */
852	0,			/* segment descriptor priority level */
853	1,			/* segment descriptor present */
854	0, 0,
855	1,			/* default 32 vs 16 bit size */
856	1  			/* limit granularity (byte/page units)*/ },
857};
858
859static struct soft_segment_descriptor ldt_segs[] = {
860	/* Null Descriptor - overwritten by call gate */
861{	0x0,			/* segment base address  */
862	0x0,			/* length - all address space */
863	0,			/* segment type */
864	0,			/* segment descriptor priority level */
865	0,			/* segment descriptor present */
866	0, 0,
867	0,			/* default 32 vs 16 bit size */
868	0  			/* limit granularity (byte/page units)*/ },
869	/* Null Descriptor - overwritten by call gate */
870{	0x0,			/* segment base address  */
871	0x0,			/* length - all address space */
872	0,			/* segment type */
873	0,			/* segment descriptor priority level */
874	0,			/* segment descriptor present */
875	0, 0,
876	0,			/* default 32 vs 16 bit size */
877	0  			/* limit granularity (byte/page units)*/ },
878	/* Null Descriptor - overwritten by call gate */
879{	0x0,			/* segment base address  */
880	0x0,			/* length - all address space */
881	0,			/* segment type */
882	0,			/* segment descriptor priority level */
883	0,			/* segment descriptor present */
884	0, 0,
885	0,			/* default 32 vs 16 bit size */
886	0  			/* limit granularity (byte/page units)*/ },
887	/* Code Descriptor for user */
888{	0x0,			/* segment base address  */
889	0xfffff,		/* length - all address space */
890	SDT_MEMERA,		/* segment type */
891	SEL_UPL,		/* segment descriptor priority level */
892	1,			/* segment descriptor present */
893	0, 0,
894	1,			/* default 32 vs 16 bit size */
895	1  			/* limit granularity (byte/page units)*/ },
896	/* Data Descriptor for user */
897{	0x0,			/* segment base address  */
898	0xfffff,		/* length - all address space */
899	SDT_MEMRWA,		/* segment type */
900	SEL_UPL,		/* segment descriptor priority level */
901	1,			/* segment descriptor present */
902	0, 0,
903	1,			/* default 32 vs 16 bit size */
904	1  			/* limit granularity (byte/page units)*/ },
905};
906
907void
908setidt(idx, func, typ, dpl, selec)
909	int idx;
910	inthand_t *func;
911	int typ;
912	int dpl;
913	int selec;
914{
915	struct gate_descriptor *ip = idt + idx;
916
917	ip->gd_looffset = (int)func;
918	ip->gd_selector = selec;
919	ip->gd_stkcpy = 0;
920	ip->gd_xx = 0;
921	ip->gd_type = typ;
922	ip->gd_dpl = dpl;
923	ip->gd_p = 1;
924	ip->gd_hioffset = ((int)func)>>16 ;
925}
926
927#define	IDTVEC(name)	__CONCAT(X,name)
928
929extern inthand_t
930	IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
931	IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
932	IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
933	IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
934	IDTVEC(syscall), IDTVEC(int0x80_syscall);
935
936void
937sdtossd(sd, ssd)
938	struct segment_descriptor *sd;
939	struct soft_segment_descriptor *ssd;
940{
941	ssd->ssd_base  = (sd->sd_hibase << 24) | sd->sd_lobase;
942	ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
943	ssd->ssd_type  = sd->sd_type;
944	ssd->ssd_dpl   = sd->sd_dpl;
945	ssd->ssd_p     = sd->sd_p;
946	ssd->ssd_def32 = sd->sd_def32;
947	ssd->ssd_gran  = sd->sd_gran;
948}
949
950void
951init386(first)
952	int first;
953{
954	int x;
955	unsigned biosbasemem, biosextmem;
956	struct gate_descriptor *gdp;
957	int gsel_tss;
958	struct isa_device *idp;
959	/* table descriptors - used to load tables by microp */
960	struct region_descriptor r_gdt, r_idt;
961	int	pagesinbase, pagesinext;
962	int	target_page, pa_indx;
963	int	off;
964
965	proc0.p_addr = proc0paddr;
966
967	atdevbase = ISA_HOLE_START + KERNBASE;
968
969	/*
970	 * Initialize the console before we print anything out.
971	 */
972	cninit();
973
974	/*
975	 * make gdt memory segments, the code segment goes up to end of the
976	 * page with etext in it, the data segment goes to the end of
977	 * the address space
978	 */
979	/*
980	 * XXX text protection is temporarily (?) disabled.  The limit was
981	 * i386_btop(round_page(etext)) - 1.
982	 */
983	gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1;
984	gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1;
985	for (x = 0; x < NGDT; x++)
986		ssdtosd(&gdt_segs[x], &gdt[x].sd);
987
988	/* make ldt memory segments */
989	/*
990	 * The data segment limit must not cover the user area because we
991	 * don't want the user area to be writable in copyout() etc. (page
992	 * level protection is lost in kernel mode on 386's).  Also, we
993	 * don't want the user area to be writable directly (page level
994	 * protection of the user area is not available on 486's with
995	 * CR0_WP set, because there is no user-read/kernel-write mode).
996	 *
997	 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  And it
998	 * should be spelled ...MAX_USER...
999	 */
1000#define VM_END_USER_RW_ADDRESS	VM_MAXUSER_ADDRESS
1001	/*
1002	 * The code segment limit has to cover the user area until we move
1003	 * the signal trampoline out of the user area.  This is safe because
1004	 * the code segment cannot be written to directly.
1005	 */
1006#define VM_END_USER_R_ADDRESS	(VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE)
1007	ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1;
1008	ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1;
1009	/* Note. eventually want private ldts per process */
1010	for (x = 0; x < NLDT; x++)
1011		ssdtosd(&ldt_segs[x], &ldt[x].sd);
1012
1013	/* exceptions */
1014	for (x = 0; x < NIDT; x++)
1015		setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1016	setidt(0, &IDTVEC(div),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1017	setidt(1, &IDTVEC(dbg),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1018	setidt(2, &IDTVEC(nmi),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1019 	setidt(3, &IDTVEC(bpt),  SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1020	setidt(4, &IDTVEC(ofl),  SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1021	setidt(5, &IDTVEC(bnd),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1022	setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1023	setidt(7, &IDTVEC(dna),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1024	setidt(8, 0,  SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1025	setidt(9, &IDTVEC(fpusegm),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1026	setidt(10, &IDTVEC(tss),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1027	setidt(11, &IDTVEC(missing),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1028	setidt(12, &IDTVEC(stk),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1029	setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1030	setidt(14, &IDTVEC(page),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1031	setidt(15, &IDTVEC(rsvd),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1032	setidt(16, &IDTVEC(fpu),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1033	setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1034	setidt(18, &IDTVEC(mchk),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1035 	setidt(0x80, &IDTVEC(int0x80_syscall),
1036			SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1037
1038#include	"isa.h"
1039#if	NISA >0
1040	isa_defaultirq();
1041#endif
1042	rand_initialize();
1043
1044	r_gdt.rd_limit = sizeof(gdt) - 1;
1045	r_gdt.rd_base =  (int) gdt;
1046	lgdt(&r_gdt);
1047
1048	r_idt.rd_limit = sizeof(idt) - 1;
1049	r_idt.rd_base = (int) idt;
1050	lidt(&r_idt);
1051
1052	_default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1053	lldt(_default_ldt);
1054	currentldt = _default_ldt;
1055
1056#ifdef DDB
1057	kdb_init();
1058	if (boothowto & RB_KDB)
1059		Debugger("Boot flags requested debugger");
1060#endif
1061
1062	/* Use BIOS values stored in RTC CMOS RAM, since probing
1063	 * breaks certain 386 AT relics.
1064	 */
1065	biosbasemem = rtcin(RTC_BASELO)+ (rtcin(RTC_BASEHI)<<8);
1066	biosextmem = rtcin(RTC_EXTLO)+ (rtcin(RTC_EXTHI)<<8);
1067
1068	/*
1069	 * If BIOS tells us that it has more than 640k in the basemem,
1070	 *	don't believe it - set it to 640k.
1071	 */
1072	if (biosbasemem > 640) {
1073		printf("Preposterous RTC basemem of %dK, truncating to 640K\n",
1074		       biosbasemem);
1075		biosbasemem = 640;
1076	}
1077	if (bootinfo.bi_memsizes_valid && bootinfo.bi_basemem > 640) {
1078		printf("Preposterous BIOS basemem of %dK, truncating to 640K\n",
1079		       bootinfo.bi_basemem);
1080		bootinfo.bi_basemem = 640;
1081	}
1082
1083	/*
1084	 * Warn if the official BIOS interface disagrees with the RTC
1085	 * interface used above about the amount of base memory or the
1086	 * amount of extended memory.  Prefer the BIOS value for the base
1087	 * memory.  This is necessary for machines that `steal' base
1088	 * memory for use as BIOS memory, at least if we are going to use
1089	 * the BIOS for apm.  Prefer the RTC value for extended memory.
1090	 * Eventually the hackish interface shouldn't even be looked at.
1091	 */
1092	if (bootinfo.bi_memsizes_valid) {
1093		if (bootinfo.bi_basemem != biosbasemem) {
1094			vm_offset_t pa;
1095
1096			printf(
1097	"BIOS basemem (%ldK) != RTC basemem (%dK), setting to BIOS value\n",
1098			       bootinfo.bi_basemem, biosbasemem);
1099			biosbasemem = bootinfo.bi_basemem;
1100
1101			/*
1102			 * XXX if biosbasemem is now < 640, there is `hole'
1103			 * between the end of base memory and the start of
1104			 * ISA memory.  The hole may be empty or it may
1105			 * contain BIOS code or data.  Map it read/write so
1106			 * that the BIOS can write to it.  (Memory from 0 to
1107			 * the physical end of the kernel is mapped read-only
1108			 * to begin with and then parts of it are remapped.
1109			 * The parts that aren't remapped form holes that
1110			 * remain read-only and are unused by the kernel.
1111			 * The base memory area is below the physical end of
1112			 * the kernel and right now forms a read-only hole.
1113			 * The part of it from 0 to
1114			 * (trunc_page(biosbasemem * 1024) - 1) will be
1115			 * remapped and used by the kernel later.)
1116			 *
1117			 * This code is similar to the code used in
1118			 * pmap_mapdev, but since no memory needs to be
1119			 * allocated we simply change the mapping.
1120			 */
1121			for (pa = trunc_page(biosbasemem * 1024);
1122			     pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1123				unsigned *pte;
1124
1125				pte = (unsigned *)vtopte(pa + KERNBASE);
1126				*pte = pa | PG_RW | PG_V;
1127			}
1128		}
1129		if (bootinfo.bi_extmem != biosextmem)
1130			printf("BIOS extmem (%ldK) != RTC extmem (%dK)\n",
1131			       bootinfo.bi_extmem, biosextmem);
1132	}
1133
1134	pagesinbase = biosbasemem * 1024 / PAGE_SIZE;
1135	pagesinext = biosextmem * 1024 / PAGE_SIZE;
1136
1137	/*
1138	 * Special hack for chipsets that still remap the 384k hole when
1139	 *	there's 16MB of memory - this really confuses people that
1140	 *	are trying to use bus mastering ISA controllers with the
1141	 *	"16MB limit"; they only have 16MB, but the remapping puts
1142	 *	them beyond the limit.
1143	 */
1144	/*
1145	 * If extended memory is between 15-16MB (16-17MB phys address range),
1146	 *	chop it to 15MB.
1147	 */
1148	if ((pagesinext > 3840) && (pagesinext < 4096))
1149		pagesinext = 3840;
1150
1151	/*
1152	 * Maxmem isn't the "maximum memory", it's one larger than the
1153	 * highest page of the physical address space.  It should be
1154	 * called something like "Maxphyspage".
1155	 */
1156	Maxmem = pagesinext + 0x100000/PAGE_SIZE;
1157
1158#ifdef MAXMEM
1159	Maxmem = MAXMEM/4;
1160#endif
1161
1162#if NNPX > 0
1163	idp = find_isadev(isa_devtab_null, &npxdriver, 0);
1164	if (idp != NULL && idp->id_msize != 0)
1165		Maxmem = idp->id_msize / 4;
1166#endif
1167
1168	/* call pmap initialization to make new kernel address space */
1169	pmap_bootstrap (first, 0);
1170
1171	/*
1172	 * Size up each available chunk of physical memory.
1173	 */
1174
1175	/*
1176	 * We currently don't bother testing base memory.
1177	 * XXX  ...but we probably should.
1178	 */
1179	pa_indx = 0;
1180	badpages = 0;
1181	if (pagesinbase > 1) {
1182		phys_avail[pa_indx++] = PAGE_SIZE;	/* skip first page of memory */
1183		phys_avail[pa_indx] = ptoa(pagesinbase);/* memory up to the ISA hole */
1184		physmem = pagesinbase - 1;
1185	} else {
1186		/* point at first chunk end */
1187		pa_indx++;
1188	}
1189
1190	for (target_page = avail_start; target_page < ptoa(Maxmem); target_page += PAGE_SIZE) {
1191		int tmp, page_bad = FALSE;
1192
1193		/*
1194		 * map page into kernel: valid, read/write, non-cacheable
1195		 */
1196		*(int *)CMAP1 = PG_V | PG_RW | PG_N | target_page;
1197		invltlb();
1198
1199		tmp = *(int *)CADDR1;
1200		/*
1201		 * Test for alternating 1's and 0's
1202		 */
1203		*(volatile int *)CADDR1 = 0xaaaaaaaa;
1204		if (*(volatile int *)CADDR1 != 0xaaaaaaaa) {
1205			page_bad = TRUE;
1206		}
1207		/*
1208		 * Test for alternating 0's and 1's
1209		 */
1210		*(volatile int *)CADDR1 = 0x55555555;
1211		if (*(volatile int *)CADDR1 != 0x55555555) {
1212			page_bad = TRUE;
1213		}
1214		/*
1215		 * Test for all 1's
1216		 */
1217		*(volatile int *)CADDR1 = 0xffffffff;
1218		if (*(volatile int *)CADDR1 != 0xffffffff) {
1219			page_bad = TRUE;
1220		}
1221		/*
1222		 * Test for all 0's
1223		 */
1224		*(volatile int *)CADDR1 = 0x0;
1225		if (*(volatile int *)CADDR1 != 0x0) {
1226			/*
1227			 * test of page failed
1228			 */
1229			page_bad = TRUE;
1230		}
1231		/*
1232		 * Restore original value.
1233		 */
1234		*(int *)CADDR1 = tmp;
1235
1236		/*
1237		 * Adjust array of valid/good pages.
1238		 */
1239		if (page_bad == FALSE) {
1240			/*
1241			 * If this good page is a continuation of the
1242			 * previous set of good pages, then just increase
1243			 * the end pointer. Otherwise start a new chunk.
1244			 * Note that "end" points one higher than end,
1245			 * making the range >= start and < end.
1246			 */
1247			if (phys_avail[pa_indx] == target_page) {
1248				phys_avail[pa_indx] += PAGE_SIZE;
1249			} else {
1250				pa_indx++;
1251				if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1252					printf("Too many holes in the physical address space, giving up\n");
1253					pa_indx--;
1254					break;
1255				}
1256				phys_avail[pa_indx++] = target_page;	/* start */
1257				phys_avail[pa_indx] = target_page + PAGE_SIZE;	/* end */
1258			}
1259			physmem++;
1260		} else {
1261			badpages++;
1262			page_bad = FALSE;
1263		}
1264	}
1265
1266	*(int *)CMAP1 = 0;
1267	invltlb();
1268
1269	/*
1270	 * XXX
1271	 * The last chunk must contain at least one page plus the message
1272	 * buffer to avoid complicating other code (message buffer address
1273	 * calculation, etc.).
1274	 */
1275	while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1276	    round_page(sizeof(struct msgbuf)) >= phys_avail[pa_indx]) {
1277		physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1278		phys_avail[pa_indx--] = 0;
1279		phys_avail[pa_indx--] = 0;
1280	}
1281
1282	Maxmem = atop(phys_avail[pa_indx]);
1283
1284	/* Trim off space for the message buffer. */
1285	phys_avail[pa_indx] -= round_page(sizeof(struct msgbuf));
1286
1287	avail_end = phys_avail[pa_indx];
1288
1289	/* now running on new page tables, configured,and u/iom is accessible */
1290
1291	/* Map the message buffer. */
1292	for (off = 0; off < round_page(sizeof(struct msgbuf)); off += PAGE_SIZE)
1293		pmap_enter(kernel_pmap, (vm_offset_t)msgbufp + off,
1294			   avail_end + off, VM_PROT_ALL, TRUE);
1295	msgbufmapped = 1;
1296
1297	/* make a initial tss so microp can get interrupt stack on syscall! */
1298	proc0.p_addr->u_pcb.pcb_tss.tss_esp0 = (int) kstack + UPAGES*PAGE_SIZE;
1299	proc0.p_addr->u_pcb.pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1300	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1301
1302	dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1303	    dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1304	dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1305	    dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1306	dblfault_tss.tss_cr3 = IdlePTD;
1307	dblfault_tss.tss_eip = (int) dblfault_handler;
1308	dblfault_tss.tss_eflags = PSL_KERNEL;
1309	dblfault_tss.tss_ds = dblfault_tss.tss_es = dblfault_tss.tss_fs = dblfault_tss.tss_gs =
1310		GSEL(GDATA_SEL, SEL_KPL);
1311	dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1312	dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1313
1314	((struct i386tss *)gdt_segs[GPROC0_SEL].ssd_base)->tss_ioopt =
1315		(sizeof(struct i386tss))<<16;
1316
1317	ltr(gsel_tss);
1318
1319	/* make a call gate to reenter kernel with */
1320	gdp = &ldt[LSYS5CALLS_SEL].gd;
1321
1322	x = (int) &IDTVEC(syscall);
1323	gdp->gd_looffset = x++;
1324	gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1325	gdp->gd_stkcpy = 1;
1326	gdp->gd_type = SDT_SYS386CGT;
1327	gdp->gd_dpl = SEL_UPL;
1328	gdp->gd_p = 1;
1329	gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
1330
1331	/* XXX does this work? */
1332	ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1333
1334	/* transfer to user mode */
1335
1336	_ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1337	_udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1338
1339	/* setup proc 0's pcb */
1340	proc0.p_addr->u_pcb.pcb_flags = 0;
1341	proc0.p_addr->u_pcb.pcb_cr3 = IdlePTD;
1342}
1343
1344/*
1345 * The registers are in the frame; the frame is in the user area of
1346 * the process in question; when the process is active, the registers
1347 * are in "the kernel stack"; when it's not, they're still there, but
1348 * things get flipped around.  So, since p->p_md.md_regs is the whole address
1349 * of the register set, take its offset from the kernel stack, and
1350 * index into the user block.  Don't you just *love* virtual memory?
1351 * (I'm starting to think seymour is right...)
1352 */
1353#define	TF_REGP(p)	((struct trapframe *) \
1354			 ((char *)(p)->p_addr \
1355			  + ((char *)(p)->p_md.md_regs - kstack)))
1356
1357int
1358ptrace_set_pc(p, addr)
1359	struct proc *p;
1360	unsigned int addr;
1361{
1362	TF_REGP(p)->tf_eip = addr;
1363	return (0);
1364}
1365
1366int
1367ptrace_single_step(p)
1368	struct proc *p;
1369{
1370	TF_REGP(p)->tf_eflags |= PSL_T;
1371	return (0);
1372}
1373
1374int ptrace_write_u(p, off, data)
1375	struct proc *p;
1376	vm_offset_t off;
1377	int data;
1378{
1379	struct trapframe frame_copy;
1380	vm_offset_t min;
1381	struct trapframe *tp;
1382
1383	/*
1384	 * Privileged kernel state is scattered all over the user area.
1385	 * Only allow write access to parts of regs and to fpregs.
1386	 */
1387	min = (char *)p->p_md.md_regs - kstack;
1388	if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) {
1389		tp = TF_REGP(p);
1390		frame_copy = *tp;
1391		*(int *)((char *)&frame_copy + (off - min)) = data;
1392		if (!EFLAGS_SECURE(frame_copy.tf_eflags, tp->tf_eflags) ||
1393		    !CS_SECURE(frame_copy.tf_cs))
1394			return (EINVAL);
1395		*(int*)((char *)p->p_addr + off) = data;
1396		return (0);
1397	}
1398	min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu);
1399	if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) {
1400		*(int*)((char *)p->p_addr + off) = data;
1401		return (0);
1402	}
1403	return (EFAULT);
1404}
1405
1406int
1407fill_regs(p, regs)
1408	struct proc *p;
1409	struct reg *regs;
1410{
1411	struct trapframe *tp;
1412
1413	tp = TF_REGP(p);
1414	regs->r_es = tp->tf_es;
1415	regs->r_ds = tp->tf_ds;
1416	regs->r_edi = tp->tf_edi;
1417	regs->r_esi = tp->tf_esi;
1418	regs->r_ebp = tp->tf_ebp;
1419	regs->r_ebx = tp->tf_ebx;
1420	regs->r_edx = tp->tf_edx;
1421	regs->r_ecx = tp->tf_ecx;
1422	regs->r_eax = tp->tf_eax;
1423	regs->r_eip = tp->tf_eip;
1424	regs->r_cs = tp->tf_cs;
1425	regs->r_eflags = tp->tf_eflags;
1426	regs->r_esp = tp->tf_esp;
1427	regs->r_ss = tp->tf_ss;
1428	return (0);
1429}
1430
1431int
1432set_regs(p, regs)
1433	struct proc *p;
1434	struct reg *regs;
1435{
1436	struct trapframe *tp;
1437
1438	tp = TF_REGP(p);
1439	if (!EFLAGS_SECURE(regs->r_eflags, tp->tf_eflags) ||
1440	    !CS_SECURE(regs->r_cs))
1441		return (EINVAL);
1442	tp->tf_es = regs->r_es;
1443	tp->tf_ds = regs->r_ds;
1444	tp->tf_edi = regs->r_edi;
1445	tp->tf_esi = regs->r_esi;
1446	tp->tf_ebp = regs->r_ebp;
1447	tp->tf_ebx = regs->r_ebx;
1448	tp->tf_edx = regs->r_edx;
1449	tp->tf_ecx = regs->r_ecx;
1450	tp->tf_eax = regs->r_eax;
1451	tp->tf_eip = regs->r_eip;
1452	tp->tf_cs = regs->r_cs;
1453	tp->tf_eflags = regs->r_eflags;
1454	tp->tf_esp = regs->r_esp;
1455	tp->tf_ss = regs->r_ss;
1456	return (0);
1457}
1458
1459#ifndef DDB
1460void
1461Debugger(const char *msg)
1462{
1463	printf("Debugger(\"%s\") called.\n", msg);
1464}
1465#endif /* no DDB */
1466
1467#include <sys/disklabel.h>
1468
1469/*
1470 * Determine the size of the transfer, and make sure it is
1471 * within the boundaries of the partition. Adjust transfer
1472 * if needed, and signal errors or early completion.
1473 */
1474int
1475bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
1476{
1477        struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
1478        int labelsect = lp->d_partitions[0].p_offset;
1479        int maxsz = p->p_size,
1480                sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1481
1482        /* overwriting disk label ? */
1483        /* XXX should also protect bootstrap in first 8K */
1484        if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
1485#if LABELSECTOR != 0
1486            bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
1487#endif
1488            (bp->b_flags & B_READ) == 0 && wlabel == 0) {
1489                bp->b_error = EROFS;
1490                goto bad;
1491        }
1492
1493#if     defined(DOSBBSECTOR) && defined(notyet)
1494        /* overwriting master boot record? */
1495        if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
1496            (bp->b_flags & B_READ) == 0 && wlabel == 0) {
1497                bp->b_error = EROFS;
1498                goto bad;
1499        }
1500#endif
1501
1502        /* beyond partition? */
1503        if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
1504                /* if exactly at end of disk, return an EOF */
1505                if (bp->b_blkno == maxsz) {
1506                        bp->b_resid = bp->b_bcount;
1507                        return(0);
1508                }
1509                /* or truncate if part of it fits */
1510                sz = maxsz - bp->b_blkno;
1511                if (sz <= 0) {
1512                        bp->b_error = EINVAL;
1513                        goto bad;
1514                }
1515                bp->b_bcount = sz << DEV_BSHIFT;
1516        }
1517
1518        bp->b_pblkno = bp->b_blkno + p->p_offset;
1519        return(1);
1520
1521bad:
1522        bp->b_flags |= B_ERROR;
1523        return(-1);
1524}
1525