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
Line 
1/*
2 * Copyright (c) 2003 Josef Cejka
3 * Copyright (c) 2005 Jakub Jermar
4 * Copyright (c) 2025 Jiří Zárevúcky
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
31/** @addtogroup kernel_generic_console
32 * @{
33 */
34/** @file
35 */
36
37#include <abi/kio.h>
38#include <console/chardev.h>
39#include <console/console.h>
40#include <errno.h>
41#include <ipc/event.h>
42#include <log.h>
43#include <panic.h>
44#include <preemption.h>
45#include <proc/task.h>
46#include <stdatomic.h>
47#include <stdio.h>
48#include <stdlib.h> /* malloc */
49#include <str.h>
50#include <synch/mutex.h>
51#include <synch/spinlock.h>
52#include <syscall/copy.h>
53#include <sysinfo/sysinfo.h>
54
55#define KIO_PAGES 8
56#define KIO_LENGTH (KIO_PAGES * PAGE_SIZE)
57
58/** Kernel log cyclic buffer */
59static char kio[KIO_LENGTH];
60
61/** Kernel log initialized */
62static atomic_bool kio_inited = ATOMIC_VAR_INIT(false);
63
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
70/** Number of characters written to buffer. Periodically overflows. */
71static size_t kio_written = 0;
72
73/** Number of characters written to output devices. Periodically overflows. */
74static size_t kio_processed = 0;
75
76/** Last notification sent to uspace. */
77static size_t kio_notified = 0;
78
79/** Kernel log spinlock */
80IRQ_SPINLOCK_INITIALIZE(kio_lock);
81
82static IRQ_SPINLOCK_INITIALIZE(early_mbstate_lock);
83static mbstate_t early_mbstate;
84
85static indev_t stdin_sink;
86static outdev_t stdout_source;
87
88static void stdin_signal(indev_t *, indev_signal_t);
89
90static indev_operations_t stdin_ops = {
91 .poll = NULL,
92 .signal = stdin_signal
93};
94
95static void stdout_write(outdev_t *, char32_t);
96static void stdout_redraw(outdev_t *);
97static void stdout_scroll_up(outdev_t *);
98static void stdout_scroll_down(outdev_t *);
99
100static outdev_operations_t stdout_ops = {
101 .write = stdout_write,
102 .redraw = stdout_redraw,
103 .scroll_up = stdout_scroll_up,
104 .scroll_down = stdout_scroll_down
105};
106
107/** Override kernel console lockout */
108bool console_override = false;
109
110/** Standard input and output character devices */
111indev_t *stdin = NULL;
112outdev_t *stdout = NULL;
113
114indev_t *stdin_wire(void)
115{
116 if (stdin == NULL) {
117 indev_initialize("stdin", &stdin_sink, &stdin_ops);
118 stdin = &stdin_sink;
119 }
120
121 return stdin;
122}
123
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
138void stdout_wire(outdev_t *outdev)
139{
140 if (stdout == NULL) {
141 outdev_initialize("stdout", &stdout_source, &stdout_ops);
142 stdout = &stdout_source;
143 }
144
145 list_append(&outdev->link, &stdout->list);
146}
147
148static void stdout_write(outdev_t *dev, char32_t ch)
149{
150 list_foreach(dev->list, link, outdev_t, sink) {
151 if ((sink) && (sink->op->write))
152 sink->op->write(sink, ch);
153 }
154}
155
156static void stdout_redraw(outdev_t *dev)
157{
158 list_foreach(dev->list, link, outdev_t, sink) {
159 if ((sink) && (sink->op->redraw))
160 sink->op->redraw(sink);
161 }
162}
163
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
180/** Initialize kernel logging facility
181 */
182void kio_init(void)
183{
184 event_set_unmask_callback(EVENT_KIO, kio_update);
185 atomic_store(&kio_inited, true);
186}
187
188void grab_console(void)
189{
190 sysinfo_set_item_val("kconsole", NULL, true);
191 event_notify_1(EVENT_KCONSOLE, false, true);
192 bool prev = console_override;
193
194 console_override = true;
195 if ((stdout) && (stdout->op->redraw))
196 stdout->op->redraw(stdout);
197
198 if ((stdin) && (!prev)) {
199 /*
200 * Force the console to print the prompt.
201 */
202 indev_push_character(stdin, '\n');
203 }
204}
205
206void release_console(void)
207{
208 sysinfo_set_item_val("kconsole", NULL, false);
209 console_override = false;
210 event_notify_1(EVENT_KCONSOLE, false, false);
211}
212
213/** Activate kernel console override */
214sysarg_t sys_debug_console(void)
215{
216#ifdef CONFIG_KCONSOLE
217 grab_console();
218 return true;
219#else
220 return false;
221#endif
222}
223
224void kio_update(void *event)
225{
226 if (!atomic_load(&kio_inited))
227 return;
228
229 irq_spinlock_lock(&kio_lock, true);
230
231 if (kio_notified != kio_written) {
232 if (event_notify_1(EVENT_KIO, true, kio_written) == EOK)
233 kio_notified = kio_written;
234 }
235
236 irq_spinlock_unlock(&kio_lock, true);
237}
238
239/** Flush characters that are stored in the output buffer
240 *
241 */
242void kio_flush(void)
243{
244 bool ordy = ((stdout) && (stdout->op->write));
245
246 if (!ordy)
247 return;
248
249 irq_spinlock_lock(&kio_lock, true);
250
251 static mbstate_t mbstate;
252
253 /* Print characters that weren't printed earlier */
254 while (kio_written != kio_processed) {
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;
262
263 /*
264 * We need to give up the spinlock for
265 * the physical operation of writing out
266 * the character.
267 */
268 irq_spinlock_unlock(&kio_lock, true);
269 stdout->op->write(stdout, ch);
270 irq_spinlock_lock(&kio_lock, true);
271 }
272
273 irq_spinlock_unlock(&kio_lock, true);
274}
275
276void kio_push_bytes(const char *s, size_t n)
277{
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);
310}
311
312void putstr(const char *s, size_t n)
313{
314 bool ordy = ((stdout) && (stdout->op->write));
315
316 irq_spinlock_lock(&kio_lock, true);
317 kio_push_bytes(s, n);
318 irq_spinlock_unlock(&kio_lock, true);
319
320 /* Output stored characters */
321 kio_flush();
322
323 if (!ordy) {
324 /*
325 * No standard output routine defined yet.
326 * The character is still stored in the kernel log
327 * for possible future output.
328 *
329 * The early_putuchar() function is used to output
330 * the character for low-level debugging purposes.
331 * Note that the early_putuchar() function might be
332 * a no-op on certain hardware configurations.
333 */
334 early_putstr(s, n);
335 }
336
337 /* Force notification when containing a newline */
338 if (memchr(s, '\n', n) != NULL)
339 kio_update(NULL);
340}
341
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
376 rc = copy_to_uspace(buf, &kio[offset], first);
377 if (rc == EOK)
378 rc = copy_to_uspace(buf + first, &kio[0], last);
379 } else {
380 rc = copy_to_uspace(buf, &kio[offset], actual_read);
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
395/** Print using kernel facility
396 *
397 * Print to kernel log.
398 *
399 */
400sys_errno_t sys_kio(int cmd, uspace_addr_t buf, size_t size)
401{
402 char *data;
403 errno_t rc;
404
405 switch (cmd) {
406 case KIO_UPDATE:
407 kio_update(NULL);
408 return EOK;
409 case KIO_WRITE:
410 case KIO_COMMAND:
411 break;
412 default:
413 return ENOTSUP;
414 }
415
416 if (size > PAGE_SIZE)
417 return (sys_errno_t) ELIMIT;
418
419 if (size > 0) {
420 data = (char *) malloc(size + 1);
421 if (!data)
422 return (sys_errno_t) ENOMEM;
423
424 rc = copy_from_uspace(data, buf, size);
425 if (rc) {
426 free(data);
427 return (sys_errno_t) rc;
428 }
429 data[size] = 0;
430
431 uint8_t substitute = '\x1a';
432 str_sanitize(data, size, substitute);
433
434 switch (cmd) {
435 case KIO_WRITE:
436 printf("[%s(%lu)] %s\n", TASK->name,
437 (unsigned long) TASK->taskid, data);
438 break;
439 case KIO_COMMAND:
440 if (!stdin)
441 break;
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
448 free(data);
449 }
450
451 return EOK;
452}
453
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
472/** @}
473 */
Note: See TracBrowser for help on using the repository browser.