Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
CFLAGS = -c -g -ffreestanding -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fno-stack-protector -mgeneral-regs-only -fPIC -mcmodel=large -Wall -Wextra
ASFLAGS = -g -F dwarf
LDFLAGS = -n

kernel_cpp_src := $(shell find src/kernel -name *.cpp)
kernel_cpp_obj := $(patsubst src/kernel/%.cpp, build/kernel/%.o, $(kernel_cpp_src))

Expand All @@ -14,7 +18,7 @@ x86_64_obj := $(x86_64_asm_obj) $(x86_64_cpp_obj)
##################################################
$(kernel_cpp_obj): build/kernel/%.o : src/kernel/%.cpp
mkdir -p $(dir $@) && \
x86_64-elf-g++ -c -g -I src/intf -I src/x86_64/boot/intf -ffreestanding -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mgeneral-regs-only -Wall $(patsubst build/kernel/%.o, src/kernel/%.cpp, $@) -o $@
x86_64-elf-g++ -I src/intf -I src/x86_64/boot/intf $(CFLAGS) $(patsubst build/kernel/%.o, src/kernel/%.cpp, $@) -o $@



Expand All @@ -23,11 +27,11 @@ $(kernel_cpp_obj): build/kernel/%.o : src/kernel/%.cpp
##################################################
$(x86_64_asm_obj): build/x86_64/%.o : src/x86_64/%.asm
mkdir -p $(dir $@) && \
nasm -g -F dwarf -f elf64 $(patsubst build/x86_64/%.o, src/x86_64/%.asm, $@) -o $@
nasm $(ASFLAGS) -f elf64 $(patsubst build/x86_64/%.o, src/x86_64/%.asm, $@) -o $@

$(x86_64_cpp_obj): build/x86_64/%.o : src/x86_64/%.cpp
mkdir -p $(dir $@) && \
x86_64-elf-g++ -c -g -I src/intf -I src/x86_64/boot/intf -ffreestanding -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mgeneral-regs-only -Wall $(patsubst build/x86_64/%.o, src/x86_64/%.cpp, $@) -o $@
x86_64-elf-g++ -I src/intf -I src/x86_64/boot/intf $(CFLAGS) $(patsubst build/x86_64/%.o, src/x86_64/%.cpp, $@) -o $@



Expand All @@ -37,7 +41,7 @@ $(x86_64_cpp_obj): build/x86_64/%.o : src/x86_64/%.cpp
.PHONY: build-x86_64
build-x86_64: $(kernel_obj) $(x86_64_obj)
mkdir -p dist/x86_64 && \
x86_64-elf-ld -n -o dist/x86_64/kernel.bin -T targets/x86_64/linker.ld $(kernel_obj) $(x86_64_obj) && \
x86_64-elf-ld -T targets/x86_64/linker.ld $(LDFLAGS) -o dist/x86_64/kernel.bin $(kernel_obj) $(x86_64_obj) && \
cp dist/x86_64/kernel.bin targets/x86_64/iso/boot/kernel.bin &&\
grub-mkrescue /usr/lib/grub/i386-pc -o dist/x86_64/kernel.iso targets/x86_64/iso

Expand Down
5 changes: 0 additions & 5 deletions debug.sh

This file was deleted.

1 change: 1 addition & 0 deletions dev-notes.todo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Consider moving src/intf/kernel into src/kernel/intf to be more clear
File renamed without changes.
18 changes: 18 additions & 0 deletions scripts/debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

set -e

this_dir="${0%/*}"
proj_root_dir="$(cd $this_dir/..; pwd)"

# Build the project
sh $this_dir/build.sh

# Run the qemu emulator and gdb in a subshell
# Set the subshell directory to the project root
(
cd $proj_root_dir &&
qemu-system-x86_64 -cdrom dist/x86_64/kernel.iso -s -S &
gdb --quiet -ex "target remote localhost:1234" -ex "set disassembly-flavor intel" \
-ex "symbol-file dist/x86_64/kernel.bin"
)
6 changes: 3 additions & 3 deletions src/intf/drivers/keyboard/irq_handler.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "keyboard.h"
#include "util/attributes.h"

namespace drivers::keyboard
{
/// @brief Required by __attribute__((interrupt))
/// @brief Required by __interrupt
struct interrupt_frame;

__attribute__((interrupt))
void irq_handler(interrupt_frame *frame);
void __interrupt irq_handler(interrupt_frame *frame);
}
3 changes: 2 additions & 1 deletion src/intf/drivers/video/vga.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stddef.h>
#include <stdint.h>
#include "util/attributes.h"

#define VGA_BUFFER ((VGA_Cell*) 0xB8000)

Expand Down Expand Up @@ -47,7 +48,7 @@ namespace drivers::video::VGA
} Cursor;

/// @brief Represents a single cell in the VGA tracking both the character and the brush for that character.
typedef struct __attribute__((packed)) VGA_Cell {
typedef struct __packed VGA_Cell {
uint8_t character;
uint8_t color;
} VGA_Cell;
Expand Down
8 changes: 6 additions & 2 deletions src/x86_64/boot/intf/idt.h → src/intf/idt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

#include <stdint.h>
#include "isr.h"
#include "gdt.h"
#include "pic.h"
#include "util/attributes.h"

#define IDT_ENTRIES 256

Expand All @@ -10,7 +14,7 @@
#define ENTRY_GATE_TRAP 0xF

/// @brief Represents an IDT entry in the appropriate memory layout.
typedef struct __attribute__((packed)) IDT_Entry {
typedef struct __packed IDT_Entry {
uint16_t offset_low;
uint16_t segment; // GDT segment on which to run the ISR
uint8_t ist; // 3 bit offset into the IST (ignored if 0)
Expand All @@ -21,7 +25,7 @@ typedef struct __attribute__((packed)) IDT_Entry {
} IDT_Entry;

/// @brief Represents the IDT pointer to be loaded into the idtr register.
typedef struct __attribute__((packed)) IDT_Ptr {
typedef struct __packed IDT_Ptr {
uint16_t limit;
uint64_t base;
} IDT_Ptr;
Expand Down
5 changes: 2 additions & 3 deletions src/x86_64/boot/intf/isr.h → src/intf/isr.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include <stdint.h>

#include "drivers/video/vga.h"
#include "util/attributes.h"

/// @brief Represents the stack frame for an exception including
/// the interrupt number, the error code, and registers for debugging.
typedef struct __attribute__((packed)) Exception_Stack_Frame {
typedef struct __packed Exception_Stack_Frame {
uint64_t rdi, rsi, rbp, rdx, rcx, rbx, rax;
uint64_t int_no, err_code;
uint64_t rip, cs, rflags, rsp, ss;
Expand Down
16 changes: 16 additions & 0 deletions src/intf/kernel/mb2_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "multiboot2.h"
#include "kernel/mem/virtmem.h"

extern "C" uintptr_t KERNEL_VMA;

/// @brief Parses the multiboot2 information structure for relevant
/// information to the kernel and updates data with the parsed values.
/// @param MBI A pointer to the multiboot2 information structure.
void parse_multiboot(const MB2_information_structure* MBI);

/// @brief Parses the multiboot2 memory map tag and entries to
/// collect information about the available memory regions.
/// @param memmap_tag The memory map tag to parse.
void parse_memmap(MB2_tag_memmap* memmap_tag);
23 changes: 23 additions & 0 deletions src/intf/kernel/mem/bitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

#define BIT_TO_INDEX(n) (n / (8 * sizeof(size_t)))
#define BIT_TO_OFFSET(n) (n % (8 * sizeof(size_t)))

typedef struct bitmap {
private:
size_t length;
size_t* data;

public:
bitmap(void* data, size_t length) : length(length), data(reinterpret_cast<size_t*>(data))
{
memset(data, 0, DIV_ROUND_UP(length, sizeof(size_t)) * sizeof(size_t));
}

inline bool test(size_t bit);
inline void set(size_t bit);
inline void clear(size_t bit);
} bitmap;
18 changes: 18 additions & 0 deletions src/intf/kernel/mem/virtmem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <stdint.h>
#include <stddef.h>

#include "multiboot2.h"
#include "kernel/mem/bitmap.h"

#define PAGE_SIZE 4096

extern "C" uint64_t PML4[];
extern "C" uint64_t PDPT[];
extern "C" uint64_t PDPT_HH[];
extern "C" uint64_t PD_KERN[];

void init_virtmem(MB2_tag_memmap* memmap);
void* virtmem_alloc(size_t pages);
int virtmem_free(void* ptr, size_t pages);
8 changes: 8 additions & 0 deletions src/intf/kernel/util/panic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "util/attributes.h"

/// @brief Prints a panic message to the screen in white on red.
/// Clears interrupts and hangs the CPU indefinitely.
/// @param fmt The message to print.
void __noreturn kpanic(const char* fmt, ...);
37 changes: 37 additions & 0 deletions src/intf/klib/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This memory manager "liballoc" is taken from https://github.com/blanham/liballoc.
* It is licensed in the public domain.
*/

#pragma once

#include <stddef.h>
#include <stdint.h>

/// @brief This function is supposed to lock the memory data structures.
/// It could be as simple as disabling interrupts or acquiring a spinlock.
/// @return 0 if the lock was acquired successfully. Anything else is failure.
extern "C" int liballoc_lock();

/// @brief This function unlocks what was previously locked by the liballoc_lock
/// function. If it disabled interrupts, it enables interrupts. If it
/// had acquiried a spinlock, it releases the spinlock. etc.
/// @return 0 if the lock was successfully released.
extern "C" int liballoc_unlock();

/// @brief This is the hook into the local system which allocates pages.
/// The page size was set up in the liballoc_init function.
/// @param pages The number of pages required.
/// @return A pointer to the allocated memory or NULL if the pages were not allocated.
extern "C" void* liballoc_alloc(size_t pages);

/// @brief This frees previously allocated memory.
/// @param ptr The same value returned from a previous liballoc_alloc call.
/// @param pages The number of pages to free.
/// @return 0 if the memory was successfully freed.
extern "C" int liballoc_free(void* ptr, size_t pages);

extern void *kmalloc(size_t);
extern void *krealloc(void *, size_t);
extern void *kcalloc(size_t, size_t);
extern void kfree(void *);
25 changes: 25 additions & 0 deletions src/intf/klib/stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <stdarg.h>

#define PRINTF_BUFFER_SIZE (8 * sizeof(int) + 1) // 1 char per byte + null terminator

/// @brief Converts an integer into a string with a radix.
/// @param val The integer to convert.
/// @param buf The string buffer to write to.
/// @param radix The radix of the integer to write.
/// @return A pointer to the written buffer.
char* itoa(int val, char* buf, int radix);

/// @brief Writes the string pointed to by `fmt` to the VGA buffer.
/// When format specifiers are present, they are replaced by the
/// corresponding additional arguments.
/// @param fmt The string with optional format specifiers to print.
void printf(const char* fmt, ...);

/// @brief Writes the string pointed to by `fmt` to the VGA buffer.
/// When format specefiers are present, they are replaced by the
/// corresponding additional arguments.
/// @param fmt The string with optional format specifiers to print.
/// @param args The argument list.
void vprintf(const char* fmt, va_list args);
9 changes: 9 additions & 0 deletions src/intf/klib/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

void* memset(void* dest, int ch, size_t count);
void* memcpy(void* dest, const void* src, size_t count);

size_t strlen(const char* str);
Loading