| 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 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 |
|
|---|
| 65 | /** First kernel log characters */
|
|---|
| 66 | static size_t klog_start = 0;
|
|---|
| 67 |
|
|---|
| 68 | /** Number of valid kernel log characters */
|
|---|
| 69 | static size_t klog_len = 0;
|
|---|
| 70 |
|
|---|
| 71 | /** Number of stored (not printed) kernel log characters */
|
|---|
| 72 | static size_t klog_stored = 0;
|
|---|
| 73 |
|
|---|
| 74 | /** Number of stored kernel log characters for uspace */
|
|---|
| 75 | static size_t klog_uspace = 0;
|
|---|
| 76 |
|
|---|
| 77 | /** Kernel log spinlock */
|
|---|
| 78 | SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "*klog_lock");
|
|---|
| 79 |
|
|---|
| 80 | /** Physical memory area used for klog buffer */
|
|---|
| 81 | static parea_t klog_parea;
|
|---|
| 82 |
|
|---|
| 83 | static indev_t stdin_sink;
|
|---|
| 84 | static outdev_t stdout_source;
|
|---|
| 85 |
|
|---|
| 86 | static indev_operations_t stdin_ops = {
|
|---|
| 87 | .poll = NULL
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | static void stdout_write(outdev_t *, wchar_t, bool);
|
|---|
| 91 | static void stdout_redraw(outdev_t *);
|
|---|
| 92 |
|
|---|
| 93 | static outdev_operations_t stdout_ops = {
|
|---|
| 94 | .write = stdout_write,
|
|---|
| 95 | .redraw = stdout_redraw
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | /** Silence output */
|
|---|
| 99 | bool silent = false;
|
|---|
| 100 |
|
|---|
| 101 | /** Standard input and output character devices */
|
|---|
| 102 | indev_t *stdin = NULL;
|
|---|
| 103 | outdev_t *stdout = NULL;
|
|---|
| 104 |
|
|---|
| 105 | indev_t *stdin_wire(void)
|
|---|
| 106 | {
|
|---|
| 107 | if (stdin == NULL) {
|
|---|
| 108 | indev_initialize("stdin", &stdin_sink, &stdin_ops);
|
|---|
| 109 | stdin = &stdin_sink;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | return stdin;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 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);
|
|---|
| 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);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 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.
|
|---|
| 152 | *
|
|---|
| 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;
|
|---|
| 161 | klog_parea.frames = SIZE2FRAMES(sizeof(klog));
|
|---|
| 162 | ddi_parea_register(&klog_parea);
|
|---|
| 163 |
|
|---|
| 164 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
|
|---|
| 165 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
|
|---|
| 166 |
|
|---|
| 167 | spinlock_lock(&klog_lock);
|
|---|
| 168 | klog_inited = true;
|
|---|
| 169 | spinlock_unlock(&klog_lock);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | void grab_console(void)
|
|---|
| 173 | {
|
|---|
| 174 | bool prev = silent;
|
|---|
| 175 |
|
|---|
| 176 | silent = false;
|
|---|
| 177 | if ((stdout) && (stdout->op->redraw))
|
|---|
| 178 | stdout->op->redraw(stdout);
|
|---|
| 179 |
|
|---|
| 180 | if ((stdin) && (prev)) {
|
|---|
| 181 | /*
|
|---|
| 182 | * Force the console to print the prompt.
|
|---|
| 183 | */
|
|---|
| 184 | indev_push_character(stdin, '\n');
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | void release_console(void)
|
|---|
| 189 | {
|
|---|
| 190 | // FIXME arch_release_console
|
|---|
| 191 | silent = true;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /** Tell kernel to get keyboard/console access again */
|
|---|
| 195 | unative_t sys_debug_enable_console(void)
|
|---|
| 196 | {
|
|---|
| 197 | #ifdef CONFIG_KCONSOLE
|
|---|
| 198 | grab_console();
|
|---|
| 199 | return true;
|
|---|
| 200 | #else
|
|---|
| 201 | return false;
|
|---|
| 202 | #endif
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Tell kernel to relinquish keyboard/console access */
|
|---|
| 206 | unative_t sys_debug_disable_console(void)
|
|---|
| 207 | {
|
|---|
| 208 | release_console();
|
|---|
| 209 | return true;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /** Get string from input character device.
|
|---|
| 213 | *
|
|---|
| 214 | * Read characters from input character device until first occurrence
|
|---|
| 215 | * of newline character.
|
|---|
| 216 | *
|
|---|
| 217 | * @param indev Input character device.
|
|---|
| 218 | * @param buf Buffer where to store string terminated by NULL.
|
|---|
| 219 | * @param buflen Size of the buffer.
|
|---|
| 220 | *
|
|---|
| 221 | * @return Number of characters read.
|
|---|
| 222 | *
|
|---|
| 223 | */
|
|---|
| 224 | size_t gets(indev_t *indev, char *buf, size_t buflen)
|
|---|
| 225 | {
|
|---|
| 226 | size_t offset = 0;
|
|---|
| 227 | size_t count = 0;
|
|---|
| 228 | buf[offset] = 0;
|
|---|
| 229 |
|
|---|
| 230 | wchar_t ch;
|
|---|
| 231 | while ((ch = indev_pop_character(indev)) != '\n') {
|
|---|
| 232 | if (ch == '\b') {
|
|---|
| 233 | if (count > 0) {
|
|---|
| 234 | /* Space, backspace, space */
|
|---|
| 235 | putchar('\b');
|
|---|
| 236 | putchar(' ');
|
|---|
| 237 | putchar('\b');
|
|---|
| 238 |
|
|---|
| 239 | count--;
|
|---|
| 240 | offset = str_lsize(buf, count);
|
|---|
| 241 | buf[offset] = 0;
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
|
|---|
| 245 | putchar(ch);
|
|---|
| 246 | count++;
|
|---|
| 247 | buf[offset] = 0;
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | return count;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | /** Get character from input device & echo it to screen */
|
|---|
| 255 | wchar_t getc(indev_t *indev)
|
|---|
| 256 | {
|
|---|
| 257 | wchar_t ch = indev_pop_character(indev);
|
|---|
| 258 | putchar(ch);
|
|---|
| 259 | return ch;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void klog_update(void)
|
|---|
| 263 | {
|
|---|
| 264 | spinlock_lock(&klog_lock);
|
|---|
| 265 |
|
|---|
| 266 | if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
|
|---|
| 267 | event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
|
|---|
| 268 | klog_uspace = 0;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | spinlock_unlock(&klog_lock);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | void putchar(const wchar_t ch)
|
|---|
| 275 | {
|
|---|
| 276 | spinlock_lock(&klog_lock);
|
|---|
| 277 |
|
|---|
| 278 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
|
|---|
| 279 | /* Print charaters stored in kernel log */
|
|---|
| 280 | size_t i;
|
|---|
| 281 | for (i = klog_len - klog_stored; i < klog_len; i++)
|
|---|
| 282 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
|
|---|
| 283 | klog_stored = 0;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /* Store character in the cyclic kernel log */
|
|---|
| 287 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
|
|---|
| 288 | if (klog_len < KLOG_LENGTH)
|
|---|
| 289 | klog_len++;
|
|---|
| 290 | else
|
|---|
| 291 | klog_start = (klog_start + 1) % KLOG_LENGTH;
|
|---|
| 292 |
|
|---|
| 293 | if ((stdout) && (stdout->op->write))
|
|---|
| 294 | stdout->op->write(stdout, ch, silent);
|
|---|
| 295 | else {
|
|---|
| 296 | /* The character is just in the kernel log */
|
|---|
| 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 | /* Check notify uspace to update */
|
|---|
| 306 | bool update;
|
|---|
| 307 | if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
|
|---|
| 308 | update = true;
|
|---|
| 309 | else
|
|---|
| 310 | update = false;
|
|---|
| 311 |
|
|---|
| 312 | spinlock_unlock(&klog_lock);
|
|---|
| 313 |
|
|---|
| 314 | if (update)
|
|---|
| 315 | klog_update();
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /** Print using kernel facility
|
|---|
| 319 | *
|
|---|
| 320 | * Print to kernel log.
|
|---|
| 321 | *
|
|---|
| 322 | */
|
|---|
| 323 | unative_t sys_klog(int fd, const void *buf, size_t size)
|
|---|
| 324 | {
|
|---|
| 325 | char *data;
|
|---|
| 326 | int rc;
|
|---|
| 327 |
|
|---|
| 328 | if (size > PAGE_SIZE)
|
|---|
| 329 | return (unative_t) ELIMIT;
|
|---|
| 330 |
|
|---|
| 331 | if (size > 0) {
|
|---|
| 332 | data = (char *) malloc(size + 1, 0);
|
|---|
| 333 | if (!data)
|
|---|
| 334 | return (unative_t) ENOMEM;
|
|---|
| 335 |
|
|---|
| 336 | rc = copy_from_uspace(data, buf, size);
|
|---|
| 337 | if (rc) {
|
|---|
| 338 | free(data);
|
|---|
| 339 | return (unative_t) rc;
|
|---|
| 340 | }
|
|---|
| 341 | data[size] = 0;
|
|---|
| 342 |
|
|---|
| 343 | printf("%s", data);
|
|---|
| 344 | free(data);
|
|---|
| 345 | } else
|
|---|
| 346 | klog_update();
|
|---|
| 347 |
|
|---|
| 348 | return size;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /** @}
|
|---|
| 352 | */
|
|---|