source: mainline/kernel/generic/src/console/console.c@ 48fa501

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 48fa501 was 11527051, checked in by Jakub Jermar <jakub@…>, 13 years ago

Do nothing on KLOG_COMMAND if stdin is not defined.

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