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

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

Provide a way for kernel console on SKI to disable the user-space driver.

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