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