-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterm.c
279 lines (223 loc) · 5.72 KB
/
term.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <poll.h>
#include <stdbool.h>
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/uio.h>
#include <signal.h>
#include <pty.h>
#include <utmp.h>
#include "kvm/read-write.h"
#include "kvm/term.h"
#include "kvm/util.h"
#include "kvm/kvm.h"
#include "kvm/kvm-cpu.h"
#include <popcorn/utils.h>
#define TERM_FD_IN 0
#define TERM_FD_OUT 1
static struct termios orig_term;
static int term_fds[TERM_MAX_DEVS][2];
static pthread_t term_poll_thread;
/* ctrl-a is used for escape */
#define term_escape_char 0x01
int term_getc(struct kvm *kvm, int term)
{
static bool term_got_escape = false;
unsigned char c;
if (read_in_full(term_fds[term][TERM_FD_IN], &c, 1) < 0)
return -1;
if (term_got_escape) {
term_got_escape = false;
if (c == 'x')
kvm__reboot(kvm);
if (c == term_escape_char)
return c;
}
if (c == term_escape_char) {
term_got_escape = true;
return -1;
}
return c;
}
int term_putc(char *addr, int cnt, int term)
{
int ret;
int num_remaining = cnt;
while (num_remaining) {
#ifdef CONFIG_POPCORN_HYPE
#if PERF_CRITICAL_DEBUG
/* This includes TX */
#endif
#endif
ret = write(term_fds[term][TERM_FD_OUT], addr, num_remaining);
if (ret < 0)
return cnt - num_remaining;
num_remaining -= ret;
addr += ret;
}
return cnt;
}
int term_getc_iov(struct kvm *kvm, struct iovec *iov, int iovcnt, int term)
{
int c;
c = term_getc(kvm, term);
if (c < 0)
return 0;
*((char *)iov[TERM_FD_IN].iov_base) = (char)c;
return sizeof(char);
}
int term_putc_iov(struct iovec *iov, int iovcnt, int term)
{
#ifdef CONFIG_POPCORN_HYPE
#if PERF_CRITICAL_DEBUG
if (pop_get_nid()) { BUG_ON(-1); }
#endif
#endif
return writev(term_fds[term][TERM_FD_OUT], iov, iovcnt);
}
bool term_readable(int term)
{
struct pollfd pollfd = (struct pollfd) {
.fd = term_fds[term][TERM_FD_IN],
.events = POLLIN,
.revents = 0,
};
int err;
err = poll(&pollfd, 1, 0);
return (err > 0 && (pollfd.revents & POLLIN));
}
static void *term_poll_thread_loop(void *param)
{
struct pollfd fds[TERM_MAX_DEVS];
struct kvm *kvm = (struct kvm *) param;
int i;
//#if DEBUG_SERIAL
#ifdef CONFIG_POPCORN_HYPE
POP_PRINTF("\tt================= problem ======================\n");
POP_PRINTF("\tt================================================\n");
POP_PRINTF("\tt[%d/%d] %s %s(): this thread \"term-poll\"\n",
pop_get_nid(), popcorn_gettid(), __FILE__, __func__);
POP_PRINTF("\tt================================================\n");
POP_PRINTF("\tt================================================\n");
#endif
//#endif
kvm__set_thread_name("term-poll");
for (i = 0; i < TERM_MAX_DEVS; i++) {
fds[i].fd = term_fds[i][TERM_FD_IN];
fds[i].events = POLLIN;
fds[i].revents = 0;
}
while (1) {
/* Poll with infinite timeout */
if(poll(fds, TERM_MAX_DEVS, -1) < 1)
break;
#if DEBUG_SERIAL
static unsigned long cnt = 0;
cnt++;
if (cnt < 1000 || !(cnt % 1000))
CONSOLEPRINTF("\n[%d/%d] %s(): console: got #%lu\n",
pop_get_nid(), popcorn_gettid(), __func__, cnt);
else if (cnt > 1000) {
//die("console: > #1000....");
//break;
}
#endif
kvm__arch_read_term(kvm); // arch
}
die("term_poll_thread_loop: error polling device fds %d\n", errno);
return NULL;
}
static void term_cleanup(void)
{
int i;
for (i = 0; i < TERM_MAX_DEVS; i++)
tcsetattr(term_fds[i][TERM_FD_IN], TCSANOW, &orig_term);
}
static void term_sig_cleanup(int sig)
{
term_cleanup();
signal(sig, SIG_DFL);
raise(sig);
}
static void term_set_tty(int term)
{
struct termios orig_term;
int master, slave;
char new_pty[PATH_MAX];
#ifdef CONFIG_POPCORN_HYPE
POP_PRINTF("\t\t[%d/%d] %s():\n",
pop_get_nid(), popcorn_gettid(), __func__);
#endif
if (tcgetattr(STDIN_FILENO, &orig_term) < 0)
die("unable to save initial standard input settings");
orig_term.c_lflag &= ~(ICANON | ECHO | ISIG);
// if (openpty(&master, &slave, new_pty, &orig_term, NULL) < 0)
// return;
master = slave = 0xabcdef;
close(slave);
pr_info("Assigned terminal %d to pty %s\n", term, new_pty);
term_fds[term][TERM_FD_IN] = term_fds[term][TERM_FD_OUT] = master;
}
int tty_parser(const struct option *opt, const char *arg, int unset)
{
int tty = atoi(arg);
#ifdef CONFIG_POPCORN_HYPE
POP_PRINTF("\t[%d/%d] %s():\n",
pop_get_nid(), popcorn_gettid(), __func__);
#endif
term_set_tty(tty);
return 0;
}
static int term_init(struct kvm *kvm)
{
struct termios term;
int i, r;
#ifdef CONFIG_POPCORN_HYPE
if (pop_get_nid()) {
POP_PRINTF("\t[%d/%d] %s(): SKIP term..................\n",
pop_get_nid(), popcorn_gettid(), __func__);
return 0;
} else {
POP_PRINTF("\t%s(): <*> term\n", __func__);
}
#endif
for (i = 0; i < TERM_MAX_DEVS; i++) {
#ifdef CONFIG_POPCORN_HYPE
// #define STDIN_FILENO 0
// #define STDOUT_FILENO 1
// #define STDERR_FILENO 2
POP_PRINTF("\t\ttry set up dev %d\n", i);
#endif
if (term_fds[i][TERM_FD_IN] == 0) {
#ifdef CONFIG_POPCORN_HYPE
POP_PRINTF("\t\t\tset up dev %d\n", i);
#endif
term_fds[i][TERM_FD_IN] = STDIN_FILENO;
term_fds[i][TERM_FD_OUT] = STDOUT_FILENO;
}
}
/* Verify terminals */
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
return 0;
r = tcgetattr(STDIN_FILENO, &orig_term);
if (r < 0) {
pr_warning("unable to save initial standard input settings");
return r;
}
term = orig_term;
term.c_iflag &= ~(ICRNL);
term.c_lflag &= ~(ICANON | ECHO | ISIG);
tcsetattr(STDIN_FILENO, TCSANOW, &term);
/* Use our own blocking thread to read stdin, don't require a tick */
if(pthread_create(&term_poll_thread, NULL, term_poll_thread_loop,kvm))
die("Unable to create console input poll thread\n");
signal(SIGTERM, term_sig_cleanup);
atexit(term_cleanup);
return 0;
}
dev_init(term_init);
static int term_exit(struct kvm *kvm)
{
return 0;
}
dev_exit(term_exit);