Maciej Żenczykowski | 730a386 | 2020-01-27 01:10:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | /* This file is separate because it's included both by eBPF programs (via include |
| 20 | * in bpf_helpers.h) and directly by the boot time bpfloader (Loader.cpp). |
| 21 | */ |
| 22 | |
| 23 | #include <linux/bpf.h> |
| 24 | |
Maciej Żenczykowski | 56bf76b | 2020-02-18 14:44:06 -0800 | [diff] [blame] | 25 | // Pull in AID_* constants from //system/core/libcutils/include/private/android_filesystem_config.h |
Maciej Żenczykowski | 56bf76b | 2020-02-18 14:44:06 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
Maciej Żenczykowski | 6f87896 | 2020-01-27 02:58:29 -0800 | [diff] [blame] | 27 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 28 | /****************************************************************************** |
| 29 | * * |
| 30 | * ! ! ! W A R N I N G ! ! ! * |
| 31 | * * |
| 32 | * CHANGES TO THESE STRUCTURE DEFINITIONS OUTSIDE OF AOSP/MASTER *WILL* BREAK * |
| 33 | * MAINLINE MODULE COMPATIBILITY * |
| 34 | * * |
| 35 | * AND THUS MAY RESULT IN YOUR DEVICE BRICKING AT SOME ARBITRARY POINT IN * |
| 36 | * THE FUTURE * |
| 37 | * * |
| 38 | * (and even in aosp/master you may only append new fields at the very end, * |
| 39 | * you may *never* delete fields, change their types, ordering, insert in * |
| 40 | * the middle, etc. If a mainline module using the old definition has * |
| 41 | * already shipped (which happens roughly monthly), then it's set in stone) * |
| 42 | * * |
| 43 | ******************************************************************************/ |
| 44 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 45 | // These are the values used if these fields are missing |
Maciej Żenczykowski | a21256d | 2021-07-02 00:40:55 -0700 | [diff] [blame] | 46 | #define DEFAULT_BPFLOADER_MIN_VER 0u // v0.0 (this is inclusive ie. >= v0.0) |
| 47 | #define DEFAULT_BPFLOADER_MAX_VER 0x10000u // v1.0 (this is exclusive ie. < v1.0) |
Maciej Żenczykowski | df91d2b | 2021-07-05 13:51:15 -0700 | [diff] [blame] | 48 | #define DEFAULT_SIZEOF_BPF_MAP_DEF 32 // v0.0 struct: enum (uint sized) + 7 uint |
| 49 | #define DEFAULT_SIZEOF_BPF_PROG_DEF 20 // v0.0 struct: 4 uint + bool + 3 byte alignment pad |
| 50 | |
| 51 | /* |
| 52 | * The bpf_{map,prog}_def structures are compiled for different architectures. |
| 53 | * Once by the BPF compiler for the BPF architecture, and once by a C++ |
| 54 | * compiler for the native Android architecture for the bpfloader. |
| 55 | * |
| 56 | * For things to work, their layout must be the same between the two. |
| 57 | * The BPF architecture is platform independent ('64-bit LSB bpf'). |
| 58 | * So this effectively means these structures must be the same layout |
| 59 | * on 5 architectures, all of them little endian: |
| 60 | * 64-bit BPF, x86_64, arm and 32-bit x86 and arm |
| 61 | * |
| 62 | * As such for any types we use inside of these structs we must make sure that |
| 63 | * the size and alignment are the same, so the same amount of padding is used. |
| 64 | * |
| 65 | * Currently we only use: bool, enum bpf_map_type and unsigned int. |
| 66 | * Additionally we use char for padding. |
| 67 | * |
| 68 | * !!! WARNING: HERE BE DRAGONS !!! |
| 69 | * |
| 70 | * Be particularly careful with 64-bit integers. |
| 71 | * You will need to manually override their alignment to 8 bytes. |
| 72 | * |
| 73 | * To quote some parts of https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69560 |
| 74 | * |
| 75 | * Some types have weaker alignment requirements when they are structure members. |
| 76 | * |
| 77 | * unsigned long long on x86 is such a type. |
| 78 | * |
| 79 | * C distinguishes C11 _Alignof (the minimum alignment the type is guaranteed |
| 80 | * to have in all contexts, so 4, see min_align_of_type) from GNU C __alignof |
| 81 | * (the normal alignment of the type, so 8). |
| 82 | * |
| 83 | * alignof / _Alignof == minimum alignment required by target ABI |
| 84 | * __alignof / __alignof__ == preferred alignment |
| 85 | * |
| 86 | * When in a struct, apparently the minimum alignment is used. |
| 87 | */ |
| 88 | |
| 89 | _Static_assert(sizeof(bool) == 1, "sizeof bool != 1"); |
| 90 | _Static_assert(__alignof__(bool) == 1, "__alignof__ bool != 1"); |
| 91 | _Static_assert(_Alignof(bool) == 1, "_Alignof bool != 1"); |
| 92 | |
| 93 | _Static_assert(sizeof(char) == 1, "sizeof char != 1"); |
| 94 | _Static_assert(__alignof__(char) == 1, "__alignof__ char != 1"); |
| 95 | _Static_assert(_Alignof(char) == 1, "_Alignof char != 1"); |
| 96 | |
| 97 | // This basically verifies that an enum is 'just' a 32-bit int |
| 98 | _Static_assert(sizeof(enum bpf_map_type) == 4, "sizeof enum bpf_map_type != 4"); |
| 99 | _Static_assert(__alignof__(enum bpf_map_type) == 4, "__alignof__ enum bpf_map_type != 4"); |
| 100 | _Static_assert(_Alignof(enum bpf_map_type) == 4, "_Alignof enum bpf_map_type != 4"); |
| 101 | |
| 102 | // Linux kernel requires sizeof(int) == 4, sizeof(void*) == sizeof(long), sizeof(long long) == 8 |
| 103 | _Static_assert(sizeof(unsigned int) == 4, "sizeof unsigned int != 4"); |
| 104 | _Static_assert(__alignof__(unsigned int) == 4, "__alignof__ unsigned int != 4"); |
| 105 | _Static_assert(_Alignof(unsigned int) == 4, "_Alignof unsigned int != 4"); |
| 106 | |
| 107 | // We don't currently use any 64-bit types in these structs, so this is purely to document issue. |
| 108 | // Here sizeof & __alignof__ are consistent, but _Alignof is not: compile for 'aosp_cf_x86_phone' |
| 109 | _Static_assert(sizeof(unsigned long long) == 8, "sizeof unsigned long long != 8"); |
| 110 | _Static_assert(__alignof__(unsigned long long) == 8, "__alignof__ unsigned long long != 8"); |
| 111 | // BPF wants 8, but 32-bit x86 wants 4 |
| 112 | //_Static_assert(_Alignof(unsigned long long) == 8, "_Alignof unsigned long long != 8"); |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 113 | |
Maciej Żenczykowski | 730a386 | 2020-01-27 01:10:48 -0800 | [diff] [blame] | 114 | /* |
| 115 | * Map structure to be used by Android eBPF C programs. The Android eBPF loader |
| 116 | * uses this structure from eBPF object to create maps at boot time. |
| 117 | * |
| 118 | * The eBPF C program should define structure in the maps section using |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame] | 119 | * SECTION("maps") otherwise it will be ignored by the eBPF loader. |
Maciej Żenczykowski | 730a386 | 2020-01-27 01:10:48 -0800 | [diff] [blame] | 120 | * |
| 121 | * For example: |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame] | 122 | * const struct bpf_map_def SECTION("maps") mymap { .type=... , .key_size=... } |
Maciej Żenczykowski | 730a386 | 2020-01-27 01:10:48 -0800 | [diff] [blame] | 123 | * |
| 124 | * See 'bpf_helpers.h' for helpful macros for eBPF program use. |
| 125 | */ |
| 126 | struct bpf_map_def { |
| 127 | enum bpf_map_type type; |
| 128 | unsigned int key_size; |
| 129 | unsigned int value_size; |
| 130 | unsigned int max_entries; |
| 131 | unsigned int map_flags; |
| 132 | |
| 133 | // The following are not supported by the Android bpfloader: |
| 134 | // unsigned int inner_map_idx; |
| 135 | // unsigned int numa_node; |
Maciej Żenczykowski | 83f2977 | 2020-01-27 03:11:51 -0800 | [diff] [blame] | 136 | |
| 137 | unsigned int uid; // uid_t |
| 138 | unsigned int gid; // gid_t |
| 139 | unsigned int mode; // mode_t |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 140 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 141 | // The following fields were added in version 0.1 |
| 142 | unsigned int bpfloader_min_ver; // if missing, defaults to 0, ie. v0.0 |
| 143 | unsigned int bpfloader_max_ver; // if missing, defaults to 0x10000, ie. v1.0 |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 144 | |
| 145 | // The following fields were added in version 0.2 |
| 146 | // kernelVersion() must be >= min_kver and < max_kver |
| 147 | unsigned int min_kver; |
| 148 | unsigned int max_kver; |
Maciej Żenczykowski | 730a386 | 2020-01-27 01:10:48 -0800 | [diff] [blame] | 149 | }; |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 150 | |
Maciej Żenczykowski | df91d2b | 2021-07-05 13:51:15 -0700 | [diff] [blame] | 151 | // This needs to be updated whenever the above structure definition is expanded. |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 152 | _Static_assert(sizeof(struct bpf_map_def) == 48, "sizeof struct bpf_map_def != 48"); |
Maciej Żenczykowski | df91d2b | 2021-07-05 13:51:15 -0700 | [diff] [blame] | 153 | _Static_assert(__alignof__(struct bpf_map_def) == 4, "__alignof__ struct bpf_map_def != 4"); |
| 154 | _Static_assert(_Alignof(struct bpf_map_def) == 4, "_Alignof struct bpf_map_def != 4"); |
| 155 | |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 156 | struct bpf_prog_def { |
| 157 | unsigned int uid; |
| 158 | unsigned int gid; |
Maciej Żenczykowski | 07375e2 | 2020-02-19 14:23:59 -0800 | [diff] [blame] | 159 | |
Maciej Żenczykowski | fd59a4a | 2021-03-02 18:37:33 -0800 | [diff] [blame] | 160 | // kernelVersion() must be >= min_kver and < max_kver |
| 161 | unsigned int min_kver; |
| 162 | unsigned int max_kver; |
Maciej Żenczykowski | aa295c8 | 2020-06-16 17:02:48 -0700 | [diff] [blame] | 163 | |
Maciej Żenczykowski | fd59a4a | 2021-03-02 18:37:33 -0800 | [diff] [blame] | 164 | bool optional; // program section (ie. function) may fail to load, continue onto next func. |
Maciej Żenczykowski | df91d2b | 2021-07-05 13:51:15 -0700 | [diff] [blame] | 165 | char pad0[3]; |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 166 | |
Maciej Żenczykowski | 9217eee | 2021-03-03 05:28:52 -0800 | [diff] [blame] | 167 | // The following fields were added in version 0.1 |
| 168 | unsigned int bpfloader_min_ver; // if missing, defaults to 0, ie. v0.0 |
| 169 | unsigned int bpfloader_max_ver; // if missing, defaults to 0x10000, ie. v1.0 |
Maciej Żenczykowski | 36c53ba | 2021-07-05 15:20:34 -0700 | [diff] [blame] | 170 | |
| 171 | // No new fields in version 0.2 |
Connor O'Brien | 3278a16 | 2020-02-13 21:45:22 -0800 | [diff] [blame] | 172 | }; |
Maciej Żenczykowski | df91d2b | 2021-07-05 13:51:15 -0700 | [diff] [blame] | 173 | |
| 174 | // This needs to be updated whenever the above structure definition is expanded. |
| 175 | _Static_assert(sizeof(struct bpf_prog_def) == 28, "sizeof struct bpf_prog_def != 28"); |
| 176 | _Static_assert(__alignof__(struct bpf_prog_def) == 4, "__alignof__ struct bpf_prog_def != 4"); |
| 177 | _Static_assert(_Alignof(struct bpf_prog_def) == 4, "_Alignof struct bpf_prog_def != 4"); |