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

Last change on this file was 39e1b9a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 months ago

Convert console output devices to batch printing

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[2677758]1/*
[df4ed85]2 * Copyright (c) 2003 Josef Cejka
3 * Copyright (c) 2005 Jakub Jermar
[d5b37b6]4 * Copyright (c) 2025 Jiří Zárevúcky
[2677758]5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
[174156fd]31/** @addtogroup kernel_generic_console
[b45c443]32 * @{
33 */
34/** @file
35 */
36
[90dd8aee]37#include <abi/kio.h>
[2677758]38#include <console/chardev.h>
[90dd8aee]39#include <console/console.h>
40#include <errno.h>
[13a638d]41#include <ipc/event.h>
[690ad20]42#include <log.h>
[b8da2a3]43#include <panic.h>
[90dd8aee]44#include <preemption.h>
[d5b37b6]45#include <proc/task.h>
[e90cfa6]46#include <stdatomic.h>
[90dd8aee]47#include <stdio.h>
[aafed15]48#include <stdlib.h> /* malloc */
[690ad20]49#include <str.h>
[90dd8aee]50#include <synch/mutex.h>
51#include <synch/spinlock.h>
52#include <syscall/copy.h>
53#include <sysinfo/sysinfo.h>
[2677758]54
[6fa9a99d]55#define KIO_PAGES 8
[690ad20]56#define KIO_LENGTH (KIO_PAGES * PAGE_SIZE)
[c859753]57
[c0855a0]58/** Kernel log cyclic buffer */
[690ad20]59static char kio[KIO_LENGTH];
[c859753]60
[c0855a0]61/** Kernel log initialized */
[c88d91e8]62static atomic_bool kio_inited = ATOMIC_VAR_INIT(false);
[da1bafb]63
[90dd8aee]64/** A mutex for preventing interleaving of output lines from different threads.
65 * May not be held in some circumstances, so locking of any internal shared
66 * structures is still necessary.
67 */
68static MUTEX_INITIALIZE(console_mutex, MUTEX_RECURSIVE);
69
[af77459]70/** Number of characters written to buffer. Periodically overflows. */
71static size_t kio_written = 0;
[da1bafb]72
[af77459]73/** Number of characters written to output devices. Periodically overflows. */
74static size_t kio_processed = 0;
[da1bafb]75
[af77459]76/** Last notification sent to uspace. */
77static size_t kio_notified = 0;
[82b71ef1]78
[c0855a0]79/** Kernel log spinlock */
[571cc2d]80IRQ_SPINLOCK_INITIALIZE(kio_lock);
[82b71ef1]81
[39e1b9a]82static IRQ_SPINLOCK_INITIALIZE(flush_lock);
83
[690ad20]84static IRQ_SPINLOCK_INITIALIZE(early_mbstate_lock);
85static mbstate_t early_mbstate;
86
[b9c7425]87static indev_t stdin_sink;
88static outdev_t stdout_source;
89
[7ddc2c7]90static void stdin_signal(indev_t *, indev_signal_t);
91
[44b7783]92static indev_operations_t stdin_ops = {
[7ddc2c7]93 .poll = NULL,
94 .signal = stdin_signal
[44b7783]95};
96
[39e1b9a]97static void stdout_write(outdev_t *, const char *, size_t);
[da1bafb]98static void stdout_redraw(outdev_t *);
[7ddc2c7]99static void stdout_scroll_up(outdev_t *);
100static void stdout_scroll_down(outdev_t *);
[b9c7425]101
102static outdev_operations_t stdout_ops = {
[a71c158]103 .write = stdout_write,
[7ddc2c7]104 .redraw = stdout_redraw,
105 .scroll_up = stdout_scroll_up,
106 .scroll_down = stdout_scroll_down
[b9c7425]107};
108
[b366a6f4]109/** Override kernel console lockout */
110bool console_override = false;
[44b7783]111
[ac8e7a9]112/** Standard input and output character devices */
113indev_t *stdin = NULL;
114outdev_t *stdout = NULL;
[411b6a6]115
[44b7783]116indev_t *stdin_wire(void)
117{
118 if (stdin == NULL) {
[b9c7425]119 indev_initialize("stdin", &stdin_sink, &stdin_ops);
120 stdin = &stdin_sink;
[44b7783]121 }
[a35b458]122
[44b7783]123 return stdin;
124}
125
[7ddc2c7]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
[b9c7425]140void stdout_wire(outdev_t *outdev)
141{
142 if (stdout == NULL) {
143 outdev_initialize("stdout", &stdout_source, &stdout_ops);
144 stdout = &stdout_source;
145 }
[a35b458]146
[b9c7425]147 list_append(&outdev->link, &stdout->list);
148}
149
[39e1b9a]150static void stdout_write(outdev_t *dev, const char *s, size_t n)
[b9c7425]151{
[feeac0d]152 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]153 if ((sink) && (sink->op->write))
[39e1b9a]154 sink->op->write(sink, s, n);
[a71c158]155 }
156}
157
158static void stdout_redraw(outdev_t *dev)
159{
[feeac0d]160 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]161 if ((sink) && (sink->op->redraw))
162 sink->op->redraw(sink);
[b9c7425]163 }
164}
165
[7ddc2c7]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
[82b71ef1]182/** Initialize kernel logging facility
183 */
[6fa9a99d]184void kio_init(void)
[82b71ef1]185{
[6fa9a99d]186 event_set_unmask_callback(EVENT_KIO, kio_update);
[e3306d04]187 atomic_store(&kio_inited, true);
[82b71ef1]188}
189
[516ff92]190void grab_console(void)
191{
[392f0e7]192 sysinfo_set_item_val("kconsole", NULL, true);
[593e023]193 event_notify_1(EVENT_KCONSOLE, false, true);
[b366a6f4]194 bool prev = console_override;
[a35b458]195
[b366a6f4]196 console_override = true;
[a71c158]197 if ((stdout) && (stdout->op->redraw))
198 stdout->op->redraw(stdout);
[a35b458]199
[b366a6f4]200 if ((stdin) && (!prev)) {
[da1bafb]201 /*
202 * Force the console to print the prompt.
203 */
[402de0c]204 indev_push_character(stdin, '\n');
[da1bafb]205 }
[516ff92]206}
207
208void release_console(void)
209{
[392f0e7]210 sysinfo_set_item_val("kconsole", NULL, false);
[b366a6f4]211 console_override = false;
[593e023]212 event_notify_1(EVENT_KCONSOLE, false, false);
[516ff92]213}
214
[b366a6f4]215/** Activate kernel console override */
[f6ab787]216sysarg_t sys_debug_console(void)
[312cc68]217{
218#ifdef CONFIG_KCONSOLE
219 grab_console();
220 return true;
221#else
222 return false;
223#endif
224}
225
[6fa9a99d]226void kio_update(void *event)
[82b71ef1]227{
[036e97c]228 if (!atomic_load(&kio_inited))
[712c4ba]229 return;
[a35b458]230
[571cc2d]231 irq_spinlock_lock(&kio_lock, true);
[a35b458]232
[af77459]233 if (kio_notified != kio_written) {
234 if (event_notify_1(EVENT_KIO, true, kio_written) == EOK)
235 kio_notified = kio_written;
[05641a9e]236 }
[a35b458]237
[571cc2d]238 irq_spinlock_unlock(&kio_lock, true);
[82b71ef1]239}
240
[91db0280]241/** Flush characters that are stored in the output buffer
[7ddc2c7]242 *
[91db0280]243 */
244void kio_flush(void)
[973be64e]245{
[712c4ba]246 bool ordy = ((stdout) && (stdout->op->write));
[a35b458]247
[91db0280]248 if (!ordy)
249 return;
250
[571cc2d]251 irq_spinlock_lock(&kio_lock, true);
[91db0280]252
[39e1b9a]253 if (!irq_spinlock_trylock(&flush_lock)) {
254 /* Someone is currently flushing. */
255 irq_spinlock_unlock(&kio_lock, true);
256 return;
257 }
258
259 /* A small-ish local buffer so that we can write to output in chunks. */
260 char buffer[256];
[690ad20]261
[91db0280]262 /* Print characters that weren't printed earlier */
[af77459]263 while (kio_written != kio_processed) {
[690ad20]264 size_t offset = kio_processed % KIO_LENGTH;
265 size_t len = min(kio_written - kio_processed, KIO_LENGTH - offset);
[39e1b9a]266 len = min(len, sizeof(buffer));
[690ad20]267
[39e1b9a]268 /* Take out a chunk of the big buffer. */
269 memcpy(buffer, &kio[offset], len);
270 kio_processed += len;
[91db0280]271
272 /*
[39e1b9a]273 * We need to give up the spinlock for the physical operation of writing
274 * out the buffer.
[91db0280]275 */
[571cc2d]276 irq_spinlock_unlock(&kio_lock, true);
[39e1b9a]277 stdout->op->write(stdout, buffer, len);
[571cc2d]278 irq_spinlock_lock(&kio_lock, true);
[c859753]279 }
[91db0280]280
[39e1b9a]281 irq_spinlock_unlock(&flush_lock, false);
[571cc2d]282 irq_spinlock_unlock(&kio_lock, true);
[91db0280]283}
284
[690ad20]285void kio_push_bytes(const char *s, size_t n)
[91db0280]286{
[690ad20]287 /* Skip the section we know we can't keep. */
288 if (n > KIO_LENGTH) {
289 size_t lost = n - KIO_LENGTH;
290 kio_written += lost;
291 s += lost;
292 n -= lost;
293 }
294
295 size_t offset = kio_written % KIO_LENGTH;
296 if (offset + n > KIO_LENGTH) {
297 size_t first = KIO_LENGTH - offset;
298 size_t last = n - first;
299 memcpy(kio + offset, s, first);
300 memcpy(kio, s + first, last);
301 } else {
302 memcpy(kio + offset, s, n);
303 }
304
305 kio_written += n;
306}
307
308static void early_putstr(const char *s, size_t n)
309{
310 irq_spinlock_lock(&early_mbstate_lock, true);
311
312 size_t offset = 0;
313 char32_t c;
314
315 while ((c = str_decode_r(s, &offset, n, U_SPECIAL, &early_mbstate)))
316 early_putuchar(c);
317
318 irq_spinlock_unlock(&early_mbstate_lock, true);
[91db0280]319}
320
[690ad20]321void putstr(const char *s, size_t n)
[91db0280]322{
323 bool ordy = ((stdout) && (stdout->op->write));
[a35b458]324
[571cc2d]325 irq_spinlock_lock(&kio_lock, true);
[690ad20]326 kio_push_bytes(s, n);
[571cc2d]327 irq_spinlock_unlock(&kio_lock, true);
[a35b458]328
[91db0280]329 /* Output stored characters */
330 kio_flush();
[a35b458]331
[91db0280]332 if (!ordy) {
[da52547]333 /*
334 * No standard output routine defined yet.
335 * The character is still stored in the kernel log
336 * for possible future output.
337 *
[28a5ebd]338 * The early_putuchar() function is used to output
[da52547]339 * the character for low-level debugging purposes.
[ebb3538]340 * Note that the early_putuchar() function might be
[da52547]341 * a no-op on certain hardware configurations.
342 */
[690ad20]343 early_putstr(s, n);
[c859753]344 }
[a35b458]345
[690ad20]346 /* Force notification when containing a newline */
347 if (memchr(s, '\n', n) != NULL)
[6fa9a99d]348 kio_update(NULL);
[973be64e]349}
[b45c443]350
[d5b37b6]351/** Reads up to `size` characters from kio buffer starting at character `at`.
352 *
353 * @param size Maximum number of characters that can be stored in buffer.
354 * Values greater than KIO_LENGTH are silently treated as KIO_LENGTH
355 * for the purposes of calculating the return value.
356 * @return Number of characters read. Can be more than `size`.
357 * In that case, `size` characters are written to user buffer
358 * and the extra amount is the number of characters missed.
359 */
360sysarg_t sys_kio_read(uspace_addr_t buf, size_t size, size_t at)
361{
362 errno_t rc;
363 size_t missed = 0;
364
365 irq_spinlock_lock(&kio_lock, true);
366
367 if (at == kio_written) {
368 irq_spinlock_unlock(&kio_lock, true);
369 return 0;
370 }
371
372 size_t readable_chars = kio_written - at;
373 if (readable_chars > KIO_LENGTH) {
374 missed = readable_chars - KIO_LENGTH;
375 readable_chars = KIO_LENGTH;
376 }
377
378 size_t actual_read = min(readable_chars, size);
379 size_t offset = (kio_written - readable_chars) % KIO_LENGTH;
380
381 if (offset + actual_read > KIO_LENGTH) {
382 size_t first = KIO_LENGTH - offset;
383 size_t last = actual_read - first;
384
[690ad20]385 rc = copy_to_uspace(buf, &kio[offset], first);
[d5b37b6]386 if (rc == EOK)
[690ad20]387 rc = copy_to_uspace(buf + first, &kio[0], last);
[d5b37b6]388 } else {
[690ad20]389 rc = copy_to_uspace(buf, &kio[offset], actual_read);
[d5b37b6]390 }
391
392 irq_spinlock_unlock(&kio_lock, true);
393
394 if (rc != EOK) {
395 log(LF_OTHER, LVL_WARN,
396 "[%s(%" PRIu64 ")] Terminating due to invalid memory buffer"
397 " in SYS_KIO_READ.\n", TASK->name, TASK->taskid);
398 task_kill_self(true);
399 }
400
401 return actual_read + missed;
402}
403
[312cc68]404/** Print using kernel facility
405 *
406 * Print to kernel log.
407 *
408 */
[5a5269d]409sys_errno_t sys_kio(int cmd, uspace_addr_t buf, size_t size)
[312cc68]410{
411 char *data;
[b7fd2a0]412 errno_t rc;
[a801688b]413
414 switch (cmd) {
[6fa9a99d]415 case KIO_UPDATE:
416 kio_update(NULL);
[a801688b]417 return EOK;
[6fa9a99d]418 case KIO_WRITE:
419 case KIO_COMMAND:
[a801688b]420 break;
421 default:
422 return ENOTSUP;
423 }
424
[c583970]425 if (size > PAGE_SIZE)
[b7fd2a0]426 return (sys_errno_t) ELIMIT;
[a35b458]427
[c583970]428 if (size > 0) {
[11b285d]429 data = (char *) malloc(size + 1);
[312cc68]430 if (!data)
[b7fd2a0]431 return (sys_errno_t) ENOMEM;
[a35b458]432
[c583970]433 rc = copy_from_uspace(data, buf, size);
[312cc68]434 if (rc) {
435 free(data);
[b7fd2a0]436 return (sys_errno_t) rc;
[312cc68]437 }
[c583970]438 data[size] = 0;
[a35b458]439
[1db4e2ae]440 uint8_t substitute = '\x1a';
441 str_sanitize(data, size, substitute);
442
[297cb73]443 switch (cmd) {
[6fa9a99d]444 case KIO_WRITE:
[5e904dd]445 printf("[%s(%lu)] %s\n", TASK->name,
446 (unsigned long) TASK->taskid, data);
[297cb73]447 break;
[6fa9a99d]448 case KIO_COMMAND:
[11527051]449 if (!stdin)
450 break;
[297cb73]451 for (unsigned int i = 0; i < size; i++)
452 indev_push_character(stdin, data[i]);
453 indev_push_character(stdin, '\n');
454 break;
455 }
456
[312cc68]457 free(data);
[297cb73]458 }
459
[6afc9d7]460 return EOK;
[312cc68]461}
462
[90dd8aee]463/** Lock console output, ensuring that lines from different threads don't
464 * interleave. Does nothing when preemption is disabled, so that debugging
465 * and error printouts in sensitive areas still work.
466 */
467void console_lock(void)
468{
469 if (!PREEMPTION_DISABLED)
470 mutex_lock(&console_mutex);
471}
472
473/** Unlocks console output. See console_lock()
474 */
475void console_unlock(void)
476{
477 if (!PREEMPTION_DISABLED)
478 mutex_unlock(&console_mutex);
479}
480
[06e1e95]481/** @}
[b45c443]482 */
Note: See TracBrowser for help on using the repository browser.