1d07479b2SMiguel Ojeda.. SPDX-License-Identifier: GPL-2.0 2d07479b2SMiguel Ojeda 3d07479b2SMiguel OjedaQuick Start 4d07479b2SMiguel Ojeda=========== 5d07479b2SMiguel Ojeda 6d07479b2SMiguel OjedaThis document describes how to get started with kernel development in Rust. 7d07479b2SMiguel Ojeda 8d07479b2SMiguel Ojeda 9d07479b2SMiguel OjedaRequirements: Building 10d07479b2SMiguel Ojeda---------------------- 11d07479b2SMiguel Ojeda 12d07479b2SMiguel OjedaThis section explains how to fetch the tools needed for building. 13d07479b2SMiguel Ojeda 14d07479b2SMiguel OjedaSome of these requirements might be available from Linux distributions 15d07479b2SMiguel Ojedaunder names like ``rustc``, ``rust-src``, ``rust-bindgen``, etc. However, 16d07479b2SMiguel Ojedaat the time of writing, they are likely not to be recent enough unless 17d07479b2SMiguel Ojedathe distribution tracks the latest releases. 18d07479b2SMiguel Ojeda 19d07479b2SMiguel OjedaTo easily check whether the requirements are met, the following target 20d07479b2SMiguel Ojedacan be used:: 21d07479b2SMiguel Ojeda 22d07479b2SMiguel Ojeda make LLVM=1 rustavailable 23d07479b2SMiguel Ojeda 24d07479b2SMiguel OjedaThis triggers the same logic used by Kconfig to determine whether 25d07479b2SMiguel Ojeda``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not 26d07479b2SMiguel Ojedaif that is the case. 27d07479b2SMiguel Ojeda 28d07479b2SMiguel Ojeda 29d07479b2SMiguel Ojedarustc 30d07479b2SMiguel Ojeda***** 31d07479b2SMiguel Ojeda 32d07479b2SMiguel OjedaA particular version of the Rust compiler is required. Newer versions may or 33d07479b2SMiguel Ojedamay not work because, for the moment, the kernel depends on some unstable 34d07479b2SMiguel OjedaRust features. 35d07479b2SMiguel Ojeda 36d07479b2SMiguel OjedaIf ``rustup`` is being used, enter the checked out source code directory 37d07479b2SMiguel Ojedaand run:: 38d07479b2SMiguel Ojeda 39d07479b2SMiguel Ojeda rustup override set $(scripts/min-tool-version.sh rustc) 40d07479b2SMiguel Ojeda 41*2285eb2fSTrevor GrossThis will configure your working directory to use the correct version of 42*2285eb2fSTrevor Gross``rustc`` without affecting your default toolchain. If you are not using 43*2285eb2fSTrevor Gross``rustup``, fetch a standalone installer from: 44d07479b2SMiguel Ojeda 456883b29cSMiguel Ojeda https://forge.rust-lang.org/infra/other-installation-methods.html#standalone 46d07479b2SMiguel Ojeda 47d07479b2SMiguel Ojeda 48d07479b2SMiguel OjedaRust standard library source 49d07479b2SMiguel Ojeda**************************** 50d07479b2SMiguel Ojeda 51d07479b2SMiguel OjedaThe Rust standard library source is required because the build system will 52d07479b2SMiguel Ojedacross-compile ``core`` and ``alloc``. 53d07479b2SMiguel Ojeda 54d07479b2SMiguel OjedaIf ``rustup`` is being used, run:: 55d07479b2SMiguel Ojeda 56d07479b2SMiguel Ojeda rustup component add rust-src 57d07479b2SMiguel Ojeda 58d07479b2SMiguel OjedaThe components are installed per toolchain, thus upgrading the Rust compiler 59d07479b2SMiguel Ojedaversion later on requires re-adding the component. 60d07479b2SMiguel Ojeda 618cb40124STrevor GrossOtherwise, if a standalone installer is used, the Rust source tree may be 628cb40124STrevor Grossdownloaded into the toolchain's installation folder:: 63d07479b2SMiguel Ojeda 648cb40124STrevor Gross curl -L "https://static.rust-lang.org/dist/rust-src-$(scripts/min-tool-version.sh rustc).tar.gz" | 658cb40124STrevor Gross tar -xzf - -C "$(rustc --print sysroot)/lib" \ 668cb40124STrevor Gross "rust-src-$(scripts/min-tool-version.sh rustc)/rust-src/lib/" \ 678cb40124STrevor Gross --strip-components=3 68d07479b2SMiguel Ojeda 69d07479b2SMiguel OjedaIn this case, upgrading the Rust compiler version later on requires manually 708cb40124STrevor Grossupdating the source tree (this can be done by removing ``$(rustc --print 718cb40124STrevor Grosssysroot)/lib/rustlib/src/rust`` then rerunning the above command). 72d07479b2SMiguel Ojeda 73d07479b2SMiguel Ojeda 74d07479b2SMiguel Ojedalibclang 75d07479b2SMiguel Ojeda******** 76d07479b2SMiguel Ojeda 77d07479b2SMiguel Ojeda``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code 78d07479b2SMiguel Ojedain the kernel, which means LLVM needs to be installed; like when the kernel 79d07479b2SMiguel Ojedais compiled with ``CC=clang`` or ``LLVM=1``. 80d07479b2SMiguel Ojeda 81d07479b2SMiguel OjedaLinux distributions are likely to have a suitable one available, so it is 82d07479b2SMiguel Ojedabest to check that first. 83d07479b2SMiguel Ojeda 84d07479b2SMiguel OjedaThere are also some binaries for several systems and architectures uploaded at: 85d07479b2SMiguel Ojeda 86d07479b2SMiguel Ojeda https://releases.llvm.org/download.html 87d07479b2SMiguel Ojeda 88d07479b2SMiguel OjedaOtherwise, building LLVM takes quite a while, but it is not a complex process: 89d07479b2SMiguel Ojeda 90d07479b2SMiguel Ojeda https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm 91d07479b2SMiguel Ojeda 92d07479b2SMiguel OjedaPlease see Documentation/kbuild/llvm.rst for more information and further ways 93d07479b2SMiguel Ojedato fetch pre-built releases and distribution packages. 94d07479b2SMiguel Ojeda 95d07479b2SMiguel Ojeda 96d07479b2SMiguel Ojedabindgen 97d07479b2SMiguel Ojeda******* 98d07479b2SMiguel Ojeda 99d07479b2SMiguel OjedaThe bindings to the C side of the kernel are generated at build time using 100d07479b2SMiguel Ojedathe ``bindgen`` tool. A particular version is required. 101d07479b2SMiguel Ojeda 102d07479b2SMiguel OjedaInstall it via (note that this will download and build the tool from source):: 103d07479b2SMiguel Ojeda 10408ab7865SAakash Sen Sharma cargo install --locked --version $(scripts/min-tool-version.sh bindgen) bindgen-cli 105d07479b2SMiguel Ojeda 106eae90172SMiguel Ojeda``bindgen`` needs to find a suitable ``libclang`` in order to work. If it is 107eae90172SMiguel Ojedanot found (or a different ``libclang`` than the one found should be used), 108eae90172SMiguel Ojedathe process can be tweaked using the environment variables understood by 109eae90172SMiguel Ojeda``clang-sys`` (the Rust bindings crate that ``bindgen`` uses to access 110eae90172SMiguel Ojeda``libclang``): 111eae90172SMiguel Ojeda 112eae90172SMiguel Ojeda* ``LLVM_CONFIG_PATH`` can be pointed to an ``llvm-config`` executable. 113eae90172SMiguel Ojeda 114eae90172SMiguel Ojeda* Or ``LIBCLANG_PATH`` can be pointed to a ``libclang`` shared library 115eae90172SMiguel Ojeda or to the directory containing it. 116eae90172SMiguel Ojeda 117eae90172SMiguel Ojeda* Or ``CLANG_PATH`` can be pointed to a ``clang`` executable. 118eae90172SMiguel Ojeda 119eae90172SMiguel OjedaFor details, please see ``clang-sys``'s documentation at: 120eae90172SMiguel Ojeda 121eae90172SMiguel Ojeda https://github.com/KyleMayes/clang-sys#environment-variables 122eae90172SMiguel Ojeda 123d07479b2SMiguel Ojeda 124d07479b2SMiguel OjedaRequirements: Developing 125d07479b2SMiguel Ojeda------------------------ 126d07479b2SMiguel Ojeda 127d07479b2SMiguel OjedaThis section explains how to fetch the tools needed for developing. That is, 128d07479b2SMiguel Ojedathey are not needed when just building the kernel. 129d07479b2SMiguel Ojeda 130d07479b2SMiguel Ojeda 131d07479b2SMiguel Ojedarustfmt 132d07479b2SMiguel Ojeda******* 133d07479b2SMiguel Ojeda 134d07479b2SMiguel OjedaThe ``rustfmt`` tool is used to automatically format all the Rust kernel code, 135d07479b2SMiguel Ojedaincluding the generated C bindings (for details, please see 136d07479b2SMiguel Ojedacoding-guidelines.rst). 137d07479b2SMiguel Ojeda 138d07479b2SMiguel OjedaIf ``rustup`` is being used, its ``default`` profile already installs the tool, 139d07479b2SMiguel Ojedathus nothing needs to be done. If another profile is being used, the component 140d07479b2SMiguel Ojedacan be installed manually:: 141d07479b2SMiguel Ojeda 142d07479b2SMiguel Ojeda rustup component add rustfmt 143d07479b2SMiguel Ojeda 144d07479b2SMiguel OjedaThe standalone installers also come with ``rustfmt``. 145d07479b2SMiguel Ojeda 146d07479b2SMiguel Ojeda 147d07479b2SMiguel Ojedaclippy 148d07479b2SMiguel Ojeda****** 149d07479b2SMiguel Ojeda 150d07479b2SMiguel Ojeda``clippy`` is a Rust linter. Running it provides extra warnings for Rust code. 151d07479b2SMiguel OjedaIt can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see 152d07479b2SMiguel Ojedageneral-information.rst). 153d07479b2SMiguel Ojeda 154d07479b2SMiguel OjedaIf ``rustup`` is being used, its ``default`` profile already installs the tool, 155d07479b2SMiguel Ojedathus nothing needs to be done. If another profile is being used, the component 156d07479b2SMiguel Ojedacan be installed manually:: 157d07479b2SMiguel Ojeda 158d07479b2SMiguel Ojeda rustup component add clippy 159d07479b2SMiguel Ojeda 160d07479b2SMiguel OjedaThe standalone installers also come with ``clippy``. 161d07479b2SMiguel Ojeda 162d07479b2SMiguel Ojeda 163d07479b2SMiguel Ojedacargo 164d07479b2SMiguel Ojeda***** 165d07479b2SMiguel Ojeda 166d07479b2SMiguel Ojeda``cargo`` is the Rust native build system. It is currently required to run 167d07479b2SMiguel Ojedathe tests since it is used to build a custom standard library that contains 168d07479b2SMiguel Ojedathe facilities provided by the custom ``alloc`` in the kernel. The tests can 169d07479b2SMiguel Ojedabe run using the ``rusttest`` Make target. 170d07479b2SMiguel Ojeda 171d07479b2SMiguel OjedaIf ``rustup`` is being used, all the profiles already install the tool, 172d07479b2SMiguel Ojedathus nothing needs to be done. 173d07479b2SMiguel Ojeda 174d07479b2SMiguel OjedaThe standalone installers also come with ``cargo``. 175d07479b2SMiguel Ojeda 176d07479b2SMiguel Ojeda 177d07479b2SMiguel Ojedarustdoc 178d07479b2SMiguel Ojeda******* 179d07479b2SMiguel Ojeda 180d07479b2SMiguel Ojeda``rustdoc`` is the documentation tool for Rust. It generates pretty HTML 181d07479b2SMiguel Ojedadocumentation for Rust code (for details, please see 182d07479b2SMiguel Ojedageneral-information.rst). 183d07479b2SMiguel Ojeda 184d07479b2SMiguel Ojeda``rustdoc`` is also used to test the examples provided in documented Rust code 185d07479b2SMiguel Ojeda(called doctests or documentation tests). The ``rusttest`` Make target uses 186d07479b2SMiguel Ojedathis feature. 187d07479b2SMiguel Ojeda 188d07479b2SMiguel OjedaIf ``rustup`` is being used, all the profiles already install the tool, 189d07479b2SMiguel Ojedathus nothing needs to be done. 190d07479b2SMiguel Ojeda 191d07479b2SMiguel OjedaThe standalone installers also come with ``rustdoc``. 192d07479b2SMiguel Ojeda 193d07479b2SMiguel Ojeda 194d07479b2SMiguel Ojedarust-analyzer 195d07479b2SMiguel Ojeda************* 196d07479b2SMiguel Ojeda 197d07479b2SMiguel OjedaThe `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can 198d07479b2SMiguel Ojedabe used with many editors to enable syntax highlighting, completion, go to 199d07479b2SMiguel Ojedadefinition, and other features. 200d07479b2SMiguel Ojeda 201d07479b2SMiguel Ojeda``rust-analyzer`` needs a configuration file, ``rust-project.json``, which 202b603c6ccSGuillaume Plourdecan be generated by the ``rust-analyzer`` Make target:: 203b603c6ccSGuillaume Plourde 204b603c6ccSGuillaume Plourde make LLVM=1 rust-analyzer 205d07479b2SMiguel Ojeda 206d07479b2SMiguel Ojeda 207d07479b2SMiguel OjedaConfiguration 208d07479b2SMiguel Ojeda------------- 209d07479b2SMiguel Ojeda 210d07479b2SMiguel Ojeda``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup`` 211d07479b2SMiguel Ojedamenu. The option is only shown if a suitable Rust toolchain is found (see 212d07479b2SMiguel Ojedaabove), as long as the other requirements are met. In turn, this will make 213d07479b2SMiguel Ojedavisible the rest of options that depend on Rust. 214d07479b2SMiguel Ojeda 215d07479b2SMiguel OjedaAfterwards, go to:: 216d07479b2SMiguel Ojeda 217d07479b2SMiguel Ojeda Kernel hacking 218d07479b2SMiguel Ojeda -> Sample kernel code 219d07479b2SMiguel Ojeda -> Rust samples 220d07479b2SMiguel Ojeda 221d07479b2SMiguel OjedaAnd enable some sample modules either as built-in or as loadable. 222d07479b2SMiguel Ojeda 223d07479b2SMiguel Ojeda 224d07479b2SMiguel OjedaBuilding 225d07479b2SMiguel Ojeda-------- 226d07479b2SMiguel Ojeda 227d07479b2SMiguel OjedaBuilding a kernel with a complete LLVM toolchain is the best supported setup 228d07479b2SMiguel Ojedaat the moment. That is:: 229d07479b2SMiguel Ojeda 230d07479b2SMiguel Ojeda make LLVM=1 231d07479b2SMiguel Ojeda 232d07479b2SMiguel OjedaFor architectures that do not support a full LLVM toolchain, use:: 233d07479b2SMiguel Ojeda 234d07479b2SMiguel Ojeda make CC=clang 235d07479b2SMiguel Ojeda 236d07479b2SMiguel OjedaUsing GCC also works for some configurations, but it is very experimental at 237d07479b2SMiguel Ojedathe moment. 238d07479b2SMiguel Ojeda 239d07479b2SMiguel Ojeda 240d07479b2SMiguel OjedaHacking 241d07479b2SMiguel Ojeda------- 242d07479b2SMiguel Ojeda 243d07479b2SMiguel OjedaTo dive deeper, take a look at the source code of the samples 244d07479b2SMiguel Ojedaat ``samples/rust/``, the Rust support code under ``rust/`` and 245d07479b2SMiguel Ojedathe ``Rust hacking`` menu under ``Kernel hacking``. 246d07479b2SMiguel Ojeda 247d07479b2SMiguel OjedaIf GDB/Binutils is used and Rust symbols are not getting demangled, the reason 248d07479b2SMiguel Ojedais the toolchain does not support Rust's new v0 mangling scheme yet. 249d07479b2SMiguel OjedaThere are a few ways out: 250d07479b2SMiguel Ojeda 251d07479b2SMiguel Ojeda - Install a newer release (GDB >= 10.2, Binutils >= 2.36). 252d07479b2SMiguel Ojeda 253d07479b2SMiguel Ojeda - Some versions of GDB (e.g. vanilla GDB 10.1) are able to use 254d07479b2SMiguel Ojeda the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``). 255