From aca7f94f6095b77cd3515c257348daa14cb68dfb Mon Sep 17 00:00:00 2001 From: Dave Thaler Date: Thu, 11 Jan 2024 17:21:50 -0800 Subject: [PATCH] Add signed modulo instructions Signed-off-by: Dave Thaler --- src/bpf_assembler.cc | 6 ++++-- src/opcode_names.h | 15 ++++++++++----- tests/sdiv32-by-zero-imm.data | 8 ++++++++ tests/sdiv64-by-zero-imm.data | 8 ++++++++ tests/smod32-neg-by-neg-imm.data | 8 ++++++++ tests/smod32-neg-by-neg-reg.data | 9 +++++++++ tests/smod32-neg-by-pos-imm.data | 8 ++++++++ tests/smod32-neg-by-pos-reg.data | 9 +++++++++ tests/smod32-neg-by-zero-imm.data | 8 ++++++++ tests/smod32-neg-by-zero-reg.data | 9 +++++++++ tests/smod32-pos-by-neg-imm.data | 8 ++++++++ tests/smod32-pos-by-neg-reg.data | 9 +++++++++ tests/smod64-neg-by-neg-imm.data | 8 ++++++++ tests/smod64-neg-by-neg-reg.data | 9 +++++++++ tests/smod64-neg-by-pos-imm.data | 8 ++++++++ tests/smod64-neg-by-pos-reg.data | 9 +++++++++ tests/smod64-neg-by-zero-imm.data | 8 ++++++++ tests/smod64-neg-by-zero-reg.data | 9 +++++++++ tests/smod64-pos-by-neg-imm.data | 8 ++++++++ tests/smod64-pos-by-neg-reg.data | 9 +++++++++ 20 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 tests/sdiv32-by-zero-imm.data create mode 100644 tests/sdiv64-by-zero-imm.data create mode 100644 tests/smod32-neg-by-neg-imm.data create mode 100644 tests/smod32-neg-by-neg-reg.data create mode 100644 tests/smod32-neg-by-pos-imm.data create mode 100644 tests/smod32-neg-by-pos-reg.data create mode 100644 tests/smod32-neg-by-zero-imm.data create mode 100644 tests/smod32-neg-by-zero-reg.data create mode 100644 tests/smod32-pos-by-neg-imm.data create mode 100644 tests/smod32-pos-by-neg-reg.data create mode 100644 tests/smod64-neg-by-neg-imm.data create mode 100644 tests/smod64-neg-by-neg-reg.data create mode 100644 tests/smod64-neg-by-pos-imm.data create mode 100644 tests/smod64-neg-by-pos-reg.data create mode 100644 tests/smod64-neg-by-zero-imm.data create mode 100644 tests/smod64-neg-by-zero-reg.data create mode 100644 tests/smod64-pos-by-neg-imm.data create mode 100644 tests/smod64-pos-by-neg-reg.data diff --git a/src/bpf_assembler.cc b/src/bpf_assembler.cc index 693d282..c06c1ca 100644 --- a/src/bpf_assembler.cc +++ b/src/bpf_assembler.cc @@ -52,6 +52,7 @@ typedef class _bpf_assembler {"rsh", 0x7}, {"neg", 0x8}, {"mod", 0x9}, + {"smod", 0x9}, {"xor", 0xa}, {"mov", 0xb}, {"arsh", 0xc}, @@ -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; } @@ -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}}, @@ -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}}, diff --git a/src/opcode_names.h b/src/opcode_names.h index 179b8ba..ca45824 100644 --- a/src/opcode_names.h +++ b/src/opcode_names.h @@ -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 @@ -289,11 +290,15 @@ static const std::set 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}, diff --git a/tests/sdiv32-by-zero-imm.data b/tests/sdiv32-by-zero-imm.data new file mode 100644 index 0000000..7ba6f6e --- /dev/null +++ b/tests/sdiv32-by-zero-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov32 %r0, 1 +sdiv32 %r0, 0 +exit +-- result +0x0 diff --git a/tests/sdiv64-by-zero-imm.data b/tests/sdiv64-by-zero-imm.data new file mode 100644 index 0000000..83ac47d --- /dev/null +++ b/tests/sdiv64-by-zero-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov32 %r0, 1 +sdiv %r0, 0 +exit +-- result +0x0 diff --git a/tests/smod32-neg-by-neg-imm.data b/tests/smod32-neg-by-neg-imm.data new file mode 100644 index 0000000..cd0a553 --- /dev/null +++ b/tests/smod32-neg-by-neg-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov32 %r0, -13 +smod32 %r0, -3 +exit +-- result +0xffffffff diff --git a/tests/smod32-neg-by-neg-reg.data b/tests/smod32-neg-by-neg-reg.data new file mode 100644 index 0000000..3c524fb --- /dev/null +++ b/tests/smod32-neg-by-neg-reg.data @@ -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 diff --git a/tests/smod32-neg-by-pos-imm.data b/tests/smod32-neg-by-pos-imm.data new file mode 100644 index 0000000..09987c3 --- /dev/null +++ b/tests/smod32-neg-by-pos-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov32 %r0, -13 +smod32 %r0, 4 +exit +-- result +0xffffffff diff --git a/tests/smod32-neg-by-pos-reg.data b/tests/smod32-neg-by-pos-reg.data new file mode 100644 index 0000000..d52363d --- /dev/null +++ b/tests/smod32-neg-by-pos-reg.data @@ -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 diff --git a/tests/smod32-neg-by-zero-imm.data b/tests/smod32-neg-by-zero-imm.data new file mode 100644 index 0000000..ff0856b --- /dev/null +++ b/tests/smod32-neg-by-zero-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov32 %r0, -10 +smod32 %r0, 0 +exit +-- result +0xFFFFFFF6 diff --git a/tests/smod32-neg-by-zero-reg.data b/tests/smod32-neg-by-zero-reg.data new file mode 100644 index 0000000..c531753 --- /dev/null +++ b/tests/smod32-neg-by-zero-reg.data @@ -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 diff --git a/tests/smod32-pos-by-neg-imm.data b/tests/smod32-pos-by-neg-imm.data new file mode 100644 index 0000000..7c0d0e0 --- /dev/null +++ b/tests/smod32-pos-by-neg-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov32 %r0, 13 +smod32 %r0, -3 +exit +-- result +0x1 diff --git a/tests/smod32-pos-by-neg-reg.data b/tests/smod32-pos-by-neg-reg.data new file mode 100644 index 0000000..80a6acf --- /dev/null +++ b/tests/smod32-pos-by-neg-reg.data @@ -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 diff --git a/tests/smod64-neg-by-neg-imm.data b/tests/smod64-neg-by-neg-imm.data new file mode 100644 index 0000000..baee851 --- /dev/null +++ b/tests/smod64-neg-by-neg-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov %r0, -13 +smod %r0, -3 +exit +-- result +0xffffffffffffffff diff --git a/tests/smod64-neg-by-neg-reg.data b/tests/smod64-neg-by-neg-reg.data new file mode 100644 index 0000000..78520ce --- /dev/null +++ b/tests/smod64-neg-by-neg-reg.data @@ -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 diff --git a/tests/smod64-neg-by-pos-imm.data b/tests/smod64-neg-by-pos-imm.data new file mode 100644 index 0000000..2855a6d --- /dev/null +++ b/tests/smod64-neg-by-pos-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov %r0, -13 +smod %r0, 4 +exit +-- result +0xffffffffffffffff diff --git a/tests/smod64-neg-by-pos-reg.data b/tests/smod64-neg-by-pos-reg.data new file mode 100644 index 0000000..5e17f1b --- /dev/null +++ b/tests/smod64-neg-by-pos-reg.data @@ -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 diff --git a/tests/smod64-neg-by-zero-imm.data b/tests/smod64-neg-by-zero-imm.data new file mode 100644 index 0000000..4cbf902 --- /dev/null +++ b/tests/smod64-neg-by-zero-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov %r0, -10 +smod %r0, 0 +exit +-- result +0xFFFFFFFFFFFFFFF6 diff --git a/tests/smod64-neg-by-zero-reg.data b/tests/smod64-neg-by-zero-reg.data new file mode 100644 index 0000000..5267973 --- /dev/null +++ b/tests/smod64-neg-by-zero-reg.data @@ -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 diff --git a/tests/smod64-pos-by-neg-imm.data b/tests/smod64-pos-by-neg-imm.data new file mode 100644 index 0000000..9b3e4d0 --- /dev/null +++ b/tests/smod64-pos-by-neg-imm.data @@ -0,0 +1,8 @@ +# Copyright (c) Dave Thaler +# SPDX-License-Identifier: MIT +-- asm +mov %r0, 13 +smod %r0, -3 +exit +-- result +0x1 diff --git a/tests/smod64-pos-by-neg-reg.data b/tests/smod64-pos-by-neg-reg.data new file mode 100644 index 0000000..42e3307 --- /dev/null +++ b/tests/smod64-pos-by-neg-reg.data @@ -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