xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 26f9a76710a312a951848542b9ca1f44100450e2)
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$
41a4f7a4c9SDavid Greenman  *	$Id: vm_machdep.c,v 1.20 1994/04/20 07:06:20 davidg 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 
581561d038SDavid Greenman #define b_cylin b_resid
591561d038SDavid Greenman 
60d5e26ef0SDavid Greenman caddr_t		bouncememory;
61d5e26ef0SDavid Greenman vm_offset_t	bouncepa, bouncepaend;
62ed7fcbd0SDavid Greenman int		bouncepages, bpwait;
631561d038SDavid Greenman vm_map_t	io_map;
64d5e26ef0SDavid Greenman int		bmwait, bmfreeing;
65d5e26ef0SDavid Greenman 
66ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
67d5e26ef0SDavid Greenman int		bounceallocarraysize;
68d5e26ef0SDavid Greenman unsigned	*bounceallocarray;
69d5e26ef0SDavid Greenman int		bouncefree;
70d5e26ef0SDavid Greenman 
71d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
7226f9a767SRodney W. Grimes #define MAXBKVA 512
7326f9a767SRodney W. Grimes int		maxbkva=MAXBKVA*NBPG;
74d5e26ef0SDavid Greenman 
75d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
76d5e26ef0SDavid Greenman struct kvasfree {
77d5e26ef0SDavid Greenman 	vm_offset_t addr;
78d5e26ef0SDavid Greenman 	vm_offset_t size;
79d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
80d5e26ef0SDavid Greenman 
81d5e26ef0SDavid Greenman int		kvasfreecnt;
82d5e26ef0SDavid Greenman 
831561d038SDavid Greenman vm_offset_t vm_bounce_kva();
84d5e26ef0SDavid Greenman /*
85d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
86d5e26ef0SDavid Greenman  * (only 1 inplemented now)
87d5e26ef0SDavid Greenman  */
88d5e26ef0SDavid Greenman vm_offset_t
89d5e26ef0SDavid Greenman vm_bounce_page_find(count)
90d5e26ef0SDavid Greenman 	int count;
91d5e26ef0SDavid Greenman {
92d5e26ef0SDavid Greenman 	int bit;
93d5e26ef0SDavid Greenman 	int s,i;
94d5e26ef0SDavid Greenman 
95d5e26ef0SDavid Greenman 	if (count != 1)
96d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
97d5e26ef0SDavid Greenman 
98d5e26ef0SDavid Greenman 	s = splbio();
99d5e26ef0SDavid Greenman retry:
100d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
101d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
102d5e26ef0SDavid Greenman 			if (bit = ffs(~bounceallocarray[i])) {
103d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
104d5e26ef0SDavid Greenman 				bouncefree -= count;
105d5e26ef0SDavid Greenman 				splx(s);
106ed7fcbd0SDavid Greenman 				return bouncepa + (i * BITS_IN_UNSIGNED + (bit - 1)) * NBPG;
107d5e26ef0SDavid Greenman 			}
108d5e26ef0SDavid Greenman 		}
109d5e26ef0SDavid Greenman 	}
110ed7fcbd0SDavid Greenman 	bpwait = 1;
111d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
112d5e26ef0SDavid Greenman 	goto retry;
113d5e26ef0SDavid Greenman }
114d5e26ef0SDavid Greenman 
1151561d038SDavid Greenman void
1161561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1171561d038SDavid Greenman 	vm_offset_t addr;
1181561d038SDavid Greenman 	vm_offset_t size;
1191561d038SDavid Greenman 	int now;
1201561d038SDavid Greenman {
1211561d038SDavid Greenman 	int s = splbio();
1221561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
1231561d038SDavid Greenman 	kvaf[kvasfreecnt++].size = size;
1240e195446SDavid Greenman 	if( now) {
1250e195446SDavid Greenman 		/*
1260e195446SDavid Greenman 		 * this will do wakeups
1270e195446SDavid Greenman 		 */
1281561d038SDavid Greenman 		vm_bounce_kva(0,0);
1290e195446SDavid Greenman 	} else {
1300e195446SDavid Greenman 		if (bmwait) {
1310e195446SDavid Greenman 		/*
1320e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1330e195446SDavid Greenman 		 */
1341561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1350e195446SDavid Greenman 			bmwait = 0;
1360e195446SDavid Greenman 		}
1370e195446SDavid Greenman 	}
1381561d038SDavid Greenman 	splx(s);
1391561d038SDavid Greenman }
1401561d038SDavid Greenman 
141d5e26ef0SDavid Greenman /*
142d5e26ef0SDavid Greenman  * free count bounce buffer pages
143d5e26ef0SDavid Greenman  */
144d5e26ef0SDavid Greenman void
145d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
146d5e26ef0SDavid Greenman 	vm_offset_t pa;
147d5e26ef0SDavid Greenman 	int count;
148d5e26ef0SDavid Greenman {
149d5e26ef0SDavid Greenman 	int allocindex;
150d5e26ef0SDavid Greenman 	int index;
151d5e26ef0SDavid Greenman 	int bit;
152d5e26ef0SDavid Greenman 
153d5e26ef0SDavid Greenman 	if (count != 1)
154d5e26ef0SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!\n");
155d5e26ef0SDavid Greenman 
156d5e26ef0SDavid Greenman 	index = (pa - bouncepa) / NBPG;
157d5e26ef0SDavid Greenman 
158d5e26ef0SDavid Greenman 	if ((index < 0) || (index >= bouncepages))
159d5e26ef0SDavid Greenman 		panic("vm_bounce_page_free -- bad index\n");
160d5e26ef0SDavid Greenman 
161ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
162ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
163d5e26ef0SDavid Greenman 
164d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
165d5e26ef0SDavid Greenman 
166d5e26ef0SDavid Greenman 	bouncefree += count;
167ed7fcbd0SDavid Greenman 	if (bpwait) {
168ed7fcbd0SDavid Greenman 		bpwait = 0;
169d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
170d5e26ef0SDavid Greenman 	}
171ed7fcbd0SDavid Greenman }
172d5e26ef0SDavid Greenman 
173d5e26ef0SDavid Greenman /*
174d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
175d5e26ef0SDavid Greenman  */
176d5e26ef0SDavid Greenman vm_offset_t
1771561d038SDavid Greenman vm_bounce_kva(count, waitok)
178d5e26ef0SDavid Greenman 	int count;
1791561d038SDavid Greenman 	int waitok;
180d5e26ef0SDavid Greenman {
181d5e26ef0SDavid Greenman 	int tofree;
182d5e26ef0SDavid Greenman 	int i;
183d5e26ef0SDavid Greenman 	int startfree;
1846b4ac811SDavid Greenman 	vm_offset_t kva = 0;
185d5e26ef0SDavid Greenman 	int s = splbio();
1861561d038SDavid Greenman 	int size = count;
187d5e26ef0SDavid Greenman 	startfree = 0;
188d5e26ef0SDavid Greenman more:
189d5e26ef0SDavid Greenman 	if (!bmfreeing && (tofree = kvasfreecnt)) {
190d5e26ef0SDavid Greenman 		bmfreeing = 1;
191d5e26ef0SDavid Greenman 		for (i = startfree; i < kvasfreecnt; i++) {
1926b4ac811SDavid Greenman 			/*
1936b4ac811SDavid Greenman 			 * if we have a kva of the right size, no sense
1946b4ac811SDavid Greenman 			 * in freeing/reallocating...
1956b4ac811SDavid Greenman 			 * might affect fragmentation short term, but
1961561d038SDavid Greenman 			 * as long as the amount of io_map is
1976b4ac811SDavid Greenman 			 * significantly more than the maximum transfer
1986b4ac811SDavid Greenman 			 * size, I don't think that it is a problem.
1996b4ac811SDavid Greenman 			 */
200d5e26ef0SDavid Greenman 			pmap_remove(kernel_pmap,
201d5e26ef0SDavid Greenman 				kvaf[i].addr, kvaf[i].addr + kvaf[i].size);
2021561d038SDavid Greenman 			if( size && !kva && kvaf[i].size == size) {
2036b4ac811SDavid Greenman 				kva = kvaf[i].addr;
2046b4ac811SDavid Greenman 			} else {
2051561d038SDavid Greenman 				kmem_free_wakeup(io_map, kvaf[i].addr,
206d5e26ef0SDavid Greenman 					kvaf[i].size);
207d5e26ef0SDavid Greenman 			}
2086b4ac811SDavid Greenman 		}
209d5e26ef0SDavid Greenman 		if (kvasfreecnt != tofree) {
210d5e26ef0SDavid Greenman 			startfree = i;
211d5e26ef0SDavid Greenman 			bmfreeing = 0;
212d5e26ef0SDavid Greenman 			goto more;
213d5e26ef0SDavid Greenman 		}
214d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
215d5e26ef0SDavid Greenman 		bmfreeing = 0;
216d5e26ef0SDavid Greenman 	}
217d5e26ef0SDavid Greenman 
2181561d038SDavid Greenman 	if( size == 0) {
2191561d038SDavid Greenman 		splx(s);
2201561d038SDavid Greenman 		return NULL;
2211561d038SDavid Greenman 	}
2221561d038SDavid Greenman 
2231561d038SDavid Greenman 	if (!kva && !(kva = kmem_alloc_pageable(io_map, size))) {
2241561d038SDavid Greenman 		if( !waitok) {
2251561d038SDavid Greenman 			splx(s);
2261561d038SDavid Greenman 			return NULL;
2271561d038SDavid Greenman 		}
228d5e26ef0SDavid Greenman 		bmwait = 1;
2291561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
230d5e26ef0SDavid Greenman 		goto more;
231d5e26ef0SDavid Greenman 	}
232d5e26ef0SDavid Greenman 	splx(s);
233d5e26ef0SDavid Greenman 
234d5e26ef0SDavid Greenman 	return kva;
235d5e26ef0SDavid Greenman }
236d5e26ef0SDavid Greenman 
237d5e26ef0SDavid Greenman /*
2380e195446SDavid Greenman  * same as vm_bounce_kva -- but really allocate
2390e195446SDavid Greenman  */
2400e195446SDavid Greenman vm_offset_t
2410e195446SDavid Greenman vm_bounce_kva_alloc(count)
2420e195446SDavid Greenman int count;
2430e195446SDavid Greenman {
2440e195446SDavid Greenman 	int i;
2450e195446SDavid Greenman 	vm_offset_t kva;
2460e195446SDavid Greenman 	vm_offset_t pa;
2470e195446SDavid Greenman 	if( bouncepages == 0) {
2480e195446SDavid Greenman 		kva = (vm_offset_t) malloc(count*NBPG, M_TEMP, M_WAITOK);
2490e195446SDavid Greenman 		return kva;
2500e195446SDavid Greenman 	}
2510e195446SDavid Greenman 	kva = vm_bounce_kva(count, 1);
2520e195446SDavid Greenman 	for(i=0;i<count;i++) {
2530e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
2540e195446SDavid Greenman 		pmap_kenter(kva + i * NBPG, pa);
2550e195446SDavid Greenman 	}
25626f9a767SRodney W. Grimes 	pmap_update();
2570e195446SDavid Greenman 	return kva;
2580e195446SDavid Greenman }
2590e195446SDavid Greenman 
2600e195446SDavid Greenman /*
2610e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2620e195446SDavid Greenman  */
2630e195446SDavid Greenman void
2640e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2650e195446SDavid Greenman 	vm_offset_t kva;
2660e195446SDavid Greenman 	int count;
2670e195446SDavid Greenman {
2680e195446SDavid Greenman 	int i;
2690e195446SDavid Greenman 	vm_offset_t pa;
2700e195446SDavid Greenman 	if( bouncepages == 0) {
2710e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2720e195446SDavid Greenman 		return;
2730e195446SDavid Greenman 	}
2740e195446SDavid Greenman 	for(i = 0; i < count; i++) {
2750e195446SDavid Greenman 		pa = pmap_kextract(kva + i * NBPG);
2760e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2770e195446SDavid Greenman 	}
2780e195446SDavid Greenman 	vm_bounce_kva_free(kva, count);
2790e195446SDavid Greenman }
2800e195446SDavid Greenman 
2810e195446SDavid Greenman /*
282d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
283d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
284d5e26ef0SDavid Greenman  */
285d5e26ef0SDavid Greenman void
286d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
287d5e26ef0SDavid Greenman 	struct buf *bp;
288d5e26ef0SDavid Greenman {
289d5e26ef0SDavid Greenman 	int countvmpg;
290d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
291d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
292d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
293d5e26ef0SDavid Greenman 	vm_offset_t pa;
294d5e26ef0SDavid Greenman 	int dobounceflag = 0;
295d5e26ef0SDavid Greenman 	int bounceindex;
296d5e26ef0SDavid Greenman 	int i;
297d5e26ef0SDavid Greenman 	int s;
298d5e26ef0SDavid Greenman 
299d5e26ef0SDavid Greenman 	if (bouncepages == 0)
300d5e26ef0SDavid Greenman 		return;
301d5e26ef0SDavid Greenman 
3021561d038SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3031561d038SDavid Greenman 		printf("vm_bounce_alloc: b_bufsize(%d) < b_bcount(%d) !!!!\n",
3041561d038SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
3051561d038SDavid Greenman 		bp->b_bufsize = bp->b_bcount;
3061561d038SDavid Greenman 	}
3071561d038SDavid Greenman 
30826f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
30926f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
310d5e26ef0SDavid Greenman 
311d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
312d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
313d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
314d5e26ef0SDavid Greenman 
315d5e26ef0SDavid Greenman /*
316d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
317d5e26ef0SDavid Greenman  */
318d5e26ef0SDavid Greenman 	va = vapstart;
319d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
320ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
321d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
322d5e26ef0SDavid Greenman 			++dobounceflag;
323d5e26ef0SDavid Greenman 		va += NBPG;
324d5e26ef0SDavid Greenman 	}
325d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
326d5e26ef0SDavid Greenman 		return;
327d5e26ef0SDavid Greenman 
328d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
329d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
330d5e26ef0SDavid Greenman 
331d5e26ef0SDavid Greenman /*
332d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
333d5e26ef0SDavid Greenman  */
3341561d038SDavid Greenman 	kva = vm_bounce_kva(countvmpg*NBPG, 1);
335d5e26ef0SDavid Greenman 	va = vapstart;
336d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
337ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
338d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
339d5e26ef0SDavid Greenman 			/*
340d5e26ef0SDavid Greenman 			 * allocate a replacement page
341d5e26ef0SDavid Greenman 			 */
342d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
3436b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), bpa);
344d5e26ef0SDavid Greenman 			/*
345d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
346d5e26ef0SDavid Greenman 			 */
3471561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
3481561d038SDavid Greenman 				pmap_update();
349d5e26ef0SDavid Greenman 				bcopy((caddr_t) va, (caddr_t) kva + (NBPG * i), NBPG);
3501561d038SDavid Greenman 			}
351d5e26ef0SDavid Greenman 		} else {
352d5e26ef0SDavid Greenman 			/*
353d5e26ef0SDavid Greenman 			 * use original page
354d5e26ef0SDavid Greenman 			 */
3556b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), pa);
356d5e26ef0SDavid Greenman 		}
357d5e26ef0SDavid Greenman 		va += NBPG;
358d5e26ef0SDavid Greenman 	}
3596b4ac811SDavid Greenman 	pmap_update();
360d5e26ef0SDavid Greenman 
361d5e26ef0SDavid Greenman /*
362d5e26ef0SDavid Greenman  * flag the buffer as being bounced
363d5e26ef0SDavid Greenman  */
364d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
365d5e26ef0SDavid Greenman /*
366d5e26ef0SDavid Greenman  * save the original buffer kva
367d5e26ef0SDavid Greenman  */
36826f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
369d5e26ef0SDavid Greenman /*
370d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
371d5e26ef0SDavid Greenman  */
37226f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
373d5e26ef0SDavid Greenman 				((vm_offset_t) bp->b_savekva & (NBPG - 1)));
374d5e26ef0SDavid Greenman 	return;
375d5e26ef0SDavid Greenman }
376d5e26ef0SDavid Greenman 
377d5e26ef0SDavid Greenman /*
378d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
379d5e26ef0SDavid Greenman  */
380d5e26ef0SDavid Greenman void
381d5e26ef0SDavid Greenman vm_bounce_free(bp)
382d5e26ef0SDavid Greenman 	struct buf *bp;
383d5e26ef0SDavid Greenman {
384d5e26ef0SDavid Greenman 	int i;
385d5e26ef0SDavid Greenman 	vm_offset_t origkva, bouncekva;
386d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
387d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
388d5e26ef0SDavid Greenman 	int countbounce = 0;
389d5e26ef0SDavid Greenman 	vm_offset_t firstbouncepa = 0;
390d5e26ef0SDavid Greenman 	int firstbounceindex;
391d5e26ef0SDavid Greenman 	int countvmpg;
392d5e26ef0SDavid Greenman 	vm_offset_t bcount;
393d5e26ef0SDavid Greenman 	int s;
394d5e26ef0SDavid Greenman 
395d5e26ef0SDavid Greenman /*
396d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
397d5e26ef0SDavid Greenman  */
398d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
399d5e26ef0SDavid Greenman 		return;
400d5e26ef0SDavid Greenman 
401d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
40226f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
403d5e26ef0SDavid Greenman 
404d5e26ef0SDavid Greenman 	vastart = bouncekva;
405ac322158SDavid Greenman 	vaend = bouncekva + bp->b_bufsize;
406ac322158SDavid Greenman 	bcount = bp->b_bufsize;
407d5e26ef0SDavid Greenman 
408d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
409d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
410d5e26ef0SDavid Greenman 
411d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
412d5e26ef0SDavid Greenman 
413d5e26ef0SDavid Greenman /*
414d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
415d5e26ef0SDavid Greenman  */
416d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
417d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
418d5e26ef0SDavid Greenman 		vm_offset_t copycount;
419d5e26ef0SDavid Greenman 
420d5e26ef0SDavid Greenman 		copycount = i386_round_page(bouncekva + 1) - bouncekva;
421ed7fcbd0SDavid Greenman 		mybouncepa = pmap_kextract(i386_trunc_page(bouncekva));
422d5e26ef0SDavid Greenman 
423d5e26ef0SDavid Greenman /*
424d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
425d5e26ef0SDavid Greenman  */
426d5e26ef0SDavid Greenman 		if ((mybouncepa >= bouncepa) && (mybouncepa < bouncepaend)) {
427d5e26ef0SDavid Greenman 			if (copycount > bcount)
428d5e26ef0SDavid Greenman 				copycount = bcount;
429d5e26ef0SDavid Greenman /*
430d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
431d5e26ef0SDavid Greenman  */
432d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
433d5e26ef0SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, copycount);
434d5e26ef0SDavid Greenman /*
435d5e26ef0SDavid Greenman  * free the bounce allocation
436d5e26ef0SDavid Greenman  */
437d5e26ef0SDavid Greenman 			vm_bounce_page_free(i386_trunc_page(mybouncepa), 1);
438d5e26ef0SDavid Greenman 		}
439d5e26ef0SDavid Greenman 
440d5e26ef0SDavid Greenman 		origkva += copycount;
441d5e26ef0SDavid Greenman 		bouncekva += copycount;
442d5e26ef0SDavid Greenman 		bcount -= copycount;
443d5e26ef0SDavid Greenman 	}
444d5e26ef0SDavid Greenman 
445d5e26ef0SDavid Greenman /*
446d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
447d5e26ef0SDavid Greenman  */
44826f9a767SRodney W. Grimes 	bouncekva = i386_trunc_page((vm_offset_t) bp->b_data);
4491561d038SDavid Greenman 	vm_bounce_kva_free( bouncekva, countvmpg*NBPG, 0);
45026f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
451d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
452d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
453d5e26ef0SDavid Greenman 
454d5e26ef0SDavid Greenman 	return;
455d5e26ef0SDavid Greenman }
456d5e26ef0SDavid Greenman 
4575b81b6b3SRodney W. Grimes /*
4581561d038SDavid Greenman  * init the bounce buffer system
4591561d038SDavid Greenman  */
4601561d038SDavid Greenman void
4611561d038SDavid Greenman vm_bounce_init()
4621561d038SDavid Greenman {
4631561d038SDavid Greenman 	vm_offset_t minaddr, maxaddr;
4641561d038SDavid Greenman 
4651561d038SDavid Greenman 	kvasfreecnt = 0;
4661561d038SDavid Greenman 
4671561d038SDavid Greenman 	if (bouncepages == 0)
4681561d038SDavid Greenman 		return;
4691561d038SDavid Greenman 
4701561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
4711561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
4721561d038SDavid Greenman 
4731561d038SDavid Greenman 	if (!bounceallocarray)
4741561d038SDavid Greenman 		panic("Cannot allocate bounce resource array\n");
4751561d038SDavid Greenman 
4761561d038SDavid Greenman 	bzero(bounceallocarray, bounceallocarraysize * sizeof(long));
4771561d038SDavid Greenman 
4781561d038SDavid Greenman 
4791561d038SDavid Greenman 	bouncepa = pmap_kextract((vm_offset_t) bouncememory);
4801561d038SDavid Greenman 	bouncepaend = bouncepa + bouncepages * NBPG;
4811561d038SDavid Greenman 	bouncefree = bouncepages;
4821561d038SDavid Greenman }
4831561d038SDavid Greenman 
4841561d038SDavid Greenman 
48526f9a767SRodney W. Grimes #ifdef BROKEN_IN_44
486f690bbacSDavid Greenman static void
487f690bbacSDavid Greenman cldiskvamerge( kvanew, orig1, orig1cnt, orig2, orig2cnt)
488f690bbacSDavid Greenman 	vm_offset_t kvanew;
489f690bbacSDavid Greenman 	vm_offset_t orig1, orig1cnt;
490f690bbacSDavid Greenman 	vm_offset_t orig2, orig2cnt;
491f690bbacSDavid Greenman {
492f690bbacSDavid Greenman 	int i;
493f690bbacSDavid Greenman 	vm_offset_t pa;
494f690bbacSDavid Greenman /*
495f690bbacSDavid Greenman  * enter the transfer physical addresses into the new kva
496f690bbacSDavid Greenman  */
497f690bbacSDavid Greenman 	for(i=0;i<orig1cnt;i++) {
498f690bbacSDavid Greenman 		vm_offset_t pa;
499f690bbacSDavid Greenman 		pa = pmap_kextract((caddr_t) orig1 + i * PAGE_SIZE);
500f690bbacSDavid Greenman 		pmap_kenter(kvanew + i * PAGE_SIZE, pa);
501f690bbacSDavid Greenman 	}
502f690bbacSDavid Greenman 
503f690bbacSDavid Greenman 	for(i=0;i<orig2cnt;i++) {
504f690bbacSDavid Greenman 		vm_offset_t pa;
505f690bbacSDavid Greenman 		pa = pmap_kextract((caddr_t) orig2 + i * PAGE_SIZE);
506f690bbacSDavid Greenman 		pmap_kenter(kvanew + (i + orig1cnt) * PAGE_SIZE, pa);
507f690bbacSDavid Greenman 	}
508f690bbacSDavid Greenman 	pmap_update();
509f690bbacSDavid Greenman }
510f690bbacSDavid Greenman 
5111561d038SDavid Greenman void
5121561d038SDavid Greenman cldisksort(struct buf *dp, struct buf *bp, vm_offset_t maxio)
5131561d038SDavid Greenman {
5141561d038SDavid Greenman 	register struct buf *ap, *newbp;
5151561d038SDavid Greenman 	int i, trycount=0;
5161561d038SDavid Greenman 	vm_offset_t orig1pages, orig2pages;
5171561d038SDavid Greenman 	vm_offset_t orig1begin, orig2begin;
5181561d038SDavid Greenman 	vm_offset_t kvanew, kvaorig;
5191561d038SDavid Greenman 
5200e195446SDavid Greenman 	if( bp->b_bcount < MAXCLSTATS*PAGE_SIZE)
5210e195446SDavid Greenman 		++rqstats[bp->b_bcount/PAGE_SIZE];
5221561d038SDavid Greenman 	/*
5231561d038SDavid Greenman 	 * If nothing on the activity queue, then
5241561d038SDavid Greenman 	 * we become the only thing.
5251561d038SDavid Greenman 	 */
5261561d038SDavid Greenman 	ap = dp->b_actf;
5271561d038SDavid Greenman 	if(ap == NULL) {
5281561d038SDavid Greenman 		dp->b_actf = bp;
5291561d038SDavid Greenman 		dp->b_actl = bp;
5301561d038SDavid Greenman 		bp->av_forw = NULL;
5311561d038SDavid Greenman 		return;
5321561d038SDavid Greenman 	}
5331561d038SDavid Greenman 
5341561d038SDavid Greenman 	/*
5351561d038SDavid Greenman 	 * If we lie after the first (currently active)
5361561d038SDavid Greenman 	 * request, then we must locate the second request list
5371561d038SDavid Greenman 	 * and add ourselves to it.
5381561d038SDavid Greenman 	 */
5391561d038SDavid Greenman 
5400e195446SDavid Greenman 	if (bp->b_pblkno < ap->b_pblkno) {
5411561d038SDavid Greenman 		while (ap->av_forw) {
5421561d038SDavid Greenman 			/*
5431561d038SDavid Greenman 			 * Check for an ``inversion'' in the
5440e195446SDavid Greenman 			 * normally ascending block numbers,
5451561d038SDavid Greenman 			 * indicating the start of the second request list.
5461561d038SDavid Greenman 			 */
5470e195446SDavid Greenman 			if (ap->av_forw->b_pblkno < ap->b_pblkno) {
5481561d038SDavid Greenman 				/*
5491561d038SDavid Greenman 				 * Search the second request list
5501561d038SDavid Greenman 				 * for the first request at a larger
5510e195446SDavid Greenman 				 * block number.  We go before that;
5521561d038SDavid Greenman 				 * if there is no such request, we go at end.
5531561d038SDavid Greenman 				 */
5541561d038SDavid Greenman 				do {
5550e195446SDavid Greenman 					if (bp->b_pblkno < ap->av_forw->b_pblkno)
5561561d038SDavid Greenman 						goto insert;
5571561d038SDavid Greenman 					ap = ap->av_forw;
5581561d038SDavid Greenman 				} while (ap->av_forw);
5591561d038SDavid Greenman 				goto insert;		/* after last */
5601561d038SDavid Greenman 			}
5611561d038SDavid Greenman 			ap = ap->av_forw;
5621561d038SDavid Greenman 		}
5631561d038SDavid Greenman 		/*
5641561d038SDavid Greenman 		 * No inversions... we will go after the last, and
5651561d038SDavid Greenman 		 * be the first request in the second request list.
5661561d038SDavid Greenman 		 */
5671561d038SDavid Greenman 		goto insert;
5681561d038SDavid Greenman 	}
5691561d038SDavid Greenman 	/*
5701561d038SDavid Greenman 	 * Request is at/after the current request...
5711561d038SDavid Greenman 	 * sort in the first request list.
5721561d038SDavid Greenman 	 */
5731561d038SDavid Greenman 	while (ap->av_forw) {
5741561d038SDavid Greenman 		/*
5751561d038SDavid Greenman 		 * We want to go after the current request
5761561d038SDavid Greenman 		 * if there is an inversion after it (i.e. it is
5771561d038SDavid Greenman 		 * the end of the first request list), or if
5780e195446SDavid Greenman 		 * the next request is a larger block than our request.
5791561d038SDavid Greenman 		 */
5800e195446SDavid Greenman 		if (ap->av_forw->b_pblkno < ap->b_pblkno ||
5810e195446SDavid Greenman 		    bp->b_pblkno < ap->av_forw->b_pblkno )
5821561d038SDavid Greenman 			goto insert;
5831561d038SDavid Greenman 		ap = ap->av_forw;
5841561d038SDavid Greenman 	}
5851561d038SDavid Greenman 
5861561d038SDavid Greenman insert:
5870e195446SDavid Greenman 
5880e195446SDavid Greenman 	/*
5890e195446SDavid Greenman 	 * read clustering with new read-ahead disk drives hurts mostly, so
5900e195446SDavid Greenman 	 * we don't bother...
5910e195446SDavid Greenman 	 */
5920e195446SDavid Greenman 	if( bp->b_flags & B_READ)
5930e195446SDavid Greenman 		goto nocluster;
5941561d038SDavid Greenman 	/*
5951561d038SDavid Greenman 	 * we currently only cluster I/O transfers that are at page-aligned
5961561d038SDavid Greenman 	 * kvas and transfers that are multiples of page lengths.
5971561d038SDavid Greenman 	 */
5980e195446SDavid Greenman 	if ((bp->b_flags & B_BAD) == 0 &&
5990e195446SDavid Greenman 		((bp->b_bcount & PAGE_MASK) == 0) &&
600f690bbacSDavid Greenman 		(((vm_offset_t) bp->b_un.b_addr & PAGE_MASK) == 0)) {
6010e195446SDavid Greenman 		if( maxio > MAXCLSTATS*PAGE_SIZE)
6020e195446SDavid Greenman 			maxio = MAXCLSTATS*PAGE_SIZE;
6031561d038SDavid Greenman 		/*
6041561d038SDavid Greenman 		 * merge with previous?
6051561d038SDavid Greenman 		 * conditions:
6061561d038SDavid Greenman 		 * 	1) We reside physically immediately after the previous block.
6071561d038SDavid Greenman 		 *	2) The previous block is not first on the device queue because
6081561d038SDavid Greenman 		 *	   such a block might be active.
6091561d038SDavid Greenman 		 *  3) The mode of the two I/Os is identical.
6101561d038SDavid Greenman 		 *  4) The previous kva is page aligned and the previous transfer
6111561d038SDavid Greenman 		 *	   is a multiple of a page in length.
6121561d038SDavid Greenman 		 *	5) And the total I/O size would be below the maximum.
6131561d038SDavid Greenman 		 */
6140e195446SDavid Greenman 		if( (ap->b_pblkno + (ap->b_bcount / DEV_BSIZE) == bp->b_pblkno) &&
6151561d038SDavid Greenman 			(dp->b_actf != ap) &&
6161561d038SDavid Greenman 			((ap->b_flags & ~B_CLUSTER) == bp->b_flags) &&
6170e195446SDavid Greenman 			((ap->b_flags & B_BAD) == 0) &&
618f690bbacSDavid Greenman 			((ap->b_bcount & PAGE_MASK) == 0) &&
619f690bbacSDavid Greenman 			(((vm_offset_t) ap->b_un.b_addr & PAGE_MASK) == 0) &&
6201561d038SDavid Greenman 			(ap->b_bcount + bp->b_bcount < maxio)) {
6211561d038SDavid Greenman 
6221561d038SDavid Greenman 			orig1begin = (vm_offset_t) ap->b_un.b_addr;
6231561d038SDavid Greenman 			orig1pages = ap->b_bcount / PAGE_SIZE;
6241561d038SDavid Greenman 
6251561d038SDavid Greenman 			orig2begin = (vm_offset_t) bp->b_un.b_addr;
6261561d038SDavid Greenman 			orig2pages = bp->b_bcount / PAGE_SIZE;
6271561d038SDavid Greenman 			/*
6281561d038SDavid Greenman 			 * see if we can allocate a kva, if we cannot, the don't
6291561d038SDavid Greenman 			 * cluster.
6301561d038SDavid Greenman 			 */
6311561d038SDavid Greenman 			kvanew = vm_bounce_kva( PAGE_SIZE * (orig1pages + orig2pages), 0);
6321561d038SDavid Greenman 			if( !kvanew) {
6331561d038SDavid Greenman 				goto nocluster;
6341561d038SDavid Greenman 			}
6351561d038SDavid Greenman 
636f690bbacSDavid Greenman 
637f690bbacSDavid Greenman 			if( (ap->b_flags & B_CLUSTER) == 0) {
638f690bbacSDavid Greenman 
6391561d038SDavid Greenman 				/*
6401561d038SDavid Greenman 				 * get a physical buf pointer
6411561d038SDavid Greenman 				 */
6421561d038SDavid Greenman 				newbp = (struct buf *)trypbuf();
6431561d038SDavid Greenman 				if( !newbp) {
6441561d038SDavid Greenman 					vm_bounce_kva_free( kvanew, PAGE_SIZE * (orig1pages + orig2pages), 1);
6451561d038SDavid Greenman 					goto nocluster;
6461561d038SDavid Greenman 				}
6471561d038SDavid Greenman 
648f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
6491561d038SDavid Greenman 
6501561d038SDavid Greenman 				/*
6511561d038SDavid Greenman 				 * build the new bp to be handed off to the device
6521561d038SDavid Greenman 				 */
6531561d038SDavid Greenman 
6540e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
6551561d038SDavid Greenman 				*newbp = *ap;
6561561d038SDavid Greenman 				newbp->b_flags |= B_CLUSTER;
6571561d038SDavid Greenman 				newbp->b_un.b_addr = (caddr_t) kvanew;
6581561d038SDavid Greenman 				newbp->b_bcount += bp->b_bcount;
6591561d038SDavid Greenman 				newbp->b_bufsize = newbp->b_bcount;
6601561d038SDavid Greenman 				newbp->b_clusterf = ap;
6611561d038SDavid Greenman 				newbp->b_clusterl = bp;
6620e195446SDavid Greenman 				++clstats[newbp->b_bcount/PAGE_SIZE];
6631561d038SDavid Greenman 
6641561d038SDavid Greenman 				/*
6651561d038SDavid Greenman 				 * enter the new bp onto the device queue
6661561d038SDavid Greenman 				 */
6671561d038SDavid Greenman 				if( ap->av_forw)
6681561d038SDavid Greenman 					ap->av_forw->av_back = newbp;
6691561d038SDavid Greenman 				else
6701561d038SDavid Greenman 					dp->b_actl = newbp;
6711561d038SDavid Greenman 
6721561d038SDavid Greenman 				if( dp->b_actf != ap )
6731561d038SDavid Greenman 					ap->av_back->av_forw = newbp;
6741561d038SDavid Greenman 				else
6751561d038SDavid Greenman 					dp->b_actf = newbp;
6761561d038SDavid Greenman 
6771561d038SDavid Greenman 				/*
6781561d038SDavid Greenman 				 * enter the previous bps onto the cluster queue
6791561d038SDavid Greenman 				 */
6801561d038SDavid Greenman 				ap->av_forw = bp;
6811561d038SDavid Greenman 				bp->av_back = ap;
6821561d038SDavid Greenman 
6831561d038SDavid Greenman 				ap->av_back = NULL;
6841561d038SDavid Greenman 				bp->av_forw = NULL;
6851561d038SDavid Greenman 
6861561d038SDavid Greenman 			} else {
6871561d038SDavid Greenman 				vm_offset_t addr;
6881561d038SDavid Greenman 
689f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
6901561d038SDavid Greenman 				/*
6911561d038SDavid Greenman 				 * free the old kva
6921561d038SDavid Greenman 				 */
693f690bbacSDavid Greenman 				vm_bounce_kva_free( orig1begin, ap->b_bufsize, 0);
6940e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
6951561d038SDavid Greenman 
6961561d038SDavid Greenman 				ap->b_un.b_addr = (caddr_t) kvanew;
6971561d038SDavid Greenman 
6981561d038SDavid Greenman 				ap->b_clusterl->av_forw = bp;
6991561d038SDavid Greenman 				bp->av_forw = NULL;
7001561d038SDavid Greenman 				bp->av_back = ap->b_clusterl;
7011561d038SDavid Greenman 				ap->b_clusterl = bp;
7021561d038SDavid Greenman 
7031561d038SDavid Greenman 				ap->b_bcount += bp->b_bcount;
7041561d038SDavid Greenman 				ap->b_bufsize = ap->b_bcount;
7050e195446SDavid Greenman 				++clstats[ap->b_bcount/PAGE_SIZE];
7061561d038SDavid Greenman 			}
7071561d038SDavid Greenman 			return;
7081561d038SDavid Greenman 		/*
7091561d038SDavid Greenman 		 * merge with next?
7101561d038SDavid Greenman 		 * conditions:
7111561d038SDavid Greenman 		 * 	1) We reside physically before the next block.
7121561d038SDavid Greenman 		 *  3) The mode of the two I/Os is identical.
7131561d038SDavid Greenman 		 *  4) The next kva is page aligned and the next transfer
7141561d038SDavid Greenman 		 *	   is a multiple of a page in length.
7151561d038SDavid Greenman 		 *	5) And the total I/O size would be below the maximum.
7161561d038SDavid Greenman 		 */
7171561d038SDavid Greenman 		} else if( ap->av_forw &&
7180e195446SDavid Greenman 			(bp->b_pblkno + (bp->b_bcount / DEV_BSIZE) == ap->av_forw->b_pblkno) &&
7191561d038SDavid Greenman 			(bp->b_flags == (ap->av_forw->b_flags & ~B_CLUSTER)) &&
7200e195446SDavid Greenman 			((ap->av_forw->b_flags & B_BAD) == 0) &&
721f690bbacSDavid Greenman 			((ap->av_forw->b_bcount & PAGE_MASK) == 0) &&
722f690bbacSDavid Greenman 			(((vm_offset_t) ap->av_forw->b_un.b_addr & PAGE_MASK) == 0) &&
7231561d038SDavid Greenman 			(ap->av_forw->b_bcount + bp->b_bcount < maxio)) {
7241561d038SDavid Greenman 
7251561d038SDavid Greenman 			orig1begin = (vm_offset_t) bp->b_un.b_addr;
7261561d038SDavid Greenman 			orig1pages = bp->b_bcount / PAGE_SIZE;
7271561d038SDavid Greenman 
7281561d038SDavid Greenman 			orig2begin = (vm_offset_t) ap->av_forw->b_un.b_addr;
7291561d038SDavid Greenman 			orig2pages = ap->av_forw->b_bcount / PAGE_SIZE;
7301561d038SDavid Greenman 
7311561d038SDavid Greenman 			/*
7321561d038SDavid Greenman 			 * see if we can allocate a kva, if we cannot, the don't
7331561d038SDavid Greenman 			 * cluster.
7341561d038SDavid Greenman 			 */
7351561d038SDavid Greenman 			kvanew = vm_bounce_kva( PAGE_SIZE * (orig1pages + orig2pages), 0);
7361561d038SDavid Greenman 			if( !kvanew) {
7371561d038SDavid Greenman 				goto nocluster;
7381561d038SDavid Greenman 			}
7391561d038SDavid Greenman 
740f690bbacSDavid Greenman 			/*
741f690bbacSDavid Greenman 			 * if next isn't a cluster we need to create one
742f690bbacSDavid Greenman 			 */
743f690bbacSDavid Greenman 			if( (ap->av_forw->b_flags & B_CLUSTER) == 0) {
744f690bbacSDavid Greenman 
7451561d038SDavid Greenman 				/*
7461561d038SDavid Greenman 				 * get a physical buf pointer
7471561d038SDavid Greenman 				 */
7481561d038SDavid Greenman 				newbp = (struct buf *)trypbuf();
7491561d038SDavid Greenman 				if( !newbp) {
7501561d038SDavid Greenman 					vm_bounce_kva_free( kvanew, PAGE_SIZE * (orig1pages + orig2pages), 1);
7511561d038SDavid Greenman 					goto nocluster;
7521561d038SDavid Greenman 				}
7531561d038SDavid Greenman 
754f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
7551561d038SDavid Greenman 				ap = ap->av_forw;
7560e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
7571561d038SDavid Greenman 				*newbp = *ap;
7581561d038SDavid Greenman 				newbp->b_flags |= B_CLUSTER;
7591561d038SDavid Greenman 				newbp->b_un.b_addr = (caddr_t) kvanew;
7601561d038SDavid Greenman 				newbp->b_blkno = bp->b_blkno;
7610e195446SDavid Greenman 				newbp->b_pblkno = bp->b_pblkno;
7621561d038SDavid Greenman 				newbp->b_bcount += bp->b_bcount;
7631561d038SDavid Greenman 				newbp->b_bufsize = newbp->b_bcount;
7641561d038SDavid Greenman 				newbp->b_clusterf = bp;
7651561d038SDavid Greenman 				newbp->b_clusterl = ap;
7660e195446SDavid Greenman 				++clstats[newbp->b_bcount/PAGE_SIZE];
7671561d038SDavid Greenman 
7681561d038SDavid Greenman 				if( ap->av_forw)
7691561d038SDavid Greenman 					ap->av_forw->av_back = newbp;
7701561d038SDavid Greenman 				else
7711561d038SDavid Greenman 					dp->b_actl = newbp;
7721561d038SDavid Greenman 
7731561d038SDavid Greenman 				if( dp->b_actf != ap )
7741561d038SDavid Greenman 					ap->av_back->av_forw = newbp;
7751561d038SDavid Greenman 				else
7761561d038SDavid Greenman 					dp->b_actf = newbp;
7771561d038SDavid Greenman 
7781561d038SDavid Greenman 				bp->av_forw = ap;
7791561d038SDavid Greenman 				ap->av_back = bp;
7801561d038SDavid Greenman 
7811561d038SDavid Greenman 				bp->av_back = NULL;
7821561d038SDavid Greenman 				ap->av_forw = NULL;
7831561d038SDavid Greenman 			} else {
7841561d038SDavid Greenman 				vm_offset_t addr;
7851561d038SDavid Greenman 
786f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
7871561d038SDavid Greenman 				ap = ap->av_forw;
788f690bbacSDavid Greenman 				vm_bounce_kva_free( orig2begin, ap->b_bufsize, 0);
7891561d038SDavid Greenman 
7901561d038SDavid Greenman 				ap->b_un.b_addr = (caddr_t) kvanew;
7911561d038SDavid Greenman 				bp->av_forw = ap->b_clusterf;
7921561d038SDavid Greenman 				ap->b_clusterf->av_back = bp;
7931561d038SDavid Greenman 				ap->b_clusterf = bp;
7941561d038SDavid Greenman 				bp->av_back = NULL;
7950e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
7961561d038SDavid Greenman 
7971561d038SDavid Greenman 				ap->b_blkno = bp->b_blkno;
7980e195446SDavid Greenman 				ap->b_pblkno = bp->b_pblkno;
7991561d038SDavid Greenman 				ap->b_bcount += bp->b_bcount;
8001561d038SDavid Greenman 				ap->b_bufsize = ap->b_bcount;
8010e195446SDavid Greenman 				++clstats[ap->b_bcount/PAGE_SIZE];
8021561d038SDavid Greenman 
8031561d038SDavid Greenman 			}
8041561d038SDavid Greenman 			return;
8051561d038SDavid Greenman 		}
8061561d038SDavid Greenman 	}
8071561d038SDavid Greenman 	/*
8081561d038SDavid Greenman 	 * don't merge
8091561d038SDavid Greenman 	 */
8101561d038SDavid Greenman nocluster:
8110e195446SDavid Greenman 	++clstats[bp->b_bcount/PAGE_SIZE];
8121561d038SDavid Greenman 	bp->av_forw = ap->av_forw;
8131561d038SDavid Greenman 	if( bp->av_forw)
8141561d038SDavid Greenman 		bp->av_forw->av_back = bp;
8151561d038SDavid Greenman 	else
8161561d038SDavid Greenman 		dp->b_actl = bp;
8171561d038SDavid Greenman 
8181561d038SDavid Greenman 	ap->av_forw = bp;
8191561d038SDavid Greenman 	bp->av_back = ap;
8201561d038SDavid Greenman }
82126f9a767SRodney W. Grimes #endif
8221561d038SDavid Greenman 
823a4f7a4c9SDavid Greenman /*
824a4f7a4c9SDavid Greenman  * quick version of vm_fault
825a4f7a4c9SDavid Greenman  */
826a4f7a4c9SDavid Greenman 
827a4f7a4c9SDavid Greenman void
828a4f7a4c9SDavid Greenman vm_fault_quick( v, prot)
829a4f7a4c9SDavid Greenman 	vm_offset_t v;
830a4f7a4c9SDavid Greenman 	int prot;
831a4f7a4c9SDavid Greenman {
832a4f7a4c9SDavid Greenman 	if( (cpu_class == CPUCLASS_386) &&
833a4f7a4c9SDavid Greenman 		(prot & VM_PROT_WRITE))
834a4f7a4c9SDavid Greenman 		vm_fault(&curproc->p_vmspace->vm_map, v,
835a4f7a4c9SDavid Greenman 			VM_PROT_READ|VM_PROT_WRITE, FALSE);
836a4f7a4c9SDavid Greenman 	else if( prot & VM_PROT_WRITE)
837a4f7a4c9SDavid Greenman 		*(volatile char *)v += 0;
838a4f7a4c9SDavid Greenman 	else
839a4f7a4c9SDavid Greenman 		*(volatile char *)v;
840a4f7a4c9SDavid Greenman }
841a4f7a4c9SDavid Greenman 
8421561d038SDavid Greenman 
8431561d038SDavid Greenman /*
8445b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
8455b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
8465b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
8475b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
8485b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
8495b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
8505b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
8515b81b6b3SRodney W. Grimes  */
852381fe1aaSGarrett Wollman int
8535b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
8545b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
8555b81b6b3SRodney W. Grimes {
8565b81b6b3SRodney W. Grimes 	register struct user *up = p2->p_addr;
8575b81b6b3SRodney W. Grimes 	int foo, offset, addr, i;
8585b81b6b3SRodney W. Grimes 	extern char kstack[];
8595b81b6b3SRodney W. Grimes 	extern int mvesp();
8605b81b6b3SRodney W. Grimes 
8615b81b6b3SRodney W. Grimes 	/*
8625b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
8635b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
8645b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
8655b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
8665b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
8675b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
8685b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
8695b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
8705b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
8715b81b6b3SRodney W. Grimes 	 */
8725b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
8735b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
8745b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
8755b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
87626f9a767SRodney W. Grimes 	p2->p_md.md_regs = p1->p_md.md_regs;
8775b81b6b3SRodney W. Grimes 
8785b81b6b3SRodney W. Grimes 	/*
8795b81b6b3SRodney W. Grimes 	 * Wire top of address space of child to it's kstack.
8805b81b6b3SRodney W. Grimes 	 * First, fault in a page of pte's to map it.
8815b81b6b3SRodney W. Grimes 	 */
8827f8cb368SDavid Greenman #if 0
8835b81b6b3SRodney W. Grimes         addr = trunc_page((u_int)vtopte(kstack));
8845b81b6b3SRodney W. Grimes 	vm_map_pageable(&p2->p_vmspace->vm_map, addr, addr+NBPG, FALSE);
8855b81b6b3SRodney W. Grimes 	for (i=0; i < UPAGES; i++)
8867f8cb368SDavid Greenman 		pmap_enter(&p2->p_vmspace->vm_pmap, kstack+i*NBPG,
88726931201SDavid Greenman 			   pmap_extract(kernel_pmap, ((int)p2->p_addr)+i*NBPG),
88826931201SDavid Greenman 			   /*
88926931201SDavid Greenman 			    * The user area has to be mapped writable because
89026931201SDavid Greenman 			    * it contains the kernel stack (when CR0_WP is on
89126931201SDavid Greenman 			    * on a 486 there is no user-read/kernel-write
89226931201SDavid Greenman 			    * mode).  It is protected from user mode access
89326931201SDavid Greenman 			    * by the segment limits.
89426931201SDavid Greenman 			    */
89526931201SDavid Greenman 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
8967f8cb368SDavid Greenman #endif
8975b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
8985b81b6b3SRodney W. Grimes 
8995b81b6b3SRodney W. Grimes 	/*
9005b81b6b3SRodney W. Grimes 	 *
9015b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
9025b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
9035b81b6b3SRodney W. Grimes 	 */
9045b81b6b3SRodney W. Grimes 	if (savectx(up, 1)) {
9055b81b6b3SRodney W. Grimes 		/*
9065b81b6b3SRodney W. Grimes 		 * Return 1 in child.
9075b81b6b3SRodney W. Grimes 		 */
9085b81b6b3SRodney W. Grimes 		return (1);
9095b81b6b3SRodney W. Grimes 	}
9105b81b6b3SRodney W. Grimes 	return (0);
9115b81b6b3SRodney W. Grimes }
9125b81b6b3SRodney W. Grimes 
9135b81b6b3SRodney W. Grimes #ifdef notyet
9145b81b6b3SRodney W. Grimes /*
9155b81b6b3SRodney W. Grimes  * cpu_exit is called as the last action during exit.
9165b81b6b3SRodney W. Grimes  *
9175b81b6b3SRodney W. Grimes  * We change to an inactive address space and a "safe" stack,
9185b81b6b3SRodney W. Grimes  * passing thru an argument to the new stack. Now, safely isolated
9195b81b6b3SRodney W. Grimes  * from the resources we're shedding, we release the address space
9205b81b6b3SRodney W. Grimes  * and any remaining machine-dependent resources, including the
9215b81b6b3SRodney W. Grimes  * memory for the user structure and kernel stack.
9225b81b6b3SRodney W. Grimes  *
9235b81b6b3SRodney W. Grimes  * Next, we assign a dummy context to be written over by swtch,
9245b81b6b3SRodney W. Grimes  * calling it to send this process off to oblivion.
92526f9a767SRodney W. Grimes  * [The nullpcb allows us to minimize cost in mi_switch() by not having
9265b81b6b3SRodney W. Grimes  * a special case].
9275b81b6b3SRodney W. Grimes  */
9285b81b6b3SRodney W. Grimes struct proc *swtch_to_inactive();
92975124a8bSPaul Richards volatile void
9305b81b6b3SRodney W. Grimes cpu_exit(p)
9315b81b6b3SRodney W. Grimes 	register struct proc *p;
9325b81b6b3SRodney W. Grimes {
9335b81b6b3SRodney W. Grimes 	static struct pcb nullpcb;	/* pcb to overwrite on last swtch */
9345b81b6b3SRodney W. Grimes 
935960173b9SRodney W. Grimes #if NNPX > 0
9365b81b6b3SRodney W. Grimes 	npxexit(p);
937960173b9SRodney W. Grimes #endif	/* NNPX */
9385b81b6b3SRodney W. Grimes 
9395b81b6b3SRodney W. Grimes 	/* move to inactive space and stack, passing arg accross */
9405b81b6b3SRodney W. Grimes 	p = swtch_to_inactive(p);
9415b81b6b3SRodney W. Grimes 
9425b81b6b3SRodney W. Grimes 	/* drop per-process resources */
9435b81b6b3SRodney W. Grimes 	vmspace_free(p->p_vmspace);
9445b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
9455b81b6b3SRodney W. Grimes 
9465b81b6b3SRodney W. Grimes 	p->p_addr = (struct user *) &nullpcb;
94726f9a767SRodney W. Grimes 	mi_switch();
9485b81b6b3SRodney W. Grimes 	/* NOTREACHED */
9495b81b6b3SRodney W. Grimes }
9505b81b6b3SRodney W. Grimes #else
9517c2b54e8SNate Williams void
9525b81b6b3SRodney W. Grimes cpu_exit(p)
9535b81b6b3SRodney W. Grimes 	register struct proc *p;
9545b81b6b3SRodney W. Grimes {
9555b81b6b3SRodney W. Grimes 
956960173b9SRodney W. Grimes #if NNPX > 0
9575b81b6b3SRodney W. Grimes 	npxexit(p);
958960173b9SRodney W. Grimes #endif	/* NNPX */
95926f9a767SRodney W. Grimes 	curproc = p;
96026f9a767SRodney W. Grimes 	mi_switch();
9617c2b54e8SNate Williams 	/*
9627c2b54e8SNate Williams 	 * This is to shutup the compiler, and if swtch() failed I suppose
9637c2b54e8SNate Williams 	 * this would be a good thing.  This keeps gcc happy because panic
9647c2b54e8SNate Williams 	 * is a volatile void function as well.
9657c2b54e8SNate Williams 	 */
9667c2b54e8SNate Williams 	panic("cpu_exit");
9675b81b6b3SRodney W. Grimes }
9685b81b6b3SRodney W. Grimes 
969381fe1aaSGarrett Wollman void
9707f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
9717f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
9727f8cb368SDavid Greenman 	extern char kstack[];
9735b81b6b3SRodney W. Grimes 
9745b81b6b3SRodney W. Grimes 	/* drop per-process resources */
9757f8cb368SDavid Greenman  	pmap_remove(vm_map_pmap(kernel_map), (vm_offset_t) p->p_addr,
9767f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
9775b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
9787f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
9795b81b6b3SRodney W. Grimes }
9805b81b6b3SRodney W. Grimes #endif
9815b81b6b3SRodney W. Grimes 
9825b81b6b3SRodney W. Grimes /*
98326f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
98426f9a767SRodney W. Grimes  */
98526f9a767SRodney W. Grimes int
98626f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
98726f9a767SRodney W. Grimes 	struct proc *p;
98826f9a767SRodney W. Grimes 	struct vnode *vp;
98926f9a767SRodney W. Grimes 	struct ucred *cred;
99026f9a767SRodney W. Grimes {
99126f9a767SRodney W. Grimes 
99226f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
99326f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
99426f9a767SRodney W. Grimes 	    p));
99526f9a767SRodney W. Grimes }
99626f9a767SRodney W. Grimes 
99726f9a767SRodney W. Grimes /*
9985b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
9995b81b6b3SRodney W. Grimes  */
1000381fe1aaSGarrett Wollman void
10015b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
10025b81b6b3SRodney W. Grimes 	u_short *pte;
10035b81b6b3SRodney W. Grimes 	caddr_t vaddr;
10045b81b6b3SRodney W. Grimes {
10055b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
10065b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
10075b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
10085b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
10095b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
10105b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
10115b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
10125b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
10135b81b6b3SRodney W. Grimes }
10145b81b6b3SRodney W. Grimes 
10155b81b6b3SRodney W. Grimes /*
101626f9a767SRodney W. Grimes  * Move pages from one kernel virtual address to another.
101726f9a767SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
101826f9a767SRodney W. Grimes  * and size must be a multiple of CLSIZE.
101926f9a767SRodney W. Grimes  */
102026f9a767SRodney W. Grimes 
102126f9a767SRodney W. Grimes /*
102226f9a767SRodney W. Grimes  * Move pages from one kernel virtual address to another.
102326f9a767SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
102426f9a767SRodney W. Grimes  * and size must be a multiple of CLSIZE.
102526f9a767SRodney W. Grimes  */
102626f9a767SRodney W. Grimes 
102726f9a767SRodney W. Grimes void
102826f9a767SRodney W. Grimes pagemove(from, to, size)
102926f9a767SRodney W. Grimes 	register caddr_t from, to;
103026f9a767SRodney W. Grimes 	int size;
103126f9a767SRodney W. Grimes {
103226f9a767SRodney W. Grimes 	register vm_offset_t pa;
103326f9a767SRodney W. Grimes 
103426f9a767SRodney W. Grimes 	if (size & CLOFSET)
103526f9a767SRodney W. Grimes 		panic("pagemove");
103626f9a767SRodney W. Grimes 	while (size > 0) {
103726f9a767SRodney W. Grimes 		pa = pmap_kextract((vm_offset_t)from);
103826f9a767SRodney W. Grimes 		if (pa == 0)
103926f9a767SRodney W. Grimes 			panic("pagemove 2");
104026f9a767SRodney W. Grimes 		if (pmap_kextract((vm_offset_t)to) != 0)
104126f9a767SRodney W. Grimes 			panic("pagemove 3");
104226f9a767SRodney W. Grimes 		pmap_remove(kernel_pmap,
104326f9a767SRodney W. Grimes 			    (vm_offset_t)from, (vm_offset_t)from + PAGE_SIZE);
104426f9a767SRodney W. Grimes 		pmap_kenter( (vm_offset_t)to, pa);
104526f9a767SRodney W. Grimes 		from += PAGE_SIZE;
104626f9a767SRodney W. Grimes 		to += PAGE_SIZE;
104726f9a767SRodney W. Grimes 		size -= PAGE_SIZE;
104826f9a767SRodney W. Grimes 	}
104926f9a767SRodney W. Grimes 	pmap_update();
105026f9a767SRodney W. Grimes }
105126f9a767SRodney W. Grimes 
105226f9a767SRodney W. Grimes /*
10535b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
10545b81b6b3SRodney W. Grimes  */
1055aaf08d94SGarrett Wollman u_long
10567f8cb368SDavid Greenman kvtop(void *addr)
10575b81b6b3SRodney W. Grimes {
10585b81b6b3SRodney W. Grimes 	vm_offset_t va;
10595b81b6b3SRodney W. Grimes 
1060ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
10615b81b6b3SRodney W. Grimes 	if (va == 0)
10625b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
10637f8cb368SDavid Greenman 	return((int)va);
10645b81b6b3SRodney W. Grimes }
10655b81b6b3SRodney W. Grimes 
10665b81b6b3SRodney W. Grimes extern vm_map_t phys_map;
10675b81b6b3SRodney W. Grimes 
10685b81b6b3SRodney W. Grimes /*
1069ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
10705b81b6b3SRodney W. Grimes  *
1071ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
1072ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
1073ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
10745b81b6b3SRodney W. Grimes  */
1075381fe1aaSGarrett Wollman void
10765b81b6b3SRodney W. Grimes vmapbuf(bp)
10775b81b6b3SRodney W. Grimes 	register struct buf *bp;
10785b81b6b3SRodney W. Grimes {
10795b81b6b3SRodney W. Grimes 	register int npf;
10805b81b6b3SRodney W. Grimes 	register caddr_t addr;
10815b81b6b3SRodney W. Grimes 	int off;
10825b81b6b3SRodney W. Grimes 	vm_offset_t kva;
108326f9a767SRodney W. Grimes 	vm_offset_t pa, lastv, v;
10845b81b6b3SRodney W. Grimes 
108526f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
10865b81b6b3SRodney W. Grimes 		panic("vmapbuf");
108726f9a767SRodney W. Grimes 
108826f9a767SRodney W. Grimes 	lastv = 0;
108926f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
109026f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
109126f9a767SRodney W. Grimes 		addr += PAGE_SIZE) {
109226f9a767SRodney W. Grimes 
109326f9a767SRodney W. Grimes /*
109426f9a767SRodney W. Grimes  * make sure that the pde is valid and held
109526f9a767SRodney W. Grimes  */
109626f9a767SRodney W. Grimes 		v = trunc_page(((vm_offset_t)vtopte(addr)));
109726f9a767SRodney W. Grimes 		if (v != lastv) {
109826f9a767SRodney W. Grimes 			vm_fault_quick(v, VM_PROT_READ);
109926f9a767SRodney W. Grimes 			pa = pmap_extract(&curproc->p_vmspace->vm_pmap, v);
110026f9a767SRodney W. Grimes 			vm_page_hold(PHYS_TO_VM_PAGE(pa));
110126f9a767SRodney W. Grimes 			lastv = v;
110226f9a767SRodney W. Grimes 		}
110326f9a767SRodney W. Grimes 
110426f9a767SRodney W. Grimes /*
110526f9a767SRodney W. Grimes  * do the vm_fault if needed, do the copy-on-write thing when
110626f9a767SRodney W. Grimes  * reading stuff off device into memory.
110726f9a767SRodney W. Grimes  */
110826f9a767SRodney W. Grimes 		vm_fault_quick(addr,
110926f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
111026f9a767SRodney W. Grimes 		pa = pmap_extract(&curproc->p_vmspace->vm_pmap, (vm_offset_t) addr);
111126f9a767SRodney W. Grimes /*
111226f9a767SRodney W. Grimes  * hold the data page
111326f9a767SRodney W. Grimes  */
111426f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
111526f9a767SRodney W. Grimes 	}
111626f9a767SRodney W. Grimes 
11175b81b6b3SRodney W. Grimes 	addr = bp->b_saveaddr = bp->b_un.b_addr;
11185b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
1119ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + off));
11205b81b6b3SRodney W. Grimes 	kva = kmem_alloc_wait(phys_map, ctob(npf));
11215b81b6b3SRodney W. Grimes 	bp->b_un.b_addr = (caddr_t) (kva + off);
11225b81b6b3SRodney W. Grimes 	while (npf--) {
112326f9a767SRodney W. Grimes 		pa = pmap_extract(&curproc->p_vmspace->vm_pmap, (vm_offset_t)addr);
11245b81b6b3SRodney W. Grimes 		if (pa == 0)
11255b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
11266b4ac811SDavid Greenman 		pmap_kenter(kva, trunc_page(pa));
11275b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
11285b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
11295b81b6b3SRodney W. Grimes 	}
11306b4ac811SDavid Greenman 	pmap_update();
11315b81b6b3SRodney W. Grimes }
11325b81b6b3SRodney W. Grimes 
11335b81b6b3SRodney W. Grimes /*
11345b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
11355b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
11365b81b6b3SRodney W. Grimes  */
1137381fe1aaSGarrett Wollman void
11385b81b6b3SRodney W. Grimes vunmapbuf(bp)
11395b81b6b3SRodney W. Grimes 	register struct buf *bp;
11405b81b6b3SRodney W. Grimes {
11415b81b6b3SRodney W. Grimes 	register int npf;
11425b81b6b3SRodney W. Grimes 	register caddr_t addr = bp->b_un.b_addr;
114326f9a767SRodney W. Grimes 	vm_offset_t kva,va,v,lastv,pa;
11445b81b6b3SRodney W. Grimes 
11455b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
11465b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
1147ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + ((int)addr & PGOFSET)));
11485b81b6b3SRodney W. Grimes 	kva = (vm_offset_t)((int)addr & ~PGOFSET);
11495b81b6b3SRodney W. Grimes 	kmem_free_wakeup(phys_map, kva, ctob(npf));
11505b81b6b3SRodney W. Grimes 	bp->b_un.b_addr = bp->b_saveaddr;
11515b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
115226f9a767SRodney W. Grimes 
115326f9a767SRodney W. Grimes 
115426f9a767SRodney W. Grimes /*
115526f9a767SRodney W. Grimes  * unhold the pde, and data pages
115626f9a767SRodney W. Grimes  */
115726f9a767SRodney W. Grimes 	lastv = 0;
115826f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
115926f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
116026f9a767SRodney W. Grimes 		addr += NBPG) {
116126f9a767SRodney W. Grimes 
116226f9a767SRodney W. Grimes 	/*
116326f9a767SRodney W. Grimes 	 * release the data page
116426f9a767SRodney W. Grimes 	 */
116526f9a767SRodney W. Grimes 		pa = pmap_extract(&curproc->p_vmspace->vm_pmap, (vm_offset_t) addr);
116626f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
116726f9a767SRodney W. Grimes 
116826f9a767SRodney W. Grimes 	/*
116926f9a767SRodney W. Grimes 	 * and unhold the page table
117026f9a767SRodney W. Grimes 	 */
117126f9a767SRodney W. Grimes 		v = trunc_page(((vm_offset_t)vtopte(addr)));
117226f9a767SRodney W. Grimes 		if (v != lastv) {
117326f9a767SRodney W. Grimes 			pa = pmap_extract(&curproc->p_vmspace->vm_pmap, v);
117426f9a767SRodney W. Grimes 			vm_page_unhold(PHYS_TO_VM_PAGE(pa));
117526f9a767SRodney W. Grimes 			lastv = v;
117626f9a767SRodney W. Grimes 		}
117726f9a767SRodney W. Grimes 	}
11785b81b6b3SRodney W. Grimes }
11795b81b6b3SRodney W. Grimes 
11805b81b6b3SRodney W. Grimes /*
11815b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
11825b81b6b3SRodney W. Grimes  */
11837f8cb368SDavid Greenman void
11845b81b6b3SRodney W. Grimes cpu_reset() {
11855b81b6b3SRodney W. Grimes 
11865b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
11875b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
11885b81b6b3SRodney W. Grimes 
11895b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
11905b81b6b3SRodney W. Grimes 	tlbflush();
11915b81b6b3SRodney W. Grimes 	/* NOTREACHED */
11927f8cb368SDavid Greenman 	while(1);
11935b81b6b3SRodney W. Grimes }
1194b9d60b3fSDavid Greenman 
1195b9d60b3fSDavid Greenman /*
1196b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
119729360eb0SDavid Greenman  *	chunks of SGROWSIZ.
1198b9d60b3fSDavid Greenman  */
1199b9d60b3fSDavid Greenman int
1200b9d60b3fSDavid Greenman grow(p, sp)
1201b9d60b3fSDavid Greenman 	struct proc *p;
120226f9a767SRodney W. Grimes 	u_int sp;
1203b9d60b3fSDavid Greenman {
1204b9d60b3fSDavid Greenman 	unsigned int nss;
1205b9d60b3fSDavid Greenman 	caddr_t v;
1206b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
1207b9d60b3fSDavid Greenman 
1208b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
1209b9d60b3fSDavid Greenman 	    return (1);
1210b9d60b3fSDavid Greenman 
1211b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
1212b9d60b3fSDavid Greenman 
1213b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
1214b9d60b3fSDavid Greenman 		return (0);
1215b9d60b3fSDavid Greenman 
1216b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
121729360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
1218b9d60b3fSDavid Greenman 		int grow_amount;
1219b9d60b3fSDavid Greenman 		/*
1220b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
1221b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
1222b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
1223b9d60b3fSDavid Greenman 		 * is expensive).
1224b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
122529360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
1226b9d60b3fSDavid Greenman 		 */
122729360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
1228b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
122929360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
1230b9d60b3fSDavid Greenman 		/*
123129360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
1232b9d60b3fSDavid Greenman 		 * just extend to the maximum size
1233b9d60b3fSDavid Greenman 		 */
1234b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
1235b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
1236b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
1237b9d60b3fSDavid Greenman 		}
1238b9d60b3fSDavid Greenman 		if (vm_allocate(&vm->vm_map, (vm_offset_t *)&v,
1239b9d60b3fSDavid Greenman 		    grow_amount, FALSE) != KERN_SUCCESS) {
1240b9d60b3fSDavid Greenman 			return (0);
1241b9d60b3fSDavid Greenman 		}
1242b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
1243b9d60b3fSDavid Greenman 	}
1244b9d60b3fSDavid Greenman 
1245b9d60b3fSDavid Greenman 	return (1);
1246b9d60b3fSDavid Greenman }
1247