<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Emulation on 0xc0de</title>
    <link>https://0xc0de.xyz/categories/emulation/</link>
    <description>Recent content in Emulation on 0xc0de</description>
    <image>
      <title>0xc0de</title>
      <url>https://0xc0de.xyz/%3Clink%20or%20path%20of%20image%20for%20opengraph,%20twitter-cards%3E</url>
      <link>https://0xc0de.xyz/%3Clink%20or%20path%20of%20image%20for%20opengraph,%20twitter-cards%3E</link>
    </image>
    <generator>Hugo</generator>
    <language>zh-tw</language>
    <lastBuildDate>Sun, 26 Jan 2020 00:00:00 +0800</lastBuildDate>
    <atom:link href="https://0xc0de.xyz/categories/emulation/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Cross-build RISC-V QEMU</title>
      <link>https://0xc0de.xyz/posts/cross-build-riscv-qemu/index.md/</link>
      <pubDate>Sun, 26 Jan 2020 00:00:00 +0800</pubDate>
      <guid>https://0xc0de.xyz/posts/cross-build-riscv-qemu/index.md/</guid>
      <description>&lt;h2 id=&#34;在-x86-64-上-cross-build-risc-v-qemu&#34;&gt;在 x86-64 上 cross-build RISC-V QEMU&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;OS 為 Ubuntu 22.04&lt;/em&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;dpkg 加入 RISC-V 架構：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;sudo dpkg --add-architecture riscv64&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;新增 RISC-V package sources：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;sudo vim /etc/apt/sources.list&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;新增以下 RISC-V sources：&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;
&lt;table style=&#34;border-spacing:0;padding:0;margin:0;border:0;&#34;&gt;&lt;tr&gt;&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-0-1&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-0-1&#34;&gt;1&lt;/a&gt;
&lt;/span&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-0-2&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-0-2&#34;&gt;2&lt;/a&gt;
&lt;/span&gt;&lt;span style=&#34;white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f&#34; id=&#34;hl-0-3&#34;&gt;&lt;a style=&#34;outline:none;text-decoration:none;color:inherit&#34; href=&#34;#hl-0-3&#34;&gt;3&lt;/a&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td style=&#34;vertical-align:top;padding:0;margin:0;border:0;;width:100%&#34;&gt;
&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;deb [arch=riscv64] http://ports.ubuntu.com jammy main restricted universe multiverse
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;deb [arch=riscv64] http://ports.ubuntu.com jammy-updates main restricted universe multiverse
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;deb [arch=riscv64] http://ports.ubuntu.com jammy-security main restricted universe multiverse
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;並限制以下的 sources 為 amd64 (加上：&lt;code&gt;[arch=amd64]&lt;/code&gt;)：&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h2 id="在-x86-64-上-cross-build-risc-v-qemu">在 x86-64 上 cross-build RISC-V QEMU</h2>
<p><em>OS 為 Ubuntu 22.04</em></p>
<ol>
<li>
<p>dpkg 加入 RISC-V 架構：</p>
<ul>
<li><code>sudo dpkg --add-architecture riscv64</code></li>
</ul>
</li>
<li>
<p>新增 RISC-V package sources：</p>
<ul>
<li>
<p><code>sudo vim /etc/apt/sources.list</code></p>
</li>
<li>
<p>新增以下 RISC-V sources：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-2">2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-3">3</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>deb [arch=riscv64] http://ports.ubuntu.com jammy main restricted universe multiverse
</span></span><span style="display:flex;"><span>deb [arch=riscv64] http://ports.ubuntu.com jammy-updates main restricted universe multiverse
</span></span><span style="display:flex;"><span>deb [arch=riscv64] http://ports.ubuntu.com jammy-security main restricted universe multiverse
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>並限制以下的 sources 為 amd64 (加上：<code>[arch=amd64]</code>)：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-2">2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-3">3</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-security main restricted
</span></span><span style="display:flex;"><span>deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-security universe
</span></span><span style="display:flex;"><span>deb [arch=amd64] http://security.ubuntu.com/ubuntu jammy-security multiverse
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ul>
</li>
<li>
<p>更新套件清單：</p>
<ul>
<li><code>sudo apt update</code></li>
</ul>
</li>
<li>
<p>安裝開發套件：</p>
<ul>
<li><code>sudo apt install crossbuild-essential-riscv64</code></li>
<li><code>sudo apt install zlib1g-dev:riscv64 libglib2.0-dev:riscv64 libpixman-1-dev:riscv64 libpcre2-dev:riscv64 libffi-dev:riscv64 libselinux1-dev:riscv64</code></li>
</ul>
</li>
<li>
<p>由於 Ubuntu 沒提供 <code>libmount.a</code>，需手動編譯 <code>util-linux</code> 並處理 <code>crc32c</code> 符號衝突：</p>
<ol>
<li>下載 <code>util-linux</code> source codes：
<ul>
<li><code>git clone git@github.com:util-linux/util-linux.git</code></li>
</ul>
</li>
<li><code>cd util-linux</code></li>
<li><code>./autogen.sh</code></li>
<li><code>./configure --host=riscv64-linux-gnu --enable-static --disable-shared --disable-all-programs --enable-libmount --enable-libblkid CFLAGS=&quot;-Dcrc32c=util_linux_crc32c&quot;</code></li>
<li><code>make -j</code></li>
</ol>
</li>
<li>
<p>複製 <code>libmount.a</code> 及 <code>libblkid.a</code> 至系統交叉編譯路徑：</p>
<ul>
<li><code>sudo cp .libs/libmount.a .libs/libblkid.a /usr/lib/riscv64-linux-gnu/</code></li>
</ul>
</li>
<li>
<p>設定環境變數：</p>
<ul>
<li><code>export PKG_CONFIG=pkg-config</code></li>
<li><code>export PKG_CONFIG_LIBDIR=/usr/lib/riscv64-linux-gnu/pkgconfig:/usr/share/pkgconfig</code></li>
<li><code>export PKG_CONFIG_SYSROOT_DIR=/</code></li>
</ul>
</li>
<li>
<p>切到 QEMU 的資料夾並 configure QEMU (with debug info and SLiRP)：</p>
<ul>
<li><code>./configure --cross-prefix=riscv64-linux-gnu- --cpu=riscv64 --target-list=riscv64-softmmu --static --enable-debug --enable-slirp -Ddefault_library=static</code>
<ul>
<li><code>default_library</code> 是 meson built-in 的參數用來控制當使用 <code>library()</code> 的時候要怎麼編譯：
<ul>
<li><code>shared</code>：將 library 編譯成 shared library (e.g., <code>.so</code>)</li>
<li><code>static</code>：將 library 編譯成 static library (e.g., <code>.a</code>)</li>
<li><code>both</code>：同時將 library 編譯成 shared 及 static libraries</li>
</ul>
</li>
<li><code>-Ddefault_library=static</code> 可以指定將 subprojects 編譯成 static libraries (e.g., <code>libslirp.a</code>)</li>
</ul>
</li>
</ul>
</li>
<li>
<p>編譯 QEMU：</p>
<ul>
<li><code>make -j</code></li>
</ul>
</li>
<li>
<p>最後 QEMU 的執行檔會在：<code>./build/qemu-system-riscv64</code></p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-2">2</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>$ file build/qemu-system-riscv64
</span></span><span style="display:flex;"><span>build/qemu-system-riscv64: ELF 64-bit LSB executable, UCB RISC-V, RVC, double-float ABI, version <span style="color:#ae81ff">1</span> <span style="color:#f92672">(</span>SYSV<span style="color:#f92672">)</span>, statically linked, BuildID<span style="color:#f92672">[</span>sha1<span style="color:#f92672">]=</span>e5fb33b2ec9617aaae8890df5979b12e245f4f00, <span style="color:#66d9ef">for</span> GNU/Linux 4.15.0, with debug_info, not stripped
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ol>
]]></content:encoded>
    </item>
    <item>
      <title>Build QEMU environments</title>
      <link>https://0xc0de.xyz/posts/build-qemu-envs/</link>
      <pubDate>Sat, 25 Jan 2020 00:00:00 +0800</pubDate>
      <guid>https://0xc0de.xyz/posts/build-qemu-envs/</guid>
      <description>&lt;h1 id=&#34;build-qemu&#34;&gt;Build QEMU&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;安裝所需的套件：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;sudo apt install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex xinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev git libncurses5-dev libpixman-1-dev&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;下載 QEMU source codes：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git clone --recursive git@github.com:qemu/qemu.git&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;cd qemu&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;./configure --target-list=riscv64-softmmu,riscv32-softmmu,riscv64-linux-user,riscv32-linux-user&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;make -j&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;幾個好用的 debug configure 參數：
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--extra-cflags=CFLAGS&lt;/code&gt;：append extra C compiler flags QEMU_CFLAGS
&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;--extra-cflags=&amp;quot;-g3 --save-temps&amp;quot;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--extra-ldflags=LDFLAGS&lt;/code&gt;：append extra linker flags LDFLAGS&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--enable-debug-tcg&lt;/code&gt;：enable TCG debugging&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--disable-debug-tcg&lt;/code&gt;：disable TCG debugging (default)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--disable-debug-info&lt;/code&gt;：disable debugging information&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--enable-debug&lt;/code&gt;：enable common debug build options&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--disable-strip&lt;/code&gt;：disable stripping binaries&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--disable-werror&lt;/code&gt;：disable compilation abort on warning&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--enable-pie&lt;/code&gt;：build Position Independent Executables&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--disable-pie&lt;/code&gt;：do not build Position Independent Executables&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;幾個好用的執行參數：
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;-d item1,...&lt;/code&gt;：enable logging of specified items&lt;/p&gt;</description>
      <content:encoded><![CDATA[<h1 id="build-qemu">Build QEMU</h1>
<ol>
<li>
<p>安裝所需的套件：</p>
<ul>
<li><code>sudo apt install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex xinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev git libncurses5-dev libpixman-1-dev</code></li>
</ul>
</li>
<li>
<p>下載 QEMU source codes：</p>
<ul>
<li><code>git clone --recursive git@github.com:qemu/qemu.git</code></li>
</ul>
</li>
<li>
<p><code>cd qemu</code></p>
</li>
<li>
<p><code>./configure --target-list=riscv64-softmmu,riscv32-softmmu,riscv64-linux-user,riscv32-linux-user</code></p>
</li>
<li>
<p><code>make -j</code></p>
</li>
</ol>
<ul>
<li>幾個好用的 debug configure 參數：
<ul>
<li><code>--extra-cflags=CFLAGS</code>：append extra C compiler flags QEMU_CFLAGS
<ul>
<li>e.g. <code>--extra-cflags=&quot;-g3 --save-temps&quot;</code></li>
</ul>
</li>
<li><code>--extra-ldflags=LDFLAGS</code>：append extra linker flags LDFLAGS</li>
<li><code>--enable-debug-tcg</code>：enable TCG debugging</li>
<li><code>--disable-debug-tcg</code>：disable TCG debugging (default)</li>
<li><code>--disable-debug-info</code>：disable debugging information</li>
<li><code>--enable-debug</code>：enable common debug build options</li>
<li><code>--disable-strip</code>：disable stripping binaries</li>
<li><code>--disable-werror</code>：disable compilation abort on warning</li>
<li><code>--enable-pie</code>：build Position Independent Executables</li>
<li><code>--disable-pie</code>：do not build Position Independent Executables</li>
</ul>
</li>
<li>幾個好用的執行參數：
<ul>
<li>
<p><code>-d item1,...</code>：enable logging of specified items</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-10">10</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-11"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-11">11</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-12"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-12">12</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-13"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-13">13</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-14"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-14">14</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-15"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-15">15</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-16"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-16">16</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-17"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-17">17</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-18"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-18">18</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-19"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-19">19</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-20"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-20">20</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-0-21"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-0-21">21</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>Log items (comma separated):
</span></span><span style="display:flex;"><span>out_asm         show generated host assembly code for each compiled TB
</span></span><span style="display:flex;"><span>in_asm          show target assembly code for each compiled TB
</span></span><span style="display:flex;"><span>op              show micro ops for each compiled TB
</span></span><span style="display:flex;"><span>op_opt          show micro ops after optimization
</span></span><span style="display:flex;"><span>op_ind          show micro ops before indirect lowering
</span></span><span style="display:flex;"><span>int             show interrupts/exceptions in short format
</span></span><span style="display:flex;"><span>exec            show trace before each executed TB (lots of logs)
</span></span><span style="display:flex;"><span>cpu             show CPU registers before entering a TB (lots of logs)
</span></span><span style="display:flex;"><span>fpu             include FPU registers in the &#39;cpu&#39; logging
</span></span><span style="display:flex;"><span>mmu             log MMU-related activities
</span></span><span style="display:flex;"><span>pcall           x86 only: show protected mode far calls/returns/exceptions
</span></span><span style="display:flex;"><span>cpu_reset       show CPU state before CPU resets
</span></span><span style="display:flex;"><span>unimp           log unimplemented functionality
</span></span><span style="display:flex;"><span>guest_errors    log when the guest OS does something invalid (eg accessing a
</span></span><span style="display:flex;"><span>non-existent register)
</span></span><span style="display:flex;"><span>page            dump pages at beginning of user mode emulation
</span></span><span style="display:flex;"><span>nochain         do not chain compiled TBs so that &#34;exec&#34; and &#34;cpu&#34; show
</span></span><span style="display:flex;"><span>complete traces
</span></span><span style="display:flex;"><span>strace          log every user-mode syscall, its input, and its result
</span></span><span style="display:flex;"><span>trace:PATTERN   enable trace events
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p><code>-D logfile</code>：output log to logfile (default stderr)</p>
</li>
</ul>
</li>
<li>References：
<ul>
<li><a href="https://github.com/riscv/riscv-qemu/wiki">RISC-V QEMU Wiki</a></li>
<li><a href="https://www.cnblogs.com/root-wang/p/8005212.html">QEMU Build Considerations For Debugging</a></li>
</ul>
</li>
</ul>
<hr>
<h1 id="build-risc-v-toolchains">Build RISC-V toolchains</h1>
<ol>
<li>
<p>安裝所需的套件：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-2">2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-3">3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-1-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-1-4">4</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-gdscript3" data-lang="gdscript3"><span style="display:flex;"><span>sudo apt<span style="color:#f92672">-</span>get install autoconf automake autotools<span style="color:#f92672">-</span>dev curl libmpc<span style="color:#f92672">-</span>dev \
</span></span><span style="display:flex;"><span>                     libmpfr<span style="color:#f92672">-</span>dev libgmp<span style="color:#f92672">-</span>dev gawk build<span style="color:#f92672">-</span>essential bison \
</span></span><span style="display:flex;"><span>                     flex texinfo gperf libtool patchutils bc zlib1g<span style="color:#f92672">-</span>dev \
</span></span><span style="display:flex;"><span>                     libexpat<span style="color:#f92672">-</span>dev
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>下載 toolchains source codes：</p>
<ul>
<li><code>git clone --recursive [git@github.com](mailto:git@github.com):riscv/riscv-gnu-toolchain.git</code></li>
</ul>
</li>
<li>
<p><code>cd riscv-gnu-toolchain</code></p>
</li>
<li>
<p>Install：</p>
<ul>
<li>Newlib：
<ol>
<li><code>./configure --prefix=/opt/riscv</code></li>
<li><code>make</code></li>
</ol>
</li>
<li>Linux：
<ol>
<li><code>./configure --prefix=/opt/riscv</code></li>
<li><code>make linux</code></li>
</ol>
</li>
</ul>
</li>
<li>
<p>將 RISC-V toolchain binary 路徑加入 <code>PATH</code> 環境變數：</p>
<ul>
<li><code>export PATH=/opt/riscv/bin:$PATH</code></li>
</ul>
</li>
</ol>
<hr>
<h1 id="build-buildroot">Build Buildroot</h1>
<ol>
<li>下載 Buildroot source codes：
<ul>
<li><code>git clone [git@github.com](mailto:git@github.com):buildroot/buildroot.git</code></li>
</ul>
</li>
<li><code>make qemu_riscv64_virt_defconfig menuconfig</code>
<ul>
<li>如果不想由 bulidroot 編譯 Linux Kernel：
<ul>
<li>取消勾選：Kernel → Linux Kernel (<code>BR2_LINUX_KERNEL</code>)</li>
</ul>
</li>
<li>如果不想由 bulidroot 編譯 OpenSBI：
<ul>
<li>取消勾選：Bootloaders → opensbi (<code>BR2_TARGET_OPENSBI</code>)</li>
</ul>
</li>
<li>如果不想由 bulidroot 編譯 U-Boot：
<ul>
<li>取消勾選：Bootloaders →U-Boot (<code>BR2_TARGET_UBOOT</code>)</li>
</ul>
</li>
<li>如果不想由 buildroot 編譯 QEMU：
<ul>
<li>取消勾選：Host utilities → host qemu (<code>BR2_PACKAGE_HOST_QEMU</code>)</li>
</ul>
</li>
<li>如果想使用自行編譯的 toolchain：
<ul>
<li>Toolchain →Toolchain type → 選擇：External toolchain (<code>BR2_TOOLCHAIN_EXTERNAL</code>)</li>
<li>Toolchain → Toolchain → 選擇：Custom toolchain (<code>BR2_TOOLCHAIN_EXTERNAL_CUSTOM</code>)</li>
<li>Toolchain → Toolchain origin → 選擇：Pre-installed toolchain (<code>BR2_TOOLCHAIN_EXTERNAL_PREINSTALLED</code>)</li>
<li>設置 toolchain 的路徑：
<ul>
<li>Toolchain → Toolchain path (<code>BR2_TOOLCHAIN_EXTERNAL_PATH</code>)：
<ul>
<li>e.g. <code>/scratch3/yihaoc/sifive/toolchains/riscv64-unknown-linux-gnu-toolsuite-4.2.0-x86_64-linux-redhat8</code></li>
</ul>
</li>
</ul>
</li>
<li>設置 toolchain 的 prefix：
<ul>
<li>Toolchain → Toolchain prefix (<code>BR2_TOOLCHAIN_EXTERNAL_CUSTOM_PREFIX</code>)：
<ul>
<li>e.g. <code>$(ARCH)-unknown-linux-gnu</code></li>
</ul>
</li>
</ul>
</li>
<li>此外，還需根據所使用的 toolchain 設定其他的選項：
<ul>
<li>選擇 toolchain 的 gcc 版本：
<ul>
<li>Toolchain → External toolchain gcc version：
<ul>
<li>e.g. <code>14.x</code> (<code>BR2_TOOLCHAIN_EXTERNAL_GCC_14</code>)</li>
</ul>
</li>
</ul>
</li>
<li>選擇 toolchain 的 Kernel headers series：
<ul>
<li>Toolchain → External toolchain kernel headers series：
<ul>
<li>e.g. <code>6.18.x</code> (<code>BR2_TOOLCHAIN_EXTERNAL_HEADERS_6_18</code>)</li>
</ul>
</li>
</ul>
</li>
<li>選擇 toolchain 的 C library：
<ul>
<li>Toolchain → External toolchain C library：
<ul>
<li>e.g. <code>glibc</code> (<code>BR2_TOOLCHAIN_EXTERNAL_CUSTOM_GLIBC</code>)</li>
</ul>
</li>
</ul>
</li>
<li>選擇 toolchain 是否支援 RPC：
<ul>
<li>Toolchain → Toolchain has RPC support? (<code>BR2_TOOLCHAIN_EXTERNAL_INET_RPC</code>)
<ul>
<li>SiFive toolchain → <strong>No</strong></li>
</ul>
</li>
</ul>
</li>
<li>選擇 toolchain 是否支援 C++：
<ul>
<li>Toolchain → Toolchain has C++ support? (<code>BR2_TOOLCHAIN_EXTERNAL_CXX</code>)
<ul>
<li>SiFive toolchain → <strong>Yes</strong></li>
</ul>
</li>
</ul>
</li>
<li>選擇 toolchain 是否支援 Fortran：
<ul>
<li>Toolchain → Toolchain has Fortran support? (<code>BR2_TOOLCHAIN_EXTERNAL_FORTRAN</code>)
<ul>
<li>SiFive toolchain → <strong>Yes</strong></li>
</ul>
</li>
</ul>
</li>
<li>選擇 toolchain 是否支援 OpenMP：
<ul>
<li>Toolchain → Toolchain has OpenMP support?(<code>BR2_TOOLCHAIN_EXTERNAL_OPENMP</code>)
<ul>
<li>SiFive toolchain → <strong>Yes</strong></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>如果想使用 initramfs：
<ul>
<li>勾選：Filesystem images →cpio the root filesystem (for use as an initial RAM filesystem) (<code>BR2_TARGET_ROOTFS_CPIO</code>)</li>
</ul>
</li>
<li>編譯 rootfs 以及選擇 rootfs 的格式：
<ul>
<li>Filesystem images → ext2/3/4 root filesystem (<code>BR2_TARGET_ROOTFS_EXT2</code>)</li>
<li>Filesystem images → ext2/3/4 root filesystem → ext2/3/4 variant：
<ul>
<li>e.g. ext4 (<code>BR2_TARGET_ROOTFS_EXT2_4</code>)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><code>make -j</code></li>
</ol>
<hr>
<h1 id="build-busybox">Build Busybox</h1>
<ol>
<li>下載 Busybox source codes：
<ul>
<li><code>git clone [git@github.com](mailto:git@github.com):mirror/busybox.git</code></li>
</ul>
</li>
<li>編譯 Busybox：
<ul>
<li><code>make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- menuconfig</code></li>
<li>勾選：<code>Settings</code> → <code>Build static binary (no shared libs)</code> <code>(CONFIG_STATIC)</code>，以 statically linked 的方式編譯 Busybox
<ul>
<li>若無勾選，則預設會採用 dynamic linked 的方式編譯 Busybox，在建立 rootfs 的時候需要將所需的 library files 複製至 libs 資料夾，否則 Linux Kernel boot up 的時候會出現： <code>kernel panic - not syncing: No working init found.</code> 的錯誤訊息 (此訊息不一定是代表 <code>init</code>檔案找不到，無法正確執行 <code>init</code> 程式亦會顯示同樣的錯誤訊息)。</li>
</ul>
</li>
<li><code>make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j</code></li>
</ul>
</li>
<li>將 Busybox 安裝至 <code>_install</code> 資料夾：
<ul>
<li><code>make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- install</code></li>
</ul>
</li>
</ol>
<hr>
<h1 id="build-rootfs-from-busybox-if-rootfs-is-not-built-in-linux-kernel-image">Build rootfs from busybox (if rootfs is not built in Linux Kernel image)</h1>
<ol>
<li>
<p>建立大小為 10 MB 的 disk image：</p>
<ul>
<li><code>dd if=/dev/zero of=root.ext2 bs=1M count=10</code></li>
</ul>
</li>
<li>
<p>將 disk image 格式化為 <code>ext2</code>格式：</p>
<ul>
<li><code>mkfs.ext2 -F root.ext2</code></li>
</ul>
</li>
<li>
<p>掛載 rootfs：</p>
<ul>
<li><code>mkdir rootfs</code></li>
<li><code>sudo mount root.ext2 rootfs</code></li>
</ul>
</li>
<li>
<p><code>cd rootfs</code></p>
</li>
<li>
<p>複製 Busybox 至 rootfs：</p>
<ul>
<li><code>sudo cp -r ../busybox/_install/* .</code>
<ul>
<li>(假設 Busybox 的路徑為：<code>../busybox</code>)</li>
</ul>
</li>
</ul>
</li>
<li>
<p>建立所需的資料夾：</p>
<ul>
<li><code>sudo mkdir etc dev proc sys tmp</code></li>
</ul>
</li>
<li>
<p>建立 <code>etc/inittab</code> 檔：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-2">2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-3">3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-4">4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-5">5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-2-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-2-6">6</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>::sysinit:/bin/busybox mount -t proc proc /proc
</span></span><span style="display:flex;"><span>::sysinit:/bin/busybox mount -t tmpfs tmpfs /tmp
</span></span><span style="display:flex;"><span>::sysinit:/bin/busybox --install -s
</span></span><span style="display:flex;"><span>::shutdown:/bin/umount -a -r
</span></span><span style="display:flex;"><span>::shutdown:/sbin/swapoff -a
</span></span><span style="display:flex;"><span>/dev/console::sysinit:-/bin/ash
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>建立所需的裝置檔案：</p>
<ul>
<li><code>sudo mknod dev/console c 5 1</code></li>
<li><code>sudo mknod dev/null c 1 3</code></li>
</ul>
</li>
<li>
<p>解除掛載：</p>
<ul>
<li><code>cd .. &amp;&amp; sudo umount rootfs</code></li>
</ul>
</li>
</ol>
<hr>
<h1 id="bulid-opensbi">Bulid OpenSBI</h1>
<ol>
<li>下載 OpenSBI source codes：
<ul>
<li><code>git clone [git@github.com](mailto:git@github.com):riscv-software-src/opensbi.git</code></li>
</ul>
</li>
<li><code>make CROSS_COMPILE=riscv64-unknown-linux-gnu- PLATFORM=generic FW_TEXT_START=0x80000000 -j</code></li>
</ol>
<hr>
<h1 id="build-u-boot">Build U-Boot</h1>
<ol>
<li>下載 U-Boot source codes：
<ul>
<li><code>git clone [git@github.com](mailto:git@github.com):u-boot/u-boot.git</code></li>
</ul>
</li>
<li><strong>TODO</strong></li>
</ol>
<hr>
<h1 id="build-linux-kernel">Build Linux Kernel</h1>
<ol>
<li>下載 Linux Kernel source codes：
<ul>
<li><code>git clone [git@github.com](mailto:git@github.com):torvalds/linux.git</code></li>
</ul>
</li>
<li><code>make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- menuconfig</code>
<ul>
<li>如果要使用 initramfs，且將 rootfs 直接編譯至核心中，則需：
<ul>
<li>勾選：General setup → Initial RAM filesystem and RAM disk (initramfs/initrd) support (<code>CONFIG_BLK_DEV_INITRD</code>)</li>
<li>並設置 initramfs rootfs 路徑 (<code>CONFIG_INITRAMFS_SOURCE</code>)
<ul>
<li>e.g. <code>/home/yihaoc/frankchang/buildroot/output/images/rootfs.cpio</code></li>
</ul>
</li>
</ul>
</li>
<li>如果想包含 debug symbols：
<ul>
<li>開啟 Kernel debugging：
<ul>
<li>Kernel hacking → Kernel debugging (<code>DEBUG_KERNEL</code>)</li>
</ul>
</li>
<li>選擇 DWARF debug info 版本：
<ul>
<li>Kernel hacking → Compile-time checks and compiler options → Debug information
<ul>
<li>e.g. Rely on the toolchain’s implicit default DWARF version (<code>DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT</code>)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>如果想要包含 devmem：
<ul>
<li>Device Drivers → Character devices → /dev/mem virtual device support (<code>CONFIG_DEVMEM</code>)</li>
</ul>
</li>
<li>切換 Compiler optimization level 為 Optimize for size (<code>-Os</code>)
<ul>
<li>勾選：General setup →Compiler optimization level → Optimize for size (-Os) (<code>CONFIG_CC_OPTIMIZE_FOR_SIZE</code>)</li>
</ul>
</li>
</ul>
</li>
<li><code>make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j</code></li>
</ol>
<hr>
<h1 id="boot-qemu-with-linux-kernel-and-rootfs">Boot QEMU with Linux Kernel and rootfs</h1>
<h2 id="method-1-rootfs-built-in-in-linux-kernel-image">Method 1: rootfs built-in in Linux Kernel image:</h2>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-3-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-3-10">10</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>./qemu/build/qemu-system-riscv64 \
</span></span><span style="display:flex;"><span>        -nographic \
</span></span><span style="display:flex;"><span>        -machine virt \
</span></span><span style="display:flex;"><span>        -smp 4 \
</span></span><span style="display:flex;"><span>        -m 2G \
</span></span><span style="display:flex;"><span>        -bios ./opensbi/build/platform/generic/firmware/fw_jump.elf \
</span></span><span style="display:flex;"><span>        -kernel ./linux/arch/riscv/boot/Image \
</span></span><span style="display:flex;"><span>        -append &#34;root=/dev/vda ro console=ttyS0 earlycon&#34; \
</span></span><span style="display:flex;"><span>        -device virtio-net-device,netdev=usernet \
</span></span><span style="display:flex;"><span>        -netdev user,id=usernet,hostfwd=tcp::10000-:22
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>(假設 QEMU 執行檔的路徑為：<code>./qemu/build/qemu-system-riscv64</code>)</li>
<li>(假設 OpenSBI image 的路徑為：<code>./opensbi/build/platform/generic/firmware/fw_jump.elf</code>)</li>
<li>(假設 Linux Kernel image 的路徑為：<code>./linux/arch/riscv/boot/Image</code>)</li>
</ul>
<hr>
<h2 id="method-2-load-rootfs-from-disk">Method 2: Load rootfs from disk:</h2>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-10">10</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-11"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-11">11</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-4-12"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-4-12">12</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>./qemu/build/qemu-system-riscv64 \
</span></span><span style="display:flex;"><span>        -nographic \
</span></span><span style="display:flex;"><span>        -machine virt \
</span></span><span style="display:flex;"><span>        -smp 4 \
</span></span><span style="display:flex;"><span>        -m 2G \
</span></span><span style="display:flex;"><span>        -bios ./opensbi/build/platform/generic/firmware/fw_jump.elf \
</span></span><span style="display:flex;"><span>        -kernel ./linux/arch/riscv/boot/Image \
</span></span><span style="display:flex;"><span>        -append &#34;root=/dev/vda ro console=ttyS0 earlycon&#34; \
</span></span><span style="display:flex;"><span>        -device virtio-blk-device,drive=hd0 \
</span></span><span style="display:flex;"><span>        -drive file=./images/root.ext2,format=raw,id=hd0 \
</span></span><span style="display:flex;"><span>        -device virtio-net-device,netdev=usernet \
</span></span><span style="display:flex;"><span>        -netdev user,id=usernet,hostfwd=tcp::10000-:22
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li>(假設 QEMU 執行檔的路徑為：<code>./qemu/build/qemu-system-riscv64</code>)</li>
<li>(假設 OpenSBI image 的路徑為：<code>./opensbi/build/platform/generic/firmware/fw_jump.elf</code>)</li>
<li>(假設 Linux Kernel image 的路徑為：<code>./linux/arch/riscv/boot/Image</code>)</li>
<li>(假設 rootfs disk image 的路徑為：<code>./images/root.ext2</code>)</li>
<li>References：
<ul>
<li><a href="https://ithelp.ithome.com.tw/articles/10192454">第二日：環境架設及過程解析 - iT 邦幫忙 - iThome</a></li>
<li><a href="https://coldnew.github.io/6cc46ece/">使用 Busybox 建立 RISC-V 的迷你系統</a></li>
</ul>
</li>
</ul>
<hr>
<h1 id="debug-linux-kernel-with-gdb-with-qemu">Debug Linux Kernel with GDB with QEMU</h1>
<ul>
<li>
<p>Linux Kernel 需要勾選：</p>
<ul>
<li><code>Kernel hacking</code> → <code>Kernel debugging</code> <code>(CONFIG_DEBUG_KERNEL)</code></li>
<li><code>Kernel hacking</code> → <code>Compile-time checks and compiler options</code> → <code>Compile the kernel with debug info</code> <code>(CONFIG_DEBUG_INFO)</code></li>
<li><code>Kernel hacking</code> → <code>Compile-time checks and compiler options</code> → <code>Provide GDB scripts for kernel debugging</code> <code>(CONFIG_GDB_SCRIPTS)</code></li>
</ul>
</li>
<li>
<p>啟動 QEMU：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-10">10</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-11"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-11">11</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-5-12"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-5-12">12</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>./qemu/riscv64-softmmu/qemu-system-riscv64 \
</span></span><span style="display:flex;"><span>        -nographic \
</span></span><span style="display:flex;"><span>        -machine virt \
</span></span><span style="display:flex;"><span>        -smp 4 \
</span></span><span style="display:flex;"><span>        -m 2G \
</span></span><span style="display:flex;"><span>        -kernel ./riscv-pk/build/bbl \
</span></span><span style="display:flex;"><span>        -append &#34;root=/dev/vda ro console=ttyS0 earlycon&#34; \
</span></span><span style="display:flex;"><span>        -device virtio-blk-device,drive=hd0 \
</span></span><span style="display:flex;"><span>        -drive file=root.ext2,format=raw,id=hd0 \
</span></span><span style="display:flex;"><span>        -device virtio-net-device,netdev=usernet \
</span></span><span style="display:flex;"><span>        -netdev user,id=usernet,hostfwd=tcp::10000-:22 \
</span></span><span style="display:flex;"><span>        -s -S
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li><code>-S</code>：Freeze CPU at startup (use &lsquo;c&rsquo; to start execution).</li>
<li><code>-s</code>：Shorthand for <code>-gdb tcp::1234</code>.</li>
<li><code>-gdb dev</code>：Wait for gdb connection on <code>dev</code>.</li>
</ul>
</li>
<li>
<p>使用 GDB debug Linux Kernel：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-6-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-6-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-6-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-6-2">2</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>riscv64-unknown-linux-gnu-gdb vmlinux
</span></span><span style="display:flex;"><span>&gt; (gdb) target remote :1234
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>使用 CGDB debug Linux Kernel：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-7-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-7-1">1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-7-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-7-2">2</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>cgdb -d riscv64-unknown-linux-gnu-gdb vmlinux
</span></span><span style="display:flex;"><span>&gt; (gdb) target remote :1234
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>References：</p>
<ul>
<li><a href="https://en.wikibooks.org/wiki/QEMU/Debugging_with_QEMU">QEMU/Debugging with QEMU</a></li>
<li><a href="https://consen.github.io/2018/01/17/debug-linux-kernel-with-qemu-and-gdb/">使用QEMU和GDB調試Linux內核</a></li>
</ul>
</li>
</ul>
<hr>
<h1 id="debug-qemu">Debug QEMU</h1>
<ul>
<li>
<p>編譯 QEMU：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-10">10</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-11"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-11">11</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-12"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-12">12</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-13"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-13">13</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-8-14"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-8-14">14</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>./configure --help
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Advanced options (experts only):
</span></span><span style="display:flex;"><span>  --extra-cflags=CFLAGS     append extra C compiler flags QEMU_CFLAGS
</span></span><span style="display:flex;"><span>  --extra-cxxflags=CXXFLAGS append extra C++ compiler flags QEMU_CXXFLAGS
</span></span><span style="display:flex;"><span>  --extra-ldflags=LDFLAGS   append extra linker flags LDFLAGS
</span></span><span style="display:flex;"><span>  --enable-debug            enable common debug build options
</span></span><span style="display:flex;"><span>  --disable-strip           disable stripping binaries
</span></span><span style="display:flex;"><span>  --enable-gprof            QEMU profiling with gprof
</span></span><span style="display:flex;"><span>  --enable-profiler         profiler support
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>Optional features, enabled with --enable-FEATURE and
</span></span><span style="display:flex;"><span>disabled with --disable-FEATURE, default is enabled if available:
</span></span><span style="display:flex;"><span>  pie                       Position Independent Executables (--disable-pie)
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li><code>./configure --target-list=riscv64-softmmu --enable-debug</code></li>
<li><code>./configure --target-list=riscv64-softmmu --enable-debug --extra-cflags=&quot;-g3 --save-temps&quot; --extra-ldflags=&quot;-g3&quot; --disable-strip --disable-pie</code></li>
</ul>
</li>
<li>
<p>使用 GDB：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-10">10</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-9-11"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-9-11">11</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>gdb --args ./qemu/riscv64-softmmu/qemu-system-riscv64 \
</span></span><span style="display:flex;"><span>        -nographic \
</span></span><span style="display:flex;"><span>        -machine virt \
</span></span><span style="display:flex;"><span>        -smp 4 \
</span></span><span style="display:flex;"><span>        -m 2G \
</span></span><span style="display:flex;"><span>        -kernel ./riscv-pk/build/bbl \
</span></span><span style="display:flex;"><span>        -append &#34;root=/dev/vda ro console=ttyS0&#34; \
</span></span><span style="display:flex;"><span>        -device virtio-blk-device,drive=hd0 \
</span></span><span style="display:flex;"><span>        -drive file=root.ext2,format=raw,id=hd0 \
</span></span><span style="display:flex;"><span>        -device virtio-net-device,netdev=usernet \
</span></span><span style="display:flex;"><span>        -netdev user,id=usernet,hostfwd=tcp::10000-:22
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>使用 CGDB：</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-1"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-1"> 1</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-2"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-2"> 2</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-3"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-3"> 3</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-4"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-4"> 4</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-5"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-5"> 5</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-6"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-6"> 6</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-7"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-7"> 7</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-8"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-8"> 8</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-9"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-9"> 9</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-10"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-10">10</a>
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f" id="hl-10-11"><a style="outline:none;text-decoration:none;color:inherit" href="#hl-10-11">11</a>
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>cgdb --args ./qemu/riscv64-softmmu/qemu-system-riscv64 \
</span></span><span style="display:flex;"><span>        -nographic \
</span></span><span style="display:flex;"><span>        -machine virt \
</span></span><span style="display:flex;"><span>        -smp 4 \
</span></span><span style="display:flex;"><span>        -m 2G \
</span></span><span style="display:flex;"><span>        -kernel ./riscv-pk/build/bbl \
</span></span><span style="display:flex;"><span>        -append &#34;root=/dev/vda ro console=ttyS0&#34; \
</span></span><span style="display:flex;"><span>        -device virtio-blk-device,drive=hd0 \
</span></span><span style="display:flex;"><span>        -drive file=root.ext2,format=raw,id=hd0 \
</span></span><span style="display:flex;"><span>        -device virtio-net-device,netdev=usernet \
</span></span><span style="display:flex;"><span>        -netdev user,id=usernet,hostfwd=tcp::10000-:22
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>References：</p>
<ul>
<li><a href="https://www.cnblogs.com/root-wang/p/8005212.html">Setups For Debugging QEMU with GDB and DDD</a></li>
<li><a href="https://translatedcode.wordpress.com/2015/05/30/tricks-for-debugging-qemu-rr/">Tricks for debugging QEMU — rr</a></li>
</ul>
</li>
</ul>
<hr>
<h1 id="gdb--cgdb">GDB / CGDB</h1>
<ul>
<li><a href="https://github.com/hellogcc/100-gdb-tips">《100個gdb小技巧》</a></li>
<li><a href="https://cgdb.github.io/">cgdb</a></li>
<li><a href="https://tech.mozilla.com.tw/?p=3826">CGDB – 更好用的 GDB</a></li>
</ul>
<hr>
<h1 id="build-bbl-berkery-boot-loader">Build bbl (Berkery Boot Loader)</h1>
<ol>
<li>下載 riscv-pk source codes：
<ul>
<li><code>git clone [git@github.com](mailto:git@github.com):riscv/riscv-pk.git</code></li>
</ul>
</li>
<li><code>cd riscv-pk</code></li>
<li><code>mkdir build</code></li>
<li><code>cd build</code></li>
<li><code>../configure --enable-logo --with-payload=../../linux/vmlinux --host=riscv64-unknown-linux-gnu</code>
<ul>
<li>(假設 Linux Kernel vmlinux 的路徑為：<code>../../linux/vmlinux</code>)</li>
</ul>
</li>
<li><code>make -j</code></li>
</ol>
<p>P.S. 如果要使用 BBL 開機的話，QEMU 的 -kernel 參數要指定 BBL image：</p>
<p>e.g. <code>-kernel ./riscv-pk/build/bbl</code></p>
<ul>
<li>(假設 bbl 的路徑為： <code>./riscv-pk/build/bbl</code>)</li>
<li>References：
<ul>
<li><a href="https://github.com/riscv/riscv-pk">RISC-V Proxy Kernel and Boot Loader Github</a></li>
</ul>
</li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
