vm_machdep.c revision 117985
1/*- 2 * Copyright (c) 1982, 1986 The Regents of the University of California. 3 * Copyright (c) 1989, 1990 William Jolitz 4 * Copyright (c) 1994 John Dyson 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * the Systems Programming Group of the University of Utah Computer 9 * Science Department, and William Jolitz. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 40 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 41 * $FreeBSD: head/sys/amd64/amd64/vm_machdep.c 117985 2003-07-25 00:21:37Z davidxu $ 42 */ 43 44#include "opt_isa.h" 45#include "opt_kstack_pages.h" 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/malloc.h> 50#include <sys/proc.h> 51#include <sys/kse.h> 52#include <sys/bio.h> 53#include <sys/buf.h> 54#include <sys/vnode.h> 55#include <sys/vmmeter.h> 56#include <sys/kernel.h> 57#include <sys/ktr.h> 58#include <sys/mutex.h> 59#include <sys/sysctl.h> 60#include <sys/unistd.h> 61 62#include <machine/cpu.h> 63#include <machine/md_var.h> 64#include <machine/pcb.h> 65 66#include <vm/vm.h> 67#include <vm/vm_param.h> 68#include <sys/lock.h> 69#include <vm/vm_kern.h> 70#include <vm/vm_page.h> 71#include <vm/vm_map.h> 72#include <vm/vm_extern.h> 73 74#include <sys/user.h> 75 76#include <amd64/isa/isa.h> 77 78static void cpu_reset_real(void); 79 80/* 81 * Finish a fork operation, with process p2 nearly set up. 82 * Copy and update the pcb, set up the stack so that the child 83 * ready to run and return to user mode. 84 */ 85void 86cpu_fork(td1, p2, td2, flags) 87 register struct thread *td1; 88 register struct proc *p2; 89 struct thread *td2; 90 int flags; 91{ 92 register struct proc *p1; 93 struct pcb *pcb2; 94 struct mdproc *mdp2; 95 register_t savecrit; 96 97 p1 = td1->td_proc; 98 if ((flags & RFPROC) == 0) 99 return; 100 101 /* Ensure that p1's pcb is up to date. */ 102 savecrit = intr_disable(); 103 if (PCPU_GET(fpcurthread) == td1) 104 npxsave(&td1->td_pcb->pcb_save); 105 intr_restore(savecrit); 106 107 /* Point the pcb to the top of the stack */ 108 pcb2 = (struct pcb *)(td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 109 td2->td_pcb = pcb2; 110 111 /* Copy p1's pcb */ 112 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 113 114 /* Point mdproc and then copy over td1's contents */ 115 mdp2 = &p2->p_md; 116 bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 117 118 /* 119 * Create a new fresh stack for the new process. 120 * Copy the trap frame for the return to user mode as if from a 121 * syscall. This copies most of the user mode register values. 122 */ 123 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 124 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 125 126 td2->td_frame->tf_rax = 0; /* Child returns zero */ 127 td2->td_frame->tf_rflags &= ~PSL_C; /* success */ 128 td2->td_frame->tf_rdx = 1; 129 130 /* 131 * Set registers for trampoline to user mode. Leave space for the 132 * return address on stack. These are the kernel mode register values. 133 */ 134 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4); 135 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */ 136 pcb2->pcb_rbp = 0; 137 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *); 138 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */ 139 pcb2->pcb_rip = (register_t)fork_trampoline; 140 pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */ 141 /*- 142 * pcb2->pcb_savefpu: cloned above. 143 * pcb2->pcb_flags: cloned above. 144 * pcb2->pcb_onfault: cloned above (always NULL here?). 145 * pcb2->pcb_[fg]sbase: cloned above 146 */ 147 148 /* 149 * Now, cpu_switch() can schedule the new process. 150 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 151 * containing the return address when exiting cpu_switch. 152 * This will normally be to fork_trampoline(), which will have 153 * %ebx loaded with the new proc's pointer. fork_trampoline() 154 * will set up a stack to call fork_return(p, frame); to complete 155 * the return to user-mode. 156 */ 157} 158 159/* 160 * Intercept the return address from a freshly forked process that has NOT 161 * been scheduled yet. 162 * 163 * This is needed to make kernel threads stay in kernel mode. 164 */ 165void 166cpu_set_fork_handler(td, func, arg) 167 struct thread *td; 168 void (*func)(void *); 169 void *arg; 170{ 171 /* 172 * Note that the trap frame follows the args, so the function 173 * is really called like this: func(arg, frame); 174 */ 175 td->td_pcb->pcb_r12 = (long) func; /* function */ 176 td->td_pcb->pcb_rbx = (long) arg; /* first arg */ 177} 178 179void 180cpu_exit(struct thread *td) 181{ 182 struct mdproc *mdp; 183 184 mdp = &td->td_proc->p_md; 185} 186 187void 188cpu_thread_exit(struct thread *td) 189{ 190 191 npxexit(td); 192} 193 194void 195cpu_thread_clean(struct thread *td) 196{ 197} 198 199void 200cpu_sched_exit(td) 201 register struct thread *td; 202{ 203} 204 205void 206cpu_thread_setup(struct thread *td) 207{ 208 209 td->td_pcb = 210 (struct pcb *)(td->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 211 td->td_frame = (struct trapframe *)td->td_pcb - 1; 212} 213 214/* 215 * Initialize machine state (pcb and trap frame) for a new thread about to 216 * upcall. Pu t enough state in the new thread's PCB to get it to go back 217 * userret(), where we can intercept it again to set the return (upcall) 218 * Address and stack, along with those from upcals that are from other sources 219 * such as those generated in thread_userret() itself. 220 */ 221void 222cpu_set_upcall(struct thread *td, struct thread *td0) 223{ 224 struct pcb *pcb2; 225 226 /* Point the pcb to the top of the stack. */ 227 pcb2 = td->td_pcb; 228 229 /* 230 * Copy the upcall pcb. This loads kernel regs. 231 * Those not loaded individually below get their default 232 * values here. 233 * 234 * XXXKSE It might be a good idea to simply skip this as 235 * the values of the other registers may be unimportant. 236 * This would remove any requirement for knowing the KSE 237 * at this time (see the matching comment below for 238 * more analysis) (need a good safe default). 239 */ 240 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 241 242 /* 243 * Create a new fresh stack for the new thread. 244 * Don't forget to set this stack value into whatever supplies 245 * the address for the fault handlers. 246 * The contexts are filled in at the time we actually DO the 247 * upcall as only then do we know which KSE we got. 248 */ 249 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 250 251 /* 252 * Set registers for trampoline to user mode. Leave space for the 253 * return address on stack. These are the kernel mode register values. 254 */ 255 pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4); 256 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */ 257 pcb2->pcb_rbp = 0; 258 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */ 259 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */ 260 pcb2->pcb_rip = (register_t)fork_trampoline; 261 pcb2->pcb_rflags = td->td_frame->tf_rflags & ~PSL_I; /* ints disabled */ 262 /* 263 * If we didn't copy the pcb, we'd need to do the following registers: 264 * pcb2->pcb_savefpu: cloned above. 265 * pcb2->pcb_rflags: cloned above. 266 * pcb2->pcb_onfault: cloned above (always NULL here?). 267 * pcb2->pcb_[fg]sbase: cloned above 268 */ 269} 270 271/* 272 * Set that machine state for performing an upcall that has to 273 * be done in thread_userret() so that those upcalls generated 274 * in thread_userret() itself can be done as well. 275 */ 276void 277cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku) 278{ 279 280 /* 281 * Do any extra cleaning that needs to be done. 282 * The thread may have optional components 283 * that are not present in a fresh thread. 284 * This may be a recycled thread so make it look 285 * as though it's newly allocated. 286 */ 287 cpu_thread_clean(td); 288 289 /* 290 * Set the trap frame to point at the beginning of the uts 291 * function. 292 */ 293 td->td_frame->tf_rsp = 294 ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f; 295 td->td_frame->tf_rsp -= 8; 296 td->td_frame->tf_rip = (register_t)ku->ku_func; 297 298 /* 299 * Pass the address of the mailbox for this kse to the uts 300 * function as a parameter on the stack. 301 */ 302 td->td_frame->tf_rdi = (register_t)ku->ku_mailbox; 303} 304 305 306/* 307 * Force reset the processor by invalidating the entire address space! 308 */ 309 310void 311cpu_reset() 312{ 313 cpu_reset_real(); 314} 315 316static void 317cpu_reset_real() 318{ 319 320 /* 321 * Attempt to do a CPU reset via the keyboard controller, 322 * do not turn of the GateA20, as any machine that fails 323 * to do the reset here would then end up in no man's land. 324 */ 325 326 outb(IO_KBD + 4, 0xFE); 327 DELAY(500000); /* wait 0.5 sec to see if that did it */ 328 printf("Keyboard reset did not work, attempting CPU shutdown\n"); 329 DELAY(1000000); /* wait 1 sec for printf to complete */ 330 /* force a shutdown by unmapping entire address space ! */ 331 bzero((caddr_t)PML4map, PAGE_SIZE); 332 333 /* "good night, sweet prince .... <THUNK!>" */ 334 invltlb(); 335 /* NOTREACHED */ 336 while(1); 337} 338 339/* 340 * Software interrupt handler for queued VM system processing. 341 */ 342void 343swi_vm(void *dummy) 344{ 345 if (busdma_swi_pending != 0) 346 busdma_swi(); 347} 348 349/* 350 * Tell whether this address is in some physical memory region. 351 * Currently used by the kernel coredump code in order to avoid 352 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 353 * or other unpredictable behaviour. 354 */ 355 356int 357is_physical_memory(addr) 358 vm_offset_t addr; 359{ 360 361#ifdef DEV_ISA 362 /* The ISA ``memory hole''. */ 363 if (addr >= 0xa0000 && addr < 0x100000) 364 return 0; 365#endif 366 367 /* 368 * stuff other tests for known memory-mapped devices (PCI?) 369 * here 370 */ 371 372 return 1; 373} 374