Skip to content

Commit c53ccb0

Browse files
committed
baremetal: aarch64 semihosting exit
1 parent f90e690 commit c53ccb0

File tree

7 files changed

+65
-20
lines changed

7 files changed

+65
-20
lines changed

README.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ auto_reset_addr_64 = True
819819

820820
That patch breaks `--arch arm`, so don't forget to remove it if you want to go back to it.
821821

822+
When doing bare metal programming, it is likely that you will want to learn assembly language basics. Have a look at these tutorials for the userland part:
823+
824+
* https://github.com/cirosantilli/x86-assembly-cheat
825+
* https://github.com/cirosantilli/arm-assembly-cheat
826+
822827
For more information on baremetal, see the section: <<baremetal>>. The following subjects are particularly important:
823828

824829
* <<tracing>>
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.global main
2+
main:
3+
/* 0x20026 == ADP_Stopped_ApplicationExit */
4+
mov x1, #0x26
5+
movk x1, #2, lsl #16
6+
str x1, [sp,#0]
7+
8+
/* Exit status code. Host QEMU process exits with that status. */
9+
mov x0, #0
10+
str x0, [sp,#8]
11+
12+
/* x1 contains the address of parameter block.
13+
* Any memory address could be used. */
14+
mov x1, sp
15+
16+
/* SYS_EXIT */
17+
mov w0, #0x18
18+
19+
/* Do the semihosting call on A64. */
20+
hlt 0xf000

baremetal/arch/arm/semihost_exit.S

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.global main
22
main:
3+
/* SYS_EXIT */
34
mov r0, #0x18
5+
/* ADP_Stopped_ApplicationExit */
46
ldr r1, =#0x20026
7+
/* Do the semihosting call on A32. */
58
svc 0x00123456

baremetal/lib/common.c

+13
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,21 @@ int _write(int file, char *ptr, int len) {
5858
return len;
5959
}
6060

61+
/* Only 0 is supported for now, arm semihosting cannot handle it. */
6162
void _exit(int status) {
6263
#if defined(__arm__)
6364
__asm__ __volatile__ ("mov r0, #0x18; ldr r1, =#0x20026; svc 0x00123456");
65+
#elif defined(__aarch64__)
66+
/* TODO actually use the exit value here, just for fun. */
67+
__asm__ __volatile__ (
68+
"mov x1, #0x26\n" \
69+
"movk x1, #2, lsl #16\n" \
70+
"str x1, [sp,#0]\n" \
71+
"mov x0, #0\n" \
72+
"str x0, [sp,#8]\n" \
73+
"mov x1, sp\n" \
74+
"mov w0, #0x18\n" \
75+
"hlt 0xf000\n"
76+
);
6477
#endif
6578
}

build-all

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ for arch in $archs; do
2222
if [ ! "$arch" = x86_64 ]; then
2323
./build-crosstool-ng --arch "$arch"
2424
./build-baremetal --arch "$arch"
25-
./build-baremetal --arch "$arch" -g
26-
./build-baremetal --arch "$arch" -g --machine RealViewPBX
25+
./build-baremetal --arch "$arch" --gem5
26+
./build-baremetal --arch "$arch" --gem5 --machine RealViewPBX
2727
fi
2828
done

build-baremetal

+21-17
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,27 @@ def main(args, extra_args=None):
119119
bootloader_obj=bootloader_obj,
120120
common_obj=common_obj,
121121
)
122-
build_dir(
123-
os.path.join('arch', args.arch),
124-
gcc=gcc,
125-
cflags=cflags,
126-
entry_address=entry_address,
127-
bootloader_obj=bootloader_obj,
128-
common_obj=common_obj,
129-
)
130-
build_dir(
131-
os.path.join('arch', args.arch, 'no_bootloader'),
132-
gcc=gcc,
133-
cflags=cflags,
134-
entry_address=entry_address,
135-
bootloader_obj=bootloader_obj,
136-
common_obj=common_obj,
137-
bootloader=False,
138-
)
122+
arch_dir = os.path.join('arch', args.arch)
123+
if os.path.isdir(os.path.join(common.baremetal_src_dir, arch_dir)):
124+
build_dir(
125+
arch_dir,
126+
gcc=gcc,
127+
cflags=cflags,
128+
entry_address=entry_address,
129+
bootloader_obj=bootloader_obj,
130+
common_obj=common_obj,
131+
)
132+
arch_dir = os.path.join('arch', args.arch, 'no_bootloader')
133+
if os.path.isdir(os.path.join(common.baremetal_src_dir, arch_dir)):
134+
build_dir(
135+
arch_dir,
136+
gcc=gcc,
137+
cflags=cflags,
138+
entry_address=entry_address,
139+
bootloader_obj=bootloader_obj,
140+
common_obj=common_obj,
141+
bootloader=False,
142+
)
139143
return 0
140144

141145
if __name__ == '__main__':

run

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def main(args, extra_args=None):
271271
'-device', 'edu',
272272
])
273273
elif args.arch == 'arm' or args.arch == 'aarch64':
274-
extra_qemu_args.append('-semihosting')
274+
extra_emulator_args.append('-semihosting')
275275
if args.kgdb:
276276
kernel_cli += ' kgdboc=ttyAMA0,115200'
277277
if args.arch == 'arm':

0 commit comments

Comments
 (0)