This commit is contained in:
Tunacan 2020-11-22 01:30:25 +03:00
parent e1dc22b818
commit bb0bffbe56
16 changed files with 499 additions and 61 deletions

View file

@ -1,4 +1,5 @@
TARGET = i686-elf
ARCH = i386
CC = $(TARGET)-gcc
STRIP = $(TARGET)-strip
NASM = nasm
@ -9,9 +10,10 @@ LDFLAGS += -ffreestanding -O2 -nostdlib -lgcc
NASMFLAGS += -felf32
BIN = sysroot/boot/hhhos.bin
ISO = HhhOS.iso
ISO = build/HhhOS.iso
OBJS = obj/boot.o obj/kernel.o obj/string.o obj/terminal.o
OBJS = build/boot.o build/kernel.o build/string.o build/terminal.o build/idt.o build/isr.o build/util.o build/keyboard.o
ARCHDIR = arch/$(ARCH)/
.PHONY: all run clean build
@ -26,10 +28,10 @@ $(ISO): $(BIN)
$(BIN): $(OBJS)
$(CC) -T linker.ld $(LDFLAGS) -o $@ $^
obj/%.o: %.c
build/%.o: $(ARCHDIR)%.c
$(CC) -c $(CFLAGS) -Iinclude -o $@ $<
obj/%.o: %.asm
build/%.o: $(ARCHDIR)%.asm
$(NASM) $(NASMFLAGS) -o $@ $<
clean:

18
arch/i386/idt.c Normal file
View file

@ -0,0 +1,18 @@
#include <idt.h>
idt_gate_t idt[IDT_ENTRIES];
idt_register_t idt_reg;
void set_idt_gate(int n, uint32_t handler) {
idt[n].low_offset = LOW_16(handler);
idt[n].sel = KERNEL_CS;
idt[n].always0 = 0;
idt[n].flags = 0x8E;
idt[n].high_offset = HIGH_16(handler);
}
void set_idt() {
idt_reg.base = (uint32_t) &idt;
idt_reg.limit = IDT_ENTRIES * sizeof(idt_gate_t) - 1;
__asm__ __volatile__("lidtl (%0)" : : "r" (&idt_reg));
}

199
arch/i386/isr.c Normal file
View file

@ -0,0 +1,199 @@
#include <isr.h>
#include <terminal.h>
void isr0(void) {
terminal_writeline("Division By Zero");
asm("hlt");
}
void isr1(void) {
terminal_writeline("Debug");
asm("hlt");
}
void isr2(void) {
terminal_writeline("Non Maskable Interrupt");
asm("hlt");
}
void isr3(void) {
terminal_writeline("Breakpoint");
asm("hlt");
}
void isr4(void) {
terminal_writeline("Into Detected Overflow");
asm("hlt");
}
void isr5(void) {
terminal_writeline("Out of Bounds");
asm("hlt");
}
void isr6(void) {
terminal_writeline("Invalid Opcode");
asm("hlt");
}
void isr7(void) {
terminal_writeline("No Coprocessor");
asm("hlt");
}
void isr8(void) {
terminal_writeline("Double Fault");
asm("hlt");
}
void isr9(void) {
terminal_writeline("Coprocessor Segment Overrun");
asm("hlt");
}
void isr10(void) {
terminal_writeline("Bad TSS");
asm("hlt");
}
void isr11(void) {
terminal_writeline("Segment Not Present");
asm("hlt");
}
void isr12(void) {
terminal_writeline("Stack Fault");
asm("hlt");
}
void isr13(void) {
terminal_writeline("General Protection Fault");
asm("hlt");
}
void isr14(void) {
terminal_writeline("Page Fault");
asm("hlt");
}
void isr15(void) {
terminal_writeline("Unknown Interrupt");
asm("hlt");
}
void isr16(void) {
terminal_writeline("Coprocessor Fault");
asm("hlt");
}
void isr17(void) {
terminal_writeline("Alignment Check");
asm("hlt");
}
void isr18(void) {
terminal_writeline("Machine Check");
asm("hlt");
}
void isr19(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr20(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr21(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr22(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr23(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr24(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr25(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr26(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr27(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr28(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr29(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr30(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr31(void) {
terminal_writeline("Reserved");
asm("hlt");
}
void isr_install(void) {
set_idt_gate(0, (uint32_t)isr0);
set_idt_gate(1, (uint32_t)isr1);
set_idt_gate(2, (uint32_t)isr2);
set_idt_gate(3, (uint32_t)isr3);
set_idt_gate(4, (uint32_t)isr4);
set_idt_gate(5, (uint32_t)isr5);
set_idt_gate(6, (uint32_t)isr6);
set_idt_gate(7, (uint32_t)isr7);
set_idt_gate(8, (uint32_t)isr8);
set_idt_gate(9, (uint32_t)isr9);
set_idt_gate(10, (uint32_t)isr10);
set_idt_gate(11, (uint32_t)isr11);
set_idt_gate(12, (uint32_t)isr12);
set_idt_gate(13, (uint32_t)isr13);
set_idt_gate(14, (uint32_t)isr14);
set_idt_gate(15, (uint32_t)isr15);
set_idt_gate(16, (uint32_t)isr16);
set_idt_gate(17, (uint32_t)isr17);
set_idt_gate(18, (uint32_t)isr18);
set_idt_gate(19, (uint32_t)isr19);
set_idt_gate(20, (uint32_t)isr20);
set_idt_gate(21, (uint32_t)isr21);
set_idt_gate(22, (uint32_t)isr22);
set_idt_gate(23, (uint32_t)isr23);
set_idt_gate(24, (uint32_t)isr24);
set_idt_gate(25, (uint32_t)isr25);
set_idt_gate(26, (uint32_t)isr26);
set_idt_gate(27, (uint32_t)isr27);
set_idt_gate(28, (uint32_t)isr28);
set_idt_gate(29, (uint32_t)isr29);
set_idt_gate(30, (uint32_t)isr30);
set_idt_gate(31, (uint32_t)isr31);
set_idt();
}

29
arch/i386/kernel.c Normal file
View file

@ -0,0 +1,29 @@
#include <stdbool.h>
#include <stddef.h>
#include <isr.h>
#include <keyboard.h>
/* Check if the compiler thinks you are targeting the wrong operating system. */
#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
/* This operating system will only work for the 32-bit ix86 targets. */
#if !defined(__i386__)
#error "This operating system needs to be compiled with a ix86-elf compiler"
#endif
void kernel_main() {
terminal_initialize();
keyboard_init();
isr_install();
terminal_writeline("hello yes");
terminal_writestring("> ");
while (true) {
if (keycache[key_loc] == '\n') {
terminal_writestring(keycache);
}
}
}

129
arch/i386/keyboard.c Normal file
View file

@ -0,0 +1,129 @@
#include <keyboard.h>
uint8_t lastkey = 0;
char *keycache = 0;
uint16_t key_loc = 0;
uint8_t __kbd_enabled = 0;
enum KEYCODE {
NULL_KEY = 0,
Q_PRESSED = 0x10,
Q_RELEASED = 0x90,
W_PRESSED = 0x11,
W_RELEASED = 0x91,
E_PRESSED = 0x12,
E_RELEASED = 0x92,
R_PRESSED = 0x13,
R_RELEASED = 0x93,
T_PRESSED = 0x14,
T_RELEASED = 0x94,
Z_PRESSED = 0x15,
Z_RELEASED = 0x95,
U_PRESSED = 0x16,
U_RELEASED = 0x96,
I_PRESSED = 0x17,
I_RELEASED = 0x97,
O_PRESSED = 0x18,
O_RELEASED = 0x98,
P_PRESSED = 0x19,
P_RELEASED = 0x99,
A_PRESSED = 0x1E,
A_RELEASED = 0x9E,
S_PRESSED = 0x1F,
S_RELEASED = 0x9F,
D_PRESSED = 0x20,
D_RELEASED = 0xA0,
F_PRESSED = 0x21,
F_RELEASED = 0xA1,
G_PRESSED = 0x22,
G_RELEASED = 0xA2,
H_PRESSED = 0x23,
H_RELEASED = 0xA3,
J_PRESSED = 0x24,
J_RELEASED = 0xA4,
K_PRESSED = 0x25,
K_RELEASED = 0xA5,
L_PRESSED = 0x26,
L_RELEASED = 0xA6,
Y_PRESSED = 0x2C,
Y_RELEASED = 0xAC,
X_PRESSED = 0x2D,
X_RELEASED = 0xAD,
C_PRESSED = 0x2E,
C_RELEASED = 0xAE,
V_PRESSED = 0x2F,
V_RELEASED = 0xAF,
B_PRESSED = 0x30,
B_RELEASED = 0xB0,
N_PRESSED = 0x31,
N_RELEASED = 0xB1,
M_PRESSED = 0x32,
M_RELEASED = 0xB2,
ZERO_PRESSED = 0x29,
ONE_PRESSED = 0x2,
NINE_PRESSED = 0xA,
POINT_PRESSED = 0x34,
POINT_RELEASED = 0xB4,
SLASH_RELEASED = 0xB5,
BACKSPACE_PRESSED = 0xE,
BACKSPACE_RELEASED = 0x8E,
SPACE_PRESSED = 0x39,
SPACE_RELEASED = 0xB9,
ENTER_PRESSED = 0x1C,
ENTER_RELEASED = 0x9C
};
void keyboard_irq(void) {
asm volatile("pusha");
char key = keyboard_to_ascii(inb(0x60));
keycache[key_loc++] = key;
terminal_putchar(key);
asm volatile("popa");
asm volatile("iret");
}
void keyboard_init(void) {
terminal_writeline("haha keyboard go brr");
keycache = (char*)malloc(256);
memset(keycache, 0, 256);
set_idt_gate(33, (uint32_t)keyboard_irq);
__kbd_enabled = 1;
}
uint8_t keyboard_enabled(void) {
return __kbd_enabled;
}
void keyboard_read_key(void) {
lastkey = 0;
if (inb(0x64) & 1)
lastkey = inb(0x60);
}
static char* _qwertzuiop = "qwertzuiop"; // 0x10-0x1c
static char* _asdfghjkl = "asdfghjkl";
static char* _yxcvbnm = "yxcvbnm";
static char* _num = "123456789";
uint8_t keyboard_to_ascii(uint8_t key) {
if (key == 0x1C) return '\n';
if (key == 0x39) return ' ';
if (key == 0xE) return '\r';
if (key == POINT_RELEASED) return '.';
if (key == SLASH_RELEASED) return '/';
if (key == ZERO_PRESSED) return '0';
if (key >= ONE_PRESSED && key <= NINE_PRESSED)
return _num[key - ONE_PRESSED];
if (key >= 0x10 && key <= 0x1C) {
return _qwertzuiop[key - 0x10];
} else if(key >= 0x1E && key <= 0x26) {
return _asdfghjkl[key - 0x1E];
} else if(key >= 0x2C && key <= 0x32) {
return _yxcvbnm[key - 0x2C];
}
return 0;
}

17
arch/i386/util.c Normal file
View file

@ -0,0 +1,17 @@
#include <util.h>
void* malloc(size_t amount) {
char variable[amount];
return &variable;
}
static size_t rand_next = 1;
int rand(void) {
rand_next = rand_next * 1103515245 + 12345;
return (size_t)(rand_next/65536) % 32768;
}
void srand(size_t seed) {
rand_next = seed;
}

0
build/.gitkeep Normal file
View file

24
gdt.c
View file

@ -1,24 +0,0 @@
#include <stddef.h>
#include <stdint.h>
const struct gdt_entry {
size_t limit_low : 16;
size_t base_low : 24;
// attribute byte split into bitfields
size_t accessed : 1;
size_t read_write : 1; // readable for code, writable for data
size_t conforming_expand_down : 1; // conforming for code, expand down for data
size_t code : 1; // 1 for code, 0 for data
size_t always_1 : 1; // should be 1 for everything but TSS and LDT
size_t DPL : 2; // privilege level
size_t present : 1;
// and now into granularity
size_t limit_high : 4;
size_t available : 1;
size_t always_0 : 1; // should always be 0
size_t big : 1; // 32bit opcodes for code, uint32_t stack for data
size_t gran : 1; // 1 to use 4k page addressing, 0 for byte addressing
size_t base_high : 8;
} __attribute__((packed));
void initGDT() {}

37
include/idt.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef _IDT_H
#define _IDT_H 1
#include <stdint.h>
#include <util.h>
#ifdef __cplusplus
extern "C" {
#endif
#define KERNEL_CS 0x08
typedef struct {
uint16_t low_offset;
uint16_t sel;
uint8_t always0;
uint8_t flags;
uint16_t high_offset;
} __attribute__((packed)) idt_gate_t;
typedef struct {
uint16_t limit;
uint32_t base;
} __attribute__((packed)) idt_register_t;
#define IDT_ENTRIES 256
extern idt_gate_t idt[IDT_ENTRIES];
extern idt_register_t idt_reg;
void set_idt_gate(int n, uint32_t handler);
void set_idt(void);
#ifdef __cplusplus
}
#endif
#endif /* _IDT_H */

16
include/isr.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef _ISR_H
#define _ISR_H 1
#include <idt.h>
#ifdef __cplusplus
extern "C" {
#endif
void isr_install(void);
#ifdef __cplusplus
}
#endif
#endif /* _ISR_H */

24
include/keyboard.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef _KEYBOARD_H
#define _KEYBOARD_H 1
#include <terminal.h>
#include <util.h>
#include <string.h>
#include <idt.h>
#ifdef __cplusplus
extern "C" {
#endif
extern char* keycache;
extern uint16_t key_loc;
void keyboard_init(void);
uint8_t keyboard_enabled(void);
uint8_t keyboard_to_ascii(uint8_t key);
#ifdef __cplusplus
}
#endif
#endif /* _KEYBOARD_H */

24
include/util.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef _UTIL_H
#define _UTIL_H 1
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LOW_16(address) (uint16_t)((address) & 0xFFFF)
#define HIGH_16(address) (uint16_t)(((address) >> 16) & 0xFFFF)
void* malloc(size_t amount);
static size_t rand_next;
int rand(void);
void srand(size_t seed);
#ifdef __cplusplus
}
#endif
#endif /* _UTIL_H */

View file

@ -1,33 +0,0 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <terminal.h>
/* Check if the compiler thinks you are targeting the wrong operating system. */
#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
/* This tutorial will only work for the 32-bit ix86 targets. */
#if !defined(__i386__)
#error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif
static size_t rand_next = 1;
int rand(void) {
rand_next = rand_next * 1103515245 + 12345;
return (size_t)(rand_next/65536) % 32768;
}
void srand(size_t seed) {
rand_next = seed;
}
void kernel_main() {
terminal_initialize();
terminal_writeline("hello yes");
terminal_writestring("> ");
}