Skip to content

ye-junzhe/rust-for-linux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

rust-for-linux

Env

  • Debian GNU/Linux 12 (bookworm) aarch64 in Parallel Desktop Virtual Machine
  • QEMU qemu-system-aarch64 version 7.2.5
  • rust-for-linux arm64 6.6.0-rc4
  • BusyBox v1.35.0 (Debian 1:1.35.0-4+b3) multi-call binary.
图片

编译内核(Exercise 1)

使用fujita的linux分支

git clone https://github.com/fujita/linux -b rust-dev --depth=1

按照指导安装依赖,

  • 但是注意cargo install bindgen,没有后面的cli,因为fujiya使用的是比较早的linux kernel版本
make ARCH=arm64 LLVM=1 O=build defconfig

make ARCH=arm64 LLVM=1 O=build menuconfig
#set the following config to yes
General setup
        ---> [*] Rust support

make ARCH=arm64 LLVM=1 -j8
image

编译Rust模块helloworld(Exercise 2)

安装QEMU

sudo apt install qemu-user qemu-system-arm # 安装qemu 7.x.x

qemu 8.x.x以上运行DQIB kernel会报错 => "...net user not compiled into kernel..."

利用busybox制作文件系统

make menuconfig ARCH=arm64

Settings  --->
        --- Build Options
        [*] Build static binary (no shared libs)

make -j20
make install

cat qemu-init.sh

#!/bin/sh
busybox echo "[INFO] Init from a minimal initrd!"
busybox echo "============================"
busybox poweroff -f

cat qemu-initramfs.desc

dir     /bin                                                                            0755 0 0
file    /bin/busybox            busybox                                                 0755 0 0
slink   /bin/sh                 /bin/busybox                                            0755 0 0
file    /init                   qemu-init.sh                                            0755 0 0
# 制作img
# 每次修改完qemu-init.sh & qemu-initramfs.desc要记得重新制作
../linux-fujita/build/usr/gen_init_cpio qemu-initramfs.desc > qemu-initramfs.img      

按照指导添加代码

Kernel hacking
  ---> Sample Kernel code
      ---> Rust samples
              ---> <M>Print Helloworld in Rust (NEW) # 其它也要勾选

cat qemu-init.sh

#!/bin/sh
busybox echo "[INFO] Init from a minimal initrd!"
busybox echo "============================"
busybox insmod rust_helloworld.ko

busybox poweroff -f

cat qemu-initramfs.desc

dir     /bin                                                                            0755 0 0
file    /bin/busybox            busybox                                                 0755 0 0
slink   /bin/sh                 /bin/busybox                                            0755 0 0
file    /init                   qemu-init.sh                                            0755 0 0
file    /rust_helloworld.ko    ../linux-fujita/build/samples/rust/rust_helloworld.ko    0755 0 0
  • QEMU启动参数
#!/bin/sh

qemu-system-aarch64                                                                \
        -machine 'virt'                                                            \
        -cpu 'cortex-a57'                                                          \
        -m 1G                                                                      \
        -netdev user,id=net,hostfwd=tcp::2222-:22                                  \
        -kernel /home/junzhe/Dev/Software/linux-fujita/build/arch/arm64/boot/Image \
        -initrd ../busybox/qemu-initramfs.img                                      \
        -nographic                                                                 \
        -append "root=LABEL=rootfs console=ttyAMA0"                                \
图片

e1000网卡驱动(Exercise 3)

Preferences

图片

先学习编译fujita e1000,了解busybox加载模块的流程

编译rust_e1000.ko

git clone https://github.com/fujita/rust-e1000
make KDIR=/home/junzhe/Dev/Software/linux-fujita/build/ LLVM=1      

cat qemu-initramfs.desc

dir     /bin                                                                            0755 0 0
file    /bin/busybox            busybox                                                 0755 0 0
slink   /bin/sh                 /bin/busybox                                            0755 0 0
file    /init                   qemu-init.sh                                            0755 0 0
file    /rust_helloworld.ko    ../linux-fujita/build/samples/rust/rust_helloworld.ko    0755 0 0
file    /rust_e1000.ko         ../rust-e1000/rust_e1000.ko                              0755 0 0 # 新增

Linux kernel网络参数设置

cat qemu-init.sh

#!/bin/sh

busybox echo "[INFO] Init from a minimal initrd!"
busybox echo "============================"
busybox echo "[INFO] Modules Loaded"
busybox insmod rust_helloworld.ko
busybox insmod rust_e1000.ko
busybox echo "============================"
busybox echo "[INFO] Network configured"
busybox echo "IP set to 192.168.100.224"
busybox ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
busybox ifconfig eth0 192.168.100.224 netmask 255.255.255.0 broadcast 192.168.100.255 up
busybox ip addr
busybox echo "============================"

export 'PS1=(kernel) >'
busybox sh

busybox poweroff -f

QEMU启动参数

qemu-system-aarch64 \
        -machine 'virt' \
        -cpu 'cortex-a57' \
        -m 1G \
        -kernel /home/junzhe/Dev/Software/linux-fujita/build/arch/arm64/boot/Image \
        -initrd ../busybox/qemu-initramfs.img \
        -netdev tap,ifname=tap0,id=tap0,script=no,downscript=no -device e1000,netdev=tap0 \
        -nographic                                                                 \
        -append "root=LABEL=rootfs console=ttyAMA0" \
  • ping通
图片

作业3 e1000-driver 添加代码

image

e1000网卡驱动(Exercise 4)

停用内置e1000网卡驱动

在menuconfig中搜索e1000,并停用

作业4添加代码

编译后对e1000模块进行加载

image image image
busybox insmod e1000_for_linux.ko
busybox ifconfig eth0 up
busybox ifconfig eth0 10.0.2.20
busybox ip route add default via 10.0.2.2 dev eth0
  • ping通外网
image

实习项目

在树莓派模拟器上,适配Rust for Linux内核,并实现Rust Uart串口驱动

适配Rust for Linux内核

对image做处理来适配QEMU启动

fdisk显示img信息
图片
计算第一个偏移量,mount img1
图片
  • 由于较新版的Raspberry Pi OS不再支持默认用户名密码登陆,所以需要在镜像根目录添加userconf.txt,手动添加用户,利用openssl生成密码密文

echo 'raspberry' | openssl passwd -6 -stdin

图片
计算第二个偏移量,mount img2
图片
  • 复制出内核vmlinuz(后面会替换成rust-for-linux内核)和文件系统initrd
图片
  • 适配rust-for-linux 内核
# 目前最新的
git clone https://github.com/Rust-for-Linux/linux -b rust-dev --depth=1

make ARCH=arm64 LLVM=1 O=build defconfig

make ARCH=arm64 LLVM=1 O=build menuconfig
#set the following config to yes
General setup
        ---> [*] Rust support

make ARCH=arm64 LLVM=1 -j8
  • QEMU 运行
qemu-system-aarch64 \
    -cpu cortex-a57 \
    -M virt \
    -m 1G \
    -kernel ../linux/build/arch/arm64/boot/Image \
    -initrd ../raspberry-pi/initrd.img \
    -drive file=../raspberry-pi/2023-10-10-raspios-bookworm-arm64-lite.img,if=none,id=drive0,cache=writeback -device virtio-blk,drive=drive0,bootindex=0 \
    -append 'root=/dev/vda2 noresume rw' \
    -nographic \
    -no-reboot \
可以看到运行在6.7.0的Linux内核上
图片

实现Rust Uart串口驱动

Linux 中的 UART 驱动通常作为内核的一部分提供,而不是独立于内核的用户空间程序。UART 驱动的源代码位于 Linux 内核源代码的 drivers/tty/serial/ 目录下。在这个目录下,你可以找到多个 UART 驱动,每个驱动通常对应于支持的特定硬件。

TODO:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published