Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.

Commit 61d0ae8

Browse files
committed
Add mmap() and munmap() in process
1 parent c2bdfcf commit 61d0ae8

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

kernel/include/memory/user_address_space.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class UserAddressSpace {
5858
~UserAddressSpace();
5959

6060
void* allocate(size_t size, uint8_t flags = Read | Write, PageSize type = PageSize::Size4K);
61-
bool allocate_specific(uintptr_t virt_addr, size_t size, uint8_t flags, PageSize type = PageSize::Size4K);
61+
bool allocate_specific(uintptr_t virt_addr, size_t size, uint8_t flags,
62+
PageSize type = PageSize::Size4K);
6263
void free(void* ptr);
6364
bool handle_page_fault(uintptr_t fault_addr, size_t error_code);
6465

kernel/include/task/process.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
#include "libs/intrusive_list.hpp"
77
#include "memory/user_address_space.hpp"
88

9+
#define PROT_READ 0x01
10+
#define PROT_WRITE 0x02
11+
#define PROT_EXEC 0x04
12+
#define PROT_NONE 0x08
13+
14+
#define MAP_HUGE_2MB 0x01
15+
#define MAP_HUGE_1GB 0x02
16+
917
namespace kernel::cpu {
1018
struct PerCpuData;
1119
}
@@ -78,6 +86,9 @@ struct Process : public IntrusiveListNode<ProcessTag> {
7886
Process(); // User
7987
~Process();
8088

89+
void* mmap(void* addr, size_t len, int prot, int flags);
90+
void munmap(void* ptr, size_t len);
91+
8192
static void init();
8293

8394
private:

kernel/src/task/process.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#include "task/process.hpp"
12
#include "hal/smp_manager.hpp"
23
#include "boot/boot.h"
4+
#include "memory/memory.hpp"
35
#include "memory/vma.hpp"
6+
#include "libs/math.hpp"
47

58
namespace kernel::task {
69
Process* Process::kernel_proc = nullptr;
@@ -85,6 +88,57 @@ Process::~Process() {
8588
}
8689
}
8790

91+
void* Process::mmap(void* addr, size_t len, int prot, int flags) {
92+
uint8_t flag = 0;
93+
94+
if (prot & PROT_READ) {
95+
flag |= memory::Read;
96+
}
97+
98+
if (prot & PROT_WRITE) {
99+
flag |= memory::Write;
100+
}
101+
102+
if (prot & PROT_EXEC) {
103+
flag |= memory::Execute;
104+
}
105+
106+
if (prot & PROT_NONE) {
107+
// Do nothing
108+
}
109+
110+
memory::PageSize type = memory::PageSize::Size4K;
111+
uintptr_t page_size = memory::PAGE_SIZE_4K;
112+
113+
if (flags & MAP_HUGE_2MB) {
114+
type = memory::PageSize::Size2M;
115+
page_size = memory::PAGE_SIZE_2M;
116+
}
117+
118+
if (flags & MAP_HUGE_1GB) {
119+
type = memory::PageSize::Size1G;
120+
page_size = memory::PAGE_SIZE_1G;
121+
}
122+
123+
size_t aligned_size = align_up(len, page_size);
124+
void* ret = nullptr;
125+
126+
if (addr) {
127+
if (this->vma.allocate_specific(reinterpret_cast<uintptr_t>(addr), aligned_size, flag,
128+
type)) {
129+
ret = addr;
130+
}
131+
} else {
132+
ret = this->vma.allocate(aligned_size, flag, type);
133+
}
134+
135+
return ret;
136+
}
137+
138+
void Process::munmap(void* ptr, size_t) {
139+
return this->vma.free(ptr);
140+
}
141+
88142
void Process::init() {
89143
kernel_proc = new Process(memory::PageMap::get_kernel_map());
90144
}

0 commit comments

Comments
 (0)