-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcuart.c
194 lines (169 loc) · 4.7 KB
/
pcuart.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include "riscv.h"
#include "rvexec.h"
/* FIXME */
#define unlikely(C) (__builtin_expect(!!(C), 0))
extern wbk_t *wbk; /* FIXME */
static unsigned char uart[8], rbr, thr;
enum { DR = 0x01, THRE = 0x20, TEMT = 0x40, TR = THRE | TEMT };
static unsigned char lsr;
static volatile sig_atomic_t rxsig, txsig;
static void sigio(int sig) {
int e = errno;
struct pollfd fds[2] = {0};
fds[0].fd = STDIN_FILENO; fds[0].events = POLLIN;
fds[1].fd = STDOUT_FILENO; fds[1].events = POLLOUT;
poll(&fds[0], sizeof fds / sizeof fds[0], 0);
if (fds[0].revents & POLLIN) rxsig = 1;
if (fds[1].revents & POLLOUT) txsig = 1;
errno = e;
}
static void uartwbk(struct hart *t) {
if (lsr & TR) {
lsr &= ~TR;
} else if (txsig) {
txsig = 0; write(STDOUT_FILENO, &thr, 1);
}
if (write(STDOUT_FILENO, &uart[0], 1) == 1)
lsr |= TR;
else
thr = uart[0];
}
static unsigned char *uartmap(struct hart *t, xword_t addr, xword_t size, int type) {
addr &= 0xFFFFF;
if unlikely(addr >= sizeof uart || size != 1) return 0;
switch (addr & 7) {
case 0:
if (uart[3] & 0x80) break;
if (type <= MAPA) {
if (lsr & DR) {
uart[0] = rbr; lsr &= ~DR;
} else if (rxsig) {
rxsig = 0; read(STDIN_FILENO, &uart[0], 1);
}
if (read(STDIN_FILENO, &rbr, 1) == 1)
lsr |= DR;
}
if (type >= MAPA) wbk = &uartwbk;
break;
case 1: uart[1] = 0; break;
case 2: uart[2] = 0; break;
case 4: uart[4] = 0; break;
case 5:
if unlikely(rxsig) {
rxsig = 0;
if (!(lsr & DR)) {
read(STDIN_FILENO, &rbr, 1); lsr |= DR;
}
}
if unlikely(txsig) {
txsig = 0;
if (!(lsr & TR)) {
write(STDOUT_FILENO, &thr, 1); lsr |= TR;
}
}
uart[5] = lsr; break;
case 6: uart[6] = 0; break;
}
return &uart[addr & 7];
}
__attribute__((alias("uartmap"))) map_t meg100;
static struct termios origti;
static void restore(void) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &origti);
}
static struct sigaction satstp;
static void sigtstp(int sig) {
struct sigaction sa; struct termios ti;
sigset_t tstp, mask; int e = errno;
tcgetattr(STDIN_FILENO, &ti);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &origti);
sigaction(sig, &satstp, &sa);
raise(sig);
sigemptyset(&tstp); sigaddset(&tstp, sig);
sigprocmask(SIG_UNBLOCK, &tstp, &mask);
/* take pending TSTP here */
sigprocmask(SIG_SETMASK, &mask, 0);
tcgetattr(STDIN_FILENO, &origti);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &ti);
sigaction(sig, &sa, 0);
errno = e;
}
static struct sigaction saquit;
static void sigquit(int sig) {
restore(); sigaction(sig, &saquit, 0); raise(sig);
}
static struct sigaction saint;
static void sigint(int sig) {
restore(); sigaction(sig, &saint, 0); raise(sig);
}
static struct sigaction saterm;
static void sigterm(int sig) {
restore(); sigaction(sig, &saterm, 0); raise(sig);
}
__attribute__((constructor)) static void uartinit(void) {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
#ifdef O_ASYNC
sa.sa_handler = &sigio;
sigaction(SIGIO, &sa, 0);
if (fcntl(STDIN_FILENO, F_SETOWN, getpid()) < 0) {
perror("fcntl"); exit(EXIT_FAILURE);
}
int fl = fcntl(STDIN_FILENO, F_GETFL);
if (fl < 0 || fcntl(STDIN_FILENO, F_SETFL, fl | O_ASYNC | O_NONBLOCK) == -1) {
perror("fcntl"); exit(EXIT_FAILURE);
}
if ((fcntl(STDIN_FILENO, F_GETFL) & O_ASYNC) != O_ASYNC)
#endif
rxsig = 1;
#ifdef O_ASYNC
if (fcntl(STDOUT_FILENO, F_SETOWN, getpid()) < 0) {
perror("fcntl"); exit(EXIT_FAILURE);
}
fl = fcntl(STDOUT_FILENO, F_GETFL);
if (fl < 0 || fcntl(STDOUT_FILENO, F_SETFL, fl | O_ASYNC | O_NONBLOCK) == -1) {
perror("fcntl"); exit(EXIT_FAILURE);
}
if ((fcntl(STDOUT_FILENO, F_GETFL) & O_ASYNC) != O_ASYNC)
#endif
txsig = 1;
if (isatty(STDIN_FILENO) > 0) {
struct termios ti;
if (tcgetattr(STDIN_FILENO, &origti) < 0) {
perror("tcgetattr"); exit(EXIT_FAILURE);
}
#ifdef SIGTSTP
sa.sa_handler = &sigtstp;
if (sigaction(SIGTSTP, 0, &satstp) >= 0 && satstp.sa_handler != SIG_IGN)
sigaction(SIGTSTP, &sa, 0);
#endif
sa.sa_handler = &sigquit;
if (sigaction(SIGQUIT, 0, &saquit) >= 0 && saquit.sa_handler != SIG_IGN)
sigaction(SIGQUIT, &sa, 0);
sa.sa_handler = &sigint;
if (sigaction(SIGINT, 0, &saint) >= 0 && saint.sa_handler != SIG_IGN)
sigaction(SIGINT, &sa, 0);
/* FIXME SIGPIPE, SIGHUP */
sa.sa_handler = &sigterm;
sigaction(SIGTERM, &sa, &saterm);
atexit(&restore);
ti = origti;
/* FIXME set raw not cbreak? */
ti.c_iflag = ti.c_iflag & ~ICRNL;
ti.c_lflag = ti.c_lflag & ~(ICANON | ECHO) | ISIG;
ti.c_cc[VMIN] = 1; ti.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &ti) < 0) {
perror("tcsetattr"); exit(EXIT_FAILURE);
}
}
}