1 // Copyright 2018 The Fuchsia Authors 2 // 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file or at 5 // https://opensource.org/licenses/MIT 6 7 #pragma once 8 9 #include <vm/physmap.h> 10 #include <vm/pmm.h> 11 12 namespace hypervisor { 13 14 class Page { 15 public: 16 Page() = default; 17 DISALLOW_COPY_ASSIGN_AND_MOVE(Page); 18 ~Page()19 ~Page() { 20 if (page_ != nullptr) { 21 pmm_free_page(page_); 22 } 23 } 24 Alloc(uint8_t fill)25 zx_status_t Alloc(uint8_t fill) { 26 zx_status_t status = pmm_alloc_page(0, &page_, &pa_); 27 if (status != ZX_OK) { 28 return status; 29 } 30 31 page_->state = VM_PAGE_STATE_WIRED; 32 33 memset(VirtualAddress(), fill, PAGE_SIZE); 34 return ZX_OK; 35 } 36 VirtualAddress()37 void* VirtualAddress() const { 38 DEBUG_ASSERT(pa_ != 0); 39 return paddr_to_physmap(pa_); 40 } 41 42 template <typename T> VirtualAddress()43 T* VirtualAddress() const { 44 return static_cast<T*>(VirtualAddress()); 45 } 46 PhysicalAddress()47 paddr_t PhysicalAddress() const { 48 DEBUG_ASSERT(pa_ != 0); 49 return pa_; 50 } 51 IsAllocated()52 bool IsAllocated() const { return pa_ != 0; } 53 54 private: 55 vm_page* page_ = nullptr; 56 zx_paddr_t pa_ = 0; 57 }; 58 59 } // namespace hypervisor 60