artiq-zynq/runtime/src/kernel.rs

128 lines
3.4 KiB
Rust
Raw Normal View History

2020-04-17 17:05:55 +08:00
use alloc::{vec, vec::Vec, sync::Arc};
2020-04-17 15:43:00 +08:00
2020-04-17 13:18:23 +08:00
use libcortex_a9::{mutex::Mutex, sync_channel::{self, sync_channel}};
use libboard_zynq::println;
use libsupport_zynq::boot::Core1;
2020-04-17 15:43:00 +08:00
use dyld;
2020-04-17 17:05:55 +08:00
use crate::pl::csr;
use crate::rtio;
2020-04-17 15:43:00 +08:00
#[derive(Debug)]
pub enum Message {
2020-04-17 17:05:55 +08:00
LoadRequest(Arc<Vec<u8>>),
LoadCompleted,
LoadFailed,
2020-04-17 15:43:00 +08:00
}
static CHANNEL_0TO1: Mutex<Option<sync_channel::Receiver<Message>>> = Mutex::new(None);
static CHANNEL_1TO0: Mutex<Option<sync_channel::Sender<Message>>> = Mutex::new(None);
2020-04-17 13:18:23 +08:00
pub struct Control {
core1: Core1<Vec<u32>>,
2020-04-17 15:43:00 +08:00
pub tx: sync_channel::Sender<Message>,
pub rx: sync_channel::Receiver<Message>,
2020-04-17 13:18:23 +08:00
}
impl Control {
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);
Control {
core1,
tx: core0_tx,
rx: core0_rx,
}
}
pub fn restart(&mut self) {
2020-04-17 13:18:23 +08:00
*CHANNEL_0TO1.lock() = None;
*CHANNEL_1TO0.lock() = None;
self.core1.restart();
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);
self.tx = core0_tx;
self.rx = core0_rx;
2020-04-17 13:18:23 +08:00
}
}
2020-04-13 13:48:08 +08:00
2020-04-17 17:05:55 +08:00
macro_rules! api {
($i:ident) => ({
extern { static $i: u8; }
api!($i = &$i as *const _)
});
($i:ident, $d:item) => ({
$d
api!($i = $i)
});
($i:ident = $e:expr) => {
(stringify!($i), $e as *const ())
}
}
fn resolve(required: &[u8]) -> Option<u32> {
let api = &[
/* proxified syscalls */
api!(now = csr::rtio::NOW_HI_ADDR as *const _),
api!(rtio_init = rtio::init),
api!(rtio_get_destination_status = rtio::get_destination_status),
api!(rtio_get_counter = rtio::get_counter),
api!(rtio_output = rtio::output),
api!(rtio_output_wide = rtio::output_wide),
api!(rtio_input_timestamp = rtio::input_timestamp),
api!(rtio_input_data = rtio::input_data),
api!(rtio_input_timestamped_data = rtio::input_timestamped_data),
];
api.iter()
.find(|&&(exported, _)| exported.as_bytes() == required)
.map(|&(_, ptr)| ptr as u32)
}
2020-04-17 13:18:23 +08:00
#[no_mangle]
pub fn main_core1() {
println!("Core1 started");
let mut core1_tx = None;
while core1_tx.is_none() {
core1_tx = CHANNEL_1TO0.lock().take();
}
let mut core1_tx = core1_tx.unwrap();
let mut core1_rx = None;
while core1_rx.is_none() {
core1_rx = CHANNEL_0TO1.lock().take();
}
let core1_rx = core1_rx.unwrap();
2020-04-18 18:04:58 +08:00
let mut image = vec![0; 1024*1024];
2020-04-17 15:43:00 +08:00
for message in core1_rx {
match *message {
2020-04-17 17:05:55 +08:00
Message::LoadRequest(data) => {
match dyld::Library::load(&data, &mut image, &resolve) {
Ok(library) => {
core1_tx.send(Message::LoadCompleted)
},
Err(error) => {
println!("failed to load shared library: {}", error);
core1_tx.send(Message::LoadFailed)
}
}
},
2020-04-17 15:43:00 +08:00
_ => println!("Core1 received unexpected message: {:?}", message),
}
2020-04-13 13:48:08 +08:00
}
}