syscalls: fix page count for mapping in sys_execve and make caller run coreutils

This commit is contained in:
2025-10-07 23:41:55 +03:00
parent 88c1936e83
commit 26c8280059
2 changed files with 11 additions and 4 deletions
+2 -2
View File
@@ -9,7 +9,7 @@ int main() {
// Arguments for the new program // Arguments for the new program
// The first argument is conventionally the program name // The first argument is conventionally the program name
char *argv[] = { "./callee", "first_arg", "second_arg", NULL }; char *argv[] = { "/katau/core", "--help", NULL };
// Environment variables for the new program // Environment variables for the new program
char *envp[] = { "CUSTOM_VAR=Hello from caller!", "ANOTHER_VAR=123", NULL }; char *envp[] = { "CUSTOM_VAR=Hello from caller!", "ANOTHER_VAR=123", NULL };
@@ -18,7 +18,7 @@ int main() {
// The first argument is the path to the executable // The first argument is the path to the executable
// The second is the array of arguments // The second is the array of arguments
// The third is the array of environment variables // The third is the array of environment variables
execve("/katau/callee.", argv, envp); execve("/katau/core.", argv, envp);
// This part of the code will only be reached if execve fails // This part of the code will only be reached if execve fails
perror("execve failed"); perror("execve failed");
+9 -2
View File
@@ -323,10 +323,17 @@ int sys_execve(TrapFrame *tf) {
for (int i = 0; i < elf_header->e_phnum; i++) { for (int i = 0; i < elf_header->e_phnum; i++) {
Elf32_Phdr *p_header = (Elf32_Phdr *)(file_buffer + elf_header->e_phoff + i * elf_header->e_phentsize); Elf32_Phdr *p_header = (Elf32_Phdr *)(file_buffer + elf_header->e_phoff + i * elf_header->e_phentsize);
if (p_header->p_type == PT_LOAD) { if (p_header->p_type == PT_LOAD) {
for (uint32_t vaddr = p_header->p_vaddr; vaddr < p_header->p_vaddr + p_header->p_memsz; vaddr += PAGE_SIZE) { uint32_t start_addr = p_header->p_vaddr;
uint32_t end_addr = start_addr + p_header->p_memsz;
uint32_t start_page = start_addr & ~0xFFF;
uint32_t end_page = (end_addr - 1) & ~0xFFF;
for (uint32_t vaddr = start_page; vaddr <= end_page; vaddr += PAGE_SIZE) {
void* phys_addr = alloc_page(); void* phys_addr = alloc_page();
map_page(phys_addr, (void*)(vaddr & ~0xFFF), PAGE_PRESENT | PAGE_RW | PAGE_USER); map_page(phys_addr, (void*)vaddr, PAGE_PRESENT | PAGE_RW | PAGE_USER);
} }
memcpy((void*)p_header->p_vaddr, file_buffer + p_header->p_offset, p_header->p_filesz); memcpy((void*)p_header->p_vaddr, file_buffer + p_header->p_offset, p_header->p_filesz);
if (p_header->p_memsz > p_header->p_filesz) { if (p_header->p_memsz > p_header->p_filesz) {
memset((void*)(p_header->p_vaddr + p_header->p_filesz), 0, p_header->p_memsz - p_header->p_filesz); memset((void*)(p_header->p_vaddr + p_header->p_filesz), 0, p_header->p_memsz - p_header->p_filesz);