Rev 2787 | Rev 4377 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2787 | Rev 3424 | ||
|---|---|---|---|
| Line 33... | Line 33... | ||
| 33 | /** @file |
33 | /** @file |
| 34 | */ |
34 | */ |
| 35 | 35 | ||
| 36 | #include <console/console.h> |
36 | #include <console/console.h> |
| 37 | #include <console/chardev.h> |
37 | #include <console/chardev.h> |
| - | 38 | #include <sysinfo/sysinfo.h> |
|
| 38 | #include <synch/waitq.h> |
39 | #include <synch/waitq.h> |
| 39 | #include <synch/spinlock.h> |
40 | #include <synch/spinlock.h> |
| 40 | #include <arch/types.h> |
41 | #include <arch/types.h> |
| - | 42 | #include <ddi/device.h> |
|
| - | 43 | #include <ddi/irq.h> |
|
| - | 44 | #include <ddi/ddi.h> |
|
| - | 45 | #include <ipc/irq.h> |
|
| 41 | #include <arch.h> |
46 | #include <arch.h> |
| 42 | #include <func.h> |
47 | #include <func.h> |
| 43 | #include <print.h> |
48 | #include <print.h> |
| 44 | #include <atomic.h> |
49 | #include <atomic.h> |
| 45 | 50 | ||
| - | 51 | #define KLOG_SIZE PAGE_SIZE |
|
| 46 | #define BUFLEN 2048 |
52 | #define KLOG_LATENCY 8 |
| - | 53 | ||
| - | 54 | /**< Kernel log cyclic buffer */ |
|
| - | 55 | static char klog[KLOG_SIZE] __attribute__ ((aligned (PAGE_SIZE))); |
|
| - | 56 | ||
| - | 57 | /**< Kernel log initialized */ |
|
| 47 | static char debug_buffer[BUFLEN]; |
58 | static bool klog_inited = false; |
| - | 59 | /**< First kernel log characters */ |
|
| - | 60 | static index_t klog_start = 0; |
|
| - | 61 | /**< Number of valid kernel log characters */ |
|
| 48 | static size_t offset = 0; |
62 | static size_t klog_len = 0; |
| - | 63 | /**< Number of stored (not printed) kernel log characters */ |
|
| - | 64 | static size_t klog_stored = 0; |
|
| 49 | /** Initialize stdout to something that does not print, but does not fail |
65 | /**< Number of stored kernel log characters for uspace */ |
| - | 66 | static size_t klog_uspace = 0; |
|
| 50 | * |
67 | |
| 51 | * Save data in some buffer so that it could be retrieved in the debugger |
68 | /**< Kernel log spinlock */ |
| - | 69 | SPINLOCK_INITIALIZE(klog_lock); |
|
| 52 | */ |
70 | |
| 53 | static void null_putchar(chardev_t *d, const char ch) |
71 | /** Physical memory area used for klog buffer */ |
| - | 72 | static parea_t klog_parea; |
|
| 54 | { |
73 | |
| - | 74 | /* |
|
| 55 | if (offset >= BUFLEN) |
75 | * For now, we use 0 as INR. |
| - | 76 | * However, it is therefore desirable to have architecture specific |
|
| - | 77 | * definition of KLOG_VIRT_INR in the future. |
|
| 56 | offset = 0; |
78 | */ |
| 57 | debug_buffer[offset++] = ch; |
79 | #define KLOG_VIRT_INR 0 |
| 58 | } |
80 | |
| - | 81 | static irq_t klog_irq; |
|
| 59 | 82 | ||
| 60 | static chardev_operations_t null_stdout_ops = { |
83 | static chardev_operations_t null_stdout_ops = { |
| - | 84 | .suspend = NULL, |
|
| - | 85 | .resume = NULL, |
|
| 61 | .write = null_putchar |
86 | .write = NULL, |
| - | 87 | .read = NULL |
|
| 62 | }; |
88 | }; |
| 63 | 89 | ||
| 64 | chardev_t null_stdout = { |
90 | chardev_t null_stdout = { |
| 65 | .name = "null", |
91 | .name = "null", |
| 66 | .op = &null_stdout_ops |
92 | .op = &null_stdout_ops |
| 67 | }; |
93 | }; |
| 68 | 94 | ||
| - | 95 | /** Allways refuse IRQ ownership. |
|
| - | 96 | * |
|
| - | 97 | * This is not a real IRQ, so we always decline. |
|
| - | 98 | * |
|
| - | 99 | * @return Always returns IRQ_DECLINE. |
|
| - | 100 | */ |
|
| - | 101 | static irq_ownership_t klog_claim(void) |
|
| - | 102 | { |
|
| - | 103 | return IRQ_DECLINE; |
|
| - | 104 | } |
|
| - | 105 | ||
| 69 | /** Standard input character device. */ |
106 | /** Standard input character device */ |
| 70 | chardev_t *stdin = NULL; |
107 | chardev_t *stdin = NULL; |
| 71 | chardev_t *stdout = &null_stdout; |
108 | chardev_t *stdout = &null_stdout; |
| 72 | 109 | ||
| - | 110 | /** Initialize kernel logging facility |
|
| - | 111 | * |
|
| - | 112 | * The shared area contains kernel cyclic buffer. Userspace application may |
|
| - | 113 | * be notified on new data with indication of position and size |
|
| - | 114 | * of the data within the circular buffer. |
|
| - | 115 | */ |
|
| - | 116 | void klog_init(void) |
|
| - | 117 | { |
|
| - | 118 | void *faddr = (void *) KA2PA(klog); |
|
| - | 119 | ||
| - | 120 | ASSERT((uintptr_t) faddr % FRAME_SIZE == 0); |
|
| - | 121 | ASSERT(KLOG_SIZE % FRAME_SIZE == 0); |
|
| - | 122 | ||
| - | 123 | devno_t devno = device_assign_devno(); |
|
| - | 124 | ||
| - | 125 | klog_parea.pbase = (uintptr_t) faddr; |
|
| - | 126 | klog_parea.vbase = (uintptr_t) klog; |
|
| - | 127 | klog_parea.frames = SIZE2FRAMES(KLOG_SIZE); |
|
| - | 128 | klog_parea.cacheable = true; |
|
| - | 129 | ddi_parea_register(&klog_parea); |
|
| - | 130 | ||
| - | 131 | sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr); |
|
| - | 132 | sysinfo_set_item_val("klog.pages", NULL, SIZE2FRAMES(KLOG_SIZE)); |
|
| - | 133 | sysinfo_set_item_val("klog.devno", NULL, devno); |
|
| - | 134 | sysinfo_set_item_val("klog.inr", NULL, KLOG_VIRT_INR); |
|
| - | 135 | ||
| - | 136 | irq_initialize(&klog_irq); |
|
| - | 137 | klog_irq.devno = devno; |
|
| - | 138 | klog_irq.inr = KLOG_VIRT_INR; |
|
| - | 139 | klog_irq.claim = klog_claim; |
|
| - | 140 | irq_register(&klog_irq); |
|
| - | 141 | ||
| - | 142 | spinlock_lock(&klog_lock); |
|
| - | 143 | klog_inited = true; |
|
| - | 144 | spinlock_unlock(&klog_lock); |
|
| - | 145 | } |
|
| - | 146 | ||
| 73 | /** Get character from character device. Do not echo character. |
147 | /** Get character from character device. Do not echo character. |
| 74 | * |
148 | * |
| 75 | * @param chardev Character device. |
149 | * @param chardev Character device. |
| 76 | * |
150 | * |
| 77 | * @return Character read. |
151 | * @return Character read. |
| Line 88... | Line 162... | ||
| 88 | */ |
162 | */ |
| 89 | if (chardev->op->read) |
163 | if (chardev->op->read) |
| 90 | return chardev->op->read(chardev); |
164 | return chardev->op->read(chardev); |
| 91 | /* no other way of interacting with user, halt */ |
165 | /* no other way of interacting with user, halt */ |
| 92 | if (CPU) |
166 | if (CPU) |
| 93 | printf("cpu%d: ", CPU->id); |
167 | printf("cpu%u: ", CPU->id); |
| 94 | else |
168 | else |
| 95 | printf("cpu: "); |
169 | printf("cpu: "); |
| 96 | printf("halted - no kconsole\n"); |
170 | printf("halted - no kconsole\n"); |
| 97 | cpu_halt(); |
171 | cpu_halt(); |
| 98 | } |
172 | } |
| Line 157... | Line 231... | ||
| 157 | ch = _getc(chardev); |
231 | ch = _getc(chardev); |
| 158 | putchar(ch); |
232 | putchar(ch); |
| 159 | return ch; |
233 | return ch; |
| 160 | } |
234 | } |
| 161 | 235 | ||
| - | 236 | void klog_update(void) |
|
| - | 237 | { |
|
| - | 238 | spinlock_lock(&klog_lock); |
|
| - | 239 | ||
| - | 240 | if ((klog_inited) && (klog_irq.notif_cfg.notify) && (klog_uspace > 0)) { |
|
| - | 241 | ipc_irq_send_msg_3(&klog_irq, klog_start, klog_len, klog_uspace); |
|
| - | 242 | klog_uspace = 0; |
|
| - | 243 | } |
|
| - | 244 | ||
| - | 245 | spinlock_unlock(&klog_lock); |
|
| - | 246 | } |
|
| - | 247 | ||
| 162 | void putchar(char c) |
248 | void putchar(char c) |
| 163 | { |
249 | { |
| - | 250 | spinlock_lock(&klog_lock); |
|
| - | 251 | ||
| - | 252 | if ((klog_stored > 0) && (stdout->op->write)) { |
|
| - | 253 | /* Print charaters stored in kernel log */ |
|
| - | 254 | index_t i; |
|
| - | 255 | for (i = klog_len - klog_stored; i < klog_len; i++) |
|
| - | 256 | stdout->op->write(stdout, klog[(klog_start + i) % KLOG_SIZE]); |
|
| - | 257 | klog_stored = 0; |
|
| - | 258 | } |
|
| - | 259 | ||
| - | 260 | /* Store character in the cyclic kernel log */ |
|
| - | 261 | klog[(klog_start + klog_len) % KLOG_SIZE] = c; |
|
| - | 262 | if (klog_len < KLOG_SIZE) |
|
| - | 263 | klog_len++; |
|
| - | 264 | else |
|
| - | 265 | klog_start = (klog_start + 1) % KLOG_SIZE; |
|
| - | 266 | ||
| 164 | if (stdout->op->write) |
267 | if (stdout->op->write) |
| 165 | stdout->op->write(stdout, c); |
268 | stdout->op->write(stdout, c); |
| - | 269 | else { |
|
| - | 270 | /* The character is just in the kernel log */ |
|
| - | 271 | if (klog_stored < klog_len) |
|
| - | 272 | klog_stored++; |
|
| - | 273 | } |
|
| - | 274 | ||
| - | 275 | /* The character is stored for uspace */ |
|
| - | 276 | if (klog_uspace < klog_len) |
|
| - | 277 | klog_uspace++; |
|
| - | 278 | ||
| - | 279 | /* Check notify uspace to update */ |
|
| - | 280 | bool update; |
|
| - | 281 | if ((klog_uspace > KLOG_LATENCY) || (c == '\n')) |
|
| - | 282 | update = true; |
|
| - | 283 | else |
|
| - | 284 | update = false; |
|
| - | 285 | ||
| - | 286 | spinlock_unlock(&klog_lock); |
|
| - | 287 | ||
| - | 288 | if (update) |
|
| - | 289 | klog_update(); |
|
| 166 | } |
290 | } |
| 167 | 291 | ||
| 168 | /** @} |
292 | /** @} |
| 169 | */ |
293 | */ |