Changeset 19761 in vbox for trunk/src/VBox/Runtime/r0drv/freebsd/alloc-r0drv-freebsd.c
- Timestamp:
- May 17, 2009 7:30:52 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r0drv/freebsd/alloc-r0drv-freebsd.c
r18972 r19761 52 52 PRTMEMHDR rtMemAlloc(size_t cb, uint32_t fFlags) 53 53 { 54 PRTMEMHDR pHdr; 54 size_t cbAllocated = cb; 55 PRTMEMHDR pHdr = NULL; 55 56 56 /** @todo Just like OS/2, FreeBSD doesn't need this header. */ 57 pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP, 58 fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT); 57 /* 58 * Things are a bit more complicated on AMD64 for executable memory 59 * because we need to be in the ~2GB..~0 range for code. 60 */ 61 #ifdef RT_ARCH_AMD64 62 if (fFlags & RTMEMHDR_FLAG_EXEC) 63 { 64 vm_offset_t Addr = KERNBASE; 65 cbAllocated = RT_ALIGN_Z(cb + sizeof(*pHdr), PAGE_SIZE); 66 67 /* Addr contains a start address vm_map_find will start searching for suitable space at. */ 68 int rc = vm_map_find(kernel_map, NULL, 0, &Addr, 69 cbAllocated, 70 TRUE, VM_PROT_ALL, VM_PROT_ALL, 0); 71 if (rc == KERN_SUCCESS) 72 { 73 /* Add the pages. */ 74 vm_offset_t AddressDst = Addr; 75 bool fSuccess = true; 76 77 do 78 { 79 vm_pindex_t PageIndex = OFF_TO_IDX(AddressDst); 80 vm_page_t pPage; 81 82 pPage = vm_page_alloc(NULL, PageIndex, 83 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | 84 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ); 85 if (pPage) 86 { 87 vm_page_lock_queues(); 88 vm_page_wire(pPage); 89 vm_page_unlock_queues(); 90 /* Put the page into the page table now. */ 91 #if __FreeBSD_version >= 701105 92 pmap_enter(kernel_map->pmap, AddressDst, VM_PROT_NONE, pPage, 93 VM_PROT_ALL, TRUE); 94 #else 95 pmap_enter(kernel_map->pmap, AddressDst, pPage, 96 VM_PROT_ALL, TRUE); 97 #endif 98 } 99 else 100 { 101 /* 102 * Allocation failed. vm_map_remove will remove any 103 * page already allocated. 104 */ 105 fSuccess = false; 106 break; 107 } 108 AddressDst += PAGE_SIZE; 109 } while(AddressDst < (Addr + cbAllocated)); 110 111 if (fSuccess) 112 { 113 pHdr = (PRTMEMHDR)Addr; 114 115 if (fFlags & RTMEMHDR_FLAG_ZEROED) 116 bzero(pHdr, cbAllocated); 117 } 118 else 119 vm_map_remove(kernel_map, 120 Addr, 121 Addr + cb); 122 } 123 } 124 else 125 #endif 126 { 127 pHdr = (PRTMEMHDR)malloc(cb + sizeof(RTMEMHDR), M_IPRTHEAP, 128 fFlags & RTMEMHDR_FLAG_ZEROED ? M_NOWAIT | M_ZERO : M_NOWAIT); 129 } 130 59 131 if (pHdr) 60 132 { 61 133 pHdr->u32Magic = RTMEMHDR_MAGIC; 62 134 pHdr->fFlags = fFlags; 63 pHdr->cb = cb ;135 pHdr->cb = cbAllocated; 64 136 pHdr->cbReq = cb; 65 137 return pHdr; … … 72 144 { 73 145 pHdr->u32Magic += 1; 74 free(pHdr, M_IPRTHEAP); 146 147 #ifdef RT_ARCH_AMD64 148 if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC) 149 vm_map_remove(kernel_map, (vm_offset_t)pHdr, ((vm_offset_t)pHdr) + pHdr->cb); 150 else 151 #endif 152 free(pHdr, M_IPRTHEAP); 75 153 } 76 154
Note:
See TracChangeset
for help on using the changeset viewer.