test_portio_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 <vector>
23 
24 #include <test.h>
25 #include <intrinsics/portio_x64.h>
26 
27 using namespace x64;
28 
29 constexpr const auto buf_size = 4;
30 std::map<portio::port_addr_type, portio::port_32bit_type> g_ports;
31 
32 uint8_t g_buf_8bit[buf_size] = {};
33 uint16_t g_buf_16bit[buf_size] = {};
34 uint32_t g_buf_32bit[buf_size] = {};
35 
36 extern "C" uint8_t
37 __inb(uint16_t port) noexcept
38 { return gsl::narrow_cast<portio::port_8bit_type>(g_ports[port]); }
39 
40 extern "C" uint16_t
41 __inw(uint16_t port) noexcept
42 { return gsl::narrow_cast<portio::port_16bit_type>(g_ports[port]); }
43 
44 extern "C" uint32_t
45 __ind(uint16_t port) noexcept
46 { return gsl::narrow_cast<portio::port_32bit_type>(g_ports[port]); }
47 
48 extern "C" void
49 __insb(uint16_t port, uint64_t m8) noexcept
50 { (void) port; __builtin_memcpy(reinterpret_cast<void *>(m8), static_cast<void *>(g_buf_8bit), 1); }
51 
52 extern "C" void
53 __insw(uint16_t port, uint64_t m16) noexcept
54 { (void) port; __builtin_memcpy(reinterpret_cast<void *>(m16), static_cast<void *>(g_buf_16bit), 2); }
55 
56 extern "C" void
57 __insd(uint16_t port, uint64_t m32) noexcept
58 { (void) port; __builtin_memcpy(reinterpret_cast<void *>(m32), static_cast<void *>(g_buf_32bit), 4); }
59 
60 extern "C" void
61 __insbrep(uint16_t port, uint64_t m8, uint32_t count) noexcept
62 { (void) port; __builtin_memcpy(reinterpret_cast<void *>(m8), static_cast<void *>(g_buf_8bit), count * 1); }
63 
64 extern "C" void
65 __inswrep(uint16_t port, uint64_t m16, uint32_t count) noexcept
66 { (void) port; __builtin_memcpy(reinterpret_cast<void *>(m16), static_cast<void *>(g_buf_16bit), count * 2); }
67 
68 extern "C" void
69 __insdrep(uint16_t port, uint64_t m32, uint32_t count) noexcept
70 { (void) port; __builtin_memcpy(reinterpret_cast<void *>(m32), static_cast<void *>(g_buf_32bit), count * 4); }
71 
72 extern "C" void
73 __outb(uint16_t port, uint8_t val) noexcept
74 { g_ports[port] = val; }
75 
76 extern "C" void
77 __outw(uint16_t port, uint16_t val) noexcept
78 { g_ports[port] = val; }
79 
80 extern "C" void
81 __outd(uint16_t port, uint32_t val) noexcept
82 { g_ports[port] = val; }
83 
84 extern "C" void
85 __outsb(uint16_t port, uint64_t m8) noexcept
86 { (void) port; __builtin_memcpy(static_cast<void *>(g_buf_8bit), reinterpret_cast<void *>(m8), 1); }
87 
88 extern "C" void
89 __outsw(uint16_t port, uint64_t m16) noexcept
90 { (void) port; __builtin_memcpy(static_cast<void *>(g_buf_16bit), reinterpret_cast<void *>(m16), 2); }
91 
92 extern "C" void
93 __outsd(uint16_t port, uint64_t m32) noexcept
94 { (void) port; __builtin_memcpy(static_cast<void *>(g_buf_32bit), reinterpret_cast<void *>(m32), 4); }
95 
96 extern "C" void
97 __outsbrep(uint16_t port, uint64_t m8, uint32_t count) noexcept
98 { (void) port; __builtin_memcpy(static_cast<void *>(g_buf_8bit), reinterpret_cast<void *>(m8), count * 1); }
99 
100 extern "C" void
101 __outswrep(uint16_t port, uint64_t m16, uint32_t count) noexcept
102 { (void) port; __builtin_memcpy(static_cast<void *>(g_buf_16bit), reinterpret_cast<void *>(m16), count * 2); }
103 
104 extern "C" void
105 __outsdrep(uint16_t port, uint64_t m32, uint32_t count) noexcept
106 { (void) port; __builtin_memcpy(static_cast<void *>(g_buf_32bit), reinterpret_cast<void *>(m32), count * 4); }
107 
108 void
109 intrinsics_ut::test_portio_x64_byte()
110 {
111  std::vector<uint8_t> buf1 = {0, 1, 2, 3};
112  std::vector<uint8_t> buf2 = {42, 42, 42, 42};
113  std::vector<uint8_t> buf3 = {42, 42, 42, 42};
114 
115  portio::outb(10, 100U);
116  this->expect_true(portio::inb(10) == 100U);
117 
118  portio::outsb(10, buf1.data());
119  portio::insb(10, buf2.data());
120  this->expect_true(buf2.at(0) == 0);
121 
122  portio::outsb(10, reinterpret_cast<portio::integer_pointer>(buf1.data()));
123  portio::insb(10, reinterpret_cast<portio::integer_pointer>(buf2.data()));
124  this->expect_true(buf2.at(0) == 0);
125 
126  portio::outsbrep(10, buf1.data(), buf_size);
127  portio::insbrep(10, buf3.data(), buf_size);
128  this->expect_true(buf3.at(0) == 0);
129  this->expect_true(buf3.at(1) == 1);
130  this->expect_true(buf3.at(2) == 2);
131  this->expect_true(buf3.at(3) == 3);
132 
133  portio::outsbrep(10, reinterpret_cast<portio::integer_pointer>(buf1.data()), buf_size);
134  portio::insbrep(10, reinterpret_cast<portio::integer_pointer>(buf3.data()), buf_size);
135  this->expect_true(buf3.at(0) == 0);
136  this->expect_true(buf3.at(1) == 1);
137  this->expect_true(buf3.at(2) == 2);
138  this->expect_true(buf3.at(3) == 3);
139 }
140 
141 void
142 intrinsics_ut::test_portio_x64_word()
143 {
144  std::vector<uint16_t> buf1 = {0, 1, 2, 3};
145  std::vector<uint16_t> buf2 = {42, 42, 42, 42};
146  std::vector<uint16_t> buf3 = {42, 42, 42, 42};
147 
148  portio::outw(10, 10000U);
149  this->expect_true(portio::inw(10) == 10000U);
150 
151  portio::outsw(10, buf1.data());
152  portio::insw(10, buf2.data());
153  this->expect_true(buf2.at(0) == 0);
154 
155  portio::outsw(10, reinterpret_cast<portio::integer_pointer>(buf1.data()));
156  portio::insw(10, reinterpret_cast<portio::integer_pointer>(buf2.data()));
157  this->expect_true(buf2.at(0) == 0);
158 
159  portio::outswrep(10, buf1.data(), buf_size);
160  portio::inswrep(10, buf3.data(), buf_size);
161  this->expect_true(buf3.at(0) == 0);
162  this->expect_true(buf3.at(1) == 1);
163  this->expect_true(buf3.at(2) == 2);
164  this->expect_true(buf3.at(3) == 3);
165 
166  portio::outswrep(10, reinterpret_cast<portio::integer_pointer>(buf1.data()), buf_size);
167  portio::inswrep(10, reinterpret_cast<portio::integer_pointer>(buf3.data()), buf_size);
168  this->expect_true(buf3.at(0) == 0);
169  this->expect_true(buf3.at(1) == 1);
170  this->expect_true(buf3.at(2) == 2);
171  this->expect_true(buf3.at(3) == 3);
172 }
173 
174 void
175 intrinsics_ut::test_portio_x64_doubleword()
176 {
177  std::vector<uint32_t> buf1 = {0, 1, 2, 3};
178  std::vector<uint32_t> buf2 = {42, 42, 42, 42};
179  std::vector<uint32_t> buf3 = {42, 42, 42, 42};
180 
181  portio::outd(10, 10000U);
182  this->expect_true(portio::ind(10) == 10000U);
183 
184  portio::outsd(10, buf1.data());
185  portio::insd(10, buf2.data());
186  this->expect_true(buf2.at(0) == 0);
187 
188  portio::outsd(10, reinterpret_cast<portio::integer_pointer>(buf1.data()));
189  portio::insd(10, reinterpret_cast<portio::integer_pointer>(buf2.data()));
190  this->expect_true(buf2.at(0) == 0);
191 
192  portio::outsdrep(10, buf1.data(), buf_size);
193  portio::insdrep(10, buf3.data(), buf_size);
194  this->expect_true(buf3.at(0) == 0);
195  this->expect_true(buf3.at(1) == 1);
196  this->expect_true(buf3.at(2) == 2);
197  this->expect_true(buf3.at(3) == 3);
198 
199  portio::outsdrep(10, reinterpret_cast<portio::integer_pointer>(buf1.data()), buf_size);
200  portio::insdrep(10, reinterpret_cast<portio::integer_pointer>(buf3.data()), buf_size);
201  this->expect_true(buf3.at(0) == 0);
202  this->expect_true(buf3.at(1) == 1);
203  this->expect_true(buf3.at(2) == 2);
204  this->expect_true(buf3.at(3) == 3);
205 }
void __insd(uint16_t port, uint64_t m32) noexcept
auto inb(P port) noexcept
Definition: portio_x64.h:65
void __outsbrep(uint16_t port, uint64_t m8, uint32_t count) noexcept
void outsb(P port, integer_pointer m8) noexcept
Definition: portio_x64.h:125
void __outsw(uint16_t port, uint64_t m16) noexcept
void __outd(uint16_t port, uint32_t val) noexcept
auto insbrep(P port, integer_pointer m8, size_type count) noexcept
Definition: portio_x64.h:92
void outw(P port, T val) noexcept
Definition: portio_x64.h:117
void outswrep(P port, integer_pointer m16, size_type count) noexcept
Definition: portio_x64.h:146
void outsbrep(P port, integer_pointer m8, size_type count) noexcept
Definition: portio_x64.h:143
uint16_t g_buf_16bit[buf_size]
void __outsd(uint16_t port, uint64_t m32) noexcept
uint8_t __inb(uint16_t port) noexcept
void outb(P port, T val) noexcept
Definition: portio_x64.h:112
void __inswrep(uint16_t port, uint64_t m16, uint32_t count) noexcept
auto insb(P port, integer_pointer m8) noexcept
Definition: portio_x64.h:74
void outd(P port, T val) noexcept
Definition: portio_x64.h:122
uint16_t port_16bit_type
Definition: portio_x64.h:59
auto insw(P port, integer_pointer m16) noexcept
Definition: portio_x64.h:77
void outsw(P port, integer_pointer m16) noexcept
Definition: portio_x64.h:128
void __outb(uint16_t port, uint8_t val) noexcept
void uint64_t uint64_t uint64_t *rdx noexcept
auto inswrep(P port, integer_pointer m16, size_type count) noexcept
Definition: portio_x64.h:95
void __outswrep(uint16_t port, uint64_t m16, uint32_t count) noexcept
void __outsb(uint16_t port, uint64_t m8) noexcept
constexpr const auto buf_size
uint8_t g_buf_8bit[buf_size]
void outsdrep(P port, integer_pointer m32, size_type count) noexcept
Definition: portio_x64.h:149
uint32_t __ind(uint16_t port) noexcept
void __insb(uint16_t port, uint64_t m8) noexcept
std::map< portio::port_addr_type, portio::port_32bit_type > g_ports
auto insdrep(P port, integer_pointer m32, size_type count) noexcept
Definition: portio_x64.h:98
uint32_t g_buf_32bit[buf_size]
void __insw(uint16_t port, uint64_t m16) noexcept
void outsd(P port, integer_pointer m32) noexcept
Definition: portio_x64.h:131
auto insd(P port, integer_pointer m32) noexcept
Definition: portio_x64.h:80
uint8_t port_8bit_type
Definition: portio_x64.h:58
void __insdrep(uint16_t port, uint64_t m32, uint32_t count) noexcept
Definition: cache_x64.h:31
uint16_t __inw(uint16_t port) noexcept
auto inw(P port) noexcept
Definition: portio_x64.h:68
#define expect_true(a)
auto ind(P port) noexcept
Definition: portio_x64.h:71
uint32_t port_32bit_type
Definition: portio_x64.h:60
void __outsdrep(uint16_t port, uint64_t m32, uint32_t count) noexcept
void __outw(uint16_t port, uint16_t val) noexcept
void __insbrep(uint16_t port, uint64_t m8, uint32_t count) noexcept