重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
香山處理器產生的前因后果就不介紹了,有興趣的讀者可以查看香山的官方文檔,其中已經有詳細的介紹。另外本文也不是從零開始搭建香山的開發環境,官方文檔‘前端開發環境’已經體貼的給我們做好了一鍵實現環境搭建的repo。
先描述下我的工作環境:
vmware:32G物理內存,50G disk,4核CPU
OS:ubuntu 18.04
環境不同的僅做參考。第一次寫博,有錯誤和不成熟的地方還請各位大牛批評指正,小弟拜謝了。
先按照官方文檔中下載xs-env
>>git clone https://github.com/OpenXiangShan/xs-env
>>cd xs-env
>>sudo -s ./setup-tools.sh # use apt to install dependencies, you may modify it to use different pkg manager
setup-tools.sh中完成了編譯香山核所需的工具安裝,其中會下載并編譯verilator,一個開源仿真工具,也是香山核官方支持的開源仿真工具,當前香山核也支持vcs仿真,但貌似支持力度不是特別好;此腳本的另一個主要功能就是下載mill,mill是scala的構建工具,類似于sbt,貌似很多人都從sbt遷移到了mill,在github下載速度實現讓人無法忍受,經過無數次失敗后終于下載下來,為了讓小伙伴們不在經歷這個過程,已將mill-0.9.8放到了gitee上,下載后放到~/.cache/mill/download/0.9.8即可使用,注意/usr/bin/mill其實是個腳本,用于檢查~/.cache/mill/download/0.9.8文件是否存在,不存在時會從https://github.com/lihaoyi/mill/releases/download/0.9.8/0.9.8-assembly下載mill可執行文件,然后調用~/.cache/mill/download/0.9.8運行chisel代碼構建。
完成上面的工具下載安裝后,開始下載香山核編譯運行所需的代碼倉
>>source setup.sh
在這步,會下載XiangShan,NEMU,nexus-am和nutshell。NEMU是一個款類似spike的risc-v指令模擬器,nexus-am是香山所使用的應用開發框架,nutshell是另一款開源risc-v核,可能是前期做對比或開發使用。香山使用了difftest測試框架,會比較每條指令在程序運行的過程中的正確與否,NEMU就是difftest的golden model。
下載完上面的代碼后,setup.sh中會自動編譯NEMU和nexus-am中的coremark,這里還需要說下NEMU有兩種工作模式:作為單獨可執行程序的工作模式和golden model模式。兩種模式的編譯目標不同,單獨可執行程序編譯時指定riscv64-xs_defconfig,生成的文件是buidl/riscv64-nemu-interpreter;golden model模式編譯時指定riscv64-xs-ref_defconfig,生成文件是riscv64-nemu-interpreter-so。
編譯NEMU時又報錯了
xs-env/NEMU/include/checkpoint/path_manager.h:24:10: fatal error: filesystem: No such file or directory
#include
NEMU中用到了C++17的特性,需要將gcc/g++升級到8.x版本以上(想升級到9.x也不行啊,ubuntu 18.04中沒有gcc9.x的版本),通過使用apt安裝gcc-8并配置update-alternatives:
>>sudo apt install gcc-8
>>sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8
>>sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 700 --slave /user/bin/g++ g++ /usr/bin/g++-7
>>sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-8 800 auto mode
* 1 /usr/bin/gcc-7 700 manual mode
2 /usr/bin/gcc-8 800 manual mode
Pressto keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/gcc-8 to provide /usr/bin/gcc (gcc) in auto mode
在編譯過程中還出現了一次cmake版本太低的錯誤,編譯需要cmake 3.14以上,但ubuntu apt中最高只能到3.10,cmake官網下載速度實在讓人淚目,還好在gitee上找到了3.19版本的鏡像倉:
cmake-release: cmake release repo
下載后解壓縮,然后將其中的bin目錄加到PATH中即可。
OK,現在代碼都已經準備好了,開始編譯香山核。
>>cd xs-env/XiangShan
>>make init
>>make emu CONFIG=MinimalConfig EMU_TRACE=1 -j4
make init會下載香山代碼倉所用到的子模塊,make emu的參數請參考官方文檔說明,-j4這里選擇4是因為我的vmware中只有4個虛擬CPU核。
編譯過程中出現錯誤。。。
Connection:[logTimestamp] type:[func] source location:[SimTop] sink location:[BusPerfMonitor_1]
Connection:[XSPERF_CLEAN] type:[func] source location:[SimTop] sink location:[BusPerfMonitor_1]
Connection:[XSPERF_DUMP] type:[func] source location:[SimTop] sink location:[BusPerfMonitor_1]
Connection:[DISPLAY_LOG_ENABLE] type:[func] source location:[SimTop] sink location:[BusPerfMonitor_1]
1 targets failed
XiangShan.test.runMain subprocess failed
Makefile:102: recipe for target 'build/SimTop.v' failed
沒啥提示就直接failed,郁悶了一陣,根據文檔中所講,到troubleshoot中查找了下,應該是內存不夠導致的,香山核編譯時在MinimalConfig配置下需要至少32G內存,標準配置最少需要64G內存,通過如下方法修改:
1. 修改XiangShan/build.sc中的參數
override def forkArgs = Seq("-Xmx64G", "-Xss256m")
改成
override def forkArgs = Seq("-Xmx24G", "-Xss256m")
我的虛擬機有32G內存,為了保險這里設置為24G內存,請大家根據自己的PC配置自行確定此參數。
2. 物理內存不能滿足需求而又不想在加錢買內存,使用swap來補充,這里我在vmware中新增了一塊100G的虛擬硬盤,專門作為swap,即便是標準配置的香山核也可以編譯了:)。關閉虛擬機新增scsi接口硬盤后再啟動,然后在終端中操作
>>sudo mkswap -f /dev/sdb
>>sudo swapon /dev/sdb
新增的硬盤在我的vmware中顯示的/dev/sdb。
重新執行make emu CONFIG=MinimalConfig EMU_TRACE=1 -j4,經過漫長的等待終于編譯完成了,迫不及待的跑個仿真:
>>./build/emu -i ready-to-run/coremark-2-iteration.bin
運行成功!
Emu compiled at Dec 4 2022, 19:33:27
Using simulated 32768B flash
[warning]no valid flash bin path, use preset flash instead
The image is ready-to-run/coremark-2-iteration.bin
Using simulated 8192MB RAM
--diff is not given, try to use $(NEMU_HOME)/build/riscv64-nemu-interpreter-so by default
NemuProxy using /home/fenghang/xs-env/NEMU/build/riscv64-nemu-interpreter-so
The first instruction of core 0 has commited. Difftest enabled.
[NEMU] Can not find flash image: (null)
[NEMU] Use built-in image instead
[src/device/io/mmio.c:39,add_mmio_map] Add mmio map 'flash' at [0x0000000010000000, 0x00000000100fffff]
Running CoreMark for 2 iterations
2K performance run parameters for coremark.
CoreMark Size : 666
Total time (ms) : 5210
Iterations : 2
Compiler version : GCC10.2.0
seedcrc : 0xe9f5
[0]crclist : 0xe714
[0]crcmatrix : 0x1fd7
[0]crcstate : 0x8e3a
[0]crcfinal : 0x72be
Finised in 5210 ms.
==================================================
CoreMark Iterations/Sec 383
Core 0: HIT GOOD TRAP at pc = 0x800026ba
total guest instructions = 626,872
instrCnt = 626,872, cycleCnt = 587,010, IPC = 1.067907
[PERF ][time= 587011] TOP.SimTop.l_soc.core_with_l2.core.frontend.icache.missUnit.prefEntries_0: PrefetchEntryReq2, 0
[PERF ][time= 587011] TOP.SimTop.l_soc.core_with_l2.core.frontend.icache.missUnit.prefEntries_1: PrefetchEntryReq2, 0
[PERF ][time= 587011] TOP.SimTop.l_soc.core_with_l2.core.exuBlocks.scheduler.fpBusyTable: busy_count, 0
[PERF ][time= 587011] TOP.SimTop.l_soc.core_with_l2.core.exuBlocks_1.scheduler.fpBusyTable: busy_count, 0
[PERF ][time= 587011] TOP.SimTop.l_soc.core_with_l2.core.exuBlocks.scheduler.intBusyTable: busy_count, 3289028
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧