source: mainline/kernel/generic/src/console/console.c@ 550523f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 550523f5 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

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