* <!-- description --> * @brief This function allocates read/write virtual memory from the * kernel. This memory is not physically contiguous. The resulting * pointer is at least 4k aligned, so use this function sparingly * as it will always allocate at least one page. Use platform_free() * to release this memory. * * @note This function must zero the allocated memory *
| 93 | * Returns a nullptr on failure. |
| 94 | */ |
| 95 | NODISCARD void * |
| 96 | platform_alloc(uint64_t size) |
| 97 | { |
| 98 | EFI_STATUS status = EFI_SUCCESS; |
| 99 | EFI_PHYSICAL_ADDRESS ret = ((EFI_PHYSICAL_ADDRESS)0); |
| 100 | |
| 101 | platform_expects(((uint64_t)0) != size); |
| 102 | |
| 103 | if (((uint64_t)0) != (size & (HYPERVISOR_PAGE_SIZE - ((uint64_t)1)))) { |
| 104 | size += HYPERVISOR_PAGE_SIZE; |
| 105 | size &= ~(HYPERVISOR_PAGE_SIZE - ((uint64_t)1)); |
| 106 | } |
| 107 | |
| 108 | status = g_st->BootServices->AllocatePages( |
| 109 | AllocateAnyPages, EfiRuntimeServicesData, size / HYPERVISOR_PAGE_SIZE, &ret); |
| 110 | if (EFI_ERROR(status)) { |
| 111 | bferror_x64("AllocatePages failed", status); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | g_st->BootServices->SetMem((void *)ret, size, ((UINT8)0)); |
| 116 | return (void *)ret; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * <!-- description --> |
no test coverage detected