xref: /freebsd/sys/i386/i386/vm_machdep.c (revision a4f7a4c990bd3bfebfa4176b3f2c12d28fb3578a)
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"
455b81b6b3SRodney W. Grimes #include "param.h"
465b81b6b3SRodney W. Grimes #include "systm.h"
475b81b6b3SRodney W. Grimes #include "proc.h"
485b81b6b3SRodney W. Grimes #include "malloc.h"
495b81b6b3SRodney W. Grimes #include "buf.h"
505b81b6b3SRodney W. Grimes #include "user.h"
515b81b6b3SRodney W. Grimes 
525b81b6b3SRodney W. Grimes #include "../include/cpu.h"
535b81b6b3SRodney W. Grimes 
545b81b6b3SRodney W. Grimes #include "vm/vm.h"
555b81b6b3SRodney W. Grimes #include "vm/vm_kern.h"
565b81b6b3SRodney W. Grimes 
571561d038SDavid Greenman #define b_cylin b_resid
581561d038SDavid Greenman 
590e195446SDavid Greenman #define MAXCLSTATS 256
600e195446SDavid Greenman int clstats[MAXCLSTATS];
610e195446SDavid Greenman int rqstats[MAXCLSTATS];
620e195446SDavid Greenman 
630e195446SDavid Greenman 
64d5e26ef0SDavid Greenman #ifndef NOBOUNCE
65d5e26ef0SDavid Greenman 
66d5e26ef0SDavid Greenman caddr_t		bouncememory;
67d5e26ef0SDavid Greenman vm_offset_t	bouncepa, bouncepaend;
68ed7fcbd0SDavid Greenman int		bouncepages, bpwait;
691561d038SDavid Greenman vm_map_t	io_map;
70d5e26ef0SDavid Greenman int		bmwait, bmfreeing;
71d5e26ef0SDavid Greenman 
72ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
73d5e26ef0SDavid Greenman int		bounceallocarraysize;
74d5e26ef0SDavid Greenman unsigned	*bounceallocarray;
75d5e26ef0SDavid Greenman int		bouncefree;
76d5e26ef0SDavid Greenman 
77d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
781561d038SDavid Greenman #define MAXBKVA 1024
79d5e26ef0SDavid Greenman 
80d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
81d5e26ef0SDavid Greenman struct kvasfree {
82d5e26ef0SDavid Greenman 	vm_offset_t addr;
83d5e26ef0SDavid Greenman 	vm_offset_t size;
84d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
85d5e26ef0SDavid Greenman 
86d5e26ef0SDavid Greenman int		kvasfreecnt;
87d5e26ef0SDavid Greenman 
881561d038SDavid Greenman vm_offset_t vm_bounce_kva();
89d5e26ef0SDavid Greenman /*
90d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
91d5e26ef0SDavid Greenman  * (only 1 inplemented now)
92d5e26ef0SDavid Greenman  */
93d5e26ef0SDavid Greenman vm_offset_t
94d5e26ef0SDavid Greenman vm_bounce_page_find(count)
95d5e26ef0SDavid Greenman 	int count;
96d5e26ef0SDavid Greenman {
97d5e26ef0SDavid Greenman 	int bit;
98d5e26ef0SDavid Greenman 	int s,i;
99d5e26ef0SDavid Greenman 
100d5e26ef0SDavid Greenman 	if (count != 1)
101d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
102d5e26ef0SDavid Greenman 
103d5e26ef0SDavid Greenman 	s = splbio();
104d5e26ef0SDavid Greenman retry:
105d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
106d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
107d5e26ef0SDavid Greenman 			if (bit = ffs(~bounceallocarray[i])) {
108d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
109d5e26ef0SDavid Greenman 				bouncefree -= count;
110d5e26ef0SDavid Greenman 				splx(s);
111ed7fcbd0SDavid Greenman 				return bouncepa + (i * BITS_IN_UNSIGNED + (bit - 1)) * NBPG;
112d5e26ef0SDavid Greenman 			}
113d5e26ef0SDavid Greenman 		}
114d5e26ef0SDavid Greenman 	}
115ed7fcbd0SDavid Greenman 	bpwait = 1;
116d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
117d5e26ef0SDavid Greenman 	goto retry;
118d5e26ef0SDavid Greenman }
119d5e26ef0SDavid Greenman 
1201561d038SDavid Greenman void
1211561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1221561d038SDavid Greenman 	vm_offset_t addr;
1231561d038SDavid Greenman 	vm_offset_t size;
1241561d038SDavid Greenman 	int now;
1251561d038SDavid Greenman {
1261561d038SDavid Greenman 	int s = splbio();
1271561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
1281561d038SDavid Greenman 	kvaf[kvasfreecnt++].size = size;
1290e195446SDavid Greenman 	if( now) {
1300e195446SDavid Greenman 		/*
1310e195446SDavid Greenman 		 * this will do wakeups
1320e195446SDavid Greenman 		 */
1331561d038SDavid Greenman 		vm_bounce_kva(0,0);
1340e195446SDavid Greenman 	} else {
1350e195446SDavid Greenman 		if (bmwait) {
1360e195446SDavid Greenman 		/*
1370e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1380e195446SDavid Greenman 		 */
1391561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1400e195446SDavid Greenman 			bmwait = 0;
1410e195446SDavid Greenman 		}
1420e195446SDavid Greenman 	}
1431561d038SDavid Greenman 	splx(s);
1441561d038SDavid Greenman }
1451561d038SDavid Greenman 
146d5e26ef0SDavid Greenman /*
147d5e26ef0SDavid Greenman  * free count bounce buffer pages
148d5e26ef0SDavid Greenman  */
149d5e26ef0SDavid Greenman void
150d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
151d5e26ef0SDavid Greenman 	vm_offset_t pa;
152d5e26ef0SDavid Greenman 	int count;
153d5e26ef0SDavid Greenman {
154d5e26ef0SDavid Greenman 	int allocindex;
155d5e26ef0SDavid Greenman 	int index;
156d5e26ef0SDavid Greenman 	int bit;
157d5e26ef0SDavid Greenman 
158d5e26ef0SDavid Greenman 	if (count != 1)
159d5e26ef0SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!\n");
160d5e26ef0SDavid Greenman 
161d5e26ef0SDavid Greenman 	index = (pa - bouncepa) / NBPG;
162d5e26ef0SDavid Greenman 
163d5e26ef0SDavid Greenman 	if ((index < 0) || (index >= bouncepages))
164d5e26ef0SDavid Greenman 		panic("vm_bounce_page_free -- bad index\n");
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
1821561d038SDavid Greenman vm_bounce_kva(count, waitok)
183d5e26ef0SDavid Greenman 	int count;
1841561d038SDavid Greenman 	int waitok;
185d5e26ef0SDavid Greenman {
186d5e26ef0SDavid Greenman 	int tofree;
187d5e26ef0SDavid Greenman 	int i;
188d5e26ef0SDavid Greenman 	int startfree;
1896b4ac811SDavid Greenman 	vm_offset_t kva = 0;
190d5e26ef0SDavid Greenman 	int s = splbio();
1911561d038SDavid Greenman 	int size = count;
192d5e26ef0SDavid Greenman 	startfree = 0;
193d5e26ef0SDavid Greenman more:
194d5e26ef0SDavid Greenman 	if (!bmfreeing && (tofree = kvasfreecnt)) {
195d5e26ef0SDavid Greenman 		bmfreeing = 1;
196d5e26ef0SDavid Greenman 		for (i = startfree; i < kvasfreecnt; i++) {
1976b4ac811SDavid Greenman 			/*
1986b4ac811SDavid Greenman 			 * if we have a kva of the right size, no sense
1996b4ac811SDavid Greenman 			 * in freeing/reallocating...
2006b4ac811SDavid Greenman 			 * might affect fragmentation short term, but
2011561d038SDavid Greenman 			 * as long as the amount of io_map is
2026b4ac811SDavid Greenman 			 * significantly more than the maximum transfer
2036b4ac811SDavid Greenman 			 * size, I don't think that it is a problem.
2046b4ac811SDavid Greenman 			 */
205d5e26ef0SDavid Greenman 			pmap_remove(kernel_pmap,
206d5e26ef0SDavid Greenman 				kvaf[i].addr, kvaf[i].addr + kvaf[i].size);
2071561d038SDavid Greenman 			if( size && !kva && kvaf[i].size == size) {
2086b4ac811SDavid Greenman 				kva = kvaf[i].addr;
2096b4ac811SDavid Greenman 			} else {
2101561d038SDavid Greenman 				kmem_free_wakeup(io_map, kvaf[i].addr,
211d5e26ef0SDavid Greenman 					kvaf[i].size);
212d5e26ef0SDavid Greenman 			}
2136b4ac811SDavid Greenman 		}
214d5e26ef0SDavid Greenman 		if (kvasfreecnt != tofree) {
215d5e26ef0SDavid Greenman 			startfree = i;
216d5e26ef0SDavid Greenman 			bmfreeing = 0;
217d5e26ef0SDavid Greenman 			goto more;
218d5e26ef0SDavid Greenman 		}
219d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
220d5e26ef0SDavid Greenman 		bmfreeing = 0;
221d5e26ef0SDavid Greenman 	}
222d5e26ef0SDavid Greenman 
2231561d038SDavid Greenman 	if( size == 0) {
2241561d038SDavid Greenman 		splx(s);
2251561d038SDavid Greenman 		return NULL;
2261561d038SDavid Greenman 	}
2271561d038SDavid Greenman 
2281561d038SDavid Greenman 	if (!kva && !(kva = kmem_alloc_pageable(io_map, size))) {
2291561d038SDavid Greenman 		if( !waitok) {
2301561d038SDavid Greenman 			splx(s);
2311561d038SDavid Greenman 			return NULL;
2321561d038SDavid Greenman 		}
233d5e26ef0SDavid Greenman 		bmwait = 1;
2341561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
235d5e26ef0SDavid Greenman 		goto more;
236d5e26ef0SDavid Greenman 	}
237d5e26ef0SDavid Greenman 	splx(s);
238d5e26ef0SDavid Greenman 
239d5e26ef0SDavid Greenman 	return kva;
240d5e26ef0SDavid Greenman }
241d5e26ef0SDavid Greenman 
242d5e26ef0SDavid Greenman /*
2430e195446SDavid Greenman  * same as vm_bounce_kva -- but really allocate
2440e195446SDavid Greenman  */
2450e195446SDavid Greenman vm_offset_t
2460e195446SDavid Greenman vm_bounce_kva_alloc(count)
2470e195446SDavid Greenman int count;
2480e195446SDavid Greenman {
2490e195446SDavid Greenman 	int i;
2500e195446SDavid Greenman 	vm_offset_t kva;
2510e195446SDavid Greenman 	vm_offset_t pa;
2520e195446SDavid Greenman 	if( bouncepages == 0) {
2530e195446SDavid Greenman 		kva = (vm_offset_t) malloc(count*NBPG, M_TEMP, M_WAITOK);
2540e195446SDavid Greenman 		return kva;
2550e195446SDavid Greenman 	}
2560e195446SDavid Greenman 	kva = vm_bounce_kva(count, 1);
2570e195446SDavid Greenman 	for(i=0;i<count;i++) {
2580e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
2590e195446SDavid Greenman 		pmap_kenter(kva + i * NBPG, pa);
2600e195446SDavid Greenman 	}
2610e195446SDavid Greenman 	return kva;
2620e195446SDavid Greenman }
2630e195446SDavid Greenman 
2640e195446SDavid Greenman /*
2650e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2660e195446SDavid Greenman  */
2670e195446SDavid Greenman void
2680e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2690e195446SDavid Greenman 	vm_offset_t kva;
2700e195446SDavid Greenman 	int count;
2710e195446SDavid Greenman {
2720e195446SDavid Greenman 	int i;
2730e195446SDavid Greenman 	vm_offset_t pa;
2740e195446SDavid Greenman 	if( bouncepages == 0) {
2750e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2760e195446SDavid Greenman 		return;
2770e195446SDavid Greenman 	}
2780e195446SDavid Greenman 	for(i = 0; i < count; i++) {
2790e195446SDavid Greenman 		pa = pmap_kextract(kva + i * NBPG);
2800e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2810e195446SDavid Greenman 	}
2820e195446SDavid Greenman 	vm_bounce_kva_free(kva, count);
2830e195446SDavid Greenman }
2840e195446SDavid Greenman 
2850e195446SDavid Greenman /*
286d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
287d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
288d5e26ef0SDavid Greenman  */
289d5e26ef0SDavid Greenman void
290d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
291d5e26ef0SDavid Greenman 	struct buf *bp;
292d5e26ef0SDavid Greenman {
293d5e26ef0SDavid Greenman 	int countvmpg;
294d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
295d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
296d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
297d5e26ef0SDavid Greenman 	vm_offset_t pa;
298d5e26ef0SDavid Greenman 	int dobounceflag = 0;
299d5e26ef0SDavid Greenman 	int bounceindex;
300d5e26ef0SDavid Greenman 	int i;
301d5e26ef0SDavid Greenman 	int s;
302d5e26ef0SDavid Greenman 
303d5e26ef0SDavid Greenman 	if (bouncepages == 0)
304d5e26ef0SDavid Greenman 		return;
305d5e26ef0SDavid Greenman 
3061561d038SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3071561d038SDavid Greenman 		printf("vm_bounce_alloc: b_bufsize(%d) < b_bcount(%d) !!!!\n",
3081561d038SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
3091561d038SDavid Greenman 		bp->b_bufsize = bp->b_bcount;
3101561d038SDavid Greenman 	}
3111561d038SDavid Greenman 
312d5e26ef0SDavid Greenman 	vastart = (vm_offset_t) bp->b_un.b_addr;
313ac322158SDavid Greenman 	vaend = (vm_offset_t) bp->b_un.b_addr + bp->b_bufsize;
314d5e26ef0SDavid Greenman 
315d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
316d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
317d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
318d5e26ef0SDavid Greenman 
319d5e26ef0SDavid Greenman /*
320d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
321d5e26ef0SDavid Greenman  */
322d5e26ef0SDavid Greenman 	va = vapstart;
323d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
324ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
325d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
326d5e26ef0SDavid Greenman 			++dobounceflag;
327d5e26ef0SDavid Greenman 		va += NBPG;
328d5e26ef0SDavid Greenman 	}
329d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
330d5e26ef0SDavid Greenman 		return;
331d5e26ef0SDavid Greenman 
332d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
333d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
334d5e26ef0SDavid Greenman 
335d5e26ef0SDavid Greenman /*
336d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
337d5e26ef0SDavid Greenman  */
3381561d038SDavid Greenman 	kva = vm_bounce_kva(countvmpg*NBPG, 1);
339d5e26ef0SDavid Greenman 	va = vapstart;
340d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
341ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
342d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
343d5e26ef0SDavid Greenman 			/*
344d5e26ef0SDavid Greenman 			 * allocate a replacement page
345d5e26ef0SDavid Greenman 			 */
346d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
3476b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), bpa);
348d5e26ef0SDavid Greenman 			/*
349d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
350d5e26ef0SDavid Greenman 			 */
3511561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
3521561d038SDavid Greenman 				pmap_update();
353d5e26ef0SDavid Greenman 				bcopy((caddr_t) va, (caddr_t) kva + (NBPG * i), NBPG);
3541561d038SDavid Greenman 			}
355d5e26ef0SDavid Greenman 		} else {
356d5e26ef0SDavid Greenman 			/*
357d5e26ef0SDavid Greenman 			 * use original page
358d5e26ef0SDavid Greenman 			 */
3596b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), pa);
360d5e26ef0SDavid Greenman 		}
361d5e26ef0SDavid Greenman 		va += NBPG;
362d5e26ef0SDavid Greenman 	}
3636b4ac811SDavid Greenman 	pmap_update();
364d5e26ef0SDavid Greenman 
365d5e26ef0SDavid Greenman /*
366d5e26ef0SDavid Greenman  * flag the buffer as being bounced
367d5e26ef0SDavid Greenman  */
368d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
369d5e26ef0SDavid Greenman /*
370d5e26ef0SDavid Greenman  * save the original buffer kva
371d5e26ef0SDavid Greenman  */
372d5e26ef0SDavid Greenman 	bp->b_savekva = bp->b_un.b_addr;
373d5e26ef0SDavid Greenman /*
374d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
375d5e26ef0SDavid Greenman  */
376d5e26ef0SDavid Greenman 	bp->b_un.b_addr = (caddr_t) (((vm_offset_t) kva) |
377d5e26ef0SDavid Greenman 				((vm_offset_t) bp->b_savekva & (NBPG - 1)));
378d5e26ef0SDavid Greenman 	return;
379d5e26ef0SDavid Greenman }
380d5e26ef0SDavid Greenman 
381d5e26ef0SDavid Greenman /*
382d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
383d5e26ef0SDavid Greenman  */
384d5e26ef0SDavid Greenman void
385d5e26ef0SDavid Greenman vm_bounce_free(bp)
386d5e26ef0SDavid Greenman 	struct buf *bp;
387d5e26ef0SDavid Greenman {
388d5e26ef0SDavid Greenman 	int i;
389d5e26ef0SDavid Greenman 	vm_offset_t origkva, bouncekva;
390d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
391d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
392d5e26ef0SDavid Greenman 	int countbounce = 0;
393d5e26ef0SDavid Greenman 	vm_offset_t firstbouncepa = 0;
394d5e26ef0SDavid Greenman 	int firstbounceindex;
395d5e26ef0SDavid Greenman 	int countvmpg;
396d5e26ef0SDavid Greenman 	vm_offset_t bcount;
397d5e26ef0SDavid Greenman 	int s;
398d5e26ef0SDavid Greenman 
399d5e26ef0SDavid Greenman /*
400d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
401d5e26ef0SDavid Greenman  */
402d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
403d5e26ef0SDavid Greenman 		return;
404d5e26ef0SDavid Greenman 
405d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
406d5e26ef0SDavid Greenman 	bouncekva = (vm_offset_t) bp->b_un.b_addr;
407d5e26ef0SDavid Greenman 
408d5e26ef0SDavid Greenman 	vastart = bouncekva;
409ac322158SDavid Greenman 	vaend = bouncekva + bp->b_bufsize;
410ac322158SDavid Greenman 	bcount = bp->b_bufsize;
411d5e26ef0SDavid Greenman 
412d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
413d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
414d5e26ef0SDavid Greenman 
415d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
416d5e26ef0SDavid Greenman 
417d5e26ef0SDavid Greenman /*
418d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
419d5e26ef0SDavid Greenman  */
420d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
421d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
422d5e26ef0SDavid Greenman 		vm_offset_t copycount;
423d5e26ef0SDavid Greenman 
424d5e26ef0SDavid Greenman 		copycount = i386_round_page(bouncekva + 1) - bouncekva;
425ed7fcbd0SDavid Greenman 		mybouncepa = pmap_kextract(i386_trunc_page(bouncekva));
426d5e26ef0SDavid Greenman 
427d5e26ef0SDavid Greenman /*
428d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
429d5e26ef0SDavid Greenman  */
430d5e26ef0SDavid Greenman 		if ((mybouncepa >= bouncepa) && (mybouncepa < bouncepaend)) {
431d5e26ef0SDavid Greenman 			if (copycount > bcount)
432d5e26ef0SDavid Greenman 				copycount = bcount;
433d5e26ef0SDavid Greenman /*
434d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
435d5e26ef0SDavid Greenman  */
436d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
437d5e26ef0SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, copycount);
438d5e26ef0SDavid Greenman /*
439d5e26ef0SDavid Greenman  * free the bounce allocation
440d5e26ef0SDavid Greenman  */
441d5e26ef0SDavid Greenman 			vm_bounce_page_free(i386_trunc_page(mybouncepa), 1);
442d5e26ef0SDavid Greenman 		}
443d5e26ef0SDavid Greenman 
444d5e26ef0SDavid Greenman 		origkva += copycount;
445d5e26ef0SDavid Greenman 		bouncekva += copycount;
446d5e26ef0SDavid Greenman 		bcount -= copycount;
447d5e26ef0SDavid Greenman 	}
448d5e26ef0SDavid Greenman 
449d5e26ef0SDavid Greenman /*
450d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
451d5e26ef0SDavid Greenman  */
452d5e26ef0SDavid Greenman 	bouncekva = i386_trunc_page((vm_offset_t) bp->b_un.b_addr);
4531561d038SDavid Greenman 	vm_bounce_kva_free( bouncekva, countvmpg*NBPG, 0);
454d5e26ef0SDavid Greenman 	bp->b_un.b_addr = bp->b_savekva;
455d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
456d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
457d5e26ef0SDavid Greenman 
458d5e26ef0SDavid Greenman 	return;
459d5e26ef0SDavid Greenman }
460d5e26ef0SDavid Greenman 
461d5e26ef0SDavid Greenman #endif /* NOBOUNCE */
462d5e26ef0SDavid Greenman 
4635b81b6b3SRodney W. Grimes /*
4641561d038SDavid Greenman  * init the bounce buffer system
4651561d038SDavid Greenman  */
4661561d038SDavid Greenman void
4671561d038SDavid Greenman vm_bounce_init()
4681561d038SDavid Greenman {
4691561d038SDavid Greenman 	vm_offset_t minaddr, maxaddr;
4701561d038SDavid Greenman 
4711561d038SDavid Greenman 	io_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, MAXBKVA * NBPG, FALSE);
4721561d038SDavid Greenman 	kvasfreecnt = 0;
4731561d038SDavid Greenman 
4741561d038SDavid Greenman #ifndef NOBOUNCE
4751561d038SDavid Greenman 	if (bouncepages == 0)
4761561d038SDavid Greenman 		return;
4771561d038SDavid Greenman 
4781561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
4791561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
4801561d038SDavid Greenman 
4811561d038SDavid Greenman 	if (!bounceallocarray)
4821561d038SDavid Greenman 		panic("Cannot allocate bounce resource array\n");
4831561d038SDavid Greenman 
4841561d038SDavid Greenman 	bzero(bounceallocarray, bounceallocarraysize * sizeof(long));
4851561d038SDavid Greenman 
4861561d038SDavid Greenman 
4871561d038SDavid Greenman 	bouncepa = pmap_kextract((vm_offset_t) bouncememory);
4881561d038SDavid Greenman 	bouncepaend = bouncepa + bouncepages * NBPG;
4891561d038SDavid Greenman 	bouncefree = bouncepages;
4901561d038SDavid Greenman #endif
4911561d038SDavid Greenman 
4921561d038SDavid Greenman }
4931561d038SDavid Greenman 
4941561d038SDavid Greenman 
495f690bbacSDavid Greenman static void
496f690bbacSDavid Greenman cldiskvamerge( kvanew, orig1, orig1cnt, orig2, orig2cnt)
497f690bbacSDavid Greenman 	vm_offset_t kvanew;
498f690bbacSDavid Greenman 	vm_offset_t orig1, orig1cnt;
499f690bbacSDavid Greenman 	vm_offset_t orig2, orig2cnt;
500f690bbacSDavid Greenman {
501f690bbacSDavid Greenman 	int i;
502f690bbacSDavid Greenman 	vm_offset_t pa;
503f690bbacSDavid Greenman /*
504f690bbacSDavid Greenman  * enter the transfer physical addresses into the new kva
505f690bbacSDavid Greenman  */
506f690bbacSDavid Greenman 	for(i=0;i<orig1cnt;i++) {
507f690bbacSDavid Greenman 		vm_offset_t pa;
508f690bbacSDavid Greenman 		pa = pmap_kextract((caddr_t) orig1 + i * PAGE_SIZE);
509f690bbacSDavid Greenman 		pmap_kenter(kvanew + i * PAGE_SIZE, pa);
510f690bbacSDavid Greenman 	}
511f690bbacSDavid Greenman 
512f690bbacSDavid Greenman 	for(i=0;i<orig2cnt;i++) {
513f690bbacSDavid Greenman 		vm_offset_t pa;
514f690bbacSDavid Greenman 		pa = pmap_kextract((caddr_t) orig2 + i * PAGE_SIZE);
515f690bbacSDavid Greenman 		pmap_kenter(kvanew + (i + orig1cnt) * PAGE_SIZE, pa);
516f690bbacSDavid Greenman 	}
517f690bbacSDavid Greenman 	pmap_update();
518f690bbacSDavid Greenman }
519f690bbacSDavid Greenman 
5201561d038SDavid Greenman void
5211561d038SDavid Greenman cldisksort(struct buf *dp, struct buf *bp, vm_offset_t maxio)
5221561d038SDavid Greenman {
5231561d038SDavid Greenman 	register struct buf *ap, *newbp;
5241561d038SDavid Greenman 	int i, trycount=0;
5251561d038SDavid Greenman 	vm_offset_t orig1pages, orig2pages;
5261561d038SDavid Greenman 	vm_offset_t orig1begin, orig2begin;
5271561d038SDavid Greenman 	vm_offset_t kvanew, kvaorig;
5281561d038SDavid Greenman 
5290e195446SDavid Greenman 	if( bp->b_bcount < MAXCLSTATS*PAGE_SIZE)
5300e195446SDavid Greenman 		++rqstats[bp->b_bcount/PAGE_SIZE];
5311561d038SDavid Greenman 	/*
5321561d038SDavid Greenman 	 * If nothing on the activity queue, then
5331561d038SDavid Greenman 	 * we become the only thing.
5341561d038SDavid Greenman 	 */
5351561d038SDavid Greenman 	ap = dp->b_actf;
5361561d038SDavid Greenman 	if(ap == NULL) {
5371561d038SDavid Greenman 		dp->b_actf = bp;
5381561d038SDavid Greenman 		dp->b_actl = bp;
5391561d038SDavid Greenman 		bp->av_forw = NULL;
5401561d038SDavid Greenman 		return;
5411561d038SDavid Greenman 	}
5421561d038SDavid Greenman 
5431561d038SDavid Greenman 	/*
5441561d038SDavid Greenman 	 * If we lie after the first (currently active)
5451561d038SDavid Greenman 	 * request, then we must locate the second request list
5461561d038SDavid Greenman 	 * and add ourselves to it.
5471561d038SDavid Greenman 	 */
5481561d038SDavid Greenman 
5490e195446SDavid Greenman 	if (bp->b_pblkno < ap->b_pblkno) {
5501561d038SDavid Greenman 		while (ap->av_forw) {
5511561d038SDavid Greenman 			/*
5521561d038SDavid Greenman 			 * Check for an ``inversion'' in the
5530e195446SDavid Greenman 			 * normally ascending block numbers,
5541561d038SDavid Greenman 			 * indicating the start of the second request list.
5551561d038SDavid Greenman 			 */
5560e195446SDavid Greenman 			if (ap->av_forw->b_pblkno < ap->b_pblkno) {
5571561d038SDavid Greenman 				/*
5581561d038SDavid Greenman 				 * Search the second request list
5591561d038SDavid Greenman 				 * for the first request at a larger
5600e195446SDavid Greenman 				 * block number.  We go before that;
5611561d038SDavid Greenman 				 * if there is no such request, we go at end.
5621561d038SDavid Greenman 				 */
5631561d038SDavid Greenman 				do {
5640e195446SDavid Greenman 					if (bp->b_pblkno < ap->av_forw->b_pblkno)
5651561d038SDavid Greenman 						goto insert;
5661561d038SDavid Greenman 					ap = ap->av_forw;
5671561d038SDavid Greenman 				} while (ap->av_forw);
5681561d038SDavid Greenman 				goto insert;		/* after last */
5691561d038SDavid Greenman 			}
5701561d038SDavid Greenman 			ap = ap->av_forw;
5711561d038SDavid Greenman 		}
5721561d038SDavid Greenman 		/*
5731561d038SDavid Greenman 		 * No inversions... we will go after the last, and
5741561d038SDavid Greenman 		 * be the first request in the second request list.
5751561d038SDavid Greenman 		 */
5761561d038SDavid Greenman 		goto insert;
5771561d038SDavid Greenman 	}
5781561d038SDavid Greenman 	/*
5791561d038SDavid Greenman 	 * Request is at/after the current request...
5801561d038SDavid Greenman 	 * sort in the first request list.
5811561d038SDavid Greenman 	 */
5821561d038SDavid Greenman 	while (ap->av_forw) {
5831561d038SDavid Greenman 		/*
5841561d038SDavid Greenman 		 * We want to go after the current request
5851561d038SDavid Greenman 		 * if there is an inversion after it (i.e. it is
5861561d038SDavid Greenman 		 * the end of the first request list), or if
5870e195446SDavid Greenman 		 * the next request is a larger block than our request.
5881561d038SDavid Greenman 		 */
5890e195446SDavid Greenman 		if (ap->av_forw->b_pblkno < ap->b_pblkno ||
5900e195446SDavid Greenman 		    bp->b_pblkno < ap->av_forw->b_pblkno )
5911561d038SDavid Greenman 			goto insert;
5921561d038SDavid Greenman 		ap = ap->av_forw;
5931561d038SDavid Greenman 	}
5941561d038SDavid Greenman 
5951561d038SDavid Greenman insert:
5960e195446SDavid Greenman 
5970e195446SDavid Greenman 	/*
5980e195446SDavid Greenman 	 * read clustering with new read-ahead disk drives hurts mostly, so
5990e195446SDavid Greenman 	 * we don't bother...
6000e195446SDavid Greenman 	 */
6010e195446SDavid Greenman 	if( bp->b_flags & B_READ)
6020e195446SDavid Greenman 		goto nocluster;
6031561d038SDavid Greenman 	/*
6041561d038SDavid Greenman 	 * we currently only cluster I/O transfers that are at page-aligned
6051561d038SDavid Greenman 	 * kvas and transfers that are multiples of page lengths.
6061561d038SDavid Greenman 	 */
6070e195446SDavid Greenman 	if ((bp->b_flags & B_BAD) == 0 &&
6080e195446SDavid Greenman 		((bp->b_bcount & PAGE_MASK) == 0) &&
609f690bbacSDavid Greenman 		(((vm_offset_t) bp->b_un.b_addr & PAGE_MASK) == 0)) {
6100e195446SDavid Greenman 		if( maxio > MAXCLSTATS*PAGE_SIZE)
6110e195446SDavid Greenman 			maxio = MAXCLSTATS*PAGE_SIZE;
6121561d038SDavid Greenman 		/*
6131561d038SDavid Greenman 		 * merge with previous?
6141561d038SDavid Greenman 		 * conditions:
6151561d038SDavid Greenman 		 * 	1) We reside physically immediately after the previous block.
6161561d038SDavid Greenman 		 *	2) The previous block is not first on the device queue because
6171561d038SDavid Greenman 		 *	   such a block might be active.
6181561d038SDavid Greenman 		 *  3) The mode of the two I/Os is identical.
6191561d038SDavid Greenman 		 *  4) The previous kva is page aligned and the previous transfer
6201561d038SDavid Greenman 		 *	   is a multiple of a page in length.
6211561d038SDavid Greenman 		 *	5) And the total I/O size would be below the maximum.
6221561d038SDavid Greenman 		 */
6230e195446SDavid Greenman 		if( (ap->b_pblkno + (ap->b_bcount / DEV_BSIZE) == bp->b_pblkno) &&
6241561d038SDavid Greenman 			(dp->b_actf != ap) &&
6251561d038SDavid Greenman 			((ap->b_flags & ~B_CLUSTER) == bp->b_flags) &&
6260e195446SDavid Greenman 			((ap->b_flags & B_BAD) == 0) &&
627f690bbacSDavid Greenman 			((ap->b_bcount & PAGE_MASK) == 0) &&
628f690bbacSDavid Greenman 			(((vm_offset_t) ap->b_un.b_addr & PAGE_MASK) == 0) &&
6291561d038SDavid Greenman 			(ap->b_bcount + bp->b_bcount < maxio)) {
6301561d038SDavid Greenman 
6311561d038SDavid Greenman 			orig1begin = (vm_offset_t) ap->b_un.b_addr;
6321561d038SDavid Greenman 			orig1pages = ap->b_bcount / PAGE_SIZE;
6331561d038SDavid Greenman 
6341561d038SDavid Greenman 			orig2begin = (vm_offset_t) bp->b_un.b_addr;
6351561d038SDavid Greenman 			orig2pages = bp->b_bcount / PAGE_SIZE;
6361561d038SDavid Greenman 			/*
6371561d038SDavid Greenman 			 * see if we can allocate a kva, if we cannot, the don't
6381561d038SDavid Greenman 			 * cluster.
6391561d038SDavid Greenman 			 */
6401561d038SDavid Greenman 			kvanew = vm_bounce_kva( PAGE_SIZE * (orig1pages + orig2pages), 0);
6411561d038SDavid Greenman 			if( !kvanew) {
6421561d038SDavid Greenman 				goto nocluster;
6431561d038SDavid Greenman 			}
6441561d038SDavid Greenman 
645f690bbacSDavid Greenman 
646f690bbacSDavid Greenman 			if( (ap->b_flags & B_CLUSTER) == 0) {
647f690bbacSDavid Greenman 
6481561d038SDavid Greenman 				/*
6491561d038SDavid Greenman 				 * get a physical buf pointer
6501561d038SDavid Greenman 				 */
6511561d038SDavid Greenman 				newbp = (struct buf *)trypbuf();
6521561d038SDavid Greenman 				if( !newbp) {
6531561d038SDavid Greenman 					vm_bounce_kva_free( kvanew, PAGE_SIZE * (orig1pages + orig2pages), 1);
6541561d038SDavid Greenman 					goto nocluster;
6551561d038SDavid Greenman 				}
6561561d038SDavid Greenman 
657f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
6581561d038SDavid Greenman 
6591561d038SDavid Greenman 				/*
6601561d038SDavid Greenman 				 * build the new bp to be handed off to the device
6611561d038SDavid Greenman 				 */
6621561d038SDavid Greenman 
6630e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
6641561d038SDavid Greenman 				*newbp = *ap;
6651561d038SDavid Greenman 				newbp->b_flags |= B_CLUSTER;
6661561d038SDavid Greenman 				newbp->b_un.b_addr = (caddr_t) kvanew;
6671561d038SDavid Greenman 				newbp->b_bcount += bp->b_bcount;
6681561d038SDavid Greenman 				newbp->b_bufsize = newbp->b_bcount;
6691561d038SDavid Greenman 				newbp->b_clusterf = ap;
6701561d038SDavid Greenman 				newbp->b_clusterl = bp;
6710e195446SDavid Greenman 				++clstats[newbp->b_bcount/PAGE_SIZE];
6721561d038SDavid Greenman 
6731561d038SDavid Greenman 				/*
6741561d038SDavid Greenman 				 * enter the new bp onto the device queue
6751561d038SDavid Greenman 				 */
6761561d038SDavid Greenman 				if( ap->av_forw)
6771561d038SDavid Greenman 					ap->av_forw->av_back = newbp;
6781561d038SDavid Greenman 				else
6791561d038SDavid Greenman 					dp->b_actl = newbp;
6801561d038SDavid Greenman 
6811561d038SDavid Greenman 				if( dp->b_actf != ap )
6821561d038SDavid Greenman 					ap->av_back->av_forw = newbp;
6831561d038SDavid Greenman 				else
6841561d038SDavid Greenman 					dp->b_actf = newbp;
6851561d038SDavid Greenman 
6861561d038SDavid Greenman 				/*
6871561d038SDavid Greenman 				 * enter the previous bps onto the cluster queue
6881561d038SDavid Greenman 				 */
6891561d038SDavid Greenman 				ap->av_forw = bp;
6901561d038SDavid Greenman 				bp->av_back = ap;
6911561d038SDavid Greenman 
6921561d038SDavid Greenman 				ap->av_back = NULL;
6931561d038SDavid Greenman 				bp->av_forw = NULL;
6941561d038SDavid Greenman 
6951561d038SDavid Greenman 			} else {
6961561d038SDavid Greenman 				vm_offset_t addr;
6971561d038SDavid Greenman 
698f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
6991561d038SDavid Greenman 				/*
7001561d038SDavid Greenman 				 * free the old kva
7011561d038SDavid Greenman 				 */
702f690bbacSDavid Greenman 				vm_bounce_kva_free( orig1begin, ap->b_bufsize, 0);
7030e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
7041561d038SDavid Greenman 
7051561d038SDavid Greenman 				ap->b_un.b_addr = (caddr_t) kvanew;
7061561d038SDavid Greenman 
7071561d038SDavid Greenman 				ap->b_clusterl->av_forw = bp;
7081561d038SDavid Greenman 				bp->av_forw = NULL;
7091561d038SDavid Greenman 				bp->av_back = ap->b_clusterl;
7101561d038SDavid Greenman 				ap->b_clusterl = bp;
7111561d038SDavid Greenman 
7121561d038SDavid Greenman 				ap->b_bcount += bp->b_bcount;
7131561d038SDavid Greenman 				ap->b_bufsize = ap->b_bcount;
7140e195446SDavid Greenman 				++clstats[ap->b_bcount/PAGE_SIZE];
7151561d038SDavid Greenman 			}
7161561d038SDavid Greenman 			return;
7171561d038SDavid Greenman 		/*
7181561d038SDavid Greenman 		 * merge with next?
7191561d038SDavid Greenman 		 * conditions:
7201561d038SDavid Greenman 		 * 	1) We reside physically before the next block.
7211561d038SDavid Greenman 		 *  3) The mode of the two I/Os is identical.
7221561d038SDavid Greenman 		 *  4) The next kva is page aligned and the next transfer
7231561d038SDavid Greenman 		 *	   is a multiple of a page in length.
7241561d038SDavid Greenman 		 *	5) And the total I/O size would be below the maximum.
7251561d038SDavid Greenman 		 */
7261561d038SDavid Greenman 		} else if( ap->av_forw &&
7270e195446SDavid Greenman 			(bp->b_pblkno + (bp->b_bcount / DEV_BSIZE) == ap->av_forw->b_pblkno) &&
7281561d038SDavid Greenman 			(bp->b_flags == (ap->av_forw->b_flags & ~B_CLUSTER)) &&
7290e195446SDavid Greenman 			((ap->av_forw->b_flags & B_BAD) == 0) &&
730f690bbacSDavid Greenman 			((ap->av_forw->b_bcount & PAGE_MASK) == 0) &&
731f690bbacSDavid Greenman 			(((vm_offset_t) ap->av_forw->b_un.b_addr & PAGE_MASK) == 0) &&
7321561d038SDavid Greenman 			(ap->av_forw->b_bcount + bp->b_bcount < maxio)) {
7331561d038SDavid Greenman 
7341561d038SDavid Greenman 			orig1begin = (vm_offset_t) bp->b_un.b_addr;
7351561d038SDavid Greenman 			orig1pages = bp->b_bcount / PAGE_SIZE;
7361561d038SDavid Greenman 
7371561d038SDavid Greenman 			orig2begin = (vm_offset_t) ap->av_forw->b_un.b_addr;
7381561d038SDavid Greenman 			orig2pages = ap->av_forw->b_bcount / PAGE_SIZE;
7391561d038SDavid Greenman 
7401561d038SDavid Greenman 			/*
7411561d038SDavid Greenman 			 * see if we can allocate a kva, if we cannot, the don't
7421561d038SDavid Greenman 			 * cluster.
7431561d038SDavid Greenman 			 */
7441561d038SDavid Greenman 			kvanew = vm_bounce_kva( PAGE_SIZE * (orig1pages + orig2pages), 0);
7451561d038SDavid Greenman 			if( !kvanew) {
7461561d038SDavid Greenman 				goto nocluster;
7471561d038SDavid Greenman 			}
7481561d038SDavid Greenman 
749f690bbacSDavid Greenman 			/*
750f690bbacSDavid Greenman 			 * if next isn't a cluster we need to create one
751f690bbacSDavid Greenman 			 */
752f690bbacSDavid Greenman 			if( (ap->av_forw->b_flags & B_CLUSTER) == 0) {
753f690bbacSDavid Greenman 
7541561d038SDavid Greenman 				/*
7551561d038SDavid Greenman 				 * get a physical buf pointer
7561561d038SDavid Greenman 				 */
7571561d038SDavid Greenman 				newbp = (struct buf *)trypbuf();
7581561d038SDavid Greenman 				if( !newbp) {
7591561d038SDavid Greenman 					vm_bounce_kva_free( kvanew, PAGE_SIZE * (orig1pages + orig2pages), 1);
7601561d038SDavid Greenman 					goto nocluster;
7611561d038SDavid Greenman 				}
7621561d038SDavid Greenman 
763f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
7641561d038SDavid Greenman 				ap = ap->av_forw;
7650e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
7661561d038SDavid Greenman 				*newbp = *ap;
7671561d038SDavid Greenman 				newbp->b_flags |= B_CLUSTER;
7681561d038SDavid Greenman 				newbp->b_un.b_addr = (caddr_t) kvanew;
7691561d038SDavid Greenman 				newbp->b_blkno = bp->b_blkno;
7700e195446SDavid Greenman 				newbp->b_pblkno = bp->b_pblkno;
7711561d038SDavid Greenman 				newbp->b_bcount += bp->b_bcount;
7721561d038SDavid Greenman 				newbp->b_bufsize = newbp->b_bcount;
7731561d038SDavid Greenman 				newbp->b_clusterf = bp;
7741561d038SDavid Greenman 				newbp->b_clusterl = ap;
7750e195446SDavid Greenman 				++clstats[newbp->b_bcount/PAGE_SIZE];
7761561d038SDavid Greenman 
7771561d038SDavid Greenman 				if( ap->av_forw)
7781561d038SDavid Greenman 					ap->av_forw->av_back = newbp;
7791561d038SDavid Greenman 				else
7801561d038SDavid Greenman 					dp->b_actl = newbp;
7811561d038SDavid Greenman 
7821561d038SDavid Greenman 				if( dp->b_actf != ap )
7831561d038SDavid Greenman 					ap->av_back->av_forw = newbp;
7841561d038SDavid Greenman 				else
7851561d038SDavid Greenman 					dp->b_actf = newbp;
7861561d038SDavid Greenman 
7871561d038SDavid Greenman 				bp->av_forw = ap;
7881561d038SDavid Greenman 				ap->av_back = bp;
7891561d038SDavid Greenman 
7901561d038SDavid Greenman 				bp->av_back = NULL;
7911561d038SDavid Greenman 				ap->av_forw = NULL;
7921561d038SDavid Greenman 			} else {
7931561d038SDavid Greenman 				vm_offset_t addr;
7941561d038SDavid Greenman 
795f690bbacSDavid Greenman 				cldiskvamerge( kvanew, orig1begin, orig1pages, orig2begin, orig2pages);
7961561d038SDavid Greenman 				ap = ap->av_forw;
797f690bbacSDavid Greenman 				vm_bounce_kva_free( orig2begin, ap->b_bufsize, 0);
7981561d038SDavid Greenman 
7991561d038SDavid Greenman 				ap->b_un.b_addr = (caddr_t) kvanew;
8001561d038SDavid Greenman 				bp->av_forw = ap->b_clusterf;
8011561d038SDavid Greenman 				ap->b_clusterf->av_back = bp;
8021561d038SDavid Greenman 				ap->b_clusterf = bp;
8031561d038SDavid Greenman 				bp->av_back = NULL;
8040e195446SDavid Greenman 				--clstats[ap->b_bcount/PAGE_SIZE];
8051561d038SDavid Greenman 
8061561d038SDavid Greenman 				ap->b_blkno = bp->b_blkno;
8070e195446SDavid Greenman 				ap->b_pblkno = bp->b_pblkno;
8081561d038SDavid Greenman 				ap->b_bcount += bp->b_bcount;
8091561d038SDavid Greenman 				ap->b_bufsize = ap->b_bcount;
8100e195446SDavid Greenman 				++clstats[ap->b_bcount/PAGE_SIZE];
8111561d038SDavid Greenman 
8121561d038SDavid Greenman 			}
8131561d038SDavid Greenman 			return;
8141561d038SDavid Greenman 		}
8151561d038SDavid Greenman 	}
8161561d038SDavid Greenman 	/*
8171561d038SDavid Greenman 	 * don't merge
8181561d038SDavid Greenman 	 */
8191561d038SDavid Greenman nocluster:
8200e195446SDavid Greenman 	++clstats[bp->b_bcount/PAGE_SIZE];
8211561d038SDavid Greenman 	bp->av_forw = ap->av_forw;
8221561d038SDavid Greenman 	if( bp->av_forw)
8231561d038SDavid Greenman 		bp->av_forw->av_back = bp;
8241561d038SDavid Greenman 	else
8251561d038SDavid Greenman 		dp->b_actl = bp;
8261561d038SDavid Greenman 
8271561d038SDavid Greenman 	ap->av_forw = bp;
8281561d038SDavid Greenman 	bp->av_back = ap;
8291561d038SDavid Greenman }
8301561d038SDavid Greenman 
831a4f7a4c9SDavid Greenman /*
832a4f7a4c9SDavid Greenman  * quick version of vm_fault
833a4f7a4c9SDavid Greenman  */
834a4f7a4c9SDavid Greenman 
835a4f7a4c9SDavid Greenman void
836a4f7a4c9SDavid Greenman vm_fault_quick( v, prot)
837a4f7a4c9SDavid Greenman 	vm_offset_t v;
838a4f7a4c9SDavid Greenman 	int prot;
839a4f7a4c9SDavid Greenman {
840a4f7a4c9SDavid Greenman 	if( (cpu_class == CPUCLASS_386) &&
841a4f7a4c9SDavid Greenman 		(prot & VM_PROT_WRITE))
842a4f7a4c9SDavid Greenman 		vm_fault(&curproc->p_vmspace->vm_map, v,
843a4f7a4c9SDavid Greenman 			VM_PROT_READ|VM_PROT_WRITE, FALSE);
844a4f7a4c9SDavid Greenman 	else if( prot & VM_PROT_WRITE)
845a4f7a4c9SDavid Greenman 		*(volatile char *)v += 0;
846a4f7a4c9SDavid Greenman 	else
847a4f7a4c9SDavid Greenman 		*(volatile char *)v;
848a4f7a4c9SDavid Greenman }
849a4f7a4c9SDavid Greenman 
8501561d038SDavid Greenman 
8511561d038SDavid Greenman /*
8525b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
8535b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
8545b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
8555b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
8565b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
8575b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
8585b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
8595b81b6b3SRodney W. Grimes  */
860381fe1aaSGarrett Wollman int
8615b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
8625b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
8635b81b6b3SRodney W. Grimes {
8645b81b6b3SRodney W. Grimes 	register struct user *up = p2->p_addr;
8655b81b6b3SRodney W. Grimes 	int foo, offset, addr, i;
8665b81b6b3SRodney W. Grimes 	extern char kstack[];
8675b81b6b3SRodney W. Grimes 	extern int mvesp();
8685b81b6b3SRodney W. Grimes 
8695b81b6b3SRodney W. Grimes 	/*
8705b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
8715b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
8725b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
8735b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
8745b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
8755b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
8765b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
8775b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
8785b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
8795b81b6b3SRodney W. Grimes 	 */
8805b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
8815b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
8825b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
8835b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
8845b81b6b3SRodney W. Grimes 	p2->p_regs = p1->p_regs;
8855b81b6b3SRodney W. Grimes 
8865b81b6b3SRodney W. Grimes 	/*
8875b81b6b3SRodney W. Grimes 	 * Wire top of address space of child to it's kstack.
8885b81b6b3SRodney W. Grimes 	 * First, fault in a page of pte's to map it.
8895b81b6b3SRodney W. Grimes 	 */
8907f8cb368SDavid Greenman #if 0
8915b81b6b3SRodney W. Grimes         addr = trunc_page((u_int)vtopte(kstack));
8925b81b6b3SRodney W. Grimes 	vm_map_pageable(&p2->p_vmspace->vm_map, addr, addr+NBPG, FALSE);
8935b81b6b3SRodney W. Grimes 	for (i=0; i < UPAGES; i++)
8947f8cb368SDavid Greenman 		pmap_enter(&p2->p_vmspace->vm_pmap, kstack+i*NBPG,
89526931201SDavid Greenman 			   pmap_extract(kernel_pmap, ((int)p2->p_addr)+i*NBPG),
89626931201SDavid Greenman 			   /*
89726931201SDavid Greenman 			    * The user area has to be mapped writable because
89826931201SDavid Greenman 			    * it contains the kernel stack (when CR0_WP is on
89926931201SDavid Greenman 			    * on a 486 there is no user-read/kernel-write
90026931201SDavid Greenman 			    * mode).  It is protected from user mode access
90126931201SDavid Greenman 			    * by the segment limits.
90226931201SDavid Greenman 			    */
90326931201SDavid Greenman 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
9047f8cb368SDavid Greenman #endif
9055b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
9065b81b6b3SRodney W. Grimes 
9075b81b6b3SRodney W. Grimes 	/*
9085b81b6b3SRodney W. Grimes 	 *
9095b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
9105b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
9115b81b6b3SRodney W. Grimes 	 */
9125b81b6b3SRodney W. Grimes 	if (savectx(up, 1)) {
9135b81b6b3SRodney W. Grimes 		/*
9145b81b6b3SRodney W. Grimes 		 * Return 1 in child.
9155b81b6b3SRodney W. Grimes 		 */
9165b81b6b3SRodney W. Grimes 		return (1);
9175b81b6b3SRodney W. Grimes 	}
9185b81b6b3SRodney W. Grimes 	return (0);
9195b81b6b3SRodney W. Grimes }
9205b81b6b3SRodney W. Grimes 
9215b81b6b3SRodney W. Grimes #ifdef notyet
9225b81b6b3SRodney W. Grimes /*
9235b81b6b3SRodney W. Grimes  * cpu_exit is called as the last action during exit.
9245b81b6b3SRodney W. Grimes  *
9255b81b6b3SRodney W. Grimes  * We change to an inactive address space and a "safe" stack,
9265b81b6b3SRodney W. Grimes  * passing thru an argument to the new stack. Now, safely isolated
9275b81b6b3SRodney W. Grimes  * from the resources we're shedding, we release the address space
9285b81b6b3SRodney W. Grimes  * and any remaining machine-dependent resources, including the
9295b81b6b3SRodney W. Grimes  * memory for the user structure and kernel stack.
9305b81b6b3SRodney W. Grimes  *
9315b81b6b3SRodney W. Grimes  * Next, we assign a dummy context to be written over by swtch,
9325b81b6b3SRodney W. Grimes  * calling it to send this process off to oblivion.
9335b81b6b3SRodney W. Grimes  * [The nullpcb allows us to minimize cost in swtch() by not having
9345b81b6b3SRodney W. Grimes  * a special case].
9355b81b6b3SRodney W. Grimes  */
9365b81b6b3SRodney W. Grimes struct proc *swtch_to_inactive();
93775124a8bSPaul Richards volatile void
9385b81b6b3SRodney W. Grimes cpu_exit(p)
9395b81b6b3SRodney W. Grimes 	register struct proc *p;
9405b81b6b3SRodney W. Grimes {
9415b81b6b3SRodney W. Grimes 	static struct pcb nullpcb;	/* pcb to overwrite on last swtch */
9425b81b6b3SRodney W. Grimes 
943960173b9SRodney W. Grimes #if NNPX > 0
9445b81b6b3SRodney W. Grimes 	npxexit(p);
945960173b9SRodney W. Grimes #endif	/* NNPX */
9465b81b6b3SRodney W. Grimes 
9475b81b6b3SRodney W. Grimes 	/* move to inactive space and stack, passing arg accross */
9485b81b6b3SRodney W. Grimes 	p = swtch_to_inactive(p);
9495b81b6b3SRodney W. Grimes 
9505b81b6b3SRodney W. Grimes 	/* drop per-process resources */
9515b81b6b3SRodney W. Grimes 	vmspace_free(p->p_vmspace);
9525b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
9535b81b6b3SRodney W. Grimes 
9545b81b6b3SRodney W. Grimes 	p->p_addr = (struct user *) &nullpcb;
9555b81b6b3SRodney W. Grimes 	splclock();
9565b81b6b3SRodney W. Grimes 	swtch();
9575b81b6b3SRodney W. Grimes 	/* NOTREACHED */
9585b81b6b3SRodney W. Grimes }
9595b81b6b3SRodney W. Grimes #else
9607c2b54e8SNate Williams void
9615b81b6b3SRodney W. Grimes cpu_exit(p)
9625b81b6b3SRodney W. Grimes 	register struct proc *p;
9635b81b6b3SRodney W. Grimes {
9645b81b6b3SRodney W. Grimes 
965960173b9SRodney W. Grimes #if NNPX > 0
9665b81b6b3SRodney W. Grimes 	npxexit(p);
967960173b9SRodney W. Grimes #endif	/* NNPX */
9685b81b6b3SRodney W. Grimes 	splclock();
9697f8cb368SDavid Greenman 	curproc = 0;
9705b81b6b3SRodney W. Grimes 	swtch();
9717c2b54e8SNate Williams 	/*
9727c2b54e8SNate Williams 	 * This is to shutup the compiler, and if swtch() failed I suppose
9737c2b54e8SNate Williams 	 * this would be a good thing.  This keeps gcc happy because panic
9747c2b54e8SNate Williams 	 * is a volatile void function as well.
9757c2b54e8SNate Williams 	 */
9767c2b54e8SNate Williams 	panic("cpu_exit");
9775b81b6b3SRodney W. Grimes }
9785b81b6b3SRodney W. Grimes 
979381fe1aaSGarrett Wollman void
9807f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
9817f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
9827f8cb368SDavid Greenman 	extern char kstack[];
9835b81b6b3SRodney W. Grimes 
9845b81b6b3SRodney W. Grimes 	/* drop per-process resources */
9857f8cb368SDavid Greenman  	pmap_remove(vm_map_pmap(kernel_map), (vm_offset_t) p->p_addr,
9867f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
9875b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
9887f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
9895b81b6b3SRodney W. Grimes }
9905b81b6b3SRodney W. Grimes #endif
9915b81b6b3SRodney W. Grimes 
9925b81b6b3SRodney W. Grimes /*
9935b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
9945b81b6b3SRodney W. Grimes  */
995381fe1aaSGarrett Wollman void
9965b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
9975b81b6b3SRodney W. Grimes 	u_short *pte;
9985b81b6b3SRodney W. Grimes 	caddr_t vaddr;
9995b81b6b3SRodney W. Grimes {
10005b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
10015b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
10025b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
10035b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
10045b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
10055b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
10065b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
10075b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
10085b81b6b3SRodney W. Grimes }
10095b81b6b3SRodney W. Grimes 
10105b81b6b3SRodney W. Grimes /*
10115b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
10125b81b6b3SRodney W. Grimes  */
1013aaf08d94SGarrett Wollman u_long
10147f8cb368SDavid Greenman kvtop(void *addr)
10155b81b6b3SRodney W. Grimes {
10165b81b6b3SRodney W. Grimes 	vm_offset_t va;
10175b81b6b3SRodney W. Grimes 
1018ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
10195b81b6b3SRodney W. Grimes 	if (va == 0)
10205b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
10217f8cb368SDavid Greenman 	return((int)va);
10225b81b6b3SRodney W. Grimes }
10235b81b6b3SRodney W. Grimes 
10245b81b6b3SRodney W. Grimes extern vm_map_t phys_map;
10255b81b6b3SRodney W. Grimes 
10265b81b6b3SRodney W. Grimes /*
1027ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
10285b81b6b3SRodney W. Grimes  *
1029ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
1030ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
1031ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
10325b81b6b3SRodney W. Grimes  */
1033381fe1aaSGarrett Wollman void
10345b81b6b3SRodney W. Grimes vmapbuf(bp)
10355b81b6b3SRodney W. Grimes 	register struct buf *bp;
10365b81b6b3SRodney W. Grimes {
10375b81b6b3SRodney W. Grimes 	register int npf;
10385b81b6b3SRodney W. Grimes 	register caddr_t addr;
10395b81b6b3SRodney W. Grimes 	register long flags = bp->b_flags;
10405b81b6b3SRodney W. Grimes 	struct proc *p;
10415b81b6b3SRodney W. Grimes 	int off;
10425b81b6b3SRodney W. Grimes 	vm_offset_t kva;
10435b81b6b3SRodney W. Grimes 	register vm_offset_t pa;
10445b81b6b3SRodney W. Grimes 
10455b81b6b3SRodney W. Grimes 	if ((flags & B_PHYS) == 0)
10465b81b6b3SRodney W. Grimes 		panic("vmapbuf");
10475b81b6b3SRodney W. Grimes 	addr = bp->b_saveaddr = bp->b_un.b_addr;
10485b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
10495b81b6b3SRodney W. Grimes 	p = bp->b_proc;
1050ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + off));
10515b81b6b3SRodney W. Grimes 	kva = kmem_alloc_wait(phys_map, ctob(npf));
10525b81b6b3SRodney W. Grimes 	bp->b_un.b_addr = (caddr_t) (kva + off);
10535b81b6b3SRodney W. Grimes 	while (npf--) {
10545b81b6b3SRodney W. Grimes 		pa = pmap_extract(&p->p_vmspace->vm_pmap, (vm_offset_t)addr);
10555b81b6b3SRodney W. Grimes 		if (pa == 0)
10565b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
10576b4ac811SDavid Greenman 		pmap_kenter(kva, trunc_page(pa));
10585b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
10595b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
10605b81b6b3SRodney W. Grimes 	}
10616b4ac811SDavid Greenman 	pmap_update();
10625b81b6b3SRodney W. Grimes }
10635b81b6b3SRodney W. Grimes 
10645b81b6b3SRodney W. Grimes /*
10655b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
10665b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
10675b81b6b3SRodney W. Grimes  */
1068381fe1aaSGarrett Wollman void
10695b81b6b3SRodney W. Grimes vunmapbuf(bp)
10705b81b6b3SRodney W. Grimes 	register struct buf *bp;
10715b81b6b3SRodney W. Grimes {
10725b81b6b3SRodney W. Grimes 	register int npf;
10735b81b6b3SRodney W. Grimes 	register caddr_t addr = bp->b_un.b_addr;
10745b81b6b3SRodney W. Grimes 	vm_offset_t kva;
10755b81b6b3SRodney W. Grimes 
10765b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
10775b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
1078ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + ((int)addr & PGOFSET)));
10795b81b6b3SRodney W. Grimes 	kva = (vm_offset_t)((int)addr & ~PGOFSET);
10805b81b6b3SRodney W. Grimes 	kmem_free_wakeup(phys_map, kva, ctob(npf));
10815b81b6b3SRodney W. Grimes 	bp->b_un.b_addr = bp->b_saveaddr;
10825b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
10835b81b6b3SRodney W. Grimes }
10845b81b6b3SRodney W. Grimes 
10855b81b6b3SRodney W. Grimes /*
10865b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
10875b81b6b3SRodney W. Grimes  */
10887f8cb368SDavid Greenman void
10895b81b6b3SRodney W. Grimes cpu_reset() {
10905b81b6b3SRodney W. Grimes 
10915b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
10925b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
10935b81b6b3SRodney W. Grimes 
10945b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
10955b81b6b3SRodney W. Grimes 	tlbflush();
10965b81b6b3SRodney W. Grimes 	/* NOTREACHED */
10977f8cb368SDavid Greenman 	while(1);
10985b81b6b3SRodney W. Grimes }
1099b9d60b3fSDavid Greenman 
1100b9d60b3fSDavid Greenman /*
1101b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
110229360eb0SDavid Greenman  *	chunks of SGROWSIZ.
1103b9d60b3fSDavid Greenman  */
1104b9d60b3fSDavid Greenman int
1105b9d60b3fSDavid Greenman grow(p, sp)
1106b9d60b3fSDavid Greenman 	struct proc *p;
1107b9d60b3fSDavid Greenman 	int sp;
1108b9d60b3fSDavid Greenman {
1109b9d60b3fSDavid Greenman 	unsigned int nss;
1110b9d60b3fSDavid Greenman 	caddr_t v;
1111b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
1112b9d60b3fSDavid Greenman 
1113b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
1114b9d60b3fSDavid Greenman 	    return (1);
1115b9d60b3fSDavid Greenman 
1116b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
1117b9d60b3fSDavid Greenman 
1118b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
1119b9d60b3fSDavid Greenman 		return (0);
1120b9d60b3fSDavid Greenman 
1121b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
112229360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
1123b9d60b3fSDavid Greenman 		int grow_amount;
1124b9d60b3fSDavid Greenman 		/*
1125b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
1126b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
1127b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
1128b9d60b3fSDavid Greenman 		 * is expensive).
1129b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
113029360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
1131b9d60b3fSDavid Greenman 		 */
113229360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
1133b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
113429360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
1135b9d60b3fSDavid Greenman 		/*
113629360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
1137b9d60b3fSDavid Greenman 		 * just extend to the maximum size
1138b9d60b3fSDavid Greenman 		 */
1139b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
1140b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
1141b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
1142b9d60b3fSDavid Greenman 		}
1143b9d60b3fSDavid Greenman 		if (vm_allocate(&vm->vm_map, (vm_offset_t *)&v,
1144b9d60b3fSDavid Greenman 		    grow_amount, FALSE) != KERN_SUCCESS) {
1145b9d60b3fSDavid Greenman 			return (0);
1146b9d60b3fSDavid Greenman 		}
1147b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
1148b9d60b3fSDavid Greenman 	}
1149b9d60b3fSDavid Greenman 
1150b9d60b3fSDavid Greenman 	return (1);
1151b9d60b3fSDavid Greenman }
1152