vm_machdep.c revision 1.24 1 1.24 thorpej /* $NetBSD: vm_machdep.c,v 1.24 2003/01/17 22:28:49 thorpej Exp $ */
2 1.1 chris
3 1.1 chris /*
4 1.1 chris * Copyright (c) 1994-1998 Mark Brinicombe.
5 1.1 chris * Copyright (c) 1994 Brini.
6 1.1 chris * All rights reserved.
7 1.1 chris *
8 1.1 chris * This code is derived from software written for Brini by Mark Brinicombe
9 1.1 chris *
10 1.1 chris * Redistribution and use in source and binary forms, with or without
11 1.1 chris * modification, are permitted provided that the following conditions
12 1.1 chris * are met:
13 1.1 chris * 1. Redistributions of source code must retain the above copyright
14 1.1 chris * notice, this list of conditions and the following disclaimer.
15 1.1 chris * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 chris * notice, this list of conditions and the following disclaimer in the
17 1.1 chris * documentation and/or other materials provided with the distribution.
18 1.1 chris * 3. All advertising materials mentioning features or use of this software
19 1.1 chris * must display the following acknowledgement:
20 1.1 chris * This product includes software developed by Brini.
21 1.1 chris * 4. The name of the company nor the name of the author may be used to
22 1.1 chris * endorse or promote products derived from this software without specific
23 1.1 chris * prior written permission.
24 1.1 chris *
25 1.1 chris * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 1.1 chris * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 1.1 chris * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.1 chris * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 1.1 chris * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 1.1 chris * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 1.1 chris * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 chris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 chris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 chris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 chris * SUCH DAMAGE.
36 1.1 chris *
37 1.1 chris * RiscBSD kernel project
38 1.1 chris *
39 1.1 chris * vm_machdep.h
40 1.1 chris *
41 1.1 chris * vm machine specific bits
42 1.1 chris *
43 1.1 chris * Created : 08/10/94
44 1.1 chris */
45 1.1 chris
46 1.1 chris #include "opt_armfpe.h"
47 1.1 chris #include "opt_pmap_debug.h"
48 1.18 briggs #include "opt_perfctrs.h"
49 1.1 chris
50 1.1 chris #include <sys/param.h>
51 1.1 chris #include <sys/systm.h>
52 1.1 chris #include <sys/proc.h>
53 1.1 chris #include <sys/malloc.h>
54 1.1 chris #include <sys/vnode.h>
55 1.1 chris #include <sys/buf.h>
56 1.18 briggs #include <sys/pmc.h>
57 1.1 chris #include <sys/user.h>
58 1.1 chris #include <sys/exec.h>
59 1.1 chris #include <sys/syslog.h>
60 1.1 chris
61 1.1 chris #include <uvm/uvm_extern.h>
62 1.1 chris
63 1.1 chris #include <machine/cpu.h>
64 1.1 chris #include <machine/pmap.h>
65 1.1 chris #include <machine/reg.h>
66 1.1 chris #include <machine/vmparam.h>
67 1.1 chris
68 1.1 chris #ifdef ARMFPE
69 1.10 thorpej #include <arm/fpe-arm/armfpe.h>
70 1.1 chris #endif
71 1.1 chris
72 1.1 chris extern pv_addr_t systempage;
73 1.1 chris
74 1.1 chris int process_read_regs __P((struct proc *p, struct reg *regs));
75 1.1 chris int process_read_fpregs __P((struct proc *p, struct fpreg *regs));
76 1.1 chris
77 1.24 thorpej void switch_exit __P((struct lwp *l, struct lwp *l0,
78 1.24 thorpej void (*)(struct lwp *)));
79 1.1 chris extern void proc_trampoline __P((void));
80 1.1 chris
81 1.1 chris /*
82 1.1 chris * Special compilation symbols:
83 1.1 chris *
84 1.1 chris * STACKCHECKS - Fill undefined and supervisor stacks with a known pattern
85 1.1 chris * on forking and check the pattern on exit, reporting
86 1.1 chris * the amount of stack used.
87 1.1 chris */
88 1.1 chris
89 1.24 thorpej void
90 1.24 thorpej cpu_proc_fork(p1, p2)
91 1.24 thorpej struct proc *p1, *p2;
92 1.24 thorpej {
93 1.24 thorpej
94 1.24 thorpej #if defined(PERFCTRS)
95 1.24 thorpej if (PMC_ENABLED(p1))
96 1.24 thorpej pmc_md_fork(p1, p2);
97 1.24 thorpej else {
98 1.24 thorpej p2->p_md.pmc_enabled = 0;
99 1.24 thorpej p2->p_md.pmc_state = NULL;
100 1.24 thorpej }
101 1.24 thorpej #endif
102 1.24 thorpej }
103 1.24 thorpej
104 1.1 chris /*
105 1.1 chris * Finish a fork operation, with process p2 nearly set up.
106 1.1 chris * Copy and update the pcb and trap frame, making the child ready to run.
107 1.1 chris *
108 1.1 chris * Rig the child's kernel stack so that it will start out in
109 1.1 chris * proc_trampoline() and call child_return() with p2 as an
110 1.1 chris * argument. This causes the newly-created child process to go
111 1.1 chris * directly to user level with an apparent return value of 0 from
112 1.1 chris * fork(), while the parent process returns normally.
113 1.1 chris *
114 1.1 chris * p1 is the process being forked; if p1 == &proc0, we are creating
115 1.1 chris * a kernel thread, and the return path and argument are specified with
116 1.1 chris * `func' and `arg'.
117 1.1 chris *
118 1.1 chris * If an alternate user-level stack is requested (with non-zero values
119 1.1 chris * in both the stack and stacksize args), set up the user stack pointer
120 1.1 chris * accordingly.
121 1.1 chris */
122 1.1 chris void
123 1.24 thorpej cpu_lwp_fork(l1, l2, stack, stacksize, func, arg)
124 1.24 thorpej struct lwp *l1;
125 1.24 thorpej struct lwp *l2;
126 1.1 chris void *stack;
127 1.1 chris size_t stacksize;
128 1.1 chris void (*func) __P((void *));
129 1.1 chris void *arg;
130 1.1 chris {
131 1.24 thorpej struct pcb *pcb = (struct pcb *)&l2->l_addr->u_pcb;
132 1.1 chris struct trapframe *tf;
133 1.1 chris struct switchframe *sf;
134 1.1 chris
135 1.1 chris #ifdef PMAP_DEBUG
136 1.1 chris if (pmap_debug_level >= 0)
137 1.24 thorpej printf("cpu_lwp_fork: %p %p %p %p\n", l1, l2, curlwp, &lwp0);
138 1.1 chris #endif /* PMAP_DEBUG */
139 1.1 chris
140 1.1 chris #if 0 /* XXX */
141 1.24 thorpej if (l1 == curlwp) {
142 1.1 chris /* Sync the PCB before we copy it. */
143 1.1 chris savectx(curpcb);
144 1.1 chris }
145 1.18 briggs #endif
146 1.18 briggs
147 1.1 chris /* Copy the pcb */
148 1.24 thorpej *pcb = l1->l_addr->u_pcb;
149 1.1 chris
150 1.1 chris /*
151 1.1 chris * Set up the undefined stack for the process.
152 1.1 chris * Note: this stack is not in use if we are forking from p1
153 1.1 chris */
154 1.24 thorpej pcb->pcb_un.un_32.pcb32_und_sp = (u_int)l2->l_addr +
155 1.5 toshii USPACE_UNDEF_STACK_TOP;
156 1.24 thorpej pcb->pcb_un.un_32.pcb32_sp = (u_int)l2->l_addr + USPACE_SVC_STACK_TOP;
157 1.1 chris
158 1.1 chris #ifdef STACKCHECKS
159 1.1 chris /* Fill the undefined stack with a known pattern */
160 1.24 thorpej memset(((u_char *)l2->l_addr) + USPACE_UNDEF_STACK_BOTTOM, 0xdd,
161 1.1 chris (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM));
162 1.1 chris /* Fill the kernel stack with a known pattern */
163 1.24 thorpej memset(((u_char *)l2->l_addr) + USPACE_SVC_STACK_BOTTOM, 0xdd,
164 1.1 chris (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM));
165 1.1 chris #endif /* STACKCHECKS */
166 1.1 chris
167 1.1 chris #ifdef PMAP_DEBUG
168 1.1 chris if (pmap_debug_level >= 0) {
169 1.24 thorpej printf("l1->procaddr=%p l1->procaddr->u_pcb=%p pid=%d pmap=%p\n",
170 1.24 thorpej l1->l_addr, &l1->l_addr->u_pcb, l1->l_lid,
171 1.24 thorpej l1->l_proc->p_vmspace->vm_map.pmap);
172 1.24 thorpej printf("l2->procaddr=%p l2->procaddr->u_pcb=%p pid=%d pmap=%p\n",
173 1.24 thorpej l2->l_addr, &l2->l_addr->u_pcb, l2->l_lid,
174 1.24 thorpej l2->l_proc->p_vmspace->vm_map.pmap);
175 1.1 chris }
176 1.1 chris #endif /* PMAP_DEBUG */
177 1.1 chris
178 1.24 thorpej pmap_activate(l2);
179 1.1 chris
180 1.1 chris #ifdef ARMFPE
181 1.1 chris /* Initialise a new FP context for p2 and copy the context from p1 */
182 1.24 thorpej arm_fpe_core_initcontext(FP_CONTEXT(l2));
183 1.24 thorpej arm_fpe_copycontext(FP_CONTEXT(l1), FP_CONTEXT(l2));
184 1.1 chris #endif /* ARMFPE */
185 1.1 chris
186 1.24 thorpej l2->l_addr->u_pcb.pcb_tf = tf =
187 1.5 toshii (struct trapframe *)pcb->pcb_un.un_32.pcb32_sp - 1;
188 1.24 thorpej *tf = *l1->l_addr->u_pcb.pcb_tf;
189 1.1 chris
190 1.1 chris /*
191 1.1 chris * If specified, give the child a different stack.
192 1.1 chris */
193 1.1 chris if (stack != NULL)
194 1.1 chris tf->tf_usr_sp = (u_int)stack + stacksize;
195 1.1 chris
196 1.1 chris sf = (struct switchframe *)tf - 1;
197 1.13 thorpej sf->sf_spl = 0; /* always equivalent to spl0() */
198 1.1 chris sf->sf_r4 = (u_int)func;
199 1.1 chris sf->sf_r5 = (u_int)arg;
200 1.1 chris sf->sf_pc = (u_int)proc_trampoline;
201 1.5 toshii pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
202 1.1 chris }
203 1.1 chris
204 1.24 thorpej void
205 1.24 thorpej cpu_setfunc(struct lwp *l, void (*func)(void *), void *arg)
206 1.24 thorpej {
207 1.24 thorpej struct pcb *pcb = &l->l_addr->u_pcb;
208 1.24 thorpej struct trapframe *tf = pcb->pcb_tf;
209 1.24 thorpej struct switchframe *sf = (struct switchframe *)tf - 1;
210 1.24 thorpej
211 1.24 thorpej sf->sf_spl = 0; /* always equivalent to spl0() */
212 1.24 thorpej sf->sf_r4 = (u_int)func;
213 1.24 thorpej sf->sf_r5 = (u_int)arg;
214 1.24 thorpej sf->sf_pc = (u_int)proc_trampoline;
215 1.24 thorpej pcb->pcb_un.un_32.pcb32_sp = (u_int)sf;
216 1.24 thorpej }
217 1.24 thorpej
218 1.1 chris /*
219 1.1 chris * cpu_exit is called as the last action during exit.
220 1.1 chris *
221 1.1 chris * We clean up a little and then call switch_exit() with the old proc as an
222 1.1 chris * argument. switch_exit() first switches to proc0's context, and finally
223 1.1 chris * jumps into switch() to wait for another process to wake up.
224 1.1 chris */
225 1.1 chris
226 1.1 chris void
227 1.24 thorpej cpu_exit(struct lwp *l, int proc)
228 1.1 chris {
229 1.1 chris #ifdef ARMFPE
230 1.1 chris /* Abort any active FP operation and deactivate the context */
231 1.24 thorpej arm_fpe_core_abort(FP_CONTEXT(l), NULL, NULL);
232 1.1 chris arm_fpe_core_changecontext(0);
233 1.1 chris #endif /* ARMFPE */
234 1.1 chris
235 1.1 chris #ifdef STACKCHECKS
236 1.1 chris /* Report how much stack has been used - debugging */
237 1.24 thorpej if (l) {
238 1.1 chris u_char *ptr;
239 1.1 chris int loop;
240 1.1 chris
241 1.1 chris ptr = ((u_char *)p2->p_addr) + USPACE_UNDEF_STACK_BOTTOM;
242 1.1 chris for (loop = 0; loop < (USPACE_UNDEF_STACK_TOP - USPACE_UNDEF_STACK_BOTTOM)
243 1.1 chris && *ptr == 0xdd; ++loop, ++ptr) ;
244 1.1 chris log(LOG_INFO, "%d bytes of undefined stack fill pattern\n", loop);
245 1.1 chris ptr = ((u_char *)p2->p_addr) + USPACE_SVC_STACK_BOTTOM;
246 1.1 chris for (loop = 0; loop < (USPACE_SVC_STACK_TOP - USPACE_SVC_STACK_BOTTOM)
247 1.1 chris && *ptr == 0xdd; ++loop, ++ptr) ;
248 1.1 chris log(LOG_INFO, "%d bytes of svc stack fill pattern\n", loop);
249 1.1 chris }
250 1.1 chris #endif /* STACKCHECKS */
251 1.1 chris uvmexp.swtch++;
252 1.24 thorpej switch_exit(l, &lwp0, proc ? exit2 : lwp_exit2);
253 1.1 chris }
254 1.1 chris
255 1.1 chris
256 1.1 chris void
257 1.24 thorpej cpu_swapin(l)
258 1.24 thorpej struct lwp *l;
259 1.1 chris {
260 1.8 rearnsha #if 0
261 1.24 thorpej struct proc *p = l->l_proc;
262 1.24 thorpej
263 1.8 rearnsha /* Don't do this. See the comment in cpu_swapout(). */
264 1.1 chris #ifdef PMAP_DEBUG
265 1.1 chris if (pmap_debug_level >= 0)
266 1.24 thorpej printf("cpu_swapin(%p, %d, %s, %p)\n", l, l->l_lid,
267 1.1 chris p->p_comm, p->p_vmspace->vm_map.pmap);
268 1.1 chris #endif /* PMAP_DEBUG */
269 1.1 chris
270 1.16 thorpej if (vector_page < KERNEL_BASE) {
271 1.16 thorpej /* Map the vector page */
272 1.16 thorpej pmap_enter(p->p_vmspace->vm_map.pmap, vector_page,
273 1.16 thorpej systempage.pv_pa, VM_PROT_READ, VM_PROT_READ|PMAP_WIRED);
274 1.16 thorpej pmap_update(p->p_vmspace->vm_map.pmap);
275 1.16 thorpej }
276 1.8 rearnsha #endif
277 1.1 chris }
278 1.1 chris
279 1.1 chris
280 1.1 chris void
281 1.24 thorpej cpu_swapout(l)
282 1.24 thorpej struct lwp *l;
283 1.1 chris {
284 1.8 rearnsha #if 0
285 1.24 thorpej struct proc *p = l->l_proc;
286 1.24 thorpej
287 1.8 rearnsha /*
288 1.8 rearnsha * Don't do this! If the pmap is shared with another process,
289 1.8 rearnsha * it will loose it's page0 entry. That's bad news indeed.
290 1.8 rearnsha */
291 1.1 chris #ifdef PMAP_DEBUG
292 1.1 chris if (pmap_debug_level >= 0)
293 1.24 thorpej printf("cpu_swapout(%p, %d, %s, %p)\n", l, l->l_lid,
294 1.1 chris p->p_comm, &p->p_vmspace->vm_map.pmap);
295 1.1 chris #endif /* PMAP_DEBUG */
296 1.1 chris
297 1.16 thorpej if (vector_page < KERNEL_BASE) {
298 1.16 thorpej /* Free the system page mapping */
299 1.16 thorpej pmap_remove(p->p_vmspace->vm_map.pmap, vector_page,
300 1.16 thorpej vector_page + NBPG);
301 1.16 thorpej pmap_update(p->p_vmspace->vm_map.pmap);
302 1.16 thorpej }
303 1.8 rearnsha #endif
304 1.1 chris }
305 1.1 chris
306 1.1 chris
307 1.1 chris /*
308 1.1 chris * Move pages from one kernel virtual address to another.
309 1.1 chris * Both addresses are assumed to reside in the Sysmap,
310 1.14 simonb * and size must be a multiple of NBPG.
311 1.1 chris */
312 1.1 chris
313 1.1 chris void
314 1.1 chris pagemove(from, to, size)
315 1.1 chris caddr_t from, to;
316 1.1 chris size_t size;
317 1.1 chris {
318 1.20 thorpej pt_entry_t *fpte, *tpte;
319 1.20 thorpej size_t ptecnt = size >> PAGE_SHIFT;
320 1.1 chris
321 1.1 chris if (size % NBPG)
322 1.2 matt panic("pagemove: size=%08lx", (u_long) size);
323 1.1 chris
324 1.1 chris #ifdef PMAP_DEBUG
325 1.1 chris if (pmap_debug_level >= 0)
326 1.2 matt printf("pagemove: V%p to %p size %08lx\n",
327 1.2 matt from, to, (u_long) size);
328 1.1 chris #endif /* PMAP_DEBUG */
329 1.1 chris
330 1.1 chris fpte = vtopte((vaddr_t)from);
331 1.1 chris tpte = vtopte((vaddr_t)to);
332 1.1 chris
333 1.1 chris /*
334 1.1 chris * Make sure the cache does not have dirty data for the
335 1.1 chris * pages we are moving. Pages in the buffers are only
336 1.1 chris * ever moved with pagemove, so we only need to clean
337 1.1 chris * the 'from' area.
338 1.1 chris */
339 1.1 chris
340 1.12 thorpej cpu_dcache_wbinv_range((vaddr_t) from, size);
341 1.1 chris
342 1.1 chris while (size > 0) {
343 1.1 chris *tpte++ = *fpte;
344 1.1 chris *fpte++ = 0;
345 1.1 chris size -= NBPG;
346 1.1 chris }
347 1.20 thorpej PTE_SYNC_RANGE(vtopte((vaddr_t)from), ptecnt);
348 1.20 thorpej PTE_SYNC_RANGE(vtopte((vaddr_t)to), ptecnt);
349 1.1 chris //cpu_tlb_flushD();
350 1.1 chris }
351 1.1 chris
352 1.1 chris /*
353 1.1 chris * Map a user I/O request into kernel virtual address space.
354 1.1 chris * Note: the pages are already locked by uvm_vslock(), so we
355 1.1 chris * do not need to pass an access_type to pmap_enter().
356 1.1 chris */
357 1.1 chris void
358 1.1 chris vmapbuf(bp, len)
359 1.1 chris struct buf *bp;
360 1.1 chris vsize_t len;
361 1.1 chris {
362 1.1 chris vaddr_t faddr, taddr, off;
363 1.3 chris paddr_t fpa;
364 1.3 chris
365 1.1 chris
366 1.1 chris #ifdef PMAP_DEBUG
367 1.1 chris if (pmap_debug_level >= 0)
368 1.1 chris printf("vmapbuf: bp=%08x buf=%08x len=%08x\n", (u_int)bp,
369 1.1 chris (u_int)bp->b_data, (u_int)len);
370 1.1 chris #endif /* PMAP_DEBUG */
371 1.1 chris
372 1.1 chris if ((bp->b_flags & B_PHYS) == 0)
373 1.1 chris panic("vmapbuf");
374 1.1 chris
375 1.3 chris faddr = trunc_page((vaddr_t)bp->b_saveaddr = bp->b_data);
376 1.1 chris off = (vaddr_t)bp->b_data - faddr;
377 1.1 chris len = round_page(off + len);
378 1.7 chris taddr = uvm_km_valloc_wait(phys_map, len);
379 1.1 chris bp->b_data = (caddr_t)(taddr + off);
380 1.1 chris
381 1.1 chris /*
382 1.1 chris * The region is locked, so we expect that pmap_pte() will return
383 1.1 chris * non-NULL.
384 1.1 chris */
385 1.3 chris while (len) {
386 1.3 chris (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
387 1.3 chris faddr, &fpa);
388 1.4 chris pmap_enter(pmap_kernel(), taddr, fpa,
389 1.23 chris VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
390 1.3 chris faddr += PAGE_SIZE;
391 1.3 chris taddr += PAGE_SIZE;
392 1.3 chris len -= PAGE_SIZE;
393 1.3 chris }
394 1.6 chris pmap_update(pmap_kernel());
395 1.1 chris }
396 1.1 chris
397 1.1 chris /*
398 1.1 chris * Unmap a previously-mapped user I/O request.
399 1.1 chris */
400 1.1 chris void
401 1.1 chris vunmapbuf(bp, len)
402 1.1 chris struct buf *bp;
403 1.1 chris vsize_t len;
404 1.1 chris {
405 1.1 chris vaddr_t addr, off;
406 1.1 chris
407 1.1 chris #ifdef PMAP_DEBUG
408 1.1 chris if (pmap_debug_level >= 0)
409 1.1 chris printf("vunmapbuf: bp=%08x buf=%08x len=%08x\n",
410 1.1 chris (u_int)bp, (u_int)bp->b_data, (u_int)len);
411 1.1 chris #endif /* PMAP_DEBUG */
412 1.1 chris
413 1.1 chris if ((bp->b_flags & B_PHYS) == 0)
414 1.1 chris panic("vunmapbuf");
415 1.1 chris
416 1.1 chris /*
417 1.1 chris * Make sure the cache does not have dirty data for the
418 1.1 chris * pages we had mapped.
419 1.1 chris */
420 1.1 chris addr = trunc_page((vaddr_t)bp->b_data);
421 1.1 chris off = (vaddr_t)bp->b_data - addr;
422 1.1 chris len = round_page(off + len);
423 1.4 chris
424 1.4 chris pmap_remove(pmap_kernel(), addr, addr + len);
425 1.6 chris pmap_update(pmap_kernel());
426 1.3 chris uvm_km_free_wakeup(phys_map, addr, len);
427 1.1 chris bp->b_data = bp->b_saveaddr;
428 1.1 chris bp->b_saveaddr = 0;
429 1.1 chris }
430 1.1 chris
431 1.1 chris /* End of vm_machdep.c */
432