Changeset 90dd8aee in mainline


Ignore:
Timestamp:
2025-04-10T17:55:36Z (3 days ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
8165a7a, c0d814a
Parents:
c55ab66
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-10 11:33:46)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-10 17:55:36)
Message:

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.

Location:
kernel/generic
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/console/console.h

    rc55ab66 r90dd8aee  
    7676extern sysarg_t sys_debug_console(void);
    7777
     78extern void console_lock(void);
     79extern void console_unlock(void);
     80
    7881#endif /* KERN_CONSOLE_H_ */
    7982
  • kernel/generic/src/console/console.c

    rc55ab66 r90dd8aee  
    3434 */
    3535
     36#include <abi/kio.h>
     37#include <arch.h>
    3638#include <assert.h>
     39#include <atomic.h>
     40#include <console/chardev.h>
    3741#include <console/console.h>
    38 #include <console/chardev.h>
    39 #include <sysinfo/sysinfo.h>
    40 #include <synch/waitq.h>
    41 #include <synch/spinlock.h>
    42 #include <typedefs.h>
     42#include <ddi/ddi.h>
    4343#include <ddi/irq.h>
    44 #include <ddi/ddi.h>
     44#include <errno.h>
    4545#include <ipc/event.h>
    4646#include <ipc/irq.h>
    47 #include <arch.h>
     47#include <mm/frame.h> /* SIZE2FRAMES */
    4848#include <panic.h>
     49#include <preemption.h>
     50#include <proc/thread.h>
     51#include <putchar.h>
     52#include <stdatomic.h>
    4953#include <stdio.h>
    50 #include <putchar.h>
    51 #include <atomic.h>
     54#include <stdlib.h>  /* malloc */
     55#include <str.h>
     56#include <synch/mutex.h>
     57#include <synch/spinlock.h>
     58#include <synch/waitq.h>
    5259#include <syscall/copy.h>
    53 #include <errno.h>
    54 #include <str.h>
    55 #include <stdatomic.h>
    56 #include <abi/kio.h>
    57 #include <mm/frame.h> /* SIZE2FRAMES */
    58 #include <stdlib.h>  /* malloc */
     60#include <sysinfo/sysinfo.h>
     61#include <typedefs.h>
    5962
    6063#define KIO_PAGES    8
     
    6669/** Kernel log initialized */
    6770static atomic_bool kio_inited = ATOMIC_VAR_INIT(false);
     71
     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);
    6877
    6978/** First kernel log characters */
     
    395404}
    396405
     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
    397424/** @}
    398425 */
  • kernel/generic/src/log/log.c

    rc55ab66 r90dd8aee  
    151151void log_begin(log_facility_t fac, log_level_t level)
    152152{
     153        console_lock();
    153154        spinlock_lock(&log_lock);
    154155        spinlock_lock(&kio_lock);
     
    186187        kio_update(NULL);
    187188        log_update(NULL);
     189        console_unlock();
    188190}
    189191
  • kernel/generic/src/printf/vprintf.c

    rc55ab66 r90dd8aee  
    3333 */
    3434
     35#include <arch/asm.h>
     36#include <console/console.h>
    3537#include <print.h>
    3638#include <printf/printf_core.h>
    3739#include <putchar.h>
     40#include <str.h>
    3841#include <synch/spinlock.h>
    39 #include <arch/asm.h>
    4042#include <typedefs.h>
    41 #include <str.h>
    4243
    4344static int vprintf_str_write(const char *str, size_t size, void *data)
     
    7475        char32_t uc;
    7576
     77        console_lock();
     78
    7679        while ((uc = str_decode(str, &offset, STR_NO_LIMIT)) != 0) {
    7780                putuchar(uc);
     
    8083
    8184        putuchar('\n');
     85
     86        console_unlock();
    8287        return chars;
    8388}
     
    9196        };
    9297
    93         return printf_core(fmt, &ps, ap);
     98        console_lock();
     99        int ret = printf_core(fmt, &ps, ap);
     100        console_unlock();
     101        return ret;
    94102}
    95103
Note: See TracChangeset for help on using the changeset viewer.