1 // Copyright 2018 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #pragma once 6 7 #include <fbl/macros.h> 8 #include <fbl/ref_counted.h> 9 #include <fbl/ref_ptr.h> 10 #include <lib/zx/vmar.h> 11 12 namespace fzl { 13 14 // VmarManager 15 // 16 // A small utility class which manages the lifecycle of a VMAR intended to be 17 // shared among a collection of users. VmarManager will handle simple tasks such as 18 // automatically destroying the VMAR at end-of-life in addition to releasing the 19 // handle. 20 // 21 // Currently, the primary use case for a VmarManager is to be used to create a 22 // COMPACT sub-vmar in order to hold a number of VMO mappings while minimizing 23 // page table fragmentation.. 24 // 25 // See fzl::VmoMapper. 26 class VmarManager : public fbl::RefCounted<VmarManager> { 27 public: 28 // Create a new VmarManager (creating the underlying VMAR object in the 29 // process) 30 // 31 // size : the size of the VMAR region to create. 32 // parent : the parent of this VMAR, or nullptr to use the root VMAR. 33 // flags : creation flags to pass to vmar_allocate 34 static fbl::RefPtr<VmarManager> Create(size_t size, 35 fbl::RefPtr<VmarManager> parent = nullptr, 36 zx_vm_option_t options = ZX_VM_COMPACT | 37 ZX_VM_CAN_MAP_READ | 38 ZX_VM_CAN_MAP_WRITE); 39 vmar()40 const zx::vmar& vmar() const { return vmar_; } start()41 void* start() const { return start_; } size()42 uint64_t size() const { return size_; } parent()43 const fbl::RefPtr<VmarManager>& parent() const { return parent_; } 44 45 private: 46 friend class fbl::RefPtr<VmarManager>; 47 48 VmarManager() = default; ~VmarManager()49 ~VmarManager() { 50 if (vmar_.is_valid()) { 51 vmar_.destroy(); 52 } 53 } 54 55 // suppress default constructors 56 DISALLOW_COPY_ASSIGN_AND_MOVE(VmarManager); 57 58 zx::vmar vmar_; 59 void* start_ = nullptr; 60 uint64_t size_ = 0; 61 fbl::RefPtr<VmarManager> parent_; 62 }; 63 64 } // namespace fzl 65