vm_machdep.c revision 1.45 1 /* $NetBSD: vm_machdep.c,v 1.45 2008/08/07 04:16:26 matt 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.45 2008/08/07 04:16:26 matt 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 LWP l2 nearly set up.
108 *
109 * Copy and update the pcb and trapframe, making the child ready to run.
110 *
111 * Rig the child's kernel stack so that it will start out in
112 * lwp_trampoline() which will call the specified func with the argument arg.
113 *
114 * If an alternate user-level stack is requested (with non-zero values
115 * in both the stack and stacksize args), set up the user stack pointer
116 * accordingly.
117 */
118 void
119 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
120 void (*func)(void *), void *arg)
121 {
122 struct pcb *pcb = &l2->l_addr->u_pcb;
123 struct trapframe *tf;
124 struct switchframe *sf;
125
126 #ifdef PMAP_DEBUG
127 if (pmap_debug_level >= 0)
128 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
129 #endif /* PMAP_DEBUG */
130
131 #if 0 /* XXX */
132 if (l1 == curlwp) {
133 /* Sync the PCB before we copy it. */
134 savectx(curpcb);
135 }
136 #endif
137
138 l2->l_md.md_flags = l1->l_md.md_flags & MDP_VFPUSED;
139
140 #ifdef FPU_VFP
141 /*
142 * Copy the floating point state from the VFP to the PCB
143 * if this process has state stored there.
144 */
145 if (l1->l_addr->u_pcb.pcb_vfpcpu != NULL)
146 vfp_saveregs_lwp(l1, 1);
147 #endif
148
149 /* Copy the pcb */
150 *pcb = l1->l_addr->u_pcb;
151
152 /*
153 * Set up the stack for the process.
154 * Note: this stack is not in use if we are forking from p1
155 */
156 pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
157
158 #ifdef STACKCHECKS
159 /* Fill the kernel stack with a known pattern */
160 memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
161 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
162 #endif /* STACKCHECKS */
163
164 #ifdef PMAP_DEBUG
165 if (pmap_debug_level >= 0) {
166 printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
167 l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
168 l1->l_proc->p_vmspace->vm_map.pmap);
169 printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
170 l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
171 l2->l_proc->p_vmspace->vm_map.pmap);
172 }
173 #endif /* PMAP_DEBUG */
174
175 #ifdef ARMFPE
176 /* Initialise a new FP context for p2 and copy the context from p1 */
177 arm_fpe_core_initcontext(FP_CONTEXT(l2));
178 arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
179 #endif /* ARMFPE */
180
181 l2->l_addr->u_pcb.pcb_tf = tf =
182 (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
183 *tf = *l1->l_addr->u_pcb.pcb_tf;
184
185 /*
186 * If specified, give the child a different stack.
187 */
188 if (stack != NULL)
189 tf->tf_usr_sp = (u_int)stack + stacksize;
190
191 sf = (struct switchframe *)tf - 1;
192 sf->sf_r4 = (u_int)func;
193 sf->sf_r5 = (u_int)arg;
194 sf->sf_sp = (u_int)tf;
195 sf->sf_pc = (u_int)lwp_trampoline;
196 pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
197 }
198
199 /*
200 * cpu_exit is called as the last action during exit.
201 *
202 * We clean up a little and then call switch_exit() with the old proc as an
203 * argument. switch_exit() first switches to proc0's context, and finally
204 * jumps into switch() to wait for another process to wake up.
205 */
206
207 void
208 cpu_lwp_free(struct lwp *l, int proc)
209 {
210 #ifdef ARMFPE
211 /* Abort any active FP operation and deactivate the context */
212 arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
213 arm_fpe_core_changecontext(0);
214 #endif /* ARMFPE */
215
216 #ifdef FPU_VFP
217 if (l->l_addr->u_pcb.pcb_vfpcpu != NULL)
218 vfp_saveregs_lwp(l, 0);
219 #endif
220
221 #ifdef STACKCHECKS
222 /* Report how much stack has been used - debugging */
223 if (l) {
224 u_char *ptr;
225 int loop;
226
227 ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
228 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
229 && *ptr == 0xdd; ++loop, ++ptr) ;
230 log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
231 }
232 #endif /* STACKCHECKS */
233 }
234
235 void
236 cpu_lwp_free2(struct lwp *l)
237 {
238 }
239
240 void
241 cpu_swapin(l)
242 struct lwp *l;
243 {
244 #if 0
245 struct proc *p = l->l_proc;
246
247 /* Don't do this. See the comment in cpu_swapout(). */
248 #ifdef PMAP_DEBUG
249 if (pmap_debug_level >= 0)
250 printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
251 p->p_comm, p->p_vmspace->vm_map.pmap);
252 #endif /* PMAP_DEBUG */
253
254 if (vector_page < KERNEL_BASE) {
255 /* Map the vector page */
256 pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
257 systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
258 pmap_update(p->p_vmspace->vm_map.pmap);
259 }
260 #endif
261 }
262
263
264 void
265 cpu_swapout(l)
266 struct lwp *l;
267 {
268 #ifdef FPU_VFP
269 if (l->l_addr->u_pcb.pcb_vfpcpu != NULL)
270 vfp_saveregs_lwp(l, 1);
271 #endif
272
273 #if 0
274 struct proc *p = l->l_proc;
275
276 /*
277 * Don't do this! If the pmap is shared with another process,
278 * it will loose it's page0 entry. That's bad news indeed.
279 */
280 #ifdef PMAP_DEBUG
281 if (pmap_debug_level >= 0)
282 printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
283 p->p_comm, &p->p_vmspace->vm_map.pmap);
284 #endif /* PMAP_DEBUG */
285
286 if (vector_page < KERNEL_BASE) {
287 /* Free the system page mapping */
288 pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
289 vector_page + PAGE_SIZE);
290 pmap_update(p->p_vmspace->vm_map.pmap);
291 }
292 #endif
293 }
294
295 /*
296 * Map a user I/O request into kernel virtual address space.
297 * Note: the pages are already locked by uvm_vslock(), so we
298 * do not need to pass an access_type to pmap_enter().
299 */
300 void
301 vmapbuf(bp, len)
302 struct buf *bp;
303 vsize_t len;
304 {
305 vaddr_t faddr, taddr, off;
306 paddr_t fpa;
307
308
309 #ifdef PMAP_DEBUG
310 if (pmap_debug_level >= 0)
311 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
312 (u_int)bp->b_data, (u_int)len);
313 #endif /* PMAP_DEBUG */
314
315 if ((bp->b_flags & B_PHYS) == 0)
316 panic("vmapbuf");
317
318 bp->b_saveaddr = bp->b_data;
319 faddr = trunc_page((vaddr_t)bp->b_data);
320 off = (vaddr_t)bp->b_data - faddr;
321 len = round_page(off + len);
322 taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
323 bp->b_data = (void *)(taddr + off);
324
325 /*
326 * The region is locked, so we expect that pmap_pte() will return
327 * non-NULL.
328 */
329 while (len) {
330 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
331 faddr, &fpa);
332 pmap_enter(pmap_kernel(), taddr, fpa,
333 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
334 faddr += PAGE_SIZE;
335 taddr += PAGE_SIZE;
336 len -= PAGE_SIZE;
337 }
338 pmap_update(pmap_kernel());
339 }
340
341 /*
342 * Unmap a previously-mapped user I/O request.
343 */
344 void
345 vunmapbuf(bp, len)
346 struct buf *bp;
347 vsize_t len;
348 {
349 vaddr_t addr, off;
350
351 #ifdef PMAP_DEBUG
352 if (pmap_debug_level >= 0)
353 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
354 (u_int)bp, (u_int)bp->b_data, (u_int)len);
355 #endif /* PMAP_DEBUG */
356
357 if ((bp->b_flags & B_PHYS) == 0)
358 panic("vunmapbuf");
359
360 /*
361 * Make sure the cache does not have dirty data for the
362 * pages we had mapped.
363 */
364 addr = trunc_page((vaddr_t)bp->b_data);
365 off = (vaddr_t)bp->b_data - addr;
366 len = round_page(off + len);
367
368 pmap_remove(pmap_kernel(), addr, addr + len);
369 pmap_update(pmap_kernel());
370 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
371 bp->b_data = bp->b_saveaddr;
372 bp->b_saveaddr = 0;
373 }
374
375 /* End of vm_machdep.c */
376