Skip to content

Commit

Permalink
Merge pull request #181 from Alan-Jowett/jmp32
Browse files Browse the repository at this point in the history
Add support for BMP_JMP32 class
  • Loading branch information
dthaler authored Jan 16, 2024
2 parents d25c3f2 + e91a2b7 commit 787d8f2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/bpf_assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,12 @@ typedef class _bpf_assembler
_encode_jmp(const std::string& mnemonic, const std::vector<std::string>& operands)
{
ebpf_inst inst{};
inst.opcode |= EBPF_CLS_JMP;
if (mnemonic == "ja") {
inst.opcode = EBPF_CLS_JMP;
inst.offset = _decode_jump_target(operands[0]);
} else if (mnemonic == "ja32") {
inst.opcode = EBPF_CLS_JMP32;
inst.imm = _decode_jump_target(operands[0]);
} else if (mnemonic == "exit") {
inst.opcode = EBPF_OP_EXIT;
} else if (mnemonic == "call") {
Expand All @@ -310,7 +313,7 @@ typedef class _bpf_assembler
throw std::runtime_error("Invalid call mode");
}
} else {
mnemonic.ends_with("32") ? inst.opcode = EBPF_CLS_JMP32 : inst.opcode |= EBPF_CLS_JMP;
mnemonic.ends_with("32") ? inst.opcode = EBPF_CLS_JMP32 : inst.opcode = EBPF_CLS_JMP;
auto iter =
_bpf_encode_jmp_ops.find(mnemonic.ends_with("32") ? mnemonic.substr(0, mnemonic.size() - 2) : mnemonic);
// It is not possible to reach here with no match.
Expand Down Expand Up @@ -456,6 +459,7 @@ typedef class _bpf_assembler
{"be64", {&_bpf_assembler::_encode_alu, 1}}, {"call", {&_bpf_assembler::_encode_jmp, 2}},
{"div", {&_bpf_assembler::_encode_alu, 2}}, {"div32", {&_bpf_assembler::_encode_alu, 2}},
{"exit", {&_bpf_assembler::_encode_jmp, 0}}, {"ja", {&_bpf_assembler::_encode_jmp, 1}},
{"ja32", {&_bpf_assembler::_encode_jmp, 1}},
{"jeq", {&_bpf_assembler::_encode_jmp, 3}}, {"jeq32", {&_bpf_assembler::_encode_jmp, 3}},
{"jge", {&_bpf_assembler::_encode_jmp, 3}}, {"jge32", {&_bpf_assembler::_encode_jmp, 3}},
{"jgt", {&_bpf_assembler::_encode_jmp, 3}}, {"jgt32", {&_bpf_assembler::_encode_jmp, 3}},
Expand Down Expand Up @@ -636,7 +640,7 @@ typedef class _bpf_assembler
if (iter == _labels.end()) {
throw std::runtime_error(std::string("Invalid label: ") + _jump_instructions[i].value());
}
if (output[i].opcode == EBPF_OP_CALL) {
if (output[i].opcode == EBPF_OP_CALL || output[i].opcode == EBPF_OP_JA32) {
output[i].imm = static_cast<uint32_t>(iter->second - i - 1);
} else {
output[i].offset = static_cast<uint16_t>(iter->second - i - 1);
Expand Down
1 change: 1 addition & 0 deletions src/ebpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ typedef struct ebpf_inst
#define EBPF_MODE_JSLE 0xd0

#define EBPF_OP_JA (EBPF_CLS_JMP | EBPF_MODE_JA)
#define EBPF_OP_JA32 (EBPF_CLS_JMP32 | EBPF_MODE_JA)
#define EBPF_OP_JEQ_IMM (EBPF_CLS_JMP | EBPF_SRC_IMM | EBPF_MODE_JEQ)
#define EBPF_OP_JEQ_REG (EBPF_CLS_JMP | EBPF_SRC_REG | EBPF_MODE_JEQ)
#define EBPF_OP_JGT_IMM (EBPF_CLS_JMP | EBPF_SRC_IMM | EBPF_MODE_JGT)
Expand Down
3 changes: 2 additions & 1 deletion src/opcode_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static const std::map<uint8_t, std::string> opcode_names = {
{(EBPF_CLS_ALU64 | EBPF_SRC_REG | 0x30), "EBPF_OP_DIV64_REG"},
{(EBPF_CLS_JMP | 0x90), "EBPF_OP_EXIT"},
{(EBPF_CLS_JMP | 0x00), "EBPF_OP_JA"},
{(EBPF_CLS_JMP32 | 0x00), "EBPF_OP_JA32"},
{(EBPF_CLS_JMP | EBPF_SRC_IMM | 0x10), "EBPF_OP_JEQ_IMM"},
{(EBPF_CLS_JMP | EBPF_SRC_REG | 0x10), "EBPF_OP_JEQ_REG"},
{(EBPF_CLS_JMP32 | EBPF_SRC_IMM | 0x10), "EBPF_OP_JEQ32_IMM"},
Expand Down Expand Up @@ -202,7 +203,7 @@ static const std::set<bpf_conformance_instruction_t, InstCmp> instructions_from_
{bpf_conformance_test_cpu_version_t::v1, 0x00},
{bpf_conformance_test_cpu_version_t::v1, 0x04},
{bpf_conformance_test_cpu_version_t::v1, 0x05},
{bpf_conformance_test_cpu_version_t::v3, 0x06},
{bpf_conformance_test_cpu_version_t::v4, 0x06},
{bpf_conformance_test_cpu_version_t::v1, 0x07},
{bpf_conformance_test_cpu_version_t::v1, 0x0c},
{bpf_conformance_test_cpu_version_t::v1, 0x0f},
Expand Down
13 changes: 13 additions & 0 deletions tests/ja32.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
mov %r1, 0
ja32 L2
L1:
mov %r2, 0
exit
L2:
mov %r0, 0
ja32 L1
-- result
0x0

0 comments on commit 787d8f2

Please sign in to comment.