1 // Copyright 2016 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 #ifndef LIB_ZX_VMAR_H_
6 #define LIB_ZX_VMAR_H_
7 
8 #include <lib/zx/object.h>
9 #include <lib/zx/vmo.h>
10 #include <zircon/process.h>
11 
12 namespace zx {
13 
14 // A wrapper for handles to VMARs.  Note that vmar::~vmar() does not execute
15 // vmar::destroy(), it just closes the handle.
16 class vmar : public object<vmar> {
17 public:
18     static constexpr zx_obj_type_t TYPE = ZX_OBJ_TYPE_VMAR;
19 
20     constexpr vmar() = default;
21 
vmar(zx_handle_t value)22     explicit vmar(zx_handle_t value) : object(value) {}
23 
vmar(handle && h)24     explicit vmar(handle&& h) : object(h.release()) {}
25 
vmar(vmar && other)26     vmar(vmar&& other) : vmar(other.release()) {}
27 
28     vmar& operator=(vmar&& other) {
29         reset(other.release());
30         return *this;
31     }
32 
map(size_t vmar_offset,const vmo & vmo_handle,uint64_t vmo_offset,size_t len,zx_vm_option_t options,uintptr_t * ptr)33     zx_status_t map(size_t vmar_offset, const vmo& vmo_handle, uint64_t vmo_offset,
34                     size_t len, zx_vm_option_t options, uintptr_t* ptr) const {
35         return zx_vmar_map(get(), options, vmar_offset, vmo_handle.get(), vmo_offset, len, ptr);
36     }
37 
unmap(uintptr_t address,size_t len)38     zx_status_t unmap(uintptr_t address, size_t len) const {
39         return zx_vmar_unmap(get(), address, len);
40     }
41 
protect(uintptr_t address,size_t len,zx_vm_option_t prot)42     zx_status_t protect(uintptr_t address, size_t len, zx_vm_option_t prot) const {
43         return zx_vmar_protect(get(), prot, address, len);
44     }
45 
destroy()46     zx_status_t destroy() const {
47         return zx_vmar_destroy(get());
48     }
49 
50     zx_status_t allocate(size_t offset, size_t size, uint32_t flags,
51                          vmar* child, uintptr_t* child_addr) const;
52 
root_self()53     static inline unowned<vmar> root_self() {
54         return unowned<vmar>(zx_vmar_root_self());
55     }
56 };
57 
58 using unowned_vmar = unowned<vmar>;
59 
60 } // namespace zx
61 
62 #endif  // LIB_ZX_VMAR_H_
63