source: mainline/kernel/generic/src/console/console.c@ 91db0280

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

Cherrypick kernel logging facility

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