| 1 | /*
|
|---|
| 2 | * Copyright (c) 2003 Josef Cejka
|
|---|
| 3 | * Copyright (c) 2005 Jakub 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 |
|
|---|
| 30 | /** @addtogroup kernel_generic_console
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <abi/kio.h>
|
|---|
| 37 | #include <arch.h>
|
|---|
| 38 | #include <assert.h>
|
|---|
| 39 | #include <atomic.h>
|
|---|
| 40 | #include <console/chardev.h>
|
|---|
| 41 | #include <console/console.h>
|
|---|
| 42 | #include <ddi/ddi.h>
|
|---|
| 43 | #include <ddi/irq.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <ipc/event.h>
|
|---|
| 46 | #include <ipc/irq.h>
|
|---|
| 47 | #include <mm/frame.h> /* SIZE2FRAMES */
|
|---|
| 48 | #include <panic.h>
|
|---|
| 49 | #include <preemption.h>
|
|---|
| 50 | #include <proc/thread.h>
|
|---|
| 51 | #include <putchar.h>
|
|---|
| 52 | #include <stdatomic.h>
|
|---|
| 53 | #include <stdio.h>
|
|---|
| 54 | #include <stdlib.h> /* malloc */
|
|---|
| 55 | #include <str.h>
|
|---|
| 56 | #include <synch/mutex.h>
|
|---|
| 57 | #include <synch/spinlock.h>
|
|---|
| 58 | #include <synch/waitq.h>
|
|---|
| 59 | #include <syscall/copy.h>
|
|---|
| 60 | #include <sysinfo/sysinfo.h>
|
|---|
| 61 | #include <typedefs.h>
|
|---|
| 62 |
|
|---|
| 63 | #define KIO_PAGES 8
|
|---|
| 64 | #define KIO_LENGTH (KIO_PAGES * PAGE_SIZE / sizeof(char32_t))
|
|---|
| 65 |
|
|---|
| 66 | /** Kernel log cyclic buffer */
|
|---|
| 67 | char32_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
|
|---|
| 68 |
|
|---|
| 69 | /** Kernel log initialized */
|
|---|
| 70 | static atomic_bool kio_inited = ATOMIC_VAR_INIT(false);
|
|---|
| 71 |
|
|---|
| 72 | /** A mutex for preventing interleaving of output lines from different threads.
|
|---|
| 73 | * May not be held in some circumstances, so locking of any internal shared
|
|---|
| 74 | * structures is still necessary.
|
|---|
| 75 | */
|
|---|
| 76 | static MUTEX_INITIALIZE(console_mutex, MUTEX_RECURSIVE);
|
|---|
| 77 |
|
|---|
| 78 | /** First kernel log characters */
|
|---|
| 79 | static size_t kio_start = 0;
|
|---|
| 80 |
|
|---|
| 81 | /** Number of valid kernel log characters */
|
|---|
| 82 | static size_t kio_len = 0;
|
|---|
| 83 |
|
|---|
| 84 | /** Number of stored (not printed) kernel log characters */
|
|---|
| 85 | static size_t kio_stored = 0;
|
|---|
| 86 |
|
|---|
| 87 | /** Number of stored kernel log characters for uspace */
|
|---|
| 88 | static size_t kio_uspace = 0;
|
|---|
| 89 |
|
|---|
| 90 | /** Kernel log spinlock */
|
|---|
| 91 | SPINLOCK_INITIALIZE_NAME(kio_lock, "kio_lock");
|
|---|
| 92 |
|
|---|
| 93 | /** Physical memory area used for kio buffer */
|
|---|
| 94 | static parea_t kio_parea;
|
|---|
| 95 |
|
|---|
| 96 | static indev_t stdin_sink;
|
|---|
| 97 | static outdev_t stdout_source;
|
|---|
| 98 |
|
|---|
| 99 | static void stdin_signal(indev_t *, indev_signal_t);
|
|---|
| 100 |
|
|---|
| 101 | static indev_operations_t stdin_ops = {
|
|---|
| 102 | .poll = NULL,
|
|---|
| 103 | .signal = stdin_signal
|
|---|
| 104 | };
|
|---|
| 105 |
|
|---|
| 106 | static void stdout_write(outdev_t *, char32_t);
|
|---|
| 107 | static void stdout_redraw(outdev_t *);
|
|---|
| 108 | static void stdout_scroll_up(outdev_t *);
|
|---|
| 109 | static void stdout_scroll_down(outdev_t *);
|
|---|
| 110 |
|
|---|
| 111 | static outdev_operations_t stdout_ops = {
|
|---|
| 112 | .write = stdout_write,
|
|---|
| 113 | .redraw = stdout_redraw,
|
|---|
| 114 | .scroll_up = stdout_scroll_up,
|
|---|
| 115 | .scroll_down = stdout_scroll_down
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | /** Override kernel console lockout */
|
|---|
| 119 | bool console_override = false;
|
|---|
| 120 |
|
|---|
| 121 | /** Standard input and output character devices */
|
|---|
| 122 | indev_t *stdin = NULL;
|
|---|
| 123 | outdev_t *stdout = NULL;
|
|---|
| 124 |
|
|---|
| 125 | indev_t *stdin_wire(void)
|
|---|
| 126 | {
|
|---|
| 127 | if (stdin == NULL) {
|
|---|
| 128 | indev_initialize("stdin", &stdin_sink, &stdin_ops);
|
|---|
| 129 | stdin = &stdin_sink;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return stdin;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | static void stdin_signal(indev_t *indev, indev_signal_t signal)
|
|---|
| 136 | {
|
|---|
| 137 | switch (signal) {
|
|---|
| 138 | case INDEV_SIGNAL_SCROLL_UP:
|
|---|
| 139 | if (stdout != NULL)
|
|---|
| 140 | stdout_scroll_up(stdout);
|
|---|
| 141 | break;
|
|---|
| 142 | case INDEV_SIGNAL_SCROLL_DOWN:
|
|---|
| 143 | if (stdout != NULL)
|
|---|
| 144 | stdout_scroll_down(stdout);
|
|---|
| 145 | break;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void stdout_wire(outdev_t *outdev)
|
|---|
| 150 | {
|
|---|
| 151 | if (stdout == NULL) {
|
|---|
| 152 | outdev_initialize("stdout", &stdout_source, &stdout_ops);
|
|---|
| 153 | stdout = &stdout_source;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | list_append(&outdev->link, &stdout->list);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | static void stdout_write(outdev_t *dev, char32_t ch)
|
|---|
| 160 | {
|
|---|
| 161 | list_foreach(dev->list, link, outdev_t, sink) {
|
|---|
| 162 | if ((sink) && (sink->op->write))
|
|---|
| 163 | sink->op->write(sink, ch);
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | static void stdout_redraw(outdev_t *dev)
|
|---|
| 168 | {
|
|---|
| 169 | list_foreach(dev->list, link, outdev_t, sink) {
|
|---|
| 170 | if ((sink) && (sink->op->redraw))
|
|---|
| 171 | sink->op->redraw(sink);
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | static void stdout_scroll_up(outdev_t *dev)
|
|---|
| 176 | {
|
|---|
| 177 | list_foreach(dev->list, link, outdev_t, sink) {
|
|---|
| 178 | if ((sink) && (sink->op->scroll_up))
|
|---|
| 179 | sink->op->scroll_up(sink);
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | static void stdout_scroll_down(outdev_t *dev)
|
|---|
| 184 | {
|
|---|
| 185 | list_foreach(dev->list, link, outdev_t, sink) {
|
|---|
| 186 | if ((sink) && (sink->op->scroll_down))
|
|---|
| 187 | sink->op->scroll_down(sink);
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /** Initialize kernel logging facility
|
|---|
| 192 | *
|
|---|
| 193 | * The shared area contains kernel cyclic buffer. Userspace application may
|
|---|
| 194 | * be notified on new data with indication of position and size
|
|---|
| 195 | * of the data within the circular buffer.
|
|---|
| 196 | *
|
|---|
| 197 | */
|
|---|
| 198 | void kio_init(void)
|
|---|
| 199 | {
|
|---|
| 200 | void *faddr = (void *) KA2PA(kio);
|
|---|
| 201 |
|
|---|
| 202 | assert((uintptr_t) faddr % FRAME_SIZE == 0);
|
|---|
| 203 |
|
|---|
| 204 | ddi_parea_init(&kio_parea);
|
|---|
| 205 | kio_parea.pbase = (uintptr_t) faddr;
|
|---|
| 206 | kio_parea.frames = SIZE2FRAMES(sizeof(kio));
|
|---|
| 207 | kio_parea.unpriv = false;
|
|---|
| 208 | kio_parea.mapped = false;
|
|---|
| 209 | ddi_parea_register(&kio_parea);
|
|---|
| 210 |
|
|---|
| 211 | sysinfo_set_item_val("kio.faddr", NULL, (sysarg_t) faddr);
|
|---|
| 212 | sysinfo_set_item_val("kio.pages", NULL, KIO_PAGES);
|
|---|
| 213 |
|
|---|
| 214 | event_set_unmask_callback(EVENT_KIO, kio_update);
|
|---|
| 215 | atomic_store(&kio_inited, true);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | void grab_console(void)
|
|---|
| 219 | {
|
|---|
| 220 | sysinfo_set_item_val("kconsole", NULL, true);
|
|---|
| 221 | event_notify_1(EVENT_KCONSOLE, false, true);
|
|---|
| 222 | bool prev = console_override;
|
|---|
| 223 |
|
|---|
| 224 | console_override = true;
|
|---|
| 225 | if ((stdout) && (stdout->op->redraw))
|
|---|
| 226 | stdout->op->redraw(stdout);
|
|---|
| 227 |
|
|---|
| 228 | if ((stdin) && (!prev)) {
|
|---|
| 229 | /*
|
|---|
| 230 | * Force the console to print the prompt.
|
|---|
| 231 | */
|
|---|
| 232 | indev_push_character(stdin, '\n');
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | void release_console(void)
|
|---|
| 237 | {
|
|---|
| 238 | sysinfo_set_item_val("kconsole", NULL, false);
|
|---|
| 239 | console_override = false;
|
|---|
| 240 | event_notify_1(EVENT_KCONSOLE, false, false);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /** Activate kernel console override */
|
|---|
| 244 | sysarg_t sys_debug_console(void)
|
|---|
| 245 | {
|
|---|
| 246 | #ifdef CONFIG_KCONSOLE
|
|---|
| 247 | grab_console();
|
|---|
| 248 | return true;
|
|---|
| 249 | #else
|
|---|
| 250 | return false;
|
|---|
| 251 | #endif
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | void kio_update(void *event)
|
|---|
| 255 | {
|
|---|
| 256 | if (!atomic_load(&kio_inited))
|
|---|
| 257 | return;
|
|---|
| 258 |
|
|---|
| 259 | spinlock_lock(&kio_lock);
|
|---|
| 260 |
|
|---|
| 261 | if (kio_uspace > 0) {
|
|---|
| 262 | if (event_notify_3(EVENT_KIO, true, kio_start, kio_len,
|
|---|
| 263 | kio_uspace) == EOK)
|
|---|
| 264 | kio_uspace = 0;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | spinlock_unlock(&kio_lock);
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /** Flush characters that are stored in the output buffer
|
|---|
| 271 | *
|
|---|
| 272 | */
|
|---|
| 273 | void kio_flush(void)
|
|---|
| 274 | {
|
|---|
| 275 | bool ordy = ((stdout) && (stdout->op->write));
|
|---|
| 276 |
|
|---|
| 277 | if (!ordy)
|
|---|
| 278 | return;
|
|---|
| 279 |
|
|---|
| 280 | spinlock_lock(&kio_lock);
|
|---|
| 281 |
|
|---|
| 282 | /* Print characters that weren't printed earlier */
|
|---|
| 283 | while (kio_stored > 0) {
|
|---|
| 284 | char32_t tmp = kio[(kio_start + kio_len - kio_stored) % KIO_LENGTH];
|
|---|
| 285 | kio_stored--;
|
|---|
| 286 |
|
|---|
| 287 | /*
|
|---|
| 288 | * We need to give up the spinlock for
|
|---|
| 289 | * the physical operation of writing out
|
|---|
| 290 | * the character.
|
|---|
| 291 | */
|
|---|
| 292 | spinlock_unlock(&kio_lock);
|
|---|
| 293 | stdout->op->write(stdout, tmp);
|
|---|
| 294 | spinlock_lock(&kio_lock);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | spinlock_unlock(&kio_lock);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /** Put a character into the output buffer.
|
|---|
| 301 | *
|
|---|
| 302 | * The caller is required to hold kio_lock
|
|---|
| 303 | */
|
|---|
| 304 | void kio_push_char(const char32_t ch)
|
|---|
| 305 | {
|
|---|
| 306 | kio[(kio_start + kio_len) % KIO_LENGTH] = ch;
|
|---|
| 307 | if (kio_len < KIO_LENGTH)
|
|---|
| 308 | kio_len++;
|
|---|
| 309 | else
|
|---|
| 310 | kio_start = (kio_start + 1) % KIO_LENGTH;
|
|---|
| 311 |
|
|---|
| 312 | if (kio_stored < kio_len)
|
|---|
| 313 | kio_stored++;
|
|---|
| 314 |
|
|---|
| 315 | /* The character is stored for uspace */
|
|---|
| 316 | if (kio_uspace < kio_len)
|
|---|
| 317 | kio_uspace++;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | void putuchar(const char32_t ch)
|
|---|
| 321 | {
|
|---|
| 322 | bool ordy = ((stdout) && (stdout->op->write));
|
|---|
| 323 |
|
|---|
| 324 | spinlock_lock(&kio_lock);
|
|---|
| 325 | kio_push_char(ch);
|
|---|
| 326 | spinlock_unlock(&kio_lock);
|
|---|
| 327 |
|
|---|
| 328 | /* Output stored characters */
|
|---|
| 329 | kio_flush();
|
|---|
| 330 |
|
|---|
| 331 | if (!ordy) {
|
|---|
| 332 | /*
|
|---|
| 333 | * No standard output routine defined yet.
|
|---|
| 334 | * The character is still stored in the kernel log
|
|---|
| 335 | * for possible future output.
|
|---|
| 336 | *
|
|---|
| 337 | * The early_putuchar() function is used to output
|
|---|
| 338 | * the character for low-level debugging purposes.
|
|---|
| 339 | * Note that the early_putuchar() function might be
|
|---|
| 340 | * a no-op on certain hardware configurations.
|
|---|
| 341 | */
|
|---|
| 342 | early_putuchar(ch);
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | /* Force notification on newline */
|
|---|
| 346 | if (ch == '\n')
|
|---|
| 347 | kio_update(NULL);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /** Print using kernel facility
|
|---|
| 351 | *
|
|---|
| 352 | * Print to kernel log.
|
|---|
| 353 | *
|
|---|
| 354 | */
|
|---|
| 355 | sys_errno_t sys_kio(int cmd, uspace_addr_t buf, size_t size)
|
|---|
| 356 | {
|
|---|
| 357 | char *data;
|
|---|
| 358 | errno_t rc;
|
|---|
| 359 |
|
|---|
| 360 | switch (cmd) {
|
|---|
| 361 | case KIO_UPDATE:
|
|---|
| 362 | kio_update(NULL);
|
|---|
| 363 | return EOK;
|
|---|
| 364 | case KIO_WRITE:
|
|---|
| 365 | case KIO_COMMAND:
|
|---|
| 366 | break;
|
|---|
| 367 | default:
|
|---|
| 368 | return ENOTSUP;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | if (size > PAGE_SIZE)
|
|---|
| 372 | return (sys_errno_t) ELIMIT;
|
|---|
| 373 |
|
|---|
| 374 | if (size > 0) {
|
|---|
| 375 | data = (char *) malloc(size + 1);
|
|---|
| 376 | if (!data)
|
|---|
| 377 | return (sys_errno_t) ENOMEM;
|
|---|
| 378 |
|
|---|
| 379 | rc = copy_from_uspace(data, buf, size);
|
|---|
| 380 | if (rc) {
|
|---|
| 381 | free(data);
|
|---|
| 382 | return (sys_errno_t) rc;
|
|---|
| 383 | }
|
|---|
| 384 | data[size] = 0;
|
|---|
| 385 |
|
|---|
| 386 | switch (cmd) {
|
|---|
| 387 | case KIO_WRITE:
|
|---|
| 388 | printf("[%s(%lu)] %s\n", TASK->name,
|
|---|
| 389 | (unsigned long) TASK->taskid, data);
|
|---|
| 390 | break;
|
|---|
| 391 | case KIO_COMMAND:
|
|---|
| 392 | if (!stdin)
|
|---|
| 393 | break;
|
|---|
| 394 | for (unsigned int i = 0; i < size; i++)
|
|---|
| 395 | indev_push_character(stdin, data[i]);
|
|---|
| 396 | indev_push_character(stdin, '\n');
|
|---|
| 397 | break;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | free(data);
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | return EOK;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | /** Lock console output, ensuring that lines from different threads don't
|
|---|
| 407 | * interleave. Does nothing when preemption is disabled, so that debugging
|
|---|
| 408 | * and error printouts in sensitive areas still work.
|
|---|
| 409 | */
|
|---|
| 410 | void console_lock(void)
|
|---|
| 411 | {
|
|---|
| 412 | if (!PREEMPTION_DISABLED)
|
|---|
| 413 | mutex_lock(&console_mutex);
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /** Unlocks console output. See console_lock()
|
|---|
| 417 | */
|
|---|
| 418 | void console_unlock(void)
|
|---|
| 419 | {
|
|---|
| 420 | if (!PREEMPTION_DISABLED)
|
|---|
| 421 | mutex_unlock(&console_mutex);
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /** @}
|
|---|
| 425 | */
|
|---|