Got basic tty driver & implemented Stack Smash Protector

This commit is contained in:
2025-02-17 21:02:10 +01:00
parent 983174a081
commit f9dece59ce
8 changed files with 114 additions and 24 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ MAKE = make
# Flags
HOSTCCFLAGS = -Wall -Wextra -Wpedantic -Os -std=c17
CCFLAGS = -Wall -Wextra -Wpedantic -Wno-pointer-arith\
-ffreestanding -fno-stack-protector -fno-builtin -fno-pie -fcommon\
-ffreestanding -fstack-protector -fno-builtin -fno-pie -fcommon\
-O2 -std=c17 -nostdinc -nostdlib -z notext -march=i386 -m32
LDFLAGS = -melf_i386
ASFLAGS =
+41 -3
View File
@@ -1,6 +1,33 @@
#ifndef _KERNEL_H
#define _KERNEL_H
// Null definition
#define NULL ((void*)0)
/*
* Variable arguments
*/
typedef void* va_list;
#define va_start(ap, param) (void)((ap) = (char*)(&(param) + 1))
#define va_end(ap) (void)((ap) = 0)
#define va_arg(ap, type) (((type*)((ap) = ((ap) + sizeof(type))))[-1])
/*
* Utility macros
*/
#define ARRAY_SIZE(_a) (sizeof((_a)) / sizeof((_a)[0]))
#define BIT(_n) (1UL << (_n))
#define BIT_MASK(_n) (BIT(_n) - 1)
#define ALIGN(_x, _a) __ALIGN_MASK((_x), (typeof(_x))(_a) - 1)
#define __ALIGN_MASK(_x, _m) (((_x) + (_m)) & ~(_m))
#define container_of(_p, _t, _m) \
((_t*)((char*)(_p) - offsetof(_t, _m)))
#define swap(_a, _b) ({ typeof(_a) _tmp = (_a); (_a) = (_b); (_b) = (_a); })
#define min(_x, _y) ({ typeof(_x) __x = (_x); typeof(_y) __y = (_y); __x < __y ? __x : __y; })
#define max(_x, _y) ({ typeof(_x) __x = (_x); typeof(_y) __y = (_y); __x > __y ? __x : __y; })
#define ceil(_x, _y) ((((_x) + (_y)) (_y)) * (_y))
#define floor(_x, _y) ((_x) - ((_x) % (_y)))
/*
* Attribute definitions
*/
@@ -20,18 +47,29 @@
/*
* Standard int types
*/
// Unsigned
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
// Signed
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long int64_t;
// Size
typedef uint32_t size_t;
typedef int32_t ssize_t;
typedef uint32_t uintptr_t;
typedef int32_t intptr_t;
/*
* Logging and errors
*/
// TODO: Logging with printk()
// TODO: Log levels & pr_*() wrappers for printk()
#define BUG() (panic("BUG()"))
#define WARN() NULL
#define BUG_ON(_e) ((_e) ? BUG() : NULL)
#define WARN_ON(_e) ((_e) ? WARN() : NULL)
// TODO: Write a panic()
#define panic(_s) while(1);
#endif
+6 -4
View File
@@ -2,13 +2,15 @@
#define _TTY_H
/* VGA Text mode */
#define VGA_ATTR(_fg, _bg) ((_fg) | ((_bg) << 4)
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define VGA_MEMORY 0xb8000
int tty_init(void);
int tty_write(char* buf, size_t len);
int tty_putc(char c);
int tty_puts(char* s);
void tty_init(void);
void tty_clear(void);
void tty_setattr(uint8_t attr);
void tty_putc(char c);
void tty_puts(char* s);
#endif
+4 -4
View File
@@ -5,8 +5,8 @@ BIN = ./bin
INC = ./include
SRCS_C = $(wildcard $(SRC)/*.c)
SRCS_S = $(wildcard $(SRC)/*.s)
OBJS_C = $(patsubst $(SRC)/%.c, $(BIN)/%.o, $(SRCS_C))
OBJS_S = $(patsubst $(SRC)/%.s, $(BIN)/%.o, $(SRCS_S))
OBJS_C = $(patsubst $(SRC)/%.c, $(BIN)/%.c.o, $(SRCS_C))
OBJS_S = $(patsubst $(SRC)/%.s, $(BIN)/%.s.o, $(SRCS_S))
OUT = ./kernel.elf
LINKER = ./linker.ld
@@ -15,11 +15,11 @@ all: $(OUT)
$(OUT): $(OBJS_C) $(OBJS_S)
$(LD) $(LDFLAGS) -T$(LINKER) -o $@ $^
$(BIN)/%.o: $(SRC)/%.c
$(BIN)/%.c.o: $(SRC)/%.c
@mkdir -p $(dir $@)
$(CC) $(CCFLAGS) -I$(INC) -c -o $@ $^
$(BIN)/%.o: $(SRC)/%.s
$(BIN)/%.s.o: $(SRC)/%.s
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) -felf32 -o $@ $^
+5 -1
View File
@@ -1,9 +1,13 @@
#include <kernel.h>
#include <tty.h>
#define _hlt() while (1) __asm__ volatile ("cli; hlt");
__noreturn
void kmain(void) {
tty_init();
printk("Hello, World!\n");
tty_puts("Hello, World!\n");
_hlt();
}
+14
View File
@@ -0,0 +1,14 @@
/*
* Stack Smash Protector
* Source: https://wiki.osdev.org/Stack_Smashing_Protector
*/
#include <kernel.h>
// TODO: Make this randomized
#define STACK_CHK_GUARD 0x27a1abd8
uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
__noreturn void __stack_chk_fail(void) {
panic("Stack smashing detected!");
}
+44 -12
View File
@@ -12,20 +12,52 @@ void tty_init(void) {
ctx.row = 0;
ctx.col = 0;
ctx.attr = 0x07;
ctx.buf = (u8*)VGA_MEMORY;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
size_t i = (y * VGA_WIDTH + x) * 2;
ctx.buf[i] = ' ';
ctx.buf[i + 1] = ctx.attr;
}
}
ctx.buf = (uint8_t*)VGA_MEMORY;
tty_clear();
}
int tty_write(char* buf, size_t len) {}
void tty_clear(void) {
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
ctx.buf[i * 2] = ' ';
ctx.buf[i * 2 + 1] = ctx.attr;
}
}
int tty_putc(char c) {}
void tty_setattr(uint8_t attr) {
ctx.attr = attr;
}
int tty_puts(char* s) {}
// TODO:
void tty_scroll(void) {
ctx.row = 0;
ctx.col = 0;
tty_clear();
}
void tty_putat(char c, size_t row, size_t col) {
size_t i = row * VGA_WIDTH + col;
ctx.buf[i * 2] = c;
ctx.buf[i * 2 + 1] = ctx.attr;
}
void tty_putc(char c) {
if (c == '\n') {
ctx.col = 0;
ctx.row++;
if (ctx.row >= VGA_HEIGHT) tty_scroll();
return;
}
tty_putat(c, ctx.row, ctx.col++);
if (ctx.col >= VGA_WIDTH) {
ctx.col = 0;
ctx.row++;
}
if (ctx.row >= VGA_HEIGHT) tty_scroll();
}
void tty_puts(char* s) {
while (*s != '\0') tty_putc(*s++);
}
+1 -1
View File
@@ -14,7 +14,7 @@ clean:
qemu:
qemu-system-x86_64\
-kernel ./kernel/kernel.elf
-kernel ./kernel/kernel.elf\
-m 256M -accel tcg -monitor stdio -net none\
-rtc base=localtime,clock=host,driftfix=slew\
-audiodev pipewire,id=audio0 -machine pcspk-audiodev=audio0