Esempio n. 1
0
int
emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict)
{
	int addrsize, bytes, flags, in, port, prot, rep;
	uint32_t val;
	inout_func_t handler;
	void *arg;
	int error, retval;
	enum vm_reg_name idxreg;
	uint64_t gla, index, iterations, count;
	struct vm_inout_str *vis;
	struct iovec iov[2];

	bytes = vmexit->u.inout.bytes;
	in = vmexit->u.inout.in;
	port = vmexit->u.inout.port;

	assert(port < MAX_IOPORTS);
	assert(bytes == 1 || bytes == 2 || bytes == 4);

	handler = inout_handlers[port].handler;

	if (strict && handler == default_inout)
		return (-1);

	flags = inout_handlers[port].flags;
	arg = inout_handlers[port].arg;

	if (in) {
		if (!(flags & IOPORT_F_IN))
			return (-1);
	} else {
		if (!(flags & IOPORT_F_OUT))
			return (-1);
	}

	retval = 0;
	if (vmexit->u.inout.string) {
		vis = &vmexit->u.inout_str;
		rep = vis->inout.rep;
		addrsize = vis->addrsize;
		prot = in ? PROT_WRITE : PROT_READ;
		assert(addrsize == 2 || addrsize == 4 || addrsize == 8);

		/* Index register */
		idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
		index = vis->index & vie_size2mask(addrsize);

		/* Count register */
		count = vis->count & vie_size2mask(addrsize);

		/* Limit number of back-to-back in/out emulations to 16 */
		iterations = MIN(count, 16);
		while (iterations > 0) {
			if (vie_calculate_gla(vis->paging.cpu_mode,
			    vis->seg_name, &vis->seg_desc, index, bytes,
			    addrsize, prot, &gla)) {
				error = vm_inject_exception2(ctx, vcpu,
				    IDT_GP, 0);
				assert(error == 0);
				retval = INOUT_RESTART;
				break;
			}

			error = vm_gla2gpa(ctx, vcpu, &vis->paging, gla, bytes,
			    prot, iov, nitems(iov));
			assert(error == 0 || error == 1 || error == -1);
			if (error) {
				retval = (error == 1) ? INOUT_RESTART :
				    INOUT_ERROR;
				break;
			}

			if (vie_alignment_check(vis->paging.cpl, bytes,
			    vis->cr0, vis->rflags, gla)) {
				error = vm_inject_exception2(ctx, vcpu,
				    IDT_AC, 0);
				assert(error == 0);
				return (INOUT_RESTART);
			}

			val = 0;
			if (!in)
				vm_copyin(ctx, vcpu, iov, &val, bytes);

			retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
			if (retval != 0)
				break;

			if (in)
				vm_copyout(ctx, vcpu, &val, iov, bytes);

			/* Update index */
			if (vis->rflags & PSL_D)
				index -= bytes;
			else
				index += bytes;

			count--;
			iterations--;
		}

		/* Update index register */
		error = vie_update_register(ctx, vcpu, idxreg, index, addrsize);
		assert(error == 0);

		/*
		 * Update count register only if the instruction had a repeat
		 * prefix.
		 */
		if (rep) {
			error = vie_update_register(ctx, vcpu, VM_REG_GUEST_RCX,
			    count, addrsize);
			assert(error == 0);
		}

		/* Restart the instruction if more iterations remain */
		if (retval == INOUT_OK && count != 0)
			retval = INOUT_RESTART;
	} else {
		if (!in) {
			val = vmexit->u.inout.eax & vie_size2mask(bytes);
		}
		retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
		if (retval == 0 && in) {
			vmexit->u.inout.eax &= ~vie_size2mask(bytes);
			vmexit->u.inout.eax |= val & vie_size2mask(bytes);
		}
	}
	return (retval);
}
Esempio n. 2
0
/*
 * Push an error code on the stack of the new task. This is needed if the
 * task switch was triggered by a hardware exception that causes an error
 * code to be saved (e.g. #PF).
 *
 * Returns 0 on success.
 * Returns 1 if an exception was injected into the guest.
 * Returns -1 otherwise.
 */
static int
push_errcode(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
    int task_type, uint32_t errcode)
{
	struct iovec iov[2];
	struct seg_desc seg_desc;
	int stacksize, bytes, error;
	uint64_t gla, cr0, rflags;
	uint32_t esp;
	uint16_t stacksel;

	cr0 = GETREG(ctx, vcpu, VM_REG_GUEST_CR0);
	rflags = GETREG(ctx, vcpu, VM_REG_GUEST_RFLAGS);
	stacksel = GETREG(ctx, vcpu, VM_REG_GUEST_SS);

	error = vm_get_desc(ctx, vcpu, VM_REG_GUEST_SS, &seg_desc.base,
	    &seg_desc.limit, &seg_desc.access);
	assert(error == 0);

	/*
	 * Section "Error Code" in the Intel SDM vol 3: the error code is
	 * pushed on the stack as a doubleword or word (depending on the
	 * default interrupt, trap or task gate size).
	 */
	if (task_type == SDT_SYS386BSY || task_type == SDT_SYS386TSS)
		bytes = 4;
	else
		bytes = 2;

	/*
	 * PUSH instruction from Intel SDM vol 2: the 'B' flag in the
	 * stack-segment descriptor determines the size of the stack
	 * pointer outside of 64-bit mode.
	 */
	if (SEG_DESC_DEF32(seg_desc.access))
		stacksize = 4;
	else
		stacksize = 2;

	esp = GETREG(ctx, vcpu, VM_REG_GUEST_RSP);
	esp -= bytes;

	if (vie_calculate_gla(paging->cpu_mode, VM_REG_GUEST_SS,
	    &seg_desc, esp, bytes, stacksize, PROT_WRITE, &gla)) {
		sel_exception(ctx, vcpu, IDT_SS, stacksel, 1);
		return (1);
	}

	if (vie_alignment_check(paging->cpl, bytes, cr0, rflags, gla)) {
		vm_inject_ac(ctx, vcpu, 1);
		return (1);
	}

	error = vm_copy_setup(ctx, vcpu, paging, gla, bytes, PROT_WRITE,
	    iov, nitems(iov));
	if (error)
		return (error);

	vm_copyout(ctx, vcpu, &errcode, iov, bytes);
	SETREG(ctx, vcpu, VM_REG_GUEST_RSP, esp);
	return (0);
}
int
emulate_inout(int vcpu, struct vm_exit *vmexit, int strict)
{
	int addrsize, bytes, flags, in, port, prot, rep;
	uint32_t eax, val;
	inout_func_t handler;
	void *arg;
	int error, fault, retval;
	enum vm_reg_name idxreg;
	uint64_t gla, index, iterations, count;
	struct vm_inout_str *vis;
	struct iovec iov[2];

	bytes = vmexit->u.inout.bytes;
	in = vmexit->u.inout.in;
	port = vmexit->u.inout.port;

	assert(port < MAX_IOPORTS);
	assert(bytes == 1 || bytes == 2 || bytes == 4);

	handler = inout_handlers[port].handler;

	if (strict && handler == default_inout)
		return (-1);

	flags = inout_handlers[port].flags;
	arg = inout_handlers[port].arg;

	if (in) {
		if (!(flags & IOPORT_F_IN))
			return (-1);
	} else {
		if (!(flags & IOPORT_F_OUT))
			return (-1);
	}

	retval = 0;
	if (vmexit->u.inout.string) {
		vis = &vmexit->u.inout_str;
		rep = vis->inout.rep;
		addrsize = vis->addrsize;
		prot = in ? XHYVE_PROT_WRITE : XHYVE_PROT_READ;
		assert(addrsize == 2 || addrsize == 4 || addrsize == 8);

		/* Index register */
		idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
		index = vis->index & vie_size2mask(addrsize);

		/* Count register */
		count = vis->count & vie_size2mask(addrsize);

		/* Limit number of back-to-back in/out emulations to 16 */
		iterations = min(count, 16);
		while (iterations > 0) {
			assert(retval == 0);
			if (vie_calculate_gla(vis->paging.cpu_mode,
			    vis->seg_name, &vis->seg_desc, index, bytes,
			    addrsize, prot, &gla)) {
				vm_inject_gp(vcpu);
				break;
			}

			error = xh_vm_copy_setup(vcpu, &vis->paging, gla,
			    ((size_t) bytes), prot, iov, nitems(iov), &fault);
			if (error) {
				retval = -1;  /* Unrecoverable error */
				break;
			} else if (fault) {
				retval = 0;  /* Resume guest to handle fault */
				break;
			}

			if (vie_alignment_check(vis->paging.cpl, bytes,
			    vis->cr0, vis->rflags, gla)) {
				vm_inject_ac(vcpu, 0);
				break;
			}

			val = 0;
			if (!in)
				xh_vm_copyin(iov, &val, ((size_t) bytes));

			retval = handler(vcpu, in, port, bytes, &val, arg);
			if (retval != 0)
				break;

			if (in)
				xh_vm_copyout(&val, iov, ((size_t) bytes));

			/* Update index */
			if (vis->rflags & PSL_D)
				index -= ((uint64_t) bytes);
			else
				index += ((uint64_t) bytes);

			count--;
			iterations--;
		}

		/* Update index register */
		error = update_register(vcpu, idxreg, index, addrsize);
		assert(error == 0);

		/*
		 * Update count register only if the instruction had a repeat
		 * prefix.
		 */
		if (rep) {
			error = update_register(vcpu, VM_REG_GUEST_RCX, count, addrsize);
			assert(error == 0);
		}

		/* Restart the instruction if more iterations remain */
		if (retval == 0 && count != 0) {
			error = xh_vm_restart_instruction(vcpu);
			assert(error == 0);
		}
	} else {
		eax = vmexit->u.inout.eax;
		val = eax & vie_size2mask(bytes);
		retval = handler(vcpu, in, port, bytes, &val, arg);
		if (retval == 0 && in) {
			eax &= ~vie_size2mask(bytes);
			eax |= val & vie_size2mask(bytes);
			error = xh_vm_set_register(vcpu, VM_REG_GUEST_RAX, eax);
			assert(error == 0);
		}
	}
	return (retval);
}