static bool setup_pml4(uintptr_t *pml4) { PPHYSICAL_MEMORY_RANGE pm_ranges = MmGetPhysicalMemoryRanges(); bool ret = false; for (int run = 0;; ++run) { uintptr_t base_addr = pm_ranges[run].BaseAddress.QuadPart; uintptr_t bytes = pm_ranges[run].NumberOfBytes.QuadPart; if (!base_addr || !bytes) break; uintptr_t nr_pages = BYTES_TO_PAGES(bytes); for (uintptr_t page = 0; page < nr_pages; ++page) { uintptr_t page_addr = base_addr + page * PAGE_SIZE; uintptr_t *entry = ept_alloc_page(NULL, pml4, EPT_ACCESS_ALL, page_addr); if (!entry) goto out; } } /* Allocate APIC page */ ret = !!ept_alloc_page(NULL, pml4, EPT_ACCESS_ALL, __readmsr(MSR_IA32_APICBASE) & MSR_IA32_APICBASE_BASE); out: ExFreePool(pm_ranges); return ret; }
static FORCEINLINE __SystemGetMemoryInformation( VOID ) { PHYSICAL_MEMORY_RANGE *Range; ULONG Index; Range = MmGetPhysicalMemoryRanges(); for (Index = 0; Range[Index].BaseAddress.QuadPart != 0 || Range[Index].NumberOfBytes.QuadPart != 0; Index++) { PHYSICAL_ADDRESS Start; PHYSICAL_ADDRESS End; Start.QuadPart = Range[Index].BaseAddress.QuadPart; End.QuadPart = Start.QuadPart + Range[Index].NumberOfBytes.QuadPart - 1; Info("RANGE[%u] %08x.%08x - %08x.%08x\n", Index, Start.HighPart, Start.LowPart, End.HighPart, End.LowPart); } }
static inline bool is_dma(u64 addr) { PPHYSICAL_MEMORY_RANGE ranges = MmGetPhysicalMemoryRanges(); bool ret = false; for (int run = 0;; ++run) { u64 base = ranges[run].BaseAddress.QuadPart; u64 size = ranges[run].NumberOfBytes.QuadPart; ret = !(addr >= base && addr < base + size); if (!ret || !base || !size) break; } ExFreePool(ranges); return ret; }
static ULONG BalloonGetTopPage( VOID ) { PHYSICAL_MEMORY_RANGE *Range; PHYSICAL_ADDRESS TopAddress; ULONG Index; Range = MmGetPhysicalMemoryRanges(); TopAddress.QuadPart = 0ull; for (Index = 0; Range[Index].BaseAddress.QuadPart != 0 || Range[Index].NumberOfBytes.QuadPart != 0; Index++) { PHYSICAL_ADDRESS EndAddress; CHAR Key[32]; EndAddress.QuadPart = Range[Index].BaseAddress.QuadPart + Range[Index].NumberOfBytes.QuadPart; TraceInfo(("PHYSICAL MEMORY: RANGE[%u] %08x.%08x - %08x.%08x\n", Index, Range[Index].BaseAddress.HighPart, Range[Index].BaseAddress.LowPart, EndAddress.HighPart, EndAddress.LowPart)); (VOID) Xmsnprintf(Key, sizeof (Key), "data/physical-memory/range%u", Index); (VOID) xenbus_printf(XBT_NIL, Key, "base", "%08x.%08x", Range[Index].BaseAddress.HighPart, Range[Index].BaseAddress.LowPart); (VOID) xenbus_printf(XBT_NIL, Key, "end", "%08x.%08x", EndAddress.HighPart, EndAddress.LowPart); if (EndAddress.QuadPart > TopAddress.QuadPart) TopAddress.QuadPart = EndAddress.QuadPart; } TraceNotice(("PHYSICAL MEMORY: TOP = %08x.%08x\n", TopAddress.HighPart, TopAddress.LowPart)); return (ULONG)(TopAddress.QuadPart >> PAGE_SHIFT); }