Rust for Linux


Rust for Linux

The goal of this project is to add support for the Rust language to the Linux kernel. This repository contains the work that will be eventually submitted for review to the LKML.

Feel free to contribute! To start, take a look at Documentation/rust.

General discussions, announcements, questions, etc. take place on the mailing list at [email protected] (subscribe, instructions, archive). For chat, help, quick questions, informal discussion, etc. you may want to join our Zulip at (request an invitation).

All contributors to this effort are understood to have agreed to the Linux kernel development process as explained in the different files under Documentation/process.

  • [Issue #296] Rust: Use a tighter inner type for Error

    [Issue #296] Rust: Use a tighter inner type for Error

    Use NonZeroI32 as the inner type. This allows Result<()> to fit in 32-bit, instead of 64-bit.


    • Use NonZeroI32 instead of NonZeroI16
    • Rewrite kernel_const_to_error as a const function
    • Use unsafe NonZeroI32 creator in from_kernel_errno to avoid unwrap()


    • Add a comment explaining the reason for choosing NonZeroI32 to Error
    • Rewrite kernel_const_to_error() back to a macro and add static_assertion! to it.
    • Use Error(unsafe { NonZeroI16::new_unchecked(errno as i16) }) in from_kernel_error() to make it terser


    • Following Miguel's review, fix some comment format problems


    • Rewrite kernel_const_to_error() to a const function again. 😮‍💨

    Related commit: #296 May influence: #358

    opened by foxhlchen 63
  • Use `rustc` directly instead of `cargo`

    Use `rustc` directly instead of `cargo`

    This is a big PR, but most of it is interdependent to the rest.

    • Shared Rust infrastructure: libkernel, libmodule, libcore, liballoc, libcompiler_builtins.

      • The Rust modules are now much smaller since they do not contain several copies of those libraries. Our example .ko on release is just 12 KiB, down from 1.3 MiB. For reference (i.e. the bulk is now shared):
        • vmlinux on release with Rust is 23 MiB (compressed: 2.1 MiB)
        • vmlinux on release without Rust is 22 MiB (compressed: 1.9 MiB)
      • Multiple builtin modules are now supported since their symbols do not collide against each other (fixes #9).
      • Faster compilation (less crates to compile & less repetition).
      • We achieve this by compiling all the shared code to .rlibs (and the .so for the proc macro). For loadable modules, we need to rely on the upcoming v0 Rust mangling scheme, plus we need to export the Rust symbols needed by the .kos.
    • Simpler, flat file structure: now a small driver may only need a single file like drivers/char/, like in C.

      • All the rust/* and driver/char/rust_example/* files moved to fit in the new structure: less files around.
    • Only rust-lang/{rust,rust-bindgen,compiler-builtins} as dependencies.

      • Also helps with the faster compilation.
    • Dependency handling integration with Kbuild/fixdep.

      • Changes to the Rust standard library, kernel headers (bindings), rust/ source files, .rs changes, command-line changes, flag changes, etc. all trigger recompilation as needed.
      • Works as expected with parallel support (-j).
    • Support for custom --sysroot via KRUSTCFLAGS.

    • Proper make clean support.

    • Offline builds by default (there is no "online compilation" anymore; fixes #17).

    • No interleaved Cargo output (fixes #29).

    • No nightly dependency on Cargo's build-std; since now we manage the cross-compilation ourselves (should fix #27).

    • Since now a kernel can be "Rust-enabled", a new CONFIG_RUST option is added to enable/disable it manually, regardless of whether one has rustc available or not (CONFIG_HAS_RUST).

    • Improved handling of rustc flags (opt-level, debuginfo, etc.), following what the user selected for C (no Cargo profiles).

    • Added Kconfig menu for tweaking relevant rustc options, like overflow checks, debug assertions, optimization level, etc.

    • This rewrite of the Kbuild support is cleaner, i.e. less hacks in general handling paths (e.g. no more shell readlink for O=).

    • Duplicated the example driver 3 times so that we can test in the CI that 2 builtins and 2 loadables work, all at the same time.

    • Updated the quick start guide.

    • Updated CI .configs:

      • Add the new options and test with 2 builtins and 2 loadables. At the same time, remove the matrix test for builtin/loadable.
      • Debug: more things enabled (debuginfo, kgdb, unit testing, etc.) that mimic more what a developer would have. Running the CI will be slightly slower, but should be OK.
      • Release: disabled EXPERT and changed a few things to make it look more like a normal configuration.
      • Also update both configs to v5.9 while I was at it.

      (I could have split a few of these ones off into another PR, but anyway it is for the CI only and I had already done it).

    • Less extern crates needed since we pass it via rustc (closer to idiomatic 2018 edition Rust code).

    Things to note:

    • There is two more nightly features used:

      • The new Rust mangling scheme: we know it will be stable (and the default on, later on).
      • The binary dep-info output: if we remove all other nightly features, this one can easily go too.
    • The hack at exports.c to export symbols to loadable modules.

    • The hack at to get the __rust_*() functions.

    Signed-off-by: Miguel Ojeda [email protected]

    opened by ojeda 46
  • rust/kernel: remove config `#ifdef` in file

    rust/kernel: remove config `#ifdef` in file

    The use of #ifdef CONFIG_ statements in .c/.rs files is deprecated: it makes the code much more unmaintainable over time. See:

    Use a Rust-C helper instead to leverage automatic CONFIG selection in C kernel headers.

    Signed-off-by: Sven Van Asbroeck [email protected]

    opened by TheSven73 45
  • rust: check range and add type invariant to Error (issue #295)

    rust: check range and add type invariant to Error (issue #295)

    We will need to make sure that no Error with out of range error code can be constructed.

    This commit

    1. Add errno check in from_kernel_errno()
    2. Provides a unchecked version from_kernel_errno_unchecked()

    And when an invalid errno is found, it will

    1. Print a warning.
    2. Convert it to EINVAL.

    Signed-off-by: Fox Chen [email protected]

    Issue #295

    opened by foxhlchen 38
  • WIP: Seq file

    WIP: Seq file

    Add a Rust trait equivalent to the C seq_operations interface and the ability to create a /proc file from a type implementing that trait. Instead of just translating each function in the interface to Rust I used an iterator instead since it maps pretty cleanly. But happy to change it if we thing it makes more sense to keep things close to the C side.

    A couple notes:

    1. I split this into two commits, where the second contains only CI/CD changes, so the first commit can be eventually upstreamed, but not the second. Does this make sense or should we be doing something else?
    2. In the sample driver I called some panicking functions, but I can't remember what notation we decided on for for the comments on why they won't panic (just used NO PANIC).
    3. There's one point in where I needed to allocate a string, but couldn't get what I needed using try_reserve, so just left a TODO about fixing it when we have more fallible options.
    4. Added some testing in the CI that actually uses the sample driver instead of just loading/unloading it. I needed this for my own testing, but it seems like we're not doing it for any other sample drivers. Is there a reason for that?
    opened by adamrk 32
  • Depend on `syn`

    Depend on `syn`

    Add syn and others as dependencies of macros crate, and use cargo to build macros crate. Only host-facing is built this way, not any of the other crates that are compiled for the target. This cargo invocation has all crate versions locked and has --offline option specified, so it won't access Internet during the build.

    The current module! crate already shows it tedious and limited for writing proc macros without syn. Pinning and vtable handling could likely be drastically improved with the help of proc macros, and they will require parsing Rust struct/trait/impl blocks. A dependency on syn is highly desirable to avoid us essentially reinventing the wheel when building our procedural macros.

    A make rust-fetch-deps command is added and listed in the documentation as a build requirement.

    Related #278

    prio: low 
    opened by nbdd0121 29
  • rust/kernel/of: construct `of_match_table` structure at build time

    rust/kernel/of: construct `of_match_table` structure at build time

    Use const generics and const fns to construct our of_match_table structure at build time. A few runtime errors now become build time errors, which is a good thing.

    To avoid the const generics propagating into the platdev's Registration structure definition, use a dyn trait to pass the table into the registration.

    Signed-off-by: Sven Van Asbroeck [email protected]

    opened by TheSven73 28
  • binder benchmark causes kernel `BUG` on Raspberry Pi

    binder benchmark causes kernel `BUG` on Raspberry Pi

    @wedsonaf (this Issue doesn't really belong here, but I can't open an Issue on wedson's fork)

    1. follow
    2. build for Raspberry Pi: $ make bcm2835_rust_defconfig then switch on binder in defconfig
    3. Boot kernel on Raspberry Pi Zero
    4. ./ping_server /dev/rust_binder & works great!
    5. ./ping_client /dev/rust_binder oopses:
    root@raspberrypi:/home/pi# ./ping_client /dev/rust_binder
    [  149.443939] ------------[ cut here ]------------
    [  149.446521] kernel BUG at rust/helpers.c:14!
    [  149.446812] Internal error: Oops - BUG: 0 [#1] ARM
    [  149.447165] Modules linked in:
    [  149.447946] CPU: 0 PID: 359 Comm: ping_client Tainted: G        W         5.12.0-rc4+ #8
    [  149.448448] Hardware name: BCM2835
    [  149.448735] PC is at rust_helper_BUG+0x10/0x14
    [  149.449441] LR is at rust_begin_unwind+0x4/0x8
    [  149.449634] pc : [<c04631b0>]    lr : [<c0462888>]    psr: 00000013
    [  149.449919] sp : c404dcc8  ip : c0b80cec  fp : c404dcc8
    [  149.450129] r10: c437c0c0  r9 : 00000000  r8 : 49034000
    [  149.450341] r7 : c30bed2c  r6 : c30bed00  r5 : c404de58  r4 : c30bed08
    [  149.450542] r3 : 00000000  r2 : c404dcf8  r1 : c0b80cec  r0 : c404dcd0
    [  149.450853] Flags: nzcv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment none
    [  149.451220] Control: 10c53c7d  Table: 04308059  DAC: 00000051
    [  149.451421] Process ping_client (pid: 359, stack limit = 0x(ptrval))
    [  149.451793] Stack: (0xc404dcc8 to 0xc404e000)
    [  149.452278] dcc0:                   c404ddc0 c0462888 c0b20754 c0b22a98 c404dce0 c0b80cec
    [  149.452808] dce0: c404dcf8 00000001 00000000 00000000 c0b20754 00000000 c0b7fc00 00000021
    [  149.453659] dd00: 00000000 c30bed00 00000000 c404de58 c404dd90 c404ddb8 c437c0d0 c437c0c0
    [  149.454648] dd20: c404ddc0 c0749790 c07461ac c404de28 c404dd50 c0233b20 0000000c c404ddc4
    [  149.455561] dd40: c404ddd0 c404ddcc c404de84 c404de38 c404de30 c404de14 c404de04 c404de5c
    [  149.456404] dd60: c404de60 c404dd90 c404dda8 00000001 0000004c c437c0c0 bea503f8 00000000
    [  149.457521] dd80: bea503c8 00000030 0000004c 00000000 00000000 00000000 00021048 00000000
    [  149.458592] dda0: 00000800 00000000 00000000 00000000 bea503f8 00000000 00021050 00000044
    [  149.459703] ddc0: 00000024 c404de28 00000001 c073b3f8 c437cec0 c404de28 c401f960 00000000
    [  149.460790] dde0: 00000001 c073c1fc 00000000 c074b2e0 00000000 c30bed08 00000001 c074b2e0
    [  149.461852] de00: 00000002 c30bed00 c437cec0 00000001 00000001 c126662c c437cd80 00000001
    [  149.462407] de20: c437cd80 00000001 c437cec0 00000000 00000000 c0248394 c401f960 c401f960
    [  149.462842] de40: c401f960 c401f960 c16ee780 c401f840 c404de60 c02489a8 00000000 00000030
    [  149.463289] de60: 0000004c 00000000 00000000 00000000 00021048 00000000 00000800 00000000
    [  149.463822] de80: 00000000 00000000 bea503f8 00000000 c405bb40 00000000 f9cf40ab fffffff4
    [  149.464278] dea0: 00000000 c401f840 00000000 c404c000 c401f854 c405bb40 c21b3000 00000001
    [  149.464708] dec0: 00000071 00000001 ffffffea c0306201 00000001 c30bed00 c405bb40 c405bb40
    [  149.465170] dee0: c404dfa0 c0733f80 00000030 c30bed00 c404df24 00000030 c405bb40 c30bed00
    [  149.465742] df00: c404df10 c0306201 bea503c8 c0738104 c0306201 bea503c8 00000000 bea503c8
    [  149.466167] df20: 00000030 c405bb40 00000003 c16ee690 c404df60 c028d42c 00000006 00001000
    [  149.466691] df40: 00000001 c404df44 c404df44 00000000 f9cf40ab c405bb40 00000000 00000000
    [  149.467298] df60: 00000002 c0248668 c405bb40 b6fcc000 c404dfa0 c0248668 00000002 00000000
    [  149.467844] df80: f9cf40ab bea503f8 00000000 0001059d 00000036 c0100264 c404c000 00000036
    [  149.468396] dfa0: 00000000 c0100060 bea503f8 00000000 00000003 c0306201 bea503c8 bea503c8
    [  149.468864] dfc0: bea503f8 00000000 0001059d 00000036 00021048 00000000 b6fd0000 00000000
    [  149.469355] dfe0: b6ef6510 bea503b4 0001086b b6ef651c 60000010 00000003 00000000 00000000
    [  149.470240] [<c04631b0>] (rust_helper_BUG) from [<c0462888>] (rust_begin_unwind+0x4/0x8)
    [  149.470959] Code: e92d4800 e1a0b00d e92d4000 e8bd4000 (e7f001f2) 
    [  149.471647] ---[ end trace faab23bf7569842d ]---
    prio: normal • bug 
    opened by TheSven73 28
  • rust: generate bindings for helpers

    rust: generate bindings for helpers

    Helper functions are now only compiled if the wrapped function is not made directly available via bindgen. For the compiled helper functions, bindings for them are automatically generated via a second invocation to bindgen, and the corresponding binding will have its rust_helper_ prefix removed, so Rust code can call bindings::foo regardless whether foo is directly exposed or exposed via a helper.

    Details about how this works:

    • is first created via bindgen;
    • List of functions are extracted from and a rust_helper_foo macro is generated for each function directly exposed this way;
    • helpers.c can then use C preprocessor to conditionally compile helpers only if they are not directly exposed.
    • bindgen invokes again to create bindings for helpers.c.
    • bindgen output is then postprocessed to remove the rust_helper prefix and add #[link_name = ""].

    Related #352

    opened by nbdd0121 25
  • Support Rust `core::fmt::Argument` in vsprintf

    Support Rust `core::fmt::Argument` in vsprintf

    This PR adds to format %pfr format to vsprintf which formats a pointer as a core::fmt::Arguments. Doing so allows us to directly format to the internal buffer of printk, so we don't have to assemble the message on a temporary buffer on the stack.

    This does however require changes on the C-side, so I made this PR a RFC.

    opened by nbdd0121 23
  • Build-time assertion

    Build-time assertion

    Add link_time_panic and link_time_assert!. link_time_panic is defined as a const but #[inline(never)] function, so it panics when used in const context, but will not be inlined. This crate is not used when linking, so if optimiser cannot optimize away a callsite a link error is generated.

    opened by nbdd0121 23
  • Compiler Explorer wanted features & bugfixes

    Compiler Explorer wanted features & bugfixes

    Features that we would like to see:

    • [ ] Clippy support.

      • Issue:
    • [ ] Miri support.

      • Issue:
    • [x] Code formatting support.

      • Issue:
      • PR:
    • [x] MIR output support.

      • Issue:
      • PR:
    • [x] HIR output support.

      • Issue:
      • PR:
    • [x] Macro expansion support.

      • Issue:
      • PR:
    • [x] mrustc compiler support.

      • Issue:
      • PR:
    • [x] Rust GCC (i.e. gccrs) support.

      • Issue:
      • PR:
      • PR:
    • [x] rustc_codegen_gcc backend support.

      • Issue:
      • PR:
      • PR:
    • [ ] Allow using unstable features in all compilers.

      • Issue:
      • Issue:
    • [ ] Default to the latest available Rust edition.

      • Issue:
      • Issue:
    • [ ] rustfmt support (i.e. as a compiler or perhaps versioned CE tool).

      • Issue:
    • [ ] bindgen support.

      • Issue:

    Bugs that we would like to see fixed:

    • ...
    • misc prio: meta 
    opened by ojeda 0
  • rust: upgrade to Rust 1.66.0

    rust: upgrade to Rust 1.66.0

    Required changes:

    • Some new Clippy warnings (introduced since 1.62.0), as well as a few others that weren't triggered before, to be cleaned up or disabled as needed later on:

      • borrow_deref_ref (new in 1.63.0).
      • explicit_auto_deref (new in 1.64.0).
      • bool_to_int_with_if (new in 1.65.0).
      • needless_borrow.
      • type_complexity.
      • unnecessary_cast (this one I allowed it only on CONFIG_ARM).
    • rustdoc lint broken_intra_doc_links triggering on links pointing to macro_export macro_rules defined in the same module (i.e. appearing in the crate root).

      • However, even if the warning appears, the link still gets generated like in previous versions, thus it is a bit confusing -- I opened, and it appears that the link still being generated was a compatibility measure, thus we will need to adapt to the new behavior.
      • Thus I went ahead and cleaned those up in the second commit.
    • #[const_trait] in RawDeviceId (1.66.0 due to

    • -Aunused-imports for core, fixed upstream in, and prevented for the future for that cfg in

    I will have to reword the first commit for upstreaming and possibly split it somehow to make it easier to read. For the moment, this should unblock those waiting for 1.66.0 and start testing it on our side.

    As a triple-check, I redid the functions by hand and spotted a doc sentence missed from an old upgrade. Nothing important, but I included it here as the third commit too.

    Signed-off-by: Miguel Ojeda [email protected]

    opened by ojeda 11
  • objtool warnings in KernelCI report

    objtool warnings in KernelCI report

    Apologies if this has been reported already! KernelCI recently started testing with Rust and it seems that there are quite a few objtool warnings for Rust code:[email protected]/

    • kbuild 
    opened by nathanchance 1
  • Linked List `CursorMut` unsoundness

    Linked List `CursorMut` unsoundness

    The CursorMut API in rust/kernel/ is unsound, because we can store an Arc<T> in the list and then access the T mutably via CursorMut::current(). We should remove the ability to mutate G::EntryType directly from the list. The mutation is currently used (with Arc!) in driver/android/

    We should probably make it such that mutation is only possible if the G::Wrapped type permits this. Because if the list stores Boxes then mutation should be fine.

    opened by y86-dev 3
  • Get bindgen libclang version correctly

    Get bindgen libclang version correctly

    Currently the way to retreive the bindgen libclang version is susceptible to versions within the path. This commit fixes that by using tail rather than head on the version results. Fixes #942 .

    opened by jordanisaacs 0
Rust for Linux
Organization for adding support for the Rust language to the Linux kernel.
Rust for Linux
Linux kernel modules written in Rust

Linux kernel modules written in Rust A collection of in-progress experimental Linux kernel modules written for the Rust for Linux project To run the o

Milan 10 Nov 13, 2022
Linux ABI-compatible kernel written in Rust

Linux ABI-compatible kernel written in Rust ??️ Screenshot (v0.1.0-alpha.1) ?? Build dependencies To compile GalaxyOS kernel and create basic OS ISO i

Krzysztof Stefańczyk 3 Dec 5, 2022
Library for loading Linux kernel modules.

liblmod - Library for loading Linux kernel modules Features: modprobe rmmod Example code: extern crate liblmod; fn main() -> std::io::Result<()> {

Zapomnij 2 Aug 2, 2022
A comparison of operating systems written in Rust

Rust OS comparison A comparison of operating systems written in Rust. There are several open source operating systems written in Rust. Most of them ar

Markus Kohlhase 492 Jan 8, 2023
An OS kernel written in rust. Non POSIX

"Tifflin" Experimental Kernel (and eventually Operating System) This is an experiment in writing an OS Kernel in rust ( Mostly t

John Hodge (Mutabah) 618 Jan 8, 2023
A hobby operating system, in Rust

intermezzOS: kernel intermezzOS is a hobby operating system. This repository is for its kernel. See the website for more. License This project is dual

intermezzOS 1.3k Jan 1, 2023
A Rust version of the Weenix OS

Reenix This is the start of a unix like operating system written in Rust. It is based on the Weenix Operating system written for Brown's CS167/9. At t

Alex Light 311 Dec 22, 2022
A tiny 32 bit kernel written in Rust

rustboot A tiny 32 bit kernel written in Rust. I was inspired to download Rust and try to do this after seeing - a stub that lets Rust program

Charlie Somerville 1.5k Dec 30, 2022
A language-based OS to run Rust on bare metal

RustOS A simple, language-based OS. Current features: Simple VGA for seeing output Some Rust libraries (core, alloc, collections) already in Working (

null 402 Dec 8, 2022
A language-based OS to run Rust on bare metal

RustOS A simple, language-based OS. Current features: Simple VGA for seeing output Some Rust libraries (core, alloc, collections) already in Working (

null 79 Dec 3, 2022
Experimental kernel for embedded devices written in Rust

bkernel is an experimental kernel for embedded devices written in Rust. I'm mostly trying out Rust now to see how it applies to kernel development. Pr

Alexey Shmalko 84 Dec 13, 2022
Open Source Rust kernel; Runs WASM and WASI as lightweight containers.

?? etheryal Kernel etheryal kernel is an Open Source capability-based Kernel written in the Rust programming language. The kernel allows implementing

null 32 Dec 4, 2022
Basic Rust kernel using Limine

Rust Limine Barebones This is a small kernel that boots using Limine. Build First of all, download Rust ! (I guess you already did it if you are here

Quentincestino 16 Dec 23, 2022
Fast dense evaluation of Green's function kernels in Rust

Fast evaluation of Greens functions in Rust This library allows the fast evaluation of Greens functions and potential sums for Laplace, Helmholtz, and

null 8 Nov 10, 2022
Minimal x86_64 OS kernel written in Rust

rkernel A minimal x86_64 Rust OS kernel. Multiboot2 VGA driver PIC PIT PS/2 Keyboard driver PS/2 Mouse driver TSC RTC Allocator ATA PIO (In progress..

Divy Srivastava 36 Apr 26, 2022
Operating system written in Rust for NumWorks calculator (model n0110)

RustWorks An OS (eventually) for the Numworks calculator (model n0110). Setup First install Rust by following these instuctions then: rustup target ad

null 30 Nov 10, 2022
SteinsOS is an operating system written in Rust

SteinsOS is an operating system featuring non-preemptive kernel targeting on single-core armv8 architecture.

Sheng 84 Dec 15, 2022
🍒 Small, simple, and fast kernel written in Rust. 🌸

?? Small, simple, and fast kernel written in Rust. ??

Cherry Developers 5 May 20, 2022
Learn to write an embedded OS in Rust

Operating System development tutorials in Rust on the Raspberry Pi

Rust Embedded 9.9k Jan 6, 2023