chatgpt solved this one for me

This commit is contained in:
Андреев Григорий 2026-04-06 05:41:34 +03:00
parent 49ce1a2d45
commit f601c99d39
3 changed files with 54 additions and 10 deletions

View File

@ -118,17 +118,44 @@ void trap(registers_t *r) {
} }
/* takes a ptr that is supposed to be from userspace. If it is not, we /* takes a ptr that is supposed to be from userspace. If it is not, we
* return 0, if it is a valid userspace pointer, we return the usable * return 0, if it is a valid we return 1
* kernelspace pointer
*/ */
uint32_t is_userspace_ptr_mapped(uint32_t ptr) { bool is_userspace_ptr_mapped(uint32_t ptr) {
// todo: implement if (ptr >= KERNBASE) {
return ptr; return 0;
}
pde_t *pgdir = get_user_proc_page_directory();
if (!pgdir) {
return 0;
}
pde_t pde = pgdir[PDX(ptr)];
if ((pde & (PTE_P | PTE_U)) != (PTE_P | PTE_U)) {
return 0;
}
if (pde & PDE_PS) {
return 0;
}
pte_t *table = (pte_t*)P2V(PTE_ADDR(pde));
pte_t pte = table[PTX(ptr)];
if ((pte & (PTE_P | PTE_U)) != (PTE_P | PTE_U)) {
return 0;
}
return 1;
} }
static void* get_userspace_cstr(uint32_t ptr) { static void* get_userspace_cstr(uint32_t ptr) {
// todo: implement for (uint32_t addr = ptr;; addr++) {
return (void*)(ptr); if (addr == 0 || !is_userspace_ptr_mapped(addr)) {
return 0;
}
if (*(const char*)addr == '\0') {
return (void*)ptr;
}
}
} }
static int handle_puts(const char* s) { static int handle_puts(const char* s) {
@ -140,6 +167,7 @@ static int handle_puts(const char* s) {
} }
static void handle_syscall(registers_t* r) { static void handle_syscall(registers_t* r) {
int ret;
switch (r->eax) { switch (r->eax) {
case SYS_exit: case SYS_exit:
if (r->ebx == 0) { if (r->ebx == 0) {
@ -157,11 +185,16 @@ static void handle_syscall(registers_t* r) {
r->eax = 0; r->eax = 0;
break; break;
case SYS_puts: case SYS_puts:
r->eax = handle_puts(get_userspace_cstr(r->ebx)); ret = handle_puts(get_userspace_cstr(r->ebx));
if (ret < 0) {
printk("SYS_puts panic: page fault\n");
killproc();
}
r->eax = 0;
break; break;
default: default:
printk("Unknown syscall\n"); printk("Userspace panic: Unknown syscall\n");
r->eax = -1; killproc();
} }
} }

3
proc.c
View File

@ -28,6 +28,9 @@ void trapret();
void swtch(void** oldstack, void* newstack); void swtch(void** oldstack, void* newstack);
pde_t *get_user_proc_page_directory() { pde_t *get_user_proc_page_directory() {
// if (!vm.user_task) {
// return 0;
// }
return vm.user_task->pgdir; return vm.user_task->pgdir;
} }

8
user/snake.c Normal file
View File

@ -0,0 +1,8 @@
#include "../syscall.h"
#include <stdint.h>
#include <stdbool.h>
int main() {
return 0;
}