Rev 4201 | Rev 4420 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4201 | Rev 4296 | ||
|---|---|---|---|
| Line 39... | Line 39... | ||
| 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 <event/event.h> |
44 | #include <ipc/event.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 <putchar.h> |
49 | #include <putchar.h> |
| 50 | #include <atomic.h> |
50 | #include <atomic.h> |
| 51 | #include <syscall/copy.h> |
51 | #include <syscall/copy.h> |
| 52 | #include <errno.h> |
52 | #include <errno.h> |
| - | 53 | #include <string.h> |
|
| 53 | 54 | ||
| 54 | #define KLOG_SIZE PAGE_SIZE |
55 | #define KLOG_PAGES 4 |
| - | 56 | #define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t)) |
|
| 55 | #define KLOG_LATENCY 8 |
57 | #define KLOG_LATENCY 8 |
| 56 | 58 | ||
| 57 | /** Kernel log cyclic buffer */ |
59 | /** Kernel log cyclic buffer */ |
| 58 | static wchar_t klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
60 | static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE))); |
| 59 | 61 | ||
| 60 | /** Kernel log initialized */ |
62 | /** Kernel log initialized */ |
| 61 | static bool klog_inited = false; |
63 | static bool klog_inited = false; |
| 62 | /** First kernel log characters */ |
64 | /** First kernel log characters */ |
| 63 | static index_t klog_start = 0; |
65 | static index_t klog_start = 0; |
| Line 91... | Line 93... | ||
| 91 | void klog_init(void) |
93 | void klog_init(void) |
| 92 | { |
94 | { |
| 93 | void *faddr = (void *) KA2PA(klog); |
95 | void *faddr = (void *) KA2PA(klog); |
| 94 | 96 | ||
| 95 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
97 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
| 96 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
- | |
| 97 | 98 | ||
| 98 | klog_parea.pbase = (uintptr_t) faddr; |
99 | klog_parea.pbase = (uintptr_t) faddr; |
| 99 | klog_parea.frames = SIZE2FRAMES(sizeof(klog)); |
100 | klog_parea.frames = SIZE2FRAMES(sizeof(klog)); |
| 100 | ddi_parea_register(&klog_parea); |
101 | ddi_parea_register(&klog_parea); |
| 101 | 102 | ||
| 102 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
103 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
| 103 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(sizeof(klog))); |
104 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES); |
| 104 | 105 | ||
| 105 | spinlock_lock(&klog_lock); |
106 | spinlock_lock(&klog_lock); |
| 106 | klog_inited = true; |
107 | klog_inited = true; |
| 107 | spinlock_unlock(&klog_lock); |
108 | spinlock_unlock(&klog_lock); |
| 108 | } |
109 | } |
| Line 152... | Line 153... | ||
| 152 | * |
153 | * |
| 153 | * @param indev Input character device. |
154 | * @param indev Input character device. |
| 154 | * @return Character read. |
155 | * @return Character read. |
| 155 | * |
156 | * |
| 156 | */ |
157 | */ |
| 157 | uint8_t _getc(indev_t *indev) |
158 | wchar_t _getc(indev_t *indev) |
| 158 | { |
159 | { |
| 159 | if (atomic_get(&haltstate)) { |
160 | if (atomic_get(&haltstate)) { |
| 160 | /* If we are here, we are hopefully on the processor that |
161 | /* If we are here, we are hopefully on the processor that |
| 161 | * issued the 'halt' command, so proceed to read the character |
162 | * issued the 'halt' command, so proceed to read the character |
| 162 | * directly from input |
163 | * directly from input |
| Line 176... | Line 177... | ||
| 176 | } |
177 | } |
| 177 | 178 | ||
| 178 | waitq_sleep(&indev->wq); |
179 | waitq_sleep(&indev->wq); |
| 179 | ipl_t ipl = interrupts_disable(); |
180 | ipl_t ipl = interrupts_disable(); |
| 180 | spinlock_lock(&indev->lock); |
181 | spinlock_lock(&indev->lock); |
| 181 | uint8_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
182 | wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
| 182 | indev->counter--; |
183 | indev->counter--; |
| 183 | spinlock_unlock(&indev->lock); |
184 | spinlock_unlock(&indev->lock); |
| 184 | interrupts_restore(ipl); |
185 | interrupts_restore(ipl); |
| 185 | 186 | ||
| 186 | return ch; |
187 | return ch; |
| Line 190... | Line 191... | ||
| 190 | * |
191 | * |
| 191 | * Read characters from input character device until first occurrence |
192 | * Read characters from input character device until first occurrence |
| 192 | * of newline character. |
193 | * of newline character. |
| 193 | * |
194 | * |
| 194 | * @param indev Input character device. |
195 | * @param indev Input character device. |
| 195 | * @param buf Buffer where to store string terminated by '\0'. |
196 | * @param buf Buffer where to store string terminated by NULL. |
| 196 | * @param buflen Size of the buffer. |
197 | * @param buflen Size of the buffer. |
| 197 | * |
198 | * |
| 198 | * @return Number of characters read. |
199 | * @return Number of characters read. |
| 199 | * |
200 | * |
| 200 | */ |
201 | */ |
| 201 | count_t gets(indev_t *indev, char *buf, size_t buflen) |
202 | count_t gets(indev_t *indev, char *buf, size_t buflen) |
| 202 | { |
203 | { |
| 203 | index_t index = 0; |
204 | size_t offset = 0; |
| - | 205 | count_t count = 0; |
|
| - | 206 | buf[offset] = 0; |
|
| 204 | 207 | ||
| 205 | while (index < buflen) { |
208 | wchar_t ch; |
| 206 | char ch = _getc(indev); |
209 | while ((ch = _getc(indev)) != '\n') { |
| 207 | if (ch == '\b') { |
210 | if (ch == '\b') { |
| 208 | if (index > 0) { |
211 | if (count > 0) { |
| 209 | index--; |
- | |
| 210 | /* Space backspace, space */ |
212 | /* Space, backspace, space */ |
| 211 | putchar('\b'); |
213 | putchar('\b'); |
| 212 | putchar(' '); |
214 | putchar(' '); |
| 213 | putchar('\b'); |
215 | putchar('\b'); |
| - | 216 | ||
| - | 217 | count--; |
|
| - | 218 | offset = str_lsize(buf, count); |
|
| - | 219 | buf[offset] = 0; |
|
| 214 | } |
220 | } |
| 215 | continue; |
- | |
| 216 | } |
- | |
| 217 | putchar(ch); |
- | |
| 218 | - | ||
| 219 | if (ch == '\n') { /* end of string => write 0, return */ |
- | |
| 220 | buf[index] = '\0'; |
- | |
| 221 | return (count_t) index; |
- | |
| 222 | } |
221 | } |
| - | 222 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) { |
|
| - | 223 | putchar(ch); |
|
| - | 224 | count++; |
|
| 223 | buf[index++] = ch; |
225 | buf[offset] = 0; |
| - | 226 | } |
|
| 224 | } |
227 | } |
| 225 | 228 | ||
| 226 | return (count_t) index; |
229 | return count; |
| 227 | } |
230 | } |
| 228 | 231 | ||
| 229 | /** Get character from input device & echo it to screen */ |
232 | /** Get character from input device & echo it to screen */ |
| 230 | uint8_t getc(indev_t *indev) |
233 | wchar_t getc(indev_t *indev) |
| 231 | { |
234 | { |
| 232 | uint8_t ch = _getc(indev); |
235 | wchar_t ch = _getc(indev); |
| 233 | putchar(ch); |
236 | putchar(ch); |
| 234 | return ch; |
237 | return ch; |
| 235 | } |
238 | } |
| 236 | 239 | ||
| 237 | void klog_update(void) |
240 | void klog_update(void) |
| Line 252... | Line 255... | ||
| 252 | 255 | ||
| 253 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) { |
256 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) { |
| 254 | /* Print charaters stored in kernel log */ |
257 | /* Print charaters stored in kernel log */ |
| 255 | index_t i; |
258 | index_t i; |
| 256 | for (i = klog_len - klog_stored; i < klog_len; i++) |
259 | for (i = klog_len - klog_stored; i < klog_len; i++) |
| 257 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE], silent); |
260 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent); |
| 258 | klog_stored = 0; |
261 | klog_stored = 0; |
| 259 | } |
262 | } |
| 260 | 263 | ||
| 261 | /* Store character in the cyclic kernel log */ |
264 | /* Store character in the cyclic kernel log */ |
| 262 | klog[(klog_start + klog_len) % KLOG_SIZE] = ch; |
265 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch; |
| 263 | if (klog_len < KLOG_SIZE) |
266 | if (klog_len < KLOG_LENGTH) |
| 264 | klog_len++; |
267 | klog_len++; |
| 265 | else |
268 | else |
| 266 | klog_start = (klog_start + 1) % KLOG_SIZE; |
269 | klog_start = (klog_start + 1) % KLOG_LENGTH; |
| 267 | 270 | ||
| 268 | if ((stdout) && (stdout->op->write)) |
271 | if ((stdout) && (stdout->op->write)) |
| 269 | stdout->op->write(stdout, ch, silent); |
272 | stdout->op->write(stdout, ch, silent); |
| 270 | else { |
273 | else { |
| 271 | /* The character is just in the kernel log */ |
274 | /* The character is just in the kernel log */ |
| Line 293... | Line 296... | ||
| 293 | /** Print using kernel facility |
296 | /** Print using kernel facility |
| 294 | * |
297 | * |
| 295 | * Print to kernel log. |
298 | * Print to kernel log. |
| 296 | * |
299 | * |
| 297 | */ |
300 | */ |
| 298 | 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) |
| 299 | { |
302 | { |
| 300 | char *data; |
303 | char *data; |
| 301 | int rc; |
304 | int rc; |
| 302 | 305 | ||
| 303 | if (count > PAGE_SIZE) |
306 | if (size > PAGE_SIZE) |
| 304 | return ELIMIT; |
307 | return ELIMIT; |
| 305 | 308 | ||
| 306 | if (count > 0) { |
309 | if (size > 0) { |
| 307 | data = (char *) malloc(count + 1, 0); |
310 | data = (char *) malloc(size + 1, 0); |
| 308 | if (!data) |
311 | if (!data) |
| 309 | return ENOMEM; |
312 | return ENOMEM; |
| 310 | 313 | ||
| 311 | rc = copy_from_uspace(data, buf, count); |
314 | rc = copy_from_uspace(data, buf, size); |
| 312 | if (rc) { |
315 | if (rc) { |
| 313 | free(data); |
316 | free(data); |
| 314 | return rc; |
317 | return rc; |
| 315 | } |
318 | } |
| 316 | data[count] = 0; |
319 | data[size] = 0; |
| 317 | 320 | ||
| 318 | printf("%s", data); |
321 | printf("%s", data); |
| 319 | free(data); |
322 | free(data); |
| 320 | } else |
323 | } else |
| 321 | klog_update(); |
324 | klog_update(); |
| 322 | 325 | ||
| 323 | return count; |
326 | return size; |
| 324 | } |
327 | } |
| 325 | 328 | ||
| 326 | /** @} |
329 | /** @} |
| 327 | */ |
330 | */ |