| 1 |
# Implicit Thread Local Storage tapset |
| 2 |
# Copyright (C) 2020, 2021 Red Hat, Inc. |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License version 2 as |
| 6 |
# published by the Free Software Foundation. |
| 7 |
# Reference: "ELF Handling For Thread-Local Storage" by Ulrich Drepper |
| 8 |
|
| 9 |
/* |
| 10 |
* TCB Pointer points to the thread control block. dtv points to the DTV |
| 11 |
* array. DTV[1]-DTV[N] point to tls entries for module I from the link |
| 12 |
* map. DTV[1] is the main executable. The dtv val points to the tls |
| 13 |
* entry where the tls variable value is located. The tls variable value |
| 14 |
* is also located a fixed offset from the tcbhead so if relocations are |
| 15 |
* simple enough a compiler can generate a direct load. Complicated |
| 16 |
* relocations may result in a compiler calling a runtime routine |
| 17 |
* __tls_get_addr which uses the DTV. |
| 18 |
* |
| 19 |
* TCB Pointer |
| 20 |
* | |
| 21 |
* V |
| 22 |
* TLS struct tcbhead |
| 23 |
* +---------------------+ +----------------+ |
| 24 |
* |OFFSET|OFFSET|OFFSET | | void *tcb | |
| 25 |
* | 2 | 1 | 0 | | dtv_t *dtv | --+ |
| 26 |
* +---------------------+ +----------------+ | |
| 27 |
* ^-------------------+ | |
| 28 |
* v----------------------------------------+ |
| 29 |
* +----------------------------+ | |
| 30 |
* DTV |length|gen|dtv[1]|dtv[2]|...| | |
| 31 |
* +----------------------------+ | |
| 32 |
* dtv[n] | | |
| 33 |
* +---------------------+ | |
| 34 |
* |unsigned long counter| | |
| 35 |
* |struct dtv_pointer { | | |
| 36 |
* | void *val |->------+ |
| 37 |
* | void *to_free} | |
| 38 |
* +---------------------| |
| 39 |
*/ |
| 40 |
|
| 41 |
global tls_debug = 0 |
| 42 |
private global tls_link_map_name |
| 43 |
private global tls_module = 0 |
| 44 |
|
| 45 |
%{ |
| 46 |
# if defined(__DYNINST__) |
| 47 |
# if defined(__i386) || defined(__x86_64) |
| 48 |
# include <asm/prctl.h> |
| 49 |
# include <sys/prctl.h> |
| 50 |
# endif |
| 51 |
# endif |
| 52 |
%} |
| 53 |
|
| 54 |
// Get the thread context pointer that points to the tls thread control block |
| 55 |
@__private30 function get_thread_context_pointer (tls_thread_reg_val:long) %{ |
| 56 |
# ifdef __i386 |
| 57 |
{ |
| 58 |
unsigned long gs; |
| 59 |
# if defined(__KERNEL__) |
| 60 |
rdmsrl(MSR_FS_BASE, gs); |
| 61 |
# elif defined(__DYNINST__) |
| 62 |
int arch_prctl(int code, unsigned long *addr); |
| 63 |
arch_prctl(ARCH_GET_FS, &gs); |
| 64 |
# endif |
| 65 |
STAP_RETVALUE = gs; |
| 66 |
} |
| 67 |
# elif defined __x86_64 |
| 68 |
{ |
| 69 |
unsigned long fs; |
| 70 |
#if defined(__KERNEL__) |
| 71 |
rdmsrl(MSR_FS_BASE, fs); |
| 72 |
# elif defined(__DYNINST__) |
| 73 |
int arch_prctl(int code, unsigned long *addr); |
| 74 |
arch_prctl(ARCH_GET_FS, &fs); |
| 75 |
# endif |
| 76 |
STAP_RETVALUE = fs; |
| 77 |
} |
| 78 |
# elif defined __s390x__ |
| 79 |
{ |
| 80 |
unsigned long tlsval; |
| 81 |
asm volatile ("ear %0,%%a0" : "=r" (tlsval)); |
| 82 |
asm volatile ("sllg %0,%0,32" : "=r" (tlsval)); |
| 83 |
asm volatile ("ear %0,%%a1" : "=r" (tlsval)); |
| 84 |
STAP_RETVALUE = tlsval; |
| 85 |
} |
| 86 |
# elif defined __aarch64__ |
| 87 |
{ |
| 88 |
unsigned long tlsval; |
| 89 |
asm("mrs %0,tpidr_el0" : "=r" (tlsval)); |
| 90 |
STAP_RETVALUE = tlsval; |
| 91 |
} |
| 92 |
# elif defined __powerpc64__ |
| 93 |
{ |
| 94 |
unsigned long tcb; |
| 95 |
asm("subi %0,%1,28688," : "=r" (tcb) : "r" (STAP_ARG_tls_thread_reg_val)); // -0x7010 |
| 96 |
STAP_RETVALUE = tcb; |
| 97 |
} |
| 98 |
# endif |
| 99 |
%} |
| 100 |
|
| 101 |
|
| 102 |
// Get inode of followed link |
| 103 |
// TODO Does not follow symbolic links, e.g. to follow /lib64/libc.so.6 |
| 104 |
// The idea was to do 'inode = get_inode_from_path (l_name); name = inode_path(inode);' |
| 105 |
|
| 106 |
@__private30 function get_inode_from_path:long (tls_module_name) %{ |
| 107 |
struct inode *tls_inode; |
| 108 |
struct path tls_path; |
| 109 |
int code; |
| 110 |
code = kern_path(/* string */ STAP_ARG_tls_module_name, LOOKUP_FOLLOW, &tls_path); |
| 111 |
if (code == 0) |
| 112 |
STAP_RETVALUE = (int64_t)tls_path.dentry->d_inode; |
| 113 |
else |
| 114 |
STAP_RETVALUE = 0; |
| 115 |
%} |
| 116 |
|
| 117 |
|
| 118 |
// Return basename of a path |
| 119 |
|
| 120 |
@__private30 function basename (l_name:string) |
| 121 |
{ |
| 122 |
for (lni = 0; lni < strlen (l_name); lni++) { |
| 123 |
if (stringat(l_name, lni) == 0x2f) // '/' |
| 124 |
slash = lni; |
| 125 |
} |
| 126 |
if (slash > 0) { |
| 127 |
slash += 1; |
| 128 |
return substr (l_name, slash, strlen(l_name)-slash); |
| 129 |
} |
| 130 |
else |
| 131 |
return l_name; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// Setup the link_map module/l_name mapping |
| 136 |
|
| 137 |
@__private30 function setup_link_map () |
| 138 |
{ |
| 139 |
delete tls_link_map_name |
| 140 |
%( arch == "x86_64" %? |
| 141 |
l_map_count = @var("_rtld_global","/usr/lib64/ld-linux-x86-64.so.2")->_dl_nns |
| 142 |
// TODO Do namespaces need to be supported? assume [0] |
| 143 |
l_map = @var("_rtld_global","/usr/lib64/ld-linux-x86-64.so.2")->_dl_ns[0]->_ns_loaded; |
| 144 |
if (tls_debug > 1) |
| 145 |
println ("l_map=", @var("_rtld_global","/usr/lib64/ld-linux-x86-64.so.2")->_dl_ns[0]->_ns_loaded$); |
| 146 |
%) |
| 147 |
%( arch == "powerpc" %? |
| 148 |
l_map_count = @var("_rtld_global","/usr/lib64/ld64.so.2")->_dl_nns |
| 149 |
l_map = @var("_rtld_global","/usr/lib64/ld64.so.2")->_dl_ns[0]->_ns_loaded; |
| 150 |
if (tls_debug > 1) |
| 151 |
println ("l_map=", @var("_rtld_global","/usr/lib64/ld64.so.2")->_dl_ns[0]->_ns_loaded$); |
| 152 |
%) |
| 153 |
%( arch == "s390" %? |
| 154 |
l_map_count = @var("_rtld_global","/usr/lib/ld64.so.1")->_dl_nns |
| 155 |
l_map = @var("_rtld_global","/usr/lib/ld64.so.1")->_dl_ns[0]->_ns_loaded; |
| 156 |
if (tls_debug > 1) |
| 157 |
println ("l_map=", @var("_rtld_global","/usr/lib/ld64.so.1")->_dl_ns[0]->_ns_loaded$); |
| 158 |
%) |
| 159 |
|
| 160 |
while (l_map) |
| 161 |
{ |
| 162 |
l_name = user_string(l_map->l_name); |
| 163 |
if (l_name == "") { |
| 164 |
l_map = l_map->l_next |
| 165 |
continue |
| 166 |
} |
| 167 |
l_addr = user_uint64(l_map->l_addr); |
| 168 |
l_tls_modid = l_map->l_tls_modid |
| 169 |
name_base = basename (l_name); |
| 170 |
if (l_tls_modid != 0) { |
| 171 |
tls_link_map_name[l_tls_modid] = name_base; |
| 172 |
if (tls_debug) |
| 173 |
printf("link_map: %s l_addr=%#lx modid=%d\n", tls_link_map_name[l_tls_modid], l_addr, l_tls_modid); |
| 174 |
} |
| 175 |
l_map = l_map->l_next |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
// Compare module names. |
| 181 |
// sonames, real sonames, and linker sonames should compare equal |
| 182 |
|
| 183 |
@__private30 function regex_module_compare (stap_mod:string, map_mod:string) |
| 184 |
{ |
| 185 |
// Not an soname |
| 186 |
if (stap_mod !~ "^lib.*$") |
| 187 |
return stap_mod == map_mod; |
| 188 |
|
| 189 |
// Most common case libMATCH-1.soMATCH-2 |
| 190 |
// MATCH-1 is the soname minus lib prefix and .so* suffix |
| 191 |
if (stap_mod !~ "lib([-\._\+a-zA-Z0-9]+)\.so([\.0-9]*)") |
| 192 |
return 0; |
| 193 |
stap_lib = matched(1); |
| 194 |
|
| 195 |
if (map_mod !~ "lib([-\._\+a-zA-Z0-9]+)\.so([\.0-9]*)") |
| 196 |
return 0; |
| 197 |
map_lib = matched(1); |
| 198 |
if (stap_lib == map_lib) |
| 199 |
return 1; |
| 200 |
|
| 201 |
// The above will miss e.g. libc.so.6/libc-2.31.so |
| 202 |
// MATCH-1 is the soname minus lib prefix and -version.so suffix |
| 203 |
if (stap_mod !~ "lib([_\+a-zA-Z0-9]+)([-\.0-9]*)\.so([\.0-9]*)") |
| 204 |
return 0; |
| 205 |
stap_lib = matched(1); |
| 206 |
if (map_mod !~ "lib([_\+a-zA-Z0-9]+)([-\.0-9]*)\.so([\.0-9]*)") |
| 207 |
return 0; |
| 208 |
map_lib = matched(1); |
| 209 |
if (stap_lib == map_lib) |
| 210 |
return 1; |
| 211 |
else |
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
// Find the module by matching the probe name to the link_map name |
| 217 |
|
| 218 |
@__private30 function set_tls_module_by_name (tls_module:string) |
| 219 |
{ |
| 220 |
matched_proc = basename(tls_module); |
| 221 |
|
| 222 |
if (tls_debug) |
| 223 |
printf("Looking for %s in link_map\n", matched_proc); |
| 224 |
foreach (lm in tls_link_map_name) { |
| 225 |
if (lm==0) continue; |
| 226 |
if (regex_module_compare (matched_proc, tls_link_map_name[lm])) { |
| 227 |
if (tls_debug) |
| 228 |
printf("Name Match: %s \n", tls_link_map_name[lm]); |
| 229 |
return lm; |
| 230 |
} |
| 231 |
} |
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* sfunction __push_tls_address - Handle implicit tls variable relocation |
| 238 |
* |
| 239 |
* Description: Called for DW_OP_GNU_push_tls_address relocation from loc2stap.css::location_context::translate |
| 240 |
*/ |
| 241 |
|
| 242 |
function __push_tls_address (tls_var_offset:long, tls_module:string) |
| 243 |
{ |
| 244 |
%( arch == "powerpc" %? |
| 245 |
// It seems the value of r13 cannot be dependably relied on in get_thread_context_pointer |
| 246 |
r13 = register("r13") |
| 247 |
%: |
| 248 |
r13 = 0 |
| 249 |
%) |
| 250 |
tcbhead = get_thread_context_pointer (r13); |
| 251 |
if (tls_debug > 1) |
| 252 |
printf ("tls_module=%s tcbhead=%#lx/%d\n", tls_module, tcbhead, tcbhead); |
| 253 |
setup_link_map (); |
| 254 |
module = set_tls_module_by_name (tls_module); |
| 255 |
if (module >= 0) |
| 256 |
{ |
| 257 |
// dtv[-1] is module count |
| 258 |
// dtv[0] is generation count |
| 259 |
// dtv[1] is module[0] info, the main executable |
| 260 |
// dtv[2..n] is module info for dependent shared objects |
| 261 |
dtv$pointer$val = @cast(tcbhead, "tcbhead", "<linux/stp_tls.h>")->dtv[module]->pointer->val; |
| 262 |
if (tls_debug > 1) |
| 263 |
printf ("dtv$pointer$val=%#lx, tls_var_offset=%#lx, module=%d\n", dtv$pointer$val, tls_var_offset, module); |
| 264 |
return dtv$pointer$val + tls_var_offset; |
| 265 |
} |
| 266 |
else |
| 267 |
return 0; |
| 268 |
} |