2020-08-19 12:48:34 +01:00
/ / SPDX- L i c e n s e - I d e n t i f i e r : G P L - 2 . 0 - o n l y
/ / Copyright ( C ) 2 0 1 5 - 2 0 1 9 A R M L i m i t e d .
/ / Original a u t h o r : D a v e M a r t i n < D a v e . M a r t i n @arm.com>
/ /
/ / Simple F P S I M D c o n t e x t s w i t c h t e s t
/ / Repeatedly w r i t e s u n i q u e t e s t p a t t e r n s i n t o e a c h F P S I M D r e g i s t e r
/ / and r e a d s t h e m b a c k t o v e r i f y i n t e g r i t y .
/ /
/ / for x i n ` s e q 1 N R _ C P U S ` ; do fpsimd-test & pids=$pids\ $! ; done
/ / ( leave i t r u n n i n g f o r a s l o n g a s y o u w a n t . . . )
/ / kill $ p i d s
# include < a s m / u n i s t d . h >
# include " a s s e m b l e r . h "
# include " a s m - o f f s e t s . h "
# define N V R 3 2
# define M A X V L _ B ( 1 2 8 / 8 )
.macro _vldr Vn : req, X t : r e q
ld1 { v \ V n \ ( ) . 2 d } , [ x \ X t ]
.endm
.macro _vstr Vn : req, X t : r e q
st1 { v \ V n \ ( ) . 2 d } , [ x \ X t ]
.endm
/ / Generate a c c e s s o r f u n c t i o n s t o r e a d / w r i t e p r o g r a m m a t i c a l l y s e l e c t e d
/ / FPSIMD r e g i s t e r s .
/ / x0 i s t h e r e g i s t e r i n d e x t o a c c e s s
/ / x1 i s t h e m e m o r y a d d r e s s t o r e a d f r o m ( g e t v ,s e t p ) o r s t o r e t o ( s e t v ,s e t p )
/ / All c l o b b e r x0 - x2
define_ a c c e s s o r s e t v , N V R , _ v l d r
define_ a c c e s s o r g e t v , N V R , _ v s t r
/ / Declare s o m e s t o r a t e s p a c e t o s h a d o w t h e S V E r e g i s t e r c o n t e n t s :
.pushsection .text
.data
.align 4
vref :
.space MAXVL_B * NVR
scratch :
.space MAXVL_B
.popsection
/ / Generate a t e s t p a t t e r n f o r s t o r a g e i n S V E r e g i s t e r s
/ / x0 : pid ( 1 6 b i t s )
/ / x1 : register n u m b e r ( 6 b i t s )
/ / x2 : generation ( 4 b i t s )
function p a t t e r n
orr w1 , w0 , w1 , l s l #16
orr w2 , w1 , w2 , l s l #28
ldr x0 , =scratch
mov w1 , #M A X V L _ B / 4
0 : str w2 , [ x0 ] , #4
add w2 , w2 , #( 1 < < 2 2 )
subs w1 , w1 , #1
bne 0 b
ret
endfunction
/ / Get t h e a d d r e s s o f s h a d o w d a t a f o r F P S I M D V - r e g i s t e r V < x n >
.macro _adrv xd, x n , n r t m p
ldr \ x d , =vref
mov x \ n r t m p , #16
madd \ x d , x \ n r t m p , \ x n , \ x d
.endm
/ / Set u p t e s t p a t t e r n i n a F P S I M D V - r e g i s t e r
/ / x0 : pid
/ / x1 : register n u m b e r
/ / x2 : generation
function s e t u p _ v r e g
mov x4 , x30
mov x6 , x1
bl p a t t e r n
_ adrv x0 , x6 , 2
mov x5 , x0
ldr x1 , =scratch
bl m e m c p y
mov x0 , x6
mov x1 , x5
bl s e t v
ret x4
endfunction
/ / Trivial m e m o r y c o m p a r e : c o m p a r e x2 b y t e s s t a r t i n g a t a d d r e s s x0 w i t h
/ / bytes s t a r t i n g a t a d d r e s s x1 .
/ / Returns o n l y i f a l l b y t e s m a t c h ; otherwise, the program is aborted.
/ / Clobbers x0 - x5 .
function m e m c m p
cbz x2 , 1 f
mov x5 , #0
0 : ldrb w3 , [ x0 , x5 ]
ldrb w4 , [ x1 , x5 ]
add x5 , x5 , #1
cmp w3 , w4
b. n e b a r f
subs x2 , x2 , #1
b. n e 0 b
1 : ret
endfunction
/ / Verify t h a t a F P S I M D V - r e g i s t e r m a t c h e s i t s s h a d o w i n m e m o r y , e l s e a b o r t
/ / x0 : reg n u m b e r
/ / Clobbers x0 - x5 .
function c h e c k _ v r e g
mov x3 , x30
_ adrv x5 , x0 , 6
mov x4 , x0
ldr x7 , =scratch
mov x0 , x7
mov x1 , x6
bl m e m f i l l _ a e
mov x0 , x4
mov x1 , x7
bl g e t v
mov x0 , x5
mov x1 , x7
mov x2 , x6
mov x30 , x3
b m e m c m p
endfunction
/ / Any S V E r e g i s t e r m o d i f i e d h e r e c a n c a u s e c o r r u p t i o n i n t h e m a i n
/ / thread - - b u t * o n l y * t h e r e g i s t e r s m o d i f i e d h e r e .
function i r r i t a t o r _ h a n d l e r
/ / Increment t h e i r r i t a t i o n s i g n a l c o u n t ( x23 ) :
ldr x0 , [ x2 , #u c o n t e x t _ r e g s + 8 * 2 3 ]
add x0 , x0 , #1
str x0 , [ x2 , #u c o n t e x t _ r e g s + 8 * 2 3 ]
/ / Corrupt s o m e r a n d o m V - r e g s
adr x0 , . t e x t + ( i r r i t a t o r _ h a n d l e r - . t e x t ) / 1 6 * 1 6
movi v0 . 8 b , #7
movi v9 . 1 6 b , #9
movi v31 . 8 b , #31
ret
endfunction
function t e r m i n a t e _ h a n d l e r
mov w21 , w0
mov x20 , x2
puts " T e r m i n a t e d b y s i g n a l "
mov w0 , w21
bl p u t d e c
puts " , n o e r r o r , i t e r a t i o n s = "
ldr x0 , [ x20 , #u c o n t e x t _ r e g s + 8 * 2 2 ]
bl p u t d e c
puts " , s i g n a l s = "
ldr x0 , [ x20 , #u c o n t e x t _ r e g s + 8 * 2 3 ]
bl p u t d e c n
mov x0 , #0
mov x8 , #_ _ N R _ e x i t
svc #0
endfunction
/ / w0 : signal n u m b e r
/ / x1 : sa_ a c t i o n
/ / w2 : sa_ f l a g s
/ / Clobbers x0 - x6 ,x8
function s e t s i g n a l
str x30 , [ s p , #- ( ( s a _ s z + 15 ) / 1 6 * 1 6 + 1 6 ) ] !
mov w4 , w0
mov x5 , x1
mov w6 , w2
add x0 , s p , #16
mov x1 , #s a _ s z
bl m e m c l r
mov w0 , w4
add x1 , s p , #16
str w6 , [ x1 , #s a _ f l a g s ]
str x5 , [ x1 , #s a _ h a n d l e r ]
mov x2 , #0
mov x3 , #s a _ m a s k _ s z
mov x8 , #_ _ N R _ r t _ s i g a c t i o n
svc #0
cbz w0 , 1 f
puts " s i g a c t i o n f a i l u r e \ n "
b . L a b o r t
1 : ldr x30 , [ s p ] , #( ( s a _ s z + 15 ) / 1 6 * 1 6 + 1 6 )
ret
endfunction
/ / Main p r o g r a m e n t r y p o i n t
.globl _start
function _ s t a r t
_start :
/ / Sanity- c h e c k a n d r e p o r t t h e v e c t o r l e n g t h
mov x19 , #128
cmp x19 , #128
b. l o 1 f
cmp x19 , #2048
b. h i 1 f
tst x19 , #( 8 - 1 )
b. e q 2 f
1 : puts " B a d v e c t o r l e n g t h : "
mov x0 , x19
bl p u t d e c n
b . L a b o r t
2 : puts " V e c t o r l e n g t h : \ t "
mov x0 , x19
bl p u t d e c
puts " b i t s \ n "
/ / Obtain o u r P I D , t o e n s u r e t e s t p a t t e r n u n i q u e n e s s b e t w e e n p r o c e s s e s
mov x8 , #_ _ N R _ g e t p i d
svc #0
mov x20 , x0
puts " P I D : \ t "
mov x0 , x20
bl p u t d e c n
mov x23 , #0 / / I r r i t a t i o n s i g n a l c o u n t
mov w0 , #S I G I N T
adr x1 , t e r m i n a t e _ h a n d l e r
mov w2 , #S A _ S I G I N F O
bl s e t s i g n a l
mov w0 , #S I G T E R M
adr x1 , t e r m i n a t e _ h a n d l e r
mov w2 , #S A _ S I G I N F O
bl s e t s i g n a l
mov w0 , #S I G U S R 1
adr x1 , i r r i t a t o r _ h a n d l e r
mov w2 , #S A _ S I G I N F O
orr w2 , w2 , #S A _ N O D E F E R
bl s e t s i g n a l
mov x22 , #0 / / g e n e r a t i o n n u m b e r , i n c r e m e n t s p e r i t e r a t i o n
.Ltest_loop :
mov x21 , #0 / / S e t u p V - r e g s & s h a d o w w i t h t e s t p a t t e r n
0 : mov x0 , x20
mov x1 , x21
and x2 , x22 , #0xf
bl s e t u p _ v r e g
add x21 , x21 , #1
cmp x21 , #N V R
b. l o 0 b
/ / Can' t d o t h i s w h e n S V E s t a t e i s v o l a t i l e a c r o s s S V C :
mov x8 , #_ _ N R _ s c h e d _ y i e l d / / E n c o u r a g e p r e e m p t i o n
svc #0
mov x21 , #0
0 : mov x0 , x21
bl c h e c k _ v r e g
add x21 , x21 , #1
cmp x21 , #N V R
b. l o 0 b
add x22 , x22 , #1
b . L t e s t _ l o o p
.Labort :
mov x0 , #0
mov x1 , #S I G A B R T
mov x8 , #_ _ N R _ k i l l
svc #0
endfunction
function b a r f
mov x10 , x0 / / e x p e c t e d d a t a
mov x11 , x1 / / a c t u a l d a t a
mov x12 , x2 / / d a t a s i z e
2021-01-08 18:31:44 +00:00
puts " M i s m a t c h : P I D = "
2020-08-19 12:48:34 +01:00
mov x0 , x20
bl p u t d e c
puts " , i t e r a t i o n = "
mov x0 , x22
bl p u t d e c
puts " , r e g = "
mov x0 , x21
bl p u t d e c n
puts " \ t E x p e c t e d [ "
mov x0 , x10
mov x1 , x12
bl d u m p h e x
puts " ] \ n \ t G o t [ "
mov x0 , x11
mov x1 , x12
bl d u m p h e x
puts " ] \ n "
mov x8 , #_ _ N R _ e x i t
mov x1 , #1
svc #0
endfunction