vm_machdep.c revision 119563
1124208Sdes/*- 2124208Sdes * Copyright (c) 1982, 1986 The Regents of the University of California. 3124208Sdes * Copyright (c) 1989, 1990 William Jolitz 4124208Sdes * Copyright (c) 1994 John Dyson 5124208Sdes * All rights reserved. 6124208Sdes * 7124208Sdes * This code is derived from software contributed to Berkeley by 8124208Sdes * the Systems Programming Group of the University of Utah Computer 9124208Sdes * Science Department, and William Jolitz. 10124208Sdes * 11124208Sdes * Redistribution and use in source and binary forms, with or without 12124208Sdes * modification, are permitted provided that the following conditions 13124208Sdes * are met: 14124208Sdes * 1. Redistributions of source code must retain the above copyright 15124208Sdes * notice, this list of conditions and the following disclaimer. 16124208Sdes * 2. Redistributions in binary form must reproduce the above copyright 17124208Sdes * notice, this list of conditions and the following disclaimer in the 18124208Sdes * documentation and/or other materials provided with the distribution. 19124208Sdes * 3. All advertising materials mentioning features or use of this software 20124208Sdes * must display the following acknowledgement: 21124208Sdes * This product includes software developed by the University of 22124208Sdes * California, Berkeley and its contributors. 23124208Sdes * 4. Neither the name of the University nor the names of its contributors 24124208Sdes * may be used to endorse or promote products derived from this software 25124208Sdes * without specific prior written permission. 26157016Sdes * 27157016Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28157016Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29157016Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30157016Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31162852Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32124208Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33124208Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34162852Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35162852Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36162852Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37162852Sdes * SUCH DAMAGE. 38124208Sdes * 39124208Sdes * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 40124208Sdes * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 41124208Sdes * $FreeBSD: head/sys/powerpc/aim/vm_machdep.c 119563 2003-08-29 20:04:10Z alc $ 42124208Sdes */ 43124208Sdes/* 44124208Sdes * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. 45124208Sdes * All rights reserved. 46124208Sdes * 47124208Sdes * Author: Chris G. Demetriou 48124208Sdes * 49124208Sdes * Permission to use, copy, modify and distribute this software and 50124208Sdes * its documentation is hereby granted, provided that both the copyright 51124208Sdes * notice and this permission notice appear in all copies of the 52124208Sdes * software, derivative works or modified versions, and any portions 53124208Sdes * thereof, and that both notices appear in supporting documentation. 54124208Sdes * 55124208Sdes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56124208Sdes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57124208Sdes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58124208Sdes * 59124208Sdes * Carnegie Mellon requests users of this software to return to 60124208Sdes * 61124208Sdes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62124208Sdes * School of Computer Science 63124208Sdes * Carnegie Mellon University 64124208Sdes * Pittsburgh PA 15213-3890 65124208Sdes * 66124208Sdes * any improvements or extensions that they make and grant Carnegie the 67124208Sdes * rights to redistribute these changes. 68124208Sdes */ 69124208Sdes 70124208Sdes#include "opt_kstack_pages.h" 71124208Sdes 72124208Sdes#include <sys/param.h> 73124208Sdes#include <sys/systm.h> 74124208Sdes#include <sys/proc.h> 75124208Sdes#include <sys/malloc.h> 76124208Sdes#include <sys/bio.h> 77124208Sdes#include <sys/buf.h> 78124208Sdes#include <sys/ktr.h> 79124208Sdes#include <sys/lock.h> 80124208Sdes#include <sys/mutex.h> 81124208Sdes#include <sys/vnode.h> 82124208Sdes#include <sys/vmmeter.h> 83124208Sdes#include <sys/kernel.h> 84124208Sdes#include <sys/mbuf.h> 85124208Sdes#include <sys/socketvar.h> 86124208Sdes#include <sys/sysctl.h> 87124208Sdes#include <sys/unistd.h> 88124208Sdes 89124208Sdes#include <machine/clock.h> 90124208Sdes#include <machine/cpu.h> 91124208Sdes#include <machine/fpu.h> 92124208Sdes#include <machine/frame.h> 93124208Sdes#include <machine/md_var.h> 94124208Sdes 95124208Sdes#include <dev/ofw/openfirm.h> 96124208Sdes 97124208Sdes#include <vm/vm.h> 98124208Sdes#include <vm/vm_param.h> 99124208Sdes#include <vm/vm_kern.h> 100124208Sdes#include <vm/vm_page.h> 101124208Sdes#include <vm/vm_map.h> 102124208Sdes#include <vm/vm_extern.h> 103124208Sdes 104124208Sdes#include <sys/user.h> 105124208Sdes 106124208Sdesstatic void sf_buf_init(void *arg); 107124208SdesSYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL) 108124208Sdes 109124208Sdes/* 110124208Sdes * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the 111124208Sdes * sf_freelist head with the sf_lock mutex. 112124208Sdes */ 113124208Sdesstatic struct { 114124208Sdes SLIST_HEAD(, sf_buf) sf_head; 115124208Sdes struct mtx sf_lock; 116124208Sdes} sf_freelist; 117124208Sdes 118124208Sdesstatic u_int sf_buf_alloc_want; 119124208Sdes 120124208Sdes/* 121124208Sdes * Finish a fork operation, with process p2 nearly set up. 122124208Sdes * Copy and update the pcb, set up the stack so that the child 123124208Sdes * ready to run and return to user mode. 124124208Sdes */ 125124208Sdesvoid 126124208Sdescpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) 127124208Sdes{ 128124208Sdes struct proc *p1; 129124208Sdes struct trapframe *tf; 130124208Sdes struct callframe *cf; 131124208Sdes struct pcb *pcb; 132124208Sdes 133124208Sdes KASSERT(td1 == curthread || td1 == &thread0, 134124208Sdes ("cpu_fork: p1 not curproc and not proc0")); 135124208Sdes CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags); 136124208Sdes 137124208Sdes if ((flags & RFPROC) == 0) 138124208Sdes return; 139124208Sdes 140124208Sdes p1 = td1->td_proc; 141124208Sdes 142124208Sdes pcb = (struct pcb *)((td2->td_kstack + KSTACK_PAGES * PAGE_SIZE - 143124208Sdes sizeof(struct pcb)) & ~0x2fU); 144124208Sdes td2->td_pcb = pcb; 145124208Sdes 146124208Sdes /* Copy the pcb */ 147124208Sdes bcopy(td1->td_pcb, pcb, sizeof(struct pcb)); 148124208Sdes 149124208Sdes /* 150124208Sdes * Create a fresh stack for the new process. 151124208Sdes * Copy the trap frame for the return to user mode as if from a 152124208Sdes * syscall. This copies most of the user mode register values. 153124208Sdes */ 154124208Sdes tf = (struct trapframe *)pcb - 1; 155124208Sdes bcopy(td1->td_frame, tf, sizeof(*tf)); 156124208Sdes 157124208Sdes /* Set up trap frame. */ 158124208Sdes tf->fixreg[FIRSTARG] = 0; 159124208Sdes tf->fixreg[FIRSTARG + 1] = 0; 160124208Sdes tf->cr &= ~0x10000000; 161124208Sdes 162124208Sdes td2->td_frame = tf; 163124208Sdes 164157016Sdes cf = (struct callframe *)tf - 1; 165 cf->cf_func = (register_t)fork_return; 166 cf->cf_arg0 = (register_t)td2; 167 cf->cf_arg1 = (register_t)tf; 168 169 pcb->pcb_sp = (register_t)cf; 170 pcb->pcb_lr = (register_t)fork_trampoline; 171 pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR]; 172 173 /* 174 * Now cpu_switch() can schedule the new process. 175 */ 176} 177 178/* 179 * Intercept the return address from a freshly forked process that has NOT 180 * been scheduled yet. 181 * 182 * This is needed to make kernel threads stay in kernel mode. 183 */ 184void 185cpu_set_fork_handler(td, func, arg) 186 struct thread *td; 187 void (*func)(void *); 188 void *arg; 189{ 190 struct callframe *cf; 191 192 CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x", 193 (u_int)td, (u_int)func, (u_int)arg); 194 195 cf = (struct callframe *)td->td_pcb->pcb_sp; 196 197 cf->cf_func = (register_t)func; 198 cf->cf_arg0 = (register_t)arg; 199} 200 201/* 202 * cpu_exit is called as the last action during exit. 203 * We release the address space of the process, block interrupts, 204 * and call switch_exit. switch_exit switches to proc0's PCB and stack, 205 * then jumps into the middle of cpu_switch, as if it were switching 206 * from proc0. 207 */ 208void 209cpu_exit(td) 210 register struct thread *td; 211{ 212} 213 214void 215cpu_sched_exit(td) 216 register struct thread *td; 217{ 218} 219 220/* Temporary helper */ 221void 222cpu_throw(struct thread *old, struct thread *new) 223{ 224 225 cpu_switch(old, new); 226 panic("cpu_throw() didn't"); 227} 228 229/* 230 * Reset back to firmware. 231 */ 232void 233cpu_reset() 234{ 235 OF_exit(); 236} 237 238/* 239 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 240 */ 241static void 242sf_buf_init(void *arg) 243{ 244 struct sf_buf *sf_bufs; 245 vm_offset_t sf_base; 246 int i; 247 248 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF); 249 mtx_lock(&sf_freelist.sf_lock); 250 SLIST_INIT(&sf_freelist.sf_head); 251 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE); 252 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, 253 M_NOWAIT | M_ZERO); 254 for (i = 0; i < nsfbufs; i++) { 255 sf_bufs[i].kva = sf_base + i * PAGE_SIZE; 256 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list); 257 } 258 sf_buf_alloc_want = 0; 259 mtx_unlock(&sf_freelist.sf_lock); 260} 261 262/* 263 * Get an sf_buf from the freelist. Will block if none are available. 264 */ 265struct sf_buf * 266sf_buf_alloc(struct vm_page *m) 267{ 268 struct sf_buf *sf; 269 int error; 270 271 mtx_lock(&sf_freelist.sf_lock); 272 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) { 273 sf_buf_alloc_want++; 274 error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM|PCATCH, 275 "sfbufa", 0); 276 sf_buf_alloc_want--; 277 278 /* 279 * If we got a signal, don't risk going back to sleep. 280 */ 281 if (error) 282 break; 283 } 284 if (sf != NULL) { 285 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list); 286 sf->m = m; 287 pmap_qenter(sf->kva, &sf->m, 1); 288 } 289 mtx_unlock(&sf_freelist.sf_lock); 290 return (sf); 291} 292 293/* 294 * Detatch mapped page and release resources back to the system. 295 */ 296void 297sf_buf_free(void *addr, void *args) 298{ 299 struct sf_buf *sf; 300 struct vm_page *m; 301 302 sf = args; 303 pmap_qremove((vm_offset_t)addr, 1); 304 m = sf->m; 305 vm_page_lock_queues(); 306 vm_page_unwire(m, 0); 307 /* 308 * Check for the object going away on us. This can 309 * happen since we don't hold a reference to it. 310 * If so, we're responsible for freeing the page. 311 */ 312 if (m->wire_count == 0 && m->object == NULL) 313 vm_page_free(m); 314 vm_page_unlock_queues(); 315 sf->m = NULL; 316 mtx_lock(&sf_freelist.sf_lock); 317 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list); 318 if (sf_buf_alloc_want > 0) 319 wakeup_one(&sf_freelist); 320 mtx_unlock(&sf_freelist.sf_lock); 321} 322 323/* 324 * Software interrupt handler for queued VM system processing. 325 */ 326void 327swi_vm(void *dummy) 328{ 329#if 0 /* XXX: Don't have busdma stuff yet */ 330 if (busdma_swi_pending != 0) 331 busdma_swi(); 332#endif 333} 334 335/* 336 * Tell whether this address is in some physical memory region. 337 * Currently used by the kernel coredump code in order to avoid 338 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 339 * or other unpredictable behaviour. 340 */ 341 342 343int 344is_physical_memory(addr) 345 vm_offset_t addr; 346{ 347 /* 348 * stuff other tests for known memory-mapped devices (PCI?) 349 * here 350 */ 351 352 return 1; 353} 354 355/* 356 * KSE functions 357 */ 358void 359cpu_thread_exit(struct thread *td) 360{ 361} 362 363void 364cpu_thread_clean(struct thread *td) 365{ 366} 367 368void 369cpu_thread_setup(struct thread *td) 370{ 371} 372 373void 374cpu_thread_swapin(struct thread *td) 375{ 376} 377 378void 379cpu_thread_swapout(struct thread *td) 380{ 381} 382 383void 384cpu_set_upcall(struct thread *td, struct thread *td0) 385{ 386} 387 388void 389cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku) 390{ 391} 392