xref: /freebsd/sys/powerpc/powerpc/vm_machdep.c (revision 7bd6fde3)
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/sf_buf.h>
84 #include <sys/sysctl.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 #include <machine/powerpc.h>
93 
94 #include <dev/ofw/openfirm.h>
95 
96 #include <vm/vm.h>
97 #include <vm/vm_param.h>
98 #include <vm/vm_kern.h>
99 #include <vm/vm_page.h>
100 #include <vm/vm_map.h>
101 #include <vm/vm_extern.h>
102 
103 /*
104  * Finish a fork operation, with process p2 nearly set up.
105  * Copy and update the pcb, set up the stack so that the child
106  * ready to run and return to user mode.
107  */
108 void
109 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
110 {
111 	struct	proc *p1;
112 	struct	trapframe *tf;
113 	struct	callframe *cf;
114 	struct	pcb *pcb;
115 
116 	KASSERT(td1 == curthread || td1 == &thread0,
117 	    ("cpu_fork: p1 not curproc and not proc0"));
118 	CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags);
119 
120 	if ((flags & RFPROC) == 0)
121 		return;
122 
123 	p1 = td1->td_proc;
124 
125 	pcb = (struct pcb *)((td2->td_kstack +
126 	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb)) & ~0x2fU);
127 	td2->td_pcb = pcb;
128 
129 	/* Copy the pcb */
130 	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
131 
132 	/*
133 	 * Create a fresh stack for the new process.
134 	 * Copy the trap frame for the return to user mode as if from a
135 	 * syscall.  This copies most of the user mode register values.
136 	 */
137 	tf = (struct trapframe *)pcb - 1;
138 	bcopy(td1->td_frame, tf, sizeof(*tf));
139 
140 	/* Set up trap frame. */
141 	tf->fixreg[FIRSTARG] = 0;
142 	tf->fixreg[FIRSTARG + 1] = 0;
143 	tf->cr &= ~0x10000000;
144 
145 	td2->td_frame = tf;
146 
147 	cf = (struct callframe *)tf - 1;
148 	memset(cf, 0, sizeof(struct callframe));
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 	pcb->pcb_lr = (register_t)fork_trampoline;
155 	pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR];
156 
157 	/* Setup to release sched_lock in fork_exit(). */
158 	td2->td_md.md_spinlock_count = 1;
159 	td2->td_md.md_saved_msr = PSL_KERNSET;
160 
161 	/*
162  	 * Now cpu_switch() can schedule the new process.
163 	 */
164 }
165 
166 /*
167  * Intercept the return address from a freshly forked process that has NOT
168  * been scheduled yet.
169  *
170  * This is needed to make kernel threads stay in kernel mode.
171  */
172 void
173 cpu_set_fork_handler(td, func, arg)
174 	struct thread *td;
175 	void (*func)(void *);
176 	void *arg;
177 {
178 	struct	callframe *cf;
179 
180 	CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x",
181 	    (u_int)td, (u_int)func, (u_int)arg);
182 
183 	cf = (struct callframe *)td->td_pcb->pcb_sp;
184 
185 	cf->cf_func = (register_t)func;
186 	cf->cf_arg0 = (register_t)arg;
187 }
188 
189 void
190 cpu_exit(td)
191 	register struct thread *td;
192 {
193 }
194 
195 /* Temporary helper */
196 void
197 cpu_throw(struct thread *old, struct thread *new)
198 {
199 
200 	cpu_switch(old, new);
201 	panic("cpu_throw() didn't");
202 }
203 
204 /*
205  * Reset back to firmware.
206  */
207 void
208 cpu_reset()
209 {
210 	OF_reboot();
211 }
212 
213 /*
214  * Allocate an sf_buf for the given vm_page.  On this machine, however, there
215  * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
216  * returned.
217  */
218 struct sf_buf *
219 sf_buf_alloc(struct vm_page *m, int pri)
220 {
221 
222 	return ((struct sf_buf *)m);
223 }
224 
225 /*
226  * Free the sf_buf.  In fact, do nothing because there are no resources
227  * associated with the sf_buf.
228  */
229 void
230 sf_buf_free(struct sf_buf *sf)
231 {
232 }
233 
234 /*
235  * Software interrupt handler for queued VM system processing.
236  */
237 void
238 swi_vm(void *dummy)
239 {
240 #if 0 /* XXX: Don't have busdma stuff yet */
241 	if (busdma_swi_pending != 0)
242 		busdma_swi();
243 #endif
244 }
245 
246 /*
247  * Tell whether this address is in some physical memory region.
248  * Currently used by the kernel coredump code in order to avoid
249  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
250  * or other unpredictable behaviour.
251  */
252 
253 
254 int
255 is_physical_memory(addr)
256 	vm_offset_t addr;
257 {
258 	/*
259 	 * stuff other tests for known memory-mapped devices (PCI?)
260 	 * here
261 	 */
262 
263 	return 1;
264 }
265 
266 /*
267  * KSE functions
268  */
269 void
270 cpu_thread_exit(struct thread *td)
271 {
272 }
273 
274 void
275 cpu_thread_clean(struct thread *td)
276 {
277 }
278 
279 void
280 cpu_thread_setup(struct thread *td)
281 {
282 	struct pcb *pcb;
283 
284 	pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
285 	    sizeof(struct pcb)) & ~0x2fU);
286 	td->td_pcb = pcb;
287 	td->td_frame = (struct trapframe *)pcb - 1;
288 }
289 
290 void
291 cpu_thread_swapin(struct thread *td)
292 {
293 }
294 
295 void
296 cpu_thread_swapout(struct thread *td)
297 {
298 }
299 
300 void
301 cpu_set_upcall(struct thread *td, struct thread *td0)
302 {
303 	struct pcb *pcb2;
304 	struct trapframe *tf;
305 	struct callframe *cf;
306 
307 	pcb2 = td->td_pcb;
308 
309 	/* Copy the upcall pcb */
310 	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
311 
312 	/* Create a stack for the new thread */
313 	tf = td->td_frame;
314 	bcopy(td0->td_frame, tf, sizeof(struct trapframe));
315 	tf->fixreg[FIRSTARG] = 0;
316 	tf->fixreg[FIRSTARG + 1] = 0;
317 	tf->cr &= ~0x10000000;
318 
319 	/* Set registers for trampoline to user mode. */
320 	cf = (struct callframe *)tf - 1;
321 	memset(cf, 0, sizeof(struct callframe));
322 	cf->cf_func = (register_t)fork_return;
323 	cf->cf_arg0 = (register_t)td;
324 	cf->cf_arg1 = (register_t)tf;
325 
326 	pcb2->pcb_sp = (register_t)cf;
327 	pcb2->pcb_lr = (register_t)fork_trampoline;
328 	pcb2->pcb_usr = kernel_pmap->pm_sr[USER_SR];
329 
330 	/* Setup to release sched_lock in fork_exit(). */
331 	td->td_md.md_spinlock_count = 1;
332 	td->td_md.md_saved_msr = PSL_KERNSET;
333 }
334 
335 void
336 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
337 	stack_t *stack)
338 {
339         struct trapframe *tf;
340         uint32_t sp;
341 
342 	tf = td->td_frame;
343 	/* align stack and alloc space for frame ptr and saved LR */
344         sp = ((uint32_t)stack->ss_sp + stack->ss_size
345 		- 2*sizeof(u_int32_t)) & ~0x1f;
346 	bzero(tf, sizeof(struct trapframe));
347 
348 	tf->fixreg[1] = (register_t)sp;
349         tf->fixreg[3] = (register_t)arg;
350         tf->srr0 = (register_t)entry;
351         tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
352         td->td_pcb->pcb_flags = 0;
353 
354         td->td_retval[0] = (register_t)entry;
355         td->td_retval[1] = 0;
356 }
357 
358 int
359 cpu_set_user_tls(struct thread *td, void *tls_base)
360 {
361 
362 	td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
363 	return (0);
364 }
365