vcpu_intel_x64.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <gsl/gsl>
23 #include <vcpu/vcpu_intel_x64.h>
24 
26  vcpuid::type id,
27  std::unique_ptr<debug_ring> debug_ring,
28  std::unique_ptr<vmxon_intel_x64> vmxon,
29  std::unique_ptr<vmcs_intel_x64> vmcs,
30  std::unique_ptr<exit_handler_intel_x64> exit_handler,
31  std::unique_ptr<vmcs_intel_x64_state> vmm_state,
32  std::unique_ptr<vmcs_intel_x64_state> guest_state) :
33 
34  vcpu(id, std::move(debug_ring)),
35  m_vmcs_launched(false),
36  m_vmxon(std::move(vmxon)),
37  m_vmcs(std::move(vmcs)),
38  m_exit_handler(std::move(exit_handler)),
39  m_vmm_state(std::move(vmm_state)),
40  m_guest_state(std::move(guest_state))
41 { }
42 
43 void
45 {
46  auto ___ = gsl::on_failure([&]
47  { this->fini(); });
48 
49  if (!m_state_save)
50  m_state_save = std::make_unique<state_save_intel_x64>();
51 
52  if (!m_vmxon)
53  m_vmxon = std::make_unique<vmxon_intel_x64>();
54 
55  if (!m_vmcs)
56  m_vmcs = std::make_unique<vmcs_intel_x64>();
57 
58  if (!m_exit_handler)
59  m_exit_handler = std::make_unique<exit_handler_intel_x64>();
60 
61  if (!m_vmm_state)
62  m_vmm_state = std::make_unique<vmcs_intel_x64_vmm_state>();
63 
64  if (!m_guest_state)
65  m_guest_state = std::make_unique<vmcs_intel_x64_host_vm_state>();
66 
67  m_state_save->vcpuid = this->id();
68  m_state_save->vmxon_ptr = reinterpret_cast<uintptr_t>(m_vmxon.get());
69  m_state_save->vmcs_ptr = reinterpret_cast<uintptr_t>(m_vmcs.get());
70  m_state_save->exit_handler_ptr = reinterpret_cast<uintptr_t>(m_exit_handler.get());
71 
72  m_vmcs->set_state_save(m_state_save.get());
73 
74  m_exit_handler->set_vmcs(m_vmcs.get());
75  m_exit_handler->set_state_save(m_state_save.get());
76 
77  vcpu::init(data);
78 }
79 
80 void
82 { vcpu::fini(data); }
83 
84 void
86 {
87  expects(this->is_initialized());
88 
89  if (!m_vmcs_launched)
90  {
91  m_vmcs_launched = true;
92 
93  auto ___ = gsl::on_failure([&]
94  { m_vmcs_launched = false; });
95 
96  vcpu::run(data);
97 
98  auto ___ = gsl::on_failure([&]
99  { vcpu::hlt(data); });
100 
101  if (this->is_host_vm_vcpu())
102  m_vmxon->start();
103 
104  auto ___ = gsl::on_failure([&]
105  {
106  if (this->is_host_vm_vcpu())
107  m_vmxon->stop();
108  });
109 
110  m_vmcs->launch(m_vmm_state.get(), m_guest_state.get());
111  }
112  else
113  {
114  m_vmcs->load();
115  m_vmcs->resume();
116  }
117 }
118 
119 void
121 {
122  if (!this->is_initialized())
123  return;
124 
125  if (m_vmcs_launched)
126  {
127  auto ___ = gsl::on_success([&]
128  { m_vmcs_launched = false; });
129 
130  if (this->is_host_vm_vcpu())
131  m_vmxon->stop();
132  }
133 
134  vcpu::hlt(data);
135 }
expects(rbx)
uint64_t type
Definition: vcpuid.h:31
std::unique_ptr< vmcs_intel_x64_state > m_vmm_state
int64_t unsigned long void * data
void exit_handler(exit_handler_intel_x64 *exit_handler) noexcept
vcpu_intel_x64(vcpuid::type id, std::unique_ptr< debug_ring > debug_ring=nullptr, std::unique_ptr< vmxon_intel_x64 > vmxon=nullptr, std::unique_ptr< vmcs_intel_x64 > vmcs=nullptr, std::unique_ptr< exit_handler_intel_x64 > exit_handler=nullptr, std::unique_ptr< vmcs_intel_x64_state > vmm_state=nullptr, std::unique_ptr< vmcs_intel_x64_state > guest_state=nullptr)
std::unique_ptr< vmcs_intel_x64_state > m_guest_state
virtual void run(user_data *data=nullptr)
Definition: vcpu.cpp:58
std::unique_ptr< vmxon_intel_x64 > m_vmxon
Definition: vcpu.h:94
void run(user_data *data=nullptr) override
virtual void init(user_data *data=nullptr)
Definition: vcpu.cpp:39
virtual bool is_host_vm_vcpu()
Definition: vcpu.h:249
std::unique_ptr< state_save_intel_x64 > m_state_save
virtual void hlt(user_data *data=nullptr)
Definition: vcpu.cpp:66
std::unique_ptr< exit_handler_intel_x64 > m_exit_handler
void init(user_data *data=nullptr) override
void fini(user_data *data=nullptr) override
void hlt(user_data *data=nullptr) override
virtual vcpuid::type id() const
Definition: vcpu.h:209
virtual bool is_initialized()
Definition: vcpu.h:229
std::unique_ptr< vmcs_intel_x64 > m_vmcs
virtual void fini(user_data *data=nullptr)
Definition: vcpu.cpp:47