Skip to content

Commit efd2253

Browse files
committed
Automatically select appropriate entry function
1 parent 6e8472f commit efd2253

File tree

6 files changed

+18
-37
lines changed

6 files changed

+18
-37
lines changed

lib/tinykvm/machine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace tinykvm {
2828
__attribute__ ((cold))
2929
Machine::Machine(std::string_view binary, const MachineOptions& options)
3030
: m_forked {false},
31+
m_just_reset {false},
3132
m_binary {binary},
3233
memory { vMemory::New(*this, options,
3334
options.vmem_base_address, options.vmem_base_address + 0x100000, options.max_mem)
@@ -55,6 +56,7 @@ Machine::Machine(const std::vector<uint8_t>& bin, const MachineOptions& opts)
5556
Machine::Machine(const Machine& other, const MachineOptions& options)
5657
: m_prepped {false},
5758
m_forked {true},
59+
m_just_reset {true},
5860
m_binary {options.binary.empty() ? other.m_binary : options.binary},
5961
memory {*this, options, other.memory},
6062
m_stack_address {other.m_stack_address},
@@ -131,6 +133,7 @@ void Machine::reset_to(const Machine& other, const MachineOptions& options)
131133
memory.fork_reset(options);
132134
}
133135

136+
this->m_just_reset = true;
134137
this->m_mm = other.m_mm;
135138
this->m_mmap_cache = other.m_mmap_cache;
136139

lib/tinykvm/machine.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ struct Machine
3737
void timed_vmcall(address_t, float timeout, Args&&...);
3838
template <typename... Args> constexpr
3939
void timed_vmcall_stack(address_t, address_t stk, float timeout, Args&&...);
40-
/* SYSV function call with timeout, no cache flushing */
41-
template <typename... Args> constexpr
42-
void timed_reentry(address_t, float timeout, Args&&...);
43-
template <typename... Args> constexpr
44-
void timed_reentry_stack(address_t, address_t stk, float timeout, Args&&...);
4540
/* Retrieve optional return value from a vmcall */
4641
long return_value() const;
4742

@@ -230,6 +225,7 @@ struct Machine
230225
int fd = 0;
231226
bool m_prepped = false;
232227
bool m_forked = false;
228+
bool m_just_reset = false;
233229
void* m_userdata = nullptr;
234230

235231
std::string_view m_binary;

lib/tinykvm/machine_inline.hpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ void Machine::setup_call(tinykvm_x86regs& regs,
5252
regs = {};
5353
/* Set IOPL=3 to allow I/O instructions in usermode */
5454
regs.rflags = 2 | (3 << 12);
55-
regs.r15 = addr;
56-
regs.rip = this->entry_address();
55+
if (this->m_just_reset) {
56+
this->m_just_reset = false;
57+
regs.r15 = addr;
58+
regs.rip = this->entry_address();
59+
} else {
60+
regs.rip = addr;
61+
}
5762
regs.rsp = rsp;
5863
[[maybe_unused]] unsigned iargs = 0;
5964
([&] {
@@ -142,29 +147,6 @@ void Machine::timed_vmcall_stack(uint64_t addr, uint64_t stk, float timeout, Arg
142147
this->run(timeout);
143148
}
144149

145-
template <typename... Args> inline constexpr
146-
void Machine::timed_reentry(uint64_t addr, float timeout, Args&&... args)
147-
{
148-
auto& regs = vcpu.registers();
149-
this->setup_call(regs, addr,
150-
this->stack_address(), std::forward<Args> (args)...);
151-
/// This may jump directly to the guest function if DPL=3
152-
regs.rip = this->reentry_address();
153-
vcpu.set_registers(regs);
154-
this->run(timeout);
155-
}
156-
157-
template <typename... Args> inline constexpr
158-
void Machine::timed_reentry_stack(uint64_t addr, uint64_t stk, float timeout, Args&&... args)
159-
{
160-
auto& regs = vcpu.registers();
161-
this->setup_call(regs, addr, stk, std::forward<Args> (args)...);
162-
/// This may jump directly to the guest function if DPL=3
163-
regs.rip = this->reentry_address();
164-
vcpu.set_registers(regs);
165-
this->run(timeout);
166-
}
167-
168150
inline uint64_t Machine::stack_push(__u64& sp, const std::string& string)
169151
{
170152
return stack_push(sp, string.data(), string.size()+1); /* zero */

lib/tinykvm/vcpu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ Machine::address_t Machine::entry_address() const noexcept {
446446
return usercode_header().translated_vm_entry(memory);
447447
}
448448
Machine::address_t Machine::reentry_address() const noexcept {
449-
return usercode_header().translated_vm_reentry(memory);
449+
return usercode_header().translated_vm_userentry(memory);
450450
}
451451
Machine::address_t Machine::exit_address() const noexcept {
452452
return usercode_header().translated_vm_rexit(memory);

src/bench.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ int main(int argc, char** argv)
114114
printf("set_registers() average time: %lu nanos\n", set_registers_time);
115115

116116
auto fastest_call_time = micro_benchmark([&] {
117-
master_vm.timed_reentry(vmcall_address, 0.0f);
117+
master_vm.timed_vmcall(vmcall_address, 0.0f);
118118
});
119119
printf("Fastest possible vmcall time: %lu ns\n", fastest_call_time);
120120

121121
auto fastest_timed_call_time = micro_benchmark([&] {
122-
master_vm.timed_reentry(vmcall_address, 4.0f);
122+
master_vm.timed_vmcall(vmcall_address, 4.0f);
123123
});
124124
printf("Fastest possible timed vmcall time: %lu ns\n", fastest_timed_call_time);
125125

@@ -504,7 +504,7 @@ void benchmark_multiple_vms(tinykvm::Machine& master_vm, size_t NUM, size_t RESE
504504
if constexpr (FULL_RESET) {
505505
fvm[counter].timed_vmcall(vmcall_address, 4.0f);
506506
} else {
507-
fvm[counter].timed_reentry(vmcall_address, 4.0f);
507+
fvm[counter].timed_vmcall(vmcall_address, 4.0f);
508508
}
509509
asm("" : : : "memory");
510510
auto frt2 = time_now();
@@ -574,7 +574,7 @@ void benchmark_multiple_pooled_vms(tinykvm::Machine& master_vm, size_t NUM, size
574574
if constexpr (FULL_RESET) {
575575
fvm->timed_vmcall(data.addr, 4.0f);
576576
} else {
577-
fvm->timed_reentry(data.addr, 4.0f);
577+
fvm->timed_vmcall(data.addr, 4.0f);
578578
}
579579
asm("" : : : "memory");
580580
auto frt2 = time_now();

tests/unit/reset.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ extern void crash(const char *arg) {
154154
REQUIRE(output_is_hello_world);
155155

156156
output_is_hello_world = false;
157-
m.timed_reentry(m.address_of("hello_world"), 2.0f, "Hello World!");
157+
m.timed_vmcall(m.address_of("hello_world"), 2.0f, "Hello World!");
158158
REQUIRE(m.return_value() == 1023);
159159
REQUIRE(output_is_hello_world);
160160

161161
output_is_hello_world = false;
162162
try {
163-
m.timed_reentry(m.address_of("crash"), 2.0f, "Hello World!");
163+
m.timed_vmcall(m.address_of("crash"), 2.0f, "Hello World!");
164164
} catch (...) {}
165165
REQUIRE(output_is_hello_world);
166166

0 commit comments

Comments
 (0)