forked from Bareflank/hypervisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unistd.cpp
109 lines (95 loc) · 2.83 KB
/
unistd.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
//
// Copyright (C) 2019 Assured Information Security, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <bfgsl.h>
#include <bfexports.h>
#include <debug/debug_ring/debug_ring.h>
#include <debug/serial/serial_ns16550a.h>
#include <debug/serial/serial_pl011.h>
#include <hve/arch/intel_x64/vcpu.h>
#include <xue.h>
#include <mutex>
std::mutex g_write_mutex;
extern struct xue g_xue;
extern "C" void
unlock_write(void)
{ g_write_mutex.unlock(); }
static auto
g_debug_ring() noexcept
{
static bfvmm::debug_ring dr{vcpuid::invalid};
return &dr;
}
#ifdef USE_XUE
extern "C" void
debug_ring_write(const std::string &str)
{
std::lock_guard<std::mutex> guard(g_write_mutex);
g_debug_ring()->write(str);
}
#endif
extern "C" uint64_t
write_str(const std::string &str)
{
try {
std::lock_guard<std::mutex> guard(g_write_mutex);
g_debug_ring()->write(str);
#ifdef USE_XUE
xue_write(&g_xue, (const char *)str.data(), str.size());
#else
for (const auto c : str) {
bfvmm::DEFAULT_COM_DRIVER::instance()->write(c);
}
#endif
}
catch (...) {
return 0;
}
return str.length();
}
extern "C" uint64_t
unsafe_write_cstr(const char *cstr, size_t len)
{
try {
#ifdef USE_XUE
xue_write(&g_xue, cstr, len);
#else
auto str = gsl::make_span(cstr, gsl::narrow_cast<std::ptrdiff_t>(len));
for (const auto &c : str) {
bfvmm::DEFAULT_COM_DRIVER::instance()->write(c);
}
#endif
}
catch (...) {
return 0;
}
return len;
}
extern "C" int
write(int __fd, const void *__buf, size_t __nbyte)
{
if (__buf == nullptr || __nbyte == 0) {
return 0;
}
if (__fd != 1 && __fd != 2) {
return 0;
}
return gsl::narrow_cast<int>(write_str(std::string(static_cast<const char *>(__buf), __nbyte)));
}