blob: d1d0ebc29f81e7ed69f33276619ae9802cc98c42 [file] [log] [blame]
Peter Collingbournec71e0a62020-04-07 14:04:46 -07001/*
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#if defined(ANDROID_EXPERIMENTAL_MTE)
18
19#include <sys/ptrace.h>
20
21#include <bionic/mte.h>
22#include <bionic/mte_kernel.h>
23
24#include "MemoryLocal.h"
25#include "MemoryRemote.h"
26
27namespace unwindstack {
28
29long MemoryRemote::ReadTag(uint64_t addr) {
30#if defined(__aarch64__)
31 return ptrace(PTRACE_PEEKTAG, pid_, (void*)addr, nullptr);
32#else
33 (void)addr;
34 return -1;
35#endif
36}
37
38long MemoryLocal::ReadTag(uint64_t addr) {
39#if defined(__aarch64__)
40 // Check that the memory is readable first. This is racy with the ldg but there's not much
41 // we can do about it.
42 char data;
43 if (!mte_supported() || !Read(addr, &data, 1)) {
44 return -1;
45 }
46
47 __asm__ __volatile__(".arch_extension mte; ldg %0, [%0]" : "+r"(addr) : : "memory");
48 return (addr >> 56) & 0xf;
49#else
50 (void)addr;
51 return -1;
52#endif
53}
54
55} // namespace unwindstack
56
57#endif