xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 6ab46d52a5be5dd8055899247f0dcac05f3a481c)
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$
416ab46d52SBruce Evans  *	$Id: vm_machdep.c,v 1.66 1996/06/20 08:07:30 davidg Exp $
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
44960173b9SRodney W. Grimes #include "npx.h"
458890984dSGarrett Wollman #include "opt_bounce.h"
468890984dSGarrett Wollman 
4726f9a767SRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/systm.h>
4926f9a767SRodney W. Grimes #include <sys/proc.h>
5026f9a767SRodney W. Grimes #include <sys/malloc.h>
5126f9a767SRodney W. Grimes #include <sys/buf.h>
5226f9a767SRodney W. Grimes #include <sys/vnode.h>
53efeaf95aSDavid Greenman #include <sys/vmmeter.h>
545b81b6b3SRodney W. Grimes 
552320728fSRodney W. Grimes #include <machine/clock.h>
56b5e8ce9fSBruce Evans #include <machine/md_var.h>
575b81b6b3SRodney W. Grimes 
5826f9a767SRodney W. Grimes #include <vm/vm.h>
59efeaf95aSDavid Greenman #include <vm/vm_param.h>
60efeaf95aSDavid Greenman #include <vm/vm_prot.h>
61efeaf95aSDavid Greenman #include <vm/lock.h>
6226f9a767SRodney W. Grimes #include <vm/vm_kern.h>
6324a1cce3SDavid Greenman #include <vm/vm_page.h>
64efeaf95aSDavid Greenman #include <vm/vm_map.h>
65efeaf95aSDavid Greenman #include <vm/vm_extern.h>
66efeaf95aSDavid Greenman 
67efeaf95aSDavid Greenman #include <sys/user.h>
685b81b6b3SRodney W. Grimes 
692320728fSRodney W. Grimes #include <i386/isa/isa.h>
702320728fSRodney W. Grimes 
719aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
7287b91157SPoul-Henning Kamp static vm_offset_t
736c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
7487b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
756c146e28SBruce Evans 					int now));
7687b91157SPoul-Henning Kamp static vm_offset_t
776c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
7887b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
796c146e28SBruce Evans 
8087b6de2bSPoul-Henning Kamp static volatile int	kvasfreecnt;
81ae92ea44SDavid Greenman 
82d5e26ef0SDavid Greenman caddr_t		bouncememory;
8387b91157SPoul-Henning Kamp int		bouncepages;
8487b91157SPoul-Henning Kamp static int	bpwait;
8587b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
8687b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
87d5e26ef0SDavid Greenman 
88ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
8987b91157SPoul-Henning Kamp static int		bounceallocarraysize;
9087b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
9187b91157SPoul-Henning Kamp static int		bouncefree;
92d5e26ef0SDavid Greenman 
93d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
94ae92ea44SDavid Greenman #define MAXBKVA 1024
95f8845af0SPoul-Henning Kamp int		maxbkva = MAXBKVA*PAGE_SIZE;
96d5e26ef0SDavid Greenman 
97d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
9887b91157SPoul-Henning Kamp static struct kvasfree {
99d5e26ef0SDavid Greenman 	vm_offset_t addr;
100d5e26ef0SDavid Greenman 	vm_offset_t size;
101d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
102d5e26ef0SDavid Greenman 
103d5e26ef0SDavid Greenman /*
104d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
105d5e26ef0SDavid Greenman  * (only 1 inplemented now)
106d5e26ef0SDavid Greenman  */
10787b91157SPoul-Henning Kamp static vm_offset_t
108d5e26ef0SDavid Greenman vm_bounce_page_find(count)
109d5e26ef0SDavid Greenman 	int count;
110d5e26ef0SDavid Greenman {
111d5e26ef0SDavid Greenman 	int bit;
112d5e26ef0SDavid Greenman 	int s,i;
113d5e26ef0SDavid Greenman 
114d5e26ef0SDavid Greenman 	if (count != 1)
115d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
116d5e26ef0SDavid Greenman 
117d5e26ef0SDavid Greenman 	s = splbio();
118d5e26ef0SDavid Greenman retry:
119d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
120d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1213fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1223fb3086eSPoul-Henning Kamp 			if (bit) {
123d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
124d5e26ef0SDavid Greenman 				bouncefree -= count;
125d5e26ef0SDavid Greenman 				splx(s);
126ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
127d5e26ef0SDavid Greenman 			}
128d5e26ef0SDavid Greenman 		}
129d5e26ef0SDavid Greenman 	}
130ed7fcbd0SDavid Greenman 	bpwait = 1;
131d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
132d5e26ef0SDavid Greenman 	goto retry;
133d5e26ef0SDavid Greenman }
134d5e26ef0SDavid Greenman 
13587b91157SPoul-Henning Kamp static void
1361561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1371561d038SDavid Greenman 	vm_offset_t addr;
1381561d038SDavid Greenman 	vm_offset_t size;
1391561d038SDavid Greenman 	int now;
1401561d038SDavid Greenman {
1411561d038SDavid Greenman 	int s = splbio();
1421561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
143ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
144ae92ea44SDavid Greenman 	++kvasfreecnt;
1450e195446SDavid Greenman 	if( now) {
1460e195446SDavid Greenman 		/*
1470e195446SDavid Greenman 		 * this will do wakeups
1480e195446SDavid Greenman 		 */
1491561d038SDavid Greenman 		vm_bounce_kva(0,0);
1500e195446SDavid Greenman 	} else {
1510e195446SDavid Greenman 		if (bmwait) {
1520e195446SDavid Greenman 		/*
1530e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1540e195446SDavid Greenman 		 */
1551561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1560e195446SDavid Greenman 			bmwait = 0;
1570e195446SDavid Greenman 		}
1580e195446SDavid Greenman 	}
1591561d038SDavid Greenman 	splx(s);
1601561d038SDavid Greenman }
1611561d038SDavid Greenman 
162d5e26ef0SDavid Greenman /*
163d5e26ef0SDavid Greenman  * free count bounce buffer pages
164d5e26ef0SDavid Greenman  */
16587b91157SPoul-Henning Kamp static void
166d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
167d5e26ef0SDavid Greenman 	vm_offset_t pa;
168d5e26ef0SDavid Greenman 	int count;
169d5e26ef0SDavid Greenman {
170d5e26ef0SDavid Greenman 	int allocindex;
171d5e26ef0SDavid Greenman 	int index;
172d5e26ef0SDavid Greenman 	int bit;
173d5e26ef0SDavid Greenman 
174d5e26ef0SDavid Greenman 	if (count != 1)
175edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
176d5e26ef0SDavid Greenman 
177ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
178ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
179ae92ea44SDavid Greenman 			break;
180ae92ea44SDavid Greenman 	}
181d5e26ef0SDavid Greenman 
182ae92ea44SDavid Greenman 	if( index == bouncepages)
183ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
184d5e26ef0SDavid Greenman 
185ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
186ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
187d5e26ef0SDavid Greenman 
188d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
189d5e26ef0SDavid Greenman 
190d5e26ef0SDavid Greenman 	bouncefree += count;
191ed7fcbd0SDavid Greenman 	if (bpwait) {
192ed7fcbd0SDavid Greenman 		bpwait = 0;
193d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
194d5e26ef0SDavid Greenman 	}
195ed7fcbd0SDavid Greenman }
196d5e26ef0SDavid Greenman 
197d5e26ef0SDavid Greenman /*
198d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
199d5e26ef0SDavid Greenman  */
20087b91157SPoul-Henning Kamp static vm_offset_t
201ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
202ae92ea44SDavid Greenman 	int size;
2031561d038SDavid Greenman 	int waitok;
204d5e26ef0SDavid Greenman {
205d5e26ef0SDavid Greenman 	int i;
2066b4ac811SDavid Greenman 	vm_offset_t kva = 0;
207c564966bSDavid Greenman 	vm_offset_t off;
208d5e26ef0SDavid Greenman 	int s = splbio();
209d5e26ef0SDavid Greenman more:
210ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
211d5e26ef0SDavid Greenman 		bmfreeing = 1;
212ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
213f8845af0SPoul-Henning Kamp 			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
214c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
215c564966bSDavid Greenman 			}
2161561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
217d5e26ef0SDavid Greenman 				kvaf[i].size);
218d5e26ef0SDavid Greenman 		}
219d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
220d5e26ef0SDavid Greenman 		bmfreeing = 0;
221ae92ea44SDavid Greenman 		if( bmwait) {
222ae92ea44SDavid Greenman 			bmwait = 0;
223ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
224ae92ea44SDavid Greenman 		}
225d5e26ef0SDavid Greenman 	}
226d5e26ef0SDavid Greenman 
2271561d038SDavid Greenman 	if( size == 0) {
2281561d038SDavid Greenman 		splx(s);
2296ab46d52SBruce Evans 		return 0;
2301561d038SDavid Greenman 	}
2311561d038SDavid Greenman 
232ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2331561d038SDavid Greenman 		if( !waitok) {
2341561d038SDavid Greenman 			splx(s);
2356ab46d52SBruce Evans 			return 0;
2361561d038SDavid Greenman 		}
237d5e26ef0SDavid Greenman 		bmwait = 1;
2381561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
239d5e26ef0SDavid Greenman 		goto more;
240d5e26ef0SDavid Greenman 	}
241d5e26ef0SDavid Greenman 	splx(s);
242d5e26ef0SDavid Greenman 	return kva;
243d5e26ef0SDavid Greenman }
244d5e26ef0SDavid Greenman 
245d5e26ef0SDavid Greenman /*
246ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2470e195446SDavid Greenman  */
2480e195446SDavid Greenman vm_offset_t
2490e195446SDavid Greenman vm_bounce_kva_alloc(count)
2500e195446SDavid Greenman int count;
2510e195446SDavid Greenman {
2520e195446SDavid Greenman 	int i;
2530e195446SDavid Greenman 	vm_offset_t kva;
2540e195446SDavid Greenman 	vm_offset_t pa;
2550e195446SDavid Greenman 	if( bouncepages == 0) {
256f8845af0SPoul-Henning Kamp 		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
2570e195446SDavid Greenman 		return kva;
2580e195446SDavid Greenman 	}
259f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
2600e195446SDavid Greenman 	for(i=0;i<count;i++) {
2610e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
262f8845af0SPoul-Henning Kamp 		pmap_kenter(kva + i * PAGE_SIZE, pa);
2630e195446SDavid Greenman 	}
2640e195446SDavid Greenman 	return kva;
2650e195446SDavid Greenman }
2660e195446SDavid Greenman 
2670e195446SDavid Greenman /*
2680e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2690e195446SDavid Greenman  */
2700e195446SDavid Greenman void
2710e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2720e195446SDavid Greenman 	vm_offset_t kva;
2730e195446SDavid Greenman 	int count;
2740e195446SDavid Greenman {
2750e195446SDavid Greenman 	int i;
2760e195446SDavid Greenman 	vm_offset_t pa;
2770e195446SDavid Greenman 	if( bouncepages == 0) {
2780e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2790e195446SDavid Greenman 		return;
2800e195446SDavid Greenman 	}
2810e195446SDavid Greenman 	for(i = 0; i < count; i++) {
282f8845af0SPoul-Henning Kamp 		pa = pmap_kextract(kva + i * PAGE_SIZE);
2830e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2840e195446SDavid Greenman 	}
285f8845af0SPoul-Henning Kamp 	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
2860e195446SDavid Greenman }
2870e195446SDavid Greenman 
2880e195446SDavid Greenman /*
289d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
290d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
291d5e26ef0SDavid Greenman  */
292d5e26ef0SDavid Greenman void
293d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
294d5e26ef0SDavid Greenman 	struct buf *bp;
295d5e26ef0SDavid Greenman {
296d5e26ef0SDavid Greenman 	int countvmpg;
297d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
298d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
299d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
300d5e26ef0SDavid Greenman 	vm_offset_t pa;
301d5e26ef0SDavid Greenman 	int dobounceflag = 0;
302d5e26ef0SDavid Greenman 	int i;
303d5e26ef0SDavid Greenman 
304d5e26ef0SDavid Greenman 	if (bouncepages == 0)
305d5e26ef0SDavid Greenman 		return;
306d5e26ef0SDavid Greenman 
307ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
308ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
309ae92ea44SDavid Greenman 		return;
3101561d038SDavid Greenman 	}
3111561d038SDavid Greenman 
312ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3133fb3086eSPoul-Henning Kamp 		printf(
3143fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
315ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
316ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
317ae92ea44SDavid Greenman 	}
318ae92ea44SDavid Greenman 
319ae92ea44SDavid Greenman /*
320ae92ea44SDavid Greenman  *  This is not really necessary
321ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
322ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
323ae92ea44SDavid Greenman  *	}
324ae92ea44SDavid Greenman  */
325ae92ea44SDavid Greenman 
326ae92ea44SDavid Greenman 
32726f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
32826f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
329d5e26ef0SDavid Greenman 
330bd7e5f99SJohn Dyson 	vapstart = trunc_page(vastart);
331bd7e5f99SJohn Dyson 	vapend = round_page(vaend);
332f8845af0SPoul-Henning Kamp 	countvmpg = (vapend - vapstart) / PAGE_SIZE;
333d5e26ef0SDavid Greenman 
334d5e26ef0SDavid Greenman /*
335d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
336d5e26ef0SDavid Greenman  */
337d5e26ef0SDavid Greenman 	va = vapstart;
338d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
339ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
340d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
341d5e26ef0SDavid Greenman 			++dobounceflag;
3420d94caffSDavid Greenman 		if( pa == 0)
3430d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
344f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
345d5e26ef0SDavid Greenman 	}
346d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
347d5e26ef0SDavid Greenman 		return;
348d5e26ef0SDavid Greenman 
349d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
350d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
351d5e26ef0SDavid Greenman 
352d5e26ef0SDavid Greenman /*
353d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
354d5e26ef0SDavid Greenman  */
355f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
356ae92ea44SDavid Greenman #if 0
357ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
358ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
359ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
360ae92ea44SDavid Greenman #endif
361d5e26ef0SDavid Greenman 	va = vapstart;
362d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
363ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
364d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
365d5e26ef0SDavid Greenman 			/*
366d5e26ef0SDavid Greenman 			 * allocate a replacement page
367d5e26ef0SDavid Greenman 			 */
368d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
369f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
370ae92ea44SDavid Greenman #if 0
371ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
372ae92ea44SDavid Greenman #endif
373d5e26ef0SDavid Greenman 			/*
374d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
375d5e26ef0SDavid Greenman 			 */
3761561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
377f8845af0SPoul-Henning Kamp 				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
3781561d038SDavid Greenman 			}
379d5e26ef0SDavid Greenman 		} else {
380d5e26ef0SDavid Greenman 			/*
381d5e26ef0SDavid Greenman 			 * use original page
382d5e26ef0SDavid Greenman 			 */
383f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), pa);
384d5e26ef0SDavid Greenman 		}
385f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
386d5e26ef0SDavid Greenman 	}
387d5e26ef0SDavid Greenman 
388d5e26ef0SDavid Greenman /*
389d5e26ef0SDavid Greenman  * flag the buffer as being bounced
390d5e26ef0SDavid Greenman  */
391d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
392d5e26ef0SDavid Greenman /*
393d5e26ef0SDavid Greenman  * save the original buffer kva
394d5e26ef0SDavid Greenman  */
39526f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
396d5e26ef0SDavid Greenman /*
397d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
398d5e26ef0SDavid Greenman  */
39926f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
400f8845af0SPoul-Henning Kamp 				((vm_offset_t) bp->b_savekva & PAGE_MASK));
401ae92ea44SDavid Greenman #if 0
402ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
403ae92ea44SDavid Greenman #endif
404d5e26ef0SDavid Greenman 	return;
405d5e26ef0SDavid Greenman }
406d5e26ef0SDavid Greenman 
407d5e26ef0SDavid Greenman /*
408d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
409d5e26ef0SDavid Greenman  */
410d5e26ef0SDavid Greenman void
411d5e26ef0SDavid Greenman vm_bounce_free(bp)
412d5e26ef0SDavid Greenman 	struct buf *bp;
413d5e26ef0SDavid Greenman {
414d5e26ef0SDavid Greenman 	int i;
415ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
416d5e26ef0SDavid Greenman 
417d5e26ef0SDavid Greenman /*
418d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
419d5e26ef0SDavid Greenman  */
420d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
421d5e26ef0SDavid Greenman 		return;
422d5e26ef0SDavid Greenman 
423ae92ea44SDavid Greenman /*
424ae92ea44SDavid Greenman  *  This check is not necessary
425ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
426ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
427ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
428ae92ea44SDavid Greenman  *	}
429ae92ea44SDavid Greenman  */
430ae92ea44SDavid Greenman 
431d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
43226f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
433ae92ea44SDavid Greenman /*
434ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
435ae92ea44SDavid Greenman */
436d5e26ef0SDavid Greenman 
437d5e26ef0SDavid Greenman /*
438d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
439d5e26ef0SDavid Greenman  */
440ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
441d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
442d5e26ef0SDavid Greenman 		vm_offset_t copycount;
443d5e26ef0SDavid Greenman 
444bd7e5f99SJohn Dyson 		copycount = round_page(bouncekva + 1) - bouncekva;
445bd7e5f99SJohn Dyson 		mybouncepa = pmap_kextract(trunc_page(bouncekva));
446d5e26ef0SDavid Greenman 
447d5e26ef0SDavid Greenman /*
448d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
449d5e26ef0SDavid Greenman  */
450bd7e5f99SJohn Dyson 		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
451ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
452ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
453ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
454d5e26ef0SDavid Greenman /*
455d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
456d5e26ef0SDavid Greenman  */
457d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
458ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
459d5e26ef0SDavid Greenman /*
460d5e26ef0SDavid Greenman  * free the bounce allocation
461d5e26ef0SDavid Greenman  */
462ae92ea44SDavid Greenman 
463ae92ea44SDavid Greenman /*
464ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
465ae92ea44SDavid Greenman */
466ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
467d5e26ef0SDavid Greenman 		}
468d5e26ef0SDavid Greenman 
469d5e26ef0SDavid Greenman 		origkva += copycount;
470d5e26ef0SDavid Greenman 		bouncekva += copycount;
471ae92ea44SDavid Greenman 		i += copycount;
472d5e26ef0SDavid Greenman 	}
473d5e26ef0SDavid Greenman 
474d5e26ef0SDavid Greenman /*
475ae92ea44SDavid Greenman 	printf("\n");
476ae92ea44SDavid Greenman */
477ae92ea44SDavid Greenman /*
478d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
479d5e26ef0SDavid Greenman  */
480ae92ea44SDavid Greenman 
481bd7e5f99SJohn Dyson 	bouncekva= trunc_page((vm_offset_t) bp->b_data);
482bd7e5f99SJohn Dyson 	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
483ae92ea44SDavid Greenman 
484ae92ea44SDavid Greenman /*
485f8845af0SPoul-Henning Kamp 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
486ae92ea44SDavid Greenman */
487ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
48826f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
489d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
490d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
491d5e26ef0SDavid Greenman 
492d5e26ef0SDavid Greenman 	return;
493d5e26ef0SDavid Greenman }
494d5e26ef0SDavid Greenman 
495ae92ea44SDavid Greenman 
4965b81b6b3SRodney W. Grimes /*
4971561d038SDavid Greenman  * init the bounce buffer system
4981561d038SDavid Greenman  */
4991561d038SDavid Greenman void
5001561d038SDavid Greenman vm_bounce_init()
5011561d038SDavid Greenman {
502ae92ea44SDavid Greenman 	int i;
5031561d038SDavid Greenman 
5041561d038SDavid Greenman 	kvasfreecnt = 0;
5051561d038SDavid Greenman 
5061561d038SDavid Greenman 	if (bouncepages == 0)
5071561d038SDavid Greenman 		return;
5081561d038SDavid Greenman 
5091561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5101561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5111561d038SDavid Greenman 
5121561d038SDavid Greenman 	if (!bounceallocarray)
513edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5141561d038SDavid Greenman 
515ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
516ae92ea44SDavid Greenman 	if (!bouncepa)
517edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5181561d038SDavid Greenman 
5190d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5200d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5210d94caffSDavid Greenman 	}
5220d94caffSDavid Greenman 
523ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
524ae92ea44SDavid Greenman 		vm_offset_t pa;
525f8845af0SPoul-Henning Kamp 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG)
526ae92ea44SDavid Greenman 			panic("bounce memory out of range");
527ae92ea44SDavid Greenman 		if( pa == 0)
528ae92ea44SDavid Greenman 			panic("bounce memory not resident");
529ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5300d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
531ae92ea44SDavid Greenman 	}
5321561d038SDavid Greenman 	bouncefree = bouncepages;
5331561d038SDavid Greenman 
5341561d038SDavid Greenman }
5359aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5366c146e28SBruce Evans 
537a4f7a4c9SDavid Greenman /*
538a4f7a4c9SDavid Greenman  * quick version of vm_fault
539a4f7a4c9SDavid Greenman  */
540dca51299SJohn Dyson void
541a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5426c146e28SBruce Evans 	caddr_t v;
543a4f7a4c9SDavid Greenman 	int prot;
544a4f7a4c9SDavid Greenman {
5451d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5466c146e28SBruce Evans 		subyte(v, fubyte(v));
547a4f7a4c9SDavid Greenman 	else
5486c146e28SBruce Evans 		fubyte(v);
549a4f7a4c9SDavid Greenman }
550a4f7a4c9SDavid Greenman 
5511561d038SDavid Greenman /*
5525b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
5535b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
5545b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
5555b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
5565b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
5575b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
5585b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
5595b81b6b3SRodney W. Grimes  */
560381fe1aaSGarrett Wollman int
5615b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5625b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5635b81b6b3SRodney W. Grimes {
564f83a01b6SDavid Greenman 	struct pcb *pcb2 = &p2->p_addr->u_pcb;
565f83a01b6SDavid Greenman 	int sp, offset;
56668832d30SPoul-Henning Kamp 	volatile int retval;
5675b81b6b3SRodney W. Grimes 
5685b81b6b3SRodney W. Grimes 	/*
5695b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
5705b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
5715b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
5725b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
5735b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
5745b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
5755b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
5765b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
5775b81b6b3SRodney W. Grimes 	 */
578f83a01b6SDavid Greenman 
579f83a01b6SDavid Greenman 	__asm __volatile("movl %%esp,%0" : "=r" (sp));
580f83a01b6SDavid Greenman 	offset = sp - (int)kstack;
581f83a01b6SDavid Greenman 
58268832d30SPoul-Henning Kamp 	retval = 1;		/* return 1 in child */
5835b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
5845b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
58526f9a767SRodney W. Grimes 	p2->p_md.md_regs = p1->p_md.md_regs;
5865b81b6b3SRodney W. Grimes 
587f83a01b6SDavid Greenman 	*pcb2 = p1->p_addr->u_pcb;
588f83a01b6SDavid Greenman 	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
5895b81b6b3SRodney W. Grimes 
59068832d30SPoul-Henning Kamp 	retval = 0;		/* return 0 in parent */
59168832d30SPoul-Henning Kamp 	savectx(pcb2);
59268832d30SPoul-Henning Kamp 	return (retval);
5935b81b6b3SRodney W. Grimes }
5945b81b6b3SRodney W. Grimes 
5957c2b54e8SNate Williams void
5965b81b6b3SRodney W. Grimes cpu_exit(p)
5975b81b6b3SRodney W. Grimes 	register struct proc *p;
5985b81b6b3SRodney W. Grimes {
5990dbf6d73SJordan K. Hubbard #ifdef USER_LDT
6000dbf6d73SJordan K. Hubbard 	struct pcb *pcb;
6010dbf6d73SJordan K. Hubbard #endif
6025b81b6b3SRodney W. Grimes 
603960173b9SRodney W. Grimes #if NNPX > 0
6045b81b6b3SRodney W. Grimes 	npxexit(p);
605960173b9SRodney W. Grimes #endif	/* NNPX */
6060dbf6d73SJordan K. Hubbard #ifdef USER_LDT
6070dbf6d73SJordan K. Hubbard 	pcb = &p->p_addr->u_pcb;
6080dbf6d73SJordan K. Hubbard 	if (pcb->pcb_ldt != 0) {
6090dbf6d73SJordan K. Hubbard 		if (pcb == curpcb)
6100dbf6d73SJordan K. Hubbard 			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
6110dbf6d73SJordan K. Hubbard 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
6120dbf6d73SJordan K. Hubbard 			pcb->pcb_ldt_len * sizeof(union descriptor));
6130dbf6d73SJordan K. Hubbard 		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
6140dbf6d73SJordan K. Hubbard 	}
6150dbf6d73SJordan K. Hubbard #endif
6161a051896SBruce Evans 	cnt.v_swtch++;
6171a051896SBruce Evans 	cpu_switch(p);
6187c2b54e8SNate Williams 	panic("cpu_exit");
6195b81b6b3SRodney W. Grimes }
6205b81b6b3SRodney W. Grimes 
621381fe1aaSGarrett Wollman void
622bd7e5f99SJohn Dyson cpu_wait(p)
623bd7e5f99SJohn Dyson 	struct proc *p;
624bd7e5f99SJohn Dyson {
6255b81b6b3SRodney W. Grimes 	/* drop per-process resources */
626bd7e5f99SJohn Dyson 	pmap_qremove((vm_offset_t) p->p_addr, UPAGES);
6270d94caffSDavid Greenman 	kmem_free(u_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
6287f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
6295b81b6b3SRodney W. Grimes }
6305b81b6b3SRodney W. Grimes 
6315b81b6b3SRodney W. Grimes /*
63226f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
63326f9a767SRodney W. Grimes  */
63426f9a767SRodney W. Grimes int
63526f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
63626f9a767SRodney W. Grimes 	struct proc *p;
63726f9a767SRodney W. Grimes 	struct vnode *vp;
63826f9a767SRodney W. Grimes 	struct ucred *cred;
63926f9a767SRodney W. Grimes {
64026f9a767SRodney W. Grimes 
64126f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
64226f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
64326f9a767SRodney W. Grimes 	    p));
64426f9a767SRodney W. Grimes }
64526f9a767SRodney W. Grimes 
64687b6de2bSPoul-Henning Kamp #ifdef notyet
64787b91157SPoul-Henning Kamp static void
6485b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
6495b81b6b3SRodney W. Grimes 	u_short *pte;
6505b81b6b3SRodney W. Grimes 	caddr_t vaddr;
6515b81b6b3SRodney W. Grimes {
6525b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
6535b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
6545b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
6555b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
6565b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
6575b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
6585b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
6595b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
6605b81b6b3SRodney W. Grimes }
66187b6de2bSPoul-Henning Kamp #endif
66226f9a767SRodney W. Grimes 
66326f9a767SRodney W. Grimes /*
6645b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
6655b81b6b3SRodney W. Grimes  */
666aaf08d94SGarrett Wollman u_long
6677f8cb368SDavid Greenman kvtop(void *addr)
6685b81b6b3SRodney W. Grimes {
6695b81b6b3SRodney W. Grimes 	vm_offset_t va;
6705b81b6b3SRodney W. Grimes 
671ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
6725b81b6b3SRodney W. Grimes 	if (va == 0)
6735b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
6747f8cb368SDavid Greenman 	return((int)va);
6755b81b6b3SRodney W. Grimes }
6765b81b6b3SRodney W. Grimes 
6775b81b6b3SRodney W. Grimes /*
678ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
6795b81b6b3SRodney W. Grimes  *
680ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
681ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
682ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
6835b81b6b3SRodney W. Grimes  */
684381fe1aaSGarrett Wollman void
6855b81b6b3SRodney W. Grimes vmapbuf(bp)
6865b81b6b3SRodney W. Grimes 	register struct buf *bp;
6875b81b6b3SRodney W. Grimes {
6880157d6d9SJohn Dyson 	register caddr_t addr, v, kva;
68907658526SPoul-Henning Kamp 	vm_offset_t pa;
6905b81b6b3SRodney W. Grimes 
69126f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
6925b81b6b3SRodney W. Grimes 		panic("vmapbuf");
69326f9a767SRodney W. Grimes 
69412936855SDavid Greenman 	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
69526f9a767SRodney W. Grimes 	    addr < bp->b_data + bp->b_bufsize;
6960157d6d9SJohn Dyson 	    addr += PAGE_SIZE, v += PAGE_SIZE) {
69726f9a767SRodney W. Grimes 		/*
698ac269d78SDavid Greenman 		 * Do the vm_fault if needed; do the copy-on-write thing
699ac269d78SDavid Greenman 		 * when reading stuff off device into memory.
70026f9a767SRodney W. Grimes 		 */
70126f9a767SRodney W. Grimes 		vm_fault_quick(addr,
70226f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
7030157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
704641a27e6SJohn Dyson 		if (pa == 0)
705641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
70626f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
7070157d6d9SJohn Dyson 		pmap_kenter((vm_offset_t) v, pa);
70826f9a767SRodney W. Grimes 	}
70926f9a767SRodney W. Grimes 
7100157d6d9SJohn Dyson 	kva = bp->b_saveaddr;
7110157d6d9SJohn Dyson 	bp->b_saveaddr = bp->b_data;
7120157d6d9SJohn Dyson 	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
7135b81b6b3SRodney W. Grimes }
7145b81b6b3SRodney W. Grimes 
7155b81b6b3SRodney W. Grimes /*
7165b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
7175b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
7185b81b6b3SRodney W. Grimes  */
719381fe1aaSGarrett Wollman void
7205b81b6b3SRodney W. Grimes vunmapbuf(bp)
7215b81b6b3SRodney W. Grimes 	register struct buf *bp;
7225b81b6b3SRodney W. Grimes {
723c564966bSDavid Greenman 	register caddr_t addr;
72407658526SPoul-Henning Kamp 	vm_offset_t pa;
7255b81b6b3SRodney W. Grimes 
7265b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7275b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
728c564966bSDavid Greenman 
72912936855SDavid Greenman 	for (addr = (caddr_t)trunc_page(bp->b_data);
73012936855SDavid Greenman 	    addr < bp->b_data + bp->b_bufsize;
731f8845af0SPoul-Henning Kamp 	    addr += PAGE_SIZE) {
7320157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
7330157d6d9SJohn Dyson 		pmap_kremove((vm_offset_t) addr);
73426f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
73526f9a767SRodney W. Grimes 	}
7360157d6d9SJohn Dyson 
7370157d6d9SJohn Dyson 	bp->b_data = bp->b_saveaddr;
7385b81b6b3SRodney W. Grimes }
7395b81b6b3SRodney W. Grimes 
7405b81b6b3SRodney W. Grimes /*
7415b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
7425b81b6b3SRodney W. Grimes  */
7437f8cb368SDavid Greenman void
7445b81b6b3SRodney W. Grimes cpu_reset() {
7455b81b6b3SRodney W. Grimes 
7462320728fSRodney W. Grimes 	/*
7472320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
7482320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
7492320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
7502320728fSRodney W. Grimes 	 */
7515eb46edfSDavid Greenman 
7525eb46edfSDavid Greenman #ifndef BROKEN_KEYBOARD_RESET
7532320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
7542320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
7552320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
7562320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
7575eb46edfSDavid Greenman #endif
7582320728fSRodney W. Grimes 
7595b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
760f8845af0SPoul-Henning Kamp 	bzero((caddr_t) PTD, PAGE_SIZE);
7615b81b6b3SRodney W. Grimes 
7625b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
7632c7a40c7SDavid Greenman 	pmap_update();
7645b81b6b3SRodney W. Grimes 	/* NOTREACHED */
7657f8cb368SDavid Greenman 	while(1);
7665b81b6b3SRodney W. Grimes }
767b9d60b3fSDavid Greenman 
768b9d60b3fSDavid Greenman /*
769b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
77029360eb0SDavid Greenman  *	chunks of SGROWSIZ.
771b9d60b3fSDavid Greenman  */
772b9d60b3fSDavid Greenman int
773b9d60b3fSDavid Greenman grow(p, sp)
774b9d60b3fSDavid Greenman 	struct proc *p;
77526f9a767SRodney W. Grimes 	u_int sp;
776b9d60b3fSDavid Greenman {
777b9d60b3fSDavid Greenman 	unsigned int nss;
778b9d60b3fSDavid Greenman 	caddr_t v;
779b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
780b9d60b3fSDavid Greenman 
781b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
782b9d60b3fSDavid Greenman 	    return (1);
783b9d60b3fSDavid Greenman 
784b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
785b9d60b3fSDavid Greenman 
786b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
787b9d60b3fSDavid Greenman 		return (0);
788b9d60b3fSDavid Greenman 
789b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
79029360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
791b9d60b3fSDavid Greenman 		int grow_amount;
792b9d60b3fSDavid Greenman 		/*
793b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
794b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
795b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
796b9d60b3fSDavid Greenman 		 * is expensive).
797b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
79829360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
799b9d60b3fSDavid Greenman 		 */
80029360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
801b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
80229360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
803b9d60b3fSDavid Greenman 		/*
80429360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
805b9d60b3fSDavid Greenman 		 * just extend to the maximum size
806b9d60b3fSDavid Greenman 		 */
807b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
808b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
809b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
810b9d60b3fSDavid Greenman 		}
81168940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
812bd7e5f99SJohn Dyson 		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
813b9d60b3fSDavid Greenman 			return (0);
814b9d60b3fSDavid Greenman 		}
815b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
816b9d60b3fSDavid Greenman 	}
817b9d60b3fSDavid Greenman 
818b9d60b3fSDavid Greenman 	return (1);
819b9d60b3fSDavid Greenman }
820a29b63cbSJohn Dyson 
821a29b63cbSJohn Dyson /*
822a29b63cbSJohn Dyson  * prototype routine to implement the pre-zeroed page mechanism
823a29b63cbSJohn Dyson  * this routine is called from the idle loop.
824a29b63cbSJohn Dyson  */
825a29b63cbSJohn Dyson int
826a29b63cbSJohn Dyson vm_page_zero_idle() {
827a29b63cbSJohn Dyson 	vm_page_t m;
828a29b63cbSJohn Dyson 	if ((cnt.v_free_count > cnt.v_interrupt_free_min) &&
829b18bfc3dSJohn Dyson 		(m = TAILQ_FIRST(&vm_page_queue_free))) {
830a29b63cbSJohn Dyson 		TAILQ_REMOVE(&vm_page_queue_free, m, pageq);
831a29b63cbSJohn Dyson 		enable_intr();
832a29b63cbSJohn Dyson 		pmap_zero_page(VM_PAGE_TO_PHYS(m));
833a29b63cbSJohn Dyson 		disable_intr();
83423e56ee8SDavid Greenman 		TAILQ_INSERT_HEAD(&vm_page_queue_zero, m, pageq);
835bd7e5f99SJohn Dyson 		m->queue = PQ_ZERO;
836a316d390SJohn Dyson 		++vm_page_zero_count;
837a29b63cbSJohn Dyson 		return 1;
838a29b63cbSJohn Dyson 	}
839a29b63cbSJohn Dyson 	return 0;
840a29b63cbSJohn Dyson }
841