source: mainline/kernel/generic/src/console/console.c@ 313775b

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

avoid spinlocks to be taken in the direct code path to physically outputing kconsole characters to the framebuffer (this fixes ticket #243)
the spinlock in printf() (kernel) was sacrificed, this might lead potentially to scrambled kconsole output
the hack of ignoring spinlocks whose names start with "*" in the livelock detector can be removed

  • Property mode set to 100644
File size: 8.5 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 */
[171f9a1]59static wchar_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
[da1bafb]89static void stdout_write(outdev_t *, wchar_t, bool);
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
[44b7783]97/** Silence output */
98bool silent = false;
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
124static void stdout_write(outdev_t *dev, wchar_t ch, bool silent)
125{
126 link_t *cur;
127
128 for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
129 outdev_t *sink = list_get_instance(cur, outdev_t, link);
[a71c158]130 if ((sink) && (sink->op->write))
131 sink->op->write(sink, ch, silent);
132 }
133}
134
135static void stdout_redraw(outdev_t *dev)
136{
137 link_t *cur;
138
139 for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
140 outdev_t *sink = list_get_instance(cur, outdev_t, link);
141 if ((sink) && (sink->op->redraw))
142 sink->op->redraw(sink);
[b9c7425]143 }
144}
145
[82b71ef1]146/** Initialize kernel logging facility
147 *
148 * The shared area contains kernel cyclic buffer. Userspace application may
149 * be notified on new data with indication of position and size
150 * of the data within the circular buffer.
[ac8e7a9]151 *
[82b71ef1]152 */
153void klog_init(void)
154{
155 void *faddr = (void *) KA2PA(klog);
156
157 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
158
159 klog_parea.pbase = (uintptr_t) faddr;
[eec616b]160 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
[d7533c7]161 klog_parea.unpriv = false;
[82b71ef1]162 ddi_parea_register(&klog_parea);
[ac8e7a9]163
[96b02eb9]164 sysinfo_set_item_val("klog.faddr", NULL, (sysarg_t) faddr);
[13a638d]165 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
[97d42d5]166
[0496c17]167 event_set_unmask_callback(EVENT_KLOG, klog_update);
[712c4ba]168 atomic_set(&klog_inited, true);
[82b71ef1]169}
170
[516ff92]171void grab_console(void)
172{
[422fd81]173 bool prev = silent;
174
[516ff92]175 silent = false;
[a71c158]176 if ((stdout) && (stdout->op->redraw))
177 stdout->op->redraw(stdout);
[402de0c]178
[da1bafb]179 if ((stdin) && (prev)) {
180 /*
181 * Force the console to print the prompt.
182 */
[402de0c]183 indev_push_character(stdin, '\n');
[da1bafb]184 }
[516ff92]185}
186
187void release_console(void)
188{
[a71c158]189 // FIXME arch_release_console
[516ff92]190 silent = true;
191}
192
[312cc68]193/** Tell kernel to get keyboard/console access again */
[96b02eb9]194sysarg_t sys_debug_enable_console(void)
[312cc68]195{
196#ifdef CONFIG_KCONSOLE
197 grab_console();
198 return true;
199#else
200 return false;
201#endif
202}
203
204/** Tell kernel to relinquish keyboard/console access */
[96b02eb9]205sysarg_t sys_debug_disable_console(void)
[312cc68]206{
207 release_console();
208 return true;
209}
210
[ac8e7a9]211/** Get string from input character device.
[2677758]212 *
[ac8e7a9]213 * Read characters from input character device until first occurrence
[2677758]214 * of newline character.
215 *
[ac8e7a9]216 * @param indev Input character device.
[c583970]217 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]218 * @param buflen Size of the buffer.
[ff3b3197]219 *
220 * @return Number of characters read.
[ac8e7a9]221 *
[2677758]222 */
[98000fb]223size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]224{
[c583970]225 size_t offset = 0;
[98000fb]226 size_t count = 0;
[c583970]227 buf[offset] = 0;
[516ff92]228
[c583970]229 wchar_t ch;
[44b7783]230 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]231 if (ch == '\b') {
[c583970]232 if (count > 0) {
233 /* Space, backspace, space */
[e8a9dc3]234 putchar('\b');
235 putchar(' ');
236 putchar('\b');
[c583970]237
238 count--;
239 offset = str_lsize(buf, count);
240 buf[offset] = 0;
[e8a9dc3]241 }
[2677758]242 }
[c583970]243 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
244 putchar(ch);
245 count++;
246 buf[offset] = 0;
247 }
[2677758]248 }
[ac8e7a9]249
[c583970]250 return count;
[2677758]251}
252
[ac8e7a9]253/** Get character from input device & echo it to screen */
[c583970]254wchar_t getc(indev_t *indev)
[2677758]255{
[44b7783]256 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]257 putchar(ch);
[2677758]258 return ch;
259}
[973be64e]260
[82b71ef1]261void klog_update(void)
262{
[712c4ba]263 if (!atomic_get(&klog_inited))
264 return;
265
[82b71ef1]266 spinlock_lock(&klog_lock);
267
[712c4ba]268 if (klog_uspace > 0) {
[f9061b4]269 if (event_notify_3(EVENT_KLOG, true, klog_start, klog_len,
270 klog_uspace) == EOK)
271 klog_uspace = 0;
[05641a9e]272 }
[82b71ef1]273
274 spinlock_unlock(&klog_lock);
275}
276
[eec616b]277void putchar(const wchar_t ch)
[973be64e]278{
[712c4ba]279 bool ordy = ((stdout) && (stdout->op->write));
280
[82b71ef1]281 spinlock_lock(&klog_lock);
282
[712c4ba]283 /* Print charaters stored in kernel log */
284 if (ordy) {
285 while (klog_stored > 0) {
286 wchar_t tmp = klog[(klog_start + klog_len - klog_stored) % KLOG_LENGTH];
287 klog_stored--;
288
289 /*
290 * We need to give up the spinlock for
291 * the physical operation of writting out
292 * the character.
293 */
294 spinlock_unlock(&klog_lock);
295 stdout->op->write(stdout, tmp, silent);
296 spinlock_lock(&klog_lock);
297 }
[c859753]298 }
299
300 /* Store character in the cyclic kernel log */
[171f9a1]301 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
302 if (klog_len < KLOG_LENGTH)
[c859753]303 klog_len++;
304 else
[171f9a1]305 klog_start = (klog_start + 1) % KLOG_LENGTH;
[c859753]306
[712c4ba]307 if (!ordy) {
308 if (klog_stored < klog_len)
309 klog_stored++;
310 }
311
312 /* The character is stored for uspace */
313 if (klog_uspace < klog_len)
314 klog_uspace++;
315
316 spinlock_unlock(&klog_lock);
317
318 if (ordy) {
319 /*
320 * Output the character. In this case
321 * it should be no longer buffered.
322 */
[eec616b]323 stdout->op->write(stdout, ch, silent);
[712c4ba]324 } else {
[da52547]325 /*
326 * No standard output routine defined yet.
327 * The character is still stored in the kernel log
328 * for possible future output.
329 *
330 * The early_putchar() function is used to output
331 * the character for low-level debugging purposes.
332 * Note that the early_putc() function might be
333 * a no-op on certain hardware configurations.
334 */
335 early_putchar(ch);
[c859753]336 }
[82b71ef1]337
[97d42d5]338 /* Force notification on newline */
339 if (ch == '\n')
[c05a50f]340 klog_update();
[973be64e]341}
[b45c443]342
[312cc68]343/** Print using kernel facility
344 *
345 * Print to kernel log.
346 *
347 */
[96b02eb9]348sysarg_t sys_klog(int fd, const void *buf, size_t size)
[312cc68]349{
350 char *data;
351 int rc;
[eec616b]352
[c583970]353 if (size > PAGE_SIZE)
[96b02eb9]354 return (sysarg_t) ELIMIT;
[312cc68]355
[c583970]356 if (size > 0) {
357 data = (char *) malloc(size + 1, 0);
[312cc68]358 if (!data)
[96b02eb9]359 return (sysarg_t) ENOMEM;
[312cc68]360
[c583970]361 rc = copy_from_uspace(data, buf, size);
[312cc68]362 if (rc) {
363 free(data);
[96b02eb9]364 return (sysarg_t) rc;
[312cc68]365 }
[c583970]366 data[size] = 0;
[312cc68]367
368 printf("%s", data);
369 free(data);
370 } else
371 klog_update();
372
[c583970]373 return size;
[312cc68]374}
375
[06e1e95]376/** @}
[b45c443]377 */
Note: See TracBrowser for help on using the repository browser.