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.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
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 $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.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/dsched.h>
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 */
68 #include <vm/vm_param.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>
75 #include <sys/thread2.h>
76 #include <sys/mplock2.h>
78 #include <bus/isa/isa.h>
80 static void cpu_reset_real (void);
82 * Finish a fork operation, with lwp lp2 nearly set up.
83 * Copy and update the pcb, set up the stack so that the child
84 * ready to run and return to user mode.
87 cpu_fork(struct lwp *lp1, struct lwp *lp2, int flags)
91 if ((flags & RFPROC) == 0) {
92 if ((flags & RFMEM) == 0) {
93 /* unshare user LDT */
94 struct pcb *pcb1 = lp1->lwp_thread->td_pcb;
95 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt;
96 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) {
97 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len);
99 pcb1->pcb_ldt = pcb_ldt;
106 /* Ensure that lp1's pcb is up to date. */
107 if (mdcpu->gd_npxthread == lp1->lwp_thread)
108 npxsave(lp1->lwp_thread->td_savefpu);
111 * Copy lp1's PCB. This really only applies to the
112 * debug registers and FP state, but its faster to just copy the
113 * whole thing. Because we only save the PCB at switchout time,
114 * the register state may not be current.
116 pcb2 = lp2->lwp_thread->td_pcb;
117 *pcb2 = *lp1->lwp_thread->td_pcb;
120 * Create a new fresh stack for the new process.
121 * Copy the trap frame for the return to user mode as if from a
122 * syscall. This copies the user mode register values.
124 * pcb_rsp must allocate an additional call-return pointer below
125 * the trap frame which will be restored by cpu_heavy_restore from
126 * PCB_RIP, and the thread's td_sp pointer must allocate an
127 * additonal two quadwords below the pcb_rsp call-return pointer to
128 * hold the LWKT restore function pointer and rflags.
130 * The LWKT restore function pointer must be set to cpu_heavy_restore,
131 * which is our standard heavy-weight process switch-in function.
132 * YYY eventually we should shortcut fork_return and fork_trampoline
133 * to use the LWKT restore function directly so we can get rid of
134 * all the extra crap we are setting up.
136 lp2->lwp_md.md_regs = (struct trapframe *)pcb2 - 1;
137 bcopy(lp1->lwp_md.md_regs, lp2->lwp_md.md_regs, sizeof(*lp2->lwp_md.md_regs));
140 * Set registers for trampoline to user mode. Leave space for the
141 * return address on stack. These are the kernel mode register values.
143 pcb2->pcb_cr3 = vtophys(vmspace_pmap(lp2->lwp_proc->p_vmspace)->pm_pml4);
144 pcb2->pcb_cr3 |= PG_RW | PG_U | PG_V;
145 pcb2->pcb_rbx = (unsigned long)fork_return; /* fork_trampoline argument */
147 pcb2->pcb_rsp = (unsigned long)lp2->lwp_md.md_regs - sizeof(void *);
148 pcb2->pcb_r12 = (unsigned long)lp2; /* fork_trampoline argument */
152 pcb2->pcb_rip = (unsigned long)fork_trampoline;
153 lp2->lwp_thread->td_sp = (char *)(pcb2->pcb_rsp - sizeof(void *));
154 *(u_int64_t *)lp2->lwp_thread->td_sp = PSL_USER;
155 lp2->lwp_thread->td_sp -= sizeof(void *);
156 *(void **)lp2->lwp_thread->td_sp = (void *)cpu_heavy_restore;
159 * pcb2->pcb_ldt: duplicated below, if necessary.
160 * pcb2->pcb_savefpu: cloned above.
161 * pcb2->pcb_flags: cloned above (always 0 here?).
162 * pcb2->pcb_onfault: cloned above (always NULL here?).
166 * XXX don't copy the i/o pages. this should probably be fixed.
170 /* Copy the LDT, if necessary. */
171 if (pcb2->pcb_ldt != 0) {
173 pcb2->pcb_ldt->ldt_refcnt++;
175 pcb2->pcb_ldt = user_ldt_alloc(pcb2,
176 pcb2->pcb_ldt->ldt_len);
179 bcopy(&lp1->lwp_thread->td_tls, &lp2->lwp_thread->td_tls,
180 sizeof(lp2->lwp_thread->td_tls));
182 * Now, cpu_switch() can schedule the new lwp.
183 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
184 * containing the return address when exiting cpu_switch.
185 * This will normally be to fork_trampoline(), which will have
186 * %rbx loaded with the new lwp's pointer. fork_trampoline()
187 * will set up a stack to call fork_return(lp, frame); to complete
188 * the return to user-mode.
193 * Prepare new lwp to return to the address specified in params.
196 cpu_prepare_lwp(struct lwp *lp, struct lwp_params *params)
198 struct trapframe *regs = lp->lwp_md.md_regs;
199 void *bad_return = NULL;
202 regs->tf_rip = (long)params->func;
203 regs->tf_rsp = (long)params->stack;
204 /* Set up argument for function call */
205 regs->tf_rdi = (long)params->arg; /* JG Can this be in userspace addresses? */
207 * Set up fake return address. As the lwp function may never return,
208 * we simply copy out a NULL pointer and force the lwp to receive
209 * a SIGSEGV if it returns anyways.
211 regs->tf_rsp -= sizeof(void *);
212 error = copyout(&bad_return, (void *)regs->tf_rsp, sizeof(bad_return));
216 cpu_set_fork_handler(lp,
217 (void (*)(void *, struct trapframe *))generic_lwp_return, lp);
222 * Intercept the return address from a freshly forked process that has NOT
223 * been scheduled yet.
225 * This is needed to make kernel threads stay in kernel mode.
228 cpu_set_fork_handler(struct lwp *lp, void (*func)(void *, struct trapframe *),
232 * Note that the trap frame follows the args, so the function
233 * is really called like this: func(arg, frame);
235 lp->lwp_thread->td_pcb->pcb_rbx = (long)func; /* function */
236 lp->lwp_thread->td_pcb->pcb_r12 = (long)arg; /* first arg */
240 cpu_set_thread_handler(thread_t td, void (*rfunc)(void), void *func, void *arg)
242 td->td_pcb->pcb_rbx = (long)func;
243 td->td_pcb->pcb_r12 = (long)arg;
244 td->td_switch = cpu_lwkt_switch;
245 td->td_sp -= sizeof(void *);
246 *(void **)td->td_sp = rfunc; /* exit function on return */
247 td->td_sp -= sizeof(void *);
248 *(void **)td->td_sp = cpu_kthread_restore;
254 struct thread *td = curthread;
258 KKASSERT(pcb->pcb_ext == NULL); /* Some i386 functionality was dropped */
259 if (pcb->pcb_flags & PCB_DBREGS) {
261 * disable all hardware breakpoints
264 pcb->pcb_flags &= ~PCB_DBREGS;
266 td->td_gd->gd_cnt.v_swtch++;
268 dsched_exit_thread(td);
269 crit_enter_quick(td);
270 if (td->td_flags & TDF_TSLEEPQ)
272 lwkt_deschedule_self(td);
273 lwkt_remove_tdallq(td);
278 * Terminate the current thread. The caller must have already acquired
279 * the thread's rwlock and placed it on a reap list or otherwise notified
280 * a reaper of its existance. We set a special assembly switch function which
281 * releases td_rwlock after it has cleaned up the MMU state and switched
284 * Must be caller from a critical section and with the thread descheduled.
287 cpu_thread_exit(void)
289 curthread->td_switch = cpu_exit_switch;
290 curthread->td_flags |= TDF_EXITING;
292 panic("cpu_thread_exit: lwkt_switch() unexpectedly returned");
296 * Process Reaper. Called after the caller has acquired the thread's
297 * rwlock and removed it from the reap list.
300 cpu_proc_wait(struct proc *p)
302 /* drop per-process resources */
303 pmap_dispose_proc(p);
316 * Attempt to do a CPU reset via the keyboard controller,
317 * do not turn of the GateA20, as any machine that fails
318 * to do the reset here would then end up in no man's land.
321 #if !defined(BROKEN_KEYBOARD_RESET)
322 outb(IO_KBD + 4, 0xFE);
323 DELAY(500000); /* wait 0.5 sec to see if that did it */
324 kprintf("Keyboard reset did not work, attempting CPU shutdown\n");
325 DELAY(1000000); /* wait 1 sec for kprintf to complete */
328 /* force a shutdown by unmapping entire address space ! */
329 bzero((caddr_t) PTD, PAGE_SIZE);
332 /* "good night, sweet prince .... <THUNK!>" */
339 * Convert kernel VA to physical address
346 pa = pmap_kextract((vm_offset_t)addr);
348 panic("kvtop: zero page frame");
353 * Tell whether this address is in some physical memory region.
354 * Currently used by the kernel coredump code in order to avoid
355 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
356 * or other unpredictable behaviour.
360 is_physical_memory(vm_offset_t addr)
363 /* The ISA ``memory hole''. */
364 if (addr >= 0xa0000 && addr < 0x100000)
368 * stuff other tests for known memory-mapped devices (PCI?)
376 * platform-specific vmspace initialization (nothing for x86_64)
379 cpu_vmspace_alloc(struct vmspace *vm __unused)
384 cpu_vmspace_free(struct vmspace *vm __unused)
389 kvm_access_check(vm_offset_t saddr, vm_offset_t eaddr, int prot)
393 if (saddr < KvaStart)
397 for (addr = saddr; addr < eaddr; addr += PAGE_SIZE) {
398 if (pmap_extract(&kernel_pmap, addr) == 0)
401 if (!kernacc((caddr_t)saddr, eaddr - saddr, prot))