blob: ae168a6c9fa4e4053549ee0f47d32f0352cbcaed [file] [log] [blame]
Doug Horn1427b6a2018-12-11 13:19:16 -08001// 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_VMO_H_
6#define LIB_ZX_VMO_H_
7
8#include <lib/zx/handle.h>
9#include <lib/zx/object.h>
10#include <lib/zx/resource.h>
11
12namespace zx {
13
14class bti;
15
Adam Barth57eacf52020-11-04 00:38:09 +000016class vmo final : public object<vmo> {
17 public:
18 static constexpr zx_obj_type_t TYPE = ZX_OBJ_TYPE_VMO;
Doug Horn1427b6a2018-12-11 13:19:16 -080019
Adam Barth57eacf52020-11-04 00:38:09 +000020 constexpr vmo() = default;
Doug Horn1427b6a2018-12-11 13:19:16 -080021
Adam Barth57eacf52020-11-04 00:38:09 +000022 explicit vmo(zx_handle_t value) : object(value) {}
Doug Horn1427b6a2018-12-11 13:19:16 -080023
Adam Barth57eacf52020-11-04 00:38:09 +000024 explicit vmo(handle&& h) : object(h.release()) {}
Doug Horn1427b6a2018-12-11 13:19:16 -080025
Adam Barth57eacf52020-11-04 00:38:09 +000026 vmo(vmo&& other) : object(other.release()) {}
Doug Horn1427b6a2018-12-11 13:19:16 -080027
Adam Barth57eacf52020-11-04 00:38:09 +000028 vmo& operator=(vmo&& other) {
29 reset(other.release());
30 return *this;
31 }
Doug Horn1427b6a2018-12-11 13:19:16 -080032
Adam Barth57eacf52020-11-04 00:38:09 +000033 static zx_status_t create(uint64_t size, uint32_t options, vmo* result);
34 static zx_status_t create_contiguous(const bti& bti, size_t size, uint32_t alignment_log2,
35 vmo* result);
36 static zx_status_t create_physical(const resource& resource, zx_paddr_t paddr, size_t size,
37 vmo* result);
Doug Horn1427b6a2018-12-11 13:19:16 -080038
Adam Barth57eacf52020-11-04 00:38:09 +000039 zx_status_t read(void* data, uint64_t offset, size_t len) const {
40 return zx_vmo_read(get(), data, offset, len);
41 }
Doug Horn1427b6a2018-12-11 13:19:16 -080042
Adam Barth57eacf52020-11-04 00:38:09 +000043 zx_status_t write(const void* data, uint64_t offset, size_t len) const {
44 return zx_vmo_write(get(), data, offset, len);
45 }
Doug Horn1427b6a2018-12-11 13:19:16 -080046
Adam Barth57eacf52020-11-04 00:38:09 +000047 zx_status_t get_size(uint64_t* size) const { return zx_vmo_get_size(get(), size); }
Doug Horn1427b6a2018-12-11 13:19:16 -080048
Adam Barth57eacf52020-11-04 00:38:09 +000049 zx_status_t set_size(uint64_t size) const { return zx_vmo_set_size(get(), size); }
Doug Horn1427b6a2018-12-11 13:19:16 -080050
Adam Barth57eacf52020-11-04 00:38:09 +000051 zx_status_t create_child(uint32_t options, uint64_t offset, uint64_t size, vmo* result) const {
52 // Allow for the caller aliasing |result| to |this|.
53 vmo h;
54 zx_status_t status =
55 zx_vmo_create_child(get(), options, offset, size, h.reset_and_get_address());
56 result->reset(h.release());
57 return status;
58 }
Doug Horn1427b6a2018-12-11 13:19:16 -080059
Adam Barth57eacf52020-11-04 00:38:09 +000060 zx_status_t op_range(uint32_t op, uint64_t offset, uint64_t size, void* buffer,
61 size_t buffer_size) const {
62 return zx_vmo_op_range(get(), op, offset, size, buffer, buffer_size);
63 }
Doug Horn1427b6a2018-12-11 13:19:16 -080064
Adam Barth57eacf52020-11-04 00:38:09 +000065 zx_status_t set_cache_policy(uint32_t cache_policy) const {
66 return zx_vmo_set_cache_policy(get(), cache_policy);
67 }
Doug Horn1427b6a2018-12-11 13:19:16 -080068
Adam Barth57eacf52020-11-04 00:38:09 +000069 zx_status_t replace_as_executable(const resource& vmex, vmo* result) {
70 zx_handle_t h = ZX_HANDLE_INVALID;
71 zx_status_t status = zx_vmo_replace_as_executable(value_, vmex.get(), &h);
72 // We store ZX_HANDLE_INVALID to value_ before calling reset on result
73 // in case result == this.
74 value_ = ZX_HANDLE_INVALID;
75 result->reset(h);
76 return status;
77 }
Doug Horn1427b6a2018-12-11 13:19:16 -080078};
79
80using unowned_vmo = unowned<vmo>;
81
Adam Barth57eacf52020-11-04 00:38:09 +000082} // namespace zx
Doug Horn1427b6a2018-12-11 13:19:16 -080083
84#endif // LIB_ZX_VMO_H_