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