xref: /freebsd/sys/i386/i386/vm_machdep.c (revision ae92ea446fda4f708096753838f792d67781e468)
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$
41ae92ea44SDavid Greenman  *	$Id: vm_machdep.c,v 1.22 1994/05/25 08:55:23 rgrimes 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 
5326f9a767SRodney W. Grimes #include <machine/cpu.h>
545b81b6b3SRodney W. Grimes 
5526f9a767SRodney W. Grimes #include <vm/vm.h>
5626f9a767SRodney W. Grimes #include <vm/vm_kern.h>
575b81b6b3SRodney W. Grimes 
58ae92ea44SDavid Greenman #ifndef NOBOUNCE
59ae92ea44SDavid Greenman vm_map_t	io_map;
60ae92ea44SDavid Greenman volatile int	kvasfreecnt;
61ae92ea44SDavid Greenman 
621561d038SDavid Greenman 
63d5e26ef0SDavid Greenman caddr_t		bouncememory;
64ed7fcbd0SDavid Greenman int		bouncepages, bpwait;
65ae92ea44SDavid Greenman vm_offset_t	*bouncepa;
66d5e26ef0SDavid Greenman int		bmwait, bmfreeing;
67d5e26ef0SDavid Greenman 
68ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
69d5e26ef0SDavid Greenman int		bounceallocarraysize;
70d5e26ef0SDavid Greenman unsigned	*bounceallocarray;
71d5e26ef0SDavid Greenman int		bouncefree;
72d5e26ef0SDavid Greenman 
73d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
74ae92ea44SDavid Greenman #define MAXBKVA 1024
7526f9a767SRodney W. Grimes int		maxbkva = MAXBKVA*NBPG;
76d5e26ef0SDavid Greenman 
77d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
78d5e26ef0SDavid Greenman struct kvasfree {
79d5e26ef0SDavid Greenman 	vm_offset_t addr;
80d5e26ef0SDavid Greenman 	vm_offset_t size;
81d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
82d5e26ef0SDavid Greenman 
83d5e26ef0SDavid Greenman 
841561d038SDavid Greenman vm_offset_t vm_bounce_kva();
85d5e26ef0SDavid Greenman /*
86d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
87d5e26ef0SDavid Greenman  * (only 1 inplemented now)
88d5e26ef0SDavid Greenman  */
89d5e26ef0SDavid Greenman vm_offset_t
90d5e26ef0SDavid Greenman vm_bounce_page_find(count)
91d5e26ef0SDavid Greenman 	int count;
92d5e26ef0SDavid Greenman {
93d5e26ef0SDavid Greenman 	int bit;
94d5e26ef0SDavid Greenman 	int s,i;
95d5e26ef0SDavid Greenman 
96d5e26ef0SDavid Greenman 	if (count != 1)
97d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
98d5e26ef0SDavid Greenman 
99d5e26ef0SDavid Greenman 	s = splbio();
100d5e26ef0SDavid Greenman retry:
101d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
102d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
103d5e26ef0SDavid Greenman 			if (bit = ffs(~bounceallocarray[i])) {
104d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
105d5e26ef0SDavid Greenman 				bouncefree -= count;
106d5e26ef0SDavid Greenman 				splx(s);
107ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
108d5e26ef0SDavid Greenman 			}
109d5e26ef0SDavid Greenman 		}
110d5e26ef0SDavid Greenman 	}
111ed7fcbd0SDavid Greenman 	bpwait = 1;
112d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
113d5e26ef0SDavid Greenman 	goto retry;
114d5e26ef0SDavid Greenman }
115d5e26ef0SDavid Greenman 
1161561d038SDavid Greenman void
1171561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1181561d038SDavid Greenman 	vm_offset_t addr;
1191561d038SDavid Greenman 	vm_offset_t size;
1201561d038SDavid Greenman 	int now;
1211561d038SDavid Greenman {
1221561d038SDavid Greenman 	int s = splbio();
1231561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
124ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
125ae92ea44SDavid Greenman 	++kvasfreecnt;
1260e195446SDavid Greenman 	if( now) {
1270e195446SDavid Greenman 		/*
1280e195446SDavid Greenman 		 * this will do wakeups
1290e195446SDavid Greenman 		 */
1301561d038SDavid Greenman 		vm_bounce_kva(0,0);
1310e195446SDavid Greenman 	} else {
1320e195446SDavid Greenman 		if (bmwait) {
1330e195446SDavid Greenman 		/*
1340e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1350e195446SDavid Greenman 		 */
1361561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1370e195446SDavid Greenman 			bmwait = 0;
1380e195446SDavid Greenman 		}
1390e195446SDavid Greenman 	}
1401561d038SDavid Greenman 	splx(s);
1411561d038SDavid Greenman }
1421561d038SDavid Greenman 
143d5e26ef0SDavid Greenman /*
144d5e26ef0SDavid Greenman  * free count bounce buffer pages
145d5e26ef0SDavid Greenman  */
146d5e26ef0SDavid Greenman void
147d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
148d5e26ef0SDavid Greenman 	vm_offset_t pa;
149d5e26ef0SDavid Greenman 	int count;
150d5e26ef0SDavid Greenman {
151d5e26ef0SDavid Greenman 	int allocindex;
152d5e26ef0SDavid Greenman 	int index;
153d5e26ef0SDavid Greenman 	int bit;
154d5e26ef0SDavid Greenman 
155d5e26ef0SDavid Greenman 	if (count != 1)
156d5e26ef0SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!\n");
157d5e26ef0SDavid Greenman 
158ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
159ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
160ae92ea44SDavid Greenman 			break;
161ae92ea44SDavid Greenman 	}
162d5e26ef0SDavid Greenman 
163ae92ea44SDavid Greenman 	if( index == bouncepages)
164ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
165d5e26ef0SDavid Greenman 
166ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
167ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
168d5e26ef0SDavid Greenman 
169d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
170d5e26ef0SDavid Greenman 
171d5e26ef0SDavid Greenman 	bouncefree += count;
172ed7fcbd0SDavid Greenman 	if (bpwait) {
173ed7fcbd0SDavid Greenman 		bpwait = 0;
174d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
175d5e26ef0SDavid Greenman 	}
176ed7fcbd0SDavid Greenman }
177d5e26ef0SDavid Greenman 
178d5e26ef0SDavid Greenman /*
179d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
180d5e26ef0SDavid Greenman  */
181d5e26ef0SDavid Greenman vm_offset_t
182ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
183ae92ea44SDavid Greenman 	int size;
1841561d038SDavid Greenman 	int waitok;
185d5e26ef0SDavid Greenman {
186d5e26ef0SDavid Greenman 	int i;
187d5e26ef0SDavid Greenman 	int startfree;
1886b4ac811SDavid Greenman 	vm_offset_t kva = 0;
189d5e26ef0SDavid Greenman 	int s = splbio();
190d5e26ef0SDavid Greenman more:
191ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
192d5e26ef0SDavid Greenman 		bmfreeing = 1;
193ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
194d5e26ef0SDavid Greenman 			pmap_remove(kernel_pmap,
195d5e26ef0SDavid Greenman 				kvaf[i].addr, kvaf[i].addr + kvaf[i].size);
1961561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
197d5e26ef0SDavid Greenman 				kvaf[i].size);
198d5e26ef0SDavid Greenman 		}
199d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
200d5e26ef0SDavid Greenman 		bmfreeing = 0;
201ae92ea44SDavid Greenman 		if( bmwait) {
202ae92ea44SDavid Greenman 			bmwait = 0;
203ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
204ae92ea44SDavid Greenman 		}
205d5e26ef0SDavid Greenman 	}
206d5e26ef0SDavid Greenman 
2071561d038SDavid Greenman 	if( size == 0) {
2081561d038SDavid Greenman 		splx(s);
2091561d038SDavid Greenman 		return NULL;
2101561d038SDavid Greenman 	}
2111561d038SDavid Greenman 
212ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2131561d038SDavid Greenman 		if( !waitok) {
2141561d038SDavid Greenman 			splx(s);
2151561d038SDavid Greenman 			return NULL;
2161561d038SDavid Greenman 		}
217d5e26ef0SDavid Greenman 		bmwait = 1;
2181561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
219d5e26ef0SDavid Greenman 		goto more;
220d5e26ef0SDavid Greenman 	}
221d5e26ef0SDavid Greenman 	splx(s);
222d5e26ef0SDavid Greenman 	return kva;
223d5e26ef0SDavid Greenman }
224d5e26ef0SDavid Greenman 
225d5e26ef0SDavid Greenman /*
226ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2270e195446SDavid Greenman  */
2280e195446SDavid Greenman vm_offset_t
2290e195446SDavid Greenman vm_bounce_kva_alloc(count)
2300e195446SDavid Greenman int count;
2310e195446SDavid Greenman {
2320e195446SDavid Greenman 	int i;
2330e195446SDavid Greenman 	vm_offset_t kva;
2340e195446SDavid Greenman 	vm_offset_t pa;
2350e195446SDavid Greenman 	if( bouncepages == 0) {
2360e195446SDavid Greenman 		kva = (vm_offset_t) malloc(count*NBPG, M_TEMP, M_WAITOK);
2370e195446SDavid Greenman 		return kva;
2380e195446SDavid Greenman 	}
239ae92ea44SDavid Greenman 	kva = vm_bounce_kva(count*NBPG, 1);
2400e195446SDavid Greenman 	for(i=0;i<count;i++) {
2410e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
2420e195446SDavid Greenman 		pmap_kenter(kva + i * NBPG, pa);
2430e195446SDavid Greenman 	}
24426f9a767SRodney W. Grimes 	pmap_update();
2450e195446SDavid Greenman 	return kva;
2460e195446SDavid Greenman }
2470e195446SDavid Greenman 
2480e195446SDavid Greenman /*
2490e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2500e195446SDavid Greenman  */
2510e195446SDavid Greenman void
2520e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2530e195446SDavid Greenman 	vm_offset_t kva;
2540e195446SDavid Greenman 	int count;
2550e195446SDavid Greenman {
2560e195446SDavid Greenman 	int i;
2570e195446SDavid Greenman 	vm_offset_t pa;
2580e195446SDavid Greenman 	if( bouncepages == 0) {
2590e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2600e195446SDavid Greenman 		return;
2610e195446SDavid Greenman 	}
2620e195446SDavid Greenman 	for(i = 0; i < count; i++) {
2630e195446SDavid Greenman 		pa = pmap_kextract(kva + i * NBPG);
2640e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2650e195446SDavid Greenman 	}
266ae92ea44SDavid Greenman 	vm_bounce_kva_free(kva, count*NBPG, 0);
2670e195446SDavid Greenman }
2680e195446SDavid Greenman 
2690e195446SDavid Greenman /*
270d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
271d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
272d5e26ef0SDavid Greenman  */
273d5e26ef0SDavid Greenman void
274d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
275d5e26ef0SDavid Greenman 	struct buf *bp;
276d5e26ef0SDavid Greenman {
277d5e26ef0SDavid Greenman 	int countvmpg;
278d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
279d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
280d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
281d5e26ef0SDavid Greenman 	vm_offset_t pa;
282d5e26ef0SDavid Greenman 	int dobounceflag = 0;
283d5e26ef0SDavid Greenman 	int bounceindex;
284d5e26ef0SDavid Greenman 	int i;
285d5e26ef0SDavid Greenman 	int s;
286d5e26ef0SDavid Greenman 
287d5e26ef0SDavid Greenman 	if (bouncepages == 0)
288d5e26ef0SDavid Greenman 		return;
289d5e26ef0SDavid Greenman 
290ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
291ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
292ae92ea44SDavid Greenman 		return;
2931561d038SDavid Greenman 	}
2941561d038SDavid Greenman 
295ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
296ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: b_bufsize(0x%x) < b_bcount(0x%x) !!!!\n",
297ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
298ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
299ae92ea44SDavid Greenman 	}
300ae92ea44SDavid Greenman 
301ae92ea44SDavid Greenman /*
302ae92ea44SDavid Greenman  *  This is not really necessary
303ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
304ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
305ae92ea44SDavid Greenman  *	}
306ae92ea44SDavid Greenman  */
307ae92ea44SDavid Greenman 
308ae92ea44SDavid Greenman 
30926f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
31026f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
311d5e26ef0SDavid Greenman 
312d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
313d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
314d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
315d5e26ef0SDavid Greenman 
316d5e26ef0SDavid Greenman /*
317d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
318d5e26ef0SDavid Greenman  */
319d5e26ef0SDavid Greenman 	va = vapstart;
320d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
321ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
322d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
323d5e26ef0SDavid Greenman 			++dobounceflag;
324d5e26ef0SDavid Greenman 		va += NBPG;
325d5e26ef0SDavid Greenman 	}
326d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
327d5e26ef0SDavid Greenman 		return;
328d5e26ef0SDavid Greenman 
329d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
330d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
331d5e26ef0SDavid Greenman 
332d5e26ef0SDavid Greenman /*
333d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
334d5e26ef0SDavid Greenman  */
3351561d038SDavid Greenman 	kva = vm_bounce_kva(countvmpg*NBPG, 1);
336ae92ea44SDavid Greenman #if 0
337ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
338ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
339ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
340ae92ea44SDavid Greenman #endif
341d5e26ef0SDavid Greenman 	va = vapstart;
342d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
343ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
344d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
345d5e26ef0SDavid Greenman 			/*
346d5e26ef0SDavid Greenman 			 * allocate a replacement page
347d5e26ef0SDavid Greenman 			 */
348d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
3496b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), bpa);
350ae92ea44SDavid Greenman #if 0
351ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
352ae92ea44SDavid Greenman #endif
353d5e26ef0SDavid Greenman 			/*
354d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
355d5e26ef0SDavid Greenman 			 */
3561561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
3571561d038SDavid Greenman 				pmap_update();
358d5e26ef0SDavid Greenman 				bcopy((caddr_t) va, (caddr_t) kva + (NBPG * i), NBPG);
3591561d038SDavid Greenman 			}
360d5e26ef0SDavid Greenman 		} else {
361d5e26ef0SDavid Greenman 			/*
362d5e26ef0SDavid Greenman 			 * use original page
363d5e26ef0SDavid Greenman 			 */
3646b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), pa);
365d5e26ef0SDavid Greenman 		}
366d5e26ef0SDavid Greenman 		va += NBPG;
367d5e26ef0SDavid Greenman 	}
3686b4ac811SDavid Greenman 	pmap_update();
369d5e26ef0SDavid Greenman 
370d5e26ef0SDavid Greenman /*
371d5e26ef0SDavid Greenman  * flag the buffer as being bounced
372d5e26ef0SDavid Greenman  */
373d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
374d5e26ef0SDavid Greenman /*
375d5e26ef0SDavid Greenman  * save the original buffer kva
376d5e26ef0SDavid Greenman  */
37726f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
378d5e26ef0SDavid Greenman /*
379d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
380d5e26ef0SDavid Greenman  */
38126f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
382d5e26ef0SDavid Greenman 				((vm_offset_t) bp->b_savekva & (NBPG - 1)));
383ae92ea44SDavid Greenman #if 0
384ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
385ae92ea44SDavid Greenman #endif
386d5e26ef0SDavid Greenman 	return;
387d5e26ef0SDavid Greenman }
388d5e26ef0SDavid Greenman 
389d5e26ef0SDavid Greenman /*
390d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
391d5e26ef0SDavid Greenman  */
392d5e26ef0SDavid Greenman void
393d5e26ef0SDavid Greenman vm_bounce_free(bp)
394d5e26ef0SDavid Greenman 	struct buf *bp;
395d5e26ef0SDavid Greenman {
396d5e26ef0SDavid Greenman 	int i;
397ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
398d5e26ef0SDavid Greenman 	int countvmpg;
399d5e26ef0SDavid Greenman 	int s;
400d5e26ef0SDavid Greenman 
401d5e26ef0SDavid Greenman /*
402d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
403d5e26ef0SDavid Greenman  */
404d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
405d5e26ef0SDavid Greenman 		return;
406d5e26ef0SDavid Greenman 
407ae92ea44SDavid Greenman /*
408ae92ea44SDavid Greenman  *  This check is not necessary
409ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
410ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
411ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
412ae92ea44SDavid Greenman  *	}
413ae92ea44SDavid Greenman  */
414ae92ea44SDavid Greenman 
415d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
41626f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
417ae92ea44SDavid Greenman /*
418ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
419ae92ea44SDavid Greenman */
420d5e26ef0SDavid Greenman 
421d5e26ef0SDavid Greenman /*
422d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
423d5e26ef0SDavid Greenman  */
424ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
425d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
426d5e26ef0SDavid Greenman 		vm_offset_t copycount;
427d5e26ef0SDavid Greenman 
428d5e26ef0SDavid Greenman 		copycount = i386_round_page(bouncekva + 1) - bouncekva;
429ed7fcbd0SDavid Greenman 		mybouncepa = pmap_kextract(i386_trunc_page(bouncekva));
430d5e26ef0SDavid Greenman 
431d5e26ef0SDavid Greenman /*
432d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
433d5e26ef0SDavid Greenman  */
434ae92ea44SDavid Greenman 		if ( mybouncepa != pmap_kextract( i386_trunc_page( origkva))) {
435ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
436ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
437ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
438d5e26ef0SDavid Greenman /*
439d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
440d5e26ef0SDavid Greenman  */
441d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
442ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
443d5e26ef0SDavid Greenman /*
444d5e26ef0SDavid Greenman  * free the bounce allocation
445d5e26ef0SDavid Greenman  */
446ae92ea44SDavid Greenman 
447ae92ea44SDavid Greenman /*
448ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
449ae92ea44SDavid Greenman */
450ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
451d5e26ef0SDavid Greenman 		}
452d5e26ef0SDavid Greenman 
453d5e26ef0SDavid Greenman 		origkva += copycount;
454d5e26ef0SDavid Greenman 		bouncekva += copycount;
455ae92ea44SDavid Greenman 		i += copycount;
456d5e26ef0SDavid Greenman 	}
457d5e26ef0SDavid Greenman 
458d5e26ef0SDavid Greenman /*
459ae92ea44SDavid Greenman 	printf("\n");
460ae92ea44SDavid Greenman */
461ae92ea44SDavid Greenman /*
462d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
463d5e26ef0SDavid Greenman  */
464ae92ea44SDavid Greenman 
46526f9a767SRodney W. Grimes 	bouncekva= i386_trunc_page((vm_offset_t) bp->b_data);
466ae92ea44SDavid Greenman 	bouncekvaend= i386_round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
467ae92ea44SDavid Greenman 
468ae92ea44SDavid Greenman /*
469ae92ea44SDavid Greenman 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / NBPG);
470ae92ea44SDavid Greenman */
471ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
47226f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
473d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
474d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
475d5e26ef0SDavid Greenman 
476d5e26ef0SDavid Greenman 	return;
477d5e26ef0SDavid Greenman }
478d5e26ef0SDavid Greenman 
479ae92ea44SDavid Greenman 
4805b81b6b3SRodney W. Grimes /*
4811561d038SDavid Greenman  * init the bounce buffer system
4821561d038SDavid Greenman  */
4831561d038SDavid Greenman void
4841561d038SDavid Greenman vm_bounce_init()
4851561d038SDavid Greenman {
4861561d038SDavid Greenman 	vm_offset_t minaddr, maxaddr;
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)
4981561d038SDavid Greenman 		panic("Cannot allocate bounce resource array\n");
4991561d038SDavid Greenman 
500ae92ea44SDavid Greenman 	bzero(bounceallocarray, bounceallocarraysize * sizeof(unsigned));
501ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
502ae92ea44SDavid Greenman 	if (!bouncepa)
503ae92ea44SDavid Greenman 		panic("Cannot allocate physical memory array\n");
5041561d038SDavid Greenman 
505ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
506ae92ea44SDavid Greenman 		vm_offset_t pa;
507ae92ea44SDavid Greenman 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * NBPG)) >= SIXTEENMEG)
508ae92ea44SDavid Greenman 			panic("bounce memory out of range");
509ae92ea44SDavid Greenman 		if( pa == 0)
510ae92ea44SDavid Greenman 			panic("bounce memory not resident");
511ae92ea44SDavid Greenman 		bouncepa[i] = pa;
512ae92ea44SDavid Greenman 	}
5131561d038SDavid Greenman 	bouncefree = bouncepages;
5141561d038SDavid Greenman 
5151561d038SDavid Greenman }
516ae92ea44SDavid Greenman #endif /* NOBOUNCE */
517a4f7a4c9SDavid Greenman /*
518a4f7a4c9SDavid Greenman  * quick version of vm_fault
519a4f7a4c9SDavid Greenman  */
520a4f7a4c9SDavid Greenman 
521a4f7a4c9SDavid Greenman void
522a4f7a4c9SDavid Greenman vm_fault_quick( v, prot)
523a4f7a4c9SDavid Greenman 	vm_offset_t v;
524a4f7a4c9SDavid Greenman 	int prot;
525a4f7a4c9SDavid Greenman {
526a4f7a4c9SDavid Greenman 	if( (cpu_class == CPUCLASS_386) &&
527a4f7a4c9SDavid Greenman 		(prot & VM_PROT_WRITE))
528a4f7a4c9SDavid Greenman 		vm_fault(&curproc->p_vmspace->vm_map, v,
529a4f7a4c9SDavid Greenman 			VM_PROT_READ|VM_PROT_WRITE, FALSE);
530a4f7a4c9SDavid Greenman 	else if( prot & VM_PROT_WRITE)
531a4f7a4c9SDavid Greenman 		*(volatile char *)v += 0;
532a4f7a4c9SDavid Greenman 	else
533a4f7a4c9SDavid Greenman 		*(volatile 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;
5515b81b6b3SRodney W. Grimes 	int foo, offset, addr, i;
5525b81b6b3SRodney W. Grimes 	extern char kstack[];
5535b81b6b3SRodney W. Grimes 	extern int mvesp();
5545b81b6b3SRodney W. Grimes 
5555b81b6b3SRodney W. Grimes 	/*
5565b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
5575b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
5585b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
5595b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
5605b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
5615b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
5625b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
5635b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
5645b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
5655b81b6b3SRodney W. Grimes 	 */
5665b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
5675b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
5685b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
5695b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
57026f9a767SRodney W. Grimes 	p2->p_md.md_regs = p1->p_md.md_regs;
5715b81b6b3SRodney W. Grimes 
5725b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
5735b81b6b3SRodney W. Grimes 
5745b81b6b3SRodney W. Grimes 	/*
5755b81b6b3SRodney W. Grimes 	 *
5765b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
5775b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
5785b81b6b3SRodney W. Grimes 	 */
5795b81b6b3SRodney W. Grimes 	if (savectx(up, 1)) {
5805b81b6b3SRodney W. Grimes 		/*
5815b81b6b3SRodney W. Grimes 		 * Return 1 in child.
5825b81b6b3SRodney W. Grimes 		 */
5835b81b6b3SRodney W. Grimes 		return (1);
5845b81b6b3SRodney W. Grimes 	}
5855b81b6b3SRodney W. Grimes 	return (0);
5865b81b6b3SRodney W. Grimes }
5875b81b6b3SRodney W. Grimes 
5887c2b54e8SNate Williams void
5895b81b6b3SRodney W. Grimes cpu_exit(p)
5905b81b6b3SRodney W. Grimes 	register struct proc *p;
5915b81b6b3SRodney W. Grimes {
5925b81b6b3SRodney W. Grimes 
593960173b9SRodney W. Grimes #if NNPX > 0
5945b81b6b3SRodney W. Grimes 	npxexit(p);
595960173b9SRodney W. Grimes #endif	/* NNPX */
59626f9a767SRodney W. Grimes 	curproc = p;
59726f9a767SRodney W. Grimes 	mi_switch();
5987c2b54e8SNate Williams 	/*
5997c2b54e8SNate Williams 	 * This is to shutup the compiler, and if swtch() failed I suppose
6007c2b54e8SNate Williams 	 * this would be a good thing.  This keeps gcc happy because panic
6017c2b54e8SNate Williams 	 * is a volatile void function as well.
6027c2b54e8SNate Williams 	 */
6037c2b54e8SNate Williams 	panic("cpu_exit");
6045b81b6b3SRodney W. Grimes }
6055b81b6b3SRodney W. Grimes 
606381fe1aaSGarrett Wollman void
6077f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
6087f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
6097f8cb368SDavid Greenman 	extern char kstack[];
6105b81b6b3SRodney W. Grimes 
6115b81b6b3SRodney W. Grimes 	/* drop per-process resources */
6127f8cb368SDavid Greenman  	pmap_remove(vm_map_pmap(kernel_map), (vm_offset_t) p->p_addr,
6137f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
6145b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
6157f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
6165b81b6b3SRodney W. Grimes }
6175b81b6b3SRodney W. Grimes 
6185b81b6b3SRodney W. Grimes /*
61926f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
62026f9a767SRodney W. Grimes  */
62126f9a767SRodney W. Grimes int
62226f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
62326f9a767SRodney W. Grimes 	struct proc *p;
62426f9a767SRodney W. Grimes 	struct vnode *vp;
62526f9a767SRodney W. Grimes 	struct ucred *cred;
62626f9a767SRodney W. Grimes {
62726f9a767SRodney W. Grimes 
62826f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
62926f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
63026f9a767SRodney W. Grimes 	    p));
63126f9a767SRodney W. Grimes }
63226f9a767SRodney W. Grimes 
63326f9a767SRodney W. Grimes /*
6345b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
6355b81b6b3SRodney W. Grimes  */
636381fe1aaSGarrett Wollman void
6375b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
6385b81b6b3SRodney W. Grimes 	u_short *pte;
6395b81b6b3SRodney W. Grimes 	caddr_t vaddr;
6405b81b6b3SRodney W. Grimes {
6415b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
6425b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
6435b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
6445b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
6455b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
6465b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
6475b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
6485b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
6495b81b6b3SRodney W. Grimes }
6505b81b6b3SRodney W. Grimes 
6515b81b6b3SRodney W. Grimes /*
65226f9a767SRodney W. Grimes  * Move pages from one kernel virtual address to another.
65326f9a767SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
65426f9a767SRodney W. Grimes  * and size must be a multiple of CLSIZE.
65526f9a767SRodney W. Grimes  */
65626f9a767SRodney W. Grimes 
65726f9a767SRodney W. Grimes void
65826f9a767SRodney W. Grimes pagemove(from, to, size)
65926f9a767SRodney W. Grimes 	register caddr_t from, to;
66026f9a767SRodney W. Grimes 	int size;
66126f9a767SRodney W. Grimes {
66226f9a767SRodney W. Grimes 	register vm_offset_t pa;
66326f9a767SRodney W. Grimes 
66426f9a767SRodney W. Grimes 	if (size & CLOFSET)
66526f9a767SRodney W. Grimes 		panic("pagemove");
66626f9a767SRodney W. Grimes 	while (size > 0) {
66726f9a767SRodney W. Grimes 		pa = pmap_kextract((vm_offset_t)from);
66826f9a767SRodney W. Grimes 		if (pa == 0)
66926f9a767SRodney W. Grimes 			panic("pagemove 2");
67026f9a767SRodney W. Grimes 		if (pmap_kextract((vm_offset_t)to) != 0)
67126f9a767SRodney W. Grimes 			panic("pagemove 3");
67226f9a767SRodney W. Grimes 		pmap_remove(kernel_pmap,
67326f9a767SRodney W. Grimes 			    (vm_offset_t)from, (vm_offset_t)from + PAGE_SIZE);
67426f9a767SRodney W. Grimes 		pmap_kenter( (vm_offset_t)to, pa);
67526f9a767SRodney W. Grimes 		from += PAGE_SIZE;
67626f9a767SRodney W. Grimes 		to += PAGE_SIZE;
67726f9a767SRodney W. Grimes 		size -= PAGE_SIZE;
67826f9a767SRodney W. Grimes 	}
67926f9a767SRodney W. Grimes 	pmap_update();
68026f9a767SRodney W. Grimes }
68126f9a767SRodney W. Grimes 
68226f9a767SRodney W. Grimes /*
6835b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
6845b81b6b3SRodney W. Grimes  */
685aaf08d94SGarrett Wollman u_long
6867f8cb368SDavid Greenman kvtop(void *addr)
6875b81b6b3SRodney W. Grimes {
6885b81b6b3SRodney W. Grimes 	vm_offset_t va;
6895b81b6b3SRodney W. Grimes 
690ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
6915b81b6b3SRodney W. Grimes 	if (va == 0)
6925b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
6937f8cb368SDavid Greenman 	return((int)va);
6945b81b6b3SRodney W. Grimes }
6955b81b6b3SRodney W. Grimes 
6965b81b6b3SRodney W. Grimes /*
697ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
6985b81b6b3SRodney W. Grimes  *
699ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
700ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
701ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
7025b81b6b3SRodney W. Grimes  */
703381fe1aaSGarrett Wollman void
7045b81b6b3SRodney W. Grimes vmapbuf(bp)
7055b81b6b3SRodney W. Grimes 	register struct buf *bp;
7065b81b6b3SRodney W. Grimes {
7075b81b6b3SRodney W. Grimes 	register int npf;
7085b81b6b3SRodney W. Grimes 	register caddr_t addr;
7095b81b6b3SRodney W. Grimes 	int off;
7105b81b6b3SRodney W. Grimes 	vm_offset_t kva;
71126f9a767SRodney W. Grimes 	vm_offset_t pa, lastv, v;
7125b81b6b3SRodney W. Grimes 
71326f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7145b81b6b3SRodney W. Grimes 		panic("vmapbuf");
71526f9a767SRodney W. Grimes 
716ae92ea44SDavid Greenman 	/*
717ae92ea44SDavid Greenman 	 * this is the kva that is to be used for
718ae92ea44SDavid Greenman 	 * the temporary kernel mapping
719ae92ea44SDavid Greenman 	 */
720ae92ea44SDavid Greenman 	kva = (vm_offset_t) bp->b_saveaddr;
721ae92ea44SDavid Greenman 
72226f9a767SRodney W. Grimes 	lastv = 0;
72326f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
72426f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
72526f9a767SRodney W. Grimes 		addr += PAGE_SIZE) {
72626f9a767SRodney W. Grimes 
72726f9a767SRodney W. Grimes /*
72826f9a767SRodney W. Grimes  * make sure that the pde is valid and held
72926f9a767SRodney W. Grimes  */
73026f9a767SRodney W. Grimes 		v = trunc_page(((vm_offset_t)vtopte(addr)));
73126f9a767SRodney W. Grimes 		if (v != lastv) {
73226f9a767SRodney W. Grimes 			vm_fault_quick(v, VM_PROT_READ);
73326f9a767SRodney W. Grimes 			pa = pmap_extract(&curproc->p_vmspace->vm_pmap, v);
73426f9a767SRodney W. Grimes 			vm_page_hold(PHYS_TO_VM_PAGE(pa));
73526f9a767SRodney W. Grimes 			lastv = v;
73626f9a767SRodney W. Grimes 		}
73726f9a767SRodney W. Grimes 
73826f9a767SRodney W. Grimes /*
73926f9a767SRodney W. Grimes  * do the vm_fault if needed, do the copy-on-write thing when
74026f9a767SRodney W. Grimes  * reading stuff off device into memory.
74126f9a767SRodney W. Grimes  */
74226f9a767SRodney W. Grimes 		vm_fault_quick(addr,
74326f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
74426f9a767SRodney W. Grimes 		pa = pmap_extract(&curproc->p_vmspace->vm_pmap, (vm_offset_t) addr);
74526f9a767SRodney W. Grimes /*
74626f9a767SRodney W. Grimes  * hold the data page
74726f9a767SRodney W. Grimes  */
74826f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
74926f9a767SRodney W. Grimes 	}
75026f9a767SRodney W. Grimes 
751ae92ea44SDavid Greenman 	addr = bp->b_saveaddr = bp->b_data;
7525b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
753ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + off));
754ae92ea44SDavid Greenman 	bp->b_data = (caddr_t) (kva + off);
7555b81b6b3SRodney W. Grimes 	while (npf--) {
75626f9a767SRodney W. Grimes 		pa = pmap_extract(&curproc->p_vmspace->vm_pmap, (vm_offset_t)addr);
7575b81b6b3SRodney W. Grimes 		if (pa == 0)
7585b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
7596b4ac811SDavid Greenman 		pmap_kenter(kva, trunc_page(pa));
7605b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
7615b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
7625b81b6b3SRodney W. Grimes 	}
7636b4ac811SDavid Greenman 	pmap_update();
7645b81b6b3SRodney W. Grimes }
7655b81b6b3SRodney W. Grimes 
7665b81b6b3SRodney W. Grimes /*
7675b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
7685b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
7695b81b6b3SRodney W. Grimes  */
770381fe1aaSGarrett Wollman void
7715b81b6b3SRodney W. Grimes vunmapbuf(bp)
7725b81b6b3SRodney W. Grimes 	register struct buf *bp;
7735b81b6b3SRodney W. Grimes {
7745b81b6b3SRodney W. Grimes 	register int npf;
775ae92ea44SDavid Greenman 	register caddr_t addr = bp->b_data;
77626f9a767SRodney W. Grimes 	vm_offset_t kva,va,v,lastv,pa;
7775b81b6b3SRodney W. Grimes 
7785b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7795b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
780ae92ea44SDavid Greenman 	bp->b_data = bp->b_saveaddr;
7815b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
78226f9a767SRodney W. Grimes 
78326f9a767SRodney W. Grimes /*
78426f9a767SRodney W. Grimes  * unhold the pde, and data pages
78526f9a767SRodney W. Grimes  */
78626f9a767SRodney W. Grimes 	lastv = 0;
78726f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
78826f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
78926f9a767SRodney W. Grimes 		addr += NBPG) {
79026f9a767SRodney W. Grimes 
79126f9a767SRodney W. Grimes 	/*
79226f9a767SRodney W. Grimes 	 * release the data page
79326f9a767SRodney W. Grimes 	 */
79426f9a767SRodney W. Grimes 		pa = pmap_extract(&curproc->p_vmspace->vm_pmap, (vm_offset_t) addr);
79526f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
79626f9a767SRodney W. Grimes 
79726f9a767SRodney W. Grimes 	/*
79826f9a767SRodney W. Grimes 	 * and unhold the page table
79926f9a767SRodney W. Grimes 	 */
80026f9a767SRodney W. Grimes 		v = trunc_page(((vm_offset_t)vtopte(addr)));
80126f9a767SRodney W. Grimes 		if (v != lastv) {
80226f9a767SRodney W. Grimes 			pa = pmap_extract(&curproc->p_vmspace->vm_pmap, v);
80326f9a767SRodney W. Grimes 			vm_page_unhold(PHYS_TO_VM_PAGE(pa));
80426f9a767SRodney W. Grimes 			lastv = v;
80526f9a767SRodney W. Grimes 		}
806ae92ea44SDavid Greenman 		pmap_kremove( addr);
80726f9a767SRodney W. Grimes 	}
8085b81b6b3SRodney W. Grimes }
8095b81b6b3SRodney W. Grimes 
8105b81b6b3SRodney W. Grimes /*
8115b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
8125b81b6b3SRodney W. Grimes  */
8137f8cb368SDavid Greenman void
8145b81b6b3SRodney W. Grimes cpu_reset() {
8155b81b6b3SRodney W. Grimes 
8165b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
8175b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
8185b81b6b3SRodney W. Grimes 
8195b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
8205b81b6b3SRodney W. Grimes 	tlbflush();
8215b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8227f8cb368SDavid Greenman 	while(1);
8235b81b6b3SRodney W. Grimes }
824b9d60b3fSDavid Greenman 
825b9d60b3fSDavid Greenman /*
826b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
82729360eb0SDavid Greenman  *	chunks of SGROWSIZ.
828b9d60b3fSDavid Greenman  */
829b9d60b3fSDavid Greenman int
830b9d60b3fSDavid Greenman grow(p, sp)
831b9d60b3fSDavid Greenman 	struct proc *p;
83226f9a767SRodney W. Grimes 	u_int sp;
833b9d60b3fSDavid Greenman {
834b9d60b3fSDavid Greenman 	unsigned int nss;
835b9d60b3fSDavid Greenman 	caddr_t v;
836b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
837b9d60b3fSDavid Greenman 
838b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
839b9d60b3fSDavid Greenman 	    return (1);
840b9d60b3fSDavid Greenman 
841b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
842b9d60b3fSDavid Greenman 
843b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
844b9d60b3fSDavid Greenman 		return (0);
845b9d60b3fSDavid Greenman 
846b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
84729360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
848b9d60b3fSDavid Greenman 		int grow_amount;
849b9d60b3fSDavid Greenman 		/*
850b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
851b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
852b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
853b9d60b3fSDavid Greenman 		 * is expensive).
854b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
85529360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
856b9d60b3fSDavid Greenman 		 */
85729360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
858b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
85929360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
860b9d60b3fSDavid Greenman 		/*
86129360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
862b9d60b3fSDavid Greenman 		 * just extend to the maximum size
863b9d60b3fSDavid Greenman 		 */
864b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
865b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
866b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
867b9d60b3fSDavid Greenman 		}
868b9d60b3fSDavid Greenman 		if (vm_allocate(&vm->vm_map, (vm_offset_t *)&v,
869b9d60b3fSDavid Greenman 		    grow_amount, FALSE) != KERN_SUCCESS) {
870b9d60b3fSDavid Greenman 			return (0);
871b9d60b3fSDavid Greenman 		}
872b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
873b9d60b3fSDavid Greenman 	}
874b9d60b3fSDavid Greenman 
875b9d60b3fSDavid Greenman 	return (1);
876b9d60b3fSDavid Greenman }
877