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