1 /* $NetBSD: vm_machdep.c,v 1.11 1995/04/10 16:49:02 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * @(#)vm_machdep.c 8.2 (Berkeley) 9/23/93 45 */ 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/proc.h> 50 #include <sys/user.h> 51 #include <sys/core.h> 52 #include <sys/malloc.h> 53 #include <sys/buf.h> 54 #include <sys/exec.h> 55 #include <sys/vnode.h> 56 57 #include <vm/vm.h> 58 #include <vm/vm_kern.h> 59 60 #include <machine/cpu.h> 61 #include <machine/frame.h> 62 63 #include <sparc/sparc/cache.h> 64 65 /* 66 * Move pages from one kernel virtual address to another. 67 */ 68 pagemove(from, to, size) 69 register caddr_t from, to; 70 int size; 71 { 72 register vm_offset_t pa; 73 74 if (size & CLOFSET || (int)from & CLOFSET || (int)to & CLOFSET) 75 panic("pagemove 1"); 76 while (size > 0) { 77 pa = pmap_extract(pmap_kernel(), (vm_offset_t)from); 78 if (pa == 0) 79 panic("pagemove 2"); 80 pmap_remove(pmap_kernel(), 81 (vm_offset_t)from, (vm_offset_t)from + PAGE_SIZE); 82 pmap_enter(pmap_kernel(), 83 (vm_offset_t)to, pa, VM_PROT_READ|VM_PROT_WRITE, 1); 84 from += PAGE_SIZE; 85 to += PAGE_SIZE; 86 size -= PAGE_SIZE; 87 } 88 } 89 90 /* 91 * Map an IO request into kernel virtual address space. 92 * 93 * ### pmap_enter distributes this mapping to all contexts ... maybe 94 * we should avoid this extra work 95 * 96 * THIS IS NOT IDEAL -- WE NEED ONLY VIRTUAL SPACE BUT kmem_alloc_wait 97 * DOES WORK DESIGNED TO SUPPLY PHYSICAL SPACE ON DEMAND LATER 98 */ 99 vmapbuf(bp) 100 register struct buf *bp; 101 { 102 register int npf; 103 register caddr_t addr; 104 struct proc *p; 105 int off; 106 vm_offset_t kva; 107 register vm_offset_t pa; 108 109 if ((bp->b_flags & B_PHYS) == 0) 110 panic("vmapbuf"); 111 addr = bp->b_saveaddr = bp->b_un.b_addr; 112 off = (int)addr & PGOFSET; 113 p = bp->b_proc; 114 npf = btoc(round_page(bp->b_bcount + off)); 115 kva = kmem_alloc_wait(phys_map, ctob(npf)); 116 bp->b_un.b_addr = (caddr_t) (kva + off); 117 while (npf--) { 118 pa = pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map), 119 (vm_offset_t)addr); 120 if (pa == 0) 121 panic("vmapbuf: null page frame"); 122 pmap_enter(vm_map_pmap(phys_map), kva, 123 trunc_page(pa) | PMAP_NC, 124 VM_PROT_READ|VM_PROT_WRITE, 1); 125 addr += PAGE_SIZE; 126 kva += PAGE_SIZE; 127 } 128 } 129 130 /* 131 * Free the io map addresses associated with this IO operation. 132 */ 133 vunmapbuf(bp) 134 register struct buf *bp; 135 { 136 register vm_offset_t kva = (vm_offset_t)bp->b_un.b_addr; 137 register int off, npf; 138 139 if ((bp->b_flags & B_PHYS) == 0) 140 panic("vunmapbuf"); 141 off = (int)kva & PGOFSET; 142 kva -= off; 143 npf = btoc(round_page(bp->b_bcount + off)); 144 kmem_free_wakeup(phys_map, kva, ctob(npf)); 145 bp->b_un.b_addr = bp->b_saveaddr; 146 bp->b_saveaddr = NULL; 147 if (vactype != VAC_NONE) 148 cache_flush(bp->b_un.b_addr, bp->b_bcount - bp->b_resid); 149 } 150 151 /* 152 * Allocate physical memory space in the dvma virtual address range. 153 */ 154 caddr_t 155 dvma_malloc(size) 156 size_t size; 157 { 158 vm_size_t vsize; 159 caddr_t va; 160 161 vsize = round_page(size); 162 va = (caddr_t)kmem_alloc(phys_map, vsize); 163 if (va == NULL) 164 panic("dvma_malloc"); 165 kvm_uncache(va, vsize >> PGSHIFT); 166 return (va); 167 } 168 169 /* 170 * The offset of the topmost frame in the kernel stack. 171 */ 172 #define TOPFRAMEOFF (USPACE-sizeof(struct trapframe)-sizeof(struct frame)) 173 174 /* 175 * Finish a fork operation, with process p2 nearly set up. 176 * Copy and update the kernel stack and pcb, making the child 177 * ready to run, and marking it so that it can return differently 178 * than the parent. Returns 1 in the child process, 0 in the parent. 179 * 180 * This function relies on the fact that the pcb is 181 * the first element in struct user. 182 */ 183 cpu_fork(p1, p2) 184 register struct proc *p1, *p2; 185 { 186 register struct pcb *opcb = &p1->p_addr->u_pcb; 187 register struct pcb *npcb = &p2->p_addr->u_pcb; 188 register u_int sp, topframe, off, ssize; 189 190 /* 191 * Save all the registers to p1's stack or, in the case of 192 * user registers and invalid stack pointers, to opcb. 193 * snapshot() also sets the given pcb's pcb_sp and pcb_psr 194 * to the current %sp and %psr, and sets pcb_pc to a stub 195 * which returns 1. We then copy the whole pcb to p2; 196 * when switch() selects p2 to run, it will run at the stub, 197 * rather than at the copying code below, and cpu_fork 198 * will return 1. 199 * 200 * Note that the order `*npcb = *opcb, snapshot(npcb)' is wrong, 201 * as user registers might then wind up only in opcb. 202 * We could call save_user_windows first, 203 * but that would only save 3 stores anyway. 204 * 205 * If process p1 has an FPU state, we must copy it. If it is 206 * the FPU user, we must save the FPU state first. 207 */ 208 snapshot(opcb); 209 bcopy((caddr_t)opcb, (caddr_t)npcb, sizeof(struct pcb)); 210 if (p1->p_md.md_fpstate) { 211 if (p1 == fpproc) 212 savefpstate(p1->p_md.md_fpstate); 213 p2->p_md.md_fpstate = malloc(sizeof(struct fpstate), 214 M_SUBPROC, M_WAITOK); 215 bcopy(p1->p_md.md_fpstate, p2->p_md.md_fpstate, 216 sizeof(struct fpstate)); 217 } else 218 p2->p_md.md_fpstate = NULL; 219 220 /* 221 * Copy the active part of the kernel stack, 222 * then adjust each kernel sp -- the frame pointer 223 * in the top frame is a user sp -- in the child's copy, 224 * including the initial one in the child's pcb. 225 */ 226 sp = npcb->pcb_sp; /* points to old kernel stack */ 227 ssize = (u_int)opcb + USPACE - sp; 228 if (ssize >= USPACE - sizeof(struct pcb)) 229 panic("cpu_fork 1"); 230 off = (u_int)npcb - (u_int)opcb; 231 qcopy((caddr_t)sp, (caddr_t)sp + off, ssize); 232 sp += off; 233 npcb->pcb_sp = sp; 234 topframe = (u_int)npcb + TOPFRAMEOFF; 235 while (sp < topframe) 236 sp = ((struct rwindow *)sp)->rw_in[6] += off; 237 if (sp != topframe) 238 panic("cpu_fork 2"); 239 /* 240 * This might be unnecessary, but it may be possible for the child 241 * to run in ptrace or sendsig before it returns from fork. 242 */ 243 p2->p_md.md_tf = (struct trapframe *)((int)p1->p_md.md_tf + off); 244 return (0); 245 } 246 247 /* 248 * cpu_exit is called as the last action during exit. 249 * We release the address space and machine-dependent resources, 250 * including the memory for the user structure and kernel stack. 251 * Since the latter is also the interrupt stack, we release it 252 * from assembly code after switching to a temporary pcb+stack. 253 */ 254 void 255 cpu_exit(p) 256 struct proc *p; 257 { 258 register struct fpstate *fs; 259 260 if ((fs = p->p_md.md_fpstate) != NULL) { 261 if (p == fpproc) { 262 savefpstate(fs); 263 fpproc = NULL; 264 } 265 free((void *)fs, M_SUBPROC); 266 } 267 vmspace_free(p->p_vmspace); 268 switchexit(kernel_map, p->p_addr, USPACE); 269 /* NOTREACHED */ 270 } 271 272 /* 273 * cpu_coredump is called to write a core dump header. 274 * (should this be defined elsewhere? machdep.c?) 275 */ 276 int 277 cpu_coredump(p, vp, cred, chdr) 278 struct proc *p; 279 struct vnode *vp; 280 struct ucred *cred; 281 struct core *chdr; 282 { 283 int error; 284 register struct user *up = p->p_addr; 285 struct md_coredump md_core; 286 struct coreseg cseg; 287 288 CORE_SETMAGIC(*chdr, COREMAGIC, MID_SPARC, 0); 289 chdr->c_hdrsize = ALIGN(sizeof(*chdr)); 290 chdr->c_seghdrsize = ALIGN(sizeof(cseg)); 291 chdr->c_cpusize = sizeof(md_core); 292 293 md_core.md_tf = *p->p_md.md_tf; 294 if (p->p_md.md_fpstate) { 295 if (p == fpproc) 296 savefpstate(p->p_md.md_fpstate); 297 md_core.md_fpstate = *p->p_md.md_fpstate; 298 } else 299 bzero((caddr_t)&md_core.md_fpstate, sizeof(struct fpstate)); 300 301 CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_SPARC, CORE_CPU); 302 cseg.c_addr = 0; 303 cseg.c_size = chdr->c_cpusize; 304 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cseg, chdr->c_seghdrsize, 305 (off_t)chdr->c_hdrsize, UIO_SYSSPACE, 306 IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, p); 307 if (error) 308 return error; 309 310 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&md_core, sizeof(md_core), 311 (off_t)(chdr->c_hdrsize + chdr->c_seghdrsize), UIO_SYSSPACE, 312 IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, p); 313 if (!error) 314 chdr->c_nseg++; 315 316 return error; 317 } 318