kernel: add some basic features

+ feat list:
+ * gdt/idt
+ * brokie pmm
This commit is contained in:
RaphProductions 2025-05-18 15:54:12 +02:00
parent a1e27c2730
commit 89bb8c8a4b
31 changed files with 854 additions and 23 deletions

29
kernel/src/lib/ansi.c Normal file
View file

@ -0,0 +1,29 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* ansi.c - ANSI escape sequence generation functions implementation.
*/
#include <deps/npf.h>
#include <lib/ansi.h>
#include <stdint.h>
static char __ansi_bg_buf[32];
static char __ansi_fg_buf[32];
char *ansi_gen_bg(uint8_t r, uint8_t g, uint8_t b) {
int len = npf_snprintf(__ansi_bg_buf, sizeof(__ansi_bg_buf), "\033[48;2;%d;%d;%d;m", r, g, b);
if (len < 0 || len >= (int)sizeof(__ansi_bg_buf)) {
return "\033[0m";
}
return __ansi_bg_buf;
}
char *ansi_gen_fg(uint8_t r, uint8_t g, uint8_t b) {
int len = npf_snprintf(__ansi_fg_buf, sizeof(__ansi_fg_buf), "\033[38;2;%d;%d;%d;m", r, g, b);
if (len < 0 || len >= (int)sizeof(__ansi_fg_buf)) {
return "\033[0m";
}
return __ansi_fg_buf;
}

15
kernel/src/lib/ansi.h Normal file
View file

@ -0,0 +1,15 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* ansi.h - ANSI escape sequence generation functions.
*/
#pragma once
#include <stdint.h>
#define ANSI_CLEAR_SCREEN "\033[H\033[2J"
#define ANSI_RESET_COLOR "\033[39m\\033[49m"
char *ansi_gen_bg(uint8_t r, uint8_t g, uint8_t b);
char *ansi_gen_fg(uint8_t r, uint8_t g, uint8_t b);

53
kernel/src/lib/log.c Normal file
View file

@ -0,0 +1,53 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* log.c - Kernel logging interface.
*/
#include <stdarg.h>
#include <lib/log.h>
static int __logger_max_loglevel = 0;
static log_output_func __logger_outputs[16];
static int __logger_output_count = 0;
static char* prelog[7] = {
"\033[38;2;169;68;66;mFAULT | \033[39m",
"\033[38;2;217;83;79;mERROR | \033[39m",
"\033[38;2;240;173;78;mWARN | \033[39m",
"\033[38;2;240;240;240;mNOTICE | \033[39m",
"\033[38;2;92;184;92;mINFO | \033[39m",
"\033[38;2;87;201;193;mDEBUG | \033[39m",
"\033[38;2;150;150;150;mTRACE | \033[39m",
};
void log_init(int max_loglevel) {
__logger_max_loglevel = max_loglevel;
}
bool log_register_output(log_output_func fn) {
if (fn && __logger_output_count <= 16) {
__logger_output_count ++;
__logger_outputs[__logger_output_count - 1] = fn;
return true;
}
return false;
}
void log(int loglevel, char *str, ...) {
if (__logger_max_loglevel == 0 || __logger_output_count == 0)
return; // The user forgot to initialize the logger.
if (loglevel > __logger_max_loglevel)
return; // The user does not want this type of log to show up.
va_list vl;
va_start(vl, str);
for (int i = 0; i < __logger_output_count; i++) {
__logger_outputs[i](prelog[loglevel - 1], (void*)0);
__logger_outputs[i](str, &vl);
}
va_end(vl);
}

35
kernel/src/lib/log.h Normal file
View file

@ -0,0 +1,35 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* log.h - Kernel logging interface.
*/
#pragma once
// Log levels in order of increasing severity
#include <stdarg.h>
#include <stdbool.h>
#define LOGLEVEL_TRACE 7
#define LOGLEVEL_DEBUG 6
#define LOGLEVEL_INFO 5
#define LOGLEVEL_NOTICE 4
#define LOGLEVEL_WARNING 3
#define LOGLEVEL_ERROR 2
#define LOGLEVEL_FATAL 1
typedef void(*log_output_func)(char *str, va_list *vl);
void log_init(int max_loglevel);
bool log_register_output(log_output_func fn);
void log(int loglevel, char *str, ...);
// Shortcuts to log
#define trace(str, ...) log(LOGLEVEL_TRACE, str, ##__VA_ARGS__)
#define debug(str, ...) log(LOGLEVEL_DEBUG, str, ##__VA_ARGS__)
#define info(str, ...) log(LOGLEVEL_INFO, str, ##__VA_ARGS__)
#define notice(str, ...) log(LOGLEVEL_NOTICE, str, ##__VA_ARGS__)
#define warn(str, ...) log(LOGLEVEL_WARNING, str, ##__VA_ARGS__)
#define error(str, ...) log(LOGLEVEL_ERROR, str, ##__VA_ARGS__)
#define fatal(str, ...) log(LOGLEVEL_FATAL, str, ##__VA_ARGS__)

View file

@ -0,0 +1,26 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* logoutputs_sk.c - Output functions for the logger
*/
#include "deps/npf.h"
#include <dev/tty.h>
#include <lib/logoutputs_sk.h>
void sklogoutput_tty(char *str, va_list *vl) {
if (!vl) {
tty_puts(str);
return;
}
char buf[2048];
npf_vsnprintf(buf, 2048, str, *vl);
tty_puts(buf);
}
void sklogoutput_e9(char *str, va_list *vl) {
// TODO: implement this
(void)str;
(void)vl;
}

View file

@ -0,0 +1,16 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* logoutputs_sk.h - Output functions for the logger
*/
#pragma once
// Output to the terminal.
#include <stdarg.h>
void sklogoutput_tty(char *str, va_list *vl);
// Output to QEMU & Bochs's E9 port.
void sklogoutput_e9(char *str, va_list *vl);

18
kernel/src/lib/npf.c Normal file
View file

@ -0,0 +1,18 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* npf.c - nanoprintf configuration and implementation.
*/
#define NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_LARGE_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_SMALL_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS 0
#define NANOPRINTF_USE_BINARY_FORMAT_SPECIFIERS 1
#define NANOPRINTF_USE_WRITEBACK_FORMAT_SPECIFIERS 1
// Compile nanoprintf in this translation unit.
#define NANOPRINTF_IMPLEMENTATION
#include <deps/npf.h>

View file

@ -1,3 +1,10 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* string.c - String manipulation functions implementation.
*/
#include <lib/string.h>
size_t strlen(char *str) {

View file

@ -1,3 +1,10 @@
/*
* The Soaplin Kernel
* Copyright (C) 2025 The SILD Project
*
* string.h - String manipulation functions.
*/
#pragma once
#include <stddef.h>