Skip to content

Commit

Permalink
Add platform configs
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Mar 25, 2022
1 parent 8c40da5 commit f07c3e3
Show file tree
Hide file tree
Showing 40 changed files with 338 additions and 292 deletions.
17 changes: 3 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@ name: Build CI
on: [push, pull_request]

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2022-02-22
override: true
components: rustfmt
- name: Check code format
run: cd kernel && cargo fmt -- --check

clippy:
runs-on: ubuntu-latest
strategy:
Expand All @@ -29,9 +16,11 @@ jobs:
profile: minimal
toolchain: nightly-2022-02-22
override: true
components: rust-src, clippy
components: rust-src, clippy, rustfmt
- name: Clippy
run: make -C kernel clippy ARCH=${{ matrix.arch }}
- name: Check code format
run: cd kernel && cargo fmt -- --check

build:
runs-on: ${{ matrix.os }}
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
target
!kernelCargo.lock
!kernel/Cargo.lock
kernel/.makeargs
kernel/linker.ld
kernel/src/platform/config.rs
.idea
.vscode
.DS_Store
17 changes: 17 additions & 0 deletions kernel/Cargo.lock

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

5 changes: 5 additions & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
[features]
rvm = []
platform-pc = []
platform-pc-rvm = []
platform-qemu-virt-arm = []
default = ["platform-pc"]

Expand All @@ -29,3 +30,7 @@ raw-cpuid = "10.3"
[target.'cfg(target_arch = "aarch64")'.dependencies]
tock-registers = "0.7"
cortex-a = "7.0"

[build-dependencies]
toml = "0.5"
serde = "1.0"
24 changes: 15 additions & 9 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@
ARCH ?= x86_64
MODE ?= release
LOG ?= warn
RVM ?= on
RVM ?= off

# Platform
ifeq ($(ARCH), x86_64)
PLATFORM ?= pc
ifeq ($(RVM), on)
PLATFORM ?= pc-rvm
else
PLATFORM ?= pc
endif
else ifeq ($(ARCH), aarch64)
PLATFORM ?= qemu-virt-arm
endif

export LOG
export ARCH
export PLATFORM
export MODE
export LOG

make_args := ARCH=$(ARCH) PLATFORM=$(PLATFORM) MODE=$(MODE) LOG=$(LOG)

# Paths
target := ../targets/$(ARCH).json
Expand All @@ -23,10 +30,6 @@ kernel_bin := $(kernel_elf).bin
# Cargo features and build args
features := platform-$(PLATFORM)

ifeq ($(RVM), on)
features += rvm
endif

build_args := --no-default-features --features "$(features)" --target $(target) -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem
ifeq ($(MODE), release)
build_args += --release
Expand Down Expand Up @@ -70,8 +73,11 @@ $(kernel_bin): kernel
user:
@cd ../user && make build

kernel: user
kernel:
@echo Arch: $(ARCH), Platform: $(PLATFORM)
ifneq ($(shell cat .makeargs), $(make_args))
@echo $(make_args) > .makeargs
endif
cargo build $(build_args)

clean:
Expand All @@ -84,7 +90,7 @@ clippy:
disasm:
@$(OBJDUMP) $(kernel_elf) | less

run: build justrun
run: user build justrun

justrun:
$(qemu) $(qemu_args)
Expand Down
73 changes: 69 additions & 4 deletions kernel/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,77 @@ use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=../user/c/src");
println!("cargo:rerun-if-changed=../user/rust/src");
insert_app_data().unwrap();
}
println!("cargo:rerun-if-changed=.makeargs");

fn insert_app_data() -> Result<()> {
let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let app_path = PathBuf::from("../user/build/").join(&arch);
let platform = if cfg!(feature = "platform-pc") {
"pc"
} else if cfg!(feature = "platform-pc-rvm") {
"pc-rvm"
} else if cfg!(feature = "platform-qemu-virt-arm") {
"qemu-virt-arm"
} else {
panic!("Unsupported platform!");
};

parse_platform_config(&arch, platform).unwrap();
link_app_data(&arch).unwrap();
}

fn parse_platform_config(arch: &str, platform: &str) -> Result<()> {
// Load config file
let config_path = PathBuf::from("platforms").join(format!("{}.toml", platform));
println!("Reading config file: {}", config_path.display());
let config_content = std::fs::read_to_string(config_path)?;
let config: toml::Value = toml::from_str(&config_content)?;

// Generate config.rs
let mut out_file = File::create("src/platform/config.rs")?;
writeln!(out_file, "// Generated by build.rs, DO NOT edit!")?;
writeln!(out_file, "#![allow(dead_code)]\n")?;
let mut write_config = |key: &str| -> Result<()> {
let toml_key = key.to_lowercase().replace('_', "-");
if let Some(v) = config[toml_key].as_str() {
writeln!(out_file, "pub const {}: usize = {};", key, v)?;
}
Ok(())
};
write_config("PHYS_MEMORY_BASE")?;
write_config("PHYS_MEMORY_SIZE")?;
write_config("KERNEL_BASE_PADDR")?;
write_config("KERNEL_BASE_VADDR")?;

writeln!(out_file, "#[rustfmt::skip]")?;
writeln!(out_file, "pub const MMIO_REGIONS: &[(usize, usize)] = &[")?;
if let Some(regions) = config["mmio-regions"].as_array() {
for r in regions {
let r = r.as_array().unwrap();
writeln!(
out_file,
" ({}, {}),",
r[0].as_str().unwrap(),
r[1].as_str().unwrap()
)?;
}
}
writeln!(out_file, "];")?;

// Update linker.ld
let kernel_base_vaddr = config["kernel-base-vaddr"]
.as_str()
.unwrap()
.replace('_', "");
let ld_content = std::fs::read_to_string("linker.lds")?;
let ld_content = ld_content
.replace("%ARCH%", arch)
.replace("%KERNEL_BASE%", &kernel_base_vaddr);
std::fs::write("linker.ld", ld_content)?;

Ok(())
}

fn link_app_data(arch: &str) -> Result<()> {
let app_path = PathBuf::from("../user/build/").join(arch);
let link_app_path = PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("link_app.S");

if let Ok(dir) = read_dir(&app_path) {
Expand Down
6 changes: 4 additions & 2 deletions kernel/src/arch/aarch64/linker.ld → kernel/linker.lds
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ENTRY(_start)
BASE_ADDRESS = 0xffff000040080000;
OUTPUT_ARCH(%ARCH%)

BASE_ADDRESS = %KERNEL_BASE%;

ENTRY(_start)
SECTIONS
{
. = BASE_ADDRESS;
Expand Down
8 changes: 8 additions & 0 deletions kernel/platforms/pc-rvm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
phys-memory-base = "0x4200_0000"
phys-memory-size = "0x800_0000" # 128M
kernel-base-paddr = "0x4200_0000"
kernel-base-vaddr = "0xffff_ff80_4200_0000"
mmio-regions = [
["0xfec00000", "0x1000"], # IO APIC
["0xfee00000", "0x1000"], # Local APIC
]
8 changes: 8 additions & 0 deletions kernel/platforms/pc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
phys-memory-base = "0"
phys-memory-size = "0x800_0000" # 128M
kernel-base-paddr = "0x20_0000"
kernel-base-vaddr = "0xffff_ff80_0020_0000"
mmio-regions = [
["0xfec0_0000", "0x1000"], # IO APIC
["0xfee0_0000", "0x1000"], # Local APIC
]
8 changes: 8 additions & 0 deletions kernel/platforms/qemu-virt-arm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
phys-memory-base = "0x4000_0000"
phys-memory-size = "0x800_0000" # 128M
kernel-base-paddr = "0x4008_0000"
kernel-base-vaddr = "0xffff_0000_4008_0000"
mmio-regions = [
["0x0900_0000", "0x1000"], # PL011 UART
["0x0800_0000", "0x2_0000"], # GICv2
]
6 changes: 6 additions & 0 deletions kernel/src/arch/aarch64/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const USER_ASPACE_BASE: usize = 0;
pub const USER_ASPACE_SIZE: usize = 0xffff_ffff_f000;
pub const KERNEL_ASPACE_BASE: usize = 0xffff_0000_0000_0000;
pub const KERNEL_ASPACE_SIZE: usize = 0x0000_ffff_ffff_f000;

pub const PHYS_VIRT_OFFSET: usize = 0xffff_0000_0000_0000;
1 change: 1 addition & 0 deletions kernel/src/arch/aarch64/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn flush_icache_all() {
unsafe { asm!("ic iallu; dsb sy; isb") };
}

#[allow(dead_code)]
pub fn wait_for_ints() {
cortex_a::asm::wfi();
}
1 change: 1 addition & 0 deletions kernel/src/arch/aarch64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod page_table;
mod percpu;
mod trap;

pub mod config;
pub mod instructions;

pub use self::context::{TaskContext, TrapFrame};
Expand Down
6 changes: 6 additions & 0 deletions kernel/src/arch/x86_64/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const USER_ASPACE_BASE: usize = 0;
pub const USER_ASPACE_SIZE: usize = 0x7fff_ffff_f000;
pub const KERNEL_ASPACE_BASE: usize = 0xffff_ff80_0000_0000;
pub const KERNEL_ASPACE_SIZE: usize = 0x0000_007f_ffff_f000;

pub const PHYS_VIRT_OFFSET: usize = 0xffff_ff80_0000_0000;
1 change: 0 additions & 1 deletion kernel/src/arch/x86_64/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub(super) struct GdtStruct {
table: &'static mut [u64],
}

#[allow(dead_code)]
impl GdtStruct {
pub fn alloc() -> Self {
Self {
Expand Down
53 changes: 0 additions & 53 deletions kernel/src/arch/x86_64/linker.ld

This file was deleted.

1 change: 1 addition & 0 deletions kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod percpu;
mod syscall;
mod trap;

pub mod config;
pub mod instructions;

pub use self::context::{TaskContext, TrapFrame};
Expand Down
2 changes: 2 additions & 0 deletions kernel/src/arch/x86_64/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use super::idt::IDT;
use crate::mm::VirtAddr;
use crate::percpu::PERCPU_ARCH_OFFSET;

#[allow(dead_code)]
pub(super) const PERCPU_USER_RSP_OFFSET: usize =
PERCPU_ARCH_OFFSET + offset_of!(ArchPerCpu, saved_user_rsp);

#[allow(dead_code)]
pub(super) const PERCPU_KERNEL_RSP_OFFSET: usize = PERCPU_ARCH_OFFSET
+ offset_of!(ArchPerCpu, tss)
+ offset_of!(TaskStateSegment, privilege_stack_table);
Expand Down
5 changes: 5 additions & 0 deletions kernel/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pub use crate::arch::config::*;
pub use crate::platform::config::*;

pub const PHYS_MEMORY_END: usize = PHYS_MEMORY_BASE + PHYS_MEMORY_SIZE;

pub const BOOT_KERNEL_STACK_SIZE: usize = 4096 * 4; // 16K
pub const USER_STACK_SIZE: usize = 4096 * 4; // 16K
pub const USER_STACK_BASE: usize = 0x7fff_0000_0000 - USER_STACK_SIZE;
Expand Down
Loading

0 comments on commit f07c3e3

Please sign in to comment.