vm_machdep.c revision 1.56 1 /* $NetBSD: vm_machdep.c,v 1.56 2012/02/19 21:06:05 rmind 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.56 2012/02/19 21:06:05 rmind 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/exec.h>
62 #include <sys/syslog.h>
63
64 #include <uvm/uvm_extern.h>
65
66 #include <machine/cpu.h>
67 #include <machine/pmap.h>
68 #include <machine/reg.h>
69 #include <machine/vmparam.h>
70
71 #ifdef ARMFPE
72 #include <arm/fpe-arm/armfpe.h>
73 #endif
74
75 extern pv_addr_t systempage;
76
77 int process_read_regs(struct proc *p, struct reg *regs);
78 int process_read_fpregs(struct proc *p, struct fpreg *regs);
79
80 void lwp_trampoline(void);
81
82 /*
83 * Special compilation symbols:
84 *
85 * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
86 * on forking and check the pattern on exit, reporting
87 * the amount of stack used.
88 */
89
90 void
91 cpu_proc_fork(struct proc *p1, struct proc *p2)
92 {
93
94 #if defined(PERFCTRS)
95 if (PMC_ENABLED(p1))
96 pmc_md_fork(p1, p2);
97 else {
98 p2->p_md.pmc_enabled = 0;
99 p2->p_md.pmc_state = NULL;
100 }
101 #endif
102 }
103
104 /*
105 * Finish a fork operation, with LWP l2 nearly set up.
106 *
107 * Copy and update the pcb and trapframe, making the child ready to run.
108 *
109 * Rig the child's kernel stack so that it will start out in
110 * lwp_trampoline() which will call the specified func with the argument arg.
111 *
112 * If an alternate user-level stack is requested (with non-zero values
113 * in both the stack and stacksize args), set up the user stack pointer
114 * accordingly.
115 */
116 void
117 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
118 void (*func)(void *), void *arg)
119 {
120 struct pcb *pcb1, *pcb2;
121 struct trapframe *tf;
122 struct switchframe *sf;
123 vaddr_t uv;
124
125 pcb1 = lwp_getpcb(l1);
126 pcb2 = lwp_getpcb(l2);
127
128 #ifdef PMAP_DEBUG
129 if (pmap_debug_level >= 0)
130 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
131 #endif /* PMAP_DEBUG */
132
133 #if 0 /* XXX */
134 if (l1 == curlwp) {
135 /* Sync the PCB before we copy it. */
136 savectx(curpcb);
137 }
138 #endif
139
140 l2->l_md.md_flags = l1->l_md.md_flags & MDP_VFPUSED;
141
142 #ifdef FPU_VFP
143 /*
144 * Copy the floating point state from the VFP to the PCB
145 * if this process has state stored there.
146 */
147 if (pcb1->pcb_vfpcpu != NULL)
148 vfp_saveregs_lwp(l1, 1);
149 #endif
150
151 /* Copy the pcb */
152 *pcb2 = *pcb1;
153
154 /*
155 * Set up the kernel stack for the process.
156 * Note: this stack is not in use if we are forking from p1
157 */
158 uv = uvm_lwp_getuarea(l2);
159 pcb2->pcb_un.un_32.pcb32_sp = uv + USPACE_SVC_STACK_TOP;
160
161 #ifdef STACKCHECKS
162 /* Fill the kernel stack with a known pattern */
163 memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
164 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
165 #endif /* STACKCHECKS */
166
167 #ifdef PMAP_DEBUG
168 if (pmap_debug_level >= 0) {
169 printf("l1: pcb=%p pid=%d pmap=%p\n",
170 pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
171 printf("l2: pcb=%p pid=%d pmap=%p\n",
172 pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
173 }
174 #endif /* PMAP_DEBUG */
175
176 #ifdef ARMFPE
177 /* Initialise a new FP context for p2 and copy the context from p1 */
178 arm_fpe_core_initcontext(FP_CONTEXT(l2));
179 arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
180 #endif /* ARMFPE */
181
182 tf = (struct trapframe *)pcb2->pcb_un.un_32.pcb32_sp - 1;
183 pcb2->pcb_tf = tf;
184 *tf = *pcb1->pcb_tf;
185
186 /*
187 * If specified, give the child a different stack.
188 */
189 if (stack != NULL)
190 tf->tf_usr_sp = (u_int)stack + stacksize;
191
192 sf = (struct switchframe *)tf - 1;
193 sf->sf_r4 = (u_int)func;
194 sf->sf_r5 = (u_int)arg;
195 sf->sf_sp = (u_int)tf;
196 sf->sf_pc = (u_int)lwp_trampoline;
197 pcb2->pcb_un.un_32.pcb32_sp = (u_int)sf;
198 }
199
200 /*
201 * cpu_exit is called as the last action during exit.
202 *
203 * We clean up a little and then call switch_exit() with the old proc as an
204 * argument. switch_exit() first switches to lwp0's context, and finally
205 * jumps into switch() to wait for another process to wake up.
206 */
207
208 void
209 cpu_lwp_free(struct lwp *l, int proc)
210 {
211 #ifdef FPU_VFP
212 struct pcb *pcb;
213 #endif
214
215 #ifdef ARMFPE
216 /* Abort any active FP operation and deactivate the context */
217 arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
218 arm_fpe_core_changecontext(0);
219 #endif /* ARMFPE */
220
221 #ifdef FPU_VFP
222 pcb = lwp_getpcb(l);
223 if (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 *)pcb + 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 /*
247 * Map a user I/O request into kernel virtual address space.
248 * Note: the pages are already locked by uvm_vslock(), so we
249 * do not need to pass an access_type to pmap_enter().
250 */
251 int
252 vmapbuf(struct buf *bp, vsize_t len)
253 {
254 vaddr_t faddr, taddr, off;
255 paddr_t fpa;
256
257
258 #ifdef PMAP_DEBUG
259 if (pmap_debug_level >= 0)
260 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
261 (u_int)bp->b_data, (u_int)len);
262 #endif /* PMAP_DEBUG */
263
264 if ((bp->b_flags & B_PHYS) == 0)
265 panic("vmapbuf");
266
267 bp->b_saveaddr = bp->b_data;
268 faddr = trunc_page((vaddr_t)bp->b_data);
269 off = (vaddr_t)bp->b_data - faddr;
270 len = round_page(off + len);
271 taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
272 bp->b_data = (void *)(taddr + off);
273
274 /*
275 * The region is locked, so we expect that pmap_pte() will return
276 * non-NULL.
277 */
278 while (len) {
279 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
280 faddr, &fpa);
281 pmap_enter(pmap_kernel(), taddr, fpa,
282 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
283 faddr += PAGE_SIZE;
284 taddr += PAGE_SIZE;
285 len -= PAGE_SIZE;
286 }
287 pmap_update(pmap_kernel());
288
289 return 0;
290 }
291
292 /*
293 * Unmap a previously-mapped user I/O request.
294 */
295 void
296 vunmapbuf(struct buf *bp, vsize_t len)
297 {
298 vaddr_t addr, off;
299
300 #ifdef PMAP_DEBUG
301 if (pmap_debug_level >= 0)
302 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
303 (u_int)bp, (u_int)bp->b_data, (u_int)len);
304 #endif /* PMAP_DEBUG */
305
306 if ((bp->b_flags & B_PHYS) == 0)
307 panic("vunmapbuf");
308
309 /*
310 * Make sure the cache does not have dirty data for the
311 * pages we had mapped.
312 */
313 addr = trunc_page((vaddr_t)bp->b_data);
314 off = (vaddr_t)bp->b_data - addr;
315 len = round_page(off + len);
316
317 pmap_remove(pmap_kernel(), addr, addr + len);
318 pmap_update(pmap_kernel());
319 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
320 bp->b_data = bp->b_saveaddr;
321 bp->b_saveaddr = 0;
322 }
323
324 /* End of vm_machdep.c */
325