Path: blob/master/tools/testing/selftests/arm64/fp/fpsimd-test.S
26289 views
// SPDX-License-Identifier: GPL-2.0-only1// Copyright (C) 2015-2019 ARM Limited.2// Original author: Dave Martin <Dave.Martin@arm.com>3//4// Simple FPSIMD context switch test5// Repeatedly writes unique test patterns into each FPSIMD register6// and reads them back to verify integrity.7//8// for x in `seq 1 NR_CPUS`; do fpsimd-test & pids=$pids\ $! ; done9// (leave it running for as long as you want...)10// kill $pids1112#include <asm/unistd.h>13#include "assembler.h"14#include "asm-offsets.h"1516#define NVR 3217#define MAXVL_B (128 / 8)1819.macro _vldr Vn:req, Xt:req20ld1 {v\Vn\().2d}, [x\Xt]21.endm2223.macro _vstr Vn:req, Xt:req24st1 {v\Vn\().2d}, [x\Xt]25.endm2627// Generate accessor functions to read/write programmatically selected28// FPSIMD registers.29// x0 is the register index to access30// x1 is the memory address to read from (getv,setp) or store to (setv,setp)31// All clobber x0-x232define_accessor setv, NVR, _vldr33define_accessor getv, NVR, _vstr3435// Declare some storate space to shadow the SVE register contents:36.pushsection .text37.data38.align 439vref:40.space MAXVL_B * NVR41scratch:42.space MAXVL_B43.popsection4445// Generate a test pattern for storage in SVE registers46// x0: pid (16 bits)47// x1: register number (6 bits)48// x2: generation (4 bits)49function pattern50orr w1, w0, w1, lsl #1651orr w2, w1, w2, lsl #285253ldr x0, =scratch54mov w1, #MAXVL_B / 455560: str w2, [x0], #457add w2, w2, #(1 << 22)58subs w1, w1, #159bne 0b6061ret62endfunction6364// Get the address of shadow data for FPSIMD V-register V<xn>65.macro _adrv xd, xn, nrtmp66ldr \xd, =vref67mov x\nrtmp, #1668madd \xd, x\nrtmp, \xn, \xd69.endm7071// Set up test pattern in a FPSIMD V-register72// x0: pid73// x1: register number74// x2: generation75function setup_vreg76mov x4, x307778mov x6, x179bl pattern80_adrv x0, x6, 281mov x5, x082ldr x1, =scratch83bl memcpy8485mov x0, x686mov x1, x587bl setv8889ret x490endfunction9192// Trivial memory compare: compare x2 bytes starting at address x0 with93// bytes starting at address x1.94// Returns only if all bytes match; otherwise, the program is aborted.95// Clobbers x0-x5.96function memcmp97cbz x2, 1f9899mov x5, #01000: ldrb w3, [x0, x5]101ldrb w4, [x1, x5]102add x5, x5, #1103cmp w3, w4104b.ne barf105subs x2, x2, #1106b.ne 0b1071081: ret109endfunction110111// Verify that a FPSIMD V-register matches its shadow in memory, else abort112// x0: reg number113// Clobbers x0-x5.114function check_vreg115mov x3, x30116117_adrv x5, x0, 6118mov x4, x0119ldr x7, =scratch120121mov x0, x7122mov x1, x6123bl memfill_ae124125mov x0, x4126mov x1, x7127bl getv128129mov x0, x5130mov x1, x7131mov x2, x6132mov x30, x3133b memcmp134endfunction135136// Modify live register state, the signal return will undo our changes137function irritator_handler138// Increment the irritation signal count (x23):139ldr x0, [x2, #ucontext_regs + 8 * 23]140add x0, x0, #1141str x0, [x2, #ucontext_regs + 8 * 23]142143// Corrupt some random V-regs144movi v0.8b, #7145movi v9.16b, #9146movi v31.8b, #31147148ret149endfunction150151function tickle_handler152// Increment the signal count (x23):153ldr x0, [x2, #ucontext_regs + 8 * 23]154add x0, x0, #1155str x0, [x2, #ucontext_regs + 8 * 23]156157ret158endfunction159160function terminate_handler161mov w21, w0162mov x20, x2163164puts "Terminated by signal "165mov w0, w21166bl putdec167puts ", no error, iterations="168ldr x0, [x20, #ucontext_regs + 8 * 22]169bl putdec170puts ", signals="171ldr x0, [x20, #ucontext_regs + 8 * 23]172bl putdecn173174mov x0, #0175mov x8, #__NR_exit176svc #0177endfunction178179// w0: signal number180// x1: sa_action181// w2: sa_flags182// Clobbers x0-x6,x8183function setsignal184str x30, [sp, #-((sa_sz + 15) / 16 * 16 + 16)]!185186mov w4, w0187mov x5, x1188mov w6, w2189190add x0, sp, #16191mov x1, #sa_sz192bl memclr193194mov w0, w4195add x1, sp, #16196str w6, [x1, #sa_flags]197str x5, [x1, #sa_handler]198mov x2, #0199mov x3, #sa_mask_sz200mov x8, #__NR_rt_sigaction201svc #0202203cbz w0, 1f204205puts "sigaction failure\n"206b .Labort2072081: ldr x30, [sp], #((sa_sz + 15) / 16 * 16 + 16)209ret210endfunction211212// Main program entry point213.globl _start214function _start215enable_gcs216217mov x23, #0 // signal count218219mov w0, #SIGINT220adr x1, terminate_handler221mov w2, #SA_SIGINFO222bl setsignal223224mov w0, #SIGTERM225adr x1, terminate_handler226mov w2, #SA_SIGINFO227bl setsignal228229mov w0, #SIGUSR1230adr x1, irritator_handler231mov w2, #SA_SIGINFO232orr w2, w2, #SA_NODEFER233bl setsignal234235mov w0, #SIGUSR2236adr x1, tickle_handler237mov w2, #SA_SIGINFO238orr w2, w2, #SA_NODEFER239bl setsignal240241// Sanity-check and report the vector length242243mov x19, #128244cmp x19, #128245b.lo 1f246cmp x19, #2048247b.hi 1f248tst x19, #(8 - 1)249b.eq 2f2502511: puts "Bad vector length: "252mov x0, x19253bl putdecn254b .Labort2552562: puts "Vector length:\t"257mov x0, x19258bl putdec259puts " bits\n"260261// Obtain our PID, to ensure test pattern uniqueness between processes262263mov x8, #__NR_getpid264svc #0265mov x20, x0266267puts "PID:\t"268mov x0, x20269bl putdecn270271mov x22, #0 // generation number, increments per iteration272.Ltest_loop:273274mov x21, #0 // Set up V-regs & shadow with test pattern2750: mov x0, x20276mov x1, x21277and x2, x22, #0xf278bl setup_vreg279add x21, x21, #1280cmp x21, #NVR281b.lo 0b282283// Can't do this when SVE state is volatile across SVC:284mov x8, #__NR_sched_yield // Encourage preemption285svc #0286287mov x21, #02880: mov x0, x21289bl check_vreg290add x21, x21, #1291cmp x21, #NVR292b.lo 0b293294add x22, x22, #1295b .Ltest_loop296297.Labort:298mov x0, #0299mov x1, #SIGABRT300mov x8, #__NR_kill301svc #0302endfunction303304function barf305mov x10, x0 // expected data306mov x11, x1 // actual data307mov x12, x2 // data size308309puts "Mismatch: PID="310mov x0, x20311bl putdec312puts ", iteration="313mov x0, x22314bl putdec315puts ", reg="316mov x0, x21317bl putdecn318puts "\tExpected ["319mov x0, x10320mov x1, x12321bl dumphex322puts "]\n\tGot ["323mov x0, x11324mov x1, x12325bl dumphex326puts "]\n"327328mov x8, #__NR_exit329mov x1, #1330svc #0331endfunction332333334