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

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

allow compositor and console to coexist side-by-side, use the input server as a poor man's seat arbitrator

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