Skip to main content

csky_rt/
lib.rs

1#![no_std]
2#![cfg_attr(target_arch = "csky", feature(asm_experimental_arch))]
3#![deny(missing_docs)]
4
5//! Minimal reset-to-Rust runtime for C-SKY bare-metal targets.
6//!
7//! A device or board supplies `memory.x`. This crate supplies stack setup,
8//! `.data`/`.bss` initialization, a generic 32/48/64-entry vector table, and
9//! the section layout in `link.x`. It deliberately contains no SoC names,
10//! peripheral addresses, interrupt numbers, or board policy.
11//!
12//! The default 32-entry table maps every non-reset entry to the default
13//! exception handler. `vector-table-48` and `vector-table-64` append 16 or 32
14//! generic external-interrupt entries. A device runtime may additionally map
15//! architecture exception 10 or 22 to the interrupt trampoline when that is
16//! its documented dispatch contract.
17//!
18//! Reset initially points VBR at the table in `FLASH`. Applications that need
19//! mutable vectors may call `relocate_vector_table` while interrupts remain
20//! disabled. This mirrors the two policies found in C-SKY SDKs without making
21//! either one universal: some SDKs retain flash vectors, while others copy
22//! the table to RAM and update VBR.
23
24pub use csky_rt_macros::entry;
25
26/// Required byte alignment of a C-SKY vector-table base.
27///
28/// C-SKY GCC startup implementations align VBR tables to 1 KiB. Keeping the
29/// same conservative contract across the supported CK80x profiles also makes
30/// flash and relocated RAM tables interchangeable.
31pub const VECTOR_TABLE_ALIGNMENT: usize = 1024;
32
33const VECTOR_TABLE_ENTRIES: usize = if cfg!(feature = "vector-table-64") {
34    64
35} else if cfg!(feature = "vector-table-48") {
36    48
37} else {
38    32
39};
40
41#[cfg(target_arch = "csky")]
42const EXTERNAL_INTERRUPT_ENTRIES: usize = VECTOR_TABLE_ENTRIES - 32;
43
44#[cfg(target_arch = "csky")]
45unsafe extern "C" {
46    #[allow(dead_code)]
47    fn csky_rt_irq();
48    fn csky_rt_default_exception();
49    static csky_rt_vector_table: u32;
50}
51
52#[cfg(target_arch = "csky")]
53macro_rules! emit_runtime {
54    ($exception_10:path, $exception_22:path) => {
55        core::arch::global_asm!(
56            r#"
57    .section .vector_table, "a"
58    .align 10
59    .globl csky_rt_vector_table
60csky_rt_vector_table:
61    .long csky_rt_reset
62    .rept 9
63    .long csky_rt_default_exception
64    .endr
65    .long {exception_10}
66    .rept 11
67    .long csky_rt_default_exception
68    .endr
69    .long {exception_22}
70    .rept 9
71    .long csky_rt_default_exception
72    .endr
73    /* A 32-entry table ends here. The 48/64-entry modes append generic
74       external IRQ slots; the owning device crate defines their meaning. */
75    .rept {external_entries}
76    .long csky_rt_irq
77    .endr
78
79    .section .text.csky_rt_reset, "ax"
80    .align 2
81    .globl csky_rt_reset
82    .type csky_rt_reset, %function
83csky_rt_reset:
84    lrw r0, __stack_top
85    mov16 sp, r0
86    lrw r0, csky_rt_vector_table
87    .short 0xc000, 0x6421 /* mtcr r0, cr<1, 0> (VBR) */
88    .short 0xc100, 0x7420 /* psrset ee */
89    br16 csky_rt_start
90.Lhalt:
91    br16 .Lhalt
92    .size csky_rt_reset, . - csky_rt_reset
93
94    .section .text.csky_rt_irq, "ax"
95    .align 2
96    .globl csky_rt_irq
97    .type csky_rt_irq, %function
98csky_rt_irq:
99    .if {nested_entry}
100    nie
101    .short 0x1462 /* ipush; LLVM global_asm does not inherit +e1 yet */
102    /* `ipush` preserves the low interrupt context. A high-register CPU
103       variant must additionally preserve r18-r31, and every wrapper must
104       preserve r15 before calling an ordinary Rust function. */
105    .if {high_registers}
106    .short 0x142e /* subi16 sp, sp, 56 */
107    .short 0xd64e, 0x1c2d /* stm32 r18-r31, (sp) */
108    .endif
109    .short 0x14d0 /* push16 r15 */
110    .if {high_registers} == 0
111    /* Keep the PC-relative call word-aligned. */
112    mov16 r0, r0
113    .endif
114    jbsr csky_irq_handler
115    ld32.w r15, (sp, 0)
116    addi16 sp, sp, 4
117    .if {high_registers}
118    .short 0xd24e, 0x1c2d /* ldm32 r18-r31, (sp) */
119    .short 0x140e /* addi16 sp, sp, 56 */
120    .endif
121    .short 0x1463 /* ipop; LLVM global_asm does not inherit +e1 yet */
122    nir
123    .else
124    subi16 sp, sp, 28
125    st32.w r0, (sp, 0)
126    st32.w r1, (sp, 4)
127    st32.w r2, (sp, 8)
128    st32.w r3, (sp, 12)
129    st32.w r12, (sp, 16)
130    st32.w r13, (sp, 20)
131    st32.w r15, (sp, 24)
132    subi16 sp, sp, 8
133    .short 0xc002, 0x6020 /* mfcr r0, cr<2, 0> (EPSR) */
134    st32.w r0, (sp, 4)
135    .short 0xc004, 0x6021 /* mfcr r1, cr<4, 0> (EPC) */
136    st32.w r1, (sp, 0)
137    jbsr csky_irq_handler
138    ld32.w r1, (sp, 0)
139    .short 0xc001, 0x6424 /* mtcr r1, cr<4, 0> (EPC) */
140    ld32.w r0, (sp, 4)
141    .short 0xc000, 0x6422 /* mtcr r0, cr<2, 0> (EPSR) */
142    addi16 sp, sp, 8
143    ld32.w r0, (sp, 0)
144    ld32.w r1, (sp, 4)
145    ld32.w r2, (sp, 8)
146    ld32.w r3, (sp, 12)
147    ld32.w r12, (sp, 16)
148    ld32.w r13, (sp, 20)
149    ld32.w r15, (sp, 24)
150    addi16 sp, sp, 28
151    .short 0xc000, 0x4020 /* rte */
152    .endif
153    .size csky_rt_irq, . - csky_rt_irq
154
155    .section .text.csky_rt_default_exception, "ax"
156    .align 2
157    .globl csky_rt_default_exception
158    .type csky_rt_default_exception, %function
159csky_rt_default_exception:
160    br16 csky_rt_default_exception
161    .size csky_rt_default_exception, . - csky_rt_default_exception
162
163    .section .note.GNU-stack, "", %progbits
164"#,
165            external_entries = const EXTERNAL_INTERRUPT_ENTRIES,
166            nested_entry = const cfg!(feature = "nested-interrupt-entry") as usize,
167            high_registers = const (cfg!(feature = "high-registers")
168                || cfg!(feature = "ck805")
169                || cfg!(feature = "ck807")) as usize,
170            exception_10 = sym $exception_10,
171            exception_22 = sym $exception_22,
172        );
173    };
174}
175
176#[cfg(all(
177    target_arch = "csky",
178    feature = "exception-10-interrupt",
179    feature = "exception-22-interrupt"
180))]
181emit_runtime!(csky_rt_irq, csky_rt_irq);
182
183#[cfg(all(
184    target_arch = "csky",
185    feature = "exception-10-interrupt",
186    not(feature = "exception-22-interrupt")
187))]
188emit_runtime!(csky_rt_irq, csky_rt_default_exception);
189
190#[cfg(all(
191    target_arch = "csky",
192    not(feature = "exception-10-interrupt"),
193    feature = "exception-22-interrupt"
194))]
195emit_runtime!(csky_rt_default_exception, csky_rt_irq);
196
197#[cfg(all(
198    target_arch = "csky",
199    not(feature = "exception-10-interrupt"),
200    not(feature = "exception-22-interrupt")
201))]
202emit_runtime!(csky_rt_default_exception, csky_rt_default_exception);
203
204const CPU_FEATURE_COUNT: usize = cfg!(feature = "ck801") as usize
205    + cfg!(feature = "ck802") as usize
206    + cfg!(feature = "ck803") as usize
207    + cfg!(feature = "ck804") as usize
208    + cfg!(feature = "ck805") as usize
209    + cfg!(feature = "ck807") as usize;
210
211const _: () = assert!(
212    CPU_FEATURE_COUNT <= 1,
213    "csky-rt CPU features are mutually exclusive"
214);
215
216#[cfg(target_arch = "csky")]
217const _: () = assert!(
218    CPU_FEATURE_COUNT == 1,
219    "select exactly one csky-rt CPU feature: ck801, ck802, ck803, ck804, ck805, or ck807"
220);
221
222#[cfg(all(feature = "vector-table-48", feature = "vector-table-64"))]
223compile_error!("csky-rt vector-table-48 and vector-table-64 are mutually exclusive");
224
225#[cfg(all(feature = "nested-interrupt-entry", feature = "ck807"))]
226compile_error!(
227    "csky-rt nested-interrupt-entry is not supported on CK807; use the non-nested exception frame"
228);
229
230#[cfg(all(feature = "high-registers", any(feature = "ck801", feature = "ck802")))]
231compile_error!("csky-rt high-registers is not available on CK801 or CK802");
232
233#[cfg(target_arch = "csky")]
234unsafe extern "C" {
235    static mut __bss_start: u8;
236    static mut __bss_end: u8;
237    static mut __data_start: u8;
238    static mut __data_end: u8;
239    static __data_load: u8;
240
241    fn csky_main() -> !;
242}
243
244/// Returns the number of entries emitted in the selected vector table.
245#[inline]
246pub const fn vector_table_entries() -> usize {
247    VECTOR_TABLE_ENTRIES
248}
249
250/// Returns the required byte alignment of a vector-table base.
251#[inline]
252pub const fn vector_table_alignment() -> usize {
253    VECTOR_TABLE_ALIGNMENT
254}
255
256/// Copies the flash vector table to RAM and points VBR at the copy.
257///
258/// The reset path uses the immutable flash table by default. This function is
259/// the explicit alternative for devices that require mutable vectors.
260///
261/// # Safety
262///
263/// `destination` must be non-null, writable, aligned to
264/// [`VECTOR_TABLE_ALIGNMENT`], and valid for [`vector_table_entries`] `u32`
265/// values. It must not overlap the source flash table and must remain allocated
266/// for as long as VBR refers to it. Interrupts and exceptions that use the
267/// table must remain masked until copying and VBR replacement complete.
268#[cfg(target_arch = "csky")]
269pub unsafe fn relocate_vector_table(destination: *mut u32) {
270    let source = core::ptr::addr_of!(csky_rt_vector_table);
271    for index in 0..VECTOR_TABLE_ENTRIES {
272        unsafe {
273            destination
274                .add(index)
275                .write_volatile(source.add(index).read_volatile());
276        }
277    }
278    csky::asm::sync();
279    unsafe { csky::register::vbr::write(destination as u32) };
280}
281
282/// Symbol referenced by the assembly reset entry.
283///
284/// # Safety
285///
286/// This must be entered only by the runtime reset assembly after establishing
287/// a valid stack and masking interrupts.
288#[cfg(target_arch = "csky")]
289#[unsafe(no_mangle)]
290pub unsafe extern "C" fn csky_rt_start() -> ! {
291    unsafe {
292        csky::interrupt::disable();
293        initialize_data();
294        zero_bss();
295        csky_main()
296    }
297}
298
299#[cfg(target_arch = "csky")]
300unsafe fn initialize_data() {
301    let mut source = core::ptr::addr_of!(__data_load);
302    let mut destination = core::ptr::addr_of_mut!(__data_start);
303    let end = core::ptr::addr_of_mut!(__data_end);
304
305    if !core::ptr::eq(source, destination.cast_const()) {
306        while destination < end {
307            unsafe {
308                destination.write_volatile(source.read_volatile());
309                destination = destination.add(1);
310                source = source.add(1);
311            }
312        }
313    }
314}
315
316#[cfg(target_arch = "csky")]
317unsafe fn zero_bss() {
318    let mut current = core::ptr::addr_of_mut!(__bss_start);
319    let end = core::ptr::addr_of_mut!(__bss_end);
320
321    while current < end {
322        unsafe {
323            current.write_volatile(0);
324            current = current.add(1);
325        }
326    }
327}
328
329#[cfg(test)]
330mod tests {
331    #[test]
332    fn vector_table_uses_conservative_vbr_alignment() {
333        assert_eq!(super::vector_table_alignment(), 1024);
334        assert!(super::vector_table_alignment().is_power_of_two());
335    }
336
337    #[test]
338    #[cfg(not(any(feature = "vector-table-48", feature = "vector-table-64")))]
339    fn default_vector_table_has_32_entries() {
340        assert_eq!(super::vector_table_entries(), 32);
341    }
342
343    #[test]
344    #[cfg(feature = "vector-table-48")]
345    fn selected_vector_table_has_48_entries() {
346        assert_eq!(super::vector_table_entries(), 48);
347    }
348
349    #[test]
350    #[cfg(feature = "vector-table-64")]
351    fn selected_vector_table_has_64_entries() {
352        assert_eq!(super::vector_table_entries(), 64);
353    }
354}