vm_machdep.c revision 127086
136865Sdfr/*- 236865Sdfr * Copyright (c) 1982, 1986 The Regents of the University of California. 336865Sdfr * Copyright (c) 1989, 1990 William Jolitz 436865Sdfr * Copyright (c) 1994 John Dyson 536865Sdfr * All rights reserved. 636865Sdfr * 736865Sdfr * This code is derived from software contributed to Berkeley by 836865Sdfr * the Systems Programming Group of the University of Utah Computer 936865Sdfr * Science Department, and William Jolitz. 1036865Sdfr * 1136865Sdfr * Redistribution and use in source and binary forms, with or without 1236865Sdfr * modification, are permitted provided that the following conditions 1336865Sdfr * are met: 1436865Sdfr * 1. Redistributions of source code must retain the above copyright 1536865Sdfr * notice, this list of conditions and the following disclaimer. 1636865Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1736865Sdfr * notice, this list of conditions and the following disclaimer in the 1836865Sdfr * documentation and/or other materials provided with the distribution. 1936865Sdfr * 3. All advertising materials mentioning features or use of this software 2036865Sdfr * must display the following acknowledgement: 2136865Sdfr * This product includes software developed by the University of 2236865Sdfr * California, Berkeley and its contributors. 2336865Sdfr * 4. Neither the name of the University nor the names of its contributors 2436865Sdfr * may be used to endorse or promote products derived from this software 2536865Sdfr * without specific prior written permission. 2636865Sdfr * 2736865Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2836865Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2936865Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3036865Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3136865Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3236865Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3336865Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3436865Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3536865Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3636865Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3736865Sdfr * SUCH DAMAGE. 3836865Sdfr * 3936865Sdfr * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 4036865Sdfr * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 4150477Speter * $FreeBSD: head/sys/powerpc/aim/vm_machdep.c 127086 2004-03-16 19:04:28Z alc $ 4236865Sdfr */ 4336865Sdfr/* 4436865Sdfr * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. 4536865Sdfr * All rights reserved. 4636865Sdfr * 4736865Sdfr * Author: Chris G. Demetriou 48126474Sgrehan * 4936865Sdfr * Permission to use, copy, modify and distribute this software and 5036865Sdfr * its documentation is hereby granted, provided that both the copyright 5136865Sdfr * notice and this permission notice appear in all copies of the 5236865Sdfr * software, derivative works or modified versions, and any portions 5336865Sdfr * thereof, and that both notices appear in supporting documentation. 54126474Sgrehan * 55126474Sgrehan * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56126474Sgrehan * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 5736865Sdfr * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58126474Sgrehan * 5936865Sdfr * Carnegie Mellon requests users of this software to return to 6036865Sdfr * 6136865Sdfr * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 6236865Sdfr * School of Computer Science 6336865Sdfr * Carnegie Mellon University 6436865Sdfr * Pittsburgh PA 15213-3890 6536865Sdfr * 6636865Sdfr * any improvements or extensions that they make and grant Carnegie the 6736865Sdfr * rights to redistribute these changes. 6836865Sdfr */ 6936865Sdfr 70118239Speter#include "opt_kstack_pages.h" 71118239Speter 7236865Sdfr#include <sys/param.h> 7336865Sdfr#include <sys/systm.h> 7436865Sdfr#include <sys/proc.h> 7536865Sdfr#include <sys/malloc.h> 7660041Sphk#include <sys/bio.h> 7736865Sdfr#include <sys/buf.h> 7885201Smp#include <sys/ktr.h> 7985201Smp#include <sys/lock.h> 8067365Sjhb#include <sys/mutex.h> 8136865Sdfr#include <sys/vnode.h> 8236865Sdfr#include <sys/vmmeter.h> 8336865Sdfr#include <sys/kernel.h> 84119563Salc#include <sys/mbuf.h> 85122780Salc#include <sys/sf_buf.h> 8636865Sdfr#include <sys/sysctl.h> 8754207Speter#include <sys/unistd.h> 8836865Sdfr 8936865Sdfr#include <machine/clock.h> 9036865Sdfr#include <machine/cpu.h> 9141499Sdfr#include <machine/fpu.h> 9297397Sbenno#include <machine/frame.h> 9336865Sdfr#include <machine/md_var.h> 9436865Sdfr 9578342Sbenno#include <dev/ofw/openfirm.h> 9678342Sbenno 9736865Sdfr#include <vm/vm.h> 9836865Sdfr#include <vm/vm_param.h> 9936865Sdfr#include <vm/vm_kern.h> 10036865Sdfr#include <vm/vm_page.h> 10136865Sdfr#include <vm/vm_map.h> 10236865Sdfr#include <vm/vm_extern.h> 10336865Sdfr 10436865Sdfr#include <sys/user.h> 10536865Sdfr 106119563Salcstatic void sf_buf_init(void *arg); 107119563SalcSYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL) 108119563Salc 10936865Sdfr/* 110119563Salc * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the 111119563Salc * sf_freelist head with the sf_lock mutex. 112119563Salc */ 113119563Salcstatic struct { 114119563Salc SLIST_HEAD(, sf_buf) sf_head; 115119563Salc struct mtx sf_lock; 116119563Salc} sf_freelist; 117119563Salc 118119563Salcstatic u_int sf_buf_alloc_want; 119119563Salc 120119563Salc/* 12136865Sdfr * Finish a fork operation, with process p2 nearly set up. 12236865Sdfr * Copy and update the pcb, set up the stack so that the child 12336865Sdfr * ready to run and return to user mode. 12436865Sdfr */ 12536865Sdfrvoid 12690361Sjuliancpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) 12736865Sdfr{ 12884977Sbenno struct proc *p1; 12984977Sbenno struct trapframe *tf; 13084977Sbenno struct callframe *cf; 13190643Sbenno struct pcb *pcb; 13284977Sbenno 13391467Sbenno KASSERT(td1 == curthread || td1 == &thread0, 13490643Sbenno ("cpu_fork: p1 not curproc and not proc0")); 13584977Sbenno CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags); 13684977Sbenno 13784977Sbenno if ((flags & RFPROC) == 0) 13884977Sbenno return; 13984977Sbenno 14084977Sbenno p1 = td1->td_proc; 14184977Sbenno 14291467Sbenno pcb = (struct pcb *)((td2->td_kstack + KSTACK_PAGES * PAGE_SIZE - 14391467Sbenno sizeof(struct pcb)) & ~0x2fU); 14490643Sbenno td2->td_pcb = pcb; 14584977Sbenno 14690643Sbenno /* Copy the pcb */ 14790643Sbenno bcopy(td1->td_pcb, pcb, sizeof(struct pcb)); 14884977Sbenno 14984977Sbenno /* 15090643Sbenno * Create a fresh stack for the new process. 15184977Sbenno * Copy the trap frame for the return to user mode as if from a 15284977Sbenno * syscall. This copies most of the user mode register values. 15384977Sbenno */ 15490643Sbenno tf = (struct trapframe *)pcb - 1; 15590643Sbenno bcopy(td1->td_frame, tf, sizeof(*tf)); 15684977Sbenno 15797397Sbenno /* Set up trap frame. */ 15897397Sbenno tf->fixreg[FIRSTARG] = 0; 15997397Sbenno tf->fixreg[FIRSTARG + 1] = 0; 16097397Sbenno tf->cr &= ~0x10000000; 16190643Sbenno 16290643Sbenno td2->td_frame = tf; 16390643Sbenno 16490643Sbenno cf = (struct callframe *)tf - 1; 16591467Sbenno cf->cf_func = (register_t)fork_return; 16691467Sbenno cf->cf_arg0 = (register_t)td2; 16791467Sbenno cf->cf_arg1 = (register_t)tf; 16884977Sbenno 16991467Sbenno pcb->pcb_sp = (register_t)cf; 17091467Sbenno pcb->pcb_lr = (register_t)fork_trampoline; 171105611Sgrehan pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR]; 17284977Sbenno 17390643Sbenno /* 17490643Sbenno * Now cpu_switch() can schedule the new process. 17590643Sbenno */ 17636865Sdfr} 17736865Sdfr 17836865Sdfr/* 17936865Sdfr * Intercept the return address from a freshly forked process that has NOT 18036865Sdfr * been scheduled yet. 18136865Sdfr * 18236865Sdfr * This is needed to make kernel threads stay in kernel mode. 18336865Sdfr */ 18436865Sdfrvoid 18583682Smpcpu_set_fork_handler(td, func, arg) 18683682Smp struct thread *td; 18792842Salfred void (*func)(void *); 18848391Speter void *arg; 18936865Sdfr{ 19084977Sbenno struct callframe *cf; 19184977Sbenno 19284977Sbenno CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x", 19384977Sbenno (u_int)td, (u_int)func, (u_int)arg); 19484977Sbenno 19591467Sbenno cf = (struct callframe *)td->td_pcb->pcb_sp; 19684977Sbenno 19791467Sbenno cf->cf_func = (register_t)func; 19891467Sbenno cf->cf_arg0 = (register_t)arg; 19936865Sdfr} 20036865Sdfr 20136865Sdfr/* 20236865Sdfr * cpu_exit is called as the last action during exit. 20336865Sdfr * We release the address space of the process, block interrupts, 20436865Sdfr * and call switch_exit. switch_exit switches to proc0's PCB and stack, 20536865Sdfr * then jumps into the middle of cpu_switch, as if it were switching 20636865Sdfr * from proc0. 20736865Sdfr */ 20836865Sdfrvoid 20983682Smpcpu_exit(td) 21083682Smp register struct thread *td; 21136865Sdfr{ 21236865Sdfr} 21336865Sdfr 21436865Sdfrvoid 21598765Sjakecpu_sched_exit(td) 21698765Sjake register struct thread *td; 21798765Sjake{ 21898765Sjake} 21998765Sjake 22083276Speter/* Temporary helper */ 22183276Spetervoid 222118893Sgrehancpu_throw(struct thread *old, struct thread *new) 22383276Speter{ 22483276Speter 225118893Sgrehan cpu_switch(old, new); 22683276Speter panic("cpu_throw() didn't"); 22736865Sdfr} 22836865Sdfr 22936865Sdfr/* 23065557Sjasone * Reset back to firmware. 23136865Sdfr */ 23236865Sdfrvoid 23336865Sdfrcpu_reset() 23436865Sdfr{ 23578342Sbenno OF_exit(); 23636865Sdfr} 23736865Sdfr 23836865Sdfr/* 239119563Salc * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 240119563Salc */ 241119563Salcstatic void 242119563Salcsf_buf_init(void *arg) 243119563Salc{ 244119563Salc struct sf_buf *sf_bufs; 245119563Salc vm_offset_t sf_base; 246119563Salc int i; 247119563Salc 248119563Salc mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF); 249119563Salc SLIST_INIT(&sf_freelist.sf_head); 250119563Salc sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE); 251119563Salc sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, 252119563Salc M_NOWAIT | M_ZERO); 253119563Salc for (i = 0; i < nsfbufs; i++) { 254119563Salc sf_bufs[i].kva = sf_base + i * PAGE_SIZE; 255119563Salc SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list); 256119563Salc } 257119563Salc sf_buf_alloc_want = 0; 258119563Salc} 259119563Salc 260119563Salc/* 261119563Salc * Get an sf_buf from the freelist. Will block if none are available. 262119563Salc */ 263119563Salcstruct sf_buf * 264119563Salcsf_buf_alloc(struct vm_page *m) 265119563Salc{ 266119563Salc struct sf_buf *sf; 267119563Salc int error; 268119563Salc 269119563Salc mtx_lock(&sf_freelist.sf_lock); 270119563Salc while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) { 271119563Salc sf_buf_alloc_want++; 272123929Ssilby mbstat.sf_allocwait++; 273119563Salc error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM|PCATCH, 274119563Salc "sfbufa", 0); 275119563Salc sf_buf_alloc_want--; 276119563Salc 277119563Salc /* 278126474Sgrehan * If we got a signal, don't risk going back to sleep. 279119563Salc */ 280119563Salc if (error) 281119563Salc break; 282119563Salc } 283119563Salc if (sf != NULL) { 284119563Salc SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list); 285119563Salc sf->m = m; 286123884Ssilby nsfbufsused++; 287123920Ssilby nsfbufspeak = imax(nsfbufspeak, nsfbufsused); 288119563Salc pmap_qenter(sf->kva, &sf->m, 1); 289119563Salc } 290119563Salc mtx_unlock(&sf_freelist.sf_lock); 291119563Salc return (sf); 292119563Salc} 293119563Salc 294119563Salc/* 295127086Salc * Release resources back to the system. 296119563Salc */ 297119563Salcvoid 298127086Salcsf_buf_free(struct sf_buf *sf) 299119563Salc{ 300119563Salc 301127086Salc pmap_qremove(sf->kva, 1); 302119563Salc mtx_lock(&sf_freelist.sf_lock); 303119563Salc SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list); 304123884Ssilby nsfbufsused--; 305119563Salc if (sf_buf_alloc_want > 0) 306119563Salc wakeup_one(&sf_freelist); 307119563Salc mtx_unlock(&sf_freelist.sf_lock); 308119563Salc} 309119563Salc 310119563Salc/* 31136865Sdfr * Software interrupt handler for queued VM system processing. 312126474Sgrehan */ 313126474Sgrehanvoid 314126474Sgrehanswi_vm(void *dummy) 315126474Sgrehan{ 31678342Sbenno#if 0 /* XXX: Don't have busdma stuff yet */ 31736865Sdfr if (busdma_swi_pending != 0) 31836865Sdfr busdma_swi(); 31978342Sbenno#endif 32036865Sdfr} 32136865Sdfr 32236865Sdfr/* 32336865Sdfr * Tell whether this address is in some physical memory region. 32436865Sdfr * Currently used by the kernel coredump code in order to avoid 32536865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs, 32636865Sdfr * or other unpredictable behaviour. 32736865Sdfr */ 32836865Sdfr 32936865Sdfr 33036865Sdfrint 33136865Sdfris_physical_memory(addr) 33236865Sdfr vm_offset_t addr; 33336865Sdfr{ 33436865Sdfr /* 33536865Sdfr * stuff other tests for known memory-mapped devices (PCI?) 33636865Sdfr * here 33736865Sdfr */ 33836865Sdfr 33936865Sdfr return 1; 34036865Sdfr} 34199659Sbenno 34299659Sbenno/* 34399659Sbenno * KSE functions 34499659Sbenno */ 34599659Sbennovoid 346126474Sgrehancpu_thread_exit(struct thread *td) 34799659Sbenno{ 34899659Sbenno} 34999659Sbenno 35099659Sbennovoid 351126474Sgrehancpu_thread_clean(struct thread *td) 352107180Smux{ 353107180Smux} 354107180Smux 355107180Smuxvoid 35699659Sbennocpu_thread_setup(struct thread *td) 35799659Sbenno{ 358126474Sgrehan struct pcb *pcb; 359126474Sgrehan 360126474Sgrehan pcb = (struct pcb *)((td->td_kstack + KSTACK_PAGES * PAGE_SIZE - 361126474Sgrehan sizeof(struct pcb)) & ~0x2fU); 362126474Sgrehan td->td_pcb = pcb; 363126474Sgrehan td->td_frame = (struct trapframe *)pcb - 1; 364119004Smarcel} 36599659Sbenno 366119004Smarcelvoid 367119004Smarcelcpu_thread_swapin(struct thread *td) 368119004Smarcel{ 36999659Sbenno} 37099659Sbenno 37199659Sbennovoid 372119004Smarcelcpu_thread_swapout(struct thread *td) 373119004Smarcel{ 374119004Smarcel} 375119004Smarcel 376119004Smarcelvoid 377115858Smarcelcpu_set_upcall(struct thread *td, struct thread *td0) 37899659Sbenno{ 379126474Sgrehan struct pcb *pcb2; 380126474Sgrehan struct trapframe *tf; 381126474Sgrehan struct callframe *cf; 382126474Sgrehan 383126474Sgrehan pcb2 = td->td_pcb; 384126474Sgrehan 385126474Sgrehan /* Copy the upcall pcb */ 386126474Sgrehan bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 387126474Sgrehan 388126474Sgrehan /* Create a stack for the new thread */ 389126474Sgrehan tf = td->td_frame; 390126474Sgrehan bcopy(td0->td_frame, tf, sizeof(struct trapframe)); 391126474Sgrehan tf->fixreg[FIRSTARG] = 0; 392126474Sgrehan tf->fixreg[FIRSTARG + 1] = 0; 393126474Sgrehan tf->cr &= ~0x10000000; 394126474Sgrehan 395126474Sgrehan /* Set registers for trampoline to user mode. */ 396126474Sgrehan cf = (struct callframe *)tf - 1; 397126474Sgrehan cf->cf_func = (register_t)fork_return; 398126474Sgrehan cf->cf_arg0 = (register_t)td; 399126474Sgrehan cf->cf_arg1 = (register_t)tf; 400126474Sgrehan 401126474Sgrehan pcb2->pcb_sp = (register_t)cf; 402126474Sgrehan pcb2->pcb_lr = (register_t)fork_trampoline; 403126474Sgrehan pcb2->pcb_usr = kernel_pmap->pm_sr[USER_SR]; 40499659Sbenno} 40599659Sbenno 40699659Sbennovoid 407111028Sjeffcpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku) 40899659Sbenno{ 409126474Sgrehan struct trapframe *tf; 410126474Sgrehan uint32_t sp; 411126474Sgrehan 412126474Sgrehan tf = td->td_frame; 413126474Sgrehan /* align stack and alloc space for frame ptr and saved LR */ 414126474Sgrehan sp = ((uint32_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size 415126474Sgrehan - 2*sizeof(u_int32_t)) & ~0x1f; 416126474Sgrehan bzero(tf, sizeof(struct trapframe)); 417126474Sgrehan 418126474Sgrehan tf->fixreg[1] = (register_t)sp; 419126474Sgrehan tf->fixreg[3] = (register_t)ku->ku_mailbox; 420126474Sgrehan tf->srr0 = (register_t)ku->ku_func; 421126474Sgrehan tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT; 422126474Sgrehan td->td_pcb->pcb_flags = 0; 423126474Sgrehan 424126474Sgrehan td->td_retval[0] = (register_t)ku->ku_func; 425126474Sgrehan td->td_retval[1] = 0; 42699659Sbenno} 427