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/interpret_helpers.h"
18
19 #include <signal.h>
20 #include <sys/syscall.h>
21 #include <unistd.h>
22
23 #include <cstdint>
24
25 #include "berberis/base/checks.h"
26 #include "berberis/base/logging.h"
27 #include "berberis/guest_state/guest_addr.h"
28
29 namespace berberis {
30
31 namespace {
32
GetRiscv64InsnSize(GuestAddr pc)33 uint8_t GetRiscv64InsnSize(GuestAddr pc) {
34 constexpr uint16_t kInsnLenMask = uint16_t{0b11};
35 if ((*ToHostAddr<const uint16_t>(pc) & kInsnLenMask) != kInsnLenMask) {
36 return 2;
37 }
38 return 4;
39 }
40
41 } // namespace
42
UndefinedInsn(GuestAddr pc)43 void UndefinedInsn(GuestAddr pc) {
44 auto* addr = ToHostAddr<const uint16_t>(pc);
45 uint8_t size = GetRiscv64InsnSize(pc);
46 if (size == 2) {
47 ALOGE("Undefined riscv64 instruction 0x%" PRIx16 " at %p", *addr, addr);
48 } else {
49 CHECK_EQ(size, 4);
50 // Warning: do not cast and dereference the pointer since the address may not be 4-bytes
51 // aligned.
52 uint32_t code;
53 memcpy(&code, addr, sizeof(code));
54 ALOGE("Undefined riscv64 instruction 0x%" PRIx32 " at %p", code, addr);
55 }
56 #ifdef __GLIBC__
57 // Our old 2.17 glibc has a bug resulting in raise() failing after any CLONE_VM.
58 // Work around by calling tgkill directly.
59 pid_t pid = syscall(__NR_getpid);
60 pid_t tid = syscall(__NR_gettid);
61 syscall(SYS_tgkill, pid, tid, SIGILL);
62 #else
63 raise(SIGILL);
64 #endif
65 }
66
67 } // namespace berberis
68