Skip to content

Commit

Permalink
Add unconditional byteswap instructions (#180)
Browse files Browse the repository at this point in the history
* Add unconditional byteswap instructions

Addresses more of issue #152

Signed-off-by: Dave Thaler <dthaler1968@gmail.com>

* Simplify swap tests

Signed-off-by: Dave Thaler <dthaler1968@gmail.com>

---------

Signed-off-by: Dave Thaler <dthaler1968@gmail.com>
Co-authored-by: Alan Jowett <alanjo@microsoft.com>
  • Loading branch information
dthaler and Alan-Jowett committed Jan 16, 2024
1 parent 3a32dae commit d25c3f2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/bpf_assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef class _bpf_assembler
{"arsh", 0xc},
{"le", 0xd},
{"be", 0xd},
{"swap", 0xd},
};

const std::unordered_map<std::string, int> _bpf_encode_jmp_ops{
Expand Down Expand Up @@ -246,6 +247,11 @@ typedef class _bpf_assembler
inst.dst = _decode_register(operands[0]);
inst.imm = _decode_imm32(mnemonic.substr(2));
return inst;
} else if (mnemonic.starts_with("swap")) {
inst.opcode = EBPF_OP_SWAP;
inst.dst = _decode_register(operands[0]);
inst.imm = _decode_imm32(mnemonic.substr(4));
return inst;
}
if (mnemonic.starts_with("sdiv") || mnemonic.starts_with("smod")) {
inst.offset = 1;
Expand Down Expand Up @@ -479,7 +485,9 @@ typedef class _bpf_assembler
{"stxb", {&_bpf_assembler::_encode_stx, 2}}, {"stxdw", {&_bpf_assembler::_encode_stx, 2}},
{"stxh", {&_bpf_assembler::_encode_stx, 2}}, {"stxw", {&_bpf_assembler::_encode_stx, 2}},
{"sub", {&_bpf_assembler::_encode_alu, 2}}, {"sub32", {&_bpf_assembler::_encode_alu, 2}},
{"xor", {&_bpf_assembler::_encode_alu, 2}}, {"xor32", {&_bpf_assembler::_encode_alu, 2}},
{"swap16", {&_bpf_assembler::_encode_alu, 1}},{"swap32", {&_bpf_assembler::_encode_alu, 1}},
{"swap64", {&_bpf_assembler::_encode_alu, 1}},{"xor", {&_bpf_assembler::_encode_alu, 2}},
{"xor32", {&_bpf_assembler::_encode_alu, 2}},
};

const std::unordered_map<std::string, std::tuple<bpf_encode_t, size_t>> _bpf_encode_atomic_ops{
Expand Down
1 change: 1 addition & 0 deletions src/ebpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ typedef struct ebpf_inst
#define EBPF_OP_MOV64_REG (EBPF_CLS_ALU64 | EBPF_SRC_REG | EBPF_ALU_OP_MOV)
#define EBPF_OP_ARSH64_IMM (EBPF_CLS_ALU64 | EBPF_SRC_IMM | EBPF_ALU_OP_ARSH)
#define EBPF_OP_ARSH64_REG (EBPF_CLS_ALU64 | EBPF_SRC_REG | EBPF_ALU_OP_ARSH)
#define EBPF_OP_SWAP (EBPF_CLS_ALU64 | EBPF_SRC_IMM | EBPF_ALU_OP_END)

#define EBPF_OP_LDXW (EBPF_CLS_LDX | EBPF_MODE_MEM | EBPF_SIZE_W)
#define EBPF_OP_LDXH (EBPF_CLS_LDX | EBPF_MODE_MEM | EBPF_SIZE_H)
Expand Down
3 changes: 3 additions & 0 deletions src/opcode_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ static const std::set<bpf_conformance_instruction_t, InstCmp> instructions_from_
{bpf_conformance_test_cpu_version_t::v1, 0xd4, 0x00, 0x40},
{bpf_conformance_test_cpu_version_t::v2, 0xd5},
{bpf_conformance_test_cpu_version_t::v3, 0xd6},
{bpf_conformance_test_cpu_version_t::v4, 0xd7, 0x00, 0x10},
{bpf_conformance_test_cpu_version_t::v4, 0xd7, 0x00, 0x20},
{bpf_conformance_test_cpu_version_t::v4, 0xd7, 0x00, 0x40},
{bpf_conformance_test_cpu_version_t::v3, 0xdb, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v3, 0xdb, 0x00, 0x01},
{bpf_conformance_test_cpu_version_t::v3, 0xdb, 0x00, 0x40},
Expand Down
8 changes: 8 additions & 0 deletions tests/swap16.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r0, 0x8877665544332211
swap16 %r0
exit
-- result
0x1122
8 changes: 8 additions & 0 deletions tests/swap32.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r0, 0x8877665544332211
swap32 %r0
exit
-- result
0x11223344
8 changes: 8 additions & 0 deletions tests/swap64.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Big Switch Networks, Inc
# SPDX-License-Identifier: Apache-2.0
-- asm
lddw %r0, 0x8877665544332211
swap64 %r0
exit
-- result
0x1122334455667788

0 comments on commit d25c3f2

Please sign in to comment.