source: mainline/kernel/generic/src/console/console.c@ 207e8880

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207e8880 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

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