xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 23e56ee8d756f043e47e83ffe17c21b160ed940d)
15b81b6b3SRodney W. Grimes /*-
25b81b6b3SRodney W. Grimes  * Copyright (c) 1982, 1986 The Regents of the University of California.
35b81b6b3SRodney W. Grimes  * Copyright (c) 1989, 1990 William Jolitz
41561d038SDavid Greenman  * Copyright (c) 1994 John Dyson
55b81b6b3SRodney W. Grimes  * All rights reserved.
65b81b6b3SRodney W. Grimes  *
75b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
85b81b6b3SRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
95b81b6b3SRodney W. Grimes  * Science Department, and William Jolitz.
105b81b6b3SRodney W. Grimes  *
115b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
125b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
135b81b6b3SRodney W. Grimes  * are met:
145b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
155b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
165b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
175b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
185b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
195b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
205b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
215b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
225b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
235b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
245b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
255b81b6b3SRodney W. Grimes  *    without specific prior written permission.
265b81b6b3SRodney W. Grimes  *
275b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
285b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
295b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
305b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
315b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
325b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
335b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
345b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
355b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
365b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
375b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
385b81b6b3SRodney W. Grimes  *
39960173b9SRodney W. Grimes  *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
405b81b6b3SRodney W. Grimes  *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
4123e56ee8SDavid Greenman  *	$Id: vm_machdep.c,v 1.42 1995/09/03 20:39:22 dyson Exp $
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
44960173b9SRodney W. Grimes #include "npx.h"
4526f9a767SRodney W. Grimes #include <sys/param.h>
4626f9a767SRodney W. Grimes #include <sys/systm.h>
4726f9a767SRodney W. Grimes #include <sys/proc.h>
4826f9a767SRodney W. Grimes #include <sys/malloc.h>
4926f9a767SRodney W. Grimes #include <sys/buf.h>
5026f9a767SRodney W. Grimes #include <sys/vnode.h>
5126f9a767SRodney W. Grimes #include <sys/user.h>
525b81b6b3SRodney W. Grimes 
532320728fSRodney W. Grimes #include <machine/clock.h>
5426f9a767SRodney W. Grimes #include <machine/cpu.h>
55b5e8ce9fSBruce Evans #include <machine/md_var.h>
565b81b6b3SRodney W. Grimes 
5726f9a767SRodney W. Grimes #include <vm/vm.h>
5826f9a767SRodney W. Grimes #include <vm/vm_kern.h>
5924a1cce3SDavid Greenman #include <vm/vm_page.h>
605b81b6b3SRodney W. Grimes 
612320728fSRodney W. Grimes #include <i386/isa/isa.h>
622320728fSRodney W. Grimes 
639aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
64ae92ea44SDavid Greenman volatile int	kvasfreecnt;
65ae92ea44SDavid Greenman 
66d5e26ef0SDavid Greenman caddr_t		bouncememory;
67ed7fcbd0SDavid Greenman int		bouncepages, bpwait;
68ae92ea44SDavid Greenman vm_offset_t	*bouncepa;
69d5e26ef0SDavid Greenman int		bmwait, bmfreeing;
70d5e26ef0SDavid Greenman 
71ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
72d5e26ef0SDavid Greenman int		bounceallocarraysize;
73d5e26ef0SDavid Greenman unsigned	*bounceallocarray;
74d5e26ef0SDavid Greenman int		bouncefree;
75d5e26ef0SDavid Greenman 
76d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
77ae92ea44SDavid Greenman #define MAXBKVA 1024
7826f9a767SRodney W. Grimes int		maxbkva = MAXBKVA*NBPG;
79d5e26ef0SDavid Greenman 
80d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
81d5e26ef0SDavid Greenman struct kvasfree {
82d5e26ef0SDavid Greenman 	vm_offset_t addr;
83d5e26ef0SDavid Greenman 	vm_offset_t size;
84d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
85d5e26ef0SDavid Greenman 
86d5e26ef0SDavid Greenman 
871561d038SDavid Greenman vm_offset_t vm_bounce_kva();
88d5e26ef0SDavid Greenman /*
89d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
90d5e26ef0SDavid Greenman  * (only 1 inplemented now)
91d5e26ef0SDavid Greenman  */
92d5e26ef0SDavid Greenman vm_offset_t
93d5e26ef0SDavid Greenman vm_bounce_page_find(count)
94d5e26ef0SDavid Greenman 	int count;
95d5e26ef0SDavid Greenman {
96d5e26ef0SDavid Greenman 	int bit;
97d5e26ef0SDavid Greenman 	int s,i;
98d5e26ef0SDavid Greenman 
99d5e26ef0SDavid Greenman 	if (count != 1)
100d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
101d5e26ef0SDavid Greenman 
102d5e26ef0SDavid Greenman 	s = splbio();
103d5e26ef0SDavid Greenman retry:
104d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
105d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1063fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1073fb3086eSPoul-Henning Kamp 			if (bit) {
108d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
109d5e26ef0SDavid Greenman 				bouncefree -= count;
110d5e26ef0SDavid Greenman 				splx(s);
111ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
112d5e26ef0SDavid Greenman 			}
113d5e26ef0SDavid Greenman 		}
114d5e26ef0SDavid Greenman 	}
115ed7fcbd0SDavid Greenman 	bpwait = 1;
116d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
117d5e26ef0SDavid Greenman 	goto retry;
118d5e26ef0SDavid Greenman }
119d5e26ef0SDavid Greenman 
1201561d038SDavid Greenman void
1211561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1221561d038SDavid Greenman 	vm_offset_t addr;
1231561d038SDavid Greenman 	vm_offset_t size;
1241561d038SDavid Greenman 	int now;
1251561d038SDavid Greenman {
1261561d038SDavid Greenman 	int s = splbio();
1271561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
128ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
129ae92ea44SDavid Greenman 	++kvasfreecnt;
1300e195446SDavid Greenman 	if( now) {
1310e195446SDavid Greenman 		/*
1320e195446SDavid Greenman 		 * this will do wakeups
1330e195446SDavid Greenman 		 */
1341561d038SDavid Greenman 		vm_bounce_kva(0,0);
1350e195446SDavid Greenman 	} else {
1360e195446SDavid Greenman 		if (bmwait) {
1370e195446SDavid Greenman 		/*
1380e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1390e195446SDavid Greenman 		 */
1401561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1410e195446SDavid Greenman 			bmwait = 0;
1420e195446SDavid Greenman 		}
1430e195446SDavid Greenman 	}
1441561d038SDavid Greenman 	splx(s);
1451561d038SDavid Greenman }
1461561d038SDavid Greenman 
147d5e26ef0SDavid Greenman /*
148d5e26ef0SDavid Greenman  * free count bounce buffer pages
149d5e26ef0SDavid Greenman  */
150d5e26ef0SDavid Greenman void
151d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
152d5e26ef0SDavid Greenman 	vm_offset_t pa;
153d5e26ef0SDavid Greenman 	int count;
154d5e26ef0SDavid Greenman {
155d5e26ef0SDavid Greenman 	int allocindex;
156d5e26ef0SDavid Greenman 	int index;
157d5e26ef0SDavid Greenman 	int bit;
158d5e26ef0SDavid Greenman 
159d5e26ef0SDavid Greenman 	if (count != 1)
160edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
161d5e26ef0SDavid Greenman 
162ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
163ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
164ae92ea44SDavid Greenman 			break;
165ae92ea44SDavid Greenman 	}
166d5e26ef0SDavid Greenman 
167ae92ea44SDavid Greenman 	if( index == bouncepages)
168ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
169d5e26ef0SDavid Greenman 
170ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
171ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
172d5e26ef0SDavid Greenman 
173d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
174d5e26ef0SDavid Greenman 
175d5e26ef0SDavid Greenman 	bouncefree += count;
176ed7fcbd0SDavid Greenman 	if (bpwait) {
177ed7fcbd0SDavid Greenman 		bpwait = 0;
178d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
179d5e26ef0SDavid Greenman 	}
180ed7fcbd0SDavid Greenman }
181d5e26ef0SDavid Greenman 
182d5e26ef0SDavid Greenman /*
183d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
184d5e26ef0SDavid Greenman  */
185d5e26ef0SDavid Greenman vm_offset_t
186ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
187ae92ea44SDavid Greenman 	int size;
1881561d038SDavid Greenman 	int waitok;
189d5e26ef0SDavid Greenman {
190d5e26ef0SDavid Greenman 	int i;
1916b4ac811SDavid Greenman 	vm_offset_t kva = 0;
192c564966bSDavid Greenman 	vm_offset_t off;
193d5e26ef0SDavid Greenman 	int s = splbio();
194d5e26ef0SDavid Greenman more:
195ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
196d5e26ef0SDavid Greenman 		bmfreeing = 1;
197ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
198c564966bSDavid Greenman 			for(off=0;off<kvaf[i].size;off+=NBPG) {
199c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
200c564966bSDavid Greenman 			}
2011561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
202d5e26ef0SDavid Greenman 				kvaf[i].size);
203d5e26ef0SDavid Greenman 		}
204d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
205d5e26ef0SDavid Greenman 		bmfreeing = 0;
206ae92ea44SDavid Greenman 		if( bmwait) {
207ae92ea44SDavid Greenman 			bmwait = 0;
208ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
209ae92ea44SDavid Greenman 		}
210d5e26ef0SDavid Greenman 	}
211d5e26ef0SDavid Greenman 
2121561d038SDavid Greenman 	if( size == 0) {
2131561d038SDavid Greenman 		splx(s);
2141561d038SDavid Greenman 		return NULL;
2151561d038SDavid Greenman 	}
2161561d038SDavid Greenman 
217ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2181561d038SDavid Greenman 		if( !waitok) {
2191561d038SDavid Greenman 			splx(s);
2201561d038SDavid Greenman 			return NULL;
2211561d038SDavid Greenman 		}
222d5e26ef0SDavid Greenman 		bmwait = 1;
2231561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
224d5e26ef0SDavid Greenman 		goto more;
225d5e26ef0SDavid Greenman 	}
226d5e26ef0SDavid Greenman 	splx(s);
227d5e26ef0SDavid Greenman 	return kva;
228d5e26ef0SDavid Greenman }
229d5e26ef0SDavid Greenman 
230d5e26ef0SDavid Greenman /*
231ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2320e195446SDavid Greenman  */
2330e195446SDavid Greenman vm_offset_t
2340e195446SDavid Greenman vm_bounce_kva_alloc(count)
2350e195446SDavid Greenman int count;
2360e195446SDavid Greenman {
2370e195446SDavid Greenman 	int i;
2380e195446SDavid Greenman 	vm_offset_t kva;
2390e195446SDavid Greenman 	vm_offset_t pa;
2400e195446SDavid Greenman 	if( bouncepages == 0) {
2410e195446SDavid Greenman 		kva = (vm_offset_t) malloc(count*NBPG, M_TEMP, M_WAITOK);
2420e195446SDavid Greenman 		return kva;
2430e195446SDavid Greenman 	}
244ae92ea44SDavid Greenman 	kva = vm_bounce_kva(count*NBPG, 1);
2450e195446SDavid Greenman 	for(i=0;i<count;i++) {
2460e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
2470e195446SDavid Greenman 		pmap_kenter(kva + i * NBPG, pa);
2480e195446SDavid Greenman 	}
2490e195446SDavid Greenman 	return kva;
2500e195446SDavid Greenman }
2510e195446SDavid Greenman 
2520e195446SDavid Greenman /*
2530e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2540e195446SDavid Greenman  */
2550e195446SDavid Greenman void
2560e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2570e195446SDavid Greenman 	vm_offset_t kva;
2580e195446SDavid Greenman 	int count;
2590e195446SDavid Greenman {
2600e195446SDavid Greenman 	int i;
2610e195446SDavid Greenman 	vm_offset_t pa;
2620e195446SDavid Greenman 	if( bouncepages == 0) {
2630e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2640e195446SDavid Greenman 		return;
2650e195446SDavid Greenman 	}
2660e195446SDavid Greenman 	for(i = 0; i < count; i++) {
2670e195446SDavid Greenman 		pa = pmap_kextract(kva + i * NBPG);
2680e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2690e195446SDavid Greenman 	}
270ae92ea44SDavid Greenman 	vm_bounce_kva_free(kva, count*NBPG, 0);
2710e195446SDavid Greenman }
2720e195446SDavid Greenman 
2730e195446SDavid Greenman /*
274d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
275d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
276d5e26ef0SDavid Greenman  */
277d5e26ef0SDavid Greenman void
278d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
279d5e26ef0SDavid Greenman 	struct buf *bp;
280d5e26ef0SDavid Greenman {
281d5e26ef0SDavid Greenman 	int countvmpg;
282d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
283d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
284d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
285d5e26ef0SDavid Greenman 	vm_offset_t pa;
286d5e26ef0SDavid Greenman 	int dobounceflag = 0;
287d5e26ef0SDavid Greenman 	int i;
288d5e26ef0SDavid Greenman 
289d5e26ef0SDavid Greenman 	if (bouncepages == 0)
290d5e26ef0SDavid Greenman 		return;
291d5e26ef0SDavid Greenman 
292ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
293ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
294ae92ea44SDavid Greenman 		return;
2951561d038SDavid Greenman 	}
2961561d038SDavid Greenman 
297ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
2983fb3086eSPoul-Henning Kamp 		printf(
2993fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
300ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
301ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
302ae92ea44SDavid Greenman 	}
303ae92ea44SDavid Greenman 
304ae92ea44SDavid Greenman /*
305ae92ea44SDavid Greenman  *  This is not really necessary
306ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
307ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
308ae92ea44SDavid Greenman  *	}
309ae92ea44SDavid Greenman  */
310ae92ea44SDavid Greenman 
311ae92ea44SDavid Greenman 
31226f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
31326f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
314d5e26ef0SDavid Greenman 
315d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
316d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
317d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
318d5e26ef0SDavid Greenman 
319d5e26ef0SDavid Greenman /*
320d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
321d5e26ef0SDavid Greenman  */
322d5e26ef0SDavid Greenman 	va = vapstart;
323d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
324ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
325d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
326d5e26ef0SDavid Greenman 			++dobounceflag;
3270d94caffSDavid Greenman 		if( pa == 0)
3280d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
329d5e26ef0SDavid Greenman 		va += NBPG;
330d5e26ef0SDavid Greenman 	}
331d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
332d5e26ef0SDavid Greenman 		return;
333d5e26ef0SDavid Greenman 
334d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
335d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
336d5e26ef0SDavid Greenman 
337d5e26ef0SDavid Greenman /*
338d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
339d5e26ef0SDavid Greenman  */
3401561d038SDavid Greenman 	kva = vm_bounce_kva(countvmpg*NBPG, 1);
341ae92ea44SDavid Greenman #if 0
342ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
343ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
344ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
345ae92ea44SDavid Greenman #endif
346d5e26ef0SDavid Greenman 	va = vapstart;
347d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
348ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
349d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
350d5e26ef0SDavid Greenman 			/*
351d5e26ef0SDavid Greenman 			 * allocate a replacement page
352d5e26ef0SDavid Greenman 			 */
353d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
3546b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), bpa);
355ae92ea44SDavid Greenman #if 0
356ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
357ae92ea44SDavid Greenman #endif
358d5e26ef0SDavid Greenman 			/*
359d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
360d5e26ef0SDavid Greenman 			 */
3611561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
362d5e26ef0SDavid Greenman 				bcopy((caddr_t) va, (caddr_t) kva + (NBPG * i), NBPG);
3631561d038SDavid Greenman 			}
364d5e26ef0SDavid Greenman 		} else {
365d5e26ef0SDavid Greenman 			/*
366d5e26ef0SDavid Greenman 			 * use original page
367d5e26ef0SDavid Greenman 			 */
3686b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), pa);
369d5e26ef0SDavid Greenman 		}
370d5e26ef0SDavid Greenman 		va += NBPG;
371d5e26ef0SDavid Greenman 	}
372d5e26ef0SDavid Greenman 
373d5e26ef0SDavid Greenman /*
374d5e26ef0SDavid Greenman  * flag the buffer as being bounced
375d5e26ef0SDavid Greenman  */
376d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
377d5e26ef0SDavid Greenman /*
378d5e26ef0SDavid Greenman  * save the original buffer kva
379d5e26ef0SDavid Greenman  */
38026f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
381d5e26ef0SDavid Greenman /*
382d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
383d5e26ef0SDavid Greenman  */
38426f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
385d5e26ef0SDavid Greenman 				((vm_offset_t) bp->b_savekva & (NBPG - 1)));
386ae92ea44SDavid Greenman #if 0
387ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
388ae92ea44SDavid Greenman #endif
389d5e26ef0SDavid Greenman 	return;
390d5e26ef0SDavid Greenman }
391d5e26ef0SDavid Greenman 
392d5e26ef0SDavid Greenman /*
393d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
394d5e26ef0SDavid Greenman  */
395d5e26ef0SDavid Greenman void
396d5e26ef0SDavid Greenman vm_bounce_free(bp)
397d5e26ef0SDavid Greenman 	struct buf *bp;
398d5e26ef0SDavid Greenman {
399d5e26ef0SDavid Greenman 	int i;
400ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
401d5e26ef0SDavid Greenman 
402d5e26ef0SDavid Greenman /*
403d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
404d5e26ef0SDavid Greenman  */
405d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
406d5e26ef0SDavid Greenman 		return;
407d5e26ef0SDavid Greenman 
408ae92ea44SDavid Greenman /*
409ae92ea44SDavid Greenman  *  This check is not necessary
410ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
411ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
412ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
413ae92ea44SDavid Greenman  *	}
414ae92ea44SDavid Greenman  */
415ae92ea44SDavid Greenman 
416d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
41726f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
418ae92ea44SDavid Greenman /*
419ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
420ae92ea44SDavid Greenman */
421d5e26ef0SDavid Greenman 
422d5e26ef0SDavid Greenman /*
423d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
424d5e26ef0SDavid Greenman  */
425ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
426d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
427d5e26ef0SDavid Greenman 		vm_offset_t copycount;
428d5e26ef0SDavid Greenman 
429d5e26ef0SDavid Greenman 		copycount = i386_round_page(bouncekva + 1) - bouncekva;
430ed7fcbd0SDavid Greenman 		mybouncepa = pmap_kextract(i386_trunc_page(bouncekva));
431d5e26ef0SDavid Greenman 
432d5e26ef0SDavid Greenman /*
433d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
434d5e26ef0SDavid Greenman  */
435ae92ea44SDavid Greenman 		if ( mybouncepa != pmap_kextract( i386_trunc_page( origkva))) {
436ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
437ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
438ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
439d5e26ef0SDavid Greenman /*
440d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
441d5e26ef0SDavid Greenman  */
442d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
443ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
444d5e26ef0SDavid Greenman /*
445d5e26ef0SDavid Greenman  * free the bounce allocation
446d5e26ef0SDavid Greenman  */
447ae92ea44SDavid Greenman 
448ae92ea44SDavid Greenman /*
449ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
450ae92ea44SDavid Greenman */
451ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
452d5e26ef0SDavid Greenman 		}
453d5e26ef0SDavid Greenman 
454d5e26ef0SDavid Greenman 		origkva += copycount;
455d5e26ef0SDavid Greenman 		bouncekva += copycount;
456ae92ea44SDavid Greenman 		i += copycount;
457d5e26ef0SDavid Greenman 	}
458d5e26ef0SDavid Greenman 
459d5e26ef0SDavid Greenman /*
460ae92ea44SDavid Greenman 	printf("\n");
461ae92ea44SDavid Greenman */
462ae92ea44SDavid Greenman /*
463d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
464d5e26ef0SDavid Greenman  */
465ae92ea44SDavid Greenman 
46626f9a767SRodney W. Grimes 	bouncekva= i386_trunc_page((vm_offset_t) bp->b_data);
467ae92ea44SDavid Greenman 	bouncekvaend= i386_round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
468ae92ea44SDavid Greenman 
469ae92ea44SDavid Greenman /*
470ae92ea44SDavid Greenman 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / NBPG);
471ae92ea44SDavid Greenman */
472ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
47326f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
474d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
475d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
476d5e26ef0SDavid Greenman 
477d5e26ef0SDavid Greenman 	return;
478d5e26ef0SDavid Greenman }
479d5e26ef0SDavid Greenman 
480ae92ea44SDavid Greenman 
4815b81b6b3SRodney W. Grimes /*
4821561d038SDavid Greenman  * init the bounce buffer system
4831561d038SDavid Greenman  */
4841561d038SDavid Greenman void
4851561d038SDavid Greenman vm_bounce_init()
4861561d038SDavid Greenman {
487ae92ea44SDavid Greenman 	int i;
4881561d038SDavid Greenman 
4891561d038SDavid Greenman 	kvasfreecnt = 0;
4901561d038SDavid Greenman 
4911561d038SDavid Greenman 	if (bouncepages == 0)
4921561d038SDavid Greenman 		return;
4931561d038SDavid Greenman 
4941561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
4951561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
4961561d038SDavid Greenman 
4971561d038SDavid Greenman 	if (!bounceallocarray)
498edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
4991561d038SDavid Greenman 
500ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
501ae92ea44SDavid Greenman 	if (!bouncepa)
502edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5031561d038SDavid Greenman 
5040d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5050d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5060d94caffSDavid Greenman 	}
5070d94caffSDavid Greenman 
508ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
509ae92ea44SDavid Greenman 		vm_offset_t pa;
510ae92ea44SDavid Greenman 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * NBPG)) >= SIXTEENMEG)
511ae92ea44SDavid Greenman 			panic("bounce memory out of range");
512ae92ea44SDavid Greenman 		if( pa == 0)
513ae92ea44SDavid Greenman 			panic("bounce memory not resident");
514ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5150d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
516ae92ea44SDavid Greenman 	}
5171561d038SDavid Greenman 	bouncefree = bouncepages;
5181561d038SDavid Greenman 
5191561d038SDavid Greenman }
5209aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
521a4f7a4c9SDavid Greenman /*
522a4f7a4c9SDavid Greenman  * quick version of vm_fault
523a4f7a4c9SDavid Greenman  */
524a4f7a4c9SDavid Greenman 
525a4f7a4c9SDavid Greenman void
526a4f7a4c9SDavid Greenman vm_fault_quick( v, prot)
527a4f7a4c9SDavid Greenman 	vm_offset_t v;
528a4f7a4c9SDavid Greenman 	int prot;
529a4f7a4c9SDavid Greenman {
5301d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5311d1b971bSDavid Greenman 		subyte((char *)v, fubyte((char *)v));
532a4f7a4c9SDavid Greenman 	else
5331d1b971bSDavid Greenman 		(void) fubyte((char *)v);
534a4f7a4c9SDavid Greenman }
535a4f7a4c9SDavid Greenman 
5361561d038SDavid Greenman 
5371561d038SDavid Greenman /*
5385b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
5395b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
5405b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
5415b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
5425b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
5435b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
5445b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
5455b81b6b3SRodney W. Grimes  */
546381fe1aaSGarrett Wollman int
5475b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5485b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5495b81b6b3SRodney W. Grimes {
5505b81b6b3SRodney W. Grimes 	register struct user *up = p2->p_addr;
5513fb3086eSPoul-Henning Kamp 	int offset;
5525b81b6b3SRodney W. Grimes 
5535b81b6b3SRodney W. Grimes 	/*
5545b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
5555b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
5565b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
5575b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
5585b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
5595b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
5605b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
5615b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
5625b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
5635b81b6b3SRodney W. Grimes 	 */
5645b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
5655b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
5665b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
5675b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
56826f9a767SRodney W. Grimes 	p2->p_md.md_regs = p1->p_md.md_regs;
5695b81b6b3SRodney W. Grimes 
5705b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
5715b81b6b3SRodney W. Grimes 
5725b81b6b3SRodney W. Grimes 	/*
5735b81b6b3SRodney W. Grimes 	 *
5745b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
5755b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
5765b81b6b3SRodney W. Grimes 	 */
5773fb3086eSPoul-Henning Kamp 	if (savectx(&up->u_pcb, 1)) {
5785b81b6b3SRodney W. Grimes 		/*
5795b81b6b3SRodney W. Grimes 		 * Return 1 in child.
5805b81b6b3SRodney W. Grimes 		 */
5815b81b6b3SRodney W. Grimes 		return (1);
5825b81b6b3SRodney W. Grimes 	}
5835b81b6b3SRodney W. Grimes 	return (0);
5845b81b6b3SRodney W. Grimes }
5855b81b6b3SRodney W. Grimes 
5867c2b54e8SNate Williams void
5875b81b6b3SRodney W. Grimes cpu_exit(p)
5885b81b6b3SRodney W. Grimes 	register struct proc *p;
5895b81b6b3SRodney W. Grimes {
5905b81b6b3SRodney W. Grimes 
591960173b9SRodney W. Grimes #if NNPX > 0
5925b81b6b3SRodney W. Grimes 	npxexit(p);
593960173b9SRodney W. Grimes #endif	/* NNPX */
5941a051896SBruce Evans 	cnt.v_swtch++;
5951a051896SBruce Evans 	cpu_switch(p);
5967c2b54e8SNate Williams 	panic("cpu_exit");
5975b81b6b3SRodney W. Grimes }
5985b81b6b3SRodney W. Grimes 
599381fe1aaSGarrett Wollman void
6007f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
6017f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
6025b81b6b3SRodney W. Grimes 
6035b81b6b3SRodney W. Grimes 	/* drop per-process resources */
6040d94caffSDavid Greenman  	pmap_remove(vm_map_pmap(u_map), (vm_offset_t) p->p_addr,
6057f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
6060d94caffSDavid Greenman 	kmem_free(u_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
6077f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
6085b81b6b3SRodney W. Grimes }
6095b81b6b3SRodney W. Grimes 
6105b81b6b3SRodney W. Grimes /*
61126f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
61226f9a767SRodney W. Grimes  */
61326f9a767SRodney W. Grimes int
61426f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
61526f9a767SRodney W. Grimes 	struct proc *p;
61626f9a767SRodney W. Grimes 	struct vnode *vp;
61726f9a767SRodney W. Grimes 	struct ucred *cred;
61826f9a767SRodney W. Grimes {
61926f9a767SRodney W. Grimes 
62026f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
62126f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
62226f9a767SRodney W. Grimes 	    p));
62326f9a767SRodney W. Grimes }
62426f9a767SRodney W. Grimes 
62526f9a767SRodney W. Grimes /*
6265b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
6275b81b6b3SRodney W. Grimes  */
628381fe1aaSGarrett Wollman void
6295b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
6305b81b6b3SRodney W. Grimes 	u_short *pte;
6315b81b6b3SRodney W. Grimes 	caddr_t vaddr;
6325b81b6b3SRodney W. Grimes {
6335b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
6345b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
6355b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
6365b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
6375b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
6385b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
6395b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
6405b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
6415b81b6b3SRodney W. Grimes }
6425b81b6b3SRodney W. Grimes 
6435b81b6b3SRodney W. Grimes /*
64426f9a767SRodney W. Grimes  * Move pages from one kernel virtual address to another.
64526f9a767SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
64626f9a767SRodney W. Grimes  * and size must be a multiple of CLSIZE.
64726f9a767SRodney W. Grimes  */
64826f9a767SRodney W. Grimes 
64926f9a767SRodney W. Grimes void
65026f9a767SRodney W. Grimes pagemove(from, to, size)
65126f9a767SRodney W. Grimes 	register caddr_t from, to;
65226f9a767SRodney W. Grimes 	int size;
65326f9a767SRodney W. Grimes {
65426f9a767SRodney W. Grimes 	register vm_offset_t pa;
65526f9a767SRodney W. Grimes 
65626f9a767SRodney W. Grimes 	if (size & CLOFSET)
65726f9a767SRodney W. Grimes 		panic("pagemove");
65826f9a767SRodney W. Grimes 	while (size > 0) {
65926f9a767SRodney W. Grimes 		pa = pmap_kextract((vm_offset_t)from);
66026f9a767SRodney W. Grimes 		if (pa == 0)
66126f9a767SRodney W. Grimes 			panic("pagemove 2");
66226f9a767SRodney W. Grimes 		if (pmap_kextract((vm_offset_t)to) != 0)
66326f9a767SRodney W. Grimes 			panic("pagemove 3");
664c564966bSDavid Greenman 		pmap_kremove((vm_offset_t)from);
66526f9a767SRodney W. Grimes 		pmap_kenter((vm_offset_t)to, pa);
66626f9a767SRodney W. Grimes 		from += PAGE_SIZE;
66726f9a767SRodney W. Grimes 		to += PAGE_SIZE;
66826f9a767SRodney W. Grimes 		size -= PAGE_SIZE;
66926f9a767SRodney W. Grimes 	}
67026f9a767SRodney W. Grimes }
67126f9a767SRodney W. Grimes 
67226f9a767SRodney W. Grimes /*
6735b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
6745b81b6b3SRodney W. Grimes  */
675aaf08d94SGarrett Wollman u_long
6767f8cb368SDavid Greenman kvtop(void *addr)
6775b81b6b3SRodney W. Grimes {
6785b81b6b3SRodney W. Grimes 	vm_offset_t va;
6795b81b6b3SRodney W. Grimes 
680ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
6815b81b6b3SRodney W. Grimes 	if (va == 0)
6825b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
6837f8cb368SDavid Greenman 	return((int)va);
6845b81b6b3SRodney W. Grimes }
6855b81b6b3SRodney W. Grimes 
6865b81b6b3SRodney W. Grimes /*
687ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
6885b81b6b3SRodney W. Grimes  *
689ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
690ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
691ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
6925b81b6b3SRodney W. Grimes  */
693381fe1aaSGarrett Wollman void
6945b81b6b3SRodney W. Grimes vmapbuf(bp)
6955b81b6b3SRodney W. Grimes 	register struct buf *bp;
6965b81b6b3SRodney W. Grimes {
6975b81b6b3SRodney W. Grimes 	register int npf;
6985b81b6b3SRodney W. Grimes 	register caddr_t addr;
6995b81b6b3SRodney W. Grimes 	int off;
7005b81b6b3SRodney W. Grimes 	vm_offset_t kva;
701641a27e6SJohn Dyson 	vm_offset_t pa, v;
7025b81b6b3SRodney W. Grimes 
70326f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7045b81b6b3SRodney W. Grimes 		panic("vmapbuf");
70526f9a767SRodney W. Grimes 
706ae92ea44SDavid Greenman 	/*
707ae92ea44SDavid Greenman 	 * this is the kva that is to be used for
708ae92ea44SDavid Greenman 	 * the temporary kernel mapping
709ae92ea44SDavid Greenman 	 */
710ae92ea44SDavid Greenman 	kva = (vm_offset_t) bp->b_saveaddr;
711ae92ea44SDavid Greenman 
71226f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
71326f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
71426f9a767SRodney W. Grimes 		addr += PAGE_SIZE) {
71526f9a767SRodney W. Grimes 
71626f9a767SRodney W. Grimes /*
71726f9a767SRodney W. Grimes  * do the vm_fault if needed, do the copy-on-write thing when
71826f9a767SRodney W. Grimes  * reading stuff off device into memory.
71926f9a767SRodney W. Grimes  */
72026f9a767SRodney W. Grimes 		vm_fault_quick(addr,
72126f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
722c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t) addr);
723641a27e6SJohn Dyson 		if (pa == 0)
724641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
72526f9a767SRodney W. Grimes /*
72626f9a767SRodney W. Grimes  * hold the data page
72726f9a767SRodney W. Grimes  */
728641a27e6SJohn Dyson #ifdef DIAGNOSTIC
729641a27e6SJohn Dyson 		if( VM_PAGE_TO_PHYS(PHYS_TO_VM_PAGE(pa)) != pa)
730641a27e6SJohn Dyson 			panic("vmapbuf: confused PHYS_TO_VM_PAGE mapping");
731641a27e6SJohn Dyson #endif
73226f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
73326f9a767SRodney W. Grimes 	}
73426f9a767SRodney W. Grimes 
735ae92ea44SDavid Greenman 	addr = bp->b_saveaddr = bp->b_data;
7365b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
737ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + off));
738ae92ea44SDavid Greenman 	bp->b_data = (caddr_t) (kva + off);
7395b81b6b3SRodney W. Grimes 	while (npf--) {
740c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t)addr);
7415b81b6b3SRodney W. Grimes 		if (pa == 0)
7425b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
7436b4ac811SDavid Greenman 		pmap_kenter(kva, trunc_page(pa));
7445b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
7455b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
7465b81b6b3SRodney W. Grimes 	}
7475b81b6b3SRodney W. Grimes }
7485b81b6b3SRodney W. Grimes 
7495b81b6b3SRodney W. Grimes /*
7505b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
7515b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
7525b81b6b3SRodney W. Grimes  */
753381fe1aaSGarrett Wollman void
7545b81b6b3SRodney W. Grimes vunmapbuf(bp)
7555b81b6b3SRodney W. Grimes 	register struct buf *bp;
7565b81b6b3SRodney W. Grimes {
757c564966bSDavid Greenman 	register caddr_t addr;
758641a27e6SJohn Dyson 	vm_offset_t v,pa;
7595b81b6b3SRodney W. Grimes 
7605b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7615b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
762c564966bSDavid Greenman 
763c564966bSDavid Greenman 	for (addr = (caddr_t)trunc_page((vm_offset_t) bp->b_data);
764c564966bSDavid Greenman 		addr < bp->b_data + bp->b_bufsize;
765c564966bSDavid Greenman 		addr += NBPG)
766c564966bSDavid Greenman 		pmap_kremove((vm_offset_t) addr);
767c564966bSDavid Greenman 
768ae92ea44SDavid Greenman 	bp->b_data = bp->b_saveaddr;
7695b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
77026f9a767SRodney W. Grimes 
77126f9a767SRodney W. Grimes /*
77226f9a767SRodney W. Grimes  * unhold the pde, and data pages
77326f9a767SRodney W. Grimes  */
774c564966bSDavid Greenman 	for (addr = (caddr_t)trunc_page((vm_offset_t) bp->b_data);
77526f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
77626f9a767SRodney W. Grimes 		addr += NBPG) {
77726f9a767SRodney W. Grimes 	/*
77826f9a767SRodney W. Grimes 	 * release the data page
77926f9a767SRodney W. Grimes 	 */
780c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t) addr);
78126f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
78226f9a767SRodney W. Grimes 	}
7835b81b6b3SRodney W. Grimes }
7845b81b6b3SRodney W. Grimes 
7855b81b6b3SRodney W. Grimes /*
7865b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
7875b81b6b3SRodney W. Grimes  */
7887f8cb368SDavid Greenman void
7895b81b6b3SRodney W. Grimes cpu_reset() {
7905b81b6b3SRodney W. Grimes 
7912320728fSRodney W. Grimes 	/*
7922320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
7932320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
7942320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
7952320728fSRodney W. Grimes 	 */
7965eb46edfSDavid Greenman 
7975eb46edfSDavid Greenman #ifndef BROKEN_KEYBOARD_RESET
7982320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
7992320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
8002320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
8012320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
8025eb46edfSDavid Greenman #endif
8032320728fSRodney W. Grimes 
8045b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
8055b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
8065b81b6b3SRodney W. Grimes 
8075b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
8082c7a40c7SDavid Greenman 	pmap_update();
8095b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8107f8cb368SDavid Greenman 	while(1);
8115b81b6b3SRodney W. Grimes }
812b9d60b3fSDavid Greenman 
813b9d60b3fSDavid Greenman /*
814b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
81529360eb0SDavid Greenman  *	chunks of SGROWSIZ.
816b9d60b3fSDavid Greenman  */
817b9d60b3fSDavid Greenman int
818b9d60b3fSDavid Greenman grow(p, sp)
819b9d60b3fSDavid Greenman 	struct proc *p;
82026f9a767SRodney W. Grimes 	u_int sp;
821b9d60b3fSDavid Greenman {
822b9d60b3fSDavid Greenman 	unsigned int nss;
823b9d60b3fSDavid Greenman 	caddr_t v;
824b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
825b9d60b3fSDavid Greenman 
826b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
827b9d60b3fSDavid Greenman 	    return (1);
828b9d60b3fSDavid Greenman 
829b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
830b9d60b3fSDavid Greenman 
831b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
832b9d60b3fSDavid Greenman 		return (0);
833b9d60b3fSDavid Greenman 
834b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
83529360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
836b9d60b3fSDavid Greenman 		int grow_amount;
837b9d60b3fSDavid Greenman 		/*
838b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
839b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
840b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
841b9d60b3fSDavid Greenman 		 * is expensive).
842b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
84329360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
844b9d60b3fSDavid Greenman 		 */
84529360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
846b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
84729360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
848b9d60b3fSDavid Greenman 		/*
84929360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
850b9d60b3fSDavid Greenman 		 * just extend to the maximum size
851b9d60b3fSDavid Greenman 		 */
852b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
853b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
854b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
855b9d60b3fSDavid Greenman 		}
85668940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
85768940ac1SDavid Greenman 		    grow_amount, FALSE) != KERN_SUCCESS)) {
858b9d60b3fSDavid Greenman 			return (0);
859b9d60b3fSDavid Greenman 		}
860b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
861b9d60b3fSDavid Greenman 	}
862b9d60b3fSDavid Greenman 
863b9d60b3fSDavid Greenman 	return (1);
864b9d60b3fSDavid Greenman }
865a29b63cbSJohn Dyson 
866a29b63cbSJohn Dyson /*
867a29b63cbSJohn Dyson  * prototype routine to implement the pre-zeroed page mechanism
868a29b63cbSJohn Dyson  * this routine is called from the idle loop.
869a29b63cbSJohn Dyson  */
870a29b63cbSJohn Dyson int
871a29b63cbSJohn Dyson vm_page_zero_idle() {
872a29b63cbSJohn Dyson 	vm_page_t m;
873a29b63cbSJohn Dyson 	if ((cnt.v_free_count > cnt.v_interrupt_free_min) &&
874a29b63cbSJohn Dyson 		(m = vm_page_queue_free.tqh_first)) {
875a29b63cbSJohn Dyson 		TAILQ_REMOVE(&vm_page_queue_free, m, pageq);
876a29b63cbSJohn Dyson 		enable_intr();
877a29b63cbSJohn Dyson 		pmap_zero_page(VM_PAGE_TO_PHYS(m));
878a29b63cbSJohn Dyson 		disable_intr();
87923e56ee8SDavid Greenman 		TAILQ_INSERT_HEAD(&vm_page_queue_zero, m, pageq);
880a29b63cbSJohn Dyson 		return 1;
881a29b63cbSJohn Dyson 	}
882a29b63cbSJohn Dyson 	return 0;
883a29b63cbSJohn Dyson }
884