Fix pin_memory.
authorcoderain <coderain@sdf.org>
Tue, 25 Sep 2018 13:50:31 +0000 (15:50 +0200)
committercoderain <coderain@sdf.org>
Tue, 25 Sep 2018 13:50:31 +0000 (15:50 +0200)
kernel/src/memory/memory.c

index 4d0798ac23b1819412aaa9dbdb58ec7be38f007b..1045b5770c0a84a22ae913be1495446d4c8b07e3 100644 (file)
@@ -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);
     }