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 #ifndef BERBERIS_RUNTIME_PRIMITIVES_GUEST_FUNCTION_WRAPPER_IMPL_H_
18 #define BERBERIS_RUNTIME_PRIMITIVES_GUEST_FUNCTION_WRAPPER_IMPL_H_
19 
20 #include <cstddef>
21 
22 #include "berberis/guest_state/guest_addr.h"
23 #include "berberis/runtime_primitives/host_code.h"
24 
25 namespace berberis {
26 
27 struct GuestArgumentBuffer;
28 
29 // Wrap guest function so that host can call it as if it was host function.
30 // Signature is "<return-type><param-type>*":
31 //   v - void
32 //   p - pointer
33 //   i - int32
34 //   l - int64
35 //   f - fp32
36 //   d - fp64
37 // Guest runner is a function to actually invoke guest code. The default
38 // is RunGuestCall, custom runners might add pre- and post-processing.
39 
40 using GuestRunnerFunc = void (*)(GuestAddr pc, GuestArgumentBuffer* buf);
41 using IsAddressGuestExecutableFunc = bool (*)(GuestAddr pc);
42 
43 HostCode WrapGuestFunctionImpl(GuestAddr pc,
44                                const char* signature,
45                                GuestRunnerFunc guest_runner,
46                                const char* name);
47 
48 struct NamedGuestFunctionWrapper {
49   const char* name;
50   HostCode (*wrapper)(GuestAddr pc);
51 };
52 
53 GuestAddr SlowFindGuestAddrByWrapperAddr(void* wrapper_addr);
54 
55 void InitGuestFunctionWrapper(IsAddressGuestExecutableFunc func);
56 
57 }  // namespace berberis
58 
59 #endif  // BERBERIS_RUNTIME_PRIMITIVES_GUEST_FUNCTION_WRAPPER_IMPL_H_
60