xref: /linux/tools/testing/selftests/bpf/verifier/atomic_xor.c (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
198d666d0SBrendan Jackman {
298d666d0SBrendan Jackman 	"BPF_ATOMIC XOR without fetch",
398d666d0SBrendan Jackman 	.insns = {
498d666d0SBrendan Jackman 		/* val = 0x110; */
598d666d0SBrendan Jackman 		BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x110),
698d666d0SBrendan Jackman 		/* atomic_xor(&val, 0x011); */
798d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_1, 0x011),
898d666d0SBrendan Jackman 		BPF_ATOMIC_OP(BPF_DW, BPF_XOR, BPF_REG_10, BPF_REG_1, -8),
998d666d0SBrendan Jackman 		/* if (val != 0x101) exit(2); */
10*45df3052SIlya Leoshkevich 		BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_10, -8),
1198d666d0SBrendan Jackman 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0x101, 2),
1298d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 2),
1398d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
1498d666d0SBrendan Jackman 		/* r1 should not be clobbered, no BPF_FETCH flag */
1598d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 0),
1698d666d0SBrendan Jackman 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x011, 1),
1798d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 1),
1898d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
1998d666d0SBrendan Jackman 	},
2098d666d0SBrendan Jackman 	.result = ACCEPT,
2198d666d0SBrendan Jackman },
2298d666d0SBrendan Jackman {
2398d666d0SBrendan Jackman 	"BPF_ATOMIC XOR with fetch",
2498d666d0SBrendan Jackman 	.insns = {
2598d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 123),
2698d666d0SBrendan Jackman 		/* val = 0x110; */
2798d666d0SBrendan Jackman 		BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0x110),
2898d666d0SBrendan Jackman 		/* old = atomic_fetch_xor(&val, 0x011); */
2998d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_1, 0x011),
3098d666d0SBrendan Jackman 		BPF_ATOMIC_OP(BPF_DW, BPF_XOR | BPF_FETCH, BPF_REG_10, BPF_REG_1, -8),
3198d666d0SBrendan Jackman 		/* if (old != 0x110) exit(3); */
3298d666d0SBrendan Jackman 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x110, 2),
3398d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 3),
3498d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
3598d666d0SBrendan Jackman 		/* if (val != 0x101) exit(2); */
3698d666d0SBrendan Jackman 		BPF_LDX_MEM(BPF_DW, BPF_REG_1, BPF_REG_10, -8),
3798d666d0SBrendan Jackman 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_1, 0x101, 2),
3898d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_1, 2),
3998d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
4098d666d0SBrendan Jackman 		/* Check R0 wasn't clobbered (fxor fear of x86 JIT bug) */
4198d666d0SBrendan Jackman 		BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 123, 2),
4298d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 1),
4398d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
4498d666d0SBrendan Jackman 		/* exit(0); */
4598d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 0),
4698d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
4798d666d0SBrendan Jackman 	},
4898d666d0SBrendan Jackman 	.result = ACCEPT,
4998d666d0SBrendan Jackman },
5098d666d0SBrendan Jackman {
5198d666d0SBrendan Jackman 	"BPF_ATOMIC XOR with fetch 32bit",
5298d666d0SBrendan Jackman 	.insns = {
5398d666d0SBrendan Jackman 		/* r0 = (s64) -1 */
5498d666d0SBrendan Jackman 		BPF_MOV64_IMM(BPF_REG_0, 0),
5598d666d0SBrendan Jackman 		BPF_ALU64_IMM(BPF_SUB, BPF_REG_0, 1),
5698d666d0SBrendan Jackman 		/* val = 0x110; */
5798d666d0SBrendan Jackman 		BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0x110),
5898d666d0SBrendan Jackman 		/* old = atomic_fetch_xor(&val, 0x011); */
5998d666d0SBrendan Jackman 		BPF_MOV32_IMM(BPF_REG_1, 0x011),
6098d666d0SBrendan Jackman 		BPF_ATOMIC_OP(BPF_W, BPF_XOR | BPF_FETCH, BPF_REG_10, BPF_REG_1, -4),
6198d666d0SBrendan Jackman 		/* if (old != 0x110) exit(3); */
6298d666d0SBrendan Jackman 		BPF_JMP32_IMM(BPF_JEQ, BPF_REG_1, 0x110, 2),
6398d666d0SBrendan Jackman 		BPF_MOV32_IMM(BPF_REG_0, 3),
6498d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
6598d666d0SBrendan Jackman 		/* if (val != 0x101) exit(2); */
6698d666d0SBrendan Jackman 		BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_10, -4),
6798d666d0SBrendan Jackman 		BPF_JMP32_IMM(BPF_JEQ, BPF_REG_1, 0x101, 2),
6898d666d0SBrendan Jackman 		BPF_MOV32_IMM(BPF_REG_1, 2),
6998d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
7098d666d0SBrendan Jackman 		/* Check R0 wasn't clobbered (fxor fear of x86 JIT bug)
7198d666d0SBrendan Jackman 		 * It should be -1 so add 1 to get exit code.
7298d666d0SBrendan Jackman 		 */
7398d666d0SBrendan Jackman 		BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1),
7498d666d0SBrendan Jackman 		BPF_EXIT_INSN(),
7598d666d0SBrendan Jackman 	},
7698d666d0SBrendan Jackman 	.result = ACCEPT,
7798d666d0SBrendan Jackman },
78