source: mainline/kernel/generic/src/console/console.c@ 8165a7a

Last change on this file since 8165a7a was 90dd8aee, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 months ago

Introduce a console lock to prevent line splitting in kernel output

It is a common occurrence to see broken lines on screen or in serial
output due to bad timing. This is an attempt to prevent that without
breaking anything.

It could be considered a stopgap solution, since the whole console
stack deserves a better/faster implementation.

  • 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
[174156fd]30/** @addtogroup kernel_generic_console
[b45c443]31 * @{
32 */
33/** @file
34 */
35
[90dd8aee]36#include <abi/kio.h>
37#include <arch.h>
[63e27ef]38#include <assert.h>
[90dd8aee]39#include <atomic.h>
[2677758]40#include <console/chardev.h>
[90dd8aee]41#include <console/console.h>
[82b71ef1]42#include <ddi/ddi.h>
[90dd8aee]43#include <ddi/irq.h>
44#include <errno.h>
[13a638d]45#include <ipc/event.h>
[82b71ef1]46#include <ipc/irq.h>
[90dd8aee]47#include <mm/frame.h> /* SIZE2FRAMES */
[b8da2a3]48#include <panic.h>
[90dd8aee]49#include <preemption.h>
50#include <proc/thread.h>
[eec616b]51#include <putchar.h>
[e90cfa6]52#include <stdatomic.h>
[90dd8aee]53#include <stdio.h>
[aafed15]54#include <stdlib.h> /* malloc */
[90dd8aee]55#include <str.h>
56#include <synch/mutex.h>
57#include <synch/spinlock.h>
58#include <synch/waitq.h>
59#include <syscall/copy.h>
60#include <sysinfo/sysinfo.h>
61#include <typedefs.h>
[2677758]62
[6fa9a99d]63#define KIO_PAGES 8
[28a5ebd]64#define KIO_LENGTH (KIO_PAGES * PAGE_SIZE / sizeof(char32_t))
[c859753]65
[c0855a0]66/** Kernel log cyclic buffer */
[28a5ebd]67char32_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
[c859753]68
[c0855a0]69/** Kernel log initialized */
[c88d91e8]70static atomic_bool kio_inited = ATOMIC_VAR_INIT(false);
[da1bafb]71
[90dd8aee]72/** A mutex for preventing interleaving of output lines from different threads.
73 * May not be held in some circumstances, so locking of any internal shared
74 * structures is still necessary.
75 */
76static MUTEX_INITIALIZE(console_mutex, MUTEX_RECURSIVE);
77
[c0855a0]78/** First kernel log characters */
[6fa9a99d]79static size_t kio_start = 0;
[da1bafb]80
[c0855a0]81/** Number of valid kernel log characters */
[6fa9a99d]82static size_t kio_len = 0;
[da1bafb]83
[c0855a0]84/** Number of stored (not printed) kernel log characters */
[6fa9a99d]85static size_t kio_stored = 0;
[da1bafb]86
[c0855a0]87/** Number of stored kernel log characters for uspace */
[6fa9a99d]88static size_t kio_uspace = 0;
[82b71ef1]89
[c0855a0]90/** Kernel log spinlock */
[91db0280]91SPINLOCK_INITIALIZE_NAME(kio_lock, "kio_lock");
[82b71ef1]92
[6fa9a99d]93/** Physical memory area used for kio buffer */
94static parea_t kio_parea;
[516ff92]95
[b9c7425]96static indev_t stdin_sink;
97static outdev_t stdout_source;
98
[7ddc2c7]99static void stdin_signal(indev_t *, indev_signal_t);
100
[44b7783]101static indev_operations_t stdin_ops = {
[7ddc2c7]102 .poll = NULL,
103 .signal = stdin_signal
[44b7783]104};
105
[28a5ebd]106static void stdout_write(outdev_t *, char32_t);
[da1bafb]107static void stdout_redraw(outdev_t *);
[7ddc2c7]108static void stdout_scroll_up(outdev_t *);
109static void stdout_scroll_down(outdev_t *);
[b9c7425]110
111static outdev_operations_t stdout_ops = {
[a71c158]112 .write = stdout_write,
[7ddc2c7]113 .redraw = stdout_redraw,
114 .scroll_up = stdout_scroll_up,
115 .scroll_down = stdout_scroll_down
[b9c7425]116};
117
[b366a6f4]118/** Override kernel console lockout */
119bool console_override = false;
[44b7783]120
[ac8e7a9]121/** Standard input and output character devices */
122indev_t *stdin = NULL;
123outdev_t *stdout = NULL;
[411b6a6]124
[44b7783]125indev_t *stdin_wire(void)
126{
127 if (stdin == NULL) {
[b9c7425]128 indev_initialize("stdin", &stdin_sink, &stdin_ops);
129 stdin = &stdin_sink;
[44b7783]130 }
[a35b458]131
[44b7783]132 return stdin;
133}
134
[7ddc2c7]135static void stdin_signal(indev_t *indev, indev_signal_t signal)
136{
137 switch (signal) {
138 case INDEV_SIGNAL_SCROLL_UP:
139 if (stdout != NULL)
140 stdout_scroll_up(stdout);
141 break;
142 case INDEV_SIGNAL_SCROLL_DOWN:
143 if (stdout != NULL)
144 stdout_scroll_down(stdout);
145 break;
146 }
147}
148
[b9c7425]149void stdout_wire(outdev_t *outdev)
150{
151 if (stdout == NULL) {
152 outdev_initialize("stdout", &stdout_source, &stdout_ops);
153 stdout = &stdout_source;
154 }
[a35b458]155
[b9c7425]156 list_append(&outdev->link, &stdout->list);
157}
158
[28a5ebd]159static void stdout_write(outdev_t *dev, char32_t ch)
[b9c7425]160{
[feeac0d]161 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]162 if ((sink) && (sink->op->write))
[b366a6f4]163 sink->op->write(sink, ch);
[a71c158]164 }
165}
166
167static void stdout_redraw(outdev_t *dev)
168{
[feeac0d]169 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]170 if ((sink) && (sink->op->redraw))
171 sink->op->redraw(sink);
[b9c7425]172 }
173}
174
[7ddc2c7]175static void stdout_scroll_up(outdev_t *dev)
176{
177 list_foreach(dev->list, link, outdev_t, sink) {
178 if ((sink) && (sink->op->scroll_up))
179 sink->op->scroll_up(sink);
180 }
181}
182
183static void stdout_scroll_down(outdev_t *dev)
184{
185 list_foreach(dev->list, link, outdev_t, sink) {
186 if ((sink) && (sink->op->scroll_down))
187 sink->op->scroll_down(sink);
188 }
189}
190
[82b71ef1]191/** Initialize kernel logging facility
192 *
193 * The shared area contains kernel cyclic buffer. Userspace application may
194 * be notified on new data with indication of position and size
195 * of the data within the circular buffer.
[ac8e7a9]196 *
[82b71ef1]197 */
[6fa9a99d]198void kio_init(void)
[82b71ef1]199{
[6fa9a99d]200 void *faddr = (void *) KA2PA(kio);
[a35b458]201
[63e27ef]202 assert((uintptr_t) faddr % FRAME_SIZE == 0);
[a35b458]203
[6f7071b]204 ddi_parea_init(&kio_parea);
[6fa9a99d]205 kio_parea.pbase = (uintptr_t) faddr;
206 kio_parea.frames = SIZE2FRAMES(sizeof(kio));
207 kio_parea.unpriv = false;
208 kio_parea.mapped = false;
209 ddi_parea_register(&kio_parea);
[a35b458]210
[6fa9a99d]211 sysinfo_set_item_val("kio.faddr", NULL, (sysarg_t) faddr);
212 sysinfo_set_item_val("kio.pages", NULL, KIO_PAGES);
[a35b458]213
[6fa9a99d]214 event_set_unmask_callback(EVENT_KIO, kio_update);
[e3306d04]215 atomic_store(&kio_inited, true);
[82b71ef1]216}
217
[516ff92]218void grab_console(void)
219{
[392f0e7]220 sysinfo_set_item_val("kconsole", NULL, true);
[593e023]221 event_notify_1(EVENT_KCONSOLE, false, true);
[b366a6f4]222 bool prev = console_override;
[a35b458]223
[b366a6f4]224 console_override = true;
[a71c158]225 if ((stdout) && (stdout->op->redraw))
226 stdout->op->redraw(stdout);
[a35b458]227
[b366a6f4]228 if ((stdin) && (!prev)) {
[da1bafb]229 /*
230 * Force the console to print the prompt.
231 */
[402de0c]232 indev_push_character(stdin, '\n');
[da1bafb]233 }
[516ff92]234}
235
236void release_console(void)
237{
[392f0e7]238 sysinfo_set_item_val("kconsole", NULL, false);
[b366a6f4]239 console_override = false;
[593e023]240 event_notify_1(EVENT_KCONSOLE, false, false);
[516ff92]241}
242
[b366a6f4]243/** Activate kernel console override */
[f6ab787]244sysarg_t sys_debug_console(void)
[312cc68]245{
246#ifdef CONFIG_KCONSOLE
247 grab_console();
248 return true;
249#else
250 return false;
251#endif
252}
253
[6fa9a99d]254void kio_update(void *event)
[82b71ef1]255{
[036e97c]256 if (!atomic_load(&kio_inited))
[712c4ba]257 return;
[a35b458]258
[6fa9a99d]259 spinlock_lock(&kio_lock);
[a35b458]260
[6fa9a99d]261 if (kio_uspace > 0) {
262 if (event_notify_3(EVENT_KIO, true, kio_start, kio_len,
263 kio_uspace) == EOK)
264 kio_uspace = 0;
[05641a9e]265 }
[a35b458]266
[6fa9a99d]267 spinlock_unlock(&kio_lock);
[82b71ef1]268}
269
[91db0280]270/** Flush characters that are stored in the output buffer
[7ddc2c7]271 *
[91db0280]272 */
273void kio_flush(void)
[973be64e]274{
[712c4ba]275 bool ordy = ((stdout) && (stdout->op->write));
[a35b458]276
[91db0280]277 if (!ordy)
278 return;
279
[6fa9a99d]280 spinlock_lock(&kio_lock);
[91db0280]281
282 /* Print characters that weren't printed earlier */
283 while (kio_stored > 0) {
[28a5ebd]284 char32_t tmp = kio[(kio_start + kio_len - kio_stored) % KIO_LENGTH];
[91db0280]285 kio_stored--;
286
287 /*
288 * We need to give up the spinlock for
289 * the physical operation of writing out
290 * the character.
291 */
292 spinlock_unlock(&kio_lock);
293 stdout->op->write(stdout, tmp);
294 spinlock_lock(&kio_lock);
[c859753]295 }
[91db0280]296
297 spinlock_unlock(&kio_lock);
298}
299
300/** Put a character into the output buffer.
[7ddc2c7]301 *
[91db0280]302 * The caller is required to hold kio_lock
303 */
[28a5ebd]304void kio_push_char(const char32_t ch)
[91db0280]305{
[6fa9a99d]306 kio[(kio_start + kio_len) % KIO_LENGTH] = ch;
307 if (kio_len < KIO_LENGTH)
308 kio_len++;
[c859753]309 else
[6fa9a99d]310 kio_start = (kio_start + 1) % KIO_LENGTH;
[a35b458]311
[91db0280]312 if (kio_stored < kio_len)
313 kio_stored++;
[a35b458]314
[712c4ba]315 /* The character is stored for uspace */
[6fa9a99d]316 if (kio_uspace < kio_len)
317 kio_uspace++;
[91db0280]318}
319
[28a5ebd]320void putuchar(const char32_t ch)
[91db0280]321{
322 bool ordy = ((stdout) && (stdout->op->write));
[a35b458]323
[91db0280]324 spinlock_lock(&kio_lock);
325 kio_push_char(ch);
[6fa9a99d]326 spinlock_unlock(&kio_lock);
[a35b458]327
[91db0280]328 /* Output stored characters */
329 kio_flush();
[a35b458]330
[91db0280]331 if (!ordy) {
[da52547]332 /*
333 * No standard output routine defined yet.
334 * The character is still stored in the kernel log
335 * for possible future output.
336 *
[28a5ebd]337 * The early_putuchar() function is used to output
[da52547]338 * the character for low-level debugging purposes.
[ebb3538]339 * Note that the early_putuchar() function might be
[da52547]340 * a no-op on certain hardware configurations.
341 */
[28a5ebd]342 early_putuchar(ch);
[c859753]343 }
[a35b458]344
[97d42d5]345 /* Force notification on newline */
346 if (ch == '\n')
[6fa9a99d]347 kio_update(NULL);
[973be64e]348}
[b45c443]349
[312cc68]350/** Print using kernel facility
351 *
352 * Print to kernel log.
353 *
354 */
[5a5269d]355sys_errno_t sys_kio(int cmd, uspace_addr_t buf, size_t size)
[312cc68]356{
357 char *data;
[b7fd2a0]358 errno_t rc;
[a801688b]359
360 switch (cmd) {
[6fa9a99d]361 case KIO_UPDATE:
362 kio_update(NULL);
[a801688b]363 return EOK;
[6fa9a99d]364 case KIO_WRITE:
365 case KIO_COMMAND:
[a801688b]366 break;
367 default:
368 return ENOTSUP;
369 }
370
[c583970]371 if (size > PAGE_SIZE)
[b7fd2a0]372 return (sys_errno_t) ELIMIT;
[a35b458]373
[c583970]374 if (size > 0) {
[11b285d]375 data = (char *) malloc(size + 1);
[312cc68]376 if (!data)
[b7fd2a0]377 return (sys_errno_t) ENOMEM;
[a35b458]378
[c583970]379 rc = copy_from_uspace(data, buf, size);
[312cc68]380 if (rc) {
381 free(data);
[b7fd2a0]382 return (sys_errno_t) rc;
[312cc68]383 }
[c583970]384 data[size] = 0;
[a35b458]385
[297cb73]386 switch (cmd) {
[6fa9a99d]387 case KIO_WRITE:
[5e904dd]388 printf("[%s(%lu)] %s\n", TASK->name,
389 (unsigned long) TASK->taskid, data);
[297cb73]390 break;
[6fa9a99d]391 case KIO_COMMAND:
[11527051]392 if (!stdin)
393 break;
[297cb73]394 for (unsigned int i = 0; i < size; i++)
395 indev_push_character(stdin, data[i]);
396 indev_push_character(stdin, '\n');
397 break;
398 }
399
[312cc68]400 free(data);
[297cb73]401 }
402
[6afc9d7]403 return EOK;
[312cc68]404}
405
[90dd8aee]406/** Lock console output, ensuring that lines from different threads don't
407 * interleave. Does nothing when preemption is disabled, so that debugging
408 * and error printouts in sensitive areas still work.
409 */
410void console_lock(void)
411{
412 if (!PREEMPTION_DISABLED)
413 mutex_lock(&console_mutex);
414}
415
416/** Unlocks console output. See console_lock()
417 */
418void console_unlock(void)
419{
420 if (!PREEMPTION_DISABLED)
421 mutex_unlock(&console_mutex);
422}
423
[06e1e95]424/** @}
[b45c443]425 */
Note: See TracBrowser for help on using the repository browser.