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
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>
[6fa9a99d]54#include <abi/kio.h>
[2677758]55
[6fa9a99d]56#define KIO_PAGES 8
57#define KIO_LENGTH (KIO_PAGES * PAGE_SIZE / sizeof(wchar_t))
[c859753]58
[c0855a0]59/** Kernel log cyclic buffer */
[6fa9a99d]60wchar_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
[c859753]61
[c0855a0]62/** Kernel log initialized */
[6fa9a99d]63static atomic_t kio_inited = {false};
[da1bafb]64
[c0855a0]65/** First kernel log characters */
[6fa9a99d]66static size_t kio_start = 0;
[da1bafb]67
[c0855a0]68/** Number of valid kernel log characters */
[6fa9a99d]69static size_t kio_len = 0;
[da1bafb]70
[c0855a0]71/** Number of stored (not printed) kernel log characters */
[6fa9a99d]72static size_t kio_stored = 0;
[da1bafb]73
[c0855a0]74/** Number of stored kernel log characters for uspace */
[6fa9a99d]75static size_t kio_uspace = 0;
[82b71ef1]76
[c0855a0]77/** Kernel log spinlock */
[91db0280]78SPINLOCK_INITIALIZE_NAME(kio_lock, "kio_lock");
[82b71ef1]79
[6fa9a99d]80/** Physical memory area used for kio buffer */
81static parea_t kio_parea;
[516ff92]82
[b9c7425]83static indev_t stdin_sink;
84static outdev_t stdout_source;
85
[7ddc2c7]86static void stdin_signal(indev_t *, indev_signal_t);
87
[44b7783]88static indev_operations_t stdin_ops = {
[7ddc2c7]89 .poll = NULL,
90 .signal = stdin_signal
[44b7783]91};
92
[b366a6f4]93static void stdout_write(outdev_t *, wchar_t);
[da1bafb]94static void stdout_redraw(outdev_t *);
[7ddc2c7]95static void stdout_scroll_up(outdev_t *);
96static void stdout_scroll_down(outdev_t *);
[b9c7425]97
98static outdev_operations_t stdout_ops = {
[a71c158]99 .write = stdout_write,
[7ddc2c7]100 .redraw = stdout_redraw,
101 .scroll_up = stdout_scroll_up,
102 .scroll_down = stdout_scroll_down
[b9c7425]103};
104
[b366a6f4]105/** Override kernel console lockout */
106bool console_override = false;
[44b7783]107
[ac8e7a9]108/** Standard input and output character devices */
109indev_t *stdin = NULL;
110outdev_t *stdout = NULL;
[411b6a6]111
[44b7783]112indev_t *stdin_wire(void)
113{
114 if (stdin == NULL) {
[b9c7425]115 indev_initialize("stdin", &stdin_sink, &stdin_ops);
116 stdin = &stdin_sink;
[44b7783]117 }
118
119 return stdin;
120}
121
[7ddc2c7]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
[b9c7425]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
[b366a6f4]146static void stdout_write(outdev_t *dev, wchar_t ch)
[b9c7425]147{
[feeac0d]148 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]149 if ((sink) && (sink->op->write))
[b366a6f4]150 sink->op->write(sink, ch);
[a71c158]151 }
152}
153
154static void stdout_redraw(outdev_t *dev)
155{
[feeac0d]156 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]157 if ((sink) && (sink->op->redraw))
158 sink->op->redraw(sink);
[b9c7425]159 }
160}
161
[7ddc2c7]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
[82b71ef1]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.
[ac8e7a9]183 *
[82b71ef1]184 */
[6fa9a99d]185void kio_init(void)
[82b71ef1]186{
[6fa9a99d]187 void *faddr = (void *) KA2PA(kio);
[82b71ef1]188
189 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
190
[6fa9a99d]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);
[ac8e7a9]196
[6fa9a99d]197 sysinfo_set_item_val("kio.faddr", NULL, (sysarg_t) faddr);
198 sysinfo_set_item_val("kio.pages", NULL, KIO_PAGES);
[97d42d5]199
[6fa9a99d]200 event_set_unmask_callback(EVENT_KIO, kio_update);
201 atomic_set(&kio_inited, true);
[82b71ef1]202}
203
[516ff92]204void grab_console(void)
205{
[b366a6f4]206 bool prev = console_override;
[422fd81]207
[b366a6f4]208 console_override = true;
[a71c158]209 if ((stdout) && (stdout->op->redraw))
210 stdout->op->redraw(stdout);
[402de0c]211
[b366a6f4]212 if ((stdin) && (!prev)) {
[da1bafb]213 /*
214 * Force the console to print the prompt.
215 */
[402de0c]216 indev_push_character(stdin, '\n');
[da1bafb]217 }
[516ff92]218}
219
220void release_console(void)
221{
[b366a6f4]222 console_override = false;
[516ff92]223}
224
[b366a6f4]225/** Activate kernel console override */
[f6ab787]226sysarg_t sys_debug_console(void)
[312cc68]227{
228#ifdef CONFIG_KCONSOLE
229 grab_console();
230 return true;
231#else
232 return false;
233#endif
234}
235
[ac8e7a9]236/** Get string from input character device.
[2677758]237 *
[ac8e7a9]238 * Read characters from input character device until first occurrence
[2677758]239 * of newline character.
240 *
[ac8e7a9]241 * @param indev Input character device.
[c583970]242 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]243 * @param buflen Size of the buffer.
[ff3b3197]244 *
245 * @return Number of characters read.
[ac8e7a9]246 *
[2677758]247 */
[98000fb]248size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]249{
[c583970]250 size_t offset = 0;
[98000fb]251 size_t count = 0;
[c583970]252 buf[offset] = 0;
[516ff92]253
[c583970]254 wchar_t ch;
[44b7783]255 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]256 if (ch == '\b') {
[c583970]257 if (count > 0) {
258 /* Space, backspace, space */
[e8a9dc3]259 putchar('\b');
260 putchar(' ');
261 putchar('\b');
[c583970]262
263 count--;
264 offset = str_lsize(buf, count);
265 buf[offset] = 0;
[e8a9dc3]266 }
[2677758]267 }
[7ddc2c7]268
[c583970]269 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
270 putchar(ch);
271 count++;
272 buf[offset] = 0;
273 }
[2677758]274 }
[ac8e7a9]275
[c583970]276 return count;
[2677758]277}
278
[ac8e7a9]279/** Get character from input device & echo it to screen */
[c583970]280wchar_t getc(indev_t *indev)
[2677758]281{
[44b7783]282 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]283 putchar(ch);
[2677758]284 return ch;
285}
[973be64e]286
[6fa9a99d]287void kio_update(void *event)
[82b71ef1]288{
[6fa9a99d]289 if (!atomic_get(&kio_inited))
[712c4ba]290 return;
291
[6fa9a99d]292 spinlock_lock(&kio_lock);
[82b71ef1]293
[6fa9a99d]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;
[05641a9e]298 }
[82b71ef1]299
[6fa9a99d]300 spinlock_unlock(&kio_lock);
[82b71ef1]301}
302
[91db0280]303/** Flush characters that are stored in the output buffer
[7ddc2c7]304 *
[91db0280]305 */
306void kio_flush(void)
[973be64e]307{
[712c4ba]308 bool ordy = ((stdout) && (stdout->op->write));
309
[91db0280]310 if (!ordy)
311 return;
312
[6fa9a99d]313 spinlock_lock(&kio_lock);
[91db0280]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);
[c859753]328 }
[91db0280]329
330 spinlock_unlock(&kio_lock);
331}
332
333/** Put a character into the output buffer.
[7ddc2c7]334 *
[91db0280]335 * The caller is required to hold kio_lock
336 */
337void kio_push_char(const wchar_t ch)
338{
[6fa9a99d]339 kio[(kio_start + kio_len) % KIO_LENGTH] = ch;
340 if (kio_len < KIO_LENGTH)
341 kio_len++;
[c859753]342 else
[6fa9a99d]343 kio_start = (kio_start + 1) % KIO_LENGTH;
[c859753]344
[91db0280]345 if (kio_stored < kio_len)
346 kio_stored++;
[712c4ba]347
348 /* The character is stored for uspace */
[6fa9a99d]349 if (kio_uspace < kio_len)
350 kio_uspace++;
[91db0280]351}
352
353void putchar(const wchar_t ch)
354{
355 bool ordy = ((stdout) && (stdout->op->write));
[712c4ba]356
[91db0280]357 spinlock_lock(&kio_lock);
358 kio_push_char(ch);
[6fa9a99d]359 spinlock_unlock(&kio_lock);
[712c4ba]360
[91db0280]361 /* Output stored characters */
362 kio_flush();
363
364 if (!ordy) {
[da52547]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);
[c859753]376 }
[82b71ef1]377
[97d42d5]378 /* Force notification on newline */
379 if (ch == '\n')
[6fa9a99d]380 kio_update(NULL);
[973be64e]381}
[b45c443]382
[312cc68]383/** Print using kernel facility
384 *
385 * Print to kernel log.
386 *
387 */
[6fa9a99d]388sysarg_t sys_kio(int cmd, const void *buf, size_t size)
[312cc68]389{
390 char *data;
391 int rc;
[a801688b]392
393 switch (cmd) {
[6fa9a99d]394 case KIO_UPDATE:
395 kio_update(NULL);
[a801688b]396 return EOK;
[6fa9a99d]397 case KIO_WRITE:
398 case KIO_COMMAND:
[a801688b]399 break;
400 default:
401 return ENOTSUP;
402 }
403
[c583970]404 if (size > PAGE_SIZE)
[96b02eb9]405 return (sysarg_t) ELIMIT;
[312cc68]406
[c583970]407 if (size > 0) {
408 data = (char *) malloc(size + 1, 0);
[312cc68]409 if (!data)
[96b02eb9]410 return (sysarg_t) ENOMEM;
[312cc68]411
[c583970]412 rc = copy_from_uspace(data, buf, size);
[312cc68]413 if (rc) {
414 free(data);
[96b02eb9]415 return (sysarg_t) rc;
[312cc68]416 }
[c583970]417 data[size] = 0;
[312cc68]418
[297cb73]419 switch (cmd) {
[6fa9a99d]420 case KIO_WRITE:
[297cb73]421 printf("%s", data);
422 break;
[6fa9a99d]423 case KIO_COMMAND:
[11527051]424 if (!stdin)
425 break;
[297cb73]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
[312cc68]432 free(data);
[297cb73]433 }
434
[c583970]435 return size;
[312cc68]436}
437
[06e1e95]438/** @}
[b45c443]439 */
Note: See TracBrowser for help on using the repository browser.