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 * Copyright (c) 2008 The DragonFly Project. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department, and William Jolitz. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 41 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 42 * $FreeBSD: src/sys/i386/i386/vm_machdep.c,v 1.132.2.9 2003/01/25 19:02:23 dillon Exp $ 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/proc.h> 49 #include <sys/buf.h> 50 #include <sys/interrupt.h> 51 #include <sys/vnode.h> 52 #include <sys/vmmeter.h> 53 #include <sys/kernel.h> 54 #include <sys/sysctl.h> 55 #include <sys/unistd.h> 56 #include <sys/lwp.h> 57 58 #include <machine/clock.h> 59 #include <machine/cpu.h> 60 #include <machine/md_var.h> 61 #include <machine/smp.h> 62 #include <machine/pcb.h> 63 #include <machine/pcb_ext.h> 64 #include <machine/segments.h> 65 #include <machine/globaldata.h> /* npxthread */ 66 67 #include <vm/vm.h> 68 #include <vm/vm_param.h> 69 #include <sys/lock.h> 70 #include <vm/vm_kern.h> 71 #include <vm/vm_page.h> 72 #include <vm/vm_map.h> 73 #include <vm/vm_extern.h> 74 75 #include <sys/thread2.h> 76 77 #include <bus/isa/isa.h> 78 79 char machine[] = MACHINE; 80 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, 81 machine, 0, "Machine class"); 82 83 u_int cpu_vendor_id = 0; /* XXX */ 84 85 /* 86 * Finish a fork operation, with lwp lp2 nearly set up. 87 * Copy and update the pcb, set up the stack so that the child 88 * ready to run and return to user mode. 89 */ 90 void 91 cpu_fork(struct lwp *lp1, struct lwp *lp2, int flags) 92 { 93 struct pcb *pcb2; 94 95 if ((flags & RFPROC) == 0) { 96 if ((flags & RFMEM) == 0) { 97 /* unshare user LDT */ 98 struct pcb *pcb1 = lp1->lwp_thread->td_pcb; 99 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt; 100 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) { 101 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len); 102 user_ldt_free(pcb1); 103 pcb1->pcb_ldt = pcb_ldt; 104 set_user_ldt(pcb1); 105 } 106 } 107 return; 108 } 109 110 /* Ensure that lp1's pcb is up to date. */ 111 if (mdcpu->gd_npxthread == lp1->lwp_thread) 112 npxsave(lp1->lwp_thread->td_savefpu); 113 114 /* 115 * Copy lp1's PCB. This really only applies to the 116 * debug registers and FP state, but its faster to just copy the 117 * whole thing. Because we only save the PCB at switchout time, 118 * the register state may not be current. 119 */ 120 pcb2 = lp2->lwp_thread->td_pcb; 121 *pcb2 = *lp1->lwp_thread->td_pcb; 122 123 /* 124 * Create a new fresh stack for the new process. 125 * Copy the trap frame for the return to user mode as if from a 126 * syscall. This copies the user mode register values. 127 * 128 * pcb_rsp must allocate an additional call-return pointer below 129 * the trap frame which will be restored by cpu_heavy_restore from 130 * PCB_RIP, and the thread's td_sp pointer must allocate an 131 * additonal two quadwords below the pcb_rsp call-return pointer to 132 * hold the LWKT restore function pointer and rflags. 133 * 134 * The LWKT restore function pointer must be set to cpu_heavy_restore, 135 * which is our standard heavy-weight process switch-in function. 136 * YYY eventually we should shortcut fork_return and fork_trampoline 137 * to use the LWKT restore function directly so we can get rid of 138 * all the extra crap we are setting up. 139 */ 140 lp2->lwp_md.md_regs = (struct trapframe *)pcb2 - 1; 141 bcopy(lp1->lwp_md.md_regs, lp2->lwp_md.md_regs, sizeof(*lp2->lwp_md.md_regs)); 142 143 /* 144 * Set registers for trampoline to user mode. Leave space for the 145 * return address on stack. These are the kernel mode register values. 146 */ 147 pcb2->pcb_unused01 = 0; 148 pcb2->pcb_rbx = (unsigned long)fork_return; /* fork_trampoline argument */ 149 pcb2->pcb_rbp = 0; 150 pcb2->pcb_rsp = (unsigned long)lp2->lwp_md.md_regs - sizeof(void *); 151 pcb2->pcb_r12 = (unsigned long)lp2; /* fork_trampoline argument */ 152 pcb2->pcb_r13 = 0; 153 pcb2->pcb_r14 = 0; 154 pcb2->pcb_r15 = 0; 155 pcb2->pcb_rip = (unsigned long)fork_trampoline; 156 lp2->lwp_thread->td_sp = (char *)(pcb2->pcb_rsp - sizeof(void *)); 157 *(u_int64_t *)lp2->lwp_thread->td_sp = PSL_USER; 158 lp2->lwp_thread->td_sp -= sizeof(void *); 159 *(void **)lp2->lwp_thread->td_sp = (void *)cpu_heavy_restore; 160 161 /* 162 * pcb2->pcb_ldt: duplicated below, if necessary. 163 * pcb2->pcb_savefpu: cloned above. 164 * pcb2->pcb_flags: cloned above (always 0 here?). 165 * pcb2->pcb_onfault: cloned above (always NULL here?). 166 */ 167 168 /* 169 * XXX don't copy the i/o pages. this should probably be fixed. 170 */ 171 pcb2->pcb_ext = NULL; 172 173 /* Copy the LDT, if necessary. */ 174 if (pcb2->pcb_ldt != NULL) { 175 if (flags & RFMEM) { 176 atomic_add_int(&pcb2->pcb_ldt->ldt_refcnt, 1); 177 } else { 178 pcb2->pcb_ldt = user_ldt_alloc(pcb2, 179 pcb2->pcb_ldt->ldt_len); 180 } 181 } 182 bcopy(&lp1->lwp_thread->td_tls, &lp2->lwp_thread->td_tls, 183 sizeof(lp2->lwp_thread->td_tls)); 184 /* 185 * Now, cpu_switch() can schedule the new lwp. 186 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 187 * containing the return address when exiting cpu_switch. 188 * This will normally be to fork_trampoline(), which will have 189 * %rbx loaded with the new lwp's pointer. fork_trampoline() 190 * will set up a stack to call fork_return(lp, frame); to complete 191 * the return to user-mode. 192 */ 193 } 194 195 /* 196 * Prepare new lwp to return to the address specified in params. 197 */ 198 int 199 cpu_prepare_lwp(struct lwp *lp, struct lwp_params *params) 200 { 201 struct trapframe *regs = lp->lwp_md.md_regs; 202 void *bad_return = NULL; 203 int error; 204 205 regs->tf_rip = (long)params->lwp_func; 206 regs->tf_rsp = (long)params->lwp_stack; 207 /* Set up argument for function call */ 208 regs->tf_rdi = (long)params->lwp_arg; /* JG Can this be in userspace addresses? */ 209 /* 210 * Set up fake return address. As the lwp function may never return, 211 * we simply copy out a NULL pointer and force the lwp to receive 212 * a SIGSEGV if it returns anyways. 213 */ 214 regs->tf_rsp -= sizeof(void *); 215 error = copyout(&bad_return, (void *)regs->tf_rsp, sizeof(bad_return)); 216 if (error) 217 return (error); 218 219 cpu_set_fork_handler(lp, 220 (void (*)(void *, struct trapframe *))generic_lwp_return, lp); 221 return (0); 222 } 223 224 /* 225 * Intercept the return address from a freshly forked process that has NOT 226 * been scheduled yet. 227 * 228 * This is needed to make kernel threads stay in kernel mode. 229 */ 230 void 231 cpu_set_fork_handler(struct lwp *lp, void (*func)(void *, struct trapframe *), 232 void *arg) 233 { 234 /* 235 * Note that the trap frame follows the args, so the function 236 * is really called like this: func(arg, frame); 237 */ 238 lp->lwp_thread->td_pcb->pcb_rbx = (long)func; /* function */ 239 lp->lwp_thread->td_pcb->pcb_r12 = (long)arg; /* first arg */ 240 } 241 242 void 243 cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg) 244 { 245 td->td_pcb->pcb_rbx = (long)func; 246 td->td_pcb->pcb_r12 = (long)arg; 247 td->td_switch = cpu_lwkt_switch; 248 td->td_sp -= sizeof(void *); 249 *(void **)td->td_sp = rfunc; /* exit function on return */ 250 td->td_sp -= sizeof(void *); 251 *(void **)td->td_sp = cpu_kthread_restore; 252 } 253 254 void 255 cpu_lwp_exit(void) 256 { 257 struct thread *td = curthread; 258 struct pcb *pcb; 259 260 pcb = td->td_pcb; 261 262 /* Some x86 functionality was dropped */ 263 KKASSERT(pcb->pcb_ext == NULL); 264 265 /* 266 * disable all hardware breakpoints 267 */ 268 if (pcb->pcb_flags & PCB_DBREGS) { 269 reset_dbregs(); 270 pcb->pcb_flags &= ~PCB_DBREGS; 271 } 272 td->td_gd->gd_cnt.v_swtch++; 273 274 crit_enter_quick(td); 275 if (td->td_flags & TDF_TSLEEPQ) 276 tsleep_remove(td); 277 lwkt_deschedule_self(td); 278 lwkt_remove_tdallq(td); 279 cpu_thread_exit(); 280 } 281 282 /* 283 * Terminate the current thread. The caller must have already acquired 284 * the thread's rwlock and placed it on a reap list or otherwise notified 285 * a reaper of its existance. We set a special assembly switch function which 286 * releases td_rwlock after it has cleaned up the MMU state and switched 287 * out the stack. 288 * 289 * Must be caller from a critical section and with the thread descheduled. 290 */ 291 void 292 cpu_thread_exit(void) 293 { 294 npxexit(); 295 curthread->td_switch = cpu_exit_switch; 296 curthread->td_flags |= TDF_EXITING; 297 lwkt_switch(); 298 panic("cpu_thread_exit: lwkt_switch() unexpectedly returned"); 299 } 300 301 /* 302 * Used by /dev/kmem to determine if we can safely read or write 303 * the requested KVA range. Some portions of kernel memory are 304 * not governed by our virtual page table. 305 */ 306 extern int64_t _end; 307 extern void _start(void); 308 309 int 310 kvm_access_check(vm_offset_t saddr, vm_offset_t eaddr, int prot) 311 { 312 vm_offset_t addr; 313 314 if (saddr >= trunc_page((vm_offset_t)&_start) && 315 eaddr <= round_page((vm_offset_t)&_end)) { 316 return 0; 317 } 318 if (saddr < KvaStart) 319 return EFAULT; 320 if (eaddr >= KvaEnd) 321 return EFAULT; 322 for (addr = saddr; addr < eaddr; addr += PAGE_SIZE) { 323 if (pmap_kextract(addr) == 0) 324 return EFAULT; 325 } 326 if (!kernacc((caddr_t)saddr, eaddr - saddr, prot)) 327 return EFAULT; 328 return 0; 329 } 330