| [2677758] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2003 Josef Cejka
|
|---|
| 3 | * Copyright (c) 2005 Jakub Jermar
|
|---|
| [2677758] | 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 |
|
|---|
| [06e1e95] | 30 | /** @addtogroup genericconsole
|
|---|
| [b45c443] | 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| [2677758] | 36 | #include <console/console.h>
|
|---|
| 37 | #include <console/chardev.h>
|
|---|
| [82b71ef1] | 38 | #include <sysinfo/sysinfo.h>
|
|---|
| [2677758] | 39 | #include <synch/waitq.h>
|
|---|
| 40 | #include <synch/spinlock.h>
|
|---|
| [d99c1d2] | 41 | #include <typedefs.h>
|
|---|
| [82b71ef1] | 42 | #include <ddi/irq.h>
|
|---|
| 43 | #include <ddi/ddi.h>
|
|---|
| [13a638d] | 44 | #include <ipc/event.h>
|
|---|
| [82b71ef1] | 45 | #include <ipc/irq.h>
|
|---|
| [2677758] | 46 | #include <arch.h>
|
|---|
| [b8da2a3] | 47 | #include <panic.h>
|
|---|
| [93b84b3] | 48 | #include <print.h>
|
|---|
| [eec616b] | 49 | #include <putchar.h>
|
|---|
| [23684b7] | 50 | #include <atomic.h>
|
|---|
| [312cc68] | 51 | #include <syscall/copy.h>
|
|---|
| 52 | #include <errno.h>
|
|---|
| [19f857a] | 53 | #include <str.h>
|
|---|
| [2677758] | 54 |
|
|---|
| [13a638d] | 55 | #define KLOG_PAGES 4
|
|---|
| 56 | #define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
|
|---|
| [eec616b] | 57 | #define KLOG_LATENCY 8
|
|---|
| [c859753] | 58 |
|
|---|
| [c0855a0] | 59 | /** Kernel log cyclic buffer */
|
|---|
| [171f9a1] | 60 | static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE)));
|
|---|
| [c859753] | 61 |
|
|---|
| [c0855a0] | 62 | /** Kernel log initialized */
|
|---|
| [82b71ef1] | 63 | static bool klog_inited = false;
|
|---|
| [da1bafb] | 64 |
|
|---|
| [c0855a0] | 65 | /** First kernel log characters */
|
|---|
| [98000fb] | 66 | static size_t klog_start = 0;
|
|---|
| [da1bafb] | 67 |
|
|---|
| [c0855a0] | 68 | /** Number of valid kernel log characters */
|
|---|
| [c859753] | 69 | static size_t klog_len = 0;
|
|---|
| [da1bafb] | 70 |
|
|---|
| [c0855a0] | 71 | /** Number of stored (not printed) kernel log characters */
|
|---|
| [c859753] | 72 | static size_t klog_stored = 0;
|
|---|
| [da1bafb] | 73 |
|
|---|
| [c0855a0] | 74 | /** Number of stored kernel log characters for uspace */
|
|---|
| [82b71ef1] | 75 | static size_t klog_uspace = 0;
|
|---|
| 76 |
|
|---|
| [c0855a0] | 77 | /** Kernel log spinlock */
|
|---|
| [a71c158] | 78 | SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "*klog_lock");
|
|---|
| [82b71ef1] | 79 |
|
|---|
| 80 | /** Physical memory area used for klog buffer */
|
|---|
| 81 | static parea_t klog_parea;
|
|---|
| [516ff92] | 82 |
|
|---|
| [b9c7425] | 83 | static indev_t stdin_sink;
|
|---|
| 84 | static outdev_t stdout_source;
|
|---|
| 85 |
|
|---|
| [44b7783] | 86 | static indev_operations_t stdin_ops = {
|
|---|
| 87 | .poll = NULL
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| [da1bafb] | 90 | static void stdout_write(outdev_t *, wchar_t, bool);
|
|---|
| 91 | static void stdout_redraw(outdev_t *);
|
|---|
| [b9c7425] | 92 |
|
|---|
| 93 | static outdev_operations_t stdout_ops = {
|
|---|
| [a71c158] | 94 | .write = stdout_write,
|
|---|
| 95 | .redraw = stdout_redraw
|
|---|
| [b9c7425] | 96 | };
|
|---|
| 97 |
|
|---|
| [44b7783] | 98 | /** Silence output */
|
|---|
| 99 | bool silent = false;
|
|---|
| 100 |
|
|---|
| [ac8e7a9] | 101 | /** Standard input and output character devices */
|
|---|
| 102 | indev_t *stdin = NULL;
|
|---|
| 103 | outdev_t *stdout = NULL;
|
|---|
| [411b6a6] | 104 |
|
|---|
| [44b7783] | 105 | indev_t *stdin_wire(void)
|
|---|
| 106 | {
|
|---|
| 107 | if (stdin == NULL) {
|
|---|
| [b9c7425] | 108 | indev_initialize("stdin", &stdin_sink, &stdin_ops);
|
|---|
| 109 | stdin = &stdin_sink;
|
|---|
| [44b7783] | 110 | }
|
|---|
| 111 |
|
|---|
| 112 | return stdin;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| [b9c7425] | 115 | void stdout_wire(outdev_t *outdev)
|
|---|
| 116 | {
|
|---|
| 117 | if (stdout == NULL) {
|
|---|
| 118 | outdev_initialize("stdout", &stdout_source, &stdout_ops);
|
|---|
| 119 | stdout = &stdout_source;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | list_append(&outdev->link, &stdout->list);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | static void stdout_write(outdev_t *dev, wchar_t ch, bool silent)
|
|---|
| 126 | {
|
|---|
| 127 | link_t *cur;
|
|---|
| 128 |
|
|---|
| 129 | for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
|
|---|
| 130 | outdev_t *sink = list_get_instance(cur, outdev_t, link);
|
|---|
| [a71c158] | 131 | if ((sink) && (sink->op->write))
|
|---|
| 132 | sink->op->write(sink, ch, silent);
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static void stdout_redraw(outdev_t *dev)
|
|---|
| 137 | {
|
|---|
| 138 | link_t *cur;
|
|---|
| 139 |
|
|---|
| 140 | for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
|
|---|
| 141 | outdev_t *sink = list_get_instance(cur, outdev_t, link);
|
|---|
| 142 | if ((sink) && (sink->op->redraw))
|
|---|
| 143 | sink->op->redraw(sink);
|
|---|
| [b9c7425] | 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| [82b71ef1] | 147 | /** Initialize kernel logging facility
|
|---|
| 148 | *
|
|---|
| 149 | * The shared area contains kernel cyclic buffer. Userspace application may
|
|---|
| 150 | * be notified on new data with indication of position and size
|
|---|
| 151 | * of the data within the circular buffer.
|
|---|
| [ac8e7a9] | 152 | *
|
|---|
| [82b71ef1] | 153 | */
|
|---|
| 154 | void klog_init(void)
|
|---|
| 155 | {
|
|---|
| 156 | void *faddr = (void *) KA2PA(klog);
|
|---|
| 157 |
|
|---|
| 158 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
|
|---|
| 159 |
|
|---|
| 160 | klog_parea.pbase = (uintptr_t) faddr;
|
|---|
| [eec616b] | 161 | klog_parea.frames = SIZE2FRAMES(sizeof(klog));
|
|---|
| [d7533c7] | 162 | klog_parea.unpriv = false;
|
|---|
| [82b71ef1] | 163 | ddi_parea_register(&klog_parea);
|
|---|
| [ac8e7a9] | 164 |
|
|---|
| [96b02eb9] | 165 | sysinfo_set_item_val("klog.faddr", NULL, (sysarg_t) faddr);
|
|---|
| [13a638d] | 166 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
|
|---|
| [ac8e7a9] | 167 |
|
|---|
| [82b71ef1] | 168 | spinlock_lock(&klog_lock);
|
|---|
| 169 | klog_inited = true;
|
|---|
| 170 | spinlock_unlock(&klog_lock);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| [516ff92] | 173 | void grab_console(void)
|
|---|
| 174 | {
|
|---|
| [422fd81] | 175 | bool prev = silent;
|
|---|
| 176 |
|
|---|
| [516ff92] | 177 | silent = false;
|
|---|
| [a71c158] | 178 | if ((stdout) && (stdout->op->redraw))
|
|---|
| 179 | stdout->op->redraw(stdout);
|
|---|
| [402de0c] | 180 |
|
|---|
| [da1bafb] | 181 | if ((stdin) && (prev)) {
|
|---|
| 182 | /*
|
|---|
| 183 | * Force the console to print the prompt.
|
|---|
| 184 | */
|
|---|
| [402de0c] | 185 | indev_push_character(stdin, '\n');
|
|---|
| [da1bafb] | 186 | }
|
|---|
| [516ff92] | 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void release_console(void)
|
|---|
| 190 | {
|
|---|
| [a71c158] | 191 | // FIXME arch_release_console
|
|---|
| [516ff92] | 192 | silent = true;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| [312cc68] | 195 | /** Tell kernel to get keyboard/console access again */
|
|---|
| [96b02eb9] | 196 | sysarg_t sys_debug_enable_console(void)
|
|---|
| [312cc68] | 197 | {
|
|---|
| 198 | #ifdef CONFIG_KCONSOLE
|
|---|
| 199 | grab_console();
|
|---|
| 200 | return true;
|
|---|
| 201 | #else
|
|---|
| 202 | return false;
|
|---|
| 203 | #endif
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /** Tell kernel to relinquish keyboard/console access */
|
|---|
| [96b02eb9] | 207 | sysarg_t sys_debug_disable_console(void)
|
|---|
| [312cc68] | 208 | {
|
|---|
| 209 | release_console();
|
|---|
| 210 | return true;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| [ac8e7a9] | 213 | /** Get string from input character device.
|
|---|
| [2677758] | 214 | *
|
|---|
| [ac8e7a9] | 215 | * Read characters from input character device until first occurrence
|
|---|
| [2677758] | 216 | * of newline character.
|
|---|
| 217 | *
|
|---|
| [ac8e7a9] | 218 | * @param indev Input character device.
|
|---|
| [c583970] | 219 | * @param buf Buffer where to store string terminated by NULL.
|
|---|
| [abbc16e] | 220 | * @param buflen Size of the buffer.
|
|---|
| [ff3b3197] | 221 | *
|
|---|
| 222 | * @return Number of characters read.
|
|---|
| [ac8e7a9] | 223 | *
|
|---|
| [2677758] | 224 | */
|
|---|
| [98000fb] | 225 | size_t gets(indev_t *indev, char *buf, size_t buflen)
|
|---|
| [2677758] | 226 | {
|
|---|
| [c583970] | 227 | size_t offset = 0;
|
|---|
| [98000fb] | 228 | size_t count = 0;
|
|---|
| [c583970] | 229 | buf[offset] = 0;
|
|---|
| [516ff92] | 230 |
|
|---|
| [c583970] | 231 | wchar_t ch;
|
|---|
| [44b7783] | 232 | while ((ch = indev_pop_character(indev)) != '\n') {
|
|---|
| [e8a9dc3] | 233 | if (ch == '\b') {
|
|---|
| [c583970] | 234 | if (count > 0) {
|
|---|
| 235 | /* Space, backspace, space */
|
|---|
| [e8a9dc3] | 236 | putchar('\b');
|
|---|
| 237 | putchar(' ');
|
|---|
| 238 | putchar('\b');
|
|---|
| [c583970] | 239 |
|
|---|
| 240 | count--;
|
|---|
| 241 | offset = str_lsize(buf, count);
|
|---|
| 242 | buf[offset] = 0;
|
|---|
| [e8a9dc3] | 243 | }
|
|---|
| [2677758] | 244 | }
|
|---|
| [c583970] | 245 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
|
|---|
| 246 | putchar(ch);
|
|---|
| 247 | count++;
|
|---|
| 248 | buf[offset] = 0;
|
|---|
| 249 | }
|
|---|
| [2677758] | 250 | }
|
|---|
| [ac8e7a9] | 251 |
|
|---|
| [c583970] | 252 | return count;
|
|---|
| [2677758] | 253 | }
|
|---|
| 254 |
|
|---|
| [ac8e7a9] | 255 | /** Get character from input device & echo it to screen */
|
|---|
| [c583970] | 256 | wchar_t getc(indev_t *indev)
|
|---|
| [2677758] | 257 | {
|
|---|
| [44b7783] | 258 | wchar_t ch = indev_pop_character(indev);
|
|---|
| [e8a9dc3] | 259 | putchar(ch);
|
|---|
| [2677758] | 260 | return ch;
|
|---|
| 261 | }
|
|---|
| [973be64e] | 262 |
|
|---|
| [82b71ef1] | 263 | void klog_update(void)
|
|---|
| 264 | {
|
|---|
| 265 | spinlock_lock(&klog_lock);
|
|---|
| 266 |
|
|---|
| [18251cc] | 267 | if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
|
|---|
| [05641a9e] | 268 | event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
|
|---|
| 269 | klog_uspace = 0;
|
|---|
| 270 | }
|
|---|
| [82b71ef1] | 271 |
|
|---|
| 272 | spinlock_unlock(&klog_lock);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| [eec616b] | 275 | void putchar(const wchar_t ch)
|
|---|
| [973be64e] | 276 | {
|
|---|
| [82b71ef1] | 277 | spinlock_lock(&klog_lock);
|
|---|
| 278 |
|
|---|
| [ac8e7a9] | 279 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
|
|---|
| [c859753] | 280 | /* Print charaters stored in kernel log */
|
|---|
| [98000fb] | 281 | size_t i;
|
|---|
| [c859753] | 282 | for (i = klog_len - klog_stored; i < klog_len; i++)
|
|---|
| [171f9a1] | 283 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
|
|---|
| [c859753] | 284 | klog_stored = 0;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | /* Store character in the cyclic kernel log */
|
|---|
| [171f9a1] | 288 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
|
|---|
| 289 | if (klog_len < KLOG_LENGTH)
|
|---|
| [c859753] | 290 | klog_len++;
|
|---|
| 291 | else
|
|---|
| [171f9a1] | 292 | klog_start = (klog_start + 1) % KLOG_LENGTH;
|
|---|
| [c859753] | 293 |
|
|---|
| [ac8e7a9] | 294 | if ((stdout) && (stdout->op->write))
|
|---|
| [eec616b] | 295 | stdout->op->write(stdout, ch, silent);
|
|---|
| [c859753] | 296 | else {
|
|---|
| [da52547] | 297 | /*
|
|---|
| 298 | * No standard output routine defined yet.
|
|---|
| 299 | * The character is still stored in the kernel log
|
|---|
| 300 | * for possible future output.
|
|---|
| 301 | *
|
|---|
| 302 | * The early_putchar() function is used to output
|
|---|
| 303 | * the character for low-level debugging purposes.
|
|---|
| 304 | * Note that the early_putc() function might be
|
|---|
| 305 | * a no-op on certain hardware configurations.
|
|---|
| 306 | *
|
|---|
| 307 | */
|
|---|
| 308 | early_putchar(ch);
|
|---|
| 309 |
|
|---|
| [c859753] | 310 | if (klog_stored < klog_len)
|
|---|
| 311 | klog_stored++;
|
|---|
| 312 | }
|
|---|
| [82b71ef1] | 313 |
|
|---|
| 314 | /* The character is stored for uspace */
|
|---|
| 315 | if (klog_uspace < klog_len)
|
|---|
| 316 | klog_uspace++;
|
|---|
| 317 |
|
|---|
| [c05a50f] | 318 | /* Check notify uspace to update */
|
|---|
| 319 | bool update;
|
|---|
| [eec616b] | 320 | if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
|
|---|
| [c05a50f] | 321 | update = true;
|
|---|
| 322 | else
|
|---|
| 323 | update = false;
|
|---|
| 324 |
|
|---|
| [82b71ef1] | 325 | spinlock_unlock(&klog_lock);
|
|---|
| 326 |
|
|---|
| [c05a50f] | 327 | if (update)
|
|---|
| 328 | klog_update();
|
|---|
| [973be64e] | 329 | }
|
|---|
| [b45c443] | 330 |
|
|---|
| [312cc68] | 331 | /** Print using kernel facility
|
|---|
| 332 | *
|
|---|
| 333 | * Print to kernel log.
|
|---|
| 334 | *
|
|---|
| 335 | */
|
|---|
| [96b02eb9] | 336 | sysarg_t sys_klog(int fd, const void *buf, size_t size)
|
|---|
| [312cc68] | 337 | {
|
|---|
| 338 | char *data;
|
|---|
| 339 | int rc;
|
|---|
| [eec616b] | 340 |
|
|---|
| [c583970] | 341 | if (size > PAGE_SIZE)
|
|---|
| [96b02eb9] | 342 | return (sysarg_t) ELIMIT;
|
|---|
| [312cc68] | 343 |
|
|---|
| [c583970] | 344 | if (size > 0) {
|
|---|
| 345 | data = (char *) malloc(size + 1, 0);
|
|---|
| [312cc68] | 346 | if (!data)
|
|---|
| [96b02eb9] | 347 | return (sysarg_t) ENOMEM;
|
|---|
| [312cc68] | 348 |
|
|---|
| [c583970] | 349 | rc = copy_from_uspace(data, buf, size);
|
|---|
| [312cc68] | 350 | if (rc) {
|
|---|
| 351 | free(data);
|
|---|
| [96b02eb9] | 352 | return (sysarg_t) rc;
|
|---|
| [312cc68] | 353 | }
|
|---|
| [c583970] | 354 | data[size] = 0;
|
|---|
| [312cc68] | 355 |
|
|---|
| 356 | printf("%s", data);
|
|---|
| 357 | free(data);
|
|---|
| 358 | } else
|
|---|
| 359 | klog_update();
|
|---|
| 360 |
|
|---|
| [c583970] | 361 | return size;
|
|---|
| [312cc68] | 362 | }
|
|---|
| 363 |
|
|---|
| [06e1e95] | 364 | /** @}
|
|---|
| [b45c443] | 365 | */
|
|---|