Skip to content

Commit

Permalink
arm: Minimal stub
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
  • Loading branch information
andyhhp committed Jun 21, 2022
1 parent 07e9624 commit bc86e2d
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
57 changes: 57 additions & 0 deletions arch/arm/arm32/head.S
@@ -0,0 +1,57 @@
.section ".text.head", "ax", %progbits
.code 32

#define ZIMAGE32_MAGIC 0x016f2818
#define ZIMAGE32_LE 0x04030201

/*
* The Linux zImage32 header has no offical documentation.
*
* It is formed as:
*
* u32 nops[8]; // Old a.out header
* u32 code0; // Branch to start logic
* u32 zimage32_magic; // 0x016f2818
* u32 start; // Start in RAM, or 0 for relocatable
* u32 end; // End in RAM, or size for relocatable
* u32 endian_magic; // 0x04030201 for LE, 0x01020304 ro BE
* u32 kexec_magic; // 0x45454545 (if kexec_addr present)
* u32 kexec_addr;
* ... // Future extentions
*/
.globl _start
_start:

.rept 8
nop
.endr

b startup /* code0 */
.word ZIMAGE32_MAGIC /* zimage32_magic */
.word 0 /* start: 0 -> relocatable */
.word _end - _start /* end: (size) */
.word ZIMAGE32_LE /* endian_magic */

startup:
/* Calculate r9 = phys-offset */
ldr r0, =_start
adr r8, _start
sub r9, r8, r0

/* sp = &stack[4096] + phys-offset */
ldr sp, =stack + 4096
add sp, sp, r9

bl test_main

1: b 1b

.type startup, %function
.size startup, . - startup

/*
* Local variables:
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/
51 changes: 51 additions & 0 deletions arch/arm/arm64/head.S
@@ -0,0 +1,51 @@
.section ".text.head", "ax", %progbits

#define ZIMAGE64_MAGIC 0x644d5241

#define ZIMAGE64_FLAGS_LE (0 << 0)
#define ZIMAGE64_FLAGS_PG_UNK (0 << 1)
#define ZIMAGE64_FLAGS_PHYS_ANY (1 << 3)

#define ZIMAGE64_FLAGS \
(ZIMAGE64_FLAGS_LE | ZIMAGE64_FLAGS_PG_UNK | ZIMAGE64_FLAGS_PHYS_ANY)

/*
* zImage64 header:
* See https://www.kernel.org/doc/html/latest/arm64/booting.html
*/
.globl _start
_start:
b startup /* code0 */
nop /* code1 */
.quad 0 /* text_offset: 0 -> relocatable */
.quad _end - _start /* size */
.quad ZIMAGE64_FLAGS /* flags */
.quad 0 /* reserved */
.quad 0 /* reserved */
.quad 0 /* reserved */
.long ZIMAGE64_MAGIC /* magic */
.long 0 /* reserved */

startup:
/* Calculate x21 = phys-offset */
ldr x22, =_start
adr x21, _start
sub x21, x21, x22

/* sp = &stack[4096] + phys-offset */
ldr x0, =stack + 4096
add sp, x0, x22

bl test_main

1: b 1b

.type startup, %function
.size startup, . - startup

/*
* Local variables:
* tab-width: 8
* indent-tabs-mode: nil
* End:
*/
60 changes: 60 additions & 0 deletions arch/arm/link.lds.S
@@ -0,0 +1,60 @@
#include <arch/page.h>

#if defined(__arm__)
OUTPUT_ARCH(arm)
OUTPUT_FORMAT("elf32-littlearm")
#elif defined(__aarch64__)
OUTPUT_ARCH(aarch64)
OUTPUT_FORMAT("elf64-littleaarch64")
#endif

ENTRY(_start)

SECTIONS
{
. = XTF_VIRT_START;

/* _start = .; Provided in .text.head */

.text : {
*(.text.head)
*(.text)
}

. = ALIGN(PAGE_SIZE);

.data : {
*(.data)

. = ALIGN(PAGE_SIZE);
*(.data.page_aligned)
}

. = ALIGN(PAGE_SIZE);

.rodata : {
*(.rodata)
*(.rodata.*)
}

. = ALIGN(PAGE_SIZE);

.bss : {
__start_bss = .;
*(.bss)

. = ALIGN(PAGE_SIZE);
*(.bss.page_aligned)

__end_bss = .;
}

_end = .;

/*
* It is possible for a GNU linker to add a .note.gnu.build-id section
* before .text which causes zimage header to be shifted resulting in
* a bad magic. Discard this section to prevent errors.
*/
/DISCARD/ : { *(.note.gnu.build-id) }
}
9 changes: 9 additions & 0 deletions tests/stub/Makefile
@@ -0,0 +1,9 @@
include $(ROOT)/build/common.mk

NAME := stub
CATEGORY := special
TEST-ENVS := $(ALL_ENVIRONMENTS)

obj-perenv += main.o

include $(ROOT)/build/gen.mk
53 changes: 53 additions & 0 deletions tests/stub/main.c
@@ -0,0 +1,53 @@
/**
* Minimal C logic
*/

#ifdef __aarch64__
# define COND(_32, _64) _64
#else
# define COND(_32, _64) _32
#endif

const char test_title[] = "Hello from ARM" COND("32", "64") "\n";

char __attribute__((section(".bss.page_aligned"))) stack[4096];

void test_main(void)
{
#if defined(__arm__) || defined(__aarch64__)
register unsigned long nr asm (COND("r12", "x16"));
register unsigned long a0 asm (COND("r0", "x0"));
register unsigned long a1 asm (COND("r1", "x1"));
register unsigned long a2 asm (COND("r2", "x2"));

nr = 18; /* __HYPERVISOR_console_io */
a0 = 0; /* CONSOLEIO_write */
a1 = sizeof(test_title); /* len */
a2 = (unsigned long)test_title; /* ptr */
asm volatile ("hvc #0xea1"
: [nr] "+r" (nr), [a0] "+r" (a0),
[a1] "+r" (a1), [a2] "+r" (a2)
:
: "memory");

unsigned int reason = 0; /* SHUTDOWN_poweroff */

nr = 29; /* __HYPERVISOR_sched_op */
a0 = 2; /* SCHEDOP_shutdown */
a1 = (unsigned long)&reason; /* ptr */
asm volatile ("hvc #0xea1"
: [nr] "+r" (nr), [a0] "+r" (a0), [a1] "+r" (a1)
:
: "memory");
#endif
}

/*
* Local variables:
* mode: C
* c-file-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/

0 comments on commit bc86e2d

Please sign in to comment.