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