I found the following piece of code in u-boot/arch/arm/lib/semihosting.c that uses bkpt
and other instructions and provides input and output operands even though they are not specified in the ASM template:
static noinline long smh_trap(unsigned int sysnum, void *addr){ register long result asm("r0");#if defined(CONFIG_ARM64) asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));#elif defined(CONFIG_CPU_V7M) asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));#else /* Note - untested placeholder */ asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));#endif return result;}
Minimal, verifiable example:
#include <stdio.h>#include <stdlib.h>int main(void){ register long result asm("r0"); void *addr = 0; unsigned int sysnum = 0; __asm__ volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr)); return EXIT_SUCCESS;}
According to ARM Architecture Reference Manual bkpt
instructiontakes a single imm parameter and according to my reading of GCC manualsection on inline assembly GCC does not allow providing operands if theyare not specified in the template. Output assembly generated with -S
:
.arch armv6 .eabi_attribute 28, 1 .eabi_attribute 20, 1 .eabi_attribute 21, 1 .eabi_attribute 23, 3 .eabi_attribute 24, 1 .eabi_attribute 25, 1 .eabi_attribute 26, 2 .eabi_attribute 30, 6 .eabi_attribute 34, 1 .eabi_attribute 18, 4 .file "bkpt-so.c" .text .align 2 .global main .arch armv6 .syntax unified .arm .fpu vfp .type main, %functionmain: @ args = 0, pretend = 0, frame = 8 @ frame_needed = 1, uses_anonymous_args = 0 @ link register save eliminated. str fp, [sp, #-4]! add fp, sp, #0 sub sp, sp, #12 mov r3, #0 str r3, [fp, #-8] mov r3, #0 str r3, [fp, #-12] ldr r2, [fp, #-12] ldr r3, [fp, #-8] mov r0, r2 .syntax divided@ 10 "bkpt-so.c" 1 bkpt #0xAB@ 0 "" 2 .arm .syntax unified mov r3, #0 mov r0, r3 add sp, fp, #0 @ sp needed ldr fp, [sp], #4 bx lr .size main, .-main .ident "GCC: (Raspbian 8.3.0-6+rpi1) 8.3.0" .section .note.GNU-stack,"",%progbits
So what's the point of "=r" (result) : "0"(sysnum), "r"(addr)
in this line:
__asm__ volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
?