source: mainline/kernel/generic/src/console/console.c@ 55b77d9

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

Separate list_t typedef from link_t (kernel part).

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