From 105eb7c2bcea61d281123325e74f990b9862158e Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 16 Apr 2020 23:58:04 +0200 Subject: [PATCH] runtime: encapsulate core1 state in KernelControl --- runtime/src/comms.rs | 21 +++++++++++++++++---- runtime/src/control.rs | 38 ++++++++++++++++++++++++++++++++++++++ runtime/src/main.rs | 11 ++--------- 3 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 runtime/src/control.rs diff --git a/runtime/src/comms.rs b/runtime/src/comms.rs index 3c71e14..895884c 100644 --- a/runtime/src/comms.rs +++ b/runtime/src/comms.rs @@ -2,6 +2,7 @@ use core::{mem::transmute, task::Poll}; use core::fmt; use core::cmp::min; use core::cell::RefCell; +use alloc::rc::Rc; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; @@ -21,6 +22,7 @@ use libcortex_a9::sync_channel; use libasync::{smoltcp::{Sockets, TcpStream}, task}; use crate::kernel; +use crate::control::KernelControl; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -141,7 +143,7 @@ async fn send_header(stream: &TcpStream, reply: Reply) -> Result<()> { Ok(()) } -async fn handle_connection(stream: TcpStream) -> Result<()> { +async fn handle_connection(stream: TcpStream, control: Rc>>) -> Result<()> { expect(&stream, b"ARTIQ coredev\n").await?; loop { expect(&stream, &[0x5a, 0x5a, 0x5a, 0x5a]).await?; @@ -163,6 +165,14 @@ async fn handle_connection(stream: TcpStream) -> Result<()> { send_header(&stream, Reply::LoadCompleted).await?; } println!("length={}, {:?}", length, &kernel_buffer[..256]); + + // TODO: dyld + + control.borrow_mut() + .take() + .map(|control| control.reset()); + + *control.borrow_mut() = Some(KernelControl::start(8192)); } _ => return Err(Error::UnrecognizedPacket) } @@ -173,7 +183,7 @@ async fn handle_connection(stream: TcpStream) -> Result<()> { const HWADDR: [u8; 6] = [0, 0x23, 0xab, 0xad, 0x1d, 0xea]; const IPADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 52])); -pub fn main(mut sc_tx: sync_channel::Sender, mut sc_rx: sync_channel::Receiver) { +pub fn main() { let eth = zynq::eth::Eth::default(HWADDR.clone()); const RX_LEN: usize = 8; let mut rx_descs = (0..RX_LEN) @@ -209,11 +219,14 @@ pub fn main(mut sc_tx: sync_channel::Sender, mut sc_rx: sync_channel::Rec Sockets::init(32); - task::spawn(async { + let control: Rc>> = Rc::new(RefCell::new(None)); + + task::spawn(async move { loop { while let stream = TcpStream::accept(1381, 2048, 2048).await.unwrap() { + let control = control.clone(); task::spawn(async { - let _ = handle_connection(stream) + let _ = handle_connection(stream, control) .await .map_err(|e| println!("Connection: {}", e)); }); diff --git a/runtime/src/control.rs b/runtime/src/control.rs new file mode 100644 index 0000000..a36f6a0 --- /dev/null +++ b/runtime/src/control.rs @@ -0,0 +1,38 @@ +use alloc::{vec, vec::Vec}; +use libcortex_a9::{mutex::Mutex, sync_channel::{self, sync_channel}}; +use libsupport_zynq::boot::Core1; + +pub static CHANNEL_0TO1: Mutex>> = Mutex::new(None); +pub static CHANNEL_1TO0: Mutex>> = Mutex::new(None); + +/// Interface for core 0 to control core 1 start and reset +pub struct KernelControl { + core1: Core1>, + pub tx: sync_channel::Sender, + pub rx: sync_channel::Receiver, +} + +impl KernelControl { + pub fn start(stack_size: usize) -> Self { + let stack = vec![0; stack_size / 4]; + let core1 = Core1::start(stack); + + let (core0_tx, core1_rx) = sync_channel(4); + let (core1_tx, core0_rx) = sync_channel(4); + *CHANNEL_0TO1.lock() = Some(core1_rx); + *CHANNEL_1TO0.lock() = Some(core1_tx); + + KernelControl { + core1, + tx: core0_tx, + rx: core0_rx, + } + } + + pub fn reset(self) { + *CHANNEL_0TO1.lock() = None; + *CHANNEL_1TO0.lock() = None; + + self.core1.reset(); + } +} diff --git a/runtime/src/main.rs b/runtime/src/main.rs index 8f6118f..c371647 100644 --- a/runtime/src/main.rs +++ b/runtime/src/main.rs @@ -16,6 +16,7 @@ mod comms; mod pl; mod rtio; mod kernel; +mod control; fn identifier_read(buf: &mut [u8]) -> &str { @@ -50,15 +51,7 @@ pub fn main_core0() { println!("Detected gateware: {}", identifier_read(&mut [0; 64])); - let core1_stack = unsafe { &mut STACK_CORE1[..] }; - let core1 = boot::Core1::start(core1_stack); - - let (mut core0_tx, core1_rx) = sync_channel(4); - let (core1_tx, mut core0_rx) = sync_channel(4); - *CHANNEL_0TO1.lock() = Some(core1_rx); - *CHANNEL_1TO0.lock() = Some(core1_tx); - - comms::main(core0_tx, core0_rx); + comms::main(); } #[no_mangle]