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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b6f3e7e 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
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 atomic_t 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 atomic_set(&klog_inited, true);
169}
170
171void grab_console(void)
172{
173 bool prev = silent;
174
175 silent = false;
176 if ((stdout) && (stdout->op->redraw))
177 stdout->op->redraw(stdout);
178
179 if ((stdin) && (prev)) {
180 /*
181 * Force the console to print the prompt.
182 */
183 indev_push_character(stdin, '\n');
184 }
185}
186
187void release_console(void)
188{
189 // FIXME arch_release_console
190 silent = true;
191}
192
193/** Tell kernel to get keyboard/console access again */
194sysarg_t sys_debug_enable_console(void)
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 */
205sysarg_t sys_debug_disable_console(void)
206{
207 release_console();
208 return true;
209}
210
211/** Get string from input character device.
212 *
213 * Read characters from input character device until first occurrence
214 * of newline character.
215 *
216 * @param indev Input character device.
217 * @param buf Buffer where to store string terminated by NULL.
218 * @param buflen Size of the buffer.
219 *
220 * @return Number of characters read.
221 *
222 */
223size_t gets(indev_t *indev, char *buf, size_t buflen)
224{
225 size_t offset = 0;
226 size_t count = 0;
227 buf[offset] = 0;
228
229 wchar_t ch;
230 while ((ch = indev_pop_character(indev)) != '\n') {
231 if (ch == '\b') {
232 if (count > 0) {
233 /* Space, backspace, space */
234 putchar('\b');
235 putchar(' ');
236 putchar('\b');
237
238 count--;
239 offset = str_lsize(buf, count);
240 buf[offset] = 0;
241 }
242 }
243 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
244 putchar(ch);
245 count++;
246 buf[offset] = 0;
247 }
248 }
249
250 return count;
251}
252
253/** Get character from input device & echo it to screen */
254wchar_t getc(indev_t *indev)
255{
256 wchar_t ch = indev_pop_character(indev);
257 putchar(ch);
258 return ch;
259}
260
261void klog_update(void)
262{
263 if (!atomic_get(&klog_inited))
264 return;
265
266 spinlock_lock(&klog_lock);
267
268 if (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 bool ordy = ((stdout) && (stdout->op->write));
280
281 spinlock_lock(&klog_lock);
282
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 }
298 }
299
300 /* Store character in the cyclic kernel log */
301 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
302 if (klog_len < KLOG_LENGTH)
303 klog_len++;
304 else
305 klog_start = (klog_start + 1) % KLOG_LENGTH;
306
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 */
323 stdout->op->write(stdout, ch, silent);
324 } else {
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);
336 }
337
338 /* Force notification on newline */
339 if (ch == '\n')
340 klog_update();
341}
342
343/** Print using kernel facility
344 *
345 * Print to kernel log.
346 *
347 */
348sysarg_t sys_klog(int fd, const void *buf, size_t size)
349{
350 char *data;
351 int rc;
352
353 if (size > PAGE_SIZE)
354 return (sysarg_t) ELIMIT;
355
356 if (size > 0) {
357 data = (char *) malloc(size + 1, 0);
358 if (!data)
359 return (sysarg_t) ENOMEM;
360
361 rc = copy_from_uspace(data, buf, size);
362 if (rc) {
363 free(data);
364 return (sysarg_t) rc;
365 }
366 data[size] = 0;
367
368 printf("%s", data);
369 free(data);
370 } else
371 klog_update();
372
373 return size;
374}
375
376/** @}
377 */
Note: See TracBrowser for help on using the repository browser.