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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f6ab787 was f6ab787, checked in by Martin Decky <martin@…>, 11 years ago

rename sys_debug_activate_console to sys_debug_console in order to anticipate more generic use of the syscall in the near future

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