xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 61324207f1850e4dd80672beed9b0a92856ff196)
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$
4161324207SKATO Takenori  *	$Id: vm_machdep.c,v 1.103 1998/03/14 03:02:15 tegge 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"
4861324207SKATO Takenori #ifdef PC98
4961324207SKATO Takenori #include "opt_pc98.h"
5061324207SKATO Takenori #endif
518890984dSGarrett Wollman 
5226f9a767SRodney W. Grimes #include <sys/param.h>
5326f9a767SRodney W. Grimes #include <sys/systm.h>
5426f9a767SRodney W. Grimes #include <sys/proc.h>
5526f9a767SRodney W. Grimes #include <sys/malloc.h>
5626f9a767SRodney W. Grimes #include <sys/buf.h>
5726f9a767SRodney W. Grimes #include <sys/vnode.h>
58efeaf95aSDavid Greenman #include <sys/vmmeter.h>
5966095752SJohn Dyson #include <sys/kernel.h>
6066095752SJohn Dyson #include <sys/sysctl.h>
615b81b6b3SRodney W. Grimes 
622320728fSRodney W. Grimes #include <machine/clock.h>
63a2a1c95cSPeter Wemm #include <machine/cpu.h>
641f8745a9SPeter Wemm #include <machine/md_var.h>
658fa40736SBruce Evans #ifdef SMP
668fa40736SBruce Evans #include <machine/smp.h>
678fa40736SBruce Evans #endif
685b81b6b3SRodney W. Grimes 
6926f9a767SRodney W. Grimes #include <vm/vm.h>
70efeaf95aSDavid Greenman #include <vm/vm_param.h>
71efeaf95aSDavid Greenman #include <vm/vm_prot.h>
72996c772fSJohn Dyson #include <sys/lock.h>
7326f9a767SRodney W. Grimes #include <vm/vm_kern.h>
7424a1cce3SDavid Greenman #include <vm/vm_page.h>
75efeaf95aSDavid Greenman #include <vm/vm_map.h>
76efeaf95aSDavid Greenman #include <vm/vm_extern.h>
77efeaf95aSDavid Greenman 
78efeaf95aSDavid Greenman #include <sys/user.h>
795b81b6b3SRodney W. Grimes 
80e30f0011SSatoshi Asami #ifdef PC98
81e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h>
82e30f0011SSatoshi Asami #else
832320728fSRodney W. Grimes #include <i386/isa/isa.h>
84e30f0011SSatoshi Asami #endif
852320728fSRodney W. Grimes 
869aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
8787b91157SPoul-Henning Kamp static vm_offset_t
886c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
8987b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
906c146e28SBruce Evans 					int now));
9187b91157SPoul-Henning Kamp static vm_offset_t
926c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
9387b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
946c146e28SBruce Evans 
9587b6de2bSPoul-Henning Kamp static volatile int	kvasfreecnt;
96ae92ea44SDavid Greenman 
97d5e26ef0SDavid Greenman caddr_t		bouncememory;
9887b91157SPoul-Henning Kamp static int	bpwait;
9987b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
10087b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
101d5e26ef0SDavid Greenman 
102ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
10387b91157SPoul-Henning Kamp static int		bounceallocarraysize;
10487b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
10587b91157SPoul-Henning Kamp static int		bouncefree;
106d5e26ef0SDavid Greenman 
107e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA)
108e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096)			/* 15MB boundary */
109e30f0011SSatoshi Asami #else
110d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
111e30f0011SSatoshi Asami #endif
112ae92ea44SDavid Greenman #define MAXBKVA 1024
113f8845af0SPoul-Henning Kamp int		maxbkva = MAXBKVA*PAGE_SIZE;
114d5e26ef0SDavid Greenman 
115d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
11687b91157SPoul-Henning Kamp static struct kvasfree {
117d5e26ef0SDavid Greenman 	vm_offset_t addr;
118d5e26ef0SDavid Greenman 	vm_offset_t size;
119d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
120d5e26ef0SDavid Greenman 
121d5e26ef0SDavid Greenman /*
122d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
123d5e26ef0SDavid Greenman  * (only 1 inplemented now)
124d5e26ef0SDavid Greenman  */
12587b91157SPoul-Henning Kamp static vm_offset_t
126d5e26ef0SDavid Greenman vm_bounce_page_find(count)
127d5e26ef0SDavid Greenman 	int count;
128d5e26ef0SDavid Greenman {
129d5e26ef0SDavid Greenman 	int bit;
130d5e26ef0SDavid Greenman 	int s,i;
131d5e26ef0SDavid Greenman 
132d5e26ef0SDavid Greenman 	if (count != 1)
133d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
134d5e26ef0SDavid Greenman 
135d5e26ef0SDavid Greenman 	s = splbio();
136d5e26ef0SDavid Greenman retry:
137d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
138d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1393fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1403fb3086eSPoul-Henning Kamp 			if (bit) {
141d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
142d5e26ef0SDavid Greenman 				bouncefree -= count;
143d5e26ef0SDavid Greenman 				splx(s);
144ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
145d5e26ef0SDavid Greenman 			}
146d5e26ef0SDavid Greenman 		}
147d5e26ef0SDavid Greenman 	}
148ed7fcbd0SDavid Greenman 	bpwait = 1;
149d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
150d5e26ef0SDavid Greenman 	goto retry;
151d5e26ef0SDavid Greenman }
152d5e26ef0SDavid Greenman 
15387b91157SPoul-Henning Kamp static void
1541561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1551561d038SDavid Greenman 	vm_offset_t addr;
1561561d038SDavid Greenman 	vm_offset_t size;
1571561d038SDavid Greenman 	int now;
1581561d038SDavid Greenman {
1591561d038SDavid Greenman 	int s = splbio();
1601561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
161ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
162ae92ea44SDavid Greenman 	++kvasfreecnt;
1630e195446SDavid Greenman 	if( now) {
1640e195446SDavid Greenman 		/*
1650e195446SDavid Greenman 		 * this will do wakeups
1660e195446SDavid Greenman 		 */
1671561d038SDavid Greenman 		vm_bounce_kva(0,0);
1680e195446SDavid Greenman 	} else {
1690e195446SDavid Greenman 		if (bmwait) {
1700e195446SDavid Greenman 		/*
1710e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1720e195446SDavid Greenman 		 */
1731561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1740e195446SDavid Greenman 			bmwait = 0;
1750e195446SDavid Greenman 		}
1760e195446SDavid Greenman 	}
1771561d038SDavid Greenman 	splx(s);
1781561d038SDavid Greenman }
1791561d038SDavid Greenman 
180d5e26ef0SDavid Greenman /*
181d5e26ef0SDavid Greenman  * free count bounce buffer pages
182d5e26ef0SDavid Greenman  */
18387b91157SPoul-Henning Kamp static void
184d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
185d5e26ef0SDavid Greenman 	vm_offset_t pa;
186d5e26ef0SDavid Greenman 	int count;
187d5e26ef0SDavid Greenman {
188d5e26ef0SDavid Greenman 	int allocindex;
189d5e26ef0SDavid Greenman 	int index;
190d5e26ef0SDavid Greenman 	int bit;
191d5e26ef0SDavid Greenman 
192d5e26ef0SDavid Greenman 	if (count != 1)
193edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
194d5e26ef0SDavid Greenman 
195ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
196ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
197ae92ea44SDavid Greenman 			break;
198ae92ea44SDavid Greenman 	}
199d5e26ef0SDavid Greenman 
200ae92ea44SDavid Greenman 	if( index == bouncepages)
201ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
202d5e26ef0SDavid Greenman 
203ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
204ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
205d5e26ef0SDavid Greenman 
206d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
207d5e26ef0SDavid Greenman 
208d5e26ef0SDavid Greenman 	bouncefree += count;
209ed7fcbd0SDavid Greenman 	if (bpwait) {
210ed7fcbd0SDavid Greenman 		bpwait = 0;
211d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
212d5e26ef0SDavid Greenman 	}
213ed7fcbd0SDavid Greenman }
214d5e26ef0SDavid Greenman 
215d5e26ef0SDavid Greenman /*
216d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
217d5e26ef0SDavid Greenman  */
21887b91157SPoul-Henning Kamp static vm_offset_t
219ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
220ae92ea44SDavid Greenman 	int size;
2211561d038SDavid Greenman 	int waitok;
222d5e26ef0SDavid Greenman {
223d5e26ef0SDavid Greenman 	int i;
2246b4ac811SDavid Greenman 	vm_offset_t kva = 0;
225c564966bSDavid Greenman 	vm_offset_t off;
226d5e26ef0SDavid Greenman 	int s = splbio();
227d5e26ef0SDavid Greenman more:
228ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
229d5e26ef0SDavid Greenman 		bmfreeing = 1;
230ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
231f8845af0SPoul-Henning Kamp 			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
232c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
233c564966bSDavid Greenman 			}
2341561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
235d5e26ef0SDavid Greenman 				kvaf[i].size);
236d5e26ef0SDavid Greenman 		}
237d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
238d5e26ef0SDavid Greenman 		bmfreeing = 0;
239ae92ea44SDavid Greenman 		if( bmwait) {
240ae92ea44SDavid Greenman 			bmwait = 0;
241ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
242ae92ea44SDavid Greenman 		}
243d5e26ef0SDavid Greenman 	}
244d5e26ef0SDavid Greenman 
2451561d038SDavid Greenman 	if( size == 0) {
2461561d038SDavid Greenman 		splx(s);
2476ab46d52SBruce Evans 		return 0;
2481561d038SDavid Greenman 	}
2491561d038SDavid Greenman 
250ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2511561d038SDavid Greenman 		if( !waitok) {
2521561d038SDavid Greenman 			splx(s);
2536ab46d52SBruce Evans 			return 0;
2541561d038SDavid Greenman 		}
255d5e26ef0SDavid Greenman 		bmwait = 1;
2561561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
257d5e26ef0SDavid Greenman 		goto more;
258d5e26ef0SDavid Greenman 	}
259d5e26ef0SDavid Greenman 	splx(s);
260d5e26ef0SDavid Greenman 	return kva;
261d5e26ef0SDavid Greenman }
262d5e26ef0SDavid Greenman 
263d5e26ef0SDavid Greenman /*
264ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2650e195446SDavid Greenman  */
2660e195446SDavid Greenman vm_offset_t
2670e195446SDavid Greenman vm_bounce_kva_alloc(count)
2680e195446SDavid Greenman int count;
2690e195446SDavid Greenman {
2700e195446SDavid Greenman 	int i;
2710e195446SDavid Greenman 	vm_offset_t kva;
2720e195446SDavid Greenman 	vm_offset_t pa;
2730e195446SDavid Greenman 	if( bouncepages == 0) {
274f8845af0SPoul-Henning Kamp 		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
2750e195446SDavid Greenman 		return kva;
2760e195446SDavid Greenman 	}
277f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
2780e195446SDavid Greenman 	for(i=0;i<count;i++) {
2790e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
280f8845af0SPoul-Henning Kamp 		pmap_kenter(kva + i * PAGE_SIZE, pa);
2810e195446SDavid Greenman 	}
2820e195446SDavid Greenman 	return kva;
2830e195446SDavid Greenman }
2840e195446SDavid Greenman 
2850e195446SDavid Greenman /*
2860e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2870e195446SDavid Greenman  */
2880e195446SDavid Greenman void
2890e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2900e195446SDavid Greenman 	vm_offset_t kva;
2910e195446SDavid Greenman 	int count;
2920e195446SDavid Greenman {
2930e195446SDavid Greenman 	int i;
2940e195446SDavid Greenman 	vm_offset_t pa;
2950e195446SDavid Greenman 	if( bouncepages == 0) {
2960e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
2970e195446SDavid Greenman 		return;
2980e195446SDavid Greenman 	}
2990e195446SDavid Greenman 	for(i = 0; i < count; i++) {
300f8845af0SPoul-Henning Kamp 		pa = pmap_kextract(kva + i * PAGE_SIZE);
3010e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
3020e195446SDavid Greenman 	}
303f8845af0SPoul-Henning Kamp 	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
3040e195446SDavid Greenman }
3050e195446SDavid Greenman 
3060e195446SDavid Greenman /*
307d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
308d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
309d5e26ef0SDavid Greenman  */
310d5e26ef0SDavid Greenman void
311d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
312d5e26ef0SDavid Greenman 	struct buf *bp;
313d5e26ef0SDavid Greenman {
314d5e26ef0SDavid Greenman 	int countvmpg;
315d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
316d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
317d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
318d5e26ef0SDavid Greenman 	vm_offset_t pa;
319d5e26ef0SDavid Greenman 	int dobounceflag = 0;
320d5e26ef0SDavid Greenman 	int i;
321d5e26ef0SDavid Greenman 
322d5e26ef0SDavid Greenman 	if (bouncepages == 0)
323d5e26ef0SDavid Greenman 		return;
324d5e26ef0SDavid Greenman 
325ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
326ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
327ae92ea44SDavid Greenman 		return;
3281561d038SDavid Greenman 	}
3291561d038SDavid Greenman 
330ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3313fb3086eSPoul-Henning Kamp 		printf(
3323fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
333ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
334ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
335ae92ea44SDavid Greenman 	}
336ae92ea44SDavid Greenman 
337ae92ea44SDavid Greenman /*
338ae92ea44SDavid Greenman  *  This is not really necessary
339ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
340ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
341ae92ea44SDavid Greenman  *	}
342ae92ea44SDavid Greenman  */
343ae92ea44SDavid Greenman 
344ae92ea44SDavid Greenman 
34526f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
34626f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
347d5e26ef0SDavid Greenman 
348bd7e5f99SJohn Dyson 	vapstart = trunc_page(vastart);
349bd7e5f99SJohn Dyson 	vapend = round_page(vaend);
350f8845af0SPoul-Henning Kamp 	countvmpg = (vapend - vapstart) / PAGE_SIZE;
351d5e26ef0SDavid Greenman 
352d5e26ef0SDavid Greenman /*
353d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
354d5e26ef0SDavid Greenman  */
355d5e26ef0SDavid Greenman 	va = vapstart;
356d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
357ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
358d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
359d5e26ef0SDavid Greenman 			++dobounceflag;
3600d94caffSDavid Greenman 		if( pa == 0)
3610d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
362f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
363d5e26ef0SDavid Greenman 	}
364d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
365d5e26ef0SDavid Greenman 		return;
366d5e26ef0SDavid Greenman 
367d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
368d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
369d5e26ef0SDavid Greenman 
370d5e26ef0SDavid Greenman /*
371d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
372d5e26ef0SDavid Greenman  */
373f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
374ae92ea44SDavid Greenman #if 0
375ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
376ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
377ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
378ae92ea44SDavid Greenman #endif
379d5e26ef0SDavid Greenman 	va = vapstart;
380d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
381ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
382d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
383d5e26ef0SDavid Greenman 			/*
384d5e26ef0SDavid Greenman 			 * allocate a replacement page
385d5e26ef0SDavid Greenman 			 */
386d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
387f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
388ae92ea44SDavid Greenman #if 0
389ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
390ae92ea44SDavid Greenman #endif
391d5e26ef0SDavid Greenman 			/*
392d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
393d5e26ef0SDavid Greenman 			 */
3941561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
395f8845af0SPoul-Henning Kamp 				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
3961561d038SDavid Greenman 			}
397d5e26ef0SDavid Greenman 		} else {
398d5e26ef0SDavid Greenman 			/*
399d5e26ef0SDavid Greenman 			 * use original page
400d5e26ef0SDavid Greenman 			 */
401f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), pa);
402d5e26ef0SDavid Greenman 		}
403f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
404d5e26ef0SDavid Greenman 	}
405d5e26ef0SDavid Greenman 
406d5e26ef0SDavid Greenman /*
407d5e26ef0SDavid Greenman  * flag the buffer as being bounced
408d5e26ef0SDavid Greenman  */
409d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
410d5e26ef0SDavid Greenman /*
411d5e26ef0SDavid Greenman  * save the original buffer kva
412d5e26ef0SDavid Greenman  */
41326f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
414d5e26ef0SDavid Greenman /*
415d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
416d5e26ef0SDavid Greenman  */
41726f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
418f8845af0SPoul-Henning Kamp 				((vm_offset_t) bp->b_savekva & PAGE_MASK));
419ae92ea44SDavid Greenman #if 0
420ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
421ae92ea44SDavid Greenman #endif
422d5e26ef0SDavid Greenman 	return;
423d5e26ef0SDavid Greenman }
424d5e26ef0SDavid Greenman 
425d5e26ef0SDavid Greenman /*
426d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
427d5e26ef0SDavid Greenman  */
428d5e26ef0SDavid Greenman void
429d5e26ef0SDavid Greenman vm_bounce_free(bp)
430d5e26ef0SDavid Greenman 	struct buf *bp;
431d5e26ef0SDavid Greenman {
432d5e26ef0SDavid Greenman 	int i;
433ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
434d5e26ef0SDavid Greenman 
435d5e26ef0SDavid Greenman /*
436d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
437d5e26ef0SDavid Greenman  */
438d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
439d5e26ef0SDavid Greenman 		return;
440d5e26ef0SDavid Greenman 
441ae92ea44SDavid Greenman /*
442ae92ea44SDavid Greenman  *  This check is not necessary
443ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
444ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
445ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
446ae92ea44SDavid Greenman  *	}
447ae92ea44SDavid Greenman  */
448ae92ea44SDavid Greenman 
449d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
45026f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
451ae92ea44SDavid Greenman /*
452ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
453ae92ea44SDavid Greenman */
454d5e26ef0SDavid Greenman 
455d5e26ef0SDavid Greenman /*
456d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
457d5e26ef0SDavid Greenman  */
458ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
459d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
460d5e26ef0SDavid Greenman 		vm_offset_t copycount;
461d5e26ef0SDavid Greenman 
462bd7e5f99SJohn Dyson 		copycount = round_page(bouncekva + 1) - bouncekva;
463bd7e5f99SJohn Dyson 		mybouncepa = pmap_kextract(trunc_page(bouncekva));
464d5e26ef0SDavid Greenman 
465d5e26ef0SDavid Greenman /*
466d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
467d5e26ef0SDavid Greenman  */
468bd7e5f99SJohn Dyson 		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
469ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
470ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
471ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
472d5e26ef0SDavid Greenman /*
473d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
474d5e26ef0SDavid Greenman  */
475d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
476ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
477d5e26ef0SDavid Greenman /*
478d5e26ef0SDavid Greenman  * free the bounce allocation
479d5e26ef0SDavid Greenman  */
480ae92ea44SDavid Greenman 
481ae92ea44SDavid Greenman /*
482ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
483ae92ea44SDavid Greenman */
484ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
485d5e26ef0SDavid Greenman 		}
486d5e26ef0SDavid Greenman 
487d5e26ef0SDavid Greenman 		origkva += copycount;
488d5e26ef0SDavid Greenman 		bouncekva += copycount;
489ae92ea44SDavid Greenman 		i += copycount;
490d5e26ef0SDavid Greenman 	}
491d5e26ef0SDavid Greenman 
492d5e26ef0SDavid Greenman /*
493ae92ea44SDavid Greenman 	printf("\n");
494ae92ea44SDavid Greenman */
495ae92ea44SDavid Greenman /*
496d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
497d5e26ef0SDavid Greenman  */
498ae92ea44SDavid Greenman 
499bd7e5f99SJohn Dyson 	bouncekva= trunc_page((vm_offset_t) bp->b_data);
500bd7e5f99SJohn Dyson 	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
501ae92ea44SDavid Greenman 
502ae92ea44SDavid Greenman /*
503f8845af0SPoul-Henning Kamp 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
504ae92ea44SDavid Greenman */
505ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
50626f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
507d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
508d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
509d5e26ef0SDavid Greenman 
510d5e26ef0SDavid Greenman 	return;
511d5e26ef0SDavid Greenman }
512d5e26ef0SDavid Greenman 
513ae92ea44SDavid Greenman 
5145b81b6b3SRodney W. Grimes /*
5151561d038SDavid Greenman  * init the bounce buffer system
5161561d038SDavid Greenman  */
5171561d038SDavid Greenman void
5181561d038SDavid Greenman vm_bounce_init()
5191561d038SDavid Greenman {
520ae92ea44SDavid Greenman 	int i;
5211561d038SDavid Greenman 
5221561d038SDavid Greenman 	kvasfreecnt = 0;
5231561d038SDavid Greenman 
5241561d038SDavid Greenman 	if (bouncepages == 0)
5251561d038SDavid Greenman 		return;
5261561d038SDavid Greenman 
5271561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5281561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5291561d038SDavid Greenman 
5301561d038SDavid Greenman 	if (!bounceallocarray)
531edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5321561d038SDavid Greenman 
533ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
534ae92ea44SDavid Greenman 	if (!bouncepa)
535edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5361561d038SDavid Greenman 
5370d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5380d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5390d94caffSDavid Greenman 	}
5400d94caffSDavid Greenman 
541ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
542ae92ea44SDavid Greenman 		vm_offset_t pa;
5430621c31aSJohn Dyson 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) {
5440621c31aSJohn Dyson 			printf("vm_bounce_init: bounce memory out of range -- bounce disabled\n");
5450621c31aSJohn Dyson 			free(bounceallocarray, M_TEMP);
5460621c31aSJohn Dyson 			bounceallocarray = NULL;
5470621c31aSJohn Dyson 			free(bouncepa, M_TEMP);
5480621c31aSJohn Dyson 			bouncepa = NULL;
5490621c31aSJohn Dyson 			bouncepages = 0;
5500621c31aSJohn Dyson 			break;
5510621c31aSJohn Dyson 		}
552ae92ea44SDavid Greenman 		if( pa == 0)
553ae92ea44SDavid Greenman 			panic("bounce memory not resident");
554ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5550d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
556ae92ea44SDavid Greenman 	}
5571561d038SDavid Greenman 	bouncefree = bouncepages;
5581561d038SDavid Greenman 
5591561d038SDavid Greenman }
5609aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5616c146e28SBruce Evans 
562a4f7a4c9SDavid Greenman /*
563a4f7a4c9SDavid Greenman  * quick version of vm_fault
564a4f7a4c9SDavid Greenman  */
565dca51299SJohn Dyson void
566a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5676c146e28SBruce Evans 	caddr_t v;
568a4f7a4c9SDavid Greenman 	int prot;
569a4f7a4c9SDavid Greenman {
5701d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5716c146e28SBruce Evans 		subyte(v, fubyte(v));
572a4f7a4c9SDavid Greenman 	else
5736c146e28SBruce Evans 		fubyte(v);
574a4f7a4c9SDavid Greenman }
575a4f7a4c9SDavid Greenman 
5761561d038SDavid Greenman /*
5775b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
578a2a1c95cSPeter Wemm  * Copy and update the pcb, set up the stack so that the child
579a2a1c95cSPeter Wemm  * ready to run and return to user mode.
5805b81b6b3SRodney W. Grimes  */
581a2a1c95cSPeter Wemm void
5825b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5835b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5845b81b6b3SRodney W. Grimes {
585f83a01b6SDavid Greenman 	struct pcb *pcb2 = &p2->p_addr->u_pcb;
5865b81b6b3SRodney W. Grimes 
5879f449d2aSBruce Evans #if NNPX > 0
5881f8745a9SPeter Wemm 	/* Ensure that p1's pcb is up to date. */
5891f8745a9SPeter Wemm 	if (npxproc == p1)
5901f8745a9SPeter Wemm 		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
5919f449d2aSBruce Evans #endif
5921f8745a9SPeter Wemm 
5931f8745a9SPeter Wemm 	/* Copy p1's pcb. */
5941f8745a9SPeter Wemm 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
595a2a1c95cSPeter Wemm 
596a2a1c95cSPeter Wemm 	/*
597a2a1c95cSPeter Wemm 	 * Create a new fresh stack for the new process.
5981f8745a9SPeter Wemm 	 * Copy the trap frame for the return to user mode as if from a
5991f8745a9SPeter Wemm 	 * syscall.  This copies the user mode register values.
600a2a1c95cSPeter Wemm 	 */
6011f8745a9SPeter Wemm 	p2->p_md.md_regs = (struct trapframe *)
6021f8745a9SPeter Wemm 			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
6031f8745a9SPeter Wemm 	*p2->p_md.md_regs = *p1->p_md.md_regs;
604a2a1c95cSPeter Wemm 
605a2a1c95cSPeter Wemm 	/*
606a2a1c95cSPeter Wemm 	 * Set registers for trampoline to user mode.  Leave space for the
607a2a1c95cSPeter Wemm 	 * return address on stack.  These are the kernel mode register values.
608a2a1c95cSPeter Wemm 	 */
6091f8745a9SPeter Wemm 	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
6101f8745a9SPeter Wemm 	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
611a2a1c95cSPeter Wemm 	pcb2->pcb_esi = (int)fork_return;
6121f8745a9SPeter Wemm 	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
613a2a1c95cSPeter Wemm 	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
6141f8745a9SPeter Wemm 	pcb2->pcb_ebx = (int)p2;
6151f8745a9SPeter Wemm 	pcb2->pcb_eip = (int)fork_trampoline;
6161f8745a9SPeter Wemm 	/*
6171f8745a9SPeter Wemm 	 * pcb2->pcb_ldt:	duplicated below, if necessary.
6181f8745a9SPeter Wemm 	 * pcb2->pcb_ldt_len:	cloned above.
6191f8745a9SPeter Wemm 	 * pcb2->pcb_savefpu:	cloned above.
6201f8745a9SPeter Wemm 	 * pcb2->pcb_flags:	cloned above (always 0 here?).
6211f8745a9SPeter Wemm 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
6221f8745a9SPeter Wemm 	 */
6235b81b6b3SRodney W. Grimes 
62448a09cf2SJohn Dyson #ifdef VM86
62548a09cf2SJohn Dyson 	/*
62648a09cf2SJohn Dyson 	 * XXX don't copy the i/o pages.  this should probably be fixed.
62748a09cf2SJohn Dyson 	 */
62848a09cf2SJohn Dyson 	pcb2->pcb_ext = 0;
62948a09cf2SJohn Dyson #endif
63048a09cf2SJohn Dyson 
6318c39a127SStefan Eßer #ifdef USER_LDT
6328c39a127SStefan Eßer         /* Copy the LDT, if necessary. */
633a2a1c95cSPeter Wemm         if (pcb2->pcb_ldt != 0) {
6348c39a127SStefan Eßer                 union descriptor *new_ldt;
635a2a1c95cSPeter Wemm                 size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
6368c39a127SStefan Eßer 
6378c39a127SStefan Eßer                 new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
638a2a1c95cSPeter Wemm                 bcopy(pcb2->pcb_ldt, new_ldt, len);
639a2a1c95cSPeter Wemm                 pcb2->pcb_ldt = (caddr_t)new_ldt;
6408c39a127SStefan Eßer         }
6418c39a127SStefan Eßer #endif
6428c39a127SStefan Eßer 
643a2a1c95cSPeter Wemm 	/*
644a2a1c95cSPeter Wemm 	 * Now, cpu_switch() can schedule the new process.
645a2a1c95cSPeter Wemm 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
646a2a1c95cSPeter Wemm 	 * containing the return address when exiting cpu_switch.
647a2a1c95cSPeter Wemm 	 * This will normally be to proc_trampoline(), which will have
648a2a1c95cSPeter Wemm 	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
649a2a1c95cSPeter Wemm 	 * will set up a stack to call fork_return(p, frame); to complete
650a2a1c95cSPeter Wemm 	 * the return to user-mode.
651a2a1c95cSPeter Wemm 	 */
652a2a1c95cSPeter Wemm }
653a2a1c95cSPeter Wemm 
654a2a1c95cSPeter Wemm /*
655a2a1c95cSPeter Wemm  * Intercept the return address from a freshly forked process that has NOT
656a2a1c95cSPeter Wemm  * been scheduled yet.
657a2a1c95cSPeter Wemm  *
658a2a1c95cSPeter Wemm  * This is needed to make kernel threads stay in kernel mode.
659a2a1c95cSPeter Wemm  */
660a2a1c95cSPeter Wemm void
661a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg)
662a2a1c95cSPeter Wemm 	struct proc *p;
663a2a1c95cSPeter Wemm 	void (*func) __P((void *));
664a2a1c95cSPeter Wemm 	void *arg;
665a2a1c95cSPeter Wemm {
666a2a1c95cSPeter Wemm 	/*
667a2a1c95cSPeter Wemm 	 * Note that the trap frame follows the args, so the function
668a2a1c95cSPeter Wemm 	 * is really called like this:  func(arg, frame);
669a2a1c95cSPeter Wemm 	 */
670a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
671a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
6725b81b6b3SRodney W. Grimes }
6735b81b6b3SRodney W. Grimes 
6747c2b54e8SNate Williams void
6755b81b6b3SRodney W. Grimes cpu_exit(p)
6765b81b6b3SRodney W. Grimes 	register struct proc *p;
6775b81b6b3SRodney W. Grimes {
67848a09cf2SJohn Dyson #if defined(USER_LDT) || defined(VM86)
67948a09cf2SJohn Dyson 	struct pcb *pcb = &p->p_addr->u_pcb;
6800dbf6d73SJordan K. Hubbard #endif
6815b81b6b3SRodney W. Grimes 
682960173b9SRodney W. Grimes #if NNPX > 0
6835b81b6b3SRodney W. Grimes 	npxexit(p);
684960173b9SRodney W. Grimes #endif	/* NNPX */
68548a09cf2SJohn Dyson #ifdef VM86
68648a09cf2SJohn Dyson 	if (pcb->pcb_ext != 0) {
68748a09cf2SJohn Dyson 	        /*
68848a09cf2SJohn Dyson 		 * XXX do we need to move the TSS off the allocated pages
68948a09cf2SJohn Dyson 		 * before freeing them?  (not done here)
69048a09cf2SJohn Dyson 		 */
69148a09cf2SJohn Dyson 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
69248a09cf2SJohn Dyson 		    ctob(IOPAGES + 1));
69348a09cf2SJohn Dyson 		pcb->pcb_ext = 0;
69448a09cf2SJohn Dyson 	}
69548a09cf2SJohn Dyson #endif
6960dbf6d73SJordan K. Hubbard #ifdef USER_LDT
6970dbf6d73SJordan K. Hubbard 	if (pcb->pcb_ldt != 0) {
6980dbf6d73SJordan K. Hubbard 		if (pcb == curpcb)
6990dbf6d73SJordan K. Hubbard 			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
7000dbf6d73SJordan K. Hubbard 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
7010dbf6d73SJordan K. Hubbard 			pcb->pcb_ldt_len * sizeof(union descriptor));
7020dbf6d73SJordan K. Hubbard 		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
7030dbf6d73SJordan K. Hubbard 	}
7040dbf6d73SJordan K. Hubbard #endif
7051a051896SBruce Evans 	cnt.v_swtch++;
7061a051896SBruce Evans 	cpu_switch(p);
7077c2b54e8SNate Williams 	panic("cpu_exit");
7085b81b6b3SRodney W. Grimes }
7095b81b6b3SRodney W. Grimes 
710381fe1aaSGarrett Wollman void
711bd7e5f99SJohn Dyson cpu_wait(p)
712bd7e5f99SJohn Dyson 	struct proc *p;
713bd7e5f99SJohn Dyson {
7145b81b6b3SRodney W. Grimes 	/* drop per-process resources */
715675878e7SJohn Dyson 	pmap_dispose_proc(p);
7162d8acc0fSJohn Dyson 
7172d8acc0fSJohn Dyson 	/* and clean-out the vmspace */
7187f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
7195b81b6b3SRodney W. Grimes }
7205b81b6b3SRodney W. Grimes 
7215b81b6b3SRodney W. Grimes /*
72226f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
72326f9a767SRodney W. Grimes  */
72426f9a767SRodney W. Grimes int
72526f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
72626f9a767SRodney W. Grimes 	struct proc *p;
72726f9a767SRodney W. Grimes 	struct vnode *vp;
72826f9a767SRodney W. Grimes 	struct ucred *cred;
72926f9a767SRodney W. Grimes {
73026f9a767SRodney W. Grimes 
73126f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
73226f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
73326f9a767SRodney W. Grimes 	    p));
73426f9a767SRodney W. Grimes }
73526f9a767SRodney W. Grimes 
73687b6de2bSPoul-Henning Kamp #ifdef notyet
73787b91157SPoul-Henning Kamp static void
7385b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
7395b81b6b3SRodney W. Grimes 	u_short *pte;
7405b81b6b3SRodney W. Grimes 	caddr_t vaddr;
7415b81b6b3SRodney W. Grimes {
7425b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
7435b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
7445b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
7455b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
7465b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
7475b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
7485b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
7495b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
7505b81b6b3SRodney W. Grimes }
75187b6de2bSPoul-Henning Kamp #endif
75226f9a767SRodney W. Grimes 
75326f9a767SRodney W. Grimes /*
7545b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
7555b81b6b3SRodney W. Grimes  */
756aaf08d94SGarrett Wollman u_long
7577f8cb368SDavid Greenman kvtop(void *addr)
7585b81b6b3SRodney W. Grimes {
7595b81b6b3SRodney W. Grimes 	vm_offset_t va;
7605b81b6b3SRodney W. Grimes 
761ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
7625b81b6b3SRodney W. Grimes 	if (va == 0)
7635b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
7647f8cb368SDavid Greenman 	return((int)va);
7655b81b6b3SRodney W. Grimes }
7665b81b6b3SRodney W. Grimes 
7675b81b6b3SRodney W. Grimes /*
768ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
7695b81b6b3SRodney W. Grimes  *
770ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
771ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
772ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
7735b81b6b3SRodney W. Grimes  */
774381fe1aaSGarrett Wollman void
7755b81b6b3SRodney W. Grimes vmapbuf(bp)
7765b81b6b3SRodney W. Grimes 	register struct buf *bp;
7775b81b6b3SRodney W. Grimes {
7780157d6d9SJohn Dyson 	register caddr_t addr, v, kva;
77907658526SPoul-Henning Kamp 	vm_offset_t pa;
7805b81b6b3SRodney W. Grimes 
78126f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7825b81b6b3SRodney W. Grimes 		panic("vmapbuf");
78326f9a767SRodney W. Grimes 
78412936855SDavid Greenman 	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
78526f9a767SRodney W. Grimes 	    addr < bp->b_data + bp->b_bufsize;
7860157d6d9SJohn Dyson 	    addr += PAGE_SIZE, v += PAGE_SIZE) {
78726f9a767SRodney W. Grimes 		/*
788ac269d78SDavid Greenman 		 * Do the vm_fault if needed; do the copy-on-write thing
789ac269d78SDavid Greenman 		 * when reading stuff off device into memory.
79026f9a767SRodney W. Grimes 		 */
79126f9a767SRodney W. Grimes 		vm_fault_quick(addr,
79226f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
7930157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
794641a27e6SJohn Dyson 		if (pa == 0)
795641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
79626f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
7970157d6d9SJohn Dyson 		pmap_kenter((vm_offset_t) v, pa);
79826f9a767SRodney W. Grimes 	}
79926f9a767SRodney W. Grimes 
8000157d6d9SJohn Dyson 	kva = bp->b_saveaddr;
8010157d6d9SJohn Dyson 	bp->b_saveaddr = bp->b_data;
8020157d6d9SJohn Dyson 	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
8035b81b6b3SRodney W. Grimes }
8045b81b6b3SRodney W. Grimes 
8055b81b6b3SRodney W. Grimes /*
8065b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
8075b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
8085b81b6b3SRodney W. Grimes  */
809381fe1aaSGarrett Wollman void
8105b81b6b3SRodney W. Grimes vunmapbuf(bp)
8115b81b6b3SRodney W. Grimes 	register struct buf *bp;
8125b81b6b3SRodney W. Grimes {
813c564966bSDavid Greenman 	register caddr_t addr;
81407658526SPoul-Henning Kamp 	vm_offset_t pa;
8155b81b6b3SRodney W. Grimes 
8165b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
8175b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
818c564966bSDavid Greenman 
81912936855SDavid Greenman 	for (addr = (caddr_t)trunc_page(bp->b_data);
82012936855SDavid Greenman 	    addr < bp->b_data + bp->b_bufsize;
821f8845af0SPoul-Henning Kamp 	    addr += PAGE_SIZE) {
8220157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
8230157d6d9SJohn Dyson 		pmap_kremove((vm_offset_t) addr);
82426f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
82526f9a767SRodney W. Grimes 	}
8260157d6d9SJohn Dyson 
8270157d6d9SJohn Dyson 	bp->b_data = bp->b_saveaddr;
8285b81b6b3SRodney W. Grimes }
8295b81b6b3SRodney W. Grimes 
8305b81b6b3SRodney W. Grimes /*
8315b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
8325b81b6b3SRodney W. Grimes  */
8337f8cb368SDavid Greenman void
834d447dbeeSBruce Evans cpu_reset()
835d447dbeeSBruce Evans {
836d447dbeeSBruce Evans 
83703245f09SKATO Takenori #ifdef PC98
83803245f09SKATO Takenori 	/*
83903245f09SKATO Takenori 	 * Attempt to do a CPU reset via CPU reset port.
84003245f09SKATO Takenori 	 */
841d447dbeeSBruce Evans 	disable_intr();
84203245f09SKATO Takenori 	outb(0x37, 0x0f);		/* SHUT0 = 0. */
84303245f09SKATO Takenori 	outb(0x37, 0x0b);		/* SHUT1 = 0. */
84403245f09SKATO Takenori 	outb(0xf0, 0x00);		/* Reset. */
84503245f09SKATO Takenori #else
8462320728fSRodney W. Grimes 	/*
8472320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
8482320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
8492320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
8502320728fSRodney W. Grimes 	 */
8515eb46edfSDavid Greenman 
85203245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET)
8532320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
8542320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
8552320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
8562320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
8575eb46edfSDavid Greenman #endif
85803245f09SKATO Takenori #endif /* PC98 */
8595b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
860f8845af0SPoul-Henning Kamp 	bzero((caddr_t) PTD, PAGE_SIZE);
8615b81b6b3SRodney W. Grimes 
8625b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
86327e9b35eSJohn Dyson 	invltlb();
8645b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8657f8cb368SDavid Greenman 	while(1);
8665b81b6b3SRodney W. Grimes }
867b9d60b3fSDavid Greenman 
868b9d60b3fSDavid Greenman /*
869b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
87029360eb0SDavid Greenman  *	chunks of SGROWSIZ.
871b9d60b3fSDavid Greenman  */
872b9d60b3fSDavid Greenman int
873b9d60b3fSDavid Greenman grow(p, sp)
874b9d60b3fSDavid Greenman 	struct proc *p;
87526f9a767SRodney W. Grimes 	u_int sp;
876b9d60b3fSDavid Greenman {
877b9d60b3fSDavid Greenman 	unsigned int nss;
878b9d60b3fSDavid Greenman 	caddr_t v;
879b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
880b9d60b3fSDavid Greenman 
881b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
882b9d60b3fSDavid Greenman 	    return (1);
883b9d60b3fSDavid Greenman 
884b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
885b9d60b3fSDavid Greenman 
886b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
887b9d60b3fSDavid Greenman 		return (0);
888b9d60b3fSDavid Greenman 
889b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
89029360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
891b9d60b3fSDavid Greenman 		int grow_amount;
892b9d60b3fSDavid Greenman 		/*
893b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
894b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
895b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
896b9d60b3fSDavid Greenman 		 * is expensive).
897b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
89829360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
899b9d60b3fSDavid Greenman 		 */
90029360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
901b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
90229360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
903b9d60b3fSDavid Greenman 		/*
90429360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
905b9d60b3fSDavid Greenman 		 * just extend to the maximum size
906b9d60b3fSDavid Greenman 		 */
907b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
908b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
909b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
910b9d60b3fSDavid Greenman 		}
91168940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
912bd7e5f99SJohn Dyson 		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
913b9d60b3fSDavid Greenman 			return (0);
914b9d60b3fSDavid Greenman 		}
915b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
916b9d60b3fSDavid Greenman 	}
917b9d60b3fSDavid Greenman 
918b9d60b3fSDavid Greenman 	return (1);
919b9d60b3fSDavid Greenman }
920a29b63cbSJohn Dyson 
9217762bc7bSBruce Evans static int cnt_prezero;
92266095752SJohn Dyson 
92366095752SJohn Dyson SYSCTL_INT(_machdep, OID_AUTO, cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
92466095752SJohn Dyson 
925a29b63cbSJohn Dyson /*
92683eab616SBruce Evans  * Implement the pre-zeroed page mechanism.
92783eab616SBruce Evans  * This routine is called from the idle loop.
928a29b63cbSJohn Dyson  */
929a29b63cbSJohn Dyson int
93083eab616SBruce Evans vm_page_zero_idle()
93183eab616SBruce Evans {
93283eab616SBruce Evans 	static int free_rover;
933a29b63cbSJohn Dyson 	vm_page_t m;
93483eab616SBruce Evans 	int s;
93583eab616SBruce Evans 
93683eab616SBruce Evans 	/*
93783eab616SBruce Evans 	 * XXX
9388e56e561SJohn Dyson 	 * We stop zeroing pages when there are sufficent prezeroed pages.
9398e56e561SJohn Dyson 	 * This threshold isn't really needed, except we want to
9408e56e561SJohn Dyson 	 * bypass unneeded calls to vm_page_list_find, and the
9418e56e561SJohn Dyson 	 * associated cache flush and latency.  The pre-zero will
9428e56e561SJohn Dyson 	 * still be called when there are significantly more
9438e56e561SJohn Dyson 	 * non-prezeroed pages than zeroed pages.  The threshold
9448e56e561SJohn Dyson 	 * of half the number of reserved pages is arbitrary, but
9458e56e561SJohn Dyson 	 * approximately the right amount.  Eventually, we should
9468e56e561SJohn Dyson 	 * perhaps interrupt the zero operation when a process
9478e56e561SJohn Dyson 	 * is found to be ready to run.
9488e56e561SJohn Dyson 	 */
94983eab616SBruce Evans 	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
95083eab616SBruce Evans 		return (0);
9519a3b3e8bSPeter Wemm #ifdef SMP
95266095752SJohn Dyson 	if (try_mplock()) {
9539a3b3e8bSPeter Wemm #endif
95483eab616SBruce Evans 		s = splvm();
955d20d60beSTor Egge 		__asm __volatile("sti" : : : "memory");
95683eab616SBruce Evans 		m = vm_page_list_find(PQ_FREE, free_rover);
95783eab616SBruce Evans 		if (m != NULL) {
9585070c7f8SJohn Dyson 			--(*vm_page_queues[m->queue].lcnt);
9595070c7f8SJohn Dyson 			TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
96095f67de2STor Egge 			m->queue = PQ_NONE;
96183eab616SBruce Evans 			splx(s);
96266095752SJohn Dyson #if 0
9639a3b3e8bSPeter Wemm 			rel_mplock();
9649a3b3e8bSPeter Wemm #endif
965a29b63cbSJohn Dyson 			pmap_zero_page(VM_PAGE_TO_PHYS(m));
9667762bc7bSBruce Evans #if 0
9679a3b3e8bSPeter Wemm 			get_mplock();
9689a3b3e8bSPeter Wemm #endif
96983eab616SBruce Evans 			(void)splvm();
9705070c7f8SJohn Dyson 			m->queue = PQ_ZERO + m->pc;
9715070c7f8SJohn Dyson 			++(*vm_page_queues[m->queue].lcnt);
9727762bc7bSBruce Evans 			TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m,
9737762bc7bSBruce Evans 			    pageq);
9745070c7f8SJohn Dyson 			free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
975a316d390SJohn Dyson 			++vm_page_zero_count;
97666095752SJohn Dyson 			++cnt_prezero;
977a29b63cbSJohn Dyson 		}
97883eab616SBruce Evans 		splx(s);
979d20d60beSTor Egge 		__asm __volatile("cli" : : : "memory");
9809a3b3e8bSPeter Wemm #ifdef SMP
9819a3b3e8bSPeter Wemm 		rel_mplock();
9827762bc7bSBruce Evans #endif
98383eab616SBruce Evans 		return (1);
9847762bc7bSBruce Evans #ifdef SMP
985a29b63cbSJohn Dyson 	}
98666095752SJohn Dyson #endif
98766095752SJohn Dyson 	return (0);
98866095752SJohn Dyson }
989e0b78e19SJoerg Wunsch 
990e0b78e19SJoerg Wunsch /*
99157d7d7b3SJustin T. Gibbs  * Software interrupt handler for queued VM system processing.
99257d7d7b3SJustin T. Gibbs  */
99357d7d7b3SJustin T. Gibbs void
99457d7d7b3SJustin T. Gibbs swi_vm()
99557d7d7b3SJustin T. Gibbs {
99657d7d7b3SJustin T. Gibbs 	if (busdma_swi_pending != 0)
99757d7d7b3SJustin T. Gibbs 		busdma_swi();
99857d7d7b3SJustin T. Gibbs }
99957d7d7b3SJustin T. Gibbs 
100057d7d7b3SJustin T. Gibbs /*
1001cae6f73aSJoerg Wunsch  * Tell whether this address is in some physical memory region.
1002e0b78e19SJoerg Wunsch  * Currently used by the kernel coredump code in order to avoid
1003e0b78e19SJoerg Wunsch  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
1004e0b78e19SJoerg Wunsch  * or other unpredictable behaviour.
1005e0b78e19SJoerg Wunsch  */
1006e0b78e19SJoerg Wunsch 
1007e0b78e19SJoerg Wunsch #include "isa.h"
1008e0b78e19SJoerg Wunsch 
1009e0b78e19SJoerg Wunsch int
1010cae6f73aSJoerg Wunsch is_physical_memory(addr)
1011e0b78e19SJoerg Wunsch 	vm_offset_t addr;
1012e0b78e19SJoerg Wunsch {
1013e0b78e19SJoerg Wunsch 
1014e0b78e19SJoerg Wunsch #if NISA > 0
1015e0b78e19SJoerg Wunsch 	/* The ISA ``memory hole''. */
1016e0b78e19SJoerg Wunsch 	if (addr >= 0xa0000 && addr < 0x100000)
1017cae6f73aSJoerg Wunsch 		return 0;
1018e0b78e19SJoerg Wunsch #endif
1019e0b78e19SJoerg Wunsch 
1020e0b78e19SJoerg Wunsch 	/*
1021e0b78e19SJoerg Wunsch 	 * stuff other tests for known memory-mapped devices (PCI?)
1022e0b78e19SJoerg Wunsch 	 * here
1023e0b78e19SJoerg Wunsch 	 */
1024e0b78e19SJoerg Wunsch 
1025cae6f73aSJoerg Wunsch 	return 1;
1026e0b78e19SJoerg Wunsch }
1027