Commit e54e772f authored by Ze Zuo's avatar Ze Zuo
Browse files

samples/bpf: Add program for memory access by spe

hulk inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I9GZAQ


CVE: NA

--------------------------------

spe record ring buffer just for ctx origin flaw.

Signed-off-by: default avatarZe Zuo <zuoze1@huawei.com>
parent f71523ca
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0

include Makefile.arch

INSTALL ?= install
CLANG ?= clang
CC ?= gcc

BPFTOOL ?= bpftool
KERNEL_DIR ?= ../../../

MKFLAGS = -I$(KERNEL_DIR)/tools/lib -I$(KERNEL_DIR)/tools/include/uapi/ \
	-D__BPF_TRACING__ -D__TARGET_ARCH_${SRCARCH}
LDLIBBPF = -L$(KERNEL_DIR)/tools/lib/bpf/ -l:libbpf.a

all:
	$(CLANG) -O2 -g -Wall -target bpf -I. ${MKFLAGS} -c spe-mem-sampling-record.bpf.c -o spe-mem-sampling-record.bpf.o
	$(BPFTOOL) gen skeleton spe-mem-sampling-record.bpf.o > spe-mem-sampling-record.skel.h
	$(CC) -O2 -g -Wall ${MKFLAGS} spe-mem-sampling-record.user.c -o spe-mem-sampling-record ${LDLIBBPF} -lelf -lz --static

clean:
	rm -f spe-mem-sampling-record
	rm -f vmlinux.h
	rm -f *.o
	rm -f *.skel.h
+45 −0
Original line number Diff line number Diff line
HOSTARCH := $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ \
                                  -e s/sun4u/sparc/ -e s/sparc64/sparc/ \
                                  -e /arm64/!s/arm.*/arm/ -e s/sa110/arm/ \
                                  -e s/s390x/s390/ -e s/parisc64/parisc/ \
                                  -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
                                  -e s/sh[234].*/sh/ -e s/aarch64.*/arm64/ \
                                  -e s/riscv.*/riscv/ -e s/loongarch.*/loongarch/)

ifndef ARCH
ARCH := $(HOSTARCH)
endif

SRCARCH := $(ARCH)

# Additional ARCH settings for x86
ifeq ($(ARCH),i386)
        SRCARCH := x86
endif
ifeq ($(ARCH),x86_64)
        SRCARCH := x86
endif

# Additional ARCH settings for sparc
ifeq ($(ARCH),sparc32)
       SRCARCH := sparc
endif
ifeq ($(ARCH),sparc64)
       SRCARCH := sparc
endif

# Additional ARCH settings for loongarch
ifeq ($(ARCH),loongarch32)
       SRCARCH := loongarch
endif

ifeq ($(ARCH),loongarch64)
       SRCARCH := loongarch
endif

LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1)
ifeq ($(LP64), 1)
  IS_64_BIT := 1
else
  IS_64_BIT := 0
endif
+0 −0

Empty file added.

+38 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2024, Huawei Technologies Ltd */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include "spe-mem-sampling-record.h"

char LICENSE[] SEC("license") = "Dual BSD/GPL";

/* BPF ringbuf map */
struct {
	__uint(type, BPF_MAP_TYPE_RINGBUF);
	__uint(max_entries, 256 * 1024 /* 256 KB */);
} rb SEC(".maps");

SEC("raw_tracepoint/spe_record")
int handle_exec(struct bpf_raw_tracepoint_args *ctx)
{
	// TP_PROTO(struct mem_sampling_record *record)
	struct mem_sampling_record *rd = (struct mem_sampling_record *)ctx->args[0];
	struct event *e;

	e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
	if (!e)
		return 0;

	if (bpf_get_current_comm(e->comm, sizeof(e->comm)))
		e->comm[0] = 0;

	e->context_id = BPF_CORE_READ(rd, context_id);
	e->virt_addr = BPF_CORE_READ(rd, virt_addr);
	e->phys_addr = BPF_CORE_READ(rd, phys_addr);
	e->latency = BPF_CORE_READ(rd, latency);

	bpf_ringbuf_submit(e, 0);
	return 0;
}
+47 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
/* Copyright (c) 2024, Huawei Technologies Ltd */
#ifndef __SPE_RECORD_H
#define __SPE_RECORD_H

enum mem_sampling_sample_type {
	MEM_SAMPLING_L1D_ACCESS		= 1 << 0,
	MEM_SAMPLING_L1D_MISS		= 1 << 1,
	MEM_SAMPLING_LLC_ACCESS		= 1 << 2,
	MEM_SAMPLING_LLC_MISS		= 1 << 3,
	MEM_SAMPLING_TLB_ACCESS		= 1 << 4,
	MEM_SAMPLING_TLB_MISS		= 1 << 5,
	MEM_SAMPLING_BRANCH_MISS	= 1 << 6,
	MEM_SAMPLING_REMOTE_ACCESS	= 1 << 7,
};

struct mem_sampling_record {
	enum mem_sampling_sample_type	type;
	int				err;
	unsigned int			op;
	unsigned int			latency;
	unsigned long long		from_ip;
	unsigned long long		to_ip;
	unsigned long long		timestamp;
	unsigned long long		virt_addr;
	unsigned long long		phys_addr;
	unsigned long long		context_id;
	unsigned char			source;
};

/* definition of a sample sent to user-space from BPF program */
struct event {
	enum mem_sampling_sample_type	type;
	int				err;
	unsigned int			op;
	unsigned int			latency;
	unsigned long long		from_ip;
	unsigned long long		to_ip;
	unsigned long long		timestamp;
	unsigned long long		virt_addr;
	unsigned long long		phys_addr;
	unsigned long long		context_id;
	unsigned char			source;
	char				comm[16];
};

#endif /* __SPE_RECORD_H */
Loading