annotate src/console.cbc @ 395:17e8a4bc06a7 default tip

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