0
|
1 // Console input and output.
|
|
2 // Input is from the keyboard or serial port.
|
|
3 // Output is written to the screen and serial port.
|
|
4
|
|
5 #include "types.h"
|
|
6 #include "defs.h"
|
|
7 #include "param.h"
|
|
8 #include "spinlock.h"
|
|
9 #include "fs.h"
|
|
10 #include "file.h"
|
|
11 #include "memlayout.h"
|
|
12 #include "mmu.h"
|
|
13 #include "proc.h"
|
|
14
|
32
|
15 __code cbc_consoleread1 ();
|
|
16 __code cbc_consoleread2 ();
|
31
|
17
|
0
|
18 static void consputc (int);
|
|
19
|
|
20 static int panicked = 0;
|
|
21
|
|
22 static struct {
|
|
23 struct spinlock lock;
|
|
24 int locking;
|
|
25 } cons;
|
|
26
|
|
27 static void printint (int xx, int base, int sign)
|
|
28 {
|
|
29 static char digits[] = "0123456789abcdef";
|
|
30 char buf[16];
|
|
31 int i;
|
|
32 uint x;
|
|
33
|
|
34 if (sign && (sign = xx < 0)) {
|
|
35 x = -xx;
|
|
36 } else {
|
|
37 x = xx;
|
|
38 }
|
|
39
|
|
40 i = 0;
|
|
41
|
|
42 do {
|
|
43 buf[i++] = digits[x % base];
|
|
44 } while ((x /= base) != 0);
|
|
45
|
|
46 if (sign) {
|
|
47 buf[i++] = '-';
|
|
48 }
|
|
49
|
|
50 while (--i >= 0) {
|
|
51 consputc(buf[i]);
|
|
52 }
|
|
53 }
|
|
54 //PAGEBREAK: 50
|
|
55
|
|
56 // Print to the console. only understands %d, %x, %p, %s.
|
|
57 void cprintf (char *fmt, ...)
|
|
58 {
|
|
59 int i, c, locking;
|
|
60 uint *argp;
|
|
61 char *s;
|
|
62
|
|
63 locking = cons.locking;
|
|
64
|
|
65 if (locking) {
|
|
66 acquire(&cons.lock);
|
|
67 }
|
|
68
|
|
69 if (fmt == 0) {
|
|
70 panic("null fmt");
|
|
71 }
|
|
72
|
|
73 argp = (uint*) (void*) (&fmt + 1);
|
|
74
|
|
75 for (i = 0; (c = fmt[i] & 0xff) != 0; i++) {
|
|
76 if (c != '%') {
|
|
77 consputc(c);
|
|
78 continue;
|
|
79 }
|
|
80
|
|
81 c = fmt[++i] & 0xff;
|
|
82
|
|
83 if (c == 0) {
|
|
84 break;
|
|
85 }
|
|
86
|
|
87 switch (c) {
|
|
88 case 'd':
|
|
89 printint(*argp++, 10, 1);
|
|
90 break;
|
|
91
|
|
92 case 'x':
|
|
93 case 'p':
|
|
94 printint(*argp++, 16, 0);
|
|
95 break;
|
|
96
|
|
97 case 's':
|
|
98 if ((s = (char*) *argp++) == 0) {
|
|
99 s = "(null)";
|
|
100 }
|
|
101
|
|
102 for (; *s; s++) {
|
|
103 consputc(*s);
|
|
104 }
|
|
105 break;
|
|
106
|
|
107 case '%':
|
|
108 consputc('%');
|
|
109 break;
|
|
110
|
|
111 default:
|
|
112 // Print unknown % sequence to draw attention.
|
|
113 consputc('%');
|
|
114 consputc(c);
|
|
115 break;
|
|
116 }
|
|
117 }
|
|
118
|
|
119 if (locking) {
|
|
120 release(&cons.lock);
|
|
121 }
|
|
122 }
|
|
123
|
30
|
124 __code cbc_panic (char *s)
|
|
125 {
|
|
126 cli();
|
|
127
|
|
128 cons.locking = 0;
|
|
129
|
|
130 cprintf("cpu%d: panic: ", cpu->id);
|
|
131
|
|
132 show_callstk(s);
|
|
133 panicked = 1; // freeze other CPU
|
|
134
|
|
135 while (1)
|
|
136 ;
|
|
137 }
|
|
138
|
0
|
139 void panic (char *s)
|
|
140 {
|
|
141 cli();
|
|
142
|
|
143 cons.locking = 0;
|
|
144
|
|
145 cprintf("cpu%d: panic: ", cpu->id);
|
|
146
|
|
147 show_callstk(s);
|
|
148 panicked = 1; // freeze other CPU
|
|
149
|
|
150 while (1)
|
|
151 ;
|
|
152 }
|
|
153
|
|
154 //PAGEBREAK: 50
|
|
155 #define BACKSPACE 0x100
|
|
156 #define CRTPORT 0x3d4
|
|
157
|
|
158 void consputc (int c)
|
|
159 {
|
|
160 if (panicked) {
|
|
161 cli();
|
|
162 while (1)
|
|
163 ;
|
|
164 }
|
|
165
|
|
166 if (c == BACKSPACE) {
|
|
167 uartputc('\b');
|
|
168 uartputc(' ');
|
|
169 uartputc('\b');
|
|
170 } else {
|
|
171 uartputc(c);
|
|
172 }
|
|
173
|
|
174 // cgaputc(c);
|
|
175 }
|
|
176
|
|
177 #define INPUT_BUF 512
|
|
178 struct {
|
|
179 struct spinlock lock;
|
|
180 char buf[INPUT_BUF];
|
|
181 uint r; // Read index
|
|
182 uint w; // Write index
|
|
183 uint e; // Edit index
|
|
184 } input;
|
|
185
|
|
186 #define C(x) ((x)-'@') // Control-x
|
|
187 void consoleintr (int (*getc) (void))
|
|
188 {
|
|
189 int c;
|
|
190
|
|
191 acquire(&input.lock);
|
|
192
|
|
193 while ((c = getc()) >= 0) {
|
|
194 switch (c) {
|
|
195 case C('P'): // Process listing.
|
|
196 procdump();
|
|
197 break;
|
|
198
|
|
199 case C('U'): // Kill line.
|
|
200 while ((input.e != input.w) && (input.buf[(input.e - 1) % INPUT_BUF] != '\n')) {
|
|
201 input.e--;
|
|
202 consputc(BACKSPACE);
|
|
203 }
|
|
204
|
|
205 break;
|
|
206
|
|
207 case C('H'):
|
|
208 case '\x7f': // Backspace
|
|
209 if (input.e != input.w) {
|
|
210 input.e--;
|
|
211 consputc(BACKSPACE);
|
|
212 }
|
|
213
|
|
214 break;
|
|
215
|
|
216 default:
|
|
217 if ((c != 0) && (input.e - input.r < INPUT_BUF)) {
|
|
218 c = (c == '\r') ? '\n' : c;
|
|
219
|
|
220 input.buf[input.e++ % INPUT_BUF] = c;
|
|
221 consputc(c);
|
|
222
|
|
223 if (c == '\n' || c == C('D') || input.e == input.r + INPUT_BUF) {
|
|
224 input.w = input.e;
|
|
225 wakeup(&input.r);
|
|
226 }
|
|
227 }
|
|
228
|
|
229 break;
|
|
230 }
|
|
231 }
|
|
232
|
|
233 release(&input.lock);
|
|
234 }
|
|
235
|
32
|
236 __code cbc_consoleread2 ()
|
31
|
237 {
|
|
238 struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
|
32
|
239 __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
|
31
|
240 if (input.r == input.w) {
|
|
241 if (proc->killed) {
|
|
242 release(&input.lock);
|
|
243 ilock(ip);
|
|
244 goto next(-1);
|
|
245 }
|
|
246 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
|
|
247 }
|
32
|
248 goto cbc_consoleread1();
|
31
|
249 }
|
|
250
|
32
|
251 __code cbc_consoleread1 ()
|
24
|
252 {
|
|
253 int cont = 1;
|
26
|
254 int n = proc->cbc_arg.cbc_console_arg.n;
|
|
255 int target = proc->cbc_arg.cbc_console_arg.target;
|
|
256 char* dst = proc->cbc_arg.cbc_console_arg.dst;
|
|
257 struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
|
31
|
258 struct file *f = proc->cbc_arg.cbc_console_arg.f;
|
32
|
259 __code(*next)(int ret) = proc->cbc_arg.cbc_console_arg.next;
|
24
|
260
|
26
|
261 int c = input.buf[input.r++ % INPUT_BUF];
|
24
|
262
|
31
|
263 if (c == C('D')) { // EOF
|
|
264 if (n < target) {
|
|
265 // Save ^D for next time, to make sure
|
|
266 // caller gets a 0-byte result.
|
|
267 input.r--;
|
|
268 }
|
|
269 cont = 0;
|
|
270 }
|
24
|
271
|
31
|
272 *dst++ = c;
|
|
273 --n;
|
24
|
274
|
31
|
275 if (c == '\n') {
|
|
276 cont = 0;
|
|
277 }
|
24
|
278
|
32
|
279 if (cont == 1) {
|
|
280 if (n > 0) {
|
|
281 proc->cbc_arg.cbc_console_arg.n = n;
|
|
282 proc->cbc_arg.cbc_console_arg.target = target;
|
|
283 proc->cbc_arg.cbc_console_arg.dst = dst;
|
|
284 proc->cbc_arg.cbc_console_arg.ip = ip;
|
|
285 proc->cbc_arg.cbc_console_arg.f = f;
|
|
286 proc->cbc_arg.cbc_console_arg.next = next;
|
|
287 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
|
|
288 }
|
24
|
289 }
|
|
290
|
|
291 release(&input.lock);
|
|
292 ilock(ip);
|
|
293
|
31
|
294 int r = target - n;
|
|
295
|
|
296 if (r > 0)
|
|
297 f->off += r;
|
|
298 iunlock(f->ip);
|
|
299
|
|
300 //goto next(target - n);
|
|
301
|
32
|
302 goto next(r);
|
24
|
303 }
|
|
304
|
31
|
305 __code cbc_consoleread (struct inode *ip, char *dst, int n, struct file *f, __code(*next)(int ret))
|
29
|
306 {
|
|
307 uint target;
|
|
308
|
|
309 iunlock(ip);
|
|
310
|
|
311 target = n;
|
|
312 acquire(&input.lock);
|
|
313
|
31
|
314 if (n > 0) {
|
|
315 proc->cbc_arg.cbc_console_arg.n = n;
|
|
316 proc->cbc_arg.cbc_console_arg.target = target;
|
|
317 proc->cbc_arg.cbc_console_arg.dst = dst;
|
|
318 proc->cbc_arg.cbc_console_arg.ip = ip;
|
|
319 proc->cbc_arg.cbc_console_arg.f = f;
|
|
320 proc->cbc_arg.cbc_console_arg.next = next;
|
|
321 if (input.r == input.w) {
|
29
|
322 if (proc->killed) {
|
|
323 release(&input.lock);
|
|
324 ilock(ip);
|
|
325 goto next(-1);
|
|
326 }
|
|
327
|
31
|
328 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
|
29
|
329 }
|
31
|
330 goto cbc_consoleread1(0);
|
29
|
331 }
|
|
332 }
|
|
333
|
0
|
334 int consoleread (struct inode *ip, char *dst, int n)
|
|
335 {
|
|
336 uint target;
|
|
337 int c;
|
|
338
|
|
339 iunlock(ip);
|
|
340
|
|
341 target = n;
|
|
342 acquire(&input.lock);
|
|
343
|
|
344 while (n > 0) {
|
|
345 while (input.r == input.w) {
|
|
346 if (proc->killed) {
|
|
347 release(&input.lock);
|
|
348 ilock(ip);
|
|
349 return -1;
|
|
350 }
|
|
351
|
|
352 sleep(&input.r, &input.lock);
|
|
353 }
|
|
354
|
|
355 c = input.buf[input.r++ % INPUT_BUF];
|
|
356
|
|
357 if (c == C('D')) { // EOF
|
|
358 if (n < target) {
|
|
359 // Save ^D for next time, to make sure
|
|
360 // caller gets a 0-byte result.
|
|
361 input.r--;
|
|
362 }
|
|
363
|
|
364 break;
|
|
365 }
|
|
366
|
|
367 *dst++ = c;
|
|
368 --n;
|
|
369
|
|
370 if (c == '\n') {
|
|
371 break;
|
|
372 }
|
|
373 }
|
|
374
|
|
375 release(&input.lock);
|
|
376 ilock(ip);
|
|
377
|
|
378 return target - n;
|
|
379 }
|
|
380
|
|
381 int consolewrite (struct inode *ip, char *buf, int n)
|
|
382 {
|
|
383 int i;
|
|
384
|
|
385 iunlock(ip);
|
|
386
|
|
387 acquire(&cons.lock);
|
|
388
|
|
389 for (i = 0; i < n; i++) {
|
|
390 consputc(buf[i] & 0xff);
|
|
391 }
|
|
392
|
|
393 release(&cons.lock);
|
|
394
|
|
395 ilock(ip);
|
|
396
|
|
397 return n;
|
|
398 }
|
|
399
|
|
400 void consoleinit (void)
|
|
401 {
|
|
402 initlock(&cons.lock, "console");
|
|
403 initlock(&input.lock, "input");
|
|
404
|
|
405 devsw[CONSOLE].write = consolewrite;
|
|
406 devsw[CONSOLE].read = consoleread;
|
27
|
407 //cbc_devsw[CONSOLE].write = cbc_consolewrite;
|
24
|
408 cbc_devsw[CONSOLE].read = cbc_consoleread;
|
0
|
409
|
|
410 cons.locking = 1;
|
|
411 }
|
|
412
|