vm_machdep.c revision 1.24 1 /* $NetBSD: vm_machdep.c,v 1.24 2003/01/17 22:28:49 thorpej 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 "opt_armfpe.h"
47 #include "opt_pmap_debug.h"
48 #include "opt_perfctrs.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/vnode.h>
55 #include <sys/buf.h>
56 #include <sys/pmc.h>
57 #include <sys/user.h>
58 #include <sys/exec.h>
59 #include <sys/syslog.h>
60
61 #include <uvm/uvm_extern.h>
62
63 #include <machine/cpu.h>
64 #include <machine/pmap.h>
65 #include <machine/reg.h>
66 #include <machine/vmparam.h>
67
68 #ifdef ARMFPE
69 #include <arm/fpe-arm/armfpe.h>
70 #endif
71
72 extern pv_addr_t systempage;
73
74 int process_read_regs __P((struct proc *p, struct reg *regs));
75 int process_read_fpregs __P((struct proc *p, struct fpreg *regs));
76
77 void switch_exit __P((struct lwp *l, struct lwp *l0,
78 void (*)(struct lwp *)));
79 extern void proc_trampoline __P((void));
80
81 /*
82 * Special compilation symbols:
83 *
84 * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
85 * on forking and check the pattern on exit, reporting
86 * the amount of stack used.
87 */
88
89 void
90 cpu_proc_fork(p1, p2)
91 struct proc *p1, *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 process p2 nearly set up.
106 * Copy and update the pcb and trap frame, making the child ready to run.
107 *
108 * Rig the child's kernel stack so that it will start out in
109 * proc_trampoline() and call child_return() with p2 as an
110 * argument. This causes the newly-created child process to go
111 * directly to user level with an apparent return value of 0 from
112 * fork(), while the parent process returns normally.
113 *
114 * p1 is the process being forked; if p1 == &proc0, we are creating
115 * a kernel thread, and the return path and argument are specified with
116 * `func' and `arg'.
117 *
118 * If an alternate user-level stack is requested (with non-zero values
119 * in both the stack and stacksize args), set up the user stack pointer
120 * accordingly.
121 */
122 void
123 cpu_lwp_fork(l1, l2, stack, stacksize, func, arg)
124 struct lwp *l1;
125 struct lwp *l2;
126 void *stack;
127 size_t stacksize;
128 void (*func) __P((void *));
129 void *arg;
130 {
131 struct pcb *pcb = (struct pcb *)&l2->l_addr->u_pcb;
132 struct trapframe *tf;
133 struct switchframe *sf;
134
135 #ifdef PMAP_DEBUG
136 if (pmap_debug_level >= 0)
137 printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
138 #endif /* PMAP_DEBUG */
139
140 #if 0 /* XXX */
141 if (l1 == curlwp) {
142 /* Sync the PCB before we copy it. */
143 savectx(curpcb);
144 }
145 #endif
146
147 /* Copy the pcb */
148 *pcb = l1->l_addr->u_pcb;
149
150 /*
151 * Set up the undefined stack for the process.
152 * Note: this stack is not in use if we are forking from p1
153 */
154 pcb->pcb_un.un_32.pcb32_und_sp = (u_int)l2->l_addr +
155 USPACE_UNDEF_STACK_TOP;
156 pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
157
158 #ifdef STACKCHECKS
159 /* Fill the undefined stack with a known pattern */
160 memset(((u_char *)l2->l_addr) + USPACE_UNDEF_STACK_BOTTOM, 0xdd,
161 (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM));
162 /* Fill the kernel stack with a known pattern */
163 memset(((u_char *)l2->l_addr) + 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->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
170 l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
171 l1->l_proc->p_vmspace->vm_map.pmap);
172 printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
173 l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
174 l2->l_proc->p_vmspace->vm_map.pmap);
175 }
176 #endif /* PMAP_DEBUG */
177
178 pmap_activate(l2);
179
180 #ifdef ARMFPE
181 /* Initialise a new FP context for p2 and copy the context from p1 */
182 arm_fpe_core_initcontext(FP_CONTEXT(l2));
183 arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
184 #endif /* ARMFPE */
185
186 l2->l_addr->u_pcb.pcb_tf = tf =
187 (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
188 *tf = *l1->l_addr->u_pcb.pcb_tf;
189
190 /*
191 * If specified, give the child a different stack.
192 */
193 if (stack != NULL)
194 tf->tf_usr_sp = (u_int)stack + stacksize;
195
196 sf = (struct switchframe *)tf - 1;
197 sf->sf_spl = 0; /* always equivalent to spl0() */
198 sf->sf_r4 = (u_int)func;
199 sf->sf_r5 = (u_int)arg;
200 sf->sf_pc = (u_int)proc_trampoline;
201 pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
202 }
203
204 void
205 cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
206 {
207 struct pcb *pcb = &l->l_addr->u_pcb;
208 struct trapframe *tf = pcb->pcb_tf;
209 struct switchframe *sf = (struct switchframe *)tf - 1;
210
211 sf->sf_spl = 0; /* always equivalent to spl0() */
212 sf->sf_r4 = (u_int)func;
213 sf->sf_r5 = (u_int)arg;
214 sf->sf_pc = (u_int)proc_trampoline;
215 pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
216 }
217
218 /*
219 * cpu_exit is called as the last action during exit.
220 *
221 * We clean up a little and then call switch_exit() with the old proc as an
222 * argument. switch_exit() first switches to proc0's context, and finally
223 * jumps into switch() to wait for another process to wake up.
224 */
225
226 void
227 cpu_exit(struct lwp *l, int proc)
228 {
229 #ifdef ARMFPE
230 /* Abort any active FP operation and deactivate the context */
231 arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
232 arm_fpe_core_changecontext(0);
233 #endif /* ARMFPE */
234
235 #ifdef STACKCHECKS
236 /* Report how much stack has been used - debugging */
237 if (l) {
238 u_char *ptr;
239 int loop;
240
241 ptr = ((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM;
242 for (loop = 0; loop < (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM)
243 && *ptr == 0xdd; ++loop, ++ptr) ;
244 log(LOG_INFO, "%d bytes of undefined stack fill pattern\n", loop);
245 ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
246 for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
247 && *ptr == 0xdd; ++loop, ++ptr) ;
248 log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
249 }
250 #endif /* STACKCHECKS */
251 uvmexp.swtch++;
252 switch_exit(l, &lwp0, proc ? exit2 : lwp_exit2);
253 }
254
255
256 void
257 cpu_swapin(l)
258 struct lwp *l;
259 {
260 #if 0
261 struct proc *p = l->l_proc;
262
263 /* Don't do this. See the comment in cpu_swapout(). */
264 #ifdef PMAP_DEBUG
265 if (pmap_debug_level >= 0)
266 printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
267 p->p_comm, p->p_vmspace->vm_map.pmap);
268 #endif /* PMAP_DEBUG */
269
270 if (vector_page < KERNEL_BASE) {
271 /* Map the vector page */
272 pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
273 systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
274 pmap_update(p->p_vmspace->vm_map.pmap);
275 }
276 #endif
277 }
278
279
280 void
281 cpu_swapout(l)
282 struct lwp *l;
283 {
284 #if 0
285 struct proc *p = l->l_proc;
286
287 /*
288 * Don't do this! If the pmap is shared with another process,
289 * it will loose it's page0 entry. That's bad news indeed.
290 */
291 #ifdef PMAP_DEBUG
292 if (pmap_debug_level >= 0)
293 printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
294 p->p_comm, &p->p_vmspace->vm_map.pmap);
295 #endif /* PMAP_DEBUG */
296
297 if (vector_page < KERNEL_BASE) {
298 /* Free the system page mapping */
299 pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
300 vector_page + NBPG);
301 pmap_update(p->p_vmspace->vm_map.pmap);
302 }
303 #endif
304 }
305
306
307 /*
308 * Move pages from one kernel virtual address to another.
309 * Both addresses are assumed to reside in the Sysmap,
310 * and size must be a multiple of NBPG.
311 */
312
313 void
314 pagemove(from, to, size)
315 caddr_t from, to;
316 size_t size;
317 {
318 pt_entry_t *fpte, *tpte;
319 size_t ptecnt = size >> PAGE_SHIFT;
320
321 if (size % NBPG)
322 panic("pagemove: size=%08lx", (u_long) size);
323
324 #ifdef PMAP_DEBUG
325 if (pmap_debug_level >= 0)
326 printf("pagemove: V%p to %p size %08lx\n",
327 from, to, (u_long) size);
328 #endif /* PMAP_DEBUG */
329
330 fpte = vtopte((vaddr_t)from);
331 tpte = vtopte((vaddr_t)to);
332
333 /*
334 * Make sure the cache does not have dirty data for the
335 * pages we are moving. Pages in the buffers are only
336 * ever moved with pagemove, so we only need to clean
337 * the 'from' area.
338 */
339
340 cpu_dcache_wbinv_range((vaddr_t) from, size);
341
342 while (size > 0) {
343 *tpte++ = *fpte;
344 *fpte++ = 0;
345 size -= NBPG;
346 }
347 PTE_SYNC_RANGE(vtopte((vaddr_t)from), ptecnt);
348 PTE_SYNC_RANGE(vtopte((vaddr_t)to), ptecnt);
349 //cpu_tlb_flushD();
350 }
351
352 /*
353 * Map a user I/O request into kernel virtual address space.
354 * Note: the pages are already locked by uvm_vslock(), so we
355 * do not need to pass an access_type to pmap_enter().
356 */
357 void
358 vmapbuf(bp, len)
359 struct buf *bp;
360 vsize_t len;
361 {
362 vaddr_t faddr, taddr, off;
363 paddr_t fpa;
364
365
366 #ifdef PMAP_DEBUG
367 if (pmap_debug_level >= 0)
368 printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
369 (u_int)bp->b_data, (u_int)len);
370 #endif /* PMAP_DEBUG */
371
372 if ((bp->b_flags & B_PHYS) == 0)
373 panic("vmapbuf");
374
375 faddr = trunc_page((vaddr_t)bp->b_saveaddr = bp->b_data);
376 off = (vaddr_t)bp->b_data - faddr;
377 len = round_page(off + len);
378 taddr = uvm_km_valloc_wait(phys_map, len);
379 bp->b_data = (caddr_t)(taddr + off);
380
381 /*
382 * The region is locked, so we expect that pmap_pte() will return
383 * non-NULL.
384 */
385 while (len) {
386 (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
387 faddr, &fpa);
388 pmap_enter(pmap_kernel(), taddr, fpa,
389 VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
390 faddr += PAGE_SIZE;
391 taddr += PAGE_SIZE;
392 len -= PAGE_SIZE;
393 }
394 pmap_update(pmap_kernel());
395 }
396
397 /*
398 * Unmap a previously-mapped user I/O request.
399 */
400 void
401 vunmapbuf(bp, len)
402 struct buf *bp;
403 vsize_t len;
404 {
405 vaddr_t addr, off;
406
407 #ifdef PMAP_DEBUG
408 if (pmap_debug_level >= 0)
409 printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
410 (u_int)bp, (u_int)bp->b_data, (u_int)len);
411 #endif /* PMAP_DEBUG */
412
413 if ((bp->b_flags & B_PHYS) == 0)
414 panic("vunmapbuf");
415
416 /*
417 * Make sure the cache does not have dirty data for the
418 * pages we had mapped.
419 */
420 addr = trunc_page((vaddr_t)bp->b_data);
421 off = (vaddr_t)bp->b_data - addr;
422 len = round_page(off + len);
423
424 pmap_remove(pmap_kernel(), addr, addr + len);
425 pmap_update(pmap_kernel());
426 uvm_km_free_wakeup(phys_map, addr, len);
427 bp->b_data = bp->b_saveaddr;
428 bp->b_saveaddr = 0;
429 }
430
431 /* End of vm_machdep.c */
432