blob: 8f0bcf40d12abb318c8f348049fece67c19ab612 [file] [log] [blame]
Martijn Coenen30791002016-12-01 15:40:46 +01001/*
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#define LOG_TAG "libhidlmemory"
17
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070018#include <map>
19#include <mutex>
20#include <string>
21
Martijn Coenen30791002016-12-01 15:40:46 +010022#include <hidlmemory/mapping.h>
23
24#include <android-base/logging.h>
25#include <android/hidl/memory/1.0/IMapper.h>
26#include <hidl/HidlSupport.h>
Yifan Hong694bd8d2018-05-11 14:19:42 -070027#include <log/log.h>
Martijn Coenen30791002016-12-01 15:40:46 +010028
29using android::sp;
30using android::hidl::memory::V1_0::IMemory;
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070031using android::hidl::memory::V1_0::IMapper;
Martijn Coenen30791002016-12-01 15:40:46 +010032
33namespace android {
34namespace hardware {
35
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070036static std::map<std::string, sp<IMapper>> gMappersByName;
37static std::mutex gMutex;
Martijn Coenen30791002016-12-01 15:40:46 +010038
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070039static inline sp<IMapper> getMapperService(const std::string& name) {
40 std::unique_lock<std::mutex> _lock(gMutex);
41 auto iter = gMappersByName.find(name);
42 if (iter != gMappersByName.end()) {
43 return iter->second;
44 }
45
46 sp<IMapper> mapper = IMapper::getService(name, true /* getStub */);
47 if (mapper != nullptr) {
48 gMappersByName[name] = mapper;
49 }
50 return mapper;
51}
52
53sp<IMemory> mapMemory(const hidl_memory& memory) {
54
55 sp<IMapper> mapper = getMapperService(memory.name());
Martijn Coenen30791002016-12-01 15:40:46 +010056
57 if (mapper == nullptr) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070058 LOG(ERROR) << "Could not fetch mapper for " << memory.name() << " shared memory";
59 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010060 }
61
62 if (mapper->isRemote()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070063 LOG(ERROR) << "IMapper must be a passthrough service.";
64 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010065 }
66
Yifan Hong694bd8d2018-05-11 14:19:42 -070067 // hidl_memory's size is stored in uint64_t, but mapMemory's mmap will map
68 // size in size_t. If size is over SIZE_MAX, mapMemory could succeed
69 // but the mapped memory's actual size will be smaller than the reported size.
70 if (memory.size() > SIZE_MAX) {
71 LOG(ERROR) << "Cannot map " << memory.size() << " bytes of memory because it is too large.";
72 android_errorWriteLog(0x534e4554, "79376389");
73 return nullptr;
74 }
75
Martijn Coenend272cb92017-01-02 15:20:38 +010076 Return<sp<IMemory>> ret = mapper->mapMemory(memory);
Martijn Coenen30791002016-12-01 15:40:46 +010077
78 if (!ret.isOk()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070079 LOG(ERROR) << "hidl_memory map returned transport error.";
80 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010081 }
82
Martijn Coenend272cb92017-01-02 15:20:38 +010083 return ret;
Martijn Coenen30791002016-12-01 15:40:46 +010084}
85
86} // namespace hardware
Martijn Coenend272cb92017-01-02 15:20:38 +010087} // namespace android