| 1 |
// uconversions tapset -- BPF version |
| 2 |
// Copyright (C) 2018-2022 Red Hat Inc. |
| 3 |
// |
| 4 |
// This file is part of systemtap, and is free software. You can |
| 5 |
// redistribute it and/or modify it under the terms of the GNU General |
| 6 |
// Public License (GPL); either version 2, or (at your option) any |
| 7 |
// later version. |
| 8 |
|
| 9 |
// TODO: Really need a solution for redundant docstrings. |
| 10 |
// TODO: Use jump_to_catch/register_error for user_long{,_warn} type functions. |
| 11 |
|
| 12 |
function user_string_n:string (addr:long, n:long) |
| 13 |
{ /* pure */ /* bpf */ |
| 14 |
// TODO: Does not provide address in error message. |
| 15 |
return user_string_n(addr, n, "user string copy fault") |
| 16 |
} |
| 17 |
|
| 18 |
function user_string_n:string (addr:long, n:long, err_msg:string) |
| 19 |
%( 1 == 0 %? |
| 20 |
{ /* pure */ /* bpf */ |
| 21 |
/* !!! ACHTUNG !!! |
| 22 |
* bpf uses the same bpf_probe_read() helpers for kernel and user |
| 23 |
* addresses, on the assumption that the address spaces coincide. |
| 24 |
* Which only really works on x86_64 in Current Day. |
| 25 |
* |
| 26 |
* If the address space is changed, it may return the wrong data. |
| 27 |
* TODO PR25168: Fix this as soon as BPF ships proper, separate |
| 28 |
* bpf_probe_read_{user,kernel}() helpers. |
| 29 |
*/ |
| 30 |
%( arch == "x86_64" %? |
| 31 |
// TODO: Does not use the provided err_msg. |
| 32 |
return kernel_string_n(addr, n) // calls probe_read_str() |
| 33 |
%: |
| 34 |
// TODO: Use error() function. |
| 35 |
print("ERROR(unsupported): %s", err_msg) |
| 36 |
exit() |
| 37 |
%) |
| 38 |
} |
| 39 |
%: |
| 40 |
%{ /* bpf */ /* pure */ |
| 41 |
/* if (n > BPF_MAXSTRINGLEN) n = BPF_MAXSTRINGLEN; */ |
| 42 |
jle $n, BPF_MAXSTRINGLEN, skip |
| 43 |
mov $n, BPF_MAXSTRINGLEN |
| 44 |
|
| 45 |
/* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN); |
| 46 |
buf[0] = 0x0; // guarantee NUL byte |
| 47 |
rc = bpf_probe_read_user_str(buf, n, addr); */ |
| 48 |
skip: alloc, $buf, BPF_MAXSTRINGLEN |
| 49 |
stw [$buf+0], 0 /* guarantee NUL byte */ |
| 50 |
call $rc, probe_read_user_str, $buf, $n, $addr |
| 51 |
call -, printf, "hello from the new bpf assembler" |
| 52 |
/* TODO <- should work if the helper is named bpf_probe_read_user_str() too */ |
| 53 |
/* TODO <- substitute probe_read_str automatically for older bpf? */ |
| 54 |
|
| 55 |
/* if (rc < 0) error("...", addr); */ |
| 56 |
jsge $rc, 0, done |
| 57 |
call -, printf, "ERROR: string copy fault at %p [man error::fault]\n", $addr |
| 58 |
/* TODO <- document stapbpf version of error::fault */ |
| 59 |
/* TODO <- return value reg should be optional */ |
| 60 |
/* TODO <- use jump_to_catch error handling? */ |
| 61 |
call -, printf, "%s", $err_msg |
| 62 |
call -, exit |
| 63 |
|
| 64 |
/* return buf; */ |
| 65 |
done: mov $$, $buf |
| 66 |
%} |
| 67 |
%) |
| 68 |
|
| 69 |
function user_string_n_warn:string (addr:long, n:long, warn_msg:string) |
| 70 |
%( 1 == 1 %? |
| 71 |
{ /* pure */ /* bpf */ |
| 72 |
/* !!! ACHTUNG !!! |
| 73 |
* bpf uses the same bpf_probe_read() helpers for kernel and user |
| 74 |
* addresses, on the assumption that the address spaces coincide. |
| 75 |
* Which only really works on x86_64 in Current Day. |
| 76 |
* |
| 77 |
* If the address space is changed, it may return the wrong data. |
| 78 |
* TODO PR25168: Fix this as soon as BPF ships proper, separate |
| 79 |
* bpf_probe_read_{user,kernel}() helpers. |
| 80 |
*/ |
| 81 |
%( arch == "x86_64" %? |
| 82 |
return kernel_string_n(addr, n, warn_msg) // calls probe_read_str() |
| 83 |
%: |
| 84 |
return warn_msg // don't even bother |
| 85 |
%) |
| 86 |
} |
| 87 |
%: |
| 88 |
%{ /* bpf */ /* pure */ |
| 89 |
/* buf = bpf_stk_alloc(BPF_MAXSTRINGLEN); |
| 90 |
buf[0] = 0x0; // guarantee NUL byte |
| 91 |
rc = bpf_probe_read_user_str(buf, BPF_MAXSTRINGLEN, addr); */ |
| 92 |
alloc, $buf, BPF_MAXSTRINGLEN; |
| 93 |
0x62, $buf, -, -, 0x0; /* stw [$buf+0], 0x0 -- guarantee NUL byte */ |
| 94 |
call, $rc, probe_read_user_str, $buf, BPF_MAXSTRINGLEN, $addr; |
| 95 |
|
| 96 |
/* if (rc < 0) return err_msg; |
| 97 |
return buf; */ |
| 98 |
0xc5, $rc, -, _err, 0; /* jslt $rc, 0, _err */ |
| 99 |
0xbf, $$, $buf, -, -; /* mov $$, $buf */ |
| 100 |
0x05, -, -, _done, -; /* ja _done; */ |
| 101 |
|
| 102 |
label, _err; |
| 103 |
0xbf, $$, $warn_msg, -, -; /* mov $$, $warn_msg */ |
| 104 |
|
| 105 |
label, _done; |
| 106 |
%} |
| 107 |
%) |
| 108 |
|
| 109 |
function __bpf_probe_read_user_error:long (addr:long, size:long) |
| 110 |
%{ /* bpf */ /* pure */ |
| 111 |
/* if (size > 8) error("...", addr, size); */ |
| 112 |
0xb5, $size, -, _skip, 8; /* jle $n, 8, _skip */ |
| 113 |
call, -, printf, "ERROR: attempted to read %ul (>8) bytes from %p into unsigned long\n", $size, $addr; |
| 114 |
call, -, exit; |
| 115 |
|
| 116 |
label, _skip; |
| 117 |
/* buf = bpf_stk_alloc(8); // size <= 8 |
| 118 |
*buf = (unsigned long)0x0; // guarantee leading zeroes |
| 119 |
rc = bpf_probe_read(buf, size, addr); |
| 120 |
*/ |
| 121 |
alloc, $buf, 8; |
| 122 |
0x7a, $buf, -, -, 0x0; /* stdw [buf+0], 0 -- guarantee leading zeroes */ |
| 123 |
/* TODO PR25168: probe_read is used for both kernel and user memory. |
| 124 |
BPF will soon deprecate these in favour of separate functions. */ |
| 125 |
call, $rc, probe_read, $buf, $size, $addr; |
| 126 |
/* call, $rc, probe_read_user, $buf, $size, $addr; */ |
| 127 |
/* XXX code for testing LITTLE ENDIAN / BIG ENDIAN */ |
| 128 |
/* 0xb7, $rc, -, -, 0x0; |
| 129 |
0x62, $buf, -, -, 0xdeadbeef; |
| 130 |
0x62, $buf, -, 4, 0xea7b3375; /* end test */ |
| 131 |
|
| 132 |
/* if (rc < 0) error("...", addr); */ |
| 133 |
0x75, $rc, -, _done, 0; /* jsge $rc, 0, _done */ |
| 134 |
call, -, printf, "ERROR: user copy fault at %p [man error::fault]\n", $addr; /* TODO document stapbpf version of error::fault */ |
| 135 |
call, -, exit; |
| 136 |
|
| 137 |
label, _done; |
| 138 |
/* return *(unsigned long *)buf; */ |
| 139 |
0x79, $$, $buf, 0x0, -; /* ldxdw $$, buf */ |
| 140 |
%} |
| 141 |
// XXX I assumed I would need to return buf >> 8*(8-size) here, |
| 142 |
// but buf is already correctly aligned for smaller sizes. Hmm. |
| 143 |
|
| 144 |
function __signext:long (src:long, size:long) { /* pure */ /* bpf */ |
| 145 |
// XXX 2'S COMPLEMENT; assumes upper bits of size are zero |
| 146 |
// as provided by __bpf_probe_read_user_error() |
| 147 |
// thanks to https://graphics.stanford.edu/~seander/bithacks |
| 148 |
bits = 8*size |
| 149 |
mask = 1 << (bits-1) |
| 150 |
return (src ^ mask) - mask |
| 151 |
} |
| 152 |
|
| 153 |
function __bpf_probe_read_user_error_signed (addr:long, size:long) { /* pure */ /* bpf */ |
| 154 |
return __signext(__bpf_probe_read_user_error(addr, size), size) |
| 155 |
} |
| 156 |
|
| 157 |
// TODO We can't be sure of exact lengths of {char,short,int,long} |
| 158 |
// so the best guess for now is 1,2,4,8 to match the BPF definitions. |
| 159 |
// However, I'm pretty sure sizeof.stp uses some @cast() trickery to |
| 160 |
// get the size of a data type. Should we capture it as a macro? |
| 161 |
|
| 162 |
function user_char_error:long (addr:long) { /* pure */ /* bpf */ |
| 163 |
// XXX: Note that the lkm version is also reading a signed char, |
| 164 |
// which gets sign extended to a long just like this. I was surprised. |
| 165 |
return __bpf_probe_read_user_error_signed(addr, 1) |
| 166 |
} |
| 167 |
|
| 168 |
function user_short_error:long (addr:long) { /* pure */ /* bpf */ |
| 169 |
return __bpf_probe_read_user_error_signed(addr, 2) |
| 170 |
} |
| 171 |
|
| 172 |
function user_ushort_error:long (addr:long) { /* pure */ /* bpf */ |
| 173 |
return __bpf_probe_read_user_error(addr, 2) |
| 174 |
} |
| 175 |
|
| 176 |
function user_int_error:long (addr:long) { /* pure */ /* bpf */ |
| 177 |
return __bpf_probe_read_user_error_signed(addr, 4) |
| 178 |
} |
| 179 |
|
| 180 |
function user_long_error:long (addr:long) { /* pure */ /* bpf */ |
| 181 |
/* XXX code for testing LITTLE ENDIAN / BIG ENDIAN */ |
| 182 |
/* printf("1: %lx\n2: %x\n4: %lx\n8: %lx\n", |
| 183 |
__bpf_probe_read_user_error(addr, 1), |
| 184 |
__bpf_probe_read_user_error(addr, 2), |
| 185 |
__bpf_probe_read_user_error(addr, 4), |
| 186 |
__bpf_probe_read_user_error(addr, 8)) /* end test */ |
| 187 |
return __bpf_probe_read_user_error(addr, 8) // XXX no sign extension needed |
| 188 |
} |
| 189 |
|
| 190 |
function user_ulong_error:long (addr:long) { /* pure */ /* bpf */ |
| 191 |
return __bpf_probe_read_user_error(addr, 8) |
| 192 |
} |
| 193 |
|
| 194 |
function user_int8_error:long (addr:long) { /* pure */ /* bpf */ |
| 195 |
return __bpf_probe_read_user_error_signed(addr, 1) |
| 196 |
} |
| 197 |
|
| 198 |
function user_uint8_error:long (addr:long) { /* pure */ /* bpf */ |
| 199 |
return __bpf_probe_read_user_error(addr, 1) |
| 200 |
} |
| 201 |
|
| 202 |
function user_int16_error:long (addr:long) { /* pure */ /* bpf */ |
| 203 |
return __bpf_probe_read_user_error_signed(addr, 2) |
| 204 |
} |
| 205 |
|
| 206 |
function user_uint16_error:long (addr:long) { /* pure */ /* bpf */ |
| 207 |
return __bpf_probe_read_user_error(addr, 2) |
| 208 |
} |
| 209 |
|
| 210 |
function user_int32_error:long (addr:long) { /* pure */ /* bpf */ |
| 211 |
return __bpf_probe_read_user_error_signed(addr, 4) |
| 212 |
} |
| 213 |
|
| 214 |
function user_uint32_error:long (addr:long) { /* pure */ /* bpf */ |
| 215 |
return __bpf_probe_read_user_error(addr, 4) |
| 216 |
} |
| 217 |
|
| 218 |
function user_int64_error:long (addr:long) { /* pure */ /* bpf */ |
| 219 |
return __bpf_probe_read_user_error(addr, 8) // XXX no sign extension needed |
| 220 |
} |
| 221 |
|
| 222 |
function user_uint64_error:long (addr:long) { /* pure */ /* bpf */ |
| 223 |
return __bpf_probe_read_user_error_(addr, 8) |
| 224 |
} |