fpsimd-test.S 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2015-2019 ARM Limited.
  3. // Original author: Dave Martin <[email protected]>
  4. //
  5. // Simple FPSIMD context switch test
  6. // Repeatedly writes unique test patterns into each FPSIMD register
  7. // and reads them back to verify integrity.
  8. //
  9. // for x in `seq 1 NR_CPUS`; do fpsimd-test & pids=$pids\ $! ; done
  10. // (leave it running for as long as you want...)
  11. // kill $pids
  12. #include <asm/unistd.h>
  13. #include "assembler.h"
  14. #include "asm-offsets.h"
  15. #define NVR 32
  16. #define MAXVL_B (128 / 8)
  17. .macro _vldr Vn:req, Xt:req
  18. ld1 {v\Vn\().2d}, [x\Xt]
  19. .endm
  20. .macro _vstr Vn:req, Xt:req
  21. st1 {v\Vn\().2d}, [x\Xt]
  22. .endm
  23. // Generate accessor functions to read/write programmatically selected
  24. // FPSIMD registers.
  25. // x0 is the register index to access
  26. // x1 is the memory address to read from (getv,setp) or store to (setv,setp)
  27. // All clobber x0-x2
  28. define_accessor setv, NVR, _vldr
  29. define_accessor getv, NVR, _vstr
  30. // Declare some storate space to shadow the SVE register contents:
  31. .pushsection .text
  32. .data
  33. .align 4
  34. vref:
  35. .space MAXVL_B * NVR
  36. scratch:
  37. .space MAXVL_B
  38. .popsection
  39. // Generate a test pattern for storage in SVE registers
  40. // x0: pid (16 bits)
  41. // x1: register number (6 bits)
  42. // x2: generation (4 bits)
  43. function pattern
  44. orr w1, w0, w1, lsl #16
  45. orr w2, w1, w2, lsl #28
  46. ldr x0, =scratch
  47. mov w1, #MAXVL_B / 4
  48. 0: str w2, [x0], #4
  49. add w2, w2, #(1 << 22)
  50. subs w1, w1, #1
  51. bne 0b
  52. ret
  53. endfunction
  54. // Get the address of shadow data for FPSIMD V-register V<xn>
  55. .macro _adrv xd, xn, nrtmp
  56. ldr \xd, =vref
  57. mov x\nrtmp, #16
  58. madd \xd, x\nrtmp, \xn, \xd
  59. .endm
  60. // Set up test pattern in a FPSIMD V-register
  61. // x0: pid
  62. // x1: register number
  63. // x2: generation
  64. function setup_vreg
  65. mov x4, x30
  66. mov x6, x1
  67. bl pattern
  68. _adrv x0, x6, 2
  69. mov x5, x0
  70. ldr x1, =scratch
  71. bl memcpy
  72. mov x0, x6
  73. mov x1, x5
  74. bl setv
  75. ret x4
  76. endfunction
  77. // Trivial memory compare: compare x2 bytes starting at address x0 with
  78. // bytes starting at address x1.
  79. // Returns only if all bytes match; otherwise, the program is aborted.
  80. // Clobbers x0-x5.
  81. function memcmp
  82. cbz x2, 1f
  83. mov x5, #0
  84. 0: ldrb w3, [x0, x5]
  85. ldrb w4, [x1, x5]
  86. add x5, x5, #1
  87. cmp w3, w4
  88. b.ne barf
  89. subs x2, x2, #1
  90. b.ne 0b
  91. 1: ret
  92. endfunction
  93. // Verify that a FPSIMD V-register matches its shadow in memory, else abort
  94. // x0: reg number
  95. // Clobbers x0-x5.
  96. function check_vreg
  97. mov x3, x30
  98. _adrv x5, x0, 6
  99. mov x4, x0
  100. ldr x7, =scratch
  101. mov x0, x7
  102. mov x1, x6
  103. bl memfill_ae
  104. mov x0, x4
  105. mov x1, x7
  106. bl getv
  107. mov x0, x5
  108. mov x1, x7
  109. mov x2, x6
  110. mov x30, x3
  111. b memcmp
  112. endfunction
  113. // Any SVE register modified here can cause corruption in the main
  114. // thread -- but *only* the registers modified here.
  115. function irritator_handler
  116. // Increment the irritation signal count (x23):
  117. ldr x0, [x2, #ucontext_regs + 8 * 23]
  118. add x0, x0, #1
  119. str x0, [x2, #ucontext_regs + 8 * 23]
  120. // Corrupt some random V-regs
  121. adr x0, .text + (irritator_handler - .text) / 16 * 16
  122. movi v0.8b, #7
  123. movi v9.16b, #9
  124. movi v31.8b, #31
  125. ret
  126. endfunction
  127. function tickle_handler
  128. // Increment the signal count (x23):
  129. ldr x0, [x2, #ucontext_regs + 8 * 23]
  130. add x0, x0, #1
  131. str x0, [x2, #ucontext_regs + 8 * 23]
  132. ret
  133. endfunction
  134. function terminate_handler
  135. mov w21, w0
  136. mov x20, x2
  137. puts "Terminated by signal "
  138. mov w0, w21
  139. bl putdec
  140. puts ", no error, iterations="
  141. ldr x0, [x20, #ucontext_regs + 8 * 22]
  142. bl putdec
  143. puts ", signals="
  144. ldr x0, [x20, #ucontext_regs + 8 * 23]
  145. bl putdecn
  146. mov x0, #0
  147. mov x8, #__NR_exit
  148. svc #0
  149. endfunction
  150. // w0: signal number
  151. // x1: sa_action
  152. // w2: sa_flags
  153. // Clobbers x0-x6,x8
  154. function setsignal
  155. str x30, [sp, #-((sa_sz + 15) / 16 * 16 + 16)]!
  156. mov w4, w0
  157. mov x5, x1
  158. mov w6, w2
  159. add x0, sp, #16
  160. mov x1, #sa_sz
  161. bl memclr
  162. mov w0, w4
  163. add x1, sp, #16
  164. str w6, [x1, #sa_flags]
  165. str x5, [x1, #sa_handler]
  166. mov x2, #0
  167. mov x3, #sa_mask_sz
  168. mov x8, #__NR_rt_sigaction
  169. svc #0
  170. cbz w0, 1f
  171. puts "sigaction failure\n"
  172. b .Labort
  173. 1: ldr x30, [sp], #((sa_sz + 15) / 16 * 16 + 16)
  174. ret
  175. endfunction
  176. // Main program entry point
  177. .globl _start
  178. function _start
  179. _start:
  180. mov x23, #0 // signal count
  181. mov w0, #SIGINT
  182. adr x1, terminate_handler
  183. mov w2, #SA_SIGINFO
  184. bl setsignal
  185. mov w0, #SIGTERM
  186. adr x1, terminate_handler
  187. mov w2, #SA_SIGINFO
  188. bl setsignal
  189. mov w0, #SIGUSR1
  190. adr x1, irritator_handler
  191. mov w2, #SA_SIGINFO
  192. orr w2, w2, #SA_NODEFER
  193. bl setsignal
  194. mov w0, #SIGUSR2
  195. adr x1, tickle_handler
  196. mov w2, #SA_SIGINFO
  197. orr w2, w2, #SA_NODEFER
  198. bl setsignal
  199. // Sanity-check and report the vector length
  200. mov x19, #128
  201. cmp x19, #128
  202. b.lo 1f
  203. cmp x19, #2048
  204. b.hi 1f
  205. tst x19, #(8 - 1)
  206. b.eq 2f
  207. 1: puts "Bad vector length: "
  208. mov x0, x19
  209. bl putdecn
  210. b .Labort
  211. 2: puts "Vector length:\t"
  212. mov x0, x19
  213. bl putdec
  214. puts " bits\n"
  215. // Obtain our PID, to ensure test pattern uniqueness between processes
  216. mov x8, #__NR_getpid
  217. svc #0
  218. mov x20, x0
  219. puts "PID:\t"
  220. mov x0, x20
  221. bl putdecn
  222. mov x22, #0 // generation number, increments per iteration
  223. .Ltest_loop:
  224. mov x21, #0 // Set up V-regs & shadow with test pattern
  225. 0: mov x0, x20
  226. mov x1, x21
  227. and x2, x22, #0xf
  228. bl setup_vreg
  229. add x21, x21, #1
  230. cmp x21, #NVR
  231. b.lo 0b
  232. // Can't do this when SVE state is volatile across SVC:
  233. mov x8, #__NR_sched_yield // Encourage preemption
  234. svc #0
  235. mov x21, #0
  236. 0: mov x0, x21
  237. bl check_vreg
  238. add x21, x21, #1
  239. cmp x21, #NVR
  240. b.lo 0b
  241. add x22, x22, #1
  242. b .Ltest_loop
  243. .Labort:
  244. mov x0, #0
  245. mov x1, #SIGABRT
  246. mov x8, #__NR_kill
  247. svc #0
  248. endfunction
  249. function barf
  250. mov x10, x0 // expected data
  251. mov x11, x1 // actual data
  252. mov x12, x2 // data size
  253. puts "Mismatch: PID="
  254. mov x0, x20
  255. bl putdec
  256. puts ", iteration="
  257. mov x0, x22
  258. bl putdec
  259. puts ", reg="
  260. mov x0, x21
  261. bl putdecn
  262. puts "\tExpected ["
  263. mov x0, x10
  264. mov x1, x12
  265. bl dumphex
  266. puts "]\n\tGot ["
  267. mov x0, x11
  268. mov x1, x12
  269. bl dumphex
  270. puts "]\n"
  271. mov x8, #__NR_exit
  272. mov x1, #1
  273. svc #0
  274. endfunction