vm_machdep.c revision 161797
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 161797 2006-09-01 06:05:40Z marcel $
4236865Sdfr */
43139825Simp/*-
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
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>
82119563Salc#include <sys/mbuf.h>
83122780Salc#include <sys/sf_buf.h>
8436865Sdfr#include <sys/sysctl.h>
8554207Speter#include <sys/unistd.h>
8636865Sdfr
8736865Sdfr#include <machine/cpu.h>
8841499Sdfr#include <machine/fpu.h>
8997397Sbenno#include <machine/frame.h>
9036865Sdfr#include <machine/md_var.h>
91138129Sdas#include <machine/pcb.h>
9236865Sdfr
9378342Sbenno#include <dev/ofw/openfirm.h>
9478342Sbenno
9536865Sdfr#include <vm/vm.h>
9636865Sdfr#include <vm/vm_param.h>
9736865Sdfr#include <vm/vm_kern.h>
9836865Sdfr#include <vm/vm_page.h>
9936865Sdfr#include <vm/vm_map.h>
10036865Sdfr#include <vm/vm_extern.h>
10136865Sdfr
10236865Sdfr/*
10336865Sdfr * Finish a fork operation, with process p2 nearly set up.
10436865Sdfr * Copy and update the pcb, set up the stack so that the child
10536865Sdfr * ready to run and return to user mode.
10636865Sdfr */
10736865Sdfrvoid
10890361Sjuliancpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
10936865Sdfr{
11084977Sbenno	struct	proc *p1;
11184977Sbenno	struct	trapframe *tf;
11284977Sbenno	struct	callframe *cf;
11390643Sbenno	struct	pcb *pcb;
11484977Sbenno
11591467Sbenno	KASSERT(td1 == curthread || td1 == &thread0,
11690643Sbenno	    ("cpu_fork: p1 not curproc and not proc0"));
11784977Sbenno	CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
11884977Sbenno
11984977Sbenno	if ((flags & RFPROC) == 0)
12084977Sbenno		return;
12184977Sbenno
12284977Sbenno	p1 = td1->td_proc;
12384977Sbenno
124127582Speter	pcb = (struct pcb *)((td2->td_kstack +
125127582Speter	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fU);
12690643Sbenno	td2->td_pcb = pcb;
12784977Sbenno
12890643Sbenno	/* Copy the pcb */
12990643Sbenno	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
13084977Sbenno
13184977Sbenno	/*
13290643Sbenno	 * Create a fresh stack for the new process.
13384977Sbenno	 * Copy the trap frame for the return to user mode as if from a
13484977Sbenno	 * syscall.  This copies most of the user mode register values.
13584977Sbenno	 */
13690643Sbenno	tf = (struct trapframe *)pcb - 1;
13790643Sbenno	bcopy(td1->td_frame, tf, sizeof(*tf));
13884977Sbenno
13997397Sbenno	/* Set up trap frame. */
14097397Sbenno	tf->fixreg[FIRSTARG] = 0;
14197397Sbenno	tf->fixreg[FIRSTARG + 1] = 0;
14297397Sbenno	tf->cr &= ~0x10000000;
14390643Sbenno
14490643Sbenno	td2->td_frame = tf;
14590643Sbenno
14690643Sbenno	cf = (struct callframe *)tf - 1;
147132520Sgrehan	memset(cf, 0, sizeof(struct callframe));
14891467Sbenno	cf->cf_func = (register_t)fork_return;
14991467Sbenno	cf->cf_arg0 = (register_t)td2;
15091467Sbenno	cf->cf_arg1 = (register_t)tf;
15184977Sbenno
15291467Sbenno	pcb->pcb_sp = (register_t)cf;
15391467Sbenno	pcb->pcb_lr = (register_t)fork_trampoline;
154105611Sgrehan	pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR];
15584977Sbenno
156144637Sjhb	/* Setup to release sched_lock in fork_exit(). */
157144637Sjhb	td2->td_md.md_spinlock_count = 1;
158144637Sjhb	td2->td_md.md_saved_msr = PSL_KERNSET;
159144637Sjhb
16090643Sbenno	/*
16190643Sbenno 	 * Now cpu_switch() can schedule the new process.
16290643Sbenno	 */
16336865Sdfr}
16436865Sdfr
16536865Sdfr/*
16636865Sdfr * Intercept the return address from a freshly forked process that has NOT
16736865Sdfr * been scheduled yet.
16836865Sdfr *
16936865Sdfr * This is needed to make kernel threads stay in kernel mode.
17036865Sdfr */
17136865Sdfrvoid
17283682Smpcpu_set_fork_handler(td, func, arg)
17383682Smp	struct thread *td;
17492842Salfred	void (*func)(void *);
17548391Speter	void *arg;
17636865Sdfr{
17784977Sbenno	struct	callframe *cf;
17884977Sbenno
17984977Sbenno	CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x",
18084977Sbenno	    (u_int)td, (u_int)func, (u_int)arg);
18184977Sbenno
18291467Sbenno	cf = (struct callframe *)td->td_pcb->pcb_sp;
18384977Sbenno
18491467Sbenno	cf->cf_func = (register_t)func;
18591467Sbenno	cf->cf_arg0 = (register_t)arg;
18636865Sdfr}
18736865Sdfr
18836865Sdfrvoid
18983682Smpcpu_exit(td)
19083682Smp	register struct thread *td;
19136865Sdfr{
19236865Sdfr}
19336865Sdfr
19483276Speter/* Temporary helper */
19583276Spetervoid
196118893Sgrehancpu_throw(struct thread *old, struct thread *new)
19783276Speter{
19883276Speter
199118893Sgrehan	cpu_switch(old, new);
20083276Speter	panic("cpu_throw() didn't");
20136865Sdfr}
20236865Sdfr
20336865Sdfr/*
20465557Sjasone * Reset back to firmware.
20536865Sdfr */
20636865Sdfrvoid
20736865Sdfrcpu_reset()
20836865Sdfr{
20978342Sbenno	OF_exit();
21036865Sdfr}
21136865Sdfr
21236865Sdfr/*
213128395Salc * Allocate an sf_buf for the given vm_page.  On this machine, however, there
214128395Salc * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
215128395Salc * returned.
216119563Salc */
217119563Salcstruct sf_buf *
218127788Salcsf_buf_alloc(struct vm_page *m, int pri)
219119563Salc{
220119563Salc
221128395Salc	return ((struct sf_buf *)m);
222119563Salc}
223119563Salc
224119563Salc/*
225128395Salc * Free the sf_buf.  In fact, do nothing because there are no resources
226128395Salc * associated with the sf_buf.
227119563Salc */
228119563Salcvoid
229127086Salcsf_buf_free(struct sf_buf *sf)
230119563Salc{
231119563Salc}
232119563Salc
233119563Salc/*
23436865Sdfr * Software interrupt handler for queued VM system processing.
235126474Sgrehan */
236126474Sgrehanvoid
237126474Sgrehanswi_vm(void *dummy)
238126474Sgrehan{
23978342Sbenno#if 0 /* XXX: Don't have busdma stuff yet */
24036865Sdfr	if (busdma_swi_pending != 0)
24136865Sdfr		busdma_swi();
24278342Sbenno#endif
24336865Sdfr}
24436865Sdfr
24536865Sdfr/*
24636865Sdfr * Tell whether this address is in some physical memory region.
24736865Sdfr * Currently used by the kernel coredump code in order to avoid
24836865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs,
24936865Sdfr * or other unpredictable behaviour.
25036865Sdfr */
25136865Sdfr
25236865Sdfr
25336865Sdfrint
25436865Sdfris_physical_memory(addr)
25536865Sdfr	vm_offset_t addr;
25636865Sdfr{
25736865Sdfr	/*
25836865Sdfr	 * stuff other tests for known memory-mapped devices (PCI?)
25936865Sdfr	 * here
26036865Sdfr	 */
26136865Sdfr
26236865Sdfr	return 1;
26336865Sdfr}
26499659Sbenno
26599659Sbenno/*
26699659Sbenno * KSE functions
26799659Sbenno */
26899659Sbennovoid
269126474Sgrehancpu_thread_exit(struct thread *td)
27099659Sbenno{
27199659Sbenno}
27299659Sbenno
27399659Sbennovoid
274126474Sgrehancpu_thread_clean(struct thread *td)
275107180Smux{
276107180Smux}
277107180Smux
278107180Smuxvoid
27999659Sbennocpu_thread_setup(struct thread *td)
28099659Sbenno{
281126474Sgrehan	struct pcb *pcb;
282126474Sgrehan
283127618Sbenno	pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
284126474Sgrehan	    sizeof(struct pcb)) & ~0x2fU);
285126474Sgrehan	td->td_pcb = pcb;
286126474Sgrehan	td->td_frame = (struct trapframe *)pcb - 1;
287119004Smarcel}
28899659Sbenno
289119004Smarcelvoid
290119004Smarcelcpu_thread_swapin(struct thread *td)
291119004Smarcel{
29299659Sbenno}
29399659Sbenno
29499659Sbennovoid
295119004Smarcelcpu_thread_swapout(struct thread *td)
296119004Smarcel{
297119004Smarcel}
298119004Smarcel
299119004Smarcelvoid
300115858Smarcelcpu_set_upcall(struct thread *td, struct thread *td0)
30199659Sbenno{
302126474Sgrehan	struct pcb *pcb2;
303126474Sgrehan	struct trapframe *tf;
304126474Sgrehan	struct callframe *cf;
305126474Sgrehan
306126474Sgrehan	pcb2 = td->td_pcb;
307126474Sgrehan
308126474Sgrehan	/* Copy the upcall pcb */
309126474Sgrehan	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
310126474Sgrehan
311126474Sgrehan	/* Create a stack for the new thread */
312126474Sgrehan	tf = td->td_frame;
313126474Sgrehan	bcopy(td0->td_frame, tf, sizeof(struct trapframe));
314126474Sgrehan	tf->fixreg[FIRSTARG] = 0;
315126474Sgrehan	tf->fixreg[FIRSTARG + 1] = 0;
316126474Sgrehan	tf->cr &= ~0x10000000;
317126474Sgrehan
318126474Sgrehan	/* Set registers for trampoline to user mode. */
319126474Sgrehan	cf = (struct callframe *)tf - 1;
320132520Sgrehan	memset(cf, 0, sizeof(struct callframe));
321126474Sgrehan	cf->cf_func = (register_t)fork_return;
322126474Sgrehan	cf->cf_arg0 = (register_t)td;
323126474Sgrehan	cf->cf_arg1 = (register_t)tf;
324126474Sgrehan
325126474Sgrehan	pcb2->pcb_sp = (register_t)cf;
326126474Sgrehan	pcb2->pcb_lr = (register_t)fork_trampoline;
327126474Sgrehan	pcb2->pcb_usr = kernel_pmap->pm_sr[USER_SR];
328144637Sjhb
329144637Sjhb	/* Setup to release sched_lock in fork_exit(). */
330144637Sjhb	td->td_md.md_spinlock_count = 1;
331144637Sjhb	td->td_md.md_saved_msr = PSL_KERNSET;
33299659Sbenno}
33399659Sbenno
33499659Sbennovoid
335145433Sdavidxucpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
336145433Sdavidxu	stack_t *stack)
33799659Sbenno{
338126474Sgrehan        struct trapframe *tf;
339126474Sgrehan        uint32_t sp;
340126474Sgrehan
341126474Sgrehan	tf = td->td_frame;
342126474Sgrehan	/* align stack and alloc space for frame ptr and saved LR */
343145433Sdavidxu        sp = ((uint32_t)stack->ss_sp + stack->ss_size
344126474Sgrehan		- 2*sizeof(u_int32_t)) & ~0x1f;
345126474Sgrehan	bzero(tf, sizeof(struct trapframe));
346126474Sgrehan
347126474Sgrehan	tf->fixreg[1] = (register_t)sp;
348145433Sdavidxu        tf->fixreg[3] = (register_t)arg;
349145433Sdavidxu        tf->srr0 = (register_t)entry;
350126474Sgrehan        tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
351126474Sgrehan        td->td_pcb->pcb_flags = 0;
352126474Sgrehan
353145433Sdavidxu        td->td_retval[0] = (register_t)entry;
354126474Sgrehan        td->td_retval[1] = 0;
35599659Sbenno}
356145433Sdavidxu
357147889Sdavidxuint
358145433Sdavidxucpu_set_user_tls(struct thread *td, void *tls_base)
359145433Sdavidxu{
360145433Sdavidxu
361161797Smarcel	td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
362147889Sdavidxu	return (0);
363145433Sdavidxu}
364