Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@ An implementation of [openkal][kal] on the RISC-V Supervisor Binary Interface.

```toml
[dependencies]
openkal = "0.25.0"
openkal-opensbi = "0.9.1"
openkal = "0.26.0"
openkal-opensbi = "0.10.1"
```

## What 0.10.1 corrects

The manifest states the interfaces this implementation provides,
`[kernel-abi] provides-interfaces`: `openkal.abort`, `openkal.env`,
`openkal.memory`, `openkal.stream` and `openkal.time`, which `kal_interfaces()`
answered already. A package that states no list is read as providing every
interface, so a C library above it (openkal-musl, which asks
`cfg(interface = ...)`) was built calling `openkal.fs`, `.process` and `.task`,
and the link failed on the names this implementation does not define.

A program's status leaves the machine. SBI's system reset carries a reason,
none or failure, and not a number, so a program that ended with 7 and one that
ended with 0 shut the machine down alike: measured under OpenSBI on QEMU's
`virt`, the emulator ended with 0 for a program that returned 7. The standalone
start reads the device tree the firmware hands over in `a1`; where it describes
a test finisher (a node compatible with `sifive,test1` or `sifive,test0`),
`kal_exit` writes the status to its register, `(status << 16) | 0x3333`, or
`0x5555` for zero, and the machine ends with that status; an abort writes 134.
Where the tree describes none, the reset's reason is what remains, and nothing
is written to a machine that has no such device.

## What 0.10 changes (openkal 0.26)

Nothing but the specification's version: the manifest declares `openkal =
"^0.26.0"`. openkal 0.26 requires two things of `openkal.process`, a start
bounded by the environment alone (clause 7.6) and a unit claimed only where a
reference can be made (clause 7.18). This implementation does not provide that
interface, and a program here receives no arguments and no variables, so there
is nothing of either clause to hold.

## What 0.9.1 corrects

The manifest declares `openkal = "^0.25.0"`. An implementation written against a
Expand Down
7 changes: 5 additions & 2 deletions mcpp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[package]
namespace = "mcpplibs"
name = "openkal-opensbi"
version = "0.9.1"
version = "0.10.1"
description = "An implementation of openkal on the RISC-V Supervisor Binary Interface, portable across every machine whose firmware provides one"
license = "Apache-2.0"

Expand All @@ -32,9 +32,12 @@ repo = "https://github.com/mcpplibs/openkal-opensbi"

# The contract, not an implementation of it. Declaring it turns a version
# mismatch into a resolution-time message rather than a link-time one.
[kernel-abi]
provides-interfaces = ["openkal.abort", "openkal.env", "openkal.memory", "openkal.stream", "openkal.time"]

[dependencies]
# Every patch release of the specification's minor version (openkal clause 8).
openkal = "^0.25.0"
openkal = "^0.26.0"

# WHAT RECEIVES CONTROL, WHICH IS A STATEMENT ABOUT THE PROGRAM.
#
Expand Down
102 changes: 102 additions & 0 deletions src/finisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// THE STATUS LEAVES THE MACHINE THROUGH THE DEVICE THE MACHINE STATES FOR IT (0.10.1).
//
// SBI's system reset carries a reason, none or failure, and not a number: a program that ended with
// 7 and one that ended with 0 both shut the machine down the same way, and an emulator running a
// test reported 0 for both (measured: QEMU's `virt' under OpenSBI 1.5, status 0 for a program that
// returned 7). A machine that has a device to end with a status describes it in the device tree the
// firmware hands the program in a1: a node compatible with "sifive,test1" or "sifive,test0", the
// test finisher, whose register ends the machine with (status << 16) | 0x3333, or passes with
// 0x5555. Where the tree states one, the status is written there; where it states none, the
// reset's reason is what remains, and a machine without the device is not written to.
#include "finisher.h"

namespace {

using u8 = unsigned char;
using u32 = unsigned int;
using u64 = unsigned long long;

volatile u32* g_finisher = nullptr;

u32 be32(const u8* p) {
return static_cast<u32>(p[0]) << 24 | static_cast<u32>(p[1]) << 16 | static_cast<u32>(p[2]) << 8 | p[3];
}

bool same(const char* a, const char* b) {
while (*a && *a == *b) { ++a; ++b; }
return *a == *b;
}

// Whether a `compatible' value, a list of terminated strings, names the test finisher.
bool finisher_compatible(const u8* v, u32 len) {
for (u32 at = 0; at < len;) {
const char* s = reinterpret_cast<const char*>(v + at);
if (same(s, "sifive,test1") || same(s, "sifive,test0")) return true;
while (at < len && v[at]) ++at;
++at;
}
return false;
}

constexpr u32 kBeginNode = 1, kEndNode = 2, kProp = 3, kNop = 4;
constexpr u32 kMaxDepth = 16;

} // namespace

void okbsbi_find_finisher(const void* fdt) {
const auto* h = static_cast<const u8*>(fdt);
if (!h || be32(h) != 0xd00dfeedu) return;
const u8* structure = h + be32(h + 8);
const char* strings = reinterpret_cast<const char*>(h + be32(h + 12));
const u32 size = be32(h + 36);
// The address cells each node states for its children, and what the node at each depth has
// stated. Each depth is set as its node begins, and not zeroed in a block: a program with no C
// library has no `memset' for the compiler to call.
u32 cells[kMaxDepth];
bool compatible[kMaxDepth];
u64 reg[kMaxDepth];
u32 depth = 0;
cells[0] = 2;
compatible[0] = false;
reg[0] = 0;
for (u32 at = 0; at + 4 <= size;) {
const u32 token = be32(structure + at);
at += 4;
if (token == kBeginNode) {
while (at < size && structure[at]) ++at;
at = (at + 4) & ~3u;
if (++depth >= kMaxDepth) return;
cells[depth] = 2;
compatible[depth] = false;
reg[depth] = 0;
} else if (token == kProp) {
// A length beyond the structure is a tree this reader does not trust.
if (at + 8 > size) return;
const u32 len = be32(structure + at), name = be32(structure + at + 4);
if (len > size - at - 8) return;
const u8* value = structure + at + 8;
at = (at + 8 + len + 3) & ~3u;
const char* n = strings + name;
if (same(n, "#address-cells") && len == 4) cells[depth] = be32(value);
else if (same(n, "compatible")) compatible[depth] = finisher_compatible(value, len);
else if (same(n, "reg") && depth > 0 && len >= 4 * cells[depth - 1])
reg[depth] = cells[depth - 1] == 2 ? (static_cast<u64>(be32(value)) << 32 | be32(value + 4))
: be32(value);
} else if (token == kEndNode) {
if (depth > 0 && compatible[depth] && reg[depth]) {
g_finisher = reinterpret_cast<volatile u32*>(static_cast<unsigned long>(reg[depth]));
return;
}
if (depth == 0) return;
--depth;
} else if (token != kNop) {
return; // the end of the structure, or a token this reader does not know
}
}
}

void okbsbi_finish(int status) {
if (!g_finisher) return;
const u32 code = static_cast<u32>(status) & 0xff;
*g_finisher = code == 0 ? 0x5555u : (code << 16) | 0x3333u;
}
7 changes: 7 additions & 0 deletions src/finisher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// The device a machine states for ending with a status (finisher.cpp, 0.10.1).
#pragma once

// Reads the device tree the firmware handed the program, once, at its start.
void okbsbi_find_finisher(const void* fdt);
// Ends the machine with `status' where the tree stated the device; returns where it stated none.
void okbsbi_finish(int status);
4 changes: 4 additions & 0 deletions src/kal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <openkal/memory.h>
#include <openkal/stream.h>

#include "finisher.h"
#include "sbi.h"

namespace {
Expand Down Expand Up @@ -163,6 +164,8 @@ KAL_NORETURN void kal_abort(const char* msg, kal_uintptr len) {
if (msg && len) write_all(reinterpret_cast<const unsigned char*>(msg), len);
const unsigned char nl = '\n';
write_all(&nl, 1);
// 134, as a program ended by SIGABRT reports itself on the systems that have the signal.
okbsbi_finish(134);
sbi_call(SBI_EXT_SRST, SBI_SRST_RESET,
SBI_SRST_TYPE_SHUTDOWN, SBI_SRST_REASON_FAILURE, 0);
// Reached only if the firmware declined to reset, which no conforming
Expand All @@ -171,6 +174,7 @@ KAL_NORETURN void kal_abort(const char* msg, kal_uintptr len) {
}

KAL_NORETURN void kal_exit(int code) {
okbsbi_finish(code);
sbi_call(SBI_EXT_SRST, SBI_SRST_RESET, SBI_SRST_TYPE_SHUTDOWN,
code == 0 ? SBI_SRST_REASON_NONE : SBI_SRST_REASON_FAILURE, 0);
for (;;) {}
Expand Down
11 changes: 8 additions & 3 deletions src/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// other does: a program written directly against openkal has no such symbol,
// and then this file runs the initialisers and calls `main' itself.
#ifdef OPENKAL_OPENSBI_STANDALONE
#include "finisher.h"

#include <openkal/abort.h>
#include <openkal/memory.h>
Expand All @@ -42,7 +43,7 @@ int main(int, char**, char**);
[[gnu::weak]] int __libc_start_main(int (*)(int, char**, char**), int, char**,
void (*)(), void (*)(), void (*)());

[[noreturn]] void __okb_start_c(void);
[[noreturn]] void __okb_start_c(unsigned long hart, const void* fdt);

}

Expand Down Expand Up @@ -175,7 +176,8 @@ extern "C" { void* __dso_handle = &__dso_handle; }

// The stack, then C. `la` of a linker-defined symbol resolves at link time, so
// the sequence needs nothing to have been set up before it runs --- which is
// the situation it is in.
// the situation it is in. The firmware's a0 and a1, the hart and the device
// tree, are left as they arrived: they are the C function's two arguments.
//
// In `.text.entry` rather than `.text`, because the linker script places
// that section first. Firmware jumps to the load address, not to `_start`: the
Expand All @@ -190,7 +192,10 @@ asm(".section .text.entry\n"
"1: j 1b\n"
".size _start,.-_start\n");

extern "C" [[noreturn]] void __okb_start_c(void) {
extern "C" [[noreturn]] void __okb_start_c(unsigned long, const void* fdt) {
// The device the machine states for ending with a status (finisher.cpp, 0.10.1).
okbsbi_find_finisher(fdt);

// Zero and null rather than a vector: openkal.env is what a program above
// this reads its arguments through, and this environment's answer there is
// that there are none. Passing the same answer here keeps the two from
Expand Down
Loading