source: mainline/kernel/generic/src/console/console.c@ 81983e3

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

explicit typecasts for better code readability

  • Property mode set to 100644
File size: 8.0 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 */
[98000fb]64static size_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 */
[a71c158]73SPINLOCK_STATIC_INITIALIZE_NAME(klog_lock, "*klog_lock");
[82b71ef1]74
75/** Physical memory area used for klog buffer */
76static parea_t klog_parea;
[516ff92]77
[b9c7425]78static indev_t stdin_sink;
79static outdev_t stdout_source;
80
[44b7783]81static indev_operations_t stdin_ops = {
82 .poll = NULL
83};
84
[b9c7425]85static void stdout_write(outdev_t *dev, wchar_t ch, bool silent);
[a71c158]86static void stdout_redraw(outdev_t *dev);
[b9c7425]87
88static outdev_operations_t stdout_ops = {
[a71c158]89 .write = stdout_write,
90 .redraw = stdout_redraw
[b9c7425]91};
92
[44b7783]93/** Silence output */
94bool silent = false;
95
[ac8e7a9]96/** Standard input and output character devices */
97indev_t *stdin = NULL;
98outdev_t *stdout = NULL;
[411b6a6]99
[44b7783]100indev_t *stdin_wire(void)
101{
102 if (stdin == NULL) {
[b9c7425]103 indev_initialize("stdin", &stdin_sink, &stdin_ops);
104 stdin = &stdin_sink;
[44b7783]105 }
106
107 return stdin;
108}
109
[b9c7425]110void stdout_wire(outdev_t *outdev)
111{
112 if (stdout == NULL) {
113 outdev_initialize("stdout", &stdout_source, &stdout_ops);
114 stdout = &stdout_source;
115 }
116
117 list_append(&outdev->link, &stdout->list);
118}
119
120static void stdout_write(outdev_t *dev, wchar_t ch, bool silent)
121{
122 link_t *cur;
123
124 for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
125 outdev_t *sink = list_get_instance(cur, outdev_t, link);
[a71c158]126 if ((sink) && (sink->op->write))
127 sink->op->write(sink, ch, silent);
128 }
129}
130
131static void stdout_redraw(outdev_t *dev)
132{
133 link_t *cur;
134
135 for (cur = dev->list.next; cur != &dev->list; cur = cur->next) {
136 outdev_t *sink = list_get_instance(cur, outdev_t, link);
137 if ((sink) && (sink->op->redraw))
138 sink->op->redraw(sink);
[b9c7425]139 }
140}
141
[82b71ef1]142/** Initialize kernel logging facility
143 *
144 * The shared area contains kernel cyclic buffer. Userspace application may
145 * be notified on new data with indication of position and size
146 * of the data within the circular buffer.
[ac8e7a9]147 *
[82b71ef1]148 */
149void klog_init(void)
150{
151 void *faddr = (void *) KA2PA(klog);
152
153 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
154
155 klog_parea.pbase = (uintptr_t) faddr;
[eec616b]156 klog_parea.frames = SIZE2FRAMES(sizeof(klog));
[82b71ef1]157 ddi_parea_register(&klog_parea);
[ac8e7a9]158
[82b71ef1]159 sysinfo_set_item_val("klog.faddr", NULL, (unative_t) faddr);
[13a638d]160 sysinfo_set_item_val("klog.pages", NULL, KLOG_PAGES);
[ac8e7a9]161
[82b71ef1]162 spinlock_lock(&klog_lock);
163 klog_inited = true;
164 spinlock_unlock(&klog_lock);
165}
166
[516ff92]167void grab_console(void)
168{
[422fd81]169 bool prev = silent;
170
[516ff92]171 silent = false;
[a71c158]172 if ((stdout) && (stdout->op->redraw))
173 stdout->op->redraw(stdout);
[402de0c]174
175 /* Force the console to print the prompt */
[422fd81]176 if ((stdin) && (prev))
[402de0c]177 indev_push_character(stdin, '\n');
[516ff92]178}
179
180void release_console(void)
181{
[a71c158]182 // FIXME arch_release_console
[516ff92]183 silent = true;
184}
185
[312cc68]186/** Tell kernel to get keyboard/console access again */
187unative_t sys_debug_enable_console(void)
188{
189#ifdef CONFIG_KCONSOLE
190 grab_console();
191 return true;
192#else
193 return false;
194#endif
195}
196
197/** Tell kernel to relinquish keyboard/console access */
198unative_t sys_debug_disable_console(void)
199{
200 release_console();
201 return true;
202}
203
[ac8e7a9]204/** Get string from input character device.
[2677758]205 *
[ac8e7a9]206 * Read characters from input character device until first occurrence
[2677758]207 * of newline character.
208 *
[ac8e7a9]209 * @param indev Input character device.
[c583970]210 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]211 * @param buflen Size of the buffer.
[ff3b3197]212 *
213 * @return Number of characters read.
[ac8e7a9]214 *
[2677758]215 */
[98000fb]216size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]217{
[c583970]218 size_t offset = 0;
[98000fb]219 size_t count = 0;
[c583970]220 buf[offset] = 0;
[516ff92]221
[c583970]222 wchar_t ch;
[44b7783]223 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]224 if (ch == '\b') {
[c583970]225 if (count > 0) {
226 /* Space, backspace, space */
[e8a9dc3]227 putchar('\b');
228 putchar(' ');
229 putchar('\b');
[c583970]230
231 count--;
232 offset = str_lsize(buf, count);
233 buf[offset] = 0;
[e8a9dc3]234 }
[2677758]235 }
[c583970]236 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
237 putchar(ch);
238 count++;
239 buf[offset] = 0;
240 }
[2677758]241 }
[ac8e7a9]242
[c583970]243 return count;
[2677758]244}
245
[ac8e7a9]246/** Get character from input device & echo it to screen */
[c583970]247wchar_t getc(indev_t *indev)
[2677758]248{
[44b7783]249 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]250 putchar(ch);
[2677758]251 return ch;
252}
[973be64e]253
[82b71ef1]254void klog_update(void)
255{
256 spinlock_lock(&klog_lock);
257
[18251cc]258 if ((klog_inited) && (event_is_subscribed(EVENT_KLOG)) && (klog_uspace > 0)) {
[05641a9e]259 event_notify_3(EVENT_KLOG, klog_start, klog_len, klog_uspace);
260 klog_uspace = 0;
261 }
[82b71ef1]262
263 spinlock_unlock(&klog_lock);
264}
265
[eec616b]266void putchar(const wchar_t ch)
[973be64e]267{
[82b71ef1]268 spinlock_lock(&klog_lock);
269
[ac8e7a9]270 if ((klog_stored > 0) && (stdout) && (stdout->op->write)) {
[c859753]271 /* Print charaters stored in kernel log */
[98000fb]272 size_t i;
[c859753]273 for (i = klog_len - klog_stored; i < klog_len; i++)
[171f9a1]274 stdout->op->write(stdout, klog[(klog_start + i) % KLOG_LENGTH], silent);
[c859753]275 klog_stored = 0;
276 }
277
278 /* Store character in the cyclic kernel log */
[171f9a1]279 klog[(klog_start + klog_len) % KLOG_LENGTH] = ch;
280 if (klog_len < KLOG_LENGTH)
[c859753]281 klog_len++;
282 else
[171f9a1]283 klog_start = (klog_start + 1) % KLOG_LENGTH;
[c859753]284
[ac8e7a9]285 if ((stdout) && (stdout->op->write))
[eec616b]286 stdout->op->write(stdout, ch, silent);
[c859753]287 else {
288 /* The character is just in the kernel log */
289 if (klog_stored < klog_len)
290 klog_stored++;
291 }
[82b71ef1]292
293 /* The character is stored for uspace */
294 if (klog_uspace < klog_len)
295 klog_uspace++;
296
[c05a50f]297 /* Check notify uspace to update */
298 bool update;
[eec616b]299 if ((klog_uspace > KLOG_LATENCY) || (ch == '\n'))
[c05a50f]300 update = true;
301 else
302 update = false;
303
[82b71ef1]304 spinlock_unlock(&klog_lock);
305
[c05a50f]306 if (update)
307 klog_update();
[973be64e]308}
[b45c443]309
[312cc68]310/** Print using kernel facility
311 *
312 * Print to kernel log.
313 *
314 */
[c583970]315unative_t sys_klog(int fd, const void *buf, size_t size)
[312cc68]316{
317 char *data;
318 int rc;
[eec616b]319
[c583970]320 if (size > PAGE_SIZE)
[9c70ed6]321 return (unative_t) ELIMIT;
[312cc68]322
[c583970]323 if (size > 0) {
324 data = (char *) malloc(size + 1, 0);
[312cc68]325 if (!data)
[9c70ed6]326 return (unative_t) ENOMEM;
[312cc68]327
[c583970]328 rc = copy_from_uspace(data, buf, size);
[312cc68]329 if (rc) {
330 free(data);
[9c70ed6]331 return (unative_t) rc;
[312cc68]332 }
[c583970]333 data[size] = 0;
[312cc68]334
335 printf("%s", data);
336 free(data);
337 } else
338 klog_update();
339
[c583970]340 return size;
[312cc68]341}
342
[06e1e95]343/** @}
[b45c443]344 */
Note: See TracBrowser for help on using the repository browser.