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