1 /* $NetBSD: vm_machdep.c,v 1.120 2026/11/10 12:34:23 kalvisd Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Ludd, University of Lule}, Sweden. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: vm_machdep.c,v 1.120 2026/11/10 12:34:23 kalvisd Exp $"); 30 31 #include "opt_execfmt.h" 32 #include "opt_compat_ultrix.h" 33 #include "opt_multiprocessor.h" 34 #include "opt_cputype.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/buf.h> 39 #include <sys/core.h> 40 #include <sys/cpu.h> 41 #include <sys/exec.h> 42 #include <sys/exec_aout.h> 43 #include <sys/proc.h> 44 #include <sys/syscallargs.h> 45 46 #include <uvm/uvm_extern.h> 47 48 #include <machine/vmparam.h> 49 #include <machine/macros.h> 50 #include <machine/frame.h> 51 #include <machine/sid.h> 52 53 /* 54 * Finish a fork operation, with process p2 nearly set up. 55 * Copy and update the pcb and trap frame, making the child ready to run. 56 * 57 * Rig the child's kernel stack so that it will start out in 58 * cpu_lwp_bootstrap() and call child_return() with p2 as an 59 * argument. This causes the newly-created child process to go 60 * directly to user level with an apparent return value of 0 from 61 * fork(), while the parent process returns normally. 62 * 63 * p1 is the process being forked; if p1 == &proc0, we are creating 64 * a kernel thread, and the return path and argument are specified with 65 * `func' and `arg'. 66 * 67 * If an alternate user-level stack is requested (with non-zero values 68 * in both the stack and stacksize args), set up the user stack pointer 69 * accordingly. 70 * 71 * cpu_lwp_fork() copies parent process trapframe and creates a fake CALLS 72 * frame on top of it, so that it can safely call child_return(). 73 * We also take away mapping for the fourth page after pcb, so that 74 * we get something like a "red zone" for the kernel stack. 75 */ 76 void cpu_lwp_bootstrap(void); 77 void 78 cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack, size_t stacksize, 79 void (*func)(void *), void *arg) 80 { 81 struct callsframe *cf; 82 extern int sret; /* Return address in trap routine */ 83 84 struct pcb * const pcb2 = lwp_getpcb(l2); 85 86 #ifdef DIAGNOSTIC 87 /* 88 * if p1 != curlwp && p1 == &proc0, we're creating a kernel thread. 89 */ 90 if (l1 != curlwp && l1 != &lwp0) 91 panic("cpu_lwp_fork: curlwp"); 92 #endif 93 94 /* 95 * Clear new pcb 96 */ 97 memset(pcb2, 0, sizeof(*pcb2)); 98 99 /* 100 * Set initial values for the base and length registers. 101 * 102 * Set the initial AST level to AST_PCB (no ASTs pending) 103 */ 104 pcb2->P0BR = (struct pte *)KERNBASE; 105 pcb2->P0LR = (0 / sizeof(struct pte)) | AST_PCB; 106 pcb2->P1BR = (struct pte *)KERNBASE; 107 pcb2->P1LR = (1 << 21) - (0 / sizeof(struct pte)); 108 109 /* 110 * Copy the trap frame. 111 */ 112 const vaddr_t uv = uvm_lwp_getuarea(l2); 113 struct trapframe * const tf = (struct trapframe *)(uv + USPACE) - 1; 114 l2->l_md.md_utf = tf; 115 *tf = *l1->l_md.md_utf; 116 117 /* Address space for the new LWP will be activated when the 118 * LWP is next scheduled */ 119 120 /* Mark guard page invalid in kernel stack */ 121 kvtopte((uintptr_t)uv + REDZONEADDR)->pg_v = 0; 122 123 /* 124 * Set up the calls frame above (below) the trapframe and populate 125 * it with something good. This is so that we can simulate that we 126 * called cpu_lwp_bootstrap with a CALLS insn and it can return to 127 * sret. 128 */ 129 cf = (struct callsframe *)tf - 1; 130 cf->ca_cond = 0; 131 cf->ca_maskpsw = 0x20000000; /* CALLS stack frame, no registers */ 132 cf->ca_ap = 0; 133 cf->ca_fp = 0; 134 cf->ca_pc = (uintptr_t)&sret; /* return PC; userspace trampoline */ 135 cf->ca_argno = 1; 136 cf->ca_arg1 = 0; /* unused */ 137 138 /* 139 * Set up internal defs in PCB. This matches the "fake" CALLS frame 140 * that were constructed earlier. 141 */ 142 pcb2->pcb_onfault = NULL; 143 pcb2->AP = (uintptr_t)&cf->ca_argno; 144 pcb2->KSP = (uintptr_t)cf; 145 pcb2->FP = (uintptr_t)cf; 146 pcb2->PC = (uintptr_t)cpu_lwp_bootstrap + 2; 147 pcb2->PSL = PSL_HIGHIPL; 148 pcb2->ESP = (uintptr_t)&pcb2->pcb_onfault; 149 pcb2->SSP = (uintptr_t)l2; 150 151 /* pcb->R[0] (oldlwp) set by Swtchto */ 152 pcb2->R[1] = (uintptr_t)l2; 153 pcb2->R[2] = (uintptr_t)func; 154 pcb2->R[3] = (uintptr_t)arg; 155 pcb2->pcb_paddr = kvtophys(pcb2); 156 157 /* 158 * If specified, give the child a different stack. 159 */ 160 if (stack != NULL) 161 tf->tf_sp = (uintptr_t)stack + stacksize; 162 163 /* 164 * Set the last return information after fork(). 165 * This is only interesting if the child will return to userspace, 166 * but doesn't hurt otherwise. 167 */ 168 tf->tf_r0 = l1->l_proc->p_pid; /* parent pid. (shouldn't be needed) */ 169 tf->tf_r1 = 1; 170 tf->tf_psl = PSL_U|PSL_PREVU; 171 } 172 173 vaddr_t 174 cpu_lwp_pc(struct lwp *l) 175 { 176 return l->l_md.md_utf->tf_pc; 177 } 178 179 void 180 cpu_lwp_free(struct lwp *l, int proc) 181 { 182 183 (void)l; 184 (void)proc; 185 } 186 187 void 188 cpu_lwp_free2(struct lwp *l) 189 { 190 191 (void)l; 192 } 193 194 #ifdef EXEC_AOUT 195 int 196 cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp) 197 { 198 return ENOEXEC; 199 } 200 #endif 201 202 int 203 sys_sysarch(struct lwp *l, const struct sys_sysarch_args *uap, register_t *retval) 204 { 205 return (ENOSYS); 206 } 207 208 /* 209 * Map in a bunch of pages read/writable for the kernel. 210 */ 211 void 212 ioaccess(vaddr_t vaddr, paddr_t paddr, size_t npgs) 213 { 214 uint32_t *pte = (uint32_t *)kvtopte(vaddr); 215 int i; 216 217 for (i = 0; i < npgs; i++) 218 pte[i] = PG_V | PG_KW | (PG_PFNUM(paddr) + i); 219 } 220 221 /* 222 * Opposite to the above: just forget their mapping. 223 */ 224 void 225 iounaccess(vaddr_t vaddr, size_t npgs) 226 { 227 uint32_t *pte = (uint32_t *)kvtopte(vaddr); 228 int i; 229 230 for (i = 0; i < npgs; i++) 231 pte[i] = 0; 232 mtpr(0, PR_TBIA); 233 } 234 235 /* 236 * Map a user I/O request into kernel virtual address space. 237 * Note: the pages are already locked by uvm_vslock(), so we 238 * do not need to pass an access_type to pmap_enter(). 239 */ 240 int 241 vmapbuf(struct buf *bp, vsize_t len) 242 { 243 #if VAX46 || VAX48 || VAX49 || VAX53 || VAXANY 244 vaddr_t faddr, taddr, off; 245 paddr_t pa; 246 struct proc *p; 247 248 if (vax_boardtype != VAX_BTYP_46 249 && vax_boardtype != VAX_BTYP_48 250 && vax_boardtype != VAX_BTYP_49 251 && vax_boardtype != VAX_BTYP_53) 252 return 0; 253 if ((bp->b_flags & B_PHYS) == 0) 254 panic("vmapbuf"); 255 p = bp->b_proc; 256 bp->b_saveaddr = bp->b_data; 257 faddr = trunc_page((vaddr_t)bp->b_saveaddr); 258 off = (vaddr_t)bp->b_data - faddr; 259 len = round_page(off + len); 260 taddr = uvm_km_alloc(phys_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA); 261 bp->b_data = (void *)(taddr + off); 262 len = atop(len); 263 while (len--) { 264 if (pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map), faddr, 265 &pa) == false) 266 panic("vmapbuf: null page frame"); 267 pmap_enter(vm_map_pmap(phys_map), taddr, trunc_page(pa), 268 VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED); 269 faddr += PAGE_SIZE; 270 taddr += PAGE_SIZE; 271 } 272 pmap_update(vm_map_pmap(phys_map)); 273 #endif 274 275 return 0; 276 } 277 278 /* 279 * Unmap a previously-mapped user I/O request. 280 */ 281 void 282 vunmapbuf(struct buf *bp, vsize_t len) 283 { 284 #if VAX46 || VAX48 || VAX49 || VAX53 || VAXANY 285 vaddr_t addr, off; 286 287 if (vax_boardtype != VAX_BTYP_46 288 && vax_boardtype != VAX_BTYP_48 289 && vax_boardtype != VAX_BTYP_49 290 && vax_boardtype != VAX_BTYP_53) 291 return; 292 if ((bp->b_flags & B_PHYS) == 0) 293 panic("vunmapbuf"); 294 addr = trunc_page((vaddr_t)bp->b_data); 295 off = (vaddr_t)bp->b_data - addr; 296 len = round_page(off + len); 297 pmap_remove(vm_map_pmap(phys_map), addr, addr + len); 298 pmap_update(vm_map_pmap(phys_map)); 299 uvm_km_free(phys_map, addr, len, UVM_KMF_VAONLY); 300 bp->b_data = bp->b_saveaddr; 301 bp->b_saveaddr = NULL; 302 #endif 303 } 304