RISC-V Bare Bones: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
m Low level detailed explanation: NuttX RTOS on Star64 Soc
m update source tags to syntaxhighlight
 
Line 11:
The thing we will make here is a simple serial port output/reader.
 
<sourcesyntaxhighlight lang="c">
#include <stdint.h>
#include <stddef.h>
Line 37:
return;
}
</syntaxhighlight>
</source>
 
This will print a simple "Hello world!" and a newline following, after that it will echo any input given to the serial port.
Line 44:
Be wary we are just setting up stack and jumping into kernel. It's the kernel's job to set up IRQs and other system stuff
 
<sourcesyntaxhighlight lang="asm">
.section .init
 
Line 83:
.end
 
</syntaxhighlight>
</source>
 
Take in account that you need to implement a type of locking if you want to support SMP, and implement a trampoline or a jail for the CPUs. Be wary that we haven't enabled interrupts just yet and any return from kmain will result in invalid opcodes being executed.
Line 91:
We need to specify where our kernel will be loaded at. For this example we will use the start of the RAM as our load address.
 
<sourcesyntaxhighlight lang="c">
ENTRY(start);
Line 118:
}
}
</syntaxhighlight>
</source>
 
== Final ==
 
The process is pretty straightforward:
<sourcesyntaxhighlight lang="bash">
riscv64-elf-gcc -Wall -Wextra -c -mcmodel=medany kernel.c -o kernel.o -ffreestanding
riscv64-elf-as -c entry.S -o entry.o
riscv64-elf-ld -T linker.ld -lgcc -nostdlib kernel.o entry.o -o kernel.elf
</syntaxhighlight>
</source>
 
Once this is done, the kernel can be run with the following:
<sourcesyntaxhighlight lang="bash">
qemu-system-riscv64 -machine virt -bios none -kernel kernel.elf -serial mon:stdio
</syntaxhighlight>
</source>
 
You will promptly see a terminal with the "Hello world!" and you can "write stuff" to it. A serial terminal is very "boring", but you still have a lot at your disposal, you have a PLIC, a CLIC and a bunch of opportunities to learn PCI internals.