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. |
Ariel Miculas | 917b2e0 | 2023-04-26 23:49:23 +0300 | [diff] [blame^] | 19 | * |
| 20 | * Sorted alphabetically. |
Miguel Ojeda | 12f5772 | 2021-07-03 16:52:41 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/bug.h> |
| 24 | #include <linux/build_bug.h> |
Asahi Lina | c7e20fa | 2023-04-03 18:48:11 +0900 | [diff] [blame] | 25 | #include <linux/err.h> |
Gary Guo | d2e3115 | 2023-05-31 17:44:50 +0000 | [diff] [blame] | 26 | #include <linux/errname.h> |
Wedson Almeida Filho | 6d20d62 | 2023-04-11 02:45:33 -0300 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Ariel Miculas | 917b2e0 | 2023-04-26 23:49:23 +0300 | [diff] [blame^] | 28 | #include <linux/refcount.h> |
Wedson Almeida Filho | 313c428 | 2023-04-11 02:45:39 -0300 | [diff] [blame] | 29 | #include <linux/sched/signal.h> |
Ariel Miculas | 917b2e0 | 2023-04-26 23:49:23 +0300 | [diff] [blame^] | 30 | #include <linux/spinlock.h> |
Wedson Almeida Filho | 19096bc | 2023-03-26 00:57:38 -0300 | [diff] [blame] | 31 | #include <linux/wait.h> |
Miguel Ojeda | 12f5772 | 2021-07-03 16:52:41 +0200 | [diff] [blame] | 32 | |
| 33 | __noreturn void rust_helper_BUG(void) |
| 34 | { |
| 35 | BUG(); |
| 36 | } |
| 37 | EXPORT_SYMBOL_GPL(rust_helper_BUG); |
| 38 | |
Wedson Almeida Filho | 6d20d62 | 2023-04-11 02:45:33 -0300 | [diff] [blame] | 39 | void rust_helper_mutex_lock(struct mutex *lock) |
| 40 | { |
| 41 | mutex_lock(lock); |
| 42 | } |
| 43 | EXPORT_SYMBOL_GPL(rust_helper_mutex_lock); |
| 44 | |
Wedson Almeida Filho | c6d917a | 2023-04-19 14:44:26 -0300 | [diff] [blame] | 45 | void rust_helper___spin_lock_init(spinlock_t *lock, const char *name, |
| 46 | struct lock_class_key *key) |
| 47 | { |
| 48 | #ifdef CONFIG_DEBUG_SPINLOCK |
| 49 | __raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG); |
| 50 | #else |
| 51 | spin_lock_init(lock); |
| 52 | #endif |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init); |
| 55 | |
| 56 | void rust_helper_spin_lock(spinlock_t *lock) |
| 57 | { |
| 58 | spin_lock(lock); |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(rust_helper_spin_lock); |
| 61 | |
| 62 | void rust_helper_spin_unlock(spinlock_t *lock) |
| 63 | { |
| 64 | spin_unlock(lock); |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(rust_helper_spin_unlock); |
| 67 | |
Wedson Almeida Filho | 19096bc | 2023-03-26 00:57:38 -0300 | [diff] [blame] | 68 | void rust_helper_init_wait(struct wait_queue_entry *wq_entry) |
| 69 | { |
| 70 | init_wait(wq_entry); |
| 71 | } |
| 72 | EXPORT_SYMBOL_GPL(rust_helper_init_wait); |
| 73 | |
Wedson Almeida Filho | 313c428 | 2023-04-11 02:45:39 -0300 | [diff] [blame] | 74 | int rust_helper_signal_pending(struct task_struct *t) |
| 75 | { |
| 76 | return signal_pending(t); |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(rust_helper_signal_pending); |
| 79 | |
Wedson Almeida Filho | 9dc0436 | 2022-12-28 06:03:40 +0000 | [diff] [blame] | 80 | refcount_t rust_helper_REFCOUNT_INIT(int n) |
| 81 | { |
| 82 | return (refcount_t)REFCOUNT_INIT(n); |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT); |
| 85 | |
| 86 | void rust_helper_refcount_inc(refcount_t *r) |
| 87 | { |
| 88 | refcount_inc(r); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(rust_helper_refcount_inc); |
| 91 | |
| 92 | bool rust_helper_refcount_dec_and_test(refcount_t *r) |
| 93 | { |
| 94 | return refcount_dec_and_test(r); |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); |
| 97 | |
Asahi Lina | c7e20fa | 2023-04-03 18:48:11 +0900 | [diff] [blame] | 98 | __force void *rust_helper_ERR_PTR(long err) |
| 99 | { |
| 100 | return ERR_PTR(err); |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); |
| 103 | |
Sven Van Asbroeck | 752417b | 2023-04-03 18:48:14 +0900 | [diff] [blame] | 104 | bool rust_helper_IS_ERR(__force const void *ptr) |
| 105 | { |
| 106 | return IS_ERR(ptr); |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(rust_helper_IS_ERR); |
| 109 | |
| 110 | long rust_helper_PTR_ERR(__force const void *ptr) |
| 111 | { |
| 112 | return PTR_ERR(ptr); |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR); |
| 115 | |
Gary Guo | d2e3115 | 2023-05-31 17:44:50 +0000 | [diff] [blame] | 116 | const char *rust_helper_errname(int err) |
| 117 | { |
| 118 | return errname(err); |
| 119 | } |
| 120 | EXPORT_SYMBOL_GPL(rust_helper_errname); |
| 121 | |
Wedson Almeida Filho | 8da7a2b | 2023-04-11 02:45:40 -0300 | [diff] [blame] | 122 | struct task_struct *rust_helper_get_current(void) |
| 123 | { |
| 124 | return current; |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(rust_helper_get_current); |
| 127 | |
Wedson Almeida Filho | 313c428 | 2023-04-11 02:45:39 -0300 | [diff] [blame] | 128 | void rust_helper_get_task_struct(struct task_struct *t) |
| 129 | { |
| 130 | get_task_struct(t); |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(rust_helper_get_task_struct); |
| 133 | |
| 134 | void rust_helper_put_task_struct(struct task_struct *t) |
| 135 | { |
| 136 | put_task_struct(t); |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(rust_helper_put_task_struct); |
| 139 | |
Miguel Ojeda | 12f5772 | 2021-07-03 16:52:41 +0200 | [diff] [blame] | 140 | /* |
| 141 | * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type |
| 142 | * as the Rust `usize` type, so we can use it in contexts where Rust |
| 143 | * expects a `usize` like slice (array) indices. `usize` is defined to be |
| 144 | * the same as C's `uintptr_t` type (can hold any pointer) but not |
| 145 | * necessarily the same as `size_t` (can hold the size of any single |
| 146 | * object). Most modern platforms use the same concrete integer type for |
| 147 | * both of them, but in case we find ourselves on a platform where |
| 148 | * that's not true, fail early instead of risking ABI or |
| 149 | * integer-overflow issues. |
| 150 | * |
| 151 | * If your platform fails this assertion, it means that you are in |
| 152 | * danger of integer-overflow bugs (even if you attempt to remove |
| 153 | * `--size_t-is-usize`). It may be easiest to change the kernel ABI on |
| 154 | * your platform such that `size_t` matches `uintptr_t` (i.e., to increase |
| 155 | * `size_t`, because `uintptr_t` has to be at least as big as `size_t`). |
| 156 | */ |
| 157 | static_assert( |
| 158 | sizeof(size_t) == sizeof(uintptr_t) && |
| 159 | __alignof__(size_t) == __alignof__(uintptr_t), |
| 160 | "Rust code expects C `size_t` to match Rust `usize`" |
| 161 | ); |