xref: /openbmc/linux/rust/helpers.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
112f57721SMiguel Ojeda // SPDX-License-Identifier: GPL-2.0
212f57721SMiguel Ojeda /*
312f57721SMiguel Ojeda  * Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions
412f57721SMiguel Ojeda  * cannot be called either. This file explicitly creates functions ("helpers")
512f57721SMiguel Ojeda  * that wrap those so that they can be called from Rust.
612f57721SMiguel Ojeda  *
712f57721SMiguel Ojeda  * Even though Rust kernel modules should never use directly the bindings, some
812f57721SMiguel Ojeda  * of these helpers need to be exported because Rust generics and inlined
912f57721SMiguel Ojeda  * functions may not get their code generated in the crate where they are
1012f57721SMiguel Ojeda  * defined. Other helpers, called from non-inline functions, may not be
1112f57721SMiguel Ojeda  * exported, in principle. However, in general, the Rust compiler does not
1212f57721SMiguel Ojeda  * guarantee codegen will be performed for a non-inline function either.
1312f57721SMiguel Ojeda  * Therefore, this file exports all the helpers. In the future, this may be
1412f57721SMiguel Ojeda  * revisited to reduce the number of exports after the compiler is informed
1512f57721SMiguel Ojeda  * about the places codegen is required.
1612f57721SMiguel Ojeda  *
1712f57721SMiguel Ojeda  * All symbols are exported as GPL-only to guarantee no GPL-only feature is
1812f57721SMiguel Ojeda  * accidentally exposed.
19917b2e00SAriel Miculas  *
20917b2e00SAriel Miculas  * Sorted alphabetically.
2112f57721SMiguel Ojeda  */
2212f57721SMiguel Ojeda 
23a66d733dSMiguel Ojeda #include <kunit/test-bug.h>
2412f57721SMiguel Ojeda #include <linux/bug.h>
2512f57721SMiguel Ojeda #include <linux/build_bug.h>
26c7e20faaSAsahi Lina #include <linux/err.h>
27d2e3115dSGary Guo #include <linux/errname.h>
286d20d629SWedson Almeida Filho #include <linux/mutex.h>
29917b2e00SAriel Miculas #include <linux/refcount.h>
30313c4281SWedson Almeida Filho #include <linux/sched/signal.h>
31917b2e00SAriel Miculas #include <linux/spinlock.h>
3219096bceSWedson Almeida Filho #include <linux/wait.h>
3312f57721SMiguel Ojeda 
rust_helper_BUG(void)3412f57721SMiguel Ojeda __noreturn void rust_helper_BUG(void)
3512f57721SMiguel Ojeda {
3612f57721SMiguel Ojeda 	BUG();
3712f57721SMiguel Ojeda }
3812f57721SMiguel Ojeda EXPORT_SYMBOL_GPL(rust_helper_BUG);
3912f57721SMiguel Ojeda 
rust_helper_mutex_lock(struct mutex * lock)406d20d629SWedson Almeida Filho void rust_helper_mutex_lock(struct mutex *lock)
416d20d629SWedson Almeida Filho {
426d20d629SWedson Almeida Filho 	mutex_lock(lock);
436d20d629SWedson Almeida Filho }
446d20d629SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_mutex_lock);
456d20d629SWedson Almeida Filho 
rust_helper___spin_lock_init(spinlock_t * lock,const char * name,struct lock_class_key * key)46c6d917a4SWedson Almeida Filho void rust_helper___spin_lock_init(spinlock_t *lock, const char *name,
47c6d917a4SWedson Almeida Filho 				  struct lock_class_key *key)
48c6d917a4SWedson Almeida Filho {
49c6d917a4SWedson Almeida Filho #ifdef CONFIG_DEBUG_SPINLOCK
50c6d917a4SWedson Almeida Filho 	__raw_spin_lock_init(spinlock_check(lock), name, key, LD_WAIT_CONFIG);
51c6d917a4SWedson Almeida Filho #else
52c6d917a4SWedson Almeida Filho 	spin_lock_init(lock);
53c6d917a4SWedson Almeida Filho #endif
54c6d917a4SWedson Almeida Filho }
55c6d917a4SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper___spin_lock_init);
56c6d917a4SWedson Almeida Filho 
rust_helper_spin_lock(spinlock_t * lock)57c6d917a4SWedson Almeida Filho void rust_helper_spin_lock(spinlock_t *lock)
58c6d917a4SWedson Almeida Filho {
59c6d917a4SWedson Almeida Filho 	spin_lock(lock);
60c6d917a4SWedson Almeida Filho }
61c6d917a4SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_spin_lock);
62c6d917a4SWedson Almeida Filho 
rust_helper_spin_unlock(spinlock_t * lock)63c6d917a4SWedson Almeida Filho void rust_helper_spin_unlock(spinlock_t *lock)
64c6d917a4SWedson Almeida Filho {
65c6d917a4SWedson Almeida Filho 	spin_unlock(lock);
66c6d917a4SWedson Almeida Filho }
67c6d917a4SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
68c6d917a4SWedson Almeida Filho 
rust_helper_init_wait(struct wait_queue_entry * wq_entry)6919096bceSWedson Almeida Filho void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
7019096bceSWedson Almeida Filho {
7119096bceSWedson Almeida Filho 	init_wait(wq_entry);
7219096bceSWedson Almeida Filho }
7319096bceSWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_init_wait);
7419096bceSWedson Almeida Filho 
rust_helper_signal_pending(struct task_struct * t)75313c4281SWedson Almeida Filho int rust_helper_signal_pending(struct task_struct *t)
76313c4281SWedson Almeida Filho {
77313c4281SWedson Almeida Filho 	return signal_pending(t);
78313c4281SWedson Almeida Filho }
79313c4281SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
80313c4281SWedson Almeida Filho 
rust_helper_REFCOUNT_INIT(int n)819dc04365SWedson Almeida Filho refcount_t rust_helper_REFCOUNT_INIT(int n)
829dc04365SWedson Almeida Filho {
839dc04365SWedson Almeida Filho 	return (refcount_t)REFCOUNT_INIT(n);
849dc04365SWedson Almeida Filho }
859dc04365SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_REFCOUNT_INIT);
869dc04365SWedson Almeida Filho 
rust_helper_refcount_inc(refcount_t * r)879dc04365SWedson Almeida Filho void rust_helper_refcount_inc(refcount_t *r)
889dc04365SWedson Almeida Filho {
899dc04365SWedson Almeida Filho 	refcount_inc(r);
909dc04365SWedson Almeida Filho }
919dc04365SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_refcount_inc);
929dc04365SWedson Almeida Filho 
rust_helper_refcount_dec_and_test(refcount_t * r)939dc04365SWedson Almeida Filho bool rust_helper_refcount_dec_and_test(refcount_t *r)
949dc04365SWedson Almeida Filho {
959dc04365SWedson Almeida Filho 	return refcount_dec_and_test(r);
969dc04365SWedson Almeida Filho }
979dc04365SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
989dc04365SWedson Almeida Filho 
rust_helper_ERR_PTR(long err)99c7e20faaSAsahi Lina __force void *rust_helper_ERR_PTR(long err)
100c7e20faaSAsahi Lina {
101c7e20faaSAsahi Lina 	return ERR_PTR(err);
102c7e20faaSAsahi Lina }
103c7e20faaSAsahi Lina EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR);
104c7e20faaSAsahi Lina 
rust_helper_IS_ERR(__force const void * ptr)105752417b3SSven Van Asbroeck bool rust_helper_IS_ERR(__force const void *ptr)
106752417b3SSven Van Asbroeck {
107752417b3SSven Van Asbroeck 	return IS_ERR(ptr);
108752417b3SSven Van Asbroeck }
109752417b3SSven Van Asbroeck EXPORT_SYMBOL_GPL(rust_helper_IS_ERR);
110752417b3SSven Van Asbroeck 
rust_helper_PTR_ERR(__force const void * ptr)111752417b3SSven Van Asbroeck long rust_helper_PTR_ERR(__force const void *ptr)
112752417b3SSven Van Asbroeck {
113752417b3SSven Van Asbroeck 	return PTR_ERR(ptr);
114752417b3SSven Van Asbroeck }
115752417b3SSven Van Asbroeck EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR);
116752417b3SSven Van Asbroeck 
rust_helper_errname(int err)117d2e3115dSGary Guo const char *rust_helper_errname(int err)
118d2e3115dSGary Guo {
119d2e3115dSGary Guo 	return errname(err);
120d2e3115dSGary Guo }
121d2e3115dSGary Guo EXPORT_SYMBOL_GPL(rust_helper_errname);
122d2e3115dSGary Guo 
rust_helper_get_current(void)1238da7a2b7SWedson Almeida Filho struct task_struct *rust_helper_get_current(void)
1248da7a2b7SWedson Almeida Filho {
1258da7a2b7SWedson Almeida Filho 	return current;
1268da7a2b7SWedson Almeida Filho }
1278da7a2b7SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_get_current);
1288da7a2b7SWedson Almeida Filho 
rust_helper_get_task_struct(struct task_struct * t)129313c4281SWedson Almeida Filho void rust_helper_get_task_struct(struct task_struct *t)
130313c4281SWedson Almeida Filho {
131313c4281SWedson Almeida Filho 	get_task_struct(t);
132313c4281SWedson Almeida Filho }
133313c4281SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_get_task_struct);
134313c4281SWedson Almeida Filho 
rust_helper_put_task_struct(struct task_struct * t)135313c4281SWedson Almeida Filho void rust_helper_put_task_struct(struct task_struct *t)
136313c4281SWedson Almeida Filho {
137313c4281SWedson Almeida Filho 	put_task_struct(t);
138313c4281SWedson Almeida Filho }
139313c4281SWedson Almeida Filho EXPORT_SYMBOL_GPL(rust_helper_put_task_struct);
140313c4281SWedson Almeida Filho 
rust_helper_kunit_get_current_test(void)141a66d733dSMiguel Ojeda struct kunit *rust_helper_kunit_get_current_test(void)
142a66d733dSMiguel Ojeda {
143a66d733dSMiguel Ojeda 	return kunit_get_current_test();
144a66d733dSMiguel Ojeda }
145a66d733dSMiguel Ojeda EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test);
146a66d733dSMiguel Ojeda 
14712f57721SMiguel Ojeda /*
148*08ab7865SAakash Sen Sharma  * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
149*08ab7865SAakash Sen Sharma  * use it in contexts where Rust expects a `usize` like slice (array) indices.
150*08ab7865SAakash Sen Sharma  * `usize` is defined to be the same as C's `uintptr_t` type (can hold any
151*08ab7865SAakash Sen Sharma  * pointer) but not necessarily the same as `size_t` (can hold the size of any
152*08ab7865SAakash Sen Sharma  * single object). Most modern platforms use the same concrete integer type for
15312f57721SMiguel Ojeda  * both of them, but in case we find ourselves on a platform where
15412f57721SMiguel Ojeda  * that's not true, fail early instead of risking ABI or
15512f57721SMiguel Ojeda  * integer-overflow issues.
15612f57721SMiguel Ojeda  *
15712f57721SMiguel Ojeda  * If your platform fails this assertion, it means that you are in
158*08ab7865SAakash Sen Sharma  * danger of integer-overflow bugs (even if you attempt to add
159*08ab7865SAakash Sen Sharma  * `--no-size_t-is-usize`). It may be easiest to change the kernel ABI on
16012f57721SMiguel Ojeda  * your platform such that `size_t` matches `uintptr_t` (i.e., to increase
16112f57721SMiguel Ojeda  * `size_t`, because `uintptr_t` has to be at least as big as `size_t`).
16212f57721SMiguel Ojeda  */
16312f57721SMiguel Ojeda static_assert(
16412f57721SMiguel Ojeda 	sizeof(size_t) == sizeof(uintptr_t) &&
16512f57721SMiguel Ojeda 	__alignof__(size_t) == __alignof__(uintptr_t),
16612f57721SMiguel Ojeda 	"Rust code expects C `size_t` to match Rust `usize`"
16712f57721SMiguel Ojeda );
168