Rev 4153 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4153 | Rev 4263 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2003 Josef Cejka |
2 | * Copyright (c) 2003 Josef Cejka |
3 | * Copyright (c) 2005 Jakub Jermar |
3 | * Copyright (c) 2005 Jakub Jermar |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | /** @addtogroup genericconsole |
30 | /** @addtogroup genericconsole |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | /** @file |
33 | /** @file |
34 | */ |
34 | */ |
35 | 35 | ||
36 | #include <console/console.h> |
36 | #include <console/console.h> |
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/irq.h> |
42 | #include <ddi/irq.h> |
43 | #include <ddi/ddi.h> |
43 | #include <ddi/ddi.h> |
- | 44 | #include <ipc/event.h> |
|
44 | #include <ipc/irq.h> |
45 | #include <ipc/irq.h> |
45 | #include <arch.h> |
46 | #include <arch.h> |
46 | #include <func.h> |
47 | #include <func.h> |
47 | #include <print.h> |
48 | #include <print.h> |
- | 49 | #include <putchar.h> |
|
48 | #include <atomic.h> |
50 | #include <atomic.h> |
49 | #include <syscall/copy.h> |
51 | #include <syscall/copy.h> |
50 | #include <errno.h> |
52 | #include <errno.h> |
- | 53 | #include <string.h> |
|
51 | 54 | ||
52 | #define KLOG_SIZE PAGE_SIZE |
55 | #define KLOG_PAGES 4 |
- | 56 | #define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t)) |
|
53 | #define KLOG_LATENCY 8 |
57 | #define KLOG_LATENCY 8 |
54 | 58 | ||
55 | /** Kernel log cyclic buffer */ |
59 | /** Kernel log cyclic buffer */ |
56 | static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
60 | static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE))); |
57 | 61 | ||
58 | /** Kernel log initialized */ |
62 | /** Kernel log initialized */ |
59 | static bool klog_inited = false; |
63 | static bool klog_inited = false; |
60 | /** First kernel log characters */ |
64 | /** First kernel log characters */ |
61 | static index_t klog_start = 0; |
65 | static index_t klog_start = 0; |
62 | /** Number of valid kernel log characters */ |
66 | /** Number of valid kernel log characters */ |
63 | static size_t klog_len = 0; |
67 | static size_t klog_len = 0; |
64 | /** Number of stored (not printed) kernel log characters */ |
68 | /** Number of stored (not printed) kernel log characters */ |
65 | static size_t klog_stored = 0; |
69 | static size_t klog_stored = 0; |
66 | /** Number of stored kernel log characters for uspace */ |
70 | /** Number of stored kernel log characters for uspace */ |
67 | static size_t klog_uspace = 0; |
71 | static size_t klog_uspace = 0; |
68 | 72 | ||
69 | /** Silence output */ |
73 | /** Silence output */ |
70 | bool silent = false; |
74 | bool silent = false; |
71 | 75 | ||
72 | /** Kernel log spinlock */ |
76 | /** Kernel log spinlock */ |
73 | SPINLOCK_INITIALIZE(klog_lock); |
77 | SPINLOCK_INITIALIZE(klog_lock); |
74 | 78 | ||
75 | /** Physical memory area used for klog buffer */ |
79 | /** Physical memory area used for klog buffer */ |
76 | static parea_t klog_parea; |
80 | static parea_t klog_parea; |
77 | 81 | ||
78 | /** Standard input and output character devices */ |
82 | /** Standard input and output character devices */ |
79 | indev_t *stdin = NULL; |
83 | indev_t *stdin = NULL; |
80 | outdev_t *stdout = NULL; |
84 | outdev_t *stdout = NULL; |
81 | 85 | ||
82 | /** Initialize kernel logging facility |
86 | /** Initialize kernel logging facility |
83 | * |
87 | * |
84 | * The shared area contains kernel cyclic buffer. Userspace application may |
88 | * The shared area contains kernel cyclic buffer. Userspace application may |
85 | * be notified on new data with indication of position and size |
89 | * be notified on new data with indication of position and size |
86 | * of the data within the circular buffer. |
90 | * of the data within the circular buffer. |
87 | * |
91 | * |
88 | */ |
92 | */ |
89 | void klog_init(void) |
93 | void klog_init(void) |
90 | { |
94 | { |
91 | void *faddr = (void *) KA2PA(klog); |
95 | void *faddr = (void *) KA2PA(klog); |
92 | 96 | ||
93 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
97 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
94 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
- | |
95 | 98 | ||
96 | klog_parea.pbase = (uintptr_t) faddr; |
99 | klog_parea.pbase = (uintptr_t) faddr; |
97 | klog_parea.frames = SIZE2FRAMES(KLOG_SIZE); |
100 | klog_parea.frames = SIZE2FRAMES(sizeof(klog)); |
98 | ddi_parea_register(&klog_parea); |
101 | ddi_parea_register(&klog_parea); |
99 | 102 | ||
100 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
103 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
101 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE)); |
104 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES); |
102 | - | ||
103 | //irq_initialize(&klog_irq); |
- | |
104 | //klog_irq.devno = devno; |
- | |
105 | //klog_irq.inr = KLOG_VIRT_INR; |
- | |
106 | //klog_irq.claim = klog_claim; |
- | |
107 | //irq_register(&klog_irq); |
- | |
108 | 105 | ||
109 | spinlock_lock(&klog_lock); |
106 | spinlock_lock(&klog_lock); |
110 | klog_inited = true; |
107 | klog_inited = true; |
111 | spinlock_unlock(&klog_lock); |
108 | spinlock_unlock(&klog_lock); |
112 | } |
109 | } |
113 | 110 | ||
114 | void grab_console(void) |
111 | void grab_console(void) |
115 | { |
112 | { |
116 | silent = false; |
113 | silent = false; |
117 | arch_grab_console(); |
114 | arch_grab_console(); |
118 | } |
115 | } |
119 | 116 | ||
120 | void release_console(void) |
117 | void release_console(void) |
121 | { |
118 | { |
122 | silent = true; |
119 | silent = true; |
123 | arch_release_console(); |
120 | arch_release_console(); |
124 | } |
121 | } |
125 | 122 | ||
126 | /** Tell kernel to get keyboard/console access again */ |
123 | /** Tell kernel to get keyboard/console access again */ |
127 | unative_t sys_debug_enable_console(void) |
124 | unative_t sys_debug_enable_console(void) |
128 | { |
125 | { |
129 | #ifdef CONFIG_KCONSOLE |
126 | #ifdef CONFIG_KCONSOLE |
130 | grab_console(); |
127 | grab_console(); |
131 | return true; |
128 | return true; |
132 | #else |
129 | #else |
133 | return false; |
130 | return false; |
134 | #endif |
131 | #endif |
135 | } |
132 | } |
136 | 133 | ||
137 | /** Tell kernel to relinquish keyboard/console access */ |
134 | /** Tell kernel to relinquish keyboard/console access */ |
138 | unative_t sys_debug_disable_console(void) |
135 | unative_t sys_debug_disable_console(void) |
139 | { |
136 | { |
140 | release_console(); |
137 | release_console(); |
141 | return true; |
138 | return true; |
142 | } |
139 | } |
143 | 140 | ||
144 | bool check_poll(indev_t *indev) |
141 | bool check_poll(indev_t *indev) |
145 | { |
142 | { |
146 | if (indev == NULL) |
143 | if (indev == NULL) |
147 | return false; |
144 | return false; |
148 | 145 | ||
149 | if (indev->op == NULL) |
146 | if (indev->op == NULL) |
150 | return false; |
147 | return false; |
151 | 148 | ||
152 | return (indev->op->poll != NULL); |
149 | return (indev->op->poll != NULL); |
153 | } |
150 | } |
154 | 151 | ||
155 | /** Get character from input character device. Do not echo character. |
152 | /** Get character from input character device. Do not echo character. |
156 | * |
153 | * |
157 | * @param indev Input character device. |
154 | * @param indev Input character device. |
158 | * @return Character read. |
155 | * @return Character read. |
159 | * |
156 | * |
160 | */ |
157 | */ |
161 | uint8_t _getc(indev_t *indev) |
158 | wchar_t _getc(indev_t *indev) |
162 | { |
159 | { |
163 | if (atomic_get(&haltstate)) { |
160 | if (atomic_get(&haltstate)) { |
164 | /* If we are here, we are hopefully on the processor that |
161 | /* If we are here, we are hopefully on the processor that |
165 | * issued the 'halt' command, so proceed to read the character |
162 | * issued the 'halt' command, so proceed to read the character |
166 | * directly from input |
163 | * directly from input |
167 | */ |
164 | */ |
168 | if (check_poll(indev)) |
165 | if (check_poll(indev)) |
169 | return indev->op->poll(indev); |
166 | return indev->op->poll(indev); |
170 | 167 | ||
171 | /* No other way of interacting with user */ |
168 | /* No other way of interacting with user */ |
172 | interrupts_disable(); |
169 | interrupts_disable(); |
173 | 170 | ||
174 | if (CPU) |
171 | if (CPU) |
175 | printf("cpu%u: ", CPU->id); |
172 | printf("cpu%u: ", CPU->id); |
176 | else |
173 | else |
177 | printf("cpu: "); |
174 | printf("cpu: "); |
178 | printf("halted (no polling input)\n"); |
175 | printf("halted (no polling input)\n"); |
179 | cpu_halt(); |
176 | cpu_halt(); |
180 | } |
177 | } |
181 | 178 | ||
182 | waitq_sleep(&indev->wq); |
179 | waitq_sleep(&indev->wq); |
183 | ipl_t ipl = interrupts_disable(); |
180 | ipl_t ipl = interrupts_disable(); |
184 | spinlock_lock(&indev->lock); |
181 | spinlock_lock(&indev->lock); |
185 | uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
182 | wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
186 | indev->counter--; |
183 | indev->counter--; |
187 | spinlock_unlock(&indev->lock); |
184 | spinlock_unlock(&indev->lock); |
188 | interrupts_restore(ipl); |
185 | interrupts_restore(ipl); |
189 | 186 | ||
190 | return ch; |
187 | return ch; |
191 | } |
188 | } |
192 | 189 | ||
193 | /** Get string from input character device. |
190 | /** Get string from input character device. |
194 | * |
191 | * |
195 | * Read characters from input character device until first occurrence |
192 | * Read characters from input character device until first occurrence |
196 | * of newline character. |
193 | * of newline character. |
197 | * |
194 | * |
198 | * @param indev Input character device. |
195 | * @param indev Input character device. |
199 | * @param buf Buffer where to store string terminated by '\0'. |
196 | * @param buf Buffer where to store string terminated by NULL. |
200 | * @param buflen Size of the buffer. |
197 | * @param buflen Size of the buffer. |
201 | * |
198 | * |
202 | * @return Number of characters read. |
199 | * @return Number of characters read. |
203 | * |
200 | * |
204 | */ |
201 | */ |
205 | count_t gets(indev_t *indev, char *buf, size_t buflen) |
202 | count_t gets(indev_t *indev, char *buf, size_t buflen) |
206 | { |
203 | { |
207 | index_t index = 0; |
204 | size_t offset = 0; |
- | 205 | count_t count = 0; |
|
- | 206 | buf[offset] = 0; |
|
208 | 207 | ||
209 | while (index < buflen) { |
208 | wchar_t ch; |
210 | char ch = _getc(indev); |
209 | while ((ch = _getc(indev)) != '\n') { |
211 | if (ch == '\b') { |
210 | if (ch == '\b') { |
212 | if (index > 0) { |
211 | if (count > 0) { |
213 | index--; |
- | |
214 | /* Space backspace, space */ |
212 | /* Space, backspace, space */ |
215 | putchar('\b'); |
213 | putchar('\b'); |
216 | putchar(' '); |
214 | putchar(' '); |
217 | putchar('\b'); |
215 | putchar('\b'); |
- | 216 | ||
- | 217 | count--; |
|
- | 218 | offset = str_lsize(buf, count); |
|
- | 219 | buf[offset] = 0; |
|
218 | } |
220 | } |
219 | continue; |
- | |
220 | } |
- | |
221 | putchar(ch); |
- | |
222 | - | ||
223 | if (ch == '\n') { /* end of string => write 0, return */ |
- | |
224 | buf[index] = '\0'; |
- | |
225 | return (count_t) index; |
- | |
226 | } |
221 | } |
- | 222 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) { |
|
- | 223 | putchar(ch); |
|
- | 224 | count++; |
|
227 | buf[index++] = ch; |
225 | buf[offset] = 0; |
- | 226 | } |
|
228 | } |
227 | } |
229 | 228 | ||
230 | return (count_t) index; |
229 | return count; |
231 | } |
230 | } |
232 | 231 | ||
233 | /** Get character from input device & echo it to screen */ |
232 | /** Get character from input device & echo it to screen */ |
234 | uint8_t getc(indev_t *indev) |
233 | wchar_t getc(indev_t *indev) |
235 | { |
234 | { |
236 | uint8_t ch = _getc(indev); |
235 | wchar_t ch = _getc(indev); |
237 | putchar(ch); |
236 | putchar(ch); |
238 | return ch; |
237 | return ch; |
239 | } |
238 | } |
240 | 239 | ||
241 | void klog_update(void) |
240 | void klog_update(void) |
242 | { |
241 | { |
243 | spinlock_lock(&klog_lock); |
242 | spinlock_lock(&klog_lock); |
244 | 243 | ||
245 | // if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) { |
244 | if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) { |
246 | // ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace); |
245 | event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace); |
247 | // klog_uspace = 0; |
246 | klog_uspace = 0; |
248 | // } |
247 | } |
249 | 248 | ||
250 | spinlock_unlock(&klog_lock); |
249 | spinlock_unlock(&klog_lock); |
251 | } |
250 | } |
252 | 251 | ||
253 | void putchar(char c) |
252 | void putchar(const wchar_t ch) |
254 | { |
253 | { |
255 | spinlock_lock(&klog_lock); |
254 | spinlock_lock(&klog_lock); |
256 | 255 | ||
257 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) { |
256 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) { |
258 | /* Print charaters stored in kernel log */ |
257 | /* Print charaters stored in kernel log */ |
259 | index_t i; |
258 | index_t i; |
260 | for (i = klog_len - klog_stored; i < klog_len; i++) |
259 | for (i = klog_len - klog_stored; i < klog_len; i++) |
261 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent); |
260 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent); |
262 | klog_stored = 0; |
261 | klog_stored = 0; |
263 | } |
262 | } |
264 | 263 | ||
265 | /* Store character in the cyclic kernel log */ |
264 | /* Store character in the cyclic kernel log */ |
266 | klog[(klog_start + klog_len) % KLOG_SIZE] = c; |
265 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch; |
267 | if (klog_len < KLOG_SIZE) |
266 | if (klog_len < KLOG_LENGTH) |
268 | klog_len++; |
267 | klog_len++; |
269 | else |
268 | else |
270 | klog_start = (klog_start + 1) % KLOG_SIZE; |
269 | klog_start = (klog_start + 1) % KLOG_LENGTH; |
271 | 270 | ||
272 | if ((stdout) && (stdout->op->write)) |
271 | if ((stdout) && (stdout->op->write)) |
273 | stdout->op->write(stdout, c, silent); |
272 | stdout->op->write(stdout, ch, silent); |
274 | else { |
273 | else { |
275 | /* The character is just in the kernel log */ |
274 | /* The character is just in the kernel log */ |
276 | if (klog_stored < klog_len) |
275 | if (klog_stored < klog_len) |
277 | klog_stored++; |
276 | klog_stored++; |
278 | } |
277 | } |
279 | 278 | ||
280 | /* The character is stored for uspace */ |
279 | /* The character is stored for uspace */ |
281 | if (klog_uspace < klog_len) |
280 | if (klog_uspace < klog_len) |
282 | klog_uspace++; |
281 | klog_uspace++; |
283 | 282 | ||
284 | /* Check notify uspace to update */ |
283 | /* Check notify uspace to update */ |
285 | bool update; |
284 | bool update; |
286 | if ((klog_uspace > KLOG_LATENCY) || (c == '\n')) |
285 | if ((klog_uspace > KLOG_LATENCY) || (ch == '\n')) |
287 | update = true; |
286 | update = true; |
288 | else |
287 | else |
289 | update = false; |
288 | update = false; |
290 | 289 | ||
291 | spinlock_unlock(&klog_lock); |
290 | spinlock_unlock(&klog_lock); |
292 | 291 | ||
293 | if (update) |
292 | if (update) |
294 | klog_update(); |
293 | klog_update(); |
295 | } |
294 | } |
296 | 295 | ||
297 | /** Print using kernel facility |
296 | /** Print using kernel facility |
298 | * |
297 | * |
299 | * Print to kernel log. |
298 | * Print to kernel log. |
300 | * |
299 | * |
301 | */ |
300 | */ |
302 | unative_t sys_klog(int fd, const void * buf, size_t count) |
301 | unative_t sys_klog(int fd, const void *buf, size_t size) |
303 | { |
302 | { |
304 | char *data; |
303 | char *data; |
305 | int rc; |
304 | int rc; |
306 | 305 | ||
307 | if (count > PAGE_SIZE) |
306 | if (size > PAGE_SIZE) |
308 | return ELIMIT; |
307 | return ELIMIT; |
309 | 308 | ||
310 | if (count > 0) { |
309 | if (size > 0) { |
311 | data = (char *) malloc(count + 1, 0); |
310 | data = (char *) malloc(size + 1, 0); |
312 | if (!data) |
311 | if (!data) |
313 | return ENOMEM; |
312 | return ENOMEM; |
314 | 313 | ||
315 | rc = copy_from_uspace(data, buf, count); |
314 | rc = copy_from_uspace(data, buf, size); |
316 | if (rc) { |
315 | if (rc) { |
317 | free(data); |
316 | free(data); |
318 | return rc; |
317 | return rc; |
319 | } |
318 | } |
320 | data[count] = 0; |
319 | data[size] = 0; |
321 | 320 | ||
322 | printf("%s", data); |
321 | printf("%s", data); |
323 | free(data); |
322 | free(data); |
324 | } else |
323 | } else |
325 | klog_update(); |
324 | klog_update(); |
326 | 325 | ||
327 | return count; |
326 | return size; |
328 | } |
327 | } |
329 | 328 | ||
330 | /** @} |
329 | /** @} |
331 | */ |
330 | */ |
332 | 331 |