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
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91
40 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
48 #include "opt_compat.h"
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/pioctl.h>
62 #include <sys/sf_buf.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
68 #include <sys/vmmeter.h>
70 #include <machine/cpu.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/specialreg.h>
74 #include <machine/tss.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_param.h>
83 #include <amd64/isa/isa.h>
85 static void cpu_reset_real(void);
87 static void cpu_reset_proxy(void);
88 static u_int cpu_reset_proxyid;
89 static volatile u_int cpu_reset_proxy_active;
93 * Finish a fork operation, with process p2 nearly set up.
94 * Copy and update the pcb, set up the stack so that the child
95 * ready to run and return to user mode.
98 cpu_fork(td1, p2, td2, flags)
99 register struct thread *td1;
100 register struct proc *p2;
104 register struct proc *p1;
106 struct mdproc *mdp1, *mdp2;
107 struct proc_ldt *pldt;
111 if ((flags & RFPROC) == 0) {
112 if ((flags & RFMEM) == 0) {
113 /* unshare user LDT */
116 if ((pldt = mdp1->md_ldt) != NULL &&
117 pldt->ldt_refcnt > 1 &&
118 user_ldt_alloc(p1, 1) == NULL)
119 panic("could not copy LDT");
120 mtx_unlock(&dt_lock);
125 /* Ensure that td1's pcb is up to date. */
128 /* Point the pcb to the top of the stack */
129 pcb2 = (struct pcb *)(td2->td_kstack +
130 td2->td_kstack_pages * PAGE_SIZE) - 1;
134 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
136 /* Properly initialize pcb_save */
137 pcb2->pcb_save = &pcb2->pcb_user_save;
139 /* Point mdproc and then copy over td1's contents */
141 bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
144 * Create a new fresh stack for the new process.
145 * Copy the trap frame for the return to user mode as if from a
146 * syscall. This copies most of the user mode register values.
148 td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
149 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
151 td2->td_frame->tf_rax = 0; /* Child returns zero */
152 td2->td_frame->tf_rflags &= ~PSL_C; /* success */
153 td2->td_frame->tf_rdx = 1;
156 * If the parent process has the trap bit set (i.e. a debugger had
157 * single stepped the process to the system call), we need to clear
158 * the trap flag from the new frame unless the debugger had set PF_FORK
159 * on the parent. Otherwise, the child will receive a (likely
160 * unexpected) SIGTRAP when it executes the first instruction after
161 * returning to userland.
163 if ((p1->p_pfsflags & PF_FORK) == 0)
164 td2->td_frame->tf_rflags &= ~PSL_T;
167 * Set registers for trampoline to user mode. Leave space for the
168 * return address on stack. These are the kernel mode register values.
170 pmap2 = vmspace_pmap(p2->p_vmspace);
171 pcb2->pcb_cr3 = DMAP_TO_PHYS((vm_offset_t)pmap2->pm_pml4);
172 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */
174 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
175 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */
176 pcb2->pcb_rip = (register_t)fork_trampoline;
178 * pcb2->pcb_dr*: cloned above.
179 * pcb2->pcb_savefpu: cloned above.
180 * pcb2->pcb_flags: cloned above.
181 * pcb2->pcb_onfault: cloned above (always NULL here?).
182 * pcb2->pcb_[fg]sbase: cloned above
185 /* Setup to release spin count in fork_exit(). */
186 td2->td_md.md_spinlock_count = 1;
187 td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
189 /* As an i386, do not copy io permission bitmap. */
190 pcb2->pcb_tssp = NULL;
192 /* New segment registers. */
193 pcb2->pcb_full_iret = 1;
195 /* Copy the LDT, if necessary. */
196 mdp1 = &td1->td_proc->p_md;
199 if (mdp1->md_ldt != NULL) {
201 mdp1->md_ldt->ldt_refcnt++;
202 mdp2->md_ldt = mdp1->md_ldt;
203 bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
204 system_segment_descriptor));
207 mdp2->md_ldt = user_ldt_alloc(p2, 0);
208 if (mdp2->md_ldt == NULL)
209 panic("could not copy LDT");
210 amd64_set_ldt_data(td2, 0, max_ldt_segment,
211 (struct user_segment_descriptor *)
212 mdp1->md_ldt->ldt_base);
216 mtx_unlock(&dt_lock);
219 * Now, cpu_switch() can schedule the new process.
220 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
221 * containing the return address when exiting cpu_switch.
222 * This will normally be to fork_trampoline(), which will have
223 * %ebx loaded with the new proc's pointer. fork_trampoline()
224 * will set up a stack to call fork_return(p, frame); to complete
225 * the return to user-mode.
230 * Intercept the return address from a freshly forked process that has NOT
231 * been scheduled yet.
233 * This is needed to make kernel threads stay in kernel mode.
236 cpu_set_fork_handler(td, func, arg)
238 void (*func)(void *);
242 * Note that the trap frame follows the args, so the function
243 * is really called like this: func(arg, frame);
245 td->td_pcb->pcb_r12 = (long) func; /* function */
246 td->td_pcb->pcb_rbx = (long) arg; /* first arg */
250 cpu_exit(struct thread *td)
254 * If this process has a custom LDT, release it.
257 if (td->td_proc->p_md.md_ldt != 0)
260 mtx_unlock(&dt_lock);
264 cpu_thread_exit(struct thread *td)
269 if (td == PCPU_GET(fpcurthread))
275 /* Disable any hardware breakpoints. */
276 if (pcb->pcb_flags & PCB_DBREGS) {
278 pcb->pcb_flags &= ~PCB_DBREGS;
283 cpu_thread_clean(struct thread *td)
292 if (pcb->pcb_tssp != NULL) {
293 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_tssp,
295 pcb->pcb_tssp = NULL;
300 cpu_thread_swapin(struct thread *td)
305 cpu_thread_swapout(struct thread *td)
310 cpu_thread_alloc(struct thread *td)
313 td->td_pcb = (struct pcb *)(td->td_kstack +
314 td->td_kstack_pages * PAGE_SIZE) - 1;
315 td->td_frame = (struct trapframe *)td->td_pcb - 1;
316 td->td_pcb->pcb_save = &td->td_pcb->pcb_user_save;
320 cpu_thread_free(struct thread *td)
323 cpu_thread_clean(td);
327 cpu_set_syscall_retval(struct thread *td, int error)
332 td->td_frame->tf_rax = td->td_retval[0];
333 td->td_frame->tf_rdx = td->td_retval[1];
334 td->td_frame->tf_rflags &= ~PSL_C;
339 * Reconstruct pc, we know that 'syscall' is 2 bytes,
340 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
341 * We saved this in tf_err.
342 * %r10 (which was holding the value of %rcx) is restored
343 * for the next iteration.
344 * %r10 restore is only required for freebsd/amd64 processes,
345 * but shall be innocent for any ia32 ABI.
347 td->td_frame->tf_rip -= td->td_frame->tf_err;
348 td->td_frame->tf_r10 = td->td_frame->tf_rcx;
355 if (td->td_proc->p_sysent->sv_errsize) {
356 if (error >= td->td_proc->p_sysent->sv_errsize)
357 error = -1; /* XXX */
359 error = td->td_proc->p_sysent->sv_errtbl[error];
361 td->td_frame->tf_rax = error;
362 td->td_frame->tf_rflags |= PSL_C;
368 * Initialize machine state (pcb and trap frame) for a new thread about to
369 * upcall. Put enough state in the new thread's PCB to get it to go back
370 * userret(), where we can intercept it again to set the return (upcall)
371 * Address and stack, along with those from upcals that are from other sources
372 * such as those generated in thread_userret() itself.
375 cpu_set_upcall(struct thread *td, struct thread *td0)
379 /* Point the pcb to the top of the stack. */
383 * Copy the upcall pcb. This loads kernel regs.
384 * Those not loaded individually below get their default
387 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
388 pcb2->pcb_flags &= ~(PCB_FPUINITDONE | PCB_USERFPUINITDONE);
389 pcb2->pcb_save = &pcb2->pcb_user_save;
390 pcb2->pcb_full_iret = 1;
393 * Create a new fresh stack for the new thread.
395 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
397 /* If the current thread has the trap bit set (i.e. a debugger had
398 * single stepped the process to the system call), we need to clear
399 * the trap flag from the new frame. Otherwise, the new thread will
400 * receive a (likely unexpected) SIGTRAP when it executes the first
401 * instruction after returning to userland.
403 td->td_frame->tf_rflags &= ~PSL_T;
406 * Set registers for trampoline to user mode. Leave space for the
407 * return address on stack. These are the kernel mode register values.
409 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */
411 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */
412 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */
413 pcb2->pcb_rip = (register_t)fork_trampoline;
415 * If we didn't copy the pcb, we'd need to do the following registers:
416 * pcb2->pcb_cr3: cloned above.
417 * pcb2->pcb_dr*: cloned above.
418 * pcb2->pcb_savefpu: cloned above.
419 * pcb2->pcb_onfault: cloned above (always NULL here?).
420 * pcb2->pcb_[fg]sbase: cloned above
423 /* Setup to release spin count in fork_exit(). */
424 td->td_md.md_spinlock_count = 1;
425 td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
429 * Set that machine state for performing an upcall that has to
430 * be done in thread_userret() so that those upcalls generated
431 * in thread_userret() itself can be done as well.
434 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
439 * Do any extra cleaning that needs to be done.
440 * The thread may have optional components
441 * that are not present in a fresh thread.
442 * This may be a recycled thread so make it look
443 * as though it's newly allocated.
445 cpu_thread_clean(td);
447 #ifdef COMPAT_FREEBSD32
448 if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
450 * Set the trap frame to point at the beginning of the uts
453 td->td_frame->tf_rbp = 0;
454 td->td_frame->tf_rsp =
455 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
456 td->td_frame->tf_rip = (uintptr_t)entry;
459 * Pass the address of the mailbox for this kse to the uts
460 * function as a parameter on the stack.
462 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
463 (uint32_t)(uintptr_t)arg);
470 * Set the trap frame to point at the beginning of the uts
473 td->td_frame->tf_rbp = 0;
474 td->td_frame->tf_rsp =
475 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
476 td->td_frame->tf_rsp -= 8;
477 td->td_frame->tf_rip = (register_t)entry;
478 td->td_frame->tf_ds = _udatasel;
479 td->td_frame->tf_es = _udatasel;
480 td->td_frame->tf_fs = _ufssel;
481 td->td_frame->tf_gs = _ugssel;
482 td->td_frame->tf_flags = TF_HASSEGS;
485 * Pass the address of the mailbox for this kse to the uts
486 * function as a parameter on the stack.
488 td->td_frame->tf_rdi = (register_t)arg;
492 cpu_set_user_tls(struct thread *td, void *tls_base)
495 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
498 #ifdef COMPAT_FREEBSD32
499 if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
500 td->td_pcb->pcb_gsbase = (register_t)tls_base;
504 td->td_pcb->pcb_fsbase = (register_t)tls_base;
505 td->td_pcb->pcb_full_iret = 1;
514 cpu_reset_proxy_active = 1;
515 while (cpu_reset_proxy_active == 1)
516 ; /* Wait for other cpu to see that we've started */
517 stop_cpus((1<<cpu_reset_proxyid));
518 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
532 map = PCPU_GET(other_cpus) & ~stopped_cpus;
534 printf("cpu_reset: Stopping other CPUs\n");
538 if (PCPU_GET(cpuid) != 0) {
539 cpu_reset_proxyid = PCPU_GET(cpuid);
540 cpustop_restartfunc = cpu_reset_proxy;
541 cpu_reset_proxy_active = 0;
542 printf("cpu_reset: Restarting BSP\n");
544 /* Restart CPU #0. */
545 atomic_store_rel_int(&started_cpus, 1 << 0);
548 while (cpu_reset_proxy_active == 0 && cnt < 10000000)
549 cnt++; /* Wait for BSP to announce restart */
550 if (cpu_reset_proxy_active == 0)
551 printf("cpu_reset: Failed to restart BSP\n");
553 cpu_reset_proxy_active = 2;
569 struct region_descriptor null_idt;
575 * Attempt to do a CPU reset via the keyboard controller,
576 * do not turn off GateA20, as any machine that fails
577 * to do the reset here would then end up in no man's land.
579 outb(IO_KBD + 4, 0xFE);
580 DELAY(500000); /* wait 0.5 sec to see if that did it */
583 * Attempt to force a reset via the Reset Control register at
584 * I/O port 0xcf9. Bit 2 forces a system reset when it
585 * transitions from 0 to 1. Bit 1 selects the type of reset
586 * to attempt: 0 selects a "soft" reset, and 1 selects a
587 * "hard" reset. We try a "hard" reset. The first write sets
588 * bit 1 to select a "hard" reset and clears bit 2. The
589 * second write forces a 0 -> 1 transition in bit 2 to trigger
594 DELAY(500000); /* wait 0.5 sec to see if that did it */
597 * Attempt to force a reset via the Fast A20 and Init register
598 * at I/O port 0x92. Bit 1 serves as an alternate A20 gate.
599 * Bit 0 asserts INIT# when set to 1. We are careful to only
600 * preserve bit 1 while setting bit 0. We also must clear bit
601 * 0 before setting it if it isn't already clear.
606 outb(0x92, b & 0xfe);
608 DELAY(500000); /* wait 0.5 sec to see if that did it */
611 printf("No known reset method worked, attempting CPU shutdown\n");
612 DELAY(1000000); /* wait 1 sec for printf to complete */
615 null_idt.rd_limit = 0;
616 null_idt.rd_base = 0;
619 /* "good night, sweet prince .... <THUNK!>" */
627 * Allocate an sf_buf for the given vm_page. On this machine, however, there
628 * is no sf_buf object. Instead, an opaque pointer to the given vm_page is
632 sf_buf_alloc(struct vm_page *m, int pri)
635 return ((struct sf_buf *)m);
639 * Free the sf_buf. In fact, do nothing because there are no resources
640 * associated with the sf_buf.
643 sf_buf_free(struct sf_buf *sf)
648 * Software interrupt handler for queued VM system processing.
653 if (busdma_swi_pending != 0)
658 * Tell whether this address is in some physical memory region.
659 * Currently used by the kernel coredump code in order to avoid
660 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
661 * or other unpredictable behaviour.
665 is_physical_memory(vm_paddr_t addr)
669 /* The ISA ``memory hole''. */
670 if (addr >= 0xa0000 && addr < 0x100000)
675 * stuff other tests for known memory-mapped devices (PCI?)