X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/b0c2a4be2d1f4f93ab01d0858979a12ef0735ec1..509574b18a3373030cd0d7b979499499ff06dd9b:/lunaix-os/kernel/mm/dmm.c diff --git a/lunaix-os/kernel/mm/dmm.c b/lunaix-os/kernel/mm/dmm.c index ae89908..d582625 100644 --- a/lunaix-os/kernel/mm/dmm.c +++ b/lunaix-os/kernel/mm/dmm.c @@ -17,8 +17,27 @@ #include #include #include +#include #include +#include + + +__DEFINE_LXSYSCALL1(int, sbrk, size_t, size) { + heap_context_t* uheap = &__current->mm.u_heap; + mutex_lock(&uheap->lock); + void* r = lxsbrk(uheap, size); + mutex_unlock(&uheap->lock); + return r; +} + +__DEFINE_LXSYSCALL1(void*, brk, void*, addr) { + heap_context_t* uheap = &__current->mm.u_heap; + mutex_lock(&uheap->lock); + int r = lxbrk(uheap, addr); + mutex_unlock(&uheap->lock); + return r; +} int dmm_init(heap_context_t* heap) @@ -26,18 +45,19 @@ dmm_init(heap_context_t* heap) assert((uintptr_t)heap->start % BOUNDARY == 0); heap->brk = heap->start; + mutex_init(&heap->lock); - return vmm_alloc_page(heap->brk, PG_PREM_RW) != NULL; + return vmm_alloc_page(__current->pid, heap->brk, NULL, PG_PREM_RW, 0) != NULL; } int -lxsbrk(heap_context_t* heap, void* addr) +lxbrk(heap_context_t* heap, void* addr) { - return lxbrk(heap, addr - heap->brk) != NULL; + return -(lxsbrk(heap, addr - heap->brk) == (void*)-1); } void* -lxbrk(heap_context_t* heap, size_t size) +lxsbrk(heap_context_t* heap, size_t size) { if (size == 0) { return heap->brk; @@ -51,17 +71,17 @@ lxbrk(heap_context_t* heap, size_t size) // any invalid situations if (next >= heap->max_addr || next < current_brk) { - return NULL; + __current->k_status = LXINVLDPTR; + return (void*)-1; } uintptr_t diff = PG_ALIGN(next) - PG_ALIGN(current_brk); if (diff) { // if next do require new pages to be allocated - if (!vmm_alloc_pages((void*)(PG_ALIGN(current_brk) + PG_SIZE), + if (!vmm_alloc_pages(__current->pid, (void*)(PG_ALIGN(current_brk) + PG_SIZE), diff, - PG_PREM_RW)) { - // for debugging - assert_msg(0, "unable to brk"); + PG_PREM_RW, 0)) { + __current->k_status = LXHEAPFULL; return NULL; } }