source: mainline/kernel/generic/src/console/console.c@ 97d42d5

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

get rid of KLOG_LATENCY (thanks to the event notification unmask callback this is no longer needed)

  • 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 */
[171f9a1]59static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE)));
[c859753]60
[c0855a0]61/** Kernel log initialized */
[82b71ef1]62static bool 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 */
[a71c158]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);
[ac8e7a9]168
[82b71ef1]169 spinlock_lock(&klog_lock);
170 klog_inited = true;
171 spinlock_unlock(&klog_lock);
172}
173
[516ff92]174void grab_console(void)
175{
[422fd81]176 bool prev = silent;
177
[516ff92]178 silent = false;
[a71c158]179 if ((stdout) && (stdout->op->redraw))
180 stdout->op->redraw(stdout);
[402de0c]181
[da1bafb]182 if ((stdin) && (prev)) {
183 /*
184 * Force the console to print the prompt.
185 */
[402de0c]186 indev_push_character(stdin, '\n');
[da1bafb]187 }
[516ff92]188}
189
190void release_console(void)
191{
[a71c158]192 // FIXME arch_release_console
[516ff92]193 silent = true;
194}
195
[312cc68]196/** Tell kernel to get keyboard/console access again */
[96b02eb9]197sysarg_t sys_debug_enable_console(void)
[312cc68]198{
199#ifdef CONFIG_KCONSOLE
200 grab_console();
201 return true;
202#else
203 return false;
204#endif
205}
206
207/** Tell kernel to relinquish keyboard/console access */
[96b02eb9]208sysarg_t sys_debug_disable_console(void)
[312cc68]209{
210 release_console();
211 return true;
212}
213
[ac8e7a9]214/** Get string from input character device.
[2677758]215 *
[ac8e7a9]216 * Read characters from input character device until first occurrence
[2677758]217 * of newline character.
218 *
[ac8e7a9]219 * @param indev Input character device.
[c583970]220 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]221 * @param buflen Size of the buffer.
[ff3b3197]222 *
223 * @return Number of characters read.
[ac8e7a9]224 *
[2677758]225 */
[98000fb]226size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]227{
[c583970]228 size_t offset = 0;
[98000fb]229 size_t count = 0;
[c583970]230 buf[offset] = 0;
[516ff92]231
[c583970]232 wchar_t ch;
[44b7783]233 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]234 if (ch == '\b') {
[c583970]235 if (count > 0) {
236 /* Space, backspace, space */
[e8a9dc3]237 putchar('\b');
238 putchar(' ');
239 putchar('\b');
[c583970]240
241 count--;
242 offset = str_lsize(buf, count);
243 buf[offset] = 0;
[e8a9dc3]244 }
[2677758]245 }
[c583970]246 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
247 putchar(ch);
248 count++;
249 buf[offset] = 0;
250 }
[2677758]251 }
[ac8e7a9]252
[c583970]253 return count;
[2677758]254}
255
[ac8e7a9]256/** Get character from input device & echo it to screen */
[c583970]257wchar_t getc(indev_t *indev)
[2677758]258{
[44b7783]259 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]260 putchar(ch);
[2677758]261 return ch;
262}
[973be64e]263
[82b71ef1]264void klog_update(void)
265{
266 spinlock_lock(&klog_lock);
267
[f9061b4]268 if ((klog_inited) && (klog_uspace > 0)) {
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{
[82b71ef1]279 spinlock_lock(&klog_lock);
280
[ac8e7a9]281 if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
[c859753]282 /* Print charaters stored in kernel log */
[98000fb]283 size_t i;
[c859753]284 for (i = klog_len - klog_stored; i < klog_len; i++)
[171f9a1]285 stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
[c859753]286 klog_stored = 0;
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
[ac8e7a9]296 if ((stdout) && (stdout->op->write))
[eec616b]297 stdout->op->write(stdout, ch, silent);
[c859753]298 else {
[da52547]299 /*
300 * No standard output routine defined yet.
301 * The character is still stored in the kernel log
302 * for possible future output.
303 *
304 * The early_putchar() function is used to output
305 * the character for low-level debugging purposes.
306 * Note that the early_putc() function might be
307 * a no-op on certain hardware configurations.
308 *
309 */
310 early_putchar(ch);
311
[c859753]312 if (klog_stored < klog_len)
313 klog_stored++;
314 }
[82b71ef1]315
316 /* The character is stored for uspace */
317 if (klog_uspace < klog_len)
318 klog_uspace++;
319
320 spinlock_unlock(&klog_lock);
321
[97d42d5]322 /* Force notification on newline */
323 if (ch == '\n')
[c05a50f]324 klog_update();
[973be64e]325}
[b45c443]326
[312cc68]327/** Print using kernel facility
328 *
329 * Print to kernel log.
330 *
331 */
[96b02eb9]332sysarg_t sys_klog(int fd, const void *buf, size_t size)
[312cc68]333{
334 char *data;
335 int rc;
[eec616b]336
[c583970]337 if (size > PAGE_SIZE)
[96b02eb9]338 return (sysarg_t) ELIMIT;
[312cc68]339
[c583970]340 if (size > 0) {
341 data = (char *) malloc(size + 1, 0);
[312cc68]342 if (!data)
[96b02eb9]343 return (sysarg_t) ENOMEM;
[312cc68]344
[c583970]345 rc = copy_from_uspace(data, buf, size);
[312cc68]346 if (rc) {
347 free(data);
[96b02eb9]348 return (sysarg_t) rc;
[312cc68]349 }
[c583970]350 data[size] = 0;
[312cc68]351
352 printf("%s", data);
353 free(data);
354 } else
355 klog_update();
356
[c583970]357 return size;
[312cc68]358}
359
[06e1e95]360/** @}
[b45c443]361 */
Note: See TracBrowser for help on using the repository browser.