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/runtime_library.h"
18 
19 #include <cstdint>
20 #include <cstring>
21 
22 #include "berberis/calling_conventions/calling_conventions_riscv64.h"
23 #include "berberis/guest_abi/guest_arguments.h"
24 #include "berberis/guest_os_primitives/guest_thread_manager.h"
25 #include "berberis/guest_os_primitives/scoped_pending_signals.h"
26 #include "berberis/guest_state/guest_addr.h"
27 #include "berberis/guest_state/guest_state_arch.h"
28 #include "berberis/instrument/guest_call.h"
29 #include "berberis/runtime_primitives/virtual_guest_call_frame.h"
30 
31 namespace berberis {
32 
33 // Call a guest function from the host with the given arguments. The return
34 // value is stored in the first argument of the buffer after the call returns.
35 //
36 // Within the guest call stack, the host has its own call frame. This stack
37 // frame is allocated by the ScopedVirtualGuestCallFrame instance. Virtual guest call frame
38 // simulates the minimum necessary prologue and epilogue for saving and
39 // restoring the frame pointer and return address on the stack. Within that call
40 // frame, we can make further adjustments to the stack pointer, such as
41 // allocating space on the stack for arguments that spill past the registers.
42 //
43 // To visualize this, consider a guest function GuestFuncA, which calls
44 // HostFuncB, with no arguments:
45 //   void GuestFuncA() {
46 //     HostFuncB();
47 //   }
48 //
49 // HostFuncB then calls GuestFuncC with 100 arguments:
50 //   void HostFuncB() {
51 //     GuestFuncC(1, 2, 3, ..., 98, 99, 100);
52 //   }
53 //
54 // At the time that HostFuncB calls GuestFuncC, GuestFuncA is at the top of the
55 // guest call stack because HostFuncB was entered directly. If no call frame for
56 // the host function is created, GuestFuncA would call GuestFuncC directly from
57 // the perspective of the guest. The arguments for GuestFuncC would appear in
58 // GuestFuncA's frame.
59 //
60 // RISC-V uses a calling convention with caller clean-up. In these types of
61 // calling conventions, the caller is responsible for deallocating arguments
62 // passed on the stack after the callee returns. However, GuestFuncC is unaware
63 // of these arguments, so a stand-in call frame for HostFuncB, GuestFuncCCaller,
64 // is created. GuestFuncCCaller populates the stack arguments for GuestFuncC,
65 // makes the call, and then deallocates the frame.
RunGuestCall(GuestAddr pc,GuestArgumentBuffer * buf)66 void RunGuestCall(GuestAddr pc, GuestArgumentBuffer* buf) {
67   GuestThread* thread = GetCurrentGuestThread();
68   ThreadState* state = thread->state();
69 
70   ScopedPendingSignalsEnabler scoped_pending_signals_enabler(thread);
71 
72   ScopedVirtualGuestCallFrame virtual_guest_call_frame(&state->cpu, pc);
73 
74   // Copy argc int and fp_argc float registers for the arguments into the argument buffer.
75   memcpy(&(state->cpu.x[A0]), buf->argv, buf->argc * sizeof(buf->argv[0]));
76   memcpy(&(state->cpu.f[FA0]), buf->fp_argv, buf->fp_argc * sizeof(buf->fp_argv[0]));
77 
78   // sp -= stack_argc
79   SetXReg<SP>(state->cpu, GetXReg<SP>(state->cpu) - buf->stack_argc);
80   // sp = align_down(sp, ...)
81   SetXReg<SP>(
82       state->cpu,
83       AlignDown(GetXReg<SP>(state->cpu), riscv64::CallingConventions::kStackAlignmentBeforeCall));
84 
85   memcpy(ToHostAddr<void>(GetXReg<SP>(state->cpu)), buf->stack_argv, buf->stack_argc);
86 
87   if (kInstrumentWrappers) {
88     OnWrappedGuestCall(state, pc);
89   }
90 
91   ExecuteGuestCall(state);
92 
93   if (kInstrumentWrappers) {
94     OnWrappedGuestReturn(state, pc);
95   }
96 
97   // Copy resc int and fp_resc float registers for the results from the buffer to the cpu state.
98   memcpy(buf->argv, &(state->cpu.x[A0]), buf->resc * sizeof(buf->argv[0]));
99   memcpy(buf->fp_argv, &(state->cpu.f[FA0]), buf->fp_resc * sizeof(buf->fp_argv[0]));
100 }
101 
102 }  // namespace berberis
103