xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 87b911575102f86e2cbceac54e8b0518d27200cd)
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$
4187b91157SPoul-Henning Kamp  *	$Id: vm_machdep.c,v 1.46 1995/12/07 12:45:40 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>
51efeaf95aSDavid Greenman #include <sys/vmmeter.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>
58efeaf95aSDavid Greenman #include <vm/vm_param.h>
59efeaf95aSDavid Greenman #include <vm/vm_prot.h>
60efeaf95aSDavid Greenman #include <vm/lock.h>
6126f9a767SRodney W. Grimes #include <vm/vm_kern.h>
6224a1cce3SDavid Greenman #include <vm/vm_page.h>
63efeaf95aSDavid Greenman #include <vm/vm_map.h>
64efeaf95aSDavid Greenman #include <vm/vm_extern.h>
65efeaf95aSDavid Greenman 
66efeaf95aSDavid Greenman #include <sys/user.h>
675b81b6b3SRodney W. Grimes 
682320728fSRodney W. Grimes #include <i386/isa/isa.h>
692320728fSRodney W. Grimes 
706c146e28SBruce Evans extern void	pagemove __P((caddr_t from, caddr_t to, int size));
7187b91157SPoul-Henning Kamp static void	setredzone __P((u_short *pte, caddr_t vaddr));
7287b91157SPoul-Henning Kamp static void	vm_fault_quick __P((caddr_t v, int prot));
736c146e28SBruce Evans 
749aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
7587b91157SPoul-Henning Kamp static vm_offset_t
766c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
7787b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
786c146e28SBruce Evans 					int now));
7987b91157SPoul-Henning Kamp static vm_offset_t
806c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
8187b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
826c146e28SBruce Evans 
8387b91157SPoul-Henning Kamp static static volatile int	kvasfreecnt;
84ae92ea44SDavid Greenman 
85d5e26ef0SDavid Greenman caddr_t		bouncememory;
8687b91157SPoul-Henning Kamp int		bouncepages;
8787b91157SPoul-Henning Kamp static int	bpwait;
8887b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
8987b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
90d5e26ef0SDavid Greenman 
91ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
9287b91157SPoul-Henning Kamp static int		bounceallocarraysize;
9387b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
9487b91157SPoul-Henning Kamp static int		bouncefree;
95d5e26ef0SDavid Greenman 
96d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
97ae92ea44SDavid Greenman #define MAXBKVA 1024
9826f9a767SRodney W. Grimes int		maxbkva = MAXBKVA*NBPG;
99d5e26ef0SDavid Greenman 
100d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
10187b91157SPoul-Henning Kamp static struct kvasfree {
102d5e26ef0SDavid Greenman 	vm_offset_t addr;
103d5e26ef0SDavid Greenman 	vm_offset_t size;
104d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
105d5e26ef0SDavid Greenman 
106d5e26ef0SDavid Greenman /*
107d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
108d5e26ef0SDavid Greenman  * (only 1 inplemented now)
109d5e26ef0SDavid Greenman  */
11087b91157SPoul-Henning Kamp static vm_offset_t
111d5e26ef0SDavid Greenman vm_bounce_page_find(count)
112d5e26ef0SDavid Greenman 	int count;
113d5e26ef0SDavid Greenman {
114d5e26ef0SDavid Greenman 	int bit;
115d5e26ef0SDavid Greenman 	int s,i;
116d5e26ef0SDavid Greenman 
117d5e26ef0SDavid Greenman 	if (count != 1)
118d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
119d5e26ef0SDavid Greenman 
120d5e26ef0SDavid Greenman 	s = splbio();
121d5e26ef0SDavid Greenman retry:
122d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
123d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1243fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1253fb3086eSPoul-Henning Kamp 			if (bit) {
126d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
127d5e26ef0SDavid Greenman 				bouncefree -= count;
128d5e26ef0SDavid Greenman 				splx(s);
129ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
130d5e26ef0SDavid Greenman 			}
131d5e26ef0SDavid Greenman 		}
132d5e26ef0SDavid Greenman 	}
133ed7fcbd0SDavid Greenman 	bpwait = 1;
134d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
135d5e26ef0SDavid Greenman 	goto retry;
136d5e26ef0SDavid Greenman }
137d5e26ef0SDavid Greenman 
13887b91157SPoul-Henning Kamp static void
1391561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1401561d038SDavid Greenman 	vm_offset_t addr;
1411561d038SDavid Greenman 	vm_offset_t size;
1421561d038SDavid Greenman 	int now;
1431561d038SDavid Greenman {
1441561d038SDavid Greenman 	int s = splbio();
1451561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
146ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
147ae92ea44SDavid Greenman 	++kvasfreecnt;
1480e195446SDavid Greenman 	if( now) {
1490e195446SDavid Greenman 		/*
1500e195446SDavid Greenman 		 * this will do wakeups
1510e195446SDavid Greenman 		 */
1521561d038SDavid Greenman 		vm_bounce_kva(0,0);
1530e195446SDavid Greenman 	} else {
1540e195446SDavid Greenman 		if (bmwait) {
1550e195446SDavid Greenman 		/*
1560e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1570e195446SDavid Greenman 		 */
1581561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1590e195446SDavid Greenman 			bmwait = 0;
1600e195446SDavid Greenman 		}
1610e195446SDavid Greenman 	}
1621561d038SDavid Greenman 	splx(s);
1631561d038SDavid Greenman }
1641561d038SDavid Greenman 
165d5e26ef0SDavid Greenman /*
166d5e26ef0SDavid Greenman  * free count bounce buffer pages
167d5e26ef0SDavid Greenman  */
16887b91157SPoul-Henning Kamp static void
169d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
170d5e26ef0SDavid Greenman 	vm_offset_t pa;
171d5e26ef0SDavid Greenman 	int count;
172d5e26ef0SDavid Greenman {
173d5e26ef0SDavid Greenman 	int allocindex;
174d5e26ef0SDavid Greenman 	int index;
175d5e26ef0SDavid Greenman 	int bit;
176d5e26ef0SDavid Greenman 
177d5e26ef0SDavid Greenman 	if (count != 1)
178edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
179d5e26ef0SDavid Greenman 
180ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
181ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
182ae92ea44SDavid Greenman 			break;
183ae92ea44SDavid Greenman 	}
184d5e26ef0SDavid Greenman 
185ae92ea44SDavid Greenman 	if( index == bouncepages)
186ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
187d5e26ef0SDavid Greenman 
188ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
189ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
190d5e26ef0SDavid Greenman 
191d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
192d5e26ef0SDavid Greenman 
193d5e26ef0SDavid Greenman 	bouncefree += count;
194ed7fcbd0SDavid Greenman 	if (bpwait) {
195ed7fcbd0SDavid Greenman 		bpwait = 0;
196d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
197d5e26ef0SDavid Greenman 	}
198ed7fcbd0SDavid Greenman }
199d5e26ef0SDavid Greenman 
200d5e26ef0SDavid Greenman /*
201d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
202d5e26ef0SDavid Greenman  */
20387b91157SPoul-Henning Kamp static vm_offset_t
204ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
205ae92ea44SDavid Greenman 	int size;
2061561d038SDavid Greenman 	int waitok;
207d5e26ef0SDavid Greenman {
208d5e26ef0SDavid Greenman 	int i;
2096b4ac811SDavid Greenman 	vm_offset_t kva = 0;
210c564966bSDavid Greenman 	vm_offset_t off;
211d5e26ef0SDavid Greenman 	int s = splbio();
212d5e26ef0SDavid Greenman more:
213ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
214d5e26ef0SDavid Greenman 		bmfreeing = 1;
215ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
216c564966bSDavid Greenman 			for(off=0;off<kvaf[i].size;off+=NBPG) {
217c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
218c564966bSDavid Greenman 			}
2191561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
220d5e26ef0SDavid Greenman 				kvaf[i].size);
221d5e26ef0SDavid Greenman 		}
222d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
223d5e26ef0SDavid Greenman 		bmfreeing = 0;
224ae92ea44SDavid Greenman 		if( bmwait) {
225ae92ea44SDavid Greenman 			bmwait = 0;
226ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
227ae92ea44SDavid Greenman 		}
228d5e26ef0SDavid Greenman 	}
229d5e26ef0SDavid Greenman 
2301561d038SDavid Greenman 	if( size == 0) {
2311561d038SDavid Greenman 		splx(s);
2321561d038SDavid Greenman 		return NULL;
2331561d038SDavid Greenman 	}
2341561d038SDavid Greenman 
235ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2361561d038SDavid Greenman 		if( !waitok) {
2371561d038SDavid Greenman 			splx(s);
2381561d038SDavid Greenman 			return NULL;
2391561d038SDavid Greenman 		}
240d5e26ef0SDavid Greenman 		bmwait = 1;
2411561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
242d5e26ef0SDavid Greenman 		goto more;
243d5e26ef0SDavid Greenman 	}
244d5e26ef0SDavid Greenman 	splx(s);
245d5e26ef0SDavid Greenman 	return kva;
246d5e26ef0SDavid Greenman }
247d5e26ef0SDavid Greenman 
248d5e26ef0SDavid Greenman /*
249ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2500e195446SDavid Greenman  */
2510e195446SDavid Greenman vm_offset_t
2520e195446SDavid Greenman vm_bounce_kva_alloc(count)
2530e195446SDavid Greenman int count;
2540e195446SDavid Greenman {
2550e195446SDavid Greenman 	int i;
2560e195446SDavid Greenman 	vm_offset_t kva;
2570e195446SDavid Greenman 	vm_offset_t pa;
2580e195446SDavid Greenman 	if( bouncepages == 0) {
2590e195446SDavid Greenman 		kva = (vm_offset_t) malloc(count*NBPG, M_TEMP, M_WAITOK);
2600e195446SDavid Greenman 		return kva;
2610e195446SDavid Greenman 	}
262ae92ea44SDavid Greenman 	kva = vm_bounce_kva(count*NBPG, 1);
2630e195446SDavid Greenman 	for(i=0;i<count;i++) {
2640e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
2650e195446SDavid Greenman 		pmap_kenter(kva + i * NBPG, pa);
2660e195446SDavid Greenman 	}
2670e195446SDavid Greenman 	return kva;
2680e195446SDavid Greenman }
2690e195446SDavid Greenman 
2700e195446SDavid Greenman /*
2710e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2720e195446SDavid Greenman  */
2730e195446SDavid Greenman void
2740e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2750e195446SDavid Greenman 	vm_offset_t kva;
2760e195446SDavid Greenman 	int count;
2770e195446SDavid Greenman {
2780e195446SDavid Greenman 	int i;
2790e195446SDavid Greenman 	vm_offset_t pa;
2800e195446SDavid Greenman 	if( bouncepages == 0) {
2810e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2820e195446SDavid Greenman 		return;
2830e195446SDavid Greenman 	}
2840e195446SDavid Greenman 	for(i = 0; i < count; i++) {
2850e195446SDavid Greenman 		pa = pmap_kextract(kva + i * NBPG);
2860e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2870e195446SDavid Greenman 	}
288ae92ea44SDavid Greenman 	vm_bounce_kva_free(kva, count*NBPG, 0);
2890e195446SDavid Greenman }
2900e195446SDavid Greenman 
2910e195446SDavid Greenman /*
292d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
293d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
294d5e26ef0SDavid Greenman  */
295d5e26ef0SDavid Greenman void
296d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
297d5e26ef0SDavid Greenman 	struct buf *bp;
298d5e26ef0SDavid Greenman {
299d5e26ef0SDavid Greenman 	int countvmpg;
300d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
301d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
302d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
303d5e26ef0SDavid Greenman 	vm_offset_t pa;
304d5e26ef0SDavid Greenman 	int dobounceflag = 0;
305d5e26ef0SDavid Greenman 	int i;
306d5e26ef0SDavid Greenman 
307d5e26ef0SDavid Greenman 	if (bouncepages == 0)
308d5e26ef0SDavid Greenman 		return;
309d5e26ef0SDavid Greenman 
310ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
311ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
312ae92ea44SDavid Greenman 		return;
3131561d038SDavid Greenman 	}
3141561d038SDavid Greenman 
315ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3163fb3086eSPoul-Henning Kamp 		printf(
3173fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
318ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
319ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
320ae92ea44SDavid Greenman 	}
321ae92ea44SDavid Greenman 
322ae92ea44SDavid Greenman /*
323ae92ea44SDavid Greenman  *  This is not really necessary
324ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
325ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
326ae92ea44SDavid Greenman  *	}
327ae92ea44SDavid Greenman  */
328ae92ea44SDavid Greenman 
329ae92ea44SDavid Greenman 
33026f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
33126f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
332d5e26ef0SDavid Greenman 
333d5e26ef0SDavid Greenman 	vapstart = i386_trunc_page(vastart);
334d5e26ef0SDavid Greenman 	vapend = i386_round_page(vaend);
335d5e26ef0SDavid Greenman 	countvmpg = (vapend - vapstart) / NBPG;
336d5e26ef0SDavid Greenman 
337d5e26ef0SDavid Greenman /*
338d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
339d5e26ef0SDavid Greenman  */
340d5e26ef0SDavid Greenman 	va = vapstart;
341d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
342ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
343d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
344d5e26ef0SDavid Greenman 			++dobounceflag;
3450d94caffSDavid Greenman 		if( pa == 0)
3460d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
347d5e26ef0SDavid Greenman 		va += NBPG;
348d5e26ef0SDavid Greenman 	}
349d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
350d5e26ef0SDavid Greenman 		return;
351d5e26ef0SDavid Greenman 
352d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
353d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
354d5e26ef0SDavid Greenman 
355d5e26ef0SDavid Greenman /*
356d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
357d5e26ef0SDavid Greenman  */
3581561d038SDavid Greenman 	kva = vm_bounce_kva(countvmpg*NBPG, 1);
359ae92ea44SDavid Greenman #if 0
360ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
361ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
362ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
363ae92ea44SDavid Greenman #endif
364d5e26ef0SDavid Greenman 	va = vapstart;
365d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
366ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
367d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
368d5e26ef0SDavid Greenman 			/*
369d5e26ef0SDavid Greenman 			 * allocate a replacement page
370d5e26ef0SDavid Greenman 			 */
371d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
3726b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), bpa);
373ae92ea44SDavid Greenman #if 0
374ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
375ae92ea44SDavid Greenman #endif
376d5e26ef0SDavid Greenman 			/*
377d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
378d5e26ef0SDavid Greenman 			 */
3791561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
380d5e26ef0SDavid Greenman 				bcopy((caddr_t) va, (caddr_t) kva + (NBPG * i), NBPG);
3811561d038SDavid Greenman 			}
382d5e26ef0SDavid Greenman 		} else {
383d5e26ef0SDavid Greenman 			/*
384d5e26ef0SDavid Greenman 			 * use original page
385d5e26ef0SDavid Greenman 			 */
3866b4ac811SDavid Greenman 			pmap_kenter(kva + (NBPG * i), pa);
387d5e26ef0SDavid Greenman 		}
388d5e26ef0SDavid Greenman 		va += NBPG;
389d5e26ef0SDavid Greenman 	}
390d5e26ef0SDavid Greenman 
391d5e26ef0SDavid Greenman /*
392d5e26ef0SDavid Greenman  * flag the buffer as being bounced
393d5e26ef0SDavid Greenman  */
394d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
395d5e26ef0SDavid Greenman /*
396d5e26ef0SDavid Greenman  * save the original buffer kva
397d5e26ef0SDavid Greenman  */
39826f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
399d5e26ef0SDavid Greenman /*
400d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
401d5e26ef0SDavid Greenman  */
40226f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
403d5e26ef0SDavid Greenman 				((vm_offset_t) bp->b_savekva & (NBPG - 1)));
404ae92ea44SDavid Greenman #if 0
405ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
406ae92ea44SDavid Greenman #endif
407d5e26ef0SDavid Greenman 	return;
408d5e26ef0SDavid Greenman }
409d5e26ef0SDavid Greenman 
410d5e26ef0SDavid Greenman /*
411d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
412d5e26ef0SDavid Greenman  */
413d5e26ef0SDavid Greenman void
414d5e26ef0SDavid Greenman vm_bounce_free(bp)
415d5e26ef0SDavid Greenman 	struct buf *bp;
416d5e26ef0SDavid Greenman {
417d5e26ef0SDavid Greenman 	int i;
418ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
419d5e26ef0SDavid Greenman 
420d5e26ef0SDavid Greenman /*
421d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
422d5e26ef0SDavid Greenman  */
423d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
424d5e26ef0SDavid Greenman 		return;
425d5e26ef0SDavid Greenman 
426ae92ea44SDavid Greenman /*
427ae92ea44SDavid Greenman  *  This check is not necessary
428ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
429ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
430ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
431ae92ea44SDavid Greenman  *	}
432ae92ea44SDavid Greenman  */
433ae92ea44SDavid Greenman 
434d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
43526f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
436ae92ea44SDavid Greenman /*
437ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
438ae92ea44SDavid Greenman */
439d5e26ef0SDavid Greenman 
440d5e26ef0SDavid Greenman /*
441d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
442d5e26ef0SDavid Greenman  */
443ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
444d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
445d5e26ef0SDavid Greenman 		vm_offset_t copycount;
446d5e26ef0SDavid Greenman 
447d5e26ef0SDavid Greenman 		copycount = i386_round_page(bouncekva + 1) - bouncekva;
448ed7fcbd0SDavid Greenman 		mybouncepa = pmap_kextract(i386_trunc_page(bouncekva));
449d5e26ef0SDavid Greenman 
450d5e26ef0SDavid Greenman /*
451d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
452d5e26ef0SDavid Greenman  */
453ae92ea44SDavid Greenman 		if ( mybouncepa != pmap_kextract( i386_trunc_page( origkva))) {
454ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
455ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
456ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
457d5e26ef0SDavid Greenman /*
458d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
459d5e26ef0SDavid Greenman  */
460d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
461ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
462d5e26ef0SDavid Greenman /*
463d5e26ef0SDavid Greenman  * free the bounce allocation
464d5e26ef0SDavid Greenman  */
465ae92ea44SDavid Greenman 
466ae92ea44SDavid Greenman /*
467ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
468ae92ea44SDavid Greenman */
469ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
470d5e26ef0SDavid Greenman 		}
471d5e26ef0SDavid Greenman 
472d5e26ef0SDavid Greenman 		origkva += copycount;
473d5e26ef0SDavid Greenman 		bouncekva += copycount;
474ae92ea44SDavid Greenman 		i += copycount;
475d5e26ef0SDavid Greenman 	}
476d5e26ef0SDavid Greenman 
477d5e26ef0SDavid Greenman /*
478ae92ea44SDavid Greenman 	printf("\n");
479ae92ea44SDavid Greenman */
480ae92ea44SDavid Greenman /*
481d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
482d5e26ef0SDavid Greenman  */
483ae92ea44SDavid Greenman 
48426f9a767SRodney W. Grimes 	bouncekva= i386_trunc_page((vm_offset_t) bp->b_data);
485ae92ea44SDavid Greenman 	bouncekvaend= i386_round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
486ae92ea44SDavid Greenman 
487ae92ea44SDavid Greenman /*
488ae92ea44SDavid Greenman 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / NBPG);
489ae92ea44SDavid Greenman */
490ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
49126f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
492d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
493d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
494d5e26ef0SDavid Greenman 
495d5e26ef0SDavid Greenman 	return;
496d5e26ef0SDavid Greenman }
497d5e26ef0SDavid Greenman 
498ae92ea44SDavid Greenman 
4995b81b6b3SRodney W. Grimes /*
5001561d038SDavid Greenman  * init the bounce buffer system
5011561d038SDavid Greenman  */
5021561d038SDavid Greenman void
5031561d038SDavid Greenman vm_bounce_init()
5041561d038SDavid Greenman {
505ae92ea44SDavid Greenman 	int i;
5061561d038SDavid Greenman 
5071561d038SDavid Greenman 	kvasfreecnt = 0;
5081561d038SDavid Greenman 
5091561d038SDavid Greenman 	if (bouncepages == 0)
5101561d038SDavid Greenman 		return;
5111561d038SDavid Greenman 
5121561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5131561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5141561d038SDavid Greenman 
5151561d038SDavid Greenman 	if (!bounceallocarray)
516edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5171561d038SDavid Greenman 
518ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
519ae92ea44SDavid Greenman 	if (!bouncepa)
520edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5211561d038SDavid Greenman 
5220d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5230d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5240d94caffSDavid Greenman 	}
5250d94caffSDavid Greenman 
526ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
527ae92ea44SDavid Greenman 		vm_offset_t pa;
528ae92ea44SDavid Greenman 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * NBPG)) >= SIXTEENMEG)
529ae92ea44SDavid Greenman 			panic("bounce memory out of range");
530ae92ea44SDavid Greenman 		if( pa == 0)
531ae92ea44SDavid Greenman 			panic("bounce memory not resident");
532ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5330d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
534ae92ea44SDavid Greenman 	}
5351561d038SDavid Greenman 	bouncefree = bouncepages;
5361561d038SDavid Greenman 
5371561d038SDavid Greenman }
5389aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5396c146e28SBruce Evans 
540a4f7a4c9SDavid Greenman /*
541a4f7a4c9SDavid Greenman  * quick version of vm_fault
542a4f7a4c9SDavid Greenman  */
54387b91157SPoul-Henning Kamp static void
544a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5456c146e28SBruce Evans 	caddr_t v;
546a4f7a4c9SDavid Greenman 	int prot;
547a4f7a4c9SDavid Greenman {
5481d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5496c146e28SBruce Evans 		subyte(v, fubyte(v));
550a4f7a4c9SDavid Greenman 	else
5516c146e28SBruce Evans 		fubyte(v);
552a4f7a4c9SDavid Greenman }
553a4f7a4c9SDavid Greenman 
5541561d038SDavid Greenman /*
5555b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
5565b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
5575b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
5585b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
5595b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
5605b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
5615b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
5625b81b6b3SRodney W. Grimes  */
563381fe1aaSGarrett Wollman int
5645b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5655b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5665b81b6b3SRodney W. Grimes {
5675b81b6b3SRodney W. Grimes 	register struct user *up = p2->p_addr;
5683fb3086eSPoul-Henning Kamp 	int offset;
5695b81b6b3SRodney W. Grimes 
5705b81b6b3SRodney W. Grimes 	/*
5715b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
5725b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
5735b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
5745b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
5755b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
5765b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
5775b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
5785b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
5795b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
5805b81b6b3SRodney W. Grimes 	 */
5815b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
5825b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
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 
5875b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
5885b81b6b3SRodney W. Grimes 
5895b81b6b3SRodney W. Grimes 	/*
5905b81b6b3SRodney W. Grimes 	 *
5915b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
5925b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
5935b81b6b3SRodney W. Grimes 	 */
5943fb3086eSPoul-Henning Kamp 	if (savectx(&up->u_pcb, 1)) {
5955b81b6b3SRodney W. Grimes 		/*
5965b81b6b3SRodney W. Grimes 		 * Return 1 in child.
5975b81b6b3SRodney W. Grimes 		 */
5985b81b6b3SRodney W. Grimes 		return (1);
5995b81b6b3SRodney W. Grimes 	}
6005b81b6b3SRodney W. Grimes 	return (0);
6015b81b6b3SRodney W. Grimes }
6025b81b6b3SRodney W. Grimes 
6037c2b54e8SNate Williams void
6045b81b6b3SRodney W. Grimes cpu_exit(p)
6055b81b6b3SRodney W. Grimes 	register struct proc *p;
6065b81b6b3SRodney W. Grimes {
6075b81b6b3SRodney W. Grimes 
608960173b9SRodney W. Grimes #if NNPX > 0
6095b81b6b3SRodney W. Grimes 	npxexit(p);
610960173b9SRodney W. Grimes #endif	/* NNPX */
6111a051896SBruce Evans 	cnt.v_swtch++;
6121a051896SBruce Evans 	cpu_switch(p);
6137c2b54e8SNate Williams 	panic("cpu_exit");
6145b81b6b3SRodney W. Grimes }
6155b81b6b3SRodney W. Grimes 
616381fe1aaSGarrett Wollman void
6177f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
6187f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
6195b81b6b3SRodney W. Grimes 
6205b81b6b3SRodney W. Grimes 	/* drop per-process resources */
6210d94caffSDavid Greenman  	pmap_remove(vm_map_pmap(u_map), (vm_offset_t) p->p_addr,
6227f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
6230d94caffSDavid Greenman 	kmem_free(u_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
6247f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
6255b81b6b3SRodney W. Grimes }
6265b81b6b3SRodney W. Grimes 
6275b81b6b3SRodney W. Grimes /*
62826f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
62926f9a767SRodney W. Grimes  */
63026f9a767SRodney W. Grimes int
63126f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
63226f9a767SRodney W. Grimes 	struct proc *p;
63326f9a767SRodney W. Grimes 	struct vnode *vp;
63426f9a767SRodney W. Grimes 	struct ucred *cred;
63526f9a767SRodney W. Grimes {
63626f9a767SRodney W. Grimes 
63726f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
63826f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
63926f9a767SRodney W. Grimes 	    p));
64026f9a767SRodney W. Grimes }
64126f9a767SRodney W. Grimes 
64226f9a767SRodney W. Grimes /*
6435b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
6445b81b6b3SRodney W. Grimes  */
64587b91157SPoul-Henning Kamp static void
6465b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
6475b81b6b3SRodney W. Grimes 	u_short *pte;
6485b81b6b3SRodney W. Grimes 	caddr_t vaddr;
6495b81b6b3SRodney W. Grimes {
6505b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
6515b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
6525b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
6535b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
6545b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
6555b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
6565b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
6575b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
6585b81b6b3SRodney W. Grimes }
6595b81b6b3SRodney W. Grimes 
6605b81b6b3SRodney W. Grimes /*
66126f9a767SRodney W. Grimes  * Move pages from one kernel virtual address to another.
66226f9a767SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
66326f9a767SRodney W. Grimes  * and size must be a multiple of CLSIZE.
66426f9a767SRodney W. Grimes  */
66526f9a767SRodney W. Grimes 
66687b91157SPoul-Henning Kamp static void
66726f9a767SRodney W. Grimes pagemove(from, to, size)
66826f9a767SRodney W. Grimes 	register caddr_t from, to;
66926f9a767SRodney W. Grimes 	int size;
67026f9a767SRodney W. Grimes {
67126f9a767SRodney W. Grimes 	register vm_offset_t pa;
67226f9a767SRodney W. Grimes 
67326f9a767SRodney W. Grimes 	if (size & CLOFSET)
67426f9a767SRodney W. Grimes 		panic("pagemove");
67526f9a767SRodney W. Grimes 	while (size > 0) {
67626f9a767SRodney W. Grimes 		pa = pmap_kextract((vm_offset_t)from);
67726f9a767SRodney W. Grimes 		if (pa == 0)
67826f9a767SRodney W. Grimes 			panic("pagemove 2");
67926f9a767SRodney W. Grimes 		if (pmap_kextract((vm_offset_t)to) != 0)
68026f9a767SRodney W. Grimes 			panic("pagemove 3");
681c564966bSDavid Greenman 		pmap_kremove((vm_offset_t)from);
68226f9a767SRodney W. Grimes 		pmap_kenter((vm_offset_t)to, pa);
68326f9a767SRodney W. Grimes 		from += PAGE_SIZE;
68426f9a767SRodney W. Grimes 		to += PAGE_SIZE;
68526f9a767SRodney W. Grimes 		size -= PAGE_SIZE;
68626f9a767SRodney W. Grimes 	}
68726f9a767SRodney W. Grimes }
68826f9a767SRodney W. Grimes 
68926f9a767SRodney W. Grimes /*
6905b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
6915b81b6b3SRodney W. Grimes  */
692aaf08d94SGarrett Wollman u_long
6937f8cb368SDavid Greenman kvtop(void *addr)
6945b81b6b3SRodney W. Grimes {
6955b81b6b3SRodney W. Grimes 	vm_offset_t va;
6965b81b6b3SRodney W. Grimes 
697ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
6985b81b6b3SRodney W. Grimes 	if (va == 0)
6995b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
7007f8cb368SDavid Greenman 	return((int)va);
7015b81b6b3SRodney W. Grimes }
7025b81b6b3SRodney W. Grimes 
7035b81b6b3SRodney W. Grimes /*
704ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
7055b81b6b3SRodney W. Grimes  *
706ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
707ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
708ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
7095b81b6b3SRodney W. Grimes  */
710381fe1aaSGarrett Wollman void
7115b81b6b3SRodney W. Grimes vmapbuf(bp)
7125b81b6b3SRodney W. Grimes 	register struct buf *bp;
7135b81b6b3SRodney W. Grimes {
7145b81b6b3SRodney W. Grimes 	register int npf;
7155b81b6b3SRodney W. Grimes 	register caddr_t addr;
7165b81b6b3SRodney W. Grimes 	int off;
7175b81b6b3SRodney W. Grimes 	vm_offset_t kva;
71807658526SPoul-Henning Kamp 	vm_offset_t pa;
7195b81b6b3SRodney W. Grimes 
72026f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7215b81b6b3SRodney W. Grimes 		panic("vmapbuf");
72226f9a767SRodney W. Grimes 
723ae92ea44SDavid Greenman 	/*
724ae92ea44SDavid Greenman 	 * this is the kva that is to be used for
725ae92ea44SDavid Greenman 	 * the temporary kernel mapping
726ae92ea44SDavid Greenman 	 */
727ae92ea44SDavid Greenman 	kva = (vm_offset_t) bp->b_saveaddr;
728ae92ea44SDavid Greenman 
72926f9a767SRodney W. Grimes 	for (addr = (caddr_t)trunc_page(bp->b_data);
73026f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
73126f9a767SRodney W. Grimes 		addr += PAGE_SIZE) {
73226f9a767SRodney W. Grimes 
73326f9a767SRodney W. Grimes /*
73426f9a767SRodney W. Grimes  * do the vm_fault if needed, do the copy-on-write thing when
73526f9a767SRodney W. Grimes  * reading stuff off device into memory.
73626f9a767SRodney W. Grimes  */
73726f9a767SRodney W. Grimes 		vm_fault_quick(addr,
73826f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
739c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t) addr);
740641a27e6SJohn Dyson 		if (pa == 0)
741641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
74226f9a767SRodney W. Grimes /*
74326f9a767SRodney W. Grimes  * hold the data page
74426f9a767SRodney W. Grimes  */
745641a27e6SJohn Dyson #ifdef DIAGNOSTIC
746641a27e6SJohn Dyson 		if( VM_PAGE_TO_PHYS(PHYS_TO_VM_PAGE(pa)) != pa)
747641a27e6SJohn Dyson 			panic("vmapbuf: confused PHYS_TO_VM_PAGE mapping");
748641a27e6SJohn Dyson #endif
74926f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
75026f9a767SRodney W. Grimes 	}
75126f9a767SRodney W. Grimes 
752ae92ea44SDavid Greenman 	addr = bp->b_saveaddr = bp->b_data;
7535b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
754ac322158SDavid Greenman 	npf = btoc(round_page(bp->b_bufsize + off));
755ae92ea44SDavid Greenman 	bp->b_data = (caddr_t) (kva + off);
7565b81b6b3SRodney W. Grimes 	while (npf--) {
757c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t)addr);
7585b81b6b3SRodney W. Grimes 		if (pa == 0)
7595b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
7606b4ac811SDavid Greenman 		pmap_kenter(kva, trunc_page(pa));
7615b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
7625b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
7635b81b6b3SRodney W. Grimes 	}
7645b81b6b3SRodney W. Grimes }
7655b81b6b3SRodney W. Grimes 
7665b81b6b3SRodney W. Grimes /*
7675b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
7685b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
7695b81b6b3SRodney W. Grimes  */
770381fe1aaSGarrett Wollman void
7715b81b6b3SRodney W. Grimes vunmapbuf(bp)
7725b81b6b3SRodney W. Grimes 	register struct buf *bp;
7735b81b6b3SRodney W. Grimes {
774c564966bSDavid Greenman 	register caddr_t addr;
77507658526SPoul-Henning Kamp 	vm_offset_t pa;
7765b81b6b3SRodney W. Grimes 
7775b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7785b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
779c564966bSDavid Greenman 
780c564966bSDavid Greenman 	for (addr = (caddr_t)trunc_page((vm_offset_t) bp->b_data);
781c564966bSDavid Greenman 		addr < bp->b_data + bp->b_bufsize;
782c564966bSDavid Greenman 		addr += NBPG)
783c564966bSDavid Greenman 		pmap_kremove((vm_offset_t) addr);
784c564966bSDavid Greenman 
785ae92ea44SDavid Greenman 	bp->b_data = bp->b_saveaddr;
7865b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
78726f9a767SRodney W. Grimes 
78826f9a767SRodney W. Grimes /*
78926f9a767SRodney W. Grimes  * unhold the pde, and data pages
79026f9a767SRodney W. Grimes  */
791c564966bSDavid Greenman 	for (addr = (caddr_t)trunc_page((vm_offset_t) bp->b_data);
79226f9a767SRodney W. Grimes 		addr < bp->b_data + bp->b_bufsize;
79326f9a767SRodney W. Grimes 		addr += NBPG) {
79426f9a767SRodney W. Grimes 	/*
79526f9a767SRodney W. Grimes 	 * release the data page
79626f9a767SRodney W. Grimes 	 */
797c564966bSDavid Greenman 		pa = pmap_kextract((vm_offset_t) addr);
79826f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
79926f9a767SRodney W. Grimes 	}
8005b81b6b3SRodney W. Grimes }
8015b81b6b3SRodney W. Grimes 
8025b81b6b3SRodney W. Grimes /*
8035b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
8045b81b6b3SRodney W. Grimes  */
8057f8cb368SDavid Greenman void
8065b81b6b3SRodney W. Grimes cpu_reset() {
8075b81b6b3SRodney W. Grimes 
8082320728fSRodney W. Grimes 	/*
8092320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
8102320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
8112320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
8122320728fSRodney W. Grimes 	 */
8135eb46edfSDavid Greenman 
8145eb46edfSDavid Greenman #ifndef BROKEN_KEYBOARD_RESET
8152320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
8162320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
8172320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
8182320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
8195eb46edfSDavid Greenman #endif
8202320728fSRodney W. Grimes 
8215b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
8225b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
8235b81b6b3SRodney W. Grimes 
8245b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
8252c7a40c7SDavid Greenman 	pmap_update();
8265b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8277f8cb368SDavid Greenman 	while(1);
8285b81b6b3SRodney W. Grimes }
829b9d60b3fSDavid Greenman 
830b9d60b3fSDavid Greenman /*
831b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
83229360eb0SDavid Greenman  *	chunks of SGROWSIZ.
833b9d60b3fSDavid Greenman  */
834b9d60b3fSDavid Greenman int
835b9d60b3fSDavid Greenman grow(p, sp)
836b9d60b3fSDavid Greenman 	struct proc *p;
83726f9a767SRodney W. Grimes 	u_int sp;
838b9d60b3fSDavid Greenman {
839b9d60b3fSDavid Greenman 	unsigned int nss;
840b9d60b3fSDavid Greenman 	caddr_t v;
841b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
842b9d60b3fSDavid Greenman 
843b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
844b9d60b3fSDavid Greenman 	    return (1);
845b9d60b3fSDavid Greenman 
846b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
847b9d60b3fSDavid Greenman 
848b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
849b9d60b3fSDavid Greenman 		return (0);
850b9d60b3fSDavid Greenman 
851b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
85229360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
853b9d60b3fSDavid Greenman 		int grow_amount;
854b9d60b3fSDavid Greenman 		/*
855b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
856b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
857b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
858b9d60b3fSDavid Greenman 		 * is expensive).
859b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
86029360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
861b9d60b3fSDavid Greenman 		 */
86229360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
863b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
86429360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
865b9d60b3fSDavid Greenman 		/*
86629360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
867b9d60b3fSDavid Greenman 		 * just extend to the maximum size
868b9d60b3fSDavid Greenman 		 */
869b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
870b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
871b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
872b9d60b3fSDavid Greenman 		}
87368940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
87468940ac1SDavid Greenman 		    grow_amount, FALSE) != KERN_SUCCESS)) {
875b9d60b3fSDavid Greenman 			return (0);
876b9d60b3fSDavid Greenman 		}
877b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
878b9d60b3fSDavid Greenman 	}
879b9d60b3fSDavid Greenman 
880b9d60b3fSDavid Greenman 	return (1);
881b9d60b3fSDavid Greenman }
882a29b63cbSJohn Dyson 
883a29b63cbSJohn Dyson /*
884a29b63cbSJohn Dyson  * prototype routine to implement the pre-zeroed page mechanism
885a29b63cbSJohn Dyson  * this routine is called from the idle loop.
886a29b63cbSJohn Dyson  */
887a29b63cbSJohn Dyson int
888a29b63cbSJohn Dyson vm_page_zero_idle() {
889a29b63cbSJohn Dyson 	vm_page_t m;
890a29b63cbSJohn Dyson 	if ((cnt.v_free_count > cnt.v_interrupt_free_min) &&
891a29b63cbSJohn Dyson 		(m = vm_page_queue_free.tqh_first)) {
892a29b63cbSJohn Dyson 		TAILQ_REMOVE(&vm_page_queue_free, m, pageq);
893a29b63cbSJohn Dyson 		enable_intr();
894a29b63cbSJohn Dyson 		pmap_zero_page(VM_PAGE_TO_PHYS(m));
895a29b63cbSJohn Dyson 		disable_intr();
89623e56ee8SDavid Greenman 		TAILQ_INSERT_HEAD(&vm_page_queue_zero, m, pageq);
897a29b63cbSJohn Dyson 		return 1;
898a29b63cbSJohn Dyson 	}
899a29b63cbSJohn Dyson 	return 0;
900a29b63cbSJohn Dyson }
901