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
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 list_foreach(dev->list, cur) {
127 outdev_t *sink = list_get_instance(cur, outdev_t, link);
128 if ((sink) && (sink->op->write))
129 sink->op->write(sink, ch, silent);
130 }
131}
132
133static void stdout_redraw(outdev_t *dev)
134{
135 list_foreach(dev->list, cur) {
136 outdev_t *sink = list_get_instance(cur, outdev_t, link);
137 if ((sink) && (sink->op->redraw))
138 sink->op->redraw(sink);
139 }
140}
141
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.
147 *
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;
156 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
157 klog_parea.unpriv = 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 = silent;
170
171 silent = false;
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 // FIXME arch_release_console
186 silent = true;
187}
188
189/** Tell kernel to get keyboard/console access again */
190sysarg_t sys_debug_enable_console(void)
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 */
201sysarg_t sys_debug_disable_console(void)
202{
203 release_console();
204 return true;
205}
206
207/** Get string from input character device.
208 *
209 * Read characters from input character device until first occurrence
210 * of newline character.
211 *
212 * @param indev Input character device.
213 * @param buf Buffer where to store string terminated by NULL.
214 * @param buflen Size of the buffer.
215 *
216 * @return Number of characters read.
217 *
218 */
219size_t gets(indev_t *indev, char *buf, size_t buflen)
220{
221 size_t offset = 0;
222 size_t count = 0;
223 buf[offset] = 0;
224
225 wchar_t ch;
226 while ((ch = indev_pop_character(indev)) != '\n') {
227 if (ch == '\b') {
228 if (count > 0) {
229 /* Space, backspace, space */
230 putchar('\b');
231 putchar(' ');
232 putchar('\b');
233
234 count--;
235 offset = str_lsize(buf, count);
236 buf[offset] = 0;
237 }
238 }
239 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
240 putchar(ch);
241 count++;
242 buf[offset] = 0;
243 }
244 }
245
246 return count;
247}
248
249/** Get character from input device & echo it to screen */
250wchar_t getc(indev_t *indev)
251{
252 wchar_t ch = indev_pop_character(indev);
253 putchar(ch);
254 return ch;
255}
256
257void klog_update(void)
258{
259 if (!atomic_get(&klog_inited))
260 return;
261
262 spinlock_lock(&klog_lock);
263
264 if (klog_uspace > 0) {
265 if (event_notify_3(EVENT_KLOG, true, klog_start, klog_len,
266 klog_uspace) == EOK)
267 klog_uspace = 0;
268 }
269
270 spinlock_unlock(&klog_lock);
271}
272
273void putchar(const wchar_t ch)
274{
275 bool ordy = ((stdout) && (stdout->op->write));
276
277 spinlock_lock(&klog_lock);
278
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 }
294 }
295
296 /* Store character in the cyclic kernel log */
297 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
298 if (klog_len < KLOG_LENGTH)
299 klog_len++;
300 else
301 klog_start = (klog_start + 1) % KLOG_LENGTH;
302
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 */
319 stdout->op->write(stdout, ch, silent);
320 } else {
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);
332 }
333
334 /* Force notification on newline */
335 if (ch == '\n')
336 klog_update();
337}
338
339/** Print using kernel facility
340 *
341 * Print to kernel log.
342 *
343 */
344sysarg_t sys_klog(int fd, const void *buf, size_t size)
345{
346 char *data;
347 int rc;
348
349 if (size > PAGE_SIZE)
350 return (sysarg_t) ELIMIT;
351
352 if (size > 0) {
353 data = (char *) malloc(size + 1, 0);
354 if (!data)
355 return (sysarg_t) ENOMEM;
356
357 rc = copy_from_uspace(data, buf, size);
358 if (rc) {
359 free(data);
360 return (sysarg_t) rc;
361 }
362 data[size] = 0;
363
364 printf("%s", data);
365 free(data);
366 } else
367 klog_update();
368
369 return size;
370}
371
372/** @}
373 */
Note: See TracBrowser for help on using the repository browser.