Skip to content

Commit

Permalink
Add signed modulo instructions
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Jan 12, 2024
1 parent 13de786 commit aca7f94
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/bpf_assembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef class _bpf_assembler
{"rsh", 0x7},
{"neg", 0x8},
{"mod", 0x9},
{"smod", 0x9},
{"xor", 0xa},
{"mov", 0xb},
{"arsh", 0xc},
Expand Down Expand Up @@ -246,7 +247,7 @@ typedef class _bpf_assembler
inst.imm = _decode_imm32(mnemonic.substr(2));
return inst;
}
if (mnemonic.starts_with("sdiv")) {
if (mnemonic.starts_with("sdiv") || mnemonic.starts_with("smod")) {
inst.offset = 1;
}

Expand Down Expand Up @@ -448,7 +449,6 @@ typedef class _bpf_assembler
{"be16", {&_bpf_assembler::_encode_alu, 1}}, {"be32", {&_bpf_assembler::_encode_alu, 1}},
{"be64", {&_bpf_assembler::_encode_alu, 1}}, {"call", {&_bpf_assembler::_encode_jmp, 2}},
{"div", {&_bpf_assembler::_encode_alu, 2}}, {"div32", {&_bpf_assembler::_encode_alu, 2}},
{"sdiv", {&_bpf_assembler::_encode_alu, 2}}, {"sdiv32", {&_bpf_assembler::_encode_alu, 2}},
{"exit", {&_bpf_assembler::_encode_jmp, 0}}, {"ja", {&_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}},
Expand All @@ -472,6 +472,8 @@ typedef class _bpf_assembler
{"neg", {&_bpf_assembler::_encode_alu, 1}}, {"neg32", {&_bpf_assembler::_encode_alu, 1}},
{"or", {&_bpf_assembler::_encode_alu, 2}}, {"or32", {&_bpf_assembler::_encode_alu, 2}},
{"rsh", {&_bpf_assembler::_encode_alu, 2}}, {"rsh32", {&_bpf_assembler::_encode_alu, 2}},
{"sdiv", {&_bpf_assembler::_encode_alu, 2}}, {"sdiv32", {&_bpf_assembler::_encode_alu, 2}},
{"smod", {&_bpf_assembler::_encode_alu, 2}}, {"smod32", {&_bpf_assembler::_encode_alu, 2}},
{"stb", {&_bpf_assembler::_encode_st, 2}}, {"stdw", {&_bpf_assembler::_encode_st, 2}},
{"sth", {&_bpf_assembler::_encode_st, 2}}, {"stw", {&_bpf_assembler::_encode_st, 2}},
{"stxb", {&_bpf_assembler::_encode_stx, 2}}, {"stxdw", {&_bpf_assembler::_encode_stx, 2}},
Expand Down
15 changes: 10 additions & 5 deletions src/opcode_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ needs_imm(uint8_t opcode)
inline bool
needs_offset(uint8_t opcode)
{
return opcode == 0x34 || opcode == 0x37 || opcode == 0x3c || opcode == 0x3f;
return opcode == 0x34 || opcode == 0x37 || opcode == 0x3c || opcode == 0x3f || opcode == 0x94 || opcode == 0x97 ||
opcode == 0x9c || opcode == 0x9f || opcode == 0xb4 || opcode == 0xb7 || opcode == 0xbc || opcode == 0xbf;
}

class bpf_conformance_instruction_t
Expand Down Expand Up @@ -289,11 +290,15 @@ static const std::set<bpf_conformance_instruction_t, InstCmp> instructions_from_
{bpf_conformance_test_cpu_version_t::v3, 0x85, 0x01},
{bpf_conformance_test_cpu_version_t::v3, 0x85, 0x02},
{bpf_conformance_test_cpu_version_t::v1, 0x87},
{bpf_conformance_test_cpu_version_t::v1, 0x94},
{bpf_conformance_test_cpu_version_t::v1, 0x94, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0x94, 0x00, 0x00, 0x01},
{bpf_conformance_test_cpu_version_t::v1, 0x95},
{bpf_conformance_test_cpu_version_t::v1, 0x97},
{bpf_conformance_test_cpu_version_t::v1, 0x9c},
{bpf_conformance_test_cpu_version_t::v1, 0x9f},
{bpf_conformance_test_cpu_version_t::v1, 0x97, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0x97, 0x00, 0x00, 0x01},
{bpf_conformance_test_cpu_version_t::v1, 0x9c, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0x9c, 0x00, 0x00, 0x01},
{bpf_conformance_test_cpu_version_t::v1, 0x9f, 0x00, 0x00, 0x00},
{bpf_conformance_test_cpu_version_t::v4, 0x9f, 0x00, 0x00, 0x01},
{bpf_conformance_test_cpu_version_t::v1, 0xa4},
{bpf_conformance_test_cpu_version_t::v2, 0xa5},
{bpf_conformance_test_cpu_version_t::v3, 0xa6},
Expand Down
8 changes: 8 additions & 0 deletions tests/sdiv32-by-zero-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, 1
sdiv32 %r0, 0
exit
-- result
0x0
8 changes: 8 additions & 0 deletions tests/sdiv64-by-zero-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, 1
sdiv %r0, 0
exit
-- result
0x0
8 changes: 8 additions & 0 deletions tests/smod32-neg-by-neg-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, -13
smod32 %r0, -3
exit
-- result
0xffffffff
9 changes: 9 additions & 0 deletions tests/smod32-neg-by-neg-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, -13
mov32 %r1, -3
smod32 %r0, %r1
exit
-- result
0xffffffff
8 changes: 8 additions & 0 deletions tests/smod32-neg-by-pos-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, -13
smod32 %r0, 4
exit
-- result
0xffffffff
9 changes: 9 additions & 0 deletions tests/smod32-neg-by-pos-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, -13
mov32 %r1, 4
smod32 %r0, %r1
exit
-- result
0xffffffff
8 changes: 8 additions & 0 deletions tests/smod32-neg-by-zero-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, -10
smod32 %r0, 0
exit
-- result
0xFFFFFFF6
9 changes: 9 additions & 0 deletions tests/smod32-neg-by-zero-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, -10
mov32 %r1, 0
smod32 %r0, %r1
exit
-- result
0xFFFFFFF6
8 changes: 8 additions & 0 deletions tests/smod32-pos-by-neg-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, 13
smod32 %r0, -3
exit
-- result
0x1
9 changes: 9 additions & 0 deletions tests/smod32-pos-by-neg-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov32 %r0, 13
mov32 %r1, -3
smod32 %r0, %r1
exit
-- result
0x1
8 changes: 8 additions & 0 deletions tests/smod64-neg-by-neg-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, -13
smod %r0, -3
exit
-- result
0xffffffffffffffff
9 changes: 9 additions & 0 deletions tests/smod64-neg-by-neg-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, -13
mov %r1, -3
smod %r0, %r1
exit
-- result
0xffffffffffffffff
8 changes: 8 additions & 0 deletions tests/smod64-neg-by-pos-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, -13
smod %r0, 4
exit
-- result
0xffffffffffffffff
9 changes: 9 additions & 0 deletions tests/smod64-neg-by-pos-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, -13
mov %r1, 4
smod %r0, %r1
exit
-- result
0xffffffffffffffff
8 changes: 8 additions & 0 deletions tests/smod64-neg-by-zero-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, -10
smod %r0, 0
exit
-- result
0xFFFFFFFFFFFFFFF6
9 changes: 9 additions & 0 deletions tests/smod64-neg-by-zero-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, -10
mov %r1, 0
smod %r0, %r1
exit
-- result
0xFFFFFFFFFFFFFFF6
8 changes: 8 additions & 0 deletions tests/smod64-pos-by-neg-imm.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, 13
smod %r0, -3
exit
-- result
0x1
9 changes: 9 additions & 0 deletions tests/smod64-pos-by-neg-reg.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Dave Thaler
# SPDX-License-Identifier: MIT
-- asm
mov %r0, 13
mov %r1, -3
smod %r0, %r1
exit
-- result
0x1

0 comments on commit aca7f94

Please sign in to comment.