Tag: rust

  • Setting up Rust Environment for Linux Kernel Development

    Setting up Rust Environment for Linux Kernel Development

    Hey 👋

    It’s me again, still writing about Rust before actually going beyond the hello world part of the documentation, but I will let you know why.

    I am currently going through Chris Simmonds’ terrific book “Mastering Embedded Linux Programming, and while reading through the chapter covering the kernel configuration and compilation , I stumbled upon a nice youtube video from the linux foundation covering the same topic but using the Rust fork as the codebase.

    So, since I have an interest in Rust too, I said why not combining both ? Which was actually a good choice because both the video and the book covered each other gaps.

    While watching the video, I took a note of most of the commands in a github gist which I think should be pretty useful for my future self and and other people too. I thought also of taking the notes here because why not ..

    #
    # Instructions for the video https://www.youtube.com/watch?v=tPs1uRqOnlk excluding setting up the env ( e.g. installing Rust and llvm)
    #
    
    
    #
    # Kernel
    #
    
    $ git clone --depth=1 https://github.com/Rust-for-Linux/linux.git
    $ cd linux/
    $ make allnoconfig qemu-busybox-min.config
    $ make allnoconfig qemu-busybox-min.config rust.config
    $ make LLVM=1 rustavailable # should say Rust is available if you have all dependencies
    $ make LLVM=1 -j4
    
    #
    # Busybox
    #
    $ cd busybox
    $ git clone --depth=1 https://github.com/mirror/busybox.git
    $ make defconfig
    $ make menuconfig # then from settings select use static libraries 
    # make -j4
    $ cd ./_install/
    $ find . | cpio -H newc -o | gzip > ../ramdisk.img
    
    
    #
    # running qemu
    #
    $ cd linux/
    $ qemu-system-x86_64 -nographic -kernel vmlinux # should panic
    $ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img # should start
    
    
    #
    # adding proc filesystem to enable ps
    #
    $ cd busybox/_install/
    $ vim etc/init.d/rcS 
    # then add this content inside 
    # mkdir -p /proc 
    # mount -t proc none /proc
    $ find . | cpio -H newc -o | gzip > ../ramdisk.img
    $ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img # ps should work
    
    
    #
    # enabling a rust sample kernel module
    #
    $ make LLVM=1 menuconfig
    # then select the samples from kernel hacking --> samples --> rust samples
    
    
    #
    # enable networking
    #
    $ cd busybox/_install/
    $ vim etc/init.d/rcS 
    # then add this content inside to enable loopback interface 
    # ifconfig lo up
    # udhcpc -i eth0
    $ mkdir -p usr/share/udhcpc
    $ cp ../examples/udhcp/simple.script usr/share/udhcpc/default.script
    $ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img -nic user,model=rtl8139
    $ qemu-system-x86_64 -nographic -kernel vmlinux -initrd ../busybox/ramdisk.img -nic user,model=rtl8139,hostfwd=tcp::5556-:8080

  • How to downgrade Rust compiler to a specific version ?

    How to downgrade Rust compiler to a specific version ?

    Hi !

    There are a couple of pages/stackoverflow questions which mention how to install a specific Rust version which is pretty straightforward using rustup install command

    abdelah@abdelah-VirtualBox:~/workspace/linux$ rustup install 1.62.0
    info: syncing channel updates for '1.62.0-x86_64-unknown-linux-gnu'
    info: latest update on 2022-06-30, rust version 1.62.0 (a8314ef7d 2022-06-27)
    ....

    However, there is one tiny step also needed to set the default compiler to the newly installed version, for instance if you run the command above and invoke rustc --version you would still get the previously installed default version.

    abdelah@abdelah-VirtualBox:~/workspace/linux$ rustc --version
    rustc 1.66.1 (90743e729 2023-01-10)

    I am pretty new to Rust ( haven’t read the hello world example fully yet to be honest ), but I already like how the commands come intuitively to mind opposed to other popular tools such as git (always scratched my head trying variants of git checkout and git reset to clean a working directory )

    So, back to topic. Now we need to set the default compiler version to 1.62.0, for that we just need to execute rustup deault my-preferred-version but first we can get the list of installed versions like this

    abdelah@abdelah-VirtualBox:~/workspace/linux$ rustup toolchain list
    stable-x86_64-unknown-linux-gnu (default)
    1.62.0-x86_64-unknown-linux-gnu

    Then set the default version

    abdelah@abdelah-VirtualBox:~/workspace/linux$ rustup default 1.62.0-x86_64-unknown-linux-gnu
    info: using existing install for '1.62.0-x86_64-unknown-linux-gnu'
    info: default toolchain set to '1.62.0-x86_64-unknown-linux-gnu'
    
      1.62.0-x86_64-unknown-linux-gnu unchanged - rustc 1.62.0 (a8314ef7d 2022-06-27)

    Now, let’s do a smoke test on the compiler version

    abdelah@abdelah-VirtualBox:~/workspace/linux$ rustc --version
    rustc 1.62.0 (a8314ef7d 2022-06-27)

    Okay, this works now !

    You might be wondering what would be a usecase for using a specific older compiler version, well my answer to this would be this snippet from Rust documentation in the Linux kernel

    rustc
    *****
    
    A particular version of the Rust compiler is required. Newer versions may or may not work because, for the moment, the kernel depends on some unstable
    Rust features.
    

    Starting Linux kernel release v6.1, Rust support was introduced and this grabbed my attention as Rust is claimed to be safer and more secure than C/C++. Safety and Security are two words that one would hear everyday if you work at any company producing or contributing to the production of passenger cars.

    I wanted to have a look and play around a bit and then to test if my environment is ready to build Rust in the kernel

    abdelah@abdelah-VirtualBox:~/workspace/linux$ make LLVM=1 rustavailable
    ***
    *** Rust compiler 'rustc' is too new. This may or may not work.
    ***   Your version:     1.66.1
    ***   Expected version: 1.62.0
    ***
    Rust is available!

    And because I wouldn’t take may or may not work as an answer, I downgraded my compiler version.