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