| 1 | /* SPDX-License-Identifier: MIT |
| 2 | * |
| 3 | * lttng/lttng-bytecode.h |
| 4 | * |
| 5 | * LTTng modules bytecode header. |
| 6 | * |
| 7 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | */ |
| 9 | |
| 10 | #ifndef _LTTNG_BYTECODE_H |
| 11 | #define _LTTNG_BYTECODE_H |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | |
| 15 | #include <lttng/events.h> |
| 16 | #include <lttng/events-internal.h> |
| 17 | #include <lttng/bytecode.h> |
| 18 | |
| 19 | /* Interpreter stack length, in number of entries */ |
| 20 | #define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */ |
| 21 | #define INTERPRETER_STACK_EMPTY 1 |
| 22 | #define INTERPRETER_MAX_DATA_LEN 65536 |
| 23 | |
| 24 | #ifdef DEBUG |
| 25 | #define dbg_printk(fmt, args...) \ |
| 26 | printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \ |
| 27 | __FILE__, __func__, __LINE__, ## args) |
| 28 | #else |
| 29 | #define dbg_printk(fmt, args...) \ |
| 30 | do { \ |
| 31 | /* do nothing but check printf format */ \ |
| 32 | if (0) \ |
| 33 | printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \ |
| 34 | __FILE__, __func__, __LINE__, ## args); \ |
| 35 | } while (0) |
| 36 | #endif |
| 37 | |
| 38 | /* Linked bytecode. Child of struct lttng_kernel_bytecode_runtime. */ |
| 39 | struct bytecode_runtime { |
| 40 | struct lttng_kernel_bytecode_runtime p; |
| 41 | size_t data_len; |
| 42 | size_t data_alloc_len; |
| 43 | char *data; |
| 44 | uint16_t len; |
| 45 | char code[0]; |
| 46 | }; |
| 47 | |
| 48 | enum entry_type { |
| 49 | REG_S64, |
| 50 | REG_U64, |
| 51 | REG_DOUBLE, |
| 52 | REG_STRING, |
| 53 | REG_STAR_GLOB_STRING, |
| 54 | REG_TYPE_UNKNOWN, |
| 55 | REG_PTR, |
| 56 | }; |
| 57 | |
| 58 | enum load_type { |
| 59 | LOAD_ROOT_CONTEXT, |
| 60 | LOAD_ROOT_APP_CONTEXT, |
| 61 | LOAD_ROOT_PAYLOAD, |
| 62 | LOAD_OBJECT, |
| 63 | }; |
| 64 | |
| 65 | enum object_type { |
| 66 | OBJECT_TYPE_S8, |
| 67 | OBJECT_TYPE_S16, |
| 68 | OBJECT_TYPE_S32, |
| 69 | OBJECT_TYPE_S64, |
| 70 | OBJECT_TYPE_U8, |
| 71 | OBJECT_TYPE_U16, |
| 72 | OBJECT_TYPE_U32, |
| 73 | OBJECT_TYPE_U64, |
| 74 | |
| 75 | OBJECT_TYPE_SIGNED_ENUM, |
| 76 | OBJECT_TYPE_UNSIGNED_ENUM, |
| 77 | |
| 78 | OBJECT_TYPE_DOUBLE, |
| 79 | OBJECT_TYPE_STRING, |
| 80 | OBJECT_TYPE_STRING_SEQUENCE, |
| 81 | |
| 82 | OBJECT_TYPE_SEQUENCE, |
| 83 | OBJECT_TYPE_ARRAY, |
| 84 | OBJECT_TYPE_STRUCT, |
| 85 | OBJECT_TYPE_VARIANT, |
| 86 | |
| 87 | OBJECT_TYPE_DYNAMIC, |
| 88 | }; |
| 89 | |
| 90 | struct bytecode_get_index_data { |
| 91 | uint64_t offset; /* in bytes */ |
| 92 | size_t ctx_index; |
| 93 | size_t array_len; |
| 94 | /* |
| 95 | * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT |
| 96 | * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the |
| 97 | * interpreter needs to find it from the event fields and types to |
| 98 | * support variants. |
| 99 | */ |
| 100 | const struct lttng_kernel_event_field *field; |
| 101 | struct { |
| 102 | size_t len; |
| 103 | enum object_type type; |
| 104 | bool rev_bo; /* reverse byte order */ |
| 105 | } elem; |
| 106 | }; |
| 107 | |
| 108 | /* Validation stack */ |
| 109 | struct vstack_load { |
| 110 | enum load_type type; |
| 111 | enum object_type object_type; |
| 112 | const struct lttng_kernel_event_field *field; |
| 113 | bool rev_bo; /* reverse byte order */ |
| 114 | }; |
| 115 | |
| 116 | struct vstack_entry { |
| 117 | enum entry_type type; |
| 118 | struct vstack_load load; |
| 119 | }; |
| 120 | |
| 121 | struct vstack { |
| 122 | int top; /* top of stack */ |
| 123 | struct vstack_entry e[INTERPRETER_STACK_LEN]; |
| 124 | }; |
| 125 | |
| 126 | static inline |
| 127 | void vstack_init(struct vstack *stack) |
| 128 | { |
| 129 | stack->top = -1; |
| 130 | } |
| 131 | |
| 132 | static inline |
| 133 | struct vstack_entry *vstack_ax(struct vstack *stack) |
| 134 | { |
| 135 | if (unlikely(stack->top < 0)) |
| 136 | return NULL; |
| 137 | return &stack->e[stack->top]; |
| 138 | } |
| 139 | |
| 140 | static inline |
| 141 | struct vstack_entry *vstack_bx(struct vstack *stack) |
| 142 | { |
| 143 | if (unlikely(stack->top < 1)) |
| 144 | return NULL; |
| 145 | return &stack->e[stack->top - 1]; |
| 146 | } |
| 147 | |
| 148 | static inline |
| 149 | int vstack_push(struct vstack *stack) |
| 150 | { |
| 151 | if (stack->top >= INTERPRETER_STACK_LEN - 1) { |
| 152 | printk(KERN_WARNING "LTTng: filter: Stack full\n"); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | ++stack->top; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static inline |
| 160 | int vstack_pop(struct vstack *stack) |
| 161 | { |
| 162 | if (unlikely(stack->top < 0)) { |
| 163 | printk(KERN_WARNING "LTTng: filter: Stack empty\n"); |
| 164 | return -EINVAL; |
| 165 | } |
| 166 | stack->top--; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* Execution stack */ |
| 171 | enum estack_string_literal_type { |
| 172 | ESTACK_STRING_LITERAL_TYPE_NONE, |
| 173 | ESTACK_STRING_LITERAL_TYPE_PLAIN, |
| 174 | ESTACK_STRING_LITERAL_TYPE_STAR_GLOB, |
| 175 | }; |
| 176 | |
| 177 | struct load_ptr { |
| 178 | enum load_type type; |
| 179 | enum object_type object_type; |
| 180 | const void *ptr; |
| 181 | size_t nr_elem; |
| 182 | bool rev_bo; |
| 183 | /* Temporary place-holders for contexts. */ |
| 184 | union { |
| 185 | int64_t s64; |
| 186 | uint64_t u64; |
| 187 | double d; |
| 188 | } u; |
| 189 | const struct lttng_kernel_event_field *field; |
| 190 | }; |
| 191 | |
| 192 | struct estack_entry { |
| 193 | enum entry_type type; |
| 194 | union { |
| 195 | int64_t v; |
| 196 | |
| 197 | struct { |
| 198 | const char *str; |
| 199 | const char __user *user_str; |
| 200 | size_t seq_len; |
| 201 | enum estack_string_literal_type literal_type; |
| 202 | int user; /* is string from userspace ? */ |
| 203 | } s; |
| 204 | struct load_ptr ptr; |
| 205 | } u; |
| 206 | }; |
| 207 | |
| 208 | struct estack { |
| 209 | int top; /* top of stack */ |
| 210 | struct estack_entry e[INTERPRETER_STACK_LEN]; |
| 211 | }; |
| 212 | |
| 213 | #define estack_ax_v ax |
| 214 | #define estack_bx_v bx |
| 215 | |
| 216 | #define estack_ax_t ax_t |
| 217 | #define estack_bx_t bx_t |
| 218 | |
| 219 | #define estack_ax(stack, top) \ |
| 220 | ({ \ |
| 221 | BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \ |
| 222 | &(stack)->e[top]; \ |
| 223 | }) |
| 224 | |
| 225 | #define estack_bx(stack, top) \ |
| 226 | ({ \ |
| 227 | BUG_ON((top) <= INTERPRETER_STACK_EMPTY + 1); \ |
| 228 | &(stack)->e[(top) - 1]; \ |
| 229 | }) |
| 230 | |
| 231 | #define estack_push(stack, top, ax, bx, ax_t, bx_t) \ |
| 232 | do { \ |
| 233 | BUG_ON((top) >= INTERPRETER_STACK_LEN - 1); \ |
| 234 | (stack)->e[(top) - 1].u.v = (bx); \ |
| 235 | (stack)->e[(top) - 1].type = (bx_t); \ |
| 236 | (bx) = (ax); \ |
| 237 | (bx_t) = (ax_t); \ |
| 238 | ++(top); \ |
| 239 | } while (0) |
| 240 | |
| 241 | #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \ |
| 242 | do { \ |
| 243 | BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \ |
| 244 | (ax) = (bx); \ |
| 245 | (ax_t) = (bx_t); \ |
| 246 | (bx) = (stack)->e[(top) - 2].u.v; \ |
| 247 | (bx_t) = (stack)->e[(top) - 2].type; \ |
| 248 | (top)--; \ |
| 249 | } while (0) |
| 250 | |
| 251 | enum lttng_interpreter_type { |
| 252 | LTTNG_INTERPRETER_TYPE_S64, |
| 253 | LTTNG_INTERPRETER_TYPE_U64, |
| 254 | LTTNG_INTERPRETER_TYPE_SIGNED_ENUM, |
| 255 | LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM, |
| 256 | LTTNG_INTERPRETER_TYPE_DOUBLE, |
| 257 | LTTNG_INTERPRETER_TYPE_STRING, |
| 258 | LTTNG_INTERPRETER_TYPE_SEQUENCE, |
| 259 | }; |
| 260 | |
| 261 | /* |
| 262 | * Represents the output parameter of the lttng interpreter. |
| 263 | * Currently capturable field classes are integer, double, string and sequence |
| 264 | * of integer. |
| 265 | */ |
| 266 | struct lttng_interpreter_output { |
| 267 | enum lttng_interpreter_type type; |
| 268 | union { |
| 269 | int64_t s; |
| 270 | uint64_t u; |
| 271 | |
| 272 | struct { |
| 273 | const char *str; |
| 274 | size_t len; |
| 275 | } str; |
| 276 | struct { |
| 277 | const void *ptr; |
| 278 | size_t nr_elem; |
| 279 | |
| 280 | /* Inner type. */ |
| 281 | const struct lttng_kernel_type_common *nested_type; |
| 282 | } sequence; |
| 283 | } u; |
| 284 | }; |
| 285 | |
| 286 | const char *lttng_bytecode_print_op(enum bytecode_op op); |
| 287 | |
| 288 | void lttng_bytecode_sync_state(struct lttng_kernel_bytecode_runtime *runtime); |
| 289 | int lttng_bytecode_validate(struct bytecode_runtime *bytecode); |
| 290 | int lttng_bytecode_specialize(const struct lttng_kernel_event_desc *event_desc, |
| 291 | struct bytecode_runtime *bytecode); |
| 292 | |
| 293 | int lttng_bytecode_interpret_error(struct lttng_kernel_bytecode_runtime *bytecode_runtime, |
| 294 | const char *stack_data, |
| 295 | struct lttng_kernel_probe_ctx *probe_ctx, |
| 296 | void *ctx); |
| 297 | |
| 298 | int lttng_bytecode_interpret(struct lttng_kernel_bytecode_runtime *kernel_bytecode, |
| 299 | const char *interpreter_stack_data, |
| 300 | struct lttng_kernel_probe_ctx *lttng_probe_ctx, |
| 301 | void *caller_ctx); |
| 302 | |
| 303 | #endif /* _LTTNG_FILTER_H */ |