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/guest_os_primitives/guest_setjmp.h"
18
19 #include <csetjmp>
20 #include <cstdint>
21 #include <cstring> // memcpy
22
23 #include "berberis/base/host_signal.h"
24 #include "berberis/guest_state/guest_state.h"
25
26 namespace berberis {
27
28 namespace {
29
30 // jmp_buf format is totally Bionic-private (see bionic/libc/arch-riscv64/bionic/setjmp.S)
31 // We don't have to use the original format as save/restore is only done here.
32 // Still, let's keep it compatible with some release as it might help debugging...
33 //
34 // word name description
35 // 0 sigflag/cookie setjmp cookie in top 31 bits, signal mask flag in low bit
36 // 1 sigmask 64-bit signal mask
37 // 2 ra
38 // 3 sp
39 // 4 gp
40 // 5 s0
41 // ......
42 // 16 s11
43 // 17 fs0
44 // ......
45 // 28 fs11
46 // 29 checksum
47 // _JBLEN: defined in bionic/libc/include/setjmp.h
48
49 const int kJmpBufSigFlagAndCookieWord = 0;
50 const int kJmpBufSigMaskWord = 1;
51 const int kJmpBufRaWord = 2;
52 const int kJmpBufCoreBaseWord = 5;
53 const int kJmpBufFloatingPointBaseWord = 17;
54 const int kJmpBufChecksumWord = 29;
55 // jmp_buf should be at least 32 words long.
56 // Use the last word to store the address of the host jmp_buf.
57 const int kJmpBufHostBufWord = 31;
58
59 // jmp_buf cookie can be anything but 0 (see bionic/tests/setjmp_test.cpp: setjmp_cookie)
60 // ATTENTION: Keep low bit 0 for signal mask flag.
61 const uint64_t kJmpBufCookie = 0x123210ULL;
62
CalcJumpBufChecksum(const uint64_t * buf)63 uint64_t CalcJumpBufChecksum(const uint64_t* buf) {
64 uint64_t res = 0;
65 for (int i = 0; i < kJmpBufChecksumWord; ++i) {
66 res ^= buf[i];
67 }
68 return res;
69 }
70
71 } // namespace
72
SaveRegsToJumpBuf(const ThreadState * state,void * guest_jmp_buf,int save_sig_mask)73 void SaveRegsToJumpBuf(const ThreadState* state, void* guest_jmp_buf, int save_sig_mask) {
74 uint64_t* buf = reinterpret_cast<uint64_t*>(guest_jmp_buf);
75
76 // Clear the buffer in case the format has gaps.
77 memset(buf, 0, kJmpBufChecksumWord * sizeof(uint64_t));
78
79 // Cookie, signal flag, signal mask
80 buf[kJmpBufSigFlagAndCookieWord] = kJmpBufCookie;
81 if (save_sig_mask) {
82 buf[kJmpBufSigFlagAndCookieWord] |= 0x1;
83 RTSigprocmaskSyscallOrDie(
84 SIG_SETMASK, nullptr, reinterpret_cast<HostSigset*>(buf + kJmpBufSigMaskWord));
85 }
86
87 // Copy contiguous sets of registers below.
88 constexpr size_t kXRegSize = sizeof(state->cpu.x[0]);
89 constexpr size_t kFRegSize = sizeof(state->cpu.f[0]);
90
91 // ra, sp, gp are contiguous.
92 memcpy(buf + kJmpBufRaWord, state->cpu.x + RA, 3 * kXRegSize);
93
94 // s0 - s1
95 memcpy(buf + kJmpBufCoreBaseWord, state->cpu.x + S0, 2 * kXRegSize);
96 // s2 - s11
97 memcpy(buf + kJmpBufCoreBaseWord + 2, state->cpu.x + S2, 10 * kXRegSize);
98
99 // fs0 - fs1
100 memcpy(buf + kJmpBufFloatingPointBaseWord, state->cpu.f + FS0, 2 * kFRegSize);
101 // fs2 - fs11
102 memcpy(buf + kJmpBufFloatingPointBaseWord + 2, state->cpu.f + FS2, 10 * kFRegSize);
103
104 // Checksum
105 buf[kJmpBufChecksumWord] = CalcJumpBufChecksum(buf);
106 }
107
RestoreRegsFromJumpBuf(ThreadState * state,void * guest_jmp_buf,int retval)108 void RestoreRegsFromJumpBuf(ThreadState* state, void* guest_jmp_buf, int retval) {
109 const uint64_t* buf = reinterpret_cast<const uint64_t*>(guest_jmp_buf);
110
111 // Checksum
112 if (buf[kJmpBufChecksumWord] != CalcJumpBufChecksum(buf)) {
113 LOG_ALWAYS_FATAL("setjmp checksum mismatch");
114 }
115
116 // Cookie
117 if ((buf[kJmpBufSigFlagAndCookieWord] & ~0x1) != kJmpBufCookie) {
118 LOG_ALWAYS_FATAL("setjmp cookie mismatch");
119 }
120
121 // Signal mask
122 if (buf[kJmpBufSigFlagAndCookieWord] & 0x1) {
123 RTSigprocmaskSyscallOrDie(
124 SIG_SETMASK, reinterpret_cast<const HostSigset*>(buf + kJmpBufSigMaskWord), nullptr);
125 }
126
127 // Copy contiguous sets of registers below.
128 constexpr size_t kXRegSize = sizeof(state->cpu.x[0]);
129 constexpr size_t kFRegSize = sizeof(state->cpu.f[0]);
130
131 // ra, sp, gp
132 memcpy(state->cpu.x + RA, buf + kJmpBufRaWord, 3 * kXRegSize);
133
134 // s0 - s1
135 memcpy(state->cpu.x + S0, buf + kJmpBufCoreBaseWord, 2 * kXRegSize);
136 // s2 - s11
137 memcpy(state->cpu.x + S2, buf + kJmpBufCoreBaseWord + 2, 10 * kXRegSize);
138
139 // fs0 - fs1
140 memcpy(state->cpu.f + S0, buf + kJmpBufFloatingPointBaseWord, 2 * kFRegSize);
141 // fs2 - fs11
142 memcpy(state->cpu.f + S2, buf + kJmpBufFloatingPointBaseWord + 2, 10 * kFRegSize);
143
144 // Function return
145 CPUState& cpu = state->cpu;
146 SetInsnAddr(cpu, GetLinkRegister(cpu));
147 SetXReg<A0>(cpu, retval);
148 }
149
GetHostJmpBufPtr(void * guest_jmp_buf)150 jmp_buf** GetHostJmpBufPtr(void* guest_jmp_buf) {
151 uint64_t* buf = reinterpret_cast<uint64_t*>(guest_jmp_buf);
152 return reinterpret_cast<jmp_buf**>(buf + kJmpBufHostBufWord);
153 }
154
155 } // namespace berberis
156