| 1 |
package asm |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type Class uint8 |
| 17 |
|
| 18 |
const classMask OpCode = 0x07 |
| 19 |
|
| 20 |
const ( |
| 21 |
|
| 22 |
|
| 23 |
LdClass Class = 0x00 |
| 24 |
|
| 25 |
LdXClass Class = 0x01 |
| 26 |
|
| 27 |
StClass Class = 0x02 |
| 28 |
|
| 29 |
StXClass Class = 0x03 |
| 30 |
|
| 31 |
ALUClass Class = 0x04 |
| 32 |
|
| 33 |
JumpClass Class = 0x05 |
| 34 |
|
| 35 |
|
| 36 |
Jump32Class Class = 0x06 |
| 37 |
|
| 38 |
ALU64Class Class = 0x07 |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
func (cls Class) IsLoad() bool { |
| 43 |
return cls == LdClass || cls == LdXClass |
| 44 |
} |
| 45 |
|
| 46 |
|
| 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 |
|
| 56 |
func (cls Class) IsALU() bool { |
| 57 |
return cls == ALUClass || cls == ALU64Class |
| 58 |
} |
| 59 |
|
| 60 |
|
| 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 |
|
| 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 |
type OpCode uint32 |
| 97 |
|
| 98 |
|
| 99 |
const InvalidOpCode OpCode = 0xffff |
| 100 |
|
| 101 |
|
| 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 |
|
| 113 |
|
| 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 |
|
| 126 |
func (op OpCode) Class() Class { |
| 127 |
return Class(op & classMask) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (op OpCode) Mode() Mode { |
| 132 |
if !op.Class().isLoadOrStore() { |
| 133 |
return InvalidMode |
| 134 |
} |
| 135 |
return Mode(op & modeMask) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (op OpCode) Size() Size { |
| 140 |
if !op.Class().isLoadOrStore() { |
| 141 |
return InvalidSize |
| 142 |
} |
| 143 |
return Size(op & sizeMask) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 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 |
|
| 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 |
|
| 163 |
func (op OpCode) ALUOp() ALUOp { |
| 164 |
if !op.Class().IsALU() { |
| 165 |
return InvalidALUOp |
| 166 |
} |
| 167 |
return ALUOp(op & aluMask) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
func (op OpCode) Endianness() Endianness { |
| 172 |
if op.ALUOp() != Swap { |
| 173 |
return InvalidEndian |
| 174 |
} |
| 175 |
return Endianness(op & endianMask) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (op OpCode) JumpOp() JumpOp { |
| 181 |
if !op.Class().IsJump() { |
| 182 |
return InvalidJumpOp |
| 183 |
} |
| 184 |
|
| 185 |
jumpOp := JumpOp(op & jumpMask) |
| 186 |
|
| 187 |
|
| 188 |
if op.Class() == Jump32Class && (jumpOp == Exit || jumpOp == Call) { |
| 189 |
return InvalidJumpOp |
| 190 |
} |
| 191 |
|
| 192 |
return jumpOp |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 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 |
|
| 206 |
|
| 207 |
|
| 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 |
|
| 223 |
|
| 224 |
|
| 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 |
|
| 233 |
|
| 234 |
|
| 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 |
|
| 243 |
|
| 244 |
|
| 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 |
|
| 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 |
|
| 288 |
f.WriteString("B") |
| 289 |
} |
| 290 |
|
| 291 |
f.WriteString(op.ALUOp().String()) |
| 292 |
|
| 293 |
if op.ALUOp() == Swap { |
| 294 |
if op.Class() == ALUClass { |
| 295 |
|
| 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 |
|
| 325 |
func valid(value, mask OpCode) bool { |
| 326 |
return value & ^mask == 0 |
| 327 |
} |