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
Line 
1/*
2 * Copyright (c) 2003 Josef Cejka
3 * Copyright (c) 2005 Jakub Jermar
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
30/** @addtogroup genericconsole
31 * @{
32 */
33/** @file
34 */
35
36#include <console/console.h>
37#include <console/chardev.h>
38#include <sysinfo/sysinfo.h>
39#include <synch/waitq.h>
40#include <synch/spinlock.h>
41#include <typedefs.h>
42#include <ddi/irq.h>
43#include <ddi/ddi.h>
44#include <ipc/event.h>
45#include <ipc/irq.h>
46#include <arch.h>
47#include <panic.h>
48#include <print.h>
49#include <putchar.h>
50#include <atomic.h>
51#include <syscall/copy.h>
52#include <errno.h>
53#include <str.h>
54
55#define KLOG_PAGES 8
56#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
57
58/** Kernel log cyclic buffer */
59static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE)));
60
61/** Kernel log initialized */
62static bool klog_inited = false;
63
64/** First kernel log characters */
65static size_t klog_start = 0;
66
67/** Number of valid kernel log characters */
68static size_t klog_len = 0;
69
70/** Number of stored (not printed) kernel log characters */
71static size_t klog_stored = 0;
72
73/** Number of stored kernel log characters for uspace */
74static size_t klog_uspace = 0;
75
76/** Kernel log spinlock */
77SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "*klog_lock");
78
79/** Physical memory area used for klog buffer */
80static parea_t klog_parea;
81
82static indev_t stdin_sink;
83static outdev_t stdout_source;
84
85static indev_operations_t stdin_ops = {
86 .poll = NULL
87};
88
89static void stdout_write(outdev_t *, wchar_t, bool);
90static void stdout_redraw(outdev_t *);
91
92static outdev_operations_t stdout_ops = {
93 .write = stdout_write,
94 .redraw = stdout_redraw
95};
96
97/** Silence output */
98bool silent = false;
99
100/** Standard input and output character devices */
101indev_t *stdin = NULL;
102outdev_t *stdout = NULL;
103
104indev_t *stdin_wire(void)
105{
106 if (stdin == NULL) {
107 indev_initialize("stdin", &stdin_sink, &stdin_ops);
108 stdin = &stdin_sink;
109 }
110
111 return stdin;
112}
113
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);
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);
143 }
144}
145
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.
151 *
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;
160 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
161 klog_parea.unpriv = false;
162 ddi_parea_register(&klog_parea);
163
164 sysinfo_set_item_val("klog.faddr", NULL, (sysarg_t) faddr);
165 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
166
167 event_set_unmask_callback(EVENT_KLOG, klog_update);
168
169 spinlock_lock(&klog_lock);
170 klog_inited = true;
171 spinlock_unlock(&klog_lock);
172}
173
174void grab_console(void)
175{
176 bool prev = silent;
177
178 silent = false;
179 if ((stdout) && (stdout->op->redraw))
180 stdout->op->redraw(stdout);
181
182 if ((stdin) && (prev)) {
183 /*
184 * Force the console to print the prompt.
185 */
186 indev_push_character(stdin, '\n');
187 }
188}
189
190void release_console(void)
191{
192 // FIXME arch_release_console
193 silent = true;
194}
195
196/** Tell kernel to get keyboard/console access again */
197sysarg_t sys_debug_enable_console(void)
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 */
208sysarg_t sys_debug_disable_console(void)
209{
210 release_console();
211 return true;
212}
213
214/** Get string from input character device.
215 *
216 * Read characters from input character device until first occurrence
217 * of newline character.
218 *
219 * @param indev Input character device.
220 * @param buf Buffer where to store string terminated by NULL.
221 * @param buflen Size of the buffer.
222 *
223 * @return Number of characters read.
224 *
225 */
226size_t gets(indev_t *indev, char *buf, size_t buflen)
227{
228 size_t offset = 0;
229 size_t count = 0;
230 buf[offset] = 0;
231
232 wchar_t ch;
233 while ((ch = indev_pop_character(indev)) != '\n') {
234 if (ch == '\b') {
235 if (count > 0) {
236 /* Space, backspace, space */
237 putchar('\b');
238 putchar(' ');
239 putchar('\b');
240
241 count--;
242 offset = str_lsize(buf, count);
243 buf[offset] = 0;
244 }
245 }
246 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
247 putchar(ch);
248 count++;
249 buf[offset] = 0;
250 }
251 }
252
253 return count;
254}
255
256/** Get character from input device & echo it to screen */
257wchar_t getc(indev_t *indev)
258{
259 wchar_t ch = indev_pop_character(indev);
260 putchar(ch);
261 return ch;
262}
263
264void klog_update(void)
265{
266 spinlock_lock(&klog_lock);
267
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;
272 }
273
274 spinlock_unlock(&klog_lock);
275}
276
277void putchar(const wchar_t ch)
278{
279 spinlock_lock(&klog_lock);
280
281 if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
282 /* Print charaters stored in kernel log */
283 size_t i;
284 for (i = klog_len - klog_stored; i < klog_len; i++)
285 stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
286 klog_stored = 0;
287 }
288
289 /* Store character in the cyclic kernel log */
290 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
291 if (klog_len < KLOG_LENGTH)
292 klog_len++;
293 else
294 klog_start = (klog_start + 1) % KLOG_LENGTH;
295
296 if ((stdout) && (stdout->op->write))
297 stdout->op->write(stdout, ch, silent);
298 else {
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
312 if (klog_stored < klog_len)
313 klog_stored++;
314 }
315
316 /* The character is stored for uspace */
317 if (klog_uspace < klog_len)
318 klog_uspace++;
319
320 spinlock_unlock(&klog_lock);
321
322 /* Force notification on newline */
323 if (ch == '\n')
324 klog_update();
325}
326
327/** Print using kernel facility
328 *
329 * Print to kernel log.
330 *
331 */
332sysarg_t sys_klog(int fd, const void *buf, size_t size)
333{
334 char *data;
335 int rc;
336
337 if (size > PAGE_SIZE)
338 return (sysarg_t) ELIMIT;
339
340 if (size > 0) {
341 data = (char *) malloc(size + 1, 0);
342 if (!data)
343 return (sysarg_t) ENOMEM;
344
345 rc = copy_from_uspace(data, buf, size);
346 if (rc) {
347 free(data);
348 return (sysarg_t) rc;
349 }
350 data[size] = 0;
351
352 printf("%s", data);
353 free(data);
354 } else
355 klog_update();
356
357 return size;
358}
359
360/** @}
361 */
Note: See TracBrowser for help on using the repository browser.