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