forge
login
or
join now
awoo.ren
/
sandy-kernel
star
0
fork
atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
this repo has no description
star
0
fork
atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
overview
issues
pulls
pipelines
Init
roufpup
3 months ago
83ae6f3c
+269
15 changed files
expand all
collapse all
unified
split
.envrc
.gitignore
.zed
settings.json
kernel
.cargo
config.toml
.gitignore
Cargo.lock
Cargo.toml
build.rs
justfile
linker.ld
rust-toolchain.toml
src
kernel.s
main.rs
uart.rs
shell.nix
+1
.envrc
reviewed
···
1
1
+
use_nix
+3
.gitignore
reviewed
···
1
1
+
OpenSBI/
2
2
+
.direnv/
3
3
+
target/
+18
.zed/settings.json
reviewed
···
1
1
+
// Folder-specific settings
2
2
+
//
3
3
+
// For a full list of overridable settings, and general information on folder-specific settings,
4
4
+
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
5
5
+
{
6
6
+
"lsp": {
7
7
+
"rust-analyzer": {
8
8
+
"initialization_options": {
9
9
+
"cargo": {
10
10
+
"target": "riscv64gc-unknown-linux-gnu",
11
11
+
},
12
12
+
"check": {
13
13
+
"allTargets": false,
14
14
+
},
15
15
+
},
16
16
+
},
17
17
+
},
18
18
+
}
+15
kernel/.cargo/config.toml
reviewed
···
1
1
+
[build]
2
2
+
target = "riscv64gc-unknown-linux-gnu"
3
3
+
4
4
+
[target.riscv64gc-unknown-linux-gnu]
5
5
+
linker = "riscv64-unknown-linux-gnu-ld"
6
6
+
rustflags = [
7
7
+
"-C",
8
8
+
"link-arg=-static",
9
9
+
"-C",
10
10
+
"link-arg=-nostdlib",
11
11
+
"-C",
12
12
+
"relocation-model=static",
13
13
+
"-C",
14
14
+
"link-arg=-T./linker.ld",
15
15
+
]
+1
kernel/.gitignore
reviewed
···
1
1
+
/target
+32
kernel/Cargo.lock
reviewed
···
1
1
+
# This file is automatically @generated by Cargo.
2
2
+
# It is not intended for manual editing.
3
3
+
version = 4
4
4
+
5
5
+
[[package]]
6
6
+
name = "cc"
7
7
+
version = "1.2.30"
8
8
+
source = "registry+https://github.com/rust-lang/crates.io-index"
9
9
+
checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7"
10
10
+
dependencies = [
11
11
+
"shlex",
12
12
+
]
13
13
+
14
14
+
[[package]]
15
15
+
name = "fdt"
16
16
+
version = "0.1.5"
17
17
+
source = "registry+https://github.com/rust-lang/crates.io-index"
18
18
+
checksum = "784a4df722dc6267a04af36895398f59d21d07dce47232adf31ec0ff2fa45e67"
19
19
+
20
20
+
[[package]]
21
21
+
name = "kernel"
22
22
+
version = "0.1.0"
23
23
+
dependencies = [
24
24
+
"cc",
25
25
+
"fdt",
26
26
+
]
27
27
+
28
28
+
[[package]]
29
29
+
name = "shlex"
30
30
+
version = "1.3.0"
31
31
+
source = "registry+https://github.com/rust-lang/crates.io-index"
32
32
+
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
+19
kernel/Cargo.toml
reviewed
···
1
1
+
[package]
2
2
+
name = "kernel"
3
3
+
version = "0.1.0"
4
4
+
edition = "2024"
5
5
+
test = false
6
6
+
bench = false
7
7
+
8
8
+
[dependencies]
9
9
+
fdt = "0.1.5"
10
10
+
11
11
+
[build-dependencies]
12
12
+
cc = "1.2.30"
13
13
+
14
14
+
15
15
+
[profile.dev]
16
16
+
panic = "abort"
17
17
+
18
18
+
[profile.release]
19
19
+
panic = "abort"
+7
kernel/build.rs
reviewed
···
1
1
+
fn main() {
2
2
+
cc::Build::new()
3
3
+
.file("src/kernel.s")
4
4
+
.flag("-march=rv64gc")
5
5
+
.compiler("riscv64-unknown-linux-gnu-gcc")
6
6
+
.compile("boot");
7
7
+
}
+2
kernel/justfile
reviewed
···
1
1
+
run:
2
2
+
cargo build --release && qemu-system-riscv64 -nographic -machine virt -bios ../OpenSBI/build/platform/generic/firmware/fw_dynamic.elf -kernel ./target/riscv64gc-unknown-linux-gnu/release/kernel
+21
kernel/linker.ld
reviewed
···
1
1
+
ENTRY(_start)
2
2
+
3
3
+
SECTIONS {
4
4
+
. = 0x00000000801ff000;
5
5
+
.text : {
6
6
+
*(.text*)
7
7
+
}
8
8
+
9
9
+
.rodata : {
10
10
+
*(.rodata*)
11
11
+
}
12
12
+
13
13
+
.data : {
14
14
+
*(.data*)
15
15
+
}
16
16
+
17
17
+
.bss : {
18
18
+
*(.bss*)
19
19
+
*(COMMON)
20
20
+
}
21
21
+
}
+3
kernel/rust-toolchain.toml
reviewed
···
1
1
+
[toolchain]
2
2
+
targets = ["riscv64gc-unknown-linux-gnu"]
3
3
+
channel = "stable"
+17
kernel/src/kernel.s
reviewed
···
1
1
+
.section .text
2
2
+
.globl _start
3
3
+
_start:
4
4
+
# Set up stack pointer (adjust as you want)
5
5
+
la sp, stack_top
6
6
+
7
7
+
# Call rust_main (extern function)
8
8
+
call rust_main
9
9
+
10
10
+
# Infinite loop after kmain returns
11
11
+
1: j 1b
12
12
+
13
13
+
.section .bss
14
14
+
.align 12 # 2^12 = 4096 bytes stack alignment
15
15
+
.space 0x4000 # 16KB stack
16
16
+
17
17
+
stack_top:
+39
kernel/src/main.rs
reviewed
···
1
1
+
#![no_std]
2
2
+
#![no_main]
3
3
+
4
4
+
use core::panic::PanicInfo;
5
5
+
6
6
+
use fdt::Fdt;
7
7
+
8
8
+
pub mod uart;
9
9
+
10
10
+
// Debugging
11
11
+
const CLINT: usize = 0x0200_0000;
12
12
+
const MTIME: *const u64 = (CLINT + 0xBFF8) as *const u64;
13
13
+
14
14
+
pub fn read_mtime() -> u64 {
15
15
+
unsafe { core::ptr::read_volatile(MTIME) }
16
16
+
}
17
17
+
18
18
+
pub fn get_uart_address(device_tree: usize) {
19
19
+
// fdt::node::Fdt::
20
20
+
}
21
21
+
22
22
+
#[unsafe(no_mangle)]
23
23
+
pub extern "C" fn rust_main(_hartid: usize, _dtb: usize) -> ! {
24
24
+
uprintln!("Hello RiscV :3");
25
25
+
26
26
+
unsafe {
27
27
+
let fdt = Fdt::from_ptr(_dtb as *const u8).ok().unwrap();
28
28
+
}
29
29
+
30
30
+
#[allow(clippy::empty_loop)]
31
31
+
loop {}
32
32
+
}
33
33
+
34
34
+
#[panic_handler]
35
35
+
fn panic(_info: &PanicInfo) -> ! {
36
36
+
uprintln!("Woopsie we panicked :<");
37
37
+
38
38
+
loop {}
39
39
+
}
+50
kernel/src/uart.rs
reviewed
···
1
1
+
use core::fmt::{self, Write};
2
2
+
3
3
+
const UART: usize = 0x1000_0000;
4
4
+
const UART_STATUS: usize = UART + 0x5;
5
5
+
const MASK: u8 = 1 << 5;
6
6
+
7
7
+
pub struct Uart;
8
8
+
9
9
+
impl Uart {
10
10
+
pub fn write_byte(src: u8) {
11
11
+
unsafe {
12
12
+
let status = UART_STATUS as *mut u8;
13
13
+
let reg = UART as *mut u8;
14
14
+
15
15
+
while core::ptr::read_volatile(status) & MASK == 0 {}
16
16
+
17
17
+
core::ptr::write_volatile(reg, src);
18
18
+
}
19
19
+
}
20
20
+
21
21
+
pub fn print(args: fmt::Arguments) {
22
22
+
Uart.write_fmt(args).ok();
23
23
+
}
24
24
+
}
25
25
+
26
26
+
impl Write for Uart {
27
27
+
fn write_str(&mut self, s: &str) -> core::fmt::Result {
28
28
+
for byte in s.as_bytes() {
29
29
+
Self::write_byte(*byte);
30
30
+
}
31
31
+
Ok(())
32
32
+
}
33
33
+
}
34
34
+
35
35
+
#[macro_export]
36
36
+
macro_rules! uprint {
37
37
+
($($arg:tt)*) => {{
38
38
+
$crate::uart::Uart::print(core::format_args!($($arg)*));
39
39
+
}};
40
40
+
}
41
41
+
42
42
+
#[macro_export]
43
43
+
macro_rules! uprintln {
44
44
+
() => {
45
45
+
$crate::uprint!("\n")
46
46
+
};
47
47
+
($($arg:tt)*) => {{
48
48
+
uprint!("{}\n", core::format_args!($($arg)*));
49
49
+
}}
50
50
+
}
+41
shell.nix
reviewed
···
1
1
+
let
2
2
+
rust_overlay = import (
3
3
+
fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz"
4
4
+
);
5
5
+
6
6
+
pkgs = import <nixpkgs> {
7
7
+
overlays = [
8
8
+
rust_overlay
9
9
+
(_: prev: {
10
10
+
riscv-rust = prev.rust-bin.stable.latest.default.override {
11
11
+
extensions = [
12
12
+
"rust-src"
13
13
+
"rust-analyzer"
14
14
+
"clippy"
15
15
+
];
16
16
+
targets = [ "riscv64gc-unknown-linux-gnu" ];
17
17
+
};
18
18
+
})
19
19
+
];
20
20
+
};
21
21
+
22
22
+
riscv-toolchain = import <nixpkgs> {
23
23
+
localSystem = "x86_64-linux";
24
24
+
crossSystem = {
25
25
+
config = "riscv64-unknown-linux-gnu";
26
26
+
};
27
27
+
};
28
28
+
in
29
29
+
pkgs.mkShell {
30
30
+
nativeBuildInputs = with pkgs; [
31
31
+
git
32
32
+
riscv-toolchain.buildPackages.gcc
33
33
+
python312
34
34
+
dtc
35
35
+
riscv-rust
36
36
+
];
37
37
+
38
38
+
shellHook = ''
39
39
+
echo "`git --version`"
40
40
+
'';
41
41
+
}