2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 Neel Natu <neel@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/_iovec.h>
37 #include <x86/segments.h>
38 #include <x86/specialreg.h>
39 #include <machine/vmm.h>
40 #include <machine/vmm_instruction_emul.h>
54 * Using 'struct i386tss' is tempting but causes myriad sign extension
55 * issues because all of its fields are defined as signed integers.
97 static_assert(sizeof(struct tss32) == 104, "compile-time assertion failed");
99 #define SEL_START(sel) (((sel) & ~0x7))
100 #define SEL_LIMIT(sel) (((sel) | 0x7))
101 #define TSS_BUSY(type) (((type) & 0x2) != 0)
104 GETREG(struct vmctx *ctx, int vcpu, int reg)
109 error = vm_get_register(ctx, vcpu, reg, &val);
115 SETREG(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
119 error = vm_set_register(ctx, vcpu, reg, val);
123 static struct seg_desc
124 usd_to_seg_desc(struct user_segment_descriptor *usd)
126 struct seg_desc seg_desc;
128 seg_desc.base = (u_int)USD_GETBASE(usd);
130 seg_desc.limit = (u_int)(USD_GETLIMIT(usd) << 12) | 0xfff;
132 seg_desc.limit = (u_int)USD_GETLIMIT(usd);
133 seg_desc.access = usd->sd_type | usd->sd_dpl << 5 | usd->sd_p << 7;
134 seg_desc.access |= usd->sd_xx << 12;
135 seg_desc.access |= usd->sd_def32 << 14;
136 seg_desc.access |= usd->sd_gran << 15;
142 * Inject an exception with an error code that is a segment selector.
143 * The format of the error code is described in section 6.13, "Error Code",
144 * Intel SDM volume 3.
146 * Bit 0 (EXT) denotes whether the exception occurred during delivery
147 * of an external event like an interrupt.
149 * Bit 1 (IDT) indicates whether the selector points to a gate descriptor
152 * Bit 2(GDT/LDT) has the usual interpretation of Table Indicator (TI).
155 sel_exception(struct vmctx *ctx, int vcpu, int vector, uint16_t sel, int ext)
158 * Bit 2 from the selector is retained as-is in the error code.
160 * Bit 1 can be safely cleared because none of the selectors
161 * encountered during task switch emulation refer to a task
164 * Bit 0 is set depending on the value of 'ext'.
169 vm_inject_fault(ctx, vcpu, vector, 1, sel);
173 * Return 0 if the selector 'sel' in within the limits of the GDT/LDT
174 * and non-zero otherwise.
177 desc_table_limit_check(struct vmctx *ctx, int vcpu, uint16_t sel)
180 uint32_t limit, access;
183 reg = ISLDT(sel) ? VM_REG_GUEST_LDTR : VM_REG_GUEST_GDTR;
184 error = vm_get_desc(ctx, vcpu, reg, &base, &limit, &access);
187 if (reg == VM_REG_GUEST_LDTR) {
188 if (SEG_DESC_UNUSABLE(access) || !SEG_DESC_PRESENT(access))
192 if (limit < SEL_LIMIT(sel))
199 * Read/write the segment descriptor 'desc' into the GDT/LDT slot referenced
200 * by the selector 'sel'.
202 * Returns 0 on success.
203 * Returns 1 if an exception was injected into the guest.
204 * Returns -1 otherwise.
207 desc_table_rw(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
208 uint16_t sel, struct user_segment_descriptor *desc, bool doread,
213 uint32_t limit, access;
216 reg = ISLDT(sel) ? VM_REG_GUEST_LDTR : VM_REG_GUEST_GDTR;
217 error = vm_get_desc(ctx, vcpu, reg, &base, &limit, &access);
219 assert(limit >= SEL_LIMIT(sel));
221 error = vm_copy_setup(ctx, vcpu, paging, base + SEL_START(sel),
222 sizeof(*desc), doread ? PROT_READ : PROT_WRITE, iov, nitems(iov),
224 if (error || *faultptr)
228 vm_copyin(ctx, vcpu, iov, desc, sizeof(*desc));
230 vm_copyout(ctx, vcpu, desc, iov, sizeof(*desc));
235 desc_table_read(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
236 uint16_t sel, struct user_segment_descriptor *desc, int *faultptr)
238 return (desc_table_rw(ctx, vcpu, paging, sel, desc, true, faultptr));
242 desc_table_write(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
243 uint16_t sel, struct user_segment_descriptor *desc, int *faultptr)
245 return (desc_table_rw(ctx, vcpu, paging, sel, desc, false, faultptr));
249 * Read the TSS descriptor referenced by 'sel' into 'desc'.
251 * Returns 0 on success.
252 * Returns 1 if an exception was injected into the guest.
253 * Returns -1 otherwise.
256 read_tss_descriptor(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts,
257 uint16_t sel, struct user_segment_descriptor *desc, int *faultptr)
259 struct vm_guest_paging sup_paging;
263 assert(IDXSEL(sel) != 0);
265 /* Fetch the new TSS descriptor */
266 if (desc_table_limit_check(ctx, vcpu, sel)) {
267 if (ts->reason == TSR_IRET)
268 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
270 sel_exception(ctx, vcpu, IDT_GP, sel, ts->ext);
274 sup_paging = ts->paging;
275 sup_paging.cpl = 0; /* implicit supervisor mode */
276 error = desc_table_read(ctx, vcpu, &sup_paging, sel, desc, faultptr);
281 code_desc(int sd_type)
283 /* code descriptor */
284 return ((sd_type & 0x18) == 0x18);
288 stack_desc(int sd_type)
290 /* writable data descriptor */
291 return ((sd_type & 0x1A) == 0x12);
295 data_desc(int sd_type)
297 /* data descriptor or a readable code descriptor */
298 return ((sd_type & 0x18) == 0x10 || (sd_type & 0x1A) == 0x1A);
302 ldt_desc(int sd_type)
305 return (sd_type == SDT_SYSLDT);
309 * Validate the descriptor 'seg_desc' associated with 'segment'.
312 validate_seg_desc(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts,
313 int segment, struct seg_desc *seg_desc, int *faultptr)
315 struct vm_guest_paging sup_paging;
316 struct user_segment_descriptor usd;
320 bool ldtseg, codeseg, stackseg, dataseg, conforming;
322 ldtseg = codeseg = stackseg = dataseg = false;
324 case VM_REG_GUEST_LDTR:
327 case VM_REG_GUEST_CS:
330 case VM_REG_GUEST_SS:
333 case VM_REG_GUEST_DS:
334 case VM_REG_GUEST_ES:
335 case VM_REG_GUEST_FS:
336 case VM_REG_GUEST_GS:
343 /* Get the segment selector */
344 sel = GETREG(ctx, vcpu, segment);
346 /* LDT selector must point into the GDT */
347 if (ldtseg && ISLDT(sel)) {
348 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
352 /* Descriptor table limit check */
353 if (desc_table_limit_check(ctx, vcpu, sel)) {
354 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
359 if (IDXSEL(sel) == 0) {
360 /* Code and stack segment selectors cannot be NULL */
361 if (codeseg || stackseg) {
362 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
367 seg_desc->access = 0x10000; /* unusable */
371 /* Read the descriptor from the GDT/LDT */
372 sup_paging = ts->paging;
373 sup_paging.cpl = 0; /* implicit supervisor mode */
374 error = desc_table_read(ctx, vcpu, &sup_paging, sel, &usd, faultptr);
375 if (error || *faultptr)
378 /* Verify that the descriptor type is compatible with the segment */
379 if ((ldtseg && !ldt_desc(usd.sd_type)) ||
380 (codeseg && !code_desc(usd.sd_type)) ||
381 (dataseg && !data_desc(usd.sd_type)) ||
382 (stackseg && !stack_desc(usd.sd_type))) {
383 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
387 /* Segment must be marked present */
395 sel_exception(ctx, vcpu, idtvec, sel, ts->ext);
399 cs = GETREG(ctx, vcpu, VM_REG_GUEST_CS);
400 cpl = cs & SEL_RPL_MASK;
401 rpl = sel & SEL_RPL_MASK;
404 if (stackseg && (rpl != cpl || dpl != cpl)) {
405 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
410 conforming = (usd.sd_type & 0x4) ? true : false;
411 if ((conforming && (cpl < dpl)) ||
412 (!conforming && (cpl != dpl))) {
413 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
420 * A data segment is always non-conforming except when it's
421 * descriptor is a readable, conforming code segment.
423 if (code_desc(usd.sd_type) && (usd.sd_type & 0x4) != 0)
428 if (!conforming && (rpl > dpl || cpl > dpl)) {
429 sel_exception(ctx, vcpu, IDT_TS, sel, ts->ext);
433 *seg_desc = usd_to_seg_desc(&usd);
438 tss32_save(struct vmctx *ctx, int vcpu, struct vm_task_switch *task_switch,
439 uint32_t eip, struct tss32 *tss, struct iovec *iov)
442 /* General purpose registers */
443 tss->tss_eax = GETREG(ctx, vcpu, VM_REG_GUEST_RAX);
444 tss->tss_ecx = GETREG(ctx, vcpu, VM_REG_GUEST_RCX);
445 tss->tss_edx = GETREG(ctx, vcpu, VM_REG_GUEST_RDX);
446 tss->tss_ebx = GETREG(ctx, vcpu, VM_REG_GUEST_RBX);
447 tss->tss_esp = GETREG(ctx, vcpu, VM_REG_GUEST_RSP);
448 tss->tss_ebp = GETREG(ctx, vcpu, VM_REG_GUEST_RBP);
449 tss->tss_esi = GETREG(ctx, vcpu, VM_REG_GUEST_RSI);
450 tss->tss_edi = GETREG(ctx, vcpu, VM_REG_GUEST_RDI);
452 /* Segment selectors */
453 tss->tss_es = GETREG(ctx, vcpu, VM_REG_GUEST_ES);
454 tss->tss_cs = GETREG(ctx, vcpu, VM_REG_GUEST_CS);
455 tss->tss_ss = GETREG(ctx, vcpu, VM_REG_GUEST_SS);
456 tss->tss_ds = GETREG(ctx, vcpu, VM_REG_GUEST_DS);
457 tss->tss_fs = GETREG(ctx, vcpu, VM_REG_GUEST_FS);
458 tss->tss_gs = GETREG(ctx, vcpu, VM_REG_GUEST_GS);
461 tss->tss_eflags = GETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS);
462 if (task_switch->reason == TSR_IRET)
463 tss->tss_eflags &= ~PSL_NT;
466 /* Copy updated old TSS into guest memory */
467 vm_copyout(ctx, vcpu, tss, iov, sizeof(struct tss32));
471 update_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *sd)
475 error = vm_set_desc(ctx, vcpu, reg, sd->base, sd->limit, sd->access);
480 * Update the vcpu registers to reflect the state of the new task.
483 tss32_restore(struct vmctx *ctx, int vcpu, struct vm_task_switch *ts,
484 uint16_t ot_sel, struct tss32 *tss, struct iovec *iov, int *faultptr)
486 struct seg_desc seg_desc, seg_desc2;
487 uint64_t *pdpte, maxphyaddr, reserved;
493 if (ts->reason != TSR_IRET && ts->reason != TSR_JMP) {
494 tss->tss_link = ot_sel;
498 eflags = tss->tss_eflags;
503 SETREG(ctx, vcpu, VM_REG_GUEST_LDTR, tss->tss_ldt);
506 if (ts->paging.paging_mode != PAGING_MODE_FLAT) {
507 if (ts->paging.paging_mode == PAGING_MODE_PAE) {
509 * XXX Assuming 36-bit MAXPHYADDR.
511 maxphyaddr = (1UL << 36) - 1;
512 pdpte = paddr_guest2host(ctx, tss->tss_cr3 & ~0x1f, 32);
513 for (i = 0; i < 4; i++) {
514 /* Check reserved bits if the PDPTE is valid */
515 if (!(pdpte[i] & 0x1))
518 * Bits 2:1, 8:5 and bits above the processor's
519 * maximum physical address are reserved.
521 reserved = ~maxphyaddr | 0x1E6;
522 if (pdpte[i] & reserved) {
523 vm_inject_gp(ctx, vcpu);
527 SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE0, pdpte[0]);
528 SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE1, pdpte[1]);
529 SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE2, pdpte[2]);
530 SETREG(ctx, vcpu, VM_REG_GUEST_PDPTE3, pdpte[3]);
532 SETREG(ctx, vcpu, VM_REG_GUEST_CR3, tss->tss_cr3);
533 ts->paging.cr3 = tss->tss_cr3;
537 SETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS, eflags);
538 SETREG(ctx, vcpu, VM_REG_GUEST_RIP, tss->tss_eip);
540 /* General purpose registers */
541 SETREG(ctx, vcpu, VM_REG_GUEST_RAX, tss->tss_eax);
542 SETREG(ctx, vcpu, VM_REG_GUEST_RCX, tss->tss_ecx);
543 SETREG(ctx, vcpu, VM_REG_GUEST_RDX, tss->tss_edx);
544 SETREG(ctx, vcpu, VM_REG_GUEST_RBX, tss->tss_ebx);
545 SETREG(ctx, vcpu, VM_REG_GUEST_RSP, tss->tss_esp);
546 SETREG(ctx, vcpu, VM_REG_GUEST_RBP, tss->tss_ebp);
547 SETREG(ctx, vcpu, VM_REG_GUEST_RSI, tss->tss_esi);
548 SETREG(ctx, vcpu, VM_REG_GUEST_RDI, tss->tss_edi);
550 /* Segment selectors */
551 SETREG(ctx, vcpu, VM_REG_GUEST_ES, tss->tss_es);
552 SETREG(ctx, vcpu, VM_REG_GUEST_CS, tss->tss_cs);
553 SETREG(ctx, vcpu, VM_REG_GUEST_SS, tss->tss_ss);
554 SETREG(ctx, vcpu, VM_REG_GUEST_DS, tss->tss_ds);
555 SETREG(ctx, vcpu, VM_REG_GUEST_FS, tss->tss_fs);
556 SETREG(ctx, vcpu, VM_REG_GUEST_GS, tss->tss_gs);
559 * If this is a nested task then write out the new TSS to update
560 * the previous link field.
563 vm_copyout(ctx, vcpu, tss, iov, sizeof(*tss));
565 /* Validate segment descriptors */
566 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_LDTR, &seg_desc,
568 if (error || *faultptr)
570 update_seg_desc(ctx, vcpu, VM_REG_GUEST_LDTR, &seg_desc);
573 * Section "Checks on Guest Segment Registers", Intel SDM, Vol 3.
575 * The SS and CS attribute checks on VM-entry are inter-dependent so
576 * we need to make sure that both segments are valid before updating
577 * either of them. This ensures that the VMCS state can pass the
578 * VM-entry checks so the guest can handle any exception injected
579 * during task switch emulation.
581 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_CS, &seg_desc,
583 if (error || *faultptr)
586 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_SS, &seg_desc2,
588 if (error || *faultptr)
590 update_seg_desc(ctx, vcpu, VM_REG_GUEST_CS, &seg_desc);
591 update_seg_desc(ctx, vcpu, VM_REG_GUEST_SS, &seg_desc2);
592 ts->paging.cpl = tss->tss_cs & SEL_RPL_MASK;
594 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_DS, &seg_desc,
596 if (error || *faultptr)
598 update_seg_desc(ctx, vcpu, VM_REG_GUEST_DS, &seg_desc);
600 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_ES, &seg_desc,
602 if (error || *faultptr)
604 update_seg_desc(ctx, vcpu, VM_REG_GUEST_ES, &seg_desc);
606 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_FS, &seg_desc,
608 if (error || *faultptr)
610 update_seg_desc(ctx, vcpu, VM_REG_GUEST_FS, &seg_desc);
612 error = validate_seg_desc(ctx, vcpu, ts, VM_REG_GUEST_GS, &seg_desc,
614 if (error || *faultptr)
616 update_seg_desc(ctx, vcpu, VM_REG_GUEST_GS, &seg_desc);
622 * Push an error code on the stack of the new task. This is needed if the
623 * task switch was triggered by a hardware exception that causes an error
624 * code to be saved (e.g. #PF).
627 push_errcode(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
628 int task_type, uint32_t errcode, int *faultptr)
631 struct seg_desc seg_desc;
632 int stacksize, bytes, error;
633 uint64_t gla, cr0, rflags;
639 cr0 = GETREG(ctx, vcpu, VM_REG_GUEST_CR0);
640 rflags = GETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS);
641 stacksel = GETREG(ctx, vcpu, VM_REG_GUEST_SS);
643 error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS, &seg_desc.base,
644 &seg_desc.limit, &seg_desc.access);
648 * Section "Error Code" in the Intel SDM vol 3: the error code is
649 * pushed on the stack as a doubleword or word (depending on the
650 * default interrupt, trap or task gate size).
652 if (task_type == SDT_SYS386BSY || task_type == SDT_SYS386TSS)
658 * PUSH instruction from Intel SDM vol 2: the 'B' flag in the
659 * stack-segment descriptor determines the size of the stack
660 * pointer outside of 64-bit mode.
662 if (SEG_DESC_DEF32(seg_desc.access))
667 esp = GETREG(ctx, vcpu, VM_REG_GUEST_RSP);
670 if (vie_calculate_gla(paging->cpu_mode, VM_REG_GUEST_SS,
671 &seg_desc, esp, bytes, stacksize, PROT_WRITE, &gla)) {
672 sel_exception(ctx, vcpu, IDT_SS, stacksel, 1);
677 if (vie_alignment_check(paging->cpl, bytes, cr0, rflags, gla)) {
678 vm_inject_ac(ctx, vcpu, 1);
683 error = vm_copy_setup(ctx, vcpu, paging, gla, bytes, PROT_WRITE,
684 iov, nitems(iov), faultptr);
685 if (error || *faultptr)
688 vm_copyout(ctx, vcpu, &errcode, iov, bytes);
689 SETREG(ctx, vcpu, VM_REG_GUEST_RSP, esp);
694 * Evaluate return value from helper functions and potentially return to
697 #define CHKERR(error,fault) \
699 assert((error == 0) || (error == EFAULT)); \
701 return (VMEXIT_ABORT); \
703 return (VMEXIT_CONTINUE); \
707 vmexit_task_switch(struct vmctx *ctx, struct vm_exit *vmexit, int *pvcpu)
710 struct tss32 oldtss, newtss;
711 struct vm_task_switch *task_switch;
712 struct vm_guest_paging *paging, sup_paging;
713 struct user_segment_descriptor nt_desc, ot_desc;
714 struct iovec nt_iov[2], ot_iov[2];
715 uint64_t cr0, ot_base;
716 uint32_t eip, ot_lim, access;
717 int error, ext, fault, minlimit, nt_type, ot_type, vcpu;
718 enum task_switch_reason reason;
719 uint16_t nt_sel, ot_sel;
721 task_switch = &vmexit->u.task_switch;
722 nt_sel = task_switch->tsssel;
723 ext = vmexit->u.task_switch.ext;
724 reason = vmexit->u.task_switch.reason;
725 paging = &vmexit->u.task_switch.paging;
728 assert(paging->cpu_mode == CPU_MODE_PROTECTED);
731 * Calculate the instruction pointer to store in the old TSS.
733 eip = vmexit->rip + vmexit->inst_length;
736 * Section 4.6, "Access Rights" in Intel SDM Vol 3.
737 * The following page table accesses are implicitly supervisor mode:
738 * - accesses to GDT or LDT to load segment descriptors
739 * - accesses to the task state segment during task switch
741 sup_paging = *paging;
742 sup_paging.cpl = 0; /* implicit supervisor mode */
744 /* Fetch the new TSS descriptor */
745 error = read_tss_descriptor(ctx, vcpu, task_switch, nt_sel, &nt_desc,
747 CHKERR(error, fault);
749 nt = usd_to_seg_desc(&nt_desc);
751 /* Verify the type of the new TSS */
752 nt_type = SEG_DESC_TYPE(nt.access);
753 if (nt_type != SDT_SYS386BSY && nt_type != SDT_SYS386TSS &&
754 nt_type != SDT_SYS286BSY && nt_type != SDT_SYS286TSS) {
755 sel_exception(ctx, vcpu, IDT_TS, nt_sel, ext);
759 /* TSS descriptor must have present bit set */
760 if (!SEG_DESC_PRESENT(nt.access)) {
761 sel_exception(ctx, vcpu, IDT_NP, nt_sel, ext);
766 * TSS must have a minimum length of 104 bytes for a 32-bit TSS and
767 * 44 bytes for a 16-bit TSS.
769 if (nt_type == SDT_SYS386BSY || nt_type == SDT_SYS386TSS)
771 else if (nt_type == SDT_SYS286BSY || nt_type == SDT_SYS286TSS)
776 assert(minlimit > 0);
777 if (nt.limit < minlimit) {
778 sel_exception(ctx, vcpu, IDT_TS, nt_sel, ext);
782 /* TSS must be busy if task switch is due to IRET */
783 if (reason == TSR_IRET && !TSS_BUSY(nt_type)) {
784 sel_exception(ctx, vcpu, IDT_TS, nt_sel, ext);
789 * TSS must be available (not busy) if task switch reason is
790 * CALL, JMP, exception or interrupt.
792 if (reason != TSR_IRET && TSS_BUSY(nt_type)) {
793 sel_exception(ctx, vcpu, IDT_GP, nt_sel, ext);
797 /* Fetch the new TSS */
798 error = vm_copy_setup(ctx, vcpu, &sup_paging, nt.base, minlimit + 1,
799 PROT_READ | PROT_WRITE, nt_iov, nitems(nt_iov), &fault);
800 CHKERR(error, fault);
801 vm_copyin(ctx, vcpu, nt_iov, &newtss, minlimit + 1);
803 /* Get the old TSS selector from the guest's task register */
804 ot_sel = GETREG(ctx, vcpu, VM_REG_GUEST_TR);
805 if (ISLDT(ot_sel) || IDXSEL(ot_sel) == 0) {
807 * This might happen if a task switch was attempted without
808 * ever loading the task register with LTR. In this case the
809 * TR would contain the values from power-on:
810 * (sel = 0, base = 0, limit = 0xffff).
812 sel_exception(ctx, vcpu, IDT_TS, ot_sel, task_switch->ext);
816 /* Get the old TSS base and limit from the guest's task register */
817 error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_TR, &ot_base, &ot_lim,
820 assert(!SEG_DESC_UNUSABLE(access) && SEG_DESC_PRESENT(access));
821 ot_type = SEG_DESC_TYPE(access);
822 assert(ot_type == SDT_SYS386BSY || ot_type == SDT_SYS286BSY);
824 /* Fetch the old TSS descriptor */
825 error = read_tss_descriptor(ctx, vcpu, task_switch, ot_sel, &ot_desc,
827 CHKERR(error, fault);
829 /* Get the old TSS */
830 error = vm_copy_setup(ctx, vcpu, &sup_paging, ot_base, minlimit + 1,
831 PROT_READ | PROT_WRITE, ot_iov, nitems(ot_iov), &fault);
832 CHKERR(error, fault);
833 vm_copyin(ctx, vcpu, ot_iov, &oldtss, minlimit + 1);
836 * Clear the busy bit in the old TSS descriptor if the task switch
837 * due to an IRET or JMP instruction.
839 if (reason == TSR_IRET || reason == TSR_JMP) {
840 ot_desc.sd_type &= ~0x2;
841 error = desc_table_write(ctx, vcpu, &sup_paging, ot_sel,
843 CHKERR(error, fault);
846 if (nt_type == SDT_SYS286BSY || nt_type == SDT_SYS286TSS) {
847 EPRINTLN("Task switch to 16-bit TSS not supported");
848 return (VMEXIT_ABORT);
851 /* Save processor state in old TSS */
852 tss32_save(ctx, vcpu, task_switch, eip, &oldtss, ot_iov);
855 * If the task switch was triggered for any reason other than IRET
856 * then set the busy bit in the new TSS descriptor.
858 if (reason != TSR_IRET) {
859 nt_desc.sd_type |= 0x2;
860 error = desc_table_write(ctx, vcpu, &sup_paging, nt_sel,
862 CHKERR(error, fault);
865 /* Update task register to point at the new TSS */
866 SETREG(ctx, vcpu, VM_REG_GUEST_TR, nt_sel);
868 /* Update the hidden descriptor state of the task register */
869 nt = usd_to_seg_desc(&nt_desc);
870 update_seg_desc(ctx, vcpu, VM_REG_GUEST_TR, &nt);
873 cr0 = GETREG(ctx, vcpu, VM_REG_GUEST_CR0);
874 SETREG(ctx, vcpu, VM_REG_GUEST_CR0, cr0 | CR0_TS);
877 * We are now committed to the task switch. Any exceptions encountered
878 * after this point will be handled in the context of the new task and
879 * the saved instruction pointer will belong to the new task.
881 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RIP, newtss.tss_eip);
884 /* Load processor state from new TSS */
885 error = tss32_restore(ctx, vcpu, task_switch, ot_sel, &newtss, nt_iov,
887 CHKERR(error, fault);
890 * Section "Interrupt Tasks" in Intel SDM, Vol 3: if an exception
891 * caused an error code to be generated, this error code is copied
892 * to the stack of the new task.
894 if (task_switch->errcode_valid) {
895 assert(task_switch->ext);
896 assert(task_switch->reason == TSR_IDT_GATE);
897 error = push_errcode(ctx, vcpu, &task_switch->paging, nt_type,
898 task_switch->errcode, &fault);
899 CHKERR(error, fault);
903 * Treatment of virtual-NMI blocking if NMI is delivered through
906 * Section "Architectural State Before A VM Exit", Intel SDM, Vol3:
907 * If the virtual NMIs VM-execution control is 1, VM entry injects
908 * an NMI, and delivery of the NMI causes a task switch that causes
909 * a VM exit, virtual-NMI blocking is in effect before the VM exit
912 * Thus, virtual-NMI blocking is in effect at the time of the task
917 * Treatment of virtual-NMI unblocking on IRET from NMI handler task.
919 * Section "Changes to Instruction Behavior in VMX Non-Root Operation"
920 * If "virtual NMIs" control is 1 IRET removes any virtual-NMI blocking.
921 * This unblocking of virtual-NMI occurs even if IRET causes a fault.
923 * Thus, virtual-NMI blocking is cleared at the time of the task switch
928 * If the task switch was triggered by an event delivered through
929 * the IDT then extinguish the pending event from the vcpu's
932 if (task_switch->reason == TSR_IDT_GATE) {
933 error = vm_set_intinfo(ctx, vcpu, 0);
938 * XXX should inject debug exception if 'T' bit is 1
941 return (VMEXIT_CONTINUE);