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 19526c5394SNathan ChancellorPrebuilt stable versions of LLVM+Rust are provided on `kernel.org 20526c5394SNathan Chancellor<https://kernel.org/pub/tools/llvm/rust/>`_. These are the same slim and fast 21526c5394SNathan ChancellorLLVM toolchains from :ref:`Getting LLVM <getting_llvm>` with versions of Rust 22526c5394SNathan Chancelloradded to them that Rust for Linux supports, depending on the Linux version. Two 23526c5394SNathan Chancellorsets are provided: the "latest LLVM" and "matching LLVM" (please see the link 24526c5394SNathan Chancellorfor more information). 25526c5394SNathan Chancellor 26d07479b2SMiguel OjedaTo easily check whether the requirements are met, the following target 27d07479b2SMiguel Ojedacan be used:: 28d07479b2SMiguel Ojeda 29d07479b2SMiguel Ojeda make LLVM=1 rustavailable 30d07479b2SMiguel Ojeda 31d07479b2SMiguel OjedaThis triggers the same logic used by Kconfig to determine whether 32d07479b2SMiguel Ojeda``RUST_IS_AVAILABLE`` should be enabled; but it also explains why not 33d07479b2SMiguel Ojedaif that is the case. 34d07479b2SMiguel Ojeda 35d07479b2SMiguel Ojeda 36d07479b2SMiguel Ojedarustc 37d07479b2SMiguel Ojeda***** 38d07479b2SMiguel Ojeda 3963b27f4aSMiguel OjedaA recent version of the Rust compiler is required. 40d07479b2SMiguel Ojeda 41711cbfc7SViresh KumarIf ``rustup`` is being used, enter the kernel build directory (or use 4263b27f4aSMiguel Ojeda``--path=<build-dir>`` argument to the ``set`` sub-command) and run, 4363b27f4aSMiguel Ojedafor instance:: 44d07479b2SMiguel Ojeda 4563b27f4aSMiguel Ojeda rustup override set stable 46d07479b2SMiguel Ojeda 4763b27f4aSMiguel OjedaThis will configure your working directory to use the given version of 48711cbfc7SViresh Kumar``rustc`` without affecting your default toolchain. 49711cbfc7SViresh Kumar 50711cbfc7SViresh KumarNote that the override applies to the current working directory (and its 51711cbfc7SViresh Kumarsub-directories). 52711cbfc7SViresh Kumar 53711cbfc7SViresh KumarIf you are not using ``rustup``, fetch a standalone installer from: 54d07479b2SMiguel Ojeda 556883b29cSMiguel Ojeda https://forge.rust-lang.org/infra/other-installation-methods.html#standalone 56d07479b2SMiguel Ojeda 57d07479b2SMiguel Ojeda 58d07479b2SMiguel OjedaRust standard library source 59d07479b2SMiguel Ojeda**************************** 60d07479b2SMiguel Ojeda 61d07479b2SMiguel OjedaThe Rust standard library source is required because the build system will 62d07479b2SMiguel Ojedacross-compile ``core`` and ``alloc``. 63d07479b2SMiguel Ojeda 64d07479b2SMiguel OjedaIf ``rustup`` is being used, run:: 65d07479b2SMiguel Ojeda 66d07479b2SMiguel Ojeda rustup component add rust-src 67d07479b2SMiguel Ojeda 68d07479b2SMiguel OjedaThe components are installed per toolchain, thus upgrading the Rust compiler 69d07479b2SMiguel Ojedaversion later on requires re-adding the component. 70d07479b2SMiguel Ojeda 718cb40124STrevor GrossOtherwise, if a standalone installer is used, the Rust source tree may be 728cb40124STrevor Grossdownloaded into the toolchain's installation folder:: 73d07479b2SMiguel Ojeda 7463b27f4aSMiguel Ojeda curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" | 758cb40124STrevor Gross tar -xzf - -C "$(rustc --print sysroot)/lib" \ 7663b27f4aSMiguel Ojeda "rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \ 778cb40124STrevor Gross --strip-components=3 78d07479b2SMiguel Ojeda 79d07479b2SMiguel OjedaIn this case, upgrading the Rust compiler version later on requires manually 808cb40124STrevor Grossupdating the source tree (this can be done by removing ``$(rustc --print 818cb40124STrevor Grosssysroot)/lib/rustlib/src/rust`` then rerunning the above command). 82d07479b2SMiguel Ojeda 83d07479b2SMiguel Ojeda 84d07479b2SMiguel Ojedalibclang 85d07479b2SMiguel Ojeda******** 86d07479b2SMiguel Ojeda 87d07479b2SMiguel Ojeda``libclang`` (part of LLVM) is used by ``bindgen`` to understand the C code 88d07479b2SMiguel Ojedain the kernel, which means LLVM needs to be installed; like when the kernel 897583ce66SMiguel Ojedais compiled with ``LLVM=1``. 90d07479b2SMiguel Ojeda 91d07479b2SMiguel OjedaLinux distributions are likely to have a suitable one available, so it is 92d07479b2SMiguel Ojedabest to check that first. 93d07479b2SMiguel Ojeda 94d07479b2SMiguel OjedaThere are also some binaries for several systems and architectures uploaded at: 95d07479b2SMiguel Ojeda 96d07479b2SMiguel Ojeda https://releases.llvm.org/download.html 97d07479b2SMiguel Ojeda 98d07479b2SMiguel OjedaOtherwise, building LLVM takes quite a while, but it is not a complex process: 99d07479b2SMiguel Ojeda 100d07479b2SMiguel Ojeda https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm 101d07479b2SMiguel Ojeda 102d07479b2SMiguel OjedaPlease see Documentation/kbuild/llvm.rst for more information and further ways 103d07479b2SMiguel Ojedato fetch pre-built releases and distribution packages. 104d07479b2SMiguel Ojeda 105d07479b2SMiguel Ojeda 106d07479b2SMiguel Ojedabindgen 107d07479b2SMiguel Ojeda******* 108d07479b2SMiguel Ojeda 109d07479b2SMiguel OjedaThe bindings to the C side of the kernel are generated at build time using 110*c844fa64SMiguel Ojedathe ``bindgen`` tool. 111d07479b2SMiguel Ojeda 112*c844fa64SMiguel OjedaInstall it, for instance, via (note that this will download and build the tool 113*c844fa64SMiguel Ojedafrom source):: 114d07479b2SMiguel Ojeda 115*c844fa64SMiguel Ojeda cargo install --locked bindgen-cli 116d07479b2SMiguel Ojeda 117d49082faSMiguel Ojeda``bindgen`` uses the ``clang-sys`` crate to find a suitable ``libclang`` (which 118d49082faSMiguel Ojedamay be linked statically, dynamically or loaded at runtime). By default, the 119d49082faSMiguel Ojeda``cargo`` command above will produce a ``bindgen`` binary that will load 120d49082faSMiguel Ojeda``libclang`` at runtime. If it is not found (or a different ``libclang`` than 121d49082faSMiguel Ojedathe one found should be used), the process can be tweaked, e.g. by using the 122d49082faSMiguel Ojeda``LIBCLANG_PATH`` environment variable. For details, please see ``clang-sys``'s 123d49082faSMiguel Ojedadocumentation at: 124eae90172SMiguel Ojeda 125d49082faSMiguel Ojeda https://github.com/KyleMayes/clang-sys#linking 126eae90172SMiguel Ojeda 127eae90172SMiguel Ojeda https://github.com/KyleMayes/clang-sys#environment-variables 128eae90172SMiguel Ojeda 129d07479b2SMiguel Ojeda 130d07479b2SMiguel OjedaRequirements: Developing 131d07479b2SMiguel Ojeda------------------------ 132d07479b2SMiguel Ojeda 133d07479b2SMiguel OjedaThis section explains how to fetch the tools needed for developing. That is, 134d07479b2SMiguel Ojedathey are not needed when just building the kernel. 135d07479b2SMiguel Ojeda 136d07479b2SMiguel Ojeda 137d07479b2SMiguel Ojedarustfmt 138d07479b2SMiguel Ojeda******* 139d07479b2SMiguel Ojeda 140d07479b2SMiguel OjedaThe ``rustfmt`` tool is used to automatically format all the Rust kernel code, 141d07479b2SMiguel Ojedaincluding the generated C bindings (for details, please see 142d07479b2SMiguel Ojedacoding-guidelines.rst). 143d07479b2SMiguel Ojeda 144d07479b2SMiguel OjedaIf ``rustup`` is being used, its ``default`` profile already installs the tool, 145d07479b2SMiguel Ojedathus nothing needs to be done. If another profile is being used, the component 146d07479b2SMiguel Ojedacan be installed manually:: 147d07479b2SMiguel Ojeda 148d07479b2SMiguel Ojeda rustup component add rustfmt 149d07479b2SMiguel Ojeda 150d07479b2SMiguel OjedaThe standalone installers also come with ``rustfmt``. 151d07479b2SMiguel Ojeda 152d07479b2SMiguel Ojeda 153d07479b2SMiguel Ojedaclippy 154d07479b2SMiguel Ojeda****** 155d07479b2SMiguel Ojeda 156d07479b2SMiguel Ojeda``clippy`` is a Rust linter. Running it provides extra warnings for Rust code. 157d07479b2SMiguel OjedaIt can be run by passing ``CLIPPY=1`` to ``make`` (for details, please see 158d07479b2SMiguel Ojedageneral-information.rst). 159d07479b2SMiguel Ojeda 160d07479b2SMiguel OjedaIf ``rustup`` is being used, its ``default`` profile already installs the tool, 161d07479b2SMiguel Ojedathus nothing needs to be done. If another profile is being used, the component 162d07479b2SMiguel Ojedacan be installed manually:: 163d07479b2SMiguel Ojeda 164d07479b2SMiguel Ojeda rustup component add clippy 165d07479b2SMiguel Ojeda 166d07479b2SMiguel OjedaThe standalone installers also come with ``clippy``. 167d07479b2SMiguel Ojeda 168d07479b2SMiguel Ojeda 169d07479b2SMiguel Ojedarustdoc 170d07479b2SMiguel Ojeda******* 171d07479b2SMiguel Ojeda 172d07479b2SMiguel Ojeda``rustdoc`` is the documentation tool for Rust. It generates pretty HTML 173d07479b2SMiguel Ojedadocumentation for Rust code (for details, please see 174d07479b2SMiguel Ojedageneral-information.rst). 175d07479b2SMiguel Ojeda 176d07479b2SMiguel Ojeda``rustdoc`` is also used to test the examples provided in documented Rust code 177d07479b2SMiguel Ojeda(called doctests or documentation tests). The ``rusttest`` Make target uses 178d07479b2SMiguel Ojedathis feature. 179d07479b2SMiguel Ojeda 180d07479b2SMiguel OjedaIf ``rustup`` is being used, all the profiles already install the tool, 181d07479b2SMiguel Ojedathus nothing needs to be done. 182d07479b2SMiguel Ojeda 183d07479b2SMiguel OjedaThe standalone installers also come with ``rustdoc``. 184d07479b2SMiguel Ojeda 185d07479b2SMiguel Ojeda 186d07479b2SMiguel Ojedarust-analyzer 187d07479b2SMiguel Ojeda************* 188d07479b2SMiguel Ojeda 189d07479b2SMiguel OjedaThe `rust-analyzer <https://rust-analyzer.github.io/>`_ language server can 190d07479b2SMiguel Ojedabe used with many editors to enable syntax highlighting, completion, go to 191d07479b2SMiguel Ojedadefinition, and other features. 192d07479b2SMiguel Ojeda 193d07479b2SMiguel Ojeda``rust-analyzer`` needs a configuration file, ``rust-project.json``, which 194b603c6ccSGuillaume Plourdecan be generated by the ``rust-analyzer`` Make target:: 195b603c6ccSGuillaume Plourde 196b603c6ccSGuillaume Plourde make LLVM=1 rust-analyzer 197d07479b2SMiguel Ojeda 198d07479b2SMiguel Ojeda 199d07479b2SMiguel OjedaConfiguration 200d07479b2SMiguel Ojeda------------- 201d07479b2SMiguel Ojeda 202d07479b2SMiguel Ojeda``Rust support`` (``CONFIG_RUST``) needs to be enabled in the ``General setup`` 203d07479b2SMiguel Ojedamenu. The option is only shown if a suitable Rust toolchain is found (see 204d07479b2SMiguel Ojedaabove), as long as the other requirements are met. In turn, this will make 205d07479b2SMiguel Ojedavisible the rest of options that depend on Rust. 206d07479b2SMiguel Ojeda 207d07479b2SMiguel OjedaAfterwards, go to:: 208d07479b2SMiguel Ojeda 209d07479b2SMiguel Ojeda Kernel hacking 210d07479b2SMiguel Ojeda -> Sample kernel code 211d07479b2SMiguel Ojeda -> Rust samples 212d07479b2SMiguel Ojeda 213d07479b2SMiguel OjedaAnd enable some sample modules either as built-in or as loadable. 214d07479b2SMiguel Ojeda 215d07479b2SMiguel Ojeda 216d07479b2SMiguel OjedaBuilding 217d07479b2SMiguel Ojeda-------- 218d07479b2SMiguel Ojeda 219d07479b2SMiguel OjedaBuilding a kernel with a complete LLVM toolchain is the best supported setup 220d07479b2SMiguel Ojedaat the moment. That is:: 221d07479b2SMiguel Ojeda 222d07479b2SMiguel Ojeda make LLVM=1 223d07479b2SMiguel Ojeda 224d07479b2SMiguel OjedaUsing GCC also works for some configurations, but it is very experimental at 225d07479b2SMiguel Ojedathe moment. 226d07479b2SMiguel Ojeda 227d07479b2SMiguel Ojeda 228d07479b2SMiguel OjedaHacking 229d07479b2SMiguel Ojeda------- 230d07479b2SMiguel Ojeda 231d07479b2SMiguel OjedaTo dive deeper, take a look at the source code of the samples 232d07479b2SMiguel Ojedaat ``samples/rust/``, the Rust support code under ``rust/`` and 233d07479b2SMiguel Ojedathe ``Rust hacking`` menu under ``Kernel hacking``. 234d07479b2SMiguel Ojeda 235d07479b2SMiguel OjedaIf GDB/Binutils is used and Rust symbols are not getting demangled, the reason 236d07479b2SMiguel Ojedais the toolchain does not support Rust's new v0 mangling scheme yet. 237d07479b2SMiguel OjedaThere are a few ways out: 238d07479b2SMiguel Ojeda 239d07479b2SMiguel Ojeda - Install a newer release (GDB >= 10.2, Binutils >= 2.36). 240d07479b2SMiguel Ojeda 241d07479b2SMiguel Ojeda - Some versions of GDB (e.g. vanilla GDB 10.1) are able to use 242d07479b2SMiguel Ojeda the pre-demangled names embedded in the debug info (``CONFIG_DEBUG_INFO``). 243