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