1 /* $OpenBSD: vm_machdep.c,v 1.52 2007/05/27 20:59:25 miod Exp $ */
2 /* $NetBSD: vm_machdep.c,v 1.61 1996/05/03 19:42:35 christos Exp $ */
3
4 /*-
5 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
6 * Copyright (c) 1982, 1986 The Regents of the University of California.
7 * Copyright (c) 1989, 1990 William Jolitz
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * the Systems Programming Group of the University of Utah Computer
12 * Science Department, and William Jolitz.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
39 */
40
41 /*
42 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/signalvar.h>
49 #include <sys/malloc.h>
50 #include <sys/vnode.h>
51 #include <sys/buf.h>
52 #include <sys/user.h>
53 #include <sys/core.h>
54 #include <sys/exec.h>
55 #include <sys/ptrace.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <machine/cpu.h>
60 #include <machine/gdt.h>
61 #include <machine/reg.h>
62 #include <machine/specialreg.h>
63
64 #include "npx.h"
65
66 /*
67 * Finish a fork operation, with process p2 nearly set up.
68 * Copy and update the kernel stack and pcb, making the child
69 * ready to run, and marking it so that it can return differently
70 * than the parent. Returns 1 in the child process, 0 in the parent.
71 * We currently double-map the user area so that the stack is at the same
72 * address in each process; in the future we will probably relocate
73 * the frame pointers on the stack after copying.
74 */
75 void
76 cpu_fork(struct proc *p1, struct proc *p2, void *stack, size_t stacksize,
77 void (*func)(void *), void *arg)
78 {
79 struct pcb *pcb = &p2->p_addr->u_pcb;
80 struct trapframe *tf;
81 struct switchframe *sf;
82
83 #if NNPX > 0
84 npxsave_proc(p1, 1);
85 #endif
86
87 p2->p_md.md_flags = p1->p_md.md_flags;
88
89 /* Copy pcb from proc p1 to p2. */
90 if (p1 == curproc) {
91 /* Sync the PCB before we copy it. */
92 savectx(curpcb);
93 }
94 #ifdef DIAGNOSTIC
95 else if (p1 != &proc0)
96 panic("cpu_fork: curproc");
97 #endif
98 *pcb = p1->p_addr->u_pcb;
99
100 /*
101 * Preset these so that gdt_compact() doesn't get confused if called
102 * during the allocations below.
103 *
104 * Note: pcb_ldt_sel is handled in the pmap_activate() call when
105 * we run the new process.
106 */
107 p2->p_md.md_tss_sel = GSEL(GNULL_SEL, SEL_KPL);
108
109 /* Fix up the TSS. */
110 pcb->pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
111 pcb->pcb_tss.tss_esp0 = (int)p2->p_addr + USPACE - 16;
112
113 p2->p_md.md_tss_sel = tss_alloc(pcb);
114
115 /*
116 * Copy the trapframe, and arrange for the child to return directly
117 * through rei().
118 */
119 p2->p_md.md_regs = tf = (struct trapframe *)pcb->pcb_tss.tss_esp0 - 1;
120 *tf = *p1->p_md.md_regs;
121
122 /*
123 * If specified, give the child a different stack.
124 */
125 if (stack != NULL)
126 tf->tf_esp = (u_int)stack + stacksize;
127
128 sf = (struct switchframe *)tf - 1;
129 sf->sf_ppl = 0;
130 sf->sf_esi = (int)func;
131 sf->sf_ebx = (int)arg;
132 sf->sf_eip = (int)proc_trampoline;
133 pcb->pcb_esp = (int)sf;
134 }
135
136 /*
137 * cpu_exit is called as the last action during exit.
138 *
139 * We clean up a little and then call switch_exit() with the old proc as an
140 * argument. switch_exit() first switches to proc0's context, then does the
141 * vmspace_free() and kmem_free() that we don't do here, and finally jumps
142 * into switch() to wait for another process to wake up.
143 */
144 void
145 cpu_exit(struct proc *p)
146 {
147 #if NNPX > 0
148 /* If we were using the FPU, forget about it. */
149 if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
150 npxsave_proc(p, 0);
151 #endif
152
153 pmap_deactivate(p);
154 switch_exit(p);
155 }
156
157 void
158 cpu_wait(struct proc *p)
159 {
160 tss_free(p->p_md.md_tss_sel);
161 }
162
163 /*
164 * Dump the machine specific segment at the start of a core dump.
165 */
166 struct md_core {
167 struct reg intreg;
168 struct fpreg freg;
169 };
170
171 int
172 cpu_coredump(struct proc *p, struct vnode *vp, struct ucred *cred,
173 struct core *chdr)
174 {
175 struct md_core md_core;
176 struct coreseg cseg;
177 int error;
178
179 CORE_SETMAGIC(*chdr, COREMAGIC, MID_I386, 0);
180 chdr->c_hdrsize = ALIGN(sizeof(*chdr));
181 chdr->c_seghdrsize = ALIGN(sizeof(cseg));
182 chdr->c_cpusize = sizeof(md_core);
183
184 /* Save integer registers. */
185 error = process_read_regs(p, &md_core.intreg);
186 if (error)
187 return error;
188
189 /* Save floating point registers. */
190 error = process_read_fpregs(p, &md_core.freg);
191 if (error)
192 return error;
193
194 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_I386, CORE_CPU);
195 cseg.c_addr = 0;
196 cseg.c_size = chdr->c_cpusize;
197
198 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cseg, chdr->c_seghdrsize,
199 (off_t)chdr->c_hdrsize, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred,
200 NULL, p);
201 if (error)
202 return error;
203
204 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&md_core, sizeof(md_core),
205 (off_t)(chdr->c_hdrsize + chdr->c_seghdrsize), UIO_SYSSPACE,
206 IO_NODELOCKED|IO_UNIT, cred, NULL, p);
207 if (error)
208 return error;
209
210 chdr->c_nseg++;
211 return 0;
212 }
213
214 /*
215 * Convert kernel VA to physical address
216 */
217 int
218 kvtop(caddr_t addr)
219 {
220 paddr_t pa;
221
222 if (pmap_extract(pmap_kernel(), (vaddr_t)addr, &pa) == FALSE)
223 panic("kvtop: zero page frame");
224 return((int)pa);
225 }
226
227 /*
228 * Map an user IO request into kernel virtual address space.
229 */
230 void
231 vmapbuf(struct buf *bp, vsize_t len)
232 {
233 vaddr_t faddr, taddr, off;
234 paddr_t fpa;
235
236 if ((bp->b_flags & B_PHYS) == 0)
237 panic("vmapbuf");
238 faddr = trunc_page((vaddr_t)(bp->b_saveaddr = bp->b_data));
239 off = (vaddr_t)bp->b_data - faddr;
240 len = round_page(off + len);
241 taddr= uvm_km_valloc_wait(phys_map, len);
242 bp->b_data = (caddr_t)(taddr + off);
243 /*
244 * The region is locked, so we expect that pmap_pte() will return
245 * non-NULL.
246 * XXX: unwise to expect this in a multithreaded environment.
247 * anything can happen to a pmap between the time we lock a
248 * region, release the pmap lock, and then relock it for
249 * the pmap_extract().
250 *
251 * no need to flush TLB since we expect nothing to be mapped
252 * where we we just allocated (TLB will be flushed when our
253 * mapping is removed).
254 */
255 while (len) {
256 pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
257 faddr, &fpa);
258 pmap_kenter_pa(taddr, fpa, VM_PROT_READ|VM_PROT_WRITE);
259 faddr += PAGE_SIZE;
260 taddr += PAGE_SIZE;
261 len -= PAGE_SIZE;
262 }
263 pmap_update(pmap_kernel());
264 }
265
266 /*
267 * Free the io map PTEs associated with this IO operation.
268 * We also invalidate the TLB entries and restore the original b_addr.
269 */
270 void
271 vunmapbuf(struct buf *bp, vsize_t len)
272 {
273 vaddr_t addr, off;
274
275 if ((bp->b_flags & B_PHYS) == 0)
276 panic("vunmapbuf");
277 addr = trunc_page((vaddr_t)bp->b_data);
278 off = (vaddr_t)bp->b_data - addr;
279 len = round_page(off + len);
280 pmap_kremove(addr, len);
281 pmap_update(pmap_kernel());
282 uvm_km_free_wakeup(phys_map, addr, len);
283 bp->b_data = bp->b_saveaddr;
284 bp->b_saveaddr = 0;
285 }