Compare commits

...

2 Commits

Author SHA1 Message Date
a2c05de604 mmap(), mprotect(), munmap(): Check more stuff 2022-10-17 19:32:24 +02:00
c2ecc4fe95 Update README.md 2022-10-17 19:32:02 +02:00
2 changed files with 35 additions and 10 deletions

View File

@ -11,6 +11,7 @@ Not so much at the moment.
- Can [load userspace ELF programs](kernel/src/sys/elf/) from the initial ramdisk as user tasks.
- [System call](kernel/src/sys/) interface and bare-bones [C Library](libs/libc/).
- Some very simple [example programs](apps/), written in C, that the kernel then loads from the initial ramdisk.
- UNIX-like [multitasking primitives](kernel/src/sys/exec.cpp).
## Setup
To build and run Luna, you will need to build a [GCC Cross-Compiler](https://wiki.osdev.org/Why_do_I_need_a_Cross_Compiler%3F) and cross-binutils for `x86_64-luna`. (Yes, Luna is advanced enough that it can use its own [OS-Specific Toolchain](https://wiki.osdev.org/OS_Specific_Toolchain), instead of a bare metal target like `x86_64-elf`. It is the first of my OS projects to be able to do so. The patches for Binutils and GCC are [binutils.patch](tools/binutils.patch) and [gcc.patch](tools/gcc.patch)).

View File

@ -3,6 +3,7 @@
#include "errno.h"
#include "interrupts/Context.h"
#include "log/Log.h"
#include "memory/Memory.h"
#include "memory/MemoryManager.h"
#include "memory/VMM.h"
#include "misc/utils.h"
@ -50,6 +51,12 @@ void sys_mmap(Context* context, void* address, size_t size, int prot)
if (address)
{
kdbgln("mmap(): %ld pages at address %p, %s", size / PAGE_SIZE, address, format_prot(prot));
if (Memory::is_kernel_address((uintptr_t)address))
{
kwarnln("munmap() failed: attempted to unmap a kernel page");
context->rax = MAP_FAIL(ENOMEM);
return;
}
if (VMM::get_physical((uint64_t)address) != (uint64_t)-1) // Address is already used.
{
kwarnln("attempt to map an already mapped address");
@ -67,7 +74,7 @@ void sys_mmap(Context* context, void* address, size_t size, int prot)
}
else
{
kwarnln("mmap() failed");
kwarnln("mmap() failed: failed to allocate physical memory");
context->rax = MAP_FAIL(ENOMEM);
return;
}
@ -75,6 +82,12 @@ void sys_mmap(Context* context, void* address, size_t size, int prot)
kdbgln("mmap(): %ld pages at any address, %s", Utilities::get_blocks_from_size(PAGE_SIZE, size), format_prot(prot));
uint64_t ptr =
Scheduler::current_task()->allocator.request_virtual_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size));
if (!ptr)
{
kwarnln("mmap() failed: failed to allocate virtual address");
context->rax = MAP_FAIL(ENOMEM);
return;
}
void* result = MemoryManager::get_pages_at(ptr, Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
if (result)
{
@ -84,7 +97,7 @@ void sys_mmap(Context* context, void* address, size_t size, int prot)
}
else
{
kwarnln("mmap() failed");
kwarnln("mmap() failed: failed to allocate physical memory");
context->rax = MAP_FAIL(ENOMEM);
return;
}
@ -111,10 +124,16 @@ void sys_munmap(Context* context, void* address, size_t size)
context->rax = -EINVAL;
return;
}
uint64_t flags = VMM::get_flags((uint64_t)address);
if (!(flags & MAP_USER))
if (Memory::is_kernel_address((uintptr_t)address))
{
kwarnln("munmap() failed: attempted to unmap a non-existent or kernel page");
kwarnln("munmap() failed: attempted to unmap a kernel page");
context->rax = -EINVAL;
return;
}
uint64_t phys = VMM::get_physical((uint64_t)address);
if (phys == (uint64_t)-1)
{
kwarnln("munmap() failed: attempted to unmap a non-existent page");
context->rax = -EINVAL;
return;
}
@ -145,15 +164,20 @@ void sys_mprotect(Context* context, void* address, size_t size, int prot)
}
if (!address)
{
kwarnln("mprotect() failed: attempted to unmap page 0");
kwarnln("mprotect() failed: attempted to protect page 0");
context->rax = -EINVAL;
return;
}
uint64_t flags = VMM::get_flags((uint64_t)address);
if (!(flags & MAP_USER))
if (Memory::is_kernel_address((uintptr_t)address))
{
kwarnln("mprotect() failed: attempted to protect a non-existent or kernel page");
kwarnln("mprotect() failed: attempted to protect a kernel page");
context->rax = -EINVAL;
return;
}
uint64_t phys = VMM::get_physical((uint64_t)address);
if (phys == (uint64_t)-1)
{
kwarnln("mprotect() failed: attempted to protect a non-existent page");
context->rax = -EINVAL;
return;
}