Skip to content

Commit 4e17466

Browse files
asahilinaojeda
authored andcommitted
rust: uapi: Add UAPI crate
This crate mirrors the `bindings` crate, but will contain only UAPI bindings. Unlike the bindings crate, drivers may directly use this crate if they have to interface with userspace. Initially, just bind the generic ioctl stuff. In the future, we would also like to add additional checks to ensure that all types exposed by this crate satisfy UAPI-safety guarantees (that is, they are safely castable to/from a "bag of bits"). [ Miguel: added support for the `rustdoc` and `rusttest` targets, since otherwise they fail, and we want to keep them working. ] Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Signed-off-by: Asahi Lina <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 19096bc commit 4e17466

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

rust/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
bindings_generated.rs
44
bindings_helpers_generated.rs
5+
uapi_generated.rs
56
exports_*_generated.h
67
doc/
78
test/

rust/Makefile

+23-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
1616
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
1717
exports_kernel_generated.h
1818

19+
always-$(CONFIG_RUST) += uapi/uapi_generated.rs
20+
obj-$(CONFIG_RUST) += uapi.o
21+
1922
ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
2023
obj-$(CONFIG_RUST) += build_error.o
2124
else
@@ -113,7 +116,7 @@ rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
113116

114117
rustdoc-kernel: private rustc_target_flags = --extern alloc \
115118
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
116-
--extern bindings
119+
--extern bindings --extern uapi
117120
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
118121
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
119122
$(obj)/bindings.o FORCE
@@ -141,6 +144,9 @@ rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
141144
rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
142145
$(call if_changed,rustc_test_library)
143146

147+
rusttestlib-uapi: $(src)/uapi/lib.rs rusttest-prepare FORCE
148+
$(call if_changed,rustc_test_library)
149+
144150
quiet_cmd_rustdoc_test = RUSTDOC T $<
145151
cmd_rustdoc_test = \
146152
OBJTREE=$(abspath $(objtree)) \
@@ -223,9 +229,10 @@ rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
223229
$(call if_changed,rustdoc_test)
224230

225231
rusttest-kernel: private rustc_target_flags = --extern alloc \
226-
--extern build_error --extern macros --extern bindings
232+
--extern build_error --extern macros --extern bindings --extern uapi
227233
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
228-
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
234+
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
235+
rusttestlib-uapi FORCE
229236
$(call if_changed,rustc_test)
230237
$(call if_changed,rustc_test_library)
231238

@@ -288,6 +295,12 @@ $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
288295
$(src)/bindgen_parameters FORCE
289296
$(call if_changed_dep,bindgen)
290297

298+
$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
299+
$(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters)
300+
$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
301+
$(src)/bindgen_parameters FORCE
302+
$(call if_changed_dep,bindgen)
303+
291304
# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
292305
# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
293306
# given it is `libclang`; but for consistency, future Clang changes and/or
@@ -388,10 +401,15 @@ $(obj)/bindings.o: $(src)/bindings/lib.rs \
388401
$(obj)/bindings/bindings_helpers_generated.rs FORCE
389402
$(call if_changed_dep,rustc_library)
390403

404+
$(obj)/uapi.o: $(src)/uapi/lib.rs \
405+
$(obj)/compiler_builtins.o \
406+
$(obj)/uapi/uapi_generated.rs FORCE
407+
$(call if_changed_dep,rustc_library)
408+
391409
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
392-
--extern build_error --extern macros --extern bindings
410+
--extern build_error --extern macros --extern bindings --extern uapi
393411
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
394-
$(obj)/libmacros.so $(obj)/bindings.o FORCE
412+
$(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
395413
$(call if_changed_dep,rustc_library)
396414

397415
endif # CONFIG_RUST

rust/kernel/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub mod types;
5050
#[doc(hidden)]
5151
pub use bindings;
5252
pub use macros;
53+
pub use uapi;
5354

5455
#[doc(hidden)]
5556
pub use build_error::build_error;

rust/uapi/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! UAPI Bindings.
4+
//!
5+
//! Contains the bindings generated by `bindgen` for UAPI interfaces.
6+
//!
7+
//! This crate may be used directly by drivers that need to interact with
8+
//! userspace APIs.
9+
10+
#![no_std]
11+
#![feature(core_ffi_c)]
12+
// See <https://github.com/rust-lang/rust-bindgen/issues/1651>.
13+
#![cfg_attr(test, allow(deref_nullptr))]
14+
#![cfg_attr(test, allow(unaligned_references))]
15+
#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
16+
#![allow(
17+
clippy::all,
18+
missing_docs,
19+
non_camel_case_types,
20+
non_upper_case_globals,
21+
non_snake_case,
22+
improper_ctypes,
23+
unreachable_pub,
24+
unsafe_op_in_unsafe_fn
25+
)]
26+
27+
include!(concat!(env!("OBJTREE"), "/rust/uapi/uapi_generated.rs"));

rust/uapi/uapi_helper.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Header that contains the headers for which Rust UAPI bindings
4+
* will be automatically generated by `bindgen`.
5+
*
6+
* Sorted alphabetically.
7+
*/
8+
9+
#include <uapi/asm-generic/ioctl.h>

0 commit comments

Comments
 (0)