├── .gitignore ├── bin ├── os.bin ├── boot.bin └── kernel.bin ├── v86 ├── v86.wasm ├── seabios.bin └── vgabios.bin ├── src ├── kernel.h ├── config.h ├── memory │ ├── memory.h │ └── memory.c ├── io │ ├── io.h │ └── io.asm ├── kernel.c ├── terminal │ ├── terminal.h │ └── terminal.c ├── idt │ ├── idt.h │ ├── idt.asm │ └── idt.c ├── linker.ld ├── kernel.asm └── boot.asm ├── run.sh ├── .vscode └── settings.json ├── Makefile └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /bin/os.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelOverseer/mathematikOs/HEAD/bin/os.bin -------------------------------------------------------------------------------- /bin/boot.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelOverseer/mathematikOs/HEAD/bin/boot.bin -------------------------------------------------------------------------------- /v86/v86.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelOverseer/mathematikOs/HEAD/v86/v86.wasm -------------------------------------------------------------------------------- /bin/kernel.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelOverseer/mathematikOs/HEAD/bin/kernel.bin -------------------------------------------------------------------------------- /v86/seabios.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelOverseer/mathematikOs/HEAD/v86/seabios.bin -------------------------------------------------------------------------------- /v86/vgabios.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KernelOverseer/mathematikOs/HEAD/v86/vgabios.bin -------------------------------------------------------------------------------- /src/kernel.h: -------------------------------------------------------------------------------- 1 | #ifndef KERNEL_H 2 | #define KERNEL_H 3 | 4 | void kernel_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #/bin/bash 2 | export PREFIX="$HOME/opt/cross" 3 | export TARGET=i686-elf 4 | export PATH="$PREFIX/bin:$PATH" 5 | make run -------------------------------------------------------------------------------- /src/config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | 4 | #define IDT_ENTRIES 256 5 | #define KERNEL_CODE_SEGMENT_OFFSET 0x08 6 | 7 | #endif -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "stddef.h": "c", 4 | "idt.h": "c", 5 | "memory.h": "c" 6 | } 7 | } -------------------------------------------------------------------------------- /src/memory/memory.h: -------------------------------------------------------------------------------- 1 | #ifndef MEMORY_H 2 | #define MEMORY_H 3 | #include 4 | 5 | void *memset(void *dest, uint8_t c, uint32_t size); 6 | void *bzero(void *dest, uint32_t size); 7 | 8 | #endif -------------------------------------------------------------------------------- /src/io/io.h: -------------------------------------------------------------------------------- 1 | #ifndef IO_H 2 | #define IO_H 3 | 4 | unsigned char insb(unsigned short port); 5 | unsigned short insw(unsigned short port); 6 | 7 | void outb(unsigned short port, unsigned char val); 8 | void outw(unsigned short port, unsigned short val); 9 | 10 | #endif -------------------------------------------------------------------------------- /src/memory/memory.c: -------------------------------------------------------------------------------- 1 | #include "memory/memory.h" 2 | 3 | void *memset(void *dest, uint8_t c, uint32_t size) 4 | { 5 | uint8_t *mem = dest; 6 | while (size--) 7 | *mem++ = c; 8 | return dest; 9 | } 10 | 11 | void *bzero(void *dest, uint32_t size) 12 | { 13 | return memset(dest, 0, size); 14 | } -------------------------------------------------------------------------------- /src/kernel.c: -------------------------------------------------------------------------------- 1 | #include "kernel.h" 2 | #include "terminal/terminal.h" 3 | #include "idt/idt.h" 4 | 5 | void kernel_main() 6 | { 7 | // char str[] = "Hello World\nthis is an example"; 8 | terminal_clear(); 9 | // terminal_putstr(str); 10 | // terminal_putchar('\n'); 11 | // terminal_putnbr(-1337); 12 | 13 | idt_init(); 14 | } 15 | -------------------------------------------------------------------------------- /src/terminal/terminal.h: -------------------------------------------------------------------------------- 1 | #ifndef TERMINAL_H 2 | #define TERMINAL_H 3 | #define VGA_ROWS 25 4 | #define VGA_COLS 80 5 | #include 6 | 7 | void terminal_clear(void); 8 | void terminal_putchar(char c); 9 | void terminal_putstr(char *str); 10 | void terminal_putnbr(int32_t num); 11 | void terminal_puthex(uint32_t num); 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /src/idt/idt.h: -------------------------------------------------------------------------------- 1 | #ifndef IDT_H 2 | #define IDT_H 3 | #include 4 | #include "config.h" 5 | 6 | 7 | struct idt_entry 8 | { 9 | uint16_t offset_low; 10 | uint16_t selector; 11 | uint8_t zero; 12 | uint8_t type_attributes; 13 | uint16_t offset_high; 14 | } __attribute__((packed)); 15 | 16 | struct idt_ptr 17 | { 18 | uint16_t limit; 19 | uint32_t base; 20 | } __attribute__((packed)); 21 | 22 | void idt_init(void); 23 | 24 | #endif -------------------------------------------------------------------------------- /src/linker.ld: -------------------------------------------------------------------------------- 1 | ENTRY(_start) 2 | OUTPUT_FORMAT(binary) 3 | SECTIONS 4 | { 5 | . = 1M; 6 | .text : ALIGN(4096) 7 | { 8 | *(.text) 9 | } 10 | 11 | .rodata : ALIGN(4096) 12 | { 13 | *(.rodata) 14 | } 15 | 16 | .data : ALIGN(4096) 17 | { 18 | *(.data) 19 | } 20 | 21 | .bss : ALIGN(4096) 22 | { 23 | *(COMMON) 24 | *(.bss) 25 | } 26 | 27 | .asm : ALIGN(4096) 28 | { 29 | *(.asm) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/kernel.asm: -------------------------------------------------------------------------------- 1 | [BITS 32] 2 | 3 | CODE_SEG equ 0x08 4 | DATA_SEG equ 0x10 5 | 6 | global _start 7 | extern kernel_main 8 | 9 | _start: 10 | 11 | mov ax, DATA_SEG 12 | mov ds, ax 13 | mov es, ax 14 | mov fs, ax 15 | mov gs, ax 16 | mov ss, ax 17 | mov ebp, 0x00200000 18 | mov esp, ebp 19 | 20 | ; Remap the master PIC 21 | mov al, 00010001b 22 | out 0x20, al ; Tell master PIC 23 | 24 | mov al, 0x20 ; Interrupt 0x20 is where master ISR should start 25 | out 0x21, al 26 | 27 | mov al, 00000001b 28 | out 0x21, al 29 | ; End remap of the master PIC 30 | 31 | call kernel_main 32 | 33 | jmp $ 34 | 35 | times 512-($ - $$) db 0 36 | -------------------------------------------------------------------------------- /src/idt/idt.asm: -------------------------------------------------------------------------------- 1 | section asm 2 | global idt_load 3 | global int0_handler 4 | global int21_handler 5 | global ozaazaa 6 | 7 | extern handle_zero 8 | extern ozaazaa_handler 9 | extern keyboard_handler 10 | 11 | idt_load: 12 | push ebp 13 | mov ebp, esp 14 | 15 | mov eax, [ebp+8] 16 | lidt [eax] 17 | 18 | sti 19 | pop ebp 20 | ret 21 | 22 | ozaazaa: 23 | mov eax, 0x20 24 | out 0x20, eax 25 | iret 26 | 27 | int0_handler: 28 | pushad 29 | call handle_zero 30 | mov eax, 0x20 31 | out 0x20, eax 32 | popad 33 | iret 34 | 35 | int21_handler: 36 | pushad 37 | call keyboard_handler 38 | mov eax, 0x20 39 | out 0x20, eax 40 | popad 41 | iret -------------------------------------------------------------------------------- /src/io/io.asm: -------------------------------------------------------------------------------- 1 | section asm 2 | 3 | global insb 4 | global insws 5 | global outb 6 | global outw 7 | 8 | insb: 9 | push ebp 10 | mov ebp, esp 11 | 12 | xor eax, eax 13 | mov edx, [ebp+8] 14 | in al, dx 15 | 16 | pop ebp 17 | ret 18 | 19 | insw: 20 | push ebp 21 | mov ebp, esp 22 | 23 | xor eax, eax 24 | mov edx, [ebp+8] 25 | in ax, dx 26 | 27 | pop ebp 28 | ret 29 | 30 | outb: 31 | push ebp 32 | mov ebp, esp 33 | 34 | mov eax, [ebp+12] 35 | mov edx, [ebp+8] 36 | out dx, al 37 | 38 | pop ebp 39 | ret 40 | 41 | outw: 42 | push ebp 43 | mov ebp, esp 44 | 45 | mov eax, [ebp+12] 46 | mov edx, [ebp+8] 47 | out dx, ax 48 | 49 | pop ebp 50 | ret -------------------------------------------------------------------------------- /src/idt/idt.c: -------------------------------------------------------------------------------- 1 | #include "idt/idt.h" 2 | #include "memory/memory.h" 3 | #include "terminal/terminal.h" 4 | #include "io/io.h" 5 | 6 | struct idt_entry interrupt_descriptor_table[IDT_ENTRIES]; 7 | 8 | void idt_register(uint32_t interrupt, void *function) 9 | { 10 | struct idt_entry *entry = &interrupt_descriptor_table[interrupt]; 11 | 12 | entry->offset_low = (uint32_t)function & 0x0000ffff; 13 | entry->selector = KERNEL_CODE_SEGMENT_OFFSET; 14 | entry->zero = 0; 15 | entry->type_attributes = 0x8E; 16 | entry->offset_high = (uint32_t)function >> 16; 17 | } 18 | 19 | void idt_load(void *idt); 20 | 21 | void int0_handler(void); 22 | void int21_handler(void); 23 | void ozaazaa(void); 24 | 25 | void keyboard_handler(void) 26 | { 27 | terminal_putchar('#'); 28 | uint8_t scancode = insb(0x60); 29 | terminal_puthex(scancode); 30 | terminal_putchar('\n'); 31 | } 32 | 33 | void handle_zero(void) 34 | { 35 | terminal_putchar('#'); 36 | } 37 | 38 | void idt_init(void) 39 | { 40 | bzero(interrupt_descriptor_table, sizeof(interrupt_descriptor_table)); 41 | struct idt_ptr idt_header; 42 | 43 | idt_header.limit = sizeof(interrupt_descriptor_table) - 1; 44 | idt_header.base = (uint32_t)interrupt_descriptor_table; 45 | 46 | for (int i = 0; i < IDT_ENTRIES; i++) 47 | { 48 | idt_register(i, ozaazaa); 49 | } 50 | 51 | idt_register(0, int0_handler); 52 | idt_register(0x21, int21_handler); 53 | 54 | idt_load(&idt_header); 55 | } -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | C_FILES = ./build/kernel.o ./build/terminal/terminal.o ./build/idt/idt.o ./build/memory/memory.o 2 | ASM_FILES = ./build/kernel.asm.o ./build/idt/idt.asm.o ./build/io/io.asm.o 3 | FILES = $(ASM_FILES) $(C_FILES) 4 | INCLUDES = -I./src 5 | FLAGS = -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc 6 | 7 | all: ./bin/os.bin 8 | 9 | ./bin/os.bin: ./bin/boot.bin ./bin/kernel.bin 10 | rm -rf ./bin/os.bin 11 | dd if=./bin/boot.bin >> ./bin/os.bin 12 | dd if=./bin/kernel.bin >> ./bin/os.bin 13 | dd if=/dev/zero bs=512 count=100 >> ./bin/os.bin 14 | 15 | ./bin/kernel.bin: $(FILES) 16 | i686-elf-ld -g -relocatable $(FILES) -o ./build/kernelfull.o 17 | i686-elf-gcc $(FLAGS) -T ./src/linker.ld -o ./bin/kernel.bin -ffreestanding -O0 -nostdlib ./build/kernelfull.o 18 | 19 | ./bin/boot.bin: ./src/boot.asm 20 | nasm -f bin ./src/boot.asm -o ./bin/boot.bin 21 | 22 | $(ASM_FILES): ./build/%.asm.o : ./src/%.asm 23 | nasm -f elf -g $< -o $@ 24 | 25 | $(C_FILES): ./build/%.o : ./src/%.c 26 | i686-elf-gcc $(INCLUDES) $(FLAGS) -std=gnu99 -c $< -o $@ 27 | 28 | run: ./bin/os.bin 29 | qemu-system-i386 -hda ./bin/os.bin 30 | 31 | clean: 32 | rm -rf ./bin/boot.bin 33 | rm -rf ./bin/kernel.bin 34 | rm -rf ./bin/os.bin 35 | rm -rf ${FILES} 36 | rm -rf ./build/kernelfull.o 37 | 38 | .PHONY: all clean run -------------------------------------------------------------------------------- /src/terminal/terminal.c: -------------------------------------------------------------------------------- 1 | #include "terminal.h" 2 | 3 | uint16_t *VGA_MEMORY = (uint16_t *)0xB8000; 4 | uint8_t TERMINAL_COL = 0; 5 | uint8_t TERMINAL_ROW = 0; 6 | 7 | uint16_t terminal_create_char(char character, char color) 8 | { 9 | return (character | color << 8); 10 | } 11 | 12 | void terminal_put_character(uint8_t col, uint8_t row, char character, char color) 13 | { 14 | VGA_MEMORY[VGA_COLS * row + col] = terminal_create_char(character, color); 15 | } 16 | 17 | void terminal_clear() 18 | { 19 | for (int y = 0; y < VGA_ROWS; y++) 20 | { 21 | for (int x = 0; x < VGA_COLS; x++) 22 | { 23 | terminal_put_character(x, y, ' ', 0); 24 | } 25 | } 26 | } 27 | 28 | void terminal_putchar(char c) 29 | { 30 | if (c == '\n') 31 | { 32 | TERMINAL_ROW++; 33 | TERMINAL_COL = 0; 34 | return; 35 | } 36 | terminal_put_character(TERMINAL_COL, TERMINAL_ROW, c, 10); 37 | TERMINAL_COL++; 38 | if (TERMINAL_COL > VGA_COLS) 39 | { 40 | TERMINAL_COL = 0; 41 | TERMINAL_ROW++; 42 | } 43 | } 44 | 45 | void terminal_putstr(char *str) 46 | { 47 | int i = 0; 48 | while (str[i]) 49 | { 50 | terminal_putchar(str[i]); 51 | i++; 52 | } 53 | } 54 | 55 | void terminal_putnbr(int32_t num) 56 | { 57 | uint32_t n; 58 | 59 | if (num < 0) 60 | { 61 | terminal_putchar('-'); 62 | n = -num; 63 | } 64 | else 65 | { 66 | n = num; 67 | } 68 | if (n / 10) 69 | { 70 | terminal_putnbr(n / 10); 71 | } 72 | terminal_putchar(n % 10 + '0'); 73 | } 74 | 75 | void terminal_puthex(uint32_t num) 76 | { 77 | if (num / 16) 78 | terminal_puthex(num / 16); 79 | terminal_putchar("0123456789abcdef"[num % 16]); 80 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mathematikOs 5 | 6 | 7 | 8 | 9 | 10 | 32 |
33 |
34 |
35 | 36 |
37 |
38 | 39 | 62 | 63 | -------------------------------------------------------------------------------- /src/boot.asm: -------------------------------------------------------------------------------- 1 | ORG 0x7c00 2 | BITS 16 3 | 4 | CODE_SEG equ gdt_code - gdt_start 5 | DATA_SEG equ gdt_data - gdt_start 6 | 7 | start: 8 | cli ; clear interrupts 9 | mov ax, 0x0 10 | mov ds, ax ; setting the data segment 11 | mov es, ax ; setting the extra segment 12 | mov ss, ax ; setting the stack segment 13 | mov sp, 0x7c00 ; setting the stack pointer 14 | 15 | ; enabling A20 line 16 | in al, 0x92 17 | or al, 2 18 | out 0x92, al 19 | ; loading the GDT 20 | lgdt [gdt_descriptor] 21 | ; set PE (Protection Enable) bit in CR0 (Control Register 0) 22 | mov eax, cr0 23 | or al, 1 24 | mov cr0, eax 25 | 26 | jmp CODE_SEG:load32 27 | 28 | [BITS 32] 29 | load32: 30 | mov eax, 1 31 | mov ecx, 100 32 | mov edi, 0x0100000 33 | call ata_lba_read 34 | jmp CODE_SEG:0x0100000 35 | 36 | gdt_start: ; Start of our GDT 37 | gdt_null: ; First Null GDT entry 38 | dd 0x0 39 | dd 0x0 40 | 41 | gdt_code: ; CS SHOULD POINT TO THIS 42 | dw 0xffff ; Segment limit first 0-15 bits 43 | dw 0 ; Base first 0-15 bits 44 | db 0 ; Base 16-23 bits 45 | db 0x9a ; Access byte 46 | db 11001111b ; High 4 bit flags and the low 4 bit limit 47 | db 0 ; Base 24-31 bits 48 | 49 | ; offset 0x10 50 | gdt_data: ; DS, SS, ES, FS, GS 51 | dw 0xffff ; Segment limit first 0-15 bits 52 | dw 0 ; Base first 0-15 bits 53 | db 0 ; Base 16-23 bits 54 | db 0x92 ; Access byte 55 | db 11001111b ; High 4 bit flags and the low 4 bit limit 56 | db 0 ; Base 24-31 bits 57 | ; end of the GDT 58 | gdt_end: 59 | 60 | ; GDT Descriptor 61 | gdt_descriptor: 62 | dw gdt_end - gdt_start-1 63 | dd gdt_start 64 | 65 | ata_lba_read: 66 | mov ebx, eax, ; Backup the LBA 67 | ; Send the highest 8 bits of the lba to hard disk controller 68 | shr eax, 24 69 | or eax, 0xE0 ; Select the master drive 70 | mov dx, 0x1F6 71 | out dx, al 72 | ; Finished sending the highest 8 bits of the lba 73 | 74 | ; Send the total sectors to read 75 | mov eax, ecx 76 | mov dx, 0x1F2 77 | out dx, al 78 | ; Finished sending the total sectors to read 79 | 80 | ; Send more bits of the LBA 81 | mov eax, ebx ; Restore the backup LBA 82 | mov dx, 0x1F3 83 | out dx, al 84 | ; Finished sending more bits of the LBA 85 | 86 | ; Send more bits of the LBA 87 | mov dx, 0x1F4 88 | mov eax, ebx ; Restore the backup LBA 89 | shr eax, 8 90 | out dx, al 91 | ; Finished sending more bits of the LBA 92 | 93 | ; Send upper 16 bits of the LBA 94 | mov dx, 0x1F5 95 | mov eax, ebx ; Restore the backup LBA 96 | shr eax, 16 97 | out dx, al 98 | ; Finished sending upper 16 bits of the LBA 99 | 100 | mov dx, 0x1f7 101 | mov al, 0x20 102 | out dx, al 103 | 104 | ; Read all sectors into memory 105 | .next_sector: 106 | push ecx 107 | 108 | ; Checking if we need to read 109 | .try_again: 110 | mov dx, 0x1f7 111 | in al, dx 112 | test al, 8 113 | jz .try_again 114 | 115 | ; We need to read 256 words at a time 116 | mov ecx, 256 117 | mov dx, 0x1F0 118 | rep insw 119 | pop ecx 120 | loop .next_sector 121 | ; End of reading sectors into memory 122 | ret 123 | 124 | times 510-($ - $$) db 0 125 | dw 0xAA55 126 | --------------------------------------------------------------------------------