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