merge branch memory-shit into higher-half
This commit is contained in:
+26
-2
@@ -2,6 +2,8 @@
|
||||
#include "../include/string.h"
|
||||
#include "../include/paging.h"
|
||||
|
||||
struct tss_entry_struct tss;
|
||||
|
||||
void bss_init()
|
||||
{
|
||||
memset((void *)((int)_edata), 0, KERNEL_BSS_SIZE);
|
||||
@@ -29,6 +31,16 @@ void load_gdt(uint32_t gdt_ptr) {
|
||||
);
|
||||
}
|
||||
|
||||
void flush_tss(void) {
|
||||
asm volatile (
|
||||
"mov %0, %%ax\n\t"
|
||||
"ltr %%ax\n\t"
|
||||
:
|
||||
: "i" (0x28)
|
||||
: "ax"
|
||||
);
|
||||
}
|
||||
|
||||
static void gdt_set_entry(int num, uint32_t base_addr, uint32_t limit, char loflags, char hiflags)
|
||||
{
|
||||
num /= sizeof(struct seg_desc);
|
||||
@@ -44,6 +56,17 @@ void gdt_init(void)
|
||||
{
|
||||
uint8_t loflags;
|
||||
|
||||
memset(&tss, 0, sizeof(tss));
|
||||
tss.ss0 = KERNEL_DS;
|
||||
tss.iomap_base = 0xFFFF; // Disable I/O bitmap
|
||||
tss.esp0 = 0xC0010000; /* kernel stack address (firstly defined in boot.asm)*/
|
||||
tss.cs = KERNEL_CS;
|
||||
tss.ds = KERNEL_DS;
|
||||
tss.es = KERNEL_DS;
|
||||
tss.fs = KERNEL_DS;
|
||||
tss.gs = KERNEL_DS;
|
||||
tss.ss = KERNEL_DS;
|
||||
|
||||
gdt_set_entry(0, 0, 0, 0, 0); /* null descriptor */
|
||||
|
||||
loflags = SD_CODE | SD_CD | SD_DPL0 | SD_PRESENT;
|
||||
@@ -56,8 +79,9 @@ void gdt_init(void)
|
||||
loflags = SD_DATA | SD_CD | SD_DPL3 | SD_PRESENT;
|
||||
gdt_set_entry(USER_DS, 0, 0xFFFFFFFF, loflags, SD_OPSIZE32 | SD_PAGE4KB);
|
||||
|
||||
loflags = SD_TSSPRESENT;
|
||||
gdt_set_entry(TSS, 0, sizeof(struct tss_entry_struct), loflags, SD_OPSIZE32);
|
||||
loflags = 0x89; // P=1, DPL=0, S=0 (system), Type=0x9 (32-bit TSS)
|
||||
gdt_set_entry(TSS, (uint32_t)&tss, sizeof(struct tss_entry_struct) - 1, SD_PRESENT | SD_DPL0 | SD_TSSPRESENT, 0);
|
||||
|
||||
load_gdt((uint32_t)&gdtr);
|
||||
flush_tss();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
section .text
|
||||
global page_fault_handler
|
||||
extern handle_page_fault ; C function to process the fault
|
||||
|
||||
page_fault_handler:
|
||||
; Save registers to preserve state
|
||||
pushad ; Push EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
|
||||
push ds
|
||||
push es
|
||||
push fs
|
||||
push gs
|
||||
|
||||
; Set up kernel data segment
|
||||
mov ax, 0x10 ; Kernel data segment selector (adjust based on your GDT)
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
; Get the faulting address from CR2
|
||||
mov eax, cr2
|
||||
|
||||
; Push parameters for the C function:
|
||||
; - Faulting address (from CR2)
|
||||
; - Error code (at [esp + 48], after pushed registers and segment selectors)
|
||||
push eax ; Push CR2 (faulting address)
|
||||
mov ebx, [esp + 48] ; Get error code (adjust offset based on stack layout)
|
||||
push ebx ; Push error code
|
||||
|
||||
; Call the C handler
|
||||
call handle_page_fault
|
||||
|
||||
; Clean up parameters from stack
|
||||
add esp, 8 ; Remove error code and CR2
|
||||
|
||||
; Restore registers
|
||||
pop gs
|
||||
pop fs
|
||||
pop es
|
||||
pop ds
|
||||
popad
|
||||
|
||||
; Remove error code from stack
|
||||
add esp, 4 ; Pop error code
|
||||
|
||||
; Return from interrupt
|
||||
iret ; Restore EIP, CS, EFLAGS (and ESP, SS if privilege change)
|
||||
+27
-1
@@ -23,6 +23,7 @@ void isr_custom();
|
||||
void default_handler();
|
||||
|
||||
extern void sys_exit_handler();
|
||||
extern void page_fault_handler();
|
||||
|
||||
void isr_install() {
|
||||
idt_set_descriptor(0, (uint32)isr0, 0x8E);
|
||||
@@ -39,7 +40,7 @@ void isr_install() {
|
||||
idt_set_descriptor(11, (uint32)isr11, 0x8E);
|
||||
idt_set_descriptor(12, (uint32)isr12, 0x8E);
|
||||
idt_set_descriptor(13, (uint32)isr13, 0x8E);
|
||||
idt_set_descriptor(14, (uint32)isr14, 0x8E);
|
||||
idt_set_descriptor(14, (uint32)page_fault_handler, 0x8E);
|
||||
idt_set_descriptor(15, (uint32)isr15, 0x8E);
|
||||
idt_set_descriptor(16, (uint32)isr16, 0x8E);
|
||||
idt_set_descriptor(17, (uint32)isr17, 0x8E);
|
||||
@@ -75,6 +76,31 @@ void default_handler()
|
||||
|
||||
}
|
||||
|
||||
#include "../include/paging.h"
|
||||
|
||||
void handle_page_fault(uint32_t error_code, uint32_t faulting_address)
|
||||
{
|
||||
int present = !(error_code & 0x1); // Bit 0: 0 = not present, 1 = protection violation
|
||||
int write = error_code & 0x2; // Bit 1: 0 = read, 1 = write
|
||||
int user = error_code & 0x4; // Bit 2: 0 = supervisor, 1 = user
|
||||
int reserved = error_code & 0x8; // Bit 3: 0 = no reserved bits, 1 = reserved bits
|
||||
int instruction = error_code & 0x10; // Bit 4: 0 = data, 1 = instruction fetch
|
||||
|
||||
uint32_t pte = get_pte((void*)faulting_address);
|
||||
|
||||
printf("Page fault at address 0x%x\n", faulting_address);
|
||||
printf("Present: %X, Write: %X, User: %X, Reserved: %X, Instruction: %X\n",
|
||||
present, write, user, reserved, instruction);
|
||||
|
||||
printf("PTE: 0x%X (phys: 0x%X, %s, %s)\n", pte, pte & ~0xFFF,
|
||||
(pte & 0x2) ? "writable" : "read-only",
|
||||
(pte & 0x4) ? "user" : "supervisor");
|
||||
|
||||
printf("ERR: 0x%X\n", error_code);
|
||||
|
||||
asm("hlt");
|
||||
}
|
||||
|
||||
__attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame)
|
||||
{
|
||||
pit_init(100);
|
||||
|
||||
+25
-6
@@ -153,7 +153,13 @@ void kmain(unsigned int magic, unsigned int info)
|
||||
printf("bootloader: %s\n", mbi.boot_loader_name);
|
||||
}
|
||||
|
||||
printf("init_allocator...");
|
||||
init_allocator();
|
||||
printf("done\n");
|
||||
|
||||
printf("grub_memory_map...");
|
||||
grub_memory_map(magic, &mbi);
|
||||
printf("done\n");
|
||||
|
||||
isr_install();
|
||||
pic_remap(0x20, 0x28);
|
||||
@@ -161,14 +167,27 @@ void kmain(unsigned int magic, unsigned int info)
|
||||
|
||||
apply_pic_masks();
|
||||
|
||||
printf("done fucking with pages and memory map!\n");
|
||||
char *test_str;
|
||||
printf("test_str: %X, &test_str %X\n", test_str, &test_str);
|
||||
test_str = alloc_page();
|
||||
printf("test_str: %X, &test_str %X\n", test_str, &test_str);
|
||||
free_page(test_str);
|
||||
|
||||
alloc_page();//TODO: fix this this, because now we MUST allocate a page for checks of successfulness of allocation to pass
|
||||
//the first page is at 0x0 and in check we check by if(!<allocated page>) which returns 0 (false), yeah
|
||||
|
||||
|
||||
//while(1){}
|
||||
|
||||
//paging_init();
|
||||
//printf("paging_init done\n");
|
||||
//test_paging();
|
||||
init_allocator();
|
||||
heap_init();
|
||||
printf("after heap_init\n");
|
||||
|
||||
//heap_init();
|
||||
//printf("after heap_init\n");
|
||||
__asm__ __volatile__ ("sti");
|
||||
//scheduler_init();
|
||||
scheduler_init();
|
||||
printf("Kernel init sequence completed\n");
|
||||
|
||||
char yooo[256] = "heheh";
|
||||
@@ -185,8 +204,8 @@ void kmain(unsigned int magic, unsigned int info)
|
||||
char temp1[1024] = "rusya_krutoy";
|
||||
char temp2[1024] = "katya_tozhe_krutaya";
|
||||
|
||||
huy = heap_alloc(1024);
|
||||
huy2 = heap_alloc(1024);
|
||||
huy = alloc_page();//heap_alloc(1024);
|
||||
huy2 = alloc_page();//heap_alloc(1024);
|
||||
|
||||
memcpy(huy, temp1, sizeof(temp1));
|
||||
memcpy(huy2, temp2, sizeof(temp1));
|
||||
|
||||
Reference in New Issue
Block a user