vm_machdep.c revision 92842
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 92842 2002-03-20 23:17:50Z alfred $
4236865Sdfr */
4336865Sdfr/*
4436865Sdfr * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
4536865Sdfr * All rights reserved.
4636865Sdfr *
4736865Sdfr * Author: Chris G. Demetriou
4836865Sdfr *
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.
5436865Sdfr *
5536865Sdfr * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
5636865Sdfr * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
5736865Sdfr * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5836865Sdfr *
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
7036865Sdfr#include <sys/param.h>
7136865Sdfr#include <sys/systm.h>
7236865Sdfr#include <sys/proc.h>
7336865Sdfr#include <sys/malloc.h>
7460041Sphk#include <sys/bio.h>
7536865Sdfr#include <sys/buf.h>
7685201Smp#include <sys/ktr.h>
7785201Smp#include <sys/lock.h>
7867365Sjhb#include <sys/mutex.h>
7936865Sdfr#include <sys/vnode.h>
8036865Sdfr#include <sys/vmmeter.h>
8136865Sdfr#include <sys/kernel.h>
8236865Sdfr#include <sys/sysctl.h>
8354207Speter#include <sys/unistd.h>
8436865Sdfr
8536865Sdfr#include <machine/clock.h>
8636865Sdfr#include <machine/cpu.h>
8741499Sdfr#include <machine/fpu.h>
8836865Sdfr#include <machine/md_var.h>
8936865Sdfr
9078342Sbenno#include <dev/ofw/openfirm.h>
9178342Sbenno
9236865Sdfr#include <vm/vm.h>
9336865Sdfr#include <vm/vm_param.h>
9436865Sdfr#include <vm/vm_kern.h>
9536865Sdfr#include <vm/vm_page.h>
9636865Sdfr#include <vm/vm_map.h>
9736865Sdfr#include <vm/vm_extern.h>
9836865Sdfr
9936865Sdfr#include <sys/user.h>
10036865Sdfr
10136865Sdfr/*
10236865Sdfr * quick version of vm_fault
10336865Sdfr */
10451474Sdillonint
10536865Sdfrvm_fault_quick(v, prot)
10636865Sdfr	caddr_t v;
10736865Sdfr	int prot;
10836865Sdfr{
10951474Sdillon	int r;
11036865Sdfr	if (prot & VM_PROT_WRITE)
11151474Sdillon		r = subyte(v, fubyte(v));
11236865Sdfr	else
11351474Sdillon		r = fubyte(v);
11451474Sdillon	return(r);
11536865Sdfr}
11636865Sdfr
11736865Sdfr/*
11836865Sdfr * Finish a fork operation, with process p2 nearly set up.
11936865Sdfr * Copy and update the pcb, set up the stack so that the child
12036865Sdfr * ready to run and return to user mode.
12136865Sdfr */
12236865Sdfrvoid
12390361Sjuliancpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
12436865Sdfr{
12584977Sbenno	struct	proc *p1;
12684977Sbenno	struct	trapframe *tf;
12784977Sbenno	struct	callframe *cf;
12890643Sbenno	struct	pcb *pcb;
12984977Sbenno
13091467Sbenno	KASSERT(td1 == curthread || td1 == &thread0,
13190643Sbenno	    ("cpu_fork: p1 not curproc and not proc0"));
13284977Sbenno	CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
13384977Sbenno
13484977Sbenno	if ((flags & RFPROC) == 0)
13584977Sbenno		return;
13684977Sbenno
13784977Sbenno	p1 = td1->td_proc;
13884977Sbenno
13991467Sbenno	pcb = (struct pcb *)((td2->td_kstack + KSTACK_PAGES * PAGE_SIZE -
14091467Sbenno	    sizeof(struct pcb)) & ~0x2fU);
14190643Sbenno	td2->td_pcb = pcb;
14284977Sbenno
14390643Sbenno	/* Copy the pcb */
14490643Sbenno	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
14584977Sbenno
14684977Sbenno	/*
14790643Sbenno	 * Create a fresh stack for the new process.
14884977Sbenno	 * Copy the trap frame for the return to user mode as if from a
14984977Sbenno	 * syscall.  This copies most of the user mode register values.
15084977Sbenno	 */
15190643Sbenno	tf = (struct trapframe *)pcb - 1;
15290643Sbenno	bcopy(td1->td_frame, tf, sizeof(*tf));
15384977Sbenno
15490643Sbenno	/* XXX: Set up trap frame? */
15590643Sbenno
15690643Sbenno	td2->td_frame = tf;
15790643Sbenno
15890643Sbenno	cf = (struct callframe *)tf - 1;
15991467Sbenno	cf->cf_func = (register_t)fork_return;
16091467Sbenno	cf->cf_arg0 = (register_t)td2;
16191467Sbenno	cf->cf_arg1 = (register_t)tf;
16284977Sbenno
16391467Sbenno	pcb->pcb_sp = (register_t)cf;
16491467Sbenno	pcb->pcb_lr = (register_t)fork_trampoline;
16584977Sbenno
16690643Sbenno	/*
16790643Sbenno 	 * Now cpu_switch() can schedule the new process.
16890643Sbenno	 */
16936865Sdfr}
17036865Sdfr
17136865Sdfr/*
17236865Sdfr * Intercept the return address from a freshly forked process that has NOT
17336865Sdfr * been scheduled yet.
17436865Sdfr *
17536865Sdfr * This is needed to make kernel threads stay in kernel mode.
17636865Sdfr */
17736865Sdfrvoid
17883682Smpcpu_set_fork_handler(td, func, arg)
17983682Smp	struct thread *td;
18092842Salfred	void (*func)(void *);
18148391Speter	void *arg;
18236865Sdfr{
18384977Sbenno	struct	callframe *cf;
18484977Sbenno
18584977Sbenno	CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x",
18684977Sbenno	    (u_int)td, (u_int)func, (u_int)arg);
18784977Sbenno
18891467Sbenno	cf = (struct callframe *)td->td_pcb->pcb_sp;
18984977Sbenno
19091467Sbenno	cf->cf_func = (register_t)func;
19191467Sbenno	cf->cf_arg0 = (register_t)arg;
19236865Sdfr}
19336865Sdfr
19436865Sdfr/*
19536865Sdfr * cpu_exit is called as the last action during exit.
19636865Sdfr * We release the address space of the process, block interrupts,
19736865Sdfr * and call switch_exit.  switch_exit switches to proc0's PCB and stack,
19836865Sdfr * then jumps into the middle of cpu_switch, as if it were switching
19936865Sdfr * from proc0.
20036865Sdfr */
20136865Sdfrvoid
20283682Smpcpu_exit(td)
20383682Smp	register struct thread *td;
20436865Sdfr{
20536865Sdfr}
20636865Sdfr
20736865Sdfrvoid
20883682Smpcpu_wait(td)
20983682Smp	struct proc *td;
21036865Sdfr{
21183276Speter}
21236865Sdfr
21383276Speter/* Temporary helper */
21483276Spetervoid
21583276Spetercpu_throw(void)
21683276Speter{
21783276Speter
21883276Speter	cpu_switch();
21983276Speter	panic("cpu_throw() didn't");
22036865Sdfr}
22136865Sdfr
22236865Sdfr/*
22336865Sdfr * Dump the machine specific header information at the start of a core dump.
22436865Sdfr */
22536865Sdfrint
22683682Smpcpu_coredump(td, vp, cred)
22783682Smp	struct thread *td;
22836865Sdfr	struct vnode *vp;
22936865Sdfr	struct ucred *cred;
23036865Sdfr{
23136865Sdfr
23290643Sbenno	return (vn_rdwr(UIO_WRITE, vp, (caddr_t)td->td_proc->p_uarea,
23390643Sbenno	    ctob(UAREA_PAGES), (off_t)0, UIO_SYSSPACE, IO_UNIT, cred,
23490643Sbenno	    (int *)NULL, td));
23536865Sdfr}
23636865Sdfr
23736865Sdfr/*
23836865Sdfr * Map an IO request into kernel virtual address space.
23936865Sdfr *
24036865Sdfr * All requests are (re)mapped into kernel VA space.
24136865Sdfr * Notice that we use b_bufsize for the size of the buffer
24236865Sdfr * to be mapped.  b_bcount might be modified by the driver.
24336865Sdfr */
24436865Sdfrvoid
24536865Sdfrvmapbuf(bp)
24636865Sdfr	register struct buf *bp;
24736865Sdfr{
24836865Sdfr	register caddr_t addr, v, kva;
24936865Sdfr	vm_offset_t pa;
25036865Sdfr
25179224Sdillon	GIANT_REQUIRED;
25279224Sdillon
25336865Sdfr	if ((bp->b_flags & B_PHYS) == 0)
25436865Sdfr		panic("vmapbuf");
25536865Sdfr
25636865Sdfr	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data);
25736865Sdfr	    addr < bp->b_data + bp->b_bufsize;
25836865Sdfr	    addr += PAGE_SIZE, v += PAGE_SIZE) {
25936865Sdfr		/*
26036865Sdfr		 * Do the vm_fault if needed; do the copy-on-write thing
26136865Sdfr		 * when reading stuff off device into memory.
26236865Sdfr		 */
26384381Smjacob		vm_fault_quick((addr >= bp->b_data) ? addr : bp->b_data,
26458345Sphk			(bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
26536865Sdfr		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
26636865Sdfr		if (pa == 0)
26736865Sdfr			panic("vmapbuf: page not present");
26836865Sdfr		vm_page_hold(PHYS_TO_VM_PAGE(pa));
26936865Sdfr		pmap_kenter((vm_offset_t) v, pa);
27036865Sdfr	}
27136865Sdfr
27236865Sdfr	kva = bp->b_saveaddr;
27336865Sdfr	bp->b_saveaddr = bp->b_data;
27436865Sdfr	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
27536865Sdfr}
27636865Sdfr
27736865Sdfr/*
27836865Sdfr * Free the io map PTEs associated with this IO operation.
27936865Sdfr * We also invalidate the TLB entries and restore the original b_addr.
28036865Sdfr */
28136865Sdfrvoid
28236865Sdfrvunmapbuf(bp)
28336865Sdfr	register struct buf *bp;
28436865Sdfr{
28536865Sdfr	register caddr_t addr;
28636865Sdfr	vm_offset_t pa;
28736865Sdfr
28879224Sdillon	GIANT_REQUIRED;
28979224Sdillon
29036865Sdfr	if ((bp->b_flags & B_PHYS) == 0)
29136865Sdfr		panic("vunmapbuf");
29236865Sdfr
29336865Sdfr	for (addr = (caddr_t)trunc_page(bp->b_data);
29436865Sdfr	    addr < bp->b_data + bp->b_bufsize;
29536865Sdfr	    addr += PAGE_SIZE) {
29636865Sdfr		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
29736865Sdfr		pmap_kremove((vm_offset_t) addr);
29836865Sdfr		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
29936865Sdfr	}
30036865Sdfr
30136865Sdfr	bp->b_data = bp->b_saveaddr;
30236865Sdfr}
30336865Sdfr
30436865Sdfr/*
30565557Sjasone * Reset back to firmware.
30636865Sdfr */
30736865Sdfrvoid
30836865Sdfrcpu_reset()
30936865Sdfr{
31078342Sbenno	OF_exit();
31136865Sdfr}
31236865Sdfr
31336865Sdfrint
31443209Sjuliangrow_stack(p, sp)
31543209Sjulian	struct proc *p;
31643209Sjulian	size_t sp;
31743209Sjulian{
31843209Sjulian	int rv;
31936865Sdfr
32043209Sjulian	rv = vm_map_growstack (p, sp);
32143209Sjulian	if (rv != KERN_SUCCESS)
32243209Sjulian		return (0);
32343209Sjulian
32443209Sjulian	return (1);
32543209Sjulian}
32643209Sjulian
32736865Sdfr/*
32836865Sdfr * Software interrupt handler for queued VM system processing.
32936865Sdfr */
33036865Sdfrvoid
33167551Sjhbswi_vm(void *dummy)
33236865Sdfr{
33378342Sbenno#if 0 /* XXX: Don't have busdma stuff yet */
33436865Sdfr	if (busdma_swi_pending != 0)
33536865Sdfr		busdma_swi();
33678342Sbenno#endif
33736865Sdfr}
33836865Sdfr
33936865Sdfr/*
34036865Sdfr * Tell whether this address is in some physical memory region.
34136865Sdfr * Currently used by the kernel coredump code in order to avoid
34236865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs,
34336865Sdfr * or other unpredictable behaviour.
34436865Sdfr */
34536865Sdfr
34636865Sdfr
34736865Sdfrint
34836865Sdfris_physical_memory(addr)
34936865Sdfr	vm_offset_t addr;
35036865Sdfr{
35136865Sdfr	/*
35236865Sdfr	 * stuff other tests for known memory-mapped devices (PCI?)
35336865Sdfr	 * here
35436865Sdfr	 */
35536865Sdfr
35636865Sdfr	return 1;
35736865Sdfr}
358