Skip to content

Commit 2ef31f0

Browse files
authored
Fix interrupt table lookup for chain-loaded apps (#17)
* Set mtvec based on linker script symbols instead of hard coding. * add linker script asserts to ensure 1KB VT alignment * make sure linker assert for v2 is only included when v2 feature flag is set.
1 parent 5396b69 commit 2ef31f0

6 files changed

Lines changed: 34 additions & 5 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ASSERT(_highcode_vma_start % 1024 == 0, "vector table must be 1KB aligned for Qingke V2");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ASSERT(_start % 1024 == 0, "vector table must be 1KB aligned for Qingke V2");

qingke-rt/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,21 @@ fn main() {
6767
fs::write(out_dir.join("link.x"), include_bytes!("link-no-highcode.x")).unwrap();
6868
}
6969

70+
// V2 requires the vector table to be 1KB-aligned
71+
let has_v2 = env::var("CARGO_FEATURE_V2").is_ok();
72+
let asserts: &[u8] = match (has_v2, has_highcode_feature) {
73+
(true, true) => include_bytes!("assert-v2-align-highcode.x"),
74+
(true, false) => include_bytes!("assert-v2-align-no-highcode.x"),
75+
_ => b"",
76+
};
77+
fs::write(out_dir.join("assert-align.x"), asserts).unwrap();
78+
7079
println!("cargo:rustc-link-search={}", out_dir.display());
7180

7281
println!("cargo:rerun-if-changed=link-highcode.x");
7382
println!("cargo:rerun-if-changed=link-no-highcode.x");
83+
println!("cargo:rerun-if-changed=assert-v2-align-highcode.x");
84+
println!("cargo:rerun-if-changed=assert-v2-align-no-highcode.x");
7485
println!("cargo:rerun-if-changed=build.rs");
7586

7687
let target = env::var("TARGET").unwrap();

qingke-rt/link-highcode.x

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ SECTIONS
4949
. = ALIGN(4);
5050
} >FLASH AT>FLASH
5151

52-
/* highcode section will be copied to RAM offset 0x0 */
52+
/* highcode section will be copied to RAM */
5353
.highcode : ALIGN(4)
5454
{
5555
_highcode_lma = LOADADDR(.highcode);
5656
PROVIDE(_highcode_vma_start = .);
57-
LONG(0x00000000); /* Placeholder for the first vector */
57+
LONG(_start); /* Placeholder for the first vector */
5858
KEEP(*(.vector_table.core_interrupts));
5959
KEEP(*(.vector_table.external_interrupts));
6060
KEEP(*(.vector_table.exceptions));
@@ -118,3 +118,5 @@ SECTIONS
118118
.eh_frame (INFO) : { KEEP(*(.eh_frame)) }
119119
.eh_frame_hdr (INFO) : { *(.eh_frame_hdr) }
120120
}
121+
122+
INCLUDE assert-align.x

qingke-rt/link-no-highcode.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,5 @@ SECTIONS
103103
.eh_frame (INFO) : { KEEP(*(.eh_frame)) }
104104
.eh_frame_hdr (INFO) : { *(.eh_frame_hdr) }
105105
}
106+
107+
INCLUDE assert-align.x

qingke-rt/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,22 @@ unsafe extern "C" fn qingke_setup_interrupts() {
209209
#[cfg(not(feature = "v3a"))]
210210
unsafe {
211211
#[cfg(feature = "highcode")]
212-
mtvec::write(0x20000000, TrapMode::VectoredAddress);
213-
212+
{
213+
unsafe extern "C" {
214+
static _highcode_vma_start: u8;
215+
}
216+
mtvec::write(
217+
&raw const _highcode_vma_start as usize,
218+
TrapMode::VectoredAddress,
219+
);
220+
}
214221
#[cfg(not(feature = "highcode"))]
215-
mtvec::write(0x00000000, TrapMode::VectoredAddress);
222+
{
223+
unsafe extern "C" {
224+
static _start: u8;
225+
}
226+
mtvec::write(&raw const _start as usize, TrapMode::VectoredAddress);
227+
}
216228
}
217229

218230
unsafe {

0 commit comments

Comments
 (0)