mprotect (nr 10)
Linux Signature
int mprotect(void *addr, size_t len, int prot);
Description
Changes the access protections for the calling process’s memory pages in the range [addr, addr+len).
Implementation
- Validates
addris page-aligned andlen > 0(returns-EINVALotherwise). - Aligns
lenup to the next page boundary. - Splits/updates VMAs in the range via
Process::mprotect_vmas():- Entire VMA overlap: updates prot in place.
- Partial overlap (front, tail, middle): splits VMA and sets new prot on the affected portion.
- Converts
protto x86-64 page table flags (prot_to_page_flags()):PROT_NONE→USER_ACCESSIBLEonly (noPRESENT— any access faults).PROT_READ→PRESENT | USER_ACCESSIBLE | NO_EXECUTE.PROT_WRITE→ addsWRITABLE.PROT_EXEC→ removesNO_EXECUTE.
- Updates page table entries via
MemoryServices::update_user_page_flags()with TLB flush. - Returns 0 on success.
Lock ordering: PROCESS_TABLE first (VMA split), then MEMORY (page table update).
Returns 0 (no-op) if no VMAs overlap the requested range (Linux semantics).
Source: osl/src/syscalls/mem.rs (sys_mprotect), libkernel/src/process.rs (mprotect_vmas), libkernel/src/memory/mod.rs (update_user_page_flags)