forked from zhuhuibeishadiao/hvpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
167 lines (140 loc) · 3.28 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <hvpp/hypervisor.h>
#include <hvpp/vmexit_compositor.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>);
hypervisor* hypervisor_ = nullptr;
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_->initialize())
{
destroy();
return err;
}
//
// Create hypervisor instance.
//
hypervisor_ = new hypervisor();
if (!hypervisor_)
{
destroy();
return make_error_code_t(std::errc::not_enough_memory);
}
//
// Initialize hypervisor.
//
if (auto err = hypervisor_->initialize())
{
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);
}
//
// Initialize VM-exit handler.
//
if (auto err = vmexit_handler_->initialize())
{
destroy();
return err;
}
//
// 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.
//
hypervisor_->start(vmexit_handler_);
//
// Tell debugger we're started.
//
hvpp_info("Hypervisor started, current free memory: %" PRIu64 " MB",
memory_manager::free_bytes() / 1024 / 1024);
return error_code_t{};
}
void destroy() noexcept
{
//
// Stop and destroy hypervisor.
//
if (hypervisor_)
{
//
// Stopping the hypervisor is not strictly needed here -
// the destroy() method stops the hypervisor if necessary.
//
if (hypervisor_->is_started())
{
hypervisor_->stop();
}
hypervisor_->destroy();
delete hypervisor_;
}
//
// Destroy VM-exit handler.
//
if (vmexit_handler_)
{
//
// Print statistics into debugger.
//
std::get<vmexit_stats_handler>(vmexit_handler_->handlers).dump();
vmexit_handler_->destroy();
delete vmexit_handler_;
}
//
// Destroy device.
//
if (device_)
{
device_->destroy();
delete device_;
}
hvpp_info("Hypervisor stopped");
}
}