aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi-raw / api_guidelines.md
| 1 | # API Guidelines |
| 2 | |
| 3 | The `uefi-raw` crate should closely match the definitions in the [UEFI |
| 4 | Specification], with only some light changes to make it more friendly for use in |
| 5 | Rust (e.g. casing follows Rust's conventions and modules are used to provide |
| 6 | some hierarchy). Where the spec is inconsistent or inaccurate, the EDK2 headers |
| 7 | and sources are consulted. |
| 8 | |
| 9 | This document describes the API rules in detail. Some of these rules can be |
| 10 | checked with `cargo xtask check-raw`, and that check is run automatically in CI |
| 11 | as well. Other rules require human verification. |
| 12 | |
| 13 | If you are contributing to this crate and run into any problems, such as a case |
| 14 | that isn't covered by the rules, or a case where following the rules seems like |
| 15 | it will lead to a bad API, don't hesitate to let us know (e.g. by filing an |
| 16 | [issue]). |
| 17 | |
| 18 | ## Naming |
| 19 | |
| 20 | Type names should match the corresponding spec names, but drop the `EFI_` prefix |
| 21 | and change to `UpperCamelCase` to match Rust's convention. For example, |
| 22 | `EFI_LOAD_FILE_PROTOCOL` becomes `LoadFileProtocol`. |
| 23 | |
| 24 | Struct field names and function parameter names should match the corresponding |
| 25 | spec names, but change the case to `snake_case` to match Rust's convention. |
| 26 | |
| 27 | When defining a type that isn't part of the spec (for example, a `bitflags!` |
| 28 | type that represents a collection of constants in the spec), prefix the name |
| 29 | with a closely-associated type that is defined in the spec. For example, mode |
| 30 | constants for a `FooBarProtocol` could be collected into a `FooBarMode` type. |
| 31 | |
| 32 | It's OK to introduce minor naming changes from the specification where it |
| 33 | improves clarity. |
| 34 | |
| 35 | ## Layout |
| 36 | |
| 37 | All types must be `repr(C)`, `repr(C, packed)`, `repr(C, align(N))`, or |
| 38 | `repr(transparent)`. |
| 39 | |
| 40 | Types created with the `bitflags!` macro must set `repr(transparent)`. |
| 41 | |
| 42 | ### Dynamically Sized Types |
| 43 | |
| 44 | Some types in the spec end with a variable-length array. It's possible to |
| 45 | represent these as [Dynamically Sized Types], but that should be left to |
| 46 | higher-level APIs. In this crate, add a zero-length array at the end of the |
| 47 | struct to represent the field. For example, if a struct in the spec ends with |
| 48 | `CHAR16 Name[];`, represent that in Rust with `name: [Char16; 0]`. |
| 49 | |
| 50 | This pattern of using a `&Header` to work with dynamically-sized data is |
| 51 | rejected by the Stacked Borrows model, but allowed by Tree Borrows. See [UCG |
| 52 | issue 256] for more info. |
| 53 | |
| 54 | ## Visibility |
| 55 | |
| 56 | Everything must have `pub` visibility. |
| 57 | |
| 58 | ## Constants |
| 59 | |
| 60 | Use associated constants where possible instead of top-level constants. |
| 61 | |
| 62 | Protocols must have an associated `GUID` constant, for example: |
| 63 | |
| 64 | ```rust |
| 65 | impl RngProtocol { |
| 66 | pub const GUID: Guid = guid!("3152bca5-eade-433d-862e-c01cdc291f44"); |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | ## Pointers |
| 71 | |
| 72 | Use pointers (`*const`/`*mut`) instead of references (`&`/`&mut`). |
| 73 | |
| 74 | Function pointers must be `unsafe` and have an explicit ABI (almost always |
| 75 | `efiapi`). If a function pointer field can be null it must be wrapped in |
| 76 | `Option`. Most function pointer fields do not need to allow null pointers |
| 77 | though, unless the spec says otherwise. |
| 78 | |
| 79 | ### Mutability |
| 80 | |
| 81 | Pointer mutability (`*mut` vs `*const`) is not a UB concern the way reference |
| 82 | mutability is. In general, it is not UB to `cast_mut` a const pointer and write |
| 83 | through it. So picking `*mut` vs `*const` is more about semantics. |
| 84 | |
| 85 | Two questions decide the type: who writes through the pointer, and who owns the |
| 86 | pointee. `*mut` means "written by the firmware" or "owned by the caller, who |
| 87 | must free it", whereas `*const` means read-only for both sides. |
| 88 | |
| 89 | Pointer fields in structs follow the same rule. Pointers to firmware-owned |
| 90 | structures that consumers must only read, such as the `Mode` pointer of a |
| 91 | protocol, are generally still `*mut`. Callers that just read such structure can |
| 92 | use `cast_const()`. |
| 93 | |
| 94 | In function parameters, pick between `*const` and `*mut` based on how the |
| 95 | parameter is described in the spec. An `OUT` or `IN OUT` pointer must be |
| 96 | `*mut`. An `IN` pointer is `*const` if the parameter is described as being |
| 97 | source data, but `*mut` if the spec text says that the pointee is written, for |
| 98 | example `Buffer` of `EFI_LOAD_FILE_PROTOCOL.LoadFile()`. |
| 99 | |
| 100 | ### Mapping of C Qualifiers |
| 101 | |
| 102 | The spec annotates parameters with `IN`, `OUT`, `IN OUT`, `CONST` and |
| 103 | `OPTIONAL`. In the following tables, `T` is a data type and `F` is a function |
| 104 | pointer type such as `EFI_EVENT_NOTIFY`. |
| 105 | |
| 106 | | C | Rust | |
| 107 | |----------------------------------|------------------------------------------| |
| 108 | | `IN T Name` | `T` | |
| 109 | | `IN T *Name`, `IN CONST T *Name` | `*const T` | |
| 110 | | `IN T *Name OPTIONAL` | `*const T` | |
| 111 | | `OUT T *Name`, `IN OUT T *Name` | `*mut T` | |
| 112 | | `OUT T *Name OPTIONAL` | `*mut T` | |
| 113 | | `IN F Name` | `unsafe extern "efiapi" fn(...)` | |
| 114 | | `IN F Name OPTIONAL` | `Option<unsafe extern "efiapi" fn(...)>` | |
| 115 | |
| 116 | `OPTIONAL` only changes the Rust type for function pointers: a raw pointer can |
| 117 | already be null, whereas a bare `fn` pointer cannot, so it needs `Option`. |
| 118 | |
| 119 | Buffers (`VOID *Buffer` plus a length) follow the same rule: `*const c_void` if |
| 120 | the firmware reads them, `*mut c_void` if it writes them. Pointers to pointers |
| 121 | depend on who owns each level: |
| 122 | |
| 123 | | C | Rust | Explanation | |
| 124 | |--------------------------|------------------------|---------------------------------------| |
| 125 | | `OUT T **Name` | `*mut *mut T` | callee allocates, caller frees | |
| 126 | | `OUT T **Name` | `*mut *const T` | points into firmware-owned memory | |
| 127 | | `OUT T **Name` | `*const *mut T` | caller's array, firmware fills it | |
| 128 | | `IN OUT VOID *Name[N]` | `*const *mut c_void` | caller's buffers, firmware fills them | |
| 129 | | `IN CONST CHAR16 **Name` | `*const *const Char16` | read-only strings | |
| 130 | |
| 131 | This covers arrays too; the count parameter says how many elements the caller |
| 132 | owns, as in `LocateHandleBuffer`. `ProtocolsPerHandle` combines two rows, a |
| 133 | callee-allocated array of pointers into firmware-owned memory, hence |
| 134 | `*mut *mut *const Guid`. |
| 135 | |
| 136 | The same rule applies to return values: a pool allocation the caller must free |
| 137 | is returned as `*mut T`, but firmware-owned memory as `*const T`. |
| 138 | |
| 139 | The `This` parameter of a protocol function is `*const Self` if the function |
| 140 | only reads state, such as `QueryMode()` of `EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL`. |
| 141 | If the function logically modifies state, such as `Reset()` or `SetMode()`, it |
| 142 | is `*mut Self`, even if the protocol structure itself is not written. |
| 143 | |
| 144 | ## Allowed top-level items |
| 145 | |
| 146 | The allowed top-level items are `const`, `impl`, `macro`, `struct`, `type`, and |
| 147 | `union`. |
| 148 | |
| 149 | Rust `enum`s are not allowed; use the `bitflags!` or `newtype_enum!` macros |
| 150 | instead. |
| 151 | |
| 152 | [UEFI Specification]: https://uefi.org/specifications |
| 153 | [issue]: https://github.com/rust-osdev/uefi-rs/issues/new |
| 154 | [Dynamically Sized Types]: https://doc.rust-lang.org/reference/dynamically-sized-types.html |
| 155 | [UCG issue 256]: https://github.com/rust-lang/unsafe-code-guidelines/issues/256 |