diff --git a/Makefile b/Makefile index c6d91ed..b2499c8 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,13 @@ CC_PROBLEMATIC_FLAGS=-Wno-unused-parameter -Wno-unused-variable LD := x86_64-elf-ld $(ELFFILE): $(BUILDDIR) $(OBJFILES) - nasm -f elf64 src/idt/idt.S -o $(BUILDDIR)/idt_stub.o + nasm -f elf64 src/arch/x86/idt.S -o $(BUILDDIR)/idt_stub.o $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o # Get the symbols for debugging nm -n $(ELFFILE) | awk '$$2 ~ /[TtDdBbRr]/ {print $$1, $$3}' > symbols.map python3 symbols.py nasm -f elf64 symbols.S -o $(BUILDDIR)/symbols.o - $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o + $(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o $(BUILDDIR)/symbols.o $(BUILDDIR): @mkdir -p $(BUILDDIR) diff --git a/include/arch/x86.h b/include/arch/x86.h index 9521859..1c677d8 100644 --- a/include/arch/x86.h +++ b/include/arch/x86.h @@ -10,4 +10,53 @@ void wrmsr(uint32_t msr, uint64_t value); bool x86_has_msr(); void x86_arch_init(); +/* Interrupt Descriptor Table */ + +void idt_init(void); + +struct interrupt_descriptor { + uint16_t address_low; + uint16_t selector; + uint8_t ist; + uint8_t flags; + uint16_t address_mid; + uint32_t address_high; + uint32_t reserved; +} __attribute__((packed)); + +struct idtr { + uint16_t limit; + uint64_t base; +} __attribute__((packed)); + +// All general-purpose registers (except rsp) as stored on the stack, +// plus the values we pushed (vector number, error code) and the iret frame +// In reverse order because the stack grows downwards. +struct cpu_status_t { + uint64_t r15; + uint64_t r14; + uint64_t r13; + uint64_t r12; + uint64_t r11; + uint64_t r10; + uint64_t r9; + uint64_t r8; + uint64_t rbp; + uint64_t rdi; + uint64_t rsi; + uint64_t rdx; + uint64_t rcx; + uint64_t rbx; + uint64_t rax; + + uint64_t vector_number; + uint64_t error_code; + + uint64_t iret_rip; + uint64_t iret_cs; + uint64_t iret_flags; + uint64_t iret_rsp; + uint64_t iret_ss; +}; + #endif \ No newline at end of file diff --git a/include/idt/idt.h b/include/idt/idt.h deleted file mode 100644 index b74fb56..0000000 --- a/include/idt/idt.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * @author xamidev - * @brief Interrupt Descriptor Table setup and dispatching - * @license GPL-3.0-only - */ - -#ifndef IDT_H -#define IDT_H - -#include - -void idt_init(void); - -struct interrupt_descriptor { - uint16_t address_low; - uint16_t selector; - uint8_t ist; - uint8_t flags; - uint16_t address_mid; - uint32_t address_high; - uint32_t reserved; -} __attribute__((packed)); - -struct idtr { - uint16_t limit; - uint64_t base; -} __attribute__((packed)); - -// All general-purpose registers (except rsp) as stored on the stack, -// plus the values we pushed (vector number, error code) and the iret frame -// In reverse order because the stack grows downwards. -struct cpu_status_t { - uint64_t r15; - uint64_t r14; - uint64_t r13; - uint64_t r12; - uint64_t r11; - uint64_t r10; - uint64_t r9; - uint64_t r8; - uint64_t rbp; - uint64_t rdi; - uint64_t rsi; - uint64_t rdx; - uint64_t rcx; - uint64_t rbx; - uint64_t rax; - - uint64_t vector_number; - uint64_t error_code; - - uint64_t iret_rip; - uint64_t iret_cs; - uint64_t iret_flags; - uint64_t iret_rsp; - uint64_t iret_ss; -}; - -#endif \ No newline at end of file diff --git a/include/io/term/term.h b/include/io/term/term.h index 3ce0722..4ce1a32 100644 --- a/include/io/term/term.h +++ b/include/io/term/term.h @@ -8,8 +8,8 @@ #define TERM_H void kputs(const char* str); -void _putchar(char character); void term_init(void); int printf(const char* fmt, ...); +void internal_putc(int c, void *_); #endif diff --git a/include/kernel.h b/include/kernel.h index 58784aa..98b43ac 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -17,7 +17,7 @@ enum ErrorCodes { #include #include -#include +#include #include extern volatile uint64_t ticks; diff --git a/src/idt/idt.S b/src/arch/x86/idt.S similarity index 100% rename from src/idt/idt.S rename to src/arch/x86/idt.S diff --git a/src/idt/idt.c b/src/arch/x86/idt.c similarity index 99% rename from src/idt/idt.c rename to src/arch/x86/idt.c index 51b0eca..822033c 100644 --- a/src/idt/idt.c +++ b/src/arch/x86/idt.c @@ -4,7 +4,7 @@ * @license GPL-3.0-only */ -#include +#include #include #include #include diff --git a/src/debug/panic.c b/src/debug/panic.c index 0e91c35..22ff590 100644 --- a/src/debug/panic.c +++ b/src/debug/panic.c @@ -5,7 +5,7 @@ */ #include -#include +#include #include #include diff --git a/src/io/kbd/ps2.c b/src/io/kbd/ps2.c index d36403a..aea8c13 100644 --- a/src/io/kbd/ps2.c +++ b/src/io/kbd/ps2.c @@ -216,10 +216,10 @@ void keyboard_handler() if (c) { if (c == '\n') { - _putchar('\r'); + internal_putc('\r', NULL); } - // Should probably have a keyboard buffer here... instead of this - _putchar(c); + + internal_putc(c, NULL); keyboard_putchar(c); } } diff --git a/src/io/term/term.c b/src/io/term/term.c index 31f7b18..65b11a1 100644 --- a/src/io/term/term.c +++ b/src/io/term/term.c @@ -30,19 +30,10 @@ extern struct flanterm_context* ft_ctx; extern struct init_status init; struct spinlock_t term_lock = {0}; +struct spinlock_t printf_lock = {0}; extern int panic_count; -/* - * _putchar - Writes a character to terminal (DEPRECATED) - * @character: character to write - */ -void _putchar(char character) -{ - // TODO: Spinlock here (terminal access) - flanterm_write(ft_ctx, &character, 1); -} - /* * internal_putc - Internal putchar function * @c: char to print @@ -83,14 +74,26 @@ void internal_putc(int c, void *_) * * Return: * - number of characters sent to the callback + * %-1 - error */ int printf(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - int ret = npf_vpprintf(internal_putc, NULL, fmt, args); - va_end(args); - return ret; + if (panic_count == 0) { + spinlock_acquire(&printf_lock); + va_list args; + va_start(args, fmt); + int ret = npf_vpprintf(internal_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(internal_putc, NULL, fmt, args); + va_end(args); + return ret; + } + return -1; } /* @@ -103,30 +106,14 @@ void kputs(const char* str) { size_t i=0; while (str[i] != 0) { - _putchar(str[i]); + internal_putc(str[i], NULL); i++; } - _putchar('\r'); } extern struct flanterm_context* ft_ctx; extern struct boot_context boot_ctx; -/* - * flanterm_free_wrapper - free() wrapper for Flanterm (DEPRECATED) - * @ptr: pointer to free - * @size: amount of bytes to free - * - * This function exists solely because the Flanterm initialization - * function only accepts a free() function with a size parameter, - * and the default one doesn't have it. - */ -void flanterm_free_wrapper(void* ptr, size_t size) -{ - (void)size; - kfree(ptr); -} - /* * term_init - Video output/terminal initialization * diff --git a/src/kmain.c b/src/kmain.c index d4d3ea0..56aca6f 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include