asm: make use regular asserts that will just work on baremetal

Previously had wonky line pointer in asm_main. New interface simpler and more portable.

Add tests for ASSERT_EQ_ and family in arm and aarch64, previously on x86_64.

ASSERT_EQ_ and family in ARM can now either take =123, =addr or var, before this
the = was added on macros so var was not possible.

Define the main function directly in assembly, the C driver was useless.
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-05-23 00:00:00 +00:00
parent 72200dee4e
commit c8c4f89854
90 changed files with 1003 additions and 978 deletions

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-data-processing-instructions */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Immediate encoding.
*
@@ -11,7 +11,7 @@ LKMC_ENTRY
mov r0, 1
/* r1 = r0 + 2 */
add r1, r0, 2
LKMC_ASSERT_EQ(r1, 3)
LKMC_ASSERT_EQ(r1, =3)
/* If src == dest, we can omit one of them.
*
@@ -19,12 +19,12 @@ LKMC_ENTRY
*/
mov r0, 1
add r0, 2
LKMC_ASSERT_EQ(r0, 3)
LKMC_ASSERT_EQ(r0, =3)
/* Same as above but explicit. */
mov r0, 1
add r0, r0, 2
LKMC_ASSERT_EQ(r0, 3)
LKMC_ASSERT_EQ(r0, =3)
#if 0
/* But we cannot omit the register if there is a shift when using .syntx unified:
@@ -44,7 +44,7 @@ LKMC_ENTRY
mov r0, 1
mov r1, 2
add r2, r0, r1
LKMC_ASSERT_EQ(r2, 3)
LKMC_ASSERT_EQ(r2, =3)
/* Register encoding, omit implicit register.
*
@@ -53,6 +53,6 @@ LKMC_ENTRY
mov r0, 1
mov r1, 2
add r1, r0
LKMC_ASSERT_EQ(r1, 3)
LKMC_ASSERT_EQ(r1, =3)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,37 +1,37 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-addressing-modes */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Offset mode with immediate. Add 4 to the address register,
* which ends up * reading myvar6 instead of myvar.
*/
adr r4, myvar
ldr r5, [r4, 4]
LKMC_ASSERT_EQ(r5, 0x9ABCDEF0)
LKMC_ASSERT_EQ(r5, =0x9ABCDEF0)
/* r4 was not modified. */
LKMC_ASSERT_EQ(r4, myvar)
LKMC_ASSERT_EQ(r4, =myvar)
/* Pre-indexed mode: modify register, then use it. */
adr r4, myvar
ldr r5, [r4, 4]!
LKMC_ASSERT_EQ(r5, 0x9ABCDEF0)
LKMC_ASSERT_EQ(r5, =0x9ABCDEF0)
/* r4 was modified. */
LKMC_ASSERT_EQ(r4, myvar6)
LKMC_ASSERT_EQ(r4, =myvar6)
/* Post-indexed mode: use register, then modify it. */
adr r4, myvar
ldr r5, [r4], 4
LKMC_ASSERT_EQ(r5, 0x12345678)
LKMC_ASSERT_EQ(r5, =0x12345678)
/* r4 was modified. */
LKMC_ASSERT_EQ(r4, myvar6)
LKMC_ASSERT_EQ(r4, =myvar6)
/* Offset in register. */
adr r4, myvar
mov r5, 4
ldr r6, [r4, r5]
LKMC_ASSERT_EQ(r6, 0x9ABCDEF0)
LKMC_ASSERT_EQ(r6, =0x9ABCDEF0)
/* Offset in shifted register:
* r6 =
@@ -42,9 +42,9 @@ LKMC_ENTRY
adr r4, myvar
mov r5, 2
ldr r6, [r4, r5, lsl 1]
LKMC_ASSERT_EQ(r6, 0x9ABCDEF0)
LKMC_ASSERT_EQ(r6, =0x9ABCDEF0)
LKMC_EXIT
LKMC_EPILOGUE
myvar:
.word 0x12345678
myvar6:

View File

@@ -1,11 +1,11 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-adr-instruction */
#include "common.h"
#include <lkmc.h>
.data
data_label:
.word 0x1234678
LKMC_ENTRY
LKMC_PROLOGUE
adr r4, label
/* objdump tells us that this uses the literal pool,
* it does not get converted to adr, which is the better
@@ -30,4 +30,4 @@ label:
*/
adr r5, data_label
#endif
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,27 +1,27 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-bitwise-instructions */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* 0x00 && 0xFF == 0x00 */
mov r0, 0x00
and r0, 0xFF
LKMC_ASSERT_EQ(r0, 0x00)
LKMC_ASSERT_EQ(r0, =0x00)
/* 0x0F && 0xF0 == 0x00 */
mov r0, 0x0F
and r0, 0xF0
LKMC_ASSERT_EQ(r0, 0x00)
LKMC_ASSERT_EQ(r0, =0x00)
/* 0x0F && 0xFF == 0x0F */
mov r0, 0x0F
and r0, 0xFF
LKMC_ASSERT_EQ(r0, 0x0F)
LKMC_ASSERT_EQ(r0, =0x0F)
/* 0xF0 && 0xFF == 0xF0 */
mov r0, 0xF0
and r0, 0xFF
LKMC_ASSERT_EQ(r0, 0xF0)
LKMC_ASSERT_EQ(r0, =0xF0)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,9 +1,9 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-b-instruction */
#include "common.h"
LKMC_ENTRY
#include <lkmc.h>
LKMC_PROLOGUE
/* Jump over the fail. 26-bit PC-relative. */
b ok
LKMC_FAIL
LKMC_ASSERT_FAIL
ok:
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-beq-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Smaller*/
mov r0, 1
@@ -25,4 +25,4 @@ LKMC_ENTRY
LKMC_ASSERT(bgt)
LKMC_ASSERT(bne)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,10 +1,10 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-bfi-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
ldr r0, =0x11223344
ldr r1, =0xFFFFFFFF
bfi r1, r0, 8, 16
LKMC_ASSERT_EQ(r1, 0xFF3344FF)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =0xFF3344FF)
LKMC_EPILOGUE

View File

@@ -1,10 +1,10 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-bic-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* 0x0F & ~0x55 == 0x0F & 0xAA == 0x0A */
mov r0, 0x0F
bic r0, 0x55
LKMC_ASSERT_EQ(r0, 0x0A)
LKMC_EXIT
LKMC_ASSERT_EQ(r0, =0x0A)
LKMC_EPILOGUE

View File

@@ -1,12 +1,12 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-bl-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
mov r0, 1
bl inc
LKMC_ASSERT_EQ(r0, 2)
LKMC_EXIT
LKMC_ASSERT_EQ(r0, =2)
LKMC_EPILOGUE
/* void inc(int *i) { (*i)++ } */
inc:

View File

@@ -1,17 +1,17 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#data-bitwise-instructions */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
ldr r0, =0x7FFFFFFF
clz r1, r0
LKMC_ASSERT_EQ(r1, 1)
LKMC_ASSERT_EQ(r1, =1)
ldr r0, =0x3FFFFFFF
clz r1, r0
LKMC_ASSERT_EQ(r1, 2)
LKMC_ASSERT_EQ(r1, =2)
ldr r0, =0x1FFFFFFF
clz r1, r0
LKMC_ASSERT_EQ(r1, 3)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =3)
LKMC_EPILOGUE

View File

@@ -1,7 +1,7 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#gnu-gas-assembler-comments */
#include "common.h"
LKMC_ENTRY
#include <lkmc.h>
LKMC_PROLOGUE
# mycomment
@ mycomment
/* # only works at the beginning of the line.
@@ -11,4 +11,4 @@ LKMC_ENTRY
nop # mycomment
#endif
nop @ mycomment
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,83 +0,0 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#userland-assembly-c-standard-library */
#ifndef COMMON_ARCH_H
#define COMMON_ARCH_H
.syntax unified
/* Assert that a register equals a constant.
* * reg: the register to check
* * const: the constant to compare to. Only works for literals or labels, not for registers.
* For register / register comparison, use LKMC_ASSERT_EQ_REG.
*/
#define LKMC_ASSERT_EQ(reg, const) \
mov r0, reg; \
ldr r1, =const; \
LKMC_ASSERT_EQ_DO; \
;
#define LKMC_ASSERT_EQ_DO \
bl lkmc_assert_eq_32; \
cmp r0, 0; \
LKMC_ASSERT(beq); \
;
#define LKMC_ASSERT_EQ_REG(reg1, reg2) \
str reg2, [sp, -4]!; \
mov r0, reg1; \
ldr r1, [sp], 4; \
LKMC_ASSERT_EQ_DO; \
;
/* Assert that two arrays are the same. */
#define LKMC_ASSERT_MEMCMP(label1, label2, const_size) \
ldr r0, =label1; \
ldr r1, =label2; \
ldr r2, =const_size; \
bl lkmc_assert_memcmp; \
cmp r0, 0; \
LKMC_ASSERT(beq); \
;
/* Store all callee saved registers, and LR in case we make further BL calls.
*
* Also save the input arguments r0-r3 on the stack, so we can access them later on,
* despite those registers being overwritten.
*/
#define LKMC_ENTRY \
.text; \
.global asm_main; \
asm_main: \
stmdb sp!, {r0-r12, lr}; \
asm_main_after_prologue: \
;
/* Meant to be called at the end of LKMC_ENTRY.*
*
* Branching to "fail" makes tests fail with exit status 1.
*
* If LKMC_EXIT is reached, the program ends successfully.
*
* Restore LR and bx jump to it to return from asm_main.
*/
#define LKMC_EXIT \
mov r0, 0; \
mov r1, 0; \
b pass; \
fail: \
ldr r1, [sp]; \
str r0, [r1]; \
mov r0, 1; \
pass: \
add sp, 16; \
ldmia sp!, {r4-r12, lr}; \
bx lr; \
;
/* Always fail. */
#define LKMC_FAIL \
ldr r0, =__LINE__; \
b fail; \
;
#endif

View File

@@ -1,16 +1,18 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-conditional-execution */
#include "common.h"
#include <lkmc.h>
LKMC_PROLOGUE
mov r4, 0
mov r5, 1
LKMC_ENTRY
mov r0, 0
mov r1, 1
cmp r0, 1
/* Previous cmp failed, skip this operation. */
addeq r1, 1
LKMC_ASSERT_EQ(r1, 1)
cmp r0, 0
cmp r4, 1
addeq r5, 1
LKMC_ASSERT_EQ(r5, =1)
/* Previous passed, do this operation. */
addeq r1, 1
LKMC_ASSERT_EQ(r1, 2)
LKMC_EXIT
cmp r4, 0
addeq r5, 1
LKMC_ASSERT_EQ(r5, =2)
LKMC_EPILOGUE

View File

@@ -1,6 +1,6 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#gnu-gas-assembler data sizes */
#include "common.h"
#include <lkmc.h>
.data
mybyte:
@@ -14,12 +14,12 @@ myquad:
myocta:
.octa 0x123456789ABCDEF0123456789ABCDEF0
theend:
LKMC_ENTRY
LKMC_PROLOGUE
#define ASSERT_DIFF(label1, label2, result) \
ldr r0, =label1; \
ldr r1, =label2; \
sub r0, r1, r0; \
LKMC_ASSERT_EQ(r0, result)
LKMC_ASSERT_EQ(r0, =result)
ASSERT_DIFF(mybyte, myword, 1)
ASSERT_DIFF(myword, mylong, 4)
@@ -27,4 +27,4 @@ LKMC_ENTRY
ASSERT_DIFF(myquad, myocta, 8)
ASSERT_DIFF(myocta, theend, 16)
#undef ASSERT_DIF
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-gnu-instruction-gas-assembler-immediates */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* This is the default. We hack it in common.h however. */
.syntax divided
/* These fail. */
@@ -21,4 +21,4 @@ LKMC_ENTRY
mov r0, 0x1
mov r0, $1
mov r0, $0x1
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,6 +1,6 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-loop-instruction-over-array */
#include "common.h"
#include <lkmc.h>
#define NELEM 4
#define ELEM_SIZE 4
@@ -11,7 +11,7 @@ my_array:
my_array_expect:
.word 0x11111112, 0x22222223, 0x33333334, 0x44444445
LKMC_ENTRY
LKMC_PROLOGUE
/* Increment. */
ldr r0, =my_array
mov r1, NELEM
@@ -23,5 +23,5 @@ increment:
sub r1, 1
cmp r1, 0
bne increment
LKMC_ASSERT_MEMCMP(my_array, my_array_expect, 0x10)
LKMC_EXIT
LKMC_ASSERT_MEMCMP(my_array, my_array_expect, =0x10)
LKMC_EPILOGUE

View File

@@ -1,6 +1,6 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-loop-instruction-over-array */
#include "common.h"
#include <lkmc.h>
#define NELEM 4
#define ELEM_SIZE 4
@@ -11,7 +11,7 @@ my_array_0:
my_array_1:
.word 0x55555555, 0x66666666, 0x77777777, 0x88888888
LKMC_ENTRY
LKMC_PROLOGUE
/* Load r5, r6, r7 and r8 starting from the address in r4. Don't change r4 */
ldr r4, =my_array_0
@@ -20,11 +20,11 @@ LKMC_ENTRY
ldr r7, =0
ldr r8, =0
ldmia r4, {r5-r8}
LKMC_ASSERT_EQ(r4, my_array_0)
LKMC_ASSERT_EQ(r5, 0x11111111)
LKMC_ASSERT_EQ(r6, 0x22222222)
LKMC_ASSERT_EQ(r7, 0x33333333)
LKMC_ASSERT_EQ(r8, 0x44444444)
LKMC_ASSERT_EQ(r4, =my_array_0)
LKMC_ASSERT_EQ(r5, =0x11111111)
LKMC_ASSERT_EQ(r6, =0x22222222)
LKMC_ASSERT_EQ(r7, =0x33333333)
LKMC_ASSERT_EQ(r8, =0x44444444)
/* Swapping the order of r5 and r6 on the mnemonic makes no difference to load order.
*
@@ -38,8 +38,8 @@ LKMC_ENTRY
ldr r5, =0
ldr r6, =0
ldmia r4, {r6,r5}
LKMC_ASSERT_EQ(r5, 0x11111111)
LKMC_ASSERT_EQ(r6, 0x22222222)
LKMC_ASSERT_EQ(r5, =0x11111111)
LKMC_ASSERT_EQ(r6, =0x22222222)
#endif
/* Modify the array */
@@ -51,11 +51,11 @@ LKMC_ENTRY
stmdb r4, {r5-r8}
/* Verify that my_array_0 changed and is equal to my_array_1. */
LKMC_ASSERT_MEMCMP(my_array_0, my_array_1, 0x10)
LKMC_ASSERT_MEMCMP(my_array_0, my_array_1, =0x10)
/* Load registers and increment r4. */
ldr r4, =my_array_0
ldmia r4!, {r5-r8}
LKMC_ASSERT_EQ(r4, my_array_1)
LKMC_ASSERT_EQ(r4, =my_array_1)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-ldr-instruction-pseudo-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Mnemonic for a PC relative load:
*
@@ -12,13 +12,13 @@ LKMC_ENTRY
* ....
*/
ldr r0, myvar
LKMC_ASSERT_EQ(r0, 0x12345678)
LKMC_ASSERT_EQ(r0, =0x12345678)
/* Mnemonic PC relative load with an offset.
* Load myvar2 instead of myvar.
*/
ldr r0, myvar + 4
LKMC_ASSERT_EQ(r0, 0x9ABCDEF0)
LKMC_ASSERT_EQ(r0, =0x9ABCDEF0)
/* First store the address in r0 using a magic =myvar, which creates
* a new variable containing the address and PC-relative addresses it
@@ -33,17 +33,17 @@ LKMC_ENTRY
*/
ldr r0, =myvar
ldr r1, [r0]
LKMC_ASSERT_EQ(r1, 0x12345678)
LKMC_ASSERT_EQ(r1, =0x12345678)
/* More efficiently, use r0 as the address to read, and write to r0 itself. */
ldr r0, =myvar
ldr r0, [r0]
LKMC_ASSERT_EQ(r0, 0x12345678)
LKMC_ASSERT_EQ(r0, =0x12345678)
/* Same as =myvar but store a constant to a register.
* Can also be done with movw and movt. */
ldr r0, =0x11112222
LKMC_ASSERT_EQ(r0, 0x11112222)
LKMC_ASSERT_EQ(r0, =0x11112222)
/* We can also use GAS tolower16 and topper16 and movw and movt
* to load the address of myvar into r0 with two immediates.
@@ -56,9 +56,9 @@ LKMC_ENTRY
movw r0, #:lower16:myvar
movt r0, #:upper16:myvar
ldr r1, [r0]
LKMC_ASSERT_EQ(r1, 0x12345678)
LKMC_ASSERT_EQ(r1, =0x12345678)
LKMC_EXIT
LKMC_EPILOGUE
myvar:
.word 0x12345678
myvar2:

View File

@@ -1,12 +1,12 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-ldrh-instruction-and-ldrb */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
ldr r0, =myvar
mov r1, 0x0
ldrb r1, [r0]
LKMC_ASSERT_EQ(r1, 0x00000078)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =0x00000078)
LKMC_EPILOGUE
myvar:
.word 0x12345678

View File

@@ -1,12 +1,12 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-ldrh-instruction-and-ldrb */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
ldr r0, =myvar
mov r1, 0x0
ldrh r1, [r0]
LKMC_ASSERT_EQ(r1, 0x00005678)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =0x00005678)
LKMC_EPILOGUE
myvar:
.word 0x12345678

View File

@@ -1,6 +1,6 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#calling-convention */
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-calling-convention */
#include "common.h"
#include <lkmc.h>
.data
puts_s:
@@ -12,7 +12,7 @@ my_array_0:
my_array_1:
.word 0x55555555, 0x66666666, 0x77777777, 0x88888888
LKMC_ENTRY
LKMC_PROLOGUE
/* puts("hello world") */
/* r0 is first argument. */
ldr r0, =puts_s
@@ -49,11 +49,11 @@ LKMC_ENTRY
ldr r1, =my_array_1
ldr r2, =0x10
bl memcmp
LKMC_ASSERT_EQ(r0, 0)
LKMC_ASSERT_EQ(r0, =0)
/* exit(0) */
mov r0, 0
bl exit
/* Never reached, just for the fail symbol. */
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -0,0 +1,32 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#userland-assembly */
#include <lkmc.h>
LKMC_PROLOGUE
ldr r4, =0x12345678
ldr r5, =0x12345678
/* Passing examples. */
/* Register immediate. */
LKMC_ASSERT_EQ(r4, =0x12345678)
#if 0
/* Syntax not supported. */
LKMC_ASSERT_EQ(=0x12345678, r4)
#endif
/* Register register. */
LKMC_ASSERT_EQ_REG(r4, r5)
LKMC_ASSERT_EQ_REG(r5, r4)
/* Register memory. */
LKMC_ASSERT_EQ(r4, myvar)
#if 0
/* Syntax not supported. */
LKMC_ASSERT_EQ(myvar, r4)
#endif
/* Now let's fail. */
LKMC_ASSERT_EQ(r4, =0x12345679)
LKMC_EPILOGUE
myvar: .quad 0x12345678

View File

@@ -0,0 +1,15 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#userland-assembly */
#include <lkmc.h>
LKMC_PROLOGUE
/* Pass */
LKMC_ASSERT_MEMCMP(var0, var1, =0x10)
LKMC_ASSERT_MEMCMP(var0, var1, size)
/* Fail */
LKMC_ASSERT_MEMCMP(var0, var2, =0x10)
LKMC_EPILOGUE
var0: .long 0x11111111, 0x22222222, 0x33333333, 0x44444444
var1: .long 0x11111111, 0x22222222, 0x33333333, 0x44444444
var2: .long 0x11111111, 0x22222223, 0x23333333, 0x44444444
size: .long 0x10

View File

@@ -1,19 +1,19 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-mov-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Immediate. */
mov r0, 0
LKMC_ASSERT_EQ(r0, 0)
LKMC_ASSERT_EQ(r0, =0)
mov r0, 1
LKMC_ASSERT_EQ(r0, 1)
LKMC_ASSERT_EQ(r0, =1)
/* Register. */
mov r0, 0
mov r1, 1
mov r1, r0
LKMC_ASSERT_EQ(r1, 0)
LKMC_ASSERT_EQ(r1, =0)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-movw-and-movt-instructions */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* movt (top) and movw (TODO what is w) set the higher
* and lower 16 bits of the register.
@@ -10,7 +10,7 @@ LKMC_ENTRY
movw r0, 0xFFFF
movt r0, 0x1234
add r0, 1
LKMC_ASSERT_EQ(r0, 0x12350000)
LKMC_ASSERT_EQ(r0, =0x12350000)
/* movw also zeroes out the top bits, allowing small 16-bit
* C constants to be assigned in a single instruction.
@@ -22,6 +22,6 @@ LKMC_ENTRY
*/
ldr r0, =0x12345678
movw r0, 0x1111
LKMC_ASSERT_EQ(r0, 0x00001111)
LKMC_ASSERT_EQ(r0, =0x00001111)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -3,13 +3,13 @@
* Multiplication.
*/
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* 2 * 3 = 6 */
mov r0, 0
mov r1, 2
mov r2, 3
mul r1, r2
LKMC_ASSERT_EQ(r1, 6)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =6)
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-nop-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Disassembles as:
*
* ....
@@ -29,4 +29,4 @@ LKMC_ENTRY
/* And there are other nops as well? Disassembles as `and`. */
and r0, r0, r0
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-ldmia-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Save sp before push. */
mov r4, sp
@@ -19,13 +19,13 @@ LKMC_ENTRY
mov r7, 0
mov r8, 0
pop {r7, r8}
LKMC_ASSERT_EQ(r7, 1)
LKMC_ASSERT_EQ(r8, 2)
LKMC_ASSERT_EQ(r7, =1)
LKMC_ASSERT_EQ(r8, =2)
/* Check that stack pointer moved down by 8 bytes
* (2 registers x 4 bytes each).
*/
sub r4, r5
LKMC_ASSERT_EQ(r4, 8)
LKMC_ASSERT_EQ(r4, =8)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -3,10 +3,10 @@
* Reverse bit order.
*/
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
ldr r0, =0b00000001001000110100010101100101
rbit r1, r0
LKMC_ASSERT_EQ(r1, 0b10100110101000101100010010000000)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =0b10100110101000101100010010000000)
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#assembly-registers */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* 13 general purpose registers. */
mov r0, 0
@@ -39,12 +39,12 @@ LKMC_ENTRY
* https://stackoverflow.com/questions/32304646/arm-assembly-branch-to-address-inside-register-or-memory/54145818#54145818
*/
ldr pc, =10f
LKMC_FAIL
LKMC_ASSERT_FAIL
10:
/* Same with r15, which is the same as pc. */
ldr r15, =10f
LKMC_FAIL
LKMC_ASSERT_FAIL
10:
/* Another example with mov reading from pc. */
@@ -60,10 +60,10 @@ pc_addr:
b 1f
.word 0x12345678
1:
LKMC_ASSERT_EQ(r0, 0x12345678)
LKMC_ASSERT_EQ(r0, =0x12345678)
/* We can also use fp in GNU GAS assembly. */
mov r11, 0
mov fp, 1
LKMC_ASSERT_EQ(r11, 1)
LKMC_EXIT
LKMC_ASSERT_EQ(r11, =1)
LKMC_EPILOGUE

View File

@@ -3,16 +3,16 @@
* Reverse byte order.
*/
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* All bytes in register. */
ldr r0, =0x11223344
rev r1, r0
LKMC_ASSERT_EQ(r1, 0x44332211)
LKMC_ASSERT_EQ(r1, =0x44332211)
/* Groups of 16-bits. */
ldr r0, =0x11223344
rev16 r1, r0
LKMC_ASSERT_EQ(r1, 0x22114433)
LKMC_EXIT
LKMC_ASSERT_EQ(r1, =0x22114433)
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-s-suffix */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Result is 0, set beq. */
movs r0, 0
@@ -20,7 +20,7 @@ LKMC_ENTRY
/* movs still moves... */
mov r0, 0
movs r0, 1
LKMC_ASSERT_EQ(r0, 1)
LKMC_ASSERT_EQ(r0, =1)
/* add: the result is 0. */
mov r0, 1
@@ -32,4 +32,4 @@ LKMC_ENTRY
adds r0, 1
LKMC_ASSERT(bne)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-shift-suffixes */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* lsr */
ldr r0, =0xFFF00FFF
@@ -76,4 +76,4 @@ LKMC_ENTRY
ldr r2, =0x00000120
LKMC_ASSERT_EQ_REG(r1, r2)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,13 +1,13 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#load-and-store-instructions */
#include "common.h"
#include <lkmc.h>
.data;
/* Must be in the .data section, since we want to modify it. */
myvar:
.word 0x12345678
LKMC_ENTRY
LKMC_PROLOGUE
/* Sanity check. */
ldr r0, =myvar
ldr r1, [r0]
@@ -57,4 +57,4 @@ var_in_same_section:
str r1, =myvar
#endif
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -3,12 +3,12 @@
* Subtraction.
*/
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* 3 - 2 == 1 , register version.*/
mov r0, 3
mov r1, 2
sub r0, r0, r1
LKMC_ASSERT_EQ(r0, 1)
LKMC_EXIT
LKMC_ASSERT_EQ(r0, =1)
LKMC_EPILOGUE

View File

@@ -7,9 +7,9 @@
.syntax unified
.text
.thumb_func
.global asm_main
asm_main:
asm_main_after_prologue:
.global main
main:
main_after_prologue:
/* CBZ: cmp and branch if zero instruction. Equivalent to CMP + BEQ.
* TODO create an interesting assertion here.

View File

@@ -3,9 +3,9 @@
* Test. Same as ands, but don't store the result, just update flags.
*/
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* 0x0F && 0xF0 == 0x00, so beq. */
mov r0, 0x0F
@@ -17,6 +17,6 @@ LKMC_ENTRY
tst r0, 0x0F
LKMC_ASSERT(bne)
# r0 was not modified.
LKMC_ASSERT_EQ(r0, 0xFF)
LKMC_ASSERT_EQ(r0, =0xFF)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,7 +1,7 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-udf-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
udf 0
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,9 +1,9 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#vfp
* Adapted from: https://mindplusplus.wordpress.com/2013/06/27/arm-vfp-vector-programming-part-2-examples/ */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* Minimal single precision floating point example.
* TODO: floating point representation constraints due to 4-byte instruction?
*/
@@ -39,7 +39,7 @@ my_float_sum:
vadd.f32 s2, s0, s1
ldr r0, =my_float_sum
vstr.f32 s2, [r0]
LKMC_ASSERT_MEMCMP(my_float_sum, my_float_sum_expect, 4)
LKMC_ASSERT_MEMCMP(my_float_sum, my_float_sum_expect, =4)
#if 0
/* We can't do pseudo vldr as for ldr, fails with:
@@ -69,4 +69,4 @@ my_float_sum:
vmov s1, s0
vmov r1, s1
LKMC_ASSERT_EQ_REG(r0, r1)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,10 +1,10 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-vadd-instruction */
#include "common.h"
#include <lkmc.h>
.bss
output: .skip 16
LKMC_ENTRY
LKMC_PROLOGUE
/* Integer. */
.data
input0_u: .long 0xF1F1F1F1, 0xF2F2F2F2, 0xF3F3F3F3, 0xF4F4F4F4
@@ -20,7 +20,7 @@ LKMC_ENTRY
vadd.u ## size q2, q0, q1; \
ldr r0, =output; \
vst1.u ## size {q2}, [r0]; \
LKMC_ASSERT_MEMCMP(output, expect_u_ ## size, 0x10)
LKMC_ASSERT_MEMCMP(output, expect_u_ ## size, =0x10)
/* vadd.u32
*
@@ -54,7 +54,7 @@ LKMC_ENTRY
vadd.f ## size q2, q0, q1; \
ldr r0, =output; \
vst1. ## size {q2}, [r0]; \
LKMC_ASSERT_MEMCMP(output, expect_f_ ## size, 0x10)
LKMC_ASSERT_MEMCMP(output, expect_f_ ## size, =0x10)
/* 4x 32-bit. */
TEST(32)
@@ -68,4 +68,4 @@ LKMC_ENTRY
TEST(64)
#endif
#undef TEST
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-vcvt-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* SIMD positive. */
.data
vcvt_positive_0: .float 1.25, 2.5, 3.75, 4.0
@@ -15,7 +15,7 @@ LKMC_ENTRY
vcvt.u32.f32 q1, q0
ldr r0, =vcvt_positive_result
vst1.32 {q1}, [r0]
LKMC_ASSERT_MEMCMP(vcvt_positive_result, vcvt_positive_expect, 0x10)
LKMC_ASSERT_MEMCMP(vcvt_positive_result, vcvt_positive_expect, =0x10)
/* SIMD negative. */
.data
@@ -29,7 +29,7 @@ LKMC_ENTRY
vcvt.s32.f32 q1, q0
ldr r0, =vcvt_negative_result
vst1.32 {q1}, [r0]
LKMC_ASSERT_MEMCMP(vcvt_negative_result, vcvt_negative_expect, 0x10)
LKMC_ASSERT_MEMCMP(vcvt_negative_result, vcvt_negative_expect, =0x10)
/* Floating point. */
.data
@@ -44,7 +44,7 @@ LKMC_ENTRY
vcvt.u32.f32 s0, s0
ldr r0, =vcvt_positive_float_result
vst1.32 {d0}, [r0]
LKMC_ASSERT_MEMCMP(vcvt_positive_float_result, vcvt_positive_float_expect, 0x8)
LKMC_ASSERT_MEMCMP(vcvt_positive_float_result, vcvt_positive_float_expect, =0x8)
/* Floating point but with immediates.
*
@@ -85,6 +85,6 @@ LKMC_ENTRY
LKMC_ASSERT_MEMCMP(
vcvt_positive_double_result,
vcvt_positive_double_expect,
0x4
=0x4
)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-vcvta-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
/* SIMD positive. */
.data
vcvta_positive_0: .float 1.25, 2.5, 3.75, 4.0
@@ -18,7 +18,7 @@ LKMC_ENTRY
LKMC_ASSERT_MEMCMP(
vcvta_positive_result,
vcvta_positive_expect,
0x10
=0x10
)
/* SIMD negative. */
@@ -36,6 +36,6 @@ LKMC_ENTRY
LKMC_ASSERT_MEMCMP(
vcvta_negative_result,
vcvta_negative_expect,
0x10
=0x10
)
LKMC_EXIT
LKMC_EPILOGUE

View File

@@ -1,8 +1,8 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#arm-vcvtrr-instruction */
#include "common.h"
#include <lkmc.h>
LKMC_ENTRY
LKMC_PROLOGUE
.data
vcvtr_0: .float 1.25, 2.5, 3.75, 4.0
vcvtr_expect_zero: .word 1, 2, 3, 4
@@ -24,7 +24,7 @@ LKMC_ENTRY
LKMC_ASSERT_MEMCMP(
vcvtr_result_zero,
vcvtr_expect_zero,
0x10
=0x10
)
#if 0
@@ -40,7 +40,7 @@ LKMC_ENTRY
LKMC_ASSERT_MEMCMP(
vcvtr_result_plus_infinity,
vcvtr_expect_plus_infinity,
0x10
=0x10
)
#endif
LKMC_EXIT
LKMC_EPILOGUE