cortex-m icon indicating copy to clipboard operation
cortex-m copied to clipboard

SWI / SVC support

Open thejpster opened this issue 5 years ago • 8 comments

Could we add intrinsics for calling Software Interrupts (the SWI or SVC instruction)? We probably also want a mechanism for marshalling some number of integer arguments into registers, and back out again on the other side of the SWI (i.e. in the kernel).

TockOS do this with llvm_asm!, as you can see at https://github.com/tock/tock/blob/198ff4173e85f6b8474893c6784d38c12ead9dbb/arch/cortex-m0/src/lib.rs#L161

thejpster avatar Sep 04 '20 22:09 thejpster

As described in https://github.com/rust-embedded/cortex-m/issues/58, this is not in general possible since the specific svc ABI depends on the environment

jonas-schievink avatar Sep 04 '20 22:09 jonas-schievink

Ah. I guess it would be unreasonable to have an array of 256 function pointers to let you pick the SVC value...

thejpster avatar Sep 04 '20 23:09 thejpster

Once inline assembly is stable, we can provide a const-generic function like this:

fn svc<const NUM: u8>() {
    unsafe {
        asm!("svc {imm}", imm = const NUM);
    }
}

jonas-schievink avatar Jan 09 '21 18:01 jonas-schievink

Once inline assembly is stable, we can provide a const-generic function like this:

fn svc<const NUM: u8>() {
    unsafe {
        asm!("svc {imm}", imm = const NUM);
    }
}

Nice!

thejpster avatar Jan 16 '21 11:01 thejpster

So reading around a bit on this, some syscall ABIs just set the SVC value to be zero, as it's expensive to pull it out on the receiving side (you have to read the instruction that caused the interrupt, which will be in a different cache...). So, could we at least have a function which lets you SVC 0, and optionally populate some registers with arguments.

thejpster avatar Feb 10 '21 15:02 thejpster

Just tagging this issue as I've hit this as well, and we now have stable asm.

That being said, I'm probably also just going to go the "svc 0" route as well.

jamesmunns avatar Apr 04 '22 15:04 jamesmunns

I guess we'll want https://doc.rust-lang.org/unstable-book/language-features/asm-const.html for this one.

adamgreig avatar Apr 04 '22 16:04 adamgreig