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/virtual_guest_call_frame.h"
18
19 #include <cstdint>
20
21 #include "berberis/base/checks.h"
22 #include "berberis/guest_state/guest_addr.h"
23 #include "berberis/guest_state/guest_state.h"
24
25 namespace berberis {
26
27 GuestAddr ScopedVirtualGuestCallFrame::g_return_address_ = {};
28
29 // For RISC-V, guest function preserves at least sp and returns by jumping
30 // to address provided in ra. So here ctor emulates the following code:
31 //
32 // # save register to be changed and maintain stack alignment
33 // addi sp, sp, -16
34 // sd fp, 0(sp)
35 // sd ra, 8(sp)
36 // addi fp, x0, sp
37 //
38 // <parameters passing happens after ctor, adjusts a0-7, sp>
39 //
40 // ra = 'special-return-addr' # ensure stop after return and call guest function, as
41 // pc = 'pc' # 'special-return-addr': jalr ra, 0('pc')
42 //
43 // and dtor emulates the following code:
44 //
45 // addi sp, x0, fp
46 // # restore registers
47 // ld fp, 0(sp)
48 // ld ra, 8(sp)
49 // addi sp, sp, 16
50 //
ScopedVirtualGuestCallFrame(CPUState * cpu,GuestAddr pc)51 ScopedVirtualGuestCallFrame::ScopedVirtualGuestCallFrame(CPUState* cpu, GuestAddr pc) : cpu_(cpu) {
52 // addi sp, sp, -16
53 SetXReg<SP>(*cpu_, GetXReg<SP>(*cpu_) - 16);
54 // sd fp, 0(sp)
55 // sd ra, 8(sp)
56 uint64_t* saved_regs = ToHostAddr<uint64_t>(GetXReg<SP>(*cpu_));
57 saved_regs[0] = GetXReg<FP>(*cpu_);
58 saved_regs[1] = GetXReg<RA>(*cpu_);
59 // addi fp, x0, sp
60 SetXReg<FP>(*cpu_, GetXReg<SP>(*cpu_));
61
62 // For safety checks!
63 stack_pointer_ = GetXReg<FP>(*cpu_);
64 link_register_ = GetXReg<RA>(*cpu_);
65
66 program_counter_ = cpu_->insn_addr;
67
68 // Set pc and ra as for 'jalr ra, <guest>'.
69 SetXReg<RA>(*cpu_, g_return_address_);
70 cpu_->insn_addr = pc;
71 }
72
~ScopedVirtualGuestCallFrame()73 ScopedVirtualGuestCallFrame::~ScopedVirtualGuestCallFrame() {
74 // Safety check - returned to correct pc?
75 CHECK_EQ(g_return_address_, cpu_->insn_addr);
76 // Safety check - guest call didn't preserve fp?
77 CHECK_EQ(stack_pointer_, GetXReg<FP>(*cpu_));
78
79 SetXReg<SP>(*cpu_, GetXReg<FP>(*cpu_));
80
81 const uint64_t* saved_regs = ToHostAddr<uint64_t>(GetXReg<SP>(*cpu_));
82 // ld fp, 0(sp)
83 // ld ra, 8(sp)
84 SetXReg<FP>(*cpu_, saved_regs[0]);
85 SetXReg<RA>(*cpu_, saved_regs[1]);
86 // addi sp, sp, 16
87 SetXReg<SP>(*cpu_, GetXReg<SP>(*cpu_) + 16);
88 cpu_->insn_addr = program_counter_;
89
90 // Safety checks - guest stack was smashed?
91 CHECK_EQ(link_register_, GetXReg<RA>(*cpu_));
92 }
93
94 } // namespace berberis
95