source: mainline/kernel/generic/src/console/console.c@ 402de0c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 402de0c was 402de0c, checked in by Martin Decky <martin@…>, 16 years ago

force to kernel console to always print the prompt when switching to it from uspace

  • Property mode set to 100644
File size: 6.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
[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>
41#include <arch/types.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>
[93b84b3]47#include <print.h>
[eec616b]48#include <putchar.h>
[23684b7]49#include <atomic.h>
[312cc68]50#include <syscall/copy.h>
51#include <errno.h>
[c583970]52#include <string.h>
[2677758]53
[13a638d]54#define KLOG_PAGES 4
55#define KLOG_LENGTH (KLOG_PAGES * PAGE_SIZE / sizeof(wchar_t))
[eec616b]56#define KLOG_LATENCY 8
[c859753]57
[c0855a0]58/** Kernel log cyclic buffer */
[171f9a1]59static wchar_t klog[KLOG_LENGTH] __attribute__ ((aligned (PAGE_SIZE)));
[c859753]60
[c0855a0]61/** Kernel log initialized */
[82b71ef1]62static bool klog_inited = false;
[c0855a0]63/** First kernel log characters */
[c859753]64static index_t klog_start = 0;
[c0855a0]65/** Number of valid kernel log characters */
[c859753]66static size_t klog_len = 0;
[c0855a0]67/** Number of stored (not printed) kernel log characters */
[c859753]68static size_t klog_stored = 0;
[c0855a0]69/** Number of stored kernel log characters for uspace */
[82b71ef1]70static size_t klog_uspace = 0;
71
[c0855a0]72/** Kernel log spinlock */
[82b71ef1]73SPINLOCK_INITIALIZE(klog_lock);
74
75/** Physical memory area used for klog buffer */
76static parea_t klog_parea;
[516ff92]77
[44b7783]78static indev_operations_t stdin_ops = {
79 .poll = NULL
80};
81
82/** Silence output */
83bool silent = false;
84
[ac8e7a9]85/** Standard input and output character devices */
86indev_t *stdin = NULL;
87outdev_t *stdout = NULL;
[411b6a6]88
[44b7783]89indev_t *stdin_wire(void)
90{
91 if (stdin == NULL) {
92 stdin = malloc(sizeof(indev_t), FRAME_ATOMIC);
93 if (stdin != NULL)
94 indev_initialize("stdin", stdin, &stdin_ops);
95 }
96
97 return stdin;
98}
99
[82b71ef1]100/** Initialize kernel logging facility
101 *
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.
[ac8e7a9]105 *
[82b71ef1]106 */
107void klog_init(void)
108{
109 void *faddr = (void *) KA2PA(klog);
110
111 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
112
113 klog_parea.pbase = (uintptr_t) faddr;
[eec616b]114 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
[82b71ef1]115 ddi_parea_register(&klog_parea);
[ac8e7a9]116
[82b71ef1]117 sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
[13a638d]118 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
[ac8e7a9]119
[82b71ef1]120 spinlock_lock(&klog_lock);
121 klog_inited = true;
122 spinlock_unlock(&klog_lock);
123}
124
[516ff92]125void grab_console(void)
126{
127 silent = false;
128 arch_grab_console();
[402de0c]129
130 /* Force the console to print the prompt */
131 if (stdin)
132 indev_push_character(stdin, '\n');
[516ff92]133}
134
135void release_console(void)
136{
137 silent = true;
138 arch_release_console();
139}
140
[312cc68]141/** Tell kernel to get keyboard/console access again */
142unative_t sys_debug_enable_console(void)
143{
144#ifdef CONFIG_KCONSOLE
145 grab_console();
146 return true;
147#else
148 return false;
149#endif
150}
151
152/** Tell kernel to relinquish keyboard/console access */
153unative_t sys_debug_disable_console(void)
154{
155 release_console();
156 return true;
157}
158
[ac8e7a9]159/** Get string from input character device.
[2677758]160 *
[ac8e7a9]161 * Read characters from input character device until first occurrence
[2677758]162 * of newline character.
163 *
[ac8e7a9]164 * @param indev Input character device.
[c583970]165 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]166 * @param buflen Size of the buffer.
[ff3b3197]167 *
168 * @return Number of characters read.
[ac8e7a9]169 *
[2677758]170 */
[ac8e7a9]171count_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]172{
[c583970]173 size_t offset = 0;
174 count_t count = 0;
175 buf[offset] = 0;
[516ff92]176
[c583970]177 wchar_t ch;
[44b7783]178 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]179 if (ch == '\b') {
[c583970]180 if (count > 0) {
181 /* Space, backspace, space */
[e8a9dc3]182 putchar('\b');
183 putchar(' ');
184 putchar('\b');
[c583970]185
186 count--;
187 offset = str_lsize(buf, count);
188 buf[offset] = 0;
[e8a9dc3]189 }
[2677758]190 }
[c583970]191 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
192 putchar(ch);
193 count++;
194 buf[offset] = 0;
195 }
[2677758]196 }
[ac8e7a9]197
[c583970]198 return count;
[2677758]199}
200
[ac8e7a9]201/** Get character from input device & echo it to screen */
[c583970]202wchar_t getc(indev_t *indev)
[2677758]203{
[44b7783]204 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]205 putchar(ch);
[2677758]206 return ch;
207}
[973be64e]208
[82b71ef1]209void klog_update(void)
210{
211 spinlock_lock(&klog_lock);
212
[18251cc]213 if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
[05641a9e]214 event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
215 klog_uspace = 0;
216 }
[82b71ef1]217
218 spinlock_unlock(&klog_lock);
219}
220
[eec616b]221void putchar(const wchar_t ch)
[973be64e]222{
[82b71ef1]223 spinlock_lock(&klog_lock);
224
[ac8e7a9]225 if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
[c859753]226 /* Print charaters stored in kernel log */
227 index_t i;
228 for (i = klog_len - klog_stored; i < klog_len; i++)
[171f9a1]229 stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
[c859753]230 klog_stored = 0;
231 }
232
233 /* Store character in the cyclic kernel log */
[171f9a1]234 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
235 if (klog_len < KLOG_LENGTH)
[c859753]236 klog_len++;
237 else
[171f9a1]238 klog_start = (klog_start + 1) % KLOG_LENGTH;
[c859753]239
[ac8e7a9]240 if ((stdout) && (stdout->op->write))
[eec616b]241 stdout->op->write(stdout, ch, silent);
[c859753]242 else {
243 /* The character is just in the kernel log */
244 if (klog_stored < klog_len)
245 klog_stored++;
246 }
[82b71ef1]247
248 /* The character is stored for uspace */
249 if (klog_uspace < klog_len)
250 klog_uspace++;
251
[c05a50f]252 /* Check notify uspace to update */
253 bool update;
[eec616b]254 if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
[c05a50f]255 update = true;
256 else
257 update = false;
258
[82b71ef1]259 spinlock_unlock(&klog_lock);
260
[c05a50f]261 if (update)
262 klog_update();
[973be64e]263}
[b45c443]264
[312cc68]265/** Print using kernel facility
266 *
267 * Print to kernel log.
268 *
269 */
[c583970]270unative_t sys_klog(int fd, const void *buf, size_t size)
[312cc68]271{
272 char *data;
273 int rc;
[eec616b]274
[c583970]275 if (size > PAGE_SIZE)
[312cc68]276 return ELIMIT;
277
[c583970]278 if (size > 0) {
279 data = (char *) malloc(size + 1, 0);
[312cc68]280 if (!data)
281 return ENOMEM;
282
[c583970]283 rc = copy_from_uspace(data, buf, size);
[312cc68]284 if (rc) {
285 free(data);
286 return rc;
287 }
[c583970]288 data[size] = 0;
[312cc68]289
290 printf("%s", data);
291 free(data);
292 } else
293 klog_update();
294
[c583970]295 return size;
[312cc68]296}
297
[06e1e95]298/** @}
[b45c443]299 */
Note: See TracBrowser for help on using the repository browser.