vm_machdep.c revision 1.60 1 /* $NetBSD: vm_machdep.c,v 1.60 2012/08/29 07:09:12 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.60 2012/08/29 07:09:12 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/cpu.h>
60 #include <sys/buf.h>
61 #include <sys/pmc.h>
62 #include <sys/exec.h>
63 #include <sys/syslog.h>
64
65 #include <uvm/uvm_extern.h>
66
67 #include <machine/pcb.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(struct proc *p, struct reg *regs);
79 int process_read_fpregs(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(struct proc *p1, struct proc *p2)
93 {
94
95 #if defined(PERFCTRS)
96 if (PMC_ENABLED(p1))
97 pmc_md_fork(p1, p2);
98 else {
99 p2->p_md.pmc_enabled = 0;
100 p2->p_md.pmc_state = NULL;
101 }
102 #endif
103 }
104
105 /*
106 * Finish a fork operation, with LWP l2 nearly set up.
107 *
108 * Copy and update the pcb and trapframe, making the child ready to run.
109 *
110 * Rig the child's kernel stack so that it will start out in
111 * lwp_trampoline() which will call the specified func with the argument arg.
112 *
113 * If an alternate user-level stack is requested (with non-zero values
114 * in both the stack and stacksize args), set up the user stack pointer
115 * accordingly.
116 */
117 void
118 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
119 void (*func)(void *), void *arg)
120 {
121 struct pcb *pcb1, *pcb2;
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 & MDLWP_VFPUSED;
141
142 /* Copy the pcb */
143 *pcb2 = *pcb1;
144
145 /*
146 * Set up the kernel stack for the process.
147 * Note: this stack is not in use if we are forking from p1
148 */
149 uv = uvm_lwp_getuarea(l2);
150 pcb2->pcb_sp = uv + USPACE_SVC_STACK_TOP;
151
152 #ifdef STACKCHECKS
153 /* Fill the kernel stack with a known pattern */
154 memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
155 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
156 #endif /* STACKCHECKS */
157
158 #ifdef PMAP_DEBUG
159 if (pmap_debug_level >= 0) {
160 printf("l1: pcb=%p pid=%d pmap=%p\n",
161 pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
162 printf("l2: pcb=%p pid=%d pmap=%p\n",
163 pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
164 }
165 #endif /* PMAP_DEBUG */
166
167 #ifdef ARMFPE
168 /* Initialise a new FP context for p2 and copy the context from p1 */
169 arm_fpe_core_initcontext(FP_CONTEXT(l2));
170 arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
171 #endif /* ARMFPE */
172
173 struct trapframe *tf = (struct trapframe *)pcb2->pcb_sp - 1;
174 lwp_settrapframe(l2, tf);
175 *tf = *lwp_trapframe(l1);
176
177 /*
178 * If specified, give the child a different stack (make sure
179 * it's 8-byte aligned).
180 */
181 if (stack != NULL)
182 tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8;
183
184 sf = (struct switchframe *)tf - 1;
185 sf->sf_r4 = (u_int)func;
186 sf->sf_r5 = (u_int)arg;
187 sf->sf_r7 = PSR_USR32_MODE; /* for returning to userspace */
188 sf->sf_sp = (u_int)tf;
189 sf->sf_pc = (u_int)lwp_trampoline;
190 pcb2->pcb_sp = (u_int)sf;
191 }
192
193 /*
194 * cpu_exit is called as the last action during exit.
195 *
196 * We clean up a little and then call switch_exit() with the old proc as an
197 * argument. switch_exit() first switches to lwp0's context, and finally
198 * jumps into switch() to wait for another process to wake up.
199 */
200
201 void
202 cpu_lwp_free(struct lwp *l, int proc)
203 {
204 #ifdef ARMFPE
205 /* Abort any active FP operation and deactivate the context */
206 arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
207 arm_fpe_core_changecontext(0);
208 #endif /* ARMFPE */
209
210 #ifdef STACKCHECKS
211 /* Report how much stack has been used - debugging */
212 if (l) {
213 u_char *ptr;
214 int loop;
215
216 ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM;
217 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
218 && *ptr == 0xdd; ++loop, ++ptr) ;
219 log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
220 }
221 #endif /* STACKCHECKS */
222 }
223
224 void
225 cpu_lwp_free2(struct lwp *l)
226 {
227 }
228
229 /*
230 * Map a user I/O request into kernel virtual address space.
231 * Note: the pages are already locked by uvm_vslock(), so we
232 * do not need to pass an access_type to pmap_enter().
233 */
234 int
235 vmapbuf(struct buf *bp, vsize_t len)
236 {
237 vaddr_t faddr, taddr, off;
238 paddr_t fpa;
239
240
241 #ifdef PMAP_DEBUG
242 if (pmap_debug_level >= 0)
243 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
244 (u_int)bp->b_data, (u_int)len);
245 #endif /* PMAP_DEBUG */
246
247 if ((bp->b_flags & B_PHYS) == 0)
248 panic("vmapbuf");
249
250 bp->b_saveaddr = bp->b_data;
251 faddr = trunc_page((vaddr_t)bp->b_data);
252 off = (vaddr_t)bp->b_data - faddr;
253 len = round_page(off + len);
254 taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);
255 bp->b_data = (void *)(taddr + off);
256
257 /*
258 * The region is locked, so we expect that pmap_pte() will return
259 * non-NULL.
260 */
261 while (len) {
262 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
263 faddr, &fpa);
264 pmap_enter(pmap_kernel(), taddr, fpa,
265 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
266 faddr += PAGE_SIZE;
267 taddr += PAGE_SIZE;
268 len -= PAGE_SIZE;
269 }
270 pmap_update(pmap_kernel());
271
272 return 0;
273 }
274
275 /*
276 * Unmap a previously-mapped user I/O request.
277 */
278 void
279 vunmapbuf(struct buf *bp, vsize_t len)
280 {
281 vaddr_t addr, off;
282
283 #ifdef PMAP_DEBUG
284 if (pmap_debug_level >= 0)
285 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
286 (u_int)bp, (u_int)bp->b_data, (u_int)len);
287 #endif /* PMAP_DEBUG */
288
289 if ((bp->b_flags & B_PHYS) == 0)
290 panic("vunmapbuf");
291
292 /*
293 * Make sure the cache does not have dirty data for the
294 * pages we had mapped.
295 */
296 addr = trunc_page((vaddr_t)bp->b_data);
297 off = (vaddr_t)bp->b_data - addr;
298 len = round_page(off + len);
299
300 pmap_remove(pmap_kernel(), addr, addr + len);
301 pmap_update(pmap_kernel());
302 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
303 bp->b_data = bp->b_saveaddr;
304 bp->b_saveaddr = 0;
305 }
306
307 /* End of vm_machdep.c */
308