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