| 1 |
/* GNU/Linux/x86-64 specific low level interface, for the in-process |
| 2 |
agent library for GDB. |
| 3 |
|
| 4 |
Copyright (C) 2010-2025 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GDB. |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include <sys/mman.h> |
| 22 |
#include "tracepoint.h" |
| 23 |
#include "gdbsupport/x86-xstate.h" |
| 24 |
#include "arch/amd64-linux-tdesc.h" |
| 25 |
#include "arch/x86-linux-tdesc-features.h" |
| 26 |
|
| 27 |
/* fast tracepoints collect registers. */ |
| 28 |
|
| 29 |
#define FT_CR_RIP 0 |
| 30 |
#define FT_CR_EFLAGS 1 |
| 31 |
#define FT_CR_R8 2 |
| 32 |
#define FT_CR_R9 3 |
| 33 |
#define FT_CR_R10 4 |
| 34 |
#define FT_CR_R11 5 |
| 35 |
#define FT_CR_R12 6 |
| 36 |
#define FT_CR_R13 7 |
| 37 |
#define FT_CR_R14 8 |
| 38 |
#define FT_CR_R15 9 |
| 39 |
#define FT_CR_RAX 10 |
| 40 |
#define FT_CR_RBX 11 |
| 41 |
#define FT_CR_RCX 12 |
| 42 |
#define FT_CR_RDX 13 |
| 43 |
#define FT_CR_RSI 14 |
| 44 |
#define FT_CR_RDI 15 |
| 45 |
#define FT_CR_RBP 16 |
| 46 |
#define FT_CR_RSP 17 |
| 47 |
|
| 48 |
static const int x86_64_ft_collect_regmap[] = { |
| 49 |
FT_CR_RAX * 8, FT_CR_RBX * 8, FT_CR_RCX * 8, FT_CR_RDX * 8, |
| 50 |
FT_CR_RSI * 8, FT_CR_RDI * 8, FT_CR_RBP * 8, FT_CR_RSP * 8, |
| 51 |
FT_CR_R8 * 8, FT_CR_R9 * 8, FT_CR_R10 * 8, FT_CR_R11 * 8, |
| 52 |
FT_CR_R12 * 8, FT_CR_R13 * 8, FT_CR_R14 * 8, FT_CR_R15 * 8, |
| 53 |
FT_CR_RIP * 8, FT_CR_EFLAGS * 8 |
| 54 |
}; |
| 55 |
|
| 56 |
#define X86_64_NUM_FT_COLLECT_GREGS \ |
| 57 |
(sizeof (x86_64_ft_collect_regmap) / sizeof(x86_64_ft_collect_regmap[0])) |
| 58 |
|
| 59 |
void |
| 60 |
supply_fast_tracepoint_registers (struct regcache *regcache, |
| 61 |
const unsigned char *buf) |
| 62 |
{ |
| 63 |
int i; |
| 64 |
|
| 65 |
for (i = 0; i < X86_64_NUM_FT_COLLECT_GREGS; i++) |
| 66 |
supply_register (regcache, i, |
| 67 |
((char *) buf) + x86_64_ft_collect_regmap[i]); |
| 68 |
} |
| 69 |
|
| 70 |
ULONGEST |
| 71 |
get_raw_reg (const unsigned char *raw_regs, int regnum) |
| 72 |
{ |
| 73 |
if (regnum >= X86_64_NUM_FT_COLLECT_GREGS) |
| 74 |
return 0; |
| 75 |
|
| 76 |
return *(ULONGEST *) (raw_regs + x86_64_ft_collect_regmap[regnum]); |
| 77 |
} |
| 78 |
|
| 79 |
/* Return target_desc to use for IPA, given the tdesc index passed by |
| 80 |
gdbserver. */ |
| 81 |
|
| 82 |
const struct target_desc * |
| 83 |
get_ipa_tdesc (int idx) |
| 84 |
{ |
| 85 |
uint64_t xstate_bv = x86_linux_tdesc_idx_to_xstate_bv (idx); |
| 86 |
|
| 87 |
#if defined __ILP32__ |
| 88 |
bool is_x32 = true; |
| 89 |
#else |
| 90 |
bool is_x32 = false; |
| 91 |
#endif |
| 92 |
|
| 93 |
return amd64_linux_read_description (xstate_bv, is_x32); |
| 94 |
} |
| 95 |
|
| 96 |
/* Allocate buffer for the jump pads. The branch instruction has a |
| 97 |
reach of +/- 31-bit, and the executable is loaded at low addresses. |
| 98 |
|
| 99 |
64-bit: Use MAP_32BIT to allocate in the first 2GB. Shared |
| 100 |
libraries, being allocated at the top, are unfortunately out of |
| 101 |
luck. |
| 102 |
|
| 103 |
x32: Since MAP_32BIT is 64-bit only, do the placement manually. |
| 104 |
Try allocating at '0x80000000 - SIZE' initially, decreasing until |
| 105 |
we hit a free area. This ensures the executable is fully covered, |
| 106 |
and is as close as possible to the shared libraries, which are |
| 107 |
usually mapped at the top of the first 4GB of the address space. |
| 108 |
*/ |
| 109 |
|
| 110 |
void * |
| 111 |
alloc_jump_pad_buffer (size_t size) |
| 112 |
{ |
| 113 |
#if __ILP32__ |
| 114 |
uintptr_t addr; |
| 115 |
int pagesize; |
| 116 |
|
| 117 |
pagesize = sysconf (_SC_PAGE_SIZE); |
| 118 |
if (pagesize == -1) |
| 119 |
perror_with_name ("sysconf"); |
| 120 |
|
| 121 |
addr = 0x80000000 - size; |
| 122 |
|
| 123 |
/* size should already be page-aligned, but this can't hurt. */ |
| 124 |
addr &= ~(pagesize - 1); |
| 125 |
|
| 126 |
/* Search for a free area. If we hit 0, we're out of luck. */ |
| 127 |
for (; addr; addr -= pagesize) |
| 128 |
{ |
| 129 |
void *res; |
| 130 |
|
| 131 |
/* No MAP_FIXED - we don't want to zap someone's mapping. */ |
| 132 |
res = mmap ((void *) addr, size, |
| 133 |
PROT_READ | PROT_WRITE | PROT_EXEC, |
| 134 |
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 135 |
|
| 136 |
/* If we got what we wanted, return. */ |
| 137 |
if ((uintptr_t) res == addr) |
| 138 |
return res; |
| 139 |
|
| 140 |
/* If we got a mapping, but at a wrong address, undo it. */ |
| 141 |
if (res != MAP_FAILED) |
| 142 |
munmap (res, size); |
| 143 |
} |
| 144 |
|
| 145 |
return NULL; |
| 146 |
#else |
| 147 |
void *res = mmap (NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 148 |
MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0); |
| 149 |
|
| 150 |
if (res == MAP_FAILED) |
| 151 |
return NULL; |
| 152 |
|
| 153 |
return res; |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
void |
| 158 |
initialize_low_tracepoint (void) |
| 159 |
{ |
| 160 |
#if defined __ILP32__ |
| 161 |
for (int i = 0; i < x86_linux_x32_tdesc_count (); i++) |
| 162 |
amd64_linux_read_description |
| 163 |
(x86_linux_tdesc_idx_to_xstate_bv (i), true); |
| 164 |
#else |
| 165 |
for (int i = 0; i < x86_linux_amd64_tdesc_count (); i++) |
| 166 |
amd64_linux_read_description |
| 167 |
(x86_linux_tdesc_idx_to_xstate_bv (i), false); |
| 168 |
#endif |
| 169 |
} |