xref: /freebsd/sys/i386/i386/vm_machdep.c (revision afc6ea238fca8459050ec15d5d897354e1d0435a)
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$
41afc6ea23STor Egge  *	$Id: vm_machdep.c,v 1.107 1998/05/17 22:12:11 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
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 
902f1e7069STor Egge static void	cpu_reset_real __P((void));
912f1e7069STor Egge #ifdef SMP
922f1e7069STor Egge static void	cpu_reset_proxy __P((void));
932f1e7069STor Egge static u_int	cpu_reset_proxyid;
942f1e7069STor Egge static volatile u_int	cpu_reset_proxy_active;
952f1e7069STor Egge #endif
962f1e7069STor Egge 
979aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS
9887b91157SPoul-Henning Kamp static vm_offset_t
996c146e28SBruce Evans 		vm_bounce_kva __P((int size, int waitok));
10087b91157SPoul-Henning Kamp static void	vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size,
1016c146e28SBruce Evans 					int now));
10287b91157SPoul-Henning Kamp static vm_offset_t
1036c146e28SBruce Evans 		vm_bounce_page_find __P((int count));
10487b91157SPoul-Henning Kamp static void	vm_bounce_page_free __P((vm_offset_t pa, int count));
1056c146e28SBruce Evans 
10687b6de2bSPoul-Henning Kamp static volatile int	kvasfreecnt;
107ae92ea44SDavid Greenman 
108d5e26ef0SDavid Greenman caddr_t		bouncememory;
10987b91157SPoul-Henning Kamp static int	bpwait;
11087b91157SPoul-Henning Kamp static vm_offset_t	*bouncepa;
11187b91157SPoul-Henning Kamp static int		bmwait, bmfreeing;
112d5e26ef0SDavid Greenman 
113ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned))
11487b91157SPoul-Henning Kamp static int		bounceallocarraysize;
11587b91157SPoul-Henning Kamp static unsigned	*bounceallocarray;
11687b91157SPoul-Henning Kamp static int		bouncefree;
117d5e26ef0SDavid Greenman 
118e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA)
119e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096)			/* 15MB boundary */
120e30f0011SSatoshi Asami #else
121d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096)
122e30f0011SSatoshi Asami #endif
123ae92ea44SDavid Greenman #define MAXBKVA 1024
124f8845af0SPoul-Henning Kamp int		maxbkva = MAXBKVA*PAGE_SIZE;
125d5e26ef0SDavid Greenman 
126d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */
12787b91157SPoul-Henning Kamp static struct kvasfree {
128d5e26ef0SDavid Greenman 	vm_offset_t addr;
129d5e26ef0SDavid Greenman 	vm_offset_t size;
130d5e26ef0SDavid Greenman } kvaf[MAXBKVA];
131d5e26ef0SDavid Greenman 
132d5e26ef0SDavid Greenman /*
133d5e26ef0SDavid Greenman  * get bounce buffer pages (count physically contiguous)
134d5e26ef0SDavid Greenman  * (only 1 inplemented now)
135d5e26ef0SDavid Greenman  */
13687b91157SPoul-Henning Kamp static vm_offset_t
137d5e26ef0SDavid Greenman vm_bounce_page_find(count)
138d5e26ef0SDavid Greenman 	int count;
139d5e26ef0SDavid Greenman {
140d5e26ef0SDavid Greenman 	int bit;
141d5e26ef0SDavid Greenman 	int s,i;
142d5e26ef0SDavid Greenman 
143d5e26ef0SDavid Greenman 	if (count != 1)
144d5e26ef0SDavid Greenman 		panic("vm_bounce_page_find -- no support for > 1 page yet!!!");
145d5e26ef0SDavid Greenman 
146d5e26ef0SDavid Greenman 	s = splbio();
147d5e26ef0SDavid Greenman retry:
148d5e26ef0SDavid Greenman 	for (i = 0; i < bounceallocarraysize; i++) {
149d5e26ef0SDavid Greenman 		if (bounceallocarray[i] != 0xffffffff) {
1503fb3086eSPoul-Henning Kamp 			bit = ffs(~bounceallocarray[i]);
1513fb3086eSPoul-Henning Kamp 			if (bit) {
152d5e26ef0SDavid Greenman 				bounceallocarray[i] |= 1 << (bit - 1) ;
153d5e26ef0SDavid Greenman 				bouncefree -= count;
154d5e26ef0SDavid Greenman 				splx(s);
155ae92ea44SDavid Greenman 				return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))];
156d5e26ef0SDavid Greenman 			}
157d5e26ef0SDavid Greenman 		}
158d5e26ef0SDavid Greenman 	}
159ed7fcbd0SDavid Greenman 	bpwait = 1;
160d5e26ef0SDavid Greenman 	tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0);
161d5e26ef0SDavid Greenman 	goto retry;
162d5e26ef0SDavid Greenman }
163d5e26ef0SDavid Greenman 
16487b91157SPoul-Henning Kamp static void
1651561d038SDavid Greenman vm_bounce_kva_free(addr, size, now)
1661561d038SDavid Greenman 	vm_offset_t addr;
1671561d038SDavid Greenman 	vm_offset_t size;
1681561d038SDavid Greenman 	int now;
1691561d038SDavid Greenman {
1701561d038SDavid Greenman 	int s = splbio();
1711561d038SDavid Greenman 	kvaf[kvasfreecnt].addr = addr;
172ae92ea44SDavid Greenman 	kvaf[kvasfreecnt].size = size;
173ae92ea44SDavid Greenman 	++kvasfreecnt;
1740e195446SDavid Greenman 	if( now) {
1750e195446SDavid Greenman 		/*
1760e195446SDavid Greenman 		 * this will do wakeups
1770e195446SDavid Greenman 		 */
1781561d038SDavid Greenman 		vm_bounce_kva(0,0);
1790e195446SDavid Greenman 	} else {
1800e195446SDavid Greenman 		if (bmwait) {
1810e195446SDavid Greenman 		/*
1820e195446SDavid Greenman 		 * if anyone is waiting on the bounce-map, then wakeup
1830e195446SDavid Greenman 		 */
1841561d038SDavid Greenman 			wakeup((caddr_t) io_map);
1850e195446SDavid Greenman 			bmwait = 0;
1860e195446SDavid Greenman 		}
1870e195446SDavid Greenman 	}
1881561d038SDavid Greenman 	splx(s);
1891561d038SDavid Greenman }
1901561d038SDavid Greenman 
191d5e26ef0SDavid Greenman /*
192d5e26ef0SDavid Greenman  * free count bounce buffer pages
193d5e26ef0SDavid Greenman  */
19487b91157SPoul-Henning Kamp static void
195d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count)
196d5e26ef0SDavid Greenman 	vm_offset_t pa;
197d5e26ef0SDavid Greenman 	int count;
198d5e26ef0SDavid Greenman {
199d5e26ef0SDavid Greenman 	int allocindex;
200d5e26ef0SDavid Greenman 	int index;
201d5e26ef0SDavid Greenman 	int bit;
202d5e26ef0SDavid Greenman 
203d5e26ef0SDavid Greenman 	if (count != 1)
204edf8a815SDavid Greenman 		panic("vm_bounce_page_free -- no support for > 1 page yet!!!");
205d5e26ef0SDavid Greenman 
206ae92ea44SDavid Greenman 	for(index=0;index<bouncepages;index++) {
207ae92ea44SDavid Greenman 		if( pa == bouncepa[index])
208ae92ea44SDavid Greenman 			break;
209ae92ea44SDavid Greenman 	}
210d5e26ef0SDavid Greenman 
211ae92ea44SDavid Greenman 	if( index == bouncepages)
212ae92ea44SDavid Greenman 		panic("vm_bounce_page_free: invalid bounce buffer");
213d5e26ef0SDavid Greenman 
214ed7fcbd0SDavid Greenman 	allocindex = index / BITS_IN_UNSIGNED;
215ed7fcbd0SDavid Greenman 	bit = index % BITS_IN_UNSIGNED;
216d5e26ef0SDavid Greenman 
217d5e26ef0SDavid Greenman 	bounceallocarray[allocindex] &= ~(1 << bit);
218d5e26ef0SDavid Greenman 
219d5e26ef0SDavid Greenman 	bouncefree += count;
220ed7fcbd0SDavid Greenman 	if (bpwait) {
221ed7fcbd0SDavid Greenman 		bpwait = 0;
222d5e26ef0SDavid Greenman 		wakeup((caddr_t) &bounceallocarray);
223d5e26ef0SDavid Greenman 	}
224ed7fcbd0SDavid Greenman }
225d5e26ef0SDavid Greenman 
226d5e26ef0SDavid Greenman /*
227d5e26ef0SDavid Greenman  * allocate count bounce buffer kva pages
228d5e26ef0SDavid Greenman  */
22987b91157SPoul-Henning Kamp static vm_offset_t
230ae92ea44SDavid Greenman vm_bounce_kva(size, waitok)
231ae92ea44SDavid Greenman 	int size;
2321561d038SDavid Greenman 	int waitok;
233d5e26ef0SDavid Greenman {
234d5e26ef0SDavid Greenman 	int i;
2356b4ac811SDavid Greenman 	vm_offset_t kva = 0;
236c564966bSDavid Greenman 	vm_offset_t off;
237d5e26ef0SDavid Greenman 	int s = splbio();
238d5e26ef0SDavid Greenman more:
239ae92ea44SDavid Greenman 	if (!bmfreeing && kvasfreecnt) {
240d5e26ef0SDavid Greenman 		bmfreeing = 1;
241ae92ea44SDavid Greenman 		for (i = 0; i < kvasfreecnt; i++) {
242f8845af0SPoul-Henning Kamp 			for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) {
243c564966bSDavid Greenman 				pmap_kremove( kvaf[i].addr + off);
244c564966bSDavid Greenman 			}
2451561d038SDavid Greenman 			kmem_free_wakeup(io_map, kvaf[i].addr,
246d5e26ef0SDavid Greenman 				kvaf[i].size);
247d5e26ef0SDavid Greenman 		}
248d5e26ef0SDavid Greenman 		kvasfreecnt = 0;
249d5e26ef0SDavid Greenman 		bmfreeing = 0;
250ae92ea44SDavid Greenman 		if( bmwait) {
251ae92ea44SDavid Greenman 			bmwait = 0;
252ae92ea44SDavid Greenman 			wakeup( (caddr_t) io_map);
253ae92ea44SDavid Greenman 		}
254d5e26ef0SDavid Greenman 	}
255d5e26ef0SDavid Greenman 
2561561d038SDavid Greenman 	if( size == 0) {
2571561d038SDavid Greenman 		splx(s);
2586ab46d52SBruce Evans 		return 0;
2591561d038SDavid Greenman 	}
2601561d038SDavid Greenman 
261ae92ea44SDavid Greenman 	if ((kva = kmem_alloc_pageable(io_map, size)) == 0) {
2621561d038SDavid Greenman 		if( !waitok) {
2631561d038SDavid Greenman 			splx(s);
2646ab46d52SBruce Evans 			return 0;
2651561d038SDavid Greenman 		}
266d5e26ef0SDavid Greenman 		bmwait = 1;
2671561d038SDavid Greenman 		tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0);
268d5e26ef0SDavid Greenman 		goto more;
269d5e26ef0SDavid Greenman 	}
270d5e26ef0SDavid Greenman 	splx(s);
271d5e26ef0SDavid Greenman 	return kva;
272d5e26ef0SDavid Greenman }
273d5e26ef0SDavid Greenman 
274d5e26ef0SDavid Greenman /*
275ae92ea44SDavid Greenman  * same as vm_bounce_kva -- but really allocate (but takes pages as arg)
2760e195446SDavid Greenman  */
2770e195446SDavid Greenman vm_offset_t
2780e195446SDavid Greenman vm_bounce_kva_alloc(count)
2790e195446SDavid Greenman int count;
2800e195446SDavid Greenman {
2810e195446SDavid Greenman 	int i;
2820e195446SDavid Greenman 	vm_offset_t kva;
2830e195446SDavid Greenman 	vm_offset_t pa;
2840e195446SDavid Greenman 	if( bouncepages == 0) {
285f8845af0SPoul-Henning Kamp 		kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK);
2860e195446SDavid Greenman 		return kva;
2870e195446SDavid Greenman 	}
288f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(count*PAGE_SIZE, 1);
2890e195446SDavid Greenman 	for(i=0;i<count;i++) {
2900e195446SDavid Greenman 		pa = vm_bounce_page_find(1);
291f8845af0SPoul-Henning Kamp 		pmap_kenter(kva + i * PAGE_SIZE, pa);
2920e195446SDavid Greenman 	}
2930e195446SDavid Greenman 	return kva;
2940e195446SDavid Greenman }
2950e195446SDavid Greenman 
2960e195446SDavid Greenman /*
2970e195446SDavid Greenman  * same as vm_bounce_kva_free -- but really free
2980e195446SDavid Greenman  */
2990e195446SDavid Greenman void
3000e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count)
3010e195446SDavid Greenman 	vm_offset_t kva;
3020e195446SDavid Greenman 	int count;
3030e195446SDavid Greenman {
3040e195446SDavid Greenman 	int i;
3050e195446SDavid Greenman 	vm_offset_t pa;
3060e195446SDavid Greenman 	if( bouncepages == 0) {
3070e195446SDavid Greenman 		free((caddr_t) kva, M_TEMP);
3080e195446SDavid Greenman 		return;
3090e195446SDavid Greenman 	}
3100e195446SDavid Greenman 	for(i = 0; i < count; i++) {
311f8845af0SPoul-Henning Kamp 		pa = pmap_kextract(kva + i * PAGE_SIZE);
3120e195446SDavid Greenman 		vm_bounce_page_free(pa, 1);
3130e195446SDavid Greenman 	}
314f8845af0SPoul-Henning Kamp 	vm_bounce_kva_free(kva, count*PAGE_SIZE, 0);
3150e195446SDavid Greenman }
3160e195446SDavid Greenman 
3170e195446SDavid Greenman /*
318d5e26ef0SDavid Greenman  * do the things necessary to the struct buf to implement
319d5e26ef0SDavid Greenman  * bounce buffers...  inserted before the disk sort
320d5e26ef0SDavid Greenman  */
321d5e26ef0SDavid Greenman void
322d5e26ef0SDavid Greenman vm_bounce_alloc(bp)
323d5e26ef0SDavid Greenman 	struct buf *bp;
324d5e26ef0SDavid Greenman {
325d5e26ef0SDavid Greenman 	int countvmpg;
326d5e26ef0SDavid Greenman 	vm_offset_t vastart, vaend;
327d5e26ef0SDavid Greenman 	vm_offset_t vapstart, vapend;
328d5e26ef0SDavid Greenman 	vm_offset_t va, kva;
329d5e26ef0SDavid Greenman 	vm_offset_t pa;
330d5e26ef0SDavid Greenman 	int dobounceflag = 0;
331d5e26ef0SDavid Greenman 	int i;
332d5e26ef0SDavid Greenman 
333d5e26ef0SDavid Greenman 	if (bouncepages == 0)
334d5e26ef0SDavid Greenman 		return;
335d5e26ef0SDavid Greenman 
336ae92ea44SDavid Greenman 	if (bp->b_flags & B_BOUNCE) {
337ae92ea44SDavid Greenman 		printf("vm_bounce_alloc: called recursively???\n");
338ae92ea44SDavid Greenman 		return;
3391561d038SDavid Greenman 	}
3401561d038SDavid Greenman 
341ae92ea44SDavid Greenman 	if (bp->b_bufsize < bp->b_bcount) {
3423fb3086eSPoul-Henning Kamp 		printf(
3433fb3086eSPoul-Henning Kamp 		    "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n",
344ae92ea44SDavid Greenman 			bp->b_bufsize, bp->b_bcount);
345ae92ea44SDavid Greenman 		panic("vm_bounce_alloc");
346ae92ea44SDavid Greenman 	}
347ae92ea44SDavid Greenman 
348ae92ea44SDavid Greenman /*
349ae92ea44SDavid Greenman  *  This is not really necessary
350ae92ea44SDavid Greenman  *	if( bp->b_bufsize != bp->b_bcount) {
351ae92ea44SDavid Greenman  *		printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount);
352ae92ea44SDavid Greenman  *	}
353ae92ea44SDavid Greenman  */
354ae92ea44SDavid Greenman 
355ae92ea44SDavid Greenman 
35626f9a767SRodney W. Grimes 	vastart = (vm_offset_t) bp->b_data;
35726f9a767SRodney W. Grimes 	vaend = (vm_offset_t) bp->b_data + bp->b_bufsize;
358d5e26ef0SDavid Greenman 
359bd7e5f99SJohn Dyson 	vapstart = trunc_page(vastart);
360bd7e5f99SJohn Dyson 	vapend = round_page(vaend);
361f8845af0SPoul-Henning Kamp 	countvmpg = (vapend - vapstart) / PAGE_SIZE;
362d5e26ef0SDavid Greenman 
363d5e26ef0SDavid Greenman /*
364d5e26ef0SDavid Greenman  * if any page is above 16MB, then go into bounce-buffer mode
365d5e26ef0SDavid Greenman  */
366d5e26ef0SDavid Greenman 	va = vapstart;
367d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
368ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
369d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG)
370d5e26ef0SDavid Greenman 			++dobounceflag;
3710d94caffSDavid Greenman 		if( pa == 0)
3720d94caffSDavid Greenman 			panic("vm_bounce_alloc: Unmapped page");
373f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
374d5e26ef0SDavid Greenman 	}
375d5e26ef0SDavid Greenman 	if (dobounceflag == 0)
376d5e26ef0SDavid Greenman 		return;
377d5e26ef0SDavid Greenman 
378d5e26ef0SDavid Greenman 	if (bouncepages < dobounceflag)
379d5e26ef0SDavid Greenman 		panic("Not enough bounce buffers!!!");
380d5e26ef0SDavid Greenman 
381d5e26ef0SDavid Greenman /*
382d5e26ef0SDavid Greenman  * allocate a replacement kva for b_addr
383d5e26ef0SDavid Greenman  */
384f8845af0SPoul-Henning Kamp 	kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1);
385ae92ea44SDavid Greenman #if 0
386ae92ea44SDavid Greenman 	printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ",
387ae92ea44SDavid Greenman 		(bp->b_flags & B_READ) ? "read":"write",
388ae92ea44SDavid Greenman 			vapstart, vapend, countvmpg, kva);
389ae92ea44SDavid Greenman #endif
390d5e26ef0SDavid Greenman 	va = vapstart;
391d5e26ef0SDavid Greenman 	for (i = 0; i < countvmpg; i++) {
392ed7fcbd0SDavid Greenman 		pa = pmap_kextract(va);
393d5e26ef0SDavid Greenman 		if (pa >= SIXTEENMEG) {
394d5e26ef0SDavid Greenman 			/*
395d5e26ef0SDavid Greenman 			 * allocate a replacement page
396d5e26ef0SDavid Greenman 			 */
397d5e26ef0SDavid Greenman 			vm_offset_t bpa = vm_bounce_page_find(1);
398f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), bpa);
399ae92ea44SDavid Greenman #if 0
400ae92ea44SDavid Greenman 			printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa);
401ae92ea44SDavid Greenman #endif
402d5e26ef0SDavid Greenman 			/*
403d5e26ef0SDavid Greenman 			 * if we are writing, the copy the data into the page
404d5e26ef0SDavid Greenman 			 */
4051561d038SDavid Greenman 			if ((bp->b_flags & B_READ) == 0) {
406f8845af0SPoul-Henning Kamp 				bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE);
4071561d038SDavid Greenman 			}
408d5e26ef0SDavid Greenman 		} else {
409d5e26ef0SDavid Greenman 			/*
410d5e26ef0SDavid Greenman 			 * use original page
411d5e26ef0SDavid Greenman 			 */
412f8845af0SPoul-Henning Kamp 			pmap_kenter(kva + (PAGE_SIZE * i), pa);
413d5e26ef0SDavid Greenman 		}
414f8845af0SPoul-Henning Kamp 		va += PAGE_SIZE;
415d5e26ef0SDavid Greenman 	}
416d5e26ef0SDavid Greenman 
417d5e26ef0SDavid Greenman /*
418d5e26ef0SDavid Greenman  * flag the buffer as being bounced
419d5e26ef0SDavid Greenman  */
420d5e26ef0SDavid Greenman 	bp->b_flags |= B_BOUNCE;
421d5e26ef0SDavid Greenman /*
422d5e26ef0SDavid Greenman  * save the original buffer kva
423d5e26ef0SDavid Greenman  */
42426f9a767SRodney W. Grimes 	bp->b_savekva = bp->b_data;
425d5e26ef0SDavid Greenman /*
426d5e26ef0SDavid Greenman  * put our new kva into the buffer (offset by original offset)
427d5e26ef0SDavid Greenman  */
42826f9a767SRodney W. Grimes 	bp->b_data = (caddr_t) (((vm_offset_t) kva) |
429f8845af0SPoul-Henning Kamp 				((vm_offset_t) bp->b_savekva & PAGE_MASK));
430ae92ea44SDavid Greenman #if 0
431ae92ea44SDavid Greenman 	printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data);
432ae92ea44SDavid Greenman #endif
433d5e26ef0SDavid Greenman 	return;
434d5e26ef0SDavid Greenman }
435d5e26ef0SDavid Greenman 
436d5e26ef0SDavid Greenman /*
437d5e26ef0SDavid Greenman  * hook into biodone to free bounce buffer
438d5e26ef0SDavid Greenman  */
439d5e26ef0SDavid Greenman void
440d5e26ef0SDavid Greenman vm_bounce_free(bp)
441d5e26ef0SDavid Greenman 	struct buf *bp;
442d5e26ef0SDavid Greenman {
443d5e26ef0SDavid Greenman 	int i;
444ae92ea44SDavid Greenman 	vm_offset_t origkva, bouncekva, bouncekvaend;
445d5e26ef0SDavid Greenman 
446d5e26ef0SDavid Greenman /*
447d5e26ef0SDavid Greenman  * if this isn't a bounced buffer, then just return
448d5e26ef0SDavid Greenman  */
449d5e26ef0SDavid Greenman 	if ((bp->b_flags & B_BOUNCE) == 0)
450d5e26ef0SDavid Greenman 		return;
451d5e26ef0SDavid Greenman 
452ae92ea44SDavid Greenman /*
453ae92ea44SDavid Greenman  *  This check is not necessary
454ae92ea44SDavid Greenman  *	if (bp->b_bufsize != bp->b_bcount) {
455ae92ea44SDavid Greenman  *		printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n",
456ae92ea44SDavid Greenman  *			bp->b_bufsize, bp->b_bcount);
457ae92ea44SDavid Greenman  *	}
458ae92ea44SDavid Greenman  */
459ae92ea44SDavid Greenman 
460d5e26ef0SDavid Greenman 	origkva = (vm_offset_t) bp->b_savekva;
46126f9a767SRodney W. Grimes 	bouncekva = (vm_offset_t) bp->b_data;
462ae92ea44SDavid Greenman /*
463ae92ea44SDavid Greenman 	printf("free: %d ", bp->b_bufsize);
464ae92ea44SDavid Greenman */
465d5e26ef0SDavid Greenman 
466d5e26ef0SDavid Greenman /*
467d5e26ef0SDavid Greenman  * check every page in the kva space for b_addr
468d5e26ef0SDavid Greenman  */
469ae92ea44SDavid Greenman 	for (i = 0; i < bp->b_bufsize; ) {
470d5e26ef0SDavid Greenman 		vm_offset_t mybouncepa;
471d5e26ef0SDavid Greenman 		vm_offset_t copycount;
472d5e26ef0SDavid Greenman 
473bd7e5f99SJohn Dyson 		copycount = round_page(bouncekva + 1) - bouncekva;
474bd7e5f99SJohn Dyson 		mybouncepa = pmap_kextract(trunc_page(bouncekva));
475d5e26ef0SDavid Greenman 
476d5e26ef0SDavid Greenman /*
477d5e26ef0SDavid Greenman  * if this is a bounced pa, then process as one
478d5e26ef0SDavid Greenman  */
479bd7e5f99SJohn Dyson 		if ( mybouncepa != pmap_kextract( trunc_page( origkva))) {
480ae92ea44SDavid Greenman 			vm_offset_t tocopy = copycount;
481ae92ea44SDavid Greenman 			if (i + tocopy > bp->b_bufsize)
482ae92ea44SDavid Greenman 				tocopy = bp->b_bufsize - i;
483d5e26ef0SDavid Greenman /*
484d5e26ef0SDavid Greenman  * if this is a read, then copy from bounce buffer into original buffer
485d5e26ef0SDavid Greenman  */
486d5e26ef0SDavid Greenman 			if (bp->b_flags & B_READ)
487ae92ea44SDavid Greenman 				bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy);
488d5e26ef0SDavid Greenman /*
489d5e26ef0SDavid Greenman  * free the bounce allocation
490d5e26ef0SDavid Greenman  */
491ae92ea44SDavid Greenman 
492ae92ea44SDavid Greenman /*
493ae92ea44SDavid Greenman 			printf("(kva: %x, pa: %x)", bouncekva, mybouncepa);
494ae92ea44SDavid Greenman */
495ae92ea44SDavid Greenman 			vm_bounce_page_free(mybouncepa, 1);
496d5e26ef0SDavid Greenman 		}
497d5e26ef0SDavid Greenman 
498d5e26ef0SDavid Greenman 		origkva += copycount;
499d5e26ef0SDavid Greenman 		bouncekva += copycount;
500ae92ea44SDavid Greenman 		i += copycount;
501d5e26ef0SDavid Greenman 	}
502d5e26ef0SDavid Greenman 
503d5e26ef0SDavid Greenman /*
504ae92ea44SDavid Greenman 	printf("\n");
505ae92ea44SDavid Greenman */
506ae92ea44SDavid Greenman /*
507d5e26ef0SDavid Greenman  * add the old kva into the "to free" list
508d5e26ef0SDavid Greenman  */
509ae92ea44SDavid Greenman 
510bd7e5f99SJohn Dyson 	bouncekva= trunc_page((vm_offset_t) bp->b_data);
511bd7e5f99SJohn Dyson 	bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize);
512ae92ea44SDavid Greenman 
513ae92ea44SDavid Greenman /*
514f8845af0SPoul-Henning Kamp 	printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE);
515ae92ea44SDavid Greenman */
516ae92ea44SDavid Greenman 	vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0);
51726f9a767SRodney W. Grimes 	bp->b_data = bp->b_savekva;
518d5e26ef0SDavid Greenman 	bp->b_savekva = 0;
519d5e26ef0SDavid Greenman 	bp->b_flags &= ~B_BOUNCE;
520d5e26ef0SDavid Greenman 
521d5e26ef0SDavid Greenman 	return;
522d5e26ef0SDavid Greenman }
523d5e26ef0SDavid Greenman 
524ae92ea44SDavid Greenman 
5255b81b6b3SRodney W. Grimes /*
5261561d038SDavid Greenman  * init the bounce buffer system
5271561d038SDavid Greenman  */
5281561d038SDavid Greenman void
5291561d038SDavid Greenman vm_bounce_init()
5301561d038SDavid Greenman {
531ae92ea44SDavid Greenman 	int i;
5321561d038SDavid Greenman 
5331561d038SDavid Greenman 	kvasfreecnt = 0;
5341561d038SDavid Greenman 
5351561d038SDavid Greenman 	if (bouncepages == 0)
5361561d038SDavid Greenman 		return;
5371561d038SDavid Greenman 
5381561d038SDavid Greenman 	bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED;
5391561d038SDavid Greenman 	bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT);
5401561d038SDavid Greenman 
5411561d038SDavid Greenman 	if (!bounceallocarray)
542edf8a815SDavid Greenman 		panic("Cannot allocate bounce resource array");
5431561d038SDavid Greenman 
544ae92ea44SDavid Greenman 	bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT);
545ae92ea44SDavid Greenman 	if (!bouncepa)
546edf8a815SDavid Greenman 		panic("Cannot allocate physical memory array");
5471561d038SDavid Greenman 
5480d94caffSDavid Greenman 	for(i=0;i<bounceallocarraysize;i++) {
5490d94caffSDavid Greenman 		bounceallocarray[i] = 0xffffffff;
5500d94caffSDavid Greenman 	}
5510d94caffSDavid Greenman 
552ae92ea44SDavid Greenman 	for(i=0;i<bouncepages;i++) {
553ae92ea44SDavid Greenman 		vm_offset_t pa;
5540621c31aSJohn Dyson 		if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) {
5550621c31aSJohn Dyson 			printf("vm_bounce_init: bounce memory out of range -- bounce disabled\n");
5560621c31aSJohn Dyson 			free(bounceallocarray, M_TEMP);
5570621c31aSJohn Dyson 			bounceallocarray = NULL;
5580621c31aSJohn Dyson 			free(bouncepa, M_TEMP);
5590621c31aSJohn Dyson 			bouncepa = NULL;
5600621c31aSJohn Dyson 			bouncepages = 0;
5610621c31aSJohn Dyson 			break;
5620621c31aSJohn Dyson 		}
563ae92ea44SDavid Greenman 		if( pa == 0)
564ae92ea44SDavid Greenman 			panic("bounce memory not resident");
565ae92ea44SDavid Greenman 		bouncepa[i] = pa;
5660d94caffSDavid Greenman 		bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int))));
567ae92ea44SDavid Greenman 	}
5681561d038SDavid Greenman 	bouncefree = bouncepages;
5691561d038SDavid Greenman 
5701561d038SDavid Greenman }
5719aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */
5726c146e28SBruce Evans 
573a4f7a4c9SDavid Greenman /*
574a4f7a4c9SDavid Greenman  * quick version of vm_fault
575a4f7a4c9SDavid Greenman  */
576dca51299SJohn Dyson void
577a4f7a4c9SDavid Greenman vm_fault_quick(v, prot)
5786c146e28SBruce Evans 	caddr_t v;
579a4f7a4c9SDavid Greenman 	int prot;
580a4f7a4c9SDavid Greenman {
5811d1b971bSDavid Greenman 	if (prot & VM_PROT_WRITE)
5826c146e28SBruce Evans 		subyte(v, fubyte(v));
583a4f7a4c9SDavid Greenman 	else
5846c146e28SBruce Evans 		fubyte(v);
585a4f7a4c9SDavid Greenman }
586a4f7a4c9SDavid Greenman 
5871561d038SDavid Greenman /*
5885b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
589a2a1c95cSPeter Wemm  * Copy and update the pcb, set up the stack so that the child
590a2a1c95cSPeter Wemm  * ready to run and return to user mode.
5915b81b6b3SRodney W. Grimes  */
592a2a1c95cSPeter Wemm void
5935b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
5945b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
5955b81b6b3SRodney W. Grimes {
596f83a01b6SDavid Greenman 	struct pcb *pcb2 = &p2->p_addr->u_pcb;
5975b81b6b3SRodney W. Grimes 
5989f449d2aSBruce Evans #if NNPX > 0
5991f8745a9SPeter Wemm 	/* Ensure that p1's pcb is up to date. */
6001f8745a9SPeter Wemm 	if (npxproc == p1)
6011f8745a9SPeter Wemm 		npxsave(&p1->p_addr->u_pcb.pcb_savefpu);
6029f449d2aSBruce Evans #endif
6031f8745a9SPeter Wemm 
6041f8745a9SPeter Wemm 	/* Copy p1's pcb. */
6051f8745a9SPeter Wemm 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
606a2a1c95cSPeter Wemm 
607a2a1c95cSPeter Wemm 	/*
608a2a1c95cSPeter Wemm 	 * Create a new fresh stack for the new process.
6091f8745a9SPeter Wemm 	 * Copy the trap frame for the return to user mode as if from a
6101f8745a9SPeter Wemm 	 * syscall.  This copies the user mode register values.
611a2a1c95cSPeter Wemm 	 */
6121f8745a9SPeter Wemm 	p2->p_md.md_regs = (struct trapframe *)
613640c4313SJonathan Lemon #ifdef VM86
614640c4313SJonathan Lemon 			   ((int)p2->p_addr + UPAGES * PAGE_SIZE - 16) - 1;
615640c4313SJonathan Lemon #else
6161f8745a9SPeter Wemm 			   ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1;
617640c4313SJonathan Lemon #endif /* VM86 */
6181f8745a9SPeter Wemm 	*p2->p_md.md_regs = *p1->p_md.md_regs;
619a2a1c95cSPeter Wemm 
620a2a1c95cSPeter Wemm 	/*
621a2a1c95cSPeter Wemm 	 * Set registers for trampoline to user mode.  Leave space for the
622a2a1c95cSPeter Wemm 	 * return address on stack.  These are the kernel mode register values.
623a2a1c95cSPeter Wemm 	 */
6241f8745a9SPeter Wemm 	pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir);
6251f8745a9SPeter Wemm 	pcb2->pcb_edi = p2->p_md.md_regs->tf_edi;
626a2a1c95cSPeter Wemm 	pcb2->pcb_esi = (int)fork_return;
6271f8745a9SPeter Wemm 	pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp;
628a2a1c95cSPeter Wemm 	pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *);
6291f8745a9SPeter Wemm 	pcb2->pcb_ebx = (int)p2;
6301f8745a9SPeter Wemm 	pcb2->pcb_eip = (int)fork_trampoline;
6311f8745a9SPeter Wemm 	/*
6321f8745a9SPeter Wemm 	 * pcb2->pcb_ldt:	duplicated below, if necessary.
6331f8745a9SPeter Wemm 	 * pcb2->pcb_ldt_len:	cloned above.
6341f8745a9SPeter Wemm 	 * pcb2->pcb_savefpu:	cloned above.
6351f8745a9SPeter Wemm 	 * pcb2->pcb_flags:	cloned above (always 0 here?).
6361f8745a9SPeter Wemm 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
6371f8745a9SPeter Wemm 	 */
6385b81b6b3SRodney W. Grimes 
63948a09cf2SJohn Dyson #ifdef VM86
64048a09cf2SJohn Dyson 	/*
64148a09cf2SJohn Dyson 	 * XXX don't copy the i/o pages.  this should probably be fixed.
64248a09cf2SJohn Dyson 	 */
64348a09cf2SJohn Dyson 	pcb2->pcb_ext = 0;
64448a09cf2SJohn Dyson #endif
64548a09cf2SJohn Dyson 
6468c39a127SStefan Eßer #ifdef USER_LDT
6478c39a127SStefan Eßer         /* Copy the LDT, if necessary. */
648a2a1c95cSPeter Wemm         if (pcb2->pcb_ldt != 0) {
6498c39a127SStefan Eßer                 union descriptor *new_ldt;
650a2a1c95cSPeter Wemm                 size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor);
6518c39a127SStefan Eßer 
6528c39a127SStefan Eßer                 new_ldt = (union descriptor *)kmem_alloc(kernel_map, len);
653a2a1c95cSPeter Wemm                 bcopy(pcb2->pcb_ldt, new_ldt, len);
654a2a1c95cSPeter Wemm                 pcb2->pcb_ldt = (caddr_t)new_ldt;
6558c39a127SStefan Eßer         }
6568c39a127SStefan Eßer #endif
6578c39a127SStefan Eßer 
658a2a1c95cSPeter Wemm 	/*
659a2a1c95cSPeter Wemm 	 * Now, cpu_switch() can schedule the new process.
660a2a1c95cSPeter Wemm 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
661a2a1c95cSPeter Wemm 	 * containing the return address when exiting cpu_switch.
662a2a1c95cSPeter Wemm 	 * This will normally be to proc_trampoline(), which will have
663a2a1c95cSPeter Wemm 	 * %ebx loaded with the new proc's pointer.  proc_trampoline()
664a2a1c95cSPeter Wemm 	 * will set up a stack to call fork_return(p, frame); to complete
665a2a1c95cSPeter Wemm 	 * the return to user-mode.
666a2a1c95cSPeter Wemm 	 */
667a2a1c95cSPeter Wemm }
668a2a1c95cSPeter Wemm 
669a2a1c95cSPeter Wemm /*
670a2a1c95cSPeter Wemm  * Intercept the return address from a freshly forked process that has NOT
671a2a1c95cSPeter Wemm  * been scheduled yet.
672a2a1c95cSPeter Wemm  *
673a2a1c95cSPeter Wemm  * This is needed to make kernel threads stay in kernel mode.
674a2a1c95cSPeter Wemm  */
675a2a1c95cSPeter Wemm void
676a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg)
677a2a1c95cSPeter Wemm 	struct proc *p;
678a2a1c95cSPeter Wemm 	void (*func) __P((void *));
679a2a1c95cSPeter Wemm 	void *arg;
680a2a1c95cSPeter Wemm {
681a2a1c95cSPeter Wemm 	/*
682a2a1c95cSPeter Wemm 	 * Note that the trap frame follows the args, so the function
683a2a1c95cSPeter Wemm 	 * is really called like this:  func(arg, frame);
684a2a1c95cSPeter Wemm 	 */
685a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
686a2a1c95cSPeter Wemm 	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
6875b81b6b3SRodney W. Grimes }
6885b81b6b3SRodney W. Grimes 
6897c2b54e8SNate Williams void
6905b81b6b3SRodney W. Grimes cpu_exit(p)
6915b81b6b3SRodney W. Grimes 	register struct proc *p;
6925b81b6b3SRodney W. Grimes {
69348a09cf2SJohn Dyson #if defined(USER_LDT) || defined(VM86)
69448a09cf2SJohn Dyson 	struct pcb *pcb = &p->p_addr->u_pcb;
6950dbf6d73SJordan K. Hubbard #endif
6965b81b6b3SRodney W. Grimes 
697960173b9SRodney W. Grimes #if NNPX > 0
6985b81b6b3SRodney W. Grimes 	npxexit(p);
699960173b9SRodney W. Grimes #endif	/* NNPX */
70048a09cf2SJohn Dyson #ifdef VM86
70148a09cf2SJohn Dyson 	if (pcb->pcb_ext != 0) {
70248a09cf2SJohn Dyson 	        /*
70348a09cf2SJohn Dyson 		 * XXX do we need to move the TSS off the allocated pages
70448a09cf2SJohn Dyson 		 * before freeing them?  (not done here)
70548a09cf2SJohn Dyson 		 */
70648a09cf2SJohn Dyson 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
70748a09cf2SJohn Dyson 		    ctob(IOPAGES + 1));
70848a09cf2SJohn Dyson 		pcb->pcb_ext = 0;
70948a09cf2SJohn Dyson 	}
71048a09cf2SJohn Dyson #endif
7110dbf6d73SJordan K. Hubbard #ifdef USER_LDT
7120dbf6d73SJordan K. Hubbard 	if (pcb->pcb_ldt != 0) {
7130dbf6d73SJordan K. Hubbard 		if (pcb == curpcb)
7140dbf6d73SJordan K. Hubbard 			lldt(GSEL(GUSERLDT_SEL, SEL_KPL));
7150dbf6d73SJordan K. Hubbard 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt,
7160dbf6d73SJordan K. Hubbard 			pcb->pcb_ldt_len * sizeof(union descriptor));
7170dbf6d73SJordan K. Hubbard 		pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0;
7180dbf6d73SJordan K. Hubbard 	}
7190dbf6d73SJordan K. Hubbard #endif
7201a051896SBruce Evans 	cnt.v_swtch++;
7211a051896SBruce Evans 	cpu_switch(p);
7227c2b54e8SNate Williams 	panic("cpu_exit");
7235b81b6b3SRodney W. Grimes }
7245b81b6b3SRodney W. Grimes 
725381fe1aaSGarrett Wollman void
726bd7e5f99SJohn Dyson cpu_wait(p)
727bd7e5f99SJohn Dyson 	struct proc *p;
728bd7e5f99SJohn Dyson {
7295b81b6b3SRodney W. Grimes 	/* drop per-process resources */
730675878e7SJohn Dyson 	pmap_dispose_proc(p);
7312d8acc0fSJohn Dyson 
7322d8acc0fSJohn Dyson 	/* and clean-out the vmspace */
7337f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
7345b81b6b3SRodney W. Grimes }
7355b81b6b3SRodney W. Grimes 
7365b81b6b3SRodney W. Grimes /*
73726f9a767SRodney W. Grimes  * Dump the machine specific header information at the start of a core dump.
73826f9a767SRodney W. Grimes  */
73926f9a767SRodney W. Grimes int
74026f9a767SRodney W. Grimes cpu_coredump(p, vp, cred)
74126f9a767SRodney W. Grimes 	struct proc *p;
74226f9a767SRodney W. Grimes 	struct vnode *vp;
74326f9a767SRodney W. Grimes 	struct ucred *cred;
74426f9a767SRodney W. Grimes {
745afc6ea23STor Egge 	int error;
746afc6ea23STor Egge 	caddr_t tempuser;
74726f9a767SRodney W. Grimes 
748afc6ea23STor Egge 	tempuser = malloc(ctob(UPAGES), M_TEMP, M_WAITOK);
749afc6ea23STor Egge 	if (!tempuser)
750afc6ea23STor Egge 		return EINVAL;
751afc6ea23STor Egge 
752afc6ea23STor Egge 	bzero(tempuser, ctob(UPAGES));
753afc6ea23STor Egge 	bcopy(p->p_addr, tempuser, sizeof(struct user));
754afc6ea23STor Egge 	bcopy(p->p_md.md_regs,
755afc6ea23STor Egge 	      tempuser + ((caddr_t) p->p_md.md_regs - (caddr_t) p->p_addr),
756afc6ea23STor Egge 	      sizeof(struct trapframe));
757afc6ea23STor Egge 
758afc6ea23STor Egge 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t) tempuser,
759afc6ea23STor Egge 			ctob(UPAGES),
760afc6ea23STor Egge 			(off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
761afc6ea23STor Egge 			cred, (int *)NULL, p);
762afc6ea23STor Egge 
763afc6ea23STor Egge 	free(tempuser, M_TEMP);
764afc6ea23STor Egge 
765afc6ea23STor Egge 	return error;
76626f9a767SRodney W. Grimes }
76726f9a767SRodney W. Grimes 
76887b6de2bSPoul-Henning Kamp #ifdef notyet
76987b91157SPoul-Henning Kamp static void
7705b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
7715b81b6b3SRodney W. Grimes 	u_short *pte;
7725b81b6b3SRodney W. Grimes 	caddr_t vaddr;
7735b81b6b3SRodney W. Grimes {
7745b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
7755b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
7765b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
7775b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
7785b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
7795b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
7805b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
7815b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
7825b81b6b3SRodney W. Grimes }
78387b6de2bSPoul-Henning Kamp #endif
78426f9a767SRodney W. Grimes 
78526f9a767SRodney W. Grimes /*
7865b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
7875b81b6b3SRodney W. Grimes  */
788aaf08d94SGarrett Wollman u_long
7897f8cb368SDavid Greenman kvtop(void *addr)
7905b81b6b3SRodney W. Grimes {
7915b81b6b3SRodney W. Grimes 	vm_offset_t va;
7925b81b6b3SRodney W. Grimes 
793ed7fcbd0SDavid Greenman 	va = pmap_kextract((vm_offset_t)addr);
7945b81b6b3SRodney W. Grimes 	if (va == 0)
7955b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
7967f8cb368SDavid Greenman 	return((int)va);
7975b81b6b3SRodney W. Grimes }
7985b81b6b3SRodney W. Grimes 
7995b81b6b3SRodney W. Grimes /*
800ac322158SDavid Greenman  * Map an IO request into kernel virtual address space.
8015b81b6b3SRodney W. Grimes  *
802ac322158SDavid Greenman  * All requests are (re)mapped into kernel VA space.
803ac322158SDavid Greenman  * Notice that we use b_bufsize for the size of the buffer
804ac322158SDavid Greenman  * to be mapped.  b_bcount might be modified by the driver.
8055b81b6b3SRodney W. Grimes  */
806381fe1aaSGarrett Wollman void
8075b81b6b3SRodney W. Grimes vmapbuf(bp)
8085b81b6b3SRodney W. Grimes 	register struct buf *bp;
8095b81b6b3SRodney W. Grimes {
8100157d6d9SJohn Dyson 	register caddr_t addr, v, kva;
81107658526SPoul-Henning Kamp 	vm_offset_t pa;
8125b81b6b3SRodney W. Grimes 
81326f9a767SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
8145b81b6b3SRodney W. Grimes 		panic("vmapbuf");
81526f9a767SRodney W. Grimes 
81612936855SDavid Greenman 	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
81726f9a767SRodney W. Grimes 	    addr < bp->b_data + bp->b_bufsize;
8180157d6d9SJohn Dyson 	    addr += PAGE_SIZE, v += PAGE_SIZE) {
81926f9a767SRodney W. Grimes 		/*
820ac269d78SDavid Greenman 		 * Do the vm_fault if needed; do the copy-on-write thing
821ac269d78SDavid Greenman 		 * when reading stuff off device into memory.
82226f9a767SRodney W. Grimes 		 */
82326f9a767SRodney W. Grimes 		vm_fault_quick(addr,
82426f9a767SRodney W. Grimes 			(bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
8250157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
826641a27e6SJohn Dyson 		if (pa == 0)
827641a27e6SJohn Dyson 			panic("vmapbuf: page not present");
82826f9a767SRodney W. Grimes 		vm_page_hold(PHYS_TO_VM_PAGE(pa));
8290157d6d9SJohn Dyson 		pmap_kenter((vm_offset_t) v, pa);
83026f9a767SRodney W. Grimes 	}
83126f9a767SRodney W. Grimes 
8320157d6d9SJohn Dyson 	kva = bp->b_saveaddr;
8330157d6d9SJohn Dyson 	bp->b_saveaddr = bp->b_data;
8340157d6d9SJohn Dyson 	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
8355b81b6b3SRodney W. Grimes }
8365b81b6b3SRodney W. Grimes 
8375b81b6b3SRodney W. Grimes /*
8385b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
8395b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
8405b81b6b3SRodney W. Grimes  */
841381fe1aaSGarrett Wollman void
8425b81b6b3SRodney W. Grimes vunmapbuf(bp)
8435b81b6b3SRodney W. Grimes 	register struct buf *bp;
8445b81b6b3SRodney W. Grimes {
845c564966bSDavid Greenman 	register caddr_t addr;
84607658526SPoul-Henning Kamp 	vm_offset_t pa;
8475b81b6b3SRodney W. Grimes 
8485b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
8495b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
850c564966bSDavid Greenman 
85112936855SDavid Greenman 	for (addr = (caddr_t)trunc_page(bp->b_data);
85212936855SDavid Greenman 	    addr < bp->b_data + bp->b_bufsize;
853f8845af0SPoul-Henning Kamp 	    addr += PAGE_SIZE) {
8540157d6d9SJohn Dyson 		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
8550157d6d9SJohn Dyson 		pmap_kremove((vm_offset_t) addr);
85626f9a767SRodney W. Grimes 		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
85726f9a767SRodney W. Grimes 	}
8580157d6d9SJohn Dyson 
8590157d6d9SJohn Dyson 	bp->b_data = bp->b_saveaddr;
8605b81b6b3SRodney W. Grimes }
8615b81b6b3SRodney W. Grimes 
8625b81b6b3SRodney W. Grimes /*
8635b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
8645b81b6b3SRodney W. Grimes  */
8652f1e7069STor Egge 
8662f1e7069STor Egge #ifdef SMP
8672f1e7069STor Egge static void
8682f1e7069STor Egge cpu_reset_proxy()
8692f1e7069STor Egge {
8702f1e7069STor Egge 	u_int saved_mp_lock;
8712f1e7069STor Egge 
8722f1e7069STor Egge 	cpu_reset_proxy_active = 1;
8732f1e7069STor Egge 	while (cpu_reset_proxy_active == 1)
8742f1e7069STor Egge 		;	 /* Wait for other cpu to disable interupts */
8752f1e7069STor Egge 	saved_mp_lock = mp_lock;
8762f1e7069STor Egge 	mp_lock = 1;
8772f1e7069STor Egge 	printf("cpu_reset_proxy: Grabbed mp lock for BSP\n");
8782f1e7069STor Egge 	cpu_reset_proxy_active = 3;
8792f1e7069STor Egge 	while (cpu_reset_proxy_active == 3)
8802f1e7069STor Egge 		;	/* Wait for other cpu to enable interrupts */
8812f1e7069STor Egge 	stop_cpus((1<<cpu_reset_proxyid));
8822f1e7069STor Egge 	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
8832f1e7069STor Egge 	DELAY(1000000);
8842f1e7069STor Egge 	cpu_reset_real();
8852f1e7069STor Egge }
8862f1e7069STor Egge #endif
8872f1e7069STor Egge 
8887f8cb368SDavid Greenman void
889d447dbeeSBruce Evans cpu_reset()
890d447dbeeSBruce Evans {
8912f1e7069STor Egge #ifdef SMP
8922f1e7069STor Egge 	if (smp_active == 0) {
8932f1e7069STor Egge 		cpu_reset_real();
8942f1e7069STor Egge 		/* NOTREACHED */
8952f1e7069STor Egge 	} else {
8962f1e7069STor Egge 
8972f1e7069STor Egge 		u_int map;
8982f1e7069STor Egge 		int cnt;
8992f1e7069STor Egge 		printf("cpu_reset called on cpu#%d\n",cpuid);
9002f1e7069STor Egge 
9012f1e7069STor Egge 		map = other_cpus & ~ stopped_cpus;
9022f1e7069STor Egge 
9032f1e7069STor Egge 		if (map != 0) {
9042f1e7069STor Egge 			printf("cpu_reset: Stopping other CPUs\n");
9052f1e7069STor Egge 			stop_cpus(map);		/* Stop all other CPUs */
9062f1e7069STor Egge 		}
9072f1e7069STor Egge 
9082f1e7069STor Egge 		if (cpuid == 0) {
9092f1e7069STor Egge 			DELAY(1000000);
9102f1e7069STor Egge 			cpu_reset_real();
9112f1e7069STor Egge 			/* NOTREACHED */
9122f1e7069STor Egge 		} else {
9132f1e7069STor Egge 			/* We are not BSP (CPU #0) */
9142f1e7069STor Egge 
9152f1e7069STor Egge 			cpu_reset_proxyid = cpuid;
9162f1e7069STor Egge 			cpustop_restartfunc = cpu_reset_proxy;
9172f1e7069STor Egge 			printf("cpu_reset: Restarting BSP\n");
9182f1e7069STor Egge 			started_cpus = (1<<0);		/* Restart CPU #0 */
9192f1e7069STor Egge 
9202f1e7069STor Egge 			cnt = 0;
9212f1e7069STor Egge 			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
9222f1e7069STor Egge 				cnt++;	/* Wait for BSP to announce restart */
9232f1e7069STor Egge 			if (cpu_reset_proxy_active == 0)
9242f1e7069STor Egge 				printf("cpu_reset: Failed to restart BSP\n");
9252f1e7069STor Egge 			__asm __volatile("cli" : : : "memory");
9262f1e7069STor Egge 			cpu_reset_proxy_active = 2;
9272f1e7069STor Egge 			cnt = 0;
9282f1e7069STor Egge 			while (cpu_reset_proxy_active == 2 && cnt < 10000000)
9292f1e7069STor Egge 				cnt++;	/* Do nothing */
9302f1e7069STor Egge 			if (cpu_reset_proxy_active == 2) {
9312f1e7069STor Egge 				printf("cpu_reset: BSP did not grab mp lock\n");
9322f1e7069STor Egge 				cpu_reset_real();	/* XXX: Bogus ? */
9332f1e7069STor Egge 			}
9342f1e7069STor Egge 			cpu_reset_proxy_active = 4;
9352f1e7069STor Egge 			__asm __volatile("sti" : : : "memory");
9362f1e7069STor Egge 			while (1);
9372f1e7069STor Egge 			/* NOTREACHED */
9382f1e7069STor Egge 		}
9392f1e7069STor Egge 	}
9402f1e7069STor Egge #else
9412f1e7069STor Egge 	cpu_reset_real();
9422f1e7069STor Egge #endif
9432f1e7069STor Egge }
9442f1e7069STor Egge 
9452f1e7069STor Egge static void
9462f1e7069STor Egge cpu_reset_real()
9472f1e7069STor Egge {
948d447dbeeSBruce Evans 
94903245f09SKATO Takenori #ifdef PC98
95003245f09SKATO Takenori 	/*
95103245f09SKATO Takenori 	 * Attempt to do a CPU reset via CPU reset port.
95203245f09SKATO Takenori 	 */
953d447dbeeSBruce Evans 	disable_intr();
9548e929d65SKATO Takenori 	if ((inb(0x35) & 0xa0) != 0xa0) {
95503245f09SKATO Takenori 		outb(0x37, 0x0f);		/* SHUT0 = 0. */
95603245f09SKATO Takenori 		outb(0x37, 0x0b);		/* SHUT1 = 0. */
9578e929d65SKATO Takenori 	}
95803245f09SKATO Takenori 	outb(0xf0, 0x00);		/* Reset. */
95903245f09SKATO Takenori #else
9602320728fSRodney W. Grimes 	/*
9612320728fSRodney W. Grimes 	 * Attempt to do a CPU reset via the keyboard controller,
9622320728fSRodney W. Grimes 	 * do not turn of the GateA20, as any machine that fails
9632320728fSRodney W. Grimes 	 * to do the reset here would then end up in no man's land.
9642320728fSRodney W. Grimes 	 */
9655eb46edfSDavid Greenman 
96603245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET)
9672320728fSRodney W. Grimes 	outb(IO_KBD + 4, 0xFE);
9682320728fSRodney W. Grimes 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
9692320728fSRodney W. Grimes 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
9702320728fSRodney W. Grimes 	DELAY(1000000);	/* wait 1 sec for printf to complete */
9715eb46edfSDavid Greenman #endif
97203245f09SKATO Takenori #endif /* PC98 */
9735b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
974f8845af0SPoul-Henning Kamp 	bzero((caddr_t) PTD, PAGE_SIZE);
9755b81b6b3SRodney W. Grimes 
9765b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
97727e9b35eSJohn Dyson 	invltlb();
9785b81b6b3SRodney W. Grimes 	/* NOTREACHED */
9797f8cb368SDavid Greenman 	while(1);
9805b81b6b3SRodney W. Grimes }
981b9d60b3fSDavid Greenman 
982b9d60b3fSDavid Greenman /*
983b9d60b3fSDavid Greenman  * Grow the user stack to allow for 'sp'. This version grows the stack in
98429360eb0SDavid Greenman  *	chunks of SGROWSIZ.
985b9d60b3fSDavid Greenman  */
986b9d60b3fSDavid Greenman int
987b9d60b3fSDavid Greenman grow(p, sp)
988b9d60b3fSDavid Greenman 	struct proc *p;
98926f9a767SRodney W. Grimes 	u_int sp;
990b9d60b3fSDavid Greenman {
991b9d60b3fSDavid Greenman 	unsigned int nss;
992b9d60b3fSDavid Greenman 	caddr_t v;
993b9d60b3fSDavid Greenman 	struct vmspace *vm = p->p_vmspace;
994b9d60b3fSDavid Greenman 
995b9d60b3fSDavid Greenman 	if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK)
996b9d60b3fSDavid Greenman 	    return (1);
997b9d60b3fSDavid Greenman 
998b9d60b3fSDavid Greenman 	nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE);
999b9d60b3fSDavid Greenman 
1000b9d60b3fSDavid Greenman 	if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur)
1001b9d60b3fSDavid Greenman 		return (0);
1002b9d60b3fSDavid Greenman 
1003b9d60b3fSDavid Greenman 	if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT,
100429360eb0SDavid Greenman 	    SGROWSIZ) < nss) {
1005b9d60b3fSDavid Greenman 		int grow_amount;
1006b9d60b3fSDavid Greenman 		/*
1007b9d60b3fSDavid Greenman 		 * If necessary, grow the VM that the stack occupies
1008b9d60b3fSDavid Greenman 		 * to allow for the rlimit. This allows us to not have
1009b9d60b3fSDavid Greenman 		 * to allocate all of the VM up-front in execve (which
1010b9d60b3fSDavid Greenman 		 * is expensive).
1011b9d60b3fSDavid Greenman 		 * Grow the VM by the amount requested rounded up to
101229360eb0SDavid Greenman 		 * the nearest SGROWSIZ to provide for some hysteresis.
1013b9d60b3fSDavid Greenman 		 */
101429360eb0SDavid Greenman 		grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ);
1015b9d60b3fSDavid Greenman 		v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT,
101629360eb0SDavid Greenman 		    SGROWSIZ) - grow_amount;
1017b9d60b3fSDavid Greenman 		/*
101829360eb0SDavid Greenman 		 * If there isn't enough room to extend by SGROWSIZ, then
1019b9d60b3fSDavid Greenman 		 * just extend to the maximum size
1020b9d60b3fSDavid Greenman 		 */
1021b9d60b3fSDavid Greenman 		if (v < vm->vm_maxsaddr) {
1022b9d60b3fSDavid Greenman 			v = vm->vm_maxsaddr;
1023b9d60b3fSDavid Greenman 			grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT);
1024b9d60b3fSDavid Greenman 		}
102568940ac1SDavid Greenman 		if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v,
1026bd7e5f99SJohn Dyson 		    grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) {
1027b9d60b3fSDavid Greenman 			return (0);
1028b9d60b3fSDavid Greenman 		}
1029b9d60b3fSDavid Greenman 		vm->vm_ssize += grow_amount >> PAGE_SHIFT;
1030b9d60b3fSDavid Greenman 	}
1031b9d60b3fSDavid Greenman 
1032b9d60b3fSDavid Greenman 	return (1);
1033b9d60b3fSDavid Greenman }
1034a29b63cbSJohn Dyson 
10357762bc7bSBruce Evans static int cnt_prezero;
103666095752SJohn Dyson 
103766095752SJohn Dyson SYSCTL_INT(_machdep, OID_AUTO, cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, "");
103866095752SJohn Dyson 
1039a29b63cbSJohn Dyson /*
104083eab616SBruce Evans  * Implement the pre-zeroed page mechanism.
104183eab616SBruce Evans  * This routine is called from the idle loop.
1042a29b63cbSJohn Dyson  */
1043a29b63cbSJohn Dyson int
104483eab616SBruce Evans vm_page_zero_idle()
104583eab616SBruce Evans {
104683eab616SBruce Evans 	static int free_rover;
1047a29b63cbSJohn Dyson 	vm_page_t m;
104883eab616SBruce Evans 	int s;
104983eab616SBruce Evans 
105083eab616SBruce Evans 	/*
105183eab616SBruce Evans 	 * XXX
10528e56e561SJohn Dyson 	 * We stop zeroing pages when there are sufficent prezeroed pages.
10538e56e561SJohn Dyson 	 * This threshold isn't really needed, except we want to
10548e56e561SJohn Dyson 	 * bypass unneeded calls to vm_page_list_find, and the
10558e56e561SJohn Dyson 	 * associated cache flush and latency.  The pre-zero will
10568e56e561SJohn Dyson 	 * still be called when there are significantly more
10578e56e561SJohn Dyson 	 * non-prezeroed pages than zeroed pages.  The threshold
10588e56e561SJohn Dyson 	 * of half the number of reserved pages is arbitrary, but
10598e56e561SJohn Dyson 	 * approximately the right amount.  Eventually, we should
10608e56e561SJohn Dyson 	 * perhaps interrupt the zero operation when a process
10618e56e561SJohn Dyson 	 * is found to be ready to run.
10628e56e561SJohn Dyson 	 */
106383eab616SBruce Evans 	if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2)
106483eab616SBruce Evans 		return (0);
10659a3b3e8bSPeter Wemm #ifdef SMP
106666095752SJohn Dyson 	if (try_mplock()) {
10679a3b3e8bSPeter Wemm #endif
106883eab616SBruce Evans 		s = splvm();
1069d20d60beSTor Egge 		__asm __volatile("sti" : : : "memory");
107083eab616SBruce Evans 		m = vm_page_list_find(PQ_FREE, free_rover);
107183eab616SBruce Evans 		if (m != NULL) {
10725070c7f8SJohn Dyson 			--(*vm_page_queues[m->queue].lcnt);
10735070c7f8SJohn Dyson 			TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq);
107495f67de2STor Egge 			m->queue = PQ_NONE;
107583eab616SBruce Evans 			splx(s);
107666095752SJohn Dyson #if 0
10779a3b3e8bSPeter Wemm 			rel_mplock();
10789a3b3e8bSPeter Wemm #endif
1079a29b63cbSJohn Dyson 			pmap_zero_page(VM_PAGE_TO_PHYS(m));
10807762bc7bSBruce Evans #if 0
10819a3b3e8bSPeter Wemm 			get_mplock();
10829a3b3e8bSPeter Wemm #endif
108383eab616SBruce Evans 			(void)splvm();
10845070c7f8SJohn Dyson 			m->queue = PQ_ZERO + m->pc;
10855070c7f8SJohn Dyson 			++(*vm_page_queues[m->queue].lcnt);
10867762bc7bSBruce Evans 			TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m,
10877762bc7bSBruce Evans 			    pageq);
10885070c7f8SJohn Dyson 			free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK;
1089a316d390SJohn Dyson 			++vm_page_zero_count;
109066095752SJohn Dyson 			++cnt_prezero;
1091a29b63cbSJohn Dyson 		}
109283eab616SBruce Evans 		splx(s);
1093d20d60beSTor Egge 		__asm __volatile("cli" : : : "memory");
10949a3b3e8bSPeter Wemm #ifdef SMP
10959a3b3e8bSPeter Wemm 		rel_mplock();
10967762bc7bSBruce Evans #endif
109783eab616SBruce Evans 		return (1);
10987762bc7bSBruce Evans #ifdef SMP
1099a29b63cbSJohn Dyson 	}
110066095752SJohn Dyson #endif
110166095752SJohn Dyson 	return (0);
110266095752SJohn Dyson }
1103e0b78e19SJoerg Wunsch 
1104e0b78e19SJoerg Wunsch /*
110557d7d7b3SJustin T. Gibbs  * Software interrupt handler for queued VM system processing.
110657d7d7b3SJustin T. Gibbs  */
110757d7d7b3SJustin T. Gibbs void
110857d7d7b3SJustin T. Gibbs swi_vm()
110957d7d7b3SJustin T. Gibbs {
111057d7d7b3SJustin T. Gibbs 	if (busdma_swi_pending != 0)
111157d7d7b3SJustin T. Gibbs 		busdma_swi();
111257d7d7b3SJustin T. Gibbs }
111357d7d7b3SJustin T. Gibbs 
111457d7d7b3SJustin T. Gibbs /*
1115cae6f73aSJoerg Wunsch  * Tell whether this address is in some physical memory region.
1116e0b78e19SJoerg Wunsch  * Currently used by the kernel coredump code in order to avoid
1117e0b78e19SJoerg Wunsch  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
1118e0b78e19SJoerg Wunsch  * or other unpredictable behaviour.
1119e0b78e19SJoerg Wunsch  */
1120e0b78e19SJoerg Wunsch 
1121e0b78e19SJoerg Wunsch #include "isa.h"
1122e0b78e19SJoerg Wunsch 
1123e0b78e19SJoerg Wunsch int
1124cae6f73aSJoerg Wunsch is_physical_memory(addr)
1125e0b78e19SJoerg Wunsch 	vm_offset_t addr;
1126e0b78e19SJoerg Wunsch {
1127e0b78e19SJoerg Wunsch 
1128e0b78e19SJoerg Wunsch #if NISA > 0
1129e0b78e19SJoerg Wunsch 	/* The ISA ``memory hole''. */
1130e0b78e19SJoerg Wunsch 	if (addr >= 0xa0000 && addr < 0x100000)
1131cae6f73aSJoerg Wunsch 		return 0;
1132e0b78e19SJoerg Wunsch #endif
1133e0b78e19SJoerg Wunsch 
1134e0b78e19SJoerg Wunsch 	/*
1135e0b78e19SJoerg Wunsch 	 * stuff other tests for known memory-mapped devices (PCI?)
1136e0b78e19SJoerg Wunsch 	 * here
1137e0b78e19SJoerg Wunsch 	 */
1138e0b78e19SJoerg Wunsch 
1139cae6f73aSJoerg Wunsch 	return 1;
1140e0b78e19SJoerg Wunsch }
1141