forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.c
319 lines (276 loc) · 5.32 KB
/
console.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
#include "types.h"
#include "x86.h"
#include "traps.h"
#include "defs.h"
#include "spinlock.h"
#include "dev.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "kbd.h"
struct spinlock console_lock;
int panicked = 0;
int use_console_lock = 0;
// Copy console output to parallel port, which you can tell
// .bochsrc to copy to the stdout:
// parport1: enabled=1, file="/dev/stdout"
static void
lpt_putc(int c)
{
int i;
for(i = 0; !(inb(0x378+1) & 0x80) && i < 12800; i++)
;
outb(0x378+0, c);
outb(0x378+2, 0x08|0x04|0x01);
outb(0x378+2, 0x08);
}
static void
cons_putc(int c)
{
int crtport = 0x3d4; // io port of CGA
ushort *crt = (ushort*) 0xB8000; // base of CGA memory
int ind;
if(panicked){
cli();
for(;;)
;
}
lpt_putc(c);
// cursor position, 16 bits, col + 80*row
outb(crtport, 14);
ind = inb(crtport + 1) << 8;
outb(crtport, 15);
ind |= inb(crtport + 1);
c &= 0xff;
if(c == '\n'){
ind -= (ind % 80);
ind += 80;
} else {
c |= 0x0700; // black on white
crt[ind] = c;
ind++;
}
if((ind / 80) >= 24){
// scroll up
memmove(crt, crt + 80, sizeof(crt[0]) * (23 * 80));
ind -= 80;
memset(crt + ind, 0, sizeof(crt[0]) * ((24 * 80) - ind));
}
outb(crtport, 14);
outb(crtport + 1, ind >> 8);
outb(crtport, 15);
outb(crtport + 1, ind);
}
void
printint(int xx, int base, int sgn)
{
char buf[16];
char digits[] = "0123456789ABCDEF";
int i = 0, neg = 0;
uint x;
if(sgn && xx < 0){
neg = 1;
x = 0 - xx;
} else {
x = xx;
}
do {
buf[i++] = digits[x % base];
} while((x /= base) != 0);
if(neg)
buf[i++] = '-';
while(--i >= 0)
cons_putc(buf[i]);
}
// Print to the console. only understands %d, %x, %p, %s.
void
cprintf(char *fmt, ...)
{
int i, c, state, locking;
uint *argp;
char *s;
locking = use_console_lock;
if(locking)
acquire(&console_lock);
argp = (uint*)(void*)&fmt + 1;
state = 0;
for(i = 0; fmt[i]; i++){
c = fmt[i] & 0xff;
switch(state){
case 0:
if(c == '%')
state = '%';
else
cons_putc(c);
break;
case '%':
switch(c){
case 'd':
printint(*argp++, 10, 1);
break;
case 'x':
case 'p':
printint(*argp++, 16, 0);
break;
case 's':
s = (char*)*argp++;
if(s == 0)
s = "(null)";
for(; *s; s++)
cons_putc(*s);
break;
case '%':
cons_putc('%');
break;
default:
// Print unknown % sequence to draw attention.
cons_putc('%');
cons_putc(c);
break;
}
state = 0;
break;
}
}
if(locking)
release(&console_lock);
}
void
panic(char *s)
{
int i;
uint pcs[10];
__asm __volatile("cli");
use_console_lock = 0;
cprintf("panic (%d): ", cpu());
cprintf(s, 0);
cprintf("\n", 0);
getcallerpcs(&s, pcs);
for(i=0; i<10; i++)
cprintf(" %p", pcs[i]);
panicked = 1; // freeze other CPU
for(;;)
;
}
int
console_write(int minor, char *buf, int n)
{
int i;
acquire(&console_lock);
for(i = 0; i < n; i++)
cons_putc(buf[i] & 0xff);
release(&console_lock);
return n;
}
#define KBD_BUF 64
struct {
uchar buf[KBD_BUF];
int r;
int w;
struct spinlock lock;
} kbd;
void
kbd_intr(void)
{
static uint shift;
static uchar *charcode[4] = {
normalmap,
shiftmap,
ctlmap,
ctlmap
};
uint st, data, c;
acquire(&kbd.lock);
st = inb(KBSTATP);
if((st & KBS_DIB) == 0)
goto out;
data = inb(KBDATAP);
if(data == 0xE0) {
shift |= E0ESC;
goto out;
} else if(data & 0x80) {
// Key released
data = (shift & E0ESC ? data : data & 0x7F);
shift &= ~(shiftcode[data] | E0ESC);
goto out;
} else if(shift & E0ESC) {
// Last character was an E0 escape; or with 0x80
data |= 0x80;
shift &= ~E0ESC;
}
shift |= shiftcode[data];
shift ^= togglecode[data];
c = charcode[shift & (CTL | SHIFT)][data];
if(shift & CAPSLOCK) {
if('a' <= c && c <= 'z')
c += 'A' - 'a';
else if('A' <= c && c <= 'Z')
c += 'a' - 'A';
}
switch(c){
case 0:
// Ignore unknown keystrokes.
break;
case C('T'):
cprintf("#"); // Let user know we're still alive.
break;
case C('P'):
procdump();
break;
default:
if(((kbd.w + 1) % KBD_BUF) != kbd.r){
kbd.buf[kbd.w++] = c;
if(kbd.w >= KBD_BUF)
kbd.w = 0;
wakeup(&kbd.r);
}
break;
}
out:
release(&kbd.lock);
}
//PAGEBREAK: 25
int
console_read(int minor, char *dst, int n)
{
uint target;
int c;
target = n;
acquire(&kbd.lock);
while(n > 0){
while(kbd.r == kbd.w){
if(cp->killed){
release(&kbd.lock);
return -1;
}
sleep(&kbd.r, &kbd.lock);
}
c = kbd.buf[kbd.r++];
if(c == C('D')){ // EOF
if(n < target){
// Save ^D for next time, to make sure
// caller gets a 0-byte result.
kbd.r--;
}
break;
}
*dst++ = c;
cons_putc(c);
--n;
if(kbd.r >= KBD_BUF)
kbd.r = 0;
}
release(&kbd.lock);
return target - n;
}
void
console_init(void)
{
initlock(&console_lock, "console");
initlock(&kbd.lock, "kbd");
devsw[CONSOLE].write = console_write;
devsw[CONSOLE].read = console_read;
use_console_lock = 1;
irq_enable(IRQ_KBD);
ioapic_enable(IRQ_KBD, 0);
}