Skip to content

Commit f9155a6

Browse files
committed
build: Add initial support for aarch64 build
This commit introduces initial support for aarch64 build. It adds build scripts and bootstrap assembly code. Signed-off-by: Akira Moroo <[email protected]>
1 parent 3c1dde1 commit f9155a6

File tree

9 files changed

+111
-2
lines changed

9 files changed

+111
-2
lines changed

aarch64-unknown-none.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"llvm-target": "aarch64-unknown-none",
3+
"abi": "softfloat",
4+
"arch": "aarch64",
5+
"data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128",
6+
"disable-redzone": true,
7+
"features": "+strict-align,-neon,-fp-armv8",
8+
"linker": "rust-lld",
9+
"linker-flavor": "ld.lld",
10+
"os": "none",
11+
"executables": true,
12+
"max-atomic-width": 128,
13+
"panic-strategy": "abort",
14+
"code-model": "small",
15+
"relocation-model": "pic",
16+
"target-pointer-width": "64",
17+
"pre-link-args": {
18+
"ld.lld": ["--script=aarch64-unknown-none.ld", "--oformat=binary"]
19+
}
20+
}

aarch64-unknown-none.ld

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
ENTRY(ram64_start)
2+
3+
/* Cloud Hypervisor Memory layout:
4+
DRAM: [0x4000_0000-0xfc00_0000]
5+
FDT: [0x4000_0000-0x401f_ffff)
6+
ACPI: [0x4020_0000-0x403f_ffff)
7+
kernel: [0x4048_0000-]
8+
The stack start is at the end of the DRAM region. */
9+
ram_min = 0x40480000;
10+
11+
SECTIONS
12+
{
13+
/* Mapping the program headers and note into RAM makes the file smaller. */
14+
. = ram_min;
15+
16+
/* These sections are mapped into RAM from the file. Omitting :ram from
17+
later sections avoids emitting empty sections in the final binary. */
18+
text_start = .;
19+
.text.boot : { *(.text.boot) }
20+
.text : { *(.text .text.*) }
21+
. = ALIGN(4K);
22+
text_end = .;
23+
.data : { *(.data .data.*) }
24+
.note : { *(.note) }
25+
.rodata : { *(.rodata .rodata.*) }
26+
27+
/* The BSS section isn't mapped from file data. It is just zeroed in RAM. */
28+
.bss : {
29+
bss_start = .;
30+
*(.bss .bss.*)
31+
bss_size = . - bss_start;
32+
}
33+
34+
/* Strip symbols from the output binary (comment out to get symbols) */
35+
/DISCARD/ : {
36+
*(.symtab)
37+
*(.strtab)
38+
}
39+
}

build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fn main() {
2+
println!("cargo:rerun-if-changed=aarch64-unknown-none.json");
3+
println!("cargo:rerun-if-changed=aarch64-unknown-none.ld");
24
println!("cargo:rerun-if-changed=x86_64-unknown-none.json");
35
println!("cargo:rerun-if-changed=x86_64-unknown-none.ld");
46
}

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[toolchain]
22
channel = "nightly-2022-06-10"
33
components = ["rust-src", "clippy", "rustfmt"]
4-
targets = ["x86_64-unknown-linux-gnu"]
4+
targets = ["aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu"]
55
profile = "default"

src/arch/aarch64/asm.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (C) 2022 Akira Moroo
3+
4+
use core::arch::global_asm;
5+
6+
global_asm!(include_str!("ram64.s"));

src/arch/aarch64/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (C) 2022 Akira Moroo
3+
4+
#[cfg(not(test))]
5+
pub mod asm;

src/arch/aarch64/ram64.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
/* Copyright (C) 2022 Akira Moroo */
3+
4+
.section .text.boot, "ax"
5+
.global ram64_start
6+
7+
ram64_start:
8+
/* UEFI "MZ" signature magic instruction */
9+
add x13, x18, #0x16 /* code0 */
10+
b jump_to_rust /* code1 */
11+
12+
.quad 0 /* text_offset */
13+
.quad 0 /* image_size */
14+
.quad 0 /* flags */
15+
.quad 0 /* res2 */
16+
.quad 0 /* res3 */
17+
.quad 0 /* res4 */
18+
19+
.long 0x644d5241 /* "ARM\x64" magic number */
20+
.long 0 /* res5 */
21+
.align 3
22+
23+
jump_to_rust:
24+
/* x0 typically points to device tree at entry */
25+
ldr x0, =0x40000000
26+
27+
/* setup stack */
28+
ldr x30, =0xfc000000
29+
mov sp, x30
30+
31+
/* x0: pointer to device tree */
32+
b rust64_start

src/arch/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// Copyright (C) 2022 Akira Moroo
33

4+
#[cfg(target_arch = "aarch64")]
5+
pub mod aarch64;
6+
47
#[cfg(target_arch = "x86_64")]
58
pub mod x86_64;

src/pe.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ struct Section {
3838
}
3939

4040
impl<'a> Loader<'a> {
41+
#[cfg(target_arch = "aarch64")]
42+
const MACHINE_TYPE: u16 = 0xaa64;
4143
#[cfg(target_arch = "x86_64")]
4244
const MACHINE_TYPE: u16 = 0x8664;
4345

44-
#[cfg(target_arch = "x86_64")]
46+
#[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))]
4547
const OPTIONAL_HEADER_MAGIC: u16 = 0x20b; // PE32+
4648

4749
pub fn new(file: &'a mut dyn crate::fat::Read) -> Loader {

0 commit comments

Comments
 (0)