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_HOST_FUNCTION_WRAPPER_IMPL_H_
18 #define BERBERIS_RUNTIME_PRIMITIVES_HOST_FUNCTION_WRAPPER_IMPL_H_
19
20 #include "berberis/guest_state/guest_addr.h"
21 #include "berberis/guest_state/guest_state_opaque.h"
22 #include "berberis/runtime_primitives/checks.h"
23 #include "berberis/runtime_primitives/host_code.h"
24
25 namespace berberis {
26
27 // When guest branches to 'pc', call the host function 'func' via trampoline 'trampoline_func'.
28 // Trampoline is a helper function invoked as 'trampoline_func(func, process_state)'.
29 // It extracts guest parameters, applies necessary conversions and calls 'func', then converts
30 // the return value and writes it back to the guest state.
31 // 'name' is used for debugging.
32 using TrampolineFunc = void (*)(HostCode, ThreadState*);
33
34 struct NamedTrampolineFunc {
35 const char* name;
36 TrampolineFunc trampoline;
37 };
38
39 void MakeTrampolineCallable(GuestAddr pc,
40 bool is_host_func,
41 TrampolineFunc trampoline_func,
42 HostCode func,
43 const char* name);
44
WrapHostFunctionImpl(HostCode func,TrampolineFunc trampoline_func,const char * name)45 inline void WrapHostFunctionImpl(HostCode func, TrampolineFunc trampoline_func, const char* name) {
46 MakeTrampolineCallable(ToGuestAddr(func), true, trampoline_func, func, name);
47 }
48
49 void* UnwrapHostFunction(GuestAddr pc);
50
51 } // namespace berberis
52
53 #endif // BERBERIS_RUNTIME_PRIMITIVES_HOST_FUNCTION_WRAPPER_IMPL_H_
54