Skip to content

Commit

Permalink
Completely remove atomic emulation (#985)
Browse files Browse the repository at this point in the history
* Completely remove atomic emulation

* changelog
  • Loading branch information
MabezDev authored Dec 4, 2023
1 parent 84e5a00 commit 05f9d21
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 231 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
run: cd esp-hal-smartled/ && cargo +esp build -Zbuild-std=core --target=xtensa-esp32s3-none-elf --features=esp32s3
# Ensure documentation can be built (requires a chip feature!)
- name: rustdoc
run: cd esp-hal-smartled/ && cargo doc --features=esp32c3
run: cd esp-hal-smartled/ && cargo doc -Zbuild-std=core --target=riscv32imc-unknown-none-elf --features=esp32c3

esp32-hal:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Breaking
- Direct boot support has been removed (#903).
- `Spi::new`/`Spi::new_half_duplex` takes no gpio pin now, instead you need to call `with_pins` to setup those (#901).
- ESP32C2, ESP32C3: atomic emulation trap is now opt-in. When upgrading you must either remove [these lines](https://github.com/esp-rs/riscv-atomic-emulation-trap#usage) from your `.cargo/config.toml` or opt back in by enabling the feature. (#904)
- ESP32C2, ESP32C3, ESP32S2: atomic emulation trap has been removed. When upgrading you must either remove [these lines](https://github.com/esp-rs/riscv-atomic-emulation-trap#usage) from your `.cargo/config.toml`. Usage of `core::sync::atomic::*` in dependent crates should be replaced with [portable-atomic](https://github.com/taiki-e/portable-atomic). (#904) (#985)
- RSA driver now takes u32 words instead of u8 bytes. The expected slice length is now 4 times shorter. (#981)

## [0.13.1] - 2023-11-02
Expand Down
17 changes: 5 additions & 12 deletions esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ embedded-hal-nb = { version = "=1.0.0-rc.1", optional = true }
embedded-io = { version = "0.6.1", optional = true }
esp-synopsys-usb-otg = { version = "0.3.2", optional = true, features = ["fs", "esp32sx"] }
fugit = "0.3.7"
heapless = "0.8.0"
heapless = "0.8"
log = { version = "0.4.20", optional = true }
nb = "1.1.0"
paste = "1.0.14"
Expand All @@ -41,10 +41,7 @@ strum = { version = "0.25.0", default-features = false, features
void = { version = "1.0.2", default-features = false }
usb-device = { version = "0.2.9", optional = true }
enumset = "1.1.3"

# atomic polyfill options
portable-atomic = { version = "1.5.1", default-features = false, optional = true }
riscv-atomic-emulation-trap = { version = "0.4.1", optional = true }
portable-atomic = { version = "1.5.1", default-features = false }

# async
embedded-hal-async = { version = "=1.0.0-rc.1", optional = true }
Expand Down Expand Up @@ -81,11 +78,11 @@ serde = { version = "1.0.190", features = ["derive"] }

[features]
esp32 = ["xtensa", "esp32/rt", "procmacros/esp32", "xtensa-lx/esp32", "xtensa-lx-rt/esp32"]
esp32c2 = ["riscv", "esp32c2/rt", "procmacros/esp32c2", "portable-atomic?/unsafe-assume-single-core"]
esp32c3 = ["riscv", "esp32c3/rt", "procmacros/esp32c3", "portable-atomic?/unsafe-assume-single-core"]
esp32c2 = ["riscv", "esp32c2/rt", "procmacros/esp32c2", "portable-atomic/unsafe-assume-single-core"]
esp32c3 = ["riscv", "esp32c3/rt", "procmacros/esp32c3", "portable-atomic/unsafe-assume-single-core"]
esp32c6 = ["riscv", "esp32c6/rt", "procmacros/esp32c6"]
esp32h2 = ["riscv", "esp32h2/rt", "procmacros/esp32h2"]
esp32s2 = ["xtensa", "esp32s2/rt", "procmacros/esp32s2", "xtensa-lx/esp32s2", "xtensa-lx-rt/esp32s2", "usb-otg", "portable-atomic?/unsafe-assume-single-core"]
esp32s2 = ["xtensa", "esp32s2/rt", "procmacros/esp32s2", "xtensa-lx/esp32s2", "xtensa-lx-rt/esp32s2", "usb-otg", "portable-atomic/critical-section"]
esp32s3 = ["xtensa", "esp32s3/rt", "procmacros/esp32s3", "xtensa-lx/esp32s3", "xtensa-lx-rt/esp32s3", "usb-otg"]

# Crystal frequency selection (ESP32 and ESP32-C2 only!)
Expand Down Expand Up @@ -157,10 +154,6 @@ embassy-generic-queue-128 = ["embassy-time/generic-queue-128"]
riscv = ["critical-section/restore-state-u8", "esp-riscv-rt", "esp-riscv-rt/zero-bss"]
xtensa = ["critical-section/restore-state-u32"]

portable-atomic = ["dep:portable-atomic"]
riscv-atomic-emulation = ["dep:riscv-atomic-emulation-trap", "atomic-emulation"]
atomic-emulation = []

# Initialize / clear data sections and RTC memory
rv-init-data = ["esp-riscv-rt/init-data", "esp-riscv-rt/init-rw-text"]
rv-zero-rtc-bss = ["esp-riscv-rt/zero-rtc-fast-bss"]
Expand Down
28 changes: 4 additions & 24 deletions esp-hal-common/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,10 @@ fn main() -> Result<(), Box<dyn Error>> {
unreachable!() // We've confirmed exactly one known device was selected
};

// The C6 has an "atomic" peripheral but it's an exception, not the rule
// For S2, we just keep lying for now. The target has emulation support
// force-enabled.
let has_native_atomic_support = matches!(
device_name,
"esp32" | "esp32s2" | "esp32s3" | "esp32c6" | "esp32h2"
);

let atomic_emulation_enabled = cfg!(feature = "atomic-emulation");
let portable_atomic_enabled = cfg!(feature = "portable-atomic");

if !atomic_emulation_enabled && detect_atomic_extension("a") {
panic!("Atomic emulation flags detected in `.cargo/config.toml` but `atomic-emulation` feature is not enabled!");
}

if atomic_emulation_enabled && portable_atomic_enabled {
panic!("The `atomic-emulation` and `portable-atomic` features cannot be used together");
}

if has_native_atomic_support {
if atomic_emulation_enabled {
println!("cargo:warning={} has native atomic instructions, the `atomic-emulation` feature is not needed", device_name);
}
println!("cargo:rustc-cfg=has_native_atomic_support");
if detect_atomic_extension("a") || detect_atomic_extension("s32c1i") {
panic!(
"Atomic emulation flags detected in `.cargo/config.toml`, this is no longer supported!"
);
}

// Load the configuration file for the configured device:
Expand Down
100 changes: 5 additions & 95 deletions esp-hal-common/src/interrupt/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! interrupt15() => Priority::Priority15
//! ```

use esp_riscv_rt::riscv::register::{mcause, mepc, mtvec};
use esp_riscv_rt::riscv::register::{mcause, mtvec};
pub use esp_riscv_rt::TrapFrame;

#[cfg(not(plic))]
Expand Down Expand Up @@ -408,8 +408,10 @@ pub unsafe extern "C" fn start_trap_rust_hal(trap_frame: *mut TrapFrame) {

let cause = mcause::read();
if cause.is_exception() {
let pc = mepc::read();
handle_exception(pc, trap_frame);
extern "C" {
fn ExceptionHandler(tf: *mut TrapFrame);
}
ExceptionHandler(trap_frame);
} else {
#[cfg(feature = "interrupt-preemption")]
let interrupt_priority = _handle_priority();
Expand Down Expand Up @@ -455,98 +457,6 @@ pub unsafe extern "C" fn start_trap_rust_hal(trap_frame: *mut TrapFrame) {
}
}

/// Apply atomic emulation if needed. Call the default exception handler
/// otherwise.
///
/// # Safety
///
/// This function is called from an trap handler.
unsafe fn handle_exception(_pc: usize, trap_frame: *mut TrapFrame) {
#[cfg(feature = "atomic-emulation")]
{
let insn: usize = *(_pc as *const _);
let needs_atomic_emulation = (insn & 0b1111111) == 0b0101111;
if needs_atomic_emulation {
let mut frame = [
0,
(*trap_frame).ra,
(*trap_frame).sp,
(*trap_frame).gp,
(*trap_frame).tp,
(*trap_frame).t0,
(*trap_frame).t1,
(*trap_frame).t2,
(*trap_frame).s0,
(*trap_frame).s1,
(*trap_frame).a0,
(*trap_frame).a1,
(*trap_frame).a2,
(*trap_frame).a3,
(*trap_frame).a4,
(*trap_frame).a5,
(*trap_frame).a6,
(*trap_frame).a7,
(*trap_frame).s2,
(*trap_frame).s3,
(*trap_frame).s4,
(*trap_frame).s5,
(*trap_frame).s6,
(*trap_frame).s7,
(*trap_frame).s8,
(*trap_frame).s9,
(*trap_frame).s10,
(*trap_frame).s11,
(*trap_frame).t3,
(*trap_frame).t4,
(*trap_frame).t5,
(*trap_frame).t6,
];

riscv_atomic_emulation_trap::atomic_emulation((*trap_frame).pc, &mut frame);

(*trap_frame).ra = frame[1];
(*trap_frame).sp = frame[2];
(*trap_frame).gp = frame[3];
(*trap_frame).tp = frame[4];
(*trap_frame).t0 = frame[5];
(*trap_frame).t1 = frame[6];
(*trap_frame).t2 = frame[7];
(*trap_frame).s0 = frame[8];
(*trap_frame).s1 = frame[9];
(*trap_frame).a0 = frame[10];
(*trap_frame).a1 = frame[11];
(*trap_frame).a2 = frame[12];
(*trap_frame).a3 = frame[13];
(*trap_frame).a4 = frame[14];
(*trap_frame).a5 = frame[15];
(*trap_frame).a6 = frame[16];
(*trap_frame).a7 = frame[17];
(*trap_frame).s2 = frame[18];
(*trap_frame).s3 = frame[19];
(*trap_frame).s4 = frame[20];
(*trap_frame).s5 = frame[21];
(*trap_frame).s6 = frame[22];
(*trap_frame).s7 = frame[23];
(*trap_frame).s8 = frame[24];
(*trap_frame).s9 = frame[25];
(*trap_frame).s10 = frame[26];
(*trap_frame).s11 = frame[27];
(*trap_frame).t3 = frame[28];
(*trap_frame).t4 = frame[29];
(*trap_frame).t5 = frame[30];
(*trap_frame).t6 = frame[31];
(*trap_frame).pc = _pc + 4;

return;
}
}

extern "C" {
fn ExceptionHandler(tf: *mut TrapFrame);
}
ExceptionHandler(trap_frame);
}

#[doc(hidden)]
#[no_mangle]
pub fn _setup_interrupts() {
Expand Down
4 changes: 0 additions & 4 deletions esp-hal-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ mod soc;

#[allow(unused_imports)]
mod atomic {
#[cfg(any(has_native_atomic_support, feature = "atomic-emulation"))]
pub use core::sync::atomic::*;

#[cfg(feature = "portable-atomic")]
pub use portable_atomic::*;
}

Expand Down
2 changes: 1 addition & 1 deletion esp32-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ esp-alloc = "0.3.0"
esp-backtrace = { version = "0.9.0", features = ["esp32", "panic-handler", "exception-handler", "print-uart"] }
esp-hal-smartled = { version = "0.6.0", features = ["esp32"], path = "../esp-hal-smartled" }
esp-println = { version = "0.7.0", features = ["esp32"] }
heapless = "0.8.0"
heapless = "0.8"
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.8", default-features = false}
smart-leds = "0.3.0"
Expand Down
7 changes: 1 addition & 6 deletions esp32c2-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ embedded-io-async = "0.6.0"
embedded-hal-bus = "0.1.0-rc.1"
esp-backtrace = { version = "0.9.0", features = ["esp32c2", "panic-handler", "exception-handler", "print-uart"] }
esp-println = { version = "0.7.0", features = ["esp32c2"] }
heapless = "0.8.0"
heapless = "0.8"
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.8", default-features = false}
ssd1306 = "0.8.4"
portable-atomic = "1.5.1"
static_cell = { version = "2.0.0", features = ["nightly"] }
esp-hal-common = { features = ["portable-atomic"], path = "../esp-hal-common" } # enable portable-atomic by default for examples
hex-literal = "0.4.1"
crypto-bigint = { version = "0.5.3", default-features = false }
elliptic-curve = { version = "0.13.6", default-features = false, features = ["sec1"] }
Expand Down Expand Up @@ -82,9 +80,6 @@ embassy-generic-queue-32 = ["esp-hal-common/embassy-generic-queue-32"]
embassy-generic-queue-64 = ["esp-hal-common/embassy-generic-queue-64"]
embassy-generic-queue-128 = ["esp-hal-common/embassy-generic-queue-128"]

portable-atomic = ["esp-hal-common/portable-atomic"]
atomic-emulation = ["esp-hal-common/riscv-atomic-emulation"]

[profile.release]
debug = true

Expand Down
7 changes: 1 addition & 6 deletions esp32c3-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ embedded-hal-bus = "0.1.0-rc.1"
esp-backtrace = { version = "0.9.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] }
esp-hal-smartled = { version = "0.6.0", features = ["esp32c3"], path = "../esp-hal-smartled" }
esp-println = { version = "0.7.0", features = ["esp32c3"] }
heapless = "0.8.0"
heapless = "0.8"
hmac = { version = "0.12.1", default-features = false }
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.8", default-features = false }
smart-leds = "0.3.0"
ssd1306 = "0.8.4"
portable-atomic = "1.5.1"
esp-hal-common = { features = ["portable-atomic"], path = "../esp-hal-common" } # enable portable-atomic by default for examples
static_cell = { version = "2.0.0", features = ["nightly"] }
embassy-sync = "0.4.0"

Expand Down Expand Up @@ -88,9 +86,6 @@ embassy-generic-queue-32 = ["esp-hal-common/embassy-generic-queue-32"]
embassy-generic-queue-64 = ["esp-hal-common/embassy-generic-queue-64"]
embassy-generic-queue-128 = ["esp-hal-common/embassy-generic-queue-128"]

portable-atomic = ["esp-hal-common/portable-atomic"]
atomic-emulation = ["esp-hal-common/riscv-atomic-emulation"]

[profile.release]
debug = true

Expand Down
2 changes: 1 addition & 1 deletion esp32c6-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ embedded-hal-bus = "0.1.0-rc.1"
esp-backtrace = { version = "0.9.0", features = ["esp32c6", "panic-handler", "exception-handler", "print-uart"] }
esp-hal-smartled = { version = "0.6.0", features = ["esp32c6"], path = "../esp-hal-smartled" }
esp-println = { version = "0.7.0", features = ["esp32c6"] }
heapless = "0.8.0"
heapless = "0.8"
hmac = { version = "0.12.1", default-features = false }
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.8", default-features = false}
Expand Down
2 changes: 1 addition & 1 deletion esp32h2-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ embedded-hal-bus = "0.1.0-rc.1"
esp-backtrace = { version = "0.9.0", features = ["esp32h2", "panic-handler", "exception-handler", "print-uart"] }
esp-hal-smartled = { version = "0.6.0", features = ["esp32h2"], path = "../esp-hal-smartled" }
esp-println = { version = "0.7.0", features = ["esp32h2"] }
heapless = "0.8.0"
heapless = "0.8"
hmac = { version = "0.12.1", default-features = false }
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.8", default-features = false}
Expand Down
16 changes: 0 additions & 16 deletions esp32s2-hal/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,6 @@ rustflags = [
# LLD
# "-C", "linker=rust-lld",
# "-C", "link-arg=-Tlinkall.x",

# 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_load_store",
"--cfg", 'target_has_atomic_load_store="8"',
"--cfg", 'target_has_atomic_load_store="16"',
"--cfg", 'target_has_atomic_load_store="32"',
"--cfg", 'target_has_atomic_load_store="ptr"',
# enable cas
"--cfg", "target_has_atomic",
"--cfg", 'target_has_atomic="8"',
"--cfg", 'target_has_atomic="16"',
"--cfg", 'target_has_atomic="32"',
"--cfg", 'target_has_atomic="ptr"',
]
target = "xtensa-esp32s2-none-elf"

Expand Down
8 changes: 4 additions & 4 deletions esp32s2-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ categories = [
[dependencies]
esp-hal-common = { version = "0.13.0", features = ["esp32s2"], path = "../esp-hal-common" }
embassy-time = { version = "0.1.5", features = ["nightly"], optional = true }
xtensa-atomic-emulation-trap = "0.4.0"

[dev-dependencies]
aes = "0.8.3"
Expand All @@ -44,7 +43,10 @@ esp-alloc = "0.3.0"
esp-backtrace = { version = "0.9.0", features = ["esp32s2", "panic-handler", "print-uart", "exception-handler"] }
esp-hal-smartled = { version = "0.6.0", features = ["esp32s2"], path = "../esp-hal-smartled" }
esp-println = { version = "0.7.0", features = ["esp32s2"] }
heapless = "0.8.0"
heapless = { version = "0.8", features = ["portable-atomic"] }
# xtensa targets are not auto detected to need atomic emulation in the futures-* crates.
# see https://github.com/rust-lang/futures-rs/pull/2805 as to how to remove this dep for good
futures-util = { version = "0.3.17", default-features = false, features = ["portable-atomic"] }
hmac = { version = "0.12.1", default-features = false }
lis3dh-async = "0.8.0"
sha2 = { version = "0.10.8", default-features = false }
Expand All @@ -65,8 +67,6 @@ rt = []
ufmt = ["esp-hal-common/ufmt"]
vectored = ["esp-hal-common/vectored"]

portable-atomic = ["esp-hal-common/portable-atomic"]

# Embassy support
embassy = ["esp-hal-common/embassy"]
embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-80_000_000"]
Expand Down
Loading

0 comments on commit 05f9d21

Please sign in to comment.