Skip to the content.

课程实例-操作系统(研讨课)

目录


环境搭建

准备

RISC-V 64 交叉编译环境

可执行文件已编译好,只需解压、放到系统目录中、修改环境变量即可

  1. 解压文件
    $ tar -xvf riscv64-linux.tar.gz
    ...
    
  2. 移动到系统目录中,并设置权限
    $ sudo mv opt/riscv64-linux /opt
    $ sudo chown root:root /opt/riscv64-linux -R
    
  3. 添加/opt/riscv64-linux/bin到环境变量
    • 临时修改(重启终端失效)
      $ export PATH=/opt/riscv64-linux/bin:$PATH
      
    • 长期修改,需要将上面的命令添加到 shell 启动脚本中。以 Ubuntu 默认的 bash 为例,添加到~/.bashrc
      $ nano ~/.bashrc        // 可使用任意编辑器,这里用 nano
      // 在文件末尾另起一行,写入“export PATH=/opt/riscv64-linux/ bin:$PATH”
      $ source ~/.bashrc      // 保存后使改动生效,亦可重启终端
      

      若不确定自己使用的 shell,可以使用echo $SHELL查看

  4. 验证配置
    $ riscv64-unknown-linux-gnu-gcc --version
    riscv64-unknown-linux-gnu-gcc (GCC) 9.2.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A    PARTICULAR PURPOSE.
    

QEMU

要编译,但老师已写好编译脚本,只需解压并运行即可

  1. 解压文件
    $ tar -xvf OSLab-RISC-V.tar.gz
    ...
    
  2. 安装编译所需环境
    $ sudo apt install pkg-config libglib2.0-dev libpixman-1-dev
    ...
    
  3. 运行编译脚本。此步需要时间较长,且有大量 Warning(似乎是不同 Python 版本间==is的差异引起,助教老师说无需在意,没有报 Error 就行),请耐心
    $ cd OSLab-RISC-V
    $ ./rebuild-qemu.sh
    ...
    
  4. 验证编译
    $ ./qemu/riscv64-softmmu/qemu-system-riscv64 --version
    QEMU emulator version 4.1.1.1 DASICS
    Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
    

minicom

要求为 2.8 及以上版本

Project0 测试

克隆老师给出的仓库,进入 Project0 文件夹

检查 MakeFile 中工具路径是否与实际一致

$ cat MakeFile
...
# -----------------------------------------------------------------------
# Host Linux Variables
# -----------------------------------------------------------------------

SHELL           = /bin/sh
DIR_OSLAB       = $(HOME)/OSLab-RISC-V
DIR_QEMU        = $(DIR_OSLAB)/qemu

# -----------------------------------------------------------------------
# Build and Debug Tools
# -----------------------------------------------------------------------

CROSS_PREFIX    = riscv64-unknown-linux-gnu-
CC                              = $(CROSS_PREFIX)gcc
GDB                             = $(CROSS_PREFIX)gdb
QEMU                    = $(DIR_QEMU)/riscv64-softmmu/qemu-system-riscv64
...

上面为老师给出的配置,即之前编译的 QEMU 位于路径~/OSLab-RISC-V/qemu/riscv64-softmmu/qemu-system-riscv64

只要最初在用户主目录~下执行解压命令tar -xvf OSLab-RISC-V.tar.gz,路径应该是正确的

检查后,编译

$ make
riscv64-unknown-linux-gnu-gcc -O0 -fno-builtin -nostdlib -nostdinc -Wall -mcmodel=medany -ggdb3 -Wl,--defsym=TEXT_START=0x50000000 -T riscv.lds -o  test test.S -e main

调试运行

$ make debug
/home/user/OSLab-RISC-V/qemu/riscv64-softmmu/qemu-system-riscv64 -nographic -machine virt -m 256M -kernel  test -bios none -s -S

另起一终端使用 gdb

$ make gdb
riscv64-unknown-linux-gnu-gdb  test -ex "target remote:1234"
...
Reading symbols from test...
Remote debugging using :1234
0x0000000000010000 in ?? ()
(gdb)

如上,未报错,说明编译和运行正常,测试成功

踩坑

rebuild-uboot 报错:multiple definition of 'yylloc'

错误信息类似:

  ...
  HOSTLD  scripts/dtc/dtc
/usr/bin/ld: scripts/dtc/dtc-parser.tab.o:(.bss+0x10): multiple definition of `yylloc'; scripts/dtc/dtc-lexer.lex.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [scripts/Makefile.host:106: scripts/dtc/dtc] Error 1
make[1]: *** [scripts/Makefile.build:432: scripts/dtc] Error 2
make: *** [Makefile:528: scripts] Error 2

与成功 rebuild 的同学比较发现,gcc 版本不同

// 失败编译的gcc版本
$ gcc --version
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// 成功编译的gcc版本
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

怀疑是 gcc11 与 gcc9 的差异,检查系统中是否安装了 gcc9:

$ gcc-9 --version
gcc-9 (Ubuntu 9.5.0-1ubuntu1~22.04) 9.5.0
...

若未安装:

$ gcc-9 --version
bash: command not found: gcc-9

则使用 apt 进行安装

$ sudo apt install gcc-9 -y

随后修改 Makefile

$ nano ~/OSLab-RISC-V/u-boot/Makefile
// 大约第 255 行
HOSTCC       = cc
// 修改为
HOSTCC       = gcc-9

并重新执行编译脚本./rebuild-uboot.sh