source: mainline/kernel/generic/src/console/console.c@ e3a050c7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e3a050c7 was e3a050c7, checked in by Martin Decky <martin@…>, 13 years ago

make the kernel log a global symbol (so it can be easily found in crash dumps, etc.)

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[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>
[d99c1d2]41#include <typedefs.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>
[b8da2a3]47#include <panic.h>
[93b84b3]48#include <print.h>
[eec616b]49#include <putchar.h>
[23684b7]50#include <atomic.h>
[312cc68]51#include <syscall/copy.h>
52#include <errno.h>
[19f857a]53#include <str.h>
[2677758]54
[bec0219]55#define KLOG_PAGES 8
[13a638d]56#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
[c859753]57
[c0855a0]58/** Kernel log cyclic buffer */
[e3a050c7]59wchar_t klog[KLOG_LENGTH] __attribute__((aligned(PAGE_SIZE)));
[c859753]60
[c0855a0]61/** Kernel log initialized */
[712c4ba]62static atomic_t klog_inited = {false};
[da1bafb]63
[c0855a0]64/** First kernel log characters */
[98000fb]65static size_t klog_start = 0;
[da1bafb]66
[c0855a0]67/** Number of valid kernel log characters */
[c859753]68static size_t klog_len = 0;
[da1bafb]69
[c0855a0]70/** Number of stored (not printed) kernel log characters */
[c859753]71static size_t klog_stored = 0;
[da1bafb]72
[c0855a0]73/** Number of stored kernel log characters for uspace */
[82b71ef1]74static size_t klog_uspace = 0;
75
[c0855a0]76/** Kernel log spinlock */
[712c4ba]77SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "klog_lock");
[82b71ef1]78
79/** Physical memory area used for klog buffer */
80static parea_t klog_parea;
[516ff92]81
[b9c7425]82static indev_t stdin_sink;
83static outdev_t stdout_source;
84
[44b7783]85static indev_operations_t stdin_ops = {
86 .poll = NULL
87};
88
[b366a6f4]89static void stdout_write(outdev_t *, wchar_t);
[da1bafb]90static void stdout_redraw(outdev_t *);
[b9c7425]91
92static outdev_operations_t stdout_ops = {
[a71c158]93 .write = stdout_write,
94 .redraw = stdout_redraw
[b9c7425]95};
96
[b366a6f4]97/** Override kernel console lockout */
98bool console_override = false;
[44b7783]99
[ac8e7a9]100/** Standard input and output character devices */
101indev_t *stdin = NULL;
102outdev_t *stdout = NULL;
[411b6a6]103
[44b7783]104indev_t *stdin_wire(void)
105{
106 if (stdin == NULL) {
[b9c7425]107 indev_initialize("stdin", &stdin_sink, &stdin_ops);
108 stdin = &stdin_sink;
[44b7783]109 }
110
111 return stdin;
112}
113
[b9c7425]114void stdout_wire(outdev_t *outdev)
115{
116 if (stdout == NULL) {
117 outdev_initialize("stdout", &stdout_source, &stdout_ops);
118 stdout = &stdout_source;
119 }
120
121 list_append(&outdev->link, &stdout->list);
122}
123
[b366a6f4]124static void stdout_write(outdev_t *dev, wchar_t ch)
[b9c7425]125{
[55b77d9]126 list_foreach(dev->list, cur) {
[b9c7425]127 outdev_t *sink = list_get_instance(cur, outdev_t, link);
[a71c158]128 if ((sink) && (sink->op->write))
[b366a6f4]129 sink->op->write(sink, ch);
[a71c158]130 }
131}
132
133static void stdout_redraw(outdev_t *dev)
134{
[55b77d9]135 list_foreach(dev->list, cur) {
[a71c158]136 outdev_t *sink = list_get_instance(cur, outdev_t, link);
137 if ((sink) && (sink->op->redraw))
138 sink->op->redraw(sink);
[b9c7425]139 }
140}
141
[82b71ef1]142/** Initialize kernel logging facility
143 *
144 * The shared area contains kernel cyclic buffer. Userspace application may
145 * be notified on new data with indication of position and size
146 * of the data within the circular buffer.
[ac8e7a9]147 *
[82b71ef1]148 */
149void klog_init(void)
150{
151 void *faddr = (void *) KA2PA(klog);
152
153 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
154
155 klog_parea.pbase = (uintptr_t) faddr;
[eec616b]156 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
[d7533c7]157 klog_parea.unpriv = false;
[b366a6f4]158 klog_parea.mapped = false;
[82b71ef1]159 ddi_parea_register(&klog_parea);
[ac8e7a9]160
[96b02eb9]161 sysinfo_set_item_val("klog.faddr", NULL, (sysarg_t) faddr);
[13a638d]162 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
[97d42d5]163
[0496c17]164 event_set_unmask_callback(EVENT_KLOG, klog_update);
[712c4ba]165 atomic_set(&klog_inited, true);
[82b71ef1]166}
167
[516ff92]168void grab_console(void)
169{
[b366a6f4]170 bool prev = console_override;
[422fd81]171
[b366a6f4]172 console_override = true;
[a71c158]173 if ((stdout) && (stdout->op->redraw))
174 stdout->op->redraw(stdout);
[402de0c]175
[b366a6f4]176 if ((stdin) && (!prev)) {
[da1bafb]177 /*
178 * Force the console to print the prompt.
179 */
[402de0c]180 indev_push_character(stdin, '\n');
[da1bafb]181 }
[516ff92]182}
183
184void release_console(void)
185{
[b366a6f4]186 console_override = false;
[516ff92]187}
188
[b366a6f4]189/** Activate kernel console override */
190sysarg_t sys_debug_activate_console(void)
[312cc68]191{
192#ifdef CONFIG_KCONSOLE
193 grab_console();
194 return true;
195#else
196 return false;
197#endif
198}
199
[ac8e7a9]200/** Get string from input character device.
[2677758]201 *
[ac8e7a9]202 * Read characters from input character device until first occurrence
[2677758]203 * of newline character.
204 *
[ac8e7a9]205 * @param indev Input character device.
[c583970]206 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]207 * @param buflen Size of the buffer.
[ff3b3197]208 *
209 * @return Number of characters read.
[ac8e7a9]210 *
[2677758]211 */
[98000fb]212size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]213{
[c583970]214 size_t offset = 0;
[98000fb]215 size_t count = 0;
[c583970]216 buf[offset] = 0;
[516ff92]217
[c583970]218 wchar_t ch;
[44b7783]219 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]220 if (ch == '\b') {
[c583970]221 if (count > 0) {
222 /* Space, backspace, space */
[e8a9dc3]223 putchar('\b');
224 putchar(' ');
225 putchar('\b');
[c583970]226
227 count--;
228 offset = str_lsize(buf, count);
229 buf[offset] = 0;
[e8a9dc3]230 }
[2677758]231 }
[c583970]232 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
233 putchar(ch);
234 count++;
235 buf[offset] = 0;
236 }
[2677758]237 }
[ac8e7a9]238
[c583970]239 return count;
[2677758]240}
241
[ac8e7a9]242/** Get character from input device & echo it to screen */
[c583970]243wchar_t getc(indev_t *indev)
[2677758]244{
[44b7783]245 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]246 putchar(ch);
[2677758]247 return ch;
248}
[973be64e]249
[0fe52ef]250void klog_update(void *event)
[82b71ef1]251{
[712c4ba]252 if (!atomic_get(&klog_inited))
253 return;
254
[82b71ef1]255 spinlock_lock(&klog_lock);
256
[712c4ba]257 if (klog_uspace > 0) {
[f9061b4]258 if (event_notify_3(EVENT_KLOG, true, klog_start, klog_len,
259 klog_uspace) == EOK)
260 klog_uspace = 0;
[05641a9e]261 }
[82b71ef1]262
263 spinlock_unlock(&klog_lock);
264}
265
[eec616b]266void putchar(const wchar_t ch)
[973be64e]267{
[712c4ba]268 bool ordy = ((stdout) && (stdout->op->write));
269
[82b71ef1]270 spinlock_lock(&klog_lock);
271
[712c4ba]272 /* Print charaters stored in kernel log */
273 if (ordy) {
274 while (klog_stored > 0) {
275 wchar_t tmp = klog[(klog_start + klog_len - klog_stored) % KLOG_LENGTH];
276 klog_stored--;
277
278 /*
279 * We need to give up the spinlock for
280 * the physical operation of writting out
281 * the character.
282 */
283 spinlock_unlock(&klog_lock);
[b366a6f4]284 stdout->op->write(stdout, tmp);
[712c4ba]285 spinlock_lock(&klog_lock);
286 }
[c859753]287 }
288
289 /* Store character in the cyclic kernel log */
[171f9a1]290 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
291 if (klog_len < KLOG_LENGTH)
[c859753]292 klog_len++;
293 else
[171f9a1]294 klog_start = (klog_start + 1) % KLOG_LENGTH;
[c859753]295
[712c4ba]296 if (!ordy) {
297 if (klog_stored < klog_len)
298 klog_stored++;
299 }
300
301 /* The character is stored for uspace */
302 if (klog_uspace < klog_len)
303 klog_uspace++;
304
305 spinlock_unlock(&klog_lock);
306
307 if (ordy) {
308 /*
309 * Output the character. In this case
310 * it should be no longer buffered.
311 */
[b366a6f4]312 stdout->op->write(stdout, ch);
[712c4ba]313 } else {
[da52547]314 /*
315 * No standard output routine defined yet.
316 * The character is still stored in the kernel log
317 * for possible future output.
318 *
319 * The early_putchar() function is used to output
320 * the character for low-level debugging purposes.
321 * Note that the early_putc() function might be
322 * a no-op on certain hardware configurations.
323 */
324 early_putchar(ch);
[c859753]325 }
[82b71ef1]326
[97d42d5]327 /* Force notification on newline */
328 if (ch == '\n')
[5d0500c]329 klog_update(NULL);
[973be64e]330}
[b45c443]331
[312cc68]332/** Print using kernel facility
333 *
334 * Print to kernel log.
335 *
336 */
[96b02eb9]337sysarg_t sys_klog(int fd, const void *buf, size_t size)
[312cc68]338{
339 char *data;
340 int rc;
[eec616b]341
[c583970]342 if (size > PAGE_SIZE)
[96b02eb9]343 return (sysarg_t) ELIMIT;
[312cc68]344
[c583970]345 if (size > 0) {
346 data = (char *) malloc(size + 1, 0);
[312cc68]347 if (!data)
[96b02eb9]348 return (sysarg_t) ENOMEM;
[312cc68]349
[c583970]350 rc = copy_from_uspace(data, buf, size);
[312cc68]351 if (rc) {
352 free(data);
[96b02eb9]353 return (sysarg_t) rc;
[312cc68]354 }
[c583970]355 data[size] = 0;
[312cc68]356
357 printf("%s", data);
358 free(data);
359 } else
[5d0500c]360 klog_update(NULL);
[312cc68]361
[c583970]362 return size;
[312cc68]363}
364
[06e1e95]365/** @}
[b45c443]366 */
Note: See TracBrowser for help on using the repository browser.