Skip to content

Commit

Permalink
refactor: break up kstd crate into multiple smaller crates (#90)
Browse files Browse the repository at this point in the history
This change breaks up the monolithic `kstd` crate into multiple smaller crates (`panic`, `unwind`, `sync`, `backtrace`, `tls`, and `riscv`) that are easier to compose, work on and extend
  • Loading branch information
JonasKruckenberg authored Aug 23, 2024
1 parent 7d655d0 commit d6e7525
Show file tree
Hide file tree
Showing 145 changed files with 3,146 additions and 2,322 deletions.
363 changes: 157 additions & 206 deletions Cargo.lock

Large diffs are not rendered by default.

71 changes: 32 additions & 39 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
[workspace]
members = [
"libs/*",
"kernel",
"loader",
"loader/api",
"loader/api/macros",
"libs/ktest/macros", "build/lz4-block-compress",
]
members = ["kernel", "libs/*", "build/*", "loader", "loader/api", "loader/api/macros", "libs/ktest/macros", "libs/kconfig-declare/macros"]
resolver = "2"

[workspace.package]
Expand All @@ -22,41 +15,47 @@ large_stack_arrays = "deny"
recursive_format_impl = "deny"

[workspace.dependencies]
dtb-parser = { path = "libs/dtb-parser" }
uart-16550 = { path = "libs/uart-16550" }
vmm = { path = "libs/vmm" }
loader-api = { path = "loader/api" }
kstd = { path = "libs/kstd" }
backtrace = { path = "libs/backtrace" }
dtb-parser = { path = "libs/dtb-parser" }
kconfig-declare = { path = "libs/kconfig-declare" }
kmm = { path = "libs/kmm" }
ktest = { path = "libs/ktest" }
wast = { path = "libs/wast" }
wat = { path = "libs/wat" }
leb128 = { path = "libs/leb128" }
panic = { path = "libs/panic" }
riscv = { path = "libs/riscv" }
semihosting-logger = { path = "libs/semihosting-logger" }
sync = { path = "libs/sync" }
tls = { path = "libs/tls" }
unwind = { path = "libs/unwind" }
wasm-encoder = { path = "libs/wasm-encoder" }
wast = { path = "libs/wast" }
wat = { path = "libs/wat" }

onlyerror = { version = "0.1.4", default-features = false }
cfg-if = "1.0.0"
lock_api = "0.4.12"
log = "0.4.21"
bitflags = "2.6.0"
cfg-if = "1.0.0"
linked_list_allocator = "0.10.5"
onlyerror = { version = "0.1.4", default-features = false }
arrayvec = { version = "0.7.4", default-features = false }
ed25519-dalek = { version = "2.1.1", default-features = false }
object = { version = "0.36.0", default-features = false }
lz4_flex = { version = "0.11", default-features = false }
lock_api = "0.4.12"
object = { version = "0.36.0", default-features = false }
gimli = { version = "0.31.0", default-features = false, features = ["read"] }
linked_list_allocator = "0.10.5"
talc = "4.4.1"
#ed25519-dalek = { version = "2.1.1", default-features = false, features = ["fast"] }

# wast dependencies
bumpalo = "3.14.0"
unicode-width = { version = "0.1.9" }
memchr = { version = "2.4.1", default-features = false }
bumpalo = "3.14.0"
rustc-demangle = "0.1.24"
postcard = { version = "1.0.8", default-features = false, features = ["alloc"] }
talc = "4.4.1"
gimli = { version = "0.31.0", default-features = false, features = ["read"] }

# cranelift dependencies
hashbrown = { version = "0.14.5", default-features = false, features = [
"inline-more",
"nightly",
] }
wasmparser = { version = "0.214.0", default-features = false }
wasmparser = { version = "0.215.0", default-features = false }
target-lexicon = { version = "0.12.14", default-features = false }
cranelift-wasm = { git = "https://github.com/JonasKruckenberg/wasmtime", branch = "no_std2", default-features = false, features = [
"core",
Expand All @@ -71,16 +70,10 @@ cranelift-frontend = { git = "https://github.com/JonasKruckenberg/wasmtime", bra
cranelift-entity = { git = "https://github.com/JonasKruckenberg/wasmtime", branch = "no_std2", default-features = false }

# build dependencies
serde = { version = "1.0.203", default-features = false, features = ["derive"] }
rand_core = { version = "0.6.4", features = ["getrandom"] }
clap = { version = "4.5.7", features = ["derive"] }
env_logger = "0.11.3"
anstyle = "1.0.7"
cargo_metadata = "0.18.1"
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }
toml = "0.8.14"
toml = "0.8.19"

[profile.release]
opt-level = 3
Expand All @@ -90,14 +83,14 @@ strip = true
overflow-checks = true
split-debuginfo = "packed"

[profile.dev.package.ed25519-dalek]
opt-level = 3

[profile.test.package.ed25519-dalek]
opt-level = 3
#[profile.dev.package.ed25519-dalek]
#opt-level = 3
#
#[profile.test.package.ed25519-dalek]
#opt-level = 3

[profile.dev.package.lz4_flex]
opt-level = 3

[profile.test.package.lz4_flex]
opt-level = 3
opt-level = 3
File renamed without changes.
11 changes: 10 additions & 1 deletion configs/riscv64-qemu.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
runner = "qemu-system-riscv64"
memory-mode = "Riscv64Sv39"
wasm-features = ["a", "b"]

[kernel]
target = "./configs/riscv64gc-k23-none-kernel.json"
target-triple = "riscv64gc-k23-none-kernel"
stack-size-pages = 256
trap-stack-size-pages = 16
heap-size-pages = 8192
log-level = "trace"

[loader]
target = "./configs/riscv64imac-k23-none-loader.json"
target-triple = "riscv64imac-k23-none-loader"
target-triple = "riscv64imac-k23-none-loader"
linker-script = "./configs/loader-riscv64-qemu.ld"
stack-size-pages = 32
log-level = "trace"
3 changes: 2 additions & 1 deletion configs/riscv64gc-k23-none-kernel.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"llvm-target": "riscv64",
"max-atomic-width": 64,
"panic-strategy": "unwind",
"relocation-model": "pie",
"relocation-model": "pic",
"tls-model": "local-exec",
"supported-sanitizers": [
"kernel-address"
],
Expand Down
2 changes: 0 additions & 2 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ allow = [
"Apache-2.0", # requires license notice
"Unicode-DFS-2016", # requires license notice
"Apache-2.0 WITH LLVM-exception",
"BSD-3-Clause", # requires license notice and link to source code TODO allow?
]

[sources]
allow-git = [
"https://github.com/JonasKruckenberg/wasmtime?branch=no_std", # Cranelift no_std fork
"https://github.com/JonasKruckenberg/HdrHistogram_rust" # hdrhistogram no_std fork
]
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
just
nushell
cargo-binutils
mdbook
lz4
openssl
coreutils
Expand Down
45 changes: 21 additions & 24 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ lint config *FLAGS: (clippy config FLAGS) (check-fmt FLAGS)
clippy config $RUSTFLAGS='-Dwarnings' *CARGO_ARGS='':
#!/usr/bin/env nu
let config = open {{config}}
$env.K23_CONFIG = {{config}}

# check kernel and dependencies
({{_cargo}} clippy
-p kernel
--target $config.kernel.target
--tests
--benches
--profile {{profile}}
{{_buildstd}}
{{CARGO_ARGS}})
Expand All @@ -57,8 +56,6 @@ clippy config $RUSTFLAGS='-Dwarnings' *CARGO_ARGS='':
({{_cargo}} clippy
-p loader
--target $config.loader.target
--tests
--benches
--profile {{profile}}
{{_buildstd}}
{{CARGO_ARGS}})
Expand All @@ -67,13 +64,12 @@ clippy config $RUSTFLAGS='-Dwarnings' *CARGO_ARGS='':
check config $RUSTFLAGS='' *CARGO_ARGS='':
#!/usr/bin/env nu
let config = open {{config}}
$env.K23_CONFIG = {{config}}

# check kernel and dependencies
({{_cargo}} check
-p kernel
--target $config.kernel.target
--tests
--benches
--profile {{profile}}
{{_buildstd}}
{{CARGO_ARGS}})
Expand All @@ -82,8 +78,6 @@ check config $RUSTFLAGS='' *CARGO_ARGS='':
({{_cargo}} check
-p loader
--target $config.loader.target
--tests
--benches
--profile {{profile}}
{{_buildstd}}
{{CARGO_ARGS}})
Expand All @@ -99,14 +93,14 @@ run config CARGO_ARGS="" *ARGS="": (build config CARGO_ARGS) (_run config "targe
build config *CARGO_ARGS="": && (_make_bootimg config "target/k23/payload" CARGO_ARGS)
#!/usr/bin/env nu
let config = open {{config}}
let target = try { $config | get kernel.target } catch { $config | get target }
$env.K23_CONFIG = {{config}}

let out_dir = "{{_target_dir}}" | path join "k23"
mkdir $out_dir

let cargo_out = ({{_cargo}} build
-p kernel
--target $target
--target $config.kernel.target
--profile {{profile}}
--message-format=json
{{_buildstd}}
Expand All @@ -117,13 +111,15 @@ build config *CARGO_ARGS="": && (_make_bootimg config "target/k23/payload" CARGO
test config *CARGO_ARGS="" :
#!/usr/bin/env nu
let config = open {{config}}
let target = try { $config.kernel.target } catch { $config.target }
$env.K23_CONFIG = {{config}}

#let target = try { } catch { $config.target }
let triple = try { $config.kernel.target-triple } catch { $config.target-triple }

# CARGO_TARGET_<triple>_RUNNER
$env.CARGO_TARGET_RISCV64GC_K23_NONE_KERNEL_RUNNER = "just profile={{profile}} _runner {{config}}"

{{ _cargo }} test -p kernel --target $target {{ _buildstd }} {{ CARGO_ARGS }}
{{ _cargo }} test -p kernel --target $config.kernel.target {{ _buildstd }} {{ CARGO_ARGS }}

# This is a helper recipe designed to be used as a cargo *target runner*
# When running tests, the `cargo test` command will produce potentially many executables.
Expand All @@ -137,6 +133,8 @@ _runner config binary *ARGS: (_make_bootimg config binary) (_run config "target/
_run config binary *ARGS:
#!/usr/bin/env nu
let config = open {{ config }}
$env.K23_CONFIG = {{config}}

let runner = $config.runner

let cpu = match $runner {
Expand Down Expand Up @@ -173,15 +171,15 @@ _run config binary *ARGS:
_make_bootimg config payload *CARGO_ARGS="":
#!/usr/bin/env nu
let config = open {{config}}
let target = try { $config.loader.target } catch { $config.target }
$env.K23_CONFIG = {{config}}

let out_dir = "{{_target_dir}}" | path join "k23"
mkdir $out_dir

let loader_path = ($out_dir | path join loader)
let secret_key_path = ($out_dir | path join secret.der)
let public_key_path = ($out_dir | path join pubkey.bin)
let signature_path = ($out_dir | path join signature.bin)
#let secret_key_path = ($out_dir | path join secret.der)
#let public_key_path = ($out_dir | path join pubkey.bin)
#let signature_path = ($out_dir | path join signature.bin)
let bootimg_path = ($out_dir | path join bootimg.bin)

# Step 1: Compress the payload
Expand All @@ -190,25 +188,24 @@ _make_bootimg config payload *CARGO_ARGS="":
{{_cargo}} run -p lz4-block-compress {{payload}} $payload_lz4_path

# Step 2: Sign the compressed payload
print "Signing the compressed payload..."
#print "Signing the compressed payload..."
# Write ed25519 key pair
echo "{{_signing_key}}" | openssl pkey -outform DER -out $secret_key_path
#echo "_signing_key" | openssl pkey -outform DER -out $secret_key_path
# Do the actual signing
openssl pkeyutl -sign -inkey $secret_key_path -out $signature_path -rawin -in $payload_lz4_path
#openssl pkeyutl -sign -inkey $secret_key_path -out $signature_path -rawin -in $payload_lz4_path
# Extract the 32-byte public key
openssl pkey -in $secret_key_path -pubout -outform DER | tail -c 32 | save -f $public_key_path
#openssl pkey -in $secret_key_path -pubout -outform DER | tail -c 32 | save -f $public_key_path

# Assign environment variables so we can pick it up in the loader build script
$env.K23_VERIFYING_KEY_PATH = $public_key_path
$env.K23_SIGNATURE_PATH = $signature_path
#$env.K23_VERIFYING_KEY_PATH = $public_key_path
#$env.K23_SIGNATURE_PATH = $signature_path
$env.K23_PAYLOAD_PATH = $payload_lz4_path
$env.K23_PAYLOAD_SIZE = (stat -c %s {{payload}})

# Step 3: Build the bootloader
print "Building the bootloader..."
let cargo_out = ({{_cargo}} build
-p loader
--target $target
--target $config.loader.target
--profile {{profile}}
--message-format=json
{{_buildstd}}
Expand Down
Loading

0 comments on commit d6e7525

Please sign in to comment.