Fixed bug in GDT length calculation in bootloader. Kernel now loads its own GDT so it doesn't clobber itself while touching memory. ATAPIO now loads its own copy of the boot sector so it isn't reliant on 0x7000 being mapped. Removed debug printf statements from a lot of places.

This commit is contained in:
Xnoe 2021-10-12 21:41:31 +01:00
parent e2bb3ff634
commit f44d4ad768
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
10 changed files with 141 additions and 28 deletions

View File

@ -2,7 +2,9 @@ CFLAGS = -m32 -mgeneral-regs-only -nostdlib -fno-builtin -fno-exceptions -fno-le
LDFLAGS = LDFLAGS =
DISK_IMG_FILES = build/kernel/kernel.bin DISK_IMG_FILES = build/kernel/kernel.bin
KERNEL_OBJS = build/c_code_entry.o build/kernel/screenstuff.o build/kernel/io.o build/kernel/idt.o build/kernel/keyboard.o build/kernel/strings.o build/kernel/atapio.o build/kernel/kernel.o build/kernel/paging.o build/kernel/allocate.o KERNEL_OBJS = build/kernel/entry.o build/kernel/screenstuff.o build/kernel/io.o build/kernel/idt.o build/kernel/keyboard.o \
build/kernel/strings.o build/kernel/atapio.o build/kernel/kernel.o build/kernel/paging.o build/kernel/allocate.o \
build/kernel/gdt.o
STAGE2_OBS = build/c_code_entry.o build/boot_stage2/io.o build/boot_stage2/atapio.o build/boot_stage2/strings.o build/boot_stage2/screenstuff.o build/boot_stage2/stage2.o build/boot_stage2/paging.o STAGE2_OBS = build/c_code_entry.o build/boot_stage2/io.o build/boot_stage2/atapio.o build/boot_stage2/strings.o build/boot_stage2/screenstuff.o build/boot_stage2/stage2.o build/boot_stage2/paging.o
run: disk.img run: disk.img
@ -31,9 +33,14 @@ clean:
build/boot/boot.bin: src/boot/boot.asm build/boot/boot.bin: src/boot/boot.asm
nasm $< -o $@ nasm $< -o $@
# Boot Stage 2
build/boot_stage2/boot.bin: src/boot_stage2/boot_stage2.ld $(STAGE2_OBS) build/boot_stage2/boot.bin: src/boot_stage2/boot_stage2.ld $(STAGE2_OBS)
ld $(LDFLAGS) -T $< $(STAGE2_OBS) ld $(LDFLAGS) -T $< $(STAGE2_OBS)
build/boot_stage2/%.o: src/boot_stage2/%.c
gcc $(CFLAGS) -o $@ -c $<
# Kernel
build/kernel/kernel.bin: src/kernel/kernel.ld $(KERNEL_OBJS) build/kernel/kernel.bin: src/kernel/kernel.ld $(KERNEL_OBJS)
ld $(LDFLAGS) -T $< $(KERNEL_OBJS) ld $(LDFLAGS) -T $< $(KERNEL_OBJS)
@ -43,8 +50,9 @@ build/boot_stage2/stage2.o: src/boot_stage2/main.c
build/kernel/%.o: src/kernel/%.c build/kernel/%.o: src/kernel/%.c
gcc $(CFLAGS) -o $@ -c $< gcc $(CFLAGS) -o $@ -c $<
build/boot_stage2/%.o: src/boot_stage2/%.c build/kernel/%.o: src/kernel/%.asm
gcc $(CFLAGS) -o $@ -c $< nasm -felf32 $< -o $@
# Generic
build/%.o: src/%.asm build/%.o: src/%.asm
nasm -felf32 $< -o $@ nasm -felf32 $< -o $@

View File

@ -134,7 +134,7 @@ data:
db 0 db 0
gdt_end: gdt_end:
gdt_desc: gdt_desc:
dw gdt_desc - gdt_end - 1 dw gdt_end - gdt - 1
dd gdt dd gdt
TIMES 510 - ($ - $$) db 0 TIMES 510 - ($ - $$) db 0

View File

@ -107,7 +107,7 @@ void main() {
PTE** kernel_page_tables = 0x521000; PTE** kernel_page_tables = 0x521000;
// Mark unavailable bitmap to 0x522000 // Mark unavailable bitmap to 0x522000
mark_unavailble(bitmap, 0x522000 - (uint32_t)bitmap); mark_unavailble(bitmap, 0x4000000);
// Now we want to map some stuff. // Now we want to map some stuff.
// But first, we should load the kernel somewhere // But first, we should load the kernel somewhere
@ -124,7 +124,7 @@ void main() {
// Map the bitmap // Map the bitmap
map_many_4k_phys_to_virt(0x100000, 0xc0600000, kernel_page_directory, kernel_page_tables, 32); map_many_4k_phys_to_virt(0x100000, 0xc0600000, kernel_page_directory, kernel_page_tables, 32);
map_many_4k_phys_to_virt(0x7000, 0x7000, kernel_page_directory, kernel_page_tables, 2); map_4k_phys_to_virt(0x8000, 0x8000, kernel_page_directory, kernel_page_tables);
map_many_4k_phys_to_virt(0x8a000, 0x8a000, kernel_page_directory, kernel_page_tables, 6); map_many_4k_phys_to_virt(0x8a000, 0x8a000, kernel_page_directory, kernel_page_tables, 6);
load_file("KERNEL BIN", kernel_location); load_file("KERNEL BIN", kernel_location);

View File

@ -57,7 +57,6 @@ void* dumb_alloc(uint32_t size) {
while (size > 0) { while (size > 0) {
for (; get_bit(last_free_page, bitmap) == 0; last_free_page++); for (; get_bit(last_free_page, bitmap) == 0; last_free_page++);
printf("Free page: %d\n", last_free_page);
uint32_t phys_addr = last_free_page * 4096; uint32_t phys_addr = last_free_page * 4096;

View File

@ -23,12 +23,14 @@ uint16_t sectorsPerFAT;
void init_atapio() { void init_atapio() {
rootDirEntries = (uint8_t*)dumb_alloc(8192); rootDirEntries = (uint8_t*)dumb_alloc(8192);
FAT1 = (uint16_t*)dumb_alloc(512 * 34); FAT1 = (uint16_t*)dumb_alloc(512 * 34);
printf("RDE: %x\nFAT1: %x\n", rootDirEntries, FAT1);
countReserved = *(uint16_t*)0x7c0e; uint32_t boot_sector = (uint32_t)dumb_alloc(4096);
countFATs = *(uint8_t*)0x7c10; read_sectors(0, 1, (uint8_t*)boot_sector);
countRDEs = *(uint16_t*)0x7c11;
sectorsPerFAT = *(uint16_t*)0x7c16; countReserved = *((uint16_t*)(boot_sector + 0x0e));
countFATs = *((uint8_t*)(boot_sector + 0x10));
countRDEs = *((uint16_t*)(boot_sector + 0x11));
sectorsPerFAT = *((uint16_t*)(boot_sector + 0x16));
// Select Drive 0 on the Primary Bus // Select Drive 0 on the Primary Bus
outb(0x1f6, 0xa0); outb(0x1f6, 0xa0);

6
src/kernel/entry.asm Normal file
View File

@ -0,0 +1,6 @@
[BITS 32]
_start:
call main
extern main

76
src/kernel/gdt.c Normal file
View File

@ -0,0 +1,76 @@
#include "gdt.h"
gdt_entry gdt[] = {
(gdt_entry){ // Null Segment
.limit_lo = 0,
.limit_hi = 0,
.base_lo = 0,
.base_mid = 0,
.base_hi = 0,
.accessed = 0,
.read_write = 0,
.direction = 0,
.executable = 0,
.system_segment = 0,
.privilege = 0,
.present = 0,
.size = 0,
.granularity = 0,
.__ignored__ = 0,
},
(gdt_entry){ // Code Segment
.limit_lo = 0xffff,
.limit_hi = 0xf,
.base_lo = 0,
.base_mid = 0,
.base_hi = 0,
.accessed = 0,
.read_write = 1,
.direction = 0,
.executable = 1,
.system_segment = 1,
.privilege = 0,
.present = 1,
.size = 1,
.granularity = 1,
.__ignored__ = 0,
},
(gdt_entry){ // Data Segment
.limit_lo = 0xffff,
.limit_hi = 0xf,
.base_lo = 0,
.base_mid = 0,
.base_hi = 0,
.accessed = 0,
.read_write = 1,
.direction = 0,
.executable = 0,
.system_segment = 1,
.privilege = 0,
.present = 1,
.size = 1,
.granularity = 1,
.__ignored__ = 0,
}
};
gdt_descr descr = (gdt_descr){
.size = sizeof(gdt) - 1,
.offset = gdt,
};
__attribute__((far)) void far_call() {}
void init_gdt() {
asm volatile("lgdt %0;"
"mov $0x10, %%eax;"
"mov %%eax, %%ss;"
"mov $0x10, %%eax;"
"mov %%eax, %%ds" : : "m" (descr));
far_call();
}

32
src/kernel/gdt.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef GDT_H
#define GDT_H
#include "types.h"
typedef struct {
uint32_t limit_lo : 16;
uint32_t base_lo : 16;
uint32_t base_mid : 8;
uint32_t accessed : 1;
uint32_t read_write : 1;
uint32_t direction : 1;
uint32_t executable : 1;
uint32_t system_segment : 1;
uint32_t privilege : 2;
uint32_t present : 1;
uint32_t limit_hi : 4;
uint32_t __ignored__ : 2;
uint32_t size : 1;
uint32_t granularity : 1;
uint32_t base_hi : 8;
}__attribute__((packed)) gdt_entry;
typedef struct {
uint16_t size;
uint32_t offset;
}__attribute__((packed)) gdt_descr;
#endif

View File

@ -5,11 +5,12 @@
#include "keyboard.h" #include "keyboard.h"
#include "strings.h" #include "strings.h"
#include "atapio.h" #include "atapio.h"
#include "gdt.h"
int main() { int main() {
init_gdt();
init_idt(); init_idt();
init_term(); init_term();
// init_allocator();
printf("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved (as well as Virtual Memory / Paging!!!) and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n"); printf("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved (as well as Virtual Memory / Paging!!!) and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n");
@ -20,19 +21,11 @@ int main() {
enable_idt(); enable_idt();
init_atapio(); init_atapio();
//while (1);
uint8_t sector[512]; uint8_t sector[512];
read_sector(0, sector); read_sector(0, sector);
printf("OEM ID: %s\n", (char*)(sector+0x3)); uint8_t* filebuffer = (uint8_t*)dumb_alloc(0x3000);
char hellotxt[1024];
load_file("HELLO TXT", hellotxt);
printf("%s", hellotxt);
while (1) { while (1) {
printf(">>> "); printf(">>> ");
@ -43,7 +36,6 @@ int main() {
char* rest = split_on_first(' ', buffer); char* rest = split_on_first(' ', buffer);
if (strcmp(buffer, "help", 4)) { if (strcmp(buffer, "help", 4)) {
printf( printf(
"XnoeOS 32 Bit Mode Help.\n" "XnoeOS 32 Bit Mode Help.\n"
@ -64,7 +56,6 @@ int main() {
printf("%s\n", rest); printf("%s\n", rest);
} else if (strcmp(buffer, "type", 4)) { } else if (strcmp(buffer, "type", 4)) {
char filenamebuffer[12]; char filenamebuffer[12];
uint8_t* filebuffer = 0x1006400;
decode_filename(rest, filenamebuffer); decode_filename(rest, filenamebuffer);
if (!file_exists(filenamebuffer)) { if (!file_exists(filenamebuffer)) {
@ -72,8 +63,8 @@ int main() {
continue; continue;
} }
for (int i=0; i<1024; i++) for (int i=0; i<4096; i++)
hellotxt[i] = 0; filebuffer[i] = 0;
load_file(filenamebuffer, filebuffer); load_file(filenamebuffer, filebuffer);
printf(filebuffer); printf(filebuffer);

View File

@ -7,9 +7,8 @@ SECTIONS {
. = 0xc0000000; . = 0xc0000000;
.text : { .text : {
build/c_code_entry.o(.text) build/kernel/entry.o(.text)
build/kernel/kernel.o(.text) build/kernel/kernel.o(.text)
build/kernel/*(.text) build/kernel/*(.text)
heap_section = .;
} }
} }