vm_machdep.c revision 27535
1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * Copyright (c) 1994 John Dyson
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, and William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 *	$Id: vm_machdep.c,v 1.83 1997/06/26 02:04:34 tegge Exp $
42 */
43
44#include "npx.h"
45#include "opt_bounce.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/proc.h>
50#include <sys/malloc.h>
51#include <sys/buf.h>
52#include <sys/vnode.h>
53#include <sys/vmmeter.h>
54
55#include <machine/clock.h>
56#include <machine/cpu.h>
57#include <machine/md_var.h>
58
59#include <vm/vm.h>
60#include <vm/vm_param.h>
61#include <vm/vm_prot.h>
62#include <sys/lock.h>
63#include <vm/vm_kern.h>
64#include <vm/vm_page.h>
65#include <vm/vm_map.h>
66#include <vm/vm_extern.h>
67
68#include <sys/user.h>
69
70#ifdef PC98
71#include <pc98/pc98/pc98.h>
72#else
73#include <i386/isa/isa.h>
74#endif
75
76#ifdef BOUNCE_BUFFERS
77static vm_offset_t
78		vm_bounce_kva __P((int size, int waitok));
79static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
80					int now));
81static vm_offset_t
82		vm_bounce_page_find __P((int count));
83static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
84
85static volatile int	kvasfreecnt;
86
87caddr_t		bouncememory;
88int		bouncepages;
89static int	bpwait;
90static vm_offset_t	*bouncepa;
91static int		bmwait, bmfreeing;
92
93#define BITS_IN_UNSIGNED (8*sizeof(unsigned))
94static int		bounceallocarraysize;
95static unsigned	*bounceallocarray;
96static int		bouncefree;
97
98#if defined(PC98) && defined (EPSON_BOUNCEDMA)
99#define SIXTEENMEG (3840*4096)			/* 15MB boundary */
100#else
101#define SIXTEENMEG (4096*4096)
102#endif
103#define MAXBKVA 1024
104int		maxbkva = MAXBKVA*PAGE_SIZE;
105
106/* special list that can be used at interrupt time for eventual kva free */
107static struct kvasfree {
108	vm_offset_t addr;
109	vm_offset_t size;
110} kvaf[MAXBKVA];
111
112/*
113 * get bounce buffer pages (count physically contiguous)
114 * (only 1 inplemented now)
115 */
116static vm_offset_t
117vm_bounce_page_find(count)
118	int count;
119{
120	int bit;
121	int s,i;
122
123	if (count != 1)
124		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
125
126	s = splbio();
127retry:
128	for (i = 0; i < bounceallocarraysize; i++) {
129		if (bounceallocarray[i] != 0xffffffff) {
130			bit = ffs(~bounceallocarray[i]);
131			if (bit) {
132				bounceallocarray[i] |= 1 << (bit - 1) ;
133				bouncefree -= count;
134				splx(s);
135				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
136			}
137		}
138	}
139	bpwait = 1;
140	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
141	goto retry;
142}
143
144static void
145vm_bounce_kva_free(addr, size, now)
146	vm_offset_t addr;
147	vm_offset_t size;
148	int now;
149{
150	int s = splbio();
151	kvaf[kvasfreecnt].addr = addr;
152	kvaf[kvasfreecnt].size = size;
153	++kvasfreecnt;
154	if( now) {
155		/*
156		 * this will do wakeups
157		 */
158		vm_bounce_kva(0,0);
159	} else {
160		if (bmwait) {
161		/*
162		 * if anyone is waiting on the bounce-map, then wakeup
163		 */
164			wakeup((caddr_t) io_map);
165			bmwait = 0;
166		}
167	}
168	splx(s);
169}
170
171/*
172 * free count bounce buffer pages
173 */
174static void
175vm_bounce_page_free(pa, count)
176	vm_offset_t pa;
177	int count;
178{
179	int allocindex;
180	int index;
181	int bit;
182
183	if (count != 1)
184		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
185
186	for(index=0;index<bouncepages;index++) {
187		if( pa == bouncepa[index])
188			break;
189	}
190
191	if( index == bouncepages)
192		panic("vm_bounce_page_free: invalid bounce buffer");
193
194	allocindex = index / BITS_IN_UNSIGNED;
195	bit = index % BITS_IN_UNSIGNED;
196
197	bounceallocarray[allocindex] &= ~(1 << bit);
198
199	bouncefree += count;
200	if (bpwait) {
201		bpwait = 0;
202		wakeup((caddr_t) &bounceallocarray);
203	}
204}
205
206/*
207 * allocate count bounce buffer kva pages
208 */
209static vm_offset_t
210vm_bounce_kva(size, waitok)
211	int size;
212	int waitok;
213{
214	int i;
215	vm_offset_t kva = 0;
216	vm_offset_t off;
217	int s = splbio();
218more:
219	if (!bmfreeing && kvasfreecnt) {
220		bmfreeing = 1;
221		for (i = 0; i < kvasfreecnt; i++) {
222			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
223				pmap_kremove( kvaf[i].addr + off);
224			}
225			kmem_free_wakeup(io_map, kvaf[i].addr,
226				kvaf[i].size);
227		}
228		kvasfreecnt = 0;
229		bmfreeing = 0;
230		if( bmwait) {
231			bmwait = 0;
232			wakeup( (caddr_t) io_map);
233		}
234	}
235
236	if( size == 0) {
237		splx(s);
238		return 0;
239	}
240
241	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
242		if( !waitok) {
243			splx(s);
244			return 0;
245		}
246		bmwait = 1;
247		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
248		goto more;
249	}
250	splx(s);
251	return kva;
252}
253
254/*
255 * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
256 */
257vm_offset_t
258vm_bounce_kva_alloc(count)
259int count;
260{
261	int i;
262	vm_offset_t kva;
263	vm_offset_t pa;
264	if( bouncepages == 0) {
265		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
266		return kva;
267	}
268	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
269	for(i=0;i<count;i++) {
270		pa = vm_bounce_page_find(1);
271		pmap_kenter(kva + i * PAGE_SIZE, pa);
272	}
273	return kva;
274}
275
276/*
277 * same as vm_bounce_kva_free -- but really free
278 */
279void
280vm_bounce_kva_alloc_free(kva, count)
281	vm_offset_t kva;
282	int count;
283{
284	int i;
285	vm_offset_t pa;
286	if( bouncepages == 0) {
287		free((caddr_t) kva, M_TEMP);
288		return;
289	}
290	for(i = 0; i < count; i++) {
291		pa = pmap_kextract(kva + i * PAGE_SIZE);
292		vm_bounce_page_free(pa, 1);
293	}
294	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
295}
296
297/*
298 * do the things necessary to the struct buf to implement
299 * bounce buffers...  inserted before the disk sort
300 */
301void
302vm_bounce_alloc(bp)
303	struct buf *bp;
304{
305	int countvmpg;
306	vm_offset_t vastart, vaend;
307	vm_offset_t vapstart, vapend;
308	vm_offset_t va, kva;
309	vm_offset_t pa;
310	int dobounceflag = 0;
311	int i;
312
313	if (bouncepages == 0)
314		return;
315
316	if (bp->b_flags & B_BOUNCE) {
317		printf("vm_bounce_alloc: called recursively???\n");
318		return;
319	}
320
321	if (bp->b_bufsize < bp->b_bcount) {
322		printf(
323		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
324			bp->b_bufsize, bp->b_bcount);
325		panic("vm_bounce_alloc");
326	}
327
328/*
329 *  This is not really necessary
330 *	if( bp->b_bufsize != bp->b_bcount) {
331 *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
332 *	}
333 */
334
335
336	vastart = (vm_offset_t) bp->b_data;
337	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
338
339	vapstart = trunc_page(vastart);
340	vapend = round_page(vaend);
341	countvmpg = (vapend - vapstart) / PAGE_SIZE;
342
343/*
344 * if any page is above 16MB, then go into bounce-buffer mode
345 */
346	va = vapstart;
347	for (i = 0; i < countvmpg; i++) {
348		pa = pmap_kextract(va);
349		if (pa >= SIXTEENMEG)
350			++dobounceflag;
351		if( pa == 0)
352			panic("vm_bounce_alloc: Unmapped page");
353		va += PAGE_SIZE;
354	}
355	if (dobounceflag == 0)
356		return;
357
358	if (bouncepages < dobounceflag)
359		panic("Not enough bounce buffers!!!");
360
361/*
362 * allocate a replacement kva for b_addr
363 */
364	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
365#if 0
366	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
367		(bp->b_flags & B_READ) ? "read":"write",
368			vapstart, vapend, countvmpg, kva);
369#endif
370	va = vapstart;
371	for (i = 0; i < countvmpg; i++) {
372		pa = pmap_kextract(va);
373		if (pa >= SIXTEENMEG) {
374			/*
375			 * allocate a replacement page
376			 */
377			vm_offset_t bpa = vm_bounce_page_find(1);
378			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
379#if 0
380			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
381#endif
382			/*
383			 * if we are writing, the copy the data into the page
384			 */
385			if ((bp->b_flags & B_READ) == 0) {
386				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
387			}
388		} else {
389			/*
390			 * use original page
391			 */
392			pmap_kenter(kva + (PAGE_SIZE * i), pa);
393		}
394		va += PAGE_SIZE;
395	}
396
397/*
398 * flag the buffer as being bounced
399 */
400	bp->b_flags |= B_BOUNCE;
401/*
402 * save the original buffer kva
403 */
404	bp->b_savekva = bp->b_data;
405/*
406 * put our new kva into the buffer (offset by original offset)
407 */
408	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
409				((vm_offset_t) bp->b_savekva & PAGE_MASK));
410#if 0
411	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
412#endif
413	return;
414}
415
416/*
417 * hook into biodone to free bounce buffer
418 */
419void
420vm_bounce_free(bp)
421	struct buf *bp;
422{
423	int i;
424	vm_offset_t origkva, bouncekva, bouncekvaend;
425
426/*
427 * if this isn't a bounced buffer, then just return
428 */
429	if ((bp->b_flags & B_BOUNCE) == 0)
430		return;
431
432/*
433 *  This check is not necessary
434 *	if (bp->b_bufsize != bp->b_bcount) {
435 *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
436 *			bp->b_bufsize, bp->b_bcount);
437 *	}
438 */
439
440	origkva = (vm_offset_t) bp->b_savekva;
441	bouncekva = (vm_offset_t) bp->b_data;
442/*
443	printf("free: %d ", bp->b_bufsize);
444*/
445
446/*
447 * check every page in the kva space for b_addr
448 */
449	for (i = 0; i < bp->b_bufsize; ) {
450		vm_offset_t mybouncepa;
451		vm_offset_t copycount;
452
453		copycount = round_page(bouncekva + 1) - bouncekva;
454		mybouncepa = pmap_kextract(trunc_page(bouncekva));
455
456/*
457 * if this is a bounced pa, then process as one
458 */
459		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
460			vm_offset_t tocopy = copycount;
461			if (i + tocopy > bp->b_bufsize)
462				tocopy = bp->b_bufsize - i;
463/*
464 * if this is a read, then copy from bounce buffer into original buffer
465 */
466			if (bp->b_flags & B_READ)
467				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
468/*
469 * free the bounce allocation
470 */
471
472/*
473			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
474*/
475			vm_bounce_page_free(mybouncepa, 1);
476		}
477
478		origkva += copycount;
479		bouncekva += copycount;
480		i += copycount;
481	}
482
483/*
484	printf("\n");
485*/
486/*
487 * add the old kva into the "to free" list
488 */
489
490	bouncekva= trunc_page((vm_offset_t) bp->b_data);
491	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
492
493/*
494	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
495*/
496	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
497	bp->b_data = bp->b_savekva;
498	bp->b_savekva = 0;
499	bp->b_flags &= ~B_BOUNCE;
500
501	return;
502}
503
504
505/*
506 * init the bounce buffer system
507 */
508void
509vm_bounce_init()
510{
511	int i;
512
513	kvasfreecnt = 0;
514
515	if (bouncepages == 0)
516		return;
517
518	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
519	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
520
521	if (!bounceallocarray)
522		panic("Cannot allocate bounce resource array");
523
524	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
525	if (!bouncepa)
526		panic("Cannot allocate physical memory array");
527
528	for(i=0;i<bounceallocarraysize;i++) {
529		bounceallocarray[i] = 0xffffffff;
530	}
531
532	for(i=0;i<bouncepages;i++) {
533		vm_offset_t pa;
534		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG)
535			panic("bounce memory out of range");
536		if( pa == 0)
537			panic("bounce memory not resident");
538		bouncepa[i] = pa;
539		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
540	}
541	bouncefree = bouncepages;
542
543}
544#endif /* BOUNCE_BUFFERS */
545
546/*
547 * quick version of vm_fault
548 */
549void
550vm_fault_quick(v, prot)
551	caddr_t v;
552	int prot;
553{
554	if (prot & VM_PROT_WRITE)
555		subyte(v, fubyte(v));
556	else
557		fubyte(v);
558}
559
560/*
561 * Finish a fork operation, with process p2 nearly set up.
562 * Copy and update the pcb, set up the stack so that the child
563 * ready to run and return to user mode.
564 */
565void
566cpu_fork(p1, p2)
567	register struct proc *p1, *p2;
568{
569	struct pcb *pcb2 = &p2->p_addr->u_pcb;
570
571	/* Ensure that p1's pcb is up to date. */
572	if (npxproc == p1)
573		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
574
575	/* Copy p1's pcb. */
576	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
577
578	/*
579	 * Create a new fresh stack for the new process.
580	 * Copy the trap frame for the return to user mode as if from a
581	 * syscall.  This copies the user mode register values.
582	 */
583	p2->p_md.md_regs = (struct trapframe *)
584			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
585	*p2->p_md.md_regs = *p1->p_md.md_regs;
586
587	/*
588	 * Set registers for trampoline to user mode.  Leave space for the
589	 * return address on stack.  These are the kernel mode register values.
590	 */
591	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
592	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
593	pcb2->pcb_esi = (int)fork_return;
594	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
595	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
596	pcb2->pcb_ebx = (int)p2;
597	pcb2->pcb_eip = (int)fork_trampoline;
598	/*
599	 * pcb2->pcb_ldt:	duplicated below, if necessary.
600	 * pcb2->pcb_ldt_len:	cloned above.
601	 * pcb2->pcb_savefpu:	cloned above.
602	 * pcb2->pcb_flags:	cloned above (always 0 here?).
603	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
604	 */
605
606#ifdef USER_LDT
607        /* Copy the LDT, if necessary. */
608        if (pcb2->pcb_ldt != 0) {
609                union descriptor *new_ldt;
610                size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
611
612                new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
613                bcopy(pcb2->pcb_ldt, new_ldt, len);
614                pcb2->pcb_ldt = (caddr_t)new_ldt;
615        }
616#endif
617
618	/*
619	 * Now, cpu_switch() can schedule the new process.
620	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
621	 * containing the return address when exiting cpu_switch.
622	 * This will normally be to proc_trampoline(), which will have
623	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
624	 * will set up a stack to call fork_return(p, frame); to complete
625	 * the return to user-mode.
626	 */
627}
628
629/*
630 * Intercept the return address from a freshly forked process that has NOT
631 * been scheduled yet.
632 *
633 * This is needed to make kernel threads stay in kernel mode.
634 */
635void
636cpu_set_fork_handler(p, func, arg)
637	struct proc *p;
638	void (*func) __P((void *));
639	void *arg;
640{
641	/*
642	 * Note that the trap frame follows the args, so the function
643	 * is really called like this:  func(arg, frame);
644	 */
645	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
646	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
647}
648
649void
650cpu_exit(p)
651	register struct proc *p;
652{
653#ifdef USER_LDT
654	struct pcb *pcb;
655#endif
656
657#if NNPX > 0
658	npxexit(p);
659#endif	/* NNPX */
660#ifdef USER_LDT
661	pcb = &p->p_addr->u_pcb;
662	if (pcb->pcb_ldt != 0) {
663		if (pcb == curpcb)
664			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
665		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
666			pcb->pcb_ldt_len * sizeof(union descriptor));
667		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
668	}
669#endif
670	cnt.v_swtch++;
671	cpu_switch(p);
672	panic("cpu_exit");
673}
674
675void
676cpu_wait(p)
677	struct proc *p;
678{
679	/* drop per-process resources */
680	pmap_dispose_proc(p);
681	vmspace_free(p->p_vmspace);
682}
683
684/*
685 * Dump the machine specific header information at the start of a core dump.
686 */
687int
688cpu_coredump(p, vp, cred)
689	struct proc *p;
690	struct vnode *vp;
691	struct ucred *cred;
692{
693
694	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
695	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
696	    p));
697}
698
699#ifdef notyet
700static void
701setredzone(pte, vaddr)
702	u_short *pte;
703	caddr_t vaddr;
704{
705/* eventually do this by setting up an expand-down stack segment
706   for ss0: selector, allowing stack access down to top of u.
707   this means though that protection violations need to be handled
708   thru a double fault exception that must do an integral task
709   switch to a known good context, within which a dump can be
710   taken. a sensible scheme might be to save the initial context
711   used by sched (that has physical memory mapped 1:1 at bottom)
712   and take the dump while still in mapped mode */
713}
714#endif
715
716/*
717 * Convert kernel VA to physical address
718 */
719u_long
720kvtop(void *addr)
721{
722	vm_offset_t va;
723
724	va = pmap_kextract((vm_offset_t)addr);
725	if (va == 0)
726		panic("kvtop: zero page frame");
727	return((int)va);
728}
729
730/*
731 * Map an IO request into kernel virtual address space.
732 *
733 * All requests are (re)mapped into kernel VA space.
734 * Notice that we use b_bufsize for the size of the buffer
735 * to be mapped.  b_bcount might be modified by the driver.
736 */
737void
738vmapbuf(bp)
739	register struct buf *bp;
740{
741	register caddr_t addr, v, kva;
742	vm_offset_t pa;
743
744	if ((bp->b_flags & B_PHYS) == 0)
745		panic("vmapbuf");
746
747	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
748	    addr < bp->b_data + bp->b_bufsize;
749	    addr += PAGE_SIZE, v += PAGE_SIZE) {
750		/*
751		 * Do the vm_fault if needed; do the copy-on-write thing
752		 * when reading stuff off device into memory.
753		 */
754		vm_fault_quick(addr,
755			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
756		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
757		if (pa == 0)
758			panic("vmapbuf: page not present");
759		vm_page_hold(PHYS_TO_VM_PAGE(pa));
760		pmap_kenter((vm_offset_t) v, pa);
761	}
762
763	kva = bp->b_saveaddr;
764	bp->b_saveaddr = bp->b_data;
765	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
766}
767
768/*
769 * Free the io map PTEs associated with this IO operation.
770 * We also invalidate the TLB entries and restore the original b_addr.
771 */
772void
773vunmapbuf(bp)
774	register struct buf *bp;
775{
776	register caddr_t addr;
777	vm_offset_t pa;
778
779	if ((bp->b_flags & B_PHYS) == 0)
780		panic("vunmapbuf");
781
782	for (addr = (caddr_t)trunc_page(bp->b_data);
783	    addr < bp->b_data + bp->b_bufsize;
784	    addr += PAGE_SIZE) {
785		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
786		pmap_kremove((vm_offset_t) addr);
787		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
788	}
789
790	bp->b_data = bp->b_saveaddr;
791}
792
793/*
794 * Force reset the processor by invalidating the entire address space!
795 */
796void
797cpu_reset() {
798#ifdef PC98
799	/*
800	 * Attempt to do a CPU reset via CPU reset port.
801	 */
802	asm("cli");
803	outb(0x37, 0x0f);		/* SHUT0 = 0. */
804	outb(0x37, 0x0b);		/* SHUT1 = 0. */
805	outb(0xf0, 0x00);		/* Reset. */
806#else
807	/*
808	 * Attempt to do a CPU reset via the keyboard controller,
809	 * do not turn of the GateA20, as any machine that fails
810	 * to do the reset here would then end up in no man's land.
811	 */
812
813#if !defined(BROKEN_KEYBOARD_RESET)
814	outb(IO_KBD + 4, 0xFE);
815	DELAY(500000);	/* wait 0.5 sec to see if that did it */
816	printf("Keyboard reset did not work, attempting CPU shutdown\n");
817	DELAY(1000000);	/* wait 1 sec for printf to complete */
818#endif
819#endif /* PC98 */
820	/* force a shutdown by unmapping entire address space ! */
821	bzero((caddr_t) PTD, PAGE_SIZE);
822
823	/* "good night, sweet prince .... <THUNK!>" */
824	invltlb();
825	/* NOTREACHED */
826	while(1);
827}
828
829/*
830 * Grow the user stack to allow for 'sp'. This version grows the stack in
831 *	chunks of SGROWSIZ.
832 */
833int
834grow(p, sp)
835	struct proc *p;
836	u_int sp;
837{
838	unsigned int nss;
839	caddr_t v;
840	struct vmspace *vm = p->p_vmspace;
841
842	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
843	    return (1);
844
845	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
846
847	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
848		return (0);
849
850	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
851	    SGROWSIZ) < nss) {
852		int grow_amount;
853		/*
854		 * If necessary, grow the VM that the stack occupies
855		 * to allow for the rlimit. This allows us to not have
856		 * to allocate all of the VM up-front in execve (which
857		 * is expensive).
858		 * Grow the VM by the amount requested rounded up to
859		 * the nearest SGROWSIZ to provide for some hysteresis.
860		 */
861		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
862		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
863		    SGROWSIZ) - grow_amount;
864		/*
865		 * If there isn't enough room to extend by SGROWSIZ, then
866		 * just extend to the maximum size
867		 */
868		if (v < vm->vm_maxsaddr) {
869			v = vm->vm_maxsaddr;
870			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
871		}
872		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
873		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
874			return (0);
875		}
876		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
877	}
878
879	return (1);
880}
881
882/*
883 * Implement the pre-zeroed page mechanism.
884 * This routine is called from the idle loop.
885 */
886int
887vm_page_zero_idle()
888{
889	static int free_rover;
890	vm_page_t m;
891	int s;
892
893#ifdef WRONG
894	if (cnt.v_free_count <= cnt.v_interrupt_free_min)
895		return (0);
896#endif
897	/*
898	 * XXX
899	 * We stop zeroing pages when there are sufficent prezeroed pages.
900	 * This threshold isn't really needed, except we want to
901	 * bypass unneeded calls to vm_page_list_find, and the
902	 * associated cache flush and latency.  The pre-zero will
903	 * still be called when there are significantly more
904	 * non-prezeroed pages than zeroed pages.  The threshold
905	 * of half the number of reserved pages is arbitrary, but
906	 * approximately the right amount.  Eventually, we should
907	 * perhaps interrupt the zero operation when a process
908	 * is found to be ready to run.
909	 */
910	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
911		return (0);
912	s = splvm();
913	enable_intr();
914	m = vm_page_list_find(PQ_FREE, free_rover);
915	if (m != NULL) {
916		--(*vm_page_queues[m->queue].lcnt);
917		TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
918		splx(s);
919		pmap_zero_page(VM_PAGE_TO_PHYS(m));
920		(void)splvm();
921		m->queue = PQ_ZERO + m->pc;
922		++(*vm_page_queues[m->queue].lcnt);
923		TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq);
924		free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
925		++vm_page_zero_count;
926	}
927	splx(s);
928	disable_intr();
929	return (1);
930}
931