1 /*
2 * Copyright (C) 2023 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 #include "berberis/runtime_primitives/known_guest_function_wrapper.h"
18
19 #include <map>
20 #include <mutex>
21 #include <string>
22
23 #include "berberis/base/forever_alloc.h"
24 #include "berberis/guest_state/guest_addr.h"
25 #include "berberis/runtime_primitives/host_code.h"
26
27 namespace berberis {
28
29 namespace {
30
31 class GuestFunctionWrapper {
32 public:
GetInstance()33 static GuestFunctionWrapper* GetInstance() {
34 static auto* g_wrapper = NewForever<GuestFunctionWrapper>();
35 return g_wrapper;
36 }
37
RegisterKnown(const char * name,HostCode (* wrapper)(GuestAddr))38 void RegisterKnown(const char* name, HostCode (*wrapper)(GuestAddr)) {
39 std::lock_guard<std::mutex> guard(mutex_);
40 wrappers_.insert({name, wrapper});
41 }
42
WrapKnown(GuestAddr guest_addr,const char * name)43 HostCode WrapKnown(GuestAddr guest_addr, const char* name) {
44 std::lock_guard<std::mutex> guard(mutex_);
45 auto wrapper = wrappers_.find(name);
46 if (wrapper == end(wrappers_)) {
47 return nullptr;
48 }
49 return wrapper->second(guest_addr);
50 }
51
52 private:
53 GuestFunctionWrapper() = default;
54 GuestFunctionWrapper(const GuestFunctionWrapper&) = delete;
55 GuestFunctionWrapper& operator=(const GuestFunctionWrapper&) = delete;
56
57 friend GuestFunctionWrapper* NewForever<GuestFunctionWrapper>();
58
59 std::map<std::string, HostCode (*)(GuestAddr)> wrappers_;
60 std::mutex mutex_;
61 };
62
63 } // namespace
64
RegisterKnownGuestFunctionWrapper(const char * name,HostCode (* wrapper)(GuestAddr))65 void RegisterKnownGuestFunctionWrapper(const char* name, HostCode (*wrapper)(GuestAddr)) {
66 GuestFunctionWrapper::GetInstance()->RegisterKnown(name, wrapper);
67 }
68
WrapKnownGuestFunction(GuestAddr guest_addr,const char * name)69 HostCode WrapKnownGuestFunction(GuestAddr guest_addr, const char* name) {
70 return GuestFunctionWrapper::GetInstance()->WrapKnown(guest_addr, name);
71 }
72
73 }; // namespace berberis
74