#xtensa #atomic #emulation


An atomic emulation trap handler for non atomic Xtensa targets

5 releases (3 breaking)

0.4.0 Feb 10, 2023
0.3.1 Jan 24, 2023
0.3.0 Dec 2, 2022
0.2.0 Aug 10, 2022
0.1.0 Jun 21, 2022

#263 in Emulators

Download history 1313/week @ 2024-01-06 1558/week @ 2024-01-13 1526/week @ 2024-01-20 1225/week @ 2024-01-27 1454/week @ 2024-02-03 1114/week @ 2024-02-10 1000/week @ 2024-02-17 1411/week @ 2024-02-24 1510/week @ 2024-03-02 1708/week @ 2024-03-09 1509/week @ 2024-03-16 1429/week @ 2024-03-23 1147/week @ 2024-03-30 1220/week @ 2024-04-06 1377/week @ 2024-04-13 1271/week @ 2024-04-20

5,536 downloads per month


52 lines

Xtensa atomic emulation trap handler


Additional RUSTFLAGS

Add the following rustflags to .cargo/config.toml in your project. Take care not to overwrite any existing ones.

rustflags = [
# enable the atomic codegen option for Xtensa
"-C", "target-feature=+s32c1i",

# tell the core library have atomics even though it's not specified in the target definition
"--cfg", 'target_has_atomic="8"',
"--cfg", 'target_has_atomic="16"',
"--cfg", 'target_has_atomic="32"',
"--cfg", 'target_has_atomic="ptr"',

and then call atomic_emulation in the exception handler.

How it works

We build code for silicon that has the s32c1i feature, then when our target attempts to execute these instructions it throws an illegal instruction exception, at which point we can decode the instruction and emulate it in software.

There is only one atomic instruction to emulate in the Xtensa ISA, S32C1I. However, compare values are written to the SCOMPARE1 (Special Reg No. 12) register, so in silicon without this feature it won't exist. We need to emulate the instruction and the register for sucessful atomic emulation.

See of the ISA RM for an example atomic compare swap loop.

Instruction Format Instruction composition
WSR RSR 0001_0011_0000_0000_0000_0000

To emulate the WSR instruction, we must first decode it and verify that the target register is 12, the SCOMPARE1 register. Once that is confirmed, we can use this crates virtual SCOMPARE1 to store the value.

Emulation of the S32C1I instruction is a little more complicated. First we decode the entire instruction to get the following values:

  • target register - this contains the new value we wish to swap to
  • source register - this conaints the address in memory of the current value
  • offset - optional offset to add to the address in the source register

We deference the source address + offset to find the current value and compare it to the stored value inside our SCOMPARE1 virtual register. If they are equal, the new target value is written to memory at the source address + offset. Regardless of whether the new value is written the old value is always written back into the target register.

Usage with xtensa_lx_rt

An example of how to use this crate with xtensa-lx-rt can be found in v0.3.1.

I get linker errors when I build for debug

Follow the instructions here, and also add the following.

opt-level = 'z'

No runtime deps