Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
brk.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"
6#include "spinlock.h"
7#include <sys/syscall.h>
8intptr_t syscall_brk(void* addr)
9{
10 auto curr_thr = get_current_core_data()->active_thread;
11 auto proc = curr_thr->process;
12
13 uintptr_t old_brk = proc->heap_end;
14
15 if (!addr)
16 return (intptr_t)old_brk;
17
18 uintptr_t new_brk = (uintptr_t)addr;
19
20 if (new_brk < proc->heap_start)
21 return (intptr_t)old_brk;
22
23 spin_acquire(&proc->vm_lock);
24
25 if (new_brk > old_brk) {
26
27 uintptr_t old_page = ALIGN_UP(old_brk, PAGE_SIZE);
28 uintptr_t new_page = ALIGN_UP(new_brk, PAGE_SIZE);
29
30 serial2_printf("brk: from %x to %x\n", old_page, new_page);
31
32 for (uintptr_t v = old_page; v < new_page; v += PAGE_SIZE) {
33
34 void* phys = phys_base_alloc(1);
35
36 if (!phys) {
37 spin_release(&proc->vm_lock);
38 return (intptr_t)old_brk;
39 }
40
41 vxMmap(
42 curr_thr->page,
43 v,
44 (uintptr_t)phys,
48 );
49 }
50 }
51
52 proc->heap_end = new_brk;
53
54 spin_release(&proc->vm_lock);
55
56 return (intptr_t)new_brk;
57}
intptr_t syscall_brk(void *addr)
Definition brk.c:8
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_UP(x, align)
Definition memory_utils.h:6
void vxMmap(page_t page_dir, uint64_t virt, uint64_t phys, uint64_t flags)
Definition paging.c:223
#define PAGE_PRESENT
Definition paging.h:34
#define PAGE_USER
Definition paging.h:36
#define PAGE_SIZE
Definition paging.h:6
#define PAGE_WRITABLE
Definition paging.h:35
void * phys_base_alloc(uint64_t block)
uintptr_t heap_start
Definition process.h:12
void spin_acquire(spinlock_t *lock)
Definition spinlock.c:8
void spin_release(spinlock_t *lock)
Definition spinlock.c:19
unsigned long uintptr_t
Definition type.h:73