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$
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>
8336865Sdfr#include <sys/sysctl.h>
84199135Skib#include <sys/sysent.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
102190681Snwhitehorn/*
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	trapframe *tf;
11184977Sbenno	struct	callframe *cf;
11290643Sbenno	struct	pcb *pcb;
11384977Sbenno
11491467Sbenno	KASSERT(td1 == curthread || td1 == &thread0,
11590643Sbenno	    ("cpu_fork: p1 not curproc and not proc0"));
116209975Snwhitehorn	CTR3(KTR_PROC, "cpu_fork: called td1=%p p2=%p flags=%x",
117209975Snwhitehorn	    td1, p2, flags);
11884977Sbenno
11984977Sbenno	if ((flags & RFPROC) == 0)
12084977Sbenno		return;
12184977Sbenno
122127582Speter	pcb = (struct pcb *)((td2->td_kstack +
123209975Snwhitehorn	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fUL);
12490643Sbenno	td2->td_pcb = pcb;
12584977Sbenno
12690643Sbenno	/* Copy the pcb */
12790643Sbenno	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
12884977Sbenno
12984977Sbenno	/*
13090643Sbenno	 * Create a fresh stack for the new process.
13184977Sbenno	 * Copy the trap frame for the return to user mode as if from a
13284977Sbenno	 * syscall.  This copies most of the user mode register values.
13384977Sbenno	 */
13490643Sbenno	tf = (struct trapframe *)pcb - 1;
13590643Sbenno	bcopy(td1->td_frame, tf, sizeof(*tf));
13684977Sbenno
13797397Sbenno	/* Set up trap frame. */
13897397Sbenno	tf->fixreg[FIRSTARG] = 0;
13997397Sbenno	tf->fixreg[FIRSTARG + 1] = 0;
14097397Sbenno	tf->cr &= ~0x10000000;
14190643Sbenno
14290643Sbenno	td2->td_frame = tf;
14390643Sbenno
14490643Sbenno	cf = (struct callframe *)tf - 1;
145132520Sgrehan	memset(cf, 0, sizeof(struct callframe));
146291442Snwhitehorn	#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
147277334Snwhitehorn	cf->cf_toc = ((register_t *)fork_return)[1];
148209975Snwhitehorn	#endif
14991467Sbenno	cf->cf_func = (register_t)fork_return;
15091467Sbenno	cf->cf_arg0 = (register_t)td2;
15191467Sbenno	cf->cf_arg1 = (register_t)tf;
15284977Sbenno
15391467Sbenno	pcb->pcb_sp = (register_t)cf;
154255273Snwhitehorn	KASSERT(pcb->pcb_sp % 16 == 0, ("stack misaligned"));
155291442Snwhitehorn	#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
156209975Snwhitehorn	pcb->pcb_lr = ((register_t *)fork_trampoline)[0];
157209975Snwhitehorn	pcb->pcb_toc = ((register_t *)fork_trampoline)[1];
158209975Snwhitehorn	#else
15991467Sbenno	pcb->pcb_lr = (register_t)fork_trampoline;
160291442Snwhitehorn	pcb->pcb_context[0] = pcb->pcb_lr;
161209975Snwhitehorn	#endif
162256792Snwhitehorn	#ifdef AIM
163209975Snwhitehorn	pcb->pcb_cpu.aim.usr_vsid = 0;
164256792Snwhitehorn	#endif
16584977Sbenno
166170305Sjeff	/* Setup to release spin count in fork_exit(). */
167144637Sjhb	td2->td_md.md_spinlock_count = 1;
168144637Sjhb	td2->td_md.md_saved_msr = PSL_KERNSET;
169144637Sjhb
17090643Sbenno	/*
17190643Sbenno 	 * Now cpu_switch() can schedule the new process.
17290643Sbenno	 */
17336865Sdfr}
17436865Sdfr
17536865Sdfr/*
17636865Sdfr * Intercept the return address from a freshly forked process that has NOT
17736865Sdfr * been scheduled yet.
17836865Sdfr *
17936865Sdfr * This is needed to make kernel threads stay in kernel mode.
18036865Sdfr */
18136865Sdfrvoid
182301961Skibcpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
18336865Sdfr{
184256792Snwhitehorn	struct callframe *cf;
18584977Sbenno
186209975Snwhitehorn	CTR4(KTR_PROC, "%s called with td=%p func=%p arg=%p",
187209975Snwhitehorn	    __func__, td, func, arg);
18884977Sbenno
18991467Sbenno	cf = (struct callframe *)td->td_pcb->pcb_sp;
19084977Sbenno
19191467Sbenno	cf->cf_func = (register_t)func;
19291467Sbenno	cf->cf_arg0 = (register_t)arg;
19336865Sdfr}
19436865Sdfr
19536865Sdfrvoid
196256792Snwhitehorncpu_exit(struct thread *td)
19736865Sdfr{
198256792Snwhitehorn
19936865Sdfr}
20036865Sdfr
20136865Sdfr/*
20236865Sdfr * Software interrupt handler for queued VM system processing.
203126474Sgrehan */
204126474Sgrehanvoid
205126474Sgrehanswi_vm(void *dummy)
206126474Sgrehan{
207209950Snwhitehorn
20836865Sdfr	if (busdma_swi_pending != 0)
20936865Sdfr		busdma_swi();
21036865Sdfr}
21136865Sdfr
21236865Sdfr/*
21336865Sdfr * Tell whether this address is in some physical memory region.
21436865Sdfr * Currently used by the kernel coredump code in order to avoid
21536865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs,
21636865Sdfr * or other unpredictable behaviour.
21736865Sdfr */
21836865Sdfrint
219256792Snwhitehornis_physical_memory(vm_offset_t addr)
22036865Sdfr{
221256792Snwhitehorn
22236865Sdfr	/*
22336865Sdfr	 * stuff other tests for known memory-mapped devices (PCI?)
22436865Sdfr	 * here
22536865Sdfr	 */
226256792Snwhitehorn	return (1);
22736865Sdfr}
22899659Sbenno
22999659Sbenno/*
230209975Snwhitehorn * CPU threading functions related to the VM layer. These could be used
231209975Snwhitehorn * to map the SLB bits required for the kernel stack instead of forcing a
232209975Snwhitehorn * fixed-size KVA.
23399659Sbenno */
23499659Sbenno
23599659Sbennovoid
236119004Smarcelcpu_thread_swapin(struct thread *td)
237119004Smarcel{
238256792Snwhitehorn
23999659Sbenno}
24099659Sbenno
24199659Sbennovoid
242119004Smarcelcpu_thread_swapout(struct thread *td)
243119004Smarcel{
244256792Snwhitehorn
245119004Smarcel}
246119004Smarcel
247