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