refactoring

This commit is contained in:
Tyler Murphy 2023-07-17 19:34:52 -04:00
parent e6bec1afcd
commit 7a912d1b66
52 changed files with 563 additions and 693 deletions

4
.env
View file

@ -1,4 +1,4 @@
ARCH = i386
ARCH = i686
FORMAT = elf
CC = $(ARCH)-$(FORMAT)-gcc
LD = $(ARCH)-$(FORMAT)-ld
@ -8,4 +8,4 @@ AR = ar
CFLAGS = -ffreestanding -m32 -O2 -Wall -Wextra -pedantic # -DKERNEL_LOG
LDFLAGS = -nostdlib
QEMU = qemu-system-$(ARCH)
QEMU = qemu-system-i386

View file

@ -1,9 +1,11 @@
include ../.env
C_SRC = $(shell find src -type f -name "*.c")
C_SRC = $(shell find src -type f -name "*.c" -not -path "src/arch/*")
C_SRC += $(shell find src/arch/$(ARCH) -type f -name "*.c")
C_OBJ = $(patsubst %.c,bin/%.o, $(C_SRC))
ASM_SRC = $(shell find src -type f -name "*.asm")
ASM_SRC = $(shell find src -type f -name "*.asm" -not -path "src/arch/*")
ASM_SRC += $(shell find src/arch/$(ARCH) -type f -name "*.asm")
ASM_OBJ = $(patsubst %.asm,bin/%.asm.o, $(ASM_SRC))
CFLAGS += -Iinclude -Isrc -I../libk/include -std=gnu17
@ -22,7 +24,7 @@ $(ASM_OBJ): bin/%.asm.o : %.asm
bin/kernel.bin: $(C_OBJ) $(ASM_OBJ)
@mkdir -p $(@D)
$(LD) -o bin/kernel.bin -T linker.ld $(C_OBJ) $(ASM_OBJ) ../libk/bin/libk.a $(LDFLAGS)
$(LD) -o bin/kernel.bin -T arch/$(ARCH)/linker.ld $(C_OBJ) $(ASM_OBJ) ../libk/bin/libk.a $(LDFLAGS)
clean:
rm -fr bin

View file

@ -6,13 +6,11 @@ SECTIONS {
kernel_start = .;
.multiboot.data : {
*(.multiboot.data)
}
.multiboot.text : {
*(.multiboot.text)
}
.bootstrap : {
*(.bootstrap.data)
*(.bootstrap.stack)
*(.bootstrap.text)
}
. += 0xC0000000;
@ -32,7 +30,7 @@ SECTIONS {
{
*(COMMON)
*(.bss)
*(.bootstrap_stack)
*(.stack)
}
kernel_end = .;

View file

@ -102,5 +102,5 @@ struct FixedACPIDescriptionTable {
struct GenericAddressStructure x_gpe1_block;
};
void acpi_init(void);
void acpi_init(void *rsdp);
void acpi_poweroff(void);

View file

@ -0,0 +1,13 @@
#include <stdint.h>
extern uint8_t inb(uint16_t port);
extern void outb(uint16_t port, uint8_t val);
extern uint16_t inw(uint16_t port);
extern void outw(uint16_t port, uint16_t val);
extern uint32_t inl(uint16_t port);
extern void outl(uint16_t port, uint32_t val);
extern void io_wait(void);
extern void int_enable(void);
extern void int_disable(void);
extern void int_wait(void);
extern void halt(void);

View file

@ -0,0 +1,7 @@
#pragma once
#include <time.h>
extern void rtc_update(void);
extern struct Time rtc_utctime(void);
extern struct Time rtc_localtime(enum Timezone tz);

View file

@ -0,0 +1,6 @@
#pragma once
#define IDT_SIZE 256
#define IDT_INTERRUPTS 256
void idt_init(void);

View file

@ -2,9 +2,8 @@
#include <stdbool.h>
#include <stdint.h>
#include "acpi/acpi.h"
#include "memory/memory.h"
#include <arch/i686/mboot.h>
#include <arch/i686/memory.h>
#define CMDLINE_MAX 32

View file

@ -4,7 +4,7 @@
#define PIC_REMAP_OFFSET 0x20
void pic_remap(uint8_t offset);
void pic_remap(void);
void pic_mask(int irq);
void pic_unmask(int irq);
void pic_disable(void);

View file

@ -0,0 +1,32 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
enum vga_color {
VGA_BLACK = 0,
VGA_BLUE = 1,
VGA_GREEN = 2,
VGA_CYAN = 3,
VGA_RED = 4,
VGA_MAGENTA = 5,
VGA_BROWN = 6,
VGA_LIGHT_GREY = 7,
VGA_DARK_GREY = 8,
VGA_LIGHT_BLUE = 9,
VGA_LIGHT_GREEN = 10,
VGA_LIGHT_CYAN = 11,
VGA_LIGHT_RED = 12,
VGA_LIGHT_MAGENTA = 13,
VGA_LIGHT_BROWN = 14,
VGA_WHITE = 15,
};
#define VGA_TEXT_W 80
#define VGA_TEXT_H 25
void vgatext_write_char(char c, enum vga_color color, uint8_t x, uint8_t y);
void vgatext_write_data(uint16_t data, uint16_t index);
void vgatext_write_buf(const uint16_t *buffer);
void vgatext_cur_mov(uint8_t x, uint8_t y);
void vgatext_cur_resize(uint8_t start, uint8_t end);
void vgatext_cur_visible(bool visible);

6
kernel/include/memory.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
extern int memory_lock(void);
extern int memory_unlock(void);
extern void *memory_alloc_page(int);
extern int memory_free_page(void* ,int);

View file

@ -7,17 +7,3 @@ extern void putchar(int c);
extern void puts(const char* s);
extern void printk(const char *restrict format, ...);
extern void vprintk(const char *restrict format, va_list ap);
#ifdef KERNEL_LOG
#define debugk(msg, ...) _debugk_impl(msg, ## __VA_ARGS__)
#define succek(msg, ...) _succek_impl(msg, ## __VA_ARGS__)
#define errork(msg, ...) _errork_impl(msg, ## __VA_ARGS__)
#else
#define debugk(msg, ...)
#define succek(msg, ...)
#define errork(msg, ...)
#endif
extern void _debugk_impl(char* msg, ...);
extern void _succek_impl(char* msg, ...);
extern void _errork_impl(char* msg, ...);

10
kernel/include/sys.h Normal file
View file

@ -0,0 +1,10 @@
#pragma once
extern void arch_init(void *boot_info);
extern void arch_update(void);
extern void arch_halt(void);
extern void arch_wait_io(void);
extern void arch_wait_int(void);
extern void arch_disable_int(void);
extern void arch_enable_int(void);
extern void arch_poweroff(void);

View file

@ -2,18 +2,15 @@
#include <stdint.h>
#include <stddef.h>
#include <drivers/vga.h>
#include "color.h"
#define TERM_W 80
#define TERM_H 25
#define TERM_W VGA_TEXT_W
#define TERM_H VGA_TEXT_H
void term_init(void);
void term_reset(void);
void term_setfg(enum VGAColor color);
void term_setbg(enum VGAColor color);
void term_setfg(enum vga_color color);
void term_setbg(enum vga_color color);
void term_clear(void);
void term_scroll(int lines);
void term_setpos(uint8_t x, uint8_t y);

View file

@ -16,10 +16,25 @@ struct Time {
int leap; /// If year is a leap year (True == 1)
};
extern void rtc_update(void);
extern void rtc_set_timezone(int offset);
extern struct Time *rtc_utctime(void);
extern struct Time *rtc_localtime(void);
enum Timezone {
UTC = 0,
EST = -4
};
/**
* Sets the current timezone
*/
extern void set_timezone(enum Timezone tz);
/**
* Returns current time in UTC
*/
extern struct Time get_utctime(void);
/**
* Returns current time from current Timezone
*/
extern struct Time get_localtime(void);
/**
* Converts the time into a string format

View file

@ -3,11 +3,10 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sys.h>
#include <arch/i686/acpi.h>
#include <arch/i686/asm.h>
#include "acpi.h"
#include "boot/tag.h"
#include "print.h"
extern uintptr_t KERNEL_MAPPING;
static struct RootSystemDescriptionTable *rsdt;
static struct FixedACPIDescriptionTable *fadt;
@ -17,9 +16,7 @@ static uint16_t SLP_TYPb;
static uint16_t SLP_EN;
static uint16_t SCI_EN;
static bool is_init = false;
bool checksum(uint8_t *data, size_t len) {
static bool checksum(uint8_t *data, size_t len) {
unsigned char sum = 0;
for (size_t i = 0; i < len; i++)
sum += data[i];
@ -39,7 +36,7 @@ static void *find_fadt(void) {
return NULL;
}
static int read_s5_addr(void) {
static void read_s5_addr(void) {
uintptr_t ptr = fadt->dsdt;
char *s5_addr = (void*) (ptr + 36);
@ -69,73 +66,41 @@ static int read_s5_addr(void) {
SCI_EN = 1;
} else {
errork("\\_S5 parse error.");
return 1;
panic("\\_S5 parse error.");
}
} else {
errork("\\_S5 not present.");
return 1;
panic("\\_S5 not present.");
}
return 0;
}
void acpi_init(void) {
void acpi_init(void *ptr) {
is_init = false;
debugk("Loading ACPI");
struct BootTag *tag;
if(!get_boot_tag(ID_RSDP, &tag)) {
errork("Could not find RSDP");
return;
}
debugk("Loading RSDT");
struct RootSystemDescriptionPointer *rsdp = tag->data.rsdp;
struct RootSystemDescriptionPointer *rsdp = ptr;
if (!checksum((uint8_t*) rsdp, sizeof(struct RootSystemDescriptionPointer))) {
errork("RSDP checksum failed to validate");
return;
panic("RSDP checksum failed to validate");
}
uintptr_t rsdt_ptr = rsdp->rsdt_address;
rsdt = (void *) rsdt_ptr;
if (!checksum((uint8_t*) &rsdt->header, rsdt->header.length)) {
errork("RSDT checksum failed to validate");
return;
panic("RSDT checksum failed to validate");
}
debugk("Loading FADT");
fadt = find_fadt();
if (fadt == NULL) {
errork("Could not find FADT");
return;
panic("Could not find FADT");
}
if (!checksum((uint8_t*) &fadt->header, fadt->header.length)) {
errork("FADT checksum failed to validate");
return;
panic("FADT checksum failed to validate");
}
debugk("Reading \\_S5 Addr");
if (read_s5_addr()) {
return;
}
read_s5_addr();
outb(fadt->smi_command_port,fadt->acpi_enable);
succek("ACPI has been loaded");
is_init = true;
}
void acpi_poweroff(void) {
if (is_init) {
outw((unsigned int) fadt->pm1_a_control_block, SLP_TYPb | SLP_EN);
panic("failed to shutdown");
} else {
errork("Cannot shutdown, ACPI not loaded");
}
outw((unsigned int) fadt->pm1_a_control_block, SLP_TYPb | SLP_EN);
panic("failed to shutdown");
}

View file

@ -1,51 +1,51 @@
#include <stdint.h>
#include <arch/i686/asm.h>
static inline uint8_t inb(uint16_t port) {
uint8_t inb(uint16_t port) {
uint8_t ret;
__asm__ volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void outb(uint16_t port, uint8_t val) {
void outb(uint16_t port, uint8_t val) {
__asm__ volatile ("outb %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint16_t inw(uint16_t port) {
uint16_t inw(uint16_t port) {
uint16_t ret;
__asm__ volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void outw(uint16_t port, uint16_t val) {
void outw(uint16_t port, uint16_t val) {
__asm__ volatile ("outw %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint32_t inl(uint16_t port) {
uint32_t inl(uint16_t port) {
uint32_t ret;
__asm__ volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void outl(uint16_t port, uint32_t val) {
void outl(uint16_t port, uint32_t val) {
__asm__ volatile ("outl %0, %1" : : "a"(val), "Nd"(port));
}
static inline void io_wait(void) {
void io_wait(void) {
outb(0x80, 0);
}
static inline void int_enable(void) {
void int_enable(void) {
__asm__ volatile ("sti");
}
static inline void int_disable(void) {
void int_disable(void) {
__asm__ volatile ("cli");
}
static inline void int_wait(void) {
void int_wait(void) {
__asm__ volatile ("sti; hlt");
}
static inline void halt(void) {
void halt(void) {
__asm__ volatile ("cli; hlt");
}

View file

@ -1,9 +1,7 @@
#include <panic.h>
#include <sys.h>
#include "print.h"
#include "ps2ctrl.h"
#include "interrupt/pic.h"
#include <arch/i686/drivers/ps2ctrl.h>
#include <arch/i686/pic.h>
#include <arch/i686/asm.h>
#define STATUS_OUT_BUF ((uint8_t)0x01)
#define STATUS_IN_BUF ((uint8_t)0x02)

View file

@ -1,10 +1,8 @@
#include <panic.h>
#include <sys.h>
#include "print.h"
#include "ps2kb.h"
#include "ps2ctrl.h"
#include "interrupt/pic.h"
#include <arch/i686/drivers/ps2ctrl.h>
#include <arch/i686/pic.h>
#include <arch/i686/asm.h>
#include <drivers/ps2kb.h>
#define BUFFER_LEN 16

View file

@ -1,12 +1,8 @@
#include <panic.h>
#include <stdbool.h>
#include <string.h>
#include <sys.h>
#include "print.h"
#include "ps2mouse.h"
#include "drivers/ps2ctrl.h"
#include "interrupt/pic.h"
#include <arch/i686/drivers/ps2ctrl.h>
#include <arch/i686/pic.h>
#include <arch/i686/asm.h>
#include <drivers/ps2mouse.h>
static bool is_init = false;

View file

@ -1,7 +1,8 @@
#include "print.h"
#include "time.h"
#include <stdint.h>
#include <sys.h>
#include <time.h>
#include <arch/i686/drivers/rtc.h>
#include <arch/i686/asm.h>
#define CMOS_WRITE_PORT 0x70
#define CMOS_READ_PORT 0x71
@ -15,9 +16,16 @@
#define CMOS_REG_YEAR 0x09
#define CMOS_REG_CEN 0x32
// Live buffers to work on data
static struct Time time;
static struct Time localtime;
static int cur_offset = 0;
// Front buffers so interupts dont request data that is half done
static struct Time cur_time;
static struct Time cur_localtime;
// Current set Time Zone
static enum Timezone last_timezone = UTC;
static uint8_t cmos_read(uint8_t reg) {
uint8_t hex, ret;
@ -46,10 +54,12 @@ static void update_localtime(void) {
// set localtime
localtime = time;
// do we acutally need to do anything
if (cur_offset == 0) return;
localtime.hour += cur_offset;
// if tz is UTC, we dont need to do anythin
if (last_timezone == UTC) {
cur_localtime = localtime;
return;
}
// check if day rolled over
change = localtime.hour < 0 ? -1 : localtime.hour >= 24 ? 1 : 0;
if (!change) return;
@ -98,13 +108,11 @@ year:
localtime.yday = 0;
localtime.year -= 1900;
cur_localtime = localtime;
}
void rtc_set_timezone(int offset) {
cur_offset = offset;
}
void rtc_update(void) {
time.sec = cmos_read(CMOS_REG_SEC);
time.min = cmos_read(CMOS_REG_MIN);
@ -127,12 +135,18 @@ void rtc_update(void) {
time.year -= 1900;
update_localtime();
cur_time = time;
}
struct Time *rtc_utctime(void) {
return &time;
struct Time rtc_utctime(void) {
return cur_time;
}
struct Time *rtc_localtime(void) {
return &localtime;
struct Time rtc_localtime(enum Timezone tz) {
if (tz != last_timezone) {
last_timezone = tz;
update_localtime();
}
return cur_localtime;
}

View file

@ -0,0 +1,50 @@
#include <arch/i686/asm.h>
#include <drivers/vga.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
static uint16_t *buffer = (uint16_t*) 0xC03FF000;
static uint8_t start, end;
void vgatext_write(char c, enum vga_color color, uint8_t x, uint8_t y) {
const size_t index = y * VGA_TEXT_W + x;
buffer[index] = c | (uint16_t) color << 8;
}
void vgatext_write_data(uint16_t data, uint16_t index) {
buffer[index] = data;
}
void vgatext_write_buf(const uint16_t *src) {
memcpy(buffer, src, VGA_TEXT_W * VGA_TEXT_H * sizeof(uint16_t));
}
void vgatext_cur_mov(uint8_t x, uint8_t y) {
; const uint16_t pos = y * VGA_TEXT_W + x;
outb(0x3D4, 0x0F);
outb(0x3D5, (uint8_t) (pos & 0xFF));
outb(0x3D4, 0x0E);
outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF));
}
void vgatext_cur_resize(uint8_t s, uint8_t e) {
start = s;
end = e;
outb(0x3D4, 0x0A);
outb(0x3D5, (inb(0x3D5) & 0xC0) | start);
outb(0x3D4, 0x0B);
outb(0x3D5, (inb(0x3D5) & 0xE0) | end);
}
void vgatext_cur_visible(bool visible) {
if (visible) {
vgatext_cur_resize(start, end);
} else {
outb(0x3D4, 0x0A);
outb(0x3D5, 0x20);
}
}

View file

@ -0,0 +1,42 @@
global load_gdt
%macro gdt_entry 4
db %2 & 0xff
db (%2 >> 8) & 0xff
db %1 & 0xff
db (%1 >> 8) & 0xff
db (%1 >> 16) & 0xff
db %3
db ((%2 >> 16) & 0x0f) | (%4 << 4)
db (%1 >> 24) & 0xff
%endmacro
section .rodata
align 16
gdt_start:
gdt_entry 0, 0, 0, 0
gdt_entry 0, 0xFFFFF, 0x9A, 0xC
gdt_entry 0, 0xFFFFF, 0x92, 0xC
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
section .text
align 8
load_gdt:
cli
lgdt [gdt_descriptor]
mov eax, cr0
or al, 1
mov cr0, eax
mov ax, 0x10
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
jmp 0x08:after_gdt
after_gdt:
ret

View file

@ -5,16 +5,42 @@
#include <sys.h>
#include <print.h>
#include <panic.h>
#include <arch/i686/pic.h>
#include <arch/i686/idt.h>
#include <time.h>
#include <drivers/ps2kb.h>
#include <drivers/ps2mouse.h>
#include "acpi/acpi.h"
#include "drivers/ps2kb.h"
#include "drivers/ps2mouse.h"
#include "time.h"
#include "tty/color.h"
#include "idt.h"
#include "pic.h"
#include "tty/term.h"
struct IdtEntry {
uint16_t isr_low;
uint16_t kernel_cs;
uint8_t _reserved;
uint8_t attributes;
uint16_t isr_high;
} __attribute__((packed));
struct Idtr {
uint16_t limit;
uint32_t base;
} __attribute__((packed));
enum IDTFlags {
IDT_FLAG_GATE_TASK = 0x5,
IDT_FLAG_GATE_16BIT_INT = 0x6,
IDT_FLAG_GATE_16BIT_TRAP = 0x7,
IDT_FLAG_GATE_32BIT_INT = 0xE,
IDT_FLAG_GATE_32BIT_TRAP = 0xF,
IDT_FLAG_RING0 = (0 << 5),
IDT_FLAG_RING1 = (1 << 5),
IDT_FLAG_RING2 = (2 << 5),
IDT_FLAG_RING3 = (3 << 5),
IDT_FLAG_PRESENT = 0x80,
};
#define WIDTH 30
static char buf[WIDTH];
static int timer = -1;
@ -35,7 +61,8 @@ void idt_pic_timer(void) {
for (size_t i = 0; i < WIDTH; i++) putchar(' ');
term_setpos(TERM_W - WIDTH - 1, 0);
timetostr(rtc_localtime(), "%a %b %d %Y %H:%M:%S", buf, WIDTH);
struct Time t = get_localtime();
timetostr(&t, "%a %b %d %Y %H:%M:%S", buf, WIDTH);
printk("%s", buf);
term_load(state);
@ -101,9 +128,6 @@ static void set_descriptor(uint8_t vector, void* isr, uint8_t flags) {
}
void idt_init(void) {
debugk("Loading IDT");
idtr.base = (uintptr_t)&idt[0];
idtr.limit = (uint16_t)sizeof(struct IdtEntry) * IDT_SIZE - 1;
@ -112,7 +136,5 @@ void idt_init(void) {
}
__asm__ volatile ("lidt %0" : : "m"(idtr));
succek("IDT has been loaded");
}

View file

@ -1,10 +1,8 @@
#include <panic.h>
#include <string.h>
#include "print.h"
#include "tag.h"
#include "acpi/acpi.h"
#include "memory/memory.h"
#include <arch/i686/mboot.h>
#include <arch/i686/memory.h>
#include <arch/i686/acpi.h>
static struct BootInfo info;

View file

@ -1,11 +1,10 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys.h>
#include <panic.h>
#include "memory.h"
#include "boot/tag.h"
#include "print.h"
#include <arch/i686/memory.h>
#include <arch/i686/mboot.h>
struct MemoryArea {
uint32_t len;
@ -25,12 +24,12 @@ static uint32_t page_free_start;
static struct MemoryArea *page_start;
int memory_lock(void) {
int_disable();
// int_disable();
return 0;
}
int memory_unlock(void) {
int_enable();
// int_enable();
return 0;
}
@ -114,8 +113,6 @@ int memory_free_page(void *ptr, int pages) {
void memory_init(void) {
debugk("Loading memory pages");
memory_lock();
bitmap = NULL;
@ -185,8 +182,6 @@ void memory_init(void) {
memory_unlock();
succek("Memory loaded. %k total %k free", total_memory, free_memory);
}
uint32_t memory_total(void) {

View file

@ -0,0 +1,62 @@
global page_directory
global paging_init
global paging_finish
global KERNEL_MAPPING
KERNEL_MAPPING equ 0xC0000000
VGABUF equ 0x000B8000
section .bss
align 4096
page_directory:
resb 4096
page_table1:
resb 4096
section .bootstrap.text
align 8
paging_init:
extern kernel_start
extern kernel_end
paging_load:
mov edi, page_table1 - KERNEL_MAPPING
mov esi, 0
mov ecx, 1023
paging_cmp:
cmp esi, kernel_start
jl paging_add
cmp esi, kernel_end - KERNEL_MAPPING
jge paging_map
mov edx, esi
or edx, 0x003
mov [edi], edx
paging_add:
add esi, 4096
add edi, 4
loop paging_cmp
paging_map:
mov dword [page_table1 - KERNEL_MAPPING + 1023 * 4], VGABUF | 0x003
mov dword [page_directory - KERNEL_MAPPING + 0], page_table1 - KERNEL_MAPPING + 0x003
mov dword [page_directory - KERNEL_MAPPING + 768 * 4], page_table1 - KERNEL_MAPPING + 0x003
mov ecx, page_directory - KERNEL_MAPPING
mov cr3, ecx
mov ecx, cr0
or ecx, 0x80010000
mov cr0, ecx
ret
section .text
align 8
paging_finish:
mov dword [page_directory], 0
mov ecx, cr3
mov cr3, ecx
ret

View file

@ -1,17 +1,14 @@
#include <sys.h>
#include <print.h>
#include "pic.h"
#include <arch/i686/pic.h>
#include <arch/i686/asm.h>
#define PIC1_COMMAND_PORT 0x20
#define PIC1_DATA_PORT 0x21
#define PIC2_COMMAND_PORT 0xA0
#define PIC2_DATA_PORT 0xA1
void pic_remap(uint8_t offset) {
debugk("Remapping PIC");
void pic_remap(void) {
char a1 = inb(PIC1_DATA_PORT);
char a2 = inb(PIC2_DATA_PORT);
// control word 1
@ -22,9 +19,9 @@ void pic_remap(uint8_t offset) {
io_wait();
// control word 2
// interrupt offset
outb(PIC1_DATA_PORT, offset);
outb(PIC1_DATA_PORT, PIC_REMAP_OFFSET);
io_wait();
outb(PIC2_DATA_PORT, offset + 8);
outb(PIC2_DATA_PORT, PIC_REMAP_OFFSET + 8);
io_wait();
// control word 3
// primary pic: set which pin secondary is connected to
@ -42,8 +39,6 @@ void pic_remap(uint8_t offset) {
// clear data registers
outb(PIC1_DATA_PORT, a1);
outb(PIC2_DATA_PORT, a2);
succek("PIC has been remapped to offset 0x%X", offset);
}
void pic_mask(int irq) {

View file

@ -0,0 +1,82 @@
global start
bits 32
; create the multiboot header
section .bootstrap.data
align 8
mb_start:
dd 0xe85250d6
dd 0
dd mb_end - mb_start
dd -(0xe85250d6 + (mb_end - mb_start))
dw 0
dw 0
dd 8
mb_end:
; create the stack for bootstrapping
section .bootstrap.stack
align 16
bootstrap_stack_start:
resb 1024 ; 1 KiB
bootstrap_stack_end:
; create the stack for the kernel
section .stack
align 16
stack_start:
resb 16384 ; 16 KiB
stack_end:
; load the kernel into the higher half
section .bootstrap.text
align 8
start:
; init bootstrap stack
mov esp, bootstrap_stack_end
mov ebp, bootstrap_stack_end
; load kernel into higher half and init paging
extern paging_init
call paging_init
jmp near load_kernel
; initalize kernel after it has been loaded in higher half
section .text
align 8
load_kernel:
; init stack
mov esp, stack_end
mov ebp, stack_end
; load global descripter table
extern load_gdt
call load_gdt
; unmap kernel at page 0
extern paging_finish
call paging_finish
; initalize the FPU
finit
; push multiboot header
extern KERNEL_MAPPING
add ebx, KERNEL_MAPPING
push ebx
; start kernel
sti
call kernel_main
extern kernel_main
; hlt forever if kernel quits (it should never)
cli
halt:
hlt
jmp halt

View file

@ -0,0 +1,51 @@
#include <sys.h>
#include <arch/i686/asm.h>
#include <arch/i686/acpi.h>
#include <arch/i686/drivers/rtc.h>
#include <arch/i686/idt.h>
#include <arch/i686/pic.h>
#include <arch/i686/mboot.h>
#include <arch/i686/drivers/ps2ctrl.h>
void arch_init(void *boot_info) {
rtc_update();
idt_init();
pic_remap();
load_boot_info(boot_info);
/* havent refactored to work with paging yet */
// acpi_init();
// memory_init();
ps2ctrl_init();
}
extern void arch_update(void) {
rtc_update();
}
void arch_halt(void) {
halt();
}
void arch_wait_io(void) {
io_wait();
}
void arch_disable_int(void) {
int_disable();
}
void arch_enable_int(void) {
int_enable();
}
extern void arch_wait_int(void) {
int_wait();
}
void arch_poweroff(void) {
acpi_poweroff();
}

View file

@ -0,0 +1,17 @@
#include <time.h>
#include <arch/i686/drivers/rtc.h>
static enum Timezone cur_tz = UTC;
struct Time get_utctime(void) {
return rtc_utctime();
}
struct Time get_localtime(void) {
return rtc_localtime(cur_tz);
}
void set_timezone(enum Timezone tz) {
cur_tz = tz;
}

View file

@ -1,18 +0,0 @@
#include "cpu.h"
#include "print.h"
extern uint8_t sse_init (void);
extern uint8_t fpu_init (void);
uint8_t init_registers (uint8_t reg) {
uint8_t res = 0;
if (reg & SSE_REG)
res |= ~sse_init() & SSE_REG;
if (reg & FPU_REG)
res |= ~fpu_init() & FPU_REG;
return res;
}

View file

@ -1,8 +0,0 @@
#pragma once
#include <stdint.h>
#define SSE_REG 0x01
#define FPU_REG 0x02
uint8_t init_registers (uint8_t reg);

View file

@ -1,19 +0,0 @@
global fpu_init
fpu_init:
mov edx, cr0
and edx, (-1) - ((1 << 3) + (1 << 4))
mov cr0, edx
fninit
fnstsw [test]
cmp word [test], 0
jne no_fpu
mov eax, 0
ret
no_fpu:
mov eax, 1
ret
test: dw 0x55AA

View file

@ -1,22 +0,0 @@
global sse_init
sse_init:
mov eax, 0x1
cpuid
test edx, 1<<25
jmp no_sse
mov eax, cr0
and ax, 0xFFFB
or ax, 0x2
mov cr0, eax
mov eax, cr4
or ax, 3 << 9
mov cr4, eax
mov eax, 0
ret
no_sse:
mov eax, 1
ret

View file

@ -1,36 +0,0 @@
#pragma once
#include <stdint.h>
#define IDT_SIZE 256
#define IDT_INTERRUPTS 256
struct IdtEntry {
uint16_t isr_low;
uint16_t kernel_cs;
uint8_t _reserved;
uint8_t attributes;
uint16_t isr_high;
} __attribute__((packed));
struct Idtr {
uint16_t limit;
uint32_t base;
} __attribute__((packed));
enum IDTFlags {
IDT_FLAG_GATE_TASK = 0x5,
IDT_FLAG_GATE_16BIT_INT = 0x6,
IDT_FLAG_GATE_16BIT_TRAP = 0x7,
IDT_FLAG_GATE_32BIT_INT = 0xE,
IDT_FLAG_GATE_32BIT_TRAP = 0xF,
IDT_FLAG_RING0 = (0 << 5),
IDT_FLAG_RING1 = (1 << 5),
IDT_FLAG_RING2 = (2 << 5),
IDT_FLAG_RING3 = (3 << 5),
IDT_FLAG_PRESENT = 0x80,
};
void idt_init(void);

View file

@ -1,52 +1,29 @@
#include "acpi/acpi.h"
#include "boot/tag.h"
#include "cpu/cpu.h"
#include "drivers/ps2ctrl.h"
#include "drivers/ps2kb.h"
#include "drivers/ps2mouse.h"
#include "interrupt/idt.h"
#include "interrupt/pic.h"
#include "time.h"
#include "tty/cursor.h"
#include "tty/term.h"
#include <drivers/ps2kb.h>
#include <drivers/ps2mouse.h>
#include <panic.h>
#include <print.h>
#include <sys.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys.h>
#include <term.h>
static double x = 0, y = 0;
void kernel_boot(void *boot_info) {
(void)boot_info;
}
void kernel_main(void) {
void kernel_main(void *boot_info) {
term_init();
cursor_enable();
rtc_set_timezone(-4);
rtc_update();
idt_init();
pic_remap(PIC_REMAP_OFFSET);
// load_boot_info(boot_info);
// acpi_init();
// memory_init();
ps2ctrl_init();
arch_init(boot_info);
ps2kb_init();
ps2mouse_init();
init_registers(FPU_REG | SSE_REG);
set_timezone(EST);
while(1) {
int_wait();
arch_wait_int();
arch_update();
struct Keycode code = ps2kb_get();
if(code.key != KEY_NONE) {
@ -60,7 +37,7 @@ void kernel_main(void) {
}
if (code.key == KEY_ESCAPE) {
acpi_poweroff();
arch_poweroff();
}
struct MouseEvent event = ps2mouse_get();
@ -76,6 +53,5 @@ void kernel_main(void) {
}
term_flush();
rtc_update();
}
}

View file

@ -1,6 +1,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
struct boundary_tag {
unsigned int magic;
@ -15,11 +16,6 @@ struct boundary_tag {
struct boundary_tag *prev;
};
extern int memory_lock(void);
extern int memory_unlock(void);
extern void *memory_alloc_page(int);
extern int memory_free_page(void* ,int);
#define ALLOC_MAGIC 0xc001c0de
#define MAXCOMPLETE 5
#define MAXEXP 32

View file

@ -3,13 +3,11 @@
#include <stdlib.h>
#include <panic.h>
#include <print.h>
#include "tty/color.h"
#include "tty/term.h"
#include <term.h>
__attribute__((noreturn))
void _panic_impl(char* msg, int line, char* file, ...) {
int_disable();
arch_disable_int();
va_list args;
va_start(args, file);
term_clear();
@ -22,6 +20,6 @@ void _panic_impl(char* msg, int line, char* file, ...) {
printk("\nin %s at line %d\n", file, line);
term_flush();
while(1) {
halt();
}
arch_halt();
}
}

View file

@ -1,10 +1,9 @@
#include "tty/color.h"
#include "tty/term.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <print.h>
#include <term.h>
void printk(const char *restrict format, ...) {
va_list args;
@ -82,47 +81,3 @@ void vprintk(const char *restrict format, va_list args) {
void puts(const char *s) {
for(; *s; s++) putchar(*s);
}
void printl(enum VGAColor color, const char* msg) {
term_setbg(VGA_BLACK);
term_setfg(VGA_WHITE);
putchar('[');
term_setfg(color);
printk("%s", msg);
term_setfg(VGA_WHITE);
putchar(']');
putchar(' ');
}
void _debugk_impl(char *format, ...) {
uint16_t color = term_save_col();
printl(VGA_LIGHT_CYAN, "LOG");
va_list args;
va_start(args, format);
vprintk(format, args);
va_end(args);
if (!term_newline()) putchar('\n');
term_load_col(color);
}
void _succek_impl(char *format, ...) {
uint16_t color = term_save_col();
printl(VGA_LIGHT_GREEN, "OK");
va_list args;
va_start(args, format);
vprintk(format, args);
va_end(args);
if (!term_newline()) putchar('\n');
term_load_col(color);
}
void _errork_impl(char *format, ...) {
uint16_t color = term_save_col();
printl(VGA_LIGHT_RED, "ERR");
va_list args;
va_start(args, format);
vprintk(format, args);
va_end(args);
if (!term_newline()) putchar('\n');
term_load_col(color);
}

View file

@ -1,135 +0,0 @@
global start
extern kernel_boot
extern kernel_main
extern kernel_start
extern kernel_end
bits 32
; base, limit, access, flags
%macro gdt_entry 4
db %2 & 0xff
db (%2 >> 8) & 0xff
db %1 & 0xff
db (%1 >> 8) & 0xff
db (%1 >> 16) & 0xff
db %3
db ((%2 >> 16) & 0x0f) | (%4 << 4)
db (%1 >> 24) & 0xff
%endmacro
MAGIC equ 0xe85250d6
FLAGS equ 0
LENGTH equ mb_end - mb_start
CHECKSUM equ -(MAGIC + LENGTH)
VGABUF equ 0x000B8000
KRNMAP equ 0xC0000000
section .multiboot.data
align 8
mb_start:
dd MAGIC
dd FLAGS
dd LENGTH
dd CHECKSUM
dw 0
dw 0
dd 8
mb_end:
section .rodata
align 16
gdt_start:
gdt_entry 0, 0, 0, 0
gdt_entry 0, 0xFFFFF, 0x9A, 0xC
gdt_entry 0, 0xFFFFF, 0x92, 0xC
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
section .bootstrap_stack
align 16
stack_start:
resb 16384 ; 16 KiB
stack_end:
section .bss
align 4096
page_directory:
resb 4096
page_table1:
resb 4096
section .multiboot.text
align 8
start:
; push ebx
; call kernel_boot
; pop eax
mov edi, page_table1 - KRNMAP
mov esi, 0
mov ecx, 1023
paging_cmp:
cmp esi, kernel_start
jl paging_add
cmp esi, kernel_end - KRNMAP
jge paging_map
mov edx, esi
or edx, 0x003
mov [edi], edx
paging_add:
add esi, 4096
add edi, 4
loop paging_cmp
paging_map:
mov dword [page_table1 - KRNMAP + 1023 * 4], VGABUF | 0x003
mov dword [page_directory - KRNMAP + 0], page_table1 - KRNMAP + 0x003
mov dword [page_directory - KRNMAP + 768 * 4], page_table1 - KRNMAP + 0x003
mov ecx, page_directory - KRNMAP
mov cr3, ecx
mov ecx, cr0
or ecx, 0x80010000
mov cr0, ecx
lea ecx, load_gdt
jmp near ecx
section .text
align 8
load_gdt:
cli
lgdt [gdt_descriptor]
mov eax, cr0
or al, 1
mov cr0, eax
jmp 0x08:load_kernel
load_kernel:
mov dword [page_directory], 0
mov ecx, cr3
mov cr3, ecx
mov ax, 0x10
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov esp, stack_end
mov ebp, stack_end
sti
call kernel_main
cli
halt:
hlt
jmp halt

View file

@ -5,12 +5,10 @@
#include <sys.h>
#include <print.h>
#include "drivers/vga.h"
#include "term.h"
#include "color.h"
#include "cursor.h"
uint16_t buffer[TERM_W * TERM_H * sizeof(uint16_t)];
uint16_t *front;
uint8_t x, y;
uint8_t color;
@ -28,7 +26,6 @@ static void term_clear_line(int y) {
void term_init (void) {
x = 0;
y = 0;
front = (uint16_t*) 0xC03FF000;
term_setfg(VGA_WHITE);
term_setbg(VGA_BLACK);
term_clear();
@ -37,11 +34,11 @@ void term_init (void) {
void term_setpos(uint8_t xp, uint8_t yp) {
x = xp;
y = yp;
cursor_setpos(x, y);
vgatext_cur_mov(x, y);
}
void term_scroll (int lines) {
int_disable();
arch_disable_int();
y -= lines;
if (!lines) return;
if(lines >= TERM_H || lines <= -TERM_H) {
@ -52,14 +49,14 @@ void term_scroll (int lines) {
} else {
memmove(buffer + lines * TERM_W, buffer + lines, (TERM_H + lines) * TERM_W);
}
int_enable();
arch_enable_int();
}
void term_setfg(enum VGAColor c) {
void term_setfg(enum vga_color c) {
color = (color & 0xF0) | c;
}
void term_setbg(enum VGAColor c) {
void term_setbg(enum vga_color c) {
color = (color & 0x0F) | c << 4;
}
@ -80,7 +77,7 @@ void term_load(uint32_t state) {
x = (uint8_t) (state >> 16);
y = (uint8_t) (state >> 8);
color = (uint8_t) (state >> 0);
cursor_setpos(x, y);
vgatext_cur_mov(x, y);
}
uint16_t term_save_col(void) {
@ -124,7 +121,7 @@ void putchar(int c) {
y = TERM_H - 1;
}
cursor_setpos(x, y);
vgatext_cur_mov(x, y);
}
bool term_newline(void) {
@ -132,7 +129,7 @@ bool term_newline(void) {
}
void term_flush(void) {
int_disable();
memcpy(front, buffer, TERM_W * TERM_H * sizeof(uint16_t));
int_enable();
arch_disable_int();
vgatext_write_buf(buffer);
arch_enable_int();
}

View file

@ -1,7 +1,5 @@
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <stddef.h>
static char* ABB_WEEKDAY[7] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"

View file

@ -1,67 +0,0 @@
#include "color.h"
#include "panic.h"
bool itoac(int i, enum AnsiiColor *color) {
if (i < 0 || i > 15) return false;
*color = i;
return true;
}
bool itovc(int i, enum VGAColor *color) {
if (
(i >= 30 && i <= 37) ||
(i >= 40 && i <= 47) ||
(i >= 90 && i <= 97) ||
(i >= 100 && i <= 107)
) {
*color = i;
return true;
}
return false;
}
enum VGAColor atovc(enum AnsiiColor color) {
switch(color) {
case ANSII_FRONT_BLACK:
case ANSII_FRONT_BLACK_EMPH:
case ANSII_BACK_BLACK:
case ANSII_BACK_BLACK_EMPH:
return VGA_BLACK;
case ANSII_FRONT_RED:
case ANSII_FRONT_RED_EMPH:
case ANSII_BACK_RED:
case ANSII_BACK_RED_EMPH:
return VGA_LIGHT_RED;
case ANSII_FRONT_GREEN:
case ANSII_FRONT_GREEN_EMPH:
case ANSII_BACK_GREEN:
case ANSII_BACK_GREEN_EMPH:
return VGA_LIGHT_GREEN;
case ANSII_FRONT_YELLOW:
case ANSII_FRONT_YELLOW_EMPH:
case ANSII_BACK_YELLOW:
case ANSII_BACK_YELLOW_EMPH:
return VGA_LIGHT_BROWN;
case ANSII_FRONT_BLUE:
case ANSII_FRONT_BLUE_EMPH:
case ANSII_BACK_BLUE:
case ANSII_BACK_BLUE_EMPH:
return VGA_LIGHT_BLUE;
case ANSII_FRONT_PURPLE:
case ANSII_FRONT_PURPLE_EMPH:
case ANSII_BACK_PURPLE:
case ANSII_BACK_PURPLE_EMPH:
return VGA_LIGHT_MAGENTA;
case ANSII_FRONT_CYAN:
case ANSII_FRONT_CYAN_EMPH:
case ANSII_BACK_CYAN:
case ANSII_BACK_CYAN_EMPH:
return VGA_LIGHT_CYAN;
case ANSII_FRONT_WHITE:
case ANSII_FRONT_WHITE_EMPH:
case ANSII_BACK_WHITE:
case ANSII_BACK_WHITE_EMPH:
return VGA_WHITE;
}
panic("this should not be reached (make gcc quiet)");
}

View file

@ -1,62 +0,0 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
enum VGAColor {
VGA_BLACK = 0,
VGA_BLUE = 1,
VGA_GREEN = 2,
VGA_CYAN = 3,
VGA_RED = 4,
VGA_MAGENTA = 5,
VGA_BROWN = 6,
VGA_LIGHT_GREY = 7,
VGA_DARK_GREY = 8,
VGA_LIGHT_BLUE = 9,
VGA_LIGHT_GREEN = 10,
VGA_LIGHT_CYAN = 11,
VGA_LIGHT_RED = 12,
VGA_LIGHT_MAGENTA = 13,
VGA_LIGHT_BROWN = 14,
VGA_WHITE = 15,
};
enum AnsiiColor {
ANSII_FRONT_BLACK = 30,
ANSII_FRONT_RED = 31,
ANSII_FRONT_GREEN = 32,
ANSII_FRONT_YELLOW = 33,
ANSII_FRONT_BLUE = 34,
ANSII_FRONT_PURPLE = 35,
ANSII_FRONT_CYAN = 36,
ANSII_FRONT_WHITE = 37,
ANSII_FRONT_BLACK_EMPH = 90,
ANSII_FRONT_RED_EMPH = 91,
ANSII_FRONT_GREEN_EMPH = 92,
ANSII_FRONT_YELLOW_EMPH = 93,
ANSII_FRONT_BLUE_EMPH = 94,
ANSII_FRONT_PURPLE_EMPH = 95,
ANSII_FRONT_CYAN_EMPH = 96,
ANSII_FRONT_WHITE_EMPH = 97,
ANSII_BACK_BLACK = 40,
ANSII_BACK_RED = 41,
ANSII_BACK_GREEN = 42,
ANSII_BACK_YELLOW = 43,
ANSII_BACK_BLUE = 44,
ANSII_BACK_PURPLE = 45,
ANSII_BACK_CYAN = 46,
ANSII_BACK_WHITE = 47,
ANSII_BACK_BLACK_EMPH = 100,
ANSII_BACK_RED_EMPH = 101,
ANSII_BACK_GREEN_EMPH = 102,
ANSII_BACK_YELLOW_EMPH = 103,
ANSII_BACK_BLUE_EMPH = 104,
ANSII_BACK_PURPLE_EMPH = 105,
ANSII_BACK_CYAN_EMPH = 106,
ANSII_BACK_WHITE_EMPH = 107,
};
bool itoac(int i, enum AnsiiColor *color);
bool itovc(int i, enum VGAColor *color);
enum VGAColor atovc(enum AnsiiColor color);

View file

@ -1,30 +0,0 @@
#include <sys.h>
#include "cursor.h"
#include "term.h"
void cursor_enable(void) {
cursor_setsize(13, 16);
}
void cursor_disable(void) {
outb(0x3D4, 0x0A);
outb(0x3D5, 0x20);
}
void cursor_setsize(uint8_t start, uint8_t end) {
outb(0x3D4, 0x0A);
outb(0x3D5, (inb(0x3D5) & 0xC0) | start);
outb(0x3D4, 0x0B);
outb(0x3D5, (inb(0x3D5) & 0xE0) | end);
}
void cursor_setpos(uint8_t x, uint8_t y) {
; uint16_t pos = y * TERM_W + x;
outb(0x3D4, 0x0F);
outb(0x3D5, (uint8_t) (pos & 0xFF));
outb(0x3D4, 0x0E);
outb(0x3D5, (uint8_t) ((pos >> 8) & 0xFF));
}

View file

@ -1,9 +0,0 @@
#pragma once
#include <stdint.h>
void cursor_enable(void);
void cursor_disable(void);
void cursor_setsize(uint8_t start, uint8_t end);
void cursor_setpos(uint8_t x, uint8_t y);