]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/vm_machdep.c
amd64: stop using top of the thread' kernel stack for FPU user save area
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / vm_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1982, 1986 The Regents of the University of California.
5  * Copyright (c) 1989, 1990 William Jolitz
6  * Copyright (c) 1994 John Dyson
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * the Systems Programming Group of the University of Utah Computer
11  * Science Department, and William Jolitz.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *      from: @(#)vm_machdep.c  7.3 (Berkeley) 5/13/91
42  *      Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
43  */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include "opt_isa.h"
49 #include "opt_cpu.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/kernel.h>
56 #include <sys/ktr.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/mutex.h>
61 #include <sys/priv.h>
62 #include <sys/proc.h>
63 #include <sys/procctl.h>
64 #include <sys/smp.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
69 #include <sys/vmmeter.h>
70 #include <sys/wait.h>
71
72 #include <machine/cpu.h>
73 #include <machine/md_var.h>
74 #include <machine/pcb.h>
75 #include <machine/smp.h>
76 #include <machine/specialreg.h>
77 #include <machine/tss.h>
78
79 #include <vm/vm.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_param.h>
85
86 _Static_assert(OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
87     "OFFSETOF_MONITORBUF does not correspond with offset of pc_monitorbuf.");
88
89 void
90 set_top_of_stack_td(struct thread *td)
91 {
92         td->td_md.md_stack_base = td->td_kstack +
93             td->td_kstack_pages * PAGE_SIZE;
94 }
95
96 struct savefpu *
97 get_pcb_user_save_td(struct thread *td)
98 {
99         KASSERT(((vm_offset_t)td->td_md.md_usr_fpu_save %
100             XSAVE_AREA_ALIGN) == 0,
101             ("Unaligned pcb_user_save area ptr %p td %p",
102             td->td_md.md_usr_fpu_save, td));
103         return (td->td_md.md_usr_fpu_save);
104 }
105
106 struct pcb *
107 get_pcb_td(struct thread *td)
108 {
109
110         return (&td->td_md.md_pcb);
111 }
112
113 struct savefpu *
114 get_pcb_user_save_pcb(struct pcb *pcb)
115 {
116         struct thread *td;
117
118         td = __containerof(pcb, struct thread, td_md.md_pcb);
119         return (get_pcb_user_save_td(td));
120 }
121
122 void *
123 alloc_fpusave(int flags)
124 {
125         void *res;
126         struct savefpu_ymm *sf;
127
128         res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
129         if (use_xsave) {
130                 sf = (struct savefpu_ymm *)res;
131                 bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
132                 sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
133         }
134         return (res);
135 }
136
137 /*
138  * Common code shared between cpu_fork() and cpu_copy_thread() for
139  * initializing a thread.
140  */
141 static void
142 copy_thread(struct thread *td1, struct thread *td2)
143 {
144         struct pcb *pcb2;
145
146         pcb2 = td2->td_pcb;
147
148         /* Ensure that td1's pcb is up to date for user threads. */
149         if ((td2->td_pflags & TDP_KTHREAD) == 0) {
150                 MPASS(td1 == curthread);
151                 fpuexit(td1);
152                 update_pcb_bases(td1->td_pcb);
153         }
154
155         /* Copy td1's pcb */
156         bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
157
158         /* Properly initialize pcb_save */
159         pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
160
161         /* Kernel threads start with clean FPU and segment bases. */
162         if ((td2->td_pflags & TDP_KTHREAD) != 0) {
163                 pcb2->pcb_fsbase = 0;
164                 pcb2->pcb_gsbase = 0;
165                 clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE |
166                     PCB_KERNFPU | PCB_KERNFPU_THR);
167         } else {
168                 MPASS((pcb2->pcb_flags & (PCB_KERNFPU | PCB_KERNFPU_THR)) == 0);
169                 bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
170                     cpu_max_ext_state_size);
171         }
172
173         /*
174          * Set registers for trampoline to user mode.  Leave space for the
175          * return address on stack.  These are the kernel mode register values.
176          */
177         pcb2->pcb_r12 = (register_t)fork_return;        /* fork_trampoline argument */
178         pcb2->pcb_rbp = 0;
179         pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
180         pcb2->pcb_rbx = (register_t)td2;                /* fork_trampoline argument */
181         pcb2->pcb_rip = (register_t)fork_trampoline;
182         /*-
183          * pcb2->pcb_dr*:       cloned above.
184          * pcb2->pcb_savefpu:   cloned above.
185          * pcb2->pcb_flags:     cloned above.
186          * pcb2->pcb_onfault:   cloned above (always NULL here?).
187          * pcb2->pcb_[fg]sbase: cloned above
188          */
189
190         pcb2->pcb_tssp = NULL;
191
192         /* Setup to release spin count in fork_exit(). */
193         td2->td_md.md_spinlock_count = 1;
194         td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
195         pmap_thread_init_invl_gen(td2);
196 }
197
198 /*
199  * Finish a fork operation, with process p2 nearly set up.
200  * Copy and update the pcb, set up the stack so that the child
201  * ready to run and return to user mode.
202  */
203 void
204 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
205 {
206         struct proc *p1;
207         struct pcb *pcb2;
208         struct mdproc *mdp1, *mdp2;
209         struct proc_ldt *pldt;
210
211         p1 = td1->td_proc;
212         if ((flags & RFPROC) == 0) {
213                 if ((flags & RFMEM) == 0) {
214                         /* unshare user LDT */
215                         mdp1 = &p1->p_md;
216                         mtx_lock(&dt_lock);
217                         if ((pldt = mdp1->md_ldt) != NULL &&
218                             pldt->ldt_refcnt > 1 &&
219                             user_ldt_alloc(p1, 1) == NULL)
220                                 panic("could not copy LDT");
221                         mtx_unlock(&dt_lock);
222                 }
223                 return;
224         }
225
226         /* Point the stack and pcb to the actual location */
227         set_top_of_stack_td(td2);
228         td2->td_pcb = pcb2 = get_pcb_td(td2);
229
230         copy_thread(td1, td2);
231
232         /* Reset debug registers in the new process */
233         x86_clear_dbregs(pcb2);
234
235         /* Point mdproc and then copy over p1's contents */
236         mdp2 = &p2->p_md;
237         bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
238
239         /*
240          * Copy the trap frame for the return to user mode as if from a
241          * syscall.  This copies most of the user mode register values.
242          */
243         td2->td_frame = (struct trapframe *)td2->td_md.md_stack_base - 1;
244         bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
245
246         /* Set child return values. */
247         p2->p_sysent->sv_set_fork_retval(td2);
248
249         /*
250          * If the parent process has the trap bit set (i.e. a debugger
251          * had single stepped the process to the system call), we need
252          * to clear the trap flag from the new frame.
253          */
254         td2->td_frame->tf_rflags &= ~PSL_T;
255
256         /* As on i386, do not copy io permission bitmap. */
257         pcb2->pcb_tssp = NULL;
258
259         /* New segment registers. */
260         set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
261
262         /* Copy the LDT, if necessary. */
263         mdp1 = &td1->td_proc->p_md;
264         mdp2 = &p2->p_md;
265         if (mdp1->md_ldt == NULL) {
266                 mdp2->md_ldt = NULL;
267                 return;
268         }
269         mtx_lock(&dt_lock);
270         if (mdp1->md_ldt != NULL) {
271                 if (flags & RFMEM) {
272                         mdp1->md_ldt->ldt_refcnt++;
273                         mdp2->md_ldt = mdp1->md_ldt;
274                         bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
275                             system_segment_descriptor));
276                 } else {
277                         mdp2->md_ldt = NULL;
278                         mdp2->md_ldt = user_ldt_alloc(p2, 0);
279                         if (mdp2->md_ldt == NULL)
280                                 panic("could not copy LDT");
281                         amd64_set_ldt_data(td2, 0, max_ldt_segment,
282                             (struct user_segment_descriptor *)
283                             mdp1->md_ldt->ldt_base);
284                 }
285         } else
286                 mdp2->md_ldt = NULL;
287         mtx_unlock(&dt_lock);
288
289         /*
290          * Now, cpu_switch() can schedule the new process.
291          * pcb_rsp is loaded pointing to the cpu_switch() stack frame
292          * containing the return address when exiting cpu_switch.
293          * This will normally be to fork_trampoline(), which will have
294          * %rbx loaded with the new proc's pointer.  fork_trampoline()
295          * will set up a stack to call fork_return(p, frame); to complete
296          * the return to user-mode.
297          */
298 }
299
300 void
301 x86_set_fork_retval(struct thread *td)
302 {
303         struct trapframe *frame = td->td_frame;
304
305         frame->tf_rax = 0;              /* Child returns zero */
306         frame->tf_rflags &= ~PSL_C;     /* success */
307         frame->tf_rdx = 1;              /* System V emulation */
308 }
309
310 /*
311  * Intercept the return address from a freshly forked process that has NOT
312  * been scheduled yet.
313  *
314  * This is needed to make kernel threads stay in kernel mode.
315  */
316 void
317 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
318 {
319         /*
320          * Note that the trap frame follows the args, so the function
321          * is really called like this:  func(arg, frame);
322          */
323         td->td_pcb->pcb_r12 = (long) func;      /* function */
324         td->td_pcb->pcb_rbx = (long) arg;       /* first arg */
325 }
326
327 void
328 cpu_exit(struct thread *td)
329 {
330
331         /*
332          * If this process has a custom LDT, release it.
333          */
334         if (td->td_proc->p_md.md_ldt != NULL)
335                 user_ldt_free(td);
336 }
337
338 void
339 cpu_thread_exit(struct thread *td)
340 {
341         struct pcb *pcb;
342
343         critical_enter();
344         if (td == PCPU_GET(fpcurthread))
345                 fpudrop();
346         critical_exit();
347
348         pcb = td->td_pcb;
349
350         /* Disable any hardware breakpoints. */
351         if (pcb->pcb_flags & PCB_DBREGS) {
352                 reset_dbregs();
353                 clear_pcb_flags(pcb, PCB_DBREGS);
354         }
355 }
356
357 void
358 cpu_thread_clean(struct thread *td)
359 {
360         struct pcb *pcb;
361
362         pcb = td->td_pcb;
363
364         /*
365          * Clean TSS/iomap
366          */
367         if (pcb->pcb_tssp != NULL) {
368                 pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
369                     (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
370                 kmem_free((vm_offset_t)pcb->pcb_tssp, ctob(IOPAGES + 1));
371                 pcb->pcb_tssp = NULL;
372         }
373 }
374
375 void
376 cpu_thread_swapin(struct thread *td)
377 {
378 }
379
380 void
381 cpu_thread_swapout(struct thread *td)
382 {
383 }
384
385 void
386 cpu_thread_alloc(struct thread *td)
387 {
388         struct pcb *pcb;
389         struct xstate_hdr *xhdr;
390
391         set_top_of_stack_td(td);
392         td->td_pcb = pcb = get_pcb_td(td);
393         td->td_frame = (struct trapframe *)td->td_md.md_stack_base - 1;
394         td->td_md.md_usr_fpu_save = fpu_save_area_alloc();
395         td->td_md.md_fpu_scratch = fpu_save_area_alloc();
396         pcb->pcb_save = get_pcb_user_save_pcb(pcb);
397         if (use_xsave) {
398                 xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
399                 bzero(xhdr, sizeof(*xhdr));
400                 xhdr->xstate_bv = xsave_mask;
401         }
402 }
403
404 void
405 cpu_thread_free(struct thread *td)
406 {
407         cpu_thread_clean(td);
408
409         fpu_save_area_free(td->td_md.md_usr_fpu_save);
410         td->td_md.md_usr_fpu_save = NULL;
411         fpu_save_area_free(td->td_md.md_fpu_scratch);
412         td->td_md.md_fpu_scratch = NULL;
413 }
414
415 bool
416 cpu_exec_vmspace_reuse(struct proc *p, vm_map_t map)
417 {
418
419         return (((curproc->p_md.md_flags & P_MD_KPTI) != 0) ==
420             (vm_map_pmap(map)->pm_ucr3 != PMAP_NO_CR3));
421 }
422
423 static void
424 cpu_procctl_kpti_ctl(struct proc *p, int val)
425 {
426
427         if (pti && val == PROC_KPTI_CTL_ENABLE_ON_EXEC)
428                 p->p_md.md_flags |= P_MD_KPTI;
429         if (val == PROC_KPTI_CTL_DISABLE_ON_EXEC)
430                 p->p_md.md_flags &= ~P_MD_KPTI;
431 }
432
433 static void
434 cpu_procctl_kpti_status(struct proc *p, int *val)
435 {
436         *val = (p->p_md.md_flags & P_MD_KPTI) != 0 ?
437             PROC_KPTI_CTL_ENABLE_ON_EXEC:
438             PROC_KPTI_CTL_DISABLE_ON_EXEC;
439         if (vmspace_pmap(p->p_vmspace)->pm_ucr3 != PMAP_NO_CR3)
440                 *val |= PROC_KPTI_STATUS_ACTIVE;
441 }
442
443 static int
444 cpu_procctl_la_ctl(struct proc *p, int val)
445 {
446         int error;
447
448         error = 0;
449         switch (val) {
450         case PROC_LA_CTL_LA48_ON_EXEC:
451                 p->p_md.md_flags |= P_MD_LA48;
452                 p->p_md.md_flags &= ~P_MD_LA57;
453                 break;
454         case PROC_LA_CTL_LA57_ON_EXEC:
455                 if (la57) {
456                         p->p_md.md_flags &= ~P_MD_LA48;
457                         p->p_md.md_flags |= P_MD_LA57;
458                 } else {
459                         error = ENOTSUP;
460                 }
461                 break;
462         case PROC_LA_CTL_DEFAULT_ON_EXEC:
463                 p->p_md.md_flags &= ~(P_MD_LA48 | P_MD_LA57);
464                 break;
465         }
466         return (error);
467 }
468
469 static void
470 cpu_procctl_la_status(struct proc *p, int *val)
471 {
472         int res;
473
474         if ((p->p_md.md_flags & P_MD_LA48) != 0)
475                 res = PROC_LA_CTL_LA48_ON_EXEC;
476         else if ((p->p_md.md_flags & P_MD_LA57) != 0)
477                 res = PROC_LA_CTL_LA57_ON_EXEC;
478         else
479                 res = PROC_LA_CTL_DEFAULT_ON_EXEC;
480         if (p->p_sysent->sv_maxuser == VM_MAXUSER_ADDRESS_LA48)
481                 res |= PROC_LA_STATUS_LA48;
482         else
483                 res |= PROC_LA_STATUS_LA57;
484         *val = res;
485 }
486
487 int
488 cpu_procctl(struct thread *td, int idtype, id_t id, int com, void *data)
489 {
490         struct proc *p;
491         int error, val;
492
493         switch (com) {
494         case PROC_KPTI_CTL:
495         case PROC_KPTI_STATUS:
496         case PROC_LA_CTL:
497         case PROC_LA_STATUS:
498                 if (idtype != P_PID) {
499                         error = EINVAL;
500                         break;
501                 }
502                 if (com == PROC_KPTI_CTL) {
503                         /* sad but true and not a joke */
504                         error = priv_check(td, PRIV_IO);
505                         if (error != 0)
506                                 break;
507                 }
508                 if (com == PROC_KPTI_CTL || com == PROC_LA_CTL) {
509                         error = copyin(data, &val, sizeof(val));
510                         if (error != 0)
511                                 break;
512                 }
513                 if (com == PROC_KPTI_CTL &&
514                     val != PROC_KPTI_CTL_ENABLE_ON_EXEC &&
515                     val != PROC_KPTI_CTL_DISABLE_ON_EXEC) {
516                         error = EINVAL;
517                         break;
518                 }
519                 if (com == PROC_LA_CTL &&
520                     val != PROC_LA_CTL_LA48_ON_EXEC &&
521                     val != PROC_LA_CTL_LA57_ON_EXEC &&
522                     val != PROC_LA_CTL_DEFAULT_ON_EXEC) {
523                         error = EINVAL;
524                         break;
525                 }
526                 error = pget(id, PGET_CANSEE | PGET_NOTWEXIT | PGET_NOTID, &p);
527                 if (error != 0)
528                         break;
529                 switch (com) {
530                 case PROC_KPTI_CTL:
531                         cpu_procctl_kpti_ctl(p, val);
532                         break;
533                 case PROC_KPTI_STATUS:
534                         cpu_procctl_kpti_status(p, &val);
535                         break;
536                 case PROC_LA_CTL:
537                         error = cpu_procctl_la_ctl(p, val);
538                         break;
539                 case PROC_LA_STATUS:
540                         cpu_procctl_la_status(p, &val);
541                         break;
542                 }
543                 PROC_UNLOCK(p);
544                 if (com == PROC_KPTI_STATUS || com == PROC_LA_STATUS)
545                         error = copyout(&val, data, sizeof(val));
546                 break;
547         default:
548                 error = EINVAL;
549                 break;
550         }
551         return (error);
552 }
553
554 void
555 cpu_set_syscall_retval(struct thread *td, int error)
556 {
557         struct trapframe *frame;
558
559         frame = td->td_frame;
560         if (__predict_true(error == 0)) {
561                 frame->tf_rax = td->td_retval[0];
562                 frame->tf_rdx = td->td_retval[1];
563                 frame->tf_rflags &= ~PSL_C;
564                 return;
565         }
566
567         switch (error) {
568         case ERESTART:
569                 /*
570                  * Reconstruct pc, we know that 'syscall' is 2 bytes,
571                  * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
572                  * We saved this in tf_err.
573                  * %r10 (which was holding the value of %rcx) is restored
574                  * for the next iteration.
575                  * %r10 restore is only required for freebsd/amd64 processes,
576                  * but shall be innocent for any ia32 ABI.
577                  *
578                  * Require full context restore to get the arguments
579                  * in the registers reloaded at return to usermode.
580                  */
581                 frame->tf_rip -= frame->tf_err;
582                 frame->tf_r10 = frame->tf_rcx;
583                 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
584                 break;
585
586         case EJUSTRETURN:
587                 break;
588
589         default:
590                 frame->tf_rax = error;
591                 frame->tf_rflags |= PSL_C;
592                 break;
593         }
594 }
595
596 /*
597  * Initialize machine state, mostly pcb and trap frame for a new
598  * thread, about to return to userspace.  Put enough state in the new
599  * thread's PCB to get it to go back to the fork_return(), which
600  * finalizes the thread state and handles peculiarities of the first
601  * return to userspace for the new thread.
602  */
603 void
604 cpu_copy_thread(struct thread *td, struct thread *td0)
605 {
606         copy_thread(td0, td);
607
608         /*
609          * Copy user general-purpose registers.
610          *
611          * Some of these registers are rewritten by cpu_set_upcall()
612          * and linux_set_upcall().
613          */
614         bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
615
616         /* If the current thread has the trap bit set (i.e. a debugger had
617          * single stepped the process to the system call), we need to clear
618          * the trap flag from the new frame. Otherwise, the new thread will
619          * receive a (likely unexpected) SIGTRAP when it executes the first
620          * instruction after returning to userland.
621          */
622         td->td_frame->tf_rflags &= ~PSL_T;
623
624         set_pcb_flags_raw(td->td_pcb, PCB_FULL_IRET);
625 }
626
627 /*
628  * Set that machine state for performing an upcall that starts
629  * the entry function with the given argument.
630  */
631 void
632 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
633     stack_t *stack)
634 {
635
636         /* 
637          * Do any extra cleaning that needs to be done.
638          * The thread may have optional components
639          * that are not present in a fresh thread.
640          * This may be a recycled thread so make it look
641          * as though it's newly allocated.
642          */
643         cpu_thread_clean(td);
644
645 #ifdef COMPAT_FREEBSD32
646         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
647                 /*
648                  * Set the trap frame to point at the beginning of the entry
649                  * function.
650                  */
651                 td->td_frame->tf_rbp = 0;
652                 td->td_frame->tf_rsp =
653                    (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
654                 td->td_frame->tf_rip = (uintptr_t)entry;
655
656                 /* Return address sentinel value to stop stack unwinding. */
657                 suword32((void *)td->td_frame->tf_rsp, 0);
658
659                 /* Pass the argument to the entry point. */
660                 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
661                     (uint32_t)(uintptr_t)arg);
662
663                 return;
664         }
665 #endif
666
667         /*
668          * Set the trap frame to point at the beginning of the uts
669          * function.
670          */
671         td->td_frame->tf_rbp = 0;
672         td->td_frame->tf_rsp =
673             ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
674         td->td_frame->tf_rsp -= 8;
675         td->td_frame->tf_rip = (register_t)entry;
676         td->td_frame->tf_ds = _udatasel;
677         td->td_frame->tf_es = _udatasel;
678         td->td_frame->tf_fs = _ufssel;
679         td->td_frame->tf_gs = _ugssel;
680         td->td_frame->tf_flags = TF_HASSEGS;
681
682         /* Return address sentinel value to stop stack unwinding. */
683         suword((void *)td->td_frame->tf_rsp, 0);
684
685         /* Pass the argument to the entry point. */
686         td->td_frame->tf_rdi = (register_t)arg;
687 }
688
689 int
690 cpu_set_user_tls(struct thread *td, void *tls_base)
691 {
692         struct pcb *pcb;
693
694         if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
695                 return (EINVAL);
696
697         pcb = td->td_pcb;
698         set_pcb_flags(pcb, PCB_FULL_IRET);
699 #ifdef COMPAT_FREEBSD32
700         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
701                 pcb->pcb_gsbase = (register_t)tls_base;
702                 return (0);
703         }
704 #endif
705         pcb->pcb_fsbase = (register_t)tls_base;
706         return (0);
707 }
708
709 /*
710  * Software interrupt handler for queued VM system processing.
711  */   
712 void  
713 swi_vm(void *dummy) 
714 {     
715         if (busdma_swi_pending != 0)
716                 busdma_swi();
717 }
718
719 /*
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.
724  */
725
726 int
727 is_physical_memory(vm_paddr_t addr)
728 {
729
730 #ifdef DEV_ISA
731         /* The ISA ``memory hole''. */
732         if (addr >= 0xa0000 && addr < 0x100000)
733                 return 0;
734 #endif
735
736         /*
737          * stuff other tests for known memory-mapped devices (PCI?)
738          * here
739          */
740
741         return 1;
742 }