Rev 4310 | Rev 4323 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 510 | jermar | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2003 Josef Cejka |
| 3 | * Copyright (c) 2005 Jakub Jermar |
||
| 510 | 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 | |||
| 1888 | jermar | 30 | /** @addtogroup genericconsole |
| 1702 | cejka | 31 | * @{ |
| 32 | */ |
||
| 33 | /** @file |
||
| 34 | */ |
||
| 35 | |||
| 510 | jermar | 36 | #include <console/console.h> |
| 37 | #include <console/chardev.h> |
||
| 3097 | decky | 38 | #include <sysinfo/sysinfo.h> |
| 510 | jermar | 39 | #include <synch/waitq.h> |
| 40 | #include <synch/spinlock.h> |
||
| 41 | #include <arch/types.h> |
||
| 3097 | decky | 42 | #include <ddi/irq.h> |
| 43 | #include <ddi/ddi.h> |
||
| 4252 | decky | 44 | #include <ipc/event.h> |
| 3097 | decky | 45 | #include <ipc/irq.h> |
| 510 | jermar | 46 | #include <arch.h> |
| 607 | palkovsky | 47 | #include <print.h> |
| 4180 | decky | 48 | #include <putchar.h> |
| 1104 | jermar | 49 | #include <atomic.h> |
| 4146 | decky | 50 | #include <syscall/copy.h> |
| 51 | #include <errno.h> |
||
| 4218 | decky | 52 | #include <string.h> |
| 510 | jermar | 53 | |
| 4252 | decky | 54 | #define KLOG_PAGES 4 |
| 55 | #define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t)) |
||
| 4180 | decky | 56 | #define KLOG_LATENCY 8 |
| 1050 | palkovsky | 57 | |
| 3965 | svoboda | 58 | /** Kernel log cyclic buffer */ |
| 4226 | svoboda | 59 | static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE))); |
| 3065 | decky | 60 | |
| 3965 | svoboda | 61 | /** Kernel log initialized */ |
| 3097 | decky | 62 | static bool klog_inited = false; |
| 3965 | svoboda | 63 | /** First kernel log characters */ |
| 3065 | decky | 64 | static index_t klog_start = 0; |
| 3965 | svoboda | 65 | /** Number of valid kernel log characters */ |
| 3065 | decky | 66 | static size_t klog_len = 0; |
| 3965 | svoboda | 67 | /** Number of stored (not printed) kernel log characters */ |
| 3065 | decky | 68 | static size_t klog_stored = 0; |
| 3965 | svoboda | 69 | /** Number of stored kernel log characters for uspace */ |
| 3097 | decky | 70 | static size_t klog_uspace = 0; |
| 3065 | decky | 71 | |
| 3965 | svoboda | 72 | /** Kernel log spinlock */ |
| 3097 | decky | 73 | SPINLOCK_INITIALIZE(klog_lock); |
| 74 | |||
| 75 | /** Physical memory area used for klog buffer */ |
||
| 76 | static parea_t klog_parea; |
||
| 3844 | decky | 77 | |
| 4310 | decky | 78 | static indev_operations_t stdin_ops = { |
| 79 | .poll = NULL |
||
| 80 | }; |
||
| 81 | |||
| 82 | /** Silence output */ |
||
| 83 | bool silent = false; |
||
| 84 | |||
| 4087 | decky | 85 | /** Standard input and output character devices */ |
| 86 | indev_t *stdin = NULL; |
||
| 87 | outdev_t *stdout = NULL; |
||
| 3097 | decky | 88 | |
| 4310 | decky | 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 | |||
| 3097 | decky | 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. |
||
| 4087 | decky | 105 | * |
| 3097 | decky | 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; |
||
| 4180 | decky | 114 | klog_parea.frames = SIZE2FRAMES(sizeof(klog)); |
| 3097 | decky | 115 | ddi_parea_register(&klog_parea); |
| 4087 | decky | 116 | |
| 3097 | decky | 117 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
| 4252 | decky | 118 | sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES); |
| 3097 | decky | 119 | |
| 120 | spinlock_lock(&klog_lock); |
||
| 121 | klog_inited = true; |
||
| 122 | spinlock_unlock(&klog_lock); |
||
| 123 | } |
||
| 124 | |||
| 3844 | decky | 125 | void grab_console(void) |
| 126 | { |
||
| 127 | silent = false; |
||
| 128 | arch_grab_console(); |
||
| 4318 | decky | 129 | |
| 130 | /* Force the console to print the prompt */ |
||
| 131 | if (stdin) |
||
| 132 | indev_push_character(stdin, '\n'); |
||
| 3844 | decky | 133 | } |
| 134 | |||
| 135 | void release_console(void) |
||
| 136 | { |
||
| 137 | silent = true; |
||
| 138 | arch_release_console(); |
||
| 139 | } |
||
| 140 | |||
| 4146 | decky | 141 | /** Tell kernel to get keyboard/console access again */ |
| 142 | unative_t sys_debug_enable_console(void) |
||
| 143 | { |
||
| 144 | #ifdef CONFIG_KCONSOLE |
||
| 145 | grab_console(); |
||
| 146 | return true; |
||
| 147 | #else |
||
| 148 | return false; |
||
| 149 | #endif |
||
| 150 | } |
||
| 151 | |||
| 152 | /** Tell kernel to relinquish keyboard/console access */ |
||
| 153 | unative_t sys_debug_disable_console(void) |
||
| 154 | { |
||
| 155 | release_console(); |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 4087 | decky | 159 | /** Get string from input character device. |
| 510 | jermar | 160 | * |
| 4087 | decky | 161 | * Read characters from input character device until first occurrence |
| 510 | jermar | 162 | * of newline character. |
| 163 | * |
||
| 4087 | decky | 164 | * @param indev Input character device. |
| 4218 | decky | 165 | * @param buf Buffer where to store string terminated by NULL. |
| 1708 | jermar | 166 | * @param buflen Size of the buffer. |
| 517 | jermar | 167 | * |
| 168 | * @return Number of characters read. |
||
| 4087 | decky | 169 | * |
| 510 | jermar | 170 | */ |
| 4087 | decky | 171 | count_t gets(indev_t *indev, char *buf, size_t buflen) |
| 510 | jermar | 172 | { |
| 4218 | decky | 173 | size_t offset = 0; |
| 174 | count_t count = 0; |
||
| 175 | buf[offset] = 0; |
||
| 3844 | decky | 176 | |
| 4218 | decky | 177 | wchar_t ch; |
| 4310 | decky | 178 | while ((ch = indev_pop_character(indev)) != '\n') { |
| 588 | palkovsky | 179 | if (ch == '\b') { |
| 4218 | decky | 180 | if (count > 0) { |
| 181 | /* Space, backspace, space */ |
||
| 588 | palkovsky | 182 | putchar('\b'); |
| 183 | putchar(' '); |
||
| 184 | putchar('\b'); |
||
| 4218 | decky | 185 | |
| 186 | count--; |
||
| 187 | offset = str_lsize(buf, count); |
||
| 188 | buf[offset] = 0; |
||
| 588 | palkovsky | 189 | } |
| 510 | jermar | 190 | } |
| 4218 | decky | 191 | if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) { |
| 192 | putchar(ch); |
||
| 193 | count++; |
||
| 194 | buf[offset] = 0; |
||
| 195 | } |
||
| 510 | jermar | 196 | } |
| 4087 | decky | 197 | |
| 4218 | decky | 198 | return count; |
| 510 | jermar | 199 | } |
| 200 | |||
| 4087 | decky | 201 | /** Get character from input device & echo it to screen */ |
| 4218 | decky | 202 | wchar_t getc(indev_t *indev) |
| 510 | jermar | 203 | { |
| 4310 | decky | 204 | wchar_t ch = indev_pop_character(indev); |
| 588 | palkovsky | 205 | putchar(ch); |
| 510 | jermar | 206 | return ch; |
| 207 | } |
||
| 575 | palkovsky | 208 | |
| 3097 | decky | 209 | void klog_update(void) |
| 210 | { |
||
| 211 | spinlock_lock(&klog_lock); |
||
| 212 | |||
| 4194 | decky | 213 | if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) { |
| 4173 | jermar | 214 | event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace); |
| 215 | klog_uspace = 0; |
||
| 216 | } |
||
| 3097 | decky | 217 | |
| 218 | spinlock_unlock(&klog_lock); |
||
| 219 | } |
||
| 220 | |||
| 4180 | decky | 221 | void putchar(const wchar_t ch) |
| 575 | palkovsky | 222 | { |
| 3097 | decky | 223 | spinlock_lock(&klog_lock); |
| 224 | |||
| 4087 | decky | 225 | if ((klog_stored > 0) && (stdout) && (stdout->op->write)) { |
| 3065 | decky | 226 | /* Print charaters stored in kernel log */ |
| 227 | index_t i; |
||
| 228 | for (i = klog_len - klog_stored; i < klog_len; i++) |
||
| 4226 | svoboda | 229 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent); |
| 3065 | decky | 230 | klog_stored = 0; |
| 231 | } |
||
| 232 | |||
| 233 | /* Store character in the cyclic kernel log */ |
||
| 4226 | svoboda | 234 | klog[(klog_start + klog_len) % KLOG_LENGTH] = ch; |
| 235 | if (klog_len < KLOG_LENGTH) |
||
| 3065 | decky | 236 | klog_len++; |
| 237 | else |
||
| 4226 | svoboda | 238 | klog_start = (klog_start + 1) % KLOG_LENGTH; |
| 3065 | decky | 239 | |
| 4087 | decky | 240 | if ((stdout) && (stdout->op->write)) |
| 4180 | decky | 241 | stdout->op->write(stdout, ch, silent); |
| 3065 | decky | 242 | else { |
| 243 | /* The character is just in the kernel log */ |
||
| 244 | if (klog_stored < klog_len) |
||
| 245 | klog_stored++; |
||
| 246 | } |
||
| 3097 | decky | 247 | |
| 248 | /* The character is stored for uspace */ |
||
| 249 | if (klog_uspace < klog_len) |
||
| 250 | klog_uspace++; |
||
| 251 | |||
| 3113 | decky | 252 | /* Check notify uspace to update */ |
| 253 | bool update; |
||
| 4180 | decky | 254 | if ((klog_uspace > KLOG_LATENCY) || (ch == '\n')) |
| 3113 | decky | 255 | update = true; |
| 256 | else |
||
| 257 | update = false; |
||
| 258 | |||
| 3097 | decky | 259 | spinlock_unlock(&klog_lock); |
| 260 | |||
| 3113 | decky | 261 | if (update) |
| 262 | klog_update(); |
||
| 575 | palkovsky | 263 | } |
| 1702 | cejka | 264 | |
| 4146 | decky | 265 | /** Print using kernel facility |
| 266 | * |
||
| 267 | * Print to kernel log. |
||
| 268 | * |
||
| 269 | */ |
||
| 4218 | decky | 270 | unative_t sys_klog(int fd, const void *buf, size_t size) |
| 4146 | decky | 271 | { |
| 272 | char *data; |
||
| 273 | int rc; |
||
| 4180 | decky | 274 | |
| 4218 | decky | 275 | if (size > PAGE_SIZE) |
| 4146 | decky | 276 | return ELIMIT; |
| 277 | |||
| 4218 | decky | 278 | if (size > 0) { |
| 279 | data = (char *) malloc(size + 1, 0); |
||
| 4146 | decky | 280 | if (!data) |
| 281 | return ENOMEM; |
||
| 282 | |||
| 4218 | decky | 283 | rc = copy_from_uspace(data, buf, size); |
| 4146 | decky | 284 | if (rc) { |
| 285 | free(data); |
||
| 286 | return rc; |
||
| 287 | } |
||
| 4218 | decky | 288 | data[size] = 0; |
| 4146 | decky | 289 | |
| 290 | printf("%s", data); |
||
| 291 | free(data); |
||
| 292 | } else |
||
| 293 | klog_update(); |
||
| 294 | |||
| 4218 | decky | 295 | return size; |
| 4146 | decky | 296 | } |
| 297 | |||
| 1888 | jermar | 298 | /** @} |
| 1702 | cejka | 299 | */ |