FreeBSD/lib/libvmmapi/vmmapi.c
/*
 * From Intel Vol 3a:
 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
 */
From Intel Vol 3a:
Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
From Intel Vol 3a : Table 9 - 1 . IA - 32 Processor States Following Power - up Reset or INIT
int
vcpu_reset(struct vmctx *vmctx, int vcpu)
{
	int error;
	uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
	uint32_t desc_access, desc_limit;
	uint16_t sel;
	zero = 0;
	rflags = 0x2;
	error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
	if (error)
		goto done;
	rip = 0xfff0;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
		goto done;
	cr0 = CR0_NE;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
		goto done;
	cr4 = 0;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
		goto done;
	desc_base = 0xffff0000;
	desc_limit = 0xffff;
	desc_access = 0x0093;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
			    desc_base, desc_limit, desc_access);
	if (error)
		goto done;
	sel = 0xf000;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
		goto done;
	desc_base = 0;
	desc_limit = 0xffff;
	desc_access = 0x0093;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
			    desc_base, desc_limit, desc_access);
	if (error)
		goto done;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
			    desc_base, desc_limit, desc_access);
	if (error)
		goto done;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
			    desc_base, desc_limit, desc_access);
	if (error)
		goto done;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
			    desc_base, desc_limit, desc_access);
	if (error)
		goto done;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
			    desc_base, desc_limit, desc_access);
	if (error)
		goto done;
	sel = 0;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
		goto done;
	rdx = 0xf00;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
		goto done;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
		goto done;
	desc_base = 0;
	desc_limit = 0xffff;
	desc_access = 0;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
			    desc_base, desc_limit, desc_access);
	if (error != 0)
		goto done;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
			    desc_base, desc_limit, desc_access);
	if (error != 0)
		goto done;
	desc_base = 0;
	desc_limit = 0xffff;
	desc_access = 0x0000008b;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
	if (error)
		goto done;
	sel = 0;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
		goto done;
	desc_base = 0;
	desc_limit = 0xffff;
	desc_access = 0x00000082;
	error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
			    desc_limit, desc_access);
	if (error)
		goto done;
	sel = 0;
	if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
		goto done;
	error = 0;
done:
	return (error);
}
int vcpu_reset ( struct vmctx * vmctx , int vcpu ) { int error ; uint64_t rflags , rip , cr0 , cr4 , zero , desc_base , rdx ; uint32_t desc_access , desc_limit ; uint16_t sel ; zero = 0 ; rflags = 0x2 ; error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RFLAGS , rflags ) ; if ( error ) goto done ; rip = 0xfff0 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RIP , rip ) ) != 0 ) goto done ; cr0 = CR0_NE ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_CR0 , cr0 ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_CR3 , zero ) ) != 0 ) goto done ; cr4 = 0 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_CR4 , cr4 ) ) != 0 ) goto done ; desc_base = 0xffff0000 ; desc_limit = 0xffff ; desc_access = 0x0093 ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_CS , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; sel = 0xf000 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_CS , sel ) ) != 0 ) goto done ; desc_base = 0 ; desc_limit = 0xffff ; desc_access = 0x0093 ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_SS , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_DS , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_ES , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_FS , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_GS , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; sel = 0 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_SS , sel ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_DS , sel ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_ES , sel ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_FS , sel ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_GS , sel ) ) != 0 ) goto done ; rdx = 0xf00 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RAX , zero ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RBX , zero ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RCX , zero ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RDX , rdx ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RSI , zero ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RDI , zero ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RBP , zero ) ) != 0 ) goto done ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_RSP , zero ) ) != 0 ) goto done ; desc_base = 0 ; desc_limit = 0xffff ; desc_access = 0 ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_GDTR , desc_base , desc_limit , desc_access ) ; if ( error != 0 ) goto done ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_IDTR , desc_base , desc_limit , desc_access ) ; if ( error != 0 ) goto done ; desc_base = 0 ; desc_limit = 0xffff ; desc_access = 0x0000008b ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_TR , 0 , 0 , desc_access ) ; if ( error ) goto done ; sel = 0 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_TR , sel ) ) != 0 ) goto done ; desc_base = 0 ; desc_limit = 0xffff ; desc_access = 0x00000082 ; error = vm_set_desc ( vmctx , vcpu , VM_REG_GUEST_LDTR , desc_base , desc_limit , desc_access ) ; if ( error ) goto done ; sel = 0 ; if ( ( error = vm_set_register ( vmctx , vcpu , VM_REG_GUEST_LDTR , 0 ) ) != 0 ) goto done ; error = 0 ; done : return ( error ) ; }
From Intel Vol 3a:
 Table 9-1.
From Intel Vol 3a : Table 9 - 1 .
/*
	 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
	 */ /*
	 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
	 */ /* General purpose registers */ /* GDTR, IDTR */ /* TR */ /* LDTR */ /* XXX cr2, debug registers */