xref: /freebsd/sys/i386/i386/vm_machdep.c (revision ba682f8b9b58f6b497a5015ef70b2120b8d96663)
15b81b6b3SRodney W. Grimes /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
45b81b6b3SRodney W. Grimes  * Copyright (c) 1982, 1986 The Regents of the University of California.
55b81b6b3SRodney W. Grimes  * Copyright (c) 1989, 1990 William Jolitz
61561d038SDavid Greenman  * Copyright (c) 1994 John Dyson
75b81b6b3SRodney W. Grimes  * All rights reserved.
85b81b6b3SRodney W. Grimes  *
95b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
105b81b6b3SRodney W. Grimes  * the Systems Programming Group of the University of Utah Computer
115b81b6b3SRodney W. Grimes  * Science Department, and William Jolitz.
125b81b6b3SRodney W. Grimes  *
135b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
145b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
155b81b6b3SRodney W. Grimes  * are met:
165b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
175b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
185b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
195b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
205b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
215b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
225b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
235b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
245b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
255b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
265b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
275b81b6b3SRodney W. Grimes  *    without specific prior written permission.
285b81b6b3SRodney W. Grimes  *
295b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
305b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
315b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
325b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
335b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
345b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
355b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
365b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
375b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
385b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
395b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
405b81b6b3SRodney W. Grimes  *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
415b81b6b3SRodney W. Grimes  */
425b81b6b3SRodney W. Grimes 
439676a785SDavid E. O'Brien #include <sys/cdefs.h>
446d7d4649SBruce Evans #include "opt_isa.h"
45558226eaSPeter Wemm #include "opt_npx.h"
460d74cc48SPeter Wemm #include "opt_reset.h"
47883bd55aSPoul-Henning Kamp #include "opt_cpu.h"
488890984dSGarrett Wollman 
4926f9a767SRodney W. Grimes #include <sys/param.h>
5026f9a767SRodney W. Grimes #include <sys/systm.h>
519626b608SPoul-Henning Kamp #include <sys/bio.h>
5226f9a767SRodney W. Grimes #include <sys/buf.h>
5366095752SJohn Dyson #include <sys/kernel.h>
540384fff8SJason Evans #include <sys/ktr.h>
556d7d4649SBruce Evans #include <sys/lock.h>
566d7d4649SBruce Evans #include <sys/malloc.h>
57411d10a6SAlan Cox #include <sys/mbuf.h>
5813b0500fSJohn Baldwin #include <sys/mutex.h>
596d7d4649SBruce Evans #include <sys/proc.h>
60a7b89044SKonstantin Belousov #include <sys/sysent.h>
61e45db9b8SAlan Cox #include <sys/sf_buf.h>
626d7d4649SBruce Evans #include <sys/smp.h>
634c0e268aSStephan Uphoff #include <sys/sched.h>
6466095752SJohn Dyson #include <sys/sysctl.h>
6591c28bfdSLuoqi Chen #include <sys/unistd.h>
666d7d4649SBruce Evans #include <sys/vnode.h>
676d7d4649SBruce Evans #include <sys/vmmeter.h>
685b81b6b3SRodney W. Grimes 
69a2a1c95cSPeter Wemm #include <machine/cpu.h>
7041ee9f1cSPoul-Henning Kamp #include <machine/cputypes.h>
711f8745a9SPeter Wemm #include <machine/md_var.h>
7291c28bfdSLuoqi Chen #include <machine/pcb.h>
7391c28bfdSLuoqi Chen #include <machine/pcb_ext.h>
745c0db7c7SAlan Cox #include <machine/smp.h>
7591c28bfdSLuoqi Chen #include <machine/vm86.h>
765b81b6b3SRodney W. Grimes 
7726f9a767SRodney W. Grimes #include <vm/vm.h>
786d7d4649SBruce Evans #include <vm/vm_extern.h>
7926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
8024a1cce3SDavid Greenman #include <vm/vm_page.h>
81efeaf95aSDavid Greenman #include <vm/vm_map.h>
826d7d4649SBruce Evans #include <vm/vm_param.h>
835b81b6b3SRodney W. Grimes 
8448cae112SKonstantin Belousov _Static_assert(__OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
8540025d42SAndrey V. Elsukov     "__OFFSETOF_MONITORBUF does not correspond with offset of pc_monitorbuf.");
86e93d0cbeSKonstantin Belousov 
87824fc460SJohn Baldwin union savefpu *
88824fc460SJohn Baldwin get_pcb_user_save_td(struct thread *td)
89824fc460SJohn Baldwin {
90824fc460SJohn Baldwin 	vm_offset_t p;
912f1e7069STor Egge 
92824fc460SJohn Baldwin 	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
933ef966c4SKonstantin Belousov 	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
943ef966c4SKonstantin Belousov 	KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
95824fc460SJohn Baldwin 	return ((union savefpu *)p);
96824fc460SJohn Baldwin }
97824fc460SJohn Baldwin 
98824fc460SJohn Baldwin union savefpu *
99824fc460SJohn Baldwin get_pcb_user_save_pcb(struct pcb *pcb)
100824fc460SJohn Baldwin {
101824fc460SJohn Baldwin 	vm_offset_t p;
102824fc460SJohn Baldwin 
103824fc460SJohn Baldwin 	p = (vm_offset_t)(pcb + 1);
104824fc460SJohn Baldwin 	return ((union savefpu *)p);
105824fc460SJohn Baldwin }
106824fc460SJohn Baldwin 
107824fc460SJohn Baldwin struct pcb *
108824fc460SJohn Baldwin get_pcb_td(struct thread *td)
109824fc460SJohn Baldwin {
110824fc460SJohn Baldwin 	vm_offset_t p;
111824fc460SJohn Baldwin 
112824fc460SJohn Baldwin 	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
1133ef966c4SKonstantin Belousov 	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
1143ef966c4SKonstantin Belousov 	    sizeof(struct pcb);
115824fc460SJohn Baldwin 	return ((struct pcb *)p);
116824fc460SJohn Baldwin }
117824fc460SJohn Baldwin 
118824fc460SJohn Baldwin void *
119824fc460SJohn Baldwin alloc_fpusave(int flags)
120824fc460SJohn Baldwin {
121824fc460SJohn Baldwin 	void *res;
122824fc460SJohn Baldwin 	struct savefpu_ymm *sf;
123824fc460SJohn Baldwin 
124824fc460SJohn Baldwin 	res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
125824fc460SJohn Baldwin 	if (use_xsave) {
126824fc460SJohn Baldwin 		sf = (struct savefpu_ymm *)res;
127824fc460SJohn Baldwin 		bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
128824fc460SJohn Baldwin 		sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
129824fc460SJohn Baldwin 	}
130824fc460SJohn Baldwin 	return (res);
131824fc460SJohn Baldwin }
132a7883464SJohn Baldwin 
133a7883464SJohn Baldwin /*
134a7883464SJohn Baldwin  * Common code shared between cpu_fork() and cpu_copy_thread() for
135a7883464SJohn Baldwin  * initializing a thread.
136a7883464SJohn Baldwin  */
137a7883464SJohn Baldwin static void
138a7883464SJohn Baldwin copy_thread(struct thread *td1, struct thread *td2)
139a7883464SJohn Baldwin {
140a7883464SJohn Baldwin 	struct pcb *pcb2;
141a7883464SJohn Baldwin 
142a7883464SJohn Baldwin 	pcb2 = td2->td_pcb;
143a7883464SJohn Baldwin 
144a7883464SJohn Baldwin 	/* Ensure that td1's pcb is up to date for user threads. */
145a7883464SJohn Baldwin 	if ((td2->td_pflags & TDP_KTHREAD) == 0) {
146a7883464SJohn Baldwin 		MPASS(td1 == curthread);
147a7883464SJohn Baldwin 		td1->td_pcb->pcb_gs = rgs();
148a7883464SJohn Baldwin 		critical_enter();
149a7883464SJohn Baldwin 		if (PCPU_GET(fpcurthread) == td1)
150a7883464SJohn Baldwin 			npxsave(td1->td_pcb->pcb_save);
151a7883464SJohn Baldwin 		critical_exit();
152a7883464SJohn Baldwin 	}
153a7883464SJohn Baldwin 
154a7883464SJohn Baldwin 	/* Copy td1's pcb */
155a7883464SJohn Baldwin 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
156a7883464SJohn Baldwin 
157a7883464SJohn Baldwin 	/* Properly initialize pcb_save */
158a7883464SJohn Baldwin 	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
159a7883464SJohn Baldwin 
160a7883464SJohn Baldwin 	/* Kernel threads start with clean NPX and segment bases. */
161a7883464SJohn Baldwin 	if ((td2->td_pflags & TDP_KTHREAD) != 0) {
162a7883464SJohn Baldwin 		pcb2->pcb_gs = _udatasel;
163a7883464SJohn Baldwin 		set_fsbase(td2, 0);
164a7883464SJohn Baldwin 		set_gsbase(td2, 0);
165a7883464SJohn Baldwin 		pcb2->pcb_flags &= ~(PCB_NPXINITDONE | PCB_NPXUSERINITDONE |
166a7883464SJohn Baldwin 		    PCB_KERNNPX | PCB_KERNNPX_THR);
167a7883464SJohn Baldwin 	} else {
168a7883464SJohn Baldwin 		MPASS((pcb2->pcb_flags & (PCB_KERNNPX | PCB_KERNNPX_THR)) == 0);
169a7883464SJohn Baldwin 		bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
170a7883464SJohn Baldwin 		    cpu_max_ext_state_size);
171a7883464SJohn Baldwin 	}
172a7883464SJohn Baldwin 
173a7883464SJohn Baldwin 	/*
174a7883464SJohn Baldwin 	 * Set registers for trampoline to user mode.  Leave space for the
175a7883464SJohn Baldwin 	 * return address on stack.  These are the kernel mode register values.
176a7883464SJohn Baldwin 	 */
177a7883464SJohn Baldwin 	pcb2->pcb_edi = 0;
178a7883464SJohn Baldwin 	pcb2->pcb_esi = (int)fork_return;		    /* trampoline arg */
179a7883464SJohn Baldwin 	pcb2->pcb_ebp = 0;
180a7883464SJohn Baldwin 	pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *); /* trampoline arg */
181a7883464SJohn Baldwin 	pcb2->pcb_ebx = (int)td2;			    /* trampoline arg */
182a7883464SJohn Baldwin 	pcb2->pcb_eip = (int)fork_trampoline + setidt_disp;
183a7883464SJohn Baldwin 	/*
184a7883464SJohn Baldwin 	 * If we didn't copy the pcb, we'd need to do the following registers:
185a7883464SJohn Baldwin 	 * pcb2->pcb_cr3:	cloned above.
186a7883464SJohn Baldwin 	 * pcb2->pcb_dr*:	cloned above.
187a7883464SJohn Baldwin 	 * pcb2->pcb_savefpu:	cloned above.
188a7883464SJohn Baldwin 	 * pcb2->pcb_flags:	cloned above.
189a7883464SJohn Baldwin 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
190a7883464SJohn Baldwin 	 * pcb2->pcb_gs:	cloned above.
191a7883464SJohn Baldwin 	 * pcb2->pcb_ext:	cleared below.
192a7883464SJohn Baldwin 	 */
193a7883464SJohn Baldwin 	pcb2->pcb_ext = NULL;
194a7883464SJohn Baldwin 
195a7883464SJohn Baldwin 	/* Setup to release spin count in fork_exit(). */
196a7883464SJohn Baldwin 	td2->td_md.md_spinlock_count = 1;
197a7883464SJohn Baldwin 	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
198a7883464SJohn Baldwin }
199a7883464SJohn Baldwin 
200a4f7a4c9SDavid Greenman /*
2015b81b6b3SRodney W. Grimes  * Finish a fork operation, with process p2 nearly set up.
202a2a1c95cSPeter Wemm  * Copy and update the pcb, set up the stack so that the child
203a2a1c95cSPeter Wemm  * ready to run and return to user mode.
2045b81b6b3SRodney W. Grimes  */
205a2a1c95cSPeter Wemm void
2069c16356cSKonstantin Belousov cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
2075b81b6b3SRodney W. Grimes {
2083e85b721SEd Maste 	struct proc *p1;
20991c28bfdSLuoqi Chen 	struct pcb *pcb2;
21024db0459SJohn Baldwin 	struct mdproc *mdp2;
21191c28bfdSLuoqi Chen 
212b40ce416SJulian Elischer 	p1 = td1->td_proc;
21391c28bfdSLuoqi Chen 	if ((flags & RFPROC) == 0) {
21491c28bfdSLuoqi Chen 		if ((flags & RFMEM) == 0) {
21591c28bfdSLuoqi Chen 			/* unshare user LDT */
21645449f84SJulian Elischer 			struct mdproc *mdp1 = &p1->p_md;
2179719da13SKonstantin Belousov 			struct proc_ldt *pldt, *pldt1;
218bc2e774aSJohn Baldwin 
2190ad5e7f3SJeff Roberson 			mtx_lock_spin(&dt_lock);
2209719da13SKonstantin Belousov 			if ((pldt1 = mdp1->md_ldt) != NULL &&
2219719da13SKonstantin Belousov 			    pldt1->ldt_refcnt > 1) {
2229719da13SKonstantin Belousov 				pldt = user_ldt_alloc(mdp1, pldt1->ldt_len);
22324db0459SJohn Baldwin 				if (pldt == NULL)
2241acf256dSJohn Baldwin 					panic("could not copy LDT");
22524db0459SJohn Baldwin 				mdp1->md_ldt = pldt;
22624db0459SJohn Baldwin 				set_user_ldt(mdp1);
2279719da13SKonstantin Belousov 				user_ldt_deref(pldt1);
22802b0a160SAttilio Rao 			} else
2290ad5e7f3SJeff Roberson 				mtx_unlock_spin(&dt_lock);
23091c28bfdSLuoqi Chen 		}
23191c28bfdSLuoqi Chen 		return;
23291c28bfdSLuoqi Chen 	}
2335b81b6b3SRodney W. Grimes 
234b40ce416SJulian Elischer 	/* Point the pcb to the top of the stack */
235824fc460SJohn Baldwin 	pcb2 = get_pcb_td(td2);
236b40ce416SJulian Elischer 	td2->td_pcb = pcb2;
237b40ce416SJulian Elischer 
238a7883464SJohn Baldwin 	copy_thread(td1, td2);
2396cf9a08dSKonstantin Belousov 
2408223717cSKonstantin Belousov 	/* Reset debug registers in the new process */
241290b0d12SKonstantin Belousov 	x86_clear_dbregs(pcb2);
2428223717cSKonstantin Belousov 
24324db0459SJohn Baldwin 	/* Point mdproc and then copy over td1's contents */
24445449f84SJulian Elischer 	mdp2 = &p2->p_md;
24545449f84SJulian Elischer 	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
24624db0459SJohn Baldwin 
247a2a1c95cSPeter Wemm 	/*
2481f8745a9SPeter Wemm 	 * Copy the trap frame for the return to user mode as if from a
249a4b8c657SBruce Evans 	 * syscall.  This copies most of the user mode register values.
250d86c1f0dSKonstantin Belousov 	 * The -VM86_STACK_SPACE (-16) is so we can expand the trapframe
251d86c1f0dSKonstantin Belousov 	 * if we go to vm86.
252a2a1c95cSPeter Wemm 	 */
253d86c1f0dSKonstantin Belousov 	td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb -
254d86c1f0dSKonstantin Belousov 	    VM86_STACK_SPACE) - 1;
255b40ce416SJulian Elischer 	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
256a2a1c95cSPeter Wemm 
257de8374dfSDmitry Chagin 	/* Set child return values. */
258de8374dfSDmitry Chagin 	p2->p_sysent->sv_set_fork_retval(td2);
2590d54b9d1SJohn Baldwin 
260a2a1c95cSPeter Wemm 	/*
26159838c1aSJohn Baldwin 	 * If the parent process has the trap bit set (i.e. a debugger
26259838c1aSJohn Baldwin 	 * had single stepped the process to the system call), we need
26359838c1aSJohn Baldwin 	 * to clear the trap flag from the new frame.
2645ad5504cSKelly Yancey 	 */
2655ad5504cSKelly Yancey 	td2->td_frame->tf_eflags &= ~PSL_T;
2665ad5504cSKelly Yancey 
267a7883464SJohn Baldwin 	/* Set cr3 for the new process. */
2689a527560SKonstantin Belousov 	pcb2->pcb_cr3 = pmap_get_cr3(vmspace_pmap(p2->p_vmspace));
2695b81b6b3SRodney W. Grimes 
27048a09cf2SJohn Dyson 	/*
27148a09cf2SJohn Dyson 	 * XXX don't copy the i/o pages.  this should probably be fixed.
27248a09cf2SJohn Dyson 	 */
273a7883464SJohn Baldwin 	pcb2->pcb_ext = NULL;
27448a09cf2SJohn Dyson 
2758c39a127SStefan Eßer 	/* Copy the LDT, if necessary. */
2760ad5e7f3SJeff Roberson 	mtx_lock_spin(&dt_lock);
277753d1af1SJohn Baldwin 	if (mdp2->md_ldt != NULL) {
27891c28bfdSLuoqi Chen 		if (flags & RFMEM) {
27905dfa22fSAttilio Rao 			mdp2->md_ldt->ldt_refcnt++;
28091c28bfdSLuoqi Chen 		} else {
28124db0459SJohn Baldwin 			mdp2->md_ldt = user_ldt_alloc(mdp2,
28224db0459SJohn Baldwin 			    mdp2->md_ldt->ldt_len);
28324db0459SJohn Baldwin 			if (mdp2->md_ldt == NULL)
284df4d012bSJohn Baldwin 				panic("could not copy LDT");
28591c28bfdSLuoqi Chen 		}
2868c39a127SStefan Eßer 	}
2870ad5e7f3SJeff Roberson 	mtx_unlock_spin(&dt_lock);
2888c39a127SStefan Eßer 
289a2a1c95cSPeter Wemm 	/*
290a2a1c95cSPeter Wemm 	 * Now, cpu_switch() can schedule the new process.
291a2a1c95cSPeter Wemm 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
292a2a1c95cSPeter Wemm 	 * containing the return address when exiting cpu_switch.
29337b087a6SPeter Wemm 	 * This will normally be to fork_trampoline(), which will have
29437b087a6SPeter Wemm 	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
295a2a1c95cSPeter Wemm 	 * will set up a stack to call fork_return(p, frame); to complete
296a2a1c95cSPeter Wemm 	 * the return to user-mode.
297a2a1c95cSPeter Wemm 	 */
298a2a1c95cSPeter Wemm }
299a2a1c95cSPeter Wemm 
300de8374dfSDmitry Chagin void
301de8374dfSDmitry Chagin x86_set_fork_retval(struct thread *td)
302de8374dfSDmitry Chagin {
303de8374dfSDmitry Chagin 	struct trapframe * frame = td->td_frame;
304de8374dfSDmitry Chagin 
305de8374dfSDmitry Chagin 	frame->tf_eax = 0;		/* Child returns zero */
306de8374dfSDmitry Chagin 	frame->tf_eflags &= ~PSL_C;	/* success */
307de8374dfSDmitry Chagin 	frame->tf_edx = 1;		/* System V emulation */
308de8374dfSDmitry Chagin }
309de8374dfSDmitry Chagin 
310a2a1c95cSPeter Wemm /*
311a2a1c95cSPeter Wemm  * Intercept the return address from a freshly forked process that has NOT
312a2a1c95cSPeter Wemm  * been scheduled yet.
313a2a1c95cSPeter Wemm  *
314a2a1c95cSPeter Wemm  * This is needed to make kernel threads stay in kernel mode.
315a2a1c95cSPeter Wemm  */
316a2a1c95cSPeter Wemm void
3175c2cf818SKonstantin Belousov cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
318a2a1c95cSPeter Wemm {
319a2a1c95cSPeter Wemm 	/*
320a2a1c95cSPeter Wemm 	 * Note that the trap frame follows the args, so the function
321a2a1c95cSPeter Wemm 	 * is really called like this:  func(arg, frame);
322a2a1c95cSPeter Wemm 	 */
323b40ce416SJulian Elischer 	td->td_pcb->pcb_esi = (int) func;	/* function */
324b40ce416SJulian Elischer 	td->td_pcb->pcb_ebx = (int) arg;	/* first arg */
3255b81b6b3SRodney W. Grimes }
3265b81b6b3SRodney W. Grimes 
3277c2b54e8SNate Williams void
328e602ba25SJulian Elischer cpu_exit(struct thread *td)
329e602ba25SJulian Elischer {
330e602ba25SJulian Elischer 
331bc2e774aSJohn Baldwin 	/*
332bc2e774aSJohn Baldwin 	 * If this process has a custom LDT, release it.  Reset pc->pcb_gs
333bc2e774aSJohn Baldwin 	 * and %gs before we free it in case they refer to an LDT entry.
334bc2e774aSJohn Baldwin 	 */
3350ad5e7f3SJeff Roberson 	mtx_lock_spin(&dt_lock);
336bc2e774aSJohn Baldwin 	if (td->td_proc->p_md.md_ldt) {
33766250360SDavid Xu 		td->td_pcb->pcb_gs = _udatasel;
33866250360SDavid Xu 		load_gs(_udatasel);
339e602ba25SJulian Elischer 		user_ldt_free(td);
34002b0a160SAttilio Rao 	} else
3410ad5e7f3SJeff Roberson 		mtx_unlock_spin(&dt_lock);
342e602ba25SJulian Elischer }
343e602ba25SJulian Elischer 
344e602ba25SJulian Elischer void
345e602ba25SJulian Elischer cpu_thread_exit(struct thread *td)
3465b81b6b3SRodney W. Grimes {
3474cc99cf6SJohn Baldwin 
3481060a94fSKonstantin Belousov 	critical_enter();
349d5e1f581SDavid Xu 	if (td == PCPU_GET(fpcurthread))
350d5e1f581SDavid Xu 		npxdrop();
3511060a94fSKonstantin Belousov 	critical_exit();
352bc2e774aSJohn Baldwin 
353bc2e774aSJohn Baldwin 	/* Disable any hardware breakpoints. */
354bc2e774aSJohn Baldwin 	if (td->td_pcb->pcb_flags & PCB_DBREGS) {
355b19d9defSMaxime Henrion 		reset_dbregs();
356bc2e774aSJohn Baldwin 		td->td_pcb->pcb_flags &= ~PCB_DBREGS;
357b19d9defSMaxime Henrion 	}
358b19d9defSMaxime Henrion }
359b19d9defSMaxime Henrion 
360b19d9defSMaxime Henrion void
361696058c3SJulian Elischer cpu_thread_clean(struct thread *td)
362b19d9defSMaxime Henrion {
363b19d9defSMaxime Henrion 	struct pcb *pcb;
364b19d9defSMaxime Henrion 
365b19d9defSMaxime Henrion 	pcb = td->td_pcb;
366ae3676c4SJohn Baldwin 	if (pcb->pcb_ext != NULL) {
367e602ba25SJulian Elischer 		/* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
36848a09cf2SJohn Dyson 		/*
36948a09cf2SJohn Dyson 		 * XXX do we need to move the TSS off the allocated pages
37048a09cf2SJohn Dyson 		 * before freeing them?  (not done here)
37148a09cf2SJohn Dyson 		 */
372d86c1f0dSKonstantin Belousov 		pmap_trm_free(pcb->pcb_ext, ctob(IOPAGES + 1));
373ae3676c4SJohn Baldwin 		pcb->pcb_ext = NULL;
37448a09cf2SJohn Dyson 	}
3755b81b6b3SRodney W. Grimes }
3765b81b6b3SRodney W. Grimes 
377381fe1aaSGarrett Wollman void
3780c3967e7SMarcel Moolenaar cpu_thread_alloc(struct thread *td)
379e602ba25SJulian Elischer {
380824fc460SJohn Baldwin 	struct pcb *pcb;
381824fc460SJohn Baldwin 	struct xstate_hdr *xhdr;
382e602ba25SJulian Elischer 
383824fc460SJohn Baldwin 	td->td_pcb = pcb = get_pcb_td(td);
384d86c1f0dSKonstantin Belousov 	td->td_frame = (struct trapframe *)((caddr_t)pcb -
385d86c1f0dSKonstantin Belousov 	    VM86_STACK_SPACE) - 1;
386824fc460SJohn Baldwin 	pcb->pcb_ext = NULL;
387824fc460SJohn Baldwin 	pcb->pcb_save = get_pcb_user_save_pcb(pcb);
388824fc460SJohn Baldwin 	if (use_xsave) {
389824fc460SJohn Baldwin 		xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
390824fc460SJohn Baldwin 		bzero(xhdr, sizeof(*xhdr));
391824fc460SJohn Baldwin 		xhdr->xstate_bv = xsave_mask;
392824fc460SJohn Baldwin 	}
393e602ba25SJulian Elischer }
394e602ba25SJulian Elischer 
3950c3967e7SMarcel Moolenaar void
3960c3967e7SMarcel Moolenaar cpu_thread_free(struct thread *td)
3970c3967e7SMarcel Moolenaar {
3980c3967e7SMarcel Moolenaar 
3990c3967e7SMarcel Moolenaar 	cpu_thread_clean(td);
4000c3967e7SMarcel Moolenaar }
4010c3967e7SMarcel Moolenaar 
4026f1fe330SKonstantin Belousov bool
4036f1fe330SKonstantin Belousov cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
4046f1fe330SKonstantin Belousov {
4056f1fe330SKonstantin Belousov 
4066f1fe330SKonstantin Belousov 	return (true);
4076f1fe330SKonstantin Belousov }
4086f1fe330SKonstantin Belousov 
409fd8d844fSKonstantin Belousov int
410fd8d844fSKonstantin Belousov cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
411fd8d844fSKonstantin Belousov     int com __unused, void *data __unused)
412fd8d844fSKonstantin Belousov {
413fd8d844fSKonstantin Belousov 
414fd8d844fSKonstantin Belousov 	return (EINVAL);
415fd8d844fSKonstantin Belousov }
416fd8d844fSKonstantin Belousov 
417a7b89044SKonstantin Belousov void
418a7b89044SKonstantin Belousov cpu_set_syscall_retval(struct thread *td, int error)
419a7b89044SKonstantin Belousov {
420a7b89044SKonstantin Belousov 
421a7b89044SKonstantin Belousov 	switch (error) {
422a7b89044SKonstantin Belousov 	case 0:
423a7b89044SKonstantin Belousov 		td->td_frame->tf_eax = td->td_retval[0];
424a7b89044SKonstantin Belousov 		td->td_frame->tf_edx = td->td_retval[1];
425a7b89044SKonstantin Belousov 		td->td_frame->tf_eflags &= ~PSL_C;
426a7b89044SKonstantin Belousov 		break;
427a7b89044SKonstantin Belousov 
428a7b89044SKonstantin Belousov 	case ERESTART:
429a7b89044SKonstantin Belousov 		/*
430a7b89044SKonstantin Belousov 		 * Reconstruct pc, assuming lcall $X,y is 7 bytes, int
431a7b89044SKonstantin Belousov 		 * 0x80 is 2 bytes. We saved this in tf_err.
432a7b89044SKonstantin Belousov 		 */
433a7b89044SKonstantin Belousov 		td->td_frame->tf_eip -= td->td_frame->tf_err;
434a7b89044SKonstantin Belousov 		break;
435a7b89044SKonstantin Belousov 
436a7b89044SKonstantin Belousov 	case EJUSTRETURN:
437a7b89044SKonstantin Belousov 		break;
438a7b89044SKonstantin Belousov 
439a7b89044SKonstantin Belousov 	default:
440c26391f4SEdward Tomasz Napierala 		td->td_frame->tf_eax = error;
441a7b89044SKonstantin Belousov 		td->td_frame->tf_eflags |= PSL_C;
442a7b89044SKonstantin Belousov 		break;
443a7b89044SKonstantin Belousov 	}
444a7b89044SKonstantin Belousov }
445a7b89044SKonstantin Belousov 
446e602ba25SJulian Elischer /*
4475c2cf818SKonstantin Belousov  * Initialize machine state, mostly pcb and trap frame for a new
4485c2cf818SKonstantin Belousov  * thread, about to return to userspace.  Put enough state in the new
4495c2cf818SKonstantin Belousov  * thread's PCB to get it to go back to the fork_return(), which
4505c2cf818SKonstantin Belousov  * finalizes the thread state and handles peculiarities of the first
4515c2cf818SKonstantin Belousov  * return to userspace for the new thread.
452e602ba25SJulian Elischer  */
453e602ba25SJulian Elischer void
4545c2cf818SKonstantin Belousov cpu_copy_thread(struct thread *td, struct thread *td0)
455e602ba25SJulian Elischer {
456a7883464SJohn Baldwin 	copy_thread(td0, td);
457e602ba25SJulian Elischer 
458e602ba25SJulian Elischer 	/*
45940d593d1SJohn Baldwin 	 * Copy user general-purpose registers.
46040d593d1SJohn Baldwin 	 *
46140d593d1SJohn Baldwin 	 * Some of these registers are rewritten by cpu_set_upcall()
4623b57ddb0SJohn Baldwin 	 * and linux_set_upcall().
463e602ba25SJulian Elischer 	 */
46411e0f8e1SMarcel Moolenaar 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
465e602ba25SJulian Elischer 
4667ce5e15eSDavid Xu 	/* If the current thread has the trap bit set (i.e. a debugger had
4677ce5e15eSDavid Xu 	 * single stepped the process to the system call), we need to clear
4687ce5e15eSDavid Xu 	 * the trap flag from the new frame. Otherwise, the new thread will
4697ce5e15eSDavid Xu 	 * receive a (likely unexpected) SIGTRAP when it executes the first
4707ce5e15eSDavid Xu 	 * instruction after returning to userland.
4717ce5e15eSDavid Xu 	 */
4727ce5e15eSDavid Xu 	td->td_frame->tf_eflags &= ~PSL_T;
473e602ba25SJulian Elischer }
474e602ba25SJulian Elischer 
475e602ba25SJulian Elischer /*
4765c2cf818SKonstantin Belousov  * Set that machine state for performing an upcall that starts
4775c2cf818SKonstantin Belousov  * the entry function with the given argument.
478e602ba25SJulian Elischer  */
479*7b68fb5aSMark Johnston int
4805c2cf818SKonstantin Belousov cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
48121fc3164SDavid Xu     stack_t *stack)
482575525a0SJonathan Mini {
483575525a0SJonathan Mini 
484575525a0SJonathan Mini 	/*
485696058c3SJulian Elischer 	 * Do any extra cleaning that needs to be done.
486696058c3SJulian Elischer 	 * The thread may have optional components
487696058c3SJulian Elischer 	 * that are not present in a fresh thread.
488696058c3SJulian Elischer 	 * This may be a recycled thread so make it look
489696058c3SJulian Elischer 	 * as though it's newly allocated.
490696058c3SJulian Elischer 	 */
491696058c3SJulian Elischer 	cpu_thread_clean(td);
492696058c3SJulian Elischer 
493696058c3SJulian Elischer 	/*
4945c2cf818SKonstantin Belousov 	 * Set the trap frame to point at the beginning of the entry
495575525a0SJonathan Mini 	 * function.
496575525a0SJonathan Mini 	 */
4972396628bSDavid Xu 	td->td_frame->tf_ebp = 0;
498575525a0SJonathan Mini 	td->td_frame->tf_esp =
499fc643048SDavid Xu 	    (((int)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
50021fc3164SDavid Xu 	td->td_frame->tf_eip = (int)entry;
501575525a0SJonathan Mini 
502f236378bSTijl Coosemans 	/* Return address sentinel value to stop stack unwinding. */
503*7b68fb5aSMark Johnston 	if (suword((void *)td->td_frame->tf_esp, 0) != 0)
504*7b68fb5aSMark Johnston 		return (EFAULT);
505f236378bSTijl Coosemans 
5065c2cf818SKonstantin Belousov 	/* Pass the argument to the entry point. */
507*7b68fb5aSMark Johnston 	if (suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
508*7b68fb5aSMark Johnston 	    (int)arg) != 0)
509*7b68fb5aSMark Johnston 		return (EFAULT);
510*7b68fb5aSMark Johnston 	return (0);
51121fc3164SDavid Xu }
51221fc3164SDavid Xu 
513740fd64dSDavid Xu int
51421fc3164SDavid Xu cpu_set_user_tls(struct thread *td, void *tls_base)
51521fc3164SDavid Xu {
51621fc3164SDavid Xu 	struct segment_descriptor sd;
51721fc3164SDavid Xu 	uint32_t base;
51821fc3164SDavid Xu 
51921fc3164SDavid Xu 	/*
52021fc3164SDavid Xu 	 * Construct a descriptor and store it in the pcb for
52121fc3164SDavid Xu 	 * the next context switch.  Also store it in the gdt
52221fc3164SDavid Xu 	 * so that the load of tf_fs into %fs will activate it
52321fc3164SDavid Xu 	 * at return to userland.
52421fc3164SDavid Xu 	 */
52521fc3164SDavid Xu 	base = (uint32_t)tls_base;
52621fc3164SDavid Xu 	sd.sd_lobase = base & 0xffffff;
52721fc3164SDavid Xu 	sd.sd_hibase = (base >> 24) & 0xff;
52821fc3164SDavid Xu 	sd.sd_lolimit = 0xffff;	/* 4GB limit, wraps around */
52921fc3164SDavid Xu 	sd.sd_hilimit = 0xf;
53021fc3164SDavid Xu 	sd.sd_type  = SDT_MEMRWA;
53121fc3164SDavid Xu 	sd.sd_dpl   = SEL_UPL;
53221fc3164SDavid Xu 	sd.sd_p     = 1;
53321fc3164SDavid Xu 	sd.sd_xx    = 0;
53421fc3164SDavid Xu 	sd.sd_def32 = 1;
53521fc3164SDavid Xu 	sd.sd_gran  = 1;
53621fc3164SDavid Xu 	critical_enter();
53721fc3164SDavid Xu 	/* set %gs */
53821fc3164SDavid Xu 	td->td_pcb->pcb_gsd = sd;
53921fc3164SDavid Xu 	if (td == curthread) {
54021fc3164SDavid Xu 		PCPU_GET(fsgs_gdt)[1] = sd;
54121fc3164SDavid Xu 		load_gs(GSEL(GUGS_SEL, SEL_UPL));
54221fc3164SDavid Xu 	}
54321fc3164SDavid Xu 	critical_exit();
544740fd64dSDavid Xu 	return (0);
545e602ba25SJulian Elischer }
546e602ba25SJulian Elischer 
5475b81b6b3SRodney W. Grimes /*
5485b81b6b3SRodney W. Grimes  * Convert kernel VA to physical address
5495b81b6b3SRodney W. Grimes  */
550227f9a1cSJake Burkholder vm_paddr_t
5517f8cb368SDavid Greenman kvtop(void *addr)
5525b81b6b3SRodney W. Grimes {
553227f9a1cSJake Burkholder 	vm_paddr_t pa;
5545b81b6b3SRodney W. Grimes 
555227f9a1cSJake Burkholder 	pa = pmap_kextract((vm_offset_t)addr);
556227f9a1cSJake Burkholder 	if (pa == 0)
5575b81b6b3SRodney W. Grimes 		panic("kvtop: zero page frame");
558227f9a1cSJake Burkholder 	return (pa);
5595b81b6b3SRodney W. Grimes }
5605b81b6b3SRodney W. Grimes 
561e0b78e19SJoerg Wunsch /*
562b1e5dcf7SRobert Watson  * Get an sf_buf from the freelist.  May block if none are available.
563411d10a6SAlan Cox  */
564c8d2ffd6SGleb Smirnoff void
565c8d2ffd6SGleb Smirnoff sf_buf_map(struct sf_buf *sf, int flags)
566411d10a6SAlan Cox {
56720351fafSAlan Cox 
5689a527560SKonstantin Belousov 	pmap_sf_buf_map(sf);
5695c0db7c7SAlan Cox #ifdef SMP
570c8d2ffd6SGleb Smirnoff 	sf_buf_shootdown(sf, flags);
571c8d2ffd6SGleb Smirnoff #endif
572c8d2ffd6SGleb Smirnoff }
573c8d2ffd6SGleb Smirnoff 
574c8d2ffd6SGleb Smirnoff #ifdef SMP
5753b23ffe2SKonstantin Belousov static void
5763b23ffe2SKonstantin Belousov sf_buf_shootdown_curcpu_cb(pmap_t pmap __unused,
5773b23ffe2SKonstantin Belousov     vm_offset_t addr1 __unused, vm_offset_t addr2 __unused)
5783b23ffe2SKonstantin Belousov {
5793b23ffe2SKonstantin Belousov }
5803b23ffe2SKonstantin Belousov 
581c8d2ffd6SGleb Smirnoff void
582c8d2ffd6SGleb Smirnoff sf_buf_shootdown(struct sf_buf *sf, int flags)
583c8d2ffd6SGleb Smirnoff {
584c8d2ffd6SGleb Smirnoff 	cpuset_t other_cpus;
585c8d2ffd6SGleb Smirnoff 	u_int cpuid;
586c8d2ffd6SGleb Smirnoff 
587f6f67ea9SStephan Uphoff 	sched_pin();
588250a44f6SAttilio Rao 	cpuid = PCPU_GET(cpuid);
589250a44f6SAttilio Rao 	if (!CPU_ISSET(cpuid, &sf->cpumask)) {
590250a44f6SAttilio Rao 		CPU_SET(cpuid, &sf->cpumask);
5915c0db7c7SAlan Cox 		invlpg(sf->kva);
5924c0e268aSStephan Uphoff 	}
593f6f67ea9SStephan Uphoff 	if ((flags & SFB_CPUPRIVATE) == 0) {
594250a44f6SAttilio Rao 		other_cpus = all_cpus;
595250a44f6SAttilio Rao 		CPU_CLR(cpuid, &other_cpus);
596e2650af1SStefan Eßer 		CPU_ANDNOT(&other_cpus, &other_cpus, &sf->cpumask);
59771a19bdcSAttilio Rao 		if (!CPU_EMPTY(&other_cpus)) {
598e2650af1SStefan Eßer 			CPU_OR(&sf->cpumask, &sf->cpumask, &other_cpus);
5993b23ffe2SKonstantin Belousov 			smp_masked_invlpg(other_cpus, sf->kva, kernel_pmap,
6003b23ffe2SKonstantin Belousov 			    sf_buf_shootdown_curcpu_cb);
601f6f67ea9SStephan Uphoff 		}
602f6f67ea9SStephan Uphoff 	}
603f6f67ea9SStephan Uphoff 	sched_unpin();
604411d10a6SAlan Cox }
605c8d2ffd6SGleb Smirnoff #endif
606411d10a6SAlan Cox 
607411d10a6SAlan Cox /*
608c8d2ffd6SGleb Smirnoff  * MD part of sf_buf_free().
609411d10a6SAlan Cox  */
610c8d2ffd6SGleb Smirnoff int
611c8d2ffd6SGleb Smirnoff sf_buf_unmap(struct sf_buf *sf)
612411d10a6SAlan Cox {
613ed95805eSJohn Baldwin 
614c8d2ffd6SGleb Smirnoff 	return (0);
6150543fa53SAlan Cox }
616c8d2ffd6SGleb Smirnoff 
617c8d2ffd6SGleb Smirnoff static void
618c8d2ffd6SGleb Smirnoff sf_buf_invalidate(struct sf_buf *sf)
619c8d2ffd6SGleb Smirnoff {
620c8d2ffd6SGleb Smirnoff 	vm_page_t m = sf->m;
621c8d2ffd6SGleb Smirnoff 
622c8d2ffd6SGleb Smirnoff 	/*
623c8d2ffd6SGleb Smirnoff 	 * Use pmap_qenter to update the pte for
624c8d2ffd6SGleb Smirnoff 	 * existing mapping, in particular, the PAT
625c8d2ffd6SGleb Smirnoff 	 * settings are recalculated.
626c8d2ffd6SGleb Smirnoff 	 */
627c8d2ffd6SGleb Smirnoff 	pmap_qenter(sf->kva, &m, 1);
628d12c4465SKonstantin Belousov 	pmap_invalidate_cache_range(sf->kva, sf->kva + PAGE_SIZE);
629c8d2ffd6SGleb Smirnoff }
630c8d2ffd6SGleb Smirnoff 
631c8d2ffd6SGleb Smirnoff /*
632c8d2ffd6SGleb Smirnoff  * Invalidate the cache lines that may belong to the page, if
633c8d2ffd6SGleb Smirnoff  * (possibly old) mapping of the page by sf buffer exists.  Returns
634c8d2ffd6SGleb Smirnoff  * TRUE when mapping was found and cache invalidated.
635c8d2ffd6SGleb Smirnoff  */
636c8d2ffd6SGleb Smirnoff boolean_t
637c8d2ffd6SGleb Smirnoff sf_buf_invalidate_cache(vm_page_t m)
638c8d2ffd6SGleb Smirnoff {
639c8d2ffd6SGleb Smirnoff 
640c8d2ffd6SGleb Smirnoff 	return (sf_buf_process_page(m, sf_buf_invalidate));
641411d10a6SAlan Cox }
642