1 // Copyright 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "host-common/HostmemIdMapping.h"
15
16 namespace android {
17 namespace emulation {
18
19 // static
20 const HostmemIdMapping::Id HostmemIdMapping::kInvalidHostmemId = 0;
21
sMapping()22 static HostmemIdMapping* sMapping() {
23 static HostmemIdMapping* s = new HostmemIdMapping;
24 return s;
25 }
26
27 // static
get()28 HostmemIdMapping* HostmemIdMapping::get() {
29 return sMapping();
30 }
31
32 // TODO: Add registerHostmemFixed version that takes a predetermined id,
33 // for snapshots
add(uint64_t hva,uint64_t size,bool register_fixed,uint64_t fixed_id)34 HostmemIdMapping::Id HostmemIdMapping::add(uint64_t hva, uint64_t size, bool register_fixed, uint64_t fixed_id) {
35 if (0 == hva || 0 == size) return kInvalidHostmemId;
36
37 Id wantedId;
38
39 if (register_fixed) {
40 wantedId = fixed_id;
41 mCurrentId = wantedId + 1;
42 } else {
43 wantedId = mCurrentId++;
44 }
45
46 HostmemIdMapping::Entry entry;
47 entry.id = wantedId;
48 entry.hva = hva;
49 entry.size = size;
50 mEntries.set(wantedId, entry);
51 return wantedId;
52 }
53
remove(HostmemIdMapping::Id id)54 void HostmemIdMapping::remove(HostmemIdMapping::Id id) {
55 mEntries.erase(id);
56 }
57
get(HostmemIdMapping::Id id) const58 HostmemIdMapping::Entry HostmemIdMapping::get(HostmemIdMapping::Id id) const {
59 const HostmemIdMapping::Entry badEntry {
60 kInvalidHostmemId, 0, 0,
61 };
62
63 if (kInvalidHostmemId == id) return badEntry;
64
65 auto entry = mEntries.get(id);
66
67 if (!entry) return badEntry;
68
69 return *entry;
70 }
71
clear()72 void HostmemIdMapping::clear() {
73 mEntries.clear();
74 }
75
76 } // namespace android
77 } // namespace emulation
78
79 // C interface for use with vm operations
80 extern "C" {
81
android_emulation_hostmem_register(uint64_t hva,uint64_t size,uint32_t register_fixed,uint64_t fixed_id)82 uint64_t android_emulation_hostmem_register(uint64_t hva, uint64_t size, uint32_t register_fixed, uint64_t fixed_id) {
83 return static_cast<uint64_t>(
84 android::emulation::HostmemIdMapping::get()->add(hva, size, register_fixed, fixed_id));
85 }
86
android_emulation_hostmem_unregister(uint64_t id)87 void android_emulation_hostmem_unregister(uint64_t id) {
88 android::emulation::HostmemIdMapping::get()->remove(id);
89 }
90
android_emulation_hostmem_get_info(uint64_t id)91 HostmemEntry android_emulation_hostmem_get_info(uint64_t id) {
92 return static_cast<HostmemEntry>(
93 android::emulation::HostmemIdMapping::get()->get(id));
94 }
95
96 } // extern "C"
97