2
* Copyright (c) 2003 Josef Cejka
3
* Copyright (c) 2005 Jakub Jermar
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
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.
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.
30
/** @addtogroup genericconsole
36
#include <console/console.h>
37
#include <console/chardev.h>
38
#include <sysinfo/sysinfo.h>
39
#include <synch/waitq.h>
40
#include <synch/spinlock.h>
41
#include <arch/types.h>
44
#include <ipc/event.h>
50
#include <syscall/copy.h>
55
#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
56
#define KLOG_LATENCY 8
58
/** Kernel log cyclic buffer */
59
static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE)));
61
/** Kernel log initialized */
62
static bool klog_inited = false;
63
/** First kernel log characters */
64
static size_t klog_start = 0;
65
/** Number of valid kernel log characters */
66
static size_t klog_len = 0;
67
/** Number of stored (not printed) kernel log characters */
68
static size_t klog_stored = 0;
69
/** Number of stored kernel log characters for uspace */
70
static size_t klog_uspace = 0;
72
/** Kernel log spinlock */
73
SPINLOCK_INITIALIZE(klog_lock);
75
/** Physical memory area used for klog buffer */
76
static parea_t klog_parea;
78
static indev_operations_t stdin_ops = {
85
/** Standard input and output character devices */
86
indev_t *stdin = NULL;
87
outdev_t *stdout = NULL;
89
indev_t *stdin_wire(void)
92
stdin = malloc(sizeof(indev_t), FRAME_ATOMIC);
94
indev_initialize("stdin", stdin, &stdin_ops);
100
/** Initialize kernel logging facility
102
* The shared area contains kernel cyclic buffer. Userspace application may
103
* be notified on new data with indication of position and size
104
* of the data within the circular buffer.
109
void *faddr = (void *) KA2PA(klog);
111
ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
113
klog_parea.pbase = (uintptr_t) faddr;
114
klog_parea.frames = SIZE2FRAMES(sizeof(klog));
115
ddi_parea_register(&klog_parea);
117
sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
118
sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
120
spinlock_lock(&klog_lock);
122
spinlock_unlock(&klog_lock);
125
void grab_console(void)
132
/* Force the console to print the prompt */
133
if ((stdin) && (prev))
134
indev_push_character(stdin, '\n');
137
void release_console(void)
140
arch_release_console();
143
/** Tell kernel to get keyboard/console access again */
144
unative_t sys_debug_enable_console(void)
146
#ifdef CONFIG_KCONSOLE
154
/** Tell kernel to relinquish keyboard/console access */
155
unative_t sys_debug_disable_console(void)
161
/** Get string from input character device.
163
* Read characters from input character device until first occurrence
164
* of newline character.
166
* @param indev Input character device.
167
* @param buf Buffer where to store string terminated by NULL.
168
* @param buflen Size of the buffer.
170
* @return Number of characters read.
173
size_t gets(indev_t *indev, char *buf, size_t buflen)
180
while ((ch = indev_pop_character(indev)) != '\n') {
183
/* Space, backspace, space */
189
offset = str_lsize(buf, count);
193
if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
203
/** Get character from input device & echo it to screen */
204
wchar_t getc(indev_t *indev)
206
wchar_t ch = indev_pop_character(indev);
211
void klog_update(void)
213
spinlock_lock(&klog_lock);
215
if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
216
event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
220
spinlock_unlock(&klog_lock);
223
void putchar(const wchar_t ch)
225
spinlock_lock(&klog_lock);
227
if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
228
/* Print charaters stored in kernel log */
230
for (i = klog_len - klog_stored; i < klog_len; i++)
231
stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
235
/* Store character in the cyclic kernel log */
236
klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
237
if (klog_len < KLOG_LENGTH)
240
klog_start = (klog_start + 1) % KLOG_LENGTH;
242
if ((stdout) && (stdout->op->write))
243
stdout->op->write(stdout, ch, silent);
245
/* The character is just in the kernel log */
246
if (klog_stored < klog_len)
250
/* The character is stored for uspace */
251
if (klog_uspace < klog_len)
254
/* Check notify uspace to update */
256
if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
261
spinlock_unlock(&klog_lock);
267
/** Print using kernel facility
269
* Print to kernel log.
272
unative_t sys_klog(int fd, const void *buf, size_t size)
277
if (size > PAGE_SIZE)
281
data = (char *) malloc(size + 1, 0);
285
rc = copy_from_uspace(data, buf, size);