Compare commits
2 Commits
a002e75725
...
613f8170b6
Author | SHA1 | Date | |
---|---|---|---|
613f8170b6 | |||
62d631f1b4 |
@ -17,6 +17,7 @@
|
|||||||
#define SYS_seek 12
|
#define SYS_seek 12
|
||||||
#define SYS_exec 13
|
#define SYS_exec 13
|
||||||
#define SYS_fcntl 14
|
#define SYS_fcntl 14
|
||||||
|
#define SYS_mprotect 15
|
||||||
|
|
||||||
namespace Syscall
|
namespace Syscall
|
||||||
{
|
{
|
||||||
@ -32,11 +33,12 @@ void sys_write(Context* context, int fd, size_t size, const char* addr);
|
|||||||
void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, uint64_t col);
|
void sys_paint(Context* context, uint64_t x, uint64_t y, uint64_t w, uint64_t h, uint64_t col);
|
||||||
void sys_rand(Context* context);
|
void sys_rand(Context* context);
|
||||||
void sys_gettid(Context* context);
|
void sys_gettid(Context* context);
|
||||||
void sys_mmap(Context* context, void* address, size_t size, int flags);
|
void sys_mmap(Context* context, void* address, size_t size, int prot);
|
||||||
void sys_munmap(Context* context, void* address, size_t size);
|
void sys_munmap(Context* context, void* address, size_t size);
|
||||||
void sys_open(Context* context, const char* filename, int flags);
|
void sys_open(Context* context, const char* filename, int flags);
|
||||||
void sys_read(Context* context, int fd, size_t size, char* buffer);
|
void sys_read(Context* context, int fd, size_t size, char* buffer);
|
||||||
void sys_close(Context* context, int fd);
|
void sys_close(Context* context, int fd);
|
||||||
void sys_seek(Context* context, int fd, long offset, int whence);
|
void sys_seek(Context* context, int fd, long offset, int whence);
|
||||||
void sys_exec(Context* context, const char* pathname);
|
void sys_exec(Context* context, const char* pathname);
|
||||||
void sys_fcntl(Context* context, int fd, int command, uintptr_t arg);
|
void sys_fcntl(Context* context, int fd, int command, uintptr_t arg);
|
||||||
|
void sys_mprotect(Context* context, void* address, size_t size, int prot);
|
@ -1,7 +1,7 @@
|
|||||||
#define MODULE "gdt"
|
#define MODULE "gdt"
|
||||||
|
|
||||||
#include "gdt/GDT.h"
|
#include "gdt/GDT.h"
|
||||||
#include "assert.h"
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "std/string.h"
|
#include "std/string.h"
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
#define MODULE "init"
|
#define MODULE "init"
|
||||||
|
|
||||||
#include "init/Init.h"
|
#include "init/Init.h"
|
||||||
#include "assert.h"
|
|
||||||
#include "bootboot.h"
|
#include "bootboot.h"
|
||||||
#include "cpu/CPU.h"
|
#include "cpu/CPU.h"
|
||||||
#include "init/InitRD.h"
|
#include "init/InitRD.h"
|
||||||
#include "interrupts/Interrupts.h"
|
#include "interrupts/Interrupts.h"
|
||||||
#include "io/Serial.h"
|
#include "io/Serial.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "memory/PMM.h"
|
#include "memory/PMM.h"
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#define MODULE "isr"
|
#define MODULE "isr"
|
||||||
|
|
||||||
#include "assert.h"
|
|
||||||
#include "interrupts/Context.h"
|
#include "interrupts/Context.h"
|
||||||
#include "interrupts/IRQ.h"
|
#include "interrupts/IRQ.h"
|
||||||
#include "interrupts/Interrupts.h"
|
#include "interrupts/Interrupts.h"
|
||||||
#include "io/Serial.h"
|
#include "io/Serial.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "misc/hang.h"
|
#include "misc/hang.h"
|
||||||
#include "panic/Panic.h"
|
#include "panic/Panic.h"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#define MODULE "idt"
|
#define MODULE "idt"
|
||||||
|
|
||||||
#include "interrupts/IDT.h"
|
#include "interrupts/IDT.h"
|
||||||
#include "assert.h"
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
|
|
||||||
struct IDTEntry
|
struct IDTEntry
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#define MODULE "main"
|
#define MODULE "main"
|
||||||
|
|
||||||
#include "acpi/RSDT.h"
|
#include "acpi/RSDT.h"
|
||||||
#include "assert.h"
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "cpu/CPU.h"
|
#include "cpu/CPU.h"
|
||||||
#include "fs/VFS.h"
|
#include "fs/VFS.h"
|
||||||
@ -15,6 +14,7 @@
|
|||||||
#include "io/PCI.h"
|
#include "io/PCI.h"
|
||||||
#include "io/PIC.h"
|
#include "io/PIC.h"
|
||||||
#include "io/Serial.h"
|
#include "io/Serial.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "memory/AddressSpace.h"
|
#include "memory/AddressSpace.h"
|
||||||
#include "memory/Memory.h"
|
#include "memory/Memory.h"
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "assert.h"
|
#include "kassert.h"
|
||||||
#include "memory/KernelHeap.h"
|
#include "memory/KernelHeap.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "memory/PMM.h"
|
#include "memory/PMM.h"
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#define MODULE "mem"
|
#define MODULE "mem"
|
||||||
|
|
||||||
#include "memory/PMM.h"
|
#include "memory/PMM.h"
|
||||||
#include "assert.h"
|
|
||||||
#include "bootboot.h"
|
#include "bootboot.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "memory/Memory.h"
|
#include "memory/Memory.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "misc/utils.h"
|
#include "misc/utils.h"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#define MODULE "vmm"
|
#define MODULE "vmm"
|
||||||
|
|
||||||
#include "memory/VMM.h"
|
#include "memory/VMM.h"
|
||||||
#include "assert.h"
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "memory/PMM.h"
|
#include "memory/PMM.h"
|
||||||
#include "misc/utils.h"
|
#include "misc/utils.h"
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#define MODULE "rand"
|
#define MODULE "rand"
|
||||||
|
|
||||||
#include "rand/Mersenne.h"
|
#include "rand/Mersenne.h"
|
||||||
#include "assert.h"
|
#include "kassert.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef uint64_t word_t;
|
typedef uint64_t word_t;
|
||||||
|
@ -26,6 +26,7 @@ void Syscall::entry(Context* context)
|
|||||||
case SYS_seek: sys_seek(context, (int)context->rdi, (long)context->rsi, (int)context->rdx); break;
|
case SYS_seek: sys_seek(context, (int)context->rdi, (long)context->rsi, (int)context->rdx); break;
|
||||||
case SYS_exec: sys_exec(context, (const char*)context->rdi); break;
|
case SYS_exec: sys_exec(context, (const char*)context->rdi); break;
|
||||||
case SYS_fcntl: sys_fcntl(context, (int)context->rdi, (int)context->rsi, context->rdx); break;
|
case SYS_fcntl: sys_fcntl(context, (int)context->rdi, (int)context->rsi, context->rdx); break;
|
||||||
|
case SYS_mprotect: sys_mprotect(context, (void*)context->rdi, context->rsi, (int)context->rdx); break;
|
||||||
default: context->rax = -ENOSYS; break;
|
default: context->rax = -ENOSYS; break;
|
||||||
}
|
}
|
||||||
VMM::exit_syscall_context();
|
VMM::exit_syscall_context();
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#define MODULE "elf"
|
#define MODULE "elf"
|
||||||
|
|
||||||
#include "sys/elf/ELFLoader.h"
|
#include "sys/elf/ELFLoader.h"
|
||||||
#include "assert.h"
|
|
||||||
#include "errno.h"
|
#include "errno.h"
|
||||||
#include "fs/VFS.h"
|
#include "fs/VFS.h"
|
||||||
#include "init/InitRD.h"
|
#include "init/InitRD.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "memory/Memory.h"
|
#include "memory/Memory.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#define MODULE "exec"
|
#define MODULE "exec"
|
||||||
|
|
||||||
#include "assert.h"
|
|
||||||
#include "errno.h"
|
#include "errno.h"
|
||||||
#include "interrupts/Interrupts.h"
|
#include "interrupts/Interrupts.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "memory/PMM.h"
|
#include "memory/PMM.h"
|
||||||
#include "memory/VMM.h"
|
#include "memory/VMM.h"
|
||||||
|
@ -8,9 +8,30 @@
|
|||||||
#include "misc/utils.h"
|
#include "misc/utils.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#define MAP_READ 1
|
||||||
|
#define MAP_WRITE 2
|
||||||
|
#define MAP_NONE 0
|
||||||
|
|
||||||
#define MAP_FAIL(errno) 0xffffffffffffff00 | (unsigned char)(errno)
|
#define MAP_FAIL(errno) 0xffffffffffffff00 | (unsigned char)(errno)
|
||||||
|
|
||||||
void sys_mmap(Context* context, void* address, size_t size, int flags)
|
static const char* format_prot(int prot)
|
||||||
|
{
|
||||||
|
static char prot_string[3];
|
||||||
|
prot_string[2] = 0;
|
||||||
|
prot_string[0] = ((prot & MAP_READ) > 0) ? 'r' : '-';
|
||||||
|
prot_string[1] = ((prot & MAP_WRITE) > 0) ? 'w' : '-';
|
||||||
|
return prot_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mman_flags_from_prot(int prot)
|
||||||
|
{
|
||||||
|
prot &= 0b11;
|
||||||
|
if (prot == MAP_NONE) return 0;
|
||||||
|
if ((prot & MAP_WRITE) > 0) return MAP_USER | MAP_READ_WRITE;
|
||||||
|
return MAP_USER;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_mmap(Context* context, void* address, size_t size, int prot)
|
||||||
{
|
{
|
||||||
if (size < PAGE_SIZE)
|
if (size < PAGE_SIZE)
|
||||||
{
|
{
|
||||||
@ -24,12 +45,10 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
|
|||||||
context->rax = MAP_FAIL(EINVAL);
|
context->rax = MAP_FAIL(EINVAL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int real_flags = MAP_USER;
|
int real_flags = mman_flags_from_prot(prot);
|
||||||
if (flags & MAP_READ_WRITE) real_flags |= MAP_READ_WRITE;
|
|
||||||
if (address)
|
if (address)
|
||||||
{
|
{
|
||||||
kdbgln("mmap(): %ld pages at address %p, %s", size / PAGE_SIZE, address,
|
kdbgln("mmap(): %ld pages at address %p, %s", size / PAGE_SIZE, address, format_prot(prot));
|
||||||
real_flags & MAP_READ_WRITE ? "rw" : "ro");
|
|
||||||
if (VMM::get_physical((uint64_t)address) != (uint64_t)-1) // Address is already used.
|
if (VMM::get_physical((uint64_t)address) != (uint64_t)-1) // Address is already used.
|
||||||
{
|
{
|
||||||
kwarnln("attempt to map an already mapped address");
|
kwarnln("attempt to map an already mapped address");
|
||||||
@ -52,8 +71,7 @@ void sys_mmap(Context* context, void* address, size_t size, int flags)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
kdbgln("mmap(): %ld pages at any address, %s", Utilities::get_blocks_from_size(PAGE_SIZE, size),
|
kdbgln("mmap(): %ld pages at any address, %s", Utilities::get_blocks_from_size(PAGE_SIZE, size), format_prot(prot));
|
||||||
real_flags & MAP_READ_WRITE ? "rw" : "ro");
|
|
||||||
void* result = MemoryManager::get_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
|
void* result = MemoryManager::get_pages(Utilities::get_blocks_from_size(PAGE_SIZE, size), real_flags);
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
@ -102,4 +120,43 @@ void sys_munmap(Context* context, void* address, size_t size)
|
|||||||
kdbgln("munmap() succeeded");
|
kdbgln("munmap() succeeded");
|
||||||
context->rax = 0;
|
context->rax = 0;
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sys_mprotect(Context* context, void* address, size_t size, int prot)
|
||||||
|
{
|
||||||
|
kdbgln("mprotect(): attempting to protect %p with %s", address, format_prot(prot));
|
||||||
|
|
||||||
|
if (size < PAGE_SIZE)
|
||||||
|
{
|
||||||
|
kwarnln("mprotect() failed: size is too small");
|
||||||
|
context->rax = -EINVAL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (size % PAGE_SIZE)
|
||||||
|
{
|
||||||
|
kwarnln("mprotect() failed: size is not a multiple of PAGE_SIZE");
|
||||||
|
context->rax = -EINVAL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!address)
|
||||||
|
{
|
||||||
|
kwarnln("mprotect() failed: attempted to unmap page 0");
|
||||||
|
context->rax = -EINVAL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t flags = VMM::get_flags((uint64_t)address);
|
||||||
|
if (!(flags & MAP_USER))
|
||||||
|
{
|
||||||
|
kwarnln("mprotect() failed: attempted to protect a non-existent or kernel page");
|
||||||
|
context->rax = -EINVAL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t offset = (uint64_t)address % PAGE_SIZE;
|
||||||
|
MemoryManager::protect((void*)((uint64_t)address - offset), Utilities::get_blocks_from_size(PAGE_SIZE, size),
|
||||||
|
mman_flags_from_prot(prot));
|
||||||
|
kdbgln("mprotect() succeeded");
|
||||||
|
context->rax = 0;
|
||||||
|
return;
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
#define MODULE "sched"
|
#define MODULE "sched"
|
||||||
|
|
||||||
#include "thread/Scheduler.h"
|
#include "thread/Scheduler.h"
|
||||||
#include "assert.h"
|
|
||||||
#include "interrupts/Interrupts.h"
|
#include "interrupts/Interrupts.h"
|
||||||
|
#include "kassert.h"
|
||||||
#include "log/Log.h"
|
#include "log/Log.h"
|
||||||
#include "memory/MemoryManager.h"
|
#include "memory/MemoryManager.h"
|
||||||
#include "memory/VMM.h"
|
#include "memory/VMM.h"
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
/* Address returned by mmap when it fails. */
|
/* Address returned by mmap when it fails. */
|
||||||
#define MAP_FAILED (void*)-1
|
#define MAP_FAILED (void*)-1
|
||||||
|
|
||||||
#define PROT_READ_WRITE 1
|
#define PROT_NONE 0
|
||||||
|
#define PROT_READ 1
|
||||||
|
#define PROT_WRITE 2
|
||||||
|
|
||||||
#define PAGE_SIZE 4096
|
#define PAGE_SIZE 4096
|
||||||
|
|
||||||
@ -24,6 +26,9 @@ extern "C"
|
|||||||
* address space. */
|
* address space. */
|
||||||
int munmap(void* addr, size_t size);
|
int munmap(void* addr, size_t size);
|
||||||
|
|
||||||
|
/* Protects size bytes of memory according to the prot argument. */
|
||||||
|
int mprotect(void* addr, size_t size, int prot);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -13,7 +13,7 @@ int liballoc_unlock()
|
|||||||
|
|
||||||
void* liballoc_alloc(size_t size)
|
void* liballoc_alloc(size_t size)
|
||||||
{
|
{
|
||||||
void* result = mmap(NULL, size * PAGE_SIZE, PROT_READ_WRITE, 0, 0, 0);
|
void* result = mmap(NULL, size * PAGE_SIZE, PROT_READ | PROT_WRITE, 0, 0, 0);
|
||||||
if (result == MAP_FAILED) return 0;
|
if (result == MAP_FAILED) return 0;
|
||||||
return (void*)result;
|
return (void*)result;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user