RISC-V Meaty Skeleton with QEMU virt board: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
No edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(12 intermediate revisions by 5 users not shown)
Line 1:
{{BeginnersWarning}}
{{Rating|1}}
{{Template:Kernel designs}}
 
This tutorial assumes you have completed [[RISC-V_Bare_Bones|RISC-V Bare Bones]] on the QEMU <code>virt</code> board, or alternatively, [[HiFive-1_Bare_Bones|HiFive-1 Bare Bones]]. If not, you should complete them first for an overview of how to boot your own operating system on RISC-V. This tutorial is deliberately brief on concepts that have already been covered in the bare bones tutorials and their transitive prerequisites.
 
Line 36 ⟶ 40:
Fetch the latest version of GDB through [https://ftp.gnu.org/gnu/gdb/ https://ftp.gnu.org/gnu/gdb/]. The latest version at the time of writing is 12.1:
 
<sourcesyntaxhighlight lang="bash">
export GDB_VERSION="12.1"
wget https://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.xz
</syntaxhighlight>
</source>
 
Unpack the archive:
 
<sourcesyntaxhighlight lang="bash">
tar xvf gdb-${GDB_VERSION}.tar.xz
</syntaxhighlight>
</source>
 
Now move into the source directory:
 
<sourcesyntaxhighlight lang="bash">
pushd gdb-${GDB_VERSION}/
</syntaxhighlight>
</source>
 
Create a build directory and move into it:
 
<sourcesyntaxhighlight lang="bash">
mkdir build
pushd build/
</syntaxhighlight>
</source>
 
Now export a few variables as with building a cross-GCC or cross-binutils:
 
<sourcesyntaxhighlight lang="bash">
export PREFIX="$HOME/opt/cross"
export TARGET=riscv64-elf
export PATH="$PREFIX/bin:$PATH"
</syntaxhighlight>
</source>
 
Configuration options are mostly the same as with building cross-GCC or cross-binutils. In particular, you may wish to enable the following features:
Line 73 ⟶ 77:
* <code>--with-expat</code>: Build with Expat, a library for XML parsing. Requires development headers for Expat to be installed on the build host
 
<sourcesyntaxhighlight lang="bash">
../configure --target=$TARGET \
--prefix="$PREFIX" \
Line 83 ⟶ 87:
--enable-tui=yes \
--with-expat
</syntaxhighlight>
</source>
 
Now build the source code (this can take a while):
 
<sourcesyntaxhighlight lang="bash">
make -j$(nproc)
</syntaxhighlight>
</source>
 
And install:
 
<sourcesyntaxhighlight lang="bash">
make -C gdb install
</syntaxhighlight>
</source>
 
If you wish to keep the source tree available for conveniently re-building GDB in the future (e.g. with a different set of options), clean up the build files now:
 
<sourcesyntaxhighlight lang="bash">
make clean
</syntaxhighlight>
</source>
 
Move one level up to the project root:
 
<sourcesyntaxhighlight lang="bash">
popd
</syntaxhighlight>
</source>
 
Remove the <code>build/</code> directory we created:
 
<sourcesyntaxhighlight lang="bash">
rm -rf build/
</syntaxhighlight>
</source>
 
Move up one more level:
 
<sourcesyntaxhighlight lang="bash">
popd
</syntaxhighlight>
</source>
 
Double check our cross-debugger is properly installed:
 
<sourcesyntaxhighlight lang="bash">
which -- $TARGET-gdb || echo $TARGET-gdb is not in the PATH
</syntaxhighlight>
</source>
 
Now you may safely remove the source tree and archive, if you wish:
 
<sourcesyntaxhighlight lang="bash">
rm -rf gdb-${GDB_VERSION}/
rm gdb-${GDB_VERSION}.tar.xz
</syntaxhighlight>
</source>
 
If <code>$PREFIX/bin</code> is not in your path already, you may wish to persist it by writing it to your profile:
 
<sourcesyntaxhighlight lang="bash">
echo "export PATH=\"\$HOME/opt/cross/bin:\$PATH\"" >> $HOME/.profile
source $HOME/.profile
</syntaxhighlight>
</source>
 
==Dependencies==
Line 160 ⟶ 164:
Fetch the source code from the <code>v0.0.1</code> release of [https://github.com/DonaldKellett/marvelos https://github.com/DonaldKellett/marvelos] code-named "Meaty Skeleton", using [https://git-scm.com/ Git]:
 
<sourcesyntaxhighlight lang="bash">
git clone --branch v0.0.1 https://github.com/DonaldKellett/marvelos.git
</syntaxhighlight>
</source>
 
Operating systems development is about being an expert. Take the time to read the code carefully through and understand it. Please seek further information and help if you don't understand aspects of it. This code is minimal and almost everything is done deliberately, often to pre-emptively solve future problems.
Line 189 ⟶ 193:
Run <code>make $TARGET</code> with <code>TARGET</code> set to your desired target to execute the specified target. For example, to build the kernel image and run it in QEMU:
 
<sourcesyntaxhighlight lang="bash">
make run
</syntaxhighlight>
</source>
 
Additionally, the default target is <code>all</code> if not specified:
 
<sourcesyntaxhighlight lang="bash">
make
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="make">
# Build
CC=riscv64-elf-gcc
Line 242 ⟶ 246:
rm -vf *.o
rm -vf $(KERNEL_IMAGE)
</syntaxhighlight>
</source>
 
===Kernel source===
Line 248 ⟶ 252:
====src/common/common.h====
 
<sourcesyntaxhighlight lang="c">
#ifndef COMMON_H
#define COMMON_H
Line 256 ⟶ 260:
 
#endif
</syntaxhighlight>
</source>
 
====src/common/common.c====
 
<sourcesyntaxhighlight lang="c">
#include <stdarg.h>
#include "common.h"
Line 278 ⟶ 282:
asm volatile ("wfi");
}
</syntaxhighlight>
</source>
 
====src/syscon/syscon.h====
 
<sourcesyntaxhighlight lang="c">
#ifndef SYSCON_H
#define SYSCON_H
Line 294 ⟶ 298:
 
#endif
</syntaxhighlight>
</source>
 
====src/syscon/syscon.c====
 
<sourcesyntaxhighlight lang="c">
#include <stdint.h>
#include "syscon.h"
Line 312 ⟶ 316:
*(uint32_t *)SYSCON_ADDR = 0x7777;
}
</syntaxhighlight>
</source>
 
====src/uart/uart.h====
 
<sourcesyntaxhighlight lang="c">
#ifndef UART_H
#define UART_H
Line 333 ⟶ 337:
 
#endif
</syntaxhighlight>
</source>
 
====src/uart/uart.c====
 
<sourcesyntaxhighlight lang="c">
#include <stddef.h>
#include <stdint.h>
Line 559 ⟶ 563:
va_end(arg);
}
</syntaxhighlight>
</source>
 
====src/kmain.c====
 
<sourcesyntaxhighlight lang="c">
#include "uart/uart.h"
#include "syscon/syscon.h"
Line 579 ⟶ 583:
poweroff();
}
</syntaxhighlight>
</source>
 
==Running the project==
Line 603 ⟶ 607:
* [[HiFive-1_Bare_Bones|HiFive-1 Bare Bones]]
* [[Meaty_Skeleton|Meaty Skeleton]]
 
[[Category:RISC-V]]
[[Category:Bare_bones_tutorials]]
[[Category:C]]
[[Category:QEMU]]
[[Category:Level_1_Tutorials]]
 
===External links===
 
* [https://github.com/twilco/riscv-from-scratch RISC-V from scratch]
* [https://github.com/sgmarz/osblog The Adventures of OS]
* [https://linuxfromscratch.org/ Linux From Scratch]
* [https://github.com/riscv-software-src/opensbi OpenSBI]