1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * Copyright (c) 1994 John Dyson
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, and William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 * $FreeBSD$
42 */
43/*-
44 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
45 * All rights reserved.
46 *
47 * Author: Chris G. Demetriou
48 *
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
54 *
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 *
59 * Carnegie Mellon requests users of this software to return to
60 *
61 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62 *  School of Computer Science
63 *  Carnegie Mellon University
64 *  Pittsburgh PA 15213-3890
65 *
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
68 */
69
70#include <sys/param.h>
71#include <sys/systm.h>
72#include <sys/proc.h>
73#include <sys/malloc.h>
74#include <sys/bio.h>
75#include <sys/buf.h>
76#include <sys/ktr.h>
77#include <sys/lock.h>
78#include <sys/mutex.h>
79#include <sys/vnode.h>
80#include <sys/vmmeter.h>
81#include <sys/kernel.h>
82#include <sys/mbuf.h>
83#include <sys/sysctl.h>
84#include <sys/sysent.h>
85#include <sys/unistd.h>
86
87#include <machine/cpu.h>
88#include <machine/fpu.h>
89#include <machine/frame.h>
90#include <machine/md_var.h>
91#include <machine/pcb.h>
92
93#include <dev/ofw/openfirm.h>
94
95#include <vm/vm.h>
96#include <vm/vm_param.h>
97#include <vm/vm_kern.h>
98#include <vm/vm_page.h>
99#include <vm/vm_map.h>
100#include <vm/vm_extern.h>
101
102/*
103 * Finish a fork operation, with process p2 nearly set up.
104 * Copy and update the pcb, set up the stack so that the child
105 * ready to run and return to user mode.
106 */
107void
108cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
109{
110	struct	trapframe *tf;
111	struct	callframe *cf;
112	struct	pcb *pcb;
113
114	KASSERT(td1 == curthread || td1 == &thread0,
115	    ("cpu_fork: p1 not curproc and not proc0"));
116	CTR3(KTR_PROC, "cpu_fork: called td1=%p p2=%p flags=%x",
117	    td1, p2, flags);
118
119	if ((flags & RFPROC) == 0)
120		return;
121
122	pcb = (struct pcb *)((td2->td_kstack +
123	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fUL);
124	td2->td_pcb = pcb;
125
126	/* Copy the pcb */
127	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
128
129	/*
130	 * Create a fresh stack for the new process.
131	 * Copy the trap frame for the return to user mode as if from a
132	 * syscall.  This copies most of the user mode register values.
133	 */
134	tf = (struct trapframe *)pcb - 1;
135	bcopy(td1->td_frame, tf, sizeof(*tf));
136
137	/* Set up trap frame. */
138	tf->fixreg[FIRSTARG] = 0;
139	tf->fixreg[FIRSTARG + 1] = 0;
140	tf->cr &= ~0x10000000;
141
142	td2->td_frame = tf;
143
144	cf = (struct callframe *)tf - 1;
145	memset(cf, 0, sizeof(struct callframe));
146	#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
147	cf->cf_toc = ((register_t *)fork_return)[1];
148	#endif
149	cf->cf_func = (register_t)fork_return;
150	cf->cf_arg0 = (register_t)td2;
151	cf->cf_arg1 = (register_t)tf;
152
153	pcb->pcb_sp = (register_t)cf;
154	KASSERT(pcb->pcb_sp % 16 == 0, ("stack misaligned"));
155	#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
156	pcb->pcb_lr = ((register_t *)fork_trampoline)[0];
157	pcb->pcb_toc = ((register_t *)fork_trampoline)[1];
158	#else
159	pcb->pcb_lr = (register_t)fork_trampoline;
160	pcb->pcb_context[0] = pcb->pcb_lr;
161	#endif
162	#ifdef AIM
163	pcb->pcb_cpu.aim.usr_vsid = 0;
164	#endif
165
166	/* Setup to release spin count in fork_exit(). */
167	td2->td_md.md_spinlock_count = 1;
168	td2->td_md.md_saved_msr = PSL_KERNSET;
169
170	/*
171 	 * Now cpu_switch() can schedule the new process.
172	 */
173}
174
175/*
176 * Intercept the return address from a freshly forked process that has NOT
177 * been scheduled yet.
178 *
179 * This is needed to make kernel threads stay in kernel mode.
180 */
181void
182cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
183{
184	struct callframe *cf;
185
186	CTR4(KTR_PROC, "%s called with td=%p func=%p arg=%p",
187	    __func__, td, func, arg);
188
189	cf = (struct callframe *)td->td_pcb->pcb_sp;
190
191	cf->cf_func = (register_t)func;
192	cf->cf_arg0 = (register_t)arg;
193}
194
195void
196cpu_exit(struct thread *td)
197{
198
199}
200
201/*
202 * Software interrupt handler for queued VM system processing.
203 */
204void
205swi_vm(void *dummy)
206{
207
208	if (busdma_swi_pending != 0)
209		busdma_swi();
210}
211
212/*
213 * Tell whether this address is in some physical memory region.
214 * Currently used by the kernel coredump code in order to avoid
215 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
216 * or other unpredictable behaviour.
217 */
218int
219is_physical_memory(vm_offset_t addr)
220{
221
222	/*
223	 * stuff other tests for known memory-mapped devices (PCI?)
224	 * here
225	 */
226	return (1);
227}
228
229/*
230 * CPU threading functions related to the VM layer. These could be used
231 * to map the SLB bits required for the kernel stack instead of forcing a
232 * fixed-size KVA.
233 */
234
235void
236cpu_thread_swapin(struct thread *td)
237{
238
239}
240
241void
242cpu_thread_swapout(struct thread *td)
243{
244
245}
246
247