I am trying to write software which is, in spirit, closest to writing a system call handler for a virtual machine emulator I'm writing. It's a RISC-V emulator, and the idea is that ECALL invokes services in the emulator, rather than dispatching to supervisor- or machine-mode. Hence, the emulator is user-mode only. But that's not the important part. This just sets the stage for my question.
In the emulator proper, I have a handle table implementation, which is itself just a vector of HandleTableEntry-ies. A HandleTableEntry is defined like so:
data HandleTableEntry = Option<Rc<RefCell<&dyn Manageable>>>;
The Manageable trait is defined something like so:
trait Manageable {
fn close(&mut self, em: &mut EmState);
// .... other stuff here, not important ....
}
When the "user-mode" code makes an e-call to close a handle, I would like to invoke the close method in the handle table. Here's how I do it so far:
let which = em.cpu.xr[10]; // register a0 = handle to close
let option_resource = em.handle_table[which];
match option_resource {
Some(rc_refcell_obj) => {
let obj = rc_refcell_obj.clone();
let mut obj = obj.borrow_mut();
obj.close(em);
}
None => (), // ignore
}
em.handle_table[which] = None;
Note that this is one of many, many, many attempts to get this program logic working. However, nothing I do here is able to make this code compile. The errors vary, depending on my specific selection of code. In this case, it complains about obj not being able to be borrowed mutably. But I get different errors if I structure this code differently.
Regardless, the errors I'm receiving is not important. What is important is that I'm clearly writing this code with a completely incorrect pattern. People are writing OSes in Rust complete with user-spaces, so somehow implementing a file descriptor table-like thing has to be possible. But, I just can't figure out how to do this. Can anyone please offer advice on how to best structure the code so I can have a proper handle table implementation?
BTW: the most updated code in its proper context is here: ~vertigo/incubator (master): vm-ecosystem/vmos/src/main.rs - sourcehut git . Ignore the comments preceding the highlighted block. As per custom in software development, the comments are misleading and worse than wrong in this case (it describes yesterday's attempt at solving the problem I'm running into, and I forgot to change the comments before pushing. Sorry.)
Thanks in advance.