xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 8e929d6553a758fdaa96adfbaa1928c3850028a2)
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$
418e929d65SKATO Takenori  *	$Id: vm_machdep.c,v 1.105 1998/03/23 19:52:42 jlemon 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
68640c4313SJonathan Lemon #ifdef VM86
69640c4313SJonathan Lemon #include <machine/pcb_ext.h>
70640c4313SJonathan Lemon #include <machine/vm86.h>
71640c4313SJonathan Lemon #endif
725b81b6b3SRodney W. Grimes 
7326f9a767SRodney W. Grimes #include <vm/vm.h>
74efeaf95aSDavid Greenman #include <vm/vm_param.h>
75efeaf95aSDavid Greenman #include <vm/vm_prot.h>
76996c772fSJohn Dyson #include <sys/lock.h>
7726f9a767SRodney W. Grimes #include <vm/vm_kern.h>
7824a1cce3SDavid Greenman #include <vm/vm_page.h>
79efeaf95aSDavid Greenman #include <vm/vm_map.h>
80efeaf95aSDavid Greenman #include <vm/vm_extern.h>
81efeaf95aSDavid Greenman 
82efeaf95aSDavid Greenman #include <sys/user.h>
835b81b6b3SRodney W. Grimes 
84e30f0011SSatoshi Asami #ifdef PC98
85e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h>
86e30f0011SSatoshi Asami #else
872320728fSRodney W. Grimes #include <i386/isa/isa.h>
88e30f0011SSatoshi Asami #endif
892320728fSRodney W. Grimes 
909aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
9187b91157SPoul-Henning Kamp static vm_offset_t
926c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
9387b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
946c146e28SBruce Evans 					int now));
9587b91157SPoul-Henning Kamp static vm_offset_t
966c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
9787b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
986c146e28SBruce Evans 
9987b6de2bSPoul-Henning Kamp static volatile int	kvasfreecnt;
100ae92ea44SDavid Greenman 
101d5e26ef0SDavid Greenman caddr_t		bouncememory;
10287b91157SPoul-Henning Kamp static int	bpwait;
10387b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
10487b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
105d5e26ef0SDavid Greenman 
106ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
10787b91157SPoul-Henning Kamp static int		bounceallocarraysize;
10887b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
10987b91157SPoul-Henning Kamp static int		bouncefree;
110d5e26ef0SDavid Greenman 
111e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA)
112e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096)			/* 15MB boundary */
113e30f0011SSatoshi Asami #else
114d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
115e30f0011SSatoshi Asami #endif
116ae92ea44SDavid Greenman #define MAXBKVA 1024
117f8845af0SPoul-Henning Kamp int		maxbkva = MAXBKVA*PAGE_SIZE;
118d5e26ef0SDavid Greenman 
119d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
12087b91157SPoul-Henning Kamp static struct kvasfree {
121d5e26ef0SDavid Greenman 	vm_offset_t addr;
122d5e26ef0SDavid Greenman 	vm_offset_t size;
123d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
124d5e26ef0SDavid Greenman 
125d5e26ef0SDavid Greenman /*
126d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
127d5e26ef0SDavid Greenman  * (only 1 inplemented now)
128d5e26ef0SDavid Greenman  */
12987b91157SPoul-Henning Kamp static vm_offset_t
130d5e26ef0SDavid Greenman vm_bounce_page_find(count)
131d5e26ef0SDavid Greenman 	int count;
132d5e26ef0SDavid Greenman {
133d5e26ef0SDavid Greenman 	int bit;
134d5e26ef0SDavid Greenman 	int s,i;
135d5e26ef0SDavid Greenman 
136d5e26ef0SDavid Greenman 	if (count != 1)
137d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
138d5e26ef0SDavid Greenman 
139d5e26ef0SDavid Greenman 	s = splbio();
140d5e26ef0SDavid Greenman retry:
141d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
142d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1433fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1443fb3086eSPoul-Henning Kamp 			if (bit) {
145d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
146d5e26ef0SDavid Greenman 				bouncefree -= count;
147d5e26ef0SDavid Greenman 				splx(s);
148ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
149d5e26ef0SDavid Greenman 			}
150d5e26ef0SDavid Greenman 		}
151d5e26ef0SDavid Greenman 	}
152ed7fcbd0SDavid Greenman 	bpwait = 1;
153d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
154d5e26ef0SDavid Greenman 	goto retry;
155d5e26ef0SDavid Greenman }
156d5e26ef0SDavid Greenman 
15787b91157SPoul-Henning Kamp static void
1581561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1591561d038SDavid Greenman 	vm_offset_t addr;
1601561d038SDavid Greenman 	vm_offset_t size;
1611561d038SDavid Greenman 	int now;
1621561d038SDavid Greenman {
1631561d038SDavid Greenman 	int s = splbio();
1641561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
165ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
166ae92ea44SDavid Greenman 	++kvasfreecnt;
1670e195446SDavid Greenman 	if( now) {
1680e195446SDavid Greenman 		/*
1690e195446SDavid Greenman 		 * this will do wakeups
1700e195446SDavid Greenman 		 */
1711561d038SDavid Greenman 		vm_bounce_kva(0,0);
1720e195446SDavid Greenman 	} else {
1730e195446SDavid Greenman 		if (bmwait) {
1740e195446SDavid Greenman 		/*
1750e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1760e195446SDavid Greenman 		 */
1771561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1780e195446SDavid Greenman 			bmwait = 0;
1790e195446SDavid Greenman 		}
1800e195446SDavid Greenman 	}
1811561d038SDavid Greenman 	splx(s);
1821561d038SDavid Greenman }
1831561d038SDavid Greenman 
184d5e26ef0SDavid Greenman /*
185d5e26ef0SDavid Greenman  * free count bounce buffer pages
186d5e26ef0SDavid Greenman  */
18787b91157SPoul-Henning Kamp static void
188d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
189d5e26ef0SDavid Greenman 	vm_offset_t pa;
190d5e26ef0SDavid Greenman 	int count;
191d5e26ef0SDavid Greenman {
192d5e26ef0SDavid Greenman 	int allocindex;
193d5e26ef0SDavid Greenman 	int index;
194d5e26ef0SDavid Greenman 	int bit;
195d5e26ef0SDavid Greenman 
196d5e26ef0SDavid Greenman 	if (count != 1)
197edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
198d5e26ef0SDavid Greenman 
199ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
200ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
201ae92ea44SDavid Greenman 			break;
202ae92ea44SDavid Greenman 	}
203d5e26ef0SDavid Greenman 
204ae92ea44SDavid Greenman 	if( index == bouncepages)
205ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
206d5e26ef0SDavid Greenman 
207ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
208ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
209d5e26ef0SDavid Greenman 
210d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
211d5e26ef0SDavid Greenman 
212d5e26ef0SDavid Greenman 	bouncefree += count;
213ed7fcbd0SDavid Greenman 	if (bpwait) {
214ed7fcbd0SDavid Greenman 		bpwait = 0;
215d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
216d5e26ef0SDavid Greenman 	}
217ed7fcbd0SDavid Greenman }
218d5e26ef0SDavid Greenman 
219d5e26ef0SDavid Greenman /*
220d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
221d5e26ef0SDavid Greenman  */
22287b91157SPoul-Henning Kamp static vm_offset_t
223ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
224ae92ea44SDavid Greenman 	int size;
2251561d038SDavid Greenman 	int waitok;
226d5e26ef0SDavid Greenman {
227d5e26ef0SDavid Greenman 	int i;
2286b4ac811SDavid Greenman 	vm_offset_t kva = 0;
229c564966bSDavid Greenman 	vm_offset_t off;
230d5e26ef0SDavid Greenman 	int s = splbio();
231d5e26ef0SDavid Greenman more:
232ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
233d5e26ef0SDavid Greenman 		bmfreeing = 1;
234ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
235f8845af0SPoul-Henning Kamp 			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
236c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
237c564966bSDavid Greenman 			}
2381561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
239d5e26ef0SDavid Greenman 				kvaf[i].size);
240d5e26ef0SDavid Greenman 		}
241d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
242d5e26ef0SDavid Greenman 		bmfreeing = 0;
243ae92ea44SDavid Greenman 		if( bmwait) {
244ae92ea44SDavid Greenman 			bmwait = 0;
245ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
246ae92ea44SDavid Greenman 		}
247d5e26ef0SDavid Greenman 	}
248d5e26ef0SDavid Greenman 
2491561d038SDavid Greenman 	if( size == 0) {
2501561d038SDavid Greenman 		splx(s);
2516ab46d52SBruce Evans 		return 0;
2521561d038SDavid Greenman 	}
2531561d038SDavid Greenman 
254ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2551561d038SDavid Greenman 		if( !waitok) {
2561561d038SDavid Greenman 			splx(s);
2576ab46d52SBruce Evans 			return 0;
2581561d038SDavid Greenman 		}
259d5e26ef0SDavid Greenman 		bmwait = 1;
2601561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
261d5e26ef0SDavid Greenman 		goto more;
262d5e26ef0SDavid Greenman 	}
263d5e26ef0SDavid Greenman 	splx(s);
264d5e26ef0SDavid Greenman 	return kva;
265d5e26ef0SDavid Greenman }
266d5e26ef0SDavid Greenman 
267d5e26ef0SDavid Greenman /*
268ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2690e195446SDavid Greenman  */
2700e195446SDavid Greenman vm_offset_t
2710e195446SDavid Greenman vm_bounce_kva_alloc(count)
2720e195446SDavid Greenman int count;
2730e195446SDavid Greenman {
2740e195446SDavid Greenman 	int i;
2750e195446SDavid Greenman 	vm_offset_t kva;
2760e195446SDavid Greenman 	vm_offset_t pa;
2770e195446SDavid Greenman 	if( bouncepages == 0) {
278f8845af0SPoul-Henning Kamp 		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
2790e195446SDavid Greenman 		return kva;
2800e195446SDavid Greenman 	}
281f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
2820e195446SDavid Greenman 	for(i=0;i<count;i++) {
2830e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
284f8845af0SPoul-Henning Kamp 		pmap_kenter(kva + i * PAGE_SIZE, pa);
2850e195446SDavid Greenman 	}
2860e195446SDavid Greenman 	return kva;
2870e195446SDavid Greenman }
2880e195446SDavid Greenman 
2890e195446SDavid Greenman /*
2900e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2910e195446SDavid Greenman  */
2920e195446SDavid Greenman void
2930e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
2940e195446SDavid Greenman 	vm_offset_t kva;
2950e195446SDavid Greenman 	int count;
2960e195446SDavid Greenman {
2970e195446SDavid Greenman 	int i;
2980e195446SDavid Greenman 	vm_offset_t pa;
2990e195446SDavid Greenman 	if( bouncepages == 0) {
3000e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
3010e195446SDavid Greenman 		return;
3020e195446SDavid Greenman 	}
3030e195446SDavid Greenman 	for(i = 0; i < count; i++) {
304f8845af0SPoul-Henning Kamp 		pa = pmap_kextract(kva + i * PAGE_SIZE);
3050e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
3060e195446SDavid Greenman 	}
307f8845af0SPoul-Henning Kamp 	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
3080e195446SDavid Greenman }
3090e195446SDavid Greenman 
3100e195446SDavid Greenman /*
311d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
312d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
313d5e26ef0SDavid Greenman  */
314d5e26ef0SDavid Greenman void
315d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
316d5e26ef0SDavid Greenman 	struct buf *bp;
317d5e26ef0SDavid Greenman {
318d5e26ef0SDavid Greenman 	int countvmpg;
319d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
320d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
321d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
322d5e26ef0SDavid Greenman 	vm_offset_t pa;
323d5e26ef0SDavid Greenman 	int dobounceflag = 0;
324d5e26ef0SDavid Greenman 	int i;
325d5e26ef0SDavid Greenman 
326d5e26ef0SDavid Greenman 	if (bouncepages == 0)
327d5e26ef0SDavid Greenman 		return;
328d5e26ef0SDavid Greenman 
329ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
330ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
331ae92ea44SDavid Greenman 		return;
3321561d038SDavid Greenman 	}
3331561d038SDavid Greenman 
334ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3353fb3086eSPoul-Henning Kamp 		printf(
3363fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
337ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
338ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
339ae92ea44SDavid Greenman 	}
340ae92ea44SDavid Greenman 
341ae92ea44SDavid Greenman /*
342ae92ea44SDavid Greenman  *  This is not really necessary
343ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
344ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
345ae92ea44SDavid Greenman  *	}
346ae92ea44SDavid Greenman  */
347ae92ea44SDavid Greenman 
348ae92ea44SDavid Greenman 
34926f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
35026f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
351d5e26ef0SDavid Greenman 
352bd7e5f99SJohn Dyson 	vapstart = trunc_page(vastart);
353bd7e5f99SJohn Dyson 	vapend = round_page(vaend);
354f8845af0SPoul-Henning Kamp 	countvmpg = (vapend - vapstart) / PAGE_SIZE;
355d5e26ef0SDavid Greenman 
356d5e26ef0SDavid Greenman /*
357d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
358d5e26ef0SDavid Greenman  */
359d5e26ef0SDavid Greenman 	va = vapstart;
360d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
361ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
362d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
363d5e26ef0SDavid Greenman 			++dobounceflag;
3640d94caffSDavid Greenman 		if( pa == 0)
3650d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
366f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
367d5e26ef0SDavid Greenman 	}
368d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
369d5e26ef0SDavid Greenman 		return;
370d5e26ef0SDavid Greenman 
371d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
372d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
373d5e26ef0SDavid Greenman 
374d5e26ef0SDavid Greenman /*
375d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
376d5e26ef0SDavid Greenman  */
377f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
378ae92ea44SDavid Greenman #if 0
379ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
380ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
381ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
382ae92ea44SDavid Greenman #endif
383d5e26ef0SDavid Greenman 	va = vapstart;
384d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
385ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
386d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
387d5e26ef0SDavid Greenman 			/*
388d5e26ef0SDavid Greenman 			 * allocate a replacement page
389d5e26ef0SDavid Greenman 			 */
390d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
391f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
392ae92ea44SDavid Greenman #if 0
393ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
394ae92ea44SDavid Greenman #endif
395d5e26ef0SDavid Greenman 			/*
396d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
397d5e26ef0SDavid Greenman 			 */
3981561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
399f8845af0SPoul-Henning Kamp 				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
4001561d038SDavid Greenman 			}
401d5e26ef0SDavid Greenman 		} else {
402d5e26ef0SDavid Greenman 			/*
403d5e26ef0SDavid Greenman 			 * use original page
404d5e26ef0SDavid Greenman 			 */
405f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), pa);
406d5e26ef0SDavid Greenman 		}
407f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
408d5e26ef0SDavid Greenman 	}
409d5e26ef0SDavid Greenman 
410d5e26ef0SDavid Greenman /*
411d5e26ef0SDavid Greenman  * flag the buffer as being bounced
412d5e26ef0SDavid Greenman  */
413d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
414d5e26ef0SDavid Greenman /*
415d5e26ef0SDavid Greenman  * save the original buffer kva
416d5e26ef0SDavid Greenman  */
41726f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
418d5e26ef0SDavid Greenman /*
419d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
420d5e26ef0SDavid Greenman  */
42126f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
422f8845af0SPoul-Henning Kamp 				((vm_offset_t) bp->b_savekva & PAGE_MASK));
423ae92ea44SDavid Greenman #if 0
424ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
425ae92ea44SDavid Greenman #endif
426d5e26ef0SDavid Greenman 	return;
427d5e26ef0SDavid Greenman }
428d5e26ef0SDavid Greenman 
429d5e26ef0SDavid Greenman /*
430d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
431d5e26ef0SDavid Greenman  */
432d5e26ef0SDavid Greenman void
433d5e26ef0SDavid Greenman vm_bounce_free(bp)
434d5e26ef0SDavid Greenman 	struct buf *bp;
435d5e26ef0SDavid Greenman {
436d5e26ef0SDavid Greenman 	int i;
437ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
438d5e26ef0SDavid Greenman 
439d5e26ef0SDavid Greenman /*
440d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
441d5e26ef0SDavid Greenman  */
442d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
443d5e26ef0SDavid Greenman 		return;
444d5e26ef0SDavid Greenman 
445ae92ea44SDavid Greenman /*
446ae92ea44SDavid Greenman  *  This check is not necessary
447ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
448ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
449ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
450ae92ea44SDavid Greenman  *	}
451ae92ea44SDavid Greenman  */
452ae92ea44SDavid Greenman 
453d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
45426f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
455ae92ea44SDavid Greenman /*
456ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
457ae92ea44SDavid Greenman */
458d5e26ef0SDavid Greenman 
459d5e26ef0SDavid Greenman /*
460d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
461d5e26ef0SDavid Greenman  */
462ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
463d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
464d5e26ef0SDavid Greenman 		vm_offset_t copycount;
465d5e26ef0SDavid Greenman 
466bd7e5f99SJohn Dyson 		copycount = round_page(bouncekva + 1) - bouncekva;
467bd7e5f99SJohn Dyson 		mybouncepa = pmap_kextract(trunc_page(bouncekva));
468d5e26ef0SDavid Greenman 
469d5e26ef0SDavid Greenman /*
470d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
471d5e26ef0SDavid Greenman  */
472bd7e5f99SJohn Dyson 		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
473ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
474ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
475ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
476d5e26ef0SDavid Greenman /*
477d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
478d5e26ef0SDavid Greenman  */
479d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
480ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
481d5e26ef0SDavid Greenman /*
482d5e26ef0SDavid Greenman  * free the bounce allocation
483d5e26ef0SDavid Greenman  */
484ae92ea44SDavid Greenman 
485ae92ea44SDavid Greenman /*
486ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
487ae92ea44SDavid Greenman */
488ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
489d5e26ef0SDavid Greenman 		}
490d5e26ef0SDavid Greenman 
491d5e26ef0SDavid Greenman 		origkva += copycount;
492d5e26ef0SDavid Greenman 		bouncekva += copycount;
493ae92ea44SDavid Greenman 		i += copycount;
494d5e26ef0SDavid Greenman 	}
495d5e26ef0SDavid Greenman 
496d5e26ef0SDavid Greenman /*
497ae92ea44SDavid Greenman 	printf("\n");
498ae92ea44SDavid Greenman */
499ae92ea44SDavid Greenman /*
500d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
501d5e26ef0SDavid Greenman  */
502ae92ea44SDavid Greenman 
503bd7e5f99SJohn Dyson 	bouncekva= trunc_page((vm_offset_t) bp->b_data);
504bd7e5f99SJohn Dyson 	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
505ae92ea44SDavid Greenman 
506ae92ea44SDavid Greenman /*
507f8845af0SPoul-Henning Kamp 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
508ae92ea44SDavid Greenman */
509ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
51026f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
511d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
512d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
513d5e26ef0SDavid Greenman 
514d5e26ef0SDavid Greenman 	return;
515d5e26ef0SDavid Greenman }
516d5e26ef0SDavid Greenman 
517ae92ea44SDavid Greenman 
5185b81b6b3SRodney W. Grimes /*
5191561d038SDavid Greenman  * init the bounce buffer system
5201561d038SDavid Greenman  */
5211561d038SDavid Greenman void
5221561d038SDavid Greenman vm_bounce_init()
5231561d038SDavid Greenman {
524ae92ea44SDavid Greenman 	int i;
5251561d038SDavid Greenman 
5261561d038SDavid Greenman 	kvasfreecnt = 0;
5271561d038SDavid Greenman 
5281561d038SDavid Greenman 	if (bouncepages == 0)
5291561d038SDavid Greenman 		return;
5301561d038SDavid Greenman 
5311561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5321561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5331561d038SDavid Greenman 
5341561d038SDavid Greenman 	if (!bounceallocarray)
535edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5361561d038SDavid Greenman 
537ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
538ae92ea44SDavid Greenman 	if (!bouncepa)
539edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5401561d038SDavid Greenman 
5410d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5420d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5430d94caffSDavid Greenman 	}
5440d94caffSDavid Greenman 
545ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
546ae92ea44SDavid Greenman 		vm_offset_t pa;
5470621c31aSJohn Dyson 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) {
5480621c31aSJohn Dyson 			printf("vm_bounce_init: bounce memory out of range -- bounce disabled\n");
5490621c31aSJohn Dyson 			free(bounceallocarray, M_TEMP);
5500621c31aSJohn Dyson 			bounceallocarray = NULL;
5510621c31aSJohn Dyson 			free(bouncepa, M_TEMP);
5520621c31aSJohn Dyson 			bouncepa = NULL;
5530621c31aSJohn Dyson 			bouncepages = 0;
5540621c31aSJohn Dyson 			break;
5550621c31aSJohn Dyson 		}
556ae92ea44SDavid Greenman 		if( pa == 0)
557ae92ea44SDavid Greenman 			panic("bounce memory not resident");
558ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5590d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
560ae92ea44SDavid Greenman 	}
5611561d038SDavid Greenman 	bouncefree = bouncepages;
5621561d038SDavid Greenman 
5631561d038SDavid Greenman }
5649aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5656c146e28SBruce Evans 
566a4f7a4c9SDavid Greenman /*
567a4f7a4c9SDavid Greenman  * quick version of vm_fault
568a4f7a4c9SDavid Greenman  */
569dca51299SJohn Dyson void
570a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5716c146e28SBruce Evans 	caddr_t v;
572a4f7a4c9SDavid Greenman 	int prot;
573a4f7a4c9SDavid Greenman {
5741d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5756c146e28SBruce Evans 		subyte(v, fubyte(v));
576a4f7a4c9SDavid Greenman 	else
5776c146e28SBruce Evans 		fubyte(v);
578a4f7a4c9SDavid Greenman }
579a4f7a4c9SDavid Greenman 
5801561d038SDavid Greenman /*
5815b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
582a2a1c95cSPeter Wemm  * Copy and update the pcb, set up the stack so that the child
583a2a1c95cSPeter Wemm  * ready to run and return to user mode.
5845b81b6b3SRodney W. Grimes  */
585a2a1c95cSPeter Wemm void
5865b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5875b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5885b81b6b3SRodney W. Grimes {
589f83a01b6SDavid Greenman 	struct pcb *pcb2 = &p2->p_addr->u_pcb;
5905b81b6b3SRodney W. Grimes 
5919f449d2aSBruce Evans #if NNPX > 0
5921f8745a9SPeter Wemm 	/* Ensure that p1's pcb is up to date. */
5931f8745a9SPeter Wemm 	if (npxproc == p1)
5941f8745a9SPeter Wemm 		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
5959f449d2aSBruce Evans #endif
5961f8745a9SPeter Wemm 
5971f8745a9SPeter Wemm 	/* Copy p1's pcb. */
5981f8745a9SPeter Wemm 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
599a2a1c95cSPeter Wemm 
600a2a1c95cSPeter Wemm 	/*
601a2a1c95cSPeter Wemm 	 * Create a new fresh stack for the new process.
6021f8745a9SPeter Wemm 	 * Copy the trap frame for the return to user mode as if from a
6031f8745a9SPeter Wemm 	 * syscall.  This copies the user mode register values.
604a2a1c95cSPeter Wemm 	 */
6051f8745a9SPeter Wemm 	p2->p_md.md_regs = (struct trapframe *)
606640c4313SJonathan Lemon #ifdef VM86
607640c4313SJonathan Lemon 			   ((int)p2->p_addr + UPAGES * PAGE_SIZE - 16) - 1;
608640c4313SJonathan Lemon #else
6091f8745a9SPeter Wemm 			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
610640c4313SJonathan Lemon #endif /* VM86 */
6111f8745a9SPeter Wemm 	*p2->p_md.md_regs = *p1->p_md.md_regs;
612a2a1c95cSPeter Wemm 
613a2a1c95cSPeter Wemm 	/*
614a2a1c95cSPeter Wemm 	 * Set registers for trampoline to user mode.  Leave space for the
615a2a1c95cSPeter Wemm 	 * return address on stack.  These are the kernel mode register values.
616a2a1c95cSPeter Wemm 	 */
6171f8745a9SPeter Wemm 	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
6181f8745a9SPeter Wemm 	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
619a2a1c95cSPeter Wemm 	pcb2->pcb_esi = (int)fork_return;
6201f8745a9SPeter Wemm 	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
621a2a1c95cSPeter Wemm 	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
6221f8745a9SPeter Wemm 	pcb2->pcb_ebx = (int)p2;
6231f8745a9SPeter Wemm 	pcb2->pcb_eip = (int)fork_trampoline;
6241f8745a9SPeter Wemm 	/*
6251f8745a9SPeter Wemm 	 * pcb2->pcb_ldt:	duplicated below, if necessary.
6261f8745a9SPeter Wemm 	 * pcb2->pcb_ldt_len:	cloned above.
6271f8745a9SPeter Wemm 	 * pcb2->pcb_savefpu:	cloned above.
6281f8745a9SPeter Wemm 	 * pcb2->pcb_flags:	cloned above (always 0 here?).
6291f8745a9SPeter Wemm 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
6301f8745a9SPeter Wemm 	 */
6315b81b6b3SRodney W. Grimes 
63248a09cf2SJohn Dyson #ifdef VM86
63348a09cf2SJohn Dyson 	/*
63448a09cf2SJohn Dyson 	 * XXX don't copy the i/o pages.  this should probably be fixed.
63548a09cf2SJohn Dyson 	 */
63648a09cf2SJohn Dyson 	pcb2->pcb_ext = 0;
63748a09cf2SJohn Dyson #endif
63848a09cf2SJohn Dyson 
6398c39a127SStefan Eßer #ifdef USER_LDT
6408c39a127SStefan Eßer         /* Copy the LDT, if necessary. */
641a2a1c95cSPeter Wemm         if (pcb2->pcb_ldt != 0) {
6428c39a127SStefan Eßer                 union descriptor *new_ldt;
643a2a1c95cSPeter Wemm                 size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
6448c39a127SStefan Eßer 
6458c39a127SStefan Eßer                 new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
646a2a1c95cSPeter Wemm                 bcopy(pcb2->pcb_ldt, new_ldt, len);
647a2a1c95cSPeter Wemm                 pcb2->pcb_ldt = (caddr_t)new_ldt;
6488c39a127SStefan Eßer         }
6498c39a127SStefan Eßer #endif
6508c39a127SStefan Eßer 
651a2a1c95cSPeter Wemm 	/*
652a2a1c95cSPeter Wemm 	 * Now, cpu_switch() can schedule the new process.
653a2a1c95cSPeter Wemm 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
654a2a1c95cSPeter Wemm 	 * containing the return address when exiting cpu_switch.
655a2a1c95cSPeter Wemm 	 * This will normally be to proc_trampoline(), which will have
656a2a1c95cSPeter Wemm 	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
657a2a1c95cSPeter Wemm 	 * will set up a stack to call fork_return(p, frame); to complete
658a2a1c95cSPeter Wemm 	 * the return to user-mode.
659a2a1c95cSPeter Wemm 	 */
660a2a1c95cSPeter Wemm }
661a2a1c95cSPeter Wemm 
662a2a1c95cSPeter Wemm /*
663a2a1c95cSPeter Wemm  * Intercept the return address from a freshly forked process that has NOT
664a2a1c95cSPeter Wemm  * been scheduled yet.
665a2a1c95cSPeter Wemm  *
666a2a1c95cSPeter Wemm  * This is needed to make kernel threads stay in kernel mode.
667a2a1c95cSPeter Wemm  */
668a2a1c95cSPeter Wemm void
669a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg)
670a2a1c95cSPeter Wemm 	struct proc *p;
671a2a1c95cSPeter Wemm 	void (*func) __P((void *));
672a2a1c95cSPeter Wemm 	void *arg;
673a2a1c95cSPeter Wemm {
674a2a1c95cSPeter Wemm 	/*
675a2a1c95cSPeter Wemm 	 * Note that the trap frame follows the args, so the function
676a2a1c95cSPeter Wemm 	 * is really called like this:  func(arg, frame);
677a2a1c95cSPeter Wemm 	 */
678a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
679a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
6805b81b6b3SRodney W. Grimes }
6815b81b6b3SRodney W. Grimes 
6827c2b54e8SNate Williams void
6835b81b6b3SRodney W. Grimes cpu_exit(p)
6845b81b6b3SRodney W. Grimes 	register struct proc *p;
6855b81b6b3SRodney W. Grimes {
68648a09cf2SJohn Dyson #if defined(USER_LDT) || defined(VM86)
68748a09cf2SJohn Dyson 	struct pcb *pcb = &p->p_addr->u_pcb;
6880dbf6d73SJordan K. Hubbard #endif
6895b81b6b3SRodney W. Grimes 
690960173b9SRodney W. Grimes #if NNPX > 0
6915b81b6b3SRodney W. Grimes 	npxexit(p);
692960173b9SRodney W. Grimes #endif	/* NNPX */
69348a09cf2SJohn Dyson #ifdef VM86
69448a09cf2SJohn Dyson 	if (pcb->pcb_ext != 0) {
69548a09cf2SJohn Dyson 	        /*
69648a09cf2SJohn Dyson 		 * XXX do we need to move the TSS off the allocated pages
69748a09cf2SJohn Dyson 		 * before freeing them?  (not done here)
69848a09cf2SJohn Dyson 		 */
69948a09cf2SJohn Dyson 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
70048a09cf2SJohn Dyson 		    ctob(IOPAGES + 1));
70148a09cf2SJohn Dyson 		pcb->pcb_ext = 0;
70248a09cf2SJohn Dyson 	}
70348a09cf2SJohn Dyson #endif
7040dbf6d73SJordan K. Hubbard #ifdef USER_LDT
7050dbf6d73SJordan K. Hubbard 	if (pcb->pcb_ldt != 0) {
7060dbf6d73SJordan K. Hubbard 		if (pcb == curpcb)
7070dbf6d73SJordan K. Hubbard 			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
7080dbf6d73SJordan K. Hubbard 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
7090dbf6d73SJordan K. Hubbard 			pcb->pcb_ldt_len * sizeof(union descriptor));
7100dbf6d73SJordan K. Hubbard 		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
7110dbf6d73SJordan K. Hubbard 	}
7120dbf6d73SJordan K. Hubbard #endif
7131a051896SBruce Evans 	cnt.v_swtch++;
7141a051896SBruce Evans 	cpu_switch(p);
7157c2b54e8SNate Williams 	panic("cpu_exit");
7165b81b6b3SRodney W. Grimes }
7175b81b6b3SRodney W. Grimes 
718381fe1aaSGarrett Wollman void
719bd7e5f99SJohn Dyson cpu_wait(p)
720bd7e5f99SJohn Dyson 	struct proc *p;
721bd7e5f99SJohn Dyson {
7225b81b6b3SRodney W. Grimes 	/* drop per-process resources */
723675878e7SJohn Dyson 	pmap_dispose_proc(p);
7242d8acc0fSJohn Dyson 
7252d8acc0fSJohn Dyson 	/* and clean-out the vmspace */
7267f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
7275b81b6b3SRodney W. Grimes }
7285b81b6b3SRodney W. Grimes 
7295b81b6b3SRodney W. Grimes /*
73026f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
73126f9a767SRodney W. Grimes  */
73226f9a767SRodney W. Grimes int
73326f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
73426f9a767SRodney W. Grimes 	struct proc *p;
73526f9a767SRodney W. Grimes 	struct vnode *vp;
73626f9a767SRodney W. Grimes 	struct ucred *cred;
73726f9a767SRodney W. Grimes {
73826f9a767SRodney W. Grimes 
73926f9a767SRodney W. Grimes 	return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES),
74026f9a767SRodney W. Grimes 	    (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL,
74126f9a767SRodney W. Grimes 	    p));
74226f9a767SRodney W. Grimes }
74326f9a767SRodney W. Grimes 
74487b6de2bSPoul-Henning Kamp #ifdef notyet
74587b91157SPoul-Henning Kamp static void
7465b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
7475b81b6b3SRodney W. Grimes 	u_short *pte;
7485b81b6b3SRodney W. Grimes 	caddr_t vaddr;
7495b81b6b3SRodney W. Grimes {
7505b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
7515b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
7525b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
7535b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
7545b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
7555b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
7565b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
7575b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
7585b81b6b3SRodney W. Grimes }
75987b6de2bSPoul-Henning Kamp #endif
76026f9a767SRodney W. Grimes 
76126f9a767SRodney W. Grimes /*
7625b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
7635b81b6b3SRodney W. Grimes  */
764aaf08d94SGarrett Wollman u_long
7657f8cb368SDavid Greenman kvtop(void *addr)
7665b81b6b3SRodney W. Grimes {
7675b81b6b3SRodney W. Grimes 	vm_offset_t va;
7685b81b6b3SRodney W. Grimes 
769ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
7705b81b6b3SRodney W. Grimes 	if (va == 0)
7715b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
7727f8cb368SDavid Greenman 	return((int)va);
7735b81b6b3SRodney W. Grimes }
7745b81b6b3SRodney W. Grimes 
7755b81b6b3SRodney W. Grimes /*
776ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
7775b81b6b3SRodney W. Grimes  *
778ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
779ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
780ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
7815b81b6b3SRodney W. Grimes  */
782381fe1aaSGarrett Wollman void
7835b81b6b3SRodney W. Grimes vmapbuf(bp)
7845b81b6b3SRodney W. Grimes 	register struct buf *bp;
7855b81b6b3SRodney W. Grimes {
7860157d6d9SJohn Dyson 	register caddr_t addr, v, kva;
78707658526SPoul-Henning Kamp 	vm_offset_t pa;
7885b81b6b3SRodney W. Grimes 
78926f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
7905b81b6b3SRodney W. Grimes 		panic("vmapbuf");
79126f9a767SRodney W. Grimes 
79212936855SDavid Greenman 	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
79326f9a767SRodney W. Grimes 	    addr < bp->b_data + bp->b_bufsize;
7940157d6d9SJohn Dyson 	    addr += PAGE_SIZE, v += PAGE_SIZE) {
79526f9a767SRodney W. Grimes 		/*
796ac269d78SDavid Greenman 		 * Do the vm_fault if needed; do the copy-on-write thing
797ac269d78SDavid Greenman 		 * when reading stuff off device into memory.
79826f9a767SRodney W. Grimes 		 */
79926f9a767SRodney W. Grimes 		vm_fault_quick(addr,
80026f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
8010157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
802641a27e6SJohn Dyson 		if (pa == 0)
803641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
80426f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
8050157d6d9SJohn Dyson 		pmap_kenter((vm_offset_t) v, pa);
80626f9a767SRodney W. Grimes 	}
80726f9a767SRodney W. Grimes 
8080157d6d9SJohn Dyson 	kva = bp->b_saveaddr;
8090157d6d9SJohn Dyson 	bp->b_saveaddr = bp->b_data;
8100157d6d9SJohn Dyson 	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
8115b81b6b3SRodney W. Grimes }
8125b81b6b3SRodney W. Grimes 
8135b81b6b3SRodney W. Grimes /*
8145b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
8155b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
8165b81b6b3SRodney W. Grimes  */
817381fe1aaSGarrett Wollman void
8185b81b6b3SRodney W. Grimes vunmapbuf(bp)
8195b81b6b3SRodney W. Grimes 	register struct buf *bp;
8205b81b6b3SRodney W. Grimes {
821c564966bSDavid Greenman 	register caddr_t addr;
82207658526SPoul-Henning Kamp 	vm_offset_t pa;
8235b81b6b3SRodney W. Grimes 
8245b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
8255b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
826c564966bSDavid Greenman 
82712936855SDavid Greenman 	for (addr = (caddr_t)trunc_page(bp->b_data);
82812936855SDavid Greenman 	    addr < bp->b_data + bp->b_bufsize;
829f8845af0SPoul-Henning Kamp 	    addr += PAGE_SIZE) {
8300157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
8310157d6d9SJohn Dyson 		pmap_kremove((vm_offset_t) addr);
83226f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
83326f9a767SRodney W. Grimes 	}
8340157d6d9SJohn Dyson 
8350157d6d9SJohn Dyson 	bp->b_data = bp->b_saveaddr;
8365b81b6b3SRodney W. Grimes }
8375b81b6b3SRodney W. Grimes 
8385b81b6b3SRodney W. Grimes /*
8395b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
8405b81b6b3SRodney W. Grimes  */
8417f8cb368SDavid Greenman void
842d447dbeeSBruce Evans cpu_reset()
843d447dbeeSBruce Evans {
844d447dbeeSBruce Evans 
84503245f09SKATO Takenori #ifdef PC98
84603245f09SKATO Takenori 	/*
84703245f09SKATO Takenori 	 * Attempt to do a CPU reset via CPU reset port.
84803245f09SKATO Takenori 	 */
849d447dbeeSBruce Evans 	disable_intr();
8508e929d65SKATO Takenori 	if ((inb(0x35) & 0xa0) != 0xa0) {
85103245f09SKATO Takenori 		outb(0x37, 0x0f);		/* SHUT0 = 0. */
85203245f09SKATO Takenori 		outb(0x37, 0x0b);		/* SHUT1 = 0. */
8538e929d65SKATO Takenori 	}
85403245f09SKATO Takenori 	outb(0xf0, 0x00);		/* Reset. */
85503245f09SKATO Takenori #else
8562320728fSRodney W. Grimes 	/*
8572320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
8582320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
8592320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
8602320728fSRodney W. Grimes 	 */
8615eb46edfSDavid Greenman 
86203245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET)
8632320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
8642320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
8652320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
8662320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
8675eb46edfSDavid Greenman #endif
86803245f09SKATO Takenori #endif /* PC98 */
8695b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
870f8845af0SPoul-Henning Kamp 	bzero((caddr_t) PTD, PAGE_SIZE);
8715b81b6b3SRodney W. Grimes 
8725b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
87327e9b35eSJohn Dyson 	invltlb();
8745b81b6b3SRodney W. Grimes 	/* NOTREACHED */
8757f8cb368SDavid Greenman 	while(1);
8765b81b6b3SRodney W. Grimes }
877b9d60b3fSDavid Greenman 
878b9d60b3fSDavid Greenman /*
879b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
88029360eb0SDavid Greenman  *	chunks of SGROWSIZ.
881b9d60b3fSDavid Greenman  */
882b9d60b3fSDavid Greenman int
883b9d60b3fSDavid Greenman grow(p, sp)
884b9d60b3fSDavid Greenman 	struct proc *p;
88526f9a767SRodney W. Grimes 	u_int sp;
886b9d60b3fSDavid Greenman {
887b9d60b3fSDavid Greenman 	unsigned int nss;
888b9d60b3fSDavid Greenman 	caddr_t v;
889b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
890b9d60b3fSDavid Greenman 
891b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
892b9d60b3fSDavid Greenman 	    return (1);
893b9d60b3fSDavid Greenman 
894b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
895b9d60b3fSDavid Greenman 
896b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
897b9d60b3fSDavid Greenman 		return (0);
898b9d60b3fSDavid Greenman 
899b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
90029360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
901b9d60b3fSDavid Greenman 		int grow_amount;
902b9d60b3fSDavid Greenman 		/*
903b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
904b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
905b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
906b9d60b3fSDavid Greenman 		 * is expensive).
907b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
90829360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
909b9d60b3fSDavid Greenman 		 */
91029360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
911b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
91229360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
913b9d60b3fSDavid Greenman 		/*
91429360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
915b9d60b3fSDavid Greenman 		 * just extend to the maximum size
916b9d60b3fSDavid Greenman 		 */
917b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
918b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
919b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
920b9d60b3fSDavid Greenman 		}
92168940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
922bd7e5f99SJohn Dyson 		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
923b9d60b3fSDavid Greenman 			return (0);
924b9d60b3fSDavid Greenman 		}
925b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
926b9d60b3fSDavid Greenman 	}
927b9d60b3fSDavid Greenman 
928b9d60b3fSDavid Greenman 	return (1);
929b9d60b3fSDavid Greenman }
930a29b63cbSJohn Dyson 
9317762bc7bSBruce Evans static int cnt_prezero;
93266095752SJohn Dyson 
93366095752SJohn Dyson SYSCTL_INT(_machdep, OID_AUTO, cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
93466095752SJohn Dyson 
935a29b63cbSJohn Dyson /*
93683eab616SBruce Evans  * Implement the pre-zeroed page mechanism.
93783eab616SBruce Evans  * This routine is called from the idle loop.
938a29b63cbSJohn Dyson  */
939a29b63cbSJohn Dyson int
94083eab616SBruce Evans vm_page_zero_idle()
94183eab616SBruce Evans {
94283eab616SBruce Evans 	static int free_rover;
943a29b63cbSJohn Dyson 	vm_page_t m;
94483eab616SBruce Evans 	int s;
94583eab616SBruce Evans 
94683eab616SBruce Evans 	/*
94783eab616SBruce Evans 	 * XXX
9488e56e561SJohn Dyson 	 * We stop zeroing pages when there are sufficent prezeroed pages.
9498e56e561SJohn Dyson 	 * This threshold isn't really needed, except we want to
9508e56e561SJohn Dyson 	 * bypass unneeded calls to vm_page_list_find, and the
9518e56e561SJohn Dyson 	 * associated cache flush and latency.  The pre-zero will
9528e56e561SJohn Dyson 	 * still be called when there are significantly more
9538e56e561SJohn Dyson 	 * non-prezeroed pages than zeroed pages.  The threshold
9548e56e561SJohn Dyson 	 * of half the number of reserved pages is arbitrary, but
9558e56e561SJohn Dyson 	 * approximately the right amount.  Eventually, we should
9568e56e561SJohn Dyson 	 * perhaps interrupt the zero operation when a process
9578e56e561SJohn Dyson 	 * is found to be ready to run.
9588e56e561SJohn Dyson 	 */
95983eab616SBruce Evans 	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
96083eab616SBruce Evans 		return (0);
9619a3b3e8bSPeter Wemm #ifdef SMP
96266095752SJohn Dyson 	if (try_mplock()) {
9639a3b3e8bSPeter Wemm #endif
96483eab616SBruce Evans 		s = splvm();
965d20d60beSTor Egge 		__asm __volatile("sti" : : : "memory");
96683eab616SBruce Evans 		m = vm_page_list_find(PQ_FREE, free_rover);
96783eab616SBruce Evans 		if (m != NULL) {
9685070c7f8SJohn Dyson 			--(*vm_page_queues[m->queue].lcnt);
9695070c7f8SJohn Dyson 			TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
97095f67de2STor Egge 			m->queue = PQ_NONE;
97183eab616SBruce Evans 			splx(s);
97266095752SJohn Dyson #if 0
9739a3b3e8bSPeter Wemm 			rel_mplock();
9749a3b3e8bSPeter Wemm #endif
975a29b63cbSJohn Dyson 			pmap_zero_page(VM_PAGE_TO_PHYS(m));
9767762bc7bSBruce Evans #if 0
9779a3b3e8bSPeter Wemm 			get_mplock();
9789a3b3e8bSPeter Wemm #endif
97983eab616SBruce Evans 			(void)splvm();
9805070c7f8SJohn Dyson 			m->queue = PQ_ZERO + m->pc;
9815070c7f8SJohn Dyson 			++(*vm_page_queues[m->queue].lcnt);
9827762bc7bSBruce Evans 			TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m,
9837762bc7bSBruce Evans 			    pageq);
9845070c7f8SJohn Dyson 			free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
985a316d390SJohn Dyson 			++vm_page_zero_count;
98666095752SJohn Dyson 			++cnt_prezero;
987a29b63cbSJohn Dyson 		}
98883eab616SBruce Evans 		splx(s);
989d20d60beSTor Egge 		__asm __volatile("cli" : : : "memory");
9909a3b3e8bSPeter Wemm #ifdef SMP
9919a3b3e8bSPeter Wemm 		rel_mplock();
9927762bc7bSBruce Evans #endif
99383eab616SBruce Evans 		return (1);
9947762bc7bSBruce Evans #ifdef SMP
995a29b63cbSJohn Dyson 	}
99666095752SJohn Dyson #endif
99766095752SJohn Dyson 	return (0);
99866095752SJohn Dyson }
999e0b78e19SJoerg Wunsch 
1000e0b78e19SJoerg Wunsch /*
100157d7d7b3SJustin T. Gibbs  * Software interrupt handler for queued VM system processing.
100257d7d7b3SJustin T. Gibbs  */
100357d7d7b3SJustin T. Gibbs void
100457d7d7b3SJustin T. Gibbs swi_vm()
100557d7d7b3SJustin T. Gibbs {
100657d7d7b3SJustin T. Gibbs 	if (busdma_swi_pending != 0)
100757d7d7b3SJustin T. Gibbs 		busdma_swi();
100857d7d7b3SJustin T. Gibbs }
100957d7d7b3SJustin T. Gibbs 
101057d7d7b3SJustin T. Gibbs /*
1011cae6f73aSJoerg Wunsch  * Tell whether this address is in some physical memory region.
1012e0b78e19SJoerg Wunsch  * Currently used by the kernel coredump code in order to avoid
1013e0b78e19SJoerg Wunsch  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
1014e0b78e19SJoerg Wunsch  * or other unpredictable behaviour.
1015e0b78e19SJoerg Wunsch  */
1016e0b78e19SJoerg Wunsch 
1017e0b78e19SJoerg Wunsch #include "isa.h"
1018e0b78e19SJoerg Wunsch 
1019e0b78e19SJoerg Wunsch int
1020cae6f73aSJoerg Wunsch is_physical_memory(addr)
1021e0b78e19SJoerg Wunsch 	vm_offset_t addr;
1022e0b78e19SJoerg Wunsch {
1023e0b78e19SJoerg Wunsch 
1024e0b78e19SJoerg Wunsch #if NISA > 0
1025e0b78e19SJoerg Wunsch 	/* The ISA ``memory hole''. */
1026e0b78e19SJoerg Wunsch 	if (addr >= 0xa0000 && addr < 0x100000)
1027cae6f73aSJoerg Wunsch 		return 0;
1028e0b78e19SJoerg Wunsch #endif
1029e0b78e19SJoerg Wunsch 
1030e0b78e19SJoerg Wunsch 	/*
1031e0b78e19SJoerg Wunsch 	 * stuff other tests for known memory-mapped devices (PCI?)
1032e0b78e19SJoerg Wunsch 	 * here
1033e0b78e19SJoerg Wunsch 	 */
1034e0b78e19SJoerg Wunsch 
1035cae6f73aSJoerg Wunsch 	return 1;
1036e0b78e19SJoerg Wunsch }
1037