machdep.c revision 19828
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.211 1996/11/11 20:38:52 bde 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 <vm/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) / 6, 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*MAXBSIZE) + (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*MAXBSIZE) + (nswbuf*MAXPHYS) + pager_map_size, TRUE);
331#endif
332	buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
333				(nbuf*MAXBSIZE), 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				(32*ARG_MAX), 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	mclrefcnt = (char *)malloc(nmbclusters+PAGE_SIZE/MCLBYTES,
348				   M_MBUF, M_NOWAIT);
349	bzero(mclrefcnt, nmbclusters+PAGE_SIZE/MCLBYTES);
350	mcl_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr,
351			       nmbclusters * MCLBYTES, FALSE);
352	{
353		vm_size_t mb_map_size;
354		mb_map_size = nmbufs * MSIZE;
355		mb_map = kmem_suballoc(kmem_map, &minaddr, &maxaddr,
356				       round_page(mb_map_size), FALSE);
357	}
358
359	/*
360	 * Initialize callouts
361	 */
362	callfree = callout;
363	for (i = 1; i < ncallout; i++)
364		callout[i-1].c_next = &callout[i];
365
366#if defined(USERCONFIG_BOOT) && defined(USERCONFIG)
367	boothowto |= RB_CONFIG;
368#endif
369
370        if (boothowto & RB_CONFIG) {
371#ifdef USERCONFIG
372		userconfig();
373		cninit();	/* the preferred console may have changed */
374#else
375		printf("Sorry! no userconfig in this kernel\n");
376#endif
377	}
378
379#ifdef BOUNCE_BUFFERS
380	/*
381	 * init bounce buffers
382	 */
383	vm_bounce_init();
384#endif
385
386	printf("avail memory = %d (%dK bytes)\n", ptoa(cnt.v_free_count),
387	    ptoa(cnt.v_free_count) / 1024);
388
389	/*
390	 * Set up buffers, so they can be used to read disk labels.
391	 */
392	bufinit();
393	vm_pager_bufferinit();
394
395	/*
396	 * In verbose mode, print out the BIOS's idea of the disk geometries.
397	 */
398	if (bootverbose) {
399		printf("BIOS Geometries:\n");
400		for (i = 0; i < N_BIOS_GEOM; i++) {
401			unsigned long bios_geom;
402			int max_cylinder, max_head, max_sector;
403
404			bios_geom = bootinfo.bi_bios_geom[i];
405
406			/*
407			 * XXX the bootstrap punts a 1200K floppy geometry
408			 * when the get-disk-geometry interrupt fails.  Skip
409			 * drives that have this geometry.
410			 */
411			if (bios_geom == 0x4f010f)
412				continue;
413
414			printf(" %x:%08lx ", i, bios_geom);
415			max_cylinder = bios_geom >> 16;
416			max_head = (bios_geom >> 8) & 0xff;
417			max_sector = bios_geom & 0xff;
418			printf(
419		"0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
420			       max_cylinder, max_cylinder + 1,
421			       max_head, max_head + 1,
422			       max_sector, max_sector);
423		}
424		printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
425	}
426}
427
428int
429register_netisr(num, handler)
430	int num;
431	netisr_t *handler;
432{
433
434	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
435		printf("register_netisr: bad isr number: %d\n", num);
436		return (EINVAL);
437	}
438	netisrs[num] = handler;
439	return (0);
440}
441
442static void
443setup_netisrs(ls)
444	struct linker_set *ls;
445{
446	int i;
447	const struct netisrtab *nit;
448
449	for(i = 0; ls->ls_items[i]; i++) {
450		nit = (const struct netisrtab *)ls->ls_items[i];
451		register_netisr(nit->nit_num, nit->nit_isr);
452	}
453}
454
455/*
456 * Send an interrupt to process.
457 *
458 * Stack is set up to allow sigcode stored
459 * at top to call routine, followed by kcall
460 * to sigreturn routine below.  After sigreturn
461 * resets the signal mask, the stack, and the
462 * frame pointer, it returns to the user
463 * specified pc, psl.
464 */
465void
466sendsig(catcher, sig, mask, code)
467	sig_t catcher;
468	int sig, mask;
469	u_long code;
470{
471	register struct proc *p = curproc;
472	register int *regs;
473	register struct sigframe *fp;
474	struct sigframe sf;
475	struct sigacts *psp = p->p_sigacts;
476	int oonstack;
477
478	regs = p->p_md.md_regs;
479        oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
480	/*
481	 * Allocate and validate space for the signal handler context.
482	 */
483        if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
484	    (psp->ps_sigonstack & sigmask(sig))) {
485		fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
486		    psp->ps_sigstk.ss_size - sizeof(struct sigframe));
487		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
488	} else {
489		fp = (struct sigframe *)regs[tESP] - 1;
490	}
491
492	/*
493	 * grow() will return FALSE if the fp will not fit inside the stack
494	 *	and the stack can not be grown. useracc will return FALSE
495	 *	if access is denied.
496	 */
497	if ((grow(p, (int)fp) == FALSE) ||
498	    (useracc((caddr_t)fp, sizeof (struct sigframe), B_WRITE) == FALSE)) {
499		/*
500		 * Process has trashed its stack; give it an illegal
501		 * instruction to halt it in its tracks.
502		 */
503		SIGACTION(p, SIGILL) = SIG_DFL;
504		sig = sigmask(SIGILL);
505		p->p_sigignore &= ~sig;
506		p->p_sigcatch &= ~sig;
507		p->p_sigmask &= ~sig;
508		psignal(p, SIGILL);
509		return;
510	}
511
512	/*
513	 * Build the argument list for the signal handler.
514	 */
515	if (p->p_sysent->sv_sigtbl) {
516		if (sig < p->p_sysent->sv_sigsize)
517			sig = p->p_sysent->sv_sigtbl[sig];
518		else
519			sig = p->p_sysent->sv_sigsize + 1;
520	}
521	sf.sf_signum = sig;
522	sf.sf_code = code;
523	sf.sf_scp = &fp->sf_sc;
524	sf.sf_addr = (char *) regs[tERR];
525	sf.sf_handler = catcher;
526
527	/* save scratch registers */
528	sf.sf_sc.sc_eax = regs[tEAX];
529	sf.sf_sc.sc_ebx = regs[tEBX];
530	sf.sf_sc.sc_ecx = regs[tECX];
531	sf.sf_sc.sc_edx = regs[tEDX];
532	sf.sf_sc.sc_esi = regs[tESI];
533	sf.sf_sc.sc_edi = regs[tEDI];
534	sf.sf_sc.sc_cs = regs[tCS];
535	sf.sf_sc.sc_ds = regs[tDS];
536	sf.sf_sc.sc_ss = regs[tSS];
537	sf.sf_sc.sc_es = regs[tES];
538	sf.sf_sc.sc_isp = regs[tISP];
539
540	/*
541	 * Build the signal context to be used by sigreturn.
542	 */
543	sf.sf_sc.sc_onstack = oonstack;
544	sf.sf_sc.sc_mask = mask;
545	sf.sf_sc.sc_sp = regs[tESP];
546	sf.sf_sc.sc_fp = regs[tEBP];
547	sf.sf_sc.sc_pc = regs[tEIP];
548	sf.sf_sc.sc_ps = regs[tEFLAGS];
549
550	/*
551	 * Copy the sigframe out to the user's stack.
552	 */
553	if (copyout(&sf, fp, sizeof(struct sigframe)) != 0) {
554		/*
555		 * Something is wrong with the stack pointer.
556		 * ...Kill the process.
557		 */
558		sigexit(p, SIGILL);
559	};
560
561	regs[tESP] = (int)fp;
562	regs[tEIP] = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
563	regs[tEFLAGS] &= ~PSL_VM;
564	regs[tCS] = _ucodesel;
565	regs[tDS] = _udatasel;
566	regs[tES] = _udatasel;
567	regs[tSS] = _udatasel;
568}
569
570/*
571 * System call to cleanup state after a signal
572 * has been taken.  Reset signal mask and
573 * stack state from context left by sendsig (above).
574 * Return to previous pc and psl as specified by
575 * context left by sendsig. Check carefully to
576 * make sure that the user has not modified the
577 * state to gain improper privileges.
578 */
579int
580sigreturn(p, uap, retval)
581	struct proc *p;
582	struct sigreturn_args /* {
583		struct sigcontext *sigcntxp;
584	} */ *uap;
585	int *retval;
586{
587	register struct sigcontext *scp;
588	register struct sigframe *fp;
589	register int *regs = p->p_md.md_regs;
590	int eflags;
591
592	/*
593	 * (XXX old comment) regs[tESP] points to the return address.
594	 * The user scp pointer is above that.
595	 * The return address is faked in the signal trampoline code
596	 * for consistency.
597	 */
598	scp = uap->sigcntxp;
599	fp = (struct sigframe *)
600	     ((caddr_t)scp - offsetof(struct sigframe, sf_sc));
601
602	if (useracc((caddr_t)fp, sizeof (*fp), 0) == 0)
603		return(EINVAL);
604
605	/*
606	 * Don't allow users to change privileged or reserved flags.
607	 */
608#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
609	eflags = scp->sc_ps;
610	/*
611	 * XXX do allow users to change the privileged flag PSL_RF.  The
612	 * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
613	 * sometimes set it there too.  tf_eflags is kept in the signal
614	 * context during signal handling and there is no other place
615	 * to remember it, so the PSL_RF bit may be corrupted by the
616	 * signal handler without us knowing.  Corruption of the PSL_RF
617	 * bit at worst causes one more or one less debugger trap, so
618	 * allowing it is fairly harmless.
619	 */
620	if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs[tEFLAGS] & ~PSL_RF)) {
621#ifdef DEBUG
622    		printf("sigreturn: eflags = 0x%x\n", eflags);
623#endif
624    		return(EINVAL);
625	}
626
627	/*
628	 * Don't allow users to load a valid privileged %cs.  Let the
629	 * hardware check for invalid selectors, excess privilege in
630	 * other selectors, invalid %eip's and invalid %esp's.
631	 */
632#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
633	if (!CS_SECURE(scp->sc_cs)) {
634#ifdef DEBUG
635    		printf("sigreturn: cs = 0x%x\n", scp->sc_cs);
636#endif
637		trapsignal(p, SIGBUS, T_PROTFLT);
638		return(EINVAL);
639	}
640
641	/* restore scratch registers */
642	regs[tEAX] = scp->sc_eax;
643	regs[tEBX] = scp->sc_ebx;
644	regs[tECX] = scp->sc_ecx;
645	regs[tEDX] = scp->sc_edx;
646	regs[tESI] = scp->sc_esi;
647	regs[tEDI] = scp->sc_edi;
648	regs[tCS] = scp->sc_cs;
649	regs[tDS] = scp->sc_ds;
650	regs[tES] = scp->sc_es;
651	regs[tSS] = scp->sc_ss;
652	regs[tISP] = scp->sc_isp;
653
654	if (useracc((caddr_t)scp, sizeof (*scp), 0) == 0)
655		return(EINVAL);
656
657	if (scp->sc_onstack & 01)
658		p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
659	else
660		p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
661	p->p_sigmask = scp->sc_mask &~
662	    (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP));
663	regs[tEBP] = scp->sc_fp;
664	regs[tESP] = scp->sc_sp;
665	regs[tEIP] = scp->sc_pc;
666	regs[tEFLAGS] = eflags;
667	return(EJUSTRETURN);
668}
669
670/*
671 * Machine depdnetnt boot() routine
672 *
673 * I haven't seen anything too 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 * Clear registers on exec
693 */
694void
695setregs(p, entry, stack)
696	struct proc *p;
697	u_long entry;
698	u_long stack;
699{
700	int *regs = p->p_md.md_regs;
701
702#ifdef USER_LDT
703	struct pcb *pcb = &p->p_addr->u_pcb;
704
705	/* was i386_user_cleanup() in NetBSD */
706	if (pcb->pcb_ldt) {
707		if (pcb == curpcb)
708			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
709		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
710			pcb->pcb_ldt_len * sizeof(union descriptor));
711		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
712 	}
713#endif
714
715	bzero(regs, sizeof(struct trapframe));
716	regs[tEIP] = entry;
717	regs[tESP] = stack;
718	regs[tEFLAGS] = PSL_USER | (regs[tEFLAGS] & PSL_T);
719	regs[tSS] = _udatasel;
720	regs[tDS] = _udatasel;
721	regs[tES] = _udatasel;
722	regs[tCS] = _ucodesel;
723
724	p->p_addr->u_pcb.pcb_flags = 0;	/* no fp at all */
725	load_cr0(rcr0() | CR0_TS);	/* start emulating */
726#if	NNPX > 0
727	npxinit(__INITIAL_NPXCW__);
728#endif	/* NNPX > 0 */
729}
730
731static int
732sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS
733{
734	int error;
735	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
736		req);
737	if (!error && req->newptr)
738		resettodr();
739	return (error);
740}
741
742SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
743	&adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
744
745SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
746	CTLFLAG_RW, &disable_rtc_set, 0, "");
747
748SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
749	CTLFLAG_RD, &bootinfo, bootinfo, "");
750
751SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
752	CTLFLAG_RW, &wall_cmos_clock, 0, "");
753
754/*
755 * Initialize 386 and configure to run kernel
756 */
757
758/*
759 * Initialize segments & interrupt table
760 */
761
762int currentldt;
763int _default_ldt;
764union descriptor gdt[NGDT];		/* global descriptor table */
765struct gate_descriptor idt[NIDT];	/* interrupt descriptor table */
766union descriptor ldt[NLDT];		/* local descriptor table */
767
768static struct i386tss dblfault_tss;
769static char dblfault_stack[PAGE_SIZE];
770
771extern  struct user *proc0paddr;
772
773/* software prototypes -- in more palatable form */
774struct soft_segment_descriptor gdt_segs[] = {
775/* GNULL_SEL	0 Null Descriptor */
776{	0x0,			/* segment base address  */
777	0x0,			/* length */
778	0,			/* segment type */
779	0,			/* segment descriptor priority level */
780	0,			/* segment descriptor present */
781	0, 0,
782	0,			/* default 32 vs 16 bit size */
783	0  			/* limit granularity (byte/page units)*/ },
784/* GCODE_SEL	1 Code Descriptor for kernel */
785{	0x0,			/* segment base address  */
786	0xfffff,		/* length - all address space */
787	SDT_MEMERA,		/* segment type */
788	0,			/* segment descriptor priority level */
789	1,			/* segment descriptor present */
790	0, 0,
791	1,			/* default 32 vs 16 bit size */
792	1  			/* limit granularity (byte/page units)*/ },
793/* GDATA_SEL	2 Data Descriptor for kernel */
794{	0x0,			/* segment base address  */
795	0xfffff,		/* length - all address space */
796	SDT_MEMRWA,		/* segment type */
797	0,			/* segment descriptor priority level */
798	1,			/* segment descriptor present */
799	0, 0,
800	1,			/* default 32 vs 16 bit size */
801	1  			/* limit granularity (byte/page units)*/ },
802/* GLDT_SEL	3 LDT Descriptor */
803{	(int) ldt,		/* segment base address  */
804	sizeof(ldt)-1,		/* length - all address space */
805	SDT_SYSLDT,		/* segment type */
806	0,			/* segment descriptor priority level */
807	1,			/* segment descriptor present */
808	0, 0,
809	0,			/* unused - default 32 vs 16 bit size */
810	0  			/* limit granularity (byte/page units)*/ },
811/* GTGATE_SEL	4 Null Descriptor - Placeholder */
812{	0x0,			/* segment base address  */
813	0x0,			/* length - all address space */
814	0,			/* segment type */
815	0,			/* segment descriptor priority level */
816	0,			/* segment descriptor present */
817	0, 0,
818	0,			/* default 32 vs 16 bit size */
819	0  			/* limit granularity (byte/page units)*/ },
820/* GPANIC_SEL	5 Panic Tss Descriptor */
821{	(int) &dblfault_tss,	/* segment base address  */
822	sizeof(struct i386tss)-1,/* length - all address space */
823	SDT_SYS386TSS,		/* segment type */
824	0,			/* segment descriptor priority level */
825	1,			/* segment descriptor present */
826	0, 0,
827	0,			/* unused - default 32 vs 16 bit size */
828	0  			/* limit granularity (byte/page units)*/ },
829/* GPROC0_SEL	6 Proc 0 Tss Descriptor */
830{	(int) kstack,		/* segment base address  */
831	sizeof(struct i386tss)-1,/* length - all address space */
832	SDT_SYS386TSS,		/* segment type */
833	0,			/* segment descriptor priority level */
834	1,			/* segment descriptor present */
835	0, 0,
836	0,			/* unused - default 32 vs 16 bit size */
837	0  			/* limit granularity (byte/page units)*/ },
838/* GUSERLDT_SEL	7 User LDT Descriptor per process */
839{	(int) ldt,		/* segment base address  */
840	(512 * sizeof(union descriptor)-1),		/* length */
841	SDT_SYSLDT,		/* segment type */
842	0,			/* segment descriptor priority level */
843	1,			/* segment descriptor present */
844	0, 0,
845	0,			/* unused - default 32 vs 16 bit size */
846	0  			/* limit granularity (byte/page units)*/ },
847/* GAPMCODE32_SEL 8 APM BIOS 32-bit interface (32bit Code) */
848{	0,			/* segment base address (overwritten by APM)  */
849	0xfffff,		/* length */
850	SDT_MEMERA,		/* 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/* GAPMCODE16_SEL 9 APM BIOS 32-bit interface (16bit Code) */
857{	0,			/* segment base address (overwritten by APM)  */
858	0xfffff,		/* length */
859	SDT_MEMERA,		/* segment type */
860	0,			/* segment descriptor priority level */
861	1,			/* segment descriptor present */
862	0, 0,
863	0,			/* default 32 vs 16 bit size */
864	1  			/* limit granularity (byte/page units)*/ },
865/* GAPMDATA_SEL	10 APM BIOS 32-bit interface (Data) */
866{	0,			/* segment base address (overwritten by APM) */
867	0xfffff,		/* length */
868	SDT_MEMRWA,		/* segment type */
869	0,			/* segment descriptor priority level */
870	1,			/* segment descriptor present */
871	0, 0,
872	1,			/* default 32 vs 16 bit size */
873	1  			/* limit granularity (byte/page units)*/ },
874};
875
876static struct soft_segment_descriptor ldt_segs[] = {
877	/* Null Descriptor - overwritten by call gate */
878{	0x0,			/* segment base address  */
879	0x0,			/* length - all address space */
880	0,			/* segment type */
881	0,			/* segment descriptor priority level */
882	0,			/* segment descriptor present */
883	0, 0,
884	0,			/* default 32 vs 16 bit size */
885	0  			/* limit granularity (byte/page units)*/ },
886	/* Null Descriptor - overwritten by call gate */
887{	0x0,			/* segment base address  */
888	0x0,			/* length - all address space */
889	0,			/* segment type */
890	0,			/* segment descriptor priority level */
891	0,			/* segment descriptor present */
892	0, 0,
893	0,			/* default 32 vs 16 bit size */
894	0  			/* limit granularity (byte/page units)*/ },
895	/* Null Descriptor - overwritten by call gate */
896{	0x0,			/* segment base address  */
897	0x0,			/* length - all address space */
898	0,			/* segment type */
899	0,			/* segment descriptor priority level */
900	0,			/* segment descriptor present */
901	0, 0,
902	0,			/* default 32 vs 16 bit size */
903	0  			/* limit granularity (byte/page units)*/ },
904	/* Code Descriptor for user */
905{	0x0,			/* segment base address  */
906	0xfffff,		/* length - all address space */
907	SDT_MEMERA,		/* segment type */
908	SEL_UPL,		/* segment descriptor priority level */
909	1,			/* segment descriptor present */
910	0, 0,
911	1,			/* default 32 vs 16 bit size */
912	1  			/* limit granularity (byte/page units)*/ },
913	/* Data Descriptor for user */
914{	0x0,			/* segment base address  */
915	0xfffff,		/* length - all address space */
916	SDT_MEMRWA,		/* segment type */
917	SEL_UPL,		/* segment descriptor priority level */
918	1,			/* segment descriptor present */
919	0, 0,
920	1,			/* default 32 vs 16 bit size */
921	1  			/* limit granularity (byte/page units)*/ },
922};
923
924void
925setidt(idx, func, typ, dpl, selec)
926	int idx;
927	inthand_t *func;
928	int typ;
929	int dpl;
930	int selec;
931{
932	struct gate_descriptor *ip = idt + idx;
933
934	ip->gd_looffset = (int)func;
935	ip->gd_selector = selec;
936	ip->gd_stkcpy = 0;
937	ip->gd_xx = 0;
938	ip->gd_type = typ;
939	ip->gd_dpl = dpl;
940	ip->gd_p = 1;
941	ip->gd_hioffset = ((int)func)>>16 ;
942}
943
944#define	IDTVEC(name)	__CONCAT(X,name)
945
946extern inthand_t
947	IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
948	IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
949	IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
950	IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
951	IDTVEC(syscall), IDTVEC(int0x80_syscall);
952
953void
954sdtossd(sd, ssd)
955	struct segment_descriptor *sd;
956	struct soft_segment_descriptor *ssd;
957{
958	ssd->ssd_base  = (sd->sd_hibase << 24) | sd->sd_lobase;
959	ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
960	ssd->ssd_type  = sd->sd_type;
961	ssd->ssd_dpl   = sd->sd_dpl;
962	ssd->ssd_p     = sd->sd_p;
963	ssd->ssd_def32 = sd->sd_def32;
964	ssd->ssd_gran  = sd->sd_gran;
965}
966
967void
968init386(first)
969	int first;
970{
971	int x;
972	unsigned biosbasemem, biosextmem;
973	struct gate_descriptor *gdp;
974	int gsel_tss;
975	struct isa_device *idp;
976	/* table descriptors - used to load tables by microp */
977	struct region_descriptor r_gdt, r_idt;
978	int	pagesinbase, pagesinext;
979	int	target_page, pa_indx;
980	int	off;
981
982	proc0.p_addr = proc0paddr;
983
984	atdevbase = ISA_HOLE_START + KERNBASE;
985
986	/*
987	 * Initialize the console before we print anything out.
988	 */
989	cninit();
990
991	/*
992	 * make gdt memory segments, the code segment goes up to end of the
993	 * page with etext in it, the data segment goes to the end of
994	 * the address space
995	 */
996	/*
997	 * XXX text protection is temporarily (?) disabled.  The limit was
998	 * i386_btop(round_page(etext)) - 1.
999	 */
1000	gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1;
1001	gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1;
1002	for (x = 0; x < NGDT; x++)
1003		ssdtosd(&gdt_segs[x], &gdt[x].sd);
1004
1005	/* make ldt memory segments */
1006	/*
1007	 * The data segment limit must not cover the user area because we
1008	 * don't want the user area to be writable in copyout() etc. (page
1009	 * level protection is lost in kernel mode on 386's).  Also, we
1010	 * don't want the user area to be writable directly (page level
1011	 * protection of the user area is not available on 486's with
1012	 * CR0_WP set, because there is no user-read/kernel-write mode).
1013	 *
1014	 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max.  And it
1015	 * should be spelled ...MAX_USER...
1016	 */
1017#define VM_END_USER_RW_ADDRESS	VM_MAXUSER_ADDRESS
1018	/*
1019	 * The code segment limit has to cover the user area until we move
1020	 * the signal trampoline out of the user area.  This is safe because
1021	 * the code segment cannot be written to directly.
1022	 */
1023#define VM_END_USER_R_ADDRESS	(VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE)
1024	ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1;
1025	ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1;
1026	/* Note. eventually want private ldts per process */
1027	for (x = 0; x < NLDT; x++)
1028		ssdtosd(&ldt_segs[x], &ldt[x].sd);
1029
1030	/* exceptions */
1031	for (x = 0; x < NIDT; x++)
1032		setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1033	setidt(0, &IDTVEC(div),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1034	setidt(1, &IDTVEC(dbg),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1035	setidt(2, &IDTVEC(nmi),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1036 	setidt(3, &IDTVEC(bpt),  SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1037	setidt(4, &IDTVEC(ofl),  SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1038	setidt(5, &IDTVEC(bnd),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1039	setidt(6, &IDTVEC(ill),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1040	setidt(7, &IDTVEC(dna),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1041	setidt(8, 0,  SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1042	setidt(9, &IDTVEC(fpusegm),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1043	setidt(10, &IDTVEC(tss),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1044	setidt(11, &IDTVEC(missing),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1045	setidt(12, &IDTVEC(stk),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1046	setidt(13, &IDTVEC(prot),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1047	setidt(14, &IDTVEC(page),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1048	setidt(15, &IDTVEC(rsvd),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1049	setidt(16, &IDTVEC(fpu),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1050	setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1051	setidt(18, &IDTVEC(mchk),  SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1052 	setidt(0x80, &IDTVEC(int0x80_syscall),
1053			SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1054
1055#include	"isa.h"
1056#if	NISA >0
1057	isa_defaultirq();
1058#endif
1059	rand_initialize();
1060
1061	r_gdt.rd_limit = sizeof(gdt) - 1;
1062	r_gdt.rd_base =  (int) gdt;
1063	lgdt(&r_gdt);
1064
1065	r_idt.rd_limit = sizeof(idt) - 1;
1066	r_idt.rd_base = (int) idt;
1067	lidt(&r_idt);
1068
1069	_default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1070	lldt(_default_ldt);
1071	currentldt = _default_ldt;
1072
1073#ifdef DDB
1074	kdb_init();
1075	if (boothowto & RB_KDB)
1076		Debugger("Boot flags requested debugger");
1077#endif
1078
1079	/* Use BIOS values stored in RTC CMOS RAM, since probing
1080	 * breaks certain 386 AT relics.
1081	 */
1082	biosbasemem = rtcin(RTC_BASELO)+ (rtcin(RTC_BASEHI)<<8);
1083	biosextmem = rtcin(RTC_EXTLO)+ (rtcin(RTC_EXTHI)<<8);
1084
1085	/*
1086	 * If BIOS tells us that it has more than 640k in the basemem,
1087	 *	don't believe it - set it to 640k.
1088	 */
1089	if (biosbasemem > 640) {
1090		printf("Preposterous RTC basemem of %dK, truncating to 640K\n",
1091		       biosbasemem);
1092		biosbasemem = 640;
1093	}
1094	if (bootinfo.bi_memsizes_valid && bootinfo.bi_basemem > 640) {
1095		printf("Preposterous BIOS basemem of %dK, truncating to 640K\n",
1096		       bootinfo.bi_basemem);
1097		bootinfo.bi_basemem = 640;
1098	}
1099
1100	/*
1101	 * Warn if the official BIOS interface disagrees with the RTC
1102	 * interface used above about the amount of base memory or the
1103	 * amount of extended memory.  Prefer the BIOS value for the base
1104	 * memory.  This is necessary for machines that `steal' base
1105	 * memory for use as BIOS memory, at least if we are going to use
1106	 * the BIOS for apm.  Prefer the RTC value for extended memory.
1107	 * Eventually the hackish interface shouldn't even be looked at.
1108	 */
1109	if (bootinfo.bi_memsizes_valid) {
1110		if (bootinfo.bi_basemem != biosbasemem) {
1111			vm_offset_t pa;
1112
1113			printf(
1114	"BIOS basemem (%ldK) != RTC basemem (%dK), setting to BIOS value\n",
1115			       bootinfo.bi_basemem, biosbasemem);
1116			biosbasemem = bootinfo.bi_basemem;
1117
1118			/*
1119			 * XXX if biosbasemem is now < 640, there is `hole'
1120			 * between the end of base memory and the start of
1121			 * ISA memory.  The hole may be empty or it may
1122			 * contain BIOS code or data.  Map it read/write so
1123			 * that the BIOS can write to it.  (Memory from 0 to
1124			 * the physical end of the kernel is mapped read-only
1125			 * to begin with and then parts of it are remapped.
1126			 * The parts that aren't remapped form holes that
1127			 * remain read-only and are unused by the kernel.
1128			 * The base memory area is below the physical end of
1129			 * the kernel and right now forms a read-only hole.
1130			 * The part of it from 0 to
1131			 * (trunc_page(biosbasemem * 1024) - 1) will be
1132			 * remapped and used by the kernel later.)
1133			 *
1134			 * This code is similar to the code used in
1135			 * pmap_mapdev, but since no memory needs to be
1136			 * allocated we simply change the mapping.
1137			 */
1138			for (pa = trunc_page(biosbasemem * 1024);
1139			     pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1140				unsigned *pte;
1141
1142				pte = (unsigned *)vtopte(pa + KERNBASE);
1143				*pte = pa | PG_RW | PG_V;
1144			}
1145		}
1146		if (bootinfo.bi_extmem != biosextmem)
1147			printf("BIOS extmem (%ldK) != RTC extmem (%dK)\n",
1148			       bootinfo.bi_extmem, biosextmem);
1149	}
1150
1151	pagesinbase = biosbasemem * 1024 / PAGE_SIZE;
1152	pagesinext = biosextmem * 1024 / PAGE_SIZE;
1153
1154	/*
1155	 * Special hack for chipsets that still remap the 384k hole when
1156	 *	there's 16MB of memory - this really confuses people that
1157	 *	are trying to use bus mastering ISA controllers with the
1158	 *	"16MB limit"; they only have 16MB, but the remapping puts
1159	 *	them beyond the limit.
1160	 */
1161	/*
1162	 * If extended memory is between 15-16MB (16-17MB phys address range),
1163	 *	chop it to 15MB.
1164	 */
1165	if ((pagesinext > 3840) && (pagesinext < 4096))
1166		pagesinext = 3840;
1167
1168	/*
1169	 * Maxmem isn't the "maximum memory", it's one larger than the
1170	 * highest page of the physical address space.  It should be
1171	 * called something like "Maxphyspage".
1172	 */
1173	Maxmem = pagesinext + 0x100000/PAGE_SIZE;
1174
1175#ifdef MAXMEM
1176	Maxmem = MAXMEM/4;
1177#endif
1178
1179	idp = find_isadev(isa_devtab_null, &npxdriver, 0);
1180	if (idp != NULL && idp->id_msize != 0)
1181		Maxmem = idp->id_msize / 4;
1182
1183	/* call pmap initialization to make new kernel address space */
1184	pmap_bootstrap (first, 0);
1185
1186	/*
1187	 * Size up each available chunk of physical memory.
1188	 */
1189
1190	/*
1191	 * We currently don't bother testing base memory.
1192	 * XXX  ...but we probably should.
1193	 */
1194	pa_indx = 0;
1195	badpages = 0;
1196	if (pagesinbase > 1) {
1197		phys_avail[pa_indx++] = PAGE_SIZE;	/* skip first page of memory */
1198		phys_avail[pa_indx] = ptoa(pagesinbase);/* memory up to the ISA hole */
1199		physmem = pagesinbase - 1;
1200	} else {
1201		/* point at first chunk end */
1202		pa_indx++;
1203	}
1204
1205	for (target_page = avail_start; target_page < ptoa(Maxmem); target_page += PAGE_SIZE) {
1206		int tmp, page_bad = FALSE;
1207
1208		/*
1209		 * map page into kernel: valid, read/write, non-cacheable
1210		 */
1211		*(int *)CMAP1 = PG_V | PG_RW | PG_N | target_page;
1212		invltlb();
1213
1214		tmp = *(int *)CADDR1;
1215		/*
1216		 * Test for alternating 1's and 0's
1217		 */
1218		*(volatile int *)CADDR1 = 0xaaaaaaaa;
1219		if (*(volatile int *)CADDR1 != 0xaaaaaaaa) {
1220			page_bad = TRUE;
1221		}
1222		/*
1223		 * Test for alternating 0's and 1's
1224		 */
1225		*(volatile int *)CADDR1 = 0x55555555;
1226		if (*(volatile int *)CADDR1 != 0x55555555) {
1227			page_bad = TRUE;
1228		}
1229		/*
1230		 * Test for all 1's
1231		 */
1232		*(volatile int *)CADDR1 = 0xffffffff;
1233		if (*(volatile int *)CADDR1 != 0xffffffff) {
1234			page_bad = TRUE;
1235		}
1236		/*
1237		 * Test for all 0's
1238		 */
1239		*(volatile int *)CADDR1 = 0x0;
1240		if (*(volatile int *)CADDR1 != 0x0) {
1241			/*
1242			 * test of page failed
1243			 */
1244			page_bad = TRUE;
1245		}
1246		/*
1247		 * Restore original value.
1248		 */
1249		*(int *)CADDR1 = tmp;
1250
1251		/*
1252		 * Adjust array of valid/good pages.
1253		 */
1254		if (page_bad == FALSE) {
1255			/*
1256			 * If this good page is a continuation of the
1257			 * previous set of good pages, then just increase
1258			 * the end pointer. Otherwise start a new chunk.
1259			 * Note that "end" points one higher than end,
1260			 * making the range >= start and < end.
1261			 */
1262			if (phys_avail[pa_indx] == target_page) {
1263				phys_avail[pa_indx] += PAGE_SIZE;
1264			} else {
1265				pa_indx++;
1266				if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1267					printf("Too many holes in the physical address space, giving up\n");
1268					pa_indx--;
1269					break;
1270				}
1271				phys_avail[pa_indx++] = target_page;	/* start */
1272				phys_avail[pa_indx] = target_page + PAGE_SIZE;	/* end */
1273			}
1274			physmem++;
1275		} else {
1276			badpages++;
1277			page_bad = FALSE;
1278		}
1279	}
1280
1281	*(int *)CMAP1 = 0;
1282	invltlb();
1283
1284	/*
1285	 * XXX
1286	 * The last chunk must contain at least one page plus the message
1287	 * buffer to avoid complicating other code (message buffer address
1288	 * calculation, etc.).
1289	 */
1290	while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1291	    round_page(sizeof(struct msgbuf)) >= phys_avail[pa_indx]) {
1292		physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1293		phys_avail[pa_indx--] = 0;
1294		phys_avail[pa_indx--] = 0;
1295	}
1296
1297	Maxmem = atop(phys_avail[pa_indx]);
1298
1299	/* Trim off space for the message buffer. */
1300	phys_avail[pa_indx] -= round_page(sizeof(struct msgbuf));
1301
1302	avail_end = phys_avail[pa_indx];
1303
1304	/* now running on new page tables, configured,and u/iom is accessible */
1305
1306	/* Map the message buffer. */
1307	for (off = 0; off < round_page(sizeof(struct msgbuf)); off += PAGE_SIZE)
1308		pmap_enter(kernel_pmap, (vm_offset_t)msgbufp + off,
1309			   avail_end + off, VM_PROT_ALL, TRUE);
1310	msgbufmapped = 1;
1311
1312	/* make a initial tss so microp can get interrupt stack on syscall! */
1313	proc0.p_addr->u_pcb.pcb_tss.tss_esp0 = (int) kstack + UPAGES*PAGE_SIZE;
1314	proc0.p_addr->u_pcb.pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1315	gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1316
1317	dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1318	    dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1319	dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1320	    dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1321	dblfault_tss.tss_cr3 = IdlePTD;
1322	dblfault_tss.tss_eip = (int) dblfault_handler;
1323	dblfault_tss.tss_eflags = PSL_KERNEL;
1324	dblfault_tss.tss_ds = dblfault_tss.tss_es = dblfault_tss.tss_fs = dblfault_tss.tss_gs =
1325		GSEL(GDATA_SEL, SEL_KPL);
1326	dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1327	dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1328
1329	((struct i386tss *)gdt_segs[GPROC0_SEL].ssd_base)->tss_ioopt =
1330		(sizeof(struct i386tss))<<16;
1331
1332	ltr(gsel_tss);
1333
1334	/* make a call gate to reenter kernel with */
1335	gdp = &ldt[LSYS5CALLS_SEL].gd;
1336
1337	x = (int) &IDTVEC(syscall);
1338	gdp->gd_looffset = x++;
1339	gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1340	gdp->gd_stkcpy = 1;
1341	gdp->gd_type = SDT_SYS386CGT;
1342	gdp->gd_dpl = SEL_UPL;
1343	gdp->gd_p = 1;
1344	gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
1345
1346	/* XXX does this work? */
1347	ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1348
1349	/* transfer to user mode */
1350
1351	_ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
1352	_udatasel = LSEL(LUDATA_SEL, SEL_UPL);
1353
1354	/* setup proc 0's pcb */
1355	proc0.p_addr->u_pcb.pcb_flags = 0;
1356	proc0.p_addr->u_pcb.pcb_cr3 = IdlePTD;
1357}
1358
1359/*
1360 * The registers are in the frame; the frame is in the user area of
1361 * the process in question; when the process is active, the registers
1362 * are in "the kernel stack"; when it's not, they're still there, but
1363 * things get flipped around.  So, since p->p_md.md_regs is the whole address
1364 * of the register set, take its offset from the kernel stack, and
1365 * index into the user block.  Don't you just *love* virtual memory?
1366 * (I'm starting to think seymour is right...)
1367 */
1368#define	TF_REGP(p)	((struct trapframe *) \
1369			 ((char *)(p)->p_addr \
1370			  + ((char *)(p)->p_md.md_regs - kstack)))
1371
1372int
1373ptrace_set_pc(p, addr)
1374	struct proc *p;
1375	unsigned int addr;
1376{
1377	TF_REGP(p)->tf_eip = addr;
1378	return (0);
1379}
1380
1381int
1382ptrace_single_step(p)
1383	struct proc *p;
1384{
1385	TF_REGP(p)->tf_eflags |= PSL_T;
1386	return (0);
1387}
1388
1389int ptrace_write_u(p, off, data)
1390	struct proc *p;
1391	vm_offset_t off;
1392	int data;
1393{
1394	struct trapframe frame_copy;
1395	vm_offset_t min;
1396	struct trapframe *tp;
1397
1398	/*
1399	 * Privileged kernel state is scattered all over the user area.
1400	 * Only allow write access to parts of regs and to fpregs.
1401	 */
1402	min = (char *)p->p_md.md_regs - kstack;
1403	if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) {
1404		tp = TF_REGP(p);
1405		frame_copy = *tp;
1406		*(int *)((char *)&frame_copy + (off - min)) = data;
1407		if (!EFLAGS_SECURE(frame_copy.tf_eflags, tp->tf_eflags) ||
1408		    !CS_SECURE(frame_copy.tf_cs))
1409			return (EINVAL);
1410		*(int*)((char *)p->p_addr + off) = data;
1411		return (0);
1412	}
1413	min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu);
1414	if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) {
1415		*(int*)((char *)p->p_addr + off) = data;
1416		return (0);
1417	}
1418	return (EFAULT);
1419}
1420
1421int
1422fill_regs(p, regs)
1423	struct proc *p;
1424	struct reg *regs;
1425{
1426	struct trapframe *tp;
1427
1428	tp = TF_REGP(p);
1429	regs->r_es = tp->tf_es;
1430	regs->r_ds = tp->tf_ds;
1431	regs->r_edi = tp->tf_edi;
1432	regs->r_esi = tp->tf_esi;
1433	regs->r_ebp = tp->tf_ebp;
1434	regs->r_ebx = tp->tf_ebx;
1435	regs->r_edx = tp->tf_edx;
1436	regs->r_ecx = tp->tf_ecx;
1437	regs->r_eax = tp->tf_eax;
1438	regs->r_eip = tp->tf_eip;
1439	regs->r_cs = tp->tf_cs;
1440	regs->r_eflags = tp->tf_eflags;
1441	regs->r_esp = tp->tf_esp;
1442	regs->r_ss = tp->tf_ss;
1443	return (0);
1444}
1445
1446int
1447set_regs(p, regs)
1448	struct proc *p;
1449	struct reg *regs;
1450{
1451	struct trapframe *tp;
1452
1453	tp = TF_REGP(p);
1454	if (!EFLAGS_SECURE(regs->r_eflags, tp->tf_eflags) ||
1455	    !CS_SECURE(regs->r_cs))
1456		return (EINVAL);
1457	tp->tf_es = regs->r_es;
1458	tp->tf_ds = regs->r_ds;
1459	tp->tf_edi = regs->r_edi;
1460	tp->tf_esi = regs->r_esi;
1461	tp->tf_ebp = regs->r_ebp;
1462	tp->tf_ebx = regs->r_ebx;
1463	tp->tf_edx = regs->r_edx;
1464	tp->tf_ecx = regs->r_ecx;
1465	tp->tf_eax = regs->r_eax;
1466	tp->tf_eip = regs->r_eip;
1467	tp->tf_cs = regs->r_cs;
1468	tp->tf_eflags = regs->r_eflags;
1469	tp->tf_esp = regs->r_esp;
1470	tp->tf_ss = regs->r_ss;
1471	return (0);
1472}
1473
1474#ifndef DDB
1475void
1476Debugger(const char *msg)
1477{
1478	printf("Debugger(\"%s\") called.\n", msg);
1479}
1480#endif /* no DDB */
1481
1482#include <sys/disklabel.h>
1483#define b_cylin	b_resid
1484/*
1485 * Determine the size of the transfer, and make sure it is
1486 * within the boundaries of the partition. Adjust transfer
1487 * if needed, and signal errors or early completion.
1488 */
1489int
1490bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel)
1491{
1492        struct partition *p = lp->d_partitions + dkpart(bp->b_dev);
1493        int labelsect = lp->d_partitions[0].p_offset;
1494        int maxsz = p->p_size,
1495                sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1496
1497        /* overwriting disk label ? */
1498        /* XXX should also protect bootstrap in first 8K */
1499        if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect &&
1500#if LABELSECTOR != 0
1501            bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
1502#endif
1503            (bp->b_flags & B_READ) == 0 && wlabel == 0) {
1504                bp->b_error = EROFS;
1505                goto bad;
1506        }
1507
1508#if     defined(DOSBBSECTOR) && defined(notyet)
1509        /* overwriting master boot record? */
1510        if (bp->b_blkno + p->p_offset <= DOSBBSECTOR &&
1511            (bp->b_flags & B_READ) == 0 && wlabel == 0) {
1512                bp->b_error = EROFS;
1513                goto bad;
1514        }
1515#endif
1516
1517        /* beyond partition? */
1518        if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
1519                /* if exactly at end of disk, return an EOF */
1520                if (bp->b_blkno == maxsz) {
1521                        bp->b_resid = bp->b_bcount;
1522                        return(0);
1523                }
1524                /* or truncate if part of it fits */
1525                sz = maxsz - bp->b_blkno;
1526                if (sz <= 0) {
1527                        bp->b_error = EINVAL;
1528                        goto bad;
1529                }
1530                bp->b_bcount = sz << DEV_BSHIFT;
1531        }
1532
1533        /* calculate cylinder for disksort to order transfers with */
1534        bp->b_pblkno = bp->b_blkno + p->p_offset;
1535        bp->b_cylin = bp->b_pblkno / lp->d_secpercyl;
1536        return(1);
1537
1538bad:
1539        bp->b_flags |= B_ERROR;
1540        return(-1);
1541}
1542