Cody Schuffelen | 134ff03 | 2019-11-22 00:25:32 -0800 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2017 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "common/vsoc/lib/typed_region_view.h" |
| 20 | |
| 21 | namespace vsoc { |
| 22 | |
| 23 | /** |
| 24 | * Adds methods to create file descriptor scoped permissions. Just like |
| 25 | * TypedRegionView it can be directly constructed or subclassed. |
| 26 | * |
| 27 | * The Layout type must (in addition to requirements for TypedRegionView) also |
| 28 | * provide a nested type for the layout of the managed region. |
| 29 | */ |
| 30 | template <typename View, typename Layout> |
| 31 | class ManagerRegionView : public TypedRegionView<View, Layout> { |
| 32 | public: |
| 33 | ManagerRegionView() = default; |
| 34 | /** |
| 35 | * Creates a fd scoped permission on the managed region. |
| 36 | * |
| 37 | * The managed_region_fd is in/out parameter that can be a not yet open file |
| 38 | * descriptor. If the fd is not open yet it will open the managed region |
| 39 | * device and then create the permission. If the function returns EBUSY |
| 40 | * (meaning that we lost the race to acquire the memory) the same fd can (and |
| 41 | * is expected to) be used in a subsequent call to create a permission on |
| 42 | * another memory location. |
| 43 | * |
| 44 | * On success returns an open fd with the requested permission asociated to |
| 45 | * it. If another thread/process acquired ownership of *owner_ptr before this |
| 46 | * one returns -EBUSY, returns a different negative number otherwise. |
| 47 | */ |
| 48 | int CreateFdScopedPermission(uint32_t* owner_ptr, uint32_t owned_val, |
| 49 | uint32_t begin_offset, |
| 50 | uint32_t end_offset) { |
| 51 | return this->control_->CreateFdScopedPermission( |
| 52 | Layout::ManagedRegion::region_name, |
| 53 | this->pointer_to_region_offset(owner_ptr), owned_val, begin_offset, |
| 54 | end_offset); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | } // namespace vsoc |