source: mainline/kernel/generic/src/console/console.c@ 0328987

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0328987 was 6afc9d7, checked in by Jiri Svoboda <jiri@…>, 10 years ago

UNIX-like I/O functions should use errno to return error code for many reasons.

  • Property mode set to 100644
File size: 9.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>
[6fa9a99d]54#include <abi/kio.h>
[1066041]55#include <mm/frame.h> /* SIZE2FRAMES */
56#include <mm/slab.h> /* malloc */
[2677758]57
[6fa9a99d]58#define KIO_PAGES 8
59#define KIO_LENGTH (KIO_PAGES * PAGE_SIZE / sizeof(wchar_t))
[c859753]60
[c0855a0]61/** Kernel log cyclic buffer */
[6fa9a99d]62wchar_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
[c859753]63
[c0855a0]64/** Kernel log initialized */
[6fa9a99d]65static atomic_t kio_inited = {false};
[da1bafb]66
[c0855a0]67/** First kernel log characters */
[6fa9a99d]68static size_t kio_start = 0;
[da1bafb]69
[c0855a0]70/** Number of valid kernel log characters */
[6fa9a99d]71static size_t kio_len = 0;
[da1bafb]72
[c0855a0]73/** Number of stored (not printed) kernel log characters */
[6fa9a99d]74static size_t kio_stored = 0;
[da1bafb]75
[c0855a0]76/** Number of stored kernel log characters for uspace */
[6fa9a99d]77static size_t kio_uspace = 0;
[82b71ef1]78
[c0855a0]79/** Kernel log spinlock */
[91db0280]80SPINLOCK_INITIALIZE_NAME(kio_lock, "kio_lock");
[82b71ef1]81
[6fa9a99d]82/** Physical memory area used for kio buffer */
83static parea_t kio_parea;
[516ff92]84
[b9c7425]85static indev_t stdin_sink;
86static outdev_t stdout_source;
87
[7ddc2c7]88static void stdin_signal(indev_t *, indev_signal_t);
89
[44b7783]90static indev_operations_t stdin_ops = {
[7ddc2c7]91 .poll = NULL,
92 .signal = stdin_signal
[44b7783]93};
94
[b366a6f4]95static void stdout_write(outdev_t *, wchar_t);
[da1bafb]96static void stdout_redraw(outdev_t *);
[7ddc2c7]97static void stdout_scroll_up(outdev_t *);
98static void stdout_scroll_down(outdev_t *);
[b9c7425]99
100static outdev_operations_t stdout_ops = {
[a71c158]101 .write = stdout_write,
[7ddc2c7]102 .redraw = stdout_redraw,
103 .scroll_up = stdout_scroll_up,
104 .scroll_down = stdout_scroll_down
[b9c7425]105};
106
[b366a6f4]107/** Override kernel console lockout */
108bool console_override = false;
[44b7783]109
[ac8e7a9]110/** Standard input and output character devices */
111indev_t *stdin = NULL;
112outdev_t *stdout = NULL;
[411b6a6]113
[44b7783]114indev_t *stdin_wire(void)
115{
116 if (stdin == NULL) {
[b9c7425]117 indev_initialize("stdin", &stdin_sink, &stdin_ops);
118 stdin = &stdin_sink;
[44b7783]119 }
120
121 return stdin;
122}
123
[7ddc2c7]124static void stdin_signal(indev_t *indev, indev_signal_t signal)
125{
126 switch (signal) {
127 case INDEV_SIGNAL_SCROLL_UP:
128 if (stdout != NULL)
129 stdout_scroll_up(stdout);
130 break;
131 case INDEV_SIGNAL_SCROLL_DOWN:
132 if (stdout != NULL)
133 stdout_scroll_down(stdout);
134 break;
135 }
136}
137
[b9c7425]138void stdout_wire(outdev_t *outdev)
139{
140 if (stdout == NULL) {
141 outdev_initialize("stdout", &stdout_source, &stdout_ops);
142 stdout = &stdout_source;
143 }
144
145 list_append(&outdev->link, &stdout->list);
146}
147
[b366a6f4]148static void stdout_write(outdev_t *dev, wchar_t ch)
[b9c7425]149{
[feeac0d]150 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]151 if ((sink) && (sink->op->write))
[b366a6f4]152 sink->op->write(sink, ch);
[a71c158]153 }
154}
155
156static void stdout_redraw(outdev_t *dev)
157{
[feeac0d]158 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]159 if ((sink) && (sink->op->redraw))
160 sink->op->redraw(sink);
[b9c7425]161 }
162}
163
[7ddc2c7]164static void stdout_scroll_up(outdev_t *dev)
165{
166 list_foreach(dev->list, link, outdev_t, sink) {
167 if ((sink) && (sink->op->scroll_up))
168 sink->op->scroll_up(sink);
169 }
170}
171
172static void stdout_scroll_down(outdev_t *dev)
173{
174 list_foreach(dev->list, link, outdev_t, sink) {
175 if ((sink) && (sink->op->scroll_down))
176 sink->op->scroll_down(sink);
177 }
178}
179
[82b71ef1]180/** Initialize kernel logging facility
181 *
182 * The shared area contains kernel cyclic buffer. Userspace application may
183 * be notified on new data with indication of position and size
184 * of the data within the circular buffer.
[ac8e7a9]185 *
[82b71ef1]186 */
[6fa9a99d]187void kio_init(void)
[82b71ef1]188{
[6fa9a99d]189 void *faddr = (void *) KA2PA(kio);
[82b71ef1]190
191 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
192
[6fa9a99d]193 kio_parea.pbase = (uintptr_t) faddr;
194 kio_parea.frames = SIZE2FRAMES(sizeof(kio));
195 kio_parea.unpriv = false;
196 kio_parea.mapped = false;
197 ddi_parea_register(&kio_parea);
[ac8e7a9]198
[6fa9a99d]199 sysinfo_set_item_val("kio.faddr", NULL, (sysarg_t) faddr);
200 sysinfo_set_item_val("kio.pages", NULL, KIO_PAGES);
[97d42d5]201
[6fa9a99d]202 event_set_unmask_callback(EVENT_KIO, kio_update);
203 atomic_set(&kio_inited, true);
[82b71ef1]204}
205
[516ff92]206void grab_console(void)
207{
[593e023]208 event_notify_1(EVENT_KCONSOLE, false, true);
[b366a6f4]209 bool prev = console_override;
[422fd81]210
[b366a6f4]211 console_override = true;
[a71c158]212 if ((stdout) && (stdout->op->redraw))
213 stdout->op->redraw(stdout);
[402de0c]214
[b366a6f4]215 if ((stdin) && (!prev)) {
[da1bafb]216 /*
217 * Force the console to print the prompt.
218 */
[402de0c]219 indev_push_character(stdin, '\n');
[da1bafb]220 }
[516ff92]221}
222
223void release_console(void)
224{
[b366a6f4]225 console_override = false;
[593e023]226 event_notify_1(EVENT_KCONSOLE, false, false);
[516ff92]227}
228
[b366a6f4]229/** Activate kernel console override */
[f6ab787]230sysarg_t sys_debug_console(void)
[312cc68]231{
232#ifdef CONFIG_KCONSOLE
233 grab_console();
234 return true;
235#else
236 return false;
237#endif
238}
239
[ac8e7a9]240/** Get string from input character device.
[2677758]241 *
[ac8e7a9]242 * Read characters from input character device until first occurrence
[2677758]243 * of newline character.
244 *
[ac8e7a9]245 * @param indev Input character device.
[c583970]246 * @param buf Buffer where to store string terminated by NULL.
[abbc16e]247 * @param buflen Size of the buffer.
[ff3b3197]248 *
249 * @return Number of characters read.
[ac8e7a9]250 *
[2677758]251 */
[98000fb]252size_t gets(indev_t *indev, char *buf, size_t buflen)
[2677758]253{
[c583970]254 size_t offset = 0;
[98000fb]255 size_t count = 0;
[c583970]256 buf[offset] = 0;
[516ff92]257
[c583970]258 wchar_t ch;
[44b7783]259 while ((ch = indev_pop_character(indev)) != '\n') {
[e8a9dc3]260 if (ch == '\b') {
[c583970]261 if (count > 0) {
262 /* Space, backspace, space */
[e8a9dc3]263 putchar('\b');
264 putchar(' ');
265 putchar('\b');
[c583970]266
267 count--;
268 offset = str_lsize(buf, count);
269 buf[offset] = 0;
[e8a9dc3]270 }
[2677758]271 }
[7ddc2c7]272
[c583970]273 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
274 putchar(ch);
275 count++;
276 buf[offset] = 0;
277 }
[2677758]278 }
[ac8e7a9]279
[c583970]280 return count;
[2677758]281}
282
[ac8e7a9]283/** Get character from input device & echo it to screen */
[c583970]284wchar_t getc(indev_t *indev)
[2677758]285{
[44b7783]286 wchar_t ch = indev_pop_character(indev);
[e8a9dc3]287 putchar(ch);
[2677758]288 return ch;
289}
[973be64e]290
[6fa9a99d]291void kio_update(void *event)
[82b71ef1]292{
[6fa9a99d]293 if (!atomic_get(&kio_inited))
[712c4ba]294 return;
295
[6fa9a99d]296 spinlock_lock(&kio_lock);
[82b71ef1]297
[6fa9a99d]298 if (kio_uspace > 0) {
299 if (event_notify_3(EVENT_KIO, true, kio_start, kio_len,
300 kio_uspace) == EOK)
301 kio_uspace = 0;
[05641a9e]302 }
[82b71ef1]303
[6fa9a99d]304 spinlock_unlock(&kio_lock);
[82b71ef1]305}
306
[91db0280]307/** Flush characters that are stored in the output buffer
[7ddc2c7]308 *
[91db0280]309 */
310void kio_flush(void)
[973be64e]311{
[712c4ba]312 bool ordy = ((stdout) && (stdout->op->write));
313
[91db0280]314 if (!ordy)
315 return;
316
[6fa9a99d]317 spinlock_lock(&kio_lock);
[91db0280]318
319 /* Print characters that weren't printed earlier */
320 while (kio_stored > 0) {
321 wchar_t tmp = kio[(kio_start + kio_len - kio_stored) % KIO_LENGTH];
322 kio_stored--;
323
324 /*
325 * We need to give up the spinlock for
326 * the physical operation of writing out
327 * the character.
328 */
329 spinlock_unlock(&kio_lock);
330 stdout->op->write(stdout, tmp);
331 spinlock_lock(&kio_lock);
[c859753]332 }
[91db0280]333
334 spinlock_unlock(&kio_lock);
335}
336
337/** Put a character into the output buffer.
[7ddc2c7]338 *
[91db0280]339 * The caller is required to hold kio_lock
340 */
341void kio_push_char(const wchar_t ch)
342{
[6fa9a99d]343 kio[(kio_start + kio_len) % KIO_LENGTH] = ch;
344 if (kio_len < KIO_LENGTH)
345 kio_len++;
[c859753]346 else
[6fa9a99d]347 kio_start = (kio_start + 1) % KIO_LENGTH;
[c859753]348
[91db0280]349 if (kio_stored < kio_len)
350 kio_stored++;
[712c4ba]351
352 /* The character is stored for uspace */
[6fa9a99d]353 if (kio_uspace < kio_len)
354 kio_uspace++;
[91db0280]355}
356
357void putchar(const wchar_t ch)
358{
359 bool ordy = ((stdout) && (stdout->op->write));
[712c4ba]360
[91db0280]361 spinlock_lock(&kio_lock);
362 kio_push_char(ch);
[6fa9a99d]363 spinlock_unlock(&kio_lock);
[712c4ba]364
[91db0280]365 /* Output stored characters */
366 kio_flush();
367
368 if (!ordy) {
[da52547]369 /*
370 * No standard output routine defined yet.
371 * The character is still stored in the kernel log
372 * for possible future output.
373 *
374 * The early_putchar() function is used to output
375 * the character for low-level debugging purposes.
376 * Note that the early_putc() function might be
377 * a no-op on certain hardware configurations.
378 */
379 early_putchar(ch);
[c859753]380 }
[82b71ef1]381
[97d42d5]382 /* Force notification on newline */
383 if (ch == '\n')
[6fa9a99d]384 kio_update(NULL);
[973be64e]385}
[b45c443]386
[312cc68]387/** Print using kernel facility
388 *
389 * Print to kernel log.
390 *
391 */
[6fa9a99d]392sysarg_t sys_kio(int cmd, const void *buf, size_t size)
[312cc68]393{
394 char *data;
395 int rc;
[a801688b]396
397 switch (cmd) {
[6fa9a99d]398 case KIO_UPDATE:
399 kio_update(NULL);
[a801688b]400 return EOK;
[6fa9a99d]401 case KIO_WRITE:
402 case KIO_COMMAND:
[a801688b]403 break;
404 default:
405 return ENOTSUP;
406 }
407
[c583970]408 if (size > PAGE_SIZE)
[96b02eb9]409 return (sysarg_t) ELIMIT;
[312cc68]410
[c583970]411 if (size > 0) {
412 data = (char *) malloc(size + 1, 0);
[312cc68]413 if (!data)
[96b02eb9]414 return (sysarg_t) ENOMEM;
[312cc68]415
[c583970]416 rc = copy_from_uspace(data, buf, size);
[312cc68]417 if (rc) {
418 free(data);
[96b02eb9]419 return (sysarg_t) rc;
[312cc68]420 }
[c583970]421 data[size] = 0;
[312cc68]422
[297cb73]423 switch (cmd) {
[6fa9a99d]424 case KIO_WRITE:
[297cb73]425 printf("%s", data);
426 break;
[6fa9a99d]427 case KIO_COMMAND:
[11527051]428 if (!stdin)
429 break;
[297cb73]430 for (unsigned int i = 0; i < size; i++)
431 indev_push_character(stdin, data[i]);
432 indev_push_character(stdin, '\n');
433 break;
434 }
435
[312cc68]436 free(data);
[297cb73]437 }
438
[6afc9d7]439 return EOK;
[312cc68]440}
441
[06e1e95]442/** @}
[b45c443]443 */
Note: See TracBrowser for help on using the repository browser.