[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/device.h>
|
---|
| 43 | #include <ddi/irq.h>
|
---|
| 44 | #include <ddi/ddi.h>
|
---|
| 45 | #include <ipc/irq.h>
|
---|
[2677758] | 46 | #include <arch.h>
|
---|
[93b84b3] | 47 | #include <func.h>
|
---|
| 48 | #include <print.h>
|
---|
[23684b7] | 49 | #include <atomic.h>
|
---|
[2677758] | 50 |
|
---|
[82b71ef1] | 51 | #define KLOG_SIZE PAGE_SIZE
|
---|
[c859753] | 52 |
|
---|
| 53 | /**< Kernel log cyclic buffer */
|
---|
[82b71ef1] | 54 | static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE)));
|
---|
[c859753] | 55 |
|
---|
[82b71ef1] | 56 | /**< Kernel log initialized */
|
---|
| 57 | static bool klog_inited = false;
|
---|
[c859753] | 58 | /**< First kernel log characters */
|
---|
| 59 | static index_t klog_start = 0;
|
---|
| 60 | /**< Number of valid kernel log characters */
|
---|
| 61 | static size_t klog_len = 0;
|
---|
| 62 | /**< Number of stored (not printed) kernel log characters */
|
---|
| 63 | static size_t klog_stored = 0;
|
---|
[82b71ef1] | 64 | /**< Number of stored kernel log characters for uspace */
|
---|
| 65 | static size_t klog_uspace = 0;
|
---|
| 66 |
|
---|
| 67 | /**< Kernel log spinlock */
|
---|
| 68 | SPINLOCK_INITIALIZE(klog_lock);
|
---|
| 69 |
|
---|
| 70 | /** Physical memory area used for klog buffer */
|
---|
| 71 | static parea_t klog_parea;
|
---|
| 72 |
|
---|
| 73 | /*
|
---|
| 74 | * For now, we use 0 as INR.
|
---|
| 75 | * However, it is therefore desirable to have architecture specific
|
---|
| 76 | * definition of KLOG_VIRT_INR in the future.
|
---|
| 77 | */
|
---|
| 78 | #define KLOG_VIRT_INR 0
|
---|
| 79 |
|
---|
| 80 | static irq_t klog_irq;
|
---|
[dabe6333] | 81 |
|
---|
| 82 | static chardev_operations_t null_stdout_ops = {
|
---|
[c859753] | 83 | .suspend = NULL,
|
---|
| 84 | .resume = NULL,
|
---|
| 85 | .write = NULL,
|
---|
| 86 | .read = NULL
|
---|
[dabe6333] | 87 | };
|
---|
[4cc2ddd] | 88 |
|
---|
[dabe6333] | 89 | chardev_t null_stdout = {
|
---|
| 90 | .name = "null",
|
---|
| 91 | .op = &null_stdout_ops
|
---|
| 92 | };
|
---|
| 93 |
|
---|
[82b71ef1] | 94 | /** Allways refuse IRQ ownership.
|
---|
| 95 | *
|
---|
| 96 | * This is not a real IRQ, so we always decline.
|
---|
| 97 | *
|
---|
| 98 | * @return Always returns IRQ_DECLINE.
|
---|
| 99 | */
|
---|
| 100 | static irq_ownership_t klog_claim(void)
|
---|
| 101 | {
|
---|
| 102 | return IRQ_DECLINE;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Standard input character device */
|
---|
[2677758] | 106 | chardev_t *stdin = NULL;
|
---|
[dabe6333] | 107 | chardev_t *stdout = &null_stdout;
|
---|
[2677758] | 108 |
|
---|
[82b71ef1] | 109 | /** Initialize kernel logging facility
|
---|
| 110 | *
|
---|
| 111 | * The shared area contains kernel cyclic buffer. Userspace application may
|
---|
| 112 | * be notified on new data with indication of position and size
|
---|
| 113 | * of the data within the circular buffer.
|
---|
| 114 | */
|
---|
| 115 | void klog_init(void)
|
---|
| 116 | {
|
---|
| 117 | void *faddr = (void *) KA2PA(klog);
|
---|
| 118 |
|
---|
| 119 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
|
---|
| 120 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0);
|
---|
| 121 |
|
---|
| 122 | devno_t devno = device_assign_devno();
|
---|
| 123 |
|
---|
| 124 | klog_parea.pbase = (uintptr_t) faddr;
|
---|
| 125 | klog_parea.vbase = (uintptr_t) klog;
|
---|
| 126 | klog_parea.frames = SIZE2FRAMES(KLOG_SIZE);
|
---|
| 127 | klog_parea.cacheable = true;
|
---|
| 128 | ddi_parea_register(&klog_parea);
|
---|
| 129 |
|
---|
| 130 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
|
---|
| 131 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE));
|
---|
| 132 | sysinfo_set_item_val("klog.devno", NULL, devno);
|
---|
| 133 | sysinfo_set_item_val("klog.inr", NULL, KLOG_VIRT_INR);
|
---|
| 134 |
|
---|
| 135 | irq_initialize(&klog_irq);
|
---|
| 136 | klog_irq.devno = devno;
|
---|
| 137 | klog_irq.inr = KLOG_VIRT_INR;
|
---|
| 138 | klog_irq.claim = klog_claim;
|
---|
| 139 | irq_register(&klog_irq);
|
---|
| 140 |
|
---|
| 141 | spinlock_lock(&klog_lock);
|
---|
| 142 | klog_inited = true;
|
---|
| 143 | spinlock_unlock(&klog_lock);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[0c8e692] | 146 | /** Get character from character device. Do not echo character.
|
---|
[e8a9dc3] | 147 | *
|
---|
| 148 | * @param chardev Character device.
|
---|
| 149 | *
|
---|
| 150 | * @return Character read.
|
---|
| 151 | */
|
---|
[7f1c620] | 152 | uint8_t _getc(chardev_t *chardev)
|
---|
[e8a9dc3] | 153 | {
|
---|
[7f1c620] | 154 | uint8_t ch;
|
---|
[e8a9dc3] | 155 | ipl_t ipl;
|
---|
| 156 |
|
---|
[36e7ee98] | 157 | if (atomic_get(&haltstate)) {
|
---|
[93b84b3] | 158 | /* If we are here, we are hopefully on the processor, that
|
---|
| 159 | * issued the 'halt' command, so proceed to read the character
|
---|
| 160 | * directly from input
|
---|
| 161 | */
|
---|
| 162 | if (chardev->op->read)
|
---|
| 163 | return chardev->op->read(chardev);
|
---|
| 164 | /* no other way of interacting with user, halt */
|
---|
[36e7ee98] | 165 | if (CPU)
|
---|
[c859753] | 166 | printf("cpu%u: ", CPU->id);
|
---|
[36e7ee98] | 167 | else
|
---|
| 168 | printf("cpu: ");
|
---|
| 169 | printf("halted - no kconsole\n");
|
---|
[93b84b3] | 170 | cpu_halt();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[e8a9dc3] | 173 | waitq_sleep(&chardev->wq);
|
---|
| 174 | ipl = interrupts_disable();
|
---|
| 175 | spinlock_lock(&chardev->lock);
|
---|
| 176 | ch = chardev->buffer[(chardev->index - chardev->counter) % CHARDEV_BUFLEN];
|
---|
| 177 | chardev->counter--;
|
---|
| 178 | spinlock_unlock(&chardev->lock);
|
---|
| 179 | interrupts_restore(ipl);
|
---|
| 180 |
|
---|
| 181 | chardev->op->resume(chardev);
|
---|
| 182 |
|
---|
| 183 | return ch;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[2677758] | 186 | /** Get string from character device.
|
---|
| 187 | *
|
---|
| 188 | * Read characters from character device until first occurrence
|
---|
| 189 | * of newline character.
|
---|
| 190 | *
|
---|
| 191 | * @param chardev Character device.
|
---|
[ff3b3197] | 192 | * @param buf Buffer where to store string terminated by '\0'.
|
---|
[abbc16e] | 193 | * @param buflen Size of the buffer.
|
---|
[ff3b3197] | 194 | *
|
---|
| 195 | * @return Number of characters read.
|
---|
[2677758] | 196 | */
|
---|
[ff3b3197] | 197 | count_t gets(chardev_t *chardev, char *buf, size_t buflen)
|
---|
[2677758] | 198 | {
|
---|
| 199 | index_t index = 0;
|
---|
| 200 | char ch;
|
---|
| 201 |
|
---|
| 202 | while (index < buflen) {
|
---|
[e8a9dc3] | 203 | ch = _getc(chardev);
|
---|
| 204 | if (ch == '\b') {
|
---|
| 205 | if (index > 0) {
|
---|
| 206 | index--;
|
---|
| 207 | /* Space backspace, space */
|
---|
| 208 | putchar('\b');
|
---|
| 209 | putchar(' ');
|
---|
| 210 | putchar('\b');
|
---|
| 211 | }
|
---|
| 212 | continue;
|
---|
| 213 | }
|
---|
| 214 | putchar(ch);
|
---|
| 215 |
|
---|
[2677758] | 216 | if (ch == '\n') { /* end of string => write 0, return */
|
---|
[ff3b3197] | 217 | buf[index] = '\0';
|
---|
| 218 | return (count_t) index;
|
---|
[2677758] | 219 | }
|
---|
[ff3b3197] | 220 | buf[index++] = ch;
|
---|
[2677758] | 221 | }
|
---|
[ff3b3197] | 222 | return (count_t) index;
|
---|
[2677758] | 223 | }
|
---|
| 224 |
|
---|
[e8a9dc3] | 225 | /** Get character from device & echo it to screen */
|
---|
[7f1c620] | 226 | uint8_t getc(chardev_t *chardev)
|
---|
[2677758] | 227 | {
|
---|
[7f1c620] | 228 | uint8_t ch;
|
---|
[2677758] | 229 |
|
---|
[e8a9dc3] | 230 | ch = _getc(chardev);
|
---|
| 231 | putchar(ch);
|
---|
[2677758] | 232 | return ch;
|
---|
| 233 | }
|
---|
[973be64e] | 234 |
|
---|
[82b71ef1] | 235 | void klog_update(void)
|
---|
| 236 | {
|
---|
| 237 | spinlock_lock(&klog_lock);
|
---|
| 238 |
|
---|
| 239 | if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) {
|
---|
| 240 | ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace);
|
---|
| 241 | klog_uspace = 0;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | spinlock_unlock(&klog_lock);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[973be64e] | 247 | void putchar(char c)
|
---|
| 248 | {
|
---|
[82b71ef1] | 249 | spinlock_lock(&klog_lock);
|
---|
| 250 |
|
---|
[c859753] | 251 | if ((klog_stored > 0) && (stdout->op->write)) {
|
---|
| 252 | /* Print charaters stored in kernel log */
|
---|
| 253 | index_t i;
|
---|
| 254 | for (i = klog_len - klog_stored; i < klog_len; i++)
|
---|
| 255 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE]);
|
---|
| 256 | klog_stored = 0;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /* Store character in the cyclic kernel log */
|
---|
| 260 | klog[(klog_start + klog_len) % KLOG_SIZE] = c;
|
---|
| 261 | if (klog_len < KLOG_SIZE)
|
---|
| 262 | klog_len++;
|
---|
| 263 | else
|
---|
| 264 | klog_start = (klog_start + 1) % KLOG_SIZE;
|
---|
| 265 |
|
---|
[93b84b3] | 266 | if (stdout->op->write)
|
---|
| 267 | stdout->op->write(stdout, c);
|
---|
[c859753] | 268 | else {
|
---|
| 269 | /* The character is just in the kernel log */
|
---|
| 270 | if (klog_stored < klog_len)
|
---|
| 271 | klog_stored++;
|
---|
| 272 | }
|
---|
[82b71ef1] | 273 |
|
---|
| 274 | /* The character is stored for uspace */
|
---|
| 275 | if (klog_uspace < klog_len)
|
---|
| 276 | klog_uspace++;
|
---|
| 277 |
|
---|
| 278 | spinlock_unlock(&klog_lock);
|
---|
| 279 |
|
---|
| 280 | klog_update();
|
---|
[973be64e] | 281 | }
|
---|
[b45c443] | 282 |
|
---|
[06e1e95] | 283 | /** @}
|
---|
[b45c443] | 284 | */
|
---|