xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 6c146e28a629639d2d8261d4a1258640fc970d36)
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$
416c146e28SBruce Evans  *	$Id: vm_machdep.c,v 1.43 1995/10/01 20:10:20 davidg Exp $
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
44960173b9SRodney W. Grimes #include "npx.h"
4526f9a767SRodney W. Grimes #include <sys/param.h>
4626f9a767SRodney W. Grimes #include <sys/systm.h>
4726f9a767SRodney W. Grimes #include <sys/proc.h>
4826f9a767SRodney W. Grimes #include <sys/malloc.h>
4926f9a767SRodney W. Grimes #include <sys/buf.h>
5026f9a767SRodney W. Grimes #include <sys/vnode.h>
5126f9a767SRodney W. Grimes #include <sys/user.h>
525b81b6b3SRodney W. Grimes 
532320728fSRodney W. Grimes #include <machine/clock.h>
5426f9a767SRodney W. Grimes #include <machine/cpu.h>
55b5e8ce9fSBruce Evans #include <machine/md_var.h>
565b81b6b3SRodney W. Grimes 
5726f9a767SRodney W. Grimes #include <vm/vm.h>
5826f9a767SRodney W. Grimes #include <vm/vm_kern.h>
5924a1cce3SDavid Greenman #include <vm/vm_page.h>
605b81b6b3SRodney W. Grimes 
612320728fSRodney W. Grimes #include <i386/isa/isa.h>
622320728fSRodney W. Grimes 
636c146e28SBruce Evans extern void	pagemove __P((caddr_t from, caddr_t to, int size));
646c146e28SBruce Evans extern void	setredzone __P((u_short *pte, caddr_t vaddr));
656c146e28SBruce Evans extern void	vm_fault_quick __P((caddr_t v, int prot));
666c146e28SBruce Evans 
679aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
686c146e28SBruce Evans extern vm_offset_t
696c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
706c146e28SBruce Evans extern void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
716c146e28SBruce Evans 					int now));
726c146e28SBruce Evans extern vm_offset_t
736c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
746c146e28SBruce Evans extern void	vm_bounce_page_free __P((vm_offset_t pa, int count));
756c146e28SBruce Evans 
76ae92ea44SDavid Greenman volatile int	kvasfreecnt;
77ae92ea44SDavid Greenman 
78d5e26ef0SDavid Greenman caddr_t		bouncememory;
79ed7fcbd0SDavid Greenman int		bouncepages, bpwait;
80ae92ea44SDavid Greenman vm_offset_t	*bouncepa;
81d5e26ef0SDavid Greenman int		bmwait, bmfreeing;
82d5e26ef0SDavid Greenman 
83ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
84d5e26ef0SDavid Greenman int		bounceallocarraysize;
85d5e26ef0SDavid Greenman unsigned	*bounceallocarray;
86d5e26ef0SDavid Greenman int		bouncefree;
87d5e26ef0SDavid Greenman 
88d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
89ae92ea44SDavid Greenman #define MAXBKVA 1024
9026f9a767SRodney W. Grimes int		maxbkva = MAXBKVA*NBPG;
91d5e26ef0SDavid Greenman 
92d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
93d5e26ef0SDavid Greenman struct kvasfree {
94d5e26ef0SDavid Greenman 	vm_offset_t addr;
95d5e26ef0SDavid Greenman 	vm_offset_t size;
96d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
97d5e26ef0SDavid Greenman 
98d5e26ef0SDavid Greenman /*
99d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
100d5e26ef0SDavid Greenman  * (only 1 inplemented now)
101d5e26ef0SDavid Greenman  */
102d5e26ef0SDavid Greenman vm_offset_t
103d5e26ef0SDavid Greenman vm_bounce_page_find(count)
104d5e26ef0SDavid Greenman 	int count;
105d5e26ef0SDavid Greenman {
106d5e26ef0SDavid Greenman 	int bit;
107d5e26ef0SDavid Greenman 	int s,i;
108d5e26ef0SDavid Greenman 
109d5e26ef0SDavid Greenman 	if (count != 1)
110d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
111d5e26ef0SDavid Greenman 
112d5e26ef0SDavid Greenman 	s = splbio();
113d5e26ef0SDavid Greenman retry:
114d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
115d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1163fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1173fb3086eSPoul-Henning Kamp 			if (bit) {
118d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
119d5e26ef0SDavid Greenman 				bouncefree -= count;
120d5e26ef0SDavid Greenman 				splx(s);
121ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
122d5e26ef0SDavid Greenman 			}
123d5e26ef0SDavid Greenman 		}
124d5e26ef0SDavid Greenman 	}
125ed7fcbd0SDavid Greenman 	bpwait = 1;
126d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
127d5e26ef0SDavid Greenman 	goto retry;
128d5e26ef0SDavid Greenman }
129d5e26ef0SDavid Greenman 
1301561d038SDavid Greenman void
1311561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1321561d038SDavid Greenman 	vm_offset_t addr;
1331561d038SDavid Greenman 	vm_offset_t size;
1341561d038SDavid Greenman 	int now;
1351561d038SDavid Greenman {
1361561d038SDavid Greenman 	int s = splbio();
1371561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
138ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
139ae92ea44SDavid Greenman 	++kvasfreecnt;
1400e195446SDavid Greenman 	if( now) {
1410e195446SDavid Greenman 		/*
1420e195446SDavid Greenman 		 * this will do wakeups
1430e195446SDavid Greenman 		 */
1441561d038SDavid Greenman 		vm_bounce_kva(0,0);
1450e195446SDavid Greenman 	} else {
1460e195446SDavid Greenman 		if (bmwait) {
1470e195446SDavid Greenman 		/*
1480e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1490e195446SDavid Greenman 		 */
1501561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1510e195446SDavid Greenman 			bmwait = 0;
1520e195446SDavid Greenman 		}
1530e195446SDavid Greenman 	}
1541561d038SDavid Greenman 	splx(s);
1551561d038SDavid Greenman }
1561561d038SDavid Greenman 
157d5e26ef0SDavid Greenman /*
158d5e26ef0SDavid Greenman  * free count bounce buffer pages
159d5e26ef0SDavid Greenman  */
160d5e26ef0SDavid Greenman void
161d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
162d5e26ef0SDavid Greenman 	vm_offset_t pa;
163d5e26ef0SDavid Greenman 	int count;
164d5e26ef0SDavid Greenman {
165d5e26ef0SDavid Greenman 	int allocindex;
166d5e26ef0SDavid Greenman 	int index;
167d5e26ef0SDavid Greenman 	int bit;
168d5e26ef0SDavid Greenman 
169d5e26ef0SDavid Greenman 	if (count != 1)
170edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
171d5e26ef0SDavid Greenman 
172ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
173ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
174ae92ea44SDavid Greenman 			break;
175ae92ea44SDavid Greenman 	}
176d5e26ef0SDavid Greenman 
177ae92ea44SDavid Greenman 	if( index == bouncepages)
178ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
179d5e26ef0SDavid Greenman 
180ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
181ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
182d5e26ef0SDavid Greenman 
183d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
184d5e26ef0SDavid Greenman 
185d5e26ef0SDavid Greenman 	bouncefree += count;
186ed7fcbd0SDavid Greenman 	if (bpwait) {
187ed7fcbd0SDavid Greenman 		bpwait = 0;
188d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
189d5e26ef0SDavid Greenman 	}
190ed7fcbd0SDavid Greenman }
191d5e26ef0SDavid Greenman 
192d5e26ef0SDavid Greenman /*
193d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
194d5e26ef0SDavid Greenman  */
195d5e26ef0SDavid Greenman vm_offset_t
196ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
197ae92ea44SDavid Greenman 	int size;
1981561d038SDavid Greenman 	int waitok;
199d5e26ef0SDavid Greenman {
200d5e26ef0SDavid Greenman 	int i;
2016b4ac811SDavid Greenman 	vm_offset_t kva = 0;
202c564966bSDavid Greenman 	vm_offset_t off;
203d5e26ef0SDavid Greenman 	int s = splbio();
204d5e26ef0SDavid Greenman more:
205ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
206d5e26ef0SDavid Greenman 		bmfreeing = 1;
207ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
208c564966bSDavid Greenman 			for(off=0;off<kvaf[i].size;off+=NBPG) {
209c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
210c564966bSDavid Greenman 			}
2111561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
212d5e26ef0SDavid Greenman 				kvaf[i].size);
213d5e26ef0SDavid Greenman 		}
214d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
215d5e26ef0SDavid Greenman 		bmfreeing = 0;
216ae92ea44SDavid Greenman 		if( bmwait) {
217ae92ea44SDavid Greenman 			bmwait = 0;
218ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
219ae92ea44SDavid Greenman 		}
220d5e26ef0SDavid Greenman 	}
221d5e26ef0SDavid Greenman 
2221561d038SDavid Greenman 	if( size == 0) {
2231561d038SDavid Greenman 		splx(s);
2241561d038SDavid Greenman 		return NULL;
2251561d038SDavid Greenman 	}
2261561d038SDavid Greenman 
227ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2281561d038SDavid Greenman 		if( !waitok) {
2291561d038SDavid Greenman 			splx(s);
2301561d038SDavid Greenman 			return NULL;
2311561d038SDavid Greenman 		}
232d5e26ef0SDavid Greenman 		bmwait = 1;
2331561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
234d5e26ef0SDavid Greenman 		goto more;
235d5e26ef0SDavid Greenman 	}
236d5e26ef0SDavid Greenman 	splx(s);
237d5e26ef0SDavid Greenman 	return kva;
238d5e26ef0SDavid Greenman }
239d5e26ef0SDavid Greenman 
240d5e26ef0SDavid Greenman /*
241ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2420e195446SDavid Greenman  */
2430e195446SDavid Greenman vm_offset_t
2440e195446SDavid Greenman vm_bounce_kva_alloc(count)
2450e195446SDavid Greenman int count;
2460e195446SDavid Greenman {
2470e195446SDavid Greenman 	int i;
2480e195446SDavid Greenman 	vm_offset_t kva;
2490e195446SDavid Greenman 	vm_offset_t pa;
2500e195446SDavid Greenman 	if( bouncepages == 0) {
2510e195446SDavid Greenman 		kva = (vm_offset_t) malloc(count*NBPG, M_TEMP, M_WAITOK);
2520e195446SDavid Greenman 		return kva;
2530e195446SDavid Greenman 	}
254ae92ea44SDavid Greenman 	kva = vm_bounce_kva(count*NBPG, 1);
2550e195446SDavid Greenman 	for(i=0;i<count;i++) {
2560e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
2570e195446SDavid Greenman 		pmap_kenter(kva + i * NBPG, pa);
2580e195446SDavid Greenman 	}
2590e195446SDavid Greenman 	return kva;
2600e195446SDavid Greenman }
2610e195446SDavid Greenman 
2620e195446SDavid Greenman /*
2630e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2640e195446SDavid Greenman  */
2650e195446SDavid Greenman void
2660e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2670e195446SDavid Greenman 	vm_offset_t kva;
2680e195446SDavid Greenman 	int count;
2690e195446SDavid Greenman {
2700e195446SDavid Greenman 	int i;
2710e195446SDavid Greenman 	vm_offset_t pa;
2720e195446SDavid Greenman 	if( bouncepages == 0) {
2730e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2740e195446SDavid Greenman 		return;
2750e195446SDavid Greenman 	}
2760e195446SDavid Greenman 	for(i = 0; i < count; i++) {
2770e195446SDavid Greenman 		pa = pmap_kextract(kva + i * NBPG);
2780e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2790e195446SDavid Greenman 	}
280ae92ea44SDavid Greenman 	vm_bounce_kva_free(kva, count*NBPG, 0);
2810e195446SDavid Greenman }
2820e195446SDavid Greenman 
2830e195446SDavid Greenman /*
284d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
285d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
286d5e26ef0SDavid Greenman  */
287d5e26ef0SDavid Greenman void
288d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
289d5e26ef0SDavid Greenman 	struct buf *bp;
290d5e26ef0SDavid Greenman {
291d5e26ef0SDavid Greenman 	int countvmpg;
292d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
293d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
294d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
295d5e26ef0SDavid Greenman 	vm_offset_t pa;
296d5e26ef0SDavid Greenman 	int dobounceflag = 0;
297d5e26ef0SDavid Greenman 	int i;
298d5e26ef0SDavid Greenman 
299d5e26ef0SDavid Greenman 	if (bouncepages == 0)
300d5e26ef0SDavid Greenman 		return;
301d5e26ef0SDavid Greenman 
302ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
303ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
304ae92ea44SDavid Greenman 		return;
3051561d038SDavid Greenman 	}
3061561d038SDavid Greenman 
307ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3083fb3086eSPoul-Henning Kamp 		printf(
3093fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
310ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
311ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
312ae92ea44SDavid Greenman 	}
313ae92ea44SDavid Greenman 
314ae92ea44SDavid Greenman /*
315ae92ea44SDavid Greenman  *  This is not really necessary
316ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
317ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
318ae92ea44SDavid Greenman  *	}
319ae92ea44SDavid Greenman  */
320ae92ea44SDavid Greenman 
321ae92ea44SDavid Greenman 
32226f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
32326f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
324d5e26ef0SDavid Greenman 
325d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
326d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
327d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
328d5e26ef0SDavid Greenman 
329d5e26ef0SDavid Greenman /*
330d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
331d5e26ef0SDavid Greenman  */
332d5e26ef0SDavid Greenman 	va = vapstart;
333d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
334ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
335d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
336d5e26ef0SDavid Greenman 			++dobounceflag;
3370d94caffSDavid Greenman 		if( pa == 0)
3380d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
339d5e26ef0SDavid Greenman 		va += NBPG;
340d5e26ef0SDavid Greenman 	}
341d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
342d5e26ef0SDavid Greenman 		return;
343d5e26ef0SDavid Greenman 
344d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
345d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
346d5e26ef0SDavid Greenman 
347d5e26ef0SDavid Greenman /*
348d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
349d5e26ef0SDavid Greenman  */
3501561d038SDavid Greenman 	kva = vm_bounce_kva(countvmpg*NBPG, 1);
351ae92ea44SDavid Greenman #if 0
352ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
353ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
354ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
355ae92ea44SDavid Greenman #endif
356d5e26ef0SDavid Greenman 	va = vapstart;
357d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
358ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
359d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
360d5e26ef0SDavid Greenman 			/*
361d5e26ef0SDavid Greenman 			 * allocate a replacement page
362d5e26ef0SDavid Greenman 			 */
363d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
3646b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), bpa);
365ae92ea44SDavid Greenman #if 0
366ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
367ae92ea44SDavid Greenman #endif
368d5e26ef0SDavid Greenman 			/*
369d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
370d5e26ef0SDavid Greenman 			 */
3711561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
372d5e26ef0SDavid Greenman 				bcopy((caddr_t) va, (caddr_t) kva + (NBPG * i), NBPG);
3731561d038SDavid Greenman 			}
374d5e26ef0SDavid Greenman 		} else {
375d5e26ef0SDavid Greenman 			/*
376d5e26ef0SDavid Greenman 			 * use original page
377d5e26ef0SDavid Greenman 			 */
3786b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), pa);
379d5e26ef0SDavid Greenman 		}
380d5e26ef0SDavid Greenman 		va += NBPG;
381d5e26ef0SDavid Greenman 	}
382d5e26ef0SDavid Greenman 
383d5e26ef0SDavid Greenman /*
384d5e26ef0SDavid Greenman  * flag the buffer as being bounced
385d5e26ef0SDavid Greenman  */
386d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
387d5e26ef0SDavid Greenman /*
388d5e26ef0SDavid Greenman  * save the original buffer kva
389d5e26ef0SDavid Greenman  */
39026f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
391d5e26ef0SDavid Greenman /*
392d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
393d5e26ef0SDavid Greenman  */
39426f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
395d5e26ef0SDavid Greenman 				((vm_offset_t) bp->b_savekva & (NBPG - 1)));
396ae92ea44SDavid Greenman #if 0
397ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
398ae92ea44SDavid Greenman #endif
399d5e26ef0SDavid Greenman 	return;
400d5e26ef0SDavid Greenman }
401d5e26ef0SDavid Greenman 
402d5e26ef0SDavid Greenman /*
403d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
404d5e26ef0SDavid Greenman  */
405d5e26ef0SDavid Greenman void
406d5e26ef0SDavid Greenman vm_bounce_free(bp)
407d5e26ef0SDavid Greenman 	struct buf *bp;
408d5e26ef0SDavid Greenman {
409d5e26ef0SDavid Greenman 	int i;
410ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
411d5e26ef0SDavid Greenman 
412d5e26ef0SDavid Greenman /*
413d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
414d5e26ef0SDavid Greenman  */
415d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
416d5e26ef0SDavid Greenman 		return;
417d5e26ef0SDavid Greenman 
418ae92ea44SDavid Greenman /*
419ae92ea44SDavid Greenman  *  This check is not necessary
420ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
421ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
422ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
423ae92ea44SDavid Greenman  *	}
424ae92ea44SDavid Greenman  */
425ae92ea44SDavid Greenman 
426d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
42726f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
428ae92ea44SDavid Greenman /*
429ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
430ae92ea44SDavid Greenman */
431d5e26ef0SDavid Greenman 
432d5e26ef0SDavid Greenman /*
433d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
434d5e26ef0SDavid Greenman  */
435ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
436d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
437d5e26ef0SDavid Greenman 		vm_offset_t copycount;
438d5e26ef0SDavid Greenman 
439d5e26ef0SDavid Greenman 		copycount = i386_round_page(bouncekva + 1) - bouncekva;
440ed7fcbd0SDavid Greenman 		mybouncepa = pmap_kextract(i386_trunc_page(bouncekva));
441d5e26ef0SDavid Greenman 
442d5e26ef0SDavid Greenman /*
443d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
444d5e26ef0SDavid Greenman  */
445ae92ea44SDavid Greenman 		if ( mybouncepa != pmap_kextract( i386_trunc_page( origkva))) {
446ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
447ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
448ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
449d5e26ef0SDavid Greenman /*
450d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
451d5e26ef0SDavid Greenman  */
452d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
453ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
454d5e26ef0SDavid Greenman /*
455d5e26ef0SDavid Greenman  * free the bounce allocation
456d5e26ef0SDavid Greenman  */
457ae92ea44SDavid Greenman 
458ae92ea44SDavid Greenman /*
459ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
460ae92ea44SDavid Greenman */
461ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
462d5e26ef0SDavid Greenman 		}
463d5e26ef0SDavid Greenman 
464d5e26ef0SDavid Greenman 		origkva += copycount;
465d5e26ef0SDavid Greenman 		bouncekva += copycount;
466ae92ea44SDavid Greenman 		i += copycount;
467d5e26ef0SDavid Greenman 	}
468d5e26ef0SDavid Greenman 
469d5e26ef0SDavid Greenman /*
470ae92ea44SDavid Greenman 	printf("\n");
471ae92ea44SDavid Greenman */
472ae92ea44SDavid Greenman /*
473d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
474d5e26ef0SDavid Greenman  */
475ae92ea44SDavid Greenman 
47626f9a767SRodney W. Grimes 	bouncekva= i386_trunc_page((vm_offset_t) bp->b_data);
477ae92ea44SDavid Greenman 	bouncekvaend= i386_round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
478ae92ea44SDavid Greenman 
479ae92ea44SDavid Greenman /*
480ae92ea44SDavid Greenman 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / NBPG);
481ae92ea44SDavid Greenman */
482ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
48326f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
484d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
485d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
486d5e26ef0SDavid Greenman 
487d5e26ef0SDavid Greenman 	return;
488d5e26ef0SDavid Greenman }
489d5e26ef0SDavid Greenman 
490ae92ea44SDavid Greenman 
4915b81b6b3SRodney W. Grimes /*
4921561d038SDavid Greenman  * init the bounce buffer system
4931561d038SDavid Greenman  */
4941561d038SDavid Greenman void
4951561d038SDavid Greenman vm_bounce_init()
4961561d038SDavid Greenman {
497ae92ea44SDavid Greenman 	int i;
4981561d038SDavid Greenman 
4991561d038SDavid Greenman 	kvasfreecnt = 0;
5001561d038SDavid Greenman 
5011561d038SDavid Greenman 	if (bouncepages == 0)
5021561d038SDavid Greenman 		return;
5031561d038SDavid Greenman 
5041561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5051561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5061561d038SDavid Greenman 
5071561d038SDavid Greenman 	if (!bounceallocarray)
508edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5091561d038SDavid Greenman 
510ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
511ae92ea44SDavid Greenman 	if (!bouncepa)
512edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5131561d038SDavid Greenman 
5140d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5150d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5160d94caffSDavid Greenman 	}
5170d94caffSDavid Greenman 
518ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
519ae92ea44SDavid Greenman 		vm_offset_t pa;
520ae92ea44SDavid Greenman 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * NBPG)) >= SIXTEENMEG)
521ae92ea44SDavid Greenman 			panic("bounce memory out of range");
522ae92ea44SDavid Greenman 		if( pa == 0)
523ae92ea44SDavid Greenman 			panic("bounce memory not resident");
524ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5250d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
526ae92ea44SDavid Greenman 	}
5271561d038SDavid Greenman 	bouncefree = bouncepages;
5281561d038SDavid Greenman 
5291561d038SDavid Greenman }
5309aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5316c146e28SBruce Evans 
532a4f7a4c9SDavid Greenman /*
533a4f7a4c9SDavid Greenman  * quick version of vm_fault
534a4f7a4c9SDavid Greenman  */
535a4f7a4c9SDavid Greenman void
536a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5376c146e28SBruce Evans 	caddr_t v;
538a4f7a4c9SDavid Greenman 	int prot;
539a4f7a4c9SDavid Greenman {
5401d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5416c146e28SBruce Evans 		subyte(v, fubyte(v));
542a4f7a4c9SDavid Greenman 	else
5436c146e28SBruce Evans 		fubyte(v);
544a4f7a4c9SDavid Greenman }
545a4f7a4c9SDavid Greenman 
5461561d038SDavid Greenman /*
5475b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
5485b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
5495b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
5505b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
5515b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
5525b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
5535b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
5545b81b6b3SRodney W. Grimes  */
555381fe1aaSGarrett Wollman int
5565b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5575b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5585b81b6b3SRodney W. Grimes {
5595b81b6b3SRodney W. Grimes 	register struct user *up = p2->p_addr;
5603fb3086eSPoul-Henning Kamp 	int offset;
5615b81b6b3SRodney W. Grimes 
5625b81b6b3SRodney W. Grimes 	/*
5635b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
5645b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
5655b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
5665b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
5675b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
5685b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
5695b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
5705b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
5715b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
5725b81b6b3SRodney W. Grimes 	 */
5735b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
5745b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
5755b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
5765b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
57726f9a767SRodney W. Grimes 	p2->p_md.md_regs = p1->p_md.md_regs;
5785b81b6b3SRodney W. Grimes 
5795b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
5805b81b6b3SRodney W. Grimes 
5815b81b6b3SRodney W. Grimes 	/*
5825b81b6b3SRodney W. Grimes 	 *
5835b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
5845b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
5855b81b6b3SRodney W. Grimes 	 */
5863fb3086eSPoul-Henning Kamp 	if (savectx(&up->u_pcb, 1)) {
5875b81b6b3SRodney W. Grimes 		/*
5885b81b6b3SRodney W. Grimes 		 * Return 1 in child.
5895b81b6b3SRodney W. Grimes 		 */
5905b81b6b3SRodney W. Grimes 		return (1);
5915b81b6b3SRodney W. Grimes 	}
5925b81b6b3SRodney W. Grimes 	return (0);
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 {
5995b81b6b3SRodney W. Grimes 
600960173b9SRodney W. Grimes #if NNPX > 0
6015b81b6b3SRodney W. Grimes 	npxexit(p);
602960173b9SRodney W. Grimes #endif	/* NNPX */
6031a051896SBruce Evans 	cnt.v_swtch++;
6041a051896SBruce Evans 	cpu_switch(p);
6057c2b54e8SNate Williams 	panic("cpu_exit");
6065b81b6b3SRodney W. Grimes }
6075b81b6b3SRodney W. Grimes 
608381fe1aaSGarrett Wollman void
6097f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
6107f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
6115b81b6b3SRodney W. Grimes 
6125b81b6b3SRodney W. Grimes 	/* drop per-process resources */
6130d94caffSDavid Greenman  	pmap_remove(vm_map_pmap(u_map), (vm_offset_t) p->p_addr,
6147f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
6150d94caffSDavid Greenman 	kmem_free(u_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
6167f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
6175b81b6b3SRodney W. Grimes }
6185b81b6b3SRodney W. Grimes 
6195b81b6b3SRodney W. Grimes /*
62026f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
62126f9a767SRodney W. Grimes  */
62226f9a767SRodney W. Grimes int
62326f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
62426f9a767SRodney W. Grimes 	struct proc *p;
62526f9a767SRodney W. Grimes 	struct vnode *vp;
62626f9a767SRodney W. Grimes 	struct ucred *cred;
62726f9a767SRodney W. Grimes {
62826f9a767SRodney W. Grimes 
62926f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
63026f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
63126f9a767SRodney W. Grimes 	    p));
63226f9a767SRodney W. Grimes }
63326f9a767SRodney W. Grimes 
63426f9a767SRodney W. Grimes /*
6355b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
6365b81b6b3SRodney W. Grimes  */
637381fe1aaSGarrett Wollman void
6385b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
6395b81b6b3SRodney W. Grimes 	u_short *pte;
6405b81b6b3SRodney W. Grimes 	caddr_t vaddr;
6415b81b6b3SRodney W. Grimes {
6425b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
6435b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
6445b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
6455b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
6465b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
6475b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
6485b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
6495b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
6505b81b6b3SRodney W. Grimes }
6515b81b6b3SRodney W. Grimes 
6525b81b6b3SRodney W. Grimes /*
65326f9a767SRodney W. Grimes  * Move pages from one kernel virtual address to another.
65426f9a767SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
65526f9a767SRodney W. Grimes  * and size must be a multiple of CLSIZE.
65626f9a767SRodney W. Grimes  */
65726f9a767SRodney W. Grimes 
65826f9a767SRodney W. Grimes void
65926f9a767SRodney W. Grimes pagemove(from, to, size)
66026f9a767SRodney W. Grimes 	register caddr_t from, to;
66126f9a767SRodney W. Grimes 	int size;
66226f9a767SRodney W. Grimes {
66326f9a767SRodney W. Grimes 	register vm_offset_t pa;
66426f9a767SRodney W. Grimes 
66526f9a767SRodney W. Grimes 	if (size & CLOFSET)
66626f9a767SRodney W. Grimes 		panic("pagemove");
66726f9a767SRodney W. Grimes 	while (size > 0) {
66826f9a767SRodney W. Grimes 		pa = pmap_kextract((vm_offset_t)from);
66926f9a767SRodney W. Grimes 		if (pa == 0)
67026f9a767SRodney W. Grimes 			panic("pagemove 2");
67126f9a767SRodney W. Grimes 		if (pmap_kextract((vm_offset_t)to) != 0)
67226f9a767SRodney W. Grimes 			panic("pagemove 3");
673c564966bSDavid Greenman 		pmap_kremove((vm_offset_t)from);
67426f9a767SRodney W. Grimes 		pmap_kenter((vm_offset_t)to, pa);
67526f9a767SRodney W. Grimes 		from += PAGE_SIZE;
67626f9a767SRodney W. Grimes 		to += PAGE_SIZE;
67726f9a767SRodney W. Grimes 		size -= PAGE_SIZE;
67826f9a767SRodney W. Grimes 	}
67926f9a767SRodney W. Grimes }
68026f9a767SRodney W. Grimes 
68126f9a767SRodney W. Grimes /*
6825b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
6835b81b6b3SRodney W. Grimes  */
684aaf08d94SGarrett Wollman u_long
6857f8cb368SDavid Greenman kvtop(void *addr)
6865b81b6b3SRodney W. Grimes {
6875b81b6b3SRodney W. Grimes 	vm_offset_t va;
6885b81b6b3SRodney W. Grimes 
689ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
6905b81b6b3SRodney W. Grimes 	if (va == 0)
6915b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
6927f8cb368SDavid Greenman 	return((int)va);
6935b81b6b3SRodney W. Grimes }
6945b81b6b3SRodney W. Grimes 
6955b81b6b3SRodney W. Grimes /*
696ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
6975b81b6b3SRodney W. Grimes  *
698ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
699ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
700ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
7015b81b6b3SRodney W. Grimes  */
702381fe1aaSGarrett Wollman void
7035b81b6b3SRodney W. Grimes vmapbuf(bp)
7045b81b6b3SRodney W. Grimes 	register struct buf *bp;
7055b81b6b3SRodney W. Grimes {
7065b81b6b3SRodney W. Grimes 	register int npf;
7075b81b6b3SRodney W. Grimes 	register caddr_t addr;
7085b81b6b3SRodney W. Grimes 	int off;
7095b81b6b3SRodney W. Grimes 	vm_offset_t kva;
710641a27e6SJohn Dyson 	vm_offset_t pa, v;
7115b81b6b3SRodney W. Grimes 
71226f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7135b81b6b3SRodney W. Grimes 		panic("vmapbuf");
71426f9a767SRodney W. Grimes 
715ae92ea44SDavid Greenman 	/*
716ae92ea44SDavid Greenman 	 * this is the kva that is to be used for
717ae92ea44SDavid Greenman 	 * the temporary kernel mapping
718ae92ea44SDavid Greenman 	 */
719ae92ea44SDavid Greenman 	kva = (vm_offset_t) bp->b_saveaddr;
720ae92ea44SDavid Greenman 
72126f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
72226f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
72326f9a767SRodney W. Grimes 		addr += PAGE_SIZE) {
72426f9a767SRodney W. Grimes 
72526f9a767SRodney W. Grimes /*
72626f9a767SRodney W. Grimes  * do the vm_fault if needed, do the copy-on-write thing when
72726f9a767SRodney W. Grimes  * reading stuff off device into memory.
72826f9a767SRodney W. Grimes  */
72926f9a767SRodney W. Grimes 		vm_fault_quick(addr,
73026f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
731c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t) addr);
732641a27e6SJohn Dyson 		if (pa == 0)
733641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
73426f9a767SRodney W. Grimes /*
73526f9a767SRodney W. Grimes  * hold the data page
73626f9a767SRodney W. Grimes  */
737641a27e6SJohn Dyson #ifdef DIAGNOSTIC
738641a27e6SJohn Dyson 		if( VM_PAGE_TO_PHYS(PHYS_TO_VM_PAGE(pa)) != pa)
739641a27e6SJohn Dyson 			panic("vmapbuf: confused PHYS_TO_VM_PAGE mapping");
740641a27e6SJohn Dyson #endif
74126f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
74226f9a767SRodney W. Grimes 	}
74326f9a767SRodney W. Grimes 
744ae92ea44SDavid Greenman 	addr = bp->b_saveaddr = bp->b_data;
7455b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
746ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + off));
747ae92ea44SDavid Greenman 	bp->b_data = (caddr_t) (kva + off);
7485b81b6b3SRodney W. Grimes 	while (npf--) {
749c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t)addr);
7505b81b6b3SRodney W. Grimes 		if (pa == 0)
7515b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
7526b4ac811SDavid Greenman 		pmap_kenter(kva, trunc_page(pa));
7535b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
7545b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
7555b81b6b3SRodney W. Grimes 	}
7565b81b6b3SRodney W. Grimes }
7575b81b6b3SRodney W. Grimes 
7585b81b6b3SRodney W. Grimes /*
7595b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
7605b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
7615b81b6b3SRodney W. Grimes  */
762381fe1aaSGarrett Wollman void
7635b81b6b3SRodney W. Grimes vunmapbuf(bp)
7645b81b6b3SRodney W. Grimes 	register struct buf *bp;
7655b81b6b3SRodney W. Grimes {
766c564966bSDavid Greenman 	register caddr_t addr;
767641a27e6SJohn Dyson 	vm_offset_t v,pa;
7685b81b6b3SRodney W. Grimes 
7695b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7705b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
771c564966bSDavid Greenman 
772c564966bSDavid Greenman 	for (addr = (caddr_t)trunc_page((vm_offset_t) bp->b_data);
773c564966bSDavid Greenman 		addr < bp->b_data + bp->b_bufsize;
774c564966bSDavid Greenman 		addr += NBPG)
775c564966bSDavid Greenman 		pmap_kremove((vm_offset_t) addr);
776c564966bSDavid Greenman 
777ae92ea44SDavid Greenman 	bp->b_data = bp->b_saveaddr;
7785b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
77926f9a767SRodney W. Grimes 
78026f9a767SRodney W. Grimes /*
78126f9a767SRodney W. Grimes  * unhold the pde, and data pages
78226f9a767SRodney W. Grimes  */
783c564966bSDavid Greenman 	for (addr = (caddr_t)trunc_page((vm_offset_t) bp->b_data);
78426f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
78526f9a767SRodney W. Grimes 		addr += NBPG) {
78626f9a767SRodney W. Grimes 	/*
78726f9a767SRodney W. Grimes 	 * release the data page
78826f9a767SRodney W. Grimes 	 */
789c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t) addr);
79026f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
79126f9a767SRodney W. Grimes 	}
7925b81b6b3SRodney W. Grimes }
7935b81b6b3SRodney W. Grimes 
7945b81b6b3SRodney W. Grimes /*
7955b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
7965b81b6b3SRodney W. Grimes  */
7977f8cb368SDavid Greenman void
7985b81b6b3SRodney W. Grimes cpu_reset() {
7995b81b6b3SRodney W. Grimes 
8002320728fSRodney W. Grimes 	/*
8012320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
8022320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
8032320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
8042320728fSRodney W. Grimes 	 */
8055eb46edfSDavid Greenman 
8065eb46edfSDavid Greenman #ifndef BROKEN_KEYBOARD_RESET
8072320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
8082320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
8092320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
8102320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
8115eb46edfSDavid Greenman #endif
8122320728fSRodney W. Grimes 
8135b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
8145b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
8155b81b6b3SRodney W. Grimes 
8165b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
8172c7a40c7SDavid Greenman 	pmap_update();
8185b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8197f8cb368SDavid Greenman 	while(1);
8205b81b6b3SRodney W. Grimes }
821b9d60b3fSDavid Greenman 
822b9d60b3fSDavid Greenman /*
823b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
82429360eb0SDavid Greenman  *	chunks of SGROWSIZ.
825b9d60b3fSDavid Greenman  */
826b9d60b3fSDavid Greenman int
827b9d60b3fSDavid Greenman grow(p, sp)
828b9d60b3fSDavid Greenman 	struct proc *p;
82926f9a767SRodney W. Grimes 	u_int sp;
830b9d60b3fSDavid Greenman {
831b9d60b3fSDavid Greenman 	unsigned int nss;
832b9d60b3fSDavid Greenman 	caddr_t v;
833b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
834b9d60b3fSDavid Greenman 
835b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
836b9d60b3fSDavid Greenman 	    return (1);
837b9d60b3fSDavid Greenman 
838b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
839b9d60b3fSDavid Greenman 
840b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
841b9d60b3fSDavid Greenman 		return (0);
842b9d60b3fSDavid Greenman 
843b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
84429360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
845b9d60b3fSDavid Greenman 		int grow_amount;
846b9d60b3fSDavid Greenman 		/*
847b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
848b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
849b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
850b9d60b3fSDavid Greenman 		 * is expensive).
851b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
85229360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
853b9d60b3fSDavid Greenman 		 */
85429360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
855b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
85629360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
857b9d60b3fSDavid Greenman 		/*
85829360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
859b9d60b3fSDavid Greenman 		 * just extend to the maximum size
860b9d60b3fSDavid Greenman 		 */
861b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
862b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
863b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
864b9d60b3fSDavid Greenman 		}
86568940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
86668940ac1SDavid Greenman 		    grow_amount, FALSE) != KERN_SUCCESS)) {
867b9d60b3fSDavid Greenman 			return (0);
868b9d60b3fSDavid Greenman 		}
869b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
870b9d60b3fSDavid Greenman 	}
871b9d60b3fSDavid Greenman 
872b9d60b3fSDavid Greenman 	return (1);
873b9d60b3fSDavid Greenman }
874a29b63cbSJohn Dyson 
875a29b63cbSJohn Dyson /*
876a29b63cbSJohn Dyson  * prototype routine to implement the pre-zeroed page mechanism
877a29b63cbSJohn Dyson  * this routine is called from the idle loop.
878a29b63cbSJohn Dyson  */
879a29b63cbSJohn Dyson int
880a29b63cbSJohn Dyson vm_page_zero_idle() {
881a29b63cbSJohn Dyson 	vm_page_t m;
882a29b63cbSJohn Dyson 	if ((cnt.v_free_count > cnt.v_interrupt_free_min) &&
883a29b63cbSJohn Dyson 		(m = vm_page_queue_free.tqh_first)) {
884a29b63cbSJohn Dyson 		TAILQ_REMOVE(&vm_page_queue_free, m, pageq);
885a29b63cbSJohn Dyson 		enable_intr();
886a29b63cbSJohn Dyson 		pmap_zero_page(VM_PAGE_TO_PHYS(m));
887a29b63cbSJohn Dyson 		disable_intr();
88823e56ee8SDavid Greenman 		TAILQ_INSERT_HEAD(&vm_page_queue_zero, m, pageq);
889a29b63cbSJohn Dyson 		return 1;
890a29b63cbSJohn Dyson 	}
891a29b63cbSJohn Dyson 	return 0;
892a29b63cbSJohn Dyson }
893