Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Commit 8e49240

Browse files
committed
Optimization: Cached RIP reads
Signed-off-by: Alexandro Sanchez Bach <[email protected]>
1 parent 46fc754 commit 8e49240

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

core/cpu.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,6 @@ int cpu_vmx_execute(struct vcpu_t *vcpu, struct hax_tunnel *htun)
375375
* reason is, we have no schedule hook to get notified of preemption
376376
* This should be changed later after get better idea
377377
*/
378-
vcpu->state->_rip = vmread(vcpu, GUEST_RIP);
379-
380378
hax_handle_idt_vectoring(vcpu);
381379

382380
vmx(vcpu, exit_qualification).raw = vmread(
@@ -599,7 +597,7 @@ static void cpu_vmentry_failed(struct vcpu_t *vcpu, vmx_result_t result)
599597
uint64_t error, reason;
600598

601599
hax_error("VM entry failed: RIP=%08lx\n",
602-
(mword)vmread(vcpu, GUEST_RIP));
600+
(mword)vmcs_read(vcpu, GUEST_RIP));
603601

604602
//dump_vmcs();
605603

core/include/vcpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ void hax_panic_vcpu(struct vcpu_t *v, char *fmt, ...);
256256

257257
// Extension-specific operations
258258

259+
mword vcpu_get_rip(struct vcpu_t *vcpu);
259260
uint16_t vcpu_get_seg_selector(struct vcpu_t *vcpu, int seg);
260261
mword vcpu_get_seg_base(struct vcpu_t *vcpu, int seg);
261262
uint32_t vcpu_get_seg_limit(struct vcpu_t *vcpu, int seg);

core/include/vmx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ typedef enum component_index_t component_index_t;
338338
COMP(0, 0, W_UL, HOST_IDTR_BASE) \
339339
COMP(0, 0, W_UL, HOST_SYSENTER_ESP) \
340340
COMP(0, 0, W_UL, HOST_SYSENTER_EIP) \
341-
COMP(0, 0, W_UL, GUEST_RIP) \
341+
COMP(1, 0, W_UL, GUEST_RIP) \
342342
COMP(0, 0, W_UL, GUEST_RFLAGS) \
343343
COMP(0, 0, W_UL, GUEST_RSP) \
344344
COMP(0, 0, W_UL, GUEST_CR0) \

core/vcpu.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,7 @@ static void advance_rip(struct vcpu_t *vcpu)
17511751
vcpu->interruptibility_dirty = 1;
17521752
}
17531753

1754+
state->_rip = vcpu_get_rip(vcpu);
17541755
state->_rip += vmcs_read(vcpu, VM_EXIT_INFO_INSTRUCTION_LENGTH);
17551756
vcpu->rip_dirty = 1;
17561757
}
@@ -1966,8 +1967,8 @@ static void vmwrite_cr(struct vcpu_t *vcpu)
19661967
cr4_mask |= CR4_PAE;
19671968
eptp = vm_get_eptp(vcpu->vm);
19681969
hax_assert(eptp != INVALID_EPTP);
1969-
// hax_debug("Guest eip:%llx, EPT mode, eptp:%llx\n", vcpu->state->_rip,
1970-
// eptp);
1970+
// hax_debug("Guest eip:%llx, EPT mode, eptp:%llx\n",
1971+
// vcpu_get_rip(vcpu), eptp);
19711972
vmwrite(vcpu, GUEST_CR3, state->_cr3);
19721973
scpu_ctls |= ENABLE_EPT;
19731974
// Set PDPTEs for vCPU if it's in or about to enter PAE paging mode
@@ -2096,7 +2097,7 @@ static int vcpu_emulate_insn(struct vcpu_t *vcpu)
20962097
em_context_t *em_ctxt = &vcpu->emulate_ctxt;
20972098
uint8_t instr[INSTR_MAX_LEN] = {0};
20982099
uint32_t exit_instr_length = vmcs_read(vcpu, VM_EXIT_INFO_INSTRUCTION_LENGTH);
2099-
uint64_t rip = vcpu->state->_rip;
2100+
uint64_t rip = vcpu_get_rip(vcpu);
21002101
segment_desc_t cs;
21012102
uint64_t va;
21022103

@@ -2347,14 +2348,14 @@ static int exit_exc_nmi(struct vcpu_t *vcpu, struct hax_tunnel *htun)
23472348
}
23482349
case VECTOR_DB: {
23492350
htun->_exit_status = HAX_EXIT_DEBUG;
2350-
htun->debug.rip = vcpu->state->_rip;
2351+
htun->debug.rip = vcpu_get_rip(vcpu);
23512352
htun->debug.dr6 = vmx(vcpu, exit_qualification).raw;
23522353
htun->debug.dr7 = vmread(vcpu, GUEST_DR7);
23532354
return HAX_EXIT;
23542355
}
23552356
case VECTOR_BP: {
23562357
htun->_exit_status = HAX_EXIT_DEBUG;
2357-
htun->debug.rip = vcpu->state->_rip;
2358+
htun->debug.rip = vcpu_get_rip(vcpu);
23582359
htun->debug.dr6 = 0;
23592360
htun->debug.dr7 = 0;
23602361
return HAX_EXIT;
@@ -2736,7 +2737,7 @@ static int exit_invlpg(struct vcpu_t *vcpu, struct hax_tunnel *htun)
27362737

27372738
static int exit_rdtsc(struct vcpu_t *vcpu, struct hax_tunnel *htun)
27382739
{
2739-
hax_debug("rdtsc exiting: rip: %llx\n", vcpu->state->_rip);
2740+
hax_debug("rdtsc exiting: rip: %lx\n", vcpu_get_rip(vcpu));
27402741
return HAX_RESUME;
27412742
}
27422743

core/vmx.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ void vcpu_vmcs_flush_cache_w(struct vcpu_t *vcpu)
327327
vcpu->vmx.vmcs_cache_w.dirty = 0;
328328
}
329329

330+
mword vcpu_get_rip(struct vcpu_t *vcpu)
331+
{
332+
return vmcs_read(vcpu, GUEST_RIP);
333+
}
334+
330335
uint16_t vcpu_get_seg_selector(struct vcpu_t *vcpu, int seg)
331336
{
332337
uint16_t value;

0 commit comments

Comments
 (0)