Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
mprotect.c
Go to the documentation of this file.
1#include "hal/cpu/core.h"
2#include "hal/cpu/paging.h"
3#include "libk/serial.h"
5#include "memory/vm_manager.h"
6#include "sys/err_no.h"
7#include <sys/syscall.h>
8
9int syscall_mprotect(void* addr, size_t len, int prot) {
10 (void)addr;
11 (void)len;
12 (void)prot;
13
14 serial2_printf("mprotect: addr 0x%x, len 0x%x, prot %d\n", addr, len,
15 prot);
16 auto thr = get_current_core_data()->active_thread;
17 auto procc = thr->process;
18
19 if (!thr || !procc) {
20 return -1;
21 }
22
23 uint64_t mmap_flags = PAGE_USER;
24 if (!prot) {
25 mmap_flags = 0;
26 } else {
27 if (prot & PROT_READ)
28 mmap_flags |= PAGE_PRESENT;
29 if (prot & PROT_WRITE)
30 mmap_flags |= PAGE_WRITABLE;
31 if (!(prot & PROT_EXEC))
32 mmap_flags |= PAGE_NO_EXECUTE;
33
34 if (prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
35 mmap_flags |= PAGE_PRESENT;
36 }
37
38 auto addr_4kb = ALIGN_DOWN((uintptr_t)addr, 0x1000);
39 serial2_printf("mrpotect: base aligned 0x%x\n", addr_4kb);
40
41 auto mm = vma_find(procc->vm_page, (uintptr_t)addr);
42 if (!mm) {
43 return -1;
44 }
45
46 auto len_4kb = ALIGN_UP(len, 0x1000) / 0x1000;
47 vxMultipleMmap(thr->page, (uint64_t)addr, (uint64_t)mm->phys_address,
48 len_4kb, mmap_flags);
49
50 return 0;
51}
each_core_data * get_current_core_data(void)
Definition core.c:54
volatile uint64_t addr
Definition e1000.hpp:0
void serial2_printf(const char *fmt,...)
#define ALIGN_DOWN(x, align)
Definition memory_utils.h:9
#define ALIGN_UP(x, align)
Definition memory_utils.h:6
int syscall_mprotect(void *addr, size_t len, int prot)
Definition mprotect.c:9
size_t len
Definition oct2bin.h:7
void vxMultipleMmap(page_t page_dir, uint64_t virt, uint64_t phys, uint64_t size, uint64_t flags)
Definition paging.c:340
#define PAGE_PRESENT
Definition paging.h:34
#define PAGE_NO_EXECUTE
Definition paging.h:43
#define PAGE_USER
Definition paging.h:36
#define PAGE_WRITABLE
Definition paging.h:35
#define PROT_READ
Definition syscall.h:26
#define PROT_WRITE
Definition syscall.h:27
#define PROT_EXEC
Definition syscall.h:28
unsigned long uintptr_t
Definition type.h:73
unsigned long uint64_t
Definition type.h:25
virtual_memory_t * vma_find(struct virtual_memory_page *page, uintptr_t virt_addr)
Definition vm_manager.c:117