Slight cleanup and fixed SSP

This commit is contained in:
2026-03-22 14:55:37 +01:00
parent 8cbe050f35
commit eca7c957e2
7 changed files with 14 additions and 148 deletions
+1
View File
@@ -29,6 +29,7 @@ DS_Store
/.clangd /.clangd
/.cache /.cache
/compile_commands.json /compile_commands.json
.ssp
# Logs # Logs
*.log *.log
-25
View File
@@ -1,25 +0,0 @@
#ifndef _BITMAP_H
#define _BITMAP_H
#include <kernel.h>
#define BLOCK_SIZE 4096
#define BMAP_NOT_FOUND ((size_t)-1)
typedef struct bitmap_t {
void* map;
size_t blocks; // Eqiv. to # of bits
size_t bytes; // ceil(blocks / 8)
size_t used; // # of bits set to 0
size_t membase; // Used with allocators
int ready;
} bitmap_t;
void* bmap_ptr(bitmap_t* map, size_t bit);
void bmap_set(bitmap_t* map, size_t bit, int val);
int bmap_get(bitmap_t* map, size_t bit);
void bmap_set_area(bitmap_t* map, size_t base, size_t len, int val);
size_t bmap_get_area(bitmap_t* map, size_t len, int val);
void bmap_set_all(bitmap_t* map, int val);
#endif
+1 -4
View File
@@ -14,11 +14,8 @@
int timer_init(void); int timer_init(void);
void timer_wait(size_t ticks); void timer_wait(size_t ticks);
int pit_init(void); int hpet_init(void);
void pit_phase(uint8_t ch, uint8_t mode, uint32_t hz);
void pit_handler(struct isr_int_frame* iframe);
// TODO: rtc_init(void); // TODO: rtc_init(void);
// TODO: hpet_init(void);
#endif #endif
+8 -8
View File
@@ -9,24 +9,24 @@ OBJS_C = $(patsubst $(SRC)/%.c, $(BIN)/%.c.o, $(SRCS_C))
OBJS_S = $(patsubst $(SRC)/%.s, $(BIN)/%.s.o, $(SRCS_S)) OBJS_S = $(patsubst $(SRC)/%.s, $(BIN)/%.s.o, $(SRCS_S))
OUT = $(SYSROOT)/boot/kernel.elf OUT = $(SYSROOT)/boot/kernel.elf
LINKER = ./linker.ld LINKER = ./linker.ld
SSP = ./.ssp
# Stack Canary
SSP := 0x$(shell openssl rand -hex 8)
CCFLAGS += -DSTACK_CHK_GUARD=$(SSP)
.PHONY: all clean .PHONY: all clean
all: $(OUT) all: $(OUT)
$(OUT): $(OBJS_C) $(OBJS_S) $(OUT): $(OBJS_C) $(OBJS_S)
$(LD) $(LDFLAGS) -T$(LINKER) -o $@ $^ $(LD) $(LDFLAGS) -T$(LINKER) -o $@ $^
$(BIN)/%.c.o: $(SRC)/%.c $(BIN)/%.c.o: $(SRC)/%.c $(SSP)
@mkdir -p $(dir $@) @mkdir -p $(dir $@)
$(CC) $(CCFLAGS) -I$(INC) -c -o $@ $^ $(CC) $(CCFLAGS) -DSTACK_CHK_GUARD=$(shell cat $(SSP)) -I$(INC) -c -o $@ $<
$(BIN)/%.s.o: $(SRC)/%.s $(BIN)/%.s.o: $(SRC)/%.s
@mkdir -p $(dir $@) @mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -o $@ $^ $(AS) $(ASFLAGS) -o $@ $<
$(SSP):
echo "0x$$(openssl rand -hex 8)UL" > $(SSP)
clean: clean:
rm -fr $(BIN) $(OUT) rm -fr $(BIN) $(OUT) $(SSP)
-49
View File
@@ -1,49 +0,0 @@
#include <kernel.h>
#include <string.h>
#include <bitmap.h>
void* bmap_ptr(bitmap_t* map, size_t bit) {
return (void*)(map->membase + bit * BLOCK_SIZE);
}
void bmap_set(bitmap_t* map, size_t bit, int val) {
uint32_t* p = (uint32_t*)map->map;
if (val) p[bit / 32] |= (1 << (bit % 32));
else p[bit / 32] &= ~(1 << (bit % 32));
map->used += (val) ? 1 : -1;
}
int bmap_get(bitmap_t* map, size_t bit) {
uint32_t* p = (uint32_t*)map->map;
return (p[bit / 32] & (1 << (bit % 32))) ? 1 : 0;
}
void bmap_set_area(bitmap_t* map, size_t base, size_t len, int val) {
uint32_t align = dceil(base, BLOCK_SIZE);
uint32_t count = dceil(len, BLOCK_SIZE);
while (count-- > 0) bmap_set(map, align++, val);
}
size_t bmap_get_area(bitmap_t* map, size_t len, int val) {
if (len == 0) return BMAP_NOT_FOUND;
uint32_t found = 0;
for (size_t i = 0; i < map->blocks; i++) {
if (bmap_get(map, i) == val) found++;
else found = 0;
if (found >= len) return i - found + 1;
}
return BMAP_NOT_FOUND;
}
void bmap_set_all(bitmap_t* map, int val) {
uint8_t b = (val) ? 0xff : 0;
for (size_t i = 0; i < map->bytes; i++) ((uint8_t*)map->map)[i] = b;
if (val) map->used = map->blocks;
else map->used = 0;
}
+4 -14
View File
@@ -1,15 +1,13 @@
#include <bootloader.h> #include <bootloader.h>
#include <kernel.h>
#include <limine.h>
#include <paging.h> #include <paging.h>
#include <serial.h> #include <serial.h>
#include <splash.h> #include <splash.h>
#include <system.h> #include <system.h>
#include <timer.h>
#include <gdt.h> #include <gdt.h>
#include <idt.h>
#include <isr.h>
#include <pmm.h> #include <pmm.h>
#include <isr.h>
#include <kernel.h>
__noreturn __noreturn
void _start(void) { void _start(void) {
@@ -35,16 +33,8 @@ void _start(void) {
// Memory management // Memory management
if (pmm_init() != 0) panic("Failed to initalize Physical Memory Manager."); if (pmm_init() != 0) panic("Failed to initalize Physical Memory Manager.");
if (pag_init() != 0) panic("Failed to initalize Paging."); if (pag_init() != 0) panic("Failed to initalize Paging.");
//if (kmalloc_init() != 0) panic("Failed to initialize kernel heap.");
// Tasking // Patiently wait for things to do :)
//if (timer_init() != 0) panic("Failed to initialize timer.");
//if (syscall_init() != 0) panic("Failed to initialize syscalls.");
//if (task_init() != 0) panic("Failed to initialize tasking.");
// TODO: Load & run /sbin/init
// We don't want to halt
while (1); while (1);
} }
-48
View File
@@ -1,48 +0,0 @@
#include <kernel.h>
#include <system.h>
//#include <sched.h>
#include <isr.h>
#include <timer.h>
size_t timer_ticks = 0;
int timer_init(void) {
// TODO: Check what timer we can use and select the best one
return pit_init();
}
void timer_wait(size_t t) {
size_t req = timer_ticks + t;
while (req != timer_ticks);
}
int pit_init(void) {
isr_register(0, pit_handler);
pit_phase(0, 2, TIMER_FREQ);
return 0;
}
void pit_phase(uint8_t ch, uint8_t mode, uint32_t hz) {
if (ch > 2 || mode > 7) return;
cli();
outb(PIT_CMD, ((ch << 6) | (3 << 4) | (mode << 1)));
uint32_t div = PIT_DIV / hz;
outb(PIT_CH0 + ch, div & 0xFF);
outb(PIT_CH0 + ch, div >> 8);
debugf("[pit] Set PIT divisor to %d (%d Hz).\n", div, hz);
sti();
}
void pit_handler(struct isr_int_frame* iframe) {
(void)iframe; // We must take it as an argument even if we don't use it
timer_ticks++;
// TODO: This assumes the timer is at 1000 Hz
//if (timer_ticks % SCHED_QUANTUM == 0) schedule();
}