Skip to content

Commit 064aea7

Browse files
Nadav Har'Elavikivity
authored andcommitted
KVM: nVMX: Decoding memory operands of VMX instructions
This patch includes a utility function for decoding pointer operands of VMX instructions issued by L1 (a guest hypervisor) Signed-off-by: Nadav Har'El <nyh@il.ibm.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent b87a51a commit 064aea7

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎arch/x86/kvm/vmx.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4342,6 +4342,59 @@ static int handle_vmoff(struct kvm_vcpu *vcpu)
43424342
return 1;
43434343
}
43444344

4345+
/*
4346+
* Decode the memory-address operand of a vmx instruction, as recorded on an
4347+
* exit caused by such an instruction (run by a guest hypervisor).
4348+
* On success, returns 0. When the operand is invalid, returns 1 and throws
4349+
* #UD or #GP.
4350+
*/
4351+
static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
4352+
unsigned long exit_qualification,
4353+
u32 vmx_instruction_info, gva_t *ret)
4354+
{
4355+
/*
4356+
* According to Vol. 3B, "Information for VM Exits Due to Instruction
4357+
* Execution", on an exit, vmx_instruction_info holds most of the
4358+
* addressing components of the operand. Only the displacement part
4359+
* is put in exit_qualification (see 3B, "Basic VM-Exit Information").
4360+
* For how an actual address is calculated from all these components,
4361+
* refer to Vol. 1, "Operand Addressing".
4362+
*/
4363+
int scaling = vmx_instruction_info & 3;
4364+
int addr_size = (vmx_instruction_info >> 7) & 7;
4365+
bool is_reg = vmx_instruction_info & (1u << 10);
4366+
int seg_reg = (vmx_instruction_info >> 15) & 7;
4367+
int index_reg = (vmx_instruction_info >> 18) & 0xf;
4368+
bool index_is_valid = !(vmx_instruction_info & (1u << 22));
4369+
int base_reg = (vmx_instruction_info >> 23) & 0xf;
4370+
bool base_is_valid = !(vmx_instruction_info & (1u << 27));
4371+
4372+
if (is_reg) {
4373+
kvm_queue_exception(vcpu, UD_VECTOR);
4374+
return 1;
4375+
}
4376+
4377+
/* Addr = segment_base + offset */
4378+
/* offset = base + [index * scale] + displacement */
4379+
*ret = vmx_get_segment_base(vcpu, seg_reg);
4380+
if (base_is_valid)
4381+
*ret += kvm_register_read(vcpu, base_reg);
4382+
if (index_is_valid)
4383+
*ret += kvm_register_read(vcpu, index_reg)<<scaling;
4384+
*ret += exit_qualification; /* holds the displacement */
4385+
4386+
if (addr_size == 1) /* 32 bit */
4387+
*ret &= 0xffffffff;
4388+
4389+
/*
4390+
* TODO: throw #GP (and return 1) in various cases that the VM*
4391+
* instructions require it - e.g., offset beyond segment limit,
4392+
* unusable or unreadable/unwritable segment, non-canonical 64-bit
4393+
* address, and so on. Currently these are not checked.
4394+
*/
4395+
return 0;
4396+
}
4397+
43454398
/*
43464399
* The exit handlers return 1 if the exit was handled fully and guest execution
43474400
* may resume. Otherwise they set the kvm_run parameter to indicate what needs

‎arch/x86/kvm/x86.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3848,7 +3848,7 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
38483848
exception);
38493849
}
38503850

3851-
static int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
3851+
int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
38523852
gva_t addr, void *val, unsigned int bytes,
38533853
struct x86_exception *exception)
38543854
{
@@ -3858,6 +3858,7 @@ static int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
38583858
return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
38593859
exception);
38603860
}
3861+
EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
38613862

38623863
static int kvm_read_guest_virt_system(struct x86_emulate_ctxt *ctxt,
38633864
gva_t addr, void *val, unsigned int bytes,

‎arch/x86/kvm/x86.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ int kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip);
8181

8282
void kvm_write_tsc(struct kvm_vcpu *vcpu, u64 data);
8383

84+
int kvm_read_guest_virt(struct x86_emulate_ctxt *ctxt,
85+
gva_t addr, void *val, unsigned int bytes,
86+
struct x86_exception *exception);
87+
8488
#endif

0 commit comments

Comments
 (0)