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
|
31
|
15 __code cbc_consoleread1 (__code(*)(int));
|
|
16 __code cbc_consoleread2 (__code(*)(int));
|
|
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
|
31
|
236 __code cbc_consoleread2 (__code(*next)(int ret))
|
|
237 {
|
|
238 int n = proc->cbc_arg.cbc_console_arg.n;
|
|
239 int target = proc->cbc_arg.cbc_console_arg.target;
|
|
240 char* dst = proc->cbc_arg.cbc_console_arg.dst;
|
|
241 struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
|
|
242 struct file *f = proc->cbc_arg.cbc_console_arg.f;
|
|
243 proc->cbc_arg.cbc_console_arg.n = n;
|
|
244 proc->cbc_arg.cbc_console_arg.target = target;
|
|
245 proc->cbc_arg.cbc_console_arg.dst = dst;
|
|
246 proc->cbc_arg.cbc_console_arg.ip = ip;
|
|
247 proc->cbc_arg.cbc_console_arg.f = f;
|
|
248 proc->cbc_arg.cbc_console_arg.next = next;
|
|
249 if (input.r == input.w) {
|
|
250 if (proc->killed) {
|
|
251 release(&input.lock);
|
|
252 ilock(ip);
|
|
253 goto next(-1);
|
|
254 }
|
|
255 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
|
|
256 }
|
|
257 goto cbc_consoleread1(0);
|
|
258 }
|
|
259
|
24
|
260 __code cbc_consoleread1 (__code(*next)(int ret))
|
|
261 {
|
|
262 int cont = 1;
|
26
|
263 int n = proc->cbc_arg.cbc_console_arg.n;
|
|
264 int target = proc->cbc_arg.cbc_console_arg.target;
|
|
265 char* dst = proc->cbc_arg.cbc_console_arg.dst;
|
|
266 struct inode *ip = proc->cbc_arg.cbc_console_arg.ip;
|
31
|
267 struct file *f = proc->cbc_arg.cbc_console_arg.f;
|
24
|
268
|
26
|
269 int c = input.buf[input.r++ % INPUT_BUF];
|
24
|
270
|
31
|
271 if (c == C('D')) { // EOF
|
|
272 if (n < target) {
|
|
273 // Save ^D for next time, to make sure
|
|
274 // caller gets a 0-byte result.
|
|
275 input.r--;
|
|
276 }
|
|
277 cont = 0;
|
|
278 }
|
24
|
279
|
31
|
280 *dst++ = c;
|
|
281 --n;
|
24
|
282
|
31
|
283 if (c == '\n') {
|
|
284 cont = 0;
|
|
285 }
|
24
|
286
|
31
|
287 if (n > 0) {
|
24
|
288 proc->cbc_arg.cbc_console_arg.n = n;
|
31
|
289 proc->cbc_arg.cbc_console_arg.target = target;
|
24
|
290 proc->cbc_arg.cbc_console_arg.dst = dst;
|
|
291 proc->cbc_arg.cbc_console_arg.ip = ip;
|
31
|
292 proc->cbc_arg.cbc_console_arg.f = f;
|
|
293 proc->cbc_arg.cbc_console_arg.next = next;
|
|
294 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
|
24
|
295 }
|
|
296
|
|
297 release(&input.lock);
|
|
298 ilock(ip);
|
|
299
|
31
|
300 int r = target - n;
|
|
301
|
|
302 if (r > 0)
|
|
303 f->off += r;
|
|
304 iunlock(f->ip);
|
|
305
|
|
306 //goto next(target - n);
|
|
307
|
|
308 goto cbc_ret(r);
|
24
|
309 }
|
|
310
|
31
|
311 __code cbc_consoleread (struct inode *ip, char *dst, int n, struct file *f, __code(*next)(int ret))
|
29
|
312 {
|
|
313 uint target;
|
|
314
|
|
315 iunlock(ip);
|
|
316
|
|
317 target = n;
|
|
318 acquire(&input.lock);
|
|
319
|
31
|
320 if (n > 0) {
|
|
321 proc->cbc_arg.cbc_console_arg.n = n;
|
|
322 proc->cbc_arg.cbc_console_arg.target = target;
|
|
323 proc->cbc_arg.cbc_console_arg.dst = dst;
|
|
324 proc->cbc_arg.cbc_console_arg.ip = ip;
|
|
325 proc->cbc_arg.cbc_console_arg.f = f;
|
|
326 proc->cbc_arg.cbc_console_arg.next = next;
|
|
327 if (input.r == input.w) {
|
29
|
328 if (proc->killed) {
|
|
329 release(&input.lock);
|
|
330 ilock(ip);
|
|
331 goto next(-1);
|
|
332 }
|
|
333
|
31
|
334 goto cbc_sleep(&input.r, &input.lock, cbc_consoleread2);
|
29
|
335 }
|
31
|
336 goto cbc_consoleread1(0);
|
29
|
337 }
|
|
338 }
|
|
339
|
0
|
340 int consoleread (struct inode *ip, char *dst, int n)
|
|
341 {
|
|
342 uint target;
|
|
343 int c;
|
|
344
|
|
345 iunlock(ip);
|
|
346
|
|
347 target = n;
|
|
348 acquire(&input.lock);
|
|
349
|
|
350 while (n > 0) {
|
|
351 while (input.r == input.w) {
|
|
352 if (proc->killed) {
|
|
353 release(&input.lock);
|
|
354 ilock(ip);
|
|
355 return -1;
|
|
356 }
|
|
357
|
|
358 sleep(&input.r, &input.lock);
|
|
359 }
|
|
360
|
|
361 c = input.buf[input.r++ % INPUT_BUF];
|
|
362
|
|
363 if (c == C('D')) { // EOF
|
|
364 if (n < target) {
|
|
365 // Save ^D for next time, to make sure
|
|
366 // caller gets a 0-byte result.
|
|
367 input.r--;
|
|
368 }
|
|
369
|
|
370 break;
|
|
371 }
|
|
372
|
|
373 *dst++ = c;
|
|
374 --n;
|
|
375
|
|
376 if (c == '\n') {
|
|
377 break;
|
|
378 }
|
|
379 }
|
|
380
|
|
381 release(&input.lock);
|
|
382 ilock(ip);
|
|
383
|
|
384 return target - n;
|
|
385 }
|
|
386
|
|
387 int consolewrite (struct inode *ip, char *buf, int n)
|
|
388 {
|
|
389 int i;
|
|
390
|
|
391 iunlock(ip);
|
|
392
|
|
393 acquire(&cons.lock);
|
|
394
|
|
395 for (i = 0; i < n; i++) {
|
|
396 consputc(buf[i] & 0xff);
|
|
397 }
|
|
398
|
|
399 release(&cons.lock);
|
|
400
|
|
401 ilock(ip);
|
|
402
|
|
403 return n;
|
|
404 }
|
|
405
|
|
406 void consoleinit (void)
|
|
407 {
|
|
408 initlock(&cons.lock, "console");
|
|
409 initlock(&input.lock, "input");
|
|
410
|
|
411 devsw[CONSOLE].write = consolewrite;
|
|
412 devsw[CONSOLE].read = consoleread;
|
27
|
413 //cbc_devsw[CONSOLE].write = cbc_consolewrite;
|
24
|
414 cbc_devsw[CONSOLE].read = cbc_consoleread;
|
0
|
415
|
|
416 cons.locking = 1;
|
|
417 }
|
|
418
|