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/smp.h>
74 #include <machine/specialreg.h>
75 #include <machine/tss.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_param.h>
84 #include <x86/isa/isa.h>
86 static void cpu_reset_real(void);
88 static void cpu_reset_proxy(void);
89 static u_int cpu_reset_proxyid;
90 static volatile u_int cpu_reset_proxy_active;
94 get_pcb_user_save_td(struct thread *td)
98 p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
99 cpu_max_ext_state_size;
100 KASSERT((p % 64) == 0, ("Unaligned pcb_user_save area"));
101 return ((struct savefpu *)p);
105 get_pcb_user_save_pcb(struct pcb *pcb)
109 p = (vm_offset_t)(pcb + 1);
110 return ((struct savefpu *)p);
114 get_pcb_td(struct thread *td)
118 p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
119 cpu_max_ext_state_size - sizeof(struct pcb);
120 return ((struct pcb *)p);
124 alloc_fpusave(int flags)
127 struct savefpu_ymm *sf;
129 res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
131 sf = (struct savefpu_ymm *)res;
132 bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
133 sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
139 * Finish a fork operation, with process p2 nearly set up.
140 * Copy and update the pcb, set up the stack so that the child
141 * ready to run and return to user mode.
144 cpu_fork(td1, p2, td2, flags)
145 register struct thread *td1;
146 register struct proc *p2;
150 register struct proc *p1;
152 struct mdproc *mdp1, *mdp2;
153 struct proc_ldt *pldt;
157 if ((flags & RFPROC) == 0) {
158 if ((flags & RFMEM) == 0) {
159 /* unshare user LDT */
162 if ((pldt = mdp1->md_ldt) != NULL &&
163 pldt->ldt_refcnt > 1 &&
164 user_ldt_alloc(p1, 1) == NULL)
165 panic("could not copy LDT");
166 mtx_unlock(&dt_lock);
171 /* Ensure that td1's pcb is up to date. */
174 /* Point the pcb to the top of the stack */
175 pcb2 = get_pcb_td(td2);
179 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
181 /* Properly initialize pcb_save */
182 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
183 bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
184 cpu_max_ext_state_size);
186 /* Point mdproc and then copy over td1's contents */
188 bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
191 * Create a new fresh stack for the new process.
192 * Copy the trap frame for the return to user mode as if from a
193 * syscall. This copies most of the user mode register values.
195 td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
196 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
198 td2->td_frame->tf_rax = 0; /* Child returns zero */
199 td2->td_frame->tf_rflags &= ~PSL_C; /* success */
200 td2->td_frame->tf_rdx = 1;
203 * If the parent process has the trap bit set (i.e. a debugger had
204 * single stepped the process to the system call), we need to clear
205 * the trap flag from the new frame unless the debugger had set PF_FORK
206 * on the parent. Otherwise, the child will receive a (likely
207 * unexpected) SIGTRAP when it executes the first instruction after
208 * returning to userland.
210 if ((p1->p_pfsflags & PF_FORK) == 0)
211 td2->td_frame->tf_rflags &= ~PSL_T;
214 * Set registers for trampoline to user mode. Leave space for the
215 * return address on stack. These are the kernel mode register values.
217 pmap2 = vmspace_pmap(p2->p_vmspace);
218 pcb2->pcb_cr3 = DMAP_TO_PHYS((vm_offset_t)pmap2->pm_pml4);
219 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */
221 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
222 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */
223 pcb2->pcb_rip = (register_t)fork_trampoline;
225 * pcb2->pcb_dr*: cloned above.
226 * pcb2->pcb_savefpu: cloned above.
227 * pcb2->pcb_flags: cloned above.
228 * pcb2->pcb_onfault: cloned above (always NULL here?).
229 * pcb2->pcb_[fg]sbase: cloned above
232 /* Setup to release spin count in fork_exit(). */
233 td2->td_md.md_spinlock_count = 1;
234 td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
236 /* As an i386, do not copy io permission bitmap. */
237 pcb2->pcb_tssp = NULL;
239 /* New segment registers. */
240 set_pcb_flags(pcb2, PCB_FULL_IRET);
242 /* Copy the LDT, if necessary. */
243 mdp1 = &td1->td_proc->p_md;
246 if (mdp1->md_ldt != NULL) {
248 mdp1->md_ldt->ldt_refcnt++;
249 mdp2->md_ldt = mdp1->md_ldt;
250 bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
251 system_segment_descriptor));
254 mdp2->md_ldt = user_ldt_alloc(p2, 0);
255 if (mdp2->md_ldt == NULL)
256 panic("could not copy LDT");
257 amd64_set_ldt_data(td2, 0, max_ldt_segment,
258 (struct user_segment_descriptor *)
259 mdp1->md_ldt->ldt_base);
263 mtx_unlock(&dt_lock);
266 * Now, cpu_switch() can schedule the new process.
267 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
268 * containing the return address when exiting cpu_switch.
269 * This will normally be to fork_trampoline(), which will have
270 * %ebx loaded with the new proc's pointer. fork_trampoline()
271 * will set up a stack to call fork_return(p, frame); to complete
272 * the return to user-mode.
277 * Intercept the return address from a freshly forked process that has NOT
278 * been scheduled yet.
280 * This is needed to make kernel threads stay in kernel mode.
283 cpu_set_fork_handler(td, func, arg)
285 void (*func)(void *);
289 * Note that the trap frame follows the args, so the function
290 * is really called like this: func(arg, frame);
292 td->td_pcb->pcb_r12 = (long) func; /* function */
293 td->td_pcb->pcb_rbx = (long) arg; /* first arg */
297 cpu_exit(struct thread *td)
301 * If this process has a custom LDT, release it.
304 if (td->td_proc->p_md.md_ldt != 0)
307 mtx_unlock(&dt_lock);
311 cpu_thread_exit(struct thread *td)
316 if (td == PCPU_GET(fpcurthread))
322 /* Disable any hardware breakpoints. */
323 if (pcb->pcb_flags & PCB_DBREGS) {
325 clear_pcb_flags(pcb, PCB_DBREGS);
330 cpu_thread_clean(struct thread *td)
339 if (pcb->pcb_tssp != NULL) {
340 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_tssp,
342 pcb->pcb_tssp = NULL;
347 cpu_thread_swapin(struct thread *td)
352 cpu_thread_swapout(struct thread *td)
357 cpu_thread_alloc(struct thread *td)
360 struct xstate_hdr *xhdr;
362 td->td_pcb = pcb = get_pcb_td(td);
363 td->td_frame = (struct trapframe *)pcb - 1;
364 pcb->pcb_save = get_pcb_user_save_pcb(pcb);
366 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
367 bzero(xhdr, sizeof(*xhdr));
368 xhdr->xstate_bv = xsave_mask;
373 cpu_thread_free(struct thread *td)
376 cpu_thread_clean(td);
380 cpu_set_syscall_retval(struct thread *td, int error)
385 td->td_frame->tf_rax = td->td_retval[0];
386 td->td_frame->tf_rdx = td->td_retval[1];
387 td->td_frame->tf_rflags &= ~PSL_C;
392 * Reconstruct pc, we know that 'syscall' is 2 bytes,
393 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
394 * We saved this in tf_err.
395 * %r10 (which was holding the value of %rcx) is restored
396 * for the next iteration.
397 * %r10 restore is only required for freebsd/amd64 processes,
398 * but shall be innocent for any ia32 ABI.
400 td->td_frame->tf_rip -= td->td_frame->tf_err;
401 td->td_frame->tf_r10 = td->td_frame->tf_rcx;
408 if (td->td_proc->p_sysent->sv_errsize) {
409 if (error >= td->td_proc->p_sysent->sv_errsize)
410 error = -1; /* XXX */
412 error = td->td_proc->p_sysent->sv_errtbl[error];
414 td->td_frame->tf_rax = error;
415 td->td_frame->tf_rflags |= PSL_C;
421 * Initialize machine state (pcb and trap frame) for a new thread about to
422 * upcall. Put enough state in the new thread's PCB to get it to go back
423 * userret(), where we can intercept it again to set the return (upcall)
424 * Address and stack, along with those from upcals that are from other sources
425 * such as those generated in thread_userret() itself.
428 cpu_set_upcall(struct thread *td, struct thread *td0)
432 /* Point the pcb to the top of the stack. */
436 * Copy the upcall pcb. This loads kernel regs.
437 * Those not loaded individually below get their default
440 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
441 clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE);
442 pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
443 bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save,
444 cpu_max_ext_state_size);
445 set_pcb_flags(pcb2, PCB_FULL_IRET);
448 * Create a new fresh stack for the new thread.
450 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
452 /* If the current thread has the trap bit set (i.e. a debugger had
453 * single stepped the process to the system call), we need to clear
454 * the trap flag from the new frame. Otherwise, the new thread will
455 * receive a (likely unexpected) SIGTRAP when it executes the first
456 * instruction after returning to userland.
458 td->td_frame->tf_rflags &= ~PSL_T;
461 * Set registers for trampoline to user mode. Leave space for the
462 * return address on stack. These are the kernel mode register values.
464 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */
466 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */
467 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */
468 pcb2->pcb_rip = (register_t)fork_trampoline;
470 * If we didn't copy the pcb, we'd need to do the following registers:
471 * pcb2->pcb_cr3: cloned above.
472 * pcb2->pcb_dr*: cloned above.
473 * pcb2->pcb_savefpu: cloned above.
474 * pcb2->pcb_onfault: cloned above (always NULL here?).
475 * pcb2->pcb_[fg]sbase: cloned above
478 /* Setup to release spin count in fork_exit(). */
479 td->td_md.md_spinlock_count = 1;
480 td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
484 * Set that machine state for performing an upcall that has to
485 * be done in thread_userret() so that those upcalls generated
486 * in thread_userret() itself can be done as well.
489 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
494 * Do any extra cleaning that needs to be done.
495 * The thread may have optional components
496 * that are not present in a fresh thread.
497 * This may be a recycled thread so make it look
498 * as though it's newly allocated.
500 cpu_thread_clean(td);
502 #ifdef COMPAT_FREEBSD32
503 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
505 * Set the trap frame to point at the beginning of the uts
508 td->td_frame->tf_rbp = 0;
509 td->td_frame->tf_rsp =
510 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
511 td->td_frame->tf_rip = (uintptr_t)entry;
514 * Pass the address of the mailbox for this kse to the uts
515 * function as a parameter on the stack.
517 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
518 (uint32_t)(uintptr_t)arg);
525 * Set the trap frame to point at the beginning of the uts
528 td->td_frame->tf_rbp = 0;
529 td->td_frame->tf_rsp =
530 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
531 td->td_frame->tf_rsp -= 8;
532 td->td_frame->tf_rip = (register_t)entry;
533 td->td_frame->tf_ds = _udatasel;
534 td->td_frame->tf_es = _udatasel;
535 td->td_frame->tf_fs = _ufssel;
536 td->td_frame->tf_gs = _ugssel;
537 td->td_frame->tf_flags = TF_HASSEGS;
540 * Pass the address of the mailbox for this kse to the uts
541 * function as a parameter on the stack.
543 td->td_frame->tf_rdi = (register_t)arg;
547 cpu_set_user_tls(struct thread *td, void *tls_base)
551 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
555 set_pcb_flags(pcb, PCB_FULL_IRET);
556 #ifdef COMPAT_FREEBSD32
557 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
558 pcb->pcb_gsbase = (register_t)tls_base;
562 pcb->pcb_fsbase = (register_t)tls_base;
572 cpu_reset_proxy_active = 1;
573 while (cpu_reset_proxy_active == 1)
574 ; /* Wait for other cpu to see that we've started */
575 CPU_SETOF(cpu_reset_proxyid, &tcrp);
577 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
592 CPU_CLR(PCPU_GET(cpuid), &map);
593 CPU_NAND(&map, &stopped_cpus);
594 if (!CPU_EMPTY(&map)) {
595 printf("cpu_reset: Stopping other CPUs\n");
599 if (PCPU_GET(cpuid) != 0) {
600 cpu_reset_proxyid = PCPU_GET(cpuid);
601 cpustop_restartfunc = cpu_reset_proxy;
602 cpu_reset_proxy_active = 0;
603 printf("cpu_reset: Restarting BSP\n");
605 /* Restart CPU #0. */
606 CPU_SETOF(0, &started_cpus);
610 while (cpu_reset_proxy_active == 0 && cnt < 10000000)
611 cnt++; /* Wait for BSP to announce restart */
612 if (cpu_reset_proxy_active == 0)
613 printf("cpu_reset: Failed to restart BSP\n");
615 cpu_reset_proxy_active = 2;
631 struct region_descriptor null_idt;
637 * Attempt to do a CPU reset via the keyboard controller,
638 * do not turn off GateA20, as any machine that fails
639 * to do the reset here would then end up in no man's land.
641 outb(IO_KBD + 4, 0xFE);
642 DELAY(500000); /* wait 0.5 sec to see if that did it */
645 * Attempt to force a reset via the Reset Control register at
646 * I/O port 0xcf9. Bit 2 forces a system reset when it
647 * transitions from 0 to 1. Bit 1 selects the type of reset
648 * to attempt: 0 selects a "soft" reset, and 1 selects a
649 * "hard" reset. We try a "hard" reset. The first write sets
650 * bit 1 to select a "hard" reset and clears bit 2. The
651 * second write forces a 0 -> 1 transition in bit 2 to trigger
656 DELAY(500000); /* wait 0.5 sec to see if that did it */
659 * Attempt to force a reset via the Fast A20 and Init register
660 * at I/O port 0x92. Bit 1 serves as an alternate A20 gate.
661 * Bit 0 asserts INIT# when set to 1. We are careful to only
662 * preserve bit 1 while setting bit 0. We also must clear bit
663 * 0 before setting it if it isn't already clear.
668 outb(0x92, b & 0xfe);
670 DELAY(500000); /* wait 0.5 sec to see if that did it */
673 printf("No known reset method worked, attempting CPU shutdown\n");
674 DELAY(1000000); /* wait 1 sec for printf to complete */
677 null_idt.rd_limit = 0;
678 null_idt.rd_base = 0;
681 /* "good night, sweet prince .... <THUNK!>" */
689 * Allocate an sf_buf for the given vm_page. On this machine, however, there
690 * is no sf_buf object. Instead, an opaque pointer to the given vm_page is
694 sf_buf_alloc(struct vm_page *m, int pri)
697 return ((struct sf_buf *)m);
701 * Free the sf_buf. In fact, do nothing because there are no resources
702 * associated with the sf_buf.
705 sf_buf_free(struct sf_buf *sf)
710 * Software interrupt handler for queued VM system processing.
715 if (busdma_swi_pending != 0)
720 * Tell whether this address is in some physical memory region.
721 * Currently used by the kernel coredump code in order to avoid
722 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
723 * or other unpredictable behaviour.
727 is_physical_memory(vm_paddr_t addr)
731 /* The ISA ``memory hole''. */
732 if (addr >= 0xa0000 && addr < 0x100000)
737 * stuff other tests for known memory-mapped devices (PCI?)