Skip to content

Commit

Permalink
run idle kernel on flash
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRenblad authored and sbourdeauducq committed Sep 14, 2024
1 parent 41203df commit 4022742
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
13 changes: 9 additions & 4 deletions artiq/firmware/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ extern crate riscv;
extern crate tar_no_std;

use alloc::collections::BTreeMap;
use core::cell::RefCell;
use core::cell::{RefCell, Cell};
use core::convert::TryFrom;
use smoltcp::wire::HardwareAddress;
use urc::Urc;

use board_misoc::{csr, ident, clock, spiflash, config, net_settings, pmp, boot};
#[cfg(has_ethmac)]
Expand Down Expand Up @@ -196,6 +197,7 @@ fn startup() {

let ddma_mutex = sched::Mutex::new();
let subkernel_mutex = sched::Mutex::new();
let restart_idle = Urc::new(Cell::new(false));

let mut scheduler = sched::Scheduler::new(interface);
let io = scheduler.io();
Expand All @@ -205,15 +207,18 @@ fn startup() {
}

rtio_mgt::startup(&io, &aux_mutex, &drtio_routing_table, &up_destinations, &ddma_mutex, &subkernel_mutex);

io.spawn(4096, mgmt::thread);
{
let restart_idle = restart_idle.clone();
io.spawn(4096, move |io| { mgmt::thread(io, &restart_idle) });
}
{
let aux_mutex = aux_mutex.clone();
let drtio_routing_table = drtio_routing_table.clone();
let up_destinations = up_destinations.clone();
let ddma_mutex = ddma_mutex.clone();
let subkernel_mutex = subkernel_mutex.clone();
io.spawn(32768, move |io| { session::thread(io, &aux_mutex, &drtio_routing_table, &up_destinations, &ddma_mutex, &subkernel_mutex) });
let restart_idle = restart_idle.clone();
io.spawn(32768, move |io| { session::thread(io, &aux_mutex, &drtio_routing_table, &up_destinations, &ddma_mutex, &subkernel_mutex, &restart_idle) });
}
#[cfg(any(has_rtio_moninj, has_drtio))]
{
Expand Down
31 changes: 25 additions & 6 deletions artiq/firmware/runtime/mgmt.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use log::{self, LevelFilter};
use core::cell::Cell;

use io::{Write, ProtoWrite, Error as IoError};
use board_misoc::{config, spiflash};
use logger_artiq::BufferLogger;
use mgmt_proto::*;
use sched::{Io, TcpListener, TcpStream, Error as SchedError};
use urc::Urc;

impl From<SchedError> for Error<SchedError> {
fn from(value: SchedError) -> Error<SchedError> {
Error::Io(IoError::Other(value))
}
}

fn worker(io: &Io, stream: &mut TcpStream) -> Result<(), Error<SchedError>> {
fn worker(io: &Io, stream: &mut TcpStream, restart_idle: &Urc<Cell<bool>>) -> Result<(), Error<SchedError>> {
read_magic(stream)?;
Write::write_all(stream, "e".as_bytes())?;
info!("new connection from {}", stream.remote_endpoint());
Expand Down Expand Up @@ -84,20 +86,36 @@ fn worker(io: &Io, stream: &mut TcpStream) -> Result<(), Error<SchedError>> {
}
Request::ConfigWrite { ref key, ref value } => {
match config::write(key, value) {
Ok(_) => Reply::Success.write_to(stream),
Ok(_) => {
if key == "idle_kernel" {
io.until(|| !restart_idle.get())?;
restart_idle.set(true);
}
Reply::Success.write_to(stream)
},
Err(_) => Reply::Error.write_to(stream)
}?;
}
Request::ConfigRemove { ref key } => {
match config::remove(key) {
Ok(()) => Reply::Success.write_to(stream),
Ok(()) => {
if key == "idle_kernel" {
io.until(|| !restart_idle.get())?;
restart_idle.set(true);
}
Reply::Success.write_to(stream)
},
Err(_) => Reply::Error.write_to(stream)
}?;

}
Request::ConfigErase => {
match config::erase() {
Ok(()) => Reply::Success.write_to(stream),
Ok(()) => {
io.until(|| !restart_idle.get())?;
restart_idle.set(true);
Reply::Success.write_to(stream)
},
Err(_) => Reply::Error.write_to(stream)
}?;
}
Expand All @@ -117,16 +135,17 @@ fn worker(io: &Io, stream: &mut TcpStream) -> Result<(), Error<SchedError>> {
}
}

pub fn thread(io: Io) {
pub fn thread(io: Io, restart_idle: &Urc<Cell<bool>>) {
let listener = TcpListener::new(&io, 8192);
listener.listen(1380).expect("mgmt: cannot listen");
info!("management interface active");

loop {
let stream = listener.accept().expect("mgmt: cannot accept").into_handle();
let restart_idle = restart_idle.clone();
io.spawn(4096, move |io| {
let mut stream = TcpStream::from_handle(&io, stream);
match worker(&io, &mut stream) {
match worker(&io, &mut stream, &restart_idle) {
Ok(()) => (),
Err(Error::Io(IoError::UnexpectedEnd)) => (),
Err(err) => error!("aborted: {}", err)
Expand Down
19 changes: 13 additions & 6 deletions artiq/firmware/runtime/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ fn flash_kernel_worker(io: &Io, aux_mutex: &Mutex,
routing_table: &drtio_routing::RoutingTable,
up_destinations: &Urc<RefCell<[bool; drtio_routing::DEST_COUNT]>>,
ddma_mutex: &Mutex, subkernel_mutex: &Mutex, congress: &mut Congress,
config_key: &str) -> Result<(), Error<SchedError>> {
config_key: &str, restart_idle: Option<&Urc<Cell<bool>>>) -> Result<(), Error<SchedError>> {
let mut session = Session::new(congress);

config::read(config_key, |result| {
Expand All @@ -859,11 +859,16 @@ fn flash_kernel_worker(io: &Io, aux_mutex: &Mutex,
}
})?;
kern_run(&mut session)?;

loop {
if !rpc_queue::empty() {
unexpected!("unexpected background RPC in flash kernel")
}

if let Some(r_idle) = restart_idle {
if r_idle.get() {
return Err(Error::KernelNotFound)
}
}

if mailbox::receive() != 0 {
if process_kern_message(io, aux_mutex, routing_table, up_destinations, ddma_mutex, subkernel_mutex, None, &mut session)? {
Expand Down Expand Up @@ -897,7 +902,7 @@ fn respawn<F>(io: &Io, handle: &mut Option<ThreadHandle>, f: F)
pub fn thread(io: Io, aux_mutex: &Mutex,
routing_table: &Urc<RefCell<drtio_routing::RoutingTable>>,
up_destinations: &Urc<RefCell<[bool; drtio_routing::DEST_COUNT]>>,
ddma_mutex: &Mutex, subkernel_mutex: &Mutex) {
ddma_mutex: &Mutex, subkernel_mutex: &Mutex, restart_idle: &Urc<Cell<bool>>) {
let listener = TcpListener::new(&io, 65535);
listener.listen(1381).expect("session: cannot listen");
info!("accepting network sessions");
Expand All @@ -910,7 +915,7 @@ pub fn thread(io: Io, aux_mutex: &Mutex,
let mut congress = congress.borrow_mut();
info!("running startup kernel");
match flash_kernel_worker(&io, &aux_mutex, &routing_table, &up_destinations,
ddma_mutex, subkernel_mutex, &mut congress, "startup_kernel") {
ddma_mutex, subkernel_mutex, &mut congress, "startup_kernel", None) {
Ok(()) =>
info!("startup kernel finished"),
Err(Error::KernelNotFound) =>
Expand Down Expand Up @@ -994,11 +999,12 @@ pub fn thread(io: Io, aux_mutex: &Mutex,
let congress = congress.clone();
let ddma_mutex = ddma_mutex.clone();
let subkernel_mutex = subkernel_mutex.clone();
let restart_idle = restart_idle.clone();
respawn(&io, &mut kernel_thread, move |io| {
let routing_table = routing_table.borrow();
let mut congress = congress.borrow_mut();
match flash_kernel_worker(&io, &aux_mutex, &routing_table, &up_destinations,
&ddma_mutex, &subkernel_mutex, &mut *congress, "idle_kernel") {
&ddma_mutex, &subkernel_mutex, &mut *congress, "idle_kernel", Some(&restart_idle)) {
Ok(()) =>
info!("idle kernel finished, standing by"),
Err(Error::Protocol(host::Error::Io(
Expand All @@ -1010,7 +1016,8 @@ pub fn thread(io: Io, aux_mutex: &Mutex,
}
Err(Error::KernelNotFound) => {
debug!("no idle kernel found");
while io.relinquish().is_ok() {}
while !restart_idle.get() && io.relinquish().is_ok() {}
restart_idle.set(false);
}
Err(err) => {
error!("idle kernel aborted: {}", err);
Expand Down

0 comments on commit 4022742

Please sign in to comment.