vm_machdep.c revision 1.43 1 /* $NetBSD: vm_machdep.c,v 1.43 2008/03/15 10:19:40 rearnsha Exp $ */
2
3 /*
4 * Copyright (c) 1994-1998 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
6 * All rights reserved.
7 *
8 * This code is derived from software written for Brini by Mark Brinicombe
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Brini.
21 * 4. The name of the company nor the name of the author may be used to
22 * endorse or promote products derived from this software without specific
23 * prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * RiscBSD kernel project
38 *
39 * vm_machdep.h
40 *
41 * vm machine specific bits
42 *
43 * Created : 08/10/94
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.43 2008/03/15 10:19:40 rearnsha Exp $");
48
49 #include "opt_armfpe.h"
50 #include "opt_pmap_debug.h"
51 #include "opt_perfctrs.h"
52 #include "opt_cputypes.h"
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/proc.h>
57 #include <sys/malloc.h>
58 #include <sys/vnode.h>
59 #include <sys/buf.h>
60 #include <sys/pmc.h>
61 #include <sys/user.h>
62 #include <sys/exec.h>
63 #include <sys/syslog.h>
64
65 #include <uvm/uvm_extern.h>
66
67 #include <machine/cpu.h>
68 #include <machine/pmap.h>
69 #include <machine/reg.h>
70 #include <machine/vmparam.h>
71
72 #ifdef ARMFPE
73 #include <arm/fpe-arm/armfpe.h>
74 #endif
75
76 extern pv_addr_t systempage;
77
78 int process_read_regs __P((struct proc *p, struct reg *regs));
79 int process_read_fpregs __P((struct proc *p, struct fpreg *regs));
80
81 void lwp_trampoline(void);
82
83 /*
84 * Special compilation symbols:
85 *
86 * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
87 * on forking and check the pattern on exit, reporting
88 * the amount of stack used.
89 */
90
91 void
92 cpu_proc_fork(p1, p2)
93 struct proc *p1, *p2;
94 {
95
96 #if defined(PERFCTRS)
97 if (PMC_ENABLED(p1))
98 pmc_md_fork(p1, p2);
99 else {
100 p2->p_md.pmc_enabled = 0;
101 p2->p_md.pmc_state = NULL;
102 }
103 #endif
104 }
105
106 /*
107 * Finish a fork operation, with process p2 nearly set up.
108 * Copy and update the pcb and trap frame, making the child ready to run.
109 *
110 * Rig the child's kernel stack so that it will start out in
111 * proc_trampoline() and call child_return() with p2 as an
112 * argument. This causes the newly-created child process to go
113 * directly to user level with an apparent return value of 0 from
114 * fork(), while the parent process returns normally.
115 *
116 * p1 is the process being forked; if p1 == &proc0, we are creating
117 * a kernel thread, and the return path and argument are specified with
118 * `func' and `arg'.
119 *
120 * If an alternate user-level stack is requested (with non-zero values
121 * in both the stack and stacksize args), set up the user stack pointer
122 * accordingly.
123 */
124 void
125 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
126 void (*func)(void *), void *arg)
127 {
128 struct pcb *pcb = (struct pcb *)&l2->l_addr->u_pcb;
129 struct trapframe *tf;
130 struct switchframe *sf;
131
132 #ifdef PMAP_DEBUG
133 if (pmap_debug_level >= 0)
134 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
135 #endif /* PMAP_DEBUG */
136
137 #if 0 /* XXX */
138 if (l1 == curlwp) {
139 /* Sync the PCB before we copy it. */
140 savectx(curpcb);
141 }
142 #endif
143
144 l2->l_md.md_flags = l1->l_md.md_flags & MDP_VFPUSED;
145
146 #ifdef FPU_VFP
147 /*
148 * Copy the floating point state from the VFP to the PCB
149 * if this process has state stored there.
150 */
151 if (l1->l_addr->u_pcb.pcb_vfpcpu != NULL)
152 vfp_saveregs_lwp(l1, 1);
153 #endif
154
155 /* Copy the pcb */
156 *pcb = l1->l_addr->u_pcb;
157
158 /*
159 * Set up the stack for the process.
160 * Note: this stack is not in use if we are forking from p1
161 */
162 pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
163
164 #ifdef STACKCHECKS
165 /* Fill the kernel stack with a known pattern */
166 memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
167 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
168 #endif /* STACKCHECKS */
169
170 #ifdef PMAP_DEBUG
171 if (pmap_debug_level >= 0) {
172 printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
173 l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
174 l1->l_proc->p_vmspace->vm_map.pmap);
175 printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
176 l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
177 l2->l_proc->p_vmspace->vm_map.pmap);
178 }
179 #endif /* PMAP_DEBUG */
180
181 #ifdef ARMFPE
182 /* Initialise a new FP context for p2 and copy the context from p1 */
183 arm_fpe_core_initcontext(FP_CONTEXT(l2));
184 arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
185 #endif /* ARMFPE */
186
187 l2->l_addr->u_pcb.pcb_tf = tf =
188 (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
189 *tf = *l1->l_addr->u_pcb.pcb_tf;
190
191 /*
192 * If specified, give the child a different stack.
193 */
194 if (stack != NULL)
195 tf->tf_usr_sp = (u_int)stack + stacksize;
196
197 sf = (struct switchframe *)tf - 1;
198 sf->sf_r4 = (u_int)func;
199 sf->sf_r5 = (u_int)arg;
200 sf->sf_sp = (u_int)tf;
201 sf->sf_pc = (u_int)lwp_trampoline;
202 pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
203 }
204
205 /*
206 * cpu_exit is called as the last action during exit.
207 *
208 * We clean up a little and then call switch_exit() with the old proc as an
209 * argument. switch_exit() first switches to proc0's context, and finally
210 * jumps into switch() to wait for another process to wake up.
211 */
212
213 void
214 cpu_lwp_free(struct lwp *l, int proc)
215 {
216 #ifdef ARMFPE
217 /* Abort any active FP operation and deactivate the context */
218 arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
219 arm_fpe_core_changecontext(0);
220 #endif /* ARMFPE */
221
222 #ifdef FPU_VFP
223 if (l->l_addr->u_pcb.pcb_vfpcpu != NULL)
224 vfp_saveregs_lwp(l, 0);
225 #endif
226
227 #ifdef STACKCHECKS
228 /* Report how much stack has been used - debugging */
229 if (l) {
230 u_char *ptr;
231 int loop;
232
233 ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
234 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
235 && *ptr == 0xdd; ++loop, ++ptr) ;
236 log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
237 }
238 #endif /* STACKCHECKS */
239 }
240
241 void
242 cpu_lwp_free2(struct lwp *l)
243 {
244 }
245
246 void
247 cpu_swapin(l)
248 struct lwp *l;
249 {
250 #if 0
251 struct proc *p = l->l_proc;
252
253 /* Don't do this. See the comment in cpu_swapout(). */
254 #ifdef PMAP_DEBUG
255 if (pmap_debug_level >= 0)
256 printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
257 p->p_comm, p->p_vmspace->vm_map.pmap);
258 #endif /* PMAP_DEBUG */
259
260 if (vector_page < KERNEL_BASE) {
261 /* Map the vector page */
262 pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
263 systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
264 pmap_update(p->p_vmspace->vm_map.pmap);
265 }
266 #endif
267 }
268
269
270 void
271 cpu_swapout(l)
272 struct lwp *l;
273 {
274 #ifdef FPU_VFP
275 if (l->l_addr->u_pcb.pcb_vfpcpu != NULL)
276 vfp_saveregs_lwp(l, 1);
277 #endif
278
279 #if 0
280 struct proc *p = l->l_proc;
281
282 /*
283 * Don't do this! If the pmap is shared with another process,
284 * it will loose it's page0 entry. That's bad news indeed.
285 */
286 #ifdef PMAP_DEBUG
287 if (pmap_debug_level >= 0)
288 printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
289 p->p_comm, &p->p_vmspace->vm_map.pmap);
290 #endif /* PMAP_DEBUG */
291
292 if (vector_page < KERNEL_BASE) {
293 /* Free the system page mapping */
294 pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
295 vector_page + PAGE_SIZE);
296 pmap_update(p->p_vmspace->vm_map.pmap);
297 }
298 #endif
299 }
300
301 /*
302 * Map a user I/O request into kernel virtual address space.
303 * Note: the pages are already locked by uvm_vslock(), so we
304 * do not need to pass an access_type to pmap_enter().
305 */
306 void
307 vmapbuf(bp, len)
308 struct buf *bp;
309 vsize_t len;
310 {
311 vaddr_t faddr, taddr, off;
312 paddr_t fpa;
313
314
315 #ifdef PMAP_DEBUG
316 if (pmap_debug_level >= 0)
317 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
318 (u_int)bp->b_data, (u_int)len);
319 #endif /* PMAP_DEBUG */
320
321 if ((bp->b_flags & B_PHYS) == 0)
322 panic("vmapbuf");
323
324 bp->b_saveaddr = bp->b_data;
325 faddr = trunc_page((vaddr_t)bp->b_data);
326 off = (vaddr_t)bp->b_data - faddr;
327 len = round_page(off + len);
328 taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
329 bp->b_data = (void *)(taddr + off);
330
331 /*
332 * The region is locked, so we expect that pmap_pte() will return
333 * non-NULL.
334 */
335 while (len) {
336 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
337 faddr, &fpa);
338 pmap_enter(pmap_kernel(), taddr, fpa,
339 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
340 faddr += PAGE_SIZE;
341 taddr += PAGE_SIZE;
342 len -= PAGE_SIZE;
343 }
344 pmap_update(pmap_kernel());
345 }
346
347 /*
348 * Unmap a previously-mapped user I/O request.
349 */
350 void
351 vunmapbuf(bp, len)
352 struct buf *bp;
353 vsize_t len;
354 {
355 vaddr_t addr, off;
356
357 #ifdef PMAP_DEBUG
358 if (pmap_debug_level >= 0)
359 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
360 (u_int)bp, (u_int)bp->b_data, (u_int)len);
361 #endif /* PMAP_DEBUG */
362
363 if ((bp->b_flags & B_PHYS) == 0)
364 panic("vunmapbuf");
365
366 /*
367 * Make sure the cache does not have dirty data for the
368 * pages we had mapped.
369 */
370 addr = trunc_page((vaddr_t)bp->b_data);
371 off = (vaddr_t)bp->b_data - addr;
372 len = round_page(off + len);
373
374 pmap_remove(pmap_kernel(), addr, addr + len);
375 pmap_update(pmap_kernel());
376 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
377 bp->b_data = bp->b_saveaddr;
378 bp->b_saveaddr = 0;
379 }
380
381 /* End of vm_machdep.c */
382