vcpu_manager.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_manager.h>
24 
25 // -----------------------------------------------------------------------------
26 // Mutex
27 // -----------------------------------------------------------------------------
28 
29 #include <mutex>
30 static std::mutex g_vcpu_manager_mutex;
31 
32 // -----------------------------------------------------------------------------
33 // Implementation
34 // -----------------------------------------------------------------------------
35 
38 {
39  static vcpu_manager self;
40  return &self;
41 }
42 
43 void
45 {
46  auto ___ = gsl::on_failure([&]
47  {
48  std::lock_guard<std::mutex> guard(g_vcpu_manager_mutex);
49  m_vcpus.erase(vcpuid);
50  });
51 
52  if (auto && vcpu = add_vcpu(vcpuid, data))
53  vcpu->init(data);
54 }
55 
56 void
58 {
59  auto ___ = gsl::finally([&]
60  {
61  std::lock_guard<std::mutex> guard(g_vcpu_manager_mutex);
62  m_vcpus.erase(vcpuid);
63  });
64 
65  if (auto && vcpu = get_vcpu(vcpuid))
66  vcpu->fini(data);
67 }
68 
69 void
71 {
72  if (auto && vcpu = get_vcpu(vcpuid))
73  vcpu->run(data);
74 }
75 
76 void
78 {
79  if (auto && vcpu = get_vcpu(vcpuid))
80  vcpu->hlt(data);
81 }
82 
83 void
85 {
86  if (auto && vcpu = m_vcpus[vcpuid])
87  vcpu->write(str);
88 }
89 
90 vcpu_manager::vcpu_manager() noexcept :
91  m_vcpu_factory(std::make_unique<vcpu_factory>())
92 { }
93 
94 std::unique_ptr<vcpu> &
95 vcpu_manager::add_vcpu(vcpuid::type vcpuid, user_data *data)
96 {
97  if (!m_vcpu_factory)
98  throw std::runtime_error("invalid vcpu factory");
99 
100  if (auto && vcpu = get_vcpu(vcpuid))
101  return vcpu;
102 
103  if (auto && vcpu = m_vcpu_factory->make_vcpu(vcpuid, data))
104  {
105  std::lock_guard<std::mutex> guard(g_vcpu_manager_mutex);
106  return m_vcpus[vcpuid] = std::move(vcpu);
107  }
108 
109  throw std::runtime_error("make_vcpu returned a nullptr vcpu");
110 }
111 
112 std::unique_ptr<vcpu> &
113 vcpu_manager::get_vcpu(vcpuid::type vcpuid)
114 {
115  std::lock_guard<std::mutex> guard(g_vcpu_manager_mutex);
116  return m_vcpus[vcpuid];
117 }
virtual void run_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
uint64_t type
Definition: vcpuid.h:31
int64_t unsigned long void * data
virtual void run(user_data *data=nullptr)
Definition: vcpu.cpp:58
Definition: vcpuid.h:29
virtual void create_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
Definition: vcpu.h:94
void uint64_t uint64_t uint64_t *rdx noexcept
virtual void init(user_data *data=nullptr)
Definition: vcpu.cpp:39
virtual void write(vcpuid::type vcpuid, const std::string &str) noexcept
virtual void hlt_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
virtual void hlt(user_data *data=nullptr)
Definition: vcpu.cpp:66
virtual void delete_vcpu(vcpuid::type vcpuid, user_data *data=nullptr)
virtual void write(const std::string &str) noexcept
Definition: vcpu.cpp:74
virtual void fini(user_data *data=nullptr)
Definition: vcpu.cpp:47
static vcpu_manager * instance() noexcept