Debian Codegraph @

asm/opcode.go

raw symbols SCIP
1 package asm
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 //go:generate go tool stringer -output opcode_string.go -type=Class
9
10 // Class of operations
11 //
12 // msb lsb
13 // +---+--+---+
14 // | ?? |CLS|
15 // +---+--+---+
16 type Class uint8
17
18 const classMask OpCode = 0x07
19
20 const (
21 // LdClass loads immediate values into registers.
22 // Also used for non-standard load operations from cBPF.
23 LdClass Class = 0x00
24 // LdXClass loads memory into registers.
25 LdXClass Class = 0x01
26 // StClass stores immediate values to memory.
27 StClass Class = 0x02
28 // StXClass stores registers to memory.
29 StXClass Class = 0x03
30 // ALUClass describes arithmetic operators.
31 ALUClass Class = 0x04
32 // JumpClass describes jump operators.
33 JumpClass Class = 0x05
34 // Jump32Class describes jump operators with 32-bit comparisons.
35 // Requires kernel 5.1.
36 Jump32Class Class = 0x06
37 // ALU64Class describes arithmetic operators in 64-bit mode.
38 ALU64Class Class = 0x07
39 )
40
41 // IsLoad checks if this is either LdClass or LdXClass.
42 func (cls Class) IsLoad() bool {
43 return cls == LdClass || cls == LdXClass
44 }
45
46 // IsStore checks if this is either StClass or StXClass.
47 func (cls Class) IsStore() bool {
48 return cls == StClass || cls == StXClass
49 }
50
51 func (cls Class) isLoadOrStore() bool {
52 return cls.IsLoad() || cls.IsStore()
53 }
54
55 // IsALU checks if this is either ALUClass or ALU64Class.
56 func (cls Class) IsALU() bool {
57 return cls == ALUClass || cls == ALU64Class
58 }
59
60 // IsJump checks if this is either JumpClass or Jump32Class.
61 func (cls Class) IsJump() bool {
62 return cls == JumpClass || cls == Jump32Class
63 }
64
65 func (cls Class) isJumpOrALU() bool {
66 return cls.IsJump() || cls.IsALU()
67 }
68
69 // OpCode represents a single operation.
70 // It is not a 1:1 mapping to real eBPF opcodes.
71 //
72 // The encoding varies based on a 3-bit Class:
73 //
74 // 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
75 // ??? | CLS
76 //
77 // For ALUClass and ALUCLass32:
78 //
79 // 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
80 // 0 | OPC |S| CLS
81 //
82 // For LdClass, LdXclass, StClass and StXClass:
83 //
84 // 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
85 // 0 | MDE |SIZ| CLS
86 //
87 // For StXClass where MDE == AtomicMode:
88 //
89 // 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
90 // 0 | ATOMIC OP | MDE |SIZ| CLS
91 //
92 // For JumpClass, Jump32Class:
93 //
94 // 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
95 // 0 | OPC |S| CLS
96 type OpCode uint32
97
98 // InvalidOpCode is returned by setters on OpCode
99 const InvalidOpCode OpCode = 0xffff
100
101 // bpfOpCode returns the actual BPF opcode.
102 func (op OpCode) bpfOpCode() (byte, error) {
103 const opCodeMask = 0xff
104
105 if !valid(op, opCodeMask) {
106 return 0, fmt.Errorf("invalid opcode %x", op)
107 }
108
109 return byte(op & opCodeMask), nil
110 }
111
112 // rawInstructions returns the number of BPF instructions required
113 // to encode this opcode.
114 func (op OpCode) rawInstructions() int {
115 if op.IsDWordLoad() {
116 return 2
117 }
118 return 1
119 }
120
121 func (op OpCode) IsDWordLoad() bool {
122 return op == LoadImmOp(DWord)
123 }
124
125 // Class returns the class of operation.
126 func (op OpCode) Class() Class {
127 return Class(op & classMask)
128 }
129
130 // Mode returns the mode for load and store operations.
131 func (op OpCode) Mode() Mode {
132 if !op.Class().isLoadOrStore() {
133 return InvalidMode
134 }
135 return Mode(op & modeMask)
136 }
137
138 // Size returns the size for load and store operations.
139 func (op OpCode) Size() Size {
140 if !op.Class().isLoadOrStore() {
141 return InvalidSize
142 }
143 return Size(op & sizeMask)
144 }
145
146 // AtomicOp returns the type of atomic operation.
147 func (op OpCode) AtomicOp() AtomicOp {
148 if op.Class() != StXClass || op.Mode() != AtomicMode {
149 return InvalidAtomic
150 }
151 return AtomicOp(op & atomicMask)
152 }
153
154 // Source returns the source for branch and ALU operations.
155 func (op OpCode) Source() Source {
156 if !op.Class().isJumpOrALU() || op.ALUOp() == Swap {
157 return InvalidSource
158 }
159 return Source(op & sourceMask)
160 }
161
162 // ALUOp returns the ALUOp.
163 func (op OpCode) ALUOp() ALUOp {
164 if !op.Class().IsALU() {
165 return InvalidALUOp
166 }
167 return ALUOp(op & aluMask)
168 }
169
170 // Endianness returns the Endianness for a byte swap instruction.
171 func (op OpCode) Endianness() Endianness {
172 if op.ALUOp() != Swap {
173 return InvalidEndian
174 }
175 return Endianness(op & endianMask)
176 }
177
178 // JumpOp returns the JumpOp.
179 // Returns InvalidJumpOp if it doesn't encode a jump.
180 func (op OpCode) JumpOp() JumpOp {
181 if !op.Class().IsJump() {
182 return InvalidJumpOp
183 }
184
185 jumpOp := JumpOp(op & jumpMask)
186
187 // Some JumpOps are only supported by JumpClass, not Jump32Class.
188 if op.Class() == Jump32Class && (jumpOp == Exit || jumpOp == Call) {
189 return InvalidJumpOp
190 }
191
192 return jumpOp
193 }
194
195 // SetMode sets the mode on load and store operations.
196 //
197 // Returns InvalidOpCode if op is of the wrong class.
198 func (op OpCode) SetMode(mode Mode) OpCode {
199 if !op.Class().isLoadOrStore() || !valid(OpCode(mode), modeMask) {
200 return InvalidOpCode
201 }
202 return (op & ^modeMask) | OpCode(mode)
203 }
204
205 // SetSize sets the size on load and store operations.
206 //
207 // Returns InvalidOpCode if op is of the wrong class.
208 func (op OpCode) SetSize(size Size) OpCode {
209 if !op.Class().isLoadOrStore() || !valid(OpCode(size), sizeMask) {
210 return InvalidOpCode
211 }
212 return (op & ^sizeMask) | OpCode(size)
213 }
214
215 func (op OpCode) SetAtomicOp(atomic AtomicOp) OpCode {
216 if op.Class() != StXClass || op.Mode() != AtomicMode || !valid(OpCode(atomic), atomicMask) {
217 return InvalidOpCode
218 }
219 return (op & ^atomicMask) | OpCode(atomic)
220 }
221
222 // SetSource sets the source on jump and ALU operations.
223 //
224 // Returns InvalidOpCode if op is of the wrong class.
225 func (op OpCode) SetSource(source Source) OpCode {
226 if !op.Class().isJumpOrALU() || !valid(OpCode(source), sourceMask) {
227 return InvalidOpCode
228 }
229 return (op & ^sourceMask) | OpCode(source)
230 }
231
232 // SetALUOp sets the ALUOp on ALU operations.
233 //
234 // Returns InvalidOpCode if op is of the wrong class.
235 func (op OpCode) SetALUOp(alu ALUOp) OpCode {
236 if !op.Class().IsALU() || !valid(OpCode(alu), aluMask) {
237 return InvalidOpCode
238 }
239 return (op & ^aluMask) | OpCode(alu)
240 }
241
242 // SetJumpOp sets the JumpOp on jump operations.
243 //
244 // Returns InvalidOpCode if op is of the wrong class.
245 func (op OpCode) SetJumpOp(jump JumpOp) OpCode {
246 if !op.Class().IsJump() || !valid(OpCode(jump), jumpMask) {
247 return InvalidOpCode
248 }
249
250 newOp := (op & ^jumpMask) | OpCode(jump)
251
252 // Check newOp is legal.
253 if newOp.JumpOp() == InvalidJumpOp {
254 return InvalidOpCode
255 }
256
257 return newOp
258 }
259
260 func (op OpCode) String() string {
261 var f strings.Builder
262
263 switch class := op.Class(); {
264 case class.isLoadOrStore():
265 f.WriteString(strings.TrimSuffix(class.String(), "Class"))
266
267 mode := op.Mode()
268 f.WriteString(strings.TrimSuffix(mode.String(), "Mode"))
269
270 if atomic := op.AtomicOp(); atomic != InvalidAtomic {
271 f.WriteString(strings.TrimSuffix(atomic.String(), "Atomic"))
272 }
273
274 switch op.Size() {
275 case DWord:
276 f.WriteString("DW")
277 case Word:
278 f.WriteString("W")
279 case Half:
280 f.WriteString("H")
281 case Byte:
282 f.WriteString("B")
283 }
284
285 case class.IsALU():
286 if op.ALUOp() == Swap && op.Class() == ALU64Class {
287 // B to make BSwap, uncontitional byte swap
288 f.WriteString("B")
289 }
290
291 f.WriteString(op.ALUOp().String())
292
293 if op.ALUOp() == Swap {
294 if op.Class() == ALUClass {
295 // Width for Endian is controlled by Constant
296 f.WriteString(op.Endianness().String())
297 }
298 } else {
299 f.WriteString(strings.TrimSuffix(op.Source().String(), "Source"))
300
301 if class == ALUClass {
302 f.WriteString("32")
303 }
304 }
305
306 case class.IsJump():
307 f.WriteString(op.JumpOp().String())
308
309 if class == Jump32Class {
310 f.WriteString("32")
311 }
312
313 if jop := op.JumpOp(); jop != Exit && jop != Call && jop != Ja {
314 f.WriteString(strings.TrimSuffix(op.Source().String(), "Source"))
315 }
316
317 default:
318 fmt.Fprintf(&f, "OpCode(%#x)", uint8(op))
319 }
320
321 return f.String()
322 }
323
324 // valid returns true if all bits in value are covered by mask.
325 func valid(value, mask OpCode) bool {
326 return value & ^mask == 0
327 }