• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const struct MemEntry * entry)34 HostmemIdMapping::Id HostmemIdMapping::add(const struct MemEntry *entry) {
35     if (entry->hva == 0 || entry->size == 0) return kInvalidHostmemId;
36 
37     Id wantedId;
38 
39     if (entry->register_fixed) {
40         wantedId = entry->fixed_id;
41         mCurrentId = wantedId + 1;
42     } else {
43         wantedId = mCurrentId++;
44     }
45 
46     HostmemIdMapping::Entry hostmem_entry;
47     hostmem_entry.id = wantedId;
48     hostmem_entry.hva = entry->hva;
49     hostmem_entry.size = entry->size;
50     hostmem_entry.caching = entry->caching;
51     mEntries.set(wantedId, hostmem_entry);
52     return wantedId;
53 }
54 
remove(HostmemIdMapping::Id id)55 void HostmemIdMapping::remove(HostmemIdMapping::Id id) {
56     mEntries.erase(id);
57 }
58 
get(HostmemIdMapping::Id id) const59 HostmemIdMapping::Entry HostmemIdMapping::get(HostmemIdMapping::Id id) const {
60     const HostmemIdMapping::Entry badEntry {
61         kInvalidHostmemId, 0, 0,
62     };
63 
64     if (kInvalidHostmemId == id) return badEntry;
65 
66     auto entry = mEntries.get(id);
67 
68     if (!entry) return badEntry;
69 
70     return *entry;
71 }
72 
clear()73 void HostmemIdMapping::clear() {
74     mEntries.clear();
75 }
76 
77 } // namespace android
78 } // namespace emulation
79 
80 // C interface for use with vm operations
81 extern "C" {
82 
android_emulation_hostmem_register(const struct MemEntry * entry)83 uint64_t android_emulation_hostmem_register(const struct MemEntry *entry) {
84     return static_cast<uint64_t>(
85         android::emulation::HostmemIdMapping::get()->add(entry));
86 }
87 
android_emulation_hostmem_unregister(uint64_t id)88 void android_emulation_hostmem_unregister(uint64_t id) {
89     android::emulation::HostmemIdMapping::get()->remove(id);
90 }
91 
android_emulation_hostmem_get_info(uint64_t id)92 HostmemEntry android_emulation_hostmem_get_info(uint64_t id) {
93     return static_cast<HostmemEntry>(
94         android::emulation::HostmemIdMapping::get()->get(id));
95 }
96 
97 } // extern "C"
98