The kernel is now actually useable with paging enabled.

This commit is contained in:
Xnoe 2021-10-12 00:53:02 +01:00
parent 1225529708
commit e2bb3ff634
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
9 changed files with 108 additions and 9 deletions

View File

@ -2,7 +2,7 @@ CFLAGS = -m32 -mgeneral-regs-only -nostdlib -fno-builtin -fno-exceptions -fno-le
LDFLAGS =
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
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
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
@ -23,6 +23,7 @@ prepare:
mkdir -p build/boot
mkdir -p build/boot_stage2
mkdir -p build/kernel
mountpoint img.d | grep not || umount img.d
clean:
rm -rf build

View File

@ -119,9 +119,13 @@ void main() {
map_4k_phys_to_virt((uint32_t)kernel_page_directory, 0xc0100000, kernel_page_directory, kernel_page_tables); // Map the page directory to 0xc0100000
map_many_4k_phys_to_virt(0x121000, 0xc0101000, kernel_page_directory, kernel_page_tables, 1024); // Map 1024 pages from 0x121000 to 0xc0101000
map_4k_phys_to_virt(0xb8000, 0xc0501000, kernel_page_directory, kernel_page_tables); // Map 0xb8000 (video) to 0xc0501000
map_4k_phys_to_virt(0x521000, 0xc0502000, kernel_page_directory, kernel_page_tables); // Map the PTE** data, we'll need to convert the pointers to point at kernel space at some point.
// Map the bitmap
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(0x8f000, 0x8f000, kernel_page_directory, kernel_page_tables);
map_many_4k_phys_to_virt(0x8a000, 0x8a000, kernel_page_directory, kernel_page_tables, 6);
load_file("KERNEL BIN", kernel_location);

72
src/kernel/allocate.c Normal file
View File

@ -0,0 +1,72 @@
#include "allocate.h"
uint8_t* bitmap = 0xc0600000;
PDE* kernel_page_directory = 0xc0100000;
PTE** kernel_page_tables = 0xc0502000;
uint32_t kernel_allocate_area = 0xf0000000;
uint32_t last_free_page = 0;
/*void init_allocator() {
for (int i=0; i<1024; i++) {
kernel_page_tables[i] = 0xc0101000 + 0x1000*i;
}
}*/ // Don't do this.
void set_bit(uint32_t offset, uint8_t* buffer) {
uint32_t index = offset / 8;
uint32_t bit = offset % 8;
buffer[index] |= (1<<(7 - bit));
}
uint8_t get_bit(uint32_t offset, uint8_t* buffer) {
uint32_t index = offset / 8;
uint32_t bit = offset % 8;
return buffer[index] & (1<<(7 - bit));
}
void unset_bit(uint32_t offset, uint8_t* buffer) {
uint32_t index = offset / 8;
uint32_t bit = offset % 8;
buffer[index] &= (255 - (1<<(7 - bit)));
}
void mark_unavailble(uint32_t address, uint32_t size) {
// This function takes a physical address and length and marks the corresponding pages as unavailable.
address -= address % 4096;
if (size % 4096)
size += 4096 - (size % 4096);
address /= 4096;
size /= 4096;
for (int i=0; i<size; i++) {
unset_bit(address + i, bitmap);
}
}
void* dumb_alloc(uint32_t size) {
if (size % 4096)
size += 4096 - (size % 4096); // Since this is dumb alloc just make the amount we're allocating be in whole page sizes
uint32_t ptr = kernel_allocate_area;
while (size > 0) {
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;
mark_unavailble(phys_addr, 4096);
map_4k_phys_to_virt(phys_addr, kernel_allocate_area, kernel_page_directory, kernel_page_tables);
kernel_allocate_area += 4096;
size -= 4096;
}
return (void*)ptr;
}

15
src/kernel/allocate.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef ALLOCATE_H
#define ALLOCATE_H
#include "paging.h"
//void init_allocator();
void set_bit(uint32_t offset, uint8_t* buffer);
void unset_bit(uint32_t offset, uint8_t* buffer);
uint8_t get_bit(uint32_t offset, uint8_t* buffer);
void mark_unavailble(uint32_t address, uint32_t size);
void* dumb_alloc(uint32_t size);
#endif

View File

@ -11,8 +11,8 @@
uint16_t identify_result[256];
uint32_t total_28_lbas = 0;
uint8_t* rootDirEntries = 0x1000000;
uint16_t* FAT1 = 0x1002000;
uint8_t* rootDirEntries;
uint16_t* FAT1;
uint16_t countReserved;
uint8_t countFATs;
@ -21,6 +21,10 @@ uint16_t sectorsPerFAT;
void init_atapio() {
rootDirEntries = (uint8_t*)dumb_alloc(8192);
FAT1 = (uint16_t*)dumb_alloc(512 * 34);
printf("RDE: %x\nFAT1: %x\n", rootDirEntries, FAT1);
countReserved = *(uint16_t*)0x7c0e;
countFATs = *(uint8_t*)0x7c10;
countRDEs = *(uint16_t*)0x7c11;

View File

@ -6,6 +6,7 @@
#include "io.h"
#include "types.h"
#include "strings.h"
#include "allocate.h"
void init_atapio();
void read_sector(uint32_t address, uint8_t* buffer);

View File

@ -7,19 +7,21 @@
#include "atapio.h"
int main() {
while (1);
init_idt();
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("KERNEL OK!\n");
printf("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n");
init_keyboard();
enable_idt();
init_atapio();
//while (1);
uint8_t sector[512];
read_sector(0, sector);

View File

@ -24,7 +24,7 @@ void map_4k_phys_to_virt(uint32_t physical, uint32_t virtual, PDE* page_director
.ignored2 = 0
};
page_tables[split->pd_index][split->pt_index] = (PTE){
((PTE*)((uint32_t)page_tables[split->pd_index] + 0xbffe0000))[split->pt_index] = (PTE){
.address = physical >> 12,
.available = 0,
.global = 0,

View File

@ -1,6 +1,6 @@
#include "screenstuff.h"
uint16_t* VMEM_ADDR = (uint16_t*)0xb8000;
uint16_t* VMEM_ADDR = (uint16_t*)0xc0501000;
const int TERM_WIDTH = 80;
const int TERM_HEIGHT = 25;