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

Feature/usb layer #4

Open
wants to merge 1 commit into
base: master
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
66 changes: 55 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,10 +1,10 @@
[workspace]
resolver= "2"
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 @@ -74,7 +74,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)
1 change: 1 addition & 0 deletions cu.serial-master
1 change: 1 addition & 0 deletions cu.serial-slave
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 @@ -33,7 +33,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 = "2021"

[dependencies]
mmio = { path = "../mmio" }
usb = { path = "../usb" }
shared = { path = "../shared" }
qemu-exit = "3.0.2"
aarch64-cpu = "9.4.0"
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 core::fmt::Debug;
use usb::bus::UsbBus;

pub const BCMDEVICES: BCMDeviceMemory = BCMDeviceMemory::new(memory::map::virt::peripheral::START);
pub const USB: USB = USB::new(memory::map::virt::USB_BASE);
pub const IRQ: mmio::IRQ = mmio::IRQ::new(memory::map::virt::IRQ_BASE);
pub const UART: Uart = Uart::new(memory::map::virt::UART_BASE);
pub const TIMER: PhysicalTimer = PhysicalTimer::new(Duration::from_millis(100));
pub const UART: Uart = mmio::Uart::new(memory::map::virt::UART_BASE);
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 Down
28 changes: 15 additions & 13 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
#![feature(core_intrinsics)]
#![feature(duration_constants)]
#![feature(alloc_error_handler)]
#![feature(allocator_api)]

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


use aarch64_cpu::asm;

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

use crate::global::{BCMDEVICES, UART, TIMER};
use crate::scheduler::process::{create_init_program, create_tmp_init_program};
use crate::scheduler::process::{create_init_program};
use crate::global::{BCMDEVICES, UART, PTIMER, STIMER, USB};

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 @@ -42,10 +47,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 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 @@ -54,19 +57,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 aarch64_cpu::registers::{CNTV_TVAL_EL0, Readable};
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
17 changes: 0 additions & 17 deletions kernel/src/scheduler/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ impl Process {
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 @@ -124,17 +114,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
4 changes: 1 addition & 3 deletions mmio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod io;

mod delays;
mod gpio;
mod mbox;
pub mod mbox;
mod uart;
mod irq;
pub mod timer;
Expand All @@ -23,8 +23,6 @@ pub use gpio::GPIO;
pub use mbox::Mbox;
pub use uart::Uart;
pub use syscall::SysCall;
pub use timer::PhysicalTimer;
pub use usb::USB;
pub use irq::IRQ;
pub use bcm::BCMDeviceMemory;
pub use console::FrameBufferConsole;
Expand Down
6 changes: 5 additions & 1 deletion mmio/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ pub fn _print(args: Arguments) {
unsafe {
SCREEN.write_fmt(args).unwrap();
}
}
}

pub unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
core::slice::from_raw_parts((p as *const T) as *const u8,core::mem::size_of::<T>())
}
Loading