source: mainline/kernel/generic/src/console/console.c@ 690ad20

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

Convert kio buffer to bytes (part 1)

  • Property mode set to 100644
File size: 11.1 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
[690ad20]82static IRQ_SPINLOCK_INITIALIZE(early_mbstate_lock);
83static mbstate_t early_mbstate;
84
[b9c7425]85static indev_t stdin_sink;
86static outdev_t stdout_source;
87
[7ddc2c7]88static void stdin_signal(indev_t *, indev_signal_t);
89
[44b7783]90static indev_operations_t stdin_ops = {
[7ddc2c7]91 .poll = NULL,
92 .signal = stdin_signal
[44b7783]93};
94
[28a5ebd]95static void stdout_write(outdev_t *, char32_t);
[da1bafb]96static void stdout_redraw(outdev_t *);
[7ddc2c7]97static void stdout_scroll_up(outdev_t *);
98static void stdout_scroll_down(outdev_t *);
[b9c7425]99
100static outdev_operations_t stdout_ops = {
[a71c158]101 .write = stdout_write,
[7ddc2c7]102 .redraw = stdout_redraw,
103 .scroll_up = stdout_scroll_up,
104 .scroll_down = stdout_scroll_down
[b9c7425]105};
106
[b366a6f4]107/** Override kernel console lockout */
108bool console_override = false;
[44b7783]109
[ac8e7a9]110/** Standard input and output character devices */
111indev_t *stdin = NULL;
112outdev_t *stdout = NULL;
[411b6a6]113
[44b7783]114indev_t *stdin_wire(void)
115{
116 if (stdin == NULL) {
[b9c7425]117 indev_initialize("stdin", &stdin_sink, &stdin_ops);
118 stdin = &stdin_sink;
[44b7783]119 }
[a35b458]120
[44b7783]121 return stdin;
122}
123
[7ddc2c7]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
[b9c7425]138void stdout_wire(outdev_t *outdev)
139{
140 if (stdout == NULL) {
141 outdev_initialize("stdout", &stdout_source, &stdout_ops);
142 stdout = &stdout_source;
143 }
[a35b458]144
[b9c7425]145 list_append(&outdev->link, &stdout->list);
146}
147
[28a5ebd]148static void stdout_write(outdev_t *dev, char32_t ch)
[b9c7425]149{
[feeac0d]150 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]151 if ((sink) && (sink->op->write))
[b366a6f4]152 sink->op->write(sink, ch);
[a71c158]153 }
154}
155
156static void stdout_redraw(outdev_t *dev)
157{
[feeac0d]158 list_foreach(dev->list, link, outdev_t, sink) {
[a71c158]159 if ((sink) && (sink->op->redraw))
160 sink->op->redraw(sink);
[b9c7425]161 }
162}
163
[7ddc2c7]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
[82b71ef1]180/** Initialize kernel logging facility
181 */
[6fa9a99d]182void kio_init(void)
[82b71ef1]183{
[6fa9a99d]184 event_set_unmask_callback(EVENT_KIO, kio_update);
[e3306d04]185 atomic_store(&kio_inited, true);
[82b71ef1]186}
187
[516ff92]188void grab_console(void)
189{
[392f0e7]190 sysinfo_set_item_val("kconsole", NULL, true);
[593e023]191 event_notify_1(EVENT_KCONSOLE, false, true);
[b366a6f4]192 bool prev = console_override;
[a35b458]193
[b366a6f4]194 console_override = true;
[a71c158]195 if ((stdout) && (stdout->op->redraw))
196 stdout->op->redraw(stdout);
[a35b458]197
[b366a6f4]198 if ((stdin) && (!prev)) {
[da1bafb]199 /*
200 * Force the console to print the prompt.
201 */
[402de0c]202 indev_push_character(stdin, '\n');
[da1bafb]203 }
[516ff92]204}
205
206void release_console(void)
207{
[392f0e7]208 sysinfo_set_item_val("kconsole", NULL, false);
[b366a6f4]209 console_override = false;
[593e023]210 event_notify_1(EVENT_KCONSOLE, false, false);
[516ff92]211}
212
[b366a6f4]213/** Activate kernel console override */
[f6ab787]214sysarg_t sys_debug_console(void)
[312cc68]215{
216#ifdef CONFIG_KCONSOLE
217 grab_console();
218 return true;
219#else
220 return false;
221#endif
222}
223
[6fa9a99d]224void kio_update(void *event)
[82b71ef1]225{
[036e97c]226 if (!atomic_load(&kio_inited))
[712c4ba]227 return;
[a35b458]228
[571cc2d]229 irq_spinlock_lock(&kio_lock, true);
[a35b458]230
[af77459]231 if (kio_notified != kio_written) {
232 if (event_notify_1(EVENT_KIO, true, kio_written) == EOK)
233 kio_notified = kio_written;
[05641a9e]234 }
[a35b458]235
[571cc2d]236 irq_spinlock_unlock(&kio_lock, true);
[82b71ef1]237}
238
[91db0280]239/** Flush characters that are stored in the output buffer
[7ddc2c7]240 *
[91db0280]241 */
242void kio_flush(void)
[973be64e]243{
[712c4ba]244 bool ordy = ((stdout) && (stdout->op->write));
[a35b458]245
[91db0280]246 if (!ordy)
247 return;
248
[571cc2d]249 irq_spinlock_lock(&kio_lock, true);
[91db0280]250
[690ad20]251 static mbstate_t mbstate;
252
[91db0280]253 /* Print characters that weren't printed earlier */
[af77459]254 while (kio_written != kio_processed) {
[690ad20]255 size_t offset = kio_processed % KIO_LENGTH;
256 size_t len = min(kio_written - kio_processed, KIO_LENGTH - offset);
257 size_t bytes = 0;
258
259 char32_t ch = str_decode_r(&kio[offset], &bytes, len, U'�', &mbstate);
260 assert(bytes <= 4);
261 kio_processed += bytes;
[91db0280]262
263 /*
264 * We need to give up the spinlock for
265 * the physical operation of writing out
266 * the character.
267 */
[571cc2d]268 irq_spinlock_unlock(&kio_lock, true);
[690ad20]269 stdout->op->write(stdout, ch);
[571cc2d]270 irq_spinlock_lock(&kio_lock, true);
[c859753]271 }
[91db0280]272
[571cc2d]273 irq_spinlock_unlock(&kio_lock, true);
[91db0280]274}
275
[690ad20]276void kio_push_bytes(const char *s, size_t n)
[91db0280]277{
[690ad20]278 /* Skip the section we know we can't keep. */
279 if (n > KIO_LENGTH) {
280 size_t lost = n - KIO_LENGTH;
281 kio_written += lost;
282 s += lost;
283 n -= lost;
284 }
285
286 size_t offset = kio_written % KIO_LENGTH;
287 if (offset + n > KIO_LENGTH) {
288 size_t first = KIO_LENGTH - offset;
289 size_t last = n - first;
290 memcpy(kio + offset, s, first);
291 memcpy(kio, s + first, last);
292 } else {
293 memcpy(kio + offset, s, n);
294 }
295
296 kio_written += n;
297}
298
299static void early_putstr(const char *s, size_t n)
300{
301 irq_spinlock_lock(&early_mbstate_lock, true);
302
303 size_t offset = 0;
304 char32_t c;
305
306 while ((c = str_decode_r(s, &offset, n, U_SPECIAL, &early_mbstate)))
307 early_putuchar(c);
308
309 irq_spinlock_unlock(&early_mbstate_lock, true);
[91db0280]310}
311
[690ad20]312void putstr(const char *s, size_t n)
[91db0280]313{
314 bool ordy = ((stdout) && (stdout->op->write));
[a35b458]315
[571cc2d]316 irq_spinlock_lock(&kio_lock, true);
[690ad20]317 kio_push_bytes(s, n);
[571cc2d]318 irq_spinlock_unlock(&kio_lock, true);
[a35b458]319
[91db0280]320 /* Output stored characters */
321 kio_flush();
[a35b458]322
[91db0280]323 if (!ordy) {
[da52547]324 /*
325 * No standard output routine defined yet.
326 * The character is still stored in the kernel log
327 * for possible future output.
328 *
[28a5ebd]329 * The early_putuchar() function is used to output
[da52547]330 * the character for low-level debugging purposes.
[ebb3538]331 * Note that the early_putuchar() function might be
[da52547]332 * a no-op on certain hardware configurations.
333 */
[690ad20]334 early_putstr(s, n);
[c859753]335 }
[a35b458]336
[690ad20]337 /* Force notification when containing a newline */
338 if (memchr(s, '\n', n) != NULL)
[6fa9a99d]339 kio_update(NULL);
[973be64e]340}
[b45c443]341
[d5b37b6]342/** Reads up to `size` characters from kio buffer starting at character `at`.
343 *
344 * @param size Maximum number of characters that can be stored in buffer.
345 * Values greater than KIO_LENGTH are silently treated as KIO_LENGTH
346 * for the purposes of calculating the return value.
347 * @return Number of characters read. Can be more than `size`.
348 * In that case, `size` characters are written to user buffer
349 * and the extra amount is the number of characters missed.
350 */
351sysarg_t sys_kio_read(uspace_addr_t buf, size_t size, size_t at)
352{
353 errno_t rc;
354 size_t missed = 0;
355
356 irq_spinlock_lock(&kio_lock, true);
357
358 if (at == kio_written) {
359 irq_spinlock_unlock(&kio_lock, true);
360 return 0;
361 }
362
363 size_t readable_chars = kio_written - at;
364 if (readable_chars > KIO_LENGTH) {
365 missed = readable_chars - KIO_LENGTH;
366 readable_chars = KIO_LENGTH;
367 }
368
369 size_t actual_read = min(readable_chars, size);
370 size_t offset = (kio_written - readable_chars) % KIO_LENGTH;
371
372 if (offset + actual_read > KIO_LENGTH) {
373 size_t first = KIO_LENGTH - offset;
374 size_t last = actual_read - first;
375
[690ad20]376 rc = copy_to_uspace(buf, &kio[offset], first);
[d5b37b6]377 if (rc == EOK)
[690ad20]378 rc = copy_to_uspace(buf + first, &kio[0], last);
[d5b37b6]379 } else {
[690ad20]380 rc = copy_to_uspace(buf, &kio[offset], actual_read);
[d5b37b6]381 }
382
383 irq_spinlock_unlock(&kio_lock, true);
384
385 if (rc != EOK) {
386 log(LF_OTHER, LVL_WARN,
387 "[%s(%" PRIu64 ")] Terminating due to invalid memory buffer"
388 " in SYS_KIO_READ.\n", TASK->name, TASK->taskid);
389 task_kill_self(true);
390 }
391
392 return actual_read + missed;
393}
394
[312cc68]395/** Print using kernel facility
396 *
397 * Print to kernel log.
398 *
399 */
[5a5269d]400sys_errno_t sys_kio(int cmd, uspace_addr_t buf, size_t size)
[312cc68]401{
402 char *data;
[b7fd2a0]403 errno_t rc;
[a801688b]404
405 switch (cmd) {
[6fa9a99d]406 case KIO_UPDATE:
407 kio_update(NULL);
[a801688b]408 return EOK;
[6fa9a99d]409 case KIO_WRITE:
410 case KIO_COMMAND:
[a801688b]411 break;
412 default:
413 return ENOTSUP;
414 }
415
[c583970]416 if (size > PAGE_SIZE)
[b7fd2a0]417 return (sys_errno_t) ELIMIT;
[a35b458]418
[c583970]419 if (size > 0) {
[11b285d]420 data = (char *) malloc(size + 1);
[312cc68]421 if (!data)
[b7fd2a0]422 return (sys_errno_t) ENOMEM;
[a35b458]423
[c583970]424 rc = copy_from_uspace(data, buf, size);
[312cc68]425 if (rc) {
426 free(data);
[b7fd2a0]427 return (sys_errno_t) rc;
[312cc68]428 }
[c583970]429 data[size] = 0;
[a35b458]430
[1db4e2ae]431 uint8_t substitute = '\x1a';
432 str_sanitize(data, size, substitute);
433
[297cb73]434 switch (cmd) {
[6fa9a99d]435 case KIO_WRITE:
[5e904dd]436 printf("[%s(%lu)] %s\n", TASK->name,
437 (unsigned long) TASK->taskid, data);
[297cb73]438 break;
[6fa9a99d]439 case KIO_COMMAND:
[11527051]440 if (!stdin)
441 break;
[297cb73]442 for (unsigned int i = 0; i < size; i++)
443 indev_push_character(stdin, data[i]);
444 indev_push_character(stdin, '\n');
445 break;
446 }
447
[312cc68]448 free(data);
[297cb73]449 }
450
[6afc9d7]451 return EOK;
[312cc68]452}
453
[90dd8aee]454/** Lock console output, ensuring that lines from different threads don't
455 * interleave. Does nothing when preemption is disabled, so that debugging
456 * and error printouts in sensitive areas still work.
457 */
458void console_lock(void)
459{
460 if (!PREEMPTION_DISABLED)
461 mutex_lock(&console_mutex);
462}
463
464/** Unlocks console output. See console_lock()
465 */
466void console_unlock(void)
467{
468 if (!PREEMPTION_DISABLED)
469 mutex_unlock(&console_mutex);
470}
471
[06e1e95]472/** @}
[b45c443]473 */
Note: See TracBrowser for help on using the repository browser.