Skip to content

Commit c233ca7

Browse files
authored
Init syscall table (#4177)
* Init syscall table * CHANGELOG * Address review comments
1 parent caadbbb commit c233ca7

File tree

11 files changed

+338
-3
lines changed

11 files changed

+338
-3
lines changed

esp-hal/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,5 +699,9 @@ pub fn init(config: Config) -> Peripherals {
699699
#[cfg(feature = "psram")]
700700
crate::psram::init_psram(config.psram);
701701

702+
unsafe {
703+
esp_rom_sys::init_syscall_table();
704+
}
705+
702706
peripherals
703707
}

esp-rom-sys/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Initialize the syscall table (#4177)
1213

1314
### Changed
1415

16+
- Updated linker scripts (#4113)
1517

1618
### Fixed
1719

esp-rom-sys/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
![Crates.io](https://img.shields.io/crates/l/esp-rom-sys?labelColor=1C2C2E&style=flat-square)
77
[![Matrix](https://img.shields.io/matrix/esp-rs:matrix.org?label=join%20matrix&labelColor=1C2C2E&color=BEC5C9&logo=matrix&style=flat-square)](https://matrix.to/#/#esp-rs:matrix.org)
88

9-
ROM code support.
9+
ROM code support. This is an implementation detail of the esp-hal ecosystem crates.
1010

1111
This includes the definition of ROM code function addresses.
1212

esp-rom-sys/ld/esp32c2/rom/additional.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ PROVIDE ( atoi = __atoi );
2323
PROVIDE ( mktime = __mktime );
2424

2525
PROVIDE( abs = 0x40000558 );
26+
27+
syscall_table_ptr = 0x3fcdffd8;

esp-rom-sys/ld/esp32c3/rom/additional.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ EXTERN(__atoi);
2727
PROVIDE ( strnlen = __strnlen );
2828
PROVIDE ( atoi = __atoi );
2929
PROVIDE ( mktime = __mktime );
30+
31+
syscall_table_ptr = 0x3fcdffe0;

esp-rom-sys/ld/esp32c6/rom/additional.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ EXTERN(__atoi);
2323
PROVIDE ( strnlen = __strnlen );
2424
PROVIDE ( atoi = __atoi );
2525
PROVIDE ( mktime = __mktime );
26+
27+
syscall_table_ptr = 0x4087ffd4;

esp-rom-sys/ld/esp32h2/rom/additional.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ EXTERN(__atoi);
2525
PROVIDE ( strnlen = __strnlen );
2626
PROVIDE ( atoi = __atoi );
2727
PROVIDE ( mktime = __mktime );
28+
29+
syscall_table_ptr = 0x4084ffd4;

esp-rom-sys/ld/esp32s3/rom/additional.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ EXTERN(__atoi);
2727
PROVIDE ( strnlen = __strnlen );
2828
PROVIDE ( atoi = __atoi );
2929
PROVIDE ( mktime = __mktime );
30+
31+
syscall_table_ptr = 0x3fceffd4;

esp-rom-sys/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ macro_rules! before_snippet {
4242
}
4343

4444
pub mod rom;
45+
mod syscall;
46+
47+
pub use syscall::init_syscall_table;
4548

4649
/// This is needed by `libesp_rom.a` (if used)
4750
/// Other crates (i.e. esp-radio) also rely on this being defined somewhere
4851
#[unsafe(no_mangle)]
4952
unsafe extern "C" fn __assert_func(
5053
file: *const core::ffi::c_char,
51-
line: u32,
54+
line: i32,
5255
func: *const core::ffi::c_char,
5356
expr: *const core::ffi::c_char,
54-
) {
57+
) -> ! {
5558
unsafe {
5659
panic!(
5760
"__assert_func in {}:{} ({}): {}",

esp-rom-sys/src/syscall/mod.rs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#![allow(non_camel_case_types)]
2+
3+
// future chips or ECOs _might_ be different - at least ESP-IDF defines the struct per chip
4+
#[cfg_attr(
5+
any(esp32, esp32s2, esp32s3, esp32c2, esp32c3, esp32c6, esp32h2),
6+
path = "v1.rs"
7+
)]
8+
pub(crate) mod chip_specific;
9+
10+
#[repr(C)]
11+
#[derive(Debug, Copy, Clone)]
12+
pub struct _reent {
13+
_unused: [u8; 0],
14+
// the real struct is found here: https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/include/sys/reent.h;h=eafac960fd6ca374b7503f50bf8aa2bd1ee1573e;hb=refs/heads/main#l379
15+
}
16+
17+
pub type clock_t = ::core::ffi::c_long;
18+
19+
#[repr(C)]
20+
#[derive(Debug, Copy, Clone)]
21+
pub struct tms {
22+
_unused: [u8; 0],
23+
}
24+
25+
#[repr(C)]
26+
#[derive(Debug, Copy, Clone)]
27+
pub struct timeval {
28+
_unused: [u8; 0],
29+
}
30+
31+
#[repr(C)]
32+
#[derive(Debug, Copy, Clone)]
33+
pub struct stat {
34+
_unused: [u8; 0],
35+
}
36+
37+
#[repr(C)]
38+
#[derive(Debug, Copy, Clone)]
39+
pub struct __lock {
40+
_unused: [u8; 0],
41+
}
42+
43+
pub type _LOCK_T = *mut __lock;
44+
45+
pub type _lock_t = _LOCK_T;
46+
47+
#[repr(C)]
48+
#[derive(Debug, Copy, Clone)]
49+
pub struct _iobuf {
50+
pub _placeholder: *mut ::core::ffi::c_void,
51+
}
52+
53+
#[allow(clippy::upper_case_acronyms)]
54+
pub type FILE = _iobuf;
55+
56+
pub type va_list = *mut ::core::ffi::c_char;
57+
58+
static mut S_STUB_TABLE: core::mem::MaybeUninit<chip_specific::syscall_stub_table> =
59+
core::mem::MaybeUninit::uninit();
60+
61+
unsafe extern "C" fn not_implemented() {
62+
panic!("Function called via syscall table is not implemented!");
63+
}
64+
65+
unsafe extern "C" fn abort_wrapper() {
66+
panic!("Abort called from ROM code!");
67+
}
68+
69+
/// Initialize the syscall table.
70+
///
71+
/// # Safety
72+
/// Should only get called once.
73+
#[allow(clippy::missing_transmute_annotations)]
74+
pub unsafe fn init_syscall_table() {
75+
let syscall_table = unsafe {
76+
(&mut *core::ptr::addr_of_mut!(S_STUB_TABLE)).write(chip_specific::syscall_stub_table {
77+
__getreent: Some(core::mem::transmute(not_implemented as usize)),
78+
_malloc_r: Some(core::mem::transmute(not_implemented as usize)),
79+
_free_r: Some(core::mem::transmute(not_implemented as usize)),
80+
_realloc_r: Some(core::mem::transmute(not_implemented as usize)),
81+
_calloc_r: Some(core::mem::transmute(not_implemented as usize)),
82+
_abort: Some(abort_wrapper),
83+
_system_r: Some(core::mem::transmute(not_implemented as usize)),
84+
_rename_r: Some(core::mem::transmute(not_implemented as usize)),
85+
_times_r: Some(core::mem::transmute(not_implemented as usize)),
86+
_gettimeofday_r: Some(core::mem::transmute(not_implemented as usize)),
87+
_raise_r: Some(core::mem::transmute(not_implemented as usize)),
88+
_unlink_r: Some(core::mem::transmute(not_implemented as usize)),
89+
_link_r: Some(core::mem::transmute(not_implemented as usize)),
90+
_stat_r: Some(core::mem::transmute(not_implemented as usize)),
91+
_fstat_r: Some(core::mem::transmute(not_implemented as usize)),
92+
_sbrk_r: Some(core::mem::transmute(not_implemented as usize)),
93+
_getpid_r: Some(core::mem::transmute(not_implemented as usize)),
94+
_kill_r: Some(core::mem::transmute(not_implemented as usize)),
95+
_exit_r: Some(core::mem::transmute(not_implemented as usize)),
96+
_close_r: Some(core::mem::transmute(not_implemented as usize)),
97+
_open_r: Some(core::mem::transmute(not_implemented as usize)),
98+
_write_r: Some(core::mem::transmute(not_implemented as usize)),
99+
_lseek_r: Some(core::mem::transmute(not_implemented as usize)),
100+
_read_r: Some(core::mem::transmute(not_implemented as usize)),
101+
_retarget_lock_init: Some(core::mem::transmute(not_implemented as usize)),
102+
_retarget_lock_init_recursive: Some(core::mem::transmute(not_implemented as usize)),
103+
_retarget_lock_close: Some(core::mem::transmute(not_implemented as usize)),
104+
_retarget_lock_close_recursive: Some(core::mem::transmute(not_implemented as usize)),
105+
_retarget_lock_acquire: Some(core::mem::transmute(not_implemented as usize)),
106+
_retarget_lock_acquire_recursive: Some(core::mem::transmute(not_implemented as usize)),
107+
_retarget_lock_try_acquire: Some(core::mem::transmute(not_implemented as usize)),
108+
_retarget_lock_try_acquire_recursive: Some(core::mem::transmute(
109+
not_implemented as usize,
110+
)),
111+
_retarget_lock_release: Some(core::mem::transmute(not_implemented as usize)),
112+
_retarget_lock_release_recursive: Some(core::mem::transmute(not_implemented as usize)),
113+
_printf_float: Some(core::mem::transmute(not_implemented as usize)),
114+
_scanf_float: Some(core::mem::transmute(not_implemented as usize)),
115+
__assert_func: Some(super::__assert_func),
116+
__sinit: Some(core::mem::transmute(not_implemented as usize)),
117+
_cleanup_r: Some(core::mem::transmute(not_implemented as usize)),
118+
})
119+
};
120+
121+
cfg_if::cfg_if! {
122+
if #[cfg(esp32)] {
123+
unsafe extern "C" {
124+
static mut syscall_table_ptr_pro: *const chip_specific::syscall_stub_table;
125+
static mut syscall_table_ptr_app: *const chip_specific::syscall_stub_table;
126+
}
127+
unsafe {
128+
syscall_table_ptr_pro = syscall_table;
129+
syscall_table_ptr_app = syscall_table;
130+
}
131+
} else if #[cfg(esp32s2)] {
132+
unsafe extern "C" {
133+
static mut syscall_table_ptr_pro: *const chip_specific::syscall_stub_table;
134+
}
135+
unsafe { syscall_table_ptr_pro = syscall_table; }
136+
} else {
137+
unsafe extern "C" {
138+
static mut syscall_table_ptr: *const chip_specific::syscall_stub_table;
139+
}
140+
unsafe { syscall_table_ptr = syscall_table; }
141+
}
142+
};
143+
}

0 commit comments

Comments
 (0)