riscv_baremetal.git
3 weeks agotrap, main: implement trap handler
Kit Rhett Aultman [Sat, 24 Aug 2024 15:30:25 +0000 (11:30 -0400)]
trap, main: implement trap handler

This commit implements a trap handler, which handles a number of basic
exception cases and sets the code up for handling interrupts, which will
be really useful for making a serial console properly without relying on
looping.

Changes:
* trap.rs implements a TrapFrame struct and declares one in memory.
  This will allow the code to preserve registers and context without
  using the stack.
* trap.S implements the low-level parts of the trap handler, copying the
  general-purpose and floating-point registers, plus the trap cause and
  other context info, into the TrapFrame, and calls the Rust function
  m_trap.
* trap.rs implements m_trap, which performs dispatch based on the
  exception code and, if the exception is non-fatal, returns the address
  of the PC that the trap returns to (typically PC+4).
* main.rs expands the panic handler to log panic information to serial
  out so that fatal exceptions are logged.  It also adds the trap vector
  init code.

This particular method of performing traps is heavily inspired from
Stephen Marz' blog on bare metal Rust for RISC-V:
http://osblog.stephenmarz.com/ch4.html

8 weeks agomain: clean up logging
Kit Rhett Aultman [Sun, 30 Jun 2024 16:41:08 +0000 (12:41 -0400)]
main: clean up logging

A practice I've used for years is to prefix all logging statements with
my name in all-caps.  These provisional messages can then be easily
spotted and either kept (sans prefix) or removed.  In my haste, though,
I missed one.

2 months agoheap/linker/main: set up a heap allocator
Kit Rhett Aultman [Sun, 30 Jun 2024 16:38:01 +0000 (12:38 -0400)]
heap/linker/main: set up a heap allocator

This commit sets up a heap allocator.  Again, I cannot sufficiently
stress the usefulness of Meyer Zinn for getting things this far, and
much of what I needed to know about getting linker symbols propagated
into Rust code comes from:
https://meyerzinn.tech/posts/2023/03/08/p1-printing-and-allocating/

Meyer steps through the process of setting up the linked list allocator.
However, there's a buddy list allocator that can be used as a drop-in
replacement, and since this is a more common allocator scheme, and since
doing my own thing means writing my own code and getting more familiar
with Rust, it seemed a divergence worth taking.

2 months agoREADME.md: created README
Kit Rhett Aultman [Sun, 30 Jun 2024 05:26:12 +0000 (01:26 -0400)]
README.md: created README

Basic statement of goals, current requirements to run, and some notes.

2 months agomain/console: console abstraction and print macros
Kit Rhett Aultman [Sun, 30 Jun 2024 05:00:43 +0000 (01:00 -0400)]
main/console: console abstraction and print macros

This commit does a bit of a cleanup and re-org of the UART code so that
code doesn't need to create and carry around a reference to the UART to
print to the console.  This is now available through print!() and
println!()

As with the prior commit, this was heavily informed by Meyer Zinn's
excellent blog posts on bare-metal Rust, specifically this one:
https://meyerzinn.tech/posts/2023/03/08/p1-printing-and-allocating/

The idea here is to basically create a singleton Console object and then
only access it behind the print!() and println!() macros.  Since the
macros are designed to be used anywhere, they hide the interactions with
a global singleton which performs the debug outputs.

Even though I likely shouldn't have, I also did a rustfmt to clean up
syntax here.  Ah, well.

2 months agoHello world for qemu serial
Kit Rhett Aultman [Mon, 24 Jun 2024 04:06:17 +0000 (00:06 -0400)]
Hello world for qemu serial

This commit establishes the overall project structure needed for doing a bare
metal executive on RISC-V.  This currently supports the qemu risc64 emulator
because that is a target I can develop across multiple environments as I have
the free time.

This contains:
* Basic project structure
* rustc cross-compilation to riscv64-unknown-none-elf target
* Cargo integration for running qemu
* linking scripts for the qemu riscv64 virt "board" memory layout
* do-nothing panic handler
* _start function that contains the minimal RISC-V initialization: setting up
  the global pointer (gp), reserving a stack, and clearing the bss region
* Very simple use of the uart_16550 crate to print a "Hello world" message

This all came together mostly in an afternoon's work and wouldn't have been
possible without these exceptional resources:

"Running Rust code on RISC-V in Qemu"
https://meyerzinn.tech/posts/2023/03/05/running-rust-code-on-risc-v-in-qemu/

"RISC-V from scratch 2: Hardware layouts, linker scripts, and C runtimes"
https://twilco.github.io/riscv-from-scratch/2019/04/27/riscv-from-scratch-2.html