ioctl_private.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 <exception.h>
23 #include <ioctl_private.h>
24 #include <driver_entry_interface.h>
25 
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <sys/ioctl.h>
29 
30 // -----------------------------------------------------------------------------
31 // Unit Test Seems
32 // -----------------------------------------------------------------------------
33 
34 int
36 {
37  return open("/dev/bareflank", O_RDWR);
38 }
39 
40 int64_t
41 __attribute__((weak)) bf_send_ioctl(int fd, unsigned long request)
42 {
43  return ioctl(fd, request);
44 }
45 
46 int64_t
47 __attribute__((weak)) bf_read_ioctl(int fd, unsigned long request, void *data)
48 {
49  return ioctl(fd, request, data);
50 }
51 
52 int64_t
53 __attribute__((weak)) bf_write_ioctl(int fd, unsigned long request, const void *data)
54 {
55  return ioctl(fd, request, data);
56 }
57 
58 // -----------------------------------------------------------------------------
59 // Implementation
60 // -----------------------------------------------------------------------------
61 
63  fd(0)
64 {
65 }
66 
68 {
69  if (fd >= 0)
70  close(fd);
71 }
72 
73 void
75 {
76  if ((fd = bf_ioctl_open()) < 0)
77  throw driver_inaccessible();
78 }
79 
80 void
82 {
83  expects(len > 0);
84 
85  if (bf_write_ioctl(fd, IOCTL_ADD_MODULE_LENGTH, &len) < 0)
86  throw ioctl_failed(IOCTL_ADD_MODULE_LENGTH);
87 }
88 
89 void
90 ioctl_private::call_ioctl_add_module(gsl::not_null<module_data_type> data)
91 {
92  if (bf_write_ioctl(fd, IOCTL_ADD_MODULE, data) < 0)
93  throw ioctl_failed(IOCTL_ADD_MODULE);
94 }
95 
96 void
98 {
99  if (bf_send_ioctl(fd, IOCTL_LOAD_VMM) < 0)
100  throw ioctl_failed(IOCTL_LOAD_VMM);
101 }
102 
103 void
105 {
106  if (bf_send_ioctl(fd, IOCTL_UNLOAD_VMM) < 0)
107  throw ioctl_failed(IOCTL_UNLOAD_VMM);
108 }
109 
110 void
112 {
113  if (bf_send_ioctl(fd, IOCTL_START_VMM) < 0)
114  throw ioctl_failed(IOCTL_START_VMM);
115 }
116 
117 void
119 {
120  if (bf_send_ioctl(fd, IOCTL_STOP_VMM) < 0)
121  throw ioctl_failed(IOCTL_STOP_VMM);
122 }
123 
124 void
126 {
127  if (bf_write_ioctl(fd, IOCTL_SET_VCPUID, &vcpuid) < 0)
128  throw ioctl_failed(IOCTL_SET_VCPUID);
129 
130  if (bf_read_ioctl(fd, IOCTL_DUMP_VMM, drr) < 0)
131  throw ioctl_failed(IOCTL_DUMP_VMM);
132 }
133 
134 void
135 ioctl_private::call_ioctl_vmm_status(gsl::not_null<status_pointer> status)
136 {
137  if (bf_read_ioctl(fd, IOCTL_VMM_STATUS, status) < 0)
138  throw ioctl_failed(IOCTL_VMM_STATUS);
139 }
140 
141 void
142 ioctl_private::call_ioctl_vmcall(gsl::not_null<registers_pointer> regs, cpuid_type cpuid)
143 {
144  if (bf_write_ioctl(fd, IOCTL_SET_CPUID, &cpuid) < 0)
145  throw ioctl_failed(IOCTL_SET_CPUID);
146 
147  if (bf_write_ioctl(fd, IOCTL_VMCALL, regs) < 0)
148  throw ioctl_failed(IOCTL_VMCALL);
149 }
virtual void call_ioctl_stop_vmm()
expects(rbx)
int64_t unsigned long request
int64_t bf_read_ioctl(int64_t fd, unsigned long request, void *data)
int64_t bf_ioctl_open()
virtual void call_ioctl_load_vmm()
int64_t unsigned long void * data
virtual void call_ioctl_add_module_length(module_len_type len)
Definition: vcpuid.h:29
virtual void call_ioctl_vmm_status(gsl::not_null< status_pointer > status)
debug_ring_resources_t * drr
virtual void call_ioctl_start_vmm()
virtual void call_ioctl_dump_vmm(gsl::not_null< drr_pointer > drr, vcpuid_type vcpuid)
#define driver_inaccessible(a)
Definition: exception.h:242
int64_t bf_write_ioctl(int64_t fd, unsigned long request, const void *data)
#define ioctl_failed(a)
Definition: exception.h:262
~ioctl_private() override
virtual void call_ioctl_vmcall(gsl::not_null< registers_pointer > regs, cpuid_type cpuid)
int open(const char *file, int mode,...)
Definition: syscall.cpp:261
int __attribute__((weak)) bf_ioctl_open()
virtual void call_ioctl_add_module(gsl::not_null< module_data_type > data)
int close(int fd)
Definition: syscall.cpp:317
size_t module_len_type
Definition: ioctl_private.h:31
ioctl::vcpuid_type vcpuid_type
Definition: ioctl_private.h:35
virtual void open()
ioctl::cpuid_type cpuid_type
Definition: ioctl_private.h:34
int64_t bf_send_ioctl(int64_t fd, unsigned long request)
virtual void call_ioctl_unload_vmm()
Definition: ioctl.h:48