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