Rev 3386 | Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3386 | Rev 4153 | ||
---|---|---|---|
Line 37... | Line 37... | ||
37 | #include <console/chardev.h> |
37 | #include <console/chardev.h> |
38 | #include <sysinfo/sysinfo.h> |
38 | #include <sysinfo/sysinfo.h> |
39 | #include <synch/waitq.h> |
39 | #include <synch/waitq.h> |
40 | #include <synch/spinlock.h> |
40 | #include <synch/spinlock.h> |
41 | #include <arch/types.h> |
41 | #include <arch/types.h> |
42 | #include <ddi/device.h> |
- | |
43 | #include <ddi/irq.h> |
42 | #include <ddi/irq.h> |
44 | #include <ddi/ddi.h> |
43 | #include <ddi/ddi.h> |
45 | #include <ipc/irq.h> |
44 | #include <ipc/irq.h> |
46 | #include <arch.h> |
45 | #include <arch.h> |
47 | #include <func.h> |
46 | #include <func.h> |
48 | #include <print.h> |
47 | #include <print.h> |
49 | #include <atomic.h> |
48 | #include <atomic.h> |
- | 49 | #include <syscall/copy.h> |
|
- | 50 | #include <errno.h> |
|
50 | 51 | ||
51 | #define KLOG_SIZE PAGE_SIZE |
52 | #define KLOG_SIZE PAGE_SIZE |
52 | #define KLOG_LATENCY 8 |
53 | #define KLOG_LATENCY 8 |
53 | 54 | ||
54 | /**< Kernel log cyclic buffer */ |
55 | /** Kernel log cyclic buffer */ |
55 | static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
56 | static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
56 | 57 | ||
57 | /**< Kernel log initialized */ |
58 | /** Kernel log initialized */ |
58 | static bool klog_inited = false; |
59 | static bool klog_inited = false; |
59 | /**< First kernel log characters */ |
60 | /** First kernel log characters */ |
60 | static index_t klog_start = 0; |
61 | static index_t klog_start = 0; |
61 | /**< Number of valid kernel log characters */ |
62 | /** Number of valid kernel log characters */ |
62 | static size_t klog_len = 0; |
63 | static size_t klog_len = 0; |
63 | /**< Number of stored (not printed) kernel log characters */ |
64 | /** Number of stored (not printed) kernel log characters */ |
64 | static size_t klog_stored = 0; |
65 | static size_t klog_stored = 0; |
65 | /**< Number of stored kernel log characters for uspace */ |
66 | /** Number of stored kernel log characters for uspace */ |
66 | static size_t klog_uspace = 0; |
67 | static size_t klog_uspace = 0; |
67 | 68 | ||
- | 69 | /** Silence output */ |
|
- | 70 | bool silent = false; |
|
- | 71 | ||
68 | /**< Kernel log spinlock */ |
72 | /** Kernel log spinlock */ |
69 | SPINLOCK_INITIALIZE(klog_lock); |
73 | SPINLOCK_INITIALIZE(klog_lock); |
70 | 74 | ||
71 | /** Physical memory area used for klog buffer */ |
75 | /** Physical memory area used for klog buffer */ |
72 | static parea_t klog_parea; |
76 | static parea_t klog_parea; |
73 | - | ||
74 | /* |
- | |
75 | * For now, we use 0 as INR. |
- | |
76 | * However, it is therefore desirable to have architecture specific |
- | |
77 | * definition of KLOG_VIRT_INR in the future. |
- | |
78 | */ |
- | |
79 | #define KLOG_VIRT_INR 0 |
- | |
80 | - | ||
81 | static irq_t klog_irq; |
- | |
82 | 77 | ||
83 | static chardev_operations_t null_stdout_ops = { |
- | |
84 | .suspend = NULL, |
- | |
85 | .resume = NULL, |
- | |
86 | .write = NULL, |
- | |
87 | .read = NULL |
- | |
88 | }; |
- | |
89 | - | ||
90 | chardev_t null_stdout = { |
- | |
91 | .name = "null", |
- | |
92 | .op = &null_stdout_ops |
- | |
93 | }; |
- | |
94 | - | ||
95 | /** Allways refuse IRQ ownership. |
- | |
96 | * |
- | |
97 | * This is not a real IRQ, so we always decline. |
- | |
98 | * |
- | |
99 | * @return Always returns IRQ_DECLINE. |
- | |
100 | */ |
- | |
101 | static irq_ownership_t klog_claim(void) |
- | |
102 | { |
- | |
103 | return IRQ_DECLINE; |
- | |
104 | } |
- | |
105 | - | ||
106 | /** Standard input character device */ |
78 | /** Standard input and output character devices */ |
107 | chardev_t *stdin = NULL; |
79 | indev_t *stdin = NULL; |
108 | chardev_t *stdout = &null_stdout; |
80 | outdev_t *stdout = NULL; |
109 | 81 | ||
110 | /** Initialize kernel logging facility |
82 | /** Initialize kernel logging facility |
111 | * |
83 | * |
112 | * The shared area contains kernel cyclic buffer. Userspace application may |
84 | * The shared area contains kernel cyclic buffer. Userspace application may |
113 | * be notified on new data with indication of position and size |
85 | * be notified on new data with indication of position and size |
114 | * of the data within the circular buffer. |
86 | * of the data within the circular buffer. |
- | 87 | * |
|
115 | */ |
88 | */ |
116 | void klog_init(void) |
89 | void klog_init(void) |
117 | { |
90 | { |
118 | void *faddr = (void *) KA2PA(klog); |
91 | void *faddr = (void *) KA2PA(klog); |
119 | 92 | ||
120 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
93 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
121 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
94 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
122 | - | ||
123 | devno_t devno = device_assign_devno(); |
- | |
124 | 95 | ||
125 | klog_parea.pbase = (uintptr_t) faddr; |
96 | klog_parea.pbase = (uintptr_t) faddr; |
126 | klog_parea.vbase = (uintptr_t) klog; |
- | |
127 | klog_parea.frames = SIZE2FRAMES(KLOG_SIZE); |
97 | klog_parea.frames = SIZE2FRAMES(KLOG_SIZE); |
128 | klog_parea.cacheable = true; |
- | |
129 | ddi_parea_register(&klog_parea); |
98 | ddi_parea_register(&klog_parea); |
130 | 99 | ||
131 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
100 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
132 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE)); |
101 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE)); |
133 | sysinfo_set_item_val("klog.devno", NULL, devno); |
- | |
134 | sysinfo_set_item_val("klog.inr", NULL, KLOG_VIRT_INR); |
- | |
135 | 102 | ||
136 | irq_initialize(&klog_irq); |
103 | //irq_initialize(&klog_irq); |
137 | klog_irq.devno = devno; |
104 | //klog_irq.devno = devno; |
138 | klog_irq.inr = KLOG_VIRT_INR; |
105 | //klog_irq.inr = KLOG_VIRT_INR; |
139 | klog_irq.claim = klog_claim; |
106 | //klog_irq.claim = klog_claim; |
140 | irq_register(&klog_irq); |
107 | //irq_register(&klog_irq); |
141 | 108 | ||
142 | spinlock_lock(&klog_lock); |
109 | spinlock_lock(&klog_lock); |
143 | klog_inited = true; |
110 | klog_inited = true; |
144 | spinlock_unlock(&klog_lock); |
111 | spinlock_unlock(&klog_lock); |
145 | } |
112 | } |
146 | 113 | ||
- | 114 | void grab_console(void) |
|
- | 115 | { |
|
- | 116 | silent = false; |
|
- | 117 | arch_grab_console(); |
|
- | 118 | } |
|
- | 119 | ||
- | 120 | void release_console(void) |
|
- | 121 | { |
|
- | 122 | silent = true; |
|
- | 123 | arch_release_console(); |
|
- | 124 | } |
|
- | 125 | ||
147 | /** Get character from character device. Do not echo character. |
126 | /** Tell kernel to get keyboard/console access again */ |
- | 127 | unative_t sys_debug_enable_console(void) |
|
- | 128 | { |
|
- | 129 | #ifdef CONFIG_KCONSOLE |
|
- | 130 | grab_console(); |
|
- | 131 | return true; |
|
- | 132 | #else |
|
- | 133 | return false; |
|
- | 134 | #endif |
|
- | 135 | } |
|
- | 136 | ||
- | 137 | /** Tell kernel to relinquish keyboard/console access */ |
|
- | 138 | unative_t sys_debug_disable_console(void) |
|
- | 139 | { |
|
- | 140 | release_console(); |
|
- | 141 | return true; |
|
- | 142 | } |
|
- | 143 | ||
- | 144 | bool check_poll(indev_t *indev) |
|
- | 145 | { |
|
- | 146 | if (indev == NULL) |
|
- | 147 | return false; |
|
- | 148 | ||
- | 149 | if (indev->op == NULL) |
|
- | 150 | return false; |
|
148 | * |
151 | |
149 | * @param chardev Character device. |
152 | return (indev->op->poll != NULL); |
- | 153 | } |
|
- | 154 | ||
- | 155 | /** Get character from input character device. Do not echo character. |
|
150 | * |
156 | * |
- | 157 | * @param indev Input character device. |
|
151 | * @return Character read. |
158 | * @return Character read. |
- | 159 | * |
|
152 | */ |
160 | */ |
153 | uint8_t _getc(chardev_t *chardev) |
161 | uint8_t _getc(indev_t *indev) |
154 | { |
162 | { |
155 | uint8_t ch; |
- | |
156 | ipl_t ipl; |
- | |
157 | - | ||
158 | if (atomic_get(&haltstate)) { |
163 | if (atomic_get(&haltstate)) { |
159 | /* If we are here, we are hopefully on the processor, that |
164 | /* If we are here, we are hopefully on the processor that |
160 | * issued the 'halt' command, so proceed to read the character |
165 | * issued the 'halt' command, so proceed to read the character |
161 | * directly from input |
166 | * directly from input |
162 | */ |
167 | */ |
163 | if (chardev->op->read) |
168 | if (check_poll(indev)) |
164 | return chardev->op->read(chardev); |
169 | return indev->op->poll(indev); |
- | 170 | ||
165 | /* no other way of interacting with user, halt */ |
171 | /* No other way of interacting with user */ |
- | 172 | interrupts_disable(); |
|
- | 173 | ||
166 | if (CPU) |
174 | if (CPU) |
167 | printf("cpu%u: ", CPU->id); |
175 | printf("cpu%u: ", CPU->id); |
168 | else |
176 | else |
169 | printf("cpu: "); |
177 | printf("cpu: "); |
170 | printf("halted - no kconsole\n"); |
178 | printf("halted (no polling input)\n"); |
171 | cpu_halt(); |
179 | cpu_halt(); |
172 | } |
180 | } |
173 | 181 | ||
174 | waitq_sleep(&chardev->wq); |
182 | waitq_sleep(&indev->wq); |
175 | ipl = interrupts_disable(); |
183 | ipl_t ipl = interrupts_disable(); |
176 | spinlock_lock(&chardev->lock); |
184 | spinlock_lock(&indev->lock); |
177 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN]; |
185 | uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
178 | chardev->counter--; |
186 | indev->counter--; |
179 | spinlock_unlock(&chardev->lock); |
187 | spinlock_unlock(&indev->lock); |
180 | interrupts_restore(ipl); |
188 | interrupts_restore(ipl); |
181 | - | ||
182 | chardev->op->resume(chardev); |
- | |
183 | 189 | ||
184 | return ch; |
190 | return ch; |
185 | } |
191 | } |
186 | 192 | ||
187 | /** Get string from character device. |
193 | /** Get string from input character device. |
188 | * |
194 | * |
189 | * Read characters from character device until first occurrence |
195 | * Read characters from input character device until first occurrence |
190 | * of newline character. |
196 | * of newline character. |
191 | * |
197 | * |
192 | * @param chardev Character device. |
198 | * @param indev Input character device. |
193 | * @param buf Buffer where to store string terminated by '\0'. |
199 | * @param buf Buffer where to store string terminated by '\0'. |
194 | * @param buflen Size of the buffer. |
200 | * @param buflen Size of the buffer. |
195 | * |
201 | * |
196 | * @return Number of characters read. |
202 | * @return Number of characters read. |
- | 203 | * |
|
197 | */ |
204 | */ |
198 | count_t gets(chardev_t *chardev, char *buf, size_t buflen) |
205 | count_t gets(indev_t *indev, char *buf, size_t buflen) |
199 | { |
206 | { |
200 | index_t index = 0; |
207 | index_t index = 0; |
201 | char ch; |
- | |
202 | 208 | ||
203 | while (index < buflen) { |
209 | while (index < buflen) { |
204 | ch = _getc(chardev); |
210 | char ch = _getc(indev); |
205 | if (ch == '\b') { |
211 | if (ch == '\b') { |
206 | if (index > 0) { |
212 | if (index > 0) { |
207 | index--; |
213 | index--; |
208 | /* Space backspace, space */ |
214 | /* Space backspace, space */ |
209 | putchar('\b'); |
215 | putchar('\b'); |
Line 211... | Line 217... | ||
211 | putchar('\b'); |
217 | putchar('\b'); |
212 | } |
218 | } |
213 | continue; |
219 | continue; |
214 | } |
220 | } |
215 | putchar(ch); |
221 | putchar(ch); |
216 | 222 | ||
217 | if (ch == '\n') { /* end of string => write 0, return */ |
223 | if (ch == '\n') { /* end of string => write 0, return */ |
218 | buf[index] = '\0'; |
224 | buf[index] = '\0'; |
219 | return (count_t) index; |
225 | return (count_t) index; |
220 | } |
226 | } |
221 | buf[index++] = ch; |
227 | buf[index++] = ch; |
222 | } |
228 | } |
- | 229 | ||
223 | return (count_t) index; |
230 | return (count_t) index; |
224 | } |
231 | } |
225 | 232 | ||
226 | /** Get character from device & echo it to screen */ |
233 | /** Get character from input device & echo it to screen */ |
227 | uint8_t getc(chardev_t *chardev) |
234 | uint8_t getc(indev_t *indev) |
228 | { |
235 | { |
229 | uint8_t ch; |
- | |
230 | - | ||
231 | ch = _getc(chardev); |
236 | uint8_t ch = _getc(indev); |
232 | putchar(ch); |
237 | putchar(ch); |
233 | return ch; |
238 | return ch; |
234 | } |
239 | } |
235 | 240 | ||
236 | void klog_update(void) |
241 | void klog_update(void) |
237 | { |
242 | { |
238 | spinlock_lock(&klog_lock); |
243 | spinlock_lock(&klog_lock); |
239 | 244 | ||
240 | if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) { |
245 | // if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) { |
241 | ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace); |
246 | // ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace); |
242 | klog_uspace = 0; |
247 | // klog_uspace = 0; |
243 | } |
248 | // } |
244 | 249 | ||
245 | spinlock_unlock(&klog_lock); |
250 | spinlock_unlock(&klog_lock); |
246 | } |
251 | } |
247 | 252 | ||
248 | void putchar(char c) |
253 | void putchar(char c) |
249 | { |
254 | { |
250 | spinlock_lock(&klog_lock); |
255 | spinlock_lock(&klog_lock); |
251 | 256 | ||
252 | if ((klog_stored > 0) && (stdout->op->write)) { |
257 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) { |
253 | /* Print charaters stored in kernel log */ |
258 | /* Print charaters stored in kernel log */ |
254 | index_t i; |
259 | index_t i; |
255 | for (i = klog_len - klog_stored; i < klog_len; i++) |
260 | for (i = klog_len - klog_stored; i < klog_len; i++) |
256 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE]); |
261 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent); |
257 | klog_stored = 0; |
262 | klog_stored = 0; |
258 | } |
263 | } |
259 | 264 | ||
260 | /* Store character in the cyclic kernel log */ |
265 | /* Store character in the cyclic kernel log */ |
261 | klog[(klog_start + klog_len) % KLOG_SIZE] = c; |
266 | klog[(klog_start + klog_len) % KLOG_SIZE] = c; |
262 | if (klog_len < KLOG_SIZE) |
267 | if (klog_len < KLOG_SIZE) |
263 | klog_len++; |
268 | klog_len++; |
264 | else |
269 | else |
265 | klog_start = (klog_start + 1) % KLOG_SIZE; |
270 | klog_start = (klog_start + 1) % KLOG_SIZE; |
266 | 271 | ||
267 | if (stdout->op->write) |
272 | if ((stdout) && (stdout->op->write)) |
268 | stdout->op->write(stdout, c); |
273 | stdout->op->write(stdout, c, silent); |
269 | else { |
274 | else { |
270 | /* The character is just in the kernel log */ |
275 | /* The character is just in the kernel log */ |
271 | if (klog_stored < klog_len) |
276 | if (klog_stored < klog_len) |
272 | klog_stored++; |
277 | klog_stored++; |
273 | } |
278 | } |
Line 287... | Line 292... | ||
287 | 292 | ||
288 | if (update) |
293 | if (update) |
289 | klog_update(); |
294 | klog_update(); |
290 | } |
295 | } |
291 | 296 | ||
- | 297 | /** Print using kernel facility |
|
- | 298 | * |
|
- | 299 | * Print to kernel log. |
|
- | 300 | * |
|
- | 301 | */ |
|
- | 302 | unative_t sys_klog(int fd, const void * buf, size_t count) |
|
- | 303 | { |
|
- | 304 | char *data; |
|
- | 305 | int rc; |
|
- | 306 | ||
- | 307 | if (count > PAGE_SIZE) |
|
- | 308 | return ELIMIT; |
|
- | 309 | ||
- | 310 | if (count > 0) { |
|
- | 311 | data = (char *) malloc(count + 1, 0); |
|
- | 312 | if (!data) |
|
- | 313 | return ENOMEM; |
|
- | 314 | ||
- | 315 | rc = copy_from_uspace(data, buf, count); |
|
- | 316 | if (rc) { |
|
- | 317 | free(data); |
|
- | 318 | return rc; |
|
- | 319 | } |
|
- | 320 | data[count] = 0; |
|
- | 321 | ||
- | 322 | printf("%s", data); |
|
- | 323 | free(data); |
|
- | 324 | } else |
|
- | 325 | klog_update(); |
|
- | 326 | ||
- | 327 | return count; |
|
- | 328 | } |
|
- | 329 | ||
292 | /** @} |
330 | /** @} |
293 | */ |
331 | */ |