[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 */
|
---|
[c859753] | 64 | static index_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 |
|
---|
[44b7783] | 78 | static indev_operations_t stdin_ops = {
|
---|
| 79 | .poll = NULL
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | /** Silence output */
|
---|
| 83 | bool silent = false;
|
---|
| 84 |
|
---|
[ac8e7a9] | 85 | /** Standard input and output character devices */
|
---|
| 86 | indev_t *stdin = NULL;
|
---|
| 87 | outdev_t *stdout = NULL;
|
---|
[411b6a6] | 88 |
|
---|
[44b7783] | 89 | indev_t *stdin_wire(void)
|
---|
| 90 | {
|
---|
| 91 | if (stdin == NULL) {
|
---|
| 92 | stdin = malloc(sizeof(indev_t), FRAME_ATOMIC);
|
---|
| 93 | if (stdin != NULL)
|
---|
| 94 | indev_initialize("stdin", stdin, &stdin_ops);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return stdin;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[82b71ef1] | 100 | /** Initialize kernel logging facility
|
---|
| 101 | *
|
---|
| 102 | * The shared area contains kernel cyclic buffer. Userspace application may
|
---|
| 103 | * be notified on new data with indication of position and size
|
---|
| 104 | * of the data within the circular buffer.
|
---|
[ac8e7a9] | 105 | *
|
---|
[82b71ef1] | 106 | */
|
---|
| 107 | void klog_init(void)
|
---|
| 108 | {
|
---|
| 109 | void *faddr = (void *) KA2PA(klog);
|
---|
| 110 |
|
---|
| 111 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
|
---|
| 112 |
|
---|
| 113 | klog_parea.pbase = (uintptr_t) faddr;
|
---|
[eec616b] | 114 | klog_parea.frames = SIZE2FRAMES(sizeof(klog));
|
---|
[82b71ef1] | 115 | ddi_parea_register(&klog_parea);
|
---|
[ac8e7a9] | 116 |
|
---|
[82b71ef1] | 117 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
|
---|
[13a638d] | 118 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
|
---|
[ac8e7a9] | 119 |
|
---|
[82b71ef1] | 120 | spinlock_lock(&klog_lock);
|
---|
| 121 | klog_inited = true;
|
---|
| 122 | spinlock_unlock(&klog_lock);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[516ff92] | 125 | void grab_console(void)
|
---|
| 126 | {
|
---|
| 127 | silent = false;
|
---|
| 128 | arch_grab_console();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void release_console(void)
|
---|
| 132 | {
|
---|
| 133 | silent = true;
|
---|
| 134 | arch_release_console();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[312cc68] | 137 | /** Tell kernel to get keyboard/console access again */
|
---|
| 138 | unative_t sys_debug_enable_console(void)
|
---|
| 139 | {
|
---|
| 140 | #ifdef CONFIG_KCONSOLE
|
---|
| 141 | grab_console();
|
---|
| 142 | return true;
|
---|
| 143 | #else
|
---|
| 144 | return false;
|
---|
| 145 | #endif
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | /** Tell kernel to relinquish keyboard/console access */
|
---|
| 149 | unative_t sys_debug_disable_console(void)
|
---|
| 150 | {
|
---|
| 151 | release_console();
|
---|
| 152 | return true;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[ac8e7a9] | 155 | /** Get string from input character device.
|
---|
[2677758] | 156 | *
|
---|
[ac8e7a9] | 157 | * Read characters from input character device until first occurrence
|
---|
[2677758] | 158 | * of newline character.
|
---|
| 159 | *
|
---|
[ac8e7a9] | 160 | * @param indev Input character device.
|
---|
[c583970] | 161 | * @param buf Buffer where to store string terminated by NULL.
|
---|
[abbc16e] | 162 | * @param buflen Size of the buffer.
|
---|
[ff3b3197] | 163 | *
|
---|
| 164 | * @return Number of characters read.
|
---|
[ac8e7a9] | 165 | *
|
---|
[2677758] | 166 | */
|
---|
[ac8e7a9] | 167 | count_t gets(indev_t *indev, char *buf, size_t buflen)
|
---|
[2677758] | 168 | {
|
---|
[c583970] | 169 | size_t offset = 0;
|
---|
| 170 | count_t count = 0;
|
---|
| 171 | buf[offset] = 0;
|
---|
[516ff92] | 172 |
|
---|
[c583970] | 173 | wchar_t ch;
|
---|
[44b7783] | 174 | while ((ch = indev_pop_character(indev)) != '\n') {
|
---|
[e8a9dc3] | 175 | if (ch == '\b') {
|
---|
[c583970] | 176 | if (count > 0) {
|
---|
| 177 | /* Space, backspace, space */
|
---|
[e8a9dc3] | 178 | putchar('\b');
|
---|
| 179 | putchar(' ');
|
---|
| 180 | putchar('\b');
|
---|
[c583970] | 181 |
|
---|
| 182 | count--;
|
---|
| 183 | offset = str_lsize(buf, count);
|
---|
| 184 | buf[offset] = 0;
|
---|
[e8a9dc3] | 185 | }
|
---|
[2677758] | 186 | }
|
---|
[c583970] | 187 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
|
---|
| 188 | putchar(ch);
|
---|
| 189 | count++;
|
---|
| 190 | buf[offset] = 0;
|
---|
| 191 | }
|
---|
[2677758] | 192 | }
|
---|
[ac8e7a9] | 193 |
|
---|
[c583970] | 194 | return count;
|
---|
[2677758] | 195 | }
|
---|
| 196 |
|
---|
[ac8e7a9] | 197 | /** Get character from input device & echo it to screen */
|
---|
[c583970] | 198 | wchar_t getc(indev_t *indev)
|
---|
[2677758] | 199 | {
|
---|
[44b7783] | 200 | wchar_t ch = indev_pop_character(indev);
|
---|
[e8a9dc3] | 201 | putchar(ch);
|
---|
[2677758] | 202 | return ch;
|
---|
| 203 | }
|
---|
[973be64e] | 204 |
|
---|
[82b71ef1] | 205 | void klog_update(void)
|
---|
| 206 | {
|
---|
| 207 | spinlock_lock(&klog_lock);
|
---|
| 208 |
|
---|
[18251cc] | 209 | if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
|
---|
[05641a9e] | 210 | event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
|
---|
| 211 | klog_uspace = 0;
|
---|
| 212 | }
|
---|
[82b71ef1] | 213 |
|
---|
| 214 | spinlock_unlock(&klog_lock);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[eec616b] | 217 | void putchar(const wchar_t ch)
|
---|
[973be64e] | 218 | {
|
---|
[82b71ef1] | 219 | spinlock_lock(&klog_lock);
|
---|
| 220 |
|
---|
[ac8e7a9] | 221 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
|
---|
[c859753] | 222 | /* Print charaters stored in kernel log */
|
---|
| 223 | index_t i;
|
---|
| 224 | for (i = klog_len - klog_stored; i < klog_len; i++)
|
---|
[171f9a1] | 225 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
|
---|
[c859753] | 226 | klog_stored = 0;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /* Store character in the cyclic kernel log */
|
---|
[171f9a1] | 230 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
|
---|
| 231 | if (klog_len < KLOG_LENGTH)
|
---|
[c859753] | 232 | klog_len++;
|
---|
| 233 | else
|
---|
[171f9a1] | 234 | klog_start = (klog_start + 1) % KLOG_LENGTH;
|
---|
[c859753] | 235 |
|
---|
[ac8e7a9] | 236 | if ((stdout) && (stdout->op->write))
|
---|
[eec616b] | 237 | stdout->op->write(stdout, ch, silent);
|
---|
[c859753] | 238 | else {
|
---|
| 239 | /* The character is just in the kernel log */
|
---|
| 240 | if (klog_stored < klog_len)
|
---|
| 241 | klog_stored++;
|
---|
| 242 | }
|
---|
[82b71ef1] | 243 |
|
---|
| 244 | /* The character is stored for uspace */
|
---|
| 245 | if (klog_uspace < klog_len)
|
---|
| 246 | klog_uspace++;
|
---|
| 247 |
|
---|
[c05a50f] | 248 | /* Check notify uspace to update */
|
---|
| 249 | bool update;
|
---|
[eec616b] | 250 | if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
|
---|
[c05a50f] | 251 | update = true;
|
---|
| 252 | else
|
---|
| 253 | update = false;
|
---|
| 254 |
|
---|
[82b71ef1] | 255 | spinlock_unlock(&klog_lock);
|
---|
| 256 |
|
---|
[c05a50f] | 257 | if (update)
|
---|
| 258 | klog_update();
|
---|
[973be64e] | 259 | }
|
---|
[b45c443] | 260 |
|
---|
[312cc68] | 261 | /** Print using kernel facility
|
---|
| 262 | *
|
---|
| 263 | * Print to kernel log.
|
---|
| 264 | *
|
---|
| 265 | */
|
---|
[c583970] | 266 | unative_t sys_klog(int fd, const void *buf, size_t size)
|
---|
[312cc68] | 267 | {
|
---|
| 268 | char *data;
|
---|
| 269 | int rc;
|
---|
[eec616b] | 270 |
|
---|
[c583970] | 271 | if (size > PAGE_SIZE)
|
---|
[312cc68] | 272 | return ELIMIT;
|
---|
| 273 |
|
---|
[c583970] | 274 | if (size > 0) {
|
---|
| 275 | data = (char *) malloc(size + 1, 0);
|
---|
[312cc68] | 276 | if (!data)
|
---|
| 277 | return ENOMEM;
|
---|
| 278 |
|
---|
[c583970] | 279 | rc = copy_from_uspace(data, buf, size);
|
---|
[312cc68] | 280 | if (rc) {
|
---|
| 281 | free(data);
|
---|
| 282 | return rc;
|
---|
| 283 | }
|
---|
[c583970] | 284 | data[size] = 0;
|
---|
[312cc68] | 285 |
|
---|
| 286 | printf("%s", data);
|
---|
| 287 | free(data);
|
---|
| 288 | } else
|
---|
| 289 | klog_update();
|
---|
| 290 |
|
---|
[c583970] | 291 | return size;
|
---|
[312cc68] | 292 | }
|
---|
| 293 |
|
---|
[06e1e95] | 294 | /** @}
|
---|
[b45c443] | 295 | */
|
---|