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