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