15875cf4cSArnaldo Carvalho de Melo// SPDX-License-Identifier: GPL-2.0
2bbb7d492SWang Nan/*
3bbb7d492SWang Nan * bpf-script-test-prologue.c
4bbb7d492SWang Nan * Test BPF prologue
5bbb7d492SWang Nan */
6bbb7d492SWang Nan#ifndef LINUX_VERSION_CODE
7bbb7d492SWang Nan# error Need LINUX_VERSION_CODE
8bbb7d492SWang Nan# error Example: for 4.2 kernel, put 'clang-opt="-DLINUX_VERSION_CODE=0x40200" into llvm section of ~/.perfconfig'
9bbb7d492SWang Nan#endif
10bbb7d492SWang Nan#define SEC(NAME) __attribute__((section(NAME), used))
11bbb7d492SWang Nan
12bbb7d492SWang Nan#include <uapi/linux/fs.h>
13bbb7d492SWang Nan
149b57fb7eSWang Nan/*
159b57fb7eSWang Nan * If CONFIG_PROFILE_ALL_BRANCHES is selected,
169b57fb7eSWang Nan * 'if' is redefined after include kernel header.
179b57fb7eSWang Nan * Recover 'if' for BPF object code.
189b57fb7eSWang Nan */
199b57fb7eSWang Nan#ifdef if
209b57fb7eSWang Nan# undef if
219b57fb7eSWang Nan#endif
229b57fb7eSWang Nan
23bbb7d492SWang Nan#define FMODE_READ		0x1
24bbb7d492SWang Nan#define FMODE_WRITE		0x2
25bbb7d492SWang Nan
26bbb7d492SWang Nanstatic void (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
27bbb7d492SWang Nan	(void *) 6;
28bbb7d492SWang Nan
29bbb7d492SWang NanSEC("func=null_lseek file->f_mode offset orig")
30db26984aSWang Nanint bpf_func__null_lseek(void *ctx, int err, unsigned long _f_mode,
31bbb7d492SWang Nan			 unsigned long offset, unsigned long orig)
32bbb7d492SWang Nan{
33db26984aSWang Nan	fmode_t f_mode = (fmode_t)_f_mode;
34db26984aSWang Nan
35bbb7d492SWang Nan	if (err)
36bbb7d492SWang Nan		return 0;
37bbb7d492SWang Nan	if (f_mode & FMODE_WRITE)
38bbb7d492SWang Nan		return 0;
39bbb7d492SWang Nan	if (offset & 1)
40bbb7d492SWang Nan		return 0;
41bbb7d492SWang Nan	if (orig == SEEK_CUR)
42bbb7d492SWang Nan		return 0;
43bbb7d492SWang Nan	return 1;
44bbb7d492SWang Nan}
45bbb7d492SWang Nan
46bbb7d492SWang Nanchar _license[] SEC("license") = "GPL";
47bbb7d492SWang Nanint _version SEC("version") = LINUX_VERSION_CODE;
48