cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

kfunc_call_test.c (2358B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright (c) 2021 Facebook */
      3#include <vmlinux.h>
      4#include <bpf/bpf_helpers.h>
      5
      6extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym;
      7extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b,
      8				  __u32 c, __u64 d) __ksym;
      9
     10extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire(unsigned long *sp) __ksym;
     11extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym;
     12extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym;
     13extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym;
     14extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym;
     15extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym;
     16extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym;
     17
     18SEC("tc")
     19int kfunc_call_test2(struct __sk_buff *skb)
     20{
     21	struct bpf_sock *sk = skb->sk;
     22
     23	if (!sk)
     24		return -1;
     25
     26	sk = bpf_sk_fullsock(sk);
     27	if (!sk)
     28		return -1;
     29
     30	return bpf_kfunc_call_test2((struct sock *)sk, 1, 2);
     31}
     32
     33SEC("tc")
     34int kfunc_call_test1(struct __sk_buff *skb)
     35{
     36	struct bpf_sock *sk = skb->sk;
     37	__u64 a = 1ULL << 32;
     38	__u32 ret;
     39
     40	if (!sk)
     41		return -1;
     42
     43	sk = bpf_sk_fullsock(sk);
     44	if (!sk)
     45		return -1;
     46
     47	a = bpf_kfunc_call_test1((struct sock *)sk, 1, a | 2, 3, a | 4);
     48	ret = a >> 32;   /* ret should be 2 */
     49	ret += (__u32)a; /* ret should be 12 */
     50
     51	return ret;
     52}
     53
     54SEC("tc")
     55int kfunc_call_test_ref_btf_id(struct __sk_buff *skb)
     56{
     57	struct prog_test_ref_kfunc *pt;
     58	unsigned long s = 0;
     59	int ret = 0;
     60
     61	pt = bpf_kfunc_call_test_acquire(&s);
     62	if (pt) {
     63		if (pt->a != 42 || pt->b != 108)
     64			ret = -1;
     65		bpf_kfunc_call_test_release(pt);
     66	}
     67	return ret;
     68}
     69
     70SEC("tc")
     71int kfunc_call_test_pass(struct __sk_buff *skb)
     72{
     73	struct prog_test_pass1 p1 = {};
     74	struct prog_test_pass2 p2 = {};
     75	short a = 0;
     76	__u64 b = 0;
     77	long c = 0;
     78	char d = 0;
     79	int e = 0;
     80
     81	bpf_kfunc_call_test_pass_ctx(skb);
     82	bpf_kfunc_call_test_pass1(&p1);
     83	bpf_kfunc_call_test_pass2(&p2);
     84
     85	bpf_kfunc_call_test_mem_len_pass1(&a, sizeof(a));
     86	bpf_kfunc_call_test_mem_len_pass1(&b, sizeof(b));
     87	bpf_kfunc_call_test_mem_len_pass1(&c, sizeof(c));
     88	bpf_kfunc_call_test_mem_len_pass1(&d, sizeof(d));
     89	bpf_kfunc_call_test_mem_len_pass1(&e, sizeof(e));
     90	bpf_kfunc_call_test_mem_len_fail2(&b, -1);
     91
     92	return 0;
     93}
     94
     95char _license[] SEC("license") = "GPL";