5701725692
Toolchain and infrastructure: - Support 'MITIGATION_{RETHUNK,RETPOLINE,SLS}' (which cleans up objtool warnings), teach objtool about 'noreturn' Rust symbols and mimic '___ADDRESSABLE()' for 'module_{init,exit}'. With that, we should be objtool-warning-free, so enable it to run for all Rust object files. - KASAN (no 'SW_TAGS'), KCFI and shadow call sanitizer support. - Support 'RUSTC_VERSION', including re-config and re-build on change. - Split helpers file into several files in a folder, to avoid conflicts in it. Eventually those files will be moved to the right places with the new build system. In addition, remove the need to manually export the symbols defined there, reusing existing machinery for that. - Relax restriction on configurations with Rust + GCC plugins to just the RANDSTRUCT plugin. 'kernel' crate: - New 'list' module: doubly-linked linked list for use with reference counted values, which is heavily used by the upcoming Rust Binder. This includes 'ListArc' (a wrapper around 'Arc' that is guaranteed unique for the given ID), 'AtomicTracker' (tracks whether a 'ListArc' exists using an atomic), 'ListLinks' (the prev/next pointers for an item in a linked list), 'List' (the linked list itself), 'Iter' (an iterator over a 'List'), 'Cursor' (a cursor into a 'List' that allows to remove elements), 'ListArcField' (a field exclusively owned by a 'ListArc'), as well as support for heterogeneous lists. - New 'rbtree' module: red-black tree abstractions used by the upcoming Rust Binder. This includes 'RBTree' (the red-black tree itself), 'RBTreeNode' (a node), 'RBTreeNodeReservation' (a memory reservation for a node), 'Iter' and 'IterMut' (immutable and mutable iterators), 'Cursor' (bidirectional cursor that allows to remove elements), as well as an entry API similar to the Rust standard library one. - 'init' module: add 'write_[pin_]init' methods and the 'InPlaceWrite' trait. Add the 'assert_pinned!' macro. - 'sync' module: implement the 'InPlaceInit' trait for 'Arc' by introducing an associated type in the trait. - 'alloc' module: add 'drop_contents' method to 'BoxExt'. - 'types' module: implement the 'ForeignOwnable' trait for 'Pin<Box<T>>' and improve the trait's documentation. In addition, add the 'into_raw' method to the 'ARef' type. - 'error' module: in preparation for the upcoming Rust support for 32-bit architectures, like arm, locally allow Clippy lint for those. Documentation: - https://rust.docs.kernel.org has been announced, so link to it. - Enable rustdoc's "jump to definition" feature, making its output a bit closer to the experience in a cross-referencer. - Debian Testing now also provides recent Rust releases (outside of the freeze period), so add it to the list. MAINTAINERS: - Trevor is joining as reviewer of the "RUST" entry. And a few other small bits. -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEPjU5OPd5QIZ9jqqOGXyLc2htIW0FAmbzNz4ACgkQGXyLc2ht IW3muA/9HcPL0QqVB5+SqSRqcatmrFU/wq8Oaa6Z/No0JaynqyikK+R1WNokUd/5 WpQi4PC1OYV+ekyAuWdkooKmaSqagH5r53XlezNw+cM5zo8y7p0otVlbepQ0t3Ky pVEmfDRIeSFXsKrg91BJUKyJf70TQlgSggDVCExlanfOjPz88C1+s3EcJ/XWYGKQ cRk/XDdbF5eNaldp2MriVF0fw7XktgIrmVzxt/z0lb4PE7RaCAnO6gSQI+90Vb2d zvyOYKS4AkqE3suFvDIIUlPUv+8XbACj0c4wvBZHH5uZGTbgWUffqygJ45GqChEt c4fS/+E8VaM1z0EvxNczC0nQkfLwkTc1mgbP+sG3VZJMPVCJ2zQan1/ond7GqCpw pt6uQaGvDsAvllm7sbiAIVaAY81icqyYWKfNBXLLEL7DhY5je5Wq+E83XQ8d5u5F EuapnZhW3y12d6UCsSe9bD8W45NFoWHPXky1TzT+whTxnX1yH9YsPXbJceGSbbgd Lw3GmUtZx2bVAMToVjNFD2lPA3OmPY1e2lk0jwzTuQrEXfnZYuzbjqs3YUijb7xR AlsWfIb0IHBwHWpB7da24ezqWP2VD4eaDdD8/+LmDSj6XLngxMNWRLKmXT000eTW vIFP9GJrvag2R3YFPhrurgGpRsp8HUTLtvcZROxp2JVQGQ7Z4Ww= =52BN -----END PGP SIGNATURE----- Merge tag 'rust-6.12' of https://github.com/Rust-for-Linux/linux Pull Rust updates from Miguel Ojeda: "Toolchain and infrastructure: - Support 'MITIGATION_{RETHUNK,RETPOLINE,SLS}' (which cleans up objtool warnings), teach objtool about 'noreturn' Rust symbols and mimic '___ADDRESSABLE()' for 'module_{init,exit}'. With that, we should be objtool-warning-free, so enable it to run for all Rust object files. - KASAN (no 'SW_TAGS'), KCFI and shadow call sanitizer support. - Support 'RUSTC_VERSION', including re-config and re-build on change. - Split helpers file into several files in a folder, to avoid conflicts in it. Eventually those files will be moved to the right places with the new build system. In addition, remove the need to manually export the symbols defined there, reusing existing machinery for that. - Relax restriction on configurations with Rust + GCC plugins to just the RANDSTRUCT plugin. 'kernel' crate: - New 'list' module: doubly-linked linked list for use with reference counted values, which is heavily used by the upcoming Rust Binder. This includes 'ListArc' (a wrapper around 'Arc' that is guaranteed unique for the given ID), 'AtomicTracker' (tracks whether a 'ListArc' exists using an atomic), 'ListLinks' (the prev/next pointers for an item in a linked list), 'List' (the linked list itself), 'Iter' (an iterator over a 'List'), 'Cursor' (a cursor into a 'List' that allows to remove elements), 'ListArcField' (a field exclusively owned by a 'ListArc'), as well as support for heterogeneous lists. - New 'rbtree' module: red-black tree abstractions used by the upcoming Rust Binder. This includes 'RBTree' (the red-black tree itself), 'RBTreeNode' (a node), 'RBTreeNodeReservation' (a memory reservation for a node), 'Iter' and 'IterMut' (immutable and mutable iterators), 'Cursor' (bidirectional cursor that allows to remove elements), as well as an entry API similar to the Rust standard library one. - 'init' module: add 'write_[pin_]init' methods and the 'InPlaceWrite' trait. Add the 'assert_pinned!' macro. - 'sync' module: implement the 'InPlaceInit' trait for 'Arc' by introducing an associated type in the trait. - 'alloc' module: add 'drop_contents' method to 'BoxExt'. - 'types' module: implement the 'ForeignOwnable' trait for 'Pin<Box<T>>' and improve the trait's documentation. In addition, add the 'into_raw' method to the 'ARef' type. - 'error' module: in preparation for the upcoming Rust support for 32-bit architectures, like arm, locally allow Clippy lint for those. Documentation: - https://rust.docs.kernel.org has been announced, so link to it. - Enable rustdoc's "jump to definition" feature, making its output a bit closer to the experience in a cross-referencer. - Debian Testing now also provides recent Rust releases (outside of the freeze period), so add it to the list. MAINTAINERS: - Trevor is joining as reviewer of the "RUST" entry. And a few other small bits" * tag 'rust-6.12' of https://github.com/Rust-for-Linux/linux: (54 commits) kasan: rust: Add KASAN smoke test via UAF kbuild: rust: Enable KASAN support rust: kasan: Rust does not support KHWASAN kbuild: rust: Define probing macros for rustc kasan: simplify and clarify Makefile rust: cfi: add support for CFI_CLANG with Rust cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS rust: support for shadow call stack sanitizer docs: rust: include other expressions in conditional compilation section kbuild: rust: replace proc macros dependency on `core.o` with the version text kbuild: rust: rebuild if the version text changes kbuild: rust: re-run Kconfig if the version text changes kbuild: rust: add `CONFIG_RUSTC_VERSION` rust: avoid `box_uninit_write` feature MAINTAINERS: add Trevor Gross as Rust reviewer rust: rbtree: add `RBTree::entry` rust: rbtree: add cursor rust: rbtree: add mutable iterator rust: rbtree: add iterator rust: rbtree: add red-black tree implementation backed by the C version ...
312 lines
9.1 KiB
ReStructuredText
312 lines
9.1 KiB
ReStructuredText
.. SPDX-License-Identifier: GPL-2.0
|
|
|
|
Quick Start
|
|
===========
|
|
|
|
This document describes how to get started with kernel development in Rust.
|
|
|
|
There are a few ways to install a Rust toolchain needed for kernel development.
|
|
A simple way is to use the packages from your Linux distribution if they are
|
|
suitable -- the first section below explains this approach. An advantage of this
|
|
approach is that, typically, the distribution will match the LLVM used by Rust
|
|
and Clang.
|
|
|
|
Another way is using the prebuilt stable versions of LLVM+Rust provided on
|
|
`kernel.org <https://kernel.org/pub/tools/llvm/rust/>`_. These are the same slim
|
|
and fast LLVM toolchains from :ref:`Getting LLVM <getting_llvm>` with versions
|
|
of Rust added to them that Rust for Linux supports. Two sets are provided: the
|
|
"latest LLVM" and "matching LLVM" (please see the link for more information).
|
|
|
|
Alternatively, the next two "Requirements" sections explain each component and
|
|
how to install them through ``rustup``, the standalone installers from Rust
|
|
and/or building them.
|
|
|
|
The rest of the document explains other aspects on how to get started.
|
|
|
|
|
|
Distributions
|
|
-------------
|
|
|
|
Arch Linux
|
|
**********
|
|
|
|
Arch Linux provides recent Rust releases and thus it should generally work out
|
|
of the box, e.g.::
|
|
|
|
pacman -S rust rust-src rust-bindgen
|
|
|
|
|
|
Debian
|
|
******
|
|
|
|
Debian Testing and Debian Unstable (Sid), outside of the freeze period, provide
|
|
recent Rust releases and thus they should generally work out of the box, e.g.::
|
|
|
|
apt install rustc rust-src bindgen rustfmt rust-clippy
|
|
|
|
|
|
Fedora Linux
|
|
************
|
|
|
|
Fedora Linux provides recent Rust releases and thus it should generally work out
|
|
of the box, e.g.::
|
|
|
|
dnf install rust rust-src bindgen-cli rustfmt clippy
|
|
|
|
|
|
Gentoo Linux
|
|
************
|
|
|
|
Gentoo Linux (and especially the testing branch) provides recent Rust releases
|
|
and thus it should generally work out of the box, e.g.::
|
|
|
|
USE='rust-src rustfmt clippy' emerge dev-lang/rust dev-util/bindgen
|
|
|
|
``LIBCLANG_PATH`` may need to be set.
|
|
|
|
|
|
Nix
|
|
***
|
|
|
|
Nix (unstable channel) provides recent Rust releases and thus it should
|
|
generally work out of the box, e.g.::
|
|
|
|
{ pkgs ? import <nixpkgs> {} }:
|
|
pkgs.mkShell {
|
|
nativeBuildInputs = with pkgs; [ rustc rust-bindgen rustfmt clippy ];
|
|
RUST_LIB_SRC = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
|
}
|
|
|
|
|
|
openSUSE
|
|
********
|
|
|
|
openSUSE Slowroll and openSUSE Tumbleweed provide recent Rust releases and thus
|
|
they should generally work out of the box, e.g.::
|
|
|
|
zypper install rust rust1.79-src rust-bindgen clang
|
|
|
|
|
|
Requirements: Building
|
|
----------------------
|
|
|
|
This section explains how to fetch the tools needed for building.
|
|
|
|
To easily check whether the requirements are met, the following target
|
|
can be used::
|
|
|
|
make LLVM=1 rustavailable
|
|
|
|
This triggers the same logic used by Kconfig to determine whether
|
|
``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not
|
|
if that is the case.
|
|
|
|
|
|
rustc
|
|
*****
|
|
|
|
A recent version of the Rust compiler is required.
|
|
|
|
If ``rustup`` is being used, enter the kernel build directory (or use
|
|
``--path=<build-dir>`` argument to the ``set`` sub-command) and run,
|
|
for instance::
|
|
|
|
rustup override set stable
|
|
|
|
This will configure your working directory to use the given version of
|
|
``rustc`` without affecting your default toolchain.
|
|
|
|
Note that the override applies to the current working directory (and its
|
|
sub-directories).
|
|
|
|
If you are not using ``rustup``, fetch a standalone installer from:
|
|
|
|
https://forge.rust-lang.org/infra/other-installation-methods.html#standalone
|
|
|
|
|
|
Rust standard library source
|
|
****************************
|
|
|
|
The Rust standard library source is required because the build system will
|
|
cross-compile ``core`` and ``alloc``.
|
|
|
|
If ``rustup`` is being used, run::
|
|
|
|
rustup component add rust-src
|
|
|
|
The components are installed per toolchain, thus upgrading the Rust compiler
|
|
version later on requires re-adding the component.
|
|
|
|
Otherwise, if a standalone installer is used, the Rust source tree may be
|
|
downloaded into the toolchain's installation folder::
|
|
|
|
curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" |
|
|
tar -xzf - -C "$(rustc --print sysroot)/lib" \
|
|
"rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \
|
|
--strip-components=3
|
|
|
|
In this case, upgrading the Rust compiler version later on requires manually
|
|
updating the source tree (this can be done by removing ``$(rustc --print
|
|
sysroot)/lib/rustlib/src/rust`` then rerunning the above command).
|
|
|
|
|
|
libclang
|
|
********
|
|
|
|
``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code
|
|
in the kernel, which means LLVM needs to be installed; like when the kernel
|
|
is compiled with ``LLVM=1``.
|
|
|
|
Linux distributions are likely to have a suitable one available, so it is
|
|
best to check that first.
|
|
|
|
There are also some binaries for several systems and architectures uploaded at:
|
|
|
|
https://releases.llvm.org/download.html
|
|
|
|
Otherwise, building LLVM takes quite a while, but it is not a complex process:
|
|
|
|
https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm
|
|
|
|
Please see Documentation/kbuild/llvm.rst for more information and further ways
|
|
to fetch pre-built releases and distribution packages.
|
|
|
|
|
|
bindgen
|
|
*******
|
|
|
|
The bindings to the C side of the kernel are generated at build time using
|
|
the ``bindgen`` tool.
|
|
|
|
Install it, for instance, via (note that this will download and build the tool
|
|
from source)::
|
|
|
|
cargo install --locked bindgen-cli
|
|
|
|
``bindgen`` uses the ``clang-sys`` crate to find a suitable ``libclang`` (which
|
|
may be linked statically, dynamically or loaded at runtime). By default, the
|
|
``cargo`` command above will produce a ``bindgen`` binary that will load
|
|
``libclang`` at runtime. If it is not found (or a different ``libclang`` than
|
|
the one found should be used), the process can be tweaked, e.g. by using the
|
|
``LIBCLANG_PATH`` environment variable. For details, please see ``clang-sys``'s
|
|
documentation at:
|
|
|
|
https://github.com/KyleMayes/clang-sys#linking
|
|
|
|
https://github.com/KyleMayes/clang-sys#environment-variables
|
|
|
|
|
|
Requirements: Developing
|
|
------------------------
|
|
|
|
This section explains how to fetch the tools needed for developing. That is,
|
|
they are not needed when just building the kernel.
|
|
|
|
|
|
rustfmt
|
|
*******
|
|
|
|
The ``rustfmt`` tool is used to automatically format all the Rust kernel code,
|
|
including the generated C bindings (for details, please see
|
|
coding-guidelines.rst).
|
|
|
|
If ``rustup`` is being used, its ``default`` profile already installs the tool,
|
|
thus nothing needs to be done. If another profile is being used, the component
|
|
can be installed manually::
|
|
|
|
rustup component add rustfmt
|
|
|
|
The standalone installers also come with ``rustfmt``.
|
|
|
|
|
|
clippy
|
|
******
|
|
|
|
``clippy`` is a Rust linter. Running it provides extra warnings for Rust code.
|
|
It can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see
|
|
general-information.rst).
|
|
|
|
If ``rustup`` is being used, its ``default`` profile already installs the tool,
|
|
thus nothing needs to be done. If another profile is being used, the component
|
|
can be installed manually::
|
|
|
|
rustup component add clippy
|
|
|
|
The standalone installers also come with ``clippy``.
|
|
|
|
|
|
rustdoc
|
|
*******
|
|
|
|
``rustdoc`` is the documentation tool for Rust. It generates pretty HTML
|
|
documentation for Rust code (for details, please see
|
|
general-information.rst).
|
|
|
|
``rustdoc`` is also used to test the examples provided in documented Rust code
|
|
(called doctests or documentation tests). The ``rusttest`` Make target uses
|
|
this feature.
|
|
|
|
If ``rustup`` is being used, all the profiles already install the tool,
|
|
thus nothing needs to be done.
|
|
|
|
The standalone installers also come with ``rustdoc``.
|
|
|
|
|
|
rust-analyzer
|
|
*************
|
|
|
|
The `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can
|
|
be used with many editors to enable syntax highlighting, completion, go to
|
|
definition, and other features.
|
|
|
|
``rust-analyzer`` needs a configuration file, ``rust-project.json``, which
|
|
can be generated by the ``rust-analyzer`` Make target::
|
|
|
|
make LLVM=1 rust-analyzer
|
|
|
|
|
|
Configuration
|
|
-------------
|
|
|
|
``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup``
|
|
menu. The option is only shown if a suitable Rust toolchain is found (see
|
|
above), as long as the other requirements are met. In turn, this will make
|
|
visible the rest of options that depend on Rust.
|
|
|
|
Afterwards, go to::
|
|
|
|
Kernel hacking
|
|
-> Sample kernel code
|
|
-> Rust samples
|
|
|
|
And enable some sample modules either as built-in or as loadable.
|
|
|
|
|
|
Building
|
|
--------
|
|
|
|
Building a kernel with a complete LLVM toolchain is the best supported setup
|
|
at the moment. That is::
|
|
|
|
make LLVM=1
|
|
|
|
Using GCC also works for some configurations, but it is very experimental at
|
|
the moment.
|
|
|
|
|
|
Hacking
|
|
-------
|
|
|
|
To dive deeper, take a look at the source code of the samples
|
|
at ``samples/rust/``, the Rust support code under ``rust/`` and
|
|
the ``Rust hacking`` menu under ``Kernel hacking``.
|
|
|
|
If GDB/Binutils is used and Rust symbols are not getting demangled, the reason
|
|
is the toolchain does not support Rust's new v0 mangling scheme yet.
|
|
There are a few ways out:
|
|
|
|
- Install a newer release (GDB >= 10.2, Binutils >= 2.36).
|
|
|
|
- Some versions of GDB (e.g. vanilla GDB 10.1) are able to use
|
|
the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``).
|