Rev 3742 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
510 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2003 Josef Cejka |
3 | * Copyright (c) 2005 Jakub Jermar |
||
510 | jermar | 4 | * All rights reserved. |
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
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 |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
1888 | jermar | 30 | /** @addtogroup genericconsole |
1702 | cejka | 31 | * @{ |
32 | */ |
||
33 | /** @file |
||
34 | */ |
||
35 | |||
510 | jermar | 36 | #include <console/console.h> |
37 | #include <console/chardev.h> |
||
3097 | decky | 38 | #include <sysinfo/sysinfo.h> |
510 | jermar | 39 | #include <synch/waitq.h> |
40 | #include <synch/spinlock.h> |
||
3862 | rimsky | 41 | #include <arch/asm.h> |
510 | jermar | 42 | #include <arch/types.h> |
3097 | decky | 43 | #include <ddi/device.h> |
44 | #include <ddi/irq.h> |
||
45 | #include <ddi/ddi.h> |
||
46 | #include <ipc/irq.h> |
||
510 | jermar | 47 | #include <arch.h> |
607 | palkovsky | 48 | #include <func.h> |
49 | #include <print.h> |
||
1104 | jermar | 50 | #include <atomic.h> |
510 | jermar | 51 | |
3097 | decky | 52 | #define KLOG_SIZE PAGE_SIZE |
3113 | decky | 53 | #define KLOG_LATENCY 8 |
1050 | palkovsky | 54 | |
3065 | decky | 55 | /**< Kernel log cyclic buffer */ |
3097 | decky | 56 | static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
3065 | decky | 57 | |
3097 | decky | 58 | /**< Kernel log initialized */ |
59 | static bool klog_inited = false; |
||
3065 | decky | 60 | /**< First kernel log characters */ |
61 | static index_t klog_start = 0; |
||
62 | /**< Number of valid kernel log characters */ |
||
63 | static size_t klog_len = 0; |
||
64 | /**< Number of stored (not printed) kernel log characters */ |
||
65 | static size_t klog_stored = 0; |
||
3097 | decky | 66 | /**< Number of stored kernel log characters for uspace */ |
67 | static size_t klog_uspace = 0; |
||
3065 | decky | 68 | |
3097 | decky | 69 | /**< Kernel log spinlock */ |
70 | SPINLOCK_INITIALIZE(klog_lock); |
||
71 | |||
72 | /** Physical memory area used for klog buffer */ |
||
73 | static parea_t klog_parea; |
||
74 | |||
75 | /* |
||
76 | * For now, we use 0 as INR. |
||
77 | * However, it is therefore desirable to have architecture specific |
||
78 | * definition of KLOG_VIRT_INR in the future. |
||
79 | */ |
||
80 | #define KLOG_VIRT_INR 0 |
||
81 | |||
82 | static irq_t klog_irq; |
||
83 | |||
1050 | palkovsky | 84 | static chardev_operations_t null_stdout_ops = { |
3065 | decky | 85 | .suspend = NULL, |
86 | .resume = NULL, |
||
87 | .write = NULL, |
||
88 | .read = NULL |
||
1050 | palkovsky | 89 | }; |
2697 | decky | 90 | |
1050 | palkovsky | 91 | chardev_t null_stdout = { |
92 | .name = "null", |
||
93 | .op = &null_stdout_ops |
||
94 | }; |
||
95 | |||
3097 | decky | 96 | /** Allways refuse IRQ ownership. |
97 | * |
||
98 | * This is not a real IRQ, so we always decline. |
||
99 | * |
||
100 | * @return Always returns IRQ_DECLINE. |
||
101 | */ |
||
102 | static irq_ownership_t klog_claim(void) |
||
103 | { |
||
104 | return IRQ_DECLINE; |
||
105 | } |
||
106 | |||
107 | /** Standard input character device */ |
||
510 | jermar | 108 | chardev_t *stdin = NULL; |
1050 | palkovsky | 109 | chardev_t *stdout = &null_stdout; |
510 | jermar | 110 | |
3097 | decky | 111 | /** Initialize kernel logging facility |
112 | * |
||
113 | * The shared area contains kernel cyclic buffer. Userspace application may |
||
114 | * be notified on new data with indication of position and size |
||
115 | * of the data within the circular buffer. |
||
116 | */ |
||
117 | void klog_init(void) |
||
118 | { |
||
119 | void *faddr = (void *) KA2PA(klog); |
||
120 | |||
121 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
||
122 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
||
123 | |||
124 | devno_t devno = device_assign_devno(); |
||
125 | |||
126 | klog_parea.pbase = (uintptr_t) faddr; |
||
127 | klog_parea.vbase = (uintptr_t) klog; |
||
128 | klog_parea.frames = SIZE2FRAMES(KLOG_SIZE); |
||
129 | klog_parea.cacheable = true; |
||
130 | ddi_parea_register(&klog_parea); |
||
131 | |||
132 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
||
133 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE)); |
||
134 | sysinfo_set_item_val("klog.devno", NULL, devno); |
||
135 | sysinfo_set_item_val("klog.inr", NULL, KLOG_VIRT_INR); |
||
136 | |||
137 | irq_initialize(&klog_irq); |
||
138 | klog_irq.devno = devno; |
||
139 | klog_irq.inr = KLOG_VIRT_INR; |
||
140 | klog_irq.claim = klog_claim; |
||
141 | irq_register(&klog_irq); |
||
142 | |||
143 | spinlock_lock(&klog_lock); |
||
144 | klog_inited = true; |
||
145 | spinlock_unlock(&klog_lock); |
||
146 | } |
||
147 | |||
601 | palkovsky | 148 | /** Get character from character device. Do not echo character. |
588 | palkovsky | 149 | * |
150 | * @param chardev Character device. |
||
151 | * |
||
152 | * @return Character read. |
||
153 | */ |
||
1780 | jermar | 154 | uint8_t _getc(chardev_t *chardev) |
588 | palkovsky | 155 | { |
1780 | jermar | 156 | uint8_t ch; |
588 | palkovsky | 157 | ipl_t ipl; |
158 | |||
631 | palkovsky | 159 | if (atomic_get(&haltstate)) { |
607 | palkovsky | 160 | /* If we are here, we are hopefully on the processor, that |
161 | * issued the 'halt' command, so proceed to read the character |
||
162 | * directly from input |
||
163 | */ |
||
164 | if (chardev->op->read) |
||
165 | return chardev->op->read(chardev); |
||
166 | /* no other way of interacting with user, halt */ |
||
631 | palkovsky | 167 | if (CPU) |
3065 | decky | 168 | printf("cpu%u: ", CPU->id); |
631 | palkovsky | 169 | else |
170 | printf("cpu: "); |
||
3742 | rimsky | 171 | printf("halted (no kconsole)\n"); |
607 | palkovsky | 172 | cpu_halt(); |
173 | } |
||
174 | |||
588 | palkovsky | 175 | waitq_sleep(&chardev->wq); |
176 | ipl = interrupts_disable(); |
||
177 | spinlock_lock(&chardev->lock); |
||
178 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN]; |
||
179 | chardev->counter--; |
||
180 | spinlock_unlock(&chardev->lock); |
||
181 | interrupts_restore(ipl); |
||
182 | |||
183 | chardev->op->resume(chardev); |
||
184 | |||
185 | return ch; |
||
186 | } |
||
187 | |||
510 | jermar | 188 | /** Get string from character device. |
189 | * |
||
190 | * Read characters from character device until first occurrence |
||
191 | * of newline character. |
||
192 | * |
||
193 | * @param chardev Character device. |
||
517 | jermar | 194 | * @param buf Buffer where to store string terminated by '\0'. |
1708 | jermar | 195 | * @param buflen Size of the buffer. |
517 | jermar | 196 | * |
197 | * @return Number of characters read. |
||
510 | jermar | 198 | */ |
517 | jermar | 199 | count_t gets(chardev_t *chardev, char *buf, size_t buflen) |
510 | jermar | 200 | { |
201 | index_t index = 0; |
||
202 | char ch; |
||
203 | |||
204 | while (index < buflen) { |
||
588 | palkovsky | 205 | ch = _getc(chardev); |
206 | if (ch == '\b') { |
||
207 | if (index > 0) { |
||
208 | index--; |
||
209 | /* Space backspace, space */ |
||
210 | putchar('\b'); |
||
211 | putchar(' '); |
||
212 | putchar('\b'); |
||
213 | } |
||
214 | continue; |
||
215 | } |
||
216 | putchar(ch); |
||
217 | |||
510 | jermar | 218 | if (ch == '\n') { /* end of string => write 0, return */ |
517 | jermar | 219 | buf[index] = '\0'; |
220 | return (count_t) index; |
||
510 | jermar | 221 | } |
517 | jermar | 222 | buf[index++] = ch; |
510 | jermar | 223 | } |
517 | jermar | 224 | return (count_t) index; |
510 | jermar | 225 | } |
226 | |||
588 | palkovsky | 227 | /** Get character from device & echo it to screen */ |
1780 | jermar | 228 | uint8_t getc(chardev_t *chardev) |
510 | jermar | 229 | { |
1780 | jermar | 230 | uint8_t ch; |
510 | jermar | 231 | |
588 | palkovsky | 232 | ch = _getc(chardev); |
233 | putchar(ch); |
||
510 | jermar | 234 | return ch; |
235 | } |
||
575 | palkovsky | 236 | |
3097 | decky | 237 | void klog_update(void) |
238 | { |
||
239 | spinlock_lock(&klog_lock); |
||
240 | |||
241 | if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) { |
||
242 | ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace); |
||
243 | klog_uspace = 0; |
||
244 | } |
||
245 | |||
246 | spinlock_unlock(&klog_lock); |
||
247 | } |
||
248 | |||
575 | palkovsky | 249 | void putchar(char c) |
250 | { |
||
3097 | decky | 251 | spinlock_lock(&klog_lock); |
252 | |||
3065 | decky | 253 | if ((klog_stored > 0) && (stdout->op->write)) { |
254 | /* Print charaters stored in kernel log */ |
||
255 | index_t i; |
||
256 | for (i = klog_len - klog_stored; i < klog_len; i++) |
||
257 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE]); |
||
258 | klog_stored = 0; |
||
259 | } |
||
260 | |||
261 | /* Store character in the cyclic kernel log */ |
||
262 | klog[(klog_start + klog_len) % KLOG_SIZE] = c; |
||
263 | if (klog_len < KLOG_SIZE) |
||
264 | klog_len++; |
||
265 | else |
||
266 | klog_start = (klog_start + 1) % KLOG_SIZE; |
||
267 | |||
607 | palkovsky | 268 | if (stdout->op->write) |
269 | stdout->op->write(stdout, c); |
||
3065 | decky | 270 | else { |
271 | /* The character is just in the kernel log */ |
||
272 | if (klog_stored < klog_len) |
||
273 | klog_stored++; |
||
274 | } |
||
3097 | decky | 275 | |
276 | /* The character is stored for uspace */ |
||
277 | if (klog_uspace < klog_len) |
||
278 | klog_uspace++; |
||
279 | |||
3113 | decky | 280 | /* Check notify uspace to update */ |
281 | bool update; |
||
282 | if ((klog_uspace > KLOG_LATENCY) || (c == '\n')) |
||
283 | update = true; |
||
284 | else |
||
285 | update = false; |
||
286 | |||
3097 | decky | 287 | spinlock_unlock(&klog_lock); |
288 | |||
3113 | decky | 289 | if (update) |
290 | klog_update(); |
||
575 | palkovsky | 291 | } |
1702 | cejka | 292 | |
1888 | jermar | 293 | /** @} |
1702 | cejka | 294 | */ |