source: mainline/kernel/generic/src/console/console.c@ 7ed6ee2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7ed6ee2 was 11527051, checked in by Jakub Jermar <jakub@…>, 13 years ago

Do nothing on KLOG_COMMAND if stdin is not defined.

  • 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>
[a801688b]54#include <abi/klog.h>
[2677758]55
[bec0219]56#define KLOG_PAGES 8
[13a638d]57#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
[c859753]58
[c0855a0]59/** Kernel log cyclic buffer */
[e3a050c7]60wchar_t klog[KLOG_LENGTH] __attribute__((aligned(PAGE_SIZE)));
[c859753]61
[c0855a0]62/** Kernel log initialized */
[712c4ba]63static atomic_t klog_inited = {false};
[da1bafb]64
[c0855a0]65/** First kernel log characters */
[98000fb]66static size_t klog_start = 0;
[da1bafb]67
[c0855a0]68/** Number of valid kernel log characters */
[c859753]69static size_t klog_len = 0;
[da1bafb]70
[c0855a0]71/** Number of stored (not printed) kernel log characters */
[c859753]72static size_t klog_stored = 0;
[da1bafb]73
[c0855a0]74/** Number of stored kernel log characters for uspace */
[82b71ef1]75static size_t klog_uspace = 0;
76
[c0855a0]77/** Kernel log spinlock */
[712c4ba]78SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "klog_lock");
[82b71ef1]79
80/** Physical memory area used for klog buffer */
81static parea_t klog_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{
[55b77d9]127 list_foreach(dev->list, cur) {
[b9c7425]128 outdev_t *sink = list_get_instance(cur, outdev_t, link);
[a71c158]129 if ((sink) && (sink->op->write))
[b366a6f4]130 sink->op->write(sink, ch);
[a71c158]131 }
132}
133
134static void stdout_redraw(outdev_t *dev)
135{
[55b77d9]136 list_foreach(dev->list, cur) {
[a71c158]137 outdev_t *sink = list_get_instance(cur, outdev_t, link);
138 if ((sink) && (sink->op->redraw))
139 sink->op->redraw(sink);
[b9c7425]140 }
141}
142
[82b71ef1]143/** Initialize kernel logging facility
144 *
145 * The shared area contains kernel cyclic buffer. Userspace application may
146 * be notified on new data with indication of position and size
147 * of the data within the circular buffer.
[ac8e7a9]148 *
[82b71ef1]149 */
150void klog_init(void)
151{
152 void *faddr = (void *) KA2PA(klog);
153
154 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
155
156 klog_parea.pbase = (uintptr_t) faddr;
[eec616b]157 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
[d7533c7]158 klog_parea.unpriv = false;
[b366a6f4]159 klog_parea.mapped = false;
[82b71ef1]160 ddi_parea_register(&klog_parea);
[ac8e7a9]161
[96b02eb9]162 sysinfo_set_item_val("klog.faddr", NULL, (sysarg_t) faddr);
[13a638d]163 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
[97d42d5]164
[0496c17]165 event_set_unmask_callback(EVENT_KLOG, klog_update);
[712c4ba]166 atomic_set(&klog_inited, true);
[82b71ef1]167}
168
[516ff92]169void grab_console(void)
170{
[b366a6f4]171 bool prev = console_override;
[422fd81]172
[b366a6f4]173 console_override = true;
[a71c158]174 if ((stdout) && (stdout->op->redraw))
175 stdout->op->redraw(stdout);
[402de0c]176
[b366a6f4]177 if ((stdin) && (!prev)) {
[da1bafb]178 /*
179 * Force the console to print the prompt.
180 */
[402de0c]181 indev_push_character(stdin, '\n');
[da1bafb]182 }
[516ff92]183}
184
185void release_console(void)
186{
[b366a6f4]187 console_override = false;
[516ff92]188}
189
[b366a6f4]190/** Activate kernel console override */
191sysarg_t sys_debug_activate_console(void)
[312cc68]192{
193#ifdef CONFIG_KCONSOLE
194 grab_console();
195 return true;
196#else
197 return false;
198#endif
199}
200
[ac8e7a9]201/** Get string from input character device.
[2677758]202 *
[ac8e7a9]203 * Read characters from input character device until first occurrence
[2677758]204 * of newline character.
205 *
[ac8e7a9]206 * @param indev Input character device.
[c583970]207 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]208 * @param buflen Size of the buffer.
[ff3b3197]209 *
210 * @return Number of characters read.
[ac8e7a9]211 *
[2677758]212 */
[98000fb]213size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]214{
[c583970]215 size_t offset = 0;
[98000fb]216 size_t count = 0;
[c583970]217 buf[offset] = 0;
[516ff92]218
[c583970]219 wchar_t ch;
[44b7783]220 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]221 if (ch == '\b') {
[c583970]222 if (count > 0) {
223 /* Space, backspace, space */
[e8a9dc3]224 putchar('\b');
225 putchar(' ');
226 putchar('\b');
[c583970]227
228 count--;
229 offset = str_lsize(buf, count);
230 buf[offset] = 0;
[e8a9dc3]231 }
[2677758]232 }
[c583970]233 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
234 putchar(ch);
235 count++;
236 buf[offset] = 0;
237 }
[2677758]238 }
[ac8e7a9]239
[c583970]240 return count;
[2677758]241}
242
[ac8e7a9]243/** Get character from input device & echo it to screen */
[c583970]244wchar_t getc(indev_t *indev)
[2677758]245{
[44b7783]246 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]247 putchar(ch);
[2677758]248 return ch;
249}
[973be64e]250
[0fe52ef]251void klog_update(void *event)
[82b71ef1]252{
[712c4ba]253 if (!atomic_get(&klog_inited))
254 return;
255
[82b71ef1]256 spinlock_lock(&klog_lock);
257
[712c4ba]258 if (klog_uspace > 0) {
[f9061b4]259 if (event_notify_3(EVENT_KLOG, true, klog_start, klog_len,
260 klog_uspace) == EOK)
261 klog_uspace = 0;
[05641a9e]262 }
[82b71ef1]263
264 spinlock_unlock(&klog_lock);
265}
266
[eec616b]267void putchar(const wchar_t ch)
[973be64e]268{
[712c4ba]269 bool ordy = ((stdout) && (stdout->op->write));
270
[82b71ef1]271 spinlock_lock(&klog_lock);
272
[712c4ba]273 /* Print charaters stored in kernel log */
274 if (ordy) {
275 while (klog_stored > 0) {
276 wchar_t tmp = klog[(klog_start + klog_len - klog_stored) % KLOG_LENGTH];
277 klog_stored--;
278
279 /*
280 * We need to give up the spinlock for
281 * the physical operation of writting out
282 * the character.
283 */
284 spinlock_unlock(&klog_lock);
[b366a6f4]285 stdout->op->write(stdout, tmp);
[712c4ba]286 spinlock_lock(&klog_lock);
287 }
[c859753]288 }
289
290 /* Store character in the cyclic kernel log */
[171f9a1]291 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
292 if (klog_len < KLOG_LENGTH)
[c859753]293 klog_len++;
294 else
[171f9a1]295 klog_start = (klog_start + 1) % KLOG_LENGTH;
[c859753]296
[712c4ba]297 if (!ordy) {
298 if (klog_stored < klog_len)
299 klog_stored++;
300 }
301
302 /* The character is stored for uspace */
303 if (klog_uspace < klog_len)
304 klog_uspace++;
305
306 spinlock_unlock(&klog_lock);
307
308 if (ordy) {
309 /*
310 * Output the character. In this case
311 * it should be no longer buffered.
312 */
[b366a6f4]313 stdout->op->write(stdout, ch);
[712c4ba]314 } else {
[da52547]315 /*
316 * No standard output routine defined yet.
317 * The character is still stored in the kernel log
318 * for possible future output.
319 *
320 * The early_putchar() function is used to output
321 * the character for low-level debugging purposes.
322 * Note that the early_putc() function might be
323 * a no-op on certain hardware configurations.
324 */
325 early_putchar(ch);
[c859753]326 }
[82b71ef1]327
[97d42d5]328 /* Force notification on newline */
329 if (ch == '\n')
[5d0500c]330 klog_update(NULL);
[973be64e]331}
[b45c443]332
[312cc68]333/** Print using kernel facility
334 *
335 * Print to kernel log.
336 *
337 */
[a801688b]338sysarg_t sys_klog(int cmd, const void *buf, size_t size)
[312cc68]339{
340 char *data;
341 int rc;
[a801688b]342
343 switch (cmd) {
344 case KLOG_UPDATE:
345 klog_update(NULL);
346 return EOK;
347 case KLOG_WRITE:
348 case KLOG_COMMAND:
349 break;
350 default:
351 return ENOTSUP;
352 }
353
[c583970]354 if (size > PAGE_SIZE)
[96b02eb9]355 return (sysarg_t) ELIMIT;
[312cc68]356
[c583970]357 if (size > 0) {
358 data = (char *) malloc(size + 1, 0);
[312cc68]359 if (!data)
[96b02eb9]360 return (sysarg_t) ENOMEM;
[312cc68]361
[c583970]362 rc = copy_from_uspace(data, buf, size);
[312cc68]363 if (rc) {
364 free(data);
[96b02eb9]365 return (sysarg_t) rc;
[312cc68]366 }
[c583970]367 data[size] = 0;
[312cc68]368
[297cb73]369 switch (cmd) {
370 case KLOG_WRITE:
371 printf("%s", data);
372 break;
373 case KLOG_COMMAND:
[11527051]374 if (!stdin)
375 break;
[297cb73]376 for (unsigned int i = 0; i < size; i++)
377 indev_push_character(stdin, data[i]);
378 indev_push_character(stdin, '\n');
379 break;
380 }
381
[312cc68]382 free(data);
[297cb73]383 }
384
[c583970]385 return size;
[312cc68]386}
387
[06e1e95]388/** @}
[b45c443]389 */
Note: See TracBrowser for help on using the repository browser.