-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.c
487 lines (428 loc) · 14 KB
/
tty.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*************************************************************************//**
*****************************************************************************
* @file kernel/tty.c
* @brief The terminal driver.
*
* As a common driver, TTY accepts these MESSAGEs:
* - DEV_OPEN
* - DEV_READ
* - DEV_WRITE
*
* Besides, it accepts the other two types of MESSAGE from clock_handler() and
* a PROC (who is not FS):
*
* - MESSAGE from clock_handler(): HARD_INT
* - Every time clock interrupt occurs, the clock handler will check whether
* any key has been pressed. If so, it'll invoke inform_int() to wake up
* TTY. It is a special message because it is not from a process -- clock
* handler is not a process.
*
* - MESSAGE from a PROC: TTY_WRITE
* - TTY is a driver. In most cases MESSAGE is passed from a PROC to FS then
* to TTY. For some historical reason, PROC is allowed to pass a TTY_WRITE
* MESSAGE directly to TTY. Thus a PROC can write to a tty directly.
*
* @note Do not get confused by these function names:
* - tty_dev_read() vs tty_do_read()
* - tty_dev_read() reads chars from keyboard buffer
* - tty_do_read() handles DEV_READ message
* - tty_dev_write() vs tty_do_write() vs tty_write()
* - tty_dev_write() returns chars to a process waiting for input
* - tty_do_write() handles DEV_WRITE message
* - tty_write() handles TTY_WRITE message
*
* @author Forrest Y. Yu
* @date 2008
*****************************************************************************
*****************************************************************************/
#include "type.h"
#include "stdio.h"
#include "const.h"
#include "protect.h"
#include "string.h"
#include "fs.h"
#include "proc.h"
#include "tty.h"
#include "console.h"
#include "global.h"
#include "keyboard.h"
#include "proto.h"
#define TTY_FIRST (tty_table)
#define TTY_END (tty_table + NR_CONSOLES)
PRIVATE void init_tty (TTY* tty);
PRIVATE void tty_dev_read (TTY* tty);
PRIVATE void tty_dev_write (TTY* tty);
PRIVATE void tty_do_read (TTY* tty, MESSAGE* msg);
PRIVATE void tty_do_write (TTY* tty, MESSAGE* msg);
PRIVATE void put_key (TTY* tty, u32 key);
/*****************************************************************************
* task_tty
*****************************************************************************/
/**
* <Ring 1> Main loop of task TTY.
*****************************************************************************/
PUBLIC void task_tty()
{
TTY * tty;
MESSAGE msg;
init_keyboard();
for (tty = TTY_FIRST; tty < TTY_END; tty++)
init_tty(tty);
select_console(0);
while (1) {
for (tty = TTY_FIRST; tty < TTY_END; tty++) {
do {
tty_dev_read(tty);
tty_dev_write(tty);
} while (tty->ibuf_cnt);
}
send_recv(RECEIVE, ANY, &msg);
int src = msg.source;
assert(src != TASK_TTY);
TTY* ptty = &tty_table[msg.DEVICE];
switch (msg.type) {
case DEV_OPEN:
reset_msg(&msg);
msg.type = SYSCALL_RET;
send_recv(SEND, src, &msg);
break;
case DEV_READ:
tty_do_read(ptty, &msg);
break;
case DEV_WRITE:
tty_do_write(ptty, &msg);
break;
case HARD_INT:
/**
* waked up by clock_handler -- a key was just pressed
* @see clock_handler() inform_int()
*/
key_pressed = 0;
continue;
default:
dump_msg("TTY::unknown msg", &msg);
break;
}
}
}
/*****************************************************************************
* init_tty
*****************************************************************************/
/**
* Things to be initialized before a tty can be used:
* -# the input buffer
* -# the corresponding console
*
* @param tty TTY stands for teletype, a cool ancient magic thing.
*****************************************************************************/
PRIVATE void init_tty(TTY* tty)
{
tty->ibuf_cnt = 0;
tty->ibuf_head = tty->ibuf_tail = tty->ibuf;
tty->tty_caller = NO_TASK;
tty->tty_procnr = NO_TASK;
tty->tty_req_buf = 0;
tty->tty_left_cnt = 0;
tty->tty_trans_cnt = 0;
init_screen(tty);
}
/*****************************************************************************
* in_process
*****************************************************************************/
/**
* keyboard_read() will invoke this routine after having recognized a key press.
*
* @param tty The key press is for whom.
* @param key The integer key with metadata.
*****************************************************************************/
PUBLIC void in_process(TTY* tty, u32 key)
{
if (!(key & FLAG_EXT)) {
put_key(tty, key);
}
else {
int raw_code = key & MASK_RAW;
switch(raw_code) {
case ENTER:
put_key(tty, '\n');
break;
case BACKSPACE:
put_key(tty, '\b');
break;
case UP:
if ((key & FLAG_SHIFT_L) ||
(key & FLAG_SHIFT_R)) { /* Shift + Up */
scroll_screen(tty->console, SCR_DN);
}
break;
case DOWN:
if ((key & FLAG_SHIFT_L) ||
(key & FLAG_SHIFT_R)) { /* Shift + Down */
scroll_screen(tty->console, SCR_UP);
}
break;
case F1:
case F2:
case F3:
case F4:
case F5:
case F6:
case F7:
case F8:
case F9:
case F10:
case F11:
case F12:
if ((key & FLAG_CTRL_L) ||
(key & FLAG_CTRL_R)) { /* Alt + F1~F12 */
select_console(raw_code - F1);
}
break;
default:
break;
}
}
}
/*****************************************************************************
* put_key
*****************************************************************************/
/**
* Put a key into the in-buffer of TTY.
*
* @callergraph
*
* @param tty To which TTY the key is put.
* @param key The key. It's an integer whose higher 24 bits are metadata.
*****************************************************************************/
PRIVATE void put_key(TTY* tty, u32 key)
{
if (tty->ibuf_cnt < TTY_IN_BYTES) {
*(tty->ibuf_head) = key;
tty->ibuf_head++;
if (tty->ibuf_head == tty->ibuf + TTY_IN_BYTES)
tty->ibuf_head = tty->ibuf;
tty->ibuf_cnt++;
}
}
/*****************************************************************************
* tty_dev_read
*****************************************************************************/
/**
* Get chars from the keyboard buffer if the TTY::console is the `current'
* console.
*
* @see keyboard_read()
*
* @param tty Ptr to TTY.
*****************************************************************************/
PRIVATE void tty_dev_read(TTY* tty)
{
if (is_current_console(tty->console))
keyboard_read(tty);
}
/*****************************************************************************
* tty_dev_write
*****************************************************************************/
/**
* Echo the char just pressed and transfer it to the waiting process.
*
* @param tty Ptr to a TTY struct.
*****************************************************************************/
PRIVATE void tty_dev_write(TTY* tty)
{
while (tty->ibuf_cnt) {
char ch = *(tty->ibuf_tail);
tty->ibuf_tail++;
if (tty->ibuf_tail == tty->ibuf + TTY_IN_BYTES)
tty->ibuf_tail = tty->ibuf;
tty->ibuf_cnt--;
if (tty->tty_left_cnt) {
if (ch >= ' ' && ch <= '~') { /* printable */
out_char(tty->console, ch);
assert(tty->tty_req_buf);
void * p = tty->tty_req_buf +
tty->tty_trans_cnt;
phys_copy(p, (void *)va2la(TASK_TTY, &ch), 1);
tty->tty_trans_cnt++;
tty->tty_left_cnt--;
}
else if (ch == '\b' && tty->tty_trans_cnt) {
out_char(tty->console, ch);
tty->tty_trans_cnt--;
tty->tty_left_cnt++;
}
if (ch == '\n' || tty->tty_left_cnt == 0) {
out_char(tty->console, '\n');
assert(tty->tty_procnr != NO_TASK);
MESSAGE msg;
msg.type = RESUME_PROC;
msg.PROC_NR = tty->tty_procnr;
msg.CNT = tty->tty_trans_cnt;
send_recv(SEND, tty->tty_caller, &msg);
tty->tty_left_cnt = 0;
}
}
}
}
/*****************************************************************************
* tty_do_read
*****************************************************************************/
/**
* Invoked when task TTY receives DEV_READ message.
*
* @note The routine will return immediately after setting some members of
* TTY struct, telling FS to suspend the proc who wants to read. The real
* transfer (tty buffer -> proc buffer) is not done here.
*
* @param tty From which TTY the caller proc wants to read.
* @param msg The MESSAGE just received.
*****************************************************************************/
PRIVATE void tty_do_read(TTY* tty, MESSAGE* msg)
{
/* tell the tty: */
tty->tty_caller = msg->source; /* who called, usually FS */
tty->tty_procnr = msg->PROC_NR; /* who wants the chars */
tty->tty_req_buf = va2la(tty->tty_procnr,
msg->BUF);/* where the chars should be put */
tty->tty_left_cnt = msg->CNT; /* how many chars are requested */
tty->tty_trans_cnt= 0; /* how many chars have been transferred */
msg->type = SUSPEND_PROC;
send_recv(SEND, tty->tty_caller, msg);
}
/*****************************************************************************
* tty_do_write
*****************************************************************************/
/**
* Invoked when task TTY receives DEV_WRITE message.
*
* @param tty To which TTY the calller proc is bound.
* @param msg The MESSAGE.
*****************************************************************************/
PRIVATE void tty_do_write(TTY* tty, MESSAGE* msg)
{
char buf[TTY_OUT_BUF_LEN];
char * p = (char*)va2la(msg->PROC_NR, msg->BUF);
int i = msg->CNT;
int j;
while (i) {
int bytes = min(TTY_OUT_BUF_LEN, i);
phys_copy(va2la(TASK_TTY, buf), (void*)p, bytes);
for (j = 0; j < bytes; j++)
out_char(tty->console, buf[j]);
i -= bytes;
p += bytes;
}
msg->type = SYSCALL_RET;
send_recv(SEND, msg->source, msg);
}
/*****************************************************************************
* sys_printx
*****************************************************************************/
/**
* System calls accept four parameters. `printx' needs only two, so it wastes
* the other two.
*
* @note `printx' accepts only one parameter -- `char* s', the other one --
* `struct proc * proc' -- is pushed by kernel.asm::sys_call so that the
* kernel can easily know who invoked the system call.
*
* @note s[0] (the first char of param s) is a magic char. if it equals
* MAG_CH_PANIC, then this syscall was invoked by `panic()', which means
* something goes really wrong and the system is to be halted; if it equals
* MAG_CH_ASSERT, then this syscall was invoked by `assert()', which means
* an assertion failure has occured. @see kernel/main lib/misc.c.
*
* @param _unused1 Ignored.
* @param _unused2 Ignored.
* @param s The string to be printed.
* @param p_proc Caller proc.
*
* @return Zero if success.
*****************************************************************************/
PUBLIC int sys_printx(int _unused1, int _unused2, char* s, struct proc* p_proc)
{
const char * p;
char ch;
char reenter_err[] = "? k_reenter is incorrect for unknown reason";
reenter_err[0] = MAG_CH_PANIC;
/**
* @note Code in both Ring 0 and Ring 1~3 may invoke printx().
* If this happens in Ring 0, no linear-physical address mapping
* is needed.
*
* @attention The value of `k_reenter' is tricky here. When
* -# printx() is called in Ring 0
* - k_reenter > 0. When code in Ring 0 calls printx(),
* an `interrupt re-enter' will occur (printx() generates
* a software interrupt). Thus `k_reenter' will be increased
* by `kernel.asm::save' and be greater than 0.
* -# printx() is called in Ring 1~3
* - k_reenter == 0.
*/
if (k_reenter == 0) /* printx() called in Ring<1~3> */
p = va2la(proc2pid(p_proc), s);
else if (k_reenter > 0) /* printx() called in Ring<0> */
p = s;
else /* this should NOT happen */
p = reenter_err;
/**
* @note if assertion fails in any TASK, the system will be halted;
* if it fails in a USER PROC, it'll return like any normal syscall
* does.
*/
if ((*p == MAG_CH_PANIC) ||
(*p == MAG_CH_ASSERT && p_proc_ready < &proc_table[NR_TASKS])) {
disable_int();
char * v = (char*)V_MEM_BASE;
const char * q = p + 1; /* +1: skip the magic char */
while (v < (char*)(V_MEM_BASE + V_MEM_SIZE)) {
*v++ = *q++;
*v++ = RED_CHAR;
if (!*q) {
while (((int)v - V_MEM_BASE) % (SCR_WIDTH * 16)) {
/* *v++ = ' '; */
v++;
*v++ = GRAY_CHAR;
}
q = p + 1;
}
}
__asm__ __volatile__("hlt");
}
while ((ch = *p++) != 0) {
if (ch == MAG_CH_PANIC || ch == MAG_CH_ASSERT)
continue; /* skip the magic char */
/* TTY * ptty; */
/* for (ptty = TTY_FIRST; ptty < TTY_END; ptty++) */
/* out_char(ptty->console, ch); /\* output chars to all TTYs *\/ */
out_char(TTY_FIRST->console, ch);
}
//__asm__ __volatile__("nop;jmp 1f;ud2;1: nop");
//__asm__ __volatile__("nop;cli;1: jmp 1b;ud2;nop");
return 0;
}
/*****************************************************************************
* dump_tty_buf
*****************************************************************************/
/**
* For debug only.
*
*****************************************************************************/
PUBLIC void dump_tty_buf()
{
TTY * tty = &tty_table[1];
static char sep[] = "--------------------------------\n";
printl(sep);
printl("head: %d\n", tty->ibuf_head - tty->ibuf);
printl("tail: %d\n", tty->ibuf_tail - tty->ibuf);
printl("cnt: %d\n", tty->ibuf_cnt);
int pid = tty->tty_caller;
printl("caller: %s (%d)\n", proc_table[pid].name, pid);
pid = tty->tty_procnr;
printl("caller: %s (%d)\n", proc_table[pid].name, pid);
printl("req_buf: %d\n", (int)tty->tty_req_buf);
printl("left_cnt: %d\n", tty->tty_left_cnt);
printl("trans_cnt: %d\n", tty->tty_trans_cnt);
printl("--------------------------------\n");
strcpy(sep, "\n");
}