Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 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_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 | |
| 12 | namespace zx { |
| 13 | |
| 14 | class bti; |
| 15 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 16 | class vmo final : public object<vmo> { |
| 17 | public: |
| 18 | static constexpr zx_obj_type_t TYPE = ZX_OBJ_TYPE_VMO; |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 19 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 20 | constexpr vmo() = default; |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 21 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 22 | explicit vmo(zx_handle_t value) : object(value) {} |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 23 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 24 | explicit vmo(handle&& h) : object(h.release()) {} |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 25 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 26 | vmo(vmo&& other) : object(other.release()) {} |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 27 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 28 | vmo& operator=(vmo&& other) { |
| 29 | reset(other.release()); |
| 30 | return *this; |
| 31 | } |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 32 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 33 | 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 Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 38 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 39 | zx_status_t read(void* data, uint64_t offset, size_t len) const { |
| 40 | return zx_vmo_read(get(), data, offset, len); |
| 41 | } |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 42 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 43 | 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 Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 46 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 47 | zx_status_t get_size(uint64_t* size) const { return zx_vmo_get_size(get(), size); } |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 48 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 49 | zx_status_t set_size(uint64_t size) const { return zx_vmo_set_size(get(), size); } |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 50 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 51 | 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 Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 59 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 60 | 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 Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 64 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 65 | zx_status_t set_cache_policy(uint32_t cache_policy) const { |
| 66 | return zx_vmo_set_cache_policy(get(), cache_policy); |
| 67 | } |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 68 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 69 | 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 Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | using unowned_vmo = unowned<vmo>; |
| 81 | |
Adam Barth | 57eacf5 | 2020-11-04 00:38:09 +0000 | [diff] [blame] | 82 | } // namespace zx |
Doug Horn | 1427b6a | 2018-12-11 13:19:16 -0800 | [diff] [blame] | 83 | |
| 84 | #endif // LIB_ZX_VMO_H_ |