Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab 6 and 7 109550083 #288

Open
wants to merge 6 commits into
base: 109550083
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.release]
strip = "symbols" # This strips both symbol table (symtab) and string table (strtab) along with debugging information.
# strip = true # This strips both symbol table (symtab) and string table (strtab) along with debugging information.


[dependencies]
3 changes: 2 additions & 1 deletion kernel/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mod registers;
pub mod uart;
pub mod mailbox;
pub mod reboot;
mod device_tree;
mod device_tree;
pub mod mmu;
35 changes: 21 additions & 14 deletions kernel/src/cpu/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::cpu::{device_tree::DeviceTree, mailbox, uart};
use crate::os::stdio::{print_hex_now, println_now};
use crate::os::{allocator, shell, thread, timer};
use crate::println;
use alloc::boxed::Box;
use core::arch::{asm, global_asm};
use crate::os::file_system::{vfs, tmpfs, initramfs};

global_asm!(include_str!("boot.s"));

Expand All @@ -13,21 +13,27 @@ pub unsafe fn _start_rust() {
timer::init();
thread::init();
allocator::init();
allocator::reserve(0x0000_0000 as *mut u8, 0x0000_1000); // Device reserved memory
allocator::reserve(0x0003_0000 as *mut u8, 0x0004_0000); // Stack
allocator::reserve(0x0007_5000 as *mut u8, 0x0000_0004); // CS counter
allocator::reserve(0x0007_5100 as *mut u8, 0x0000_0004); // device tree address
allocator::reserve(0x0008_0000 as *mut u8, 0x0008_0000); // Code
allocator::reserve(0x0800_0000 as *mut u8, 0x0010_0000); // Initramfs
allocator::reserve(0xFFFF_0000_0000_2000 as *mut u8, 0x3000);
allocator::reserve(0xFFFF_0000_0000_0000 as *mut u8, 0x0000_1000); // Device reserved memory
allocator::reserve(0xFFFF_0000_0003_0000 as *mut u8, 0x0004_0000); // Stack
allocator::reserve(0xFFFF_0000_0007_5000 as *mut u8, 0x0000_0004); // CS counter
allocator::reserve(0xFFFF_0000_0007_5100 as *mut u8, 0x0000_0004); // device tree address
allocator::reserve(0xFFFF_0000_0008_0000 as *mut u8, 0x0008_0000); // Code
allocator::reserve(0xFFFF_0000_0800_0000 as *mut u8, 0x0010_0000); // Initramfs
allocator::reserve(DeviceTree::get_address(), 0x0100_0000); // Device Tree
allocator::reserve(0x0880_0000 as *mut u8, 0x0780_0000); // Simple Allocator

allocator::reserve(0xFFFF_0000_0880_0000 as *mut u8, 0x0780_0000); // Simple Allocator
// Enable interrupts
asm!("msr DAIFClr, 0xf");

vfs::register_fs(tmpfs::init());
vfs::mount("tmpfs", "/");
vfs::register_fs(initramfs::init());
vfs::mount("initramfs", "/initramfs/");

let dt = DeviceTree::init();

println_now("Device tree initialized");
// println_now("Device tree initialized");

let initrd_start = match dt.get("linux,initrd-start") {
Some(v) => {
Expand All @@ -37,7 +43,7 @@ pub unsafe fn _start_rust() {
}
val = val.swap_bytes();
println!("Initrd start address: {:#X}", val);
val
val as u64 + 0xFFFF_0000_0000_0000
}
None => {
println!("No initrd");
Expand All @@ -46,12 +52,13 @@ pub unsafe fn _start_rust() {
};
// let initrd_start = 0x8000000;

println_now("before print information");
// println_now("before print information");

print_information();

shell::start(initrd_start);
loop {}

panic!("Shell stoped.");
}

fn print_information() {
Expand All @@ -61,5 +68,5 @@ fn print_information() {
let memory = mailbox::get(mailbox::MailboxTag::GetArmMemory);
println!("Memory base: {:#010X}", memory.0);
println!("Memory size: {:#010X}", memory.1);
println!("Boot time: {} ms", timer::get_time_ms());
// println!("Boot time: {} ms", timer::get_time_ms());
}
52 changes: 28 additions & 24 deletions kernel/src/cpu/boot.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.section .text._start

_stack_start = 0x70000
_STACK_START = 0xffff000000070000
_CS_COUNTER_ADDRESS = 0x75000
_DEVICE_TREE_ADDRESS = 0x75100

.global _start
.global _bss_start
Expand All @@ -12,23 +14,35 @@ _stack_start = 0x70000
.endm

_start:
# Store the address of the device tree
# Should be removed when building for a real hardware
ldr x1, =0x75100
ldr x1, =_DEVICE_TREE_ADDRESS
str x0, [x1]

mrs x0, mpidr_el1
cbz x0, .L_parking_loop
// Clear CS counter
ldr x0, =_CS_COUNTER_ADDRESS
str xzr, [x0]

// Change EL
bl .from_el2_to_el1

# Initailize stack pointer
ldr x0, =_stack_start
mrs x0, CurrentEL
cmp x0, #0x4 // Check if we are in EL1
bne .from_el2_to_el1

// Initailize stack pointer
ldr x0, =0x70000
mov sp, x0

# CS counter
ldr x0, =0x75000
str xzr, [x0]
bl setup_mmu

ldr x1, = boot_rest // indirect branch to the virtual address
br x1

boot_rest:
// Initailize stack pointer
ldr x0, =_STACK_START
mov sp, x0

# Initialize bss
// Initialize bss
ADR_REL x0, _bss_start
ADR_REL x1, _bss_end
.L_clear_bss:
Expand All @@ -38,17 +52,11 @@ _start:
b .L_clear_bss
.L_done_clearing:

bl .from_el2_to_el1

mrs x0, CurrentEL
cmp x0, #0x4 // Check if we are in EL1
bne .from_el2_to_el1

.set_exception_vector_table:
// Initialize exception vector table
adr x0, exception_vector_table
msr vbar_el1, x0

# Call rust main function
// Call rust main function
b _start_rust

.from_el2_to_el1:
Expand All @@ -61,9 +69,5 @@ _start:
msr sp_el1, x0
eret // return to EL1

.L_parking_loop:
wfe
b .L_parking_loop

.size _start, . - _start
.type _start, function
12 changes: 6 additions & 6 deletions kernel/src/cpu/device_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Default for DeviceTree {
}

impl DeviceTree {
const DEVICE_TREE_ADDRESS: *const u32 = 0x75100 as *const u32;
const DEVICE_TREE_ADDRESS: *const u64 = 0x75100 as *const u64;
const FDT_BEGIN_NODE: u32 = 0x00000001;
const FDT_END_NODE: u32 = 0x00000002;
const FDT_PROP: u32 = 0x00000003;
Expand All @@ -124,12 +124,12 @@ impl DeviceTree {
}

pub fn get_address() -> *mut u8 {
unsafe { *(DeviceTree::DEVICE_TREE_ADDRESS) as *mut u8 }
(unsafe { *(DeviceTree::DEVICE_TREE_ADDRESS) } | 0xFFFF_0000_0000_0000) as *mut u8
}

pub fn init() -> DeviceTree {
let mut ret = DeviceTree {
device_tree_ptr: unsafe { *(DeviceTree::DEVICE_TREE_ADDRESS) } as *const u8,
device_tree_ptr: DeviceTree::get_address(),
..Default::default()
};

Expand Down Expand Up @@ -194,9 +194,9 @@ impl DeviceTree {
}

fn construct_node(&self, now_node: &mut Node, offset: usize, depth: u32) -> usize {
// println("Constructing node");
// print_dec(offset as u32);
// print_dec(depth);
// println_now("Constructing node");
// print_hex_now(offset as u32);
// print_hex_now(depth);
let mut i = offset;
loop {
// unsafe {
Expand Down
18 changes: 12 additions & 6 deletions kernel/src/cpu/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use core::{
ptr::{read_volatile, write_volatile},
usize,
};
use alloc::format;

const MAILBOX_BASE: u32 = 0x3F00_B880;
const MAILBOX_READ: u32 = MAILBOX_BASE + 0x00;
const MAILBOX_STATUS: u32 = MAILBOX_BASE + 0x18;
const MAILBOX_WRITE: u32 = MAILBOX_BASE + 0x20;
const MAILBOX_BASE: usize = 0xFFFF_0000_3F00_B880;
const MAILBOX_READ: usize = MAILBOX_BASE + 0x00;
const MAILBOX_STATUS: usize = MAILBOX_BASE + 0x18;
const MAILBOX_WRITE: usize = MAILBOX_BASE + 0x20;

const MAILBOX_EMPTY: u32 = 0x4000_0000;
const MAILBOX_FULL: u32 = 0x8000_0000;
Expand All @@ -27,9 +28,12 @@ pub enum MailboxTag {

#[inline(never)]
pub fn mailbox_call(channel: u8, mailbox: *mut u32) {
let mut mailbox_addr = mailbox as u32;
let mut mailbox_addr = mailbox.mask(0xFFFF_FFFF) as u32;
mailbox_addr = mailbox_addr & !0xF | (channel as u32 & 0xF);

// println_now(format!("MBOX_ADDR {:x}", mailbox as usize).as_str());
// println_now(format!("MBOX_ADDR {:x}", mailbox_addr).as_str());

unsafe {
while (read_volatile(MAILBOX_STATUS as *const u32) & MAILBOX_FULL) != 0 {}
write_volatile(MAILBOX_WRITE as *mut u32, mailbox_addr);
Expand Down Expand Up @@ -61,6 +65,8 @@ pub fn get(tag: MailboxTag) -> (u32, u32) {
unsafe {
let mailbox_ptr = mailbox.as_mut_ptr().add(start_idx);

// println_now(format!("MBOX_ADDR {:x}", mailbox_ptr as usize).as_str());

match tag {
MailboxTag::GetBoardRevision => {
write_volatile(mailbox_ptr.add(0), 7 * 4); // buffer size in bytes
Expand All @@ -77,7 +83,7 @@ pub fn get(tag: MailboxTag) -> (u32, u32) {
write_volatile(mailbox_ptr.add(5), 0); // value buffer
write_volatile(mailbox_ptr.add(6), END_TAG);

mailbox_call(8, mailbox.as_mut_ptr().add(start_idx) as *mut u32);
mailbox_call(8, mailbox_ptr);
}

match tag {
Expand Down
Loading