forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_tty.cpp
165 lines (133 loc) · 3.65 KB
/
sys_tty.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "stdafx.h"
#include "Emu/system_config.h"
#include "Emu/Cell/PPUThread.h"
#include "Emu/Cell/timers.hpp"
#include "sys_tty.h"
#include <deque>
#include <mutex>
LOG_CHANNEL(sys_tty);
extern fs::file g_tty;
extern atomic_t<s64> g_tty_size;
extern std::array<std::deque<std::string>, 16> g_tty_input;
extern std::mutex g_tty_mutex;
error_code sys_tty_read(s32 ch, vm::ptr<char> buf, u32 len, vm::ptr<u32> preadlen)
{
sys_tty.trace("sys_tty_read(ch=%d, buf=*0x%x, len=%d, preadlen=*0x%x)", ch, buf, len, preadlen);
if (!g_cfg.core.debug_console_mode)
{
return CELL_EIO;
}
if (ch > 15 || ch < 0 || !buf)
{
return CELL_EINVAL;
}
if (ch < SYS_TTYP_USER1)
{
sys_tty.warning("sys_tty_read called with system channel %d", ch);
}
usz chars_to_read = 0; // number of chars that will be read from the input string
std::string tty_read; // string for storage of read chars
if (len > 0)
{
std::lock_guard lock(g_tty_mutex);
if (!g_tty_input[ch].empty())
{
// reference to our first queue element
std::string& input = g_tty_input[ch].front();
// we have to stop reading at either a new line, the param len, or our input string size
usz new_line_pos = input.find_first_of('\n');
if (new_line_pos != input.npos)
{
chars_to_read = std::min(new_line_pos, static_cast<usz>(len));
}
else
{
chars_to_read = std::min(input.size(), static_cast<usz>(len));
}
// read the previously calculated number of chars from the beginning of the input string
tty_read = input.substr(0, chars_to_read);
// remove the just read text from the input string
input = input.substr(chars_to_read, input.size() - 1);
if (input.empty())
{
// pop the first queue element if it was completely consumed
g_tty_input[ch].pop_front();
}
}
}
if (!preadlen)
{
return CELL_EFAULT;
}
*preadlen = static_cast<u32>(chars_to_read);
if (chars_to_read > 0)
{
std::memcpy(buf.get_ptr(), tty_read.c_str(), chars_to_read);
sys_tty.success("sys_tty_read(ch=%d, len=%d) read %s with length %d", ch, len, tty_read, *preadlen);
}
return CELL_OK;
}
error_code sys_tty_write(ppu_thread& ppu, s32 ch, vm::cptr<char> buf, u32 len, vm::ptr<u32> pwritelen)
{
sys_tty.notice("sys_tty_write(ch=%d, buf=*0x%x, len=%d, pwritelen=*0x%x)", ch, buf, len, pwritelen);
std::string msg;
if (static_cast<s32>(len) > 0 && vm::check_addr(buf.addr(), vm::page_readable, len))
{
msg.resize(len);
if (!vm::try_access(buf.addr(), msg.data(), len, false))
{
msg.clear();
}
}
if (msg.find("abort"sv) != umax || msg.find("error"sv) != umax || [&]()
{
static atomic_t<u64> last_write = 0;
// Dump thread about every period which TTY was not being touched for about half a second
const u64 current = get_system_time();
return current - last_write.exchange(current) >= 500'000;
}())
{
std::string dump_useful_thread_info();
ppu_log.notice("\n%s", dump_useful_thread_info());
}
// Hack: write to tty even on CEX mode, but disable all error checks
if (ch < 0 || ch > 15)
{
if (g_cfg.core.debug_console_mode)
{
return CELL_EINVAL;
}
else
{
msg.clear();
}
}
if (g_cfg.core.debug_console_mode)
{
// Don't modify it in CEX mode
len = static_cast<s32>(len) > 0 ? len : 0;
}
if (static_cast<s32>(len) > 0)
{
if (!msg.empty())
{
sys_tty.notice(u8"sys_tty_write(): “%s”", msg);
if (g_tty)
{
// Lock size by making it negative
g_tty_size -= (1ll << 48);
g_tty.write(msg);
g_tty_size += (1ll << 48) + len;
}
}
else if (g_cfg.core.debug_console_mode)
{
return {CELL_EFAULT, buf.addr()};
}
}
if (!pwritelen.try_write(len))
{
return {CELL_EFAULT, pwritelen};
}
return CELL_OK;
}