vm_machdep.c revision 200
1/*- 2 * Copyright (c) 1982, 1986 The Regents of the University of California. 3 * Copyright (c) 1989, 1990 William Jolitz 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department, and William Jolitz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 39 * 40 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE 41 * -------------------- ----- ---------------------- 42 * CURRENT PATCH LEVEL: 1 00154 43 * -------------------- ----- ---------------------- 44 * 45 * 20 Apr 93 Bruce Evans New npx-0.5 code 46 * 47 */ 48 49/* 50 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 51 */ 52static char rcsid[] = "$Header: /a/cvs/386BSD/src/sys/i386/i386/vm_machdep.c,v 1.2 1993/07/18 20:56:17 paul Exp $"; 53 54#include "param.h" 55#include "systm.h" 56#include "proc.h" 57#include "malloc.h" 58#include "buf.h" 59#include "user.h" 60 61#include "../include/cpu.h" 62 63#include "vm/vm.h" 64#include "vm/vm_kern.h" 65 66/* 67 * Finish a fork operation, with process p2 nearly set up. 68 * Copy and update the kernel stack and pcb, making the child 69 * ready to run, and marking it so that it can return differently 70 * than the parent. Returns 1 in the child process, 0 in the parent. 71 * We currently double-map the user area so that the stack is at the same 72 * address in each process; in the future we will probably relocate 73 * the frame pointers on the stack after copying. 74 */ 75cpu_fork(p1, p2) 76 register struct proc *p1, *p2; 77{ 78 register struct user *up = p2->p_addr; 79 int foo, offset, addr, i; 80 extern char kstack[]; 81 extern int mvesp(); 82 83 /* 84 * Copy pcb and stack from proc p1 to p2. 85 * We do this as cheaply as possible, copying only the active 86 * part of the stack. The stack and pcb need to agree; 87 * this is tricky, as the final pcb is constructed by savectx, 88 * but its frame isn't yet on the stack when the stack is copied. 89 * swtch compensates for this when the child eventually runs. 90 * This should be done differently, with a single call 91 * that copies and updates the pcb+stack, 92 * replacing the bcopy and savectx. 93 */ 94 p2->p_addr->u_pcb = p1->p_addr->u_pcb; 95 offset = mvesp() - (int)kstack; 96 bcopy((caddr_t)kstack + offset, (caddr_t)p2->p_addr + offset, 97 (unsigned) ctob(UPAGES) - offset); 98 p2->p_regs = p1->p_regs; 99 100 /* 101 * Wire top of address space of child to it's kstack. 102 * First, fault in a page of pte's to map it. 103 */ 104 addr = trunc_page((u_int)vtopte(kstack)); 105 vm_map_pageable(&p2->p_vmspace->vm_map, addr, addr+NBPG, FALSE); 106 for (i=0; i < UPAGES; i++) 107 pmap_enter(&p2->p_vmspace->vm_pmap, kstack+i*NBPG, 108 pmap_extract(kernel_pmap, ((int)p2->p_addr)+i*NBPG), 109 /* 110 * The user area has to be mapped writable because 111 * it contains the kernel stack (when CR0_WP is on 112 * on a 486 there is no user-read/kernel-write 113 * mode). It is protected from user mode access 114 * by the segment limits. 115 */ 116 VM_PROT_READ|VM_PROT_WRITE, TRUE); 117 pmap_activate(&p2->p_vmspace->vm_pmap, &up->u_pcb); 118 119 /* 120 * 121 * Arrange for a non-local goto when the new process 122 * is started, to resume here, returning nonzero from setjmp. 123 */ 124 if (savectx(up, 1)) { 125 /* 126 * Return 1 in child. 127 */ 128 return (1); 129 } 130 return (0); 131} 132 133#ifdef notyet 134/* 135 * cpu_exit is called as the last action during exit. 136 * 137 * We change to an inactive address space and a "safe" stack, 138 * passing thru an argument to the new stack. Now, safely isolated 139 * from the resources we're shedding, we release the address space 140 * and any remaining machine-dependent resources, including the 141 * memory for the user structure and kernel stack. 142 * 143 * Next, we assign a dummy context to be written over by swtch, 144 * calling it to send this process off to oblivion. 145 * [The nullpcb allows us to minimize cost in swtch() by not having 146 * a special case]. 147 */ 148struct proc *swtch_to_inactive(); 149volatile void 150cpu_exit(p) 151 register struct proc *p; 152{ 153 static struct pcb nullpcb; /* pcb to overwrite on last swtch */ 154 155#ifdef NPX 156 npxexit(p); 157#endif 158 159 /* move to inactive space and stack, passing arg accross */ 160 p = swtch_to_inactive(p); 161 162 /* drop per-process resources */ 163 vmspace_free(p->p_vmspace); 164 kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES)); 165 166 p->p_addr = (struct user *) &nullpcb; 167 splclock(); 168 swtch(); 169 /* NOTREACHED */ 170} 171#else 172volatile void 173cpu_exit(p) 174 register struct proc *p; 175{ 176 177#ifdef NPX 178 npxexit(p); 179#endif 180 splclock(); 181 swtch(); 182} 183 184cpu_wait(p) struct proc *p; { 185 186 /* drop per-process resources */ 187 vmspace_free(p->p_vmspace); 188 kmem_free(kernel_map, (vm_offset_t)p->p_addr, ctob(UPAGES)); 189} 190#endif 191 192/* 193 * Set a red zone in the kernel stack after the u. area. 194 */ 195setredzone(pte, vaddr) 196 u_short *pte; 197 caddr_t vaddr; 198{ 199/* eventually do this by setting up an expand-down stack segment 200 for ss0: selector, allowing stack access down to top of u. 201 this means though that protection violations need to be handled 202 thru a double fault exception that must do an integral task 203 switch to a known good context, within which a dump can be 204 taken. a sensible scheme might be to save the initial context 205 used by sched (that has physical memory mapped 1:1 at bottom) 206 and take the dump while still in mapped mode */ 207} 208 209/* 210 * Move pages from one kernel virtual address to another. 211 * Both addresses are assumed to reside in the Sysmap, 212 * and size must be a multiple of CLSIZE. 213 */ 214pagemove(from, to, size) 215 register caddr_t from, to; 216 int size; 217{ 218 register struct pte *fpte, *tpte; 219 220 if (size % CLBYTES) 221 panic("pagemove"); 222 fpte = kvtopte(from); 223 tpte = kvtopte(to); 224 while (size > 0) { 225 *tpte++ = *fpte; 226 *(int *)fpte++ = 0; 227 from += NBPG; 228 to += NBPG; 229 size -= NBPG; 230 } 231 tlbflush(); 232} 233 234/* 235 * Convert kernel VA to physical address 236 */ 237kvtop(addr) 238 register caddr_t addr; 239{ 240 vm_offset_t va; 241 242 va = pmap_extract(kernel_pmap, (vm_offset_t)addr); 243 if (va == 0) 244 panic("kvtop: zero page frame"); 245 return((int)va); 246} 247 248#ifdef notdef 249/* 250 * The probe[rw] routines should probably be redone in assembler 251 * for efficiency. 252 */ 253prober(addr) 254 register u_int addr; 255{ 256 register int page; 257 register struct proc *p; 258 259 if (addr >= USRSTACK) 260 return(0); 261 p = u.u_procp; 262 page = btop(addr); 263 if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize)) 264 return(1); 265 return(0); 266} 267 268probew(addr) 269 register u_int addr; 270{ 271 register int page; 272 register struct proc *p; 273 274 if (addr >= USRSTACK) 275 return(0); 276 p = u.u_procp; 277 page = btop(addr); 278 if (page < dptov(p, p->p_dsize) || page > sptov(p, p->p_ssize)) 279 return((*(int *)vtopte(p, page) & PG_PROT) == PG_UW); 280 return(0); 281} 282 283/* 284 * NB: assumes a physically contiguous kernel page table 285 * (makes life a LOT simpler). 286 */ 287kernacc(addr, count, rw) 288 register u_int addr; 289 int count, rw; 290{ 291 register struct pde *pde; 292 register struct pte *pte; 293 register int ix, cnt; 294 extern long Syssize; 295 296 if (count <= 0) 297 return(0); 298 pde = (struct pde *)((u_int)u.u_procp->p_p0br + u.u_procp->p_szpt * NBPG); 299 ix = (addr & PD_MASK) >> PD_SHIFT; 300 cnt = ((addr + count + (1 << PD_SHIFT) - 1) & PD_MASK) >> PD_SHIFT; 301 cnt -= ix; 302 for (pde += ix; cnt; cnt--, pde++) 303 if (pde->pd_v == 0) 304 return(0); 305 ix = btop(addr-0xfe000000); 306 cnt = btop(addr-0xfe000000+count+NBPG-1); 307 if (cnt > (int)&Syssize) 308 return(0); 309 cnt -= ix; 310 for (pte = &Sysmap[ix]; cnt; cnt--, pte++) 311 if (pte->pg_v == 0 /*|| (rw == B_WRITE && pte->pg_prot == 1)*/) 312 return(0); 313 return(1); 314} 315 316useracc(addr, count, rw) 317 register u_int addr; 318 int count, rw; 319{ 320 register int (*func)(); 321 register u_int addr2; 322 extern int prober(), probew(); 323 324 if (count <= 0) 325 return(0); 326 addr2 = addr; 327 addr += count; 328 func = (rw == B_READ) ? prober : probew; 329 do { 330 if ((*func)(addr2) == 0) 331 return(0); 332 addr2 = (addr2 + NBPG) & ~PGOFSET; 333 } while (addr2 < addr); 334 return(1); 335} 336#endif 337 338extern vm_map_t phys_map; 339 340/* 341 * Map an IO request into kernel virtual address space. Requests fall into 342 * one of five catagories: 343 * 344 * B_PHYS|B_UAREA: User u-area swap. 345 * Address is relative to start of u-area (p_addr). 346 * B_PHYS|B_PAGET: User page table swap. 347 * Address is a kernel VA in usrpt (Usrptmap). 348 * B_PHYS|B_DIRTY: Dirty page push. 349 * Address is a VA in proc2's address space. 350 * B_PHYS|B_PGIN: Kernel pagein of user pages. 351 * Address is VA in user's address space. 352 * B_PHYS: User "raw" IO request. 353 * Address is VA in user's address space. 354 * 355 * All requests are (re)mapped into kernel VA space via the useriomap 356 * (a name with only slightly more meaning than "kernelmap") 357 */ 358vmapbuf(bp) 359 register struct buf *bp; 360{ 361 register int npf; 362 register caddr_t addr; 363 register long flags = bp->b_flags; 364 struct proc *p; 365 int off; 366 vm_offset_t kva; 367 register vm_offset_t pa; 368 369 if ((flags & B_PHYS) == 0) 370 panic("vmapbuf"); 371 addr = bp->b_saveaddr = bp->b_un.b_addr; 372 off = (int)addr & PGOFSET; 373 p = bp->b_proc; 374 npf = btoc(round_page(bp->b_bcount + off)); 375 kva = kmem_alloc_wait(phys_map, ctob(npf)); 376 bp->b_un.b_addr = (caddr_t) (kva + off); 377 while (npf--) { 378 pa = pmap_extract(&p->p_vmspace->vm_pmap, (vm_offset_t)addr); 379 if (pa == 0) 380 panic("vmapbuf: null page frame"); 381 pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa), 382 VM_PROT_READ|VM_PROT_WRITE, TRUE); 383 addr += PAGE_SIZE; 384 kva += PAGE_SIZE; 385 } 386} 387 388/* 389 * Free the io map PTEs associated with this IO operation. 390 * We also invalidate the TLB entries and restore the original b_addr. 391 */ 392vunmapbuf(bp) 393 register struct buf *bp; 394{ 395 register int npf; 396 register caddr_t addr = bp->b_un.b_addr; 397 vm_offset_t kva; 398 399 if ((bp->b_flags & B_PHYS) == 0) 400 panic("vunmapbuf"); 401 npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET))); 402 kva = (vm_offset_t)((int)addr & ~PGOFSET); 403 kmem_free_wakeup(phys_map, kva, ctob(npf)); 404 bp->b_un.b_addr = bp->b_saveaddr; 405 bp->b_saveaddr = NULL; 406} 407 408/* 409 * Force reset the processor by invalidating the entire address space! 410 */ 411cpu_reset() { 412 413 /* force a shutdown by unmapping entire address space ! */ 414 bzero((caddr_t) PTD, NBPG); 415 416 /* "good night, sweet prince .... <THUNK!>" */ 417 tlbflush(); 418 /* NOTREACHED */ 419} 420