artiq-zynq/src/runtime/src/kernel.rs

151 lines
4.4 KiB
Rust
Raw Normal View History

2020-04-26 09:57:42 +08:00
use core::{ptr, mem};
2020-04-24 12:55:59 +08:00
use log::{debug, error};
2020-04-30 19:33:45 +08:00
use alloc::{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 libsupport_zynq::boot::Core1;
2020-04-17 15:43:00 +08:00
use dyld;
2020-04-17 17:05:55 +08:00
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-26 09:57:42 +08:00
StartRequest,
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 {
2020-04-28 19:46:33 +08:00
core1: Core1,
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 {
2020-04-28 19:46:33 +08:00
pub fn start() -> Self {
let core1 = Core1::start();
2020-04-17 13:18:23 +08:00
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 = &[
api!(now_mu = rtio::now_mu),
api!(at_mu = rtio::at_mu),
api!(delay_mu = rtio::delay_mu),
2020-04-17 17:05:55 +08:00
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),
2020-04-26 09:57:42 +08:00
api!(__artiq_personality = 0), // HACK
2020-04-17 17:05:55 +08:00
];
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() {
2020-04-24 12:55:59 +08:00
debug!("Core1 started");
2020-04-17 13:18:23 +08:00
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-26 09:57:42 +08:00
let mut current_modinit: Option<u32> = None;
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) => {
2020-04-30 04:01:12 +08:00
match dyld::load(&data, &resolve) {
2020-04-17 17:05:55 +08:00
Ok(library) => {
2020-04-28 19:01:53 +08:00
let bss_start = library.lookup(b"__bss_start");
let end = library.lookup(b"_end");
if let Some(bss_start) = bss_start {
let end = end.unwrap();
unsafe {
ptr::write_bytes(bss_start as *mut u8, 0, (end - bss_start) as usize);
}
2020-04-26 09:57:42 +08:00
}
2020-04-28 19:01:53 +08:00
let __modinit__ = library.lookup(b"__modinit__").unwrap();
2020-04-26 09:57:42 +08:00
current_modinit = Some(__modinit__);
2020-04-26 16:10:09 +08:00
debug!("kernel loaded");
2020-04-17 17:05:55 +08:00
core1_tx.send(Message::LoadCompleted)
},
Err(error) => {
2020-04-24 12:55:59 +08:00
error!("failed to load shared library: {}", error);
2020-04-17 17:05:55 +08:00
core1_tx.send(Message::LoadFailed)
}
}
},
2020-04-26 09:57:42 +08:00
Message::StartRequest => {
2020-04-26 16:10:09 +08:00
debug!("kernel starting");
2020-04-26 09:57:42 +08:00
if let Some(__modinit__) = current_modinit {
unsafe {
(mem::transmute::<u32, fn()>(__modinit__))();
}
}
2020-04-26 16:10:09 +08:00
debug!("kernel terminated");
2020-04-26 09:57:42 +08:00
}
2020-04-24 12:55:59 +08:00
_ => error!("Core1 received unexpected message: {:?}", message),
2020-04-17 15:43:00 +08:00
}
2020-04-13 13:48:08 +08:00
}
}