Repository navigation
Expand file tree
/
Copy pathinsn.lua
More file actions
322 lines (280 loc) · 10.8 KB
/
Copy pathinsn.lua
File metadata and controls
322 lines (280 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
--
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
--
---
-- eBPF instructions, and the buffer the translator emits them into.
--
-- A buffer holds records rather than bytes, so a jump can name a label placed later and a call
-- can name a function laid out afterwards; `pack` resolves both against the positions the
-- assembler hands it. Every record carries the Lua line it came from, which is what the
-- `line_info` writer reads.
-- @module luaebpf.insn
local class = require("lunatik.class")
-- instruction classes and fields: uapi/linux/bpf.h, uapi/linux/bpf_common.h
local LD <const> = 0x00
local LDX <const> = 0x01
local ST <const> = 0x02
local STX <const> = 0x03
local ALU <const> = 0x04
local JMP <const> = 0x05
local ALU64 <const> = 0x07
local W <const> = 0x00
local H <const> = 0x08
local B <const> = 0x10
local DW <const> = 0x18
local MEM <const> = 0x60
local SRC_X <const> = 0x08
local END <const> = 0xd0
local TO_BE <const> = 0x08
local PSEUDO_CALL <const> = 1
local MAY_GOTO <const> = 0
local SIZE <const> = 8
local WORD <const> = 8
local BYTE <const> = 8
-- bpf_insn's dst_reg and src_reg share a byte as bitfields, the first declared low on a little-endian machine
local NIBBLE <const> = 4
-- the field an access of that many bytes carries, BPF_B and friends
local widths = {[1] = B, [2] = H, [4] = W, [8] = DW}
-- string.pack's i4 takes a signed word; the halves of a 64-bit immediate are unsigned
local function word(value)
value = value & 0xffffffff
if value >= 0x80000000 then
return value - 0x100000000
end
return value
end
local insn = {}
--- ALU opcodes, `BPF_ADD` and friends.
-- @table luaebpf.insn.alu
insn.alu = {
ADD = 0x00, SUB = 0x10, MUL = 0x20, DIV = 0x30, OR = 0x40, AND = 0x50,
LSH = 0x60, RSH = 0x70, NEG = 0x80, MOD = 0x90, XOR = 0xa0, MOV = 0xb0, ARSH = 0xc0,
}
--- Jump opcodes, `BPF_JEQ` and friends. Lua integers are signed, so a comparison the source
-- wrote takes a signed relation. The unsigned ones are for the bounds check before a packet
-- load, where the verifier reads the relation itself: a packet pointer and an offset are
-- unsigned there, and a signed test would leave the pointer's range unproven.
-- @table luaebpf.insn.jump
insn.jump = {
JA = 0x00, JEQ = 0x10, JGT = 0x20, JGE = 0x30, JNE = 0x50, JSGT = 0x60, JSGE = 0x70,
JLT = 0xa0, JLE = 0xb0, JSLT = 0xc0, JSLE = 0xd0, JCOND = 0xe0, CALL = 0x80, EXIT = 0x90,
}
--- Registers: `R0` to `R10`, with `FP` as the read-only frame pointer.
-- @table luaebpf.insn.reg
insn.reg = {R0 = 0, R1 = 1, R2 = 2, R3 = 3, R4 = 4, R5 = 5, R6 = 6, R7 = 7, R8 = 8, R9 = 9, FP = 10}
--- Bytes one instruction takes in the object.
-- @field luaebpf.insn.SIZE
insn.SIZE = SIZE
---
-- Whether a load or a store takes `n` bytes: eBPF has one of 1, 2, 4 and 8, so whatever a compiled
-- function reads as one number is one of those widths.
-- @function luaebpf.insn.loadable
-- @tparam integer n
-- @treturn boolean
function insn.loadable(n)
return widths[n] ~= nil
end
---
-- A code buffer.
-- @type luaebpf.insn.code
local code = class{}
local function append(self, record)
self.n = self.n + 1
record.line = self.line
self.records[self.n] = record
return self.n
end
---
-- A fresh, empty code buffer.
-- @function luaebpf.insn.new
-- @treturn luaebpf.insn.code
function insn.new()
return code:new{n = 0, records = {}, labels = {}, nlabels = 0, line = 0}
end
---
-- The Lua source line the instructions emitted from now on come from.
-- @function luaebpf.insn.code:source
-- @tparam integer line
function code:source(line)
self.line = line
end
---
-- A label, to be placed with `place` and named by a jump.
-- @function luaebpf.insn.code:label
-- @treturn integer the label
function code:label()
self.nlabels = self.nlabels + 1
return self.nlabels
end
---
-- Binds `label` to the position the next instruction takes.
-- @function luaebpf.insn.code:place
-- @tparam integer label
function code:place(label)
self.labels[label] = self.n
end
--- `dst := dst op src`, on 64 bits.
-- @function luaebpf.insn.code:alu
function code:alu(op, dst, src)
return append(self, {code = ALU64 | op | SRC_X, dst = dst, src = src, off = 0, imm = 0})
end
--- `dst := dst op imm`, on 64 bits; `imm` must fit in 32 signed bits.
-- @function luaebpf.insn.code:alui
function code:alui(op, dst, imm)
return append(self, {code = ALU64 | op, dst = dst, src = 0, off = 0, imm = imm})
end
--- `dst := -dst`; eBPF's negation is a one-operand ALU op, so it takes no source.
-- @function luaebpf.insn.code:neg
function code:neg(dst)
return self:alui(insn.alu.NEG, dst, 0)
end
--- `dst := imm`, over the whole 64-bit range.
-- @function luaebpf.insn.code:set
function code:set(dst, imm)
if imm >= -0x80000000 and imm <= 0x7fffffff then
return self:alui(insn.alu.MOV, dst, imm)
end
local at = append(self, {code = LD | DW, dst = dst, src = 0, off = 0, imm = word(imm)})
append(self, {code = 0, dst = 0, src = 0, off = 0, imm = word(imm >> 32)})
return at
end
--- `dst := *(u<8n> *)(src + off)`, zero-extended; `n` is eight bytes by default.
-- @function luaebpf.insn.code:load
function code:load(dst, src, off, n)
return append(self, {code = LDX | widths[n or WORD] | MEM, dst = dst, src = src, off = off, imm = 0})
end
--- `dst := the low n bytes of dst, read big-endian`: a byte swap on a little-endian kernel and the
-- low bytes alone on a big-endian one, zero-extended either way; `n` is 2, 4 or 8.
-- @function luaebpf.insn.code:swap
function code:swap(dst, n)
return append(self, {code = ALU | END | TO_BE, dst = dst, src = 0, off = 0, imm = n * BYTE})
end
--- `*(u<8n> *)(dst + off) := src`; `n` is eight bytes by default.
-- @function luaebpf.insn.code:store
function code:store(dst, off, src, n)
return append(self, {code = STX | widths[n or WORD] | MEM, dst = dst, src = src, off = off, imm = 0})
end
--- `*(u64 *)(dst + off) := imm`; `imm` must fit in 32 signed bits.
-- @function luaebpf.insn.code:storei
function code:storei(dst, off, imm)
return append(self, {code = ST | DW | MEM, dst = dst, src = 0, off = off, imm = imm})
end
--- Jumps to `label` when `dst op src` holds.
-- @function luaebpf.insn.code:branch
function code:branch(op, dst, src, label)
return append(self, {code = JMP | op | SRC_X, dst = dst, src = src, imm = 0, target = label})
end
--- Jumps to `label` when `dst op imm` holds.
-- @function luaebpf.insn.code:branchi
function code:branchi(op, dst, imm, label)
return append(self, {code = JMP | op, dst = dst, src = 0, imm = imm, target = label})
end
--- An unconditional jump to `label`.
-- @function luaebpf.insn.code:jump
function code:jump(label)
return append(self, {code = JMP | insn.jump.JA, dst = 0, src = 0, imm = 0, target = label})
end
--- The `may_goto` header that buys a loop the verifier cannot bound its iteration budget.
-- @function luaebpf.insn.code:maygoto
function code:maygoto(label)
return append(self, {code = JMP | insn.jump.JCOND, dst = 0, src = MAY_GOTO, imm = 0, target = label})
end
--- A BPF-to-BPF call to the function registered under `name`.
-- @function luaebpf.insn.code:call
function code:call(name)
return append(self, {code = JMP | insn.jump.CALL, dst = 0, src = PSEUDO_CALL, off = 0, imm = -1, call = name})
end
--- A call to the kernel helper of that number.
-- @function luaebpf.insn.code:helper
function code:helper(number)
return append(self, {code = JMP | insn.jump.CALL, dst = 0, src = 0, off = 0, imm = number})
end
--- A call to the kfunc of that name. The object carries a relocation against the kfunc's undefined
-- symbol, and libbpf writes the source register, the immediate and the offset from the BTF it
-- resolved the kfunc in.
-- @function luaebpf.insn.code:kfunc
function code:kfunc(name)
return append(self, {code = JMP | insn.jump.CALL, dst = 0, src = 0, off = 0, imm = -1, kfunc = name})
end
--- `dst := the map registered under `name``. The object carries a relocation against the map's
-- symbol, and libbpf replaces both the source register and the immediate with the map's file
-- descriptor, as it does for the `ld_imm64` clang emits.
-- @function luaebpf.insn.code:map
function code:map(dst, name)
local at = append(self, {code = LD | DW, dst = dst, src = 0, off = 0, imm = 0, map = name})
append(self, {code = 0, dst = 0, src = 0, off = 0, imm = 0})
return at
end
--- `dst := the address of `bytes``, which the object keeps in its `.data`, where the program may
-- write over them. The object carries a relocation against the symbol of those bytes, and libbpf
-- writes the map it makes of `.data` and the symbol's offset in its value, as it does for the
-- `ld_imm64` clang emits for a global variable.
-- @function luaebpf.insn.code:data
function code:data(dst, bytes)
local at = append(self, {code = LD | DW, dst = dst, src = 0, off = 0, imm = 0, data = bytes})
append(self, {code = 0, dst = 0, src = 0, off = 0, imm = 0})
return at
end
--- Returns from the function, with the value in `R0`.
-- @function luaebpf.insn.code:exit
function code:exit()
return append(self, {code = JMP | insn.jump.EXIT, dst = 0, src = 0, off = 0, imm = 0})
end
---
-- Number of instructions in the buffer.
-- @function luaebpf.insn.code:len
-- @treturn integer
function code:len()
return self.n
end
---
-- The Lua line each instruction came from, indexed as the buffer is.
-- @function luaebpf.insn.code:sourcelines
-- @treturn table
function code:sourcelines()
local lines = {}
for i = 1, self.n do
lines[i] = self.records[i].line
end
return lines
end
---
-- Where each relocation of one kind sits and what it names, for the object to record.
-- @function luaebpf.insn.code:relocations
-- @tparam string kind `"call"` for a BPF-to-BPF call, `"map"` for a map reference, `"kfunc"` for a
-- call to a kfunc, `"data"` for the address of bytes in `.data`, named by those bytes
-- @treturn table `{at, name}` entries, `at` an index into the buffer
function code:relocations(kind)
local found = {}
for i = 1, self.n do
local name = self.records[i][kind]
if name ~= nil then
table.insert(found, {at = i, name = name})
end
end
return found
end
---
-- The buffer as object bytes, with every jump resolved. A call keeps the `-1` its relocation
-- replaces, as the compiler libbpf was built for leaves it.
-- @function luaebpf.insn.code:pack
-- @tparam boolean bigendian whether the bytes are written big-endian
-- @treturn string
function code:pack(bigendian)
local format = (bigendian and ">" or "<") .. "I1I1i2i4"
local dstshift = bigendian and NIBBLE or 0
local srcshift = NIBBLE - dstshift
local bytes = {}
for i = 1, self.n do
local record = self.records[i]
local off = record.off or 0
if record.target ~= nil then
off = self.labels[record.target] - i
end
table.insert(bytes, string.pack(format, record.code,
(record.dst << dstshift) | (record.src << srcshift), off, record.imm))
end
return table.concat(bytes)
end
return insn