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 */
|
---|
59 | static char kio[KIO_LENGTH];
|
---|
60 |
|
---|
61 | /** Kernel log initialized */
|
---|
62 | static 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 | */
|
---|
68 | static MUTEX_INITIALIZE(console_mutex, MUTEX_RECURSIVE);
|
---|
69 |
|
---|
70 | /** Number of characters written to buffer. Periodically overflows. */
|
---|
71 | static size_t kio_written = 0;
|
---|
72 |
|
---|
73 | /** Number of characters written to output devices. Periodically overflows. */
|
---|
74 | static size_t kio_processed = 0;
|
---|
75 |
|
---|
76 | /** Last notification sent to uspace. */
|
---|
77 | static size_t kio_notified = 0;
|
---|
78 |
|
---|
79 | /** Kernel log spinlock */
|
---|
80 | IRQ_SPINLOCK_INITIALIZE(kio_lock);
|
---|
81 |
|
---|
82 | static IRQ_SPINLOCK_INITIALIZE(flush_lock);
|
---|
83 |
|
---|
84 | static IRQ_SPINLOCK_INITIALIZE(early_mbstate_lock);
|
---|
85 | static mbstate_t early_mbstate;
|
---|
86 |
|
---|
87 | static indev_t stdin_sink;
|
---|
88 | static outdev_t stdout_source;
|
---|
89 |
|
---|
90 | static void stdin_signal(indev_t *, indev_signal_t);
|
---|
91 |
|
---|
92 | static indev_operations_t stdin_ops = {
|
---|
93 | .poll = NULL,
|
---|
94 | .signal = stdin_signal
|
---|
95 | };
|
---|
96 |
|
---|
97 | static void stdout_write(outdev_t *, const char *, size_t);
|
---|
98 | static void stdout_redraw(outdev_t *);
|
---|
99 | static void stdout_scroll_up(outdev_t *);
|
---|
100 | static void stdout_scroll_down(outdev_t *);
|
---|
101 |
|
---|
102 | static 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 */
|
---|
110 | bool console_override = false;
|
---|
111 |
|
---|
112 | /** Standard input and output character devices */
|
---|
113 | indev_t *stdin = NULL;
|
---|
114 | outdev_t *stdout = NULL;
|
---|
115 |
|
---|
116 | indev_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 |
|
---|
126 | static 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 |
|
---|
140 | void 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 |
|
---|
150 | static void stdout_write(outdev_t *dev, const char *s, size_t n)
|
---|
151 | {
|
---|
152 | list_foreach(dev->list, link, outdev_t, sink) {
|
---|
153 | if ((sink) && (sink->op->write))
|
---|
154 | sink->op->write(sink, s, n);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | static 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 |
|
---|
166 | static 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 |
|
---|
174 | static 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 | void kio_init(void)
|
---|
185 | {
|
---|
186 | event_set_unmask_callback(EVENT_KIO, kio_update);
|
---|
187 | atomic_store(&kio_inited, true);
|
---|
188 | }
|
---|
189 |
|
---|
190 | void grab_console(void)
|
---|
191 | {
|
---|
192 | sysinfo_set_item_val("kconsole", NULL, true);
|
---|
193 | event_notify_1(EVENT_KCONSOLE, false, true);
|
---|
194 | bool prev = console_override;
|
---|
195 |
|
---|
196 | console_override = true;
|
---|
197 | if ((stdout) && (stdout->op->redraw))
|
---|
198 | stdout->op->redraw(stdout);
|
---|
199 |
|
---|
200 | if ((stdin) && (!prev)) {
|
---|
201 | /*
|
---|
202 | * Force the console to print the prompt.
|
---|
203 | */
|
---|
204 | indev_push_character(stdin, '\n');
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | void release_console(void)
|
---|
209 | {
|
---|
210 | sysinfo_set_item_val("kconsole", NULL, false);
|
---|
211 | console_override = false;
|
---|
212 | event_notify_1(EVENT_KCONSOLE, false, false);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Activate kernel console override */
|
---|
216 | sysarg_t sys_debug_console(void)
|
---|
217 | {
|
---|
218 | #ifdef CONFIG_KCONSOLE
|
---|
219 | grab_console();
|
---|
220 | return true;
|
---|
221 | #else
|
---|
222 | return false;
|
---|
223 | #endif
|
---|
224 | }
|
---|
225 |
|
---|
226 | void kio_update(void *event)
|
---|
227 | {
|
---|
228 | if (!atomic_load(&kio_inited))
|
---|
229 | return;
|
---|
230 |
|
---|
231 | irq_spinlock_lock(&kio_lock, true);
|
---|
232 |
|
---|
233 | if (kio_notified != kio_written) {
|
---|
234 | if (event_notify_1(EVENT_KIO, true, kio_written) == EOK)
|
---|
235 | kio_notified = kio_written;
|
---|
236 | }
|
---|
237 |
|
---|
238 | irq_spinlock_unlock(&kio_lock, true);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /** Flush characters that are stored in the output buffer
|
---|
242 | *
|
---|
243 | */
|
---|
244 | void kio_flush(void)
|
---|
245 | {
|
---|
246 | bool ordy = ((stdout) && (stdout->op->write));
|
---|
247 |
|
---|
248 | if (!ordy)
|
---|
249 | return;
|
---|
250 |
|
---|
251 | irq_spinlock_lock(&kio_lock, true);
|
---|
252 |
|
---|
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];
|
---|
261 |
|
---|
262 | /* Print characters that weren't printed earlier */
|
---|
263 | while (kio_written != kio_processed) {
|
---|
264 | size_t offset = kio_processed % KIO_LENGTH;
|
---|
265 | size_t len = min(kio_written - kio_processed, KIO_LENGTH - offset);
|
---|
266 | len = min(len, sizeof(buffer));
|
---|
267 |
|
---|
268 | /* Take out a chunk of the big buffer. */
|
---|
269 | memcpy(buffer, &kio[offset], len);
|
---|
270 | kio_processed += len;
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * We need to give up the spinlock for the physical operation of writing
|
---|
274 | * out the buffer.
|
---|
275 | */
|
---|
276 | irq_spinlock_unlock(&kio_lock, true);
|
---|
277 | stdout->op->write(stdout, buffer, len);
|
---|
278 | irq_spinlock_lock(&kio_lock, true);
|
---|
279 | }
|
---|
280 |
|
---|
281 | irq_spinlock_unlock(&flush_lock, false);
|
---|
282 | irq_spinlock_unlock(&kio_lock, true);
|
---|
283 | }
|
---|
284 |
|
---|
285 | void kio_push_bytes(const char *s, size_t n)
|
---|
286 | {
|
---|
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 |
|
---|
308 | static 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);
|
---|
319 | }
|
---|
320 |
|
---|
321 | void putstr(const char *s, size_t n)
|
---|
322 | {
|
---|
323 | bool ordy = ((stdout) && (stdout->op->write));
|
---|
324 |
|
---|
325 | irq_spinlock_lock(&kio_lock, true);
|
---|
326 | kio_push_bytes(s, n);
|
---|
327 | irq_spinlock_unlock(&kio_lock, true);
|
---|
328 |
|
---|
329 | /* Output stored characters */
|
---|
330 | kio_flush();
|
---|
331 |
|
---|
332 | if (!ordy) {
|
---|
333 | /*
|
---|
334 | * No standard output routine defined yet.
|
---|
335 | * The character is still stored in the kernel log
|
---|
336 | * for possible future output.
|
---|
337 | *
|
---|
338 | * The early_putuchar() function is used to output
|
---|
339 | * the character for low-level debugging purposes.
|
---|
340 | * Note that the early_putuchar() function might be
|
---|
341 | * a no-op on certain hardware configurations.
|
---|
342 | */
|
---|
343 | early_putstr(s, n);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* Force notification when containing a newline */
|
---|
347 | if (memchr(s, '\n', n) != NULL)
|
---|
348 | kio_update(NULL);
|
---|
349 | }
|
---|
350 |
|
---|
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 | */
|
---|
360 | sysarg_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 |
|
---|
385 | rc = copy_to_uspace(buf, &kio[offset], first);
|
---|
386 | if (rc == EOK)
|
---|
387 | rc = copy_to_uspace(buf + first, &kio[0], last);
|
---|
388 | } else {
|
---|
389 | rc = copy_to_uspace(buf, &kio[offset], actual_read);
|
---|
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 |
|
---|
404 | /** Print using kernel facility
|
---|
405 | *
|
---|
406 | * Print to kernel log.
|
---|
407 | *
|
---|
408 | */
|
---|
409 | sys_errno_t sys_kio(int cmd, uspace_addr_t buf, size_t size)
|
---|
410 | {
|
---|
411 | char *data;
|
---|
412 | errno_t rc;
|
---|
413 |
|
---|
414 | switch (cmd) {
|
---|
415 | case KIO_UPDATE:
|
---|
416 | kio_update(NULL);
|
---|
417 | return EOK;
|
---|
418 | case KIO_WRITE:
|
---|
419 | case KIO_COMMAND:
|
---|
420 | break;
|
---|
421 | default:
|
---|
422 | return ENOTSUP;
|
---|
423 | }
|
---|
424 |
|
---|
425 | if (size > PAGE_SIZE)
|
---|
426 | return (sys_errno_t) ELIMIT;
|
---|
427 |
|
---|
428 | if (size > 0) {
|
---|
429 | data = (char *) malloc(size + 1);
|
---|
430 | if (!data)
|
---|
431 | return (sys_errno_t) ENOMEM;
|
---|
432 |
|
---|
433 | rc = copy_from_uspace(data, buf, size);
|
---|
434 | if (rc) {
|
---|
435 | free(data);
|
---|
436 | return (sys_errno_t) rc;
|
---|
437 | }
|
---|
438 | data[size] = 0;
|
---|
439 |
|
---|
440 | uint8_t substitute = '\x1a';
|
---|
441 | str_sanitize(data, size, substitute);
|
---|
442 |
|
---|
443 | switch (cmd) {
|
---|
444 | case KIO_WRITE:
|
---|
445 | printf("[%s(%lu)] %s\n", TASK->name,
|
---|
446 | (unsigned long) TASK->taskid, data);
|
---|
447 | break;
|
---|
448 | case KIO_COMMAND:
|
---|
449 | if (!stdin)
|
---|
450 | break;
|
---|
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 |
|
---|
457 | free(data);
|
---|
458 | }
|
---|
459 |
|
---|
460 | return EOK;
|
---|
461 | }
|
---|
462 |
|
---|
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 | */
|
---|
467 | void console_lock(void)
|
---|
468 | {
|
---|
469 | if (!PREEMPTION_DISABLED)
|
---|
470 | mutex_lock(&console_mutex);
|
---|
471 | }
|
---|
472 |
|
---|
473 | /** Unlocks console output. See console_lock()
|
---|
474 | */
|
---|
475 | void console_unlock(void)
|
---|
476 | {
|
---|
477 | if (!PREEMPTION_DISABLED)
|
---|
478 | mutex_unlock(&console_mutex);
|
---|
479 | }
|
---|
480 |
|
---|
481 | /** @}
|
---|
482 | */
|
---|