Files
mop3/kernel/amd64/debug.c
kamkow1 e5ebd7f3ba
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 2m21s
Build documentation / build-and-deploy (push) Successful in 54s
Use a big-lock for kernel sychronization instead of fine-grained locking
2026-04-27 18:06:02 +02:00

92 lines
1.8 KiB
C

#include <amd64/debug.h>
#include <amd64/io.h>
#include <libk/printf.h>
#include <libk/std.h>
#include <libk/string.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
/* Port for printing to serial */
/* TODO: Make this configurable */
#define PORT_COM1 0x03F8
/* debugprintf buffer size */
#define BUFFER_SIZE 1024
static spin_lock_t debug_lock = SPIN_LOCK_INIT;
static bool debug_is_init = false;
/* Block until TX buffer is empty */
static bool debug_serial_tx_empty(void) { return (bool)(inb(PORT_COM1 + 5) & 0x20); }
/* Write a single character to serial */
static void debug_serial_write(char x) {
while (!debug_serial_tx_empty())
;
outb(PORT_COM1, (uint8_t)x);
}
/*
* Formatted printing to serial. serial_lock ensures that all prints are atomic.
*/
void debugprintf(const char* fmt, ...) {
if (!debug_is_init)
return;
char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
buffer[sizeof(buffer) - 1] = '\0';
const char* p = buffer;
spin_lock(&debug_lock);
while (*p) {
debug_serial_write(*p);
p++;
}
spin_unlock(&debug_lock);
}
void debugprintf_nolock(const char* fmt, ...) {
if (!debug_is_init)
return;
char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
buffer[sizeof(buffer) - 1] = '\0';
const char* p = buffer;
while (*p) {
debug_serial_write(*p);
p++;
}
}
/* Initialize serial */
void debug_init(void) {
outb(PORT_COM1 + 1, 0x00);
outb(PORT_COM1 + 3, 0x80);
outb(PORT_COM1 + 0, 0x03);
outb(PORT_COM1 + 1, 0x00);
outb(PORT_COM1 + 3, 0x03);
outb(PORT_COM1 + 2, 0xC7);
outb(PORT_COM1 + 4, 0x0B);
debug_is_init = true;
}