Miguel Ojeda | 12f5772 | 2021-07-03 16:52:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions |
| 4 | * cannot be called either. This file explicitly creates functions ("helpers") |
| 5 | * that wrap those so that they can be called from Rust. |
| 6 | * |
| 7 | * Even though Rust kernel modules should never use directly the bindings, some |
| 8 | * of these helpers need to be exported because Rust generics and inlined |
| 9 | * functions may not get their code generated in the crate where they are |
| 10 | * defined. Other helpers, called from non-inline functions, may not be |
| 11 | * exported, in principle. However, in general, the Rust compiler does not |
| 12 | * guarantee codegen will be performed for a non-inline function either. |
| 13 | * Therefore, this file exports all the helpers. In the future, this may be |
| 14 | * revisited to reduce the number of exports after the compiler is informed |
| 15 | * about the places codegen is required. |
| 16 | * |
| 17 | * All symbols are exported as GPL-only to guarantee no GPL-only feature is |
| 18 | * accidentally exposed. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/bug.h> |
| 22 | #include <linux/build_bug.h> |
Asahi Lina | c7e20fa | 2023-04-03 18:48:11 +0900 | [diff] [blame] | 23 | #include <linux/err.h> |
Wedson Almeida Filho | 9dc0436 | 2022-12-28 06:03:40 +0000 | [diff] [blame] | 24 | #include <linux/refcount.h> |
Wedson Almeida Filho | 6d20d62 | 2023-04-11 02:45:33 -0300 | [diff] [blame^] | 25 | #include <linux/mutex.h> |
Miguel Ojeda | 12f5772 | 2021-07-03 16:52:41 +0200 | [diff] [blame] | 26 | |
| 27 | __noreturn void rust_helper_BUG(void) |
| 28 | { |
| 29 | BUG(); |
| 30 | } |
| 31 | EXPORT_SYMBOL_GPL(rust_helper_BUG); |
| 32 | |
Wedson Almeida Filho | 6d20d62 | 2023-04-11 02:45:33 -0300 | [diff] [blame^] | 33 | void rust_helper_mutex_lock(struct mutex *lock) |
| 34 | { |
| 35 | mutex_lock(lock); |
| 36 | } |
| 37 | EXPORT_SYMBOL_GPL(rust_helper_mutex_lock); |
| 38 | |
Wedson Almeida Filho | 9dc0436 | 2022-12-28 06:03:40 +0000 | [diff] [blame] | 39 | refcount_t rust_helper_REFCOUNT_INIT(int n) |
| 40 | { |
| 41 | return (refcount_t)REFCOUNT_INIT(n); |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT); |
| 44 | |
| 45 | void rust_helper_refcount_inc(refcount_t *r) |
| 46 | { |
| 47 | refcount_inc(r); |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(rust_helper_refcount_inc); |
| 50 | |
| 51 | bool rust_helper_refcount_dec_and_test(refcount_t *r) |
| 52 | { |
| 53 | return refcount_dec_and_test(r); |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); |
| 56 | |
Asahi Lina | c7e20fa | 2023-04-03 18:48:11 +0900 | [diff] [blame] | 57 | __force void *rust_helper_ERR_PTR(long err) |
| 58 | { |
| 59 | return ERR_PTR(err); |
| 60 | } |
| 61 | EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); |
| 62 | |
Sven Van Asbroeck | 752417b | 2023-04-03 18:48:14 +0900 | [diff] [blame] | 63 | bool rust_helper_IS_ERR(__force const void *ptr) |
| 64 | { |
| 65 | return IS_ERR(ptr); |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(rust_helper_IS_ERR); |
| 68 | |
| 69 | long rust_helper_PTR_ERR(__force const void *ptr) |
| 70 | { |
| 71 | return PTR_ERR(ptr); |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR); |
| 74 | |
Miguel Ojeda | 12f5772 | 2021-07-03 16:52:41 +0200 | [diff] [blame] | 75 | /* |
| 76 | * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type |
| 77 | * as the Rust `usize` type, so we can use it in contexts where Rust |
| 78 | * expects a `usize` like slice (array) indices. `usize` is defined to be |
| 79 | * the same as C's `uintptr_t` type (can hold any pointer) but not |
| 80 | * necessarily the same as `size_t` (can hold the size of any single |
| 81 | * object). Most modern platforms use the same concrete integer type for |
| 82 | * both of them, but in case we find ourselves on a platform where |
| 83 | * that's not true, fail early instead of risking ABI or |
| 84 | * integer-overflow issues. |
| 85 | * |
| 86 | * If your platform fails this assertion, it means that you are in |
| 87 | * danger of integer-overflow bugs (even if you attempt to remove |
| 88 | * `--size_t-is-usize`). It may be easiest to change the kernel ABI on |
| 89 | * your platform such that `size_t` matches `uintptr_t` (i.e., to increase |
| 90 | * `size_t`, because `uintptr_t` has to be at least as big as `size_t`). |
| 91 | */ |
| 92 | static_assert( |
| 93 | sizeof(size_t) == sizeof(uintptr_t) && |
| 94 | __alignof__(size_t) == __alignof__(uintptr_t), |
| 95 | "Rust code expects C `size_t` to match Rust `usize`" |
| 96 | ); |