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