1*49859de9SYonghong Song // SPDX-License-Identifier: GPL-2.0 2*49859de9SYonghong Song 3*49859de9SYonghong Song #include <linux/bpf.h> 4*49859de9SYonghong Song #include <bpf/bpf_helpers.h> 5*49859de9SYonghong Song #include "bpf_misc.h" 6*49859de9SYonghong Song 7*49859de9SYonghong Song SEC("socket") 8*49859de9SYonghong Song __description("check w reg equal if r reg upper32 bits 0") 9*49859de9SYonghong Song __success subreg_equality_1(void)10*49859de9SYonghong Song__naked void subreg_equality_1(void) 11*49859de9SYonghong Song { 12*49859de9SYonghong Song asm volatile (" \ 13*49859de9SYonghong Song call %[bpf_ktime_get_ns]; \ 14*49859de9SYonghong Song *(u64 *)(r10 - 8) = r0; \ 15*49859de9SYonghong Song r2 = *(u32 *)(r10 - 8); \ 16*49859de9SYonghong Song /* At this point upper 4-bytes of r2 are 0, \ 17*49859de9SYonghong Song * thus insn w3 = w2 should propagate reg id, \ 18*49859de9SYonghong Song * and w2 < 9 comparison would also propagate \ 19*49859de9SYonghong Song * the range for r3. \ 20*49859de9SYonghong Song */ \ 21*49859de9SYonghong Song w3 = w2; \ 22*49859de9SYonghong Song if w2 < 9 goto l0_%=; \ 23*49859de9SYonghong Song exit; \ 24*49859de9SYonghong Song l0_%=: if r3 < 9 goto l1_%=; \ 25*49859de9SYonghong Song /* r1 read is illegal at this point */ \ 26*49859de9SYonghong Song r0 -= r1; \ 27*49859de9SYonghong Song l1_%=: exit; \ 28*49859de9SYonghong Song " : 29*49859de9SYonghong Song : __imm(bpf_ktime_get_ns) 30*49859de9SYonghong Song : __clobber_all); 31*49859de9SYonghong Song } 32*49859de9SYonghong Song 33*49859de9SYonghong Song SEC("socket") 34*49859de9SYonghong Song __description("check w reg not equal if r reg upper32 bits not 0") 35*49859de9SYonghong Song __failure __msg("R1 !read_ok") subreg_equality_2(void)36*49859de9SYonghong Song__naked void subreg_equality_2(void) 37*49859de9SYonghong Song { 38*49859de9SYonghong Song asm volatile (" \ 39*49859de9SYonghong Song call %[bpf_ktime_get_ns]; \ 40*49859de9SYonghong Song r2 = r0; \ 41*49859de9SYonghong Song /* Upper 4-bytes of r2 may not be 0, thus insn \ 42*49859de9SYonghong Song * w3 = w2 should not propagate reg id, and \ 43*49859de9SYonghong Song * w2 < 9 comparison should not propagate \ 44*49859de9SYonghong Song * the range for r3 either. \ 45*49859de9SYonghong Song */ \ 46*49859de9SYonghong Song w3 = w2; \ 47*49859de9SYonghong Song if w2 < 9 goto l0_%=; \ 48*49859de9SYonghong Song exit; \ 49*49859de9SYonghong Song l0_%=: if r3 < 9 goto l1_%=; \ 50*49859de9SYonghong Song /* r1 read is illegal at this point */ \ 51*49859de9SYonghong Song r0 -= r1; \ 52*49859de9SYonghong Song l1_%=: exit; \ 53*49859de9SYonghong Song " : 54*49859de9SYonghong Song : __imm(bpf_ktime_get_ns) 55*49859de9SYonghong Song : __clobber_all); 56*49859de9SYonghong Song } 57*49859de9SYonghong Song 58*49859de9SYonghong Song char _license[] SEC("license") = "GPL"; 59