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$ 417762bc7bSBruce Evans * $Id: vm_machdep.c,v 1.101 1998/02/25 03:56:09 dyson 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" 488890984dSGarrett Wollman 4926f9a767SRodney W. Grimes #include <sys/param.h> 5026f9a767SRodney W. Grimes #include <sys/systm.h> 5126f9a767SRodney W. Grimes #include <sys/proc.h> 5226f9a767SRodney W. Grimes #include <sys/malloc.h> 5326f9a767SRodney W. Grimes #include <sys/buf.h> 5426f9a767SRodney W. Grimes #include <sys/vnode.h> 55efeaf95aSDavid Greenman #include <sys/vmmeter.h> 5666095752SJohn Dyson #include <sys/kernel.h> 5766095752SJohn Dyson #include <sys/sysctl.h> 585b81b6b3SRodney W. Grimes 592320728fSRodney W. Grimes #include <machine/clock.h> 60a2a1c95cSPeter Wemm #include <machine/cpu.h> 611f8745a9SPeter Wemm #include <machine/md_var.h> 628fa40736SBruce Evans #ifdef SMP 638fa40736SBruce Evans #include <machine/smp.h> 648fa40736SBruce Evans #endif 655b81b6b3SRodney W. Grimes 6626f9a767SRodney W. Grimes #include <vm/vm.h> 67efeaf95aSDavid Greenman #include <vm/vm_param.h> 68efeaf95aSDavid Greenman #include <vm/vm_prot.h> 69996c772fSJohn Dyson #include <sys/lock.h> 7026f9a767SRodney W. Grimes #include <vm/vm_kern.h> 7124a1cce3SDavid Greenman #include <vm/vm_page.h> 72efeaf95aSDavid Greenman #include <vm/vm_map.h> 73efeaf95aSDavid Greenman #include <vm/vm_extern.h> 74efeaf95aSDavid Greenman 75efeaf95aSDavid Greenman #include <sys/user.h> 765b81b6b3SRodney W. Grimes 77e30f0011SSatoshi Asami #ifdef PC98 78e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h> 79e30f0011SSatoshi Asami #else 802320728fSRodney W. Grimes #include <i386/isa/isa.h> 81e30f0011SSatoshi Asami #endif 822320728fSRodney W. Grimes 839aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS 8487b91157SPoul-Henning Kamp static vm_offset_t 856c146e28SBruce Evans vm_bounce_kva __P((int size, int waitok)); 8687b91157SPoul-Henning Kamp static void vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size, 876c146e28SBruce Evans int now)); 8887b91157SPoul-Henning Kamp static vm_offset_t 896c146e28SBruce Evans vm_bounce_page_find __P((int count)); 9087b91157SPoul-Henning Kamp static void vm_bounce_page_free __P((vm_offset_t pa, int count)); 916c146e28SBruce Evans 9287b6de2bSPoul-Henning Kamp static volatile int kvasfreecnt; 93ae92ea44SDavid Greenman 94d5e26ef0SDavid Greenman caddr_t bouncememory; 9587b91157SPoul-Henning Kamp static int bpwait; 9687b91157SPoul-Henning Kamp static vm_offset_t *bouncepa; 9787b91157SPoul-Henning Kamp static int bmwait, bmfreeing; 98d5e26ef0SDavid Greenman 99ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned)) 10087b91157SPoul-Henning Kamp static int bounceallocarraysize; 10187b91157SPoul-Henning Kamp static unsigned *bounceallocarray; 10287b91157SPoul-Henning Kamp static int bouncefree; 103d5e26ef0SDavid Greenman 104e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA) 105e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096) /* 15MB boundary */ 106e30f0011SSatoshi Asami #else 107d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096) 108e30f0011SSatoshi Asami #endif 109ae92ea44SDavid Greenman #define MAXBKVA 1024 110f8845af0SPoul-Henning Kamp int maxbkva = MAXBKVA*PAGE_SIZE; 111d5e26ef0SDavid Greenman 112d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */ 11387b91157SPoul-Henning Kamp static struct kvasfree { 114d5e26ef0SDavid Greenman vm_offset_t addr; 115d5e26ef0SDavid Greenman vm_offset_t size; 116d5e26ef0SDavid Greenman } kvaf[MAXBKVA]; 117d5e26ef0SDavid Greenman 118d5e26ef0SDavid Greenman /* 119d5e26ef0SDavid Greenman * get bounce buffer pages (count physically contiguous) 120d5e26ef0SDavid Greenman * (only 1 inplemented now) 121d5e26ef0SDavid Greenman */ 12287b91157SPoul-Henning Kamp static vm_offset_t 123d5e26ef0SDavid Greenman vm_bounce_page_find(count) 124d5e26ef0SDavid Greenman int count; 125d5e26ef0SDavid Greenman { 126d5e26ef0SDavid Greenman int bit; 127d5e26ef0SDavid Greenman int s,i; 128d5e26ef0SDavid Greenman 129d5e26ef0SDavid Greenman if (count != 1) 130d5e26ef0SDavid Greenman panic("vm_bounce_page_find -- no support for > 1 page yet!!!"); 131d5e26ef0SDavid Greenman 132d5e26ef0SDavid Greenman s = splbio(); 133d5e26ef0SDavid Greenman retry: 134d5e26ef0SDavid Greenman for (i = 0; i < bounceallocarraysize; i++) { 135d5e26ef0SDavid Greenman if (bounceallocarray[i] != 0xffffffff) { 1363fb3086eSPoul-Henning Kamp bit = ffs(~bounceallocarray[i]); 1373fb3086eSPoul-Henning Kamp if (bit) { 138d5e26ef0SDavid Greenman bounceallocarray[i] |= 1 << (bit - 1) ; 139d5e26ef0SDavid Greenman bouncefree -= count; 140d5e26ef0SDavid Greenman splx(s); 141ae92ea44SDavid Greenman return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))]; 142d5e26ef0SDavid Greenman } 143d5e26ef0SDavid Greenman } 144d5e26ef0SDavid Greenman } 145ed7fcbd0SDavid Greenman bpwait = 1; 146d5e26ef0SDavid Greenman tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0); 147d5e26ef0SDavid Greenman goto retry; 148d5e26ef0SDavid Greenman } 149d5e26ef0SDavid Greenman 15087b91157SPoul-Henning Kamp static void 1511561d038SDavid Greenman vm_bounce_kva_free(addr, size, now) 1521561d038SDavid Greenman vm_offset_t addr; 1531561d038SDavid Greenman vm_offset_t size; 1541561d038SDavid Greenman int now; 1551561d038SDavid Greenman { 1561561d038SDavid Greenman int s = splbio(); 1571561d038SDavid Greenman kvaf[kvasfreecnt].addr = addr; 158ae92ea44SDavid Greenman kvaf[kvasfreecnt].size = size; 159ae92ea44SDavid Greenman ++kvasfreecnt; 1600e195446SDavid Greenman if( now) { 1610e195446SDavid Greenman /* 1620e195446SDavid Greenman * this will do wakeups 1630e195446SDavid Greenman */ 1641561d038SDavid Greenman vm_bounce_kva(0,0); 1650e195446SDavid Greenman } else { 1660e195446SDavid Greenman if (bmwait) { 1670e195446SDavid Greenman /* 1680e195446SDavid Greenman * if anyone is waiting on the bounce-map, then wakeup 1690e195446SDavid Greenman */ 1701561d038SDavid Greenman wakeup((caddr_t) io_map); 1710e195446SDavid Greenman bmwait = 0; 1720e195446SDavid Greenman } 1730e195446SDavid Greenman } 1741561d038SDavid Greenman splx(s); 1751561d038SDavid Greenman } 1761561d038SDavid Greenman 177d5e26ef0SDavid Greenman /* 178d5e26ef0SDavid Greenman * free count bounce buffer pages 179d5e26ef0SDavid Greenman */ 18087b91157SPoul-Henning Kamp static void 181d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count) 182d5e26ef0SDavid Greenman vm_offset_t pa; 183d5e26ef0SDavid Greenman int count; 184d5e26ef0SDavid Greenman { 185d5e26ef0SDavid Greenman int allocindex; 186d5e26ef0SDavid Greenman int index; 187d5e26ef0SDavid Greenman int bit; 188d5e26ef0SDavid Greenman 189d5e26ef0SDavid Greenman if (count != 1) 190edf8a815SDavid Greenman panic("vm_bounce_page_free -- no support for > 1 page yet!!!"); 191d5e26ef0SDavid Greenman 192ae92ea44SDavid Greenman for(index=0;index<bouncepages;index++) { 193ae92ea44SDavid Greenman if( pa == bouncepa[index]) 194ae92ea44SDavid Greenman break; 195ae92ea44SDavid Greenman } 196d5e26ef0SDavid Greenman 197ae92ea44SDavid Greenman if( index == bouncepages) 198ae92ea44SDavid Greenman panic("vm_bounce_page_free: invalid bounce buffer"); 199d5e26ef0SDavid Greenman 200ed7fcbd0SDavid Greenman allocindex = index / BITS_IN_UNSIGNED; 201ed7fcbd0SDavid Greenman bit = index % BITS_IN_UNSIGNED; 202d5e26ef0SDavid Greenman 203d5e26ef0SDavid Greenman bounceallocarray[allocindex] &= ~(1 << bit); 204d5e26ef0SDavid Greenman 205d5e26ef0SDavid Greenman bouncefree += count; 206ed7fcbd0SDavid Greenman if (bpwait) { 207ed7fcbd0SDavid Greenman bpwait = 0; 208d5e26ef0SDavid Greenman wakeup((caddr_t) &bounceallocarray); 209d5e26ef0SDavid Greenman } 210ed7fcbd0SDavid Greenman } 211d5e26ef0SDavid Greenman 212d5e26ef0SDavid Greenman /* 213d5e26ef0SDavid Greenman * allocate count bounce buffer kva pages 214d5e26ef0SDavid Greenman */ 21587b91157SPoul-Henning Kamp static vm_offset_t 216ae92ea44SDavid Greenman vm_bounce_kva(size, waitok) 217ae92ea44SDavid Greenman int size; 2181561d038SDavid Greenman int waitok; 219d5e26ef0SDavid Greenman { 220d5e26ef0SDavid Greenman int i; 2216b4ac811SDavid Greenman vm_offset_t kva = 0; 222c564966bSDavid Greenman vm_offset_t off; 223d5e26ef0SDavid Greenman int s = splbio(); 224d5e26ef0SDavid Greenman more: 225ae92ea44SDavid Greenman if (!bmfreeing && kvasfreecnt) { 226d5e26ef0SDavid Greenman bmfreeing = 1; 227ae92ea44SDavid Greenman for (i = 0; i < kvasfreecnt; i++) { 228f8845af0SPoul-Henning Kamp for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) { 229c564966bSDavid Greenman pmap_kremove( kvaf[i].addr + off); 230c564966bSDavid Greenman } 2311561d038SDavid Greenman kmem_free_wakeup(io_map, kvaf[i].addr, 232d5e26ef0SDavid Greenman kvaf[i].size); 233d5e26ef0SDavid Greenman } 234d5e26ef0SDavid Greenman kvasfreecnt = 0; 235d5e26ef0SDavid Greenman bmfreeing = 0; 236ae92ea44SDavid Greenman if( bmwait) { 237ae92ea44SDavid Greenman bmwait = 0; 238ae92ea44SDavid Greenman wakeup( (caddr_t) io_map); 239ae92ea44SDavid Greenman } 240d5e26ef0SDavid Greenman } 241d5e26ef0SDavid Greenman 2421561d038SDavid Greenman if( size == 0) { 2431561d038SDavid Greenman splx(s); 2446ab46d52SBruce Evans return 0; 2451561d038SDavid Greenman } 2461561d038SDavid Greenman 247ae92ea44SDavid Greenman if ((kva = kmem_alloc_pageable(io_map, size)) == 0) { 2481561d038SDavid Greenman if( !waitok) { 2491561d038SDavid Greenman splx(s); 2506ab46d52SBruce Evans return 0; 2511561d038SDavid Greenman } 252d5e26ef0SDavid Greenman bmwait = 1; 2531561d038SDavid Greenman tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0); 254d5e26ef0SDavid Greenman goto more; 255d5e26ef0SDavid Greenman } 256d5e26ef0SDavid Greenman splx(s); 257d5e26ef0SDavid Greenman return kva; 258d5e26ef0SDavid Greenman } 259d5e26ef0SDavid Greenman 260d5e26ef0SDavid Greenman /* 261ae92ea44SDavid Greenman * same as vm_bounce_kva -- but really allocate (but takes pages as arg) 2620e195446SDavid Greenman */ 2630e195446SDavid Greenman vm_offset_t 2640e195446SDavid Greenman vm_bounce_kva_alloc(count) 2650e195446SDavid Greenman int count; 2660e195446SDavid Greenman { 2670e195446SDavid Greenman int i; 2680e195446SDavid Greenman vm_offset_t kva; 2690e195446SDavid Greenman vm_offset_t pa; 2700e195446SDavid Greenman if( bouncepages == 0) { 271f8845af0SPoul-Henning Kamp kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK); 2720e195446SDavid Greenman return kva; 2730e195446SDavid Greenman } 274f8845af0SPoul-Henning Kamp kva = vm_bounce_kva(count*PAGE_SIZE, 1); 2750e195446SDavid Greenman for(i=0;i<count;i++) { 2760e195446SDavid Greenman pa = vm_bounce_page_find(1); 277f8845af0SPoul-Henning Kamp pmap_kenter(kva + i * PAGE_SIZE, pa); 2780e195446SDavid Greenman } 2790e195446SDavid Greenman return kva; 2800e195446SDavid Greenman } 2810e195446SDavid Greenman 2820e195446SDavid Greenman /* 2830e195446SDavid Greenman * same as vm_bounce_kva_free -- but really free 2840e195446SDavid Greenman */ 2850e195446SDavid Greenman void 2860e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count) 2870e195446SDavid Greenman vm_offset_t kva; 2880e195446SDavid Greenman int count; 2890e195446SDavid Greenman { 2900e195446SDavid Greenman int i; 2910e195446SDavid Greenman vm_offset_t pa; 2920e195446SDavid Greenman if( bouncepages == 0) { 2930e195446SDavid Greenman free((caddr_t) kva, M_TEMP); 2940e195446SDavid Greenman return; 2950e195446SDavid Greenman } 2960e195446SDavid Greenman for(i = 0; i < count; i++) { 297f8845af0SPoul-Henning Kamp pa = pmap_kextract(kva + i * PAGE_SIZE); 2980e195446SDavid Greenman vm_bounce_page_free(pa, 1); 2990e195446SDavid Greenman } 300f8845af0SPoul-Henning Kamp vm_bounce_kva_free(kva, count*PAGE_SIZE, 0); 3010e195446SDavid Greenman } 3020e195446SDavid Greenman 3030e195446SDavid Greenman /* 304d5e26ef0SDavid Greenman * do the things necessary to the struct buf to implement 305d5e26ef0SDavid Greenman * bounce buffers... inserted before the disk sort 306d5e26ef0SDavid Greenman */ 307d5e26ef0SDavid Greenman void 308d5e26ef0SDavid Greenman vm_bounce_alloc(bp) 309d5e26ef0SDavid Greenman struct buf *bp; 310d5e26ef0SDavid Greenman { 311d5e26ef0SDavid Greenman int countvmpg; 312d5e26ef0SDavid Greenman vm_offset_t vastart, vaend; 313d5e26ef0SDavid Greenman vm_offset_t vapstart, vapend; 314d5e26ef0SDavid Greenman vm_offset_t va, kva; 315d5e26ef0SDavid Greenman vm_offset_t pa; 316d5e26ef0SDavid Greenman int dobounceflag = 0; 317d5e26ef0SDavid Greenman int i; 318d5e26ef0SDavid Greenman 319d5e26ef0SDavid Greenman if (bouncepages == 0) 320d5e26ef0SDavid Greenman return; 321d5e26ef0SDavid Greenman 322ae92ea44SDavid Greenman if (bp->b_flags & B_BOUNCE) { 323ae92ea44SDavid Greenman printf("vm_bounce_alloc: called recursively???\n"); 324ae92ea44SDavid Greenman return; 3251561d038SDavid Greenman } 3261561d038SDavid Greenman 327ae92ea44SDavid Greenman if (bp->b_bufsize < bp->b_bcount) { 3283fb3086eSPoul-Henning Kamp printf( 3293fb3086eSPoul-Henning Kamp "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n", 330ae92ea44SDavid Greenman bp->b_bufsize, bp->b_bcount); 331ae92ea44SDavid Greenman panic("vm_bounce_alloc"); 332ae92ea44SDavid Greenman } 333ae92ea44SDavid Greenman 334ae92ea44SDavid Greenman /* 335ae92ea44SDavid Greenman * This is not really necessary 336ae92ea44SDavid Greenman * if( bp->b_bufsize != bp->b_bcount) { 337ae92ea44SDavid Greenman * printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount); 338ae92ea44SDavid Greenman * } 339ae92ea44SDavid Greenman */ 340ae92ea44SDavid Greenman 341ae92ea44SDavid Greenman 34226f9a767SRodney W. Grimes vastart = (vm_offset_t) bp->b_data; 34326f9a767SRodney W. Grimes vaend = (vm_offset_t) bp->b_data + bp->b_bufsize; 344d5e26ef0SDavid Greenman 345bd7e5f99SJohn Dyson vapstart = trunc_page(vastart); 346bd7e5f99SJohn Dyson vapend = round_page(vaend); 347f8845af0SPoul-Henning Kamp countvmpg = (vapend - vapstart) / PAGE_SIZE; 348d5e26ef0SDavid Greenman 349d5e26ef0SDavid Greenman /* 350d5e26ef0SDavid Greenman * if any page is above 16MB, then go into bounce-buffer mode 351d5e26ef0SDavid Greenman */ 352d5e26ef0SDavid Greenman va = vapstart; 353d5e26ef0SDavid Greenman for (i = 0; i < countvmpg; i++) { 354ed7fcbd0SDavid Greenman pa = pmap_kextract(va); 355d5e26ef0SDavid Greenman if (pa >= SIXTEENMEG) 356d5e26ef0SDavid Greenman ++dobounceflag; 3570d94caffSDavid Greenman if( pa == 0) 3580d94caffSDavid Greenman panic("vm_bounce_alloc: Unmapped page"); 359f8845af0SPoul-Henning Kamp va += PAGE_SIZE; 360d5e26ef0SDavid Greenman } 361d5e26ef0SDavid Greenman if (dobounceflag == 0) 362d5e26ef0SDavid Greenman return; 363d5e26ef0SDavid Greenman 364d5e26ef0SDavid Greenman if (bouncepages < dobounceflag) 365d5e26ef0SDavid Greenman panic("Not enough bounce buffers!!!"); 366d5e26ef0SDavid Greenman 367d5e26ef0SDavid Greenman /* 368d5e26ef0SDavid Greenman * allocate a replacement kva for b_addr 369d5e26ef0SDavid Greenman */ 370f8845af0SPoul-Henning Kamp kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1); 371ae92ea44SDavid Greenman #if 0 372ae92ea44SDavid Greenman printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ", 373ae92ea44SDavid Greenman (bp->b_flags & B_READ) ? "read":"write", 374ae92ea44SDavid Greenman vapstart, vapend, countvmpg, kva); 375ae92ea44SDavid Greenman #endif 376d5e26ef0SDavid Greenman va = vapstart; 377d5e26ef0SDavid Greenman for (i = 0; i < countvmpg; i++) { 378ed7fcbd0SDavid Greenman pa = pmap_kextract(va); 379d5e26ef0SDavid Greenman if (pa >= SIXTEENMEG) { 380d5e26ef0SDavid Greenman /* 381d5e26ef0SDavid Greenman * allocate a replacement page 382d5e26ef0SDavid Greenman */ 383d5e26ef0SDavid Greenman vm_offset_t bpa = vm_bounce_page_find(1); 384f8845af0SPoul-Henning Kamp pmap_kenter(kva + (PAGE_SIZE * i), bpa); 385ae92ea44SDavid Greenman #if 0 386ae92ea44SDavid Greenman printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa); 387ae92ea44SDavid Greenman #endif 388d5e26ef0SDavid Greenman /* 389d5e26ef0SDavid Greenman * if we are writing, the copy the data into the page 390d5e26ef0SDavid Greenman */ 3911561d038SDavid Greenman if ((bp->b_flags & B_READ) == 0) { 392f8845af0SPoul-Henning Kamp bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE); 3931561d038SDavid Greenman } 394d5e26ef0SDavid Greenman } else { 395d5e26ef0SDavid Greenman /* 396d5e26ef0SDavid Greenman * use original page 397d5e26ef0SDavid Greenman */ 398f8845af0SPoul-Henning Kamp pmap_kenter(kva + (PAGE_SIZE * i), pa); 399d5e26ef0SDavid Greenman } 400f8845af0SPoul-Henning Kamp va += PAGE_SIZE; 401d5e26ef0SDavid Greenman } 402d5e26ef0SDavid Greenman 403d5e26ef0SDavid Greenman /* 404d5e26ef0SDavid Greenman * flag the buffer as being bounced 405d5e26ef0SDavid Greenman */ 406d5e26ef0SDavid Greenman bp->b_flags |= B_BOUNCE; 407d5e26ef0SDavid Greenman /* 408d5e26ef0SDavid Greenman * save the original buffer kva 409d5e26ef0SDavid Greenman */ 41026f9a767SRodney W. Grimes bp->b_savekva = bp->b_data; 411d5e26ef0SDavid Greenman /* 412d5e26ef0SDavid Greenman * put our new kva into the buffer (offset by original offset) 413d5e26ef0SDavid Greenman */ 41426f9a767SRodney W. Grimes bp->b_data = (caddr_t) (((vm_offset_t) kva) | 415f8845af0SPoul-Henning Kamp ((vm_offset_t) bp->b_savekva & PAGE_MASK)); 416ae92ea44SDavid Greenman #if 0 417ae92ea44SDavid Greenman printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data); 418ae92ea44SDavid Greenman #endif 419d5e26ef0SDavid Greenman return; 420d5e26ef0SDavid Greenman } 421d5e26ef0SDavid Greenman 422d5e26ef0SDavid Greenman /* 423d5e26ef0SDavid Greenman * hook into biodone to free bounce buffer 424d5e26ef0SDavid Greenman */ 425d5e26ef0SDavid Greenman void 426d5e26ef0SDavid Greenman vm_bounce_free(bp) 427d5e26ef0SDavid Greenman struct buf *bp; 428d5e26ef0SDavid Greenman { 429d5e26ef0SDavid Greenman int i; 430ae92ea44SDavid Greenman vm_offset_t origkva, bouncekva, bouncekvaend; 431d5e26ef0SDavid Greenman 432d5e26ef0SDavid Greenman /* 433d5e26ef0SDavid Greenman * if this isn't a bounced buffer, then just return 434d5e26ef0SDavid Greenman */ 435d5e26ef0SDavid Greenman if ((bp->b_flags & B_BOUNCE) == 0) 436d5e26ef0SDavid Greenman return; 437d5e26ef0SDavid Greenman 438ae92ea44SDavid Greenman /* 439ae92ea44SDavid Greenman * This check is not necessary 440ae92ea44SDavid Greenman * if (bp->b_bufsize != bp->b_bcount) { 441ae92ea44SDavid Greenman * printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n", 442ae92ea44SDavid Greenman * bp->b_bufsize, bp->b_bcount); 443ae92ea44SDavid Greenman * } 444ae92ea44SDavid Greenman */ 445ae92ea44SDavid Greenman 446d5e26ef0SDavid Greenman origkva = (vm_offset_t) bp->b_savekva; 44726f9a767SRodney W. Grimes bouncekva = (vm_offset_t) bp->b_data; 448ae92ea44SDavid Greenman /* 449ae92ea44SDavid Greenman printf("free: %d ", bp->b_bufsize); 450ae92ea44SDavid Greenman */ 451d5e26ef0SDavid Greenman 452d5e26ef0SDavid Greenman /* 453d5e26ef0SDavid Greenman * check every page in the kva space for b_addr 454d5e26ef0SDavid Greenman */ 455ae92ea44SDavid Greenman for (i = 0; i < bp->b_bufsize; ) { 456d5e26ef0SDavid Greenman vm_offset_t mybouncepa; 457d5e26ef0SDavid Greenman vm_offset_t copycount; 458d5e26ef0SDavid Greenman 459bd7e5f99SJohn Dyson copycount = round_page(bouncekva + 1) - bouncekva; 460bd7e5f99SJohn Dyson mybouncepa = pmap_kextract(trunc_page(bouncekva)); 461d5e26ef0SDavid Greenman 462d5e26ef0SDavid Greenman /* 463d5e26ef0SDavid Greenman * if this is a bounced pa, then process as one 464d5e26ef0SDavid Greenman */ 465bd7e5f99SJohn Dyson if ( mybouncepa != pmap_kextract( trunc_page( origkva))) { 466ae92ea44SDavid Greenman vm_offset_t tocopy = copycount; 467ae92ea44SDavid Greenman if (i + tocopy > bp->b_bufsize) 468ae92ea44SDavid Greenman tocopy = bp->b_bufsize - i; 469d5e26ef0SDavid Greenman /* 470d5e26ef0SDavid Greenman * if this is a read, then copy from bounce buffer into original buffer 471d5e26ef0SDavid Greenman */ 472d5e26ef0SDavid Greenman if (bp->b_flags & B_READ) 473ae92ea44SDavid Greenman bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy); 474d5e26ef0SDavid Greenman /* 475d5e26ef0SDavid Greenman * free the bounce allocation 476d5e26ef0SDavid Greenman */ 477ae92ea44SDavid Greenman 478ae92ea44SDavid Greenman /* 479ae92ea44SDavid Greenman printf("(kva: %x, pa: %x)", bouncekva, mybouncepa); 480ae92ea44SDavid Greenman */ 481ae92ea44SDavid Greenman vm_bounce_page_free(mybouncepa, 1); 482d5e26ef0SDavid Greenman } 483d5e26ef0SDavid Greenman 484d5e26ef0SDavid Greenman origkva += copycount; 485d5e26ef0SDavid Greenman bouncekva += copycount; 486ae92ea44SDavid Greenman i += copycount; 487d5e26ef0SDavid Greenman } 488d5e26ef0SDavid Greenman 489d5e26ef0SDavid Greenman /* 490ae92ea44SDavid Greenman printf("\n"); 491ae92ea44SDavid Greenman */ 492ae92ea44SDavid Greenman /* 493d5e26ef0SDavid Greenman * add the old kva into the "to free" list 494d5e26ef0SDavid Greenman */ 495ae92ea44SDavid Greenman 496bd7e5f99SJohn Dyson bouncekva= trunc_page((vm_offset_t) bp->b_data); 497bd7e5f99SJohn Dyson bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize); 498ae92ea44SDavid Greenman 499ae92ea44SDavid Greenman /* 500f8845af0SPoul-Henning Kamp printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE); 501ae92ea44SDavid Greenman */ 502ae92ea44SDavid Greenman vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0); 50326f9a767SRodney W. Grimes bp->b_data = bp->b_savekva; 504d5e26ef0SDavid Greenman bp->b_savekva = 0; 505d5e26ef0SDavid Greenman bp->b_flags &= ~B_BOUNCE; 506d5e26ef0SDavid Greenman 507d5e26ef0SDavid Greenman return; 508d5e26ef0SDavid Greenman } 509d5e26ef0SDavid Greenman 510ae92ea44SDavid Greenman 5115b81b6b3SRodney W. Grimes /* 5121561d038SDavid Greenman * init the bounce buffer system 5131561d038SDavid Greenman */ 5141561d038SDavid Greenman void 5151561d038SDavid Greenman vm_bounce_init() 5161561d038SDavid Greenman { 517ae92ea44SDavid Greenman int i; 5181561d038SDavid Greenman 5191561d038SDavid Greenman kvasfreecnt = 0; 5201561d038SDavid Greenman 5211561d038SDavid Greenman if (bouncepages == 0) 5221561d038SDavid Greenman return; 5231561d038SDavid Greenman 5241561d038SDavid Greenman bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED; 5251561d038SDavid Greenman bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT); 5261561d038SDavid Greenman 5271561d038SDavid Greenman if (!bounceallocarray) 528edf8a815SDavid Greenman panic("Cannot allocate bounce resource array"); 5291561d038SDavid Greenman 530ae92ea44SDavid Greenman bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT); 531ae92ea44SDavid Greenman if (!bouncepa) 532edf8a815SDavid Greenman panic("Cannot allocate physical memory array"); 5331561d038SDavid Greenman 5340d94caffSDavid Greenman for(i=0;i<bounceallocarraysize;i++) { 5350d94caffSDavid Greenman bounceallocarray[i] = 0xffffffff; 5360d94caffSDavid Greenman } 5370d94caffSDavid Greenman 538ae92ea44SDavid Greenman for(i=0;i<bouncepages;i++) { 539ae92ea44SDavid Greenman vm_offset_t pa; 5400621c31aSJohn Dyson if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) { 5410621c31aSJohn Dyson printf("vm_bounce_init: bounce memory out of range -- bounce disabled\n"); 5420621c31aSJohn Dyson free(bounceallocarray, M_TEMP); 5430621c31aSJohn Dyson bounceallocarray = NULL; 5440621c31aSJohn Dyson free(bouncepa, M_TEMP); 5450621c31aSJohn Dyson bouncepa = NULL; 5460621c31aSJohn Dyson bouncepages = 0; 5470621c31aSJohn Dyson break; 5480621c31aSJohn Dyson } 549ae92ea44SDavid Greenman if( pa == 0) 550ae92ea44SDavid Greenman panic("bounce memory not resident"); 551ae92ea44SDavid Greenman bouncepa[i] = pa; 5520d94caffSDavid Greenman bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int)))); 553ae92ea44SDavid Greenman } 5541561d038SDavid Greenman bouncefree = bouncepages; 5551561d038SDavid Greenman 5561561d038SDavid Greenman } 5579aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */ 5586c146e28SBruce Evans 559a4f7a4c9SDavid Greenman /* 560a4f7a4c9SDavid Greenman * quick version of vm_fault 561a4f7a4c9SDavid Greenman */ 562dca51299SJohn Dyson void 563a4f7a4c9SDavid Greenman vm_fault_quick(v, prot) 5646c146e28SBruce Evans caddr_t v; 565a4f7a4c9SDavid Greenman int prot; 566a4f7a4c9SDavid Greenman { 5671d1b971bSDavid Greenman if (prot & VM_PROT_WRITE) 5686c146e28SBruce Evans subyte(v, fubyte(v)); 569a4f7a4c9SDavid Greenman else 5706c146e28SBruce Evans fubyte(v); 571a4f7a4c9SDavid Greenman } 572a4f7a4c9SDavid Greenman 5731561d038SDavid Greenman /* 5745b81b6b3SRodney W. Grimes * Finish a fork operation, with process p2 nearly set up. 575a2a1c95cSPeter Wemm * Copy and update the pcb, set up the stack so that the child 576a2a1c95cSPeter Wemm * ready to run and return to user mode. 5775b81b6b3SRodney W. Grimes */ 578a2a1c95cSPeter Wemm void 5795b81b6b3SRodney W. Grimes cpu_fork(p1, p2) 5805b81b6b3SRodney W. Grimes register struct proc *p1, *p2; 5815b81b6b3SRodney W. Grimes { 582f83a01b6SDavid Greenman struct pcb *pcb2 = &p2->p_addr->u_pcb; 5835b81b6b3SRodney W. Grimes 5849f449d2aSBruce Evans #if NNPX > 0 5851f8745a9SPeter Wemm /* Ensure that p1's pcb is up to date. */ 5861f8745a9SPeter Wemm if (npxproc == p1) 5871f8745a9SPeter Wemm npxsave(&p1->p_addr->u_pcb.pcb_savefpu); 5889f449d2aSBruce Evans #endif 5891f8745a9SPeter Wemm 5901f8745a9SPeter Wemm /* Copy p1's pcb. */ 5911f8745a9SPeter Wemm p2->p_addr->u_pcb = p1->p_addr->u_pcb; 592a2a1c95cSPeter Wemm 593a2a1c95cSPeter Wemm /* 594a2a1c95cSPeter Wemm * Create a new fresh stack for the new process. 5951f8745a9SPeter Wemm * Copy the trap frame for the return to user mode as if from a 5961f8745a9SPeter Wemm * syscall. This copies the user mode register values. 597a2a1c95cSPeter Wemm */ 5981f8745a9SPeter Wemm p2->p_md.md_regs = (struct trapframe *) 5991f8745a9SPeter Wemm ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1; 6001f8745a9SPeter Wemm *p2->p_md.md_regs = *p1->p_md.md_regs; 601a2a1c95cSPeter Wemm 602a2a1c95cSPeter Wemm /* 603a2a1c95cSPeter Wemm * Set registers for trampoline to user mode. Leave space for the 604a2a1c95cSPeter Wemm * return address on stack. These are the kernel mode register values. 605a2a1c95cSPeter Wemm */ 6061f8745a9SPeter Wemm pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir); 6071f8745a9SPeter Wemm pcb2->pcb_edi = p2->p_md.md_regs->tf_edi; 608a2a1c95cSPeter Wemm pcb2->pcb_esi = (int)fork_return; 6091f8745a9SPeter Wemm pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp; 610a2a1c95cSPeter Wemm pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *); 6111f8745a9SPeter Wemm pcb2->pcb_ebx = (int)p2; 6121f8745a9SPeter Wemm pcb2->pcb_eip = (int)fork_trampoline; 6131f8745a9SPeter Wemm /* 6141f8745a9SPeter Wemm * pcb2->pcb_ldt: duplicated below, if necessary. 6151f8745a9SPeter Wemm * pcb2->pcb_ldt_len: cloned above. 6161f8745a9SPeter Wemm * pcb2->pcb_savefpu: cloned above. 6171f8745a9SPeter Wemm * pcb2->pcb_flags: cloned above (always 0 here?). 6181f8745a9SPeter Wemm * pcb2->pcb_onfault: cloned above (always NULL here?). 6191f8745a9SPeter Wemm */ 6205b81b6b3SRodney W. Grimes 62148a09cf2SJohn Dyson #ifdef VM86 62248a09cf2SJohn Dyson /* 62348a09cf2SJohn Dyson * XXX don't copy the i/o pages. this should probably be fixed. 62448a09cf2SJohn Dyson */ 62548a09cf2SJohn Dyson pcb2->pcb_ext = 0; 62648a09cf2SJohn Dyson #endif 62748a09cf2SJohn Dyson 6288c39a127SStefan Eßer #ifdef USER_LDT 6298c39a127SStefan Eßer /* Copy the LDT, if necessary. */ 630a2a1c95cSPeter Wemm if (pcb2->pcb_ldt != 0) { 6318c39a127SStefan Eßer union descriptor *new_ldt; 632a2a1c95cSPeter Wemm size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor); 6338c39a127SStefan Eßer 6348c39a127SStefan Eßer new_ldt = (union descriptor *)kmem_alloc(kernel_map, len); 635a2a1c95cSPeter Wemm bcopy(pcb2->pcb_ldt, new_ldt, len); 636a2a1c95cSPeter Wemm pcb2->pcb_ldt = (caddr_t)new_ldt; 6378c39a127SStefan Eßer } 6388c39a127SStefan Eßer #endif 6398c39a127SStefan Eßer 640a2a1c95cSPeter Wemm /* 641a2a1c95cSPeter Wemm * Now, cpu_switch() can schedule the new process. 642a2a1c95cSPeter Wemm * pcb_esp is loaded pointing to the cpu_switch() stack frame 643a2a1c95cSPeter Wemm * containing the return address when exiting cpu_switch. 644a2a1c95cSPeter Wemm * This will normally be to proc_trampoline(), which will have 645a2a1c95cSPeter Wemm * %ebx loaded with the new proc's pointer. proc_trampoline() 646a2a1c95cSPeter Wemm * will set up a stack to call fork_return(p, frame); to complete 647a2a1c95cSPeter Wemm * the return to user-mode. 648a2a1c95cSPeter Wemm */ 649a2a1c95cSPeter Wemm } 650a2a1c95cSPeter Wemm 651a2a1c95cSPeter Wemm /* 652a2a1c95cSPeter Wemm * Intercept the return address from a freshly forked process that has NOT 653a2a1c95cSPeter Wemm * been scheduled yet. 654a2a1c95cSPeter Wemm * 655a2a1c95cSPeter Wemm * This is needed to make kernel threads stay in kernel mode. 656a2a1c95cSPeter Wemm */ 657a2a1c95cSPeter Wemm void 658a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg) 659a2a1c95cSPeter Wemm struct proc *p; 660a2a1c95cSPeter Wemm void (*func) __P((void *)); 661a2a1c95cSPeter Wemm void *arg; 662a2a1c95cSPeter Wemm { 663a2a1c95cSPeter Wemm /* 664a2a1c95cSPeter Wemm * Note that the trap frame follows the args, so the function 665a2a1c95cSPeter Wemm * is really called like this: func(arg, frame); 666a2a1c95cSPeter Wemm */ 667a2a1c95cSPeter Wemm p->p_addr->u_pcb.pcb_esi = (int) func; /* function */ 668a2a1c95cSPeter Wemm p->p_addr->u_pcb.pcb_ebx = (int) arg; /* first arg */ 6695b81b6b3SRodney W. Grimes } 6705b81b6b3SRodney W. Grimes 6717c2b54e8SNate Williams void 6725b81b6b3SRodney W. Grimes cpu_exit(p) 6735b81b6b3SRodney W. Grimes register struct proc *p; 6745b81b6b3SRodney W. Grimes { 67548a09cf2SJohn Dyson #if defined(USER_LDT) || defined(VM86) 67648a09cf2SJohn Dyson struct pcb *pcb = &p->p_addr->u_pcb; 6770dbf6d73SJordan K. Hubbard #endif 6785b81b6b3SRodney W. Grimes 679960173b9SRodney W. Grimes #if NNPX > 0 6805b81b6b3SRodney W. Grimes npxexit(p); 681960173b9SRodney W. Grimes #endif /* NNPX */ 68248a09cf2SJohn Dyson #ifdef VM86 68348a09cf2SJohn Dyson if (pcb->pcb_ext != 0) { 68448a09cf2SJohn Dyson /* 68548a09cf2SJohn Dyson * XXX do we need to move the TSS off the allocated pages 68648a09cf2SJohn Dyson * before freeing them? (not done here) 68748a09cf2SJohn Dyson */ 68848a09cf2SJohn Dyson kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext, 68948a09cf2SJohn Dyson ctob(IOPAGES + 1)); 69048a09cf2SJohn Dyson pcb->pcb_ext = 0; 69148a09cf2SJohn Dyson } 69248a09cf2SJohn Dyson #endif 6930dbf6d73SJordan K. Hubbard #ifdef USER_LDT 6940dbf6d73SJordan K. Hubbard if (pcb->pcb_ldt != 0) { 6950dbf6d73SJordan K. Hubbard if (pcb == curpcb) 6960dbf6d73SJordan K. Hubbard lldt(GSEL(GUSERLDT_SEL, SEL_KPL)); 6970dbf6d73SJordan K. Hubbard kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt, 6980dbf6d73SJordan K. Hubbard pcb->pcb_ldt_len * sizeof(union descriptor)); 6990dbf6d73SJordan K. Hubbard pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0; 7000dbf6d73SJordan K. Hubbard } 7010dbf6d73SJordan K. Hubbard #endif 7021a051896SBruce Evans cnt.v_swtch++; 7031a051896SBruce Evans cpu_switch(p); 7047c2b54e8SNate Williams panic("cpu_exit"); 7055b81b6b3SRodney W. Grimes } 7065b81b6b3SRodney W. Grimes 707381fe1aaSGarrett Wollman void 708bd7e5f99SJohn Dyson cpu_wait(p) 709bd7e5f99SJohn Dyson struct proc *p; 710bd7e5f99SJohn Dyson { 7115b81b6b3SRodney W. Grimes /* drop per-process resources */ 712675878e7SJohn Dyson pmap_dispose_proc(p); 7132d8acc0fSJohn Dyson 7142d8acc0fSJohn Dyson /* and clean-out the vmspace */ 7157f8cb368SDavid Greenman vmspace_free(p->p_vmspace); 7165b81b6b3SRodney W. Grimes } 7175b81b6b3SRodney W. Grimes 7185b81b6b3SRodney W. Grimes /* 71926f9a767SRodney W. Grimes * Dump the machine specific header information at the start of a core dump. 72026f9a767SRodney W. Grimes */ 72126f9a767SRodney W. Grimes int 72226f9a767SRodney W. Grimes cpu_coredump(p, vp, cred) 72326f9a767SRodney W. Grimes struct proc *p; 72426f9a767SRodney W. Grimes struct vnode *vp; 72526f9a767SRodney W. Grimes struct ucred *cred; 72626f9a767SRodney W. Grimes { 72726f9a767SRodney W. Grimes 72826f9a767SRodney W. Grimes return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES), 72926f9a767SRodney W. Grimes (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, 73026f9a767SRodney W. Grimes p)); 73126f9a767SRodney W. Grimes } 73226f9a767SRodney W. Grimes 73387b6de2bSPoul-Henning Kamp #ifdef notyet 73487b91157SPoul-Henning Kamp static void 7355b81b6b3SRodney W. Grimes setredzone(pte, vaddr) 7365b81b6b3SRodney W. Grimes u_short *pte; 7375b81b6b3SRodney W. Grimes caddr_t vaddr; 7385b81b6b3SRodney W. Grimes { 7395b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment 7405b81b6b3SRodney W. Grimes for ss0: selector, allowing stack access down to top of u. 7415b81b6b3SRodney W. Grimes this means though that protection violations need to be handled 7425b81b6b3SRodney W. Grimes thru a double fault exception that must do an integral task 7435b81b6b3SRodney W. Grimes switch to a known good context, within which a dump can be 7445b81b6b3SRodney W. Grimes taken. a sensible scheme might be to save the initial context 7455b81b6b3SRodney W. Grimes used by sched (that has physical memory mapped 1:1 at bottom) 7465b81b6b3SRodney W. Grimes and take the dump while still in mapped mode */ 7475b81b6b3SRodney W. Grimes } 74887b6de2bSPoul-Henning Kamp #endif 74926f9a767SRodney W. Grimes 75026f9a767SRodney W. Grimes /* 7515b81b6b3SRodney W. Grimes * Convert kernel VA to physical address 7525b81b6b3SRodney W. Grimes */ 753aaf08d94SGarrett Wollman u_long 7547f8cb368SDavid Greenman kvtop(void *addr) 7555b81b6b3SRodney W. Grimes { 7565b81b6b3SRodney W. Grimes vm_offset_t va; 7575b81b6b3SRodney W. Grimes 758ed7fcbd0SDavid Greenman va = pmap_kextract((vm_offset_t)addr); 7595b81b6b3SRodney W. Grimes if (va == 0) 7605b81b6b3SRodney W. Grimes panic("kvtop: zero page frame"); 7617f8cb368SDavid Greenman return((int)va); 7625b81b6b3SRodney W. Grimes } 7635b81b6b3SRodney W. Grimes 7645b81b6b3SRodney W. Grimes /* 765ac322158SDavid Greenman * Map an IO request into kernel virtual address space. 7665b81b6b3SRodney W. Grimes * 767ac322158SDavid Greenman * All requests are (re)mapped into kernel VA space. 768ac322158SDavid Greenman * Notice that we use b_bufsize for the size of the buffer 769ac322158SDavid Greenman * to be mapped. b_bcount might be modified by the driver. 7705b81b6b3SRodney W. Grimes */ 771381fe1aaSGarrett Wollman void 7725b81b6b3SRodney W. Grimes vmapbuf(bp) 7735b81b6b3SRodney W. Grimes register struct buf *bp; 7745b81b6b3SRodney W. Grimes { 7750157d6d9SJohn Dyson register caddr_t addr, v, kva; 77607658526SPoul-Henning Kamp vm_offset_t pa; 7775b81b6b3SRodney W. Grimes 77826f9a767SRodney W. Grimes if ((bp->b_flags & B_PHYS) == 0) 7795b81b6b3SRodney W. Grimes panic("vmapbuf"); 78026f9a767SRodney W. Grimes 78112936855SDavid Greenman for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 78226f9a767SRodney W. Grimes addr < bp->b_data + bp->b_bufsize; 7830157d6d9SJohn Dyson addr += PAGE_SIZE, v += PAGE_SIZE) { 78426f9a767SRodney W. Grimes /* 785ac269d78SDavid Greenman * Do the vm_fault if needed; do the copy-on-write thing 786ac269d78SDavid Greenman * when reading stuff off device into memory. 78726f9a767SRodney W. Grimes */ 78826f9a767SRodney W. Grimes vm_fault_quick(addr, 78926f9a767SRodney W. Grimes (bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 7900157d6d9SJohn Dyson pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 791641a27e6SJohn Dyson if (pa == 0) 792641a27e6SJohn Dyson panic("vmapbuf: page not present"); 79326f9a767SRodney W. Grimes vm_page_hold(PHYS_TO_VM_PAGE(pa)); 7940157d6d9SJohn Dyson pmap_kenter((vm_offset_t) v, pa); 79526f9a767SRodney W. Grimes } 79626f9a767SRodney W. Grimes 7970157d6d9SJohn Dyson kva = bp->b_saveaddr; 7980157d6d9SJohn Dyson bp->b_saveaddr = bp->b_data; 7990157d6d9SJohn Dyson bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 8005b81b6b3SRodney W. Grimes } 8015b81b6b3SRodney W. Grimes 8025b81b6b3SRodney W. Grimes /* 8035b81b6b3SRodney W. Grimes * Free the io map PTEs associated with this IO operation. 8045b81b6b3SRodney W. Grimes * We also invalidate the TLB entries and restore the original b_addr. 8055b81b6b3SRodney W. Grimes */ 806381fe1aaSGarrett Wollman void 8075b81b6b3SRodney W. Grimes vunmapbuf(bp) 8085b81b6b3SRodney W. Grimes register struct buf *bp; 8095b81b6b3SRodney W. Grimes { 810c564966bSDavid Greenman register caddr_t addr; 81107658526SPoul-Henning Kamp vm_offset_t pa; 8125b81b6b3SRodney W. Grimes 8135b81b6b3SRodney W. Grimes if ((bp->b_flags & B_PHYS) == 0) 8145b81b6b3SRodney W. Grimes panic("vunmapbuf"); 815c564966bSDavid Greenman 81612936855SDavid Greenman for (addr = (caddr_t)trunc_page(bp->b_data); 81712936855SDavid Greenman addr < bp->b_data + bp->b_bufsize; 818f8845af0SPoul-Henning Kamp addr += PAGE_SIZE) { 8190157d6d9SJohn Dyson pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 8200157d6d9SJohn Dyson pmap_kremove((vm_offset_t) addr); 82126f9a767SRodney W. Grimes vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 82226f9a767SRodney W. Grimes } 8230157d6d9SJohn Dyson 8240157d6d9SJohn Dyson bp->b_data = bp->b_saveaddr; 8255b81b6b3SRodney W. Grimes } 8265b81b6b3SRodney W. Grimes 8275b81b6b3SRodney W. Grimes /* 8285b81b6b3SRodney W. Grimes * Force reset the processor by invalidating the entire address space! 8295b81b6b3SRodney W. Grimes */ 8307f8cb368SDavid Greenman void 831d447dbeeSBruce Evans cpu_reset() 832d447dbeeSBruce Evans { 833d447dbeeSBruce Evans 83403245f09SKATO Takenori #ifdef PC98 83503245f09SKATO Takenori /* 83603245f09SKATO Takenori * Attempt to do a CPU reset via CPU reset port. 83703245f09SKATO Takenori */ 838d447dbeeSBruce Evans disable_intr(); 83903245f09SKATO Takenori outb(0x37, 0x0f); /* SHUT0 = 0. */ 84003245f09SKATO Takenori outb(0x37, 0x0b); /* SHUT1 = 0. */ 84103245f09SKATO Takenori outb(0xf0, 0x00); /* Reset. */ 84203245f09SKATO Takenori #else 8432320728fSRodney W. Grimes /* 8442320728fSRodney W. Grimes * Attempt to do a CPU reset via the keyboard controller, 8452320728fSRodney W. Grimes * do not turn of the GateA20, as any machine that fails 8462320728fSRodney W. Grimes * to do the reset here would then end up in no man's land. 8472320728fSRodney W. Grimes */ 8485eb46edfSDavid Greenman 84903245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET) 8502320728fSRodney W. Grimes outb(IO_KBD + 4, 0xFE); 8512320728fSRodney W. Grimes DELAY(500000); /* wait 0.5 sec to see if that did it */ 8522320728fSRodney W. Grimes printf("Keyboard reset did not work, attempting CPU shutdown\n"); 8532320728fSRodney W. Grimes DELAY(1000000); /* wait 1 sec for printf to complete */ 8545eb46edfSDavid Greenman #endif 85503245f09SKATO Takenori #endif /* PC98 */ 8565b81b6b3SRodney W. Grimes /* force a shutdown by unmapping entire address space ! */ 857f8845af0SPoul-Henning Kamp bzero((caddr_t) PTD, PAGE_SIZE); 8585b81b6b3SRodney W. Grimes 8595b81b6b3SRodney W. Grimes /* "good night, sweet prince .... <THUNK!>" */ 86027e9b35eSJohn Dyson invltlb(); 8615b81b6b3SRodney W. Grimes /* NOTREACHED */ 8627f8cb368SDavid Greenman while(1); 8635b81b6b3SRodney W. Grimes } 864b9d60b3fSDavid Greenman 865b9d60b3fSDavid Greenman /* 866b9d60b3fSDavid Greenman * Grow the user stack to allow for 'sp'. This version grows the stack in 86729360eb0SDavid Greenman * chunks of SGROWSIZ. 868b9d60b3fSDavid Greenman */ 869b9d60b3fSDavid Greenman int 870b9d60b3fSDavid Greenman grow(p, sp) 871b9d60b3fSDavid Greenman struct proc *p; 87226f9a767SRodney W. Grimes u_int sp; 873b9d60b3fSDavid Greenman { 874b9d60b3fSDavid Greenman unsigned int nss; 875b9d60b3fSDavid Greenman caddr_t v; 876b9d60b3fSDavid Greenman struct vmspace *vm = p->p_vmspace; 877b9d60b3fSDavid Greenman 878b9d60b3fSDavid Greenman if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK) 879b9d60b3fSDavid Greenman return (1); 880b9d60b3fSDavid Greenman 881b9d60b3fSDavid Greenman nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE); 882b9d60b3fSDavid Greenman 883b9d60b3fSDavid Greenman if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur) 884b9d60b3fSDavid Greenman return (0); 885b9d60b3fSDavid Greenman 886b9d60b3fSDavid Greenman if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT, 88729360eb0SDavid Greenman SGROWSIZ) < nss) { 888b9d60b3fSDavid Greenman int grow_amount; 889b9d60b3fSDavid Greenman /* 890b9d60b3fSDavid Greenman * If necessary, grow the VM that the stack occupies 891b9d60b3fSDavid Greenman * to allow for the rlimit. This allows us to not have 892b9d60b3fSDavid Greenman * to allocate all of the VM up-front in execve (which 893b9d60b3fSDavid Greenman * is expensive). 894b9d60b3fSDavid Greenman * Grow the VM by the amount requested rounded up to 89529360eb0SDavid Greenman * the nearest SGROWSIZ to provide for some hysteresis. 896b9d60b3fSDavid Greenman */ 89729360eb0SDavid Greenman grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ); 898b9d60b3fSDavid Greenman v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT, 89929360eb0SDavid Greenman SGROWSIZ) - grow_amount; 900b9d60b3fSDavid Greenman /* 90129360eb0SDavid Greenman * If there isn't enough room to extend by SGROWSIZ, then 902b9d60b3fSDavid Greenman * just extend to the maximum size 903b9d60b3fSDavid Greenman */ 904b9d60b3fSDavid Greenman if (v < vm->vm_maxsaddr) { 905b9d60b3fSDavid Greenman v = vm->vm_maxsaddr; 906b9d60b3fSDavid Greenman grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT); 907b9d60b3fSDavid Greenman } 90868940ac1SDavid Greenman if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v, 909bd7e5f99SJohn Dyson grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) { 910b9d60b3fSDavid Greenman return (0); 911b9d60b3fSDavid Greenman } 912b9d60b3fSDavid Greenman vm->vm_ssize += grow_amount >> PAGE_SHIFT; 913b9d60b3fSDavid Greenman } 914b9d60b3fSDavid Greenman 915b9d60b3fSDavid Greenman return (1); 916b9d60b3fSDavid Greenman } 917a29b63cbSJohn Dyson 9187762bc7bSBruce Evans static int cnt_prezero; 91966095752SJohn Dyson 92066095752SJohn Dyson SYSCTL_INT(_machdep, OID_AUTO, cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, ""); 92166095752SJohn Dyson 922a29b63cbSJohn Dyson /* 92383eab616SBruce Evans * Implement the pre-zeroed page mechanism. 92483eab616SBruce Evans * This routine is called from the idle loop. 925a29b63cbSJohn Dyson */ 926a29b63cbSJohn Dyson int 92783eab616SBruce Evans vm_page_zero_idle() 92883eab616SBruce Evans { 92983eab616SBruce Evans static int free_rover; 930a29b63cbSJohn Dyson vm_page_t m; 93183eab616SBruce Evans int s; 93283eab616SBruce Evans 93383eab616SBruce Evans /* 93483eab616SBruce Evans * XXX 9358e56e561SJohn Dyson * We stop zeroing pages when there are sufficent prezeroed pages. 9368e56e561SJohn Dyson * This threshold isn't really needed, except we want to 9378e56e561SJohn Dyson * bypass unneeded calls to vm_page_list_find, and the 9388e56e561SJohn Dyson * associated cache flush and latency. The pre-zero will 9398e56e561SJohn Dyson * still be called when there are significantly more 9408e56e561SJohn Dyson * non-prezeroed pages than zeroed pages. The threshold 9418e56e561SJohn Dyson * of half the number of reserved pages is arbitrary, but 9428e56e561SJohn Dyson * approximately the right amount. Eventually, we should 9438e56e561SJohn Dyson * perhaps interrupt the zero operation when a process 9448e56e561SJohn Dyson * is found to be ready to run. 9458e56e561SJohn Dyson */ 94683eab616SBruce Evans if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2) 94783eab616SBruce Evans return (0); 9489a3b3e8bSPeter Wemm #ifdef SMP 94966095752SJohn Dyson if (try_mplock()) { 9509a3b3e8bSPeter Wemm #endif 95183eab616SBruce Evans s = splvm(); 95283eab616SBruce Evans enable_intr(); 95383eab616SBruce Evans m = vm_page_list_find(PQ_FREE, free_rover); 95483eab616SBruce Evans if (m != NULL) { 9555070c7f8SJohn Dyson --(*vm_page_queues[m->queue].lcnt); 9565070c7f8SJohn Dyson TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq); 95795f67de2STor Egge m->queue = PQ_NONE; 95883eab616SBruce Evans splx(s); 95966095752SJohn Dyson #if 0 9609a3b3e8bSPeter Wemm rel_mplock(); 9619a3b3e8bSPeter Wemm #endif 962a29b63cbSJohn Dyson pmap_zero_page(VM_PAGE_TO_PHYS(m)); 9637762bc7bSBruce Evans #if 0 9649a3b3e8bSPeter Wemm get_mplock(); 9659a3b3e8bSPeter Wemm #endif 96683eab616SBruce Evans (void)splvm(); 9675070c7f8SJohn Dyson m->queue = PQ_ZERO + m->pc; 9685070c7f8SJohn Dyson ++(*vm_page_queues[m->queue].lcnt); 9697762bc7bSBruce Evans TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, 9707762bc7bSBruce Evans pageq); 9715070c7f8SJohn Dyson free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK; 972a316d390SJohn Dyson ++vm_page_zero_count; 97366095752SJohn Dyson ++cnt_prezero; 974a29b63cbSJohn Dyson } 97583eab616SBruce Evans splx(s); 97683eab616SBruce Evans disable_intr(); 9779a3b3e8bSPeter Wemm #ifdef SMP 9789a3b3e8bSPeter Wemm rel_mplock(); 9797762bc7bSBruce Evans #endif 98083eab616SBruce Evans return (1); 9817762bc7bSBruce Evans #ifdef SMP 982a29b63cbSJohn Dyson } 98366095752SJohn Dyson #endif 98466095752SJohn Dyson return (0); 98566095752SJohn Dyson } 986e0b78e19SJoerg Wunsch 987e0b78e19SJoerg Wunsch /* 98857d7d7b3SJustin T. Gibbs * Software interrupt handler for queued VM system processing. 98957d7d7b3SJustin T. Gibbs */ 99057d7d7b3SJustin T. Gibbs void 99157d7d7b3SJustin T. Gibbs swi_vm() 99257d7d7b3SJustin T. Gibbs { 99357d7d7b3SJustin T. Gibbs if (busdma_swi_pending != 0) 99457d7d7b3SJustin T. Gibbs busdma_swi(); 99557d7d7b3SJustin T. Gibbs } 99657d7d7b3SJustin T. Gibbs 99757d7d7b3SJustin T. Gibbs /* 998cae6f73aSJoerg Wunsch * Tell whether this address is in some physical memory region. 999e0b78e19SJoerg Wunsch * Currently used by the kernel coredump code in order to avoid 1000e0b78e19SJoerg Wunsch * dumping the ``ISA memory hole'' which could cause indefinite hangs, 1001e0b78e19SJoerg Wunsch * or other unpredictable behaviour. 1002e0b78e19SJoerg Wunsch */ 1003e0b78e19SJoerg Wunsch 1004e0b78e19SJoerg Wunsch #include "isa.h" 1005e0b78e19SJoerg Wunsch 1006e0b78e19SJoerg Wunsch int 1007cae6f73aSJoerg Wunsch is_physical_memory(addr) 1008e0b78e19SJoerg Wunsch vm_offset_t addr; 1009e0b78e19SJoerg Wunsch { 1010e0b78e19SJoerg Wunsch 1011e0b78e19SJoerg Wunsch #if NISA > 0 1012e0b78e19SJoerg Wunsch /* The ISA ``memory hole''. */ 1013e0b78e19SJoerg Wunsch if (addr >= 0xa0000 && addr < 0x100000) 1014cae6f73aSJoerg Wunsch return 0; 1015e0b78e19SJoerg Wunsch #endif 1016e0b78e19SJoerg Wunsch 1017e0b78e19SJoerg Wunsch /* 1018e0b78e19SJoerg Wunsch * stuff other tests for known memory-mapped devices (PCI?) 1019e0b78e19SJoerg Wunsch * here 1020e0b78e19SJoerg Wunsch */ 1021e0b78e19SJoerg Wunsch 1022cae6f73aSJoerg Wunsch return 1; 1023e0b78e19SJoerg Wunsch } 1024