xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 7f8cb368698e810921be03374f8c227500631f4b)
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
45b81b6b3SRodney W. Grimes  * All rights reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
75b81b6b3SRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
85b81b6b3SRodney W. Grimes  * Science Department, and William Jolitz.
95b81b6b3SRodney W. Grimes  *
105b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
115b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
125b81b6b3SRodney W. Grimes  * are met:
135b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
145b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
155b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
165b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
175b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
185b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
195b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
205b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
215b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
225b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
235b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
245b81b6b3SRodney W. Grimes  *    without specific prior written permission.
255b81b6b3SRodney W. Grimes  *
265b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
275b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
285b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
295b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
305b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
315b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
325b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
335b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
345b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
355b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
365b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
375b81b6b3SRodney W. Grimes  *
38960173b9SRodney W. Grimes  *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
395b81b6b3SRodney W. Grimes  *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
407f8cb368SDavid Greenman  *	$Id: vm_machdep.c,v 1.8 1993/12/19 00:50:10 wollman Exp $
415b81b6b3SRodney W. Grimes  */
425b81b6b3SRodney W. Grimes 
43960173b9SRodney W. Grimes #include "npx.h"
445b81b6b3SRodney W. Grimes #include "param.h"
455b81b6b3SRodney W. Grimes #include "systm.h"
465b81b6b3SRodney W. Grimes #include "proc.h"
475b81b6b3SRodney W. Grimes #include "malloc.h"
485b81b6b3SRodney W. Grimes #include "buf.h"
495b81b6b3SRodney W. Grimes #include "user.h"
505b81b6b3SRodney W. Grimes 
515b81b6b3SRodney W. Grimes #include "../include/cpu.h"
525b81b6b3SRodney W. Grimes 
535b81b6b3SRodney W. Grimes #include "vm/vm.h"
545b81b6b3SRodney W. Grimes #include "vm/vm_kern.h"
555b81b6b3SRodney W. Grimes 
565b81b6b3SRodney W. Grimes /*
575b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
585b81b6b3SRodney W. Grimes  * Copy and update the kernel stack and pcb, making the child
595b81b6b3SRodney W. Grimes  * ready to run, and marking it so that it can return differently
605b81b6b3SRodney W. Grimes  * than the parent.  Returns 1 in the child process, 0 in the parent.
615b81b6b3SRodney W. Grimes  * We currently double-map the user area so that the stack is at the same
625b81b6b3SRodney W. Grimes  * address in each process; in the future we will probably relocate
635b81b6b3SRodney W. Grimes  * the frame pointers on the stack after copying.
645b81b6b3SRodney W. Grimes  */
65381fe1aaSGarrett Wollman int
665b81b6b3SRodney W. Grimes cpu_fork(p1, p2)
675b81b6b3SRodney W. Grimes 	register struct proc *p1, *p2;
685b81b6b3SRodney W. Grimes {
695b81b6b3SRodney W. Grimes 	register struct user *up = p2->p_addr;
705b81b6b3SRodney W. Grimes 	int foo, offset, addr, i;
715b81b6b3SRodney W. Grimes 	extern char kstack[];
725b81b6b3SRodney W. Grimes 	extern int mvesp();
735b81b6b3SRodney W. Grimes 
745b81b6b3SRodney W. Grimes 	/*
755b81b6b3SRodney W. Grimes 	 * Copy pcb and stack from proc p1 to p2.
765b81b6b3SRodney W. Grimes 	 * We do this as cheaply as possible, copying only the active
775b81b6b3SRodney W. Grimes 	 * part of the stack.  The stack and pcb need to agree;
785b81b6b3SRodney W. Grimes 	 * this is tricky, as the final pcb is constructed by savectx,
795b81b6b3SRodney W. Grimes 	 * but its frame isn't yet on the stack when the stack is copied.
805b81b6b3SRodney W. Grimes 	 * swtch compensates for this when the child eventually runs.
815b81b6b3SRodney W. Grimes 	 * This should be done differently, with a single call
825b81b6b3SRodney W. Grimes 	 * that copies and updates the pcb+stack,
835b81b6b3SRodney W. Grimes 	 * replacing the bcopy and savectx.
845b81b6b3SRodney W. Grimes 	 */
855b81b6b3SRodney W. Grimes 	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
865b81b6b3SRodney W. Grimes 	offset = mvesp() - (int)kstack;
875b81b6b3SRodney W. Grimes 	bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset,
885b81b6b3SRodney W. Grimes 	    (unsigned) ctob(UPAGES) - offset);
895b81b6b3SRodney W. Grimes 	p2->p_regs = p1->p_regs;
905b81b6b3SRodney W. Grimes 
915b81b6b3SRodney W. Grimes 	/*
925b81b6b3SRodney W. Grimes 	 * Wire top of address space of child to it's kstack.
935b81b6b3SRodney W. Grimes 	 * First, fault in a page of pte's to map it.
945b81b6b3SRodney W. Grimes 	 */
957f8cb368SDavid Greenman #if 0
965b81b6b3SRodney W. Grimes         addr = trunc_page((u_int)vtopte(kstack));
975b81b6b3SRodney W. Grimes 	vm_map_pageable(&p2->p_vmspace->vm_map, addr, addr+NBPG, FALSE);
985b81b6b3SRodney W. Grimes 	for (i=0; i < UPAGES; i++)
997f8cb368SDavid Greenman 		pmap_enter(&p2->p_vmspace->vm_pmap, kstack+i*NBPG,
10026931201SDavid Greenman 			   pmap_extract(kernel_pmap, ((int)p2->p_addr)+i*NBPG),
10126931201SDavid Greenman 			   /*
10226931201SDavid Greenman 			    * The user area has to be mapped writable because
10326931201SDavid Greenman 			    * it contains the kernel stack (when CR0_WP is on
10426931201SDavid Greenman 			    * on a 486 there is no user-read/kernel-write
10526931201SDavid Greenman 			    * mode).  It is protected from user mode access
10626931201SDavid Greenman 			    * by the segment limits.
10726931201SDavid Greenman 			    */
10826931201SDavid Greenman 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
1097f8cb368SDavid Greenman #endif
1105b81b6b3SRodney W. Grimes 	pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb);
1115b81b6b3SRodney W. Grimes 
1125b81b6b3SRodney W. Grimes 	/*
1135b81b6b3SRodney W. Grimes 	 *
1145b81b6b3SRodney W. Grimes 	 * Arrange for a non-local goto when the new process
1155b81b6b3SRodney W. Grimes 	 * is started, to resume here, returning nonzero from setjmp.
1165b81b6b3SRodney W. Grimes 	 */
1175b81b6b3SRodney W. Grimes 	if (savectx(up, 1)) {
1185b81b6b3SRodney W. Grimes 		/*
1195b81b6b3SRodney W. Grimes 		 * Return 1 in child.
1205b81b6b3SRodney W. Grimes 		 */
1215b81b6b3SRodney W. Grimes 		return (1);
1225b81b6b3SRodney W. Grimes 	}
1235b81b6b3SRodney W. Grimes 	return (0);
1245b81b6b3SRodney W. Grimes }
1255b81b6b3SRodney W. Grimes 
1265b81b6b3SRodney W. Grimes #ifdef notyet
1275b81b6b3SRodney W. Grimes /*
1285b81b6b3SRodney W. Grimes  * cpu_exit is called as the last action during exit.
1295b81b6b3SRodney W. Grimes  *
1305b81b6b3SRodney W. Grimes  * We change to an inactive address space and a "safe" stack,
1315b81b6b3SRodney W. Grimes  * passing thru an argument to the new stack. Now, safely isolated
1325b81b6b3SRodney W. Grimes  * from the resources we're shedding, we release the address space
1335b81b6b3SRodney W. Grimes  * and any remaining machine-dependent resources, including the
1345b81b6b3SRodney W. Grimes  * memory for the user structure and kernel stack.
1355b81b6b3SRodney W. Grimes  *
1365b81b6b3SRodney W. Grimes  * Next, we assign a dummy context to be written over by swtch,
1375b81b6b3SRodney W. Grimes  * calling it to send this process off to oblivion.
1385b81b6b3SRodney W. Grimes  * [The nullpcb allows us to minimize cost in swtch() by not having
1395b81b6b3SRodney W. Grimes  * a special case].
1405b81b6b3SRodney W. Grimes  */
1415b81b6b3SRodney W. Grimes struct proc *swtch_to_inactive();
14275124a8bSPaul Richards volatile void
1435b81b6b3SRodney W. Grimes cpu_exit(p)
1445b81b6b3SRodney W. Grimes 	register struct proc *p;
1455b81b6b3SRodney W. Grimes {
1465b81b6b3SRodney W. Grimes 	static struct pcb nullpcb;	/* pcb to overwrite on last swtch */
1475b81b6b3SRodney W. Grimes 
148960173b9SRodney W. Grimes #if NNPX > 0
1495b81b6b3SRodney W. Grimes 	npxexit(p);
150960173b9SRodney W. Grimes #endif	/* NNPX */
1515b81b6b3SRodney W. Grimes 
1525b81b6b3SRodney W. Grimes 	/* move to inactive space and stack, passing arg accross */
1535b81b6b3SRodney W. Grimes 	p = swtch_to_inactive(p);
1545b81b6b3SRodney W. Grimes 
1555b81b6b3SRodney W. Grimes 	/* drop per-process resources */
1565b81b6b3SRodney W. Grimes 	vmspace_free(p->p_vmspace);
1575b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
1585b81b6b3SRodney W. Grimes 
1595b81b6b3SRodney W. Grimes 	p->p_addr = (struct user *) &nullpcb;
1605b81b6b3SRodney W. Grimes 	splclock();
1615b81b6b3SRodney W. Grimes 	swtch();
1625b81b6b3SRodney W. Grimes 	/* NOTREACHED */
1635b81b6b3SRodney W. Grimes }
1645b81b6b3SRodney W. Grimes #else
1657c2b54e8SNate Williams void
1665b81b6b3SRodney W. Grimes cpu_exit(p)
1675b81b6b3SRodney W. Grimes 	register struct proc *p;
1685b81b6b3SRodney W. Grimes {
1695b81b6b3SRodney W. Grimes 
170960173b9SRodney W. Grimes #if NNPX > 0
1715b81b6b3SRodney W. Grimes 	npxexit(p);
172960173b9SRodney W. Grimes #endif	/* NNPX */
1735b81b6b3SRodney W. Grimes 	splclock();
1747f8cb368SDavid Greenman 	curproc = 0;
1755b81b6b3SRodney W. Grimes 	swtch();
1767c2b54e8SNate Williams 	/*
1777c2b54e8SNate Williams 	 * This is to shutup the compiler, and if swtch() failed I suppose
1787c2b54e8SNate Williams 	 * this would be a good thing.  This keeps gcc happy because panic
1797c2b54e8SNate Williams 	 * is a volatile void function as well.
1807c2b54e8SNate Williams 	 */
1817c2b54e8SNate Williams 	panic("cpu_exit");
1825b81b6b3SRodney W. Grimes }
1835b81b6b3SRodney W. Grimes 
184381fe1aaSGarrett Wollman void
1857f8cb368SDavid Greenman cpu_wait(p) struct proc *p; {
1867f8cb368SDavid Greenman /*	extern vm_map_t upages_map; */
1877f8cb368SDavid Greenman 	extern char kstack[];
1885b81b6b3SRodney W. Grimes 
1895b81b6b3SRodney W. Grimes 	/* drop per-process resources */
1907f8cb368SDavid Greenman  	pmap_remove(vm_map_pmap(kernel_map), (vm_offset_t) p->p_addr,
1917f8cb368SDavid Greenman 		((vm_offset_t) p->p_addr) + ctob(UPAGES));
1925b81b6b3SRodney W. Grimes 	kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES));
1937f8cb368SDavid Greenman 	vmspace_free(p->p_vmspace);
1945b81b6b3SRodney W. Grimes }
1955b81b6b3SRodney W. Grimes #endif
1965b81b6b3SRodney W. Grimes 
1975b81b6b3SRodney W. Grimes /*
1985b81b6b3SRodney W. Grimes  * Set a red zone in the kernel stack after the u. area.
1995b81b6b3SRodney W. Grimes  */
200381fe1aaSGarrett Wollman void
2015b81b6b3SRodney W. Grimes setredzone(pte, vaddr)
2025b81b6b3SRodney W. Grimes 	u_short *pte;
2035b81b6b3SRodney W. Grimes 	caddr_t vaddr;
2045b81b6b3SRodney W. Grimes {
2055b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment
2065b81b6b3SRodney W. Grimes    for ss0: selector, allowing stack access down to top of u.
2075b81b6b3SRodney W. Grimes    this means though that protection violations need to be handled
2085b81b6b3SRodney W. Grimes    thru a double fault exception that must do an integral task
2095b81b6b3SRodney W. Grimes    switch to a known good context, within which a dump can be
2105b81b6b3SRodney W. Grimes    taken. a sensible scheme might be to save the initial context
2115b81b6b3SRodney W. Grimes    used by sched (that has physical memory mapped 1:1 at bottom)
2125b81b6b3SRodney W. Grimes    and take the dump while still in mapped mode */
2135b81b6b3SRodney W. Grimes }
2145b81b6b3SRodney W. Grimes 
2155b81b6b3SRodney W. Grimes /*
2165b81b6b3SRodney W. Grimes  * Move pages from one kernel virtual address to another.
2175b81b6b3SRodney W. Grimes  * Both addresses are assumed to reside in the Sysmap,
2185b81b6b3SRodney W. Grimes  * and size must be a multiple of CLSIZE.
2195b81b6b3SRodney W. Grimes  */
220381fe1aaSGarrett Wollman void
2215b81b6b3SRodney W. Grimes pagemove(from, to, size)
2225b81b6b3SRodney W. Grimes 	register caddr_t from, to;
2235b81b6b3SRodney W. Grimes 	int size;
2245b81b6b3SRodney W. Grimes {
2255b81b6b3SRodney W. Grimes 	register struct pte *fpte, *tpte;
2265b81b6b3SRodney W. Grimes 
2275b81b6b3SRodney W. Grimes 	if (size % CLBYTES)
2285b81b6b3SRodney W. Grimes 		panic("pagemove");
2295b81b6b3SRodney W. Grimes 	fpte = kvtopte(from);
2305b81b6b3SRodney W. Grimes 	tpte = kvtopte(to);
2315b81b6b3SRodney W. Grimes 	while (size > 0) {
2325b81b6b3SRodney W. Grimes 		*tpte++ = *fpte;
2335b81b6b3SRodney W. Grimes 		*(int *)fpte++ = 0;
2345b81b6b3SRodney W. Grimes 		from += NBPG;
2355b81b6b3SRodney W. Grimes 		to += NBPG;
2365b81b6b3SRodney W. Grimes 		size -= NBPG;
2375b81b6b3SRodney W. Grimes 	}
2385b81b6b3SRodney W. Grimes 	tlbflush();
2395b81b6b3SRodney W. Grimes }
2405b81b6b3SRodney W. Grimes 
2415b81b6b3SRodney W. Grimes /*
2425b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
2435b81b6b3SRodney W. Grimes  */
244aaf08d94SGarrett Wollman u_long
2457f8cb368SDavid Greenman kvtop(void *addr)
2465b81b6b3SRodney W. Grimes {
2475b81b6b3SRodney W. Grimes 	vm_offset_t va;
2485b81b6b3SRodney W. Grimes 
2495b81b6b3SRodney W. Grimes 	va = pmap_extract(kernel_pmap, (vm_offset_t)addr);
2505b81b6b3SRodney W. Grimes 	if (va == 0)
2515b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
2527f8cb368SDavid Greenman 	return((int)va);
2535b81b6b3SRodney W. Grimes }
2545b81b6b3SRodney W. Grimes 
2555b81b6b3SRodney W. Grimes #ifdef notdef
2565b81b6b3SRodney W. Grimes /*
2575b81b6b3SRodney W. Grimes  * The probe[rw] routines should probably be redone in assembler
2585b81b6b3SRodney W. Grimes  * for efficiency.
2595b81b6b3SRodney W. Grimes  */
2605b81b6b3SRodney W. Grimes prober(addr)
2615b81b6b3SRodney W. Grimes 	register u_int addr;
2625b81b6b3SRodney W. Grimes {
2635b81b6b3SRodney W. Grimes 	register int page;
2645b81b6b3SRodney W. Grimes 	register struct proc *p;
2655b81b6b3SRodney W. Grimes 
2665b81b6b3SRodney W. Grimes 	if (addr >= USRSTACK)
2675b81b6b3SRodney W. Grimes 		return(0);
2685b81b6b3SRodney W. Grimes 	p = u.u_procp;
2695b81b6b3SRodney W. Grimes 	page = btop(addr);
2705b81b6b3SRodney W. Grimes 	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
2715b81b6b3SRodney W. Grimes 		return(1);
2725b81b6b3SRodney W. Grimes 	return(0);
2735b81b6b3SRodney W. Grimes }
2745b81b6b3SRodney W. Grimes 
2755b81b6b3SRodney W. Grimes probew(addr)
2765b81b6b3SRodney W. Grimes 	register u_int addr;
2775b81b6b3SRodney W. Grimes {
2785b81b6b3SRodney W. Grimes 	register int page;
2795b81b6b3SRodney W. Grimes 	register struct proc *p;
2805b81b6b3SRodney W. Grimes 
2815b81b6b3SRodney W. Grimes 	if (addr >= USRSTACK)
2825b81b6b3SRodney W. Grimes 		return(0);
2835b81b6b3SRodney W. Grimes 	p = u.u_procp;
2845b81b6b3SRodney W. Grimes 	page = btop(addr);
2855b81b6b3SRodney W. Grimes 	if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize))
2865b81b6b3SRodney W. Grimes 		return((*(int *)vtopte(p, page) & PG_PROT) == PG_UW);
2875b81b6b3SRodney W. Grimes 	return(0);
2885b81b6b3SRodney W. Grimes }
2895b81b6b3SRodney W. Grimes 
2905b81b6b3SRodney W. Grimes /*
2915b81b6b3SRodney W. Grimes  * NB: assumes a physically contiguous kernel page table
2925b81b6b3SRodney W. Grimes  *     (makes life a LOT simpler).
2935b81b6b3SRodney W. Grimes  */
2947f8cb368SDavid Greenman int
2955b81b6b3SRodney W. Grimes kernacc(addr, count, rw)
2965b81b6b3SRodney W. Grimes 	register u_int addr;
2975b81b6b3SRodney W. Grimes 	int count, rw;
2985b81b6b3SRodney W. Grimes {
2995b81b6b3SRodney W. Grimes 	register struct pde *pde;
3005b81b6b3SRodney W. Grimes 	register struct pte *pte;
3015b81b6b3SRodney W. Grimes 	register int ix, cnt;
3025b81b6b3SRodney W. Grimes 	extern long Syssize;
3035b81b6b3SRodney W. Grimes 
3045b81b6b3SRodney W. Grimes 	if (count <= 0)
3055b81b6b3SRodney W. Grimes 		return(0);
3065b81b6b3SRodney W. Grimes 	pde = (struct pde *)((u_int)u.u_procp->p_p0br + u.u_procp->p_szpt * NBPG);
3075b81b6b3SRodney W. Grimes 	ix = (addr & PD_MASK) >> PD_SHIFT;
3085b81b6b3SRodney W. Grimes 	cnt = ((addr + count + (1 << PD_SHIFT) - 1) & PD_MASK) >> PD_SHIFT;
3095b81b6b3SRodney W. Grimes 	cnt -= ix;
3105b81b6b3SRodney W. Grimes 	for (pde += ix; cnt; cnt--, pde++)
3115b81b6b3SRodney W. Grimes 		if (pde->pd_v == 0)
3125b81b6b3SRodney W. Grimes 			return(0);
313960173b9SRodney W. Grimes 	ix = btop(addr-KERNBASE);
314960173b9SRodney W. Grimes 	cnt = btop(addr-KERNBASE+count+NBPG-1);
3155b81b6b3SRodney W. Grimes 	if (cnt > (int)&Syssize)
3165b81b6b3SRodney W. Grimes 		return(0);
3175b81b6b3SRodney W. Grimes 	cnt -= ix;
3185b81b6b3SRodney W. Grimes 	for (pte = &Sysmap[ix]; cnt; cnt--, pte++)
3195b81b6b3SRodney W. Grimes 		if (pte->pg_v == 0 /*|| (rw == B_WRITE && pte->pg_prot == 1)*/)
3205b81b6b3SRodney W. Grimes 			return(0);
3215b81b6b3SRodney W. Grimes 	return(1);
3225b81b6b3SRodney W. Grimes }
3235b81b6b3SRodney W. Grimes 
3247f8cb368SDavid Greenman int
3255b81b6b3SRodney W. Grimes useracc(addr, count, rw)
3265b81b6b3SRodney W. Grimes 	register u_int addr;
3275b81b6b3SRodney W. Grimes 	int count, rw;
3285b81b6b3SRodney W. Grimes {
3295b81b6b3SRodney W. Grimes 	register int (*func)();
3305b81b6b3SRodney W. Grimes 	register u_int addr2;
3315b81b6b3SRodney W. Grimes 	extern int prober(), probew();
3325b81b6b3SRodney W. Grimes 
3335b81b6b3SRodney W. Grimes 	if (count <= 0)
3345b81b6b3SRodney W. Grimes 		return(0);
3355b81b6b3SRodney W. Grimes 	addr2 = addr;
3365b81b6b3SRodney W. Grimes 	addr += count;
3375b81b6b3SRodney W. Grimes 	func = (rw == B_READ) ? prober : probew;
3385b81b6b3SRodney W. Grimes 	do {
3395b81b6b3SRodney W. Grimes 		if ((*func)(addr2) == 0)
3405b81b6b3SRodney W. Grimes 			return(0);
3415b81b6b3SRodney W. Grimes 		addr2 = (addr2 + NBPG) & ~PGOFSET;
3425b81b6b3SRodney W. Grimes 	} while (addr2 < addr);
3435b81b6b3SRodney W. Grimes 	return(1);
3445b81b6b3SRodney W. Grimes }
3455b81b6b3SRodney W. Grimes #endif
3465b81b6b3SRodney W. Grimes 
3475b81b6b3SRodney W. Grimes extern vm_map_t phys_map;
3485b81b6b3SRodney W. Grimes 
3495b81b6b3SRodney W. Grimes /*
3505b81b6b3SRodney W. Grimes  * Map an IO request into kernel virtual address space.  Requests fall into
3515b81b6b3SRodney W. Grimes  * one of five catagories:
3525b81b6b3SRodney W. Grimes  *
3535b81b6b3SRodney W. Grimes  *	B_PHYS|B_UAREA:	User u-area swap.
3545b81b6b3SRodney W. Grimes  *			Address is relative to start of u-area (p_addr).
3555b81b6b3SRodney W. Grimes  *	B_PHYS|B_PAGET:	User page table swap.
3565b81b6b3SRodney W. Grimes  *			Address is a kernel VA in usrpt (Usrptmap).
3575b81b6b3SRodney W. Grimes  *	B_PHYS|B_DIRTY:	Dirty page push.
3585b81b6b3SRodney W. Grimes  *			Address is a VA in proc2's address space.
3595b81b6b3SRodney W. Grimes  *	B_PHYS|B_PGIN:	Kernel pagein of user pages.
3605b81b6b3SRodney W. Grimes  *			Address is VA in user's address space.
3615b81b6b3SRodney W. Grimes  *	B_PHYS:		User "raw" IO request.
3625b81b6b3SRodney W. Grimes  *			Address is VA in user's address space.
3635b81b6b3SRodney W. Grimes  *
3645b81b6b3SRodney W. Grimes  * All requests are (re)mapped into kernel VA space via the useriomap
3655b81b6b3SRodney W. Grimes  * (a name with only slightly more meaning than "kernelmap")
3665b81b6b3SRodney W. Grimes  */
367381fe1aaSGarrett Wollman void
3685b81b6b3SRodney W. Grimes vmapbuf(bp)
3695b81b6b3SRodney W. Grimes 	register struct buf *bp;
3705b81b6b3SRodney W. Grimes {
3715b81b6b3SRodney W. Grimes 	register int npf;
3725b81b6b3SRodney W. Grimes 	register caddr_t addr;
3735b81b6b3SRodney W. Grimes 	register long flags = bp->b_flags;
3745b81b6b3SRodney W. Grimes 	struct proc *p;
3755b81b6b3SRodney W. Grimes 	int off;
3765b81b6b3SRodney W. Grimes 	vm_offset_t kva;
3775b81b6b3SRodney W. Grimes 	register vm_offset_t pa;
3785b81b6b3SRodney W. Grimes 
3795b81b6b3SRodney W. Grimes 	if ((flags & B_PHYS) == 0)
3805b81b6b3SRodney W. Grimes 		panic("vmapbuf");
3815b81b6b3SRodney W. Grimes 	addr = bp->b_saveaddr = bp->b_un.b_addr;
3825b81b6b3SRodney W. Grimes 	off = (int)addr & PGOFSET;
3835b81b6b3SRodney W. Grimes 	p = bp->b_proc;
3845b81b6b3SRodney W. Grimes 	npf = btoc(round_page(bp->b_bcount + off));
3855b81b6b3SRodney W. Grimes 	kva = kmem_alloc_wait(phys_map, ctob(npf));
3865b81b6b3SRodney W. Grimes 	bp->b_un.b_addr = (caddr_t) (kva + off);
3875b81b6b3SRodney W. Grimes 	while (npf--) {
3885b81b6b3SRodney W. Grimes 		pa = pmap_extract(&p->p_vmspace->vm_pmap, (vm_offset_t)addr);
3895b81b6b3SRodney W. Grimes 		if (pa == 0)
3905b81b6b3SRodney W. Grimes 			panic("vmapbuf: null page frame");
3915b81b6b3SRodney W. Grimes 		pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa),
3925b81b6b3SRodney W. Grimes 			   VM_PROT_READ|VM_PROT_WRITE, TRUE);
3935b81b6b3SRodney W. Grimes 		addr += PAGE_SIZE;
3945b81b6b3SRodney W. Grimes 		kva += PAGE_SIZE;
3955b81b6b3SRodney W. Grimes 	}
3965b81b6b3SRodney W. Grimes }
3975b81b6b3SRodney W. Grimes 
3985b81b6b3SRodney W. Grimes /*
3995b81b6b3SRodney W. Grimes  * Free the io map PTEs associated with this IO operation.
4005b81b6b3SRodney W. Grimes  * We also invalidate the TLB entries and restore the original b_addr.
4015b81b6b3SRodney W. Grimes  */
402381fe1aaSGarrett Wollman void
4035b81b6b3SRodney W. Grimes vunmapbuf(bp)
4045b81b6b3SRodney W. Grimes 	register struct buf *bp;
4055b81b6b3SRodney W. Grimes {
4065b81b6b3SRodney W. Grimes 	register int npf;
4075b81b6b3SRodney W. Grimes 	register caddr_t addr = bp->b_un.b_addr;
4085b81b6b3SRodney W. Grimes 	vm_offset_t kva;
4095b81b6b3SRodney W. Grimes 
4105b81b6b3SRodney W. Grimes 	if ((bp->b_flags & B_PHYS) == 0)
4115b81b6b3SRodney W. Grimes 		panic("vunmapbuf");
4125b81b6b3SRodney W. Grimes 	npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET)));
4135b81b6b3SRodney W. Grimes 	kva = (vm_offset_t)((int)addr & ~PGOFSET);
4145b81b6b3SRodney W. Grimes 	kmem_free_wakeup(phys_map, kva, ctob(npf));
4155b81b6b3SRodney W. Grimes 	bp->b_un.b_addr = bp->b_saveaddr;
4165b81b6b3SRodney W. Grimes 	bp->b_saveaddr = NULL;
4175b81b6b3SRodney W. Grimes }
4185b81b6b3SRodney W. Grimes 
4195b81b6b3SRodney W. Grimes /*
4205b81b6b3SRodney W. Grimes  * Force reset the processor by invalidating the entire address space!
4215b81b6b3SRodney W. Grimes  */
4227f8cb368SDavid Greenman void
4235b81b6b3SRodney W. Grimes cpu_reset() {
4245b81b6b3SRodney W. Grimes 
4255b81b6b3SRodney W. Grimes 	/* force a shutdown by unmapping entire address space ! */
4265b81b6b3SRodney W. Grimes 	bzero((caddr_t) PTD, NBPG);
4275b81b6b3SRodney W. Grimes 
4285b81b6b3SRodney W. Grimes 	/* "good night, sweet prince .... <THUNK!>" */
4295b81b6b3SRodney W. Grimes 	tlbflush();
4305b81b6b3SRodney W. Grimes 	/* NOTREACHED */
4317f8cb368SDavid Greenman 	while(1);
4325b81b6b3SRodney W. Grimes }
433