Annotation of sys/arch/mac68k/mac68k/vm_machdep.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: vm_machdep.c,v 1.39 2007/05/27 20:59:25 miod Exp $ */
2: /* $NetBSD: vm_machdep.c,v 1.29 1998/07/28 18:34:55 thorpej Exp $ */
3:
4: /*
5: * Copyright (c) 1988 University of Utah.
6: * Copyright (c) 1982, 1986, 1990, 1993
7: * The Regents of the University of California. All rights reserved.
8: *
9: * This code is derived from software contributed to Berkeley by
10: * the Systems Programming Group of the University of Utah Computer
11: * Science Department.
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. Redistributions in binary form must reproduce the above copyright
19: * notice, this list of conditions and the following disclaimer in the
20: * documentation and/or other materials provided with the distribution.
21: * 3. Neither the name of the University nor the names of its contributors
22: * may be used to endorse or promote products derived from this software
23: * without specific prior written permission.
24: *
25: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31: * OR 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: /*
38: * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
39: *
40: * @(#)vm_machdep.c 8.6 (Berkeley) 1/12/94
41: */
42:
43: #include <sys/param.h>
44: #include <sys/systm.h>
45: #include <sys/proc.h>
46: #include <sys/signalvar.h>
47: #include <sys/malloc.h>
48: #include <sys/buf.h>
49: #include <sys/user.h>
50: #include <sys/vnode.h>
51: #include <sys/core.h>
52: #include <sys/exec.h>
53: #include <sys/ptrace.h>
54:
55: #include <uvm/uvm_extern.h>
56:
57: #include <machine/cpu.h>
58: #include <machine/pmap.h>
59: #include <machine/pte.h>
60: #include <machine/reg.h>
61:
62: void savectx(struct pcb *);
63:
64: /*
65: * Finish a fork operation, with process p2 nearly set up.
66: * Copy and update the kernel stack and pcb, making the child
67: * ready to run, and marking it so that it can return differently
68: * than the parent. Returns 1 in the child process, 0 in the parent.
69: * We currently double-map the user area so that the stack is at the same
70: * address in each process; in the future we will probably relocate
71: * the frame pointers on the stack after copying.
72: */
73: void
74: cpu_fork(p1, p2, stack, stacksize, func, arg)
75: struct proc *p1, *p2;
76: void *stack;
77: size_t stacksize;
78: void (*func)(void *);
79: void *arg;
80: {
81: struct pcb *pcb = &p2->p_addr->u_pcb;
82: struct trapframe *tf;
83: struct switchframe *sf;
84: extern struct pcb *curpcb;
85:
86: p2->p_md.md_flags = p1->p_md.md_flags;
87:
88: /* Copy pcb from proc p1 to p2. */
89: if (p1 == curproc) {
90: /* Sync the PCB before we copy it. */
91: savectx(curpcb);
92: }
93: #ifdef DIAGNOSTIC
94: else if (p1 != &proc0)
95: panic("cpu_fork: curproc");
96: #endif
97: *pcb = p1->p_addr->u_pcb;
98:
99: /*
100: * Copy the trap frame.
101: */
102: tf = (struct trapframe *)((u_int)p2->p_addr + USPACE) -1;
103: p2->p_md.md_regs = (int *)tf;
104: *tf = *(struct trapframe *)p1->p_md.md_regs;
105:
106: /*
107: * If specified, give the child a different stack.
108: */
109: if (stack != NULL)
110: tf->tf_regs[15] = (u_int)stack + stacksize;
111:
112: sf = (struct switchframe *)tf - 1;
113: sf->sf_pc = (u_int)proc_trampoline;
114: pcb->pcb_regs[6] = (int)func; /* A2 */
115: pcb->pcb_regs[7] = (int)arg; /* A3 */
116: pcb->pcb_regs[11] = (int)sf; /* SSP */
117: pcb->pcb_ps = PSL_LOWIPL; /* start kthreads at IPL 0 */
118: }
119:
120: /*
121: * cpu_exit is called as the last action during exit.
122: * We release the address space and machine-dependent resources,
123: * block context switches and then call switch_exit() which will
124: * free our stack and user area and switch to another process.
125: * Thus, we never return.
126: */
127: void
128: cpu_exit(p)
129: struct proc *p;
130: {
131:
132: (void)splhigh();
133: switch_exit(p);
134: /* NOTREACHED */
135: }
136:
137: /*
138: * Dump the machine specific segment at the start of a core dump.
139: * This means the CPU and FPU registers. The format used here is
140: * the same one ptrace uses, so gdb can be machine independent.
141: *
142: * XXX - Generate Sun format core dumps for Sun executables?
143: */
144: struct md_core {
145: struct reg intreg;
146: struct fpreg freg;
147: };
148: int
149: cpu_coredump(p, vp, cred, chdr)
150: struct proc *p;
151: struct vnode *vp;
152: struct ucred *cred;
153: struct core *chdr;
154: {
155: struct md_core md_core;
156: struct coreseg cseg;
157: int error;
158:
159: CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
160: chdr->c_hdrsize = ALIGN(sizeof(*chdr));
161: chdr->c_seghdrsize = ALIGN(sizeof(cseg));
162: chdr->c_cpusize = sizeof(md_core);
163:
164: /* Save integer registers. */
165: error = process_read_regs(p, &md_core.intreg);
166: if (error)
167: return error;
168:
169: if (fputype) {
170: /* Save floating point registers. */
171: error = process_read_fpregs(p, &md_core.freg);
172: if (error)
173: return error;
174: } else {
175: /* Make sure these are clear. */
176: bzero((caddr_t)&md_core.freg, sizeof(md_core.freg));
177: }
178:
179: CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
180: cseg.c_addr = 0;
181: cseg.c_size = chdr->c_cpusize;
182:
183: error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cseg, chdr->c_seghdrsize,
184: (off_t)chdr->c_hdrsize, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred,
185: NULL, p);
186: if (error)
187: return error;
188:
189: error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&md_core, sizeof(md_core),
190: (off_t)(chdr->c_hdrsize + chdr->c_seghdrsize), UIO_SYSSPACE,
191: IO_NODELOCKED|IO_UNIT, cred, NULL, p);
192: if (error)
193: return error;
194:
195: chdr->c_nseg++;
196: return 0;
197: }
198:
199: /*
200: * Map an IO request into kernel virtual address space.
201: *
202: * XXX we allocate KVA space by using kmem_alloc_wait which we know
203: * allocates space without backing physical memory. This implementation
204: * is a total crock, the multiple mappings of these physical pages should
205: * be reflected in the higher-level VM structures to avoid problems.
206: */
207: void
208: vmapbuf(bp, len)
209: struct buf *bp;
210: vsize_t len;
211: {
212: struct pmap *upmap, *kpmap;
213: vaddr_t uva; /* User VA (map from) */
214: vaddr_t kva; /* Kernel VA (new to) */
215: vaddr_t pa; /* physical address */
216: vsize_t off;
217:
218: #ifdef DIAGNOSTIC
219: if ((bp->b_flags & B_PHYS) == 0)
220: panic("vmapbuf");
221: #endif
222:
223: uva = trunc_page((vaddr_t)(bp->b_saveaddr = bp->b_data));
224: off = (vaddr_t)bp->b_data - uva;
225: len = round_page(off + len);
226: kva = uvm_km_valloc_wait(phys_map, len);
227: bp->b_data = (caddr_t)(kva + off);
228:
229: upmap = vm_map_pmap(&bp->b_proc->p_vmspace->vm_map);
230: kpmap = vm_map_pmap(phys_map);
231: do {
232: if (pmap_extract(upmap, uva, &pa) == FALSE)
233: panic("vmapbuf: null page frame");
234: pmap_enter(kpmap, kva, pa, VM_PROT_READ | VM_PROT_WRITE,
235: VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
236: uva += PAGE_SIZE;
237: kva += PAGE_SIZE;
238: len -= PAGE_SIZE;
239: } while (len);
240: pmap_update(pmap_kernel());
241: }
242:
243: /*
244: * Free the io map PTEs associated with this IO operation.
245: */
246: void
247: vunmapbuf(bp, len)
248: struct buf *bp;
249: vsize_t len;
250: {
251: vaddr_t kva;
252: vsize_t off;
253:
254: #ifdef DIAGNOSTIC
255: if ((bp->b_flags & B_PHYS) == 0)
256: panic("vunmapbuf");
257: #endif
258:
259: kva = trunc_page((vaddr_t)bp->b_data);
260: off = (vaddr_t)bp->b_data - kva;
261: len = round_page(off + len);
262:
263: pmap_remove(vm_map_pmap(phys_map), kva, kva + len);
264: pmap_update(pmap_kernel());
265: uvm_km_free_wakeup(phys_map, kva, len);
266: bp->b_data = bp->b_saveaddr;
267: bp->b_saveaddr = 0;
268: }
CVSweb