xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 9f449d2aa288e4cfcd13dad8db3e565751528979)
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$
419f449d2aSBruce Evans  *	$Id: vm_machdep.c,v 1.99 1998/02/06 12:13:11 eivind Exp $
425b81b6b3SRodney W. Grimes  */
435b81b6b3SRodney W. Grimes 
44960173b9SRodney W. Grimes #include "npx.h"
458890984dSGarrett Wollman #include "opt_bounce.h"
4619d768b8SPeter Wemm #include "opt_user_ldt.h"
4798823b23SPeter Wemm #include "opt_vm86.h"
488890984dSGarrett Wollman 
4926f9a767SRodney W. Grimes #include <sys/param.h>
5026f9a767SRodney W. Grimes #include <sys/systm.h>
5126f9a767SRodney W. Grimes #include <sys/proc.h>
5226f9a767SRodney W. Grimes #include <sys/malloc.h>
5326f9a767SRodney W. Grimes #include <sys/buf.h>
5426f9a767SRodney W. Grimes #include <sys/vnode.h>
55efeaf95aSDavid Greenman #include <sys/vmmeter.h>
565b81b6b3SRodney W. Grimes 
572320728fSRodney W. Grimes #include <machine/clock.h>
58a2a1c95cSPeter Wemm #include <machine/cpu.h>
591f8745a9SPeter Wemm #include <machine/md_var.h>
608fa40736SBruce Evans #ifdef SMP
618fa40736SBruce Evans #include <machine/smp.h>
628fa40736SBruce Evans #endif
635b81b6b3SRodney W. Grimes 
6426f9a767SRodney W. Grimes #include <vm/vm.h>
65efeaf95aSDavid Greenman #include <vm/vm_param.h>
66efeaf95aSDavid Greenman #include <vm/vm_prot.h>
67996c772fSJohn Dyson #include <sys/lock.h>
6826f9a767SRodney W. Grimes #include <vm/vm_kern.h>
6924a1cce3SDavid Greenman #include <vm/vm_page.h>
70efeaf95aSDavid Greenman #include <vm/vm_map.h>
71efeaf95aSDavid Greenman #include <vm/vm_extern.h>
72efeaf95aSDavid Greenman 
73efeaf95aSDavid Greenman #include <sys/user.h>
745b81b6b3SRodney W. Grimes 
75e30f0011SSatoshi Asami #ifdef PC98
76e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h>
77e30f0011SSatoshi Asami #else
782320728fSRodney W. Grimes #include <i386/isa/isa.h>
79e30f0011SSatoshi Asami #endif
802320728fSRodney W. Grimes 
819aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
8287b91157SPoul-Henning Kamp static vm_offset_t
836c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
8487b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
856c146e28SBruce Evans 					int now));
8687b91157SPoul-Henning Kamp static vm_offset_t
876c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
8887b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
896c146e28SBruce Evans 
9087b6de2bSPoul-Henning Kamp static volatile int	kvasfreecnt;
91ae92ea44SDavid Greenman 
92d5e26ef0SDavid Greenman caddr_t		bouncememory;
9387b91157SPoul-Henning Kamp static int	bpwait;
9487b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
9587b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
96d5e26ef0SDavid Greenman 
97ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
9887b91157SPoul-Henning Kamp static int		bounceallocarraysize;
9987b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
10087b91157SPoul-Henning Kamp static int		bouncefree;
101d5e26ef0SDavid Greenman 
102e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA)
103e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096)			/* 15MB boundary */
104e30f0011SSatoshi Asami #else
105d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
106e30f0011SSatoshi Asami #endif
107ae92ea44SDavid Greenman #define MAXBKVA 1024
108f8845af0SPoul-Henning Kamp int		maxbkva = MAXBKVA*PAGE_SIZE;
109d5e26ef0SDavid Greenman 
110d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
11187b91157SPoul-Henning Kamp static struct kvasfree {
112d5e26ef0SDavid Greenman 	vm_offset_t addr;
113d5e26ef0SDavid Greenman 	vm_offset_t size;
114d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
115d5e26ef0SDavid Greenman 
116d5e26ef0SDavid Greenman /*
117d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
118d5e26ef0SDavid Greenman  * (only 1 inplemented now)
119d5e26ef0SDavid Greenman  */
12087b91157SPoul-Henning Kamp static vm_offset_t
121d5e26ef0SDavid Greenman vm_bounce_page_find(count)
122d5e26ef0SDavid Greenman 	int count;
123d5e26ef0SDavid Greenman {
124d5e26ef0SDavid Greenman 	int bit;
125d5e26ef0SDavid Greenman 	int s,i;
126d5e26ef0SDavid Greenman 
127d5e26ef0SDavid Greenman 	if (count != 1)
128d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
129d5e26ef0SDavid Greenman 
130d5e26ef0SDavid Greenman 	s = splbio();
131d5e26ef0SDavid Greenman retry:
132d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
133d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1343fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1353fb3086eSPoul-Henning Kamp 			if (bit) {
136d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
137d5e26ef0SDavid Greenman 				bouncefree -= count;
138d5e26ef0SDavid Greenman 				splx(s);
139ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
140d5e26ef0SDavid Greenman 			}
141d5e26ef0SDavid Greenman 		}
142d5e26ef0SDavid Greenman 	}
143ed7fcbd0SDavid Greenman 	bpwait = 1;
144d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
145d5e26ef0SDavid Greenman 	goto retry;
146d5e26ef0SDavid Greenman }
147d5e26ef0SDavid Greenman 
14887b91157SPoul-Henning Kamp static void
1491561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1501561d038SDavid Greenman 	vm_offset_t addr;
1511561d038SDavid Greenman 	vm_offset_t size;
1521561d038SDavid Greenman 	int now;
1531561d038SDavid Greenman {
1541561d038SDavid Greenman 	int s = splbio();
1551561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
156ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
157ae92ea44SDavid Greenman 	++kvasfreecnt;
1580e195446SDavid Greenman 	if( now) {
1590e195446SDavid Greenman 		/*
1600e195446SDavid Greenman 		 * this will do wakeups
1610e195446SDavid Greenman 		 */
1621561d038SDavid Greenman 		vm_bounce_kva(0,0);
1630e195446SDavid Greenman 	} else {
1640e195446SDavid Greenman 		if (bmwait) {
1650e195446SDavid Greenman 		/*
1660e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1670e195446SDavid Greenman 		 */
1681561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1690e195446SDavid Greenman 			bmwait = 0;
1700e195446SDavid Greenman 		}
1710e195446SDavid Greenman 	}
1721561d038SDavid Greenman 	splx(s);
1731561d038SDavid Greenman }
1741561d038SDavid Greenman 
175d5e26ef0SDavid Greenman /*
176d5e26ef0SDavid Greenman  * free count bounce buffer pages
177d5e26ef0SDavid Greenman  */
17887b91157SPoul-Henning Kamp static void
179d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
180d5e26ef0SDavid Greenman 	vm_offset_t pa;
181d5e26ef0SDavid Greenman 	int count;
182d5e26ef0SDavid Greenman {
183d5e26ef0SDavid Greenman 	int allocindex;
184d5e26ef0SDavid Greenman 	int index;
185d5e26ef0SDavid Greenman 	int bit;
186d5e26ef0SDavid Greenman 
187d5e26ef0SDavid Greenman 	if (count != 1)
188edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
189d5e26ef0SDavid Greenman 
190ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
191ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
192ae92ea44SDavid Greenman 			break;
193ae92ea44SDavid Greenman 	}
194d5e26ef0SDavid Greenman 
195ae92ea44SDavid Greenman 	if( index == bouncepages)
196ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
197d5e26ef0SDavid Greenman 
198ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
199ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
200d5e26ef0SDavid Greenman 
201d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
202d5e26ef0SDavid Greenman 
203d5e26ef0SDavid Greenman 	bouncefree += count;
204ed7fcbd0SDavid Greenman 	if (bpwait) {
205ed7fcbd0SDavid Greenman 		bpwait = 0;
206d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
207d5e26ef0SDavid Greenman 	}
208ed7fcbd0SDavid Greenman }
209d5e26ef0SDavid Greenman 
210d5e26ef0SDavid Greenman /*
211d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
212d5e26ef0SDavid Greenman  */
21387b91157SPoul-Henning Kamp static vm_offset_t
214ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
215ae92ea44SDavid Greenman 	int size;
2161561d038SDavid Greenman 	int waitok;
217d5e26ef0SDavid Greenman {
218d5e26ef0SDavid Greenman 	int i;
2196b4ac811SDavid Greenman 	vm_offset_t kva = 0;
220c564966bSDavid Greenman 	vm_offset_t off;
221d5e26ef0SDavid Greenman 	int s = splbio();
222d5e26ef0SDavid Greenman more:
223ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
224d5e26ef0SDavid Greenman 		bmfreeing = 1;
225ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
226f8845af0SPoul-Henning Kamp 			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
227c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
228c564966bSDavid Greenman 			}
2291561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
230d5e26ef0SDavid Greenman 				kvaf[i].size);
231d5e26ef0SDavid Greenman 		}
232d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
233d5e26ef0SDavid Greenman 		bmfreeing = 0;
234ae92ea44SDavid Greenman 		if( bmwait) {
235ae92ea44SDavid Greenman 			bmwait = 0;
236ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
237ae92ea44SDavid Greenman 		}
238d5e26ef0SDavid Greenman 	}
239d5e26ef0SDavid Greenman 
2401561d038SDavid Greenman 	if( size == 0) {
2411561d038SDavid Greenman 		splx(s);
2426ab46d52SBruce Evans 		return 0;
2431561d038SDavid Greenman 	}
2441561d038SDavid Greenman 
245ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2461561d038SDavid Greenman 		if( !waitok) {
2471561d038SDavid Greenman 			splx(s);
2486ab46d52SBruce Evans 			return 0;
2491561d038SDavid Greenman 		}
250d5e26ef0SDavid Greenman 		bmwait = 1;
2511561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
252d5e26ef0SDavid Greenman 		goto more;
253d5e26ef0SDavid Greenman 	}
254d5e26ef0SDavid Greenman 	splx(s);
255d5e26ef0SDavid Greenman 	return kva;
256d5e26ef0SDavid Greenman }
257d5e26ef0SDavid Greenman 
258d5e26ef0SDavid Greenman /*
259ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2600e195446SDavid Greenman  */
2610e195446SDavid Greenman vm_offset_t
2620e195446SDavid Greenman vm_bounce_kva_alloc(count)
2630e195446SDavid Greenman int count;
2640e195446SDavid Greenman {
2650e195446SDavid Greenman 	int i;
2660e195446SDavid Greenman 	vm_offset_t kva;
2670e195446SDavid Greenman 	vm_offset_t pa;
2680e195446SDavid Greenman 	if( bouncepages == 0) {
269f8845af0SPoul-Henning Kamp 		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
2700e195446SDavid Greenman 		return kva;
2710e195446SDavid Greenman 	}
272f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
2730e195446SDavid Greenman 	for(i=0;i<count;i++) {
2740e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
275f8845af0SPoul-Henning Kamp 		pmap_kenter(kva + i * PAGE_SIZE, pa);
2760e195446SDavid Greenman 	}
2770e195446SDavid Greenman 	return kva;
2780e195446SDavid Greenman }
2790e195446SDavid Greenman 
2800e195446SDavid Greenman /*
2810e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2820e195446SDavid Greenman  */
2830e195446SDavid Greenman void
2840e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2850e195446SDavid Greenman 	vm_offset_t kva;
2860e195446SDavid Greenman 	int count;
2870e195446SDavid Greenman {
2880e195446SDavid Greenman 	int i;
2890e195446SDavid Greenman 	vm_offset_t pa;
2900e195446SDavid Greenman 	if( bouncepages == 0) {
2910e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2920e195446SDavid Greenman 		return;
2930e195446SDavid Greenman 	}
2940e195446SDavid Greenman 	for(i = 0; i < count; i++) {
295f8845af0SPoul-Henning Kamp 		pa = pmap_kextract(kva + i * PAGE_SIZE);
2960e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
2970e195446SDavid Greenman 	}
298f8845af0SPoul-Henning Kamp 	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
2990e195446SDavid Greenman }
3000e195446SDavid Greenman 
3010e195446SDavid Greenman /*
302d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
303d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
304d5e26ef0SDavid Greenman  */
305d5e26ef0SDavid Greenman void
306d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
307d5e26ef0SDavid Greenman 	struct buf *bp;
308d5e26ef0SDavid Greenman {
309d5e26ef0SDavid Greenman 	int countvmpg;
310d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
311d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
312d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
313d5e26ef0SDavid Greenman 	vm_offset_t pa;
314d5e26ef0SDavid Greenman 	int dobounceflag = 0;
315d5e26ef0SDavid Greenman 	int i;
316d5e26ef0SDavid Greenman 
317d5e26ef0SDavid Greenman 	if (bouncepages == 0)
318d5e26ef0SDavid Greenman 		return;
319d5e26ef0SDavid Greenman 
320ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
321ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
322ae92ea44SDavid Greenman 		return;
3231561d038SDavid Greenman 	}
3241561d038SDavid Greenman 
325ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3263fb3086eSPoul-Henning Kamp 		printf(
3273fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
328ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
329ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
330ae92ea44SDavid Greenman 	}
331ae92ea44SDavid Greenman 
332ae92ea44SDavid Greenman /*
333ae92ea44SDavid Greenman  *  This is not really necessary
334ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
335ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
336ae92ea44SDavid Greenman  *	}
337ae92ea44SDavid Greenman  */
338ae92ea44SDavid Greenman 
339ae92ea44SDavid Greenman 
34026f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
34126f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
342d5e26ef0SDavid Greenman 
343bd7e5f99SJohn Dyson 	vapstart = trunc_page(vastart);
344bd7e5f99SJohn Dyson 	vapend = round_page(vaend);
345f8845af0SPoul-Henning Kamp 	countvmpg = (vapend - vapstart) / PAGE_SIZE;
346d5e26ef0SDavid Greenman 
347d5e26ef0SDavid Greenman /*
348d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
349d5e26ef0SDavid Greenman  */
350d5e26ef0SDavid Greenman 	va = vapstart;
351d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
352ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
353d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
354d5e26ef0SDavid Greenman 			++dobounceflag;
3550d94caffSDavid Greenman 		if( pa == 0)
3560d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
357f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
358d5e26ef0SDavid Greenman 	}
359d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
360d5e26ef0SDavid Greenman 		return;
361d5e26ef0SDavid Greenman 
362d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
363d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
364d5e26ef0SDavid Greenman 
365d5e26ef0SDavid Greenman /*
366d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
367d5e26ef0SDavid Greenman  */
368f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
369ae92ea44SDavid Greenman #if 0
370ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
371ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
372ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
373ae92ea44SDavid Greenman #endif
374d5e26ef0SDavid Greenman 	va = vapstart;
375d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
376ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
377d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
378d5e26ef0SDavid Greenman 			/*
379d5e26ef0SDavid Greenman 			 * allocate a replacement page
380d5e26ef0SDavid Greenman 			 */
381d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
382f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
383ae92ea44SDavid Greenman #if 0
384ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
385ae92ea44SDavid Greenman #endif
386d5e26ef0SDavid Greenman 			/*
387d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
388d5e26ef0SDavid Greenman 			 */
3891561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
390f8845af0SPoul-Henning Kamp 				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
3911561d038SDavid Greenman 			}
392d5e26ef0SDavid Greenman 		} else {
393d5e26ef0SDavid Greenman 			/*
394d5e26ef0SDavid Greenman 			 * use original page
395d5e26ef0SDavid Greenman 			 */
396f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), pa);
397d5e26ef0SDavid Greenman 		}
398f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
399d5e26ef0SDavid Greenman 	}
400d5e26ef0SDavid Greenman 
401d5e26ef0SDavid Greenman /*
402d5e26ef0SDavid Greenman  * flag the buffer as being bounced
403d5e26ef0SDavid Greenman  */
404d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
405d5e26ef0SDavid Greenman /*
406d5e26ef0SDavid Greenman  * save the original buffer kva
407d5e26ef0SDavid Greenman  */
40826f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
409d5e26ef0SDavid Greenman /*
410d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
411d5e26ef0SDavid Greenman  */
41226f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
413f8845af0SPoul-Henning Kamp 				((vm_offset_t) bp->b_savekva & PAGE_MASK));
414ae92ea44SDavid Greenman #if 0
415ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
416ae92ea44SDavid Greenman #endif
417d5e26ef0SDavid Greenman 	return;
418d5e26ef0SDavid Greenman }
419d5e26ef0SDavid Greenman 
420d5e26ef0SDavid Greenman /*
421d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
422d5e26ef0SDavid Greenman  */
423d5e26ef0SDavid Greenman void
424d5e26ef0SDavid Greenman vm_bounce_free(bp)
425d5e26ef0SDavid Greenman 	struct buf *bp;
426d5e26ef0SDavid Greenman {
427d5e26ef0SDavid Greenman 	int i;
428ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
429d5e26ef0SDavid Greenman 
430d5e26ef0SDavid Greenman /*
431d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
432d5e26ef0SDavid Greenman  */
433d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
434d5e26ef0SDavid Greenman 		return;
435d5e26ef0SDavid Greenman 
436ae92ea44SDavid Greenman /*
437ae92ea44SDavid Greenman  *  This check is not necessary
438ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
439ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
440ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
441ae92ea44SDavid Greenman  *	}
442ae92ea44SDavid Greenman  */
443ae92ea44SDavid Greenman 
444d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
44526f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
446ae92ea44SDavid Greenman /*
447ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
448ae92ea44SDavid Greenman */
449d5e26ef0SDavid Greenman 
450d5e26ef0SDavid Greenman /*
451d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
452d5e26ef0SDavid Greenman  */
453ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
454d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
455d5e26ef0SDavid Greenman 		vm_offset_t copycount;
456d5e26ef0SDavid Greenman 
457bd7e5f99SJohn Dyson 		copycount = round_page(bouncekva + 1) - bouncekva;
458bd7e5f99SJohn Dyson 		mybouncepa = pmap_kextract(trunc_page(bouncekva));
459d5e26ef0SDavid Greenman 
460d5e26ef0SDavid Greenman /*
461d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
462d5e26ef0SDavid Greenman  */
463bd7e5f99SJohn Dyson 		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
464ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
465ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
466ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
467d5e26ef0SDavid Greenman /*
468d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
469d5e26ef0SDavid Greenman  */
470d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
471ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
472d5e26ef0SDavid Greenman /*
473d5e26ef0SDavid Greenman  * free the bounce allocation
474d5e26ef0SDavid Greenman  */
475ae92ea44SDavid Greenman 
476ae92ea44SDavid Greenman /*
477ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
478ae92ea44SDavid Greenman */
479ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
480d5e26ef0SDavid Greenman 		}
481d5e26ef0SDavid Greenman 
482d5e26ef0SDavid Greenman 		origkva += copycount;
483d5e26ef0SDavid Greenman 		bouncekva += copycount;
484ae92ea44SDavid Greenman 		i += copycount;
485d5e26ef0SDavid Greenman 	}
486d5e26ef0SDavid Greenman 
487d5e26ef0SDavid Greenman /*
488ae92ea44SDavid Greenman 	printf("\n");
489ae92ea44SDavid Greenman */
490ae92ea44SDavid Greenman /*
491d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
492d5e26ef0SDavid Greenman  */
493ae92ea44SDavid Greenman 
494bd7e5f99SJohn Dyson 	bouncekva= trunc_page((vm_offset_t) bp->b_data);
495bd7e5f99SJohn Dyson 	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
496ae92ea44SDavid Greenman 
497ae92ea44SDavid Greenman /*
498f8845af0SPoul-Henning Kamp 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
499ae92ea44SDavid Greenman */
500ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
50126f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
502d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
503d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
504d5e26ef0SDavid Greenman 
505d5e26ef0SDavid Greenman 	return;
506d5e26ef0SDavid Greenman }
507d5e26ef0SDavid Greenman 
508ae92ea44SDavid Greenman 
5095b81b6b3SRodney W. Grimes /*
5101561d038SDavid Greenman  * init the bounce buffer system
5111561d038SDavid Greenman  */
5121561d038SDavid Greenman void
5131561d038SDavid Greenman vm_bounce_init()
5141561d038SDavid Greenman {
515ae92ea44SDavid Greenman 	int i;
5161561d038SDavid Greenman 
5171561d038SDavid Greenman 	kvasfreecnt = 0;
5181561d038SDavid Greenman 
5191561d038SDavid Greenman 	if (bouncepages == 0)
5201561d038SDavid Greenman 		return;
5211561d038SDavid Greenman 
5221561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5231561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5241561d038SDavid Greenman 
5251561d038SDavid Greenman 	if (!bounceallocarray)
526edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5271561d038SDavid Greenman 
528ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
529ae92ea44SDavid Greenman 	if (!bouncepa)
530edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5311561d038SDavid Greenman 
5320d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5330d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5340d94caffSDavid Greenman 	}
5350d94caffSDavid Greenman 
536ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
537ae92ea44SDavid Greenman 		vm_offset_t pa;
5380621c31aSJohn Dyson 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) {
5390621c31aSJohn Dyson 			printf("vm_bounce_init: bounce memory out of range -- bounce disabled\n");
5400621c31aSJohn Dyson 			free(bounceallocarray, M_TEMP);
5410621c31aSJohn Dyson 			bounceallocarray = NULL;
5420621c31aSJohn Dyson 			free(bouncepa, M_TEMP);
5430621c31aSJohn Dyson 			bouncepa = NULL;
5440621c31aSJohn Dyson 			bouncepages = 0;
5450621c31aSJohn Dyson 			break;
5460621c31aSJohn Dyson 		}
547ae92ea44SDavid Greenman 		if( pa == 0)
548ae92ea44SDavid Greenman 			panic("bounce memory not resident");
549ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5500d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
551ae92ea44SDavid Greenman 	}
5521561d038SDavid Greenman 	bouncefree = bouncepages;
5531561d038SDavid Greenman 
5541561d038SDavid Greenman }
5559aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5566c146e28SBruce Evans 
557a4f7a4c9SDavid Greenman /*
558a4f7a4c9SDavid Greenman  * quick version of vm_fault
559a4f7a4c9SDavid Greenman  */
560dca51299SJohn Dyson void
561a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5626c146e28SBruce Evans 	caddr_t v;
563a4f7a4c9SDavid Greenman 	int prot;
564a4f7a4c9SDavid Greenman {
5651d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5666c146e28SBruce Evans 		subyte(v, fubyte(v));
567a4f7a4c9SDavid Greenman 	else
5686c146e28SBruce Evans 		fubyte(v);
569a4f7a4c9SDavid Greenman }
570a4f7a4c9SDavid Greenman 
5711561d038SDavid Greenman /*
5725b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
573a2a1c95cSPeter Wemm  * Copy and update the pcb, set up the stack so that the child
574a2a1c95cSPeter Wemm  * ready to run and return to user mode.
5755b81b6b3SRodney W. Grimes  */
576a2a1c95cSPeter Wemm void
5775b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5785b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5795b81b6b3SRodney W. Grimes {
580f83a01b6SDavid Greenman 	struct pcb *pcb2 = &p2->p_addr->u_pcb;
5815b81b6b3SRodney W. Grimes 
5829f449d2aSBruce Evans #if NNPX > 0
5831f8745a9SPeter Wemm 	/* Ensure that p1's pcb is up to date. */
5841f8745a9SPeter Wemm 	if (npxproc == p1)
5851f8745a9SPeter Wemm 		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
5869f449d2aSBruce Evans #endif
5871f8745a9SPeter Wemm 
5881f8745a9SPeter Wemm 	/* Copy p1's pcb. */
5891f8745a9SPeter Wemm 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
590a2a1c95cSPeter Wemm 
591a2a1c95cSPeter Wemm 	/*
592a2a1c95cSPeter Wemm 	 * Create a new fresh stack for the new process.
5931f8745a9SPeter Wemm 	 * Copy the trap frame for the return to user mode as if from a
5941f8745a9SPeter Wemm 	 * syscall.  This copies the user mode register values.
595a2a1c95cSPeter Wemm 	 */
5961f8745a9SPeter Wemm 	p2->p_md.md_regs = (struct trapframe *)
5971f8745a9SPeter Wemm 			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
5981f8745a9SPeter Wemm 	*p2->p_md.md_regs = *p1->p_md.md_regs;
599a2a1c95cSPeter Wemm 
600a2a1c95cSPeter Wemm 	/*
601a2a1c95cSPeter Wemm 	 * Set registers for trampoline to user mode.  Leave space for the
602a2a1c95cSPeter Wemm 	 * return address on stack.  These are the kernel mode register values.
603a2a1c95cSPeter Wemm 	 */
6041f8745a9SPeter Wemm 	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
6051f8745a9SPeter Wemm 	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
606a2a1c95cSPeter Wemm 	pcb2->pcb_esi = (int)fork_return;
6071f8745a9SPeter Wemm 	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
608a2a1c95cSPeter Wemm 	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
6091f8745a9SPeter Wemm 	pcb2->pcb_ebx = (int)p2;
6101f8745a9SPeter Wemm 	pcb2->pcb_eip = (int)fork_trampoline;
6111f8745a9SPeter Wemm 	/*
6121f8745a9SPeter Wemm 	 * pcb2->pcb_ldt:	duplicated below, if necessary.
6131f8745a9SPeter Wemm 	 * pcb2->pcb_ldt_len:	cloned above.
6141f8745a9SPeter Wemm 	 * pcb2->pcb_savefpu:	cloned above.
6151f8745a9SPeter Wemm 	 * pcb2->pcb_flags:	cloned above (always 0 here?).
6161f8745a9SPeter Wemm 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
6171f8745a9SPeter Wemm 	 */
6185b81b6b3SRodney W. Grimes 
61948a09cf2SJohn Dyson #ifdef VM86
62048a09cf2SJohn Dyson 	/*
62148a09cf2SJohn Dyson 	 * XXX don't copy the i/o pages.  this should probably be fixed.
62248a09cf2SJohn Dyson 	 */
62348a09cf2SJohn Dyson 	pcb2->pcb_ext = 0;
62448a09cf2SJohn Dyson #endif
62548a09cf2SJohn Dyson 
6268c39a127SStefan Eßer #ifdef USER_LDT
6278c39a127SStefan Eßer         /* Copy the LDT, if necessary. */
628a2a1c95cSPeter Wemm         if (pcb2->pcb_ldt != 0) {
6298c39a127SStefan Eßer                 union descriptor *new_ldt;
630a2a1c95cSPeter Wemm                 size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
6318c39a127SStefan Eßer 
6328c39a127SStefan Eßer                 new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
633a2a1c95cSPeter Wemm                 bcopy(pcb2->pcb_ldt, new_ldt, len);
634a2a1c95cSPeter Wemm                 pcb2->pcb_ldt = (caddr_t)new_ldt;
6358c39a127SStefan Eßer         }
6368c39a127SStefan Eßer #endif
6378c39a127SStefan Eßer 
638a2a1c95cSPeter Wemm 	/*
639a2a1c95cSPeter Wemm 	 * Now, cpu_switch() can schedule the new process.
640a2a1c95cSPeter Wemm 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
641a2a1c95cSPeter Wemm 	 * containing the return address when exiting cpu_switch.
642a2a1c95cSPeter Wemm 	 * This will normally be to proc_trampoline(), which will have
643a2a1c95cSPeter Wemm 	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
644a2a1c95cSPeter Wemm 	 * will set up a stack to call fork_return(p, frame); to complete
645a2a1c95cSPeter Wemm 	 * the return to user-mode.
646a2a1c95cSPeter Wemm 	 */
647a2a1c95cSPeter Wemm }
648a2a1c95cSPeter Wemm 
649a2a1c95cSPeter Wemm /*
650a2a1c95cSPeter Wemm  * Intercept the return address from a freshly forked process that has NOT
651a2a1c95cSPeter Wemm  * been scheduled yet.
652a2a1c95cSPeter Wemm  *
653a2a1c95cSPeter Wemm  * This is needed to make kernel threads stay in kernel mode.
654a2a1c95cSPeter Wemm  */
655a2a1c95cSPeter Wemm void
656a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg)
657a2a1c95cSPeter Wemm 	struct proc *p;
658a2a1c95cSPeter Wemm 	void (*func) __P((void *));
659a2a1c95cSPeter Wemm 	void *arg;
660a2a1c95cSPeter Wemm {
661a2a1c95cSPeter Wemm 	/*
662a2a1c95cSPeter Wemm 	 * Note that the trap frame follows the args, so the function
663a2a1c95cSPeter Wemm 	 * is really called like this:  func(arg, frame);
664a2a1c95cSPeter Wemm 	 */
665a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
666a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
6675b81b6b3SRodney W. Grimes }
6685b81b6b3SRodney W. Grimes 
6697c2b54e8SNate Williams void
6705b81b6b3SRodney W. Grimes cpu_exit(p)
6715b81b6b3SRodney W. Grimes 	register struct proc *p;
6725b81b6b3SRodney W. Grimes {
67348a09cf2SJohn Dyson #if defined(USER_LDT) || defined(VM86)
67448a09cf2SJohn Dyson 	struct pcb *pcb = &p->p_addr->u_pcb;
6750dbf6d73SJordan K. Hubbard #endif
6765b81b6b3SRodney W. Grimes 
677960173b9SRodney W. Grimes #if NNPX > 0
6785b81b6b3SRodney W. Grimes 	npxexit(p);
679960173b9SRodney W. Grimes #endif	/* NNPX */
68048a09cf2SJohn Dyson #ifdef VM86
68148a09cf2SJohn Dyson 	if (pcb->pcb_ext != 0) {
68248a09cf2SJohn Dyson 	        /*
68348a09cf2SJohn Dyson 		 * XXX do we need to move the TSS off the allocated pages
68448a09cf2SJohn Dyson 		 * before freeing them?  (not done here)
68548a09cf2SJohn Dyson 		 */
68648a09cf2SJohn Dyson 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
68748a09cf2SJohn Dyson 		    ctob(IOPAGES + 1));
68848a09cf2SJohn Dyson 		pcb->pcb_ext = 0;
68948a09cf2SJohn Dyson 	}
69048a09cf2SJohn Dyson #endif
6910dbf6d73SJordan K. Hubbard #ifdef USER_LDT
6920dbf6d73SJordan K. Hubbard 	if (pcb->pcb_ldt != 0) {
6930dbf6d73SJordan K. Hubbard 		if (pcb == curpcb)
6940dbf6d73SJordan K. Hubbard 			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
6950dbf6d73SJordan K. Hubbard 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
6960dbf6d73SJordan K. Hubbard 			pcb->pcb_ldt_len * sizeof(union descriptor));
6970dbf6d73SJordan K. Hubbard 		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
6980dbf6d73SJordan K. Hubbard 	}
6990dbf6d73SJordan K. Hubbard #endif
7001a051896SBruce Evans 	cnt.v_swtch++;
7011a051896SBruce Evans 	cpu_switch(p);
7027c2b54e8SNate Williams 	panic("cpu_exit");
7035b81b6b3SRodney W. Grimes }
7045b81b6b3SRodney W. Grimes 
705381fe1aaSGarrett Wollman void
706bd7e5f99SJohn Dyson cpu_wait(p)
707bd7e5f99SJohn Dyson 	struct proc *p;
708bd7e5f99SJohn Dyson {
7095b81b6b3SRodney W. Grimes 	/* drop per-process resources */
710675878e7SJohn Dyson 	pmap_dispose_proc(p);
7112d8acc0fSJohn Dyson 
7122d8acc0fSJohn Dyson 	/* and clean-out the vmspace */
7137f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
7145b81b6b3SRodney W. Grimes }
7155b81b6b3SRodney W. Grimes 
7165b81b6b3SRodney W. Grimes /*
71726f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
71826f9a767SRodney W. Grimes  */
71926f9a767SRodney W. Grimes int
72026f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
72126f9a767SRodney W. Grimes 	struct proc *p;
72226f9a767SRodney W. Grimes 	struct vnode *vp;
72326f9a767SRodney W. Grimes 	struct ucred *cred;
72426f9a767SRodney W. Grimes {
72526f9a767SRodney W. Grimes 
72626f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
72726f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
72826f9a767SRodney W. Grimes 	    p));
72926f9a767SRodney W. Grimes }
73026f9a767SRodney W. Grimes 
73187b6de2bSPoul-Henning Kamp #ifdef notyet
73287b91157SPoul-Henning Kamp static void
7335b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
7345b81b6b3SRodney W. Grimes 	u_short *pte;
7355b81b6b3SRodney W. Grimes 	caddr_t vaddr;
7365b81b6b3SRodney W. Grimes {
7375b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
7385b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
7395b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
7405b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
7415b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
7425b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
7435b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
7445b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
7455b81b6b3SRodney W. Grimes }
74687b6de2bSPoul-Henning Kamp #endif
74726f9a767SRodney W. Grimes 
74826f9a767SRodney W. Grimes /*
7495b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
7505b81b6b3SRodney W. Grimes  */
751aaf08d94SGarrett Wollman u_long
7527f8cb368SDavid Greenman kvtop(void *addr)
7535b81b6b3SRodney W. Grimes {
7545b81b6b3SRodney W. Grimes 	vm_offset_t va;
7555b81b6b3SRodney W. Grimes 
756ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
7575b81b6b3SRodney W. Grimes 	if (va == 0)
7585b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
7597f8cb368SDavid Greenman 	return((int)va);
7605b81b6b3SRodney W. Grimes }
7615b81b6b3SRodney W. Grimes 
7625b81b6b3SRodney W. Grimes /*
763ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
7645b81b6b3SRodney W. Grimes  *
765ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
766ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
767ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
7685b81b6b3SRodney W. Grimes  */
769381fe1aaSGarrett Wollman void
7705b81b6b3SRodney W. Grimes vmapbuf(bp)
7715b81b6b3SRodney W. Grimes 	register struct buf *bp;
7725b81b6b3SRodney W. Grimes {
7730157d6d9SJohn Dyson 	register caddr_t addr, v, kva;
77407658526SPoul-Henning Kamp 	vm_offset_t pa;
7755b81b6b3SRodney W. Grimes 
77626f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7775b81b6b3SRodney W. Grimes 		panic("vmapbuf");
77826f9a767SRodney W. Grimes 
77912936855SDavid Greenman 	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
78026f9a767SRodney W. Grimes 	    addr < bp->b_data + bp->b_bufsize;
7810157d6d9SJohn Dyson 	    addr += PAGE_SIZE, v += PAGE_SIZE) {
78226f9a767SRodney W. Grimes 		/*
783ac269d78SDavid Greenman 		 * Do the vm_fault if needed; do the copy-on-write thing
784ac269d78SDavid Greenman 		 * when reading stuff off device into memory.
78526f9a767SRodney W. Grimes 		 */
78626f9a767SRodney W. Grimes 		vm_fault_quick(addr,
78726f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
7880157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
789641a27e6SJohn Dyson 		if (pa == 0)
790641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
79126f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
7920157d6d9SJohn Dyson 		pmap_kenter((vm_offset_t) v, pa);
79326f9a767SRodney W. Grimes 	}
79426f9a767SRodney W. Grimes 
7950157d6d9SJohn Dyson 	kva = bp->b_saveaddr;
7960157d6d9SJohn Dyson 	bp->b_saveaddr = bp->b_data;
7970157d6d9SJohn Dyson 	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
7985b81b6b3SRodney W. Grimes }
7995b81b6b3SRodney W. Grimes 
8005b81b6b3SRodney W. Grimes /*
8015b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
8025b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
8035b81b6b3SRodney W. Grimes  */
804381fe1aaSGarrett Wollman void
8055b81b6b3SRodney W. Grimes vunmapbuf(bp)
8065b81b6b3SRodney W. Grimes 	register struct buf *bp;
8075b81b6b3SRodney W. Grimes {
808c564966bSDavid Greenman 	register caddr_t addr;
80907658526SPoul-Henning Kamp 	vm_offset_t pa;
8105b81b6b3SRodney W. Grimes 
8115b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
8125b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
813c564966bSDavid Greenman 
81412936855SDavid Greenman 	for (addr = (caddr_t)trunc_page(bp->b_data);
81512936855SDavid Greenman 	    addr < bp->b_data + bp->b_bufsize;
816f8845af0SPoul-Henning Kamp 	    addr += PAGE_SIZE) {
8170157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
8180157d6d9SJohn Dyson 		pmap_kremove((vm_offset_t) addr);
81926f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
82026f9a767SRodney W. Grimes 	}
8210157d6d9SJohn Dyson 
8220157d6d9SJohn Dyson 	bp->b_data = bp->b_saveaddr;
8235b81b6b3SRodney W. Grimes }
8245b81b6b3SRodney W. Grimes 
8255b81b6b3SRodney W. Grimes /*
8265b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
8275b81b6b3SRodney W. Grimes  */
8287f8cb368SDavid Greenman void
829d447dbeeSBruce Evans cpu_reset()
830d447dbeeSBruce Evans {
831d447dbeeSBruce Evans 
83203245f09SKATO Takenori #ifdef PC98
83303245f09SKATO Takenori 	/*
83403245f09SKATO Takenori 	 * Attempt to do a CPU reset via CPU reset port.
83503245f09SKATO Takenori 	 */
836d447dbeeSBruce Evans 	disable_intr();
83703245f09SKATO Takenori 	outb(0x37, 0x0f);		/* SHUT0 = 0. */
83803245f09SKATO Takenori 	outb(0x37, 0x0b);		/* SHUT1 = 0. */
83903245f09SKATO Takenori 	outb(0xf0, 0x00);		/* Reset. */
84003245f09SKATO Takenori #else
8412320728fSRodney W. Grimes 	/*
8422320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
8432320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
8442320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
8452320728fSRodney W. Grimes 	 */
8465eb46edfSDavid Greenman 
84703245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET)
8482320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
8492320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
8502320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
8512320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
8525eb46edfSDavid Greenman #endif
85303245f09SKATO Takenori #endif /* PC98 */
8545b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
855f8845af0SPoul-Henning Kamp 	bzero((caddr_t) PTD, PAGE_SIZE);
8565b81b6b3SRodney W. Grimes 
8575b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
85827e9b35eSJohn Dyson 	invltlb();
8595b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8607f8cb368SDavid Greenman 	while(1);
8615b81b6b3SRodney W. Grimes }
862b9d60b3fSDavid Greenman 
863b9d60b3fSDavid Greenman /*
864b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
86529360eb0SDavid Greenman  *	chunks of SGROWSIZ.
866b9d60b3fSDavid Greenman  */
867b9d60b3fSDavid Greenman int
868b9d60b3fSDavid Greenman grow(p, sp)
869b9d60b3fSDavid Greenman 	struct proc *p;
87026f9a767SRodney W. Grimes 	u_int sp;
871b9d60b3fSDavid Greenman {
872b9d60b3fSDavid Greenman 	unsigned int nss;
873b9d60b3fSDavid Greenman 	caddr_t v;
874b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
875b9d60b3fSDavid Greenman 
876b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
877b9d60b3fSDavid Greenman 	    return (1);
878b9d60b3fSDavid Greenman 
879b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
880b9d60b3fSDavid Greenman 
881b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
882b9d60b3fSDavid Greenman 		return (0);
883b9d60b3fSDavid Greenman 
884b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
88529360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
886b9d60b3fSDavid Greenman 		int grow_amount;
887b9d60b3fSDavid Greenman 		/*
888b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
889b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
890b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
891b9d60b3fSDavid Greenman 		 * is expensive).
892b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
89329360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
894b9d60b3fSDavid Greenman 		 */
89529360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
896b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
89729360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
898b9d60b3fSDavid Greenman 		/*
89929360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
900b9d60b3fSDavid Greenman 		 * just extend to the maximum size
901b9d60b3fSDavid Greenman 		 */
902b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
903b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
904b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
905b9d60b3fSDavid Greenman 		}
90668940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
907bd7e5f99SJohn Dyson 		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
908b9d60b3fSDavid Greenman 			return (0);
909b9d60b3fSDavid Greenman 		}
910b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
911b9d60b3fSDavid Greenman 	}
912b9d60b3fSDavid Greenman 
913b9d60b3fSDavid Greenman 	return (1);
914b9d60b3fSDavid Greenman }
915a29b63cbSJohn Dyson 
916a29b63cbSJohn Dyson /*
91783eab616SBruce Evans  * Implement the pre-zeroed page mechanism.
91883eab616SBruce Evans  * This routine is called from the idle loop.
919a29b63cbSJohn Dyson  */
920a29b63cbSJohn Dyson int
92183eab616SBruce Evans vm_page_zero_idle()
92283eab616SBruce Evans {
92383eab616SBruce Evans 	static int free_rover;
924a29b63cbSJohn Dyson 	vm_page_t m;
92583eab616SBruce Evans 	int s;
92683eab616SBruce Evans 
92783eab616SBruce Evans #ifdef WRONG
92883eab616SBruce Evans 	if (cnt.v_free_count <= cnt.v_interrupt_free_min)
92983eab616SBruce Evans 		return (0);
93083eab616SBruce Evans #endif
93183eab616SBruce Evans 	/*
93283eab616SBruce Evans 	 * XXX
9338e56e561SJohn Dyson 	 * We stop zeroing pages when there are sufficent prezeroed pages.
9348e56e561SJohn Dyson 	 * This threshold isn't really needed, except we want to
9358e56e561SJohn Dyson 	 * bypass unneeded calls to vm_page_list_find, and the
9368e56e561SJohn Dyson 	 * associated cache flush and latency.  The pre-zero will
9378e56e561SJohn Dyson 	 * still be called when there are significantly more
9388e56e561SJohn Dyson 	 * non-prezeroed pages than zeroed pages.  The threshold
9398e56e561SJohn Dyson 	 * of half the number of reserved pages is arbitrary, but
9408e56e561SJohn Dyson 	 * approximately the right amount.  Eventually, we should
9418e56e561SJohn Dyson 	 * perhaps interrupt the zero operation when a process
9428e56e561SJohn Dyson 	 * is found to be ready to run.
9438e56e561SJohn Dyson 	 */
94483eab616SBruce Evans 	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
94583eab616SBruce Evans 		return (0);
9469a3b3e8bSPeter Wemm #ifdef SMP
9479a3b3e8bSPeter Wemm 	get_mplock();
9489a3b3e8bSPeter Wemm #endif
94983eab616SBruce Evans 	s = splvm();
95083eab616SBruce Evans 	enable_intr();
95183eab616SBruce Evans 	m = vm_page_list_find(PQ_FREE, free_rover);
95283eab616SBruce Evans 	if (m != NULL) {
9535070c7f8SJohn Dyson 		--(*vm_page_queues[m->queue].lcnt);
9545070c7f8SJohn Dyson 		TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
95595f67de2STor Egge 		m->queue = PQ_NONE;
95683eab616SBruce Evans 		splx(s);
9579a3b3e8bSPeter Wemm #ifdef SMP
9589a3b3e8bSPeter Wemm 		rel_mplock();
9599a3b3e8bSPeter Wemm #endif
960a29b63cbSJohn Dyson 		pmap_zero_page(VM_PAGE_TO_PHYS(m));
9619a3b3e8bSPeter Wemm #ifdef SMP
9629a3b3e8bSPeter Wemm 		get_mplock();
9639a3b3e8bSPeter Wemm #endif
96483eab616SBruce Evans 		(void)splvm();
9655070c7f8SJohn Dyson 		m->queue = PQ_ZERO + m->pc;
9665070c7f8SJohn Dyson 		++(*vm_page_queues[m->queue].lcnt);
9675070c7f8SJohn Dyson 		TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq);
9685070c7f8SJohn Dyson 		free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
969a316d390SJohn Dyson 		++vm_page_zero_count;
970a29b63cbSJohn Dyson 	}
97183eab616SBruce Evans 	splx(s);
97283eab616SBruce Evans 	disable_intr();
9739a3b3e8bSPeter Wemm #ifdef SMP
9749a3b3e8bSPeter Wemm 	rel_mplock();
9759a3b3e8bSPeter Wemm #endif
97683eab616SBruce Evans 	return (1);
977a29b63cbSJohn Dyson }
978e0b78e19SJoerg Wunsch 
979e0b78e19SJoerg Wunsch /*
98057d7d7b3SJustin T. Gibbs  * Software interrupt handler for queued VM system processing.
98157d7d7b3SJustin T. Gibbs  */
98257d7d7b3SJustin T. Gibbs void
98357d7d7b3SJustin T. Gibbs swi_vm()
98457d7d7b3SJustin T. Gibbs {
98557d7d7b3SJustin T. Gibbs 	if (busdma_swi_pending != 0)
98657d7d7b3SJustin T. Gibbs 		busdma_swi();
98757d7d7b3SJustin T. Gibbs }
98857d7d7b3SJustin T. Gibbs 
98957d7d7b3SJustin T. Gibbs /*
990cae6f73aSJoerg Wunsch  * Tell whether this address is in some physical memory region.
991e0b78e19SJoerg Wunsch  * Currently used by the kernel coredump code in order to avoid
992e0b78e19SJoerg Wunsch  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
993e0b78e19SJoerg Wunsch  * or other unpredictable behaviour.
994e0b78e19SJoerg Wunsch  */
995e0b78e19SJoerg Wunsch 
996e0b78e19SJoerg Wunsch #include "isa.h"
997e0b78e19SJoerg Wunsch 
998e0b78e19SJoerg Wunsch int
999cae6f73aSJoerg Wunsch is_physical_memory(addr)
1000e0b78e19SJoerg Wunsch 	vm_offset_t addr;
1001e0b78e19SJoerg Wunsch {
1002e0b78e19SJoerg Wunsch 
1003e0b78e19SJoerg Wunsch #if NISA > 0
1004e0b78e19SJoerg Wunsch 	/* The ISA ``memory hole''. */
1005e0b78e19SJoerg Wunsch 	if (addr >= 0xa0000 && addr < 0x100000)
1006cae6f73aSJoerg Wunsch 		return 0;
1007e0b78e19SJoerg Wunsch #endif
1008e0b78e19SJoerg Wunsch 
1009e0b78e19SJoerg Wunsch 	/*
1010e0b78e19SJoerg Wunsch 	 * stuff other tests for known memory-mapped devices (PCI?)
1011e0b78e19SJoerg Wunsch 	 * here
1012e0b78e19SJoerg Wunsch 	 */
1013e0b78e19SJoerg Wunsch 
1014cae6f73aSJoerg Wunsch 	return 1;
1015e0b78e19SJoerg Wunsch }
1016