xref: /netbsd-src/sys/arch/sparc/sparc/vm_machdep.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: vm_machdep.c,v 1.101 2009/11/21 04:16:52 rmind Exp $ */
2 
3 /*
4  * Copyright (c) 1996
5  *	The President and Fellows of Harvard College. All rights reserved.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Lawrence Berkeley Laboratory.
17  *	This product includes software developed by Harvard University.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *	This product includes software developed by Harvard University.
30  *	This product includes software developed by the University of
31  *	California, Berkeley and its contributors.
32  * 4. Neither the name of the University nor the names of its contributors
33  *    may be used to endorse or promote products derived from this software
34  *    without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  *
48  *	@(#)vm_machdep.c	8.2 (Berkeley) 9/23/93
49  */
50 
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.101 2009/11/21 04:16:52 rmind Exp $");
53 
54 #include "opt_multiprocessor.h"
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/core.h>
60 #include <sys/malloc.h>
61 #include <sys/buf.h>
62 #include <sys/exec.h>
63 #include <sys/vnode.h>
64 #include <sys/simplelock.h>
65 
66 #include <uvm/uvm_extern.h>
67 
68 #include <machine/cpu.h>
69 #include <machine/frame.h>
70 #include <machine/trap.h>
71 
72 #include <sparc/sparc/cpuvar.h>
73 
74 /*
75  * Map a user I/O request into kernel virtual address space.
76  * Note: the pages are already locked by uvm_vslock(), so we
77  * do not need to pass an access_type to pmap_enter().
78  */
79 void
80 vmapbuf(struct buf *bp, vsize_t len)
81 {
82 	struct pmap *upmap, *kpmap;
83 	vaddr_t uva;	/* User VA (map from) */
84 	vaddr_t kva;	/* Kernel VA (new to) */
85 	paddr_t pa; 	/* physical address */
86 	vsize_t off;
87 
88 	if ((bp->b_flags & B_PHYS) == 0)
89 		panic("vmapbuf");
90 
91 	/*
92 	 * XXX:  It might be better to round/trunc to a
93 	 * segment boundary to avoid VAC problems!
94 	 */
95 	bp->b_saveaddr = bp->b_data;
96 	uva = trunc_page((vaddr_t)bp->b_data);
97 	off = (vaddr_t)bp->b_data - uva;
98 	len = round_page(off + len);
99 	kva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
100 	bp->b_data = (void *)(kva + off);
101 
102 	/*
103 	 * We have to flush any write-back cache on the
104 	 * user-space mappings so our new mappings will
105 	 * have the correct contents.
106 	 */
107 	if (CACHEINFO.c_vactype != VAC_NONE)
108 		cache_flush((void *)uva, len);
109 
110 	upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
111 	kpmap = vm_map_pmap(kernel_map);
112 	do {
113 		if (pmap_extract(upmap, uva, &pa) == false)
114 			panic("vmapbuf: null page frame");
115 		/* Now map the page into kernel space. */
116 		pmap_enter(kpmap, kva, pa,
117 		    VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
118 		uva += PAGE_SIZE;
119 		kva += PAGE_SIZE;
120 		len -= PAGE_SIZE;
121 	} while (len);
122 	pmap_update(kpmap);
123 }
124 
125 /*
126  * Unmap a previously-mapped user I/O request.
127  */
128 void
129 vunmapbuf(struct buf *bp, vsize_t len)
130 {
131 	vaddr_t kva;
132 	vsize_t off;
133 
134 	if ((bp->b_flags & B_PHYS) == 0)
135 		panic("vunmapbuf");
136 
137 	kva = trunc_page((vaddr_t)bp->b_data);
138 	off = (vaddr_t)bp->b_data - kva;
139 	len = round_page(off + len);
140 	pmap_remove(vm_map_pmap(kernel_map), kva, kva + len);
141 	pmap_update(vm_map_pmap(kernel_map));
142 	uvm_km_free(kernel_map, kva, len, UVM_KMF_VAONLY);
143 	bp->b_data = bp->b_saveaddr;
144 	bp->b_saveaddr = NULL;
145 
146 #if 0	/* XXX: The flush above is sufficient, right? */
147 	if (CACHEINFO.c_vactype != VAC_NONE)
148 		cpuinfo.cache_flush(bp->b_data, len);
149 #endif
150 }
151 
152 
153 void
154 cpu_proc_fork(struct proc *p1, struct proc *p2)
155 {
156 
157 	p2->p_md.md_flags = p1->p_md.md_flags;
158 }
159 
160 
161 /*
162  * The offset of the topmost frame in the kernel stack.
163  */
164 #define	TOPFRAMEOFF (USPACE-sizeof(struct trapframe)-sizeof(struct frame))
165 
166 /*
167  * Finish a fork operation, with process l2 nearly set up.
168  * Copy and update the pcb and trap frame, making the child ready to run.
169  *
170  * Rig the child's kernel stack so that it will start out in
171  * lwp_trampoline() and call child_return() with l2 as an
172  * argument. This causes the newly-created child process to go
173  * directly to user level with an apparent return value of 0 from
174  * fork(), while the parent process returns normally.
175  *
176  * l1 is the process being forked; if l1 == &lwp0, we are creating
177  * a kernel thread, and the return path and argument are specified with
178  * `func' and `arg'.
179  *
180  * If an alternate user-level stack is requested (with non-zero values
181  * in both the stack and stacksize args), set up the user stack pointer
182  * accordingly.
183  */
184 void
185 cpu_lwp_fork(struct lwp *l1, struct lwp *l2,
186 	     void *stack, size_t stacksize,
187 	     void (*func)(void *), void *arg)
188 {
189 	struct pcb *opcb = lwp_getpcb(l1);
190 	struct pcb *npcb = lwp_getpcb(l2);
191 	struct trapframe *tf2;
192 	struct rwindow *rp;
193 
194 	/*
195 	 * Save all user registers to l1's stack or, in the case of
196 	 * user registers and invalid stack pointers, to opcb.
197 	 * We then copy the whole pcb to l2; when switch() selects l2
198 	 * to run, it will run at the `lwp_trampoline' stub, rather
199 	 * than returning at the copying code below.
200 	 *
201 	 * If process l1 has an FPU state, we must copy it.  If it is
202 	 * the FPU user, we must save the FPU state first.
203 	 */
204 
205 	if (l1 == curlwp) {
206 		write_user_windows();
207 		opcb->pcb_psr = getpsr();
208 	}
209 #ifdef DIAGNOSTIC
210 	else if (l1 != &lwp0)	/* XXX is this valid? */
211 		panic("cpu_lwp_fork: curlwp");
212 #endif
213 
214 	memcpy((void *)npcb, (void *)opcb, sizeof(struct pcb));
215 	if (l1->l_md.md_fpstate != NULL) {
216 		struct cpu_info *cpi;
217 		int s;
218 
219 		l2->l_md.md_fpstate = malloc(sizeof(struct fpstate),
220 		    M_SUBPROC, M_WAITOK);
221 
222 		FPU_LOCK(s);
223 		if ((cpi = l1->l_md.md_fpu) != NULL) {
224 			if (cpi->fplwp != l1)
225 				panic("FPU(%d): fplwp %p",
226 					cpi->ci_cpuid, cpi->fplwp);
227 			if (l1 == cpuinfo.fplwp)
228 				savefpstate(l1->l_md.md_fpstate);
229 #if defined(MULTIPROCESSOR)
230 			else
231 				XCALL1(savefpstate, l1->l_md.md_fpstate,
232 					1 << cpi->ci_cpuid);
233 #endif
234 		}
235 		memcpy(l2->l_md.md_fpstate, l1->l_md.md_fpstate,
236 		    sizeof(struct fpstate));
237 		FPU_UNLOCK(s);
238 	} else
239 		l2->l_md.md_fpstate = NULL;
240 
241 	l2->l_md.md_fpu = NULL;
242 
243 	/*
244 	 * Setup (kernel) stack frame that will by-pass the child
245 	 * out of the kernel. (The trap frame invariably resides at
246 	 * the tippity-top of the u. area.)
247 	 */
248 	tf2 = l2->l_md.md_tf = (struct trapframe *)
249 			((int)npcb + USPACE - sizeof(*tf2));
250 
251 	/* Copy parent's trapframe */
252 	*tf2 = *(struct trapframe *)((int)opcb + USPACE - sizeof(*tf2));
253 
254 	/*
255 	 * If specified, give the child a different stack.
256 	 */
257 	if (stack != NULL)
258 		tf2->tf_out[6] = (u_int)stack + stacksize;
259 
260 	/*
261 	 * The fork system call always uses the old system call
262 	 * convention; clear carry and skip trap instruction as
263 	 * in syscall().
264 	 * note: lwp_trampoline() sets a fresh psr when returning
265 	 * to user mode.
266 	 */
267 	/*tf2->tf_psr &= ~PSR_C;   -* success */
268 	tf2->tf_pc = tf2->tf_npc;
269 	tf2->tf_npc = tf2->tf_pc + 4;
270 
271 	/* Set return values in child mode */
272 	tf2->tf_out[0] = 0;
273 	tf2->tf_out[1] = 1;
274 
275 	/* Construct kernel frame to return to in cpu_switch() */
276 	rp = (struct rwindow *)((u_int)npcb + TOPFRAMEOFF);
277 	/**rp = *(struct rwindow *)((u_int)opcb + TOPFRAMEOFF);*/
278 	rp->rw_local[0] = (int)func;		/* Function to call */
279 	rp->rw_local[1] = (int)arg;		/* and its argument */
280 	rp->rw_local[2] = (int)l2;		/* new LWP */
281 
282 	npcb->pcb_pc = (int)lwp_trampoline - 8;
283 	npcb->pcb_sp = (int)rp;
284 	npcb->pcb_psr &= ~PSR_CWP;	/* Run in window #0 */
285 	npcb->pcb_wim = 1;		/* Fence at window #1 */
286 }
287 
288 /*
289  * Cleanup FPU state.
290  */
291 void
292 cpu_lwp_free(struct lwp *l, int proc)
293 {
294 	struct fpstate *fs;
295 
296 	if ((fs = l->l_md.md_fpstate) != NULL) {
297 		struct cpu_info *cpi;
298 		int s;
299 
300 		FPU_LOCK(s);
301 		if ((cpi = l->l_md.md_fpu) != NULL) {
302 			if (cpi->fplwp != l)
303 				panic("FPU(%d): fplwp %p",
304 					cpi->ci_cpuid, cpi->fplwp);
305 			if (l == cpuinfo.fplwp)
306 				savefpstate(fs);
307 #if defined(MULTIPROCESSOR)
308 			else
309 				XCALL1(savefpstate, fs, 1 << cpi->ci_cpuid);
310 #endif
311 			cpi->fplwp = NULL;
312 		}
313 		l->l_md.md_fpu = NULL;
314 		FPU_UNLOCK(s);
315 	}
316 }
317 
318 void
319 cpu_lwp_free2(struct lwp *l)
320 {
321 	struct fpstate *fs;
322 
323 	if ((fs = l->l_md.md_fpstate) != NULL)
324 		free((void *)fs, M_SUBPROC);
325 }
326 
327 void
328 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
329 {
330 	struct pcb *pcb = lwp_getpcb(l);
331 	/*struct trapframe *tf = l->l_md.md_tf;*/
332 	struct rwindow *rp;
333 
334 	/* Construct kernel frame to return to in cpu_switch() */
335 	rp = (struct rwindow *)((u_int)pcb + TOPFRAMEOFF);
336 	rp->rw_local[0] = (int)func;		/* Function to call */
337 	rp->rw_local[1] = (int)arg;		/* and its argument */
338 
339 	pcb->pcb_pc = (int)lwp_setfunc_trampoline - 8;
340 	pcb->pcb_sp = (int)rp;
341 	pcb->pcb_psr &= ~PSR_CWP;	/* Run in window #0 */
342 	pcb->pcb_wim = 1;		/* Fence at window #1 */
343 }
344