eBPF for Windows
bpf2c.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation
2 // SPDX-License-Identifier: MIT
3 
4 #pragma once
5 #if defined(NO_CRT)
6 typedef signed char int8_t;
7 typedef short int16_t;
8 typedef int int32_t;
9 typedef long long int64_t;
10 typedef unsigned char uint8_t;
11 typedef unsigned short uint16_t;
12 typedef unsigned int uint32_t;
13 typedef unsigned long long uint64_t;
14 #define bool _Bool
15 #define false 0
16 #define true 1
17 #define UINT32_MAX ((uint32_t)0xFFFFFFFF)
18 
19 #else
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #endif
24 
25 #include "ebpf_structs.h"
26 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #define UBPF_STACK_SIZE 512
33 
34 #define IMMEDIATE(X) (int32_t) X
35 #define OFFSET(X) (int16_t) X
36 #define POINTER(X) (uint64_t)(X)
37 
38 #if !defined(htobe16)
39 #define htobe16(X) swap16(X)
40 #define htobe32(X) swap32(X)
41 #define htobe64(X) swap64(X)
42 
43 #define htole16(X) (X)
44 #define htole32(X) (X)
45 #define htole64(X) (X)
46 #endif
47 
48  typedef struct _helper_function_entry
49  {
50  uint64_t (*address)(uint64_t r1, uint64_t r2, uint64_t r3, uint64_t r4, uint64_t r5);
51  uint32_t helper_id;
52  const char* name;
53  bool tail_call;
55 
56  typedef struct _map_entry
57  {
58  void* address;
60  const char* name;
61  } map_entry_t;
62 
63  typedef struct _program_entry
64  {
65  // DLLs put the strings into the same section, so add a marker
66  // at the start of a program entry to make it easy to find
67  // entries in the programs section.
68  uint64_t zero;
69 
70  uint64_t (*function)(void*);
71  const char* pe_section_name;
72  const char* section_name;
73  const char* program_name;
77  uint16_t helper_count;
81  const uint8_t* program_info_hash;
84 
85  typedef struct _metadata_table
86  {
87  void (*programs)(_Outptr_result_buffer_maybenull_(*count) program_entry_t** programs, _Out_ size_t* count);
88  void (*maps)(_Outptr_result_buffer_maybenull_(*count) map_entry_t** maps, _Out_ size_t* count);
89  void (*hash)(_Outptr_result_buffer_maybenull_(*size) const uint8_t** hash, _Out_ size_t* size);
91 
92  inline uint16_t
93  swap16(uint16_t value)
94  {
95  return value << 8 | value >> 8;
96  }
97 
98  inline uint32_t
99  swap32(uint32_t value)
100  {
101  return swap16(value >> 16) | ((uint32_t)swap16(value & ((1 << 16) - 1))) << 16;
102  }
103 
104  inline uint64_t
105  swap64(uint64_t value)
106  {
107  return swap32(value >> 32) | ((uint64_t)swap32(value & ((1ull << 32ull) - 1))) << 32;
108  }
109 
110  void
111  division_by_zero(uint32_t address);
112 
113 #ifdef __cplusplus
114 }
115 #endif
_program_entry::pe_section_name
const char * pe_section_name
Definition: bpf2c.h:71
metadata_table_t
struct _metadata_table metadata_table_t
_program_entry::program_type
ebpf_program_type_t * program_type
Definition: bpf2c.h:79
_program_entry::referenced_map_count
uint16_t referenced_map_count
Definition: bpf2c.h:75
_program_entry::program_info_hash
const uint8_t * program_info_hash
Definition: bpf2c.h:81
program_entry_t
struct _program_entry program_entry_t
_metadata_table::programs
void(* programs)((*count) program_entry_t **programs, size_t *count)
Definition: bpf2c.h:87
_map_entry::definition
ebpf_map_definition_in_file_t definition
Definition: bpf2c.h:59
_map_entry::address
void * address
Definition: bpf2c.h:58
map_entry_t
struct _map_entry map_entry_t
_helper_function_entry::tail_call
bool tail_call
Definition: bpf2c.h:53
swap16
uint16_t swap16(uint16_t value)
Definition: bpf2c.h:93
_map_entry::name
const char * name
Definition: bpf2c.h:60
_metadata_table::hash
void(* hash)((*size) const uint8_t **hash, size_t *size)
Definition: bpf2c.h:89
_ebpf_map_definition_in_file
eBPF Map Definition as it appears in the maps section of an ELF file.
Definition: ebpf_structs.h:114
_program_entry::program_name
const char * program_name
Definition: bpf2c.h:73
_program_entry::zero
uint64_t zero
Definition: bpf2c.h:68
_metadata_table
Definition: bpf2c.h:85
helper_function_entry_t
struct _helper_function_entry helper_function_entry_t
ebpf_program_type_t
GUID ebpf_program_type_t
Definition: ebpf_windows.h:38
swap32
uint32_t swap32(uint32_t value)
Definition: bpf2c.h:99
_program_entry::expected_attach_type
ebpf_attach_type_t * expected_attach_type
Definition: bpf2c.h:80
_helper_function_entry
Definition: bpf2c.h:48
_program_entry::program_info_hash_length
size_t program_info_hash_length
Definition: bpf2c.h:82
_program_entry::bpf_instruction_count
size_t bpf_instruction_count
Definition: bpf2c.h:78
swap64
uint64_t swap64(uint64_t value)
Definition: bpf2c.h:105
division_by_zero
void division_by_zero(uint32_t address)
_helper_function_entry::address
uint64_t(* address)(uint64_t r1, uint64_t r2, uint64_t r3, uint64_t r4, uint64_t r5)
Definition: bpf2c.h:50
_program_entry::helpers
helper_function_entry_t * helpers
Definition: bpf2c.h:76
_helper_function_entry::name
const char * name
Definition: bpf2c.h:52
_program_entry::referenced_map_indices
uint16_t * referenced_map_indices
Definition: bpf2c.h:74
_program_entry
Definition: bpf2c.h:63
_helper_function_entry::helper_id
uint32_t helper_id
Definition: bpf2c.h:51
ebpf_structs.h
_program_entry::section_name
const char * section_name
Definition: bpf2c.h:72
_metadata_table::maps
void(* maps)((*count) map_entry_t **maps, size_t *count)
Definition: bpf2c.h:88
stddef.h
_program_entry::helper_count
uint16_t helper_count
Definition: bpf2c.h:77
ebpf_attach_type_t
GUID ebpf_attach_type_t
Definition: ebpf_windows.h:39
_map_entry
Definition: bpf2c.h:56