如何为 RISC-V 编译 Linux 内核 4.20

how to compile Linux Kernel 4.20 for RISC-V

我正在尝试为 RISC-V 编译 Linux 内核 4.20.14

可以在此处找到的 RISC-V 端口未更新为最新版本,RISC-V 基金会表示它现在是官方 Linux 端口的一部分。

当我想使用此命令为 RISC 创建默认配置文件时:

make riscv-defconfig

我收到以下错误:

 HOSTCC  scripts/basic/fixdep
make[1]: *** No rule to make target 'riscv-defconfig'. Stop.
Makefile:528: recipe for target 'riscv-defconfig' failed
make: *** [riscv-defconfig] Error 2

正在检查

build_linux/linux-4.20.14/arch/riscv/configs

我看到有defconfig,但是没有命名为riscv-defconfig

如果我尝试

make defconfig

*** Default configuration is based on 'x86_64_defconfig'
#
# configuration written to .config
#

当我 运行

make menuconfig

我可以看到 arch 设置为 x86 而不是 riscv

如何使用 GCC 在 ubuntu 18.04 上交叉编译 riscv:

gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 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.

这可能没有奏效,因为您使用了目标 riscv-defconfig 而不是 defconfig - 请参阅 Chris Stratton 在他的评论中的提示。

ls -l arch/riscv/configs/
total 4
-rw-rw-r-- 1 user user 1641 Mar  5 11:59 defconfig

此后的命令集能够使用 Bootlin“bleeding-edge”工具链为 riscv 构建 4.20.14 内核:

wget https://toolchains.bootlin.com/downloads/releases/toolchains/riscv64/tarballs/riscv64--glibc--bleeding-edge-2018.11-1.tar.bz2
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.20.14.tar.xz

tar jxf riscv64--glibc--bleeding-edge-2018.11-1.tar.bz2
tar Jxf linux-4.20.14.tar.xz

export CROSS_COMPILE=$(pwd)/riscv64--glibc--bleeding-edge-2018.11-1/bin/riscv64-buildroot-linux-gnu-
cd linux-4.20.14
make ARCH=riscv mrproper defconfig

make  ARCH=riscv defconfig
  CLEAN   .
  CLEAN   arch/riscv/kernel/vdso
  CLEAN   arch/riscv/kernel
  CLEAN   usr
  CLEAN   .tmp_versions
  CLEAN   scripts/basic
  CLEAN   scripts/dtc
  CLEAN   scripts/kconfig
  CLEAN   scripts/mod
  CLEAN   scripts
  CLEAN   include/config include/generated arch/riscv/include/generated
  CLEAN   .config .config.old
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  YACC    scripts/kconfig/zconf.tab.c
  LEX     scripts/kconfig/zconf.lex.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
*** Default configuration is based on 'defconfig'


make ARCH=riscv all 
 UPD     include/config/kernel.release
  WRAP    arch/riscv/include/generated/uapi/asm/setup.h
  WRAP    arch/riscv/include/generated/uapi/asm/unistd.h
  WRAP    arch/riscv/include/generated/uapi/asm/bpf_perf_event.h
  WRAP    arch/riscv/include/generated/uapi/asm/errno.h
  WRAP    arch/riscv/include/generated/uapi/asm/fcntl.h
  WRAP    arch/riscv/include/generated/uapi/asm/ioctl.h
  WRAP    arch/riscv/include/generated/uapi/asm/ioctls.h
  WRAP    arch/riscv/include/generated/uapi/asm/ipcbuf.h
  WRAP    arch/riscv/include/generated/uapi/asm/mman.h
  WRAP    arch/riscv/include/generated/uapi/asm/msgbuf.h
  WRAP    arch/riscv/include/generated/uapi/asm/param.h
  WRAP    arch/riscv/include/generated/uapi/asm/poll.h
  WRAP    arch/riscv/include/generated/uapi/asm/posix_types.h
  WRAP    arch/riscv/include/generated/uapi/asm/resource.h
  WRAP    arch/riscv/include/generated/uapi/asm/sembuf.h
  WRAP    arch/riscv/include/generated/uapi/asm/shmbuf.h
  WRAP    arch/riscv/include/generated/uapi/asm/signal.h
  WRAP    arch/riscv/include/generated/uapi/asm/socket.h
  WRAP    arch/riscv/include/generated/uapi/asm/sockios.h
  WRAP    arch/riscv/include/generated/uapi/asm/stat.h
  ...
  CC      lib/win_minmax.o
  CC      lib/xarray.o
  GEN     lib/crc32table.h
  CC      lib/crc32.o
  AR      net/ipv6/built-in.a
  AR      net/built-in.a
  AR      lib/lib.a
  EXPORTS lib/lib-ksyms.o
  AR      lib/built-in.a
  GEN     .version
  CHK     include/generated/compile.h
  AR      built-in.a
  LD      vmlinux.o
  MODPOST vmlinux.o
  KSYM    .tmp_kallsyms1.o
  KSYM    .tmp_kallsyms2.o
  LD      vmlinux
  SYSMAP  System.map
  OBJCOPY arch/riscv/boot/Image
  Building modules, stage 2.
  MODPOST 10 modules
  GZIP    arch/riscv/boot/Image.gz
  CC      crypto/crypto_engine.mod.o
  CC      crypto/drbg.mod.o
  CC      crypto/echainiv.mod.o
  CC      crypto/hmac.mod.o
  CC      crypto/jitterentropy_rng.mod.o
  CC      crypto/sha256_generic.mod.o
  CC      drivers/char/hw_random/rng-core.mod.o
  CC      drivers/crypto/virtio/virtio_crypto.mod.o
  CC      drivers/video/backlight/lcd.mod.o
  CC      fs/nfs/flexfilelayout/nfs_layout_flexfiles.mod.o
  LD [M]  crypto/crypto_engine.ko
  LD [M]  crypto/drbg.ko
  LD [M]  crypto/hmac.ko
  LD [M]  crypto/echainiv.ko
  LD [M]  crypto/jitterentropy_rng.ko
  LD [M]  drivers/video/backlight/lcd.ko
  LD [M]  crypto/sha256_generic.ko
  LD [M]  drivers/char/hw_random/rng-core.ko
  LD [M]  fs/nfs/flexfilelayout/nfs_layout_flexfiles.ko
  LD [M]  drivers/crypto/virtio/virtio_crypto.ko

ls -l vmlinux
-rwxrwxr-x 1 user user 11134176 Mar  8 08:57 vmlinux