tasking: continue working on executing ELF files

This commit is contained in:
2025-06-06 20:20:45 +03:00
parent ea6733fd46
commit 2408b67ccc
7 changed files with 4082 additions and 4 deletions
+4 -1
View File
@@ -12,6 +12,7 @@
#include "../include/multiboot.h"
#include "../include/liballoc.h"
#include "../include/kheap.h"
#include "../include/exec_from_file.h"
#include <stdint.h>
@@ -190,7 +191,7 @@ void kmain(unsigned int magic, unsigned int info)
//printf("after heap_init\n");
kheap_init();
__asm__ __volatile__ ("sti");
//scheduler_init();
scheduler_init();
printf("Kernel init sequence completed\n");
char yooo[256] = "heheh";
@@ -217,6 +218,8 @@ void kmain(unsigned int magic, unsigned int info)
printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2);
printf("huy1: %s\nhuy2: %s", huy, huy2);
exec_from_file("/boot/katauos.bin");
while(1)
{
/* if(inportb(0x64) & 0x1)
+130
View File
@@ -0,0 +1,130 @@
#include "../include/exec_from_file.h"
#include "../include/disk.h"
#include "../include/kheap.h"
#include "../include/liballoc.h"
#include "../include/string.h"
#include "../include/paging.h"
#include "../include/task.h"
#define DivRoundUp(number, divisor) ((number + divisor - 1) / divisor)
disk_ops_t ops1 =
{
.read = ata_read,
.write = ata_write,
};
static fat_t g_fat;
int read_file(const char* path, uint8_t** buffer)
{
file_t file;
int err = fat_fopen(&file, path, "r");
if (err)
return err;
uint32_t file_size = fat_fsize(&file);
printf("file_size: %X\n", file_size);
uint8_t buf[512];
//memset(buf, 0, 512);
printf("heh\n");
*buffer = (uint8_t*)malloc(file_size);
//memset(*buffer, 0, file_size);
uint8_t* current = *buffer;
printf("eeee\n");
int cnt = fat_fread(&file, *buffer, file_size);
/*for (;;)
{
int cnt = fat_fread(&file, buf, 512);
if (cnt < 0)
return cnt;
//printf("%s\n", buf);
memcpy(current, buf, 512);
current += 512;
if (cnt < 512)
break;
}*/
return fat_fclose(&file);
}
void exec_from_file(const char* filename)
{
asm volatile("cli");
int err, cnt;
err = fat_mount(&ops1, 0, &g_fat, "ebalo");
uint8_t* file;
int result = read_file("/ebalo/testfile", &file);
printf("result: %X\n", result);
printf("buffer: %s\n", file);
Elf32_Ehdr *elf_ehdr = (Elf32_Ehdr*)file;
int program_header_table_entry_count = elf_ehdr->e_phnum;
int program_header_table_entry_size = elf_ehdr->e_phentsize;
printf("magic: %X, type: %X, entry: 0x%X\n", elf_ehdr->e_ident, elf_ehdr->e_type, elf_ehdr->e_entry);
printf("PHNUM: %X\n", program_header_table_entry_count);
printf("PHENTSIZE: %X\n\n", program_header_table_entry_size);
uint32_t* proc_pd = create_page_dir();
uint32_t proc_pd_phys = (uint32_t)get_physaddr((void*)proc_pd);
printf("proc_pd_phys: 0x%X\n", proc_pd_phys);
uint32_t* curr_pd_virt = get_page_dir(); // CR3 содержит физический адрес, но get_page_dir возвращает его как uint32_t*
uint32_t curr_pd_phys = (uint32_t)curr_pd_virt; // Просто приводим к uint32_t, так как CR3 содержит физический адрес
set_page_dir(proc_pd_phys);
for(int i = 0; i < program_header_table_entry_count; i++)
{
Elf32_Phdr *elf_phdr = (Elf32_Phdr *)((uint32_t)file + elf_ehdr->e_phoff +
i * elf_ehdr->e_phentsize);
printf("HEADER %X, type: 0x%X, vaddr: 0x%X, paddr: 0x%X, memsz: 0x%X\n", i, elf_phdr->p_type, elf_phdr->p_vaddr, elf_phdr->p_paddr, elf_phdr->p_memsz);
if(elf_phdr->p_type != PT_LOAD)
{
printf("not PT_LOAD, skipping...\n");
continue;
}
int pages_needed = (elf_phdr->p_memsz / 0x1000) + 1;
printf("pages needed: %X\n", pages_needed);
for(int j = 0; j <= pages_needed; j++)
{
uint32_t vaddr = (elf_phdr->p_vaddr & ~0xFFF) + j * 0x1000;
void* phys_addr = alloc_page();
printf("mapping 0x%X -> 0x%X\n", phys_addr, vaddr);
//map_page_in_directory(proc_pd, phys_addr, (void*)vaddr, PAGE_PRESENT | PAGE_RW | PAGE_USER);
map_page(phys_addr, (void*)vaddr, PAGE_PRESENT | PAGE_RW | PAGE_USER);
}
memcpy((void*)elf_phdr->p_vaddr, file+elf_phdr->p_offset, elf_phdr->p_filesz);
uint32_t file_start = elf_phdr->p_vaddr + elf_phdr->p_filesz;
uint32_t file_end = elf_phdr->p_vaddr + pages_needed * 0x1000;
memset((void*)file_start, 0, file_end - file_start);
printf("p_filesz: 0x%X (%X)\n", elf_phdr->p_filesz, elf_phdr->p_filesz);
printf("================================\n\n\n\n");
}
Process* p_task1 = task_create(elf_ehdr->e_entry, NULL, 0, 3, proc_pd);
set_page_dir(curr_pd_phys);
asm volatile("sti");
}
+2 -2
View File
@@ -147,11 +147,11 @@ void scheduler_init()
task_create((uint32_t)idle, NULL, 0, 0, kernel_tasks_pagedir);
void* args1[] = {(void*)42, (void*)"ebalo"};
/*void* args1[] = {(void*)42, (void*)"ebalo"};
task_create((uint32_t)task1, args1, 2, 0, kernel_tasks_pagedir);
void* args2[] = {(void*)69, (void*)420};
task_create((uint32_t)task2, args2, 2, 0, kernel_tasks_pagedir);
jump_usermode2();
jump_usermode2();*/
}
#include "../include/string.h"