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$ 4103245f09SKATO Takenori * $Id: vm_machdep.c,v 1.78 1997/04/07 07:15:56 peter Exp $ 425b81b6b3SRodney W. Grimes */ 435b81b6b3SRodney W. Grimes 44960173b9SRodney W. Grimes #include "npx.h" 458890984dSGarrett Wollman #include "opt_bounce.h" 468890984dSGarrett Wollman 4726f9a767SRodney W. Grimes #include <sys/param.h> 4826f9a767SRodney W. Grimes #include <sys/systm.h> 4926f9a767SRodney W. Grimes #include <sys/proc.h> 5026f9a767SRodney W. Grimes #include <sys/malloc.h> 5126f9a767SRodney W. Grimes #include <sys/buf.h> 5226f9a767SRodney W. Grimes #include <sys/vnode.h> 53efeaf95aSDavid Greenman #include <sys/vmmeter.h> 545b81b6b3SRodney W. Grimes 552320728fSRodney W. Grimes #include <machine/clock.h> 56b5e8ce9fSBruce Evans #include <machine/md_var.h> 57a2a1c95cSPeter Wemm #include <machine/cpu.h> 58a2a1c95cSPeter Wemm #include <machine/reg.h> 595b81b6b3SRodney W. Grimes 6026f9a767SRodney W. Grimes #include <vm/vm.h> 61efeaf95aSDavid Greenman #include <vm/vm_param.h> 62efeaf95aSDavid Greenman #include <vm/vm_prot.h> 63996c772fSJohn Dyson #include <sys/lock.h> 6426f9a767SRodney W. Grimes #include <vm/vm_kern.h> 6524a1cce3SDavid Greenman #include <vm/vm_page.h> 66efeaf95aSDavid Greenman #include <vm/vm_map.h> 67efeaf95aSDavid Greenman #include <vm/vm_extern.h> 68efeaf95aSDavid Greenman 69efeaf95aSDavid Greenman #include <sys/user.h> 705b81b6b3SRodney W. Grimes 71e30f0011SSatoshi Asami #ifdef PC98 72e30f0011SSatoshi Asami #include <pc98/pc98/pc98.h> 73e30f0011SSatoshi Asami #else 742320728fSRodney W. Grimes #include <i386/isa/isa.h> 75e30f0011SSatoshi Asami #endif 762320728fSRodney W. Grimes 779aba88bfSDavid Greenman #ifdef BOUNCE_BUFFERS 7887b91157SPoul-Henning Kamp static vm_offset_t 796c146e28SBruce Evans vm_bounce_kva __P((int size, int waitok)); 8087b91157SPoul-Henning Kamp static void vm_bounce_kva_free __P((vm_offset_t addr, vm_offset_t size, 816c146e28SBruce Evans int now)); 8287b91157SPoul-Henning Kamp static vm_offset_t 836c146e28SBruce Evans vm_bounce_page_find __P((int count)); 8487b91157SPoul-Henning Kamp static void vm_bounce_page_free __P((vm_offset_t pa, int count)); 856c146e28SBruce Evans 8687b6de2bSPoul-Henning Kamp static volatile int kvasfreecnt; 87ae92ea44SDavid Greenman 88d5e26ef0SDavid Greenman caddr_t bouncememory; 8987b91157SPoul-Henning Kamp int bouncepages; 9087b91157SPoul-Henning Kamp static int bpwait; 9187b91157SPoul-Henning Kamp static vm_offset_t *bouncepa; 9287b91157SPoul-Henning Kamp static int bmwait, bmfreeing; 93d5e26ef0SDavid Greenman 94ed7fcbd0SDavid Greenman #define BITS_IN_UNSIGNED (8*sizeof(unsigned)) 9587b91157SPoul-Henning Kamp static int bounceallocarraysize; 9687b91157SPoul-Henning Kamp static unsigned *bounceallocarray; 9787b91157SPoul-Henning Kamp static int bouncefree; 98d5e26ef0SDavid Greenman 99e30f0011SSatoshi Asami #if defined(PC98) && defined (EPSON_BOUNCEDMA) 100e30f0011SSatoshi Asami #define SIXTEENMEG (3840*4096) /* 15MB boundary */ 101e30f0011SSatoshi Asami #else 102d5e26ef0SDavid Greenman #define SIXTEENMEG (4096*4096) 103e30f0011SSatoshi Asami #endif 104ae92ea44SDavid Greenman #define MAXBKVA 1024 105f8845af0SPoul-Henning Kamp int maxbkva = MAXBKVA*PAGE_SIZE; 106d5e26ef0SDavid Greenman 107d5e26ef0SDavid Greenman /* special list that can be used at interrupt time for eventual kva free */ 10887b91157SPoul-Henning Kamp static struct kvasfree { 109d5e26ef0SDavid Greenman vm_offset_t addr; 110d5e26ef0SDavid Greenman vm_offset_t size; 111d5e26ef0SDavid Greenman } kvaf[MAXBKVA]; 112d5e26ef0SDavid Greenman 113d5e26ef0SDavid Greenman /* 114d5e26ef0SDavid Greenman * get bounce buffer pages (count physically contiguous) 115d5e26ef0SDavid Greenman * (only 1 inplemented now) 116d5e26ef0SDavid Greenman */ 11787b91157SPoul-Henning Kamp static vm_offset_t 118d5e26ef0SDavid Greenman vm_bounce_page_find(count) 119d5e26ef0SDavid Greenman int count; 120d5e26ef0SDavid Greenman { 121d5e26ef0SDavid Greenman int bit; 122d5e26ef0SDavid Greenman int s,i; 123d5e26ef0SDavid Greenman 124d5e26ef0SDavid Greenman if (count != 1) 125d5e26ef0SDavid Greenman panic("vm_bounce_page_find -- no support for > 1 page yet!!!"); 126d5e26ef0SDavid Greenman 127d5e26ef0SDavid Greenman s = splbio(); 128d5e26ef0SDavid Greenman retry: 129d5e26ef0SDavid Greenman for (i = 0; i < bounceallocarraysize; i++) { 130d5e26ef0SDavid Greenman if (bounceallocarray[i] != 0xffffffff) { 1313fb3086eSPoul-Henning Kamp bit = ffs(~bounceallocarray[i]); 1323fb3086eSPoul-Henning Kamp if (bit) { 133d5e26ef0SDavid Greenman bounceallocarray[i] |= 1 << (bit - 1) ; 134d5e26ef0SDavid Greenman bouncefree -= count; 135d5e26ef0SDavid Greenman splx(s); 136ae92ea44SDavid Greenman return bouncepa[(i * BITS_IN_UNSIGNED + (bit - 1))]; 137d5e26ef0SDavid Greenman } 138d5e26ef0SDavid Greenman } 139d5e26ef0SDavid Greenman } 140ed7fcbd0SDavid Greenman bpwait = 1; 141d5e26ef0SDavid Greenman tsleep((caddr_t) &bounceallocarray, PRIBIO, "bncwai", 0); 142d5e26ef0SDavid Greenman goto retry; 143d5e26ef0SDavid Greenman } 144d5e26ef0SDavid Greenman 14587b91157SPoul-Henning Kamp static void 1461561d038SDavid Greenman vm_bounce_kva_free(addr, size, now) 1471561d038SDavid Greenman vm_offset_t addr; 1481561d038SDavid Greenman vm_offset_t size; 1491561d038SDavid Greenman int now; 1501561d038SDavid Greenman { 1511561d038SDavid Greenman int s = splbio(); 1521561d038SDavid Greenman kvaf[kvasfreecnt].addr = addr; 153ae92ea44SDavid Greenman kvaf[kvasfreecnt].size = size; 154ae92ea44SDavid Greenman ++kvasfreecnt; 1550e195446SDavid Greenman if( now) { 1560e195446SDavid Greenman /* 1570e195446SDavid Greenman * this will do wakeups 1580e195446SDavid Greenman */ 1591561d038SDavid Greenman vm_bounce_kva(0,0); 1600e195446SDavid Greenman } else { 1610e195446SDavid Greenman if (bmwait) { 1620e195446SDavid Greenman /* 1630e195446SDavid Greenman * if anyone is waiting on the bounce-map, then wakeup 1640e195446SDavid Greenman */ 1651561d038SDavid Greenman wakeup((caddr_t) io_map); 1660e195446SDavid Greenman bmwait = 0; 1670e195446SDavid Greenman } 1680e195446SDavid Greenman } 1691561d038SDavid Greenman splx(s); 1701561d038SDavid Greenman } 1711561d038SDavid Greenman 172d5e26ef0SDavid Greenman /* 173d5e26ef0SDavid Greenman * free count bounce buffer pages 174d5e26ef0SDavid Greenman */ 17587b91157SPoul-Henning Kamp static void 176d5e26ef0SDavid Greenman vm_bounce_page_free(pa, count) 177d5e26ef0SDavid Greenman vm_offset_t pa; 178d5e26ef0SDavid Greenman int count; 179d5e26ef0SDavid Greenman { 180d5e26ef0SDavid Greenman int allocindex; 181d5e26ef0SDavid Greenman int index; 182d5e26ef0SDavid Greenman int bit; 183d5e26ef0SDavid Greenman 184d5e26ef0SDavid Greenman if (count != 1) 185edf8a815SDavid Greenman panic("vm_bounce_page_free -- no support for > 1 page yet!!!"); 186d5e26ef0SDavid Greenman 187ae92ea44SDavid Greenman for(index=0;index<bouncepages;index++) { 188ae92ea44SDavid Greenman if( pa == bouncepa[index]) 189ae92ea44SDavid Greenman break; 190ae92ea44SDavid Greenman } 191d5e26ef0SDavid Greenman 192ae92ea44SDavid Greenman if( index == bouncepages) 193ae92ea44SDavid Greenman panic("vm_bounce_page_free: invalid bounce buffer"); 194d5e26ef0SDavid Greenman 195ed7fcbd0SDavid Greenman allocindex = index / BITS_IN_UNSIGNED; 196ed7fcbd0SDavid Greenman bit = index % BITS_IN_UNSIGNED; 197d5e26ef0SDavid Greenman 198d5e26ef0SDavid Greenman bounceallocarray[allocindex] &= ~(1 << bit); 199d5e26ef0SDavid Greenman 200d5e26ef0SDavid Greenman bouncefree += count; 201ed7fcbd0SDavid Greenman if (bpwait) { 202ed7fcbd0SDavid Greenman bpwait = 0; 203d5e26ef0SDavid Greenman wakeup((caddr_t) &bounceallocarray); 204d5e26ef0SDavid Greenman } 205ed7fcbd0SDavid Greenman } 206d5e26ef0SDavid Greenman 207d5e26ef0SDavid Greenman /* 208d5e26ef0SDavid Greenman * allocate count bounce buffer kva pages 209d5e26ef0SDavid Greenman */ 21087b91157SPoul-Henning Kamp static vm_offset_t 211ae92ea44SDavid Greenman vm_bounce_kva(size, waitok) 212ae92ea44SDavid Greenman int size; 2131561d038SDavid Greenman int waitok; 214d5e26ef0SDavid Greenman { 215d5e26ef0SDavid Greenman int i; 2166b4ac811SDavid Greenman vm_offset_t kva = 0; 217c564966bSDavid Greenman vm_offset_t off; 218d5e26ef0SDavid Greenman int s = splbio(); 219d5e26ef0SDavid Greenman more: 220ae92ea44SDavid Greenman if (!bmfreeing && kvasfreecnt) { 221d5e26ef0SDavid Greenman bmfreeing = 1; 222ae92ea44SDavid Greenman for (i = 0; i < kvasfreecnt; i++) { 223f8845af0SPoul-Henning Kamp for(off=0;off<kvaf[i].size;off+=PAGE_SIZE) { 224c564966bSDavid Greenman pmap_kremove( kvaf[i].addr + off); 225c564966bSDavid Greenman } 2261561d038SDavid Greenman kmem_free_wakeup(io_map, kvaf[i].addr, 227d5e26ef0SDavid Greenman kvaf[i].size); 228d5e26ef0SDavid Greenman } 229d5e26ef0SDavid Greenman kvasfreecnt = 0; 230d5e26ef0SDavid Greenman bmfreeing = 0; 231ae92ea44SDavid Greenman if( bmwait) { 232ae92ea44SDavid Greenman bmwait = 0; 233ae92ea44SDavid Greenman wakeup( (caddr_t) io_map); 234ae92ea44SDavid Greenman } 235d5e26ef0SDavid Greenman } 236d5e26ef0SDavid Greenman 2371561d038SDavid Greenman if( size == 0) { 2381561d038SDavid Greenman splx(s); 2396ab46d52SBruce Evans return 0; 2401561d038SDavid Greenman } 2411561d038SDavid Greenman 242ae92ea44SDavid Greenman if ((kva = kmem_alloc_pageable(io_map, size)) == 0) { 2431561d038SDavid Greenman if( !waitok) { 2441561d038SDavid Greenman splx(s); 2456ab46d52SBruce Evans return 0; 2461561d038SDavid Greenman } 247d5e26ef0SDavid Greenman bmwait = 1; 2481561d038SDavid Greenman tsleep((caddr_t) io_map, PRIBIO, "bmwait", 0); 249d5e26ef0SDavid Greenman goto more; 250d5e26ef0SDavid Greenman } 251d5e26ef0SDavid Greenman splx(s); 252d5e26ef0SDavid Greenman return kva; 253d5e26ef0SDavid Greenman } 254d5e26ef0SDavid Greenman 255d5e26ef0SDavid Greenman /* 256ae92ea44SDavid Greenman * same as vm_bounce_kva -- but really allocate (but takes pages as arg) 2570e195446SDavid Greenman */ 2580e195446SDavid Greenman vm_offset_t 2590e195446SDavid Greenman vm_bounce_kva_alloc(count) 2600e195446SDavid Greenman int count; 2610e195446SDavid Greenman { 2620e195446SDavid Greenman int i; 2630e195446SDavid Greenman vm_offset_t kva; 2640e195446SDavid Greenman vm_offset_t pa; 2650e195446SDavid Greenman if( bouncepages == 0) { 266f8845af0SPoul-Henning Kamp kva = (vm_offset_t) malloc(count*PAGE_SIZE, M_TEMP, M_WAITOK); 2670e195446SDavid Greenman return kva; 2680e195446SDavid Greenman } 269f8845af0SPoul-Henning Kamp kva = vm_bounce_kva(count*PAGE_SIZE, 1); 2700e195446SDavid Greenman for(i=0;i<count;i++) { 2710e195446SDavid Greenman pa = vm_bounce_page_find(1); 272f8845af0SPoul-Henning Kamp pmap_kenter(kva + i * PAGE_SIZE, pa); 2730e195446SDavid Greenman } 2740e195446SDavid Greenman return kva; 2750e195446SDavid Greenman } 2760e195446SDavid Greenman 2770e195446SDavid Greenman /* 2780e195446SDavid Greenman * same as vm_bounce_kva_free -- but really free 2790e195446SDavid Greenman */ 2800e195446SDavid Greenman void 2810e195446SDavid Greenman vm_bounce_kva_alloc_free(kva, count) 2820e195446SDavid Greenman vm_offset_t kva; 2830e195446SDavid Greenman int count; 2840e195446SDavid Greenman { 2850e195446SDavid Greenman int i; 2860e195446SDavid Greenman vm_offset_t pa; 2870e195446SDavid Greenman if( bouncepages == 0) { 2880e195446SDavid Greenman free((caddr_t) kva, M_TEMP); 2890e195446SDavid Greenman return; 2900e195446SDavid Greenman } 2910e195446SDavid Greenman for(i = 0; i < count; i++) { 292f8845af0SPoul-Henning Kamp pa = pmap_kextract(kva + i * PAGE_SIZE); 2930e195446SDavid Greenman vm_bounce_page_free(pa, 1); 2940e195446SDavid Greenman } 295f8845af0SPoul-Henning Kamp vm_bounce_kva_free(kva, count*PAGE_SIZE, 0); 2960e195446SDavid Greenman } 2970e195446SDavid Greenman 2980e195446SDavid Greenman /* 299d5e26ef0SDavid Greenman * do the things necessary to the struct buf to implement 300d5e26ef0SDavid Greenman * bounce buffers... inserted before the disk sort 301d5e26ef0SDavid Greenman */ 302d5e26ef0SDavid Greenman void 303d5e26ef0SDavid Greenman vm_bounce_alloc(bp) 304d5e26ef0SDavid Greenman struct buf *bp; 305d5e26ef0SDavid Greenman { 306d5e26ef0SDavid Greenman int countvmpg; 307d5e26ef0SDavid Greenman vm_offset_t vastart, vaend; 308d5e26ef0SDavid Greenman vm_offset_t vapstart, vapend; 309d5e26ef0SDavid Greenman vm_offset_t va, kva; 310d5e26ef0SDavid Greenman vm_offset_t pa; 311d5e26ef0SDavid Greenman int dobounceflag = 0; 312d5e26ef0SDavid Greenman int i; 313d5e26ef0SDavid Greenman 314d5e26ef0SDavid Greenman if (bouncepages == 0) 315d5e26ef0SDavid Greenman return; 316d5e26ef0SDavid Greenman 317ae92ea44SDavid Greenman if (bp->b_flags & B_BOUNCE) { 318ae92ea44SDavid Greenman printf("vm_bounce_alloc: called recursively???\n"); 319ae92ea44SDavid Greenman return; 3201561d038SDavid Greenman } 3211561d038SDavid Greenman 322ae92ea44SDavid Greenman if (bp->b_bufsize < bp->b_bcount) { 3233fb3086eSPoul-Henning Kamp printf( 3243fb3086eSPoul-Henning Kamp "vm_bounce_alloc: b_bufsize(0x%lx) < b_bcount(0x%lx) !!\n", 325ae92ea44SDavid Greenman bp->b_bufsize, bp->b_bcount); 326ae92ea44SDavid Greenman panic("vm_bounce_alloc"); 327ae92ea44SDavid Greenman } 328ae92ea44SDavid Greenman 329ae92ea44SDavid Greenman /* 330ae92ea44SDavid Greenman * This is not really necessary 331ae92ea44SDavid Greenman * if( bp->b_bufsize != bp->b_bcount) { 332ae92ea44SDavid Greenman * printf("size: %d, count: %d\n", bp->b_bufsize, bp->b_bcount); 333ae92ea44SDavid Greenman * } 334ae92ea44SDavid Greenman */ 335ae92ea44SDavid Greenman 336ae92ea44SDavid Greenman 33726f9a767SRodney W. Grimes vastart = (vm_offset_t) bp->b_data; 33826f9a767SRodney W. Grimes vaend = (vm_offset_t) bp->b_data + bp->b_bufsize; 339d5e26ef0SDavid Greenman 340bd7e5f99SJohn Dyson vapstart = trunc_page(vastart); 341bd7e5f99SJohn Dyson vapend = round_page(vaend); 342f8845af0SPoul-Henning Kamp countvmpg = (vapend - vapstart) / PAGE_SIZE; 343d5e26ef0SDavid Greenman 344d5e26ef0SDavid Greenman /* 345d5e26ef0SDavid Greenman * if any page is above 16MB, then go into bounce-buffer mode 346d5e26ef0SDavid Greenman */ 347d5e26ef0SDavid Greenman va = vapstart; 348d5e26ef0SDavid Greenman for (i = 0; i < countvmpg; i++) { 349ed7fcbd0SDavid Greenman pa = pmap_kextract(va); 350d5e26ef0SDavid Greenman if (pa >= SIXTEENMEG) 351d5e26ef0SDavid Greenman ++dobounceflag; 3520d94caffSDavid Greenman if( pa == 0) 3530d94caffSDavid Greenman panic("vm_bounce_alloc: Unmapped page"); 354f8845af0SPoul-Henning Kamp va += PAGE_SIZE; 355d5e26ef0SDavid Greenman } 356d5e26ef0SDavid Greenman if (dobounceflag == 0) 357d5e26ef0SDavid Greenman return; 358d5e26ef0SDavid Greenman 359d5e26ef0SDavid Greenman if (bouncepages < dobounceflag) 360d5e26ef0SDavid Greenman panic("Not enough bounce buffers!!!"); 361d5e26ef0SDavid Greenman 362d5e26ef0SDavid Greenman /* 363d5e26ef0SDavid Greenman * allocate a replacement kva for b_addr 364d5e26ef0SDavid Greenman */ 365f8845af0SPoul-Henning Kamp kva = vm_bounce_kva(countvmpg*PAGE_SIZE, 1); 366ae92ea44SDavid Greenman #if 0 367ae92ea44SDavid Greenman printf("%s: vapstart: %x, vapend: %x, countvmpg: %d, kva: %x ", 368ae92ea44SDavid Greenman (bp->b_flags & B_READ) ? "read":"write", 369ae92ea44SDavid Greenman vapstart, vapend, countvmpg, kva); 370ae92ea44SDavid Greenman #endif 371d5e26ef0SDavid Greenman va = vapstart; 372d5e26ef0SDavid Greenman for (i = 0; i < countvmpg; i++) { 373ed7fcbd0SDavid Greenman pa = pmap_kextract(va); 374d5e26ef0SDavid Greenman if (pa >= SIXTEENMEG) { 375d5e26ef0SDavid Greenman /* 376d5e26ef0SDavid Greenman * allocate a replacement page 377d5e26ef0SDavid Greenman */ 378d5e26ef0SDavid Greenman vm_offset_t bpa = vm_bounce_page_find(1); 379f8845af0SPoul-Henning Kamp pmap_kenter(kva + (PAGE_SIZE * i), bpa); 380ae92ea44SDavid Greenman #if 0 381ae92ea44SDavid Greenman printf("r(%d): (%x,%x,%x) ", i, va, pa, bpa); 382ae92ea44SDavid Greenman #endif 383d5e26ef0SDavid Greenman /* 384d5e26ef0SDavid Greenman * if we are writing, the copy the data into the page 385d5e26ef0SDavid Greenman */ 3861561d038SDavid Greenman if ((bp->b_flags & B_READ) == 0) { 387f8845af0SPoul-Henning Kamp bcopy((caddr_t) va, (caddr_t) kva + (PAGE_SIZE * i), PAGE_SIZE); 3881561d038SDavid Greenman } 389d5e26ef0SDavid Greenman } else { 390d5e26ef0SDavid Greenman /* 391d5e26ef0SDavid Greenman * use original page 392d5e26ef0SDavid Greenman */ 393f8845af0SPoul-Henning Kamp pmap_kenter(kva + (PAGE_SIZE * i), pa); 394d5e26ef0SDavid Greenman } 395f8845af0SPoul-Henning Kamp va += PAGE_SIZE; 396d5e26ef0SDavid Greenman } 397d5e26ef0SDavid Greenman 398d5e26ef0SDavid Greenman /* 399d5e26ef0SDavid Greenman * flag the buffer as being bounced 400d5e26ef0SDavid Greenman */ 401d5e26ef0SDavid Greenman bp->b_flags |= B_BOUNCE; 402d5e26ef0SDavid Greenman /* 403d5e26ef0SDavid Greenman * save the original buffer kva 404d5e26ef0SDavid Greenman */ 40526f9a767SRodney W. Grimes bp->b_savekva = bp->b_data; 406d5e26ef0SDavid Greenman /* 407d5e26ef0SDavid Greenman * put our new kva into the buffer (offset by original offset) 408d5e26ef0SDavid Greenman */ 40926f9a767SRodney W. Grimes bp->b_data = (caddr_t) (((vm_offset_t) kva) | 410f8845af0SPoul-Henning Kamp ((vm_offset_t) bp->b_savekva & PAGE_MASK)); 411ae92ea44SDavid Greenman #if 0 412ae92ea44SDavid Greenman printf("b_savekva: %x, newva: %x\n", bp->b_savekva, bp->b_data); 413ae92ea44SDavid Greenman #endif 414d5e26ef0SDavid Greenman return; 415d5e26ef0SDavid Greenman } 416d5e26ef0SDavid Greenman 417d5e26ef0SDavid Greenman /* 418d5e26ef0SDavid Greenman * hook into biodone to free bounce buffer 419d5e26ef0SDavid Greenman */ 420d5e26ef0SDavid Greenman void 421d5e26ef0SDavid Greenman vm_bounce_free(bp) 422d5e26ef0SDavid Greenman struct buf *bp; 423d5e26ef0SDavid Greenman { 424d5e26ef0SDavid Greenman int i; 425ae92ea44SDavid Greenman vm_offset_t origkva, bouncekva, bouncekvaend; 426d5e26ef0SDavid Greenman 427d5e26ef0SDavid Greenman /* 428d5e26ef0SDavid Greenman * if this isn't a bounced buffer, then just return 429d5e26ef0SDavid Greenman */ 430d5e26ef0SDavid Greenman if ((bp->b_flags & B_BOUNCE) == 0) 431d5e26ef0SDavid Greenman return; 432d5e26ef0SDavid Greenman 433ae92ea44SDavid Greenman /* 434ae92ea44SDavid Greenman * This check is not necessary 435ae92ea44SDavid Greenman * if (bp->b_bufsize != bp->b_bcount) { 436ae92ea44SDavid Greenman * printf("vm_bounce_free: b_bufsize=%d, b_bcount=%d\n", 437ae92ea44SDavid Greenman * bp->b_bufsize, bp->b_bcount); 438ae92ea44SDavid Greenman * } 439ae92ea44SDavid Greenman */ 440ae92ea44SDavid Greenman 441d5e26ef0SDavid Greenman origkva = (vm_offset_t) bp->b_savekva; 44226f9a767SRodney W. Grimes bouncekva = (vm_offset_t) bp->b_data; 443ae92ea44SDavid Greenman /* 444ae92ea44SDavid Greenman printf("free: %d ", bp->b_bufsize); 445ae92ea44SDavid Greenman */ 446d5e26ef0SDavid Greenman 447d5e26ef0SDavid Greenman /* 448d5e26ef0SDavid Greenman * check every page in the kva space for b_addr 449d5e26ef0SDavid Greenman */ 450ae92ea44SDavid Greenman for (i = 0; i < bp->b_bufsize; ) { 451d5e26ef0SDavid Greenman vm_offset_t mybouncepa; 452d5e26ef0SDavid Greenman vm_offset_t copycount; 453d5e26ef0SDavid Greenman 454bd7e5f99SJohn Dyson copycount = round_page(bouncekva + 1) - bouncekva; 455bd7e5f99SJohn Dyson mybouncepa = pmap_kextract(trunc_page(bouncekva)); 456d5e26ef0SDavid Greenman 457d5e26ef0SDavid Greenman /* 458d5e26ef0SDavid Greenman * if this is a bounced pa, then process as one 459d5e26ef0SDavid Greenman */ 460bd7e5f99SJohn Dyson if ( mybouncepa != pmap_kextract( trunc_page( origkva))) { 461ae92ea44SDavid Greenman vm_offset_t tocopy = copycount; 462ae92ea44SDavid Greenman if (i + tocopy > bp->b_bufsize) 463ae92ea44SDavid Greenman tocopy = bp->b_bufsize - i; 464d5e26ef0SDavid Greenman /* 465d5e26ef0SDavid Greenman * if this is a read, then copy from bounce buffer into original buffer 466d5e26ef0SDavid Greenman */ 467d5e26ef0SDavid Greenman if (bp->b_flags & B_READ) 468ae92ea44SDavid Greenman bcopy((caddr_t) bouncekva, (caddr_t) origkva, tocopy); 469d5e26ef0SDavid Greenman /* 470d5e26ef0SDavid Greenman * free the bounce allocation 471d5e26ef0SDavid Greenman */ 472ae92ea44SDavid Greenman 473ae92ea44SDavid Greenman /* 474ae92ea44SDavid Greenman printf("(kva: %x, pa: %x)", bouncekva, mybouncepa); 475ae92ea44SDavid Greenman */ 476ae92ea44SDavid Greenman vm_bounce_page_free(mybouncepa, 1); 477d5e26ef0SDavid Greenman } 478d5e26ef0SDavid Greenman 479d5e26ef0SDavid Greenman origkva += copycount; 480d5e26ef0SDavid Greenman bouncekva += copycount; 481ae92ea44SDavid Greenman i += copycount; 482d5e26ef0SDavid Greenman } 483d5e26ef0SDavid Greenman 484d5e26ef0SDavid Greenman /* 485ae92ea44SDavid Greenman printf("\n"); 486ae92ea44SDavid Greenman */ 487ae92ea44SDavid Greenman /* 488d5e26ef0SDavid Greenman * add the old kva into the "to free" list 489d5e26ef0SDavid Greenman */ 490ae92ea44SDavid Greenman 491bd7e5f99SJohn Dyson bouncekva= trunc_page((vm_offset_t) bp->b_data); 492bd7e5f99SJohn Dyson bouncekvaend= round_page((vm_offset_t)bp->b_data + bp->b_bufsize); 493ae92ea44SDavid Greenman 494ae92ea44SDavid Greenman /* 495f8845af0SPoul-Henning Kamp printf("freeva: %d\n", (bouncekvaend - bouncekva) / PAGE_SIZE); 496ae92ea44SDavid Greenman */ 497ae92ea44SDavid Greenman vm_bounce_kva_free( bouncekva, (bouncekvaend - bouncekva), 0); 49826f9a767SRodney W. Grimes bp->b_data = bp->b_savekva; 499d5e26ef0SDavid Greenman bp->b_savekva = 0; 500d5e26ef0SDavid Greenman bp->b_flags &= ~B_BOUNCE; 501d5e26ef0SDavid Greenman 502d5e26ef0SDavid Greenman return; 503d5e26ef0SDavid Greenman } 504d5e26ef0SDavid Greenman 505ae92ea44SDavid Greenman 5065b81b6b3SRodney W. Grimes /* 5071561d038SDavid Greenman * init the bounce buffer system 5081561d038SDavid Greenman */ 5091561d038SDavid Greenman void 5101561d038SDavid Greenman vm_bounce_init() 5111561d038SDavid Greenman { 512ae92ea44SDavid Greenman int i; 5131561d038SDavid Greenman 5141561d038SDavid Greenman kvasfreecnt = 0; 5151561d038SDavid Greenman 5161561d038SDavid Greenman if (bouncepages == 0) 5171561d038SDavid Greenman return; 5181561d038SDavid Greenman 5191561d038SDavid Greenman bounceallocarraysize = (bouncepages + BITS_IN_UNSIGNED - 1) / BITS_IN_UNSIGNED; 5201561d038SDavid Greenman bounceallocarray = malloc(bounceallocarraysize * sizeof(unsigned), M_TEMP, M_NOWAIT); 5211561d038SDavid Greenman 5221561d038SDavid Greenman if (!bounceallocarray) 523edf8a815SDavid Greenman panic("Cannot allocate bounce resource array"); 5241561d038SDavid Greenman 525ae92ea44SDavid Greenman bouncepa = malloc(bouncepages * sizeof(vm_offset_t), M_TEMP, M_NOWAIT); 526ae92ea44SDavid Greenman if (!bouncepa) 527edf8a815SDavid Greenman panic("Cannot allocate physical memory array"); 5281561d038SDavid Greenman 5290d94caffSDavid Greenman for(i=0;i<bounceallocarraysize;i++) { 5300d94caffSDavid Greenman bounceallocarray[i] = 0xffffffff; 5310d94caffSDavid Greenman } 5320d94caffSDavid Greenman 533ae92ea44SDavid Greenman for(i=0;i<bouncepages;i++) { 534ae92ea44SDavid Greenman vm_offset_t pa; 535f8845af0SPoul-Henning Kamp if( (pa = pmap_kextract((vm_offset_t) bouncememory + i * PAGE_SIZE)) >= SIXTEENMEG) 536ae92ea44SDavid Greenman panic("bounce memory out of range"); 537ae92ea44SDavid Greenman if( pa == 0) 538ae92ea44SDavid Greenman panic("bounce memory not resident"); 539ae92ea44SDavid Greenman bouncepa[i] = pa; 5400d94caffSDavid Greenman bounceallocarray[i/(8*sizeof(int))] &= ~(1<<(i%(8*sizeof(int)))); 541ae92ea44SDavid Greenman } 5421561d038SDavid Greenman bouncefree = bouncepages; 5431561d038SDavid Greenman 5441561d038SDavid Greenman } 5459aba88bfSDavid Greenman #endif /* BOUNCE_BUFFERS */ 5466c146e28SBruce Evans 547a4f7a4c9SDavid Greenman /* 548a4f7a4c9SDavid Greenman * quick version of vm_fault 549a4f7a4c9SDavid Greenman */ 550dca51299SJohn Dyson void 551a4f7a4c9SDavid Greenman vm_fault_quick(v, prot) 5526c146e28SBruce Evans caddr_t v; 553a4f7a4c9SDavid Greenman int prot; 554a4f7a4c9SDavid Greenman { 5551d1b971bSDavid Greenman if (prot & VM_PROT_WRITE) 5566c146e28SBruce Evans subyte(v, fubyte(v)); 557a4f7a4c9SDavid Greenman else 5586c146e28SBruce Evans fubyte(v); 559a4f7a4c9SDavid Greenman } 560a4f7a4c9SDavid Greenman 5611561d038SDavid Greenman /* 5625b81b6b3SRodney W. Grimes * Finish a fork operation, with process p2 nearly set up. 563a2a1c95cSPeter Wemm * Copy and update the pcb, set up the stack so that the child 564a2a1c95cSPeter Wemm * ready to run and return to user mode. 5655b81b6b3SRodney W. Grimes */ 566a2a1c95cSPeter Wemm void 5675b81b6b3SRodney W. Grimes cpu_fork(p1, p2) 5685b81b6b3SRodney W. Grimes register struct proc *p1, *p2; 5695b81b6b3SRodney W. Grimes { 570f83a01b6SDavid Greenman struct pcb *pcb2 = &p2->p_addr->u_pcb; 5715b81b6b3SRodney W. Grimes 5725b81b6b3SRodney W. Grimes /* 573a2a1c95cSPeter Wemm * copy current pcb, and save current context into it while it's 574a2a1c95cSPeter Wemm * possibly in some writeback cache line. 5755b81b6b3SRodney W. Grimes */ 576a2a1c95cSPeter Wemm bcopy(&p1->p_addr->u_pcb, pcb2, sizeof(struct pcb)); 577f83a01b6SDavid Greenman pcb2->pcb_cr3 = vtophys(p2->p_vmspace->vm_pmap.pm_pdir); 578a2a1c95cSPeter Wemm savectx(pcb2); /* irrelevant? fp registers? */ 579a2a1c95cSPeter Wemm 580a2a1c95cSPeter Wemm /* 581a2a1c95cSPeter Wemm * Create a new fresh stack for the new process. 582a2a1c95cSPeter Wemm * Copy the trap frame for the return to user mode as if from a syscall. 583a2a1c95cSPeter Wemm * This copies the user mode register values. 584a2a1c95cSPeter Wemm */ 585a2a1c95cSPeter Wemm p2->p_md.md_regs = (int *)(((struct trapframe *) 586a2a1c95cSPeter Wemm ((int)p2->p_addr + (UPAGES * PAGE_SIZE))) - 1); 587a2a1c95cSPeter Wemm bcopy(p1->p_md.md_regs, p2->p_md.md_regs, sizeof(struct trapframe)); 588a2a1c95cSPeter Wemm 589a2a1c95cSPeter Wemm /* 590a2a1c95cSPeter Wemm * Set registers for trampoline to user mode. Leave space for the 591a2a1c95cSPeter Wemm * return address on stack. These are the kernel mode register values. 592a2a1c95cSPeter Wemm */ 593a2a1c95cSPeter Wemm /* XXX these overwrite most of the regs from savectx() above! */ 594a2a1c95cSPeter Wemm pcb2->pcb_eip = (int)fork_trampoline; 595a2a1c95cSPeter Wemm pcb2->pcb_esi = (int)fork_return; 596a2a1c95cSPeter Wemm pcb2->pcb_ebx = (int)p2; 597a2a1c95cSPeter Wemm pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *); 5985b81b6b3SRodney W. Grimes 5998c39a127SStefan Eßer #ifdef USER_LDT 6008c39a127SStefan Eßer /* Copy the LDT, if necessary. */ 601a2a1c95cSPeter Wemm if (pcb2->pcb_ldt != 0) { 6028c39a127SStefan Eßer union descriptor *new_ldt; 603a2a1c95cSPeter Wemm size_t len = pcb2->pcb_ldt_len * sizeof(union descriptor); 6048c39a127SStefan Eßer 6058c39a127SStefan Eßer new_ldt = (union descriptor *)kmem_alloc(kernel_map, len); 606a2a1c95cSPeter Wemm bcopy(pcb2->pcb_ldt, new_ldt, len); 607a2a1c95cSPeter Wemm pcb2->pcb_ldt = (caddr_t)new_ldt; 6088c39a127SStefan Eßer } 6098c39a127SStefan Eßer #endif 6108c39a127SStefan Eßer 611a2a1c95cSPeter Wemm /* 612a2a1c95cSPeter Wemm * Now, cpu_switch() can schedule the new process. 613a2a1c95cSPeter Wemm * pcb_esp is loaded pointing to the cpu_switch() stack frame 614a2a1c95cSPeter Wemm * containing the return address when exiting cpu_switch. 615a2a1c95cSPeter Wemm * This will normally be to proc_trampoline(), which will have 616a2a1c95cSPeter Wemm * %ebx loaded with the new proc's pointer. proc_trampoline() 617a2a1c95cSPeter Wemm * will set up a stack to call fork_return(p, frame); to complete 618a2a1c95cSPeter Wemm * the return to user-mode. 619a2a1c95cSPeter Wemm */ 620a2a1c95cSPeter Wemm } 621a2a1c95cSPeter Wemm 622a2a1c95cSPeter Wemm /* 623a2a1c95cSPeter Wemm * Intercept the return address from a freshly forked process that has NOT 624a2a1c95cSPeter Wemm * been scheduled yet. 625a2a1c95cSPeter Wemm * 626a2a1c95cSPeter Wemm * This is needed to make kernel threads stay in kernel mode. 627a2a1c95cSPeter Wemm */ 628a2a1c95cSPeter Wemm void 629a2a1c95cSPeter Wemm cpu_set_fork_handler(p, func, arg) 630a2a1c95cSPeter Wemm struct proc *p; 631a2a1c95cSPeter Wemm void (*func) __P((void *)); 632a2a1c95cSPeter Wemm void *arg; 633a2a1c95cSPeter Wemm { 634a2a1c95cSPeter Wemm /* 635a2a1c95cSPeter Wemm * Note that the trap frame follows the args, so the function 636a2a1c95cSPeter Wemm * is really called like this: func(arg, frame); 637a2a1c95cSPeter Wemm */ 638a2a1c95cSPeter Wemm p->p_addr->u_pcb.pcb_esi = (int) func; /* function */ 639a2a1c95cSPeter Wemm p->p_addr->u_pcb.pcb_ebx = (int) arg; /* first arg */ 6405b81b6b3SRodney W. Grimes } 6415b81b6b3SRodney W. Grimes 6427c2b54e8SNate Williams void 6435b81b6b3SRodney W. Grimes cpu_exit(p) 6445b81b6b3SRodney W. Grimes register struct proc *p; 6455b81b6b3SRodney W. Grimes { 6460dbf6d73SJordan K. Hubbard #ifdef USER_LDT 6470dbf6d73SJordan K. Hubbard struct pcb *pcb; 6480dbf6d73SJordan K. Hubbard #endif 6495b81b6b3SRodney W. Grimes 650960173b9SRodney W. Grimes #if NNPX > 0 6515b81b6b3SRodney W. Grimes npxexit(p); 652960173b9SRodney W. Grimes #endif /* NNPX */ 6530dbf6d73SJordan K. Hubbard #ifdef USER_LDT 6540dbf6d73SJordan K. Hubbard pcb = &p->p_addr->u_pcb; 6550dbf6d73SJordan K. Hubbard if (pcb->pcb_ldt != 0) { 6560dbf6d73SJordan K. Hubbard if (pcb == curpcb) 6570dbf6d73SJordan K. Hubbard lldt(GSEL(GUSERLDT_SEL, SEL_KPL)); 6580dbf6d73SJordan K. Hubbard kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt, 6590dbf6d73SJordan K. Hubbard pcb->pcb_ldt_len * sizeof(union descriptor)); 6600dbf6d73SJordan K. Hubbard pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0; 6610dbf6d73SJordan K. Hubbard } 6620dbf6d73SJordan K. Hubbard #endif 6631a051896SBruce Evans cnt.v_swtch++; 6641a051896SBruce Evans cpu_switch(p); 6657c2b54e8SNate Williams panic("cpu_exit"); 6665b81b6b3SRodney W. Grimes } 6675b81b6b3SRodney W. Grimes 668381fe1aaSGarrett Wollman void 669bd7e5f99SJohn Dyson cpu_wait(p) 670bd7e5f99SJohn Dyson struct proc *p; 671bd7e5f99SJohn Dyson { 6725b81b6b3SRodney W. Grimes /* drop per-process resources */ 673675878e7SJohn Dyson pmap_dispose_proc(p); 6747f8cb368SDavid Greenman vmspace_free(p->p_vmspace); 6755b81b6b3SRodney W. Grimes } 6765b81b6b3SRodney W. Grimes 6775b81b6b3SRodney W. Grimes /* 67826f9a767SRodney W. Grimes * Dump the machine specific header information at the start of a core dump. 67926f9a767SRodney W. Grimes */ 68026f9a767SRodney W. Grimes int 68126f9a767SRodney W. Grimes cpu_coredump(p, vp, cred) 68226f9a767SRodney W. Grimes struct proc *p; 68326f9a767SRodney W. Grimes struct vnode *vp; 68426f9a767SRodney W. Grimes struct ucred *cred; 68526f9a767SRodney W. Grimes { 68626f9a767SRodney W. Grimes 68726f9a767SRodney W. Grimes return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES), 68826f9a767SRodney W. Grimes (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, 68926f9a767SRodney W. Grimes p)); 69026f9a767SRodney W. Grimes } 69126f9a767SRodney W. Grimes 69287b6de2bSPoul-Henning Kamp #ifdef notyet 69387b91157SPoul-Henning Kamp static void 6945b81b6b3SRodney W. Grimes setredzone(pte, vaddr) 6955b81b6b3SRodney W. Grimes u_short *pte; 6965b81b6b3SRodney W. Grimes caddr_t vaddr; 6975b81b6b3SRodney W. Grimes { 6985b81b6b3SRodney W. Grimes /* eventually do this by setting up an expand-down stack segment 6995b81b6b3SRodney W. Grimes for ss0: selector, allowing stack access down to top of u. 7005b81b6b3SRodney W. Grimes this means though that protection violations need to be handled 7015b81b6b3SRodney W. Grimes thru a double fault exception that must do an integral task 7025b81b6b3SRodney W. Grimes switch to a known good context, within which a dump can be 7035b81b6b3SRodney W. Grimes taken. a sensible scheme might be to save the initial context 7045b81b6b3SRodney W. Grimes used by sched (that has physical memory mapped 1:1 at bottom) 7055b81b6b3SRodney W. Grimes and take the dump while still in mapped mode */ 7065b81b6b3SRodney W. Grimes } 70787b6de2bSPoul-Henning Kamp #endif 70826f9a767SRodney W. Grimes 70926f9a767SRodney W. Grimes /* 7105b81b6b3SRodney W. Grimes * Convert kernel VA to physical address 7115b81b6b3SRodney W. Grimes */ 712aaf08d94SGarrett Wollman u_long 7137f8cb368SDavid Greenman kvtop(void *addr) 7145b81b6b3SRodney W. Grimes { 7155b81b6b3SRodney W. Grimes vm_offset_t va; 7165b81b6b3SRodney W. Grimes 717ed7fcbd0SDavid Greenman va = pmap_kextract((vm_offset_t)addr); 7185b81b6b3SRodney W. Grimes if (va == 0) 7195b81b6b3SRodney W. Grimes panic("kvtop: zero page frame"); 7207f8cb368SDavid Greenman return((int)va); 7215b81b6b3SRodney W. Grimes } 7225b81b6b3SRodney W. Grimes 7235b81b6b3SRodney W. Grimes /* 724ac322158SDavid Greenman * Map an IO request into kernel virtual address space. 7255b81b6b3SRodney W. Grimes * 726ac322158SDavid Greenman * All requests are (re)mapped into kernel VA space. 727ac322158SDavid Greenman * Notice that we use b_bufsize for the size of the buffer 728ac322158SDavid Greenman * to be mapped. b_bcount might be modified by the driver. 7295b81b6b3SRodney W. Grimes */ 730381fe1aaSGarrett Wollman void 7315b81b6b3SRodney W. Grimes vmapbuf(bp) 7325b81b6b3SRodney W. Grimes register struct buf *bp; 7335b81b6b3SRodney W. Grimes { 7340157d6d9SJohn Dyson register caddr_t addr, v, kva; 73507658526SPoul-Henning Kamp vm_offset_t pa; 7365b81b6b3SRodney W. Grimes 73726f9a767SRodney W. Grimes if ((bp->b_flags & B_PHYS) == 0) 7385b81b6b3SRodney W. Grimes panic("vmapbuf"); 73926f9a767SRodney W. Grimes 74012936855SDavid Greenman for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 74126f9a767SRodney W. Grimes addr < bp->b_data + bp->b_bufsize; 7420157d6d9SJohn Dyson addr += PAGE_SIZE, v += PAGE_SIZE) { 74326f9a767SRodney W. Grimes /* 744ac269d78SDavid Greenman * Do the vm_fault if needed; do the copy-on-write thing 745ac269d78SDavid Greenman * when reading stuff off device into memory. 74626f9a767SRodney W. Grimes */ 74726f9a767SRodney W. Grimes vm_fault_quick(addr, 74826f9a767SRodney W. Grimes (bp->b_flags&B_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 7490157d6d9SJohn Dyson pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 750641a27e6SJohn Dyson if (pa == 0) 751641a27e6SJohn Dyson panic("vmapbuf: page not present"); 75226f9a767SRodney W. Grimes vm_page_hold(PHYS_TO_VM_PAGE(pa)); 7530157d6d9SJohn Dyson pmap_kenter((vm_offset_t) v, pa); 75426f9a767SRodney W. Grimes } 75526f9a767SRodney W. Grimes 7560157d6d9SJohn Dyson kva = bp->b_saveaddr; 7570157d6d9SJohn Dyson bp->b_saveaddr = bp->b_data; 7580157d6d9SJohn Dyson bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 7595b81b6b3SRodney W. Grimes } 7605b81b6b3SRodney W. Grimes 7615b81b6b3SRodney W. Grimes /* 7625b81b6b3SRodney W. Grimes * Free the io map PTEs associated with this IO operation. 7635b81b6b3SRodney W. Grimes * We also invalidate the TLB entries and restore the original b_addr. 7645b81b6b3SRodney W. Grimes */ 765381fe1aaSGarrett Wollman void 7665b81b6b3SRodney W. Grimes vunmapbuf(bp) 7675b81b6b3SRodney W. Grimes register struct buf *bp; 7685b81b6b3SRodney W. Grimes { 769c564966bSDavid Greenman register caddr_t addr; 77007658526SPoul-Henning Kamp vm_offset_t pa; 7715b81b6b3SRodney W. Grimes 7725b81b6b3SRodney W. Grimes if ((bp->b_flags & B_PHYS) == 0) 7735b81b6b3SRodney W. Grimes panic("vunmapbuf"); 774c564966bSDavid Greenman 77512936855SDavid Greenman for (addr = (caddr_t)trunc_page(bp->b_data); 77612936855SDavid Greenman addr < bp->b_data + bp->b_bufsize; 777f8845af0SPoul-Henning Kamp addr += PAGE_SIZE) { 7780157d6d9SJohn Dyson pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 7790157d6d9SJohn Dyson pmap_kremove((vm_offset_t) addr); 78026f9a767SRodney W. Grimes vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 78126f9a767SRodney W. Grimes } 7820157d6d9SJohn Dyson 7830157d6d9SJohn Dyson bp->b_data = bp->b_saveaddr; 7845b81b6b3SRodney W. Grimes } 7855b81b6b3SRodney W. Grimes 7865b81b6b3SRodney W. Grimes /* 7875b81b6b3SRodney W. Grimes * Force reset the processor by invalidating the entire address space! 7885b81b6b3SRodney W. Grimes */ 7897f8cb368SDavid Greenman void 7905b81b6b3SRodney W. Grimes cpu_reset() { 79103245f09SKATO Takenori #ifdef PC98 79203245f09SKATO Takenori /* 79303245f09SKATO Takenori * Attempt to do a CPU reset via CPU reset port. 79403245f09SKATO Takenori */ 79503245f09SKATO Takenori asm("cli"); 79603245f09SKATO Takenori outb(0x37, 0x0f); /* SHUT0 = 0. */ 79703245f09SKATO Takenori outb(0x37, 0x0b); /* SHUT1 = 0. */ 79803245f09SKATO Takenori outb(0xf0, 0x00); /* Reset. */ 79903245f09SKATO Takenori #else 8002320728fSRodney W. Grimes /* 8012320728fSRodney W. Grimes * Attempt to do a CPU reset via the keyboard controller, 8022320728fSRodney W. Grimes * do not turn of the GateA20, as any machine that fails 8032320728fSRodney W. Grimes * to do the reset here would then end up in no man's land. 8042320728fSRodney W. Grimes */ 8055eb46edfSDavid Greenman 80603245f09SKATO Takenori #if !defined(BROKEN_KEYBOARD_RESET) 8072320728fSRodney W. Grimes outb(IO_KBD + 4, 0xFE); 8082320728fSRodney W. Grimes DELAY(500000); /* wait 0.5 sec to see if that did it */ 8092320728fSRodney W. Grimes printf("Keyboard reset did not work, attempting CPU shutdown\n"); 8102320728fSRodney W. Grimes DELAY(1000000); /* wait 1 sec for printf to complete */ 8115eb46edfSDavid Greenman #endif 81203245f09SKATO Takenori #endif /* PC98 */ 8135b81b6b3SRodney W. Grimes /* force a shutdown by unmapping entire address space ! */ 814f8845af0SPoul-Henning Kamp bzero((caddr_t) PTD, PAGE_SIZE); 8155b81b6b3SRodney W. Grimes 8165b81b6b3SRodney W. Grimes /* "good night, sweet prince .... <THUNK!>" */ 81727e9b35eSJohn Dyson invltlb(); 8185b81b6b3SRodney W. Grimes /* NOTREACHED */ 8197f8cb368SDavid Greenman while(1); 8205b81b6b3SRodney W. Grimes } 821b9d60b3fSDavid Greenman 822b9d60b3fSDavid Greenman /* 823b9d60b3fSDavid Greenman * Grow the user stack to allow for 'sp'. This version grows the stack in 82429360eb0SDavid Greenman * chunks of SGROWSIZ. 825b9d60b3fSDavid Greenman */ 826b9d60b3fSDavid Greenman int 827b9d60b3fSDavid Greenman grow(p, sp) 828b9d60b3fSDavid Greenman struct proc *p; 82926f9a767SRodney W. Grimes u_int sp; 830b9d60b3fSDavid Greenman { 831b9d60b3fSDavid Greenman unsigned int nss; 832b9d60b3fSDavid Greenman caddr_t v; 833b9d60b3fSDavid Greenman struct vmspace *vm = p->p_vmspace; 834b9d60b3fSDavid Greenman 835b9d60b3fSDavid Greenman if ((caddr_t)sp <= vm->vm_maxsaddr || (unsigned)sp >= (unsigned)USRSTACK) 836b9d60b3fSDavid Greenman return (1); 837b9d60b3fSDavid Greenman 838b9d60b3fSDavid Greenman nss = roundup(USRSTACK - (unsigned)sp, PAGE_SIZE); 839b9d60b3fSDavid Greenman 840b9d60b3fSDavid Greenman if (nss > p->p_rlimit[RLIMIT_STACK].rlim_cur) 841b9d60b3fSDavid Greenman return (0); 842b9d60b3fSDavid Greenman 843b9d60b3fSDavid Greenman if (vm->vm_ssize && roundup(vm->vm_ssize << PAGE_SHIFT, 84429360eb0SDavid Greenman SGROWSIZ) < nss) { 845b9d60b3fSDavid Greenman int grow_amount; 846b9d60b3fSDavid Greenman /* 847b9d60b3fSDavid Greenman * If necessary, grow the VM that the stack occupies 848b9d60b3fSDavid Greenman * to allow for the rlimit. This allows us to not have 849b9d60b3fSDavid Greenman * to allocate all of the VM up-front in execve (which 850b9d60b3fSDavid Greenman * is expensive). 851b9d60b3fSDavid Greenman * Grow the VM by the amount requested rounded up to 85229360eb0SDavid Greenman * the nearest SGROWSIZ to provide for some hysteresis. 853b9d60b3fSDavid Greenman */ 85429360eb0SDavid Greenman grow_amount = roundup((nss - (vm->vm_ssize << PAGE_SHIFT)), SGROWSIZ); 855b9d60b3fSDavid Greenman v = (char *)USRSTACK - roundup(vm->vm_ssize << PAGE_SHIFT, 85629360eb0SDavid Greenman SGROWSIZ) - grow_amount; 857b9d60b3fSDavid Greenman /* 85829360eb0SDavid Greenman * If there isn't enough room to extend by SGROWSIZ, then 859b9d60b3fSDavid Greenman * just extend to the maximum size 860b9d60b3fSDavid Greenman */ 861b9d60b3fSDavid Greenman if (v < vm->vm_maxsaddr) { 862b9d60b3fSDavid Greenman v = vm->vm_maxsaddr; 863b9d60b3fSDavid Greenman grow_amount = MAXSSIZ - (vm->vm_ssize << PAGE_SHIFT); 864b9d60b3fSDavid Greenman } 86568940ac1SDavid Greenman if ((grow_amount == 0) || (vm_map_find(&vm->vm_map, NULL, 0, (vm_offset_t *)&v, 866bd7e5f99SJohn Dyson grow_amount, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0) != KERN_SUCCESS)) { 867b9d60b3fSDavid Greenman return (0); 868b9d60b3fSDavid Greenman } 869b9d60b3fSDavid Greenman vm->vm_ssize += grow_amount >> PAGE_SHIFT; 870b9d60b3fSDavid Greenman } 871b9d60b3fSDavid Greenman 872b9d60b3fSDavid Greenman return (1); 873b9d60b3fSDavid Greenman } 874a29b63cbSJohn Dyson 875a29b63cbSJohn Dyson /* 87683eab616SBruce Evans * Implement the pre-zeroed page mechanism. 87783eab616SBruce Evans * This routine is called from the idle loop. 878a29b63cbSJohn Dyson */ 879a29b63cbSJohn Dyson int 88083eab616SBruce Evans vm_page_zero_idle() 88183eab616SBruce Evans { 88283eab616SBruce Evans static int free_rover; 883a29b63cbSJohn Dyson vm_page_t m; 88483eab616SBruce Evans int s; 88583eab616SBruce Evans 88683eab616SBruce Evans #ifdef WRONG 88783eab616SBruce Evans if (cnt.v_free_count <= cnt.v_interrupt_free_min) 88883eab616SBruce Evans return (0); 88983eab616SBruce Evans #endif 89083eab616SBruce Evans /* 89183eab616SBruce Evans * XXX 8928e56e561SJohn Dyson * We stop zeroing pages when there are sufficent prezeroed pages. 8938e56e561SJohn Dyson * This threshold isn't really needed, except we want to 8948e56e561SJohn Dyson * bypass unneeded calls to vm_page_list_find, and the 8958e56e561SJohn Dyson * associated cache flush and latency. The pre-zero will 8968e56e561SJohn Dyson * still be called when there are significantly more 8978e56e561SJohn Dyson * non-prezeroed pages than zeroed pages. The threshold 8988e56e561SJohn Dyson * of half the number of reserved pages is arbitrary, but 8998e56e561SJohn Dyson * approximately the right amount. Eventually, we should 9008e56e561SJohn Dyson * perhaps interrupt the zero operation when a process 9018e56e561SJohn Dyson * is found to be ready to run. 9028e56e561SJohn Dyson */ 90383eab616SBruce Evans if (cnt.v_free_count - vm_page_zero_count <= cnt.v_free_reserved / 2) 90483eab616SBruce Evans return (0); 90583eab616SBruce Evans s = splvm(); 90683eab616SBruce Evans enable_intr(); 90783eab616SBruce Evans m = vm_page_list_find(PQ_FREE, free_rover); 90883eab616SBruce Evans if (m != NULL) { 9095070c7f8SJohn Dyson --(*vm_page_queues[m->queue].lcnt); 9105070c7f8SJohn Dyson TAILQ_REMOVE(vm_page_queues[m->queue].pl, m, pageq); 91183eab616SBruce Evans splx(s); 912a29b63cbSJohn Dyson pmap_zero_page(VM_PAGE_TO_PHYS(m)); 91383eab616SBruce Evans (void)splvm(); 9145070c7f8SJohn Dyson m->queue = PQ_ZERO + m->pc; 9155070c7f8SJohn Dyson ++(*vm_page_queues[m->queue].lcnt); 9165070c7f8SJohn Dyson TAILQ_INSERT_HEAD(vm_page_queues[m->queue].pl, m, pageq); 9175070c7f8SJohn Dyson free_rover = (free_rover + PQ_PRIME3) & PQ_L2_MASK; 918a316d390SJohn Dyson ++vm_page_zero_count; 919a29b63cbSJohn Dyson } 92083eab616SBruce Evans splx(s); 92183eab616SBruce Evans disable_intr(); 92283eab616SBruce Evans return (1); 923a29b63cbSJohn Dyson } 924