Programmatically Call Undefined Command Exception

I want to raise an exception for the ARM Cortex-M3 Undefined command to test my test equipment. The IAR compiler supports this using the built-in assembly as follows:

asm ("udf.w # 0");

Unfortunately, the built-in assembler GNU CC does not know this opcode for the NXP LPC177x8x. He records the diagnostics:

ccw3kZ46.s:404: Error: bad instruction `udf.w #0'

How to create a function that throws an Undefined Instruction exception?

+8
source share
4 answers

Based on the Masta79 answer:

ARMv7-M " " - ARM DDI 0403D ( , ). : 0xf7fXaXXX ( "X" ). , , ( ):

asm volatile (".word 0xf7f0a000\n");

ARMv7-M .

+10

undefined <-20 > 0xDExx. , :

.short 0xde00

: ARMv7-M, A5.2.6.

( , 0xF7Fx, 0xAxxx undefined, 32- .)

+6

...

GCC

void __builtin_trap (void)

. GCC , ( ) . , - .

ARMv7:

(define_insn "trap"
  [(trap_if (const_int 1) (const_int 0))]
  ""
  "*
  if (TARGET_ARM)
    return \".inst\\t0xe7f000f0\";
  else
    return \".inst\\t0xdeff\";
  "
  [(set (attr "length")
    (if_then_else (eq_attr "is_thumb" "yes")
              (const_int 2)
              (const_int 4)))
   (set_attr "type" "trap")
   (set_attr "conds" "unconditional")]
)

, ARM gcc 0x7f000f0 (f0 00 f0 07) 0xdeff (ff de) ( /).

, :

these encodings match the UDF instruction that is defined in the most
recent edition of the ARM architecture reference manual.

Thumb: 0xde00 | imm8  (we chose 0xff for the imm8)
ARM: 0xe7f000f0 | (imm12 << 8) | imm4  (we chose to use 0 for both imms)

LLVM __builtin_trap: 0xe7ffdefe 0xdefe:

case ARM::TRAP: {
  // Non-Darwin binutils don't yet support the "trap" mnemonic.
  // FIXME: Remove this special case when they do.
  if (!Subtarget->isTargetDarwin()) {
    //.long 0xe7ffdefe @ trap
    uint32_t Val = 0xe7ffdefeUL;
    OutStreamer.AddComment("trap");
    OutStreamer.EmitIntValue(Val, 4);
    return;
  }
  break;
}
case ARM::tTRAP: {
  // Non-Darwin binutils don't yet support the "trap" mnemonic.
  // FIXME: Remove this special case when they do.
  if (!Subtarget->isTargetDarwin()) {
    //.short 57086 @ trap
    uint16_t Val = 0xdefe;
    OutStreamer.AddComment("trap");
    OutStreamer.EmitIntValue(Val, 2);
    return;
  }
  break;
}
+6

[1] ( UDF; . , ):

  • 0xe7f...f. - ARM A1 (ARMv4T, ARMv5T, ARMv6, ARMv7, ARMv8)
  • 0xde.. - T1 (ARMv4T, ARMv5T, ARMv6, ARMv7, ARMv8)
  • 0xf7f.a... - Thumb2 T2 (ARMv6T2, ARMv7, ARMv8)
  • 0x0000.... - ARMv8-A

, , , , .

, , .short .word, :

  • asm volatile (".short 0xde00\n");/* */
  • asm volatile (".word 0xe7f000f0\n");/* */
  • asm volatile (".word 0xe7f0def0\n");/* + */

Note that the last one will be decoded in 0xdef0, 0xe7f0in the context of the thumb, and therefore will also cause an undefined instruction exception.

[1] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.architecture.reference/index.html

[2] DDI0487D_b_armv8_arm.pdf

+5
source

All Articles