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