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

test_map_lookup_percpu_elem.c (1719B)


      1// SPDX-License-Identifier: GPL-2.0
      2/* Copyright (c) 2022 Bytedance */
      3
      4#include "vmlinux.h"
      5#include <bpf/bpf_helpers.h>
      6
      7__u64 percpu_array_elem_sum = 0;
      8__u64 percpu_hash_elem_sum = 0;
      9__u64 percpu_lru_hash_elem_sum = 0;
     10const volatile int nr_cpus;
     11const volatile int my_pid;
     12
     13struct {
     14	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
     15	__uint(max_entries, 1);
     16	__type(key, __u32);
     17	__type(value, __u64);
     18} percpu_array_map SEC(".maps");
     19
     20struct {
     21	__uint(type, BPF_MAP_TYPE_PERCPU_HASH);
     22	__uint(max_entries, 1);
     23	__type(key, __u64);
     24	__type(value, __u64);
     25} percpu_hash_map SEC(".maps");
     26
     27struct {
     28	__uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
     29	__uint(max_entries, 1);
     30	__type(key, __u64);
     31	__type(value, __u64);
     32} percpu_lru_hash_map SEC(".maps");
     33
     34struct read_percpu_elem_ctx {
     35	void *map;
     36	__u64 sum;
     37};
     38
     39static int read_percpu_elem_callback(__u32 index, struct read_percpu_elem_ctx *ctx)
     40{
     41	__u64 key = 0;
     42	__u64 *value;
     43
     44	value = bpf_map_lookup_percpu_elem(ctx->map, &key, index);
     45	if (value)
     46		ctx->sum += *value;
     47	return 0;
     48}
     49
     50SEC("tp/syscalls/sys_enter_getuid")
     51int sysenter_getuid(const void *ctx)
     52{
     53	struct read_percpu_elem_ctx map_ctx;
     54
     55	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
     56		return 0;
     57
     58	map_ctx.map = &percpu_array_map;
     59	map_ctx.sum = 0;
     60	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
     61	percpu_array_elem_sum = map_ctx.sum;
     62
     63	map_ctx.map = &percpu_hash_map;
     64	map_ctx.sum = 0;
     65	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
     66	percpu_hash_elem_sum = map_ctx.sum;
     67
     68	map_ctx.map = &percpu_lru_hash_map;
     69	map_ctx.sum = 0;
     70	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
     71	percpu_lru_hash_elem_sum = map_ctx.sum;
     72
     73	return 0;
     74}
     75
     76char _license[] SEC("license") = "GPL";