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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e90cfa6 was e90cfa6, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Clean up some atomic initializations

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