xref: /freebsd/sys/i386/i386/vm_machdep.c (revision e30f001135d8f835c2d750ae17c91268fa9d0bb2)
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$
41e30f0011SSatoshi Asami  *	$Id: vm_machdep.c,v 1.70 1996/10/15 03:16:33 dyson 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 
69e30f0011SSatoshi Asami #ifdef PC98
70e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h>
71e30f0011SSatoshi Asami #else
722320728fSRodney W. Grimes #include <i386/isa/isa.h>
73e30f0011SSatoshi Asami #endif
742320728fSRodney W. Grimes 
759aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
7687b91157SPoul-Henning Kamp static vm_offset_t
776c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
7887b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
796c146e28SBruce Evans 					int now));
8087b91157SPoul-Henning Kamp static vm_offset_t
816c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
8287b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
836c146e28SBruce Evans 
8487b6de2bSPoul-Henning Kamp static volatile int	kvasfreecnt;
85ae92ea44SDavid Greenman 
86d5e26ef0SDavid Greenman caddr_t		bouncememory;
8787b91157SPoul-Henning Kamp int		bouncepages;
8887b91157SPoul-Henning Kamp static int	bpwait;
8987b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
9087b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
91d5e26ef0SDavid Greenman 
92ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
9387b91157SPoul-Henning Kamp static int		bounceallocarraysize;
9487b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
9587b91157SPoul-Henning Kamp static int		bouncefree;
96d5e26ef0SDavid Greenman 
97e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA)
98e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096)			/* 15MB boundary */
99e30f0011SSatoshi Asami #else
100d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
101e30f0011SSatoshi Asami #endif
102ae92ea44SDavid Greenman #define MAXBKVA 1024
103f8845af0SPoul-Henning Kamp int		maxbkva = MAXBKVA*PAGE_SIZE;
104d5e26ef0SDavid Greenman 
105d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
10687b91157SPoul-Henning Kamp static struct kvasfree {
107d5e26ef0SDavid Greenman 	vm_offset_t addr;
108d5e26ef0SDavid Greenman 	vm_offset_t size;
109d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
110d5e26ef0SDavid Greenman 
111d5e26ef0SDavid Greenman /*
112d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
113d5e26ef0SDavid Greenman  * (only 1 inplemented now)
114d5e26ef0SDavid Greenman  */
11587b91157SPoul-Henning Kamp static vm_offset_t
116d5e26ef0SDavid Greenman vm_bounce_page_find(count)
117d5e26ef0SDavid Greenman 	int count;
118d5e26ef0SDavid Greenman {
119d5e26ef0SDavid Greenman 	int bit;
120d5e26ef0SDavid Greenman 	int s,i;
121d5e26ef0SDavid Greenman 
122d5e26ef0SDavid Greenman 	if (count != 1)
123d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
124d5e26ef0SDavid Greenman 
125d5e26ef0SDavid Greenman 	s = splbio();
126d5e26ef0SDavid Greenman retry:
127d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
128d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1293fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1303fb3086eSPoul-Henning Kamp 			if (bit) {
131d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
132d5e26ef0SDavid Greenman 				bouncefree -= count;
133d5e26ef0SDavid Greenman 				splx(s);
134ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
135d5e26ef0SDavid Greenman 			}
136d5e26ef0SDavid Greenman 		}
137d5e26ef0SDavid Greenman 	}
138ed7fcbd0SDavid Greenman 	bpwait = 1;
139d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
140d5e26ef0SDavid Greenman 	goto retry;
141d5e26ef0SDavid Greenman }
142d5e26ef0SDavid Greenman 
14387b91157SPoul-Henning Kamp static void
1441561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1451561d038SDavid Greenman 	vm_offset_t addr;
1461561d038SDavid Greenman 	vm_offset_t size;
1471561d038SDavid Greenman 	int now;
1481561d038SDavid Greenman {
1491561d038SDavid Greenman 	int s = splbio();
1501561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
151ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
152ae92ea44SDavid Greenman 	++kvasfreecnt;
1530e195446SDavid Greenman 	if( now) {
1540e195446SDavid Greenman 		/*
1550e195446SDavid Greenman 		 * this will do wakeups
1560e195446SDavid Greenman 		 */
1571561d038SDavid Greenman 		vm_bounce_kva(0,0);
1580e195446SDavid Greenman 	} else {
1590e195446SDavid Greenman 		if (bmwait) {
1600e195446SDavid Greenman 		/*
1610e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1620e195446SDavid Greenman 		 */
1631561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1640e195446SDavid Greenman 			bmwait = 0;
1650e195446SDavid Greenman 		}
1660e195446SDavid Greenman 	}
1671561d038SDavid Greenman 	splx(s);
1681561d038SDavid Greenman }
1691561d038SDavid Greenman 
170d5e26ef0SDavid Greenman /*
171d5e26ef0SDavid Greenman  * free count bounce buffer pages
172d5e26ef0SDavid Greenman  */
17387b91157SPoul-Henning Kamp static void
174d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
175d5e26ef0SDavid Greenman 	vm_offset_t pa;
176d5e26ef0SDavid Greenman 	int count;
177d5e26ef0SDavid Greenman {
178d5e26ef0SDavid Greenman 	int allocindex;
179d5e26ef0SDavid Greenman 	int index;
180d5e26ef0SDavid Greenman 	int bit;
181d5e26ef0SDavid Greenman 
182d5e26ef0SDavid Greenman 	if (count != 1)
183edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
184d5e26ef0SDavid Greenman 
185ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
186ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
187ae92ea44SDavid Greenman 			break;
188ae92ea44SDavid Greenman 	}
189d5e26ef0SDavid Greenman 
190ae92ea44SDavid Greenman 	if( index == bouncepages)
191ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
192d5e26ef0SDavid Greenman 
193ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
194ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
195d5e26ef0SDavid Greenman 
196d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
197d5e26ef0SDavid Greenman 
198d5e26ef0SDavid Greenman 	bouncefree += count;
199ed7fcbd0SDavid Greenman 	if (bpwait) {
200ed7fcbd0SDavid Greenman 		bpwait = 0;
201d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
202d5e26ef0SDavid Greenman 	}
203ed7fcbd0SDavid Greenman }
204d5e26ef0SDavid Greenman 
205d5e26ef0SDavid Greenman /*
206d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
207d5e26ef0SDavid Greenman  */
20887b91157SPoul-Henning Kamp static vm_offset_t
209ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
210ae92ea44SDavid Greenman 	int size;
2111561d038SDavid Greenman 	int waitok;
212d5e26ef0SDavid Greenman {
213d5e26ef0SDavid Greenman 	int i;
2146b4ac811SDavid Greenman 	vm_offset_t kva = 0;
215c564966bSDavid Greenman 	vm_offset_t off;
216d5e26ef0SDavid Greenman 	int s = splbio();
217d5e26ef0SDavid Greenman more:
218ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
219d5e26ef0SDavid Greenman 		bmfreeing = 1;
220ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
221f8845af0SPoul-Henning Kamp 			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
222c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
223c564966bSDavid Greenman 			}
2241561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
225d5e26ef0SDavid Greenman 				kvaf[i].size);
226d5e26ef0SDavid Greenman 		}
227d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
228d5e26ef0SDavid Greenman 		bmfreeing = 0;
229ae92ea44SDavid Greenman 		if( bmwait) {
230ae92ea44SDavid Greenman 			bmwait = 0;
231ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
232ae92ea44SDavid Greenman 		}
233d5e26ef0SDavid Greenman 	}
234d5e26ef0SDavid Greenman 
2351561d038SDavid Greenman 	if( size == 0) {
2361561d038SDavid Greenman 		splx(s);
2376ab46d52SBruce Evans 		return 0;
2381561d038SDavid Greenman 	}
2391561d038SDavid Greenman 
240ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2411561d038SDavid Greenman 		if( !waitok) {
2421561d038SDavid Greenman 			splx(s);
2436ab46d52SBruce Evans 			return 0;
2441561d038SDavid Greenman 		}
245d5e26ef0SDavid Greenman 		bmwait = 1;
2461561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
247d5e26ef0SDavid Greenman 		goto more;
248d5e26ef0SDavid Greenman 	}
249d5e26ef0SDavid Greenman 	splx(s);
250d5e26ef0SDavid Greenman 	return kva;
251d5e26ef0SDavid Greenman }
252d5e26ef0SDavid Greenman 
253d5e26ef0SDavid Greenman /*
254ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2550e195446SDavid Greenman  */
2560e195446SDavid Greenman vm_offset_t
2570e195446SDavid Greenman vm_bounce_kva_alloc(count)
2580e195446SDavid Greenman int count;
2590e195446SDavid Greenman {
2600e195446SDavid Greenman 	int i;
2610e195446SDavid Greenman 	vm_offset_t kva;
2620e195446SDavid Greenman 	vm_offset_t pa;
2630e195446SDavid Greenman 	if( bouncepages == 0) {
264f8845af0SPoul-Henning Kamp 		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
2650e195446SDavid Greenman 		return kva;
2660e195446SDavid Greenman 	}
267f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
2680e195446SDavid Greenman 	for(i=0;i<count;i++) {
2690e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
270f8845af0SPoul-Henning Kamp 		pmap_kenter(kva + i * PAGE_SIZE, pa);
2710e195446SDavid Greenman 	}
2720e195446SDavid Greenman 	return kva;
2730e195446SDavid Greenman }
2740e195446SDavid Greenman 
2750e195446SDavid Greenman /*
2760e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2770e195446SDavid Greenman  */
2780e195446SDavid Greenman void
2790e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2800e195446SDavid Greenman 	vm_offset_t kva;
2810e195446SDavid Greenman 	int count;
2820e195446SDavid Greenman {
2830e195446SDavid Greenman 	int i;
2840e195446SDavid Greenman 	vm_offset_t pa;
2850e195446SDavid Greenman 	if( bouncepages == 0) {
2860e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2870e195446SDavid Greenman 		return;
2880e195446SDavid Greenman 	}
2890e195446SDavid Greenman 	for(i = 0; i < count; i++) {
290f8845af0SPoul-Henning Kamp 		pa = pmap_kextract(kva + i * PAGE_SIZE);
2910e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2920e195446SDavid Greenman 	}
293f8845af0SPoul-Henning Kamp 	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
2940e195446SDavid Greenman }
2950e195446SDavid Greenman 
2960e195446SDavid Greenman /*
297d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
298d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
299d5e26ef0SDavid Greenman  */
300d5e26ef0SDavid Greenman void
301d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
302d5e26ef0SDavid Greenman 	struct buf *bp;
303d5e26ef0SDavid Greenman {
304d5e26ef0SDavid Greenman 	int countvmpg;
305d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
306d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
307d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
308d5e26ef0SDavid Greenman 	vm_offset_t pa;
309d5e26ef0SDavid Greenman 	int dobounceflag = 0;
310d5e26ef0SDavid Greenman 	int i;
311d5e26ef0SDavid Greenman 
312d5e26ef0SDavid Greenman 	if (bouncepages == 0)
313d5e26ef0SDavid Greenman 		return;
314d5e26ef0SDavid Greenman 
315ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
316ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
317ae92ea44SDavid Greenman 		return;
3181561d038SDavid Greenman 	}
3191561d038SDavid Greenman 
320ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3213fb3086eSPoul-Henning Kamp 		printf(
3223fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
323ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
324ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
325ae92ea44SDavid Greenman 	}
326ae92ea44SDavid Greenman 
327ae92ea44SDavid Greenman /*
328ae92ea44SDavid Greenman  *  This is not really necessary
329ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
330ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
331ae92ea44SDavid Greenman  *	}
332ae92ea44SDavid Greenman  */
333ae92ea44SDavid Greenman 
334ae92ea44SDavid Greenman 
33526f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
33626f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
337d5e26ef0SDavid Greenman 
338bd7e5f99SJohn Dyson 	vapstart = trunc_page(vastart);
339bd7e5f99SJohn Dyson 	vapend = round_page(vaend);
340f8845af0SPoul-Henning Kamp 	countvmpg = (vapend - vapstart) / PAGE_SIZE;
341d5e26ef0SDavid Greenman 
342d5e26ef0SDavid Greenman /*
343d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
344d5e26ef0SDavid Greenman  */
345d5e26ef0SDavid Greenman 	va = vapstart;
346d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
347ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
348d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
349d5e26ef0SDavid Greenman 			++dobounceflag;
3500d94caffSDavid Greenman 		if( pa == 0)
3510d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
352f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
353d5e26ef0SDavid Greenman 	}
354d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
355d5e26ef0SDavid Greenman 		return;
356d5e26ef0SDavid Greenman 
357d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
358d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
359d5e26ef0SDavid Greenman 
360d5e26ef0SDavid Greenman /*
361d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
362d5e26ef0SDavid Greenman  */
363f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
364ae92ea44SDavid Greenman #if 0
365ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
366ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
367ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
368ae92ea44SDavid Greenman #endif
369d5e26ef0SDavid Greenman 	va = vapstart;
370d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
371ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
372d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
373d5e26ef0SDavid Greenman 			/*
374d5e26ef0SDavid Greenman 			 * allocate a replacement page
375d5e26ef0SDavid Greenman 			 */
376d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
377f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
378ae92ea44SDavid Greenman #if 0
379ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
380ae92ea44SDavid Greenman #endif
381d5e26ef0SDavid Greenman 			/*
382d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
383d5e26ef0SDavid Greenman 			 */
3841561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
385f8845af0SPoul-Henning Kamp 				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
3861561d038SDavid Greenman 			}
387d5e26ef0SDavid Greenman 		} else {
388d5e26ef0SDavid Greenman 			/*
389d5e26ef0SDavid Greenman 			 * use original page
390d5e26ef0SDavid Greenman 			 */
391f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), pa);
392d5e26ef0SDavid Greenman 		}
393f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
394d5e26ef0SDavid Greenman 	}
395d5e26ef0SDavid Greenman 
396d5e26ef0SDavid Greenman /*
397d5e26ef0SDavid Greenman  * flag the buffer as being bounced
398d5e26ef0SDavid Greenman  */
399d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
400d5e26ef0SDavid Greenman /*
401d5e26ef0SDavid Greenman  * save the original buffer kva
402d5e26ef0SDavid Greenman  */
40326f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
404d5e26ef0SDavid Greenman /*
405d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
406d5e26ef0SDavid Greenman  */
40726f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
408f8845af0SPoul-Henning Kamp 				((vm_offset_t) bp->b_savekva & PAGE_MASK));
409ae92ea44SDavid Greenman #if 0
410ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
411ae92ea44SDavid Greenman #endif
412d5e26ef0SDavid Greenman 	return;
413d5e26ef0SDavid Greenman }
414d5e26ef0SDavid Greenman 
415d5e26ef0SDavid Greenman /*
416d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
417d5e26ef0SDavid Greenman  */
418d5e26ef0SDavid Greenman void
419d5e26ef0SDavid Greenman vm_bounce_free(bp)
420d5e26ef0SDavid Greenman 	struct buf *bp;
421d5e26ef0SDavid Greenman {
422d5e26ef0SDavid Greenman 	int i;
423ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
424d5e26ef0SDavid Greenman 
425d5e26ef0SDavid Greenman /*
426d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
427d5e26ef0SDavid Greenman  */
428d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
429d5e26ef0SDavid Greenman 		return;
430d5e26ef0SDavid Greenman 
431ae92ea44SDavid Greenman /*
432ae92ea44SDavid Greenman  *  This check is not necessary
433ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
434ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
435ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
436ae92ea44SDavid Greenman  *	}
437ae92ea44SDavid Greenman  */
438ae92ea44SDavid Greenman 
439d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
44026f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
441ae92ea44SDavid Greenman /*
442ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
443ae92ea44SDavid Greenman */
444d5e26ef0SDavid Greenman 
445d5e26ef0SDavid Greenman /*
446d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
447d5e26ef0SDavid Greenman  */
448ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
449d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
450d5e26ef0SDavid Greenman 		vm_offset_t copycount;
451d5e26ef0SDavid Greenman 
452bd7e5f99SJohn Dyson 		copycount = round_page(bouncekva + 1) - bouncekva;
453bd7e5f99SJohn Dyson 		mybouncepa = pmap_kextract(trunc_page(bouncekva));
454d5e26ef0SDavid Greenman 
455d5e26ef0SDavid Greenman /*
456d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
457d5e26ef0SDavid Greenman  */
458bd7e5f99SJohn Dyson 		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
459ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
460ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
461ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
462d5e26ef0SDavid Greenman /*
463d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
464d5e26ef0SDavid Greenman  */
465d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
466ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
467d5e26ef0SDavid Greenman /*
468d5e26ef0SDavid Greenman  * free the bounce allocation
469d5e26ef0SDavid Greenman  */
470ae92ea44SDavid Greenman 
471ae92ea44SDavid Greenman /*
472ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
473ae92ea44SDavid Greenman */
474ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
475d5e26ef0SDavid Greenman 		}
476d5e26ef0SDavid Greenman 
477d5e26ef0SDavid Greenman 		origkva += copycount;
478d5e26ef0SDavid Greenman 		bouncekva += copycount;
479ae92ea44SDavid Greenman 		i += copycount;
480d5e26ef0SDavid Greenman 	}
481d5e26ef0SDavid Greenman 
482d5e26ef0SDavid Greenman /*
483ae92ea44SDavid Greenman 	printf("\n");
484ae92ea44SDavid Greenman */
485ae92ea44SDavid Greenman /*
486d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
487d5e26ef0SDavid Greenman  */
488ae92ea44SDavid Greenman 
489bd7e5f99SJohn Dyson 	bouncekva= trunc_page((vm_offset_t) bp->b_data);
490bd7e5f99SJohn Dyson 	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
491ae92ea44SDavid Greenman 
492ae92ea44SDavid Greenman /*
493f8845af0SPoul-Henning Kamp 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
494ae92ea44SDavid Greenman */
495ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
49626f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
497d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
498d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
499d5e26ef0SDavid Greenman 
500d5e26ef0SDavid Greenman 	return;
501d5e26ef0SDavid Greenman }
502d5e26ef0SDavid Greenman 
503ae92ea44SDavid Greenman 
5045b81b6b3SRodney W. Grimes /*
5051561d038SDavid Greenman  * init the bounce buffer system
5061561d038SDavid Greenman  */
5071561d038SDavid Greenman void
5081561d038SDavid Greenman vm_bounce_init()
5091561d038SDavid Greenman {
510ae92ea44SDavid Greenman 	int i;
5111561d038SDavid Greenman 
5121561d038SDavid Greenman 	kvasfreecnt = 0;
5131561d038SDavid Greenman 
5141561d038SDavid Greenman 	if (bouncepages == 0)
5151561d038SDavid Greenman 		return;
5161561d038SDavid Greenman 
5171561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5181561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5191561d038SDavid Greenman 
5201561d038SDavid Greenman 	if (!bounceallocarray)
521edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5221561d038SDavid Greenman 
523ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
524ae92ea44SDavid Greenman 	if (!bouncepa)
525edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5261561d038SDavid Greenman 
5270d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5280d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5290d94caffSDavid Greenman 	}
5300d94caffSDavid Greenman 
531ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
532ae92ea44SDavid Greenman 		vm_offset_t pa;
533f8845af0SPoul-Henning Kamp 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG)
534ae92ea44SDavid Greenman 			panic("bounce memory out of range");
535ae92ea44SDavid Greenman 		if( pa == 0)
536ae92ea44SDavid Greenman 			panic("bounce memory not resident");
537ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5380d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
539ae92ea44SDavid Greenman 	}
5401561d038SDavid Greenman 	bouncefree = bouncepages;
5411561d038SDavid Greenman 
5421561d038SDavid Greenman }
5439aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5446c146e28SBruce Evans 
545a4f7a4c9SDavid Greenman /*
546a4f7a4c9SDavid Greenman  * quick version of vm_fault
547a4f7a4c9SDavid Greenman  */
548dca51299SJohn Dyson void
549a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5506c146e28SBruce Evans 	caddr_t v;
551a4f7a4c9SDavid Greenman 	int prot;
552a4f7a4c9SDavid Greenman {
5531d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5546c146e28SBruce Evans 		subyte(v, fubyte(v));
555a4f7a4c9SDavid Greenman 	else
5566c146e28SBruce Evans 		fubyte(v);
557a4f7a4c9SDavid Greenman }
558a4f7a4c9SDavid Greenman 
5591561d038SDavid Greenman /*
5605b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
5615b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
5625b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
5635b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
5645b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
5655b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
5665b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
5675b81b6b3SRodney W. Grimes  */
568381fe1aaSGarrett Wollman int
5695b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5705b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5715b81b6b3SRodney W. Grimes {
572f83a01b6SDavid Greenman 	struct pcb *pcb2 = &p2->p_addr->u_pcb;
573f83a01b6SDavid Greenman 	int sp, offset;
57468832d30SPoul-Henning Kamp 	volatile int retval;
5755b81b6b3SRodney W. Grimes 
5765b81b6b3SRodney W. Grimes 	/*
5775b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
5785b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
5795b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
5805b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
5815b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
5825b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
5835b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
5845b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
5855b81b6b3SRodney W. Grimes 	 */
586f83a01b6SDavid Greenman 
587f83a01b6SDavid Greenman 	__asm __volatile("movl %%esp,%0" : "=r" (sp));
588f83a01b6SDavid Greenman 	offset = sp - (int)kstack;
589f83a01b6SDavid Greenman 
59068832d30SPoul-Henning Kamp 	retval = 1;		/* return 1 in child */
5915b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
5925b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
59326f9a767SRodney W. Grimes 	p2->p_md.md_regs = p1->p_md.md_regs;
5945b81b6b3SRodney W. Grimes 
595f83a01b6SDavid Greenman 	*pcb2 = p1->p_addr->u_pcb;
596f83a01b6SDavid Greenman 	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
5975b81b6b3SRodney W. Grimes 
59868832d30SPoul-Henning Kamp 	retval = 0;		/* return 0 in parent */
59968832d30SPoul-Henning Kamp 	savectx(pcb2);
60068832d30SPoul-Henning Kamp 	return (retval);
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 {
6070dbf6d73SJordan K. Hubbard #ifdef USER_LDT
6080dbf6d73SJordan K. Hubbard 	struct pcb *pcb;
6090dbf6d73SJordan K. Hubbard #endif
6105b81b6b3SRodney W. Grimes 
611960173b9SRodney W. Grimes #if NNPX > 0
6125b81b6b3SRodney W. Grimes 	npxexit(p);
613960173b9SRodney W. Grimes #endif	/* NNPX */
6140dbf6d73SJordan K. Hubbard #ifdef USER_LDT
6150dbf6d73SJordan K. Hubbard 	pcb = &p->p_addr->u_pcb;
6160dbf6d73SJordan K. Hubbard 	if (pcb->pcb_ldt != 0) {
6170dbf6d73SJordan K. Hubbard 		if (pcb == curpcb)
6180dbf6d73SJordan K. Hubbard 			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
6190dbf6d73SJordan K. Hubbard 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
6200dbf6d73SJordan K. Hubbard 			pcb->pcb_ldt_len * sizeof(union descriptor));
6210dbf6d73SJordan K. Hubbard 		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
6220dbf6d73SJordan K. Hubbard 	}
6230dbf6d73SJordan K. Hubbard #endif
6241a051896SBruce Evans 	cnt.v_swtch++;
6251a051896SBruce Evans 	cpu_switch(p);
6267c2b54e8SNate Williams 	panic("cpu_exit");
6275b81b6b3SRodney W. Grimes }
6285b81b6b3SRodney W. Grimes 
629381fe1aaSGarrett Wollman void
630bd7e5f99SJohn Dyson cpu_wait(p)
631bd7e5f99SJohn Dyson 	struct proc *p;
632bd7e5f99SJohn Dyson {
6335b81b6b3SRodney W. Grimes 	/* drop per-process resources */
634675878e7SJohn Dyson 	pmap_dispose_proc(p);
6357f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
6365b81b6b3SRodney W. Grimes }
6375b81b6b3SRodney W. Grimes 
6385b81b6b3SRodney W. Grimes /*
63926f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
64026f9a767SRodney W. Grimes  */
64126f9a767SRodney W. Grimes int
64226f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
64326f9a767SRodney W. Grimes 	struct proc *p;
64426f9a767SRodney W. Grimes 	struct vnode *vp;
64526f9a767SRodney W. Grimes 	struct ucred *cred;
64626f9a767SRodney W. Grimes {
64726f9a767SRodney W. Grimes 
64826f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
64926f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
65026f9a767SRodney W. Grimes 	    p));
65126f9a767SRodney W. Grimes }
65226f9a767SRodney W. Grimes 
65387b6de2bSPoul-Henning Kamp #ifdef notyet
65487b91157SPoul-Henning Kamp static void
6555b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
6565b81b6b3SRodney W. Grimes 	u_short *pte;
6575b81b6b3SRodney W. Grimes 	caddr_t vaddr;
6585b81b6b3SRodney W. Grimes {
6595b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
6605b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
6615b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
6625b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
6635b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
6645b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
6655b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
6665b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
6675b81b6b3SRodney W. Grimes }
66887b6de2bSPoul-Henning Kamp #endif
66926f9a767SRodney W. Grimes 
67026f9a767SRodney W. Grimes /*
6715b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
6725b81b6b3SRodney W. Grimes  */
673aaf08d94SGarrett Wollman u_long
6747f8cb368SDavid Greenman kvtop(void *addr)
6755b81b6b3SRodney W. Grimes {
6765b81b6b3SRodney W. Grimes 	vm_offset_t va;
6775b81b6b3SRodney W. Grimes 
678ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
6795b81b6b3SRodney W. Grimes 	if (va == 0)
6805b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
6817f8cb368SDavid Greenman 	return((int)va);
6825b81b6b3SRodney W. Grimes }
6835b81b6b3SRodney W. Grimes 
6845b81b6b3SRodney W. Grimes /*
685ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
6865b81b6b3SRodney W. Grimes  *
687ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
688ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
689ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
6905b81b6b3SRodney W. Grimes  */
691381fe1aaSGarrett Wollman void
6925b81b6b3SRodney W. Grimes vmapbuf(bp)
6935b81b6b3SRodney W. Grimes 	register struct buf *bp;
6945b81b6b3SRodney W. Grimes {
6950157d6d9SJohn Dyson 	register caddr_t addr, v, kva;
69607658526SPoul-Henning Kamp 	vm_offset_t pa;
6975b81b6b3SRodney W. Grimes 
69826f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
6995b81b6b3SRodney W. Grimes 		panic("vmapbuf");
70026f9a767SRodney W. Grimes 
70112936855SDavid Greenman 	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
70226f9a767SRodney W. Grimes 	    addr < bp->b_data + bp->b_bufsize;
7030157d6d9SJohn Dyson 	    addr += PAGE_SIZE, v += PAGE_SIZE) {
70426f9a767SRodney W. Grimes 		/*
705ac269d78SDavid Greenman 		 * Do the vm_fault if needed; do the copy-on-write thing
706ac269d78SDavid Greenman 		 * when reading stuff off device into memory.
70726f9a767SRodney W. Grimes 		 */
70826f9a767SRodney W. Grimes 		vm_fault_quick(addr,
70926f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
7100157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
711641a27e6SJohn Dyson 		if (pa == 0)
712641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
71326f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
7140157d6d9SJohn Dyson 		pmap_kenter((vm_offset_t) v, pa);
71526f9a767SRodney W. Grimes 	}
71626f9a767SRodney W. Grimes 
7170157d6d9SJohn Dyson 	kva = bp->b_saveaddr;
7180157d6d9SJohn Dyson 	bp->b_saveaddr = bp->b_data;
7190157d6d9SJohn Dyson 	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
7205b81b6b3SRodney W. Grimes }
7215b81b6b3SRodney W. Grimes 
7225b81b6b3SRodney W. Grimes /*
7235b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
7245b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
7255b81b6b3SRodney W. Grimes  */
726381fe1aaSGarrett Wollman void
7275b81b6b3SRodney W. Grimes vunmapbuf(bp)
7285b81b6b3SRodney W. Grimes 	register struct buf *bp;
7295b81b6b3SRodney W. Grimes {
730c564966bSDavid Greenman 	register caddr_t addr;
73107658526SPoul-Henning Kamp 	vm_offset_t pa;
7325b81b6b3SRodney W. Grimes 
7335b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7345b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
735c564966bSDavid Greenman 
73612936855SDavid Greenman 	for (addr = (caddr_t)trunc_page(bp->b_data);
73712936855SDavid Greenman 	    addr < bp->b_data + bp->b_bufsize;
738f8845af0SPoul-Henning Kamp 	    addr += PAGE_SIZE) {
7390157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
7400157d6d9SJohn Dyson 		pmap_kremove((vm_offset_t) addr);
74126f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
74226f9a767SRodney W. Grimes 	}
7430157d6d9SJohn Dyson 
7440157d6d9SJohn Dyson 	bp->b_data = bp->b_saveaddr;
7455b81b6b3SRodney W. Grimes }
7465b81b6b3SRodney W. Grimes 
7475b81b6b3SRodney W. Grimes /*
7485b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
7495b81b6b3SRodney W. Grimes  */
7507f8cb368SDavid Greenman void
7515b81b6b3SRodney W. Grimes cpu_reset() {
7525b81b6b3SRodney W. Grimes 
7532320728fSRodney W. Grimes 	/*
7542320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
7552320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
7562320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
7572320728fSRodney W. Grimes 	 */
7585eb46edfSDavid Greenman 
759e30f0011SSatoshi Asami #if !defined(BROKEN_KEYBOARD_RESET) && !defined(PC98)
7602320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
7612320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
7622320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
7632320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
7645eb46edfSDavid Greenman #endif
7652320728fSRodney W. Grimes 
7665b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
767f8845af0SPoul-Henning Kamp 	bzero((caddr_t) PTD, PAGE_SIZE);
7685b81b6b3SRodney W. Grimes 
7695b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
77027e9b35eSJohn Dyson 	invltlb();
7715b81b6b3SRodney W. Grimes 	/* NOTREACHED */
772e30f0011SSatoshi Asami #ifdef PC98
773e30f0011SSatoshi Asami 	asm("   cli ");
774e30f0011SSatoshi Asami 	outb(0x37, 0x0f);       /* SHUT 0 = 0 */
775e30f0011SSatoshi Asami 	outb(0x37, 0x0b);       /* SHUT 1 = 0 */
776e30f0011SSatoshi Asami 	outb(0xf0, 0x00);       /* reset port */
777e30f0011SSatoshi Asami #endif
7787f8cb368SDavid Greenman 	while(1);
7795b81b6b3SRodney W. Grimes }
780b9d60b3fSDavid Greenman 
781b9d60b3fSDavid Greenman /*
782b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
78329360eb0SDavid Greenman  *	chunks of SGROWSIZ.
784b9d60b3fSDavid Greenman  */
785b9d60b3fSDavid Greenman int
786b9d60b3fSDavid Greenman grow(p, sp)
787b9d60b3fSDavid Greenman 	struct proc *p;
78826f9a767SRodney W. Grimes 	u_int sp;
789b9d60b3fSDavid Greenman {
790b9d60b3fSDavid Greenman 	unsigned int nss;
791b9d60b3fSDavid Greenman 	caddr_t v;
792b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
793b9d60b3fSDavid Greenman 
794b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
795b9d60b3fSDavid Greenman 	    return (1);
796b9d60b3fSDavid Greenman 
797b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
798b9d60b3fSDavid Greenman 
799b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
800b9d60b3fSDavid Greenman 		return (0);
801b9d60b3fSDavid Greenman 
802b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
80329360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
804b9d60b3fSDavid Greenman 		int grow_amount;
805b9d60b3fSDavid Greenman 		/*
806b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
807b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
808b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
809b9d60b3fSDavid Greenman 		 * is expensive).
810b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
81129360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
812b9d60b3fSDavid Greenman 		 */
81329360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
814b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
81529360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
816b9d60b3fSDavid Greenman 		/*
81729360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
818b9d60b3fSDavid Greenman 		 * just extend to the maximum size
819b9d60b3fSDavid Greenman 		 */
820b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
821b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
822b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
823b9d60b3fSDavid Greenman 		}
82468940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
825bd7e5f99SJohn Dyson 		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
826b9d60b3fSDavid Greenman 			return (0);
827b9d60b3fSDavid Greenman 		}
828b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
829b9d60b3fSDavid Greenman 	}
830b9d60b3fSDavid Greenman 
831b9d60b3fSDavid Greenman 	return (1);
832b9d60b3fSDavid Greenman }
833a29b63cbSJohn Dyson 
834a29b63cbSJohn Dyson /*
835a29b63cbSJohn Dyson  * prototype routine to implement the pre-zeroed page mechanism
836a29b63cbSJohn Dyson  * this routine is called from the idle loop.
837a29b63cbSJohn Dyson  */
838a29b63cbSJohn Dyson int
839a29b63cbSJohn Dyson vm_page_zero_idle() {
840a29b63cbSJohn Dyson 	vm_page_t m;
8415070c7f8SJohn Dyson 	static int free_rover = 0;
842a29b63cbSJohn Dyson 	if ((cnt.v_free_count > cnt.v_interrupt_free_min) &&
8435070c7f8SJohn Dyson 		(m = vm_page_list_find(PQ_FREE, free_rover))) {
8445070c7f8SJohn Dyson 		--(*vm_page_queues[m->queue].lcnt);
8455070c7f8SJohn Dyson 		TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
846a29b63cbSJohn Dyson 		enable_intr();
847a29b63cbSJohn Dyson 		pmap_zero_page(VM_PAGE_TO_PHYS(m));
848a29b63cbSJohn Dyson 		disable_intr();
8495070c7f8SJohn Dyson 		m->queue = PQ_ZERO + m->pc;
8505070c7f8SJohn Dyson 		++(*vm_page_queues[m->queue].lcnt);
8515070c7f8SJohn Dyson 		TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq);
8525070c7f8SJohn Dyson 		free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
853a316d390SJohn Dyson 		++vm_page_zero_count;
854a29b63cbSJohn Dyson 		return 1;
855a29b63cbSJohn Dyson 	}
856a29b63cbSJohn Dyson 	return 0;
857a29b63cbSJohn Dyson }
858