xref: /netbsd-src/sys/arch/sparc/sparc/vm_machdep.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: vm_machdep.c,v 1.94 2008/01/05 22:51:34 martin 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.94 2008/01/05 22:51:34 martin Exp $");
53 
54 #include "opt_multiprocessor.h"
55 #include "opt_coredump.h"
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/proc.h>
60 #include <sys/user.h>
61 #include <sys/core.h>
62 #include <sys/malloc.h>
63 #include <sys/buf.h>
64 #include <sys/exec.h>
65 #include <sys/vnode.h>
66 #include <sys/simplelock.h>
67 
68 #include <uvm/uvm_extern.h>
69 
70 #include <machine/cpu.h>
71 #include <machine/frame.h>
72 #include <machine/trap.h>
73 
74 #include <sparc/sparc/cpuvar.h>
75 
76 /*
77  * Map a user I/O request into kernel virtual address space.
78  * Note: the pages are already locked by uvm_vslock(), so we
79  * do not need to pass an access_type to pmap_enter().
80  */
81 void
82 vmapbuf(struct buf *bp, vsize_t len)
83 {
84 	struct pmap *upmap, *kpmap;
85 	vaddr_t uva;	/* User VA (map from) */
86 	vaddr_t kva;	/* Kernel VA (new to) */
87 	paddr_t pa; 	/* physical address */
88 	vsize_t off;
89 
90 	if ((bp->b_flags & B_PHYS) == 0)
91 		panic("vmapbuf");
92 
93 	/*
94 	 * XXX:  It might be better to round/trunc to a
95 	 * segment boundary to avoid VAC problems!
96 	 */
97 	bp->b_saveaddr = bp->b_data;
98 	uva = trunc_page((vaddr_t)bp->b_data);
99 	off = (vaddr_t)bp->b_data - uva;
100 	len = round_page(off + len);
101 	kva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
102 	bp->b_data = (void *)(kva + off);
103 
104 	/*
105 	 * We have to flush any write-back cache on the
106 	 * user-space mappings so our new mappings will
107 	 * have the correct contents.
108 	 */
109 	if (CACHEINFO.c_vactype != VAC_NONE)
110 		cache_flush((void *)uva, len);
111 
112 	upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
113 	kpmap = vm_map_pmap(kernel_map);
114 	do {
115 		if (pmap_extract(upmap, uva, &pa) == false)
116 			panic("vmapbuf: null page frame");
117 		/* Now map the page into kernel space. */
118 		pmap_enter(kpmap, kva, pa,
119 		    VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
120 		uva += PAGE_SIZE;
121 		kva += PAGE_SIZE;
122 		len -= PAGE_SIZE;
123 	} while (len);
124 	pmap_update(kpmap);
125 }
126 
127 /*
128  * Unmap a previously-mapped user I/O request.
129  */
130 void
131 vunmapbuf(struct buf *bp, vsize_t len)
132 {
133 	vaddr_t kva;
134 	vsize_t off;
135 
136 	if ((bp->b_flags & B_PHYS) == 0)
137 		panic("vunmapbuf");
138 
139 	kva = trunc_page((vaddr_t)bp->b_data);
140 	off = (vaddr_t)bp->b_data - kva;
141 	len = round_page(off + len);
142 	pmap_remove(vm_map_pmap(kernel_map), kva, kva + len);
143 	pmap_update(vm_map_pmap(kernel_map));
144 	uvm_km_free(kernel_map, kva, len, UVM_KMF_VAONLY);
145 	bp->b_data = bp->b_saveaddr;
146 	bp->b_saveaddr = NULL;
147 
148 #if 0	/* XXX: The flush above is sufficient, right? */
149 	if (CACHEINFO.c_vactype != VAC_NONE)
150 		cpuinfo.cache_flush(bp->b_data, len);
151 #endif
152 }
153 
154 
155 void
156 cpu_proc_fork(struct proc *p1, struct proc *p2)
157 {
158 
159 	p2->p_md.md_flags = p1->p_md.md_flags;
160 }
161 
162 
163 /*
164  * The offset of the topmost frame in the kernel stack.
165  */
166 #define	TOPFRAMEOFF (USPACE-sizeof(struct trapframe)-sizeof(struct frame))
167 
168 /*
169  * Finish a fork operation, with process l2 nearly set up.
170  * Copy and update the pcb and trap frame, making the child ready to run.
171  *
172  * Rig the child's kernel stack so that it will start out in
173  * lwp_trampoline() and call child_return() with l2 as an
174  * argument. This causes the newly-created child process to go
175  * directly to user level with an apparent return value of 0 from
176  * fork(), while the parent process returns normally.
177  *
178  * l1 is the process being forked; if l1 == &lwp0, we are creating
179  * a kernel thread, and the return path and argument are specified with
180  * `func' and `arg'.
181  *
182  * If an alternate user-level stack is requested (with non-zero values
183  * in both the stack and stacksize args), set up the user stack pointer
184  * accordingly.
185  */
186 void
187 cpu_lwp_fork(struct lwp *l1, struct lwp *l2,
188 	     void *stack, size_t stacksize,
189 	     void (*func)(void *), void *arg)
190 {
191 	struct pcb *opcb = &l1->l_addr->u_pcb;
192 	struct pcb *npcb = &l2->l_addr->u_pcb;
193 	struct trapframe *tf2;
194 	struct rwindow *rp;
195 
196 	/*
197 	 * Save all user registers to l1's stack or, in the case of
198 	 * user registers and invalid stack pointers, to opcb.
199 	 * We then copy the whole pcb to l2; when switch() selects l2
200 	 * to run, it will run at the `lwp_trampoline' stub, rather
201 	 * than returning at the copying code below.
202 	 *
203 	 * If process l1 has an FPU state, we must copy it.  If it is
204 	 * the FPU user, we must save the FPU state first.
205 	 */
206 
207 	if (l1 == curlwp) {
208 		write_user_windows();
209 		opcb->pcb_psr = getpsr();
210 	}
211 
212 	bcopy((void *)opcb, (void *)npcb, sizeof(struct pcb));
213 	if (l1->l_md.md_fpstate != NULL) {
214 		struct cpu_info *cpi;
215 		int s;
216 
217 		l2->l_md.md_fpstate = malloc(sizeof(struct fpstate),
218 		    M_SUBPROC, M_WAITOK);
219 
220 		FPU_LOCK(s);
221 		if ((cpi = l1->l_md.md_fpu) != NULL) {
222 			if (cpi->fplwp != l1)
223 				panic("FPU(%d): fplwp %p",
224 					cpi->ci_cpuid, cpi->fplwp);
225 			if (l1 == cpuinfo.fplwp)
226 				savefpstate(l1->l_md.md_fpstate);
227 #if defined(MULTIPROCESSOR)
228 			else
229 				XCALL1(savefpstate, l1->l_md.md_fpstate,
230 					1 << cpi->ci_cpuid);
231 #endif
232 		}
233 		bcopy(l1->l_md.md_fpstate, l2->l_md.md_fpstate,
234 		    sizeof(struct fpstate));
235 		FPU_UNLOCK(s);
236 	} else
237 		l2->l_md.md_fpstate = NULL;
238 
239 	l2->l_md.md_fpu = NULL;
240 
241 	/*
242 	 * Setup (kernel) stack frame that will by-pass the child
243 	 * out of the kernel. (The trap frame invariably resides at
244 	 * the tippity-top of the u. area.)
245 	 */
246 	tf2 = l2->l_md.md_tf = (struct trapframe *)
247 			((int)npcb + USPACE - sizeof(*tf2));
248 
249 	/* Copy parent's trapframe */
250 	*tf2 = *(struct trapframe *)((int)opcb + USPACE - sizeof(*tf2));
251 
252 	/*
253 	 * If specified, give the child a different stack.
254 	 */
255 	if (stack != NULL)
256 		tf2->tf_out[6] = (u_int)stack + stacksize;
257 
258 	/*
259 	 * The fork system call always uses the old system call
260 	 * convention; clear carry and skip trap instruction as
261 	 * in syscall().
262 	 * note: lwp_trampoline() sets a fresh psr when returning
263 	 * to user mode.
264 	 */
265 	/*tf2->tf_psr &= ~PSR_C;   -* success */
266 	tf2->tf_pc = tf2->tf_npc;
267 	tf2->tf_npc = tf2->tf_pc + 4;
268 
269 	/* Set return values in child mode */
270 	tf2->tf_out[0] = 0;
271 	tf2->tf_out[1] = 1;
272 
273 	/* Construct kernel frame to return to in cpu_switch() */
274 	rp = (struct rwindow *)((u_int)npcb + TOPFRAMEOFF);
275 	rp->rw_local[0] = (int)func;		/* Function to call */
276 	rp->rw_local[1] = (int)arg;		/* and its argument */
277 	rp->rw_local[2] = (int)l2;		/* the new LWP */
278 
279 	npcb->pcb_pc = (int)lwp_trampoline - 8;
280 	npcb->pcb_sp = (int)rp;
281 	npcb->pcb_psr &= ~PSR_CWP;	/* Run in window #0 */
282 	npcb->pcb_wim = 1;		/* Fence at window #1 */
283 }
284 
285 /*
286  * Cleanup FPU state.
287  */
288 void
289 cpu_lwp_free(struct lwp *l, int proc)
290 {
291 	struct fpstate *fs;
292 
293 	if ((fs = l->l_md.md_fpstate) != NULL) {
294 		struct cpu_info *cpi;
295 		int s;
296 
297 		FPU_LOCK(s);
298 		if ((cpi = l->l_md.md_fpu) != NULL) {
299 			if (cpi->fplwp != l)
300 				panic("FPU(%d): fplwp %p",
301 					cpi->ci_cpuid, cpi->fplwp);
302 			if (l == cpuinfo.fplwp)
303 				savefpstate(fs);
304 #if defined(MULTIPROCESSOR)
305 			else
306 				XCALL1(savefpstate, fs, 1 << cpi->ci_cpuid);
307 #endif
308 			cpi->fplwp = NULL;
309 		}
310 		l->l_md.md_fpu = NULL;
311 		FPU_UNLOCK(s);
312 	}
313 }
314 
315 void
316 cpu_lwp_free2(struct lwp *l)
317 {
318 	struct fpstate *fs;
319 
320 	if ((fs = l->l_md.md_fpstate) != NULL)
321 		free((void *)fs, M_SUBPROC);
322 }
323 
324 #ifdef COREDUMP
325 /*
326  * cpu_coredump is called to write a core dump header.
327  * (should this be defined elsewhere?  machdep.c?)
328  */
329 int
330 cpu_coredump(struct lwp *l, void *iocookie, struct core *chdr)
331 {
332 	int error;
333 	struct md_coredump md_core;
334 	struct coreseg cseg;
335 
336 	if (iocookie == NULL) {
337 		CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
338 		chdr->c_hdrsize = ALIGN(sizeof(*chdr));
339 		chdr->c_seghdrsize = ALIGN(sizeof(cseg));
340 		chdr->c_cpusize = sizeof(md_core);
341 		chdr->c_nseg++;
342 		return 0;
343 	}
344 
345 	md_core.md_tf = *l->l_md.md_tf;
346 	if (l->l_md.md_fpstate) {
347 		if (l == cpuinfo.fplwp)
348 			savefpstate(l->l_md.md_fpstate);
349 		md_core.md_fpstate = *l->l_md.md_fpstate;
350 	} else
351 		bzero((void *)&md_core.md_fpstate, sizeof(struct fpstate));
352 
353 	CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
354 	cseg.c_addr = 0;
355 	cseg.c_size = chdr->c_cpusize;
356 
357 	error = coredump_write(iocookie, UIO_SYSSPACE, &cseg,
358 	    chdr->c_seghdrsize);
359 	if (error)
360 		return error;
361 
362 	return coredump_write(iocookie, UIO_SYSSPACE, &md_core,
363 	    sizeof(md_core));
364 }
365 #endif
366