-
Notifications
You must be signed in to change notification settings - Fork 221
/
main.cpp
125 lines (103 loc) · 2.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <hvpp/hypervisor.h>
#include <hvpp/lib/driver.h>
#include <hvpp/lib/assert.h>
#include <hvpp/lib/log.h>
#include <hvpp/lib/mm.h>
#include <hvpp/lib/mp.h>
#include "vmexit_custom.h"
#include "device_custom.h"
#include <cinttypes>
using namespace ia32;
using namespace hvpp;
namespace driver
{
//
// Create combined handler from these VM-exit handlers.
//
using vmexit_handler_t = vmexit_compositor_handler<
vmexit_stats_handler,
vmexit_dbgbreak_handler,
vmexit_custom_handler
>;
static_assert(std::is_base_of_v<vmexit_handler, vmexit_handler_t>);
vmexit_handler_t* vmexit_handler_ = nullptr;
device_custom* device_ = nullptr;
auto initialize() noexcept -> error_code_t
{
//
// Create device instance.
//
device_ = new device_custom();
if (!device_)
{
destroy();
return make_error_code_t(std::errc::not_enough_memory);
}
//
// Initialize device instance.
//
if (auto err = device_->create())
{
destroy();
return err;
}
//
// Create VM-exit handler instance.
//
vmexit_handler_ = new vmexit_handler_t();
if (!vmexit_handler_)
{
destroy();
return make_error_code_t(std::errc::not_enough_memory);
}
//
// Assign the vmexit_dbgbreak_handler instance to the device.
//
device_->handler(std::get<vmexit_dbgbreak_handler>(vmexit_handler_->handlers));
//
// Example: Enable tracing of I/O instructions.
//
std::get<vmexit_stats_handler>(vmexit_handler_->handlers)
.trace_bitmap().set(int(vmx::exit_reason::execute_io_instruction));
//
// Start the hypervisor.
//
if (auto err = hvpp::hypervisor::start(*vmexit_handler_))
{
destroy();
return err;
}
//
// Tell debugger we're started.
//
hvpp_info("Hypervisor started, current free memory: %" PRIu64 " MB",
mm::hypervisor_allocator()->free_bytes() / 1024 / 1024);
return {};
}
void destroy() noexcept
{
//
// Stop the hypervisor.
//
hvpp::hypervisor::stop();
//
// Destroy VM-exit handler.
//
if (vmexit_handler_)
{
//
// Print statistics into debugger.
//
std::get<vmexit_stats_handler>(vmexit_handler_->handlers).dump();
delete vmexit_handler_;
}
//
// Destroy device.
//
if (device_)
{
delete device_;
}
hvpp_info("Hypervisor stopped");
}
}