1 /*
2 * Copyright (C) 2020 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 <sys/ptrace.h>
18 #include <sys/uio.h>
19
20 #ifdef __BIONIC__
21 #include <bionic/mte.h>
22 #endif
23
24 #include "MemoryLocal.h"
25 #include "MemoryRemote.h"
26
27 namespace unwindstack {
28
ReadTag(uint64_t addr)29 long MemoryRemote::ReadTag(uint64_t addr) {
30 #if defined(__aarch64__)
31 char tag;
32 iovec iov = {&tag, 1};
33 if (ptrace(PTRACE_PEEKMTETAGS, pid_, reinterpret_cast<void*>(addr), &iov) != 0 ||
34 iov.iov_len != 1) {
35 return -1;
36 }
37 return tag;
38 #else
39 (void)addr;
40 return -1;
41 #endif
42 }
43
ReadTag(uint64_t addr)44 long MemoryLocal::ReadTag(uint64_t addr) {
45 #if defined(__aarch64__)
46 // Check that the memory is readable first. This is racy with the ldg but there's not much
47 // we can do about it.
48 char data;
49 if (!mte_supported() || !Read(addr, &data, 1)) {
50 return -1;
51 }
52
53 __asm__ __volatile__(".arch_extension mte; ldg %0, [%0]" : "+r"(addr) : : "memory");
54 return (addr >> 56) & 0xf;
55 #else
56 (void)addr;
57 return -1;
58 #endif
59 }
60
61 } // namespace unwindstack
62