Skip to content

Commit

Permalink
usb-layer poc
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbanSeurat committed May 16, 2022
1 parent 9542758 commit c34e0b6
Show file tree
Hide file tree
Showing 54 changed files with 2,392 additions and 247 deletions.
27 changes: 16 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[workspace]
members = [
"usb",
"mmio",
"shared",
"init",
"program",
"kernel",
"boot",
]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TOPTARGETS := all clean

SUBDIRS := mmio shared program init kernel boot
SUBDIRS := mmio usb shared init kernel boot

$(TOPTARGETS): $(SUBDIRS)
$(SUBDIRS):
Expand Down
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ EC : Exception Class

- Disassemble stripped version

```aarch64-none-elf-objdump -b binary -maarch64 -D program.img```
```aarch64-none-elf-objdump -b binary -maarch64 -D init.img```

- Disassemble version with symbol

Expand Down
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# TODO

- Improve Framebuffer - dual or triple buffering
- Move Framebuffer memory and DMA into kernel memory
- Refactorize Process and scheduler
- Remove tmp programs
- Implements fork/exec syscalls
- Improve mbox interface ('DMA' only)
Binary file modified init.img
Binary file not shown.
2 changes: 1 addition & 1 deletion init/.cargo/config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[target.aarch64-unknown-none]
rustflags = [
"-C", "link-arg=-Tprogram/link.ld",
"-C", "link-arg=-Tinit/link.ld",
"-C", "target-feature=-fp-armv8",
"-C", "target-cpu=cortex-a53",
]
2 changes: 1 addition & 1 deletion init/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub unsafe extern "C" fn _main() -> () {

let mut count:u128 = 0;
loop {
if count % 10000 == 0 {
if count % 1000000 == 0 {
println!("init program run at level {}", count);
}
count = count + 1;
Expand Down
Binary file modified kernel-high.img
Binary file not shown.
1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"

[dependencies]
mmio = { path = "../mmio" }
usb = { path = "../usb" }
shared = { path = "../shared" }
qemu-exit = "1.0.0"
cortex-a = { git = "https://github.com/AlbanSeurat/cortex-a", branch = "master" }
Expand Down
1 change: 0 additions & 1 deletion kernel/src/exceptions/interruptions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::global::{BCMDEVICES, SCHEDULER};
use crate::exceptions::{syscalls, debug_halt};
use shared::exceptions::handlers::ExceptionContext;
use mmio::PhysicalTimer;
use core::time::Duration;

pub unsafe fn irq_handler(e: &ExceptionContext) {
Expand Down
13 changes: 9 additions & 4 deletions kernel/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use mmio::{BCMDeviceMemory, Uart, PhysicalTimer, USB};
use mmio::{BCMDeviceMemory, Uart};
use crate::memory;
use qemu_exit::QEMUExit;
use crate::scheduler::Scheduler;
use core::time::Duration;
use mmio::timer::{PhysicalTimer, SystemTimer};
use usb::UsbBus;
use core::fmt::{Debug, Formatter};

pub const BCMDEVICES: BCMDeviceMemory = BCMDeviceMemory::new(memory::map::virt::peripheral::START);
pub const USB: USB = mmio::USB::new(memory::map::virt::USB_BASE);
pub const IRQ: mmio::IRQ = mmio::IRQ::new(memory::map::virt::IRQ_BASE);
pub const UART: Uart = mmio::Uart::new(memory::map::virt::UART_BASE);
pub const TIMER: PhysicalTimer = PhysicalTimer::new(Duration::from_millis(100));
pub const PTIMER: PhysicalTimer = PhysicalTimer::new(Duration::from_millis(100));
pub const STIMER: SystemTimer = SystemTimer::new(memory::map::virt::SYS_TIMER_BASE);
//pub const USB: USB = mmio::USB::new(memory::map::virt::USB_BASE, &STIMER);
pub const USB: UsbBus = UsbBus::new(memory::map::virt::USB_BASE, &STIMER);
pub static mut SCHEDULER: Scheduler = Scheduler::new();

#[panic_handler]
Expand All @@ -21,4 +26,4 @@ fn my_panic(info: &core::panic::PanicInfo) -> ! {
#[alloc_error_handler]
fn foo(layout: core::alloc::Layout) -> ! {
panic!("Can not allocate {:?}", layout);
}
}
33 changes: 16 additions & 17 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@
#![feature(duration_constants)]
#![feature(alloc_error_handler)]
#![feature(llvm_asm)]
#![feature(allocator_api)]

#[macro_use] extern crate alloc;
#[macro_use] extern crate mmio;

use alloc::vec::Vec;

use cortex_a::asm;
use cortex_a::regs::{ELR_EL1, RegisterReadWrite, SP_EL0, SPSR_EL1};
use cortex_a::regs::{ELR_EL1, RegisterReadWrite, SP_EL0, SPSR_EL1, CNTP_TVAL_EL0};

use memory::descriptors::{KERNEL_VIRTUAL_LAYOUT, PROGRAM_VIRTUAL_LAYOUT};
use mmio::{BCMDeviceMemory, DMA, HEAP, Uart, PhysicalTimer, IRQ};
use mmio::{BCMDeviceMemory, DMA, HEAP, Uart, IRQ};
use shared::memory::mmu::{VIRTUAL_ADDR_START, switch_user_tables};

use crate::global::{BCMDEVICES, UART, TIMER};
use crate::scheduler::process::{create_init_program, create_tmp_init_program};
use crate::global::{BCMDEVICES, UART, PTIMER, STIMER, USB};
use crate::scheduler::process::{create_init_program};
use core::str::from_utf8_unchecked;
use core::time::Duration;
use core::slice;
use alloc::boxed::Box;
use crate::usb::setup_usb;

mod memory;
mod exceptions;
mod global;
mod scheduler;
mod usb;

extern "C" {
// Boundaries of the .bss section, provided by the linker script
Expand All @@ -51,10 +53,8 @@ pub unsafe extern "C" fn _upper_kernel() -> ! {
DMA.lock().init(memory::map::physical::MMA_MEMORY_START,
memory::map::physical::MMA_MEMORY_END - memory::map::physical::MMA_MEMORY_START);
}
let v_mbox = mmio::Mbox::new_with_dma(memory::map::virt::MBOX_BASE);
let mut v_mbox = mmio::Mbox::new_with_dma(memory::map::virt::MBOX_BASE);
mmio::LOGGER.appender(UART.into());
let mut console = mmio::FrameBufferConsole::new(v_mbox, VIRTUAL_ADDR_START);
mmio::SCREEN.appender( console.into());

unsafe { print!("MMU Kernel mapping : \n{}", shared::memory::mmu::kernel_tables()); }
unsafe { print!("MMU Program mapping : \n{}", shared::memory::mmu::user_tables()); }
Expand All @@ -63,19 +63,18 @@ pub unsafe extern "C" fn _upper_kernel() -> ! {
HEAP.lock().init(memory::map::virt::KERNEL_HEAP_START,
memory::map::virt::KERNEL_HEAP_END - memory::map::virt::KERNEL_HEAP_START);
}
setup_usb(&mut v_mbox);

// setup IRQs
//UART.enable_rx_irq(&irq, &bcm);
let mut console = mmio::FrameBufferConsole::new(v_mbox, VIRTUAL_ADDR_START);
mmio::SCREEN.appender( console.into());

create_tmp_init_program();
create_tmp_init_program();
create_tmp_init_program();
create_tmp_init_program();
create_tmp_init_program();

// TODO : remove tmp program and have init with fork syscall
create_init_program();
create_init_program();

TIMER.setup(&BCMDEVICES);
PTIMER.setup(&BCMDEVICES);

unsafe { IRQ::enable(); }

loop {
Expand Down
6 changes: 2 additions & 4 deletions kernel/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use shared::memory::mapping::{Descriptor, Mapping, Translation, AttributeFields,
use core::ops::RangeInclusive;
use shared::exceptions::handlers::ExceptionContext;
use crate::scheduler::process::ProcessState::Running;
use mmio::PhysicalTimer;
use crate::global::TIMER;
use crate::global::PTIMER;
use cortex_a::regs::{CNTV_TVAL_EL0, RegisterReadWrite};
use shared::memory::mmu::VIRTUAL_ADDR_START;
use core::str::from_utf8_unchecked;
Expand Down Expand Up @@ -53,8 +52,7 @@ impl Scheduler {
}

pub unsafe fn schedule(&mut self, e: &ExceptionContext) {

TIMER.reset_counter();
PTIMER.reset_counter();

self.processes.iter_mut()
.find( | p | p.is_running())
Expand Down
19 changes: 1 addition & 18 deletions kernel/src/scheduler/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::scheduler::PROG_START;
use crate::scheduler::process::ProcessState::{Sleep, Running};
use core::cell::Cell;
use cortex_a::asm::eret;
use crate::global::{SCHEDULER, BCMDEVICES, TIMER};
use crate::global::{SCHEDULER, BCMDEVICES, PTIMER};
use core::fmt::{Debug, Formatter};
use core::{fmt, slice};
use alloc::fmt::format;
Expand Down Expand Up @@ -97,16 +97,6 @@ impl Process {
unsafe { print!("MMU Program mapping : \n{}", self.tlb); }
}

pub fn run(&mut self) {
self.state = Running;
println!("START PROCESS PID {} at {:x}", self.pid, PROG_START as u64);
SP_EL0.set(0x0040_0000);
// Indicate that we "return" to EL0
SPSR_EL1.write(SPSR_EL1::M::EL0t);
ELR_EL1.set(PROG_START as u64);
asm::eret();
}

pub fn is_running(&self) -> bool {
self.state == Running
}
Expand All @@ -130,17 +120,10 @@ impl Process {
}
}

pub(crate) fn create_tmp_init_program() -> &'static mut Process {
let process = unsafe { SCHEDULER.create_process() };
let bytes = include_bytes!("../../../program.img");
unsafe { core::ptr::copy(bytes as *const u8, PROG_START as *mut u8, bytes.len()) };
process
}

pub(crate) fn create_init_program() -> &'static mut Process {
let process = unsafe { SCHEDULER.create_process() };
let bytes = include_bytes!("../../../init.img");
unsafe { core::ptr::copy(bytes as *const u8, PROG_START as *mut u8, bytes.len()) };
//process.run();
process
}
32 changes: 32 additions & 0 deletions kernel/src/usb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use alloc::boxed::Box;
use crate::global::USB;
use core::ops::Deref;
use mmio::Mbox;

pub fn setup_usb(v_mbox: &mut Mbox) {

/*let mut device = Box::new_in(UsbDevice::new(), DmaAllocator);
USB.init(v_mbox).expect("Issue with USB.init");
USB.start().expect("ISSUE with USB.start");
let request = Box::new_in(UsbDeviceRequest {
requestType: 0x80,
requestName: 6,
value: 1 << 8,
index: 0,
length: 8
}, DmaAllocator);
USB.send_request(device.as_mut(), request.as_ref()).expect("ISSUE with USB.attachRoot");
*/


USB.init().expect("USB.init failed");

debugln!("USB starting...");
}



Binary file modified kernel8.img
Binary file not shown.
4 changes: 2 additions & 2 deletions mmio/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all: lib
lib:
all: mmio
mmio:
cargo xrustc --target aarch64-unknown-none --release
clean:
cargo clean
14 changes: 14 additions & 0 deletions mmio/src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::ptr::{NonNull};
use core::cell::RefCell;
use core::{mem, slice};
use linked_list_allocator::{Heap, LockedHeap};
use crate::DMA;

pub trait SliceAllocator {
fn alloc_slice_zeroed<'a, T>(
Expand All @@ -26,3 +27,16 @@ impl SliceAllocator for LockedHeap {
Ok(unsafe { slice::from_raw_parts_mut(self.alloc_zeroed(l) as *mut T, count_of_items) })
}
}

pub struct DmaAllocator;

unsafe impl Allocator for DmaAllocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
let allocated = unsafe { core::slice::from_raw_parts_mut(DMA.alloc(layout), layout.size()) };
Ok(NonNull::new(allocated).expect("Null Allocation"))
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
DMA.dealloc(ptr.as_ptr(), layout)
}
}
7 changes: 3 additions & 4 deletions mmio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#![feature(llvm_asm)]
#![feature(allocator_api)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(allocator_api)]

#[macro_use]
extern crate num_derive;
Expand All @@ -12,7 +12,7 @@ pub mod io;

mod delays;
mod gpio;
mod mbox;
pub mod mbox;
mod uart;
mod irq;
pub mod timer;
Expand All @@ -28,12 +28,11 @@ pub use gpio::GPIO;
pub use mbox::Mbox;
pub use uart::Uart;
pub use syscall::SysCall;
pub use usb::USB;
pub use timer::PhysicalTimer;
pub use irq::IRQ;
pub use bcm::BCMDeviceMemory;
pub use console::FrameBufferConsole;
use linked_list_allocator::LockedHeap;
pub use dma::DmaAllocator;

pub static mut LOGGER: Logger = Logger::new();
pub static mut SCREEN: Logger = Logger::new();
Expand Down
Loading

0 comments on commit c34e0b6

Please sign in to comment.