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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1c57a8 was b1c57a8, checked in by Jakub Jermar <jakub@…>, 11 years ago

Merge from lp:~adam-hraska+lp/helenos/rcu/.

Only merge from the feature branch and resolve all conflicts.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * Copyright (c) 2003 Josef Cejka
3 * Copyright (c) 2005 Jakub Jermar
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
30/** @addtogroup genericconsole
31 * @{
32 */
33/** @file
34 */
35
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 <typedefs.h>
42#include <ddi/irq.h>
43#include <ddi/ddi.h>
44#include <ipc/event.h>
45#include <ipc/irq.h>
46#include <arch.h>
47#include <panic.h>
48#include <print.h>
49#include <putchar.h>
50#include <atomic.h>
51#include <syscall/copy.h>
52#include <errno.h>
53#include <str.h>
54#include <abi/kio.h>
55#include <mm/frame.h> /* SIZE2FRAMES */
56#include <mm/slab.h> /* malloc */
57
58#define KIO_PAGES 8
59#define KIO_LENGTH (KIO_PAGES * PAGE_SIZE / sizeof(wchar_t))
60
61/** Kernel log cyclic buffer */
62wchar_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
63
64/** Kernel log initialized */
65static atomic_t kio_inited = {false};
66
67/** First kernel log characters */
68static size_t kio_start = 0;
69
70/** Number of valid kernel log characters */
71static size_t kio_len = 0;
72
73/** Number of stored (not printed) kernel log characters */
74static size_t kio_stored = 0;
75
76/** Number of stored kernel log characters for uspace */
77static size_t kio_uspace = 0;
78
79/** Kernel log spinlock */
80SPINLOCK_INITIALIZE_NAME(kio_lock, "kio_lock");
81
82/** Physical memory area used for kio buffer */
83static parea_t kio_parea;
84
85static indev_t stdin_sink;
86static outdev_t stdout_source;
87
88static void stdin_signal(indev_t *, indev_signal_t);
89
90static indev_operations_t stdin_ops = {
91 .poll = NULL,
92 .signal = stdin_signal
93};
94
95static void stdout_write(outdev_t *, wchar_t);
96static void stdout_redraw(outdev_t *);
97static void stdout_scroll_up(outdev_t *);
98static void stdout_scroll_down(outdev_t *);
99
100static outdev_operations_t stdout_ops = {
101 .write = stdout_write,
102 .redraw = stdout_redraw,
103 .scroll_up = stdout_scroll_up,
104 .scroll_down = stdout_scroll_down
105};
106
107/** Override kernel console lockout */
108bool console_override = false;
109
110/** Standard input and output character devices */
111indev_t *stdin = NULL;
112outdev_t *stdout = NULL;
113
114indev_t *stdin_wire(void)
115{
116 if (stdin == NULL) {
117 indev_initialize("stdin", &stdin_sink, &stdin_ops);
118 stdin = &stdin_sink;
119 }
120
121 return stdin;
122}
123
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
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
148static void stdout_write(outdev_t *dev, wchar_t ch)
149{
150 list_foreach(dev->list, link, outdev_t, sink) {
151 if ((sink) && (sink->op->write))
152 sink->op->write(sink, ch);
153 }
154}
155
156static void stdout_redraw(outdev_t *dev)
157{
158 list_foreach(dev->list, link, outdev_t, sink) {
159 if ((sink) && (sink->op->redraw))
160 sink->op->redraw(sink);
161 }
162}
163
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
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.
185 *
186 */
187void kio_init(void)
188{
189 void *faddr = (void *) KA2PA(kio);
190
191 ASSERT((uintptr_t) faddr % FRAME_SIZE == 0);
192
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);
198
199 sysinfo_set_item_val("kio.faddr", NULL, (sysarg_t) faddr);
200 sysinfo_set_item_val("kio.pages", NULL, KIO_PAGES);
201
202 event_set_unmask_callback(EVENT_KIO, kio_update);
203 atomic_set(&kio_inited, true);
204}
205
206void grab_console(void)
207{
208 event_notify_1(EVENT_KCONSOLE, false, true);
209 bool prev = console_override;
210
211 console_override = true;
212 if ((stdout) && (stdout->op->redraw))
213 stdout->op->redraw(stdout);
214
215 if ((stdin) && (!prev)) {
216 /*
217 * Force the console to print the prompt.
218 */
219 indev_push_character(stdin, '\n');
220 }
221}
222
223void release_console(void)
224{
225 console_override = false;
226 event_notify_1(EVENT_KCONSOLE, false, false);
227}
228
229/** Activate kernel console override */
230sysarg_t sys_debug_console(void)
231{
232#ifdef CONFIG_KCONSOLE
233 grab_console();
234 return true;
235#else
236 return false;
237#endif
238}
239
240/** Get string from input character device.
241 *
242 * Read characters from input character device until first occurrence
243 * of newline character.
244 *
245 * @param indev Input character device.
246 * @param buf Buffer where to store string terminated by NULL.
247 * @param buflen Size of the buffer.
248 *
249 * @return Number of characters read.
250 *
251 */
252size_t gets(indev_t *indev, char *buf, size_t buflen)
253{
254 size_t offset = 0;
255 size_t count = 0;
256 buf[offset] = 0;
257
258 wchar_t ch;
259 while ((ch = indev_pop_character(indev)) != '\n') {
260 if (ch == '\b') {
261 if (count > 0) {
262 /* Space, backspace, space */
263 putchar('\b');
264 putchar(' ');
265 putchar('\b');
266
267 count--;
268 offset = str_lsize(buf, count);
269 buf[offset] = 0;
270 }
271 }
272
273 if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
274 putchar(ch);
275 count++;
276 buf[offset] = 0;
277 }
278 }
279
280 return count;
281}
282
283/** Get character from input device & echo it to screen */
284wchar_t getc(indev_t *indev)
285{
286 wchar_t ch = indev_pop_character(indev);
287 putchar(ch);
288 return ch;
289}
290
291void kio_update(void *event)
292{
293 if (!atomic_get(&kio_inited))
294 return;
295
296 spinlock_lock(&kio_lock);
297
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;
302 }
303
304 spinlock_unlock(&kio_lock);
305}
306
307/** Flush characters that are stored in the output buffer
308 *
309 */
310void kio_flush(void)
311{
312 bool ordy = ((stdout) && (stdout->op->write));
313
314 if (!ordy)
315 return;
316
317 spinlock_lock(&kio_lock);
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);
332 }
333
334 spinlock_unlock(&kio_lock);
335}
336
337/** Put a character into the output buffer.
338 *
339 * The caller is required to hold kio_lock
340 */
341void kio_push_char(const wchar_t ch)
342{
343 kio[(kio_start + kio_len) % KIO_LENGTH] = ch;
344 if (kio_len < KIO_LENGTH)
345 kio_len++;
346 else
347 kio_start = (kio_start + 1) % KIO_LENGTH;
348
349 if (kio_stored < kio_len)
350 kio_stored++;
351
352 /* The character is stored for uspace */
353 if (kio_uspace < kio_len)
354 kio_uspace++;
355}
356
357void putchar(const wchar_t ch)
358{
359 bool ordy = ((stdout) && (stdout->op->write));
360
361 spinlock_lock(&kio_lock);
362 kio_push_char(ch);
363 spinlock_unlock(&kio_lock);
364
365 /* Output stored characters */
366 kio_flush();
367
368 if (!ordy) {
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);
380 }
381
382 /* Force notification on newline */
383 if (ch == '\n')
384 kio_update(NULL);
385}
386
387/** Print using kernel facility
388 *
389 * Print to kernel log.
390 *
391 */
392sysarg_t sys_kio(int cmd, const void *buf, size_t size)
393{
394 char *data;
395 int rc;
396
397 switch (cmd) {
398 case KIO_UPDATE:
399 kio_update(NULL);
400 return EOK;
401 case KIO_WRITE:
402 case KIO_COMMAND:
403 break;
404 default:
405 return ENOTSUP;
406 }
407
408 if (size > PAGE_SIZE)
409 return (sysarg_t) ELIMIT;
410
411 if (size > 0) {
412 data = (char *) malloc(size + 1, 0);
413 if (!data)
414 return (sysarg_t) ENOMEM;
415
416 rc = copy_from_uspace(data, buf, size);
417 if (rc) {
418 free(data);
419 return (sysarg_t) rc;
420 }
421 data[size] = 0;
422
423 switch (cmd) {
424 case KIO_WRITE:
425 printf("%s", data);
426 break;
427 case KIO_COMMAND:
428 if (!stdin)
429 break;
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
436 free(data);
437 }
438
439 return size;
440}
441
442/** @}
443 */
Note: See TracBrowser for help on using the repository browser.