Merge branch 'master' into mm_rewrite
[monolithium.git] / kernel / src / memory / old_memory.c
index 6a6f0ab66d41a734b42d8da23b7ddcd88db84e22..1045b5770c0a84a22ae913be1495446d4c8b07e3 100644 (file)
@@ -292,7 +292,7 @@ static inline dword_t free_page(void *virtual)
     unmap_page(virtual);
 
     page_t *page = get_page(phys);
-    if (page == NULL || page->ref_count == 0) free_physical_page(phys);
+    if (page != NULL && page->ref_count > 0) free_physical_page(phys);
 
     return ERR_SUCCESS;
 }
@@ -956,7 +956,8 @@ dword_t pin_memory(const void *virtual, void **pinned, uintptr_t size, bool_t lo
     uintptr_t i;
     uintptr_t virt_addr = PAGE_ALIGN((uintptr_t)virtual);
     void *address = (void*)PAGE_ALIGN((uintptr_t)*pinned);
-    size = PAGE_ALIGN_UP(size) >> 12;
+    size = 1 + ((((uintptr_t)virtual + size - 1) - virt_addr) >> 12);
+    if (commit_pages((void*)virt_addr, size * PAGE_SIZE) != ERR_SUCCESS) return ERR_BADPTR;
 
     memory_address_space_t *address_space = check_usermode(virtual, 1) ? &get_current_process()->memory_space : &kernel_address_space;
     lock_acquire_shared(&address_space->lock);
@@ -978,6 +979,7 @@ dword_t pin_memory(const void *virtual, void **pinned, uintptr_t size, bool_t lo
     {
         void *virt_page = (void*)(virt_addr + i * PAGE_SIZE);
         void *phys_page = get_physical_address(virt_page);
+        ASSERT(phys_page != INVALID_PAGE);
 
         if (lock_contents)
         {
@@ -987,7 +989,7 @@ dword_t pin_memory(const void *virtual, void **pinned, uintptr_t size, bool_t lo
             set_page_flags(virt_page, get_page_flags(virt_page) & ~PAGE_WRITABLE);
         }
 
-        dword_t ret = map_page(phys_page, (void*)(real_address + i), new_flags);
+        dword_t ret = map_page(phys_page, (void*)(real_address + i * PAGE_SIZE), new_flags);
         ASSERT(ret == ERR_SUCCESS);
         reference_page(phys_page);
     }