Add kprintf for DEBUG(); differentiated from printf()

This commit is contained in:
2026-03-21 10:36:54 +01:00
parent 03f87723d1
commit db36899152
8 changed files with 121 additions and 8 deletions

View File

@@ -59,4 +59,6 @@ struct cpu_status_t {
uint64_t iret_ss; uint64_t iret_ss;
}; };
struct cpu_status_t* syscall_handler(struct cpu_status_t* regs);
#endif #endif

View File

@@ -11,5 +11,6 @@ void kputs(const char* str);
void term_init(void); void term_init(void);
int printf(const char* fmt, ...); int printf(const char* fmt, ...);
void internal_putc(int c, void *_); void internal_putc(int c, void *_);
int kprintf(const char* fmt, ...);
#endif #endif

View File

@@ -21,7 +21,7 @@ enum ErrorCodes {
#include <stdbool.h> #include <stdbool.h>
extern volatile uint64_t ticks; extern volatile uint64_t ticks;
#define DEBUG(log, ...) printf("[%8u] debug: <%s>: " log "\r\n", ticks, __func__, ##__VA_ARGS__) #define DEBUG(log, ...) kprintf("[%8u] debug: <%s>: " log "\r\n", ticks, __func__, ##__VA_ARGS__)
/* #define DEBUG(log, ...) \ /* #define DEBUG(log, ...) \
printf("debug: [%s]: " log "\r\n", __FILE__, ##__VA_ARGS__); \ printf("debug: [%s]: " log "\r\n", __FILE__, ##__VA_ARGS__); \
@@ -38,6 +38,8 @@ void panic(struct cpu_status_t* ctx, const char* str);
void hcf(void); void hcf(void);
void idle(void); void idle(void);
void pedicel_main(void* arg);
/* debug */ /* debug */
void debug_stack_trace(unsigned int max_frames); void debug_stack_trace(unsigned int max_frames);
const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset); const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset);
@@ -58,6 +60,7 @@ struct init_status {
bool serial; bool serial;
bool keyboard; bool keyboard;
bool timer; bool timer;
bool all;
}; };
#endif #endif

View File

@@ -72,6 +72,9 @@ void idt_init()
// Each vector handler is 16-byte aligned, so <vector_no>*16 = address of that handler // Each vector handler is 16-byte aligned, so <vector_no>*16 = address of that handler
idt_set_entry(i, vector_0_handler + (i*16), 0); idt_set_entry(i, vector_0_handler + (i*16), 0);
} }
idt_set_entry(0x80, syscall_handler, 0);
idt_load(&idt); idt_load(&idt);
DEBUG("IDT initialized"); DEBUG("IDT initialized");
} }

26
src/arch/x86/syscall.c Normal file
View File

@@ -0,0 +1,26 @@
/*
* @author xamidev <xamidev@riseup.net>
* @brief System call handling
* @license GPL-3.0-only
*/
#include <arch/x86.h>
#include <kernel.h>
struct cpu_status_t* syscall_handler(struct cpu_status_t* regs)
{
DEBUG("Syscall %lx with argument %lx", regs->rdi, regs->rsi);
switch (regs->rdi)
{
case 0:
break;
case 1:
break;
default:
regs->rsi = 0xdeadbeef;
break;
}
return regs;
}

View File

@@ -65,6 +65,38 @@ void internal_putc(int c, void *_)
} }
} }
/*
* debug_putc - Internal DEBUG putchar function
* @c: char to print
* @_: (unused, for nanoprintf)
*
* Prints a character to the terminal if it's ready and if
* the kernel is still initializing, and also always to the
* serial interface if it's ready.
*/
void debug_putc(int c, void *_)
{
(void)_;
char ch = (char)c;
if (init.terminal && (!init.all || panic_count > 0)) {
if (panic_count == 0) {
spinlock_acquire(&term_lock);
flanterm_write(ft_ctx, &ch, 1);
spinlock_release(&term_lock);
} else {
flanterm_write(ft_ctx, &ch, 1);
}
}
if (init.serial) {
if (ch == '\n') {
skputc('\r');
}
skputc(ch);
}
}
/* /*
* printf - Fromatted printing * printf - Fromatted printing
* @fmt: format string * @fmt: format string
@@ -96,6 +128,38 @@ int printf(const char* fmt, ...)
return -1; return -1;
} }
/*
* kprintf - Fromatted DEBUG printing
* @fmt: format string
* @...: variadic arguments
*
* Wrapper for nanoprintf; to be used only for
* kernel/debug messages.
*
* Return:
* <ret> - number of characters sent to the callback
* %-1 - error
*/
int kprintf(const char* fmt, ...)
{
if (panic_count == 0) {
spinlock_acquire(&printf_lock);
va_list args;
va_start(args, fmt);
int ret = npf_vpprintf(debug_putc, NULL, fmt, args);
va_end(args);
spinlock_release(&printf_lock);
return ret;
} else {
va_list args;
va_start(args, fmt);
int ret = npf_vpprintf(debug_putc, NULL, fmt, args);
va_end(args);
return ret;
}
return -1;
}
/* /*
* kputs - Kernel puts * kputs - Kernel puts
* @str: String to write * @str: String to write

20
src/kapps/kshell.c Normal file
View File

@@ -0,0 +1,20 @@
/*
* @author xamidev <xamidev@riseup.net>
* @brief PepperOS kernel shell
* @license GPL-3.0-only
*/
#include <io/term/term.h>
/*
* pedicel_main - Kernel shell main function
* @arg: argument (optional)
*
* This is the entry point for the kernel shell process.
* It is used to start programs and to test different things
* on different real hardware easily.
*/
void pedicel_main(void* arg)
{
printf("Entering the kernel shell...\r\n");
}

View File

@@ -66,12 +66,6 @@ extern struct process_t* processes_list;
extern struct process_t* current_process; extern struct process_t* current_process;
struct process_t* idle_proc; struct process_t* idle_proc;
// Never gets executed although pedicel is scheduled?
void pedicel_main(void* arg)
{
printf("\n\n\rWelcome to PepperOS! Pedicel speaking.\r\nNothing left to do, let's go idle!\r\n");
}
void idle_main(void* arg) void idle_main(void* arg)
{ {
for (;;) { for (;;) {
@@ -125,10 +119,10 @@ void kmain()
process_init(); process_init();
idle_proc = process_create("idle", (void*)idle_main, 0); idle_proc = process_create("idle", (void*)idle_main, 0);
process_create("pedicel", (void*)pedicel_main, 0); process_create("pedicel", (void*)pedicel_main, 0);
process_create("thing", thing_main, NULL);
scheduler_init(); scheduler_init();
printf(PEPPEROS_SPLASH); printf(PEPPEROS_SPLASH);
init.all = true;
idle(); idle();
} }