blob: 8834ab4b30f5d4765c19affdca64216096a07e6f [file] [log] [blame]
Andreas Huber0a451282016-08-30 11:27:24 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MQDescriptor"
18#include <android-base/logging.h>
19
20#include <hidl/MQDescriptor.h>
21
22#include <cutils/native_handle.h>
23#include <sys/mman.h>
24
25namespace android {
26namespace hardware {
27
28MQDescriptor::MQDescriptor(
29 const std::vector<GrantorDescriptor>& grantors,
30 native_handle_t* nhandle,
31 int32_t flags,
32 size_t size)
33 : mHandle(nhandle),
34 mQuantum(size),
35 mFlags(flags) {
36 mGrantors.resize(grantors.size());
37 for (size_t i = 0; i < grantors.size(); ++i) {
38 mGrantors[i] = grantors[i];
39 }
40}
41
42MQDescriptor::MQDescriptor(const MQDescriptor &other)
43 : mGrantors(other.mGrantors),
44 mHandle(nullptr),
45 mQuantum(other.mQuantum),
46 mFlags(other.mFlags) {
47 if (other.mHandle != nullptr) {
48 mHandle = native_handle_create(
49 other.mHandle->numFds, other.mHandle->numInts);
50
51 for (int i = 0; i < other.mHandle->numFds; ++i) {
52 const_cast<native_handle_t *>(mHandle)->data[i] =
53 dup(other.mHandle->data[i]);
54 }
55
56 memcpy(&const_cast<native_handle_t *>(mHandle)->data[other.mHandle->numFds],
57 &other.mHandle->data[other.mHandle->numFds],
58 other.mHandle->numInts * sizeof(int));
59 }
60}
61
62MQDescriptor::~MQDescriptor() {
63 if (mHandle != nullptr) {
64 native_handle_close(const_cast<native_handle_t *>(mHandle));
65 native_handle_delete(const_cast<native_handle_t *>(mHandle));
66 }
67}
68
69/*
70 * TODO: Modify design to send access flags over WireMQDescriptor
71 * as well and use the same for the mmap call.
72 */
73void* MQDescriptor::mapGrantorDescr(uint32_t grantor_idx) {
74 const native_handle_t* handle = mHandle;
75 int fdIndex = mGrantors[grantor_idx].fdIndex;
76 /*
77 * Offset for mmap must be a multiple of PAGE_SIZE.
78 */
79 int mapOffset = (mGrantors[grantor_idx].offset / PAGE_SIZE) * PAGE_SIZE;
80 int mapLength =
81 mGrantors[grantor_idx].offset - mapOffset + mGrantors[grantor_idx].extent;
82
83 void* address = mmap(0, mapLength, PROT_READ | PROT_WRITE, MAP_SHARED,
84 handle->data[fdIndex], mapOffset);
85 return (address == MAP_FAILED)
86 ? nullptr
87 : (uint8_t*)address + (mGrantors[grantor_idx].offset - mapOffset);
88}
89
90void MQDescriptor::unmapGrantorDescr(void* address, uint32_t grantor_idx) {
91 const native_handle_t* handle = mHandle;
92 int mapOffset = (mGrantors[grantor_idx].offset / PAGE_SIZE) * PAGE_SIZE;
93 int mapLength =
94 mGrantors[grantor_idx].offset - mapOffset + mGrantors[grantor_idx].extent;
95 void* baseAddress =
96 (uint8_t*)address - (mGrantors[grantor_idx].offset - mapOffset);
97 if (baseAddress) munmap(baseAddress, mapLength);
98}
99
100::android::status_t MQDescriptor::readEmbeddedFromParcel(
101 const Parcel &parcel,
102 size_t parentHandle,
103 size_t parentOffset) {
104 ::android::status_t _hidl_err = ::android::OK;
105
106 size_t _hidl_grantors_child;
107
108 _hidl_err = const_cast<hidl_vec<GrantorDescriptor> *>(
109 &mGrantors)->readEmbeddedFromParcel(
110 parcel,
111 parentHandle,
112 parentOffset + offsetof(MQDescriptor, mGrantors),
113 &_hidl_grantors_child);
114
115 if (_hidl_err != ::android::OK) { return _hidl_err; }
116
117 const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle(
118 parentHandle,
119 parentOffset + offsetof(MQDescriptor, mHandle));
120
121 if (_hidl_mq_handle_ptr == nullptr) {
122 _hidl_err = ::android::UNKNOWN_ERROR;
123 return _hidl_err;
124 }
125
126_hidl_error:
127 return _hidl_err;
128}
129
130::android::status_t MQDescriptor::writeEmbeddedToParcel(
131 Parcel *parcel,
132 size_t parentHandle,
133 size_t parentOffset) const {
134 ::android::status_t _hidl_err = ::android::OK;
135
136 size_t _hidl_grantors_child;
137
138 _hidl_err = mGrantors.writeEmbeddedToParcel(
139 parcel,
140 parentHandle,
141 parentOffset + offsetof(MQDescriptor, mGrantors),
142 &_hidl_grantors_child);
143
144 if (_hidl_err != ::android::OK) { return _hidl_err; }
145
146 _hidl_err = parcel->writeEmbeddedNativeHandle(
147 mHandle,
148 parentHandle,
149 parentOffset + offsetof(MQDescriptor, mHandle));
150
151 if (_hidl_err != ::android::OK) { return _hidl_err; }
152
153_hidl_error:
154 return _hidl_err;
155}
156
157} // namespace hardware
158} // namespace android
159