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$ 41d447dbeeSBruce Evans * $Id: vm_machdep.c,v 1.91 1997/11/18 09:27:23 bde Exp $ 425b81b6b3SRodney W. Grimes */ 435b81b6b3SRodney W. Grimes 44960173b9SRodney W. Grimes #include "npx.h" 458890984dSGarrett Wollman #include "opt_bounce.h" 4698823b23SPeter Wemm #include "opt_vm86.h" 478890984dSGarrett Wollman 4826f9a767SRodney W. Grimes #include <sys/param.h> 4926f9a767SRodney W. Grimes #include <sys/systm.h> 5026f9a767SRodney W. Grimes #include <sys/proc.h> 5126f9a767SRodney W. Grimes #include <sys/malloc.h> 5226f9a767SRodney W. Grimes #include <sys/buf.h> 5326f9a767SRodney W. Grimes #include <sys/vnode.h> 54efeaf95aSDavid Greenman #include <sys/vmmeter.h> 555b81b6b3SRodney W. Grimes 562320728fSRodney W. Grimes #include <machine/clock.h> 57a2a1c95cSPeter Wemm #include <machine/cpu.h> 581f8745a9SPeter Wemm #include <machine/md_var.h> 598fa40736SBruce Evans #ifdef SMP 608fa40736SBruce Evans #include <machine/smp.h> 618fa40736SBruce Evans #endif 625b81b6b3SRodney W. Grimes 6326f9a767SRodney W. Grimes #include <vm/vm.h> 64efeaf95aSDavid Greenman #include <vm/vm_param.h> 65efeaf95aSDavid Greenman #include <vm/vm_prot.h> 66996c772fSJohn Dyson #include <sys/lock.h> 6726f9a767SRodney W. Grimes #include <vm/vm_kern.h> 6824a1cce3SDavid Greenman #include <vm/vm_page.h> 69efeaf95aSDavid Greenman #include <vm/vm_map.h> 70efeaf95aSDavid Greenman #include <vm/vm_extern.h> 71efeaf95aSDavid Greenman 72efeaf95aSDavid Greenman #include <sys/user.h> 735b81b6b3SRodney W. Grimes 74e30f0011SSatoshi Asami #ifdef PC98 75e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h> 76e30f0011SSatoshi Asami #else 772320728fSRodney W. Grimes #include <i386/isa/isa.h> 78e30f0011SSatoshi Asami #endif 792320728fSRodney W. Grimes 809aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS 8187b91157SPoul-Henning Kamp static vm_offset_t 826c146e28SBruce Evans vm_bounce_kva __P((int size, int waitok)); 8387b91157SPoul-Henning Kamp static void vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size, 846c146e28SBruce Evans int now)); 8587b91157SPoul-Henning Kamp static vm_offset_t 866c146e28SBruce Evans vm_bounce_page_find __P((int count)); 8787b91157SPoul-Henning Kamp static void vm_bounce_page_free __P((vm_offset_t pa, int count)); 886c146e28SBruce Evans 8987b6de2bSPoul-Henning Kamp static volatile int kvasfreecnt; 90ae92ea44SDavid Greenman 91d5e26ef0SDavid Greenman caddr_t bouncememory; 9287b91157SPoul-Henning Kamp static int bpwait; 9387b91157SPoul-Henning Kamp static vm_offset_t *bouncepa; 9487b91157SPoul-Henning Kamp static int bmwait, bmfreeing; 95d5e26ef0SDavid Greenman 96ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned)) 9787b91157SPoul-Henning Kamp static int bounceallocarraysize; 9887b91157SPoul-Henning Kamp static unsigned *bounceallocarray; 9987b91157SPoul-Henning Kamp static int bouncefree; 100d5e26ef0SDavid Greenman 101e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA) 102e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096) /* 15MB boundary */ 103e30f0011SSatoshi Asami #else 104d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096) 105e30f0011SSatoshi Asami #endif 106ae92ea44SDavid Greenman #define MAXBKVA 1024 107f8845af0SPoul-Henning Kamp int maxbkva = MAXBKVA*PAGE_SIZE; 108d5e26ef0SDavid Greenman 109d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */ 11087b91157SPoul-Henning Kamp static struct kvasfree { 111d5e26ef0SDavid Greenman vm_offset_t addr; 112d5e26ef0SDavid Greenman vm_offset_t size; 113d5e26ef0SDavid Greenman } kvaf[MAXBKVA]; 114d5e26ef0SDavid Greenman 115d5e26ef0SDavid Greenman /* 116d5e26ef0SDavid Greenman * get bounce buffer pages (count physically contiguous) 117d5e26ef0SDavid Greenman * (only 1 inplemented now) 118d5e26ef0SDavid Greenman */ 11987b91157SPoul-Henning Kamp static vm_offset_t 120d5e26ef0SDavid Greenman vm_bounce_page_find(count) 121d5e26ef0SDavid Greenman int count; 122d5e26ef0SDavid Greenman { 123d5e26ef0SDavid Greenman int bit; 124d5e26ef0SDavid Greenman int s,i; 125d5e26ef0SDavid Greenman 126d5e26ef0SDavid Greenman if (count != 1) 127d5e26ef0SDavid Greenman panic("vm_bounce_page_find -- no support for > 1 page yet!!!"); 128d5e26ef0SDavid Greenman 129d5e26ef0SDavid Greenman s = splbio(); 130d5e26ef0SDavid Greenman retry: 131d5e26ef0SDavid Greenman for (i = 0; i < bounceallocarraysize; i++) { 132d5e26ef0SDavid Greenman if (bounceallocarray[i] != 0xffffffff) { 1333fb3086eSPoul-Henning Kamp bit = ffs(~bounceallocarray[i]); 1343fb3086eSPoul-Henning Kamp if (bit) { 135d5e26ef0SDavid Greenman bounceallocarray[i] |= 1 << (bit - 1) ; 136d5e26ef0SDavid Greenman bouncefree -= count; 137d5e26ef0SDavid Greenman splx(s); 138ae92ea44SDavid Greenman return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))]; 139d5e26ef0SDavid Greenman } 140d5e26ef0SDavid Greenman } 141d5e26ef0SDavid Greenman } 142ed7fcbd0SDavid Greenman bpwait = 1; 143d5e26ef0SDavid Greenman tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0); 144d5e26ef0SDavid Greenman goto retry; 145d5e26ef0SDavid Greenman } 146d5e26ef0SDavid Greenman 14787b91157SPoul-Henning Kamp static void 1481561d038SDavid Greenman vm_bounce_kva_free(addr, size, now) 1491561d038SDavid Greenman vm_offset_t addr; 1501561d038SDavid Greenman vm_offset_t size; 1511561d038SDavid Greenman int now; 1521561d038SDavid Greenman { 1531561d038SDavid Greenman int s = splbio(); 1541561d038SDavid Greenman kvaf[kvasfreecnt].addr = addr; 155ae92ea44SDavid Greenman kvaf[kvasfreecnt].size = size; 156ae92ea44SDavid Greenman ++kvasfreecnt; 1570e195446SDavid Greenman if( now) { 1580e195446SDavid Greenman /* 1590e195446SDavid Greenman * this will do wakeups 1600e195446SDavid Greenman */ 1611561d038SDavid Greenman vm_bounce_kva(0,0); 1620e195446SDavid Greenman } else { 1630e195446SDavid Greenman if (bmwait) { 1640e195446SDavid Greenman /* 1650e195446SDavid Greenman * if anyone is waiting on the bounce-map, then wakeup 1660e195446SDavid Greenman */ 1671561d038SDavid Greenman wakeup((caddr_t) io_map); 1680e195446SDavid Greenman bmwait = 0; 1690e195446SDavid Greenman } 1700e195446SDavid Greenman } 1711561d038SDavid Greenman splx(s); 1721561d038SDavid Greenman } 1731561d038SDavid Greenman 174d5e26ef0SDavid Greenman /* 175d5e26ef0SDavid Greenman * free count bounce buffer pages 176d5e26ef0SDavid Greenman */ 17787b91157SPoul-Henning Kamp static void 178d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count) 179d5e26ef0SDavid Greenman vm_offset_t pa; 180d5e26ef0SDavid Greenman int count; 181d5e26ef0SDavid Greenman { 182d5e26ef0SDavid Greenman int allocindex; 183d5e26ef0SDavid Greenman int index; 184d5e26ef0SDavid Greenman int bit; 185d5e26ef0SDavid Greenman 186d5e26ef0SDavid Greenman if (count != 1) 187edf8a815SDavid Greenman panic("vm_bounce_page_free -- no support for > 1 page yet!!!"); 188d5e26ef0SDavid Greenman 189ae92ea44SDavid Greenman for(index=0;index<bouncepages;index++) { 190ae92ea44SDavid Greenman if( pa == bouncepa[index]) 191ae92ea44SDavid Greenman break; 192ae92ea44SDavid Greenman } 193d5e26ef0SDavid Greenman 194ae92ea44SDavid Greenman if( index == bouncepages) 195ae92ea44SDavid Greenman panic("vm_bounce_page_free: invalid bounce buffer"); 196d5e26ef0SDavid Greenman 197ed7fcbd0SDavid Greenman allocindex = index / BITS_IN_UNSIGNED; 198ed7fcbd0SDavid Greenman bit = index % BITS_IN_UNSIGNED; 199d5e26ef0SDavid Greenman 200d5e26ef0SDavid Greenman bounceallocarray[allocindex] &= ~(1 << bit); 201d5e26ef0SDavid Greenman 202d5e26ef0SDavid Greenman bouncefree += count; 203ed7fcbd0SDavid Greenman if (bpwait) { 204ed7fcbd0SDavid Greenman bpwait = 0; 205d5e26ef0SDavid Greenman wakeup((caddr_t) &bounceallocarray); 206d5e26ef0SDavid Greenman } 207ed7fcbd0SDavid Greenman } 208d5e26ef0SDavid Greenman 209d5e26ef0SDavid Greenman /* 210d5e26ef0SDavid Greenman * allocate count bounce buffer kva pages 211d5e26ef0SDavid Greenman */ 21287b91157SPoul-Henning Kamp static vm_offset_t 213ae92ea44SDavid Greenman vm_bounce_kva(size, waitok) 214ae92ea44SDavid Greenman int size; 2151561d038SDavid Greenman int waitok; 216d5e26ef0SDavid Greenman { 217d5e26ef0SDavid Greenman int i; 2186b4ac811SDavid Greenman vm_offset_t kva = 0; 219c564966bSDavid Greenman vm_offset_t off; 220d5e26ef0SDavid Greenman int s = splbio(); 221d5e26ef0SDavid Greenman more: 222ae92ea44SDavid Greenman if (!bmfreeing && kvasfreecnt) { 223d5e26ef0SDavid Greenman bmfreeing = 1; 224ae92ea44SDavid Greenman for (i = 0; i < kvasfreecnt; i++) { 225f8845af0SPoul-Henning Kamp for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) { 226c564966bSDavid Greenman pmap_kremove( kvaf[i].addr + off); 227c564966bSDavid Greenman } 2281561d038SDavid Greenman kmem_free_wakeup(io_map, kvaf[i].addr, 229d5e26ef0SDavid Greenman kvaf[i].size); 230d5e26ef0SDavid Greenman } 231d5e26ef0SDavid Greenman kvasfreecnt = 0; 232d5e26ef0SDavid Greenman bmfreeing = 0; 233ae92ea44SDavid Greenman if( bmwait) { 234ae92ea44SDavid Greenman bmwait = 0; 235ae92ea44SDavid Greenman wakeup( (caddr_t) io_map); 236ae92ea44SDavid Greenman } 237d5e26ef0SDavid Greenman } 238d5e26ef0SDavid Greenman 2391561d038SDavid Greenman if( size == 0) { 2401561d038SDavid Greenman splx(s); 2416ab46d52SBruce Evans return 0; 2421561d038SDavid Greenman } 2431561d038SDavid Greenman 244ae92ea44SDavid Greenman if ((kva = kmem_alloc_pageable(io_map, size)) == 0) { 2451561d038SDavid Greenman if( !waitok) { 2461561d038SDavid Greenman splx(s); 2476ab46d52SBruce Evans return 0; 2481561d038SDavid Greenman } 249d5e26ef0SDavid Greenman bmwait = 1; 2501561d038SDavid Greenman tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0); 251d5e26ef0SDavid Greenman goto more; 252d5e26ef0SDavid Greenman } 253d5e26ef0SDavid Greenman splx(s); 254d5e26ef0SDavid Greenman return kva; 255d5e26ef0SDavid Greenman } 256d5e26ef0SDavid Greenman 257d5e26ef0SDavid Greenman /* 258ae92ea44SDavid Greenman * same as vm_bounce_kva -- but really allocate (but takes pages as arg) 2590e195446SDavid Greenman */ 2600e195446SDavid Greenman vm_offset_t 2610e195446SDavid Greenman vm_bounce_kva_alloc(count) 2620e195446SDavid Greenman int count; 2630e195446SDavid Greenman { 2640e195446SDavid Greenman int i; 2650e195446SDavid Greenman vm_offset_t kva; 2660e195446SDavid Greenman vm_offset_t pa; 2670e195446SDavid Greenman if( bouncepages == 0) { 268f8845af0SPoul-Henning Kamp kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK); 2690e195446SDavid Greenman return kva; 2700e195446SDavid Greenman } 271f8845af0SPoul-Henning Kamp kva = vm_bounce_kva(count*PAGE_SIZE, 1); 2720e195446SDavid Greenman for(i=0;i<count;i++) { 2730e195446SDavid Greenman pa = vm_bounce_page_find(1); 274f8845af0SPoul-Henning Kamp pmap_kenter(kva + i * PAGE_SIZE, pa); 2750e195446SDavid Greenman } 2760e195446SDavid Greenman return kva; 2770e195446SDavid Greenman } 2780e195446SDavid Greenman 2790e195446SDavid Greenman /* 2800e195446SDavid Greenman * same as vm_bounce_kva_free -- but really free 2810e195446SDavid Greenman */ 2820e195446SDavid Greenman void 2830e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count) 2840e195446SDavid Greenman vm_offset_t kva; 2850e195446SDavid Greenman int count; 2860e195446SDavid Greenman { 2870e195446SDavid Greenman int i; 2880e195446SDavid Greenman vm_offset_t pa; 2890e195446SDavid Greenman if( bouncepages == 0) { 2900e195446SDavid Greenman free((caddr_t) kva, M_TEMP); 2910e195446SDavid Greenman return; 2920e195446SDavid Greenman } 2930e195446SDavid Greenman for(i = 0; i < count; i++) { 294f8845af0SPoul-Henning Kamp pa = pmap_kextract(kva + i * PAGE_SIZE); 2950e195446SDavid Greenman vm_bounce_page_free(pa, 1); 2960e195446SDavid Greenman } 297f8845af0SPoul-Henning Kamp vm_bounce_kva_free(kva, count*PAGE_SIZE, 0); 2980e195446SDavid Greenman } 2990e195446SDavid Greenman 3000e195446SDavid Greenman /* 301d5e26ef0SDavid Greenman * do the things necessary to the struct buf to implement 302d5e26ef0SDavid Greenman * bounce buffers... inserted before the disk sort 303d5e26ef0SDavid Greenman */ 304d5e26ef0SDavid Greenman void 305d5e26ef0SDavid Greenman vm_bounce_alloc(bp) 306d5e26ef0SDavid Greenman struct buf *bp; 307d5e26ef0SDavid Greenman { 308d5e26ef0SDavid Greenman int countvmpg; 309d5e26ef0SDavid Greenman vm_offset_t vastart, vaend; 310d5e26ef0SDavid Greenman vm_offset_t vapstart, vapend; 311d5e26ef0SDavid Greenman vm_offset_t va, kva; 312d5e26ef0SDavid Greenman vm_offset_t pa; 313d5e26ef0SDavid Greenman int dobounceflag = 0; 314d5e26ef0SDavid Greenman int i; 315d5e26ef0SDavid Greenman 316d5e26ef0SDavid Greenman if (bouncepages == 0) 317d5e26ef0SDavid Greenman return; 318d5e26ef0SDavid Greenman 319ae92ea44SDavid Greenman if (bp->b_flags & B_BOUNCE) { 320ae92ea44SDavid Greenman printf("vm_bounce_alloc: called recursively???\n"); 321ae92ea44SDavid Greenman return; 3221561d038SDavid Greenman } 3231561d038SDavid Greenman 324ae92ea44SDavid Greenman if (bp->b_bufsize < bp->b_bcount) { 3253fb3086eSPoul-Henning Kamp printf( 3263fb3086eSPoul-Henning Kamp "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n", 327ae92ea44SDavid Greenman bp->b_bufsize, bp->b_bcount); 328ae92ea44SDavid Greenman panic("vm_bounce_alloc"); 329ae92ea44SDavid Greenman } 330ae92ea44SDavid Greenman 331ae92ea44SDavid Greenman /* 332ae92ea44SDavid Greenman * This is not really necessary 333ae92ea44SDavid Greenman * if( bp->b_bufsize != bp->b_bcount) { 334ae92ea44SDavid Greenman * printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount); 335ae92ea44SDavid Greenman * } 336ae92ea44SDavid Greenman */ 337ae92ea44SDavid Greenman 338ae92ea44SDavid Greenman 33926f9a767SRodney W. Grimes vastart = (vm_offset_t) bp->b_data; 34026f9a767SRodney W. Grimes vaend = (vm_offset_t) bp->b_data + bp->b_bufsize; 341d5e26ef0SDavid Greenman 342bd7e5f99SJohn Dyson vapstart = trunc_page(vastart); 343bd7e5f99SJohn Dyson vapend = round_page(vaend); 344f8845af0SPoul-Henning Kamp countvmpg = (vapend - vapstart) / PAGE_SIZE; 345d5e26ef0SDavid Greenman 346d5e26ef0SDavid Greenman /* 347d5e26ef0SDavid Greenman * if any page is above 16MB, then go into bounce-buffer mode 348d5e26ef0SDavid Greenman */ 349d5e26ef0SDavid Greenman va = vapstart; 350d5e26ef0SDavid Greenman for (i = 0; i < countvmpg; i++) { 351ed7fcbd0SDavid Greenman pa = pmap_kextract(va); 352d5e26ef0SDavid Greenman if (pa >= SIXTEENMEG) 353d5e26ef0SDavid Greenman ++dobounceflag; 3540d94caffSDavid Greenman if( pa == 0) 3550d94caffSDavid Greenman panic("vm_bounce_alloc: Unmapped page"); 356f8845af0SPoul-Henning Kamp va += PAGE_SIZE; 357d5e26ef0SDavid Greenman } 358d5e26ef0SDavid Greenman if (dobounceflag == 0) 359d5e26ef0SDavid Greenman return; 360d5e26ef0SDavid Greenman 361d5e26ef0SDavid Greenman if (bouncepages < dobounceflag) 362d5e26ef0SDavid Greenman panic("Not enough bounce buffers!!!"); 363d5e26ef0SDavid Greenman 364d5e26ef0SDavid Greenman /* 365d5e26ef0SDavid Greenman * allocate a replacement kva for b_addr 366d5e26ef0SDavid Greenman */ 367f8845af0SPoul-Henning Kamp kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1); 368ae92ea44SDavid Greenman #if 0 369ae92ea44SDavid Greenman printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ", 370ae92ea44SDavid Greenman (bp->b_flags & B_READ) ? "read":"write", 371ae92ea44SDavid Greenman vapstart, vapend, countvmpg, kva); 372ae92ea44SDavid Greenman #endif 373d5e26ef0SDavid Greenman va = vapstart; 374d5e26ef0SDavid Greenman for (i = 0; i < countvmpg; i++) { 375ed7fcbd0SDavid Greenman pa = pmap_kextract(va); 376d5e26ef0SDavid Greenman if (pa >= SIXTEENMEG) { 377d5e26ef0SDavid Greenman /* 378d5e26ef0SDavid Greenman * allocate a replacement page 379d5e26ef0SDavid Greenman */ 380d5e26ef0SDavid Greenman vm_offset_t bpa = vm_bounce_page_find(1); 381f8845af0SPoul-Henning Kamp pmap_kenter(kva + (PAGE_SIZE * i), bpa); 382ae92ea44SDavid Greenman #if 0 383ae92ea44SDavid Greenman printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa); 384ae92ea44SDavid Greenman #endif 385d5e26ef0SDavid Greenman /* 386d5e26ef0SDavid Greenman * if we are writing, the copy the data into the page 387d5e26ef0SDavid Greenman */ 3881561d038SDavid Greenman if ((bp->b_flags & B_READ) == 0) { 389f8845af0SPoul-Henning Kamp bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE); 3901561d038SDavid Greenman } 391d5e26ef0SDavid Greenman } else { 392d5e26ef0SDavid Greenman /* 393d5e26ef0SDavid Greenman * use original page 394d5e26ef0SDavid Greenman */ 395f8845af0SPoul-Henning Kamp pmap_kenter(kva + (PAGE_SIZE * i), pa); 396d5e26ef0SDavid Greenman } 397f8845af0SPoul-Henning Kamp va += PAGE_SIZE; 398d5e26ef0SDavid Greenman } 399d5e26ef0SDavid Greenman 400d5e26ef0SDavid Greenman /* 401d5e26ef0SDavid Greenman * flag the buffer as being bounced 402d5e26ef0SDavid Greenman */ 403d5e26ef0SDavid Greenman bp->b_flags |= B_BOUNCE; 404d5e26ef0SDavid Greenman /* 405d5e26ef0SDavid Greenman * save the original buffer kva 406d5e26ef0SDavid Greenman */ 40726f9a767SRodney W. Grimes bp->b_savekva = bp->b_data; 408d5e26ef0SDavid Greenman /* 409d5e26ef0SDavid Greenman * put our new kva into the buffer (offset by original offset) 410d5e26ef0SDavid Greenman */ 41126f9a767SRodney W. Grimes bp->b_data = (caddr_t) (((vm_offset_t) kva) | 412f8845af0SPoul-Henning Kamp ((vm_offset_t) bp->b_savekva & PAGE_MASK)); 413ae92ea44SDavid Greenman #if 0 414ae92ea44SDavid Greenman printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data); 415ae92ea44SDavid Greenman #endif 416d5e26ef0SDavid Greenman return; 417d5e26ef0SDavid Greenman } 418d5e26ef0SDavid Greenman 419d5e26ef0SDavid Greenman /* 420d5e26ef0SDavid Greenman * hook into biodone to free bounce buffer 421d5e26ef0SDavid Greenman */ 422d5e26ef0SDavid Greenman void 423d5e26ef0SDavid Greenman vm_bounce_free(bp) 424d5e26ef0SDavid Greenman struct buf *bp; 425d5e26ef0SDavid Greenman { 426d5e26ef0SDavid Greenman int i; 427ae92ea44SDavid Greenman vm_offset_t origkva, bouncekva, bouncekvaend; 428d5e26ef0SDavid Greenman 429d5e26ef0SDavid Greenman /* 430d5e26ef0SDavid Greenman * if this isn't a bounced buffer, then just return 431d5e26ef0SDavid Greenman */ 432d5e26ef0SDavid Greenman if ((bp->b_flags & B_BOUNCE) == 0) 433d5e26ef0SDavid Greenman return; 434d5e26ef0SDavid Greenman 435ae92ea44SDavid Greenman /* 436ae92ea44SDavid Greenman * This check is not necessary 437ae92ea44SDavid Greenman * if (bp->b_bufsize != bp->b_bcount) { 438ae92ea44SDavid Greenman * printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n", 439ae92ea44SDavid Greenman * bp->b_bufsize, bp->b_bcount); 440ae92ea44SDavid Greenman * } 441ae92ea44SDavid Greenman */ 442ae92ea44SDavid Greenman 443d5e26ef0SDavid Greenman origkva = (vm_offset_t) bp->b_savekva; 44426f9a767SRodney W. Grimes bouncekva = (vm_offset_t) bp->b_data; 445ae92ea44SDavid Greenman /* 446ae92ea44SDavid Greenman printf("free: %d ", bp->b_bufsize); 447ae92ea44SDavid Greenman */ 448d5e26ef0SDavid Greenman 449d5e26ef0SDavid Greenman /* 450d5e26ef0SDavid Greenman * check every page in the kva space for b_addr 451d5e26ef0SDavid Greenman */ 452ae92ea44SDavid Greenman for (i = 0; i < bp->b_bufsize; ) { 453d5e26ef0SDavid Greenman vm_offset_t mybouncepa; 454d5e26ef0SDavid Greenman vm_offset_t copycount; 455d5e26ef0SDavid Greenman 456bd7e5f99SJohn Dyson copycount = round_page(bouncekva + 1) - bouncekva; 457bd7e5f99SJohn Dyson mybouncepa = pmap_kextract(trunc_page(bouncekva)); 458d5e26ef0SDavid Greenman 459d5e26ef0SDavid Greenman /* 460d5e26ef0SDavid Greenman * if this is a bounced pa, then process as one 461d5e26ef0SDavid Greenman */ 462bd7e5f99SJohn Dyson if ( mybouncepa != pmap_kextract( trunc_page( origkva))) { 463ae92ea44SDavid Greenman vm_offset_t tocopy = copycount; 464ae92ea44SDavid Greenman if (i + tocopy > bp->b_bufsize) 465ae92ea44SDavid Greenman tocopy = bp->b_bufsize - i; 466d5e26ef0SDavid Greenman /* 467d5e26ef0SDavid Greenman * if this is a read, then copy from bounce buffer into original buffer 468d5e26ef0SDavid Greenman */ 469d5e26ef0SDavid Greenman if (bp->b_flags & B_READ) 470ae92ea44SDavid Greenman bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy); 471d5e26ef0SDavid Greenman /* 472d5e26ef0SDavid Greenman * free the bounce allocation 473d5e26ef0SDavid Greenman */ 474ae92ea44SDavid Greenman 475ae92ea44SDavid Greenman /* 476ae92ea44SDavid Greenman printf("(kva: %x, pa: %x)", bouncekva, mybouncepa); 477ae92ea44SDavid Greenman */ 478ae92ea44SDavid Greenman vm_bounce_page_free(mybouncepa, 1); 479d5e26ef0SDavid Greenman } 480d5e26ef0SDavid Greenman 481d5e26ef0SDavid Greenman origkva += copycount; 482d5e26ef0SDavid Greenman bouncekva += copycount; 483ae92ea44SDavid Greenman i += copycount; 484d5e26ef0SDavid Greenman } 485d5e26ef0SDavid Greenman 486d5e26ef0SDavid Greenman /* 487ae92ea44SDavid Greenman printf("\n"); 488ae92ea44SDavid Greenman */ 489ae92ea44SDavid Greenman /* 490d5e26ef0SDavid Greenman * add the old kva into the "to free" list 491d5e26ef0SDavid Greenman */ 492ae92ea44SDavid Greenman 493bd7e5f99SJohn Dyson bouncekva= trunc_page((vm_offset_t) bp->b_data); 494bd7e5f99SJohn Dyson bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize); 495ae92ea44SDavid Greenman 496ae92ea44SDavid Greenman /* 497f8845af0SPoul-Henning Kamp printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE); 498ae92ea44SDavid Greenman */ 499ae92ea44SDavid Greenman vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0); 50026f9a767SRodney W. Grimes bp->b_data = bp->b_savekva; 501d5e26ef0SDavid Greenman bp->b_savekva = 0; 502d5e26ef0SDavid Greenman bp->b_flags &= ~B_BOUNCE; 503d5e26ef0SDavid Greenman 504d5e26ef0SDavid Greenman return; 505d5e26ef0SDavid Greenman } 506d5e26ef0SDavid Greenman 507ae92ea44SDavid Greenman 5085b81b6b3SRodney W. Grimes /* 5091561d038SDavid Greenman * init the bounce buffer system 5101561d038SDavid Greenman */ 5111561d038SDavid Greenman void 5121561d038SDavid Greenman vm_bounce_init() 5131561d038SDavid Greenman { 514ae92ea44SDavid Greenman int i; 5151561d038SDavid Greenman 5161561d038SDavid Greenman kvasfreecnt = 0; 5171561d038SDavid Greenman 5181561d038SDavid Greenman if (bouncepages == 0) 5191561d038SDavid Greenman return; 5201561d038SDavid Greenman 5211561d038SDavid Greenman bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED; 5221561d038SDavid Greenman bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT); 5231561d038SDavid Greenman 5241561d038SDavid Greenman if (!bounceallocarray) 525edf8a815SDavid Greenman panic("Cannot allocate bounce resource array"); 5261561d038SDavid Greenman 527ae92ea44SDavid Greenman bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT); 528ae92ea44SDavid Greenman if (!bouncepa) 529edf8a815SDavid Greenman panic("Cannot allocate physical memory array"); 5301561d038SDavid Greenman 5310d94caffSDavid Greenman for(i=0;i<bounceallocarraysize;i++) { 5320d94caffSDavid Greenman bounceallocarray[i] = 0xffffffff; 5330d94caffSDavid Greenman } 5340d94caffSDavid Greenman 535ae92ea44SDavid Greenman for(i=0;i<bouncepages;i++) { 536ae92ea44SDavid Greenman vm_offset_t pa; 537f8845af0SPoul-Henning Kamp if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) 538ae92ea44SDavid Greenman panic("bounce memory out of range"); 539ae92ea44SDavid Greenman if( pa == 0) 540ae92ea44SDavid Greenman panic("bounce memory not resident"); 541ae92ea44SDavid Greenman bouncepa[i] = pa; 5420d94caffSDavid Greenman bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int)))); 543ae92ea44SDavid Greenman } 5441561d038SDavid Greenman bouncefree = bouncepages; 5451561d038SDavid Greenman 5461561d038SDavid Greenman } 5479aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */ 5486c146e28SBruce Evans 549a4f7a4c9SDavid Greenman /* 550a4f7a4c9SDavid Greenman * quick version of vm_fault 551a4f7a4c9SDavid Greenman */ 552dca51299SJohn Dyson void 553a4f7a4c9SDavid Greenman vm_fault_quick(v, prot) 5546c146e28SBruce Evans caddr_t v; 555a4f7a4c9SDavid Greenman int prot; 556a4f7a4c9SDavid Greenman { 5571d1b971bSDavid Greenman if (prot & VM_PROT_WRITE) 5586c146e28SBruce Evans subyte(v, fubyte(v)); 559a4f7a4c9SDavid Greenman else 5606c146e28SBruce Evans fubyte(v); 561a4f7a4c9SDavid Greenman } 562a4f7a4c9SDavid Greenman 5631561d038SDavid Greenman /* 5645b81b6b3SRodney W. Grimes * Finish a fork operation, with process p2 nearly set up. 565a2a1c95cSPeter Wemm * Copy and update the pcb, set up the stack so that the child 566a2a1c95cSPeter Wemm * ready to run and return to user mode. 5675b81b6b3SRodney W. Grimes */ 568a2a1c95cSPeter Wemm void 5695b81b6b3SRodney W. Grimes cpu_fork(p1, p2) 5705b81b6b3SRodney W. Grimes register struct proc *p1, *p2; 5715b81b6b3SRodney W. Grimes { 572f83a01b6SDavid Greenman struct pcb *pcb2 = &p2->p_addr->u_pcb; 5735b81b6b3SRodney W. Grimes 5741f8745a9SPeter Wemm /* Ensure that p1's pcb is up to date. */ 5751f8745a9SPeter Wemm if (npxproc == p1) 5761f8745a9SPeter Wemm npxsave(&p1->p_addr->u_pcb.pcb_savefpu); 5771f8745a9SPeter Wemm 5781f8745a9SPeter Wemm /* Copy p1's pcb. */ 5791f8745a9SPeter Wemm p2->p_addr->u_pcb = p1->p_addr->u_pcb; 580a2a1c95cSPeter Wemm 581a2a1c95cSPeter Wemm /* 582a2a1c95cSPeter Wemm * Create a new fresh stack for the new process. 5831f8745a9SPeter Wemm * Copy the trap frame for the return to user mode as if from a 5841f8745a9SPeter Wemm * syscall. This copies the user mode register values. 585a2a1c95cSPeter Wemm */ 5861f8745a9SPeter Wemm p2->p_md.md_regs = (struct trapframe *) 5871f8745a9SPeter Wemm ((int)p2->p_addr + UPAGES * PAGE_SIZE) - 1; 5881f8745a9SPeter Wemm *p2->p_md.md_regs = *p1->p_md.md_regs; 589a2a1c95cSPeter Wemm 590a2a1c95cSPeter Wemm /* 591a2a1c95cSPeter Wemm * Set registers for trampoline to user mode. Leave space for the 592a2a1c95cSPeter Wemm * return address on stack. These are the kernel mode register values. 593a2a1c95cSPeter Wemm */ 5941f8745a9SPeter Wemm pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir); 5951f8745a9SPeter Wemm pcb2->pcb_edi = p2->p_md.md_regs->tf_edi; 596a2a1c95cSPeter Wemm pcb2->pcb_esi = (int)fork_return; 5971f8745a9SPeter Wemm pcb2->pcb_ebp = p2->p_md.md_regs->tf_ebp; 598a2a1c95cSPeter Wemm pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *); 5991f8745a9SPeter Wemm pcb2->pcb_ebx = (int)p2; 6001f8745a9SPeter Wemm pcb2->pcb_eip = (int)fork_trampoline; 6011f8745a9SPeter Wemm /* 6021f8745a9SPeter Wemm * pcb2->pcb_ldt: duplicated below, if necessary. 6031f8745a9SPeter Wemm * pcb2->pcb_ldt_len: cloned above. 6041f8745a9SPeter Wemm * pcb2->pcb_savefpu: cloned above. 6051f8745a9SPeter Wemm * pcb2->pcb_flags: cloned above (always 0 here?). 6061f8745a9SPeter Wemm * pcb2->pcb_onfault: cloned above (always NULL here?). 6071f8745a9SPeter Wemm */ 6085b81b6b3SRodney W. Grimes 60948a09cf2SJohn Dyson #ifdef VM86 61048a09cf2SJohn Dyson /* 61148a09cf2SJohn Dyson * XXX don't copy the i/o pages. this should probably be fixed. 61248a09cf2SJohn Dyson */ 61348a09cf2SJohn Dyson pcb2->pcb_ext = 0; 61448a09cf2SJohn Dyson #endif 61548a09cf2SJohn Dyson 6168c39a127SStefan Eßer #ifdef USER_LDT 6178c39a127SStefan Eßer /* Copy the LDT, if necessary. */ 618a2a1c95cSPeter Wemm if (pcb2->pcb_ldt != 0) { 6198c39a127SStefan Eßer union descriptor *new_ldt; 620a2a1c95cSPeter Wemm size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor); 6218c39a127SStefan Eßer 6228c39a127SStefan Eßer new_ldt = (union descriptor *)kmem_alloc(kernel_map, len); 623a2a1c95cSPeter Wemm bcopy(pcb2->pcb_ldt, new_ldt, len); 624a2a1c95cSPeter Wemm pcb2->pcb_ldt = (caddr_t)new_ldt; 6258c39a127SStefan Eßer } 6268c39a127SStefan Eßer #endif 6278c39a127SStefan Eßer 628a2a1c95cSPeter Wemm /* 629a2a1c95cSPeter Wemm * Now, cpu_switch() can schedule the new process. 630a2a1c95cSPeter Wemm * pcb_esp is loaded pointing to the cpu_switch() stack frame 631a2a1c95cSPeter Wemm * containing the return address when exiting cpu_switch. 632a2a1c95cSPeter Wemm * This will normally be to proc_trampoline(), which will have 633a2a1c95cSPeter Wemm * %ebx loaded with the new proc's pointer. proc_trampoline() 634a2a1c95cSPeter Wemm * will set up a stack to call fork_return(p, frame); to complete 635a2a1c95cSPeter Wemm * the return to user-mode. 636a2a1c95cSPeter Wemm */ 637a2a1c95cSPeter Wemm } 638a2a1c95cSPeter Wemm 639a2a1c95cSPeter Wemm /* 640a2a1c95cSPeter Wemm * Intercept the return address from a freshly forked process that has NOT 641a2a1c95cSPeter Wemm * been scheduled yet. 642a2a1c95cSPeter Wemm * 643a2a1c95cSPeter Wemm * This is needed to make kernel threads stay in kernel mode. 644a2a1c95cSPeter Wemm */ 645a2a1c95cSPeter Wemm void 646a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg) 647a2a1c95cSPeter Wemm struct proc *p; 648a2a1c95cSPeter Wemm void (*func) __P((void *)); 649a2a1c95cSPeter Wemm void *arg; 650a2a1c95cSPeter Wemm { 651a2a1c95cSPeter Wemm /* 652a2a1c95cSPeter Wemm * Note that the trap frame follows the args, so the function 653a2a1c95cSPeter Wemm * is really called like this: func(arg, frame); 654a2a1c95cSPeter Wemm */ 655a2a1c95cSPeter Wemm p->p_addr->u_pcb.pcb_esi = (int) func; /* function */ 656a2a1c95cSPeter Wemm p->p_addr->u_pcb.pcb_ebx = (int) arg; /* first arg */ 6575b81b6b3SRodney W. Grimes } 6585b81b6b3SRodney W. Grimes 6597c2b54e8SNate Williams void 6605b81b6b3SRodney W. Grimes cpu_exit(p) 6615b81b6b3SRodney W. Grimes register struct proc *p; 6625b81b6b3SRodney W. Grimes { 66348a09cf2SJohn Dyson #if defined(USER_LDT) || defined(VM86) 66448a09cf2SJohn Dyson struct pcb *pcb = &p->p_addr->u_pcb; 6650dbf6d73SJordan K. Hubbard #endif 6665b81b6b3SRodney W. Grimes 667960173b9SRodney W. Grimes #if NNPX > 0 6685b81b6b3SRodney W. Grimes npxexit(p); 669960173b9SRodney W. Grimes #endif /* NNPX */ 67048a09cf2SJohn Dyson #ifdef VM86 67148a09cf2SJohn Dyson if (pcb->pcb_ext != 0) { 67248a09cf2SJohn Dyson /* 67348a09cf2SJohn Dyson * XXX do we need to move the TSS off the allocated pages 67448a09cf2SJohn Dyson * before freeing them? (not done here) 67548a09cf2SJohn Dyson */ 67648a09cf2SJohn Dyson kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext, 67748a09cf2SJohn Dyson ctob(IOPAGES + 1)); 67848a09cf2SJohn Dyson pcb->pcb_ext = 0; 67948a09cf2SJohn Dyson } 68048a09cf2SJohn Dyson #endif 6810dbf6d73SJordan K. Hubbard #ifdef USER_LDT 6820dbf6d73SJordan K. Hubbard if (pcb->pcb_ldt != 0) { 6830dbf6d73SJordan K. Hubbard if (pcb == curpcb) 6840dbf6d73SJordan K. Hubbard lldt(GSEL(GUSERLDT_SEL, SEL_KPL)); 6850dbf6d73SJordan K. Hubbard kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt, 6860dbf6d73SJordan K. Hubbard pcb->pcb_ldt_len * sizeof(union descriptor)); 6870dbf6d73SJordan K. Hubbard pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0; 6880dbf6d73SJordan K. Hubbard } 6890dbf6d73SJordan K. Hubbard #endif 6901a051896SBruce Evans cnt.v_swtch++; 6911a051896SBruce Evans cpu_switch(p); 6927c2b54e8SNate Williams panic("cpu_exit"); 6935b81b6b3SRodney W. Grimes } 6945b81b6b3SRodney W. Grimes 695381fe1aaSGarrett Wollman void 696bd7e5f99SJohn Dyson cpu_wait(p) 697bd7e5f99SJohn Dyson struct proc *p; 698bd7e5f99SJohn Dyson { 6995b81b6b3SRodney W. Grimes /* drop per-process resources */ 700675878e7SJohn Dyson pmap_dispose_proc(p); 7017f8cb368SDavid Greenman vmspace_free(p->p_vmspace); 7025b81b6b3SRodney W. Grimes } 7035b81b6b3SRodney W. Grimes 7045b81b6b3SRodney W. Grimes /* 70526f9a767SRodney W. Grimes * Dump the machine specific header information at the start of a core dump. 70626f9a767SRodney W. Grimes */ 70726f9a767SRodney W. Grimes int 70826f9a767SRodney W. Grimes cpu_coredump(p, vp, cred) 70926f9a767SRodney W. Grimes struct proc *p; 71026f9a767SRodney W. Grimes struct vnode *vp; 71126f9a767SRodney W. Grimes struct ucred *cred; 71226f9a767SRodney W. Grimes { 71326f9a767SRodney W. Grimes 71426f9a767SRodney W. Grimes return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES), 71526f9a767SRodney W. Grimes (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, 71626f9a767SRodney W. Grimes p)); 71726f9a767SRodney W. Grimes } 71826f9a767SRodney W. Grimes 71987b6de2bSPoul-Henning Kamp #ifdef notyet 72087b91157SPoul-Henning Kamp static void 7215b81b6b3SRodney W. Grimes setredzone(pte, vaddr) 7225b81b6b3SRodney W. Grimes u_short *pte; 7235b81b6b3SRodney W. Grimes caddr_t vaddr; 7245b81b6b3SRodney W. Grimes { 7255b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment 7265b81b6b3SRodney W. Grimes for ss0: selector, allowing stack access down to top of u. 7275b81b6b3SRodney W. Grimes this means though that protection violations need to be handled 7285b81b6b3SRodney W. Grimes thru a double fault exception that must do an integral task 7295b81b6b3SRodney W. Grimes switch to a known good context, within which a dump can be 7305b81b6b3SRodney W. Grimes taken. a sensible scheme might be to save the initial context 7315b81b6b3SRodney W. Grimes used by sched (that has physical memory mapped 1:1 at bottom) 7325b81b6b3SRodney W. Grimes and take the dump while still in mapped mode */ 7335b81b6b3SRodney W. Grimes } 73487b6de2bSPoul-Henning Kamp #endif 73526f9a767SRodney W. Grimes 73626f9a767SRodney W. Grimes /* 7375b81b6b3SRodney W. Grimes * Convert kernel VA to physical address 7385b81b6b3SRodney W. Grimes */ 739aaf08d94SGarrett Wollman u_long 7407f8cb368SDavid Greenman kvtop(void *addr) 7415b81b6b3SRodney W. Grimes { 7425b81b6b3SRodney W. Grimes vm_offset_t va; 7435b81b6b3SRodney W. Grimes 744ed7fcbd0SDavid Greenman va = pmap_kextract((vm_offset_t)addr); 7455b81b6b3SRodney W. Grimes if (va == 0) 7465b81b6b3SRodney W. Grimes panic("kvtop: zero page frame"); 7477f8cb368SDavid Greenman return((int)va); 7485b81b6b3SRodney W. Grimes } 7495b81b6b3SRodney W. Grimes 7505b81b6b3SRodney W. Grimes /* 751ac322158SDavid Greenman * Map an IO request into kernel virtual address space. 7525b81b6b3SRodney W. Grimes * 753ac322158SDavid Greenman * All requests are (re)mapped into kernel VA space. 754ac322158SDavid Greenman * Notice that we use b_bufsize for the size of the buffer 755ac322158SDavid Greenman * to be mapped. b_bcount might be modified by the driver. 7565b81b6b3SRodney W. Grimes */ 757381fe1aaSGarrett Wollman void 7585b81b6b3SRodney W. Grimes vmapbuf(bp) 7595b81b6b3SRodney W. Grimes register struct buf *bp; 7605b81b6b3SRodney W. Grimes { 7610157d6d9SJohn Dyson register caddr_t addr, v, kva; 76207658526SPoul-Henning Kamp vm_offset_t pa; 7635b81b6b3SRodney W. Grimes 76426f9a767SRodney W. Grimes if ((bp->b_flags & B_PHYS) == 0) 7655b81b6b3SRodney W. Grimes panic("vmapbuf"); 76626f9a767SRodney W. Grimes 76712936855SDavid Greenman for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 76826f9a767SRodney W. Grimes addr < bp->b_data + bp->b_bufsize; 7690157d6d9SJohn Dyson addr += PAGE_SIZE, v += PAGE_SIZE) { 77026f9a767SRodney W. Grimes /* 771ac269d78SDavid Greenman * Do the vm_fault if needed; do the copy-on-write thing 772ac269d78SDavid Greenman * when reading stuff off device into memory. 77326f9a767SRodney W. Grimes */ 77426f9a767SRodney W. Grimes vm_fault_quick(addr, 77526f9a767SRodney W. Grimes (bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 7760157d6d9SJohn Dyson pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 777641a27e6SJohn Dyson if (pa == 0) 778641a27e6SJohn Dyson panic("vmapbuf: page not present"); 77926f9a767SRodney W. Grimes vm_page_hold(PHYS_TO_VM_PAGE(pa)); 7800157d6d9SJohn Dyson pmap_kenter((vm_offset_t) v, pa); 78126f9a767SRodney W. Grimes } 78226f9a767SRodney W. Grimes 7830157d6d9SJohn Dyson kva = bp->b_saveaddr; 7840157d6d9SJohn Dyson bp->b_saveaddr = bp->b_data; 7850157d6d9SJohn Dyson bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 7865b81b6b3SRodney W. Grimes } 7875b81b6b3SRodney W. Grimes 7885b81b6b3SRodney W. Grimes /* 7895b81b6b3SRodney W. Grimes * Free the io map PTEs associated with this IO operation. 7905b81b6b3SRodney W. Grimes * We also invalidate the TLB entries and restore the original b_addr. 7915b81b6b3SRodney W. Grimes */ 792381fe1aaSGarrett Wollman void 7935b81b6b3SRodney W. Grimes vunmapbuf(bp) 7945b81b6b3SRodney W. Grimes register struct buf *bp; 7955b81b6b3SRodney W. Grimes { 796c564966bSDavid Greenman register caddr_t addr; 79707658526SPoul-Henning Kamp vm_offset_t pa; 7985b81b6b3SRodney W. Grimes 7995b81b6b3SRodney W. Grimes if ((bp->b_flags & B_PHYS) == 0) 8005b81b6b3SRodney W. Grimes panic("vunmapbuf"); 801c564966bSDavid Greenman 80212936855SDavid Greenman for (addr = (caddr_t)trunc_page(bp->b_data); 80312936855SDavid Greenman addr < bp->b_data + bp->b_bufsize; 804f8845af0SPoul-Henning Kamp addr += PAGE_SIZE) { 8050157d6d9SJohn Dyson pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 8060157d6d9SJohn Dyson pmap_kremove((vm_offset_t) addr); 80726f9a767SRodney W. Grimes vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 80826f9a767SRodney W. Grimes } 8090157d6d9SJohn Dyson 8100157d6d9SJohn Dyson bp->b_data = bp->b_saveaddr; 8115b81b6b3SRodney W. Grimes } 8125b81b6b3SRodney W. Grimes 8135b81b6b3SRodney W. Grimes /* 8145b81b6b3SRodney W. Grimes * Force reset the processor by invalidating the entire address space! 8155b81b6b3SRodney W. Grimes */ 8167f8cb368SDavid Greenman void 817d447dbeeSBruce Evans cpu_reset() 818d447dbeeSBruce Evans { 819d447dbeeSBruce Evans 82003245f09SKATO Takenori #ifdef PC98 82103245f09SKATO Takenori /* 82203245f09SKATO Takenori * Attempt to do a CPU reset via CPU reset port. 82303245f09SKATO Takenori */ 824d447dbeeSBruce Evans disable_intr(); 82503245f09SKATO Takenori outb(0x37, 0x0f); /* SHUT0 = 0. */ 82603245f09SKATO Takenori outb(0x37, 0x0b); /* SHUT1 = 0. */ 82703245f09SKATO Takenori outb(0xf0, 0x00); /* Reset. */ 82803245f09SKATO Takenori #else 8292320728fSRodney W. Grimes /* 8302320728fSRodney W. Grimes * Attempt to do a CPU reset via the keyboard controller, 8312320728fSRodney W. Grimes * do not turn of the GateA20, as any machine that fails 8322320728fSRodney W. Grimes * to do the reset here would then end up in no man's land. 8332320728fSRodney W. Grimes */ 8345eb46edfSDavid Greenman 83503245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET) 8362320728fSRodney W. Grimes outb(IO_KBD + 4, 0xFE); 8372320728fSRodney W. Grimes DELAY(500000); /* wait 0.5 sec to see if that did it */ 8382320728fSRodney W. Grimes printf("Keyboard reset did not work, attempting CPU shutdown\n"); 8392320728fSRodney W. Grimes DELAY(1000000); /* wait 1 sec for printf to complete */ 8405eb46edfSDavid Greenman #endif 84103245f09SKATO Takenori #endif /* PC98 */ 8425b81b6b3SRodney W. Grimes /* force a shutdown by unmapping entire address space ! */ 843f8845af0SPoul-Henning Kamp bzero((caddr_t) PTD, PAGE_SIZE); 8445b81b6b3SRodney W. Grimes 8455b81b6b3SRodney W. Grimes /* "good night, sweet prince .... <THUNK!>" */ 84627e9b35eSJohn Dyson invltlb(); 8475b81b6b3SRodney W. Grimes /* NOTREACHED */ 8487f8cb368SDavid Greenman while(1); 8495b81b6b3SRodney W. Grimes } 850b9d60b3fSDavid Greenman 851b9d60b3fSDavid Greenman /* 852b9d60b3fSDavid Greenman * Grow the user stack to allow for 'sp'. This version grows the stack in 85329360eb0SDavid Greenman * chunks of SGROWSIZ. 854b9d60b3fSDavid Greenman */ 855b9d60b3fSDavid Greenman int 856b9d60b3fSDavid Greenman grow(p, sp) 857b9d60b3fSDavid Greenman struct proc *p; 85826f9a767SRodney W. Grimes u_int sp; 859b9d60b3fSDavid Greenman { 860b9d60b3fSDavid Greenman unsigned int nss; 861b9d60b3fSDavid Greenman caddr_t v; 862b9d60b3fSDavid Greenman struct vmspace *vm = p->p_vmspace; 863b9d60b3fSDavid Greenman 864b9d60b3fSDavid Greenman if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK) 865b9d60b3fSDavid Greenman return (1); 866b9d60b3fSDavid Greenman 867b9d60b3fSDavid Greenman nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE); 868b9d60b3fSDavid Greenman 869b9d60b3fSDavid Greenman if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur) 870b9d60b3fSDavid Greenman return (0); 871b9d60b3fSDavid Greenman 872b9d60b3fSDavid Greenman if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT, 87329360eb0SDavid Greenman SGROWSIZ) < nss) { 874b9d60b3fSDavid Greenman int grow_amount; 875b9d60b3fSDavid Greenman /* 876b9d60b3fSDavid Greenman * If necessary, grow the VM that the stack occupies 877b9d60b3fSDavid Greenman * to allow for the rlimit. This allows us to not have 878b9d60b3fSDavid Greenman * to allocate all of the VM up-front in execve (which 879b9d60b3fSDavid Greenman * is expensive). 880b9d60b3fSDavid Greenman * Grow the VM by the amount requested rounded up to 88129360eb0SDavid Greenman * the nearest SGROWSIZ to provide for some hysteresis. 882b9d60b3fSDavid Greenman */ 88329360eb0SDavid Greenman grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ); 884b9d60b3fSDavid Greenman v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT, 88529360eb0SDavid Greenman SGROWSIZ) - grow_amount; 886b9d60b3fSDavid Greenman /* 88729360eb0SDavid Greenman * If there isn't enough room to extend by SGROWSIZ, then 888b9d60b3fSDavid Greenman * just extend to the maximum size 889b9d60b3fSDavid Greenman */ 890b9d60b3fSDavid Greenman if (v < vm->vm_maxsaddr) { 891b9d60b3fSDavid Greenman v = vm->vm_maxsaddr; 892b9d60b3fSDavid Greenman grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT); 893b9d60b3fSDavid Greenman } 89468940ac1SDavid Greenman if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v, 895bd7e5f99SJohn Dyson grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) { 896b9d60b3fSDavid Greenman return (0); 897b9d60b3fSDavid Greenman } 898b9d60b3fSDavid Greenman vm->vm_ssize += grow_amount >> PAGE_SHIFT; 899b9d60b3fSDavid Greenman } 900b9d60b3fSDavid Greenman 901b9d60b3fSDavid Greenman return (1); 902b9d60b3fSDavid Greenman } 903a29b63cbSJohn Dyson 904a29b63cbSJohn Dyson /* 90583eab616SBruce Evans * Implement the pre-zeroed page mechanism. 90683eab616SBruce Evans * This routine is called from the idle loop. 907a29b63cbSJohn Dyson */ 908a29b63cbSJohn Dyson int 90983eab616SBruce Evans vm_page_zero_idle() 91083eab616SBruce Evans { 91183eab616SBruce Evans static int free_rover; 912a29b63cbSJohn Dyson vm_page_t m; 91383eab616SBruce Evans int s; 91483eab616SBruce Evans 91583eab616SBruce Evans #ifdef WRONG 91683eab616SBruce Evans if (cnt.v_free_count <= cnt.v_interrupt_free_min) 91783eab616SBruce Evans return (0); 91883eab616SBruce Evans #endif 91983eab616SBruce Evans /* 92083eab616SBruce Evans * XXX 9218e56e561SJohn Dyson * We stop zeroing pages when there are sufficent prezeroed pages. 9228e56e561SJohn Dyson * This threshold isn't really needed, except we want to 9238e56e561SJohn Dyson * bypass unneeded calls to vm_page_list_find, and the 9248e56e561SJohn Dyson * associated cache flush and latency. The pre-zero will 9258e56e561SJohn Dyson * still be called when there are significantly more 9268e56e561SJohn Dyson * non-prezeroed pages than zeroed pages. The threshold 9278e56e561SJohn Dyson * of half the number of reserved pages is arbitrary, but 9288e56e561SJohn Dyson * approximately the right amount. Eventually, we should 9298e56e561SJohn Dyson * perhaps interrupt the zero operation when a process 9308e56e561SJohn Dyson * is found to be ready to run. 9318e56e561SJohn Dyson */ 93283eab616SBruce Evans if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2) 93383eab616SBruce Evans return (0); 9349a3b3e8bSPeter Wemm #ifdef SMP 9359a3b3e8bSPeter Wemm get_mplock(); 9369a3b3e8bSPeter Wemm #endif 93783eab616SBruce Evans s = splvm(); 93883eab616SBruce Evans enable_intr(); 93983eab616SBruce Evans m = vm_page_list_find(PQ_FREE, free_rover); 94083eab616SBruce Evans if (m != NULL) { 9415070c7f8SJohn Dyson --(*vm_page_queues[m->queue].lcnt); 9425070c7f8SJohn Dyson TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq); 94383eab616SBruce Evans splx(s); 9449a3b3e8bSPeter Wemm #ifdef SMP 9459a3b3e8bSPeter Wemm rel_mplock(); 9469a3b3e8bSPeter Wemm #endif 947a29b63cbSJohn Dyson pmap_zero_page(VM_PAGE_TO_PHYS(m)); 9489a3b3e8bSPeter Wemm #ifdef SMP 9499a3b3e8bSPeter Wemm get_mplock(); 9509a3b3e8bSPeter Wemm #endif 95183eab616SBruce Evans (void)splvm(); 9525070c7f8SJohn Dyson m->queue = PQ_ZERO + m->pc; 9535070c7f8SJohn Dyson ++(*vm_page_queues[m->queue].lcnt); 9545070c7f8SJohn Dyson TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq); 9555070c7f8SJohn Dyson free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK; 956a316d390SJohn Dyson ++vm_page_zero_count; 957a29b63cbSJohn Dyson } 95883eab616SBruce Evans splx(s); 95983eab616SBruce Evans disable_intr(); 9609a3b3e8bSPeter Wemm #ifdef SMP 9619a3b3e8bSPeter Wemm rel_mplock(); 9629a3b3e8bSPeter Wemm #endif 96383eab616SBruce Evans return (1); 964a29b63cbSJohn Dyson } 965e0b78e19SJoerg Wunsch 966e0b78e19SJoerg Wunsch /* 967cae6f73aSJoerg Wunsch * Tell whether this address is in some physical memory region. 968e0b78e19SJoerg Wunsch * Currently used by the kernel coredump code in order to avoid 969e0b78e19SJoerg Wunsch * dumping the ``ISA memory hole'' which could cause indefinite hangs, 970e0b78e19SJoerg Wunsch * or other unpredictable behaviour. 971e0b78e19SJoerg Wunsch */ 972e0b78e19SJoerg Wunsch 973e0b78e19SJoerg Wunsch #include "isa.h" 974e0b78e19SJoerg Wunsch 975e0b78e19SJoerg Wunsch int 976cae6f73aSJoerg Wunsch is_physical_memory(addr) 977e0b78e19SJoerg Wunsch vm_offset_t addr; 978e0b78e19SJoerg Wunsch { 979e0b78e19SJoerg Wunsch 980e0b78e19SJoerg Wunsch #if NISA > 0 981e0b78e19SJoerg Wunsch /* The ISA ``memory hole''. */ 982e0b78e19SJoerg Wunsch if (addr >= 0xa0000 && addr < 0x100000) 983cae6f73aSJoerg Wunsch return 0; 984e0b78e19SJoerg Wunsch #endif 985e0b78e19SJoerg Wunsch 986e0b78e19SJoerg Wunsch /* 987e0b78e19SJoerg Wunsch * stuff other tests for known memory-mapped devices (PCI?) 988e0b78e19SJoerg Wunsch * here 989e0b78e19SJoerg Wunsch */ 990e0b78e19SJoerg Wunsch 991cae6f73aSJoerg Wunsch return 1; 992e0b78e19SJoerg Wunsch } 993