vm_machdep.c revision 1.62 1 /* $NetBSD: vm_machdep.c,v 1.62 2012/12/05 19:05:45 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.62 2012/12/05 19:05:45 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 extern pv_addr_t systempage;
73
74 int process_read_regs(struct proc *p, struct reg *regs);
75 int process_read_fpregs(struct proc *p, struct fpreg *regs);
76
77 void lwp_trampoline(void);
78
79 /*
80 * Special compilation symbols:
81 *
82 * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
83 * on forking and check the pattern on exit, reporting
84 * the amount of stack used.
85 */
86
87 void
88 cpu_proc_fork(struct proc *p1, struct proc *p2)
89 {
90
91 #if defined(PERFCTRS)
92 if (PMC_ENABLED(p1))
93 pmc_md_fork(p1, p2);
94 else {
95 p2->p_md.pmc_enabled = 0;
96 p2->p_md.pmc_state = NULL;
97 }
98 #endif
99 }
100
101 /*
102 * Finish a fork operation, with LWP l2 nearly set up.
103 *
104 * Copy and update the pcb and trapframe, making the child ready to run.
105 *
106 * Rig the child's kernel stack so that it will start out in
107 * lwp_trampoline() which will call the specified func with the argument arg.
108 *
109 * If an alternate user-level stack is requested (with non-zero values
110 * in both the stack and stacksize args), set up the user stack pointer
111 * accordingly.
112 */
113 void
114 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize,
115 void (*func)(void *), void *arg)
116 {
117 struct pcb *pcb1, *pcb2;
118 struct switchframe *sf;
119 vaddr_t uv;
120
121 pcb1 = lwp_getpcb(l1);
122 pcb2 = lwp_getpcb(l2);
123
124 #ifdef PMAP_DEBUG
125 if (pmap_debug_level >= 0)
126 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
127 #endif /* PMAP_DEBUG */
128
129 #if 0 /* XXX */
130 if (l1 == curlwp) {
131 /* Sync the PCB before we copy it. */
132 savectx(curpcb);
133 }
134 #endif
135
136 l2->l_md.md_flags = l1->l_md.md_flags & MDLWP_VFPUSED;
137
138 /* Copy the pcb */
139 *pcb2 = *pcb1;
140
141 /*
142 * Set up the kernel stack for the process.
143 * Note: this stack is not in use if we are forking from p1
144 */
145 uv = uvm_lwp_getuarea(l2);
146 pcb2->pcb_sp = uv + USPACE_SVC_STACK_TOP;
147
148 #ifdef STACKCHECKS
149 /* Fill the kernel stack with a known pattern */
150 memset((void *)(uv + USPACE_SVC_STACK_BOTTOM), 0xdd,
151 (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
152 #endif /* STACKCHECKS */
153
154 #ifdef PMAP_DEBUG
155 if (pmap_debug_level >= 0) {
156 printf("l1: pcb=%p pid=%d pmap=%p\n",
157 pcb1, l1->l_lid, l1->l_proc->p_vmspace->vm_map.pmap);
158 printf("l2: pcb=%p pid=%d pmap=%p\n",
159 pcb2, l2->l_lid, l2->l_proc->p_vmspace->vm_map.pmap);
160 }
161 #endif /* PMAP_DEBUG */
162
163 struct trapframe *tf = (struct trapframe *)pcb2->pcb_sp - 1;
164 lwp_settrapframe(l2, tf);
165 *tf = *lwp_trapframe(l1);
166
167 /*
168 * If specified, give the child a different stack (make sure
169 * it's 8-byte aligned).
170 */
171 if (stack != NULL)
172 tf->tf_usr_sp = ((vaddr_t)(stack) + stacksize) & -8;
173
174 sf = (struct switchframe *)tf - 1;
175 sf->sf_r4 = (u_int)func;
176 sf->sf_r5 = (u_int)arg;
177 sf->sf_r7 = PSR_USR32_MODE; /* for returning to userspace */
178 sf->sf_sp = (u_int)tf;
179 sf->sf_pc = (u_int)lwp_trampoline;
180 pcb2->pcb_sp = (u_int)sf;
181 }
182
183 /*
184 * cpu_exit is called as the last action during exit.
185 *
186 * We clean up a little and then call switch_exit() with the old proc as an
187 * argument. switch_exit() first switches to lwp0's context, and finally
188 * jumps into switch() to wait for another process to wake up.
189 */
190
191 void
192 cpu_lwp_free(struct lwp *l, int proc)
193 {
194 #ifdef STACKCHECKS
195 /* Report how much stack has been used - debugging */
196 struct pcb * const pcb = lwp_getpcb(l);
197 u_char *ptr;
198 u_int loop;
199
200 ptr = (u_char *)pcb + USPACE_SVC_STACK_BOTTOM;
201 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
202 && *ptr == 0xdd; ++loop, ++ptr) ;
203 log(LOG_INFO, "%u bytes of svc stack fill pattern\n", loop);
204 #endif /* STACKCHECKS */
205 }
206
207 void
208 cpu_lwp_free2(struct lwp *l)
209 {
210 }
211
212 /*
213 * Map a user I/O request into kernel virtual address space.
214 * Note: the pages are already locked by uvm_vslock(), so we
215 * do not need to pass an access_type to pmap_enter().
216 */
217 int
218 vmapbuf(struct buf *bp, vsize_t len)
219 {
220 vaddr_t faddr, taddr, off;
221 paddr_t fpa;
222
223
224 #ifdef PMAP_DEBUG
225 if (pmap_debug_level >= 0)
226 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
227 (u_int)bp->b_data, (u_int)len);
228 #endif /* PMAP_DEBUG */
229
230 if ((bp->b_flags & B_PHYS) == 0)
231 panic("vmapbuf");
232
233 bp->b_saveaddr = bp->b_data;
234 faddr = trunc_page((vaddr_t)bp->b_data);
235 off = (vaddr_t)bp->b_data - faddr;
236 len = round_page(off + len);
237 taddr = uvm_km_alloc(phys_map, len, atop(faddr) & uvmexp.colormask,
238 UVM_KMF_VAONLY | UVM_KMF_WAITVA | UVM_KMF_COLORMATCH);
239 bp->b_data = (void *)(taddr + off);
240
241 /*
242 * The region is locked, so we expect that pmap_pte() will return
243 * non-NULL.
244 */
245 while (len) {
246 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
247 faddr, &fpa);
248 pmap_enter(pmap_kernel(), taddr, fpa,
249 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
250 faddr += PAGE_SIZE;
251 taddr += PAGE_SIZE;
252 len -= PAGE_SIZE;
253 }
254 pmap_update(pmap_kernel());
255
256 return 0;
257 }
258
259 /*
260 * Unmap a previously-mapped user I/O request.
261 */
262 void
263 vunmapbuf(struct buf *bp, vsize_t len)
264 {
265 vaddr_t addr, off;
266
267 #ifdef PMAP_DEBUG
268 if (pmap_debug_level >= 0)
269 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
270 (u_int)bp, (u_int)bp->b_data, (u_int)len);
271 #endif /* PMAP_DEBUG */
272
273 if ((bp->b_flags & B_PHYS) == 0)
274 panic("vunmapbuf");
275
276 /*
277 * Make sure the cache does not have dirty data for the
278 * pages we had mapped.
279 */
280 addr = trunc_page((vaddr_t)bp->b_data);
281 off = (vaddr_t)bp->b_data - addr;
282 len = round_page(off + len);
283
284 pmap_remove(pmap_kernel(), addr, addr + len);
285 pmap_update(pmap_kernel());
286 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY);
287 bp->b_data = bp->b_saveaddr;
288 bp->b_saveaddr = 0;
289 }
290
291 /* End of vm_machdep.c */
292