vm_machdep.c revision 71785
1195630Ssam/*-
2195630Ssam * Copyright (c) 1982, 1986 The Regents of the University of California.
3195630Ssam * Copyright (c) 1989, 1990 William Jolitz
4195630Ssam * Copyright (c) 1994 John Dyson
5195630Ssam * All rights reserved.
6195630Ssam *
7195630Ssam * This code is derived from software contributed to Berkeley by
8195630Ssam * the Systems Programming Group of the University of Utah Computer
9195630Ssam * Science Department, and William Jolitz.
10195630Ssam *
11195630Ssam * Redistribution and use in source and binary forms, with or without
12195630Ssam * modification, are permitted provided that the following conditions
13195630Ssam * are met:
14195630Ssam * 1. Redistributions of source code must retain the above copyright
15195630Ssam *    notice, this list of conditions and the following disclaimer.
16195630Ssam * 2. Redistributions in binary form must reproduce the above copyright
17195630Ssam *    notice, this list of conditions and the following disclaimer in the
18195630Ssam *    documentation and/or other materials provided with the distribution.
19195630Ssam * 3. All advertising materials mentioning features or use of this software
20195630Ssam *    must display the following acknowledgement:
21195630Ssam *	This product includes software developed by the University of
22195630Ssam *	California, Berkeley and its contributors.
23195630Ssam * 4. Neither the name of the University nor the names of its contributors
24195630Ssam *    may be used to endorse or promote products derived from this software
25195630Ssam *    without specific prior written permission.
26195630Ssam *
27195630Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28195630Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29195630Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30195630Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31195630Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32195630Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33195630Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34195630Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35195630Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36195630Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37195630Ssam * SUCH DAMAGE.
38195630Ssam *
39195630Ssam *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40195630Ssam *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41195630Ssam * $FreeBSD: head/sys/amd64/amd64/vm_machdep.c 71785 2001-01-29 09:38:39Z peter $
42195630Ssam */
43195630Ssam
44195630Ssam#include "opt_npx.h"
45195630Ssam#include "opt_user_ldt.h"
46195630Ssam#ifdef PC98
47195630Ssam#include "opt_pc98.h"
48195630Ssam#endif
49195630Ssam#include "opt_reset.h"
50195630Ssam#include "opt_isa.h"
51195630Ssam
52195630Ssam#include <sys/param.h>
53195630Ssam#include <sys/systm.h>
54195630Ssam#include <sys/malloc.h>
55195630Ssam#include <sys/proc.h>
56195630Ssam#include <sys/bio.h>
57195630Ssam#include <sys/buf.h>
58195630Ssam#include <sys/vnode.h>
59195630Ssam#include <sys/vmmeter.h>
60195630Ssam#include <sys/kernel.h>
61195630Ssam#include <sys/ktr.h>
62195630Ssam#include <sys/mutex.h>
63195630Ssam#include <sys/sysctl.h>
64195630Ssam#include <sys/unistd.h>
65195630Ssam
66195630Ssam#include <machine/cpu.h>
67195630Ssam#include <machine/md_var.h>
68195630Ssam#ifdef SMP
69195630Ssam#include <machine/smp.h>
70195630Ssam#endif
71195630Ssam#include <machine/pcb.h>
72195630Ssam#include <machine/pcb_ext.h>
73195630Ssam#include <machine/vm86.h>
74195630Ssam
75195630Ssam#include <vm/vm.h>
76195630Ssam#include <vm/vm_param.h>
77195630Ssam#include <sys/lock.h>
78195630Ssam#include <vm/vm_kern.h>
79195630Ssam#include <vm/vm_page.h>
80195630Ssam#include <vm/vm_map.h>
81195630Ssam#include <vm/vm_extern.h>
82195630Ssam
83195630Ssam#include <sys/user.h>
84195630Ssam
85195630Ssam#ifdef PC98
86195630Ssam#include <pc98/pc98/pc98.h>
87195630Ssam#else
88195630Ssam#include <i386/isa/isa.h>
89195630Ssam#endif
90195630Ssam
91195630Ssamstatic void	cpu_reset_real __P((void));
92195630Ssam#ifdef SMP
93195630Ssamstatic void	cpu_reset_proxy __P((void));
94195630Ssamstatic u_int	cpu_reset_proxyid;
95195630Ssamstatic volatile u_int	cpu_reset_proxy_active;
96195630Ssam#endif
97195630Ssamextern int	_ucodesel, _udatasel;
98195630Ssam
99195630Ssam/*
100195630Ssam * quick version of vm_fault
101195630Ssam */
102195630Ssamint
103195630Ssamvm_fault_quick(v, prot)
104195630Ssam	caddr_t v;
105195630Ssam	int prot;
106195630Ssam{
107195630Ssam	int r;
108195630Ssam
109195630Ssam	if (prot & VM_PROT_WRITE)
110195630Ssam		r = subyte(v, fubyte(v));
111195630Ssam	else
112195630Ssam		r = fubyte(v);
113195630Ssam	return(r);
114195630Ssam}
115195630Ssam
116195630Ssam/*
117195630Ssam * Finish a fork operation, with process p2 nearly set up.
118195630Ssam * Copy and update the pcb, set up the stack so that the child
119195630Ssam * ready to run and return to user mode.
120195630Ssam */
121195630Ssamvoid
122195630Ssamcpu_fork(p1, p2, flags)
123195630Ssam	register struct proc *p1, *p2;
124195630Ssam	int flags;
125195630Ssam{
126195630Ssam	struct pcb *pcb2;
127195630Ssam
128195630Ssam	if ((flags & RFPROC) == 0) {
129195630Ssam#ifdef USER_LDT
130195630Ssam		if ((flags & RFMEM) == 0) {
131195630Ssam			/* unshare user LDT */
132195630Ssam			struct pcb *pcb1 = &p1->p_addr->u_pcb;
133195630Ssam			struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt;
134195630Ssam			if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) {
135195630Ssam				pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len);
136195630Ssam				user_ldt_free(pcb1);
137195630Ssam				pcb1->pcb_ldt = pcb_ldt;
138195630Ssam				set_user_ldt(pcb1);
139195630Ssam			}
140195630Ssam		}
141195630Ssam#endif
142195630Ssam		return;
143195630Ssam	}
144195630Ssam
145195630Ssam#ifdef DEV_NPX
146195630Ssam	/* Ensure that p1's pcb is up to date. */
147195630Ssam	if (PCPU_GET(npxproc) == p1)
148195630Ssam		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
149195630Ssam#endif
150195630Ssam
151195630Ssam	/* Copy p1's pcb. */
152195630Ssam	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
153195630Ssam	pcb2 = &p2->p_addr->u_pcb;
154195630Ssam
155195630Ssam	/*
156195630Ssam	 * Create a new fresh stack for the new process.
157195630Ssam	 * Copy the trap frame for the return to user mode as if from a
158195630Ssam	 * syscall.  This copies the user mode register values.
159195630Ssam	 */
160195630Ssam	p2->p_md.md_regs = (struct trapframe *)
161195630Ssam			   ((int)p2->p_addr + UPAGES * PAGE_SIZE - 16) - 1;
162195630Ssam	bcopy(p1->p_md.md_regs, p2->p_md.md_regs, sizeof(*p2->p_md.md_regs));
163195630Ssam
164195630Ssam	p2->p_md.md_regs->tf_eax = 0;		/* Child returns zero */
165195630Ssam	p2->p_md.md_regs->tf_eflags &= ~PSL_C;	/* success */
166195630Ssam	p2->p_md.md_regs->tf_edx = 1;
167195630Ssam
168195630Ssam	/*
169195630Ssam	 * Set registers for trampoline to user mode.  Leave space for the
170195630Ssam	 * return address on stack.  These are the kernel mode register values.
171195630Ssam	 */
172195630Ssam	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
173195630Ssam	pcb2->pcb_edi = 0;
174195630Ssam	pcb2->pcb_esi = (int)fork_return;	/* fork_trampoline argument */
175195630Ssam	pcb2->pcb_ebp = 0;
176195630Ssam	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
177195630Ssam	pcb2->pcb_ebx = (int)p2;		/* fork_trampoline argument */
178195630Ssam	pcb2->pcb_eip = (int)fork_trampoline;
179195630Ssam	/*
180195630Ssam	 * pcb2->pcb_ldt:	duplicated below, if necessary.
181195630Ssam	 * pcb2->pcb_savefpu:	cloned above.
182195630Ssam	 * pcb2->pcb_flags:	cloned above (always 0 here?).
183195630Ssam	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
184195630Ssam	 */
185195630Ssam
186195630Ssam	pcb2->pcb_schednest = 0;
187195630Ssam
188195630Ssam	/*
189195630Ssam	 * XXX don't copy the i/o pages.  this should probably be fixed.
190195630Ssam	 */
191195630Ssam	pcb2->pcb_ext = 0;
192195630Ssam
193195630Ssam#ifdef USER_LDT
194195630Ssam        /* Copy the LDT, if necessary. */
195195630Ssam        if (pcb2->pcb_ldt != 0) {
196195630Ssam		if (flags & RFMEM) {
197195630Ssam			pcb2->pcb_ldt->ldt_refcnt++;
198195630Ssam		} else {
199195630Ssam			pcb2->pcb_ldt = user_ldt_alloc(pcb2,
200195630Ssam				pcb2->pcb_ldt->ldt_len);
201195630Ssam		}
202195630Ssam        }
203195630Ssam#endif
204195630Ssam
205195630Ssam	/*
206195630Ssam	 * Now, cpu_switch() can schedule the new process.
207195630Ssam	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
208195630Ssam	 * containing the return address when exiting cpu_switch.
209195630Ssam	 * This will normally be to fork_trampoline(), which will have
210195630Ssam	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
211195630Ssam	 * will set up a stack to call fork_return(p, frame); to complete
212195630Ssam	 * the return to user-mode.
213195630Ssam	 */
214195630Ssam}
215195630Ssam
216195630Ssam/*
217195630Ssam * Intercept the return address from a freshly forked process that has NOT
218195630Ssam * been scheduled yet.
219195630Ssam *
220195630Ssam * This is needed to make kernel threads stay in kernel mode.
221195630Ssam */
222195630Ssamvoid
223195630Ssamcpu_set_fork_handler(p, func, arg)
224195630Ssam	struct proc *p;
225195630Ssam	void (*func) __P((void *));
226195630Ssam	void *arg;
227195630Ssam{
228195630Ssam	/*
229195630Ssam	 * Note that the trap frame follows the args, so the function
230195630Ssam	 * is really called like this:  func(arg, frame);
231195630Ssam	 */
232195630Ssam	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
233211397Sjoel	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
234195630Ssam}
235195630Ssam
236195630Ssamvoid
237195630Ssamcpu_exit(p)
238195630Ssam	register struct proc *p;
239195630Ssam{
240195630Ssam	struct pcb *pcb = &p->p_addr->u_pcb;
241195630Ssam
242195630Ssam#ifdef DEV_NPX
243195630Ssam	npxexit(p);
244195630Ssam#endif
245195630Ssam	if (pcb->pcb_ext != 0) {
246195630Ssam	        /*
247195630Ssam		 * XXX do we need to move the TSS off the allocated pages
248195630Ssam		 * before freeing them?  (not done here)
249195630Ssam		 */
250195630Ssam		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
251195630Ssam		    ctob(IOPAGES + 1));
252195630Ssam		pcb->pcb_ext = 0;
253195630Ssam	}
254195630Ssam#ifdef USER_LDT
255195630Ssam	user_ldt_free(pcb);
256195630Ssam#endif
257195630Ssam        if (pcb->pcb_flags & PCB_DBREGS) {
258195630Ssam                /*
259195630Ssam                 * disable all hardware breakpoints
260195630Ssam                 */
261195630Ssam                reset_dbregs();
262195630Ssam                pcb->pcb_flags &= ~PCB_DBREGS;
263195630Ssam        }
264195630Ssam	mtx_enter(&sched_lock, MTX_SPIN);
265195630Ssam	mtx_exit(&Giant, MTX_DEF | MTX_NOSWITCH);
266195630Ssam	mtx_assert(&Giant, MA_NOTOWNED);
267195630Ssam
268195630Ssam	/*
269195630Ssam	 * We have to wait until after releasing all locks before
270195630Ssam	 * changing p_stat.  If we block on a mutex then we will be
271195630Ssam	 * back at SRUN when we resume and our parent will never
272195630Ssam	 * harvest us.
273195630Ssam	 */
274195630Ssam	p->p_stat = SZOMB;
275195630Ssam
276195630Ssam	mp_fixme("assumption: p_pptr won't change at this time");
277195630Ssam	wakeup(p->p_pptr);
278195630Ssam
279195630Ssam	cnt.v_swtch++;
280195630Ssam	cpu_throw();
281195630Ssam	panic("cpu_exit");
282195630Ssam}
283232786Sgavin
284195630Ssamvoid
285195630Ssamcpu_wait(p)
286210933Sjoel	struct proc *p;
287195630Ssam{
288195630Ssam	/* drop per-process resources */
289195630Ssam	pmap_dispose_proc(p);
290195630Ssam
291195630Ssam	/* and clean-out the vmspace */
292195630Ssam	vmspace_free(p->p_vmspace);
293195630Ssam}
294195630Ssam
295195630Ssam/*
296195630Ssam * Dump the machine specific header information at the start of a core dump.
297195630Ssam */
298195630Ssamint
299195630Ssamcpu_coredump(p, vp, cred)
300195630Ssam	struct proc *p;
301195630Ssam	struct vnode *vp;
302195630Ssam	struct ucred *cred;
303195630Ssam{
304195630Ssam	int error;
305195630Ssam	caddr_t tempuser;
306195630Ssam
307195630Ssam	tempuser = malloc(ctob(UPAGES), M_TEMP, M_WAITOK | M_ZERO);
308195630Ssam	if (!tempuser)
309195630Ssam		return EINVAL;
310195630Ssam
311210676Sjoel	bcopy(p->p_addr, tempuser, sizeof(struct user));
312195630Ssam	bcopy(p->p_md.md_regs,
313195630Ssam	      tempuser + ((caddr_t) p->p_md.md_regs - (caddr_t) p->p_addr),
314195630Ssam	      sizeof(struct trapframe));
315195630Ssam
316195630Ssam	error = vn_rdwr(UIO_WRITE, vp, (caddr_t) tempuser,
317195630Ssam			ctob(UPAGES),
318195630Ssam			(off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
319195630Ssam			cred, (int *)NULL, p);
320195630Ssam
321195630Ssam	free(tempuser, M_TEMP);
322195630Ssam
323195630Ssam	return error;
324195630Ssam}
325195630Ssam
326195630Ssam#ifdef notyet
327195630Ssamstatic void
328195630Ssamsetredzone(pte, vaddr)
329195630Ssam	u_short *pte;
330195630Ssam	caddr_t vaddr;
331195630Ssam{
332195630Ssam/* eventually do this by setting up an expand-down stack segment
333195630Ssam   for ss0: selector, allowing stack access down to top of u.
334195630Ssam   this means though that protection violations need to be handled
335195630Ssam   thru a double fault exception that must do an integral task
336195630Ssam   switch to a known good context, within which a dump can be
337195630Ssam   taken. a sensible scheme might be to save the initial context
338195630Ssam   used by sched (that has physical memory mapped 1:1 at bottom)
339195630Ssam   and take the dump while still in mapped mode */
340195630Ssam}
341195630Ssam#endif
342195630Ssam
343195630Ssam/*
344195630Ssam * Convert kernel VA to physical address
345195630Ssam */
346195630Ssamu_long
347195630Ssamkvtop(void *addr)
348195630Ssam{
349195630Ssam	vm_offset_t va;
350195630Ssam
351195630Ssam	va = pmap_kextract((vm_offset_t)addr);
352195630Ssam	if (va == 0)
353195630Ssam		panic("kvtop: zero page frame");
354195630Ssam	return((int)va);
355195630Ssam}
356195630Ssam
357195630Ssam/*
358195630Ssam * Map an IO request into kernel virtual address space.
359195630Ssam *
360195630Ssam * All requests are (re)mapped into kernel VA space.
361195630Ssam * Notice that we use b_bufsize for the size of the buffer
362195630Ssam * to be mapped.  b_bcount might be modified by the driver.
363195630Ssam */
364195630Ssamvoid
365195630Ssamvmapbuf(bp)
366195630Ssam	register struct buf *bp;
367195630Ssam{
368195630Ssam	register caddr_t addr, v, kva;
369235578Sgjb	vm_offset_t pa;
370195630Ssam
371195630Ssam	if ((bp->b_flags & B_PHYS) == 0)
372195630Ssam		panic("vmapbuf");
373195630Ssam
374195630Ssam	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
375195630Ssam	    addr < bp->b_data + bp->b_bufsize;
376195630Ssam	    addr += PAGE_SIZE, v += PAGE_SIZE) {
377195630Ssam		/*
378195630Ssam		 * Do the vm_fault if needed; do the copy-on-write thing
379195630Ssam		 * when reading stuff off device into memory.
380195630Ssam		 */
381195630Ssam		vm_fault_quick(addr,
382195630Ssam			(bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
383195630Ssam		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
384195630Ssam		if (pa == 0)
385195630Ssam			panic("vmapbuf: page not present");
386195630Ssam		vm_page_hold(PHYS_TO_VM_PAGE(pa));
387195630Ssam		pmap_kenter((vm_offset_t) v, pa);
388195630Ssam	}
389195630Ssam
390195630Ssam	kva = bp->b_saveaddr;
391195630Ssam	bp->b_saveaddr = bp->b_data;
392195630Ssam	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
393195630Ssam}
394195630Ssam
395195630Ssam/*
396195630Ssam * Free the io map PTEs associated with this IO operation.
397195630Ssam * We also invalidate the TLB entries and restore the original b_addr.
398195630Ssam */
399195630Ssamvoid
400210676Sjoelvunmapbuf(bp)
401195630Ssam	register struct buf *bp;
402195630Ssam{
403195630Ssam	register caddr_t addr;
404195630Ssam	vm_offset_t pa;
405195630Ssam
406195630Ssam	if ((bp->b_flags & B_PHYS) == 0)
407195630Ssam		panic("vunmapbuf");
408195630Ssam
409195630Ssam	for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
410195630Ssam	    addr < bp->b_data + bp->b_bufsize;
411195630Ssam	    addr += PAGE_SIZE) {
412195630Ssam		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
413195630Ssam		pmap_kremove((vm_offset_t) addr);
414195630Ssam		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
415195630Ssam	}
416195630Ssam
417195630Ssam	bp->b_data = bp->b_saveaddr;
418195630Ssam}
419195630Ssam
420195630Ssam/*
421195630Ssam * Force reset the processor by invalidating the entire address space!
422195630Ssam */
423195630Ssam
424195630Ssam#ifdef SMP
425195630Ssamstatic void
426195630Ssamcpu_reset_proxy()
427195630Ssam{
428195630Ssam
429195630Ssam	cpu_reset_proxy_active = 1;
430195630Ssam	while (cpu_reset_proxy_active == 1)
431195630Ssam		;	 /* Wait for other cpu to see that we've started */
432195630Ssam	stop_cpus((1<<cpu_reset_proxyid));
433195630Ssam	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
434195630Ssam	DELAY(1000000);
435195630Ssam	cpu_reset_real();
436195630Ssam}
437195630Ssam#endif
438195630Ssam
439195630Ssamvoid
440195630Ssamcpu_reset()
441195630Ssam{
442195630Ssam#ifdef SMP
443195630Ssam	if (smp_active == 0) {
444195630Ssam		cpu_reset_real();
445195630Ssam		/* NOTREACHED */
446195630Ssam	} else {
447195630Ssam
448195630Ssam		u_int map;
449195630Ssam		int cnt;
450235578Sgjb		printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
451195630Ssam
452195630Ssam		map = PCPU_GET(other_cpus) & ~ stopped_cpus;
453195630Ssam
454195630Ssam		if (map != 0) {
455195630Ssam			printf("cpu_reset: Stopping other CPUs\n");
456195630Ssam			stop_cpus(map);		/* Stop all other CPUs */
457195630Ssam		}
458195630Ssam
459195630Ssam		if (PCPU_GET(cpuid) == 0) {
460195630Ssam			DELAY(1000000);
461195630Ssam			cpu_reset_real();
462195630Ssam			/* NOTREACHED */
463195630Ssam		} else {
464195630Ssam			/* We are not BSP (CPU #0) */
465195630Ssam
466195630Ssam			cpu_reset_proxyid = PCPU_GET(cpuid);
467195630Ssam			cpustop_restartfunc = cpu_reset_proxy;
468195630Ssam			cpu_reset_proxy_active = 0;
469195630Ssam			printf("cpu_reset: Restarting BSP\n");
470195630Ssam			started_cpus = (1<<0);		/* Restart CPU #0 */
471195630Ssam
472195630Ssam			cnt = 0;
473195630Ssam			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
474195630Ssam				cnt++;	/* Wait for BSP to announce restart */
475195630Ssam			if (cpu_reset_proxy_active == 0)
476195630Ssam				printf("cpu_reset: Failed to restart BSP\n");
477195630Ssam			enable_intr();
478195630Ssam			cpu_reset_proxy_active = 2;
479195630Ssam
480195630Ssam			while (1);
481195630Ssam			/* NOTREACHED */
482195630Ssam		}
483195630Ssam	}
484195630Ssam#else
485195630Ssam	cpu_reset_real();
486195630Ssam#endif
487195630Ssam}
488211397Sjoel
489195630Ssamstatic void
490195630Ssamcpu_reset_real()
491195630Ssam{
492195630Ssam
493211397Sjoel#ifdef PC98
494195630Ssam	/*
495195630Ssam	 * Attempt to do a CPU reset via CPU reset port.
496195630Ssam	 */
497195630Ssam	disable_intr();
498195630Ssam	if ((inb(0x35) & 0xa0) != 0xa0) {
499195630Ssam		outb(0x37, 0x0f);		/* SHUT0 = 0. */
500195630Ssam		outb(0x37, 0x0b);		/* SHUT1 = 0. */
501195630Ssam	}
502195630Ssam	outb(0xf0, 0x00);		/* Reset. */
503195630Ssam#else
504195630Ssam	/*
505195630Ssam	 * Attempt to do a CPU reset via the keyboard controller,
506195630Ssam	 * do not turn of the GateA20, as any machine that fails
507195630Ssam	 * to do the reset here would then end up in no man's land.
508195630Ssam	 */
509195630Ssam
510195630Ssam#if !defined(BROKEN_KEYBOARD_RESET)
511195630Ssam	outb(IO_KBD + 4, 0xFE);
512195630Ssam	DELAY(500000);	/* wait 0.5 sec to see if that did it */
513195630Ssam	printf("Keyboard reset did not work, attempting CPU shutdown\n");
514195630Ssam	DELAY(1000000);	/* wait 1 sec for printf to complete */
515195630Ssam#endif
516195630Ssam#endif /* PC98 */
517195630Ssam	/* force a shutdown by unmapping entire address space ! */
518195630Ssam	bzero((caddr_t) PTD, PAGE_SIZE);
519195630Ssam
520195630Ssam	/* "good night, sweet prince .... <THUNK!>" */
521195630Ssam	invltlb();
522195630Ssam	/* NOTREACHED */
523195630Ssam	while(1);
524195630Ssam}
525195630Ssam
526195630Ssamint
527195630Ssamgrow_stack(p, sp)
528195630Ssam	struct proc *p;
529195630Ssam	u_int sp;
530195630Ssam{
531195630Ssam	int rv;
532195630Ssam
533195630Ssam	rv = vm_map_growstack (p, sp);
534195630Ssam	if (rv != KERN_SUCCESS)
535195630Ssam		return (0);
536195630Ssam
537195630Ssam	return (1);
538195630Ssam}
539195630Ssam
540195630SsamSYSCTL_DECL(_vm_stats_misc);
541195630Ssam
542195630Ssamstatic int cnt_prezero;
543195630Ssam
544195630SsamSYSCTL_INT(_vm_stats_misc, OID_AUTO,
545195630Ssam	cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
546195630Ssam
547195630Ssam/*
548195630Ssam * Implement the pre-zeroed page mechanism.
549195630Ssam * This routine is called from the idle loop.
550195630Ssam */
551195630Ssam
552195630Ssam#define ZIDLE_LO(v)	((v) * 2 / 3)
553195630Ssam#define ZIDLE_HI(v)	((v) * 4 / 5)
554195630Ssam
555195630Ssamint
556195630Ssamvm_page_zero_idle()
557195630Ssam{
558195630Ssam	static int free_rover;
559195630Ssam	static int zero_state;
560195630Ssam	vm_page_t m;
561195630Ssam	int s;
562195630Ssam
563195630Ssam	/*
564195630Ssam	 * Attempt to maintain approximately 1/2 of our free pages in a
565195630Ssam	 * PG_ZERO'd state.   Add some hysteresis to (attempt to) avoid
566195630Ssam	 * generally zeroing a page when the system is near steady-state.
567195630Ssam	 * Otherwise we might get 'flutter' during disk I/O / IPC or
568195630Ssam	 * fast sleeps.  We also do not want to be continuously zeroing
569195630Ssam	 * pages because doing so may flush our L1 and L2 caches too much.
570195630Ssam	 */
571195630Ssam
572195630Ssam	if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count))
573195630Ssam		return(0);
574195630Ssam	if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
575195630Ssam		return(0);
576195630Ssam
577195630Ssam	if (mtx_try_enter(&Giant, MTX_DEF)) {
578195630Ssam		s = splvm();
579195630Ssam		zero_state = 0;
580195630Ssam		m = vm_page_list_find(PQ_FREE, free_rover, FALSE);
581195630Ssam		if (m != NULL && (m->flags & PG_ZERO) == 0) {
582195630Ssam			vm_page_queues[m->queue].lcnt--;
583195630Ssam			TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq);
584195630Ssam			m->queue = PQ_NONE;
585195630Ssam			splx(s);
586195630Ssam			pmap_zero_page(VM_PAGE_TO_PHYS(m));
587195630Ssam			(void)splvm();
588195630Ssam			vm_page_flag_set(m, PG_ZERO);
589195630Ssam			m->queue = PQ_FREE + m->pc;
590195630Ssam			vm_page_queues[m->queue].lcnt++;
591195630Ssam			TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m,
592195630Ssam			    pageq);
593195630Ssam			++vm_page_zero_count;
594195630Ssam			++cnt_prezero;
595195630Ssam			if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count))
596195630Ssam				zero_state = 1;
597195630Ssam		}
598211397Sjoel		free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK;
599195630Ssam		splx(s);
600195630Ssam		mtx_exit(&Giant, MTX_DEF);
601195630Ssam		return (1);
602195630Ssam	}
603195630Ssam	return (0);
604195630Ssam}
605195630Ssam
606195630Ssam/*
607195630Ssam * Software interrupt handler for queued VM system processing.
608195630Ssam */
609195630Ssamvoid
610195630Ssamswi_vm(void *dummy)
611195630Ssam{
612195630Ssam	if (busdma_swi_pending != 0)
613195630Ssam		busdma_swi();
614195630Ssam}
615195630Ssam
616195630Ssam/*
617195630Ssam * Tell whether this address is in some physical memory region.
618195630Ssam * Currently used by the kernel coredump code in order to avoid
619195630Ssam * dumping the ``ISA memory hole'' which could cause indefinite hangs,
620195630Ssam * or other unpredictable behaviour.
621195630Ssam */
622195630Ssam
623195630Ssamint
624195630Ssamis_physical_memory(addr)
625195630Ssam	vm_offset_t addr;
626195630Ssam{
627195630Ssam
628195630Ssam#ifdef DEV_ISA
629195630Ssam	/* The ISA ``memory hole''. */
630195630Ssam	if (addr >= 0xa0000 && addr < 0x100000)
631195630Ssam		return 0;
632195630Ssam#endif
633195630Ssam
634195630Ssam	/*
635195630Ssam	 * stuff other tests for known memory-mapped devices (PCI?)
636195630Ssam	 * here
637195630Ssam	 */
638195630Ssam
639195630Ssam	return 1;
640195630Ssam}
641195630Ssam