1/* 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 Warner Losh 5 * Copyright (c) 2023 Stormshield 6 * Copyright (c) 2023 Klara, Inc. 7 */ 8 9#include <sys/syscall.h> 10 11#define STDOUT_FILENO 1 12#define SWP_MAGIC 0xffc0 13#define SWPB_MAGIC 0xc0c0 14 15 .text 16 .file "swp_test.S" 17 .syntax unified 18 .globl main 19 .p2align 2 20 .type main,%function 21 .code 32 22 23main: 24 sub sp, #0x04 25 /* r4 is our failed test counter */ 26 mov r4, #0 27 28 movw r0, :lower16:.L.testheader 29 movt r0, :upper16:.L.testheader 30 ldr r1, =(.L.testheaderEnd - .L.testheader - 1) 31 bl print 32 33 /* Target address */ 34 mov r0, #0x03 35 str r0, [sp] 36 mov r0, sp 37 38 /* Load value */ 39 mov r1, #SWP_MAGIC 40 41 /* swp it */ 42 swp r0, r1, [r0] 43 44 /* Old value should be 3 */ 45 cmp r0, #0x03 46 bne 1f 47 48 /* Check stack value */ 49 ldr r0, [sp] 50 mov r1, #SWP_MAGIC 51 cmp r0, r1 52 bne 1f 53 b 2f 54 551: 56 /* Denote the failed test */ 57 add r4, #1 58 /* "No" part of the notification */ 59 movw r0, :lower16:.L.boknot 60 movt r0, :upper16:.L.boknot 61 ldr r1, =(.L.boknotEnd - .L.boknot - 1) 62 bl print 63 642: 65 /* Notify */ 66 movw r0, :lower16:.L.ok1 67 movt r0, :upper16:.L.ok1 68 ldr r1, =(.L.ok1End - .L.ok1 - 1) 69 bl print 70 71 movw r5, #SWPB_MAGIC 72 movt r5, #SWPB_MAGIC 73 74 /* Using r6 as our accumulator */ 75 mov r6, sp 76 /* Simplify the loop */ 77 sub r6, #1 783: 79 /* Restore our magic value every time */ 80 str r5, [sp] 81 /* Move on to the next byte */ 82 add r6, #1 83 84 /* swp it in */ 85 mov r0, r6 86 mov r1, #3 87 swpb r0, r1, [r0] 88 89 /* Check the old value */ 90 cmp r0, #0xc0 91 bne 6f 92 93 /* Check the stack value */ 94 ldrb r0, [r6] 95 cmp r0, #0x03 96 bne 6f 97 98 /* Just loop over the rest of the word and check those values. */ 99 mov r1, r6 100 sub r1, sp 101 102 mov r0, #0x00 1034: 104 cmp r0, r1 105 beq 5f 106 107 /* Check the next byte */ 108 ldrb r3, [sp, r0] 109 cmp r3, #0xc0 110 bne 6f 111 1125: 113 add r0, #0x01 114 cmp r0, #0x04 115 /* Hit the end, this one succeeded */ 116 beq 7f 117 118 /* Move on to the next byte */ 119 b 4b 120 1216: 122 /* Denote the failed test */ 123 add r4, #1 124 /* "No" part of the notification */ 125 movw r0, :lower16:.L.boknot 126 movt r0, :upper16:.L.boknot 127 ldr r1, =(.L.boknotEnd - .L.boknot - 1) 128 bl print 129 130 /* FALLTHROUGH */ 1317: 132 /* "ok" part */ 133 movw r0, :lower16:.L.bok 134 movt r0, :upper16:.L.bok 135 ldr r1, =(.L.bokEnd - .L.bok - 1) 136 bl print 137 138 /* test number */ 139 mov r0, r6 140 sub r0, sp 141 add r0, #0x32 /* "0" + 2 because we start at test 2. */ 142 mov r1, #0x01 143 str r0, [sp] 144 mov r0, sp 145 bl print 146 147 /* boklabel */ 148 movw r0, :lower16:.L.boklabel 149 movt r0, :upper16:.L.boklabel 150 ldr r1, =(.L.boklabelEnd - .L.boklabel - 1) 151 bl print 152 153 /* index */ 154 mov r0, r6 155 sub r0, sp 156 add r0, #0x30 /* "0" */ 157 str r0, [sp] 158 mov r0, sp 159 mov r1, #0x01 160 bl print 161 162 /* bokterm */ 163 movw r0, :lower16:.L.bokterm 164 movt r0, :upper16:.L.bokterm 165 ldr r1, =(.L.boktermEnd - .L.bokterm - 1) 166 bl print 167 168 mov r0, sp 169 add r0, #0x3 170 cmp r0, r6 171 bne 3b 172 173 mov r0, r4 /* retval */ 174 ldr r7, =SYS_exit 175 swi 0 176 177 .p2align 2 178 .type print,%function 179 .code 32 180print: 181 /* r0 - string, r1 = size */ 182 mov r2, r1 183 mov r1, r0 184 ldr r0, =STDOUT_FILENO 185 ldr r7, =SYS_write 186 swi 0 187 188 bx lr 189 190.L.testheader: 191 .asciz "1..5\n" 192.L.testheaderEnd: 193 .size .L.testheader, .L.testheaderEnd - .L.testheader 194 195 /* Maybe not the most efficient, but meh. */ 196.L.ok1: 197 .asciz "ok 1 - swp\n" 198.L.ok1End: 199 .size .L.ok1, .L.ok1End - .L.ok1 200 201.L.boknot: 202 .asciz "not " 203.L.boknotEnd: 204 .size .L.boknot, .L.boknotEnd - .L.boknot 205.L.bok: 206 .asciz "ok " 207.L.bokEnd: 208 .size .L.bok, .L.bokEnd - .L.bok 209.L.boklabel: 210 .asciz " - swpb[" 211.L.boklabelEnd: 212 .size .L.boklabel, .L.boklabelEnd - .L.boklabel 213.L.bokterm: 214 .asciz "]\n" 215.L.boktermEnd: 216 .size .L.bokterm, .L.boktermEnd - .L.bokterm 217