2010-05-28 23:09:12 -04:00
/ *
* Copyright 2 0 1 0 T i l e r a C o r p o r a t i o n . A l l R i g h t s R e s e r v e d .
*
* This p r o g r a m i s f r e e s o f t w a r e ; you can redistribute it and/or
* modify i t u n d e r t h e t e r m s o f t h e G N U G e n e r a l P u b l i c L i c e n s e
* as p u b l i s h e d b y t h e F r e e S o f t w a r e F o u n d a t i o n , v e r s i o n 2 .
*
* This p r o g r a m i s d i s t r i b u t e d i n t h e h o p e t h a t i t w i l l b e u s e f u l , b u t
* WITHOUT A N Y W A R R A N T Y ; without even the implied warranty of
* MERCHANTABILITY O R F I T N E S S F O R A P A R T I C U L A R P U R P O S E , G O O D T I T L E o r
* NON I N F R I N G E M E N T . S e e t h e G N U G e n e r a l P u b l i c L i c e n s e f o r
* more d e t a i l s .
*
* Support r o u t i n e s f o r a t o m i c o p e r a t i o n s . E a c h f u n c t i o n t a k e s :
*
* r0 : address t o m a n i p u l a t e
2011-02-28 15:58:39 -05:00
* r1 : pointer t o a t o m i c l o c k g u a r d i n g t h i s o p e r a t i o n ( f o r A T O M I C _ L O C K _ R E G )
2010-05-28 23:09:12 -04:00
* r2 : new v a l u e t o w r i t e , o r f o r c m p x c h g / a d d _ u n l e s s , v a l u e t o c o m p a r e a g a i n s t
* r3 : ( cmpxchg/ x c h g _ a d d _ u n l e s s ) n e w v a l u e t o w r i t e o r a d d ;
* ( atomic6 4 o p s ) h i g h w o r d o f v a l u e t o w r i t e
* r4 / r5 : ( c m p x c h g 6 4 / a d d _ u n l e s s64 ) n e w v a l u e t o w r i t e o r a d d
*
* The 3 2 - b i t r o u t i n e s r e t u r n a " s t r u c t _ _ g e t _ u s e r " s o t h a t t h e f u t e x c o d e
* has a n o p p o r t u n i t y t o r e t u r n - E F A U L T t o t h e u s e r i f n e e d e d .
* The 6 4 - b i t r o u t i n e s j u s t r e t u r n a " l o n g l o n g " w i t h t h e v a l u e ,
* since t h e y a r e o n l y u s e d f r o m k e r n e l s p a c e a n d d o n ' t e x p e c t t o f a u l t .
* Support f o r 1 6 - b i t o p s i s i n c l u d e d i n t h e f r a m e w o r k b u t w e d o n ' t p r o v i d e
* any ( x86 _ 6 4 h a s a n a t o m i c _ i n c _ s h o r t ( ) , s o w e m i g h t w a n t t o s o m e d a y ) .
*
* Note t h a t t h e c a l l e r i s a d v i s e d t o i s s u e a s u i t a b l e L 1 o r L 2
* prefetch o n t h e a d d r e s s b e i n g m a n i p u l a t e d t o a v o i d e x t r a s t a l l s .
* In a d d i t i o n , t h e h o t p a t h i s o n t w o i c a c h e l i n e s , a n d w e s t a r t w i t h
* a j u m p t o t h e s e c o n d l i n e t o m a k e s u r e t h e y a r e b o t h i n c a c h e s o
* that w e n e v e r s t a l l w a i t i n g o n i c a c h e f i l l w h i l e h o l d i n g t h e l o c k .
* ( This d o e s n ' t w o r k o u t w i t h m o s t 6 4 - b i t o p s , s i n c e t h e y c o n s u m e
* too m a n y b u n d l e s , s o m a y t a k e a n e x t r a i - c a c h e s t a l l . )
*
* These r o u t i n e s s e t t h e I N T E R R U P T _ C R I T I C A L _ S E C T I O N b i t , j u s t
* like s y s _ c m p x c h g ( ) , s o t h a t N M I s l i k e P E R F _ C O U N T w i l l n o t i n t e r r u p t
* the c o d e , j u s t p a g e f a u l t s .
*
* If t h e l o a d o r s t o r e f a u l t s i n a w a y t h a t c a n b e d i r e c t l y f i x e d i n
* the d o _ p a g e _ f a u l t _ i c s ( ) h a n d l e r ( e . g . a v m a l l o c r e f e r e n c e ) w e f i x i t
* directly, r e t u r n t o t h e i n s t r u c t i o n t h a t f a u l t e d , a n d r e t r y i t .
*
* If t h e l o a d o r s t o r e f a u l t s i n a w a y t h a t p o t e n t i a l l y r e q u i r e s u s
* to r e l e a s e t h e a t o m i c l o c k , t h e n r e t r y ( e . g . a m i g r a t i n g P T E ) , w e
* reset t h e P C i n d o _ p a g e _ f a u l t _ i c s ( ) t o t h e " t n s " i n s t r u c t i o n s o
* that o n r e t u r n w e w i l l r e a c q u i r e t h e l o c k a n d r e s t a r t t h e o p . W e
* are s o m e w h a t o v e r l o a d i n g t h e e x c e p t i o n _ t a b l e _ e n t r y n o t i o n b y d o i n g
* this, s i n c e t h o s e e n t r i e s a r e n o t n o r m a l l y u s e d f o r m i g r a t i n g P T E s .
*
* If t h e m a i n p a g e f a u l t h a n d l e r d i s c o v e r s a b a d a d d r e s s , i t w i l l s e e
* the P C p o i n t i n g t o t h e " t n s " i n s t r u c t i o n ( d u e t o t h e e a r l i e r
* exception_ t a b l e _ e n t r y p r o c e s s i n g i n d o _ p a g e _ f a u l t _ i c s ) , a n d
* re- r e s e t t h e P C t o t h e f a u l t h a n d l e r , a t o m i c _ b a d _ a d d r e s s ( ) , w h i c h
* effectively t a k e s o v e r f r o m t h e a t o m i c o p a n d c a n e i t h e r r e t u r n a
* bad " s t r u c t _ _ g e t _ u s e r " ( f o r u s e r a d d r e s s e s ) o r c a n j u s t p a n i c ( f o r
* bad k e r n e l a d d r e s s e s ) .
*
* Note t h a t i f t h e v a l u e w e w o u l d s t o r e i s t h e s a m e a s w h a t w e
2011-05-02 15:13:13 -04:00
* loaded, w e b y p a s s t h e s t o r e . O t h e r p l a t f o r m s w i t h t r u e a t o m i c s c a n
2010-05-28 23:09:12 -04:00
* make t h e g u a r a n t e e t h a t a n o n - a t o m i c _ _ c l e a r _ b i t ( ) , f o r e x a m p l e ,
* can s a f e l y r a c e w i t h a n a t o m i c t e s t _ a n d _ s e t _ b i t ( ) ; this example is
* from b i t _ s p i n l o c k . h i n s l u b _ l o c k ( ) / s l u b _ u n l o c k ( ) . W e c a n ' t d o
* that o n T i l e s i n c e t h e " a t o m i c " o p i s r e a l l y j u s t a
* read/ m o d i f y / w r i t e , a n d c a n r a c e w i t h t h e n o n - a t o m i c
* read/ m o d i f y / w r i t e . H o w e v e r , i f w e c a n s h o r t - c i r c u i t t h e w r i t e w h e n
* it i s n o t n e e d e d , i n t h e a t o m i c c a s e , w e a v o i d t h e r a c e .
* /
# include < l i n u x / l i n k a g e . h >
2011-10-05 17:09:29 -04:00
# include < a s m / a t o m i c _ 3 2 . h >
2010-05-28 23:09:12 -04:00
# include < a s m / p a g e . h >
# include < a s m / p r o c e s s o r . h >
.section .text .atomic , " ax"
ENTRY( _ _ s t a r t _ a t o m i c _ a s m _ c o d e )
.macro atomic_ o p , n a m e , b i t w i d t h , b o d y
.align 64
STD_ E N T R Y _ S E C T I O N ( _ _ a t o m i c \ n a m e , . t e x t . a t o m i c )
{
movei r24 , 1
j 4 f / * b r a n c h t o s e c o n d c a c h e l i n e * /
}
1 : {
.ifc \ bitwidth,1 6
lh r22 , r0
.else
lw r22 , r0
2010-06-25 17:02:40 -04:00
addi r28 , r0 , 4
2010-05-28 23:09:12 -04:00
.endif
}
.ifc \ bitwidth,6 4
2010-06-25 17:02:40 -04:00
lw r23 , r28
2010-05-28 23:09:12 -04:00
.endif
\ body / * s e t r24 , a n d r25 i f 6 4 - b i t * /
{
seq r26 , r22 , r24
seq r27 , r23 , r25
}
.ifc \ bitwidth,6 4
bbnst r27 , 2 f
.endif
bbs r26 , 3 f / * s k i p w r i t e - b a c k i f i t ' s t h e s a m e v a l u e * /
2 : {
.ifc \ bitwidth,1 6
sh r0 , r24
.else
sw r0 , r24
.endif
}
.ifc \ bitwidth,6 4
2010-06-25 17:02:40 -04:00
sw r28 , r25
2010-05-28 23:09:12 -04:00
.endif
mf
3 : {
move r0 , r22
.ifc \ bitwidth,6 4
move r1 , r23
.else
move r1 , z e r o
.endif
sw A T O M I C _ L O C K _ R E G _ N A M E , z e r o
}
mtspr I N T E R R U P T _ C R I T I C A L _ S E C T I O N , z e r o
jrp l r
4 : {
move A T O M I C _ L O C K _ R E G _ N A M E , r1
mtspr I N T E R R U P T _ C R I T I C A L _ S E C T I O N , r24
}
# ifndef C O N F I G _ S M P
j 1 b / * n o a t o m i c l o c k s * /
# else
{
tns r21 , A T O M I C _ L O C K _ R E G _ N A M E
moveli r23 , 2 0 4 8 / * m a x i m u m b a c k o f f t i m e i n c y c l e s * /
}
{
bzt r21 , 1 b / * b r a n c h i f l o c k a c q u i r e d * /
moveli r25 , 3 2 / * s t a r t i n g b a c k o f f t i m e i n c y c l e s * /
}
5 : mtspr I N T E R R U P T _ C R I T I C A L _ S E C T I O N , z e r o
mfspr r26 , C Y C L E _ L O W / * g e t s t a r t p o i n t f o r t h i s b a c k o f f * /
6 : mfspr r22 , C Y C L E _ L O W / * t e s t t o s e e i f w e ' v e b a c k e d o f f e n o u g h * /
sub r22 , r22 , r26
slt r22 , r22 , r25
bbst r22 , 6 b
{
mtspr I N T E R R U P T _ C R I T I C A L _ S E C T I O N , r24
shli r25 , r25 , 1 / * d o u b l e t h e b a c k o f f ; retry the tns */
}
{
tns r21 , A T O M I C _ L O C K _ R E G _ N A M E
slt r26 , r23 , r25 / * i s t h e p r o p o s e d b a c k o f f t o o b i g ? * /
}
{
bzt r21 , 1 b / * b r a n c h i f l o c k a c q u i r e d * /
mvnz r25 , r26 , r23
}
j 5 b
# endif
STD_ E N D P R O C ( _ _ a t o m i c \ n a m e )
.ifc \ bitwidth,3 2
.pushsection _ _ ex_ t a b l e ," a "
2013-08-09 15:38:43 -04:00
.align 4
2010-05-28 23:09:12 -04:00
.word 1 b, _ _ a t o m i c \ n a m e
.word 2 b, _ _ a t o m i c \ n a m e
.word _ _ atomic\ n a m e , _ _ a t o m i c _ b a d _ a d d r e s s
.popsection
.endif
.endm
2016-06-22 11:16:49 +02:00
/ *
* Use _ _ a t o m i c32 p r e f i x t o a v o i d c o l l i s i o n s w i t h G C C b u i l t i n _ _ a t o m i c f u n c t i o n s .
* /
atomic_ o p 3 2 _ c m p x c h g , 3 2 , " s e q r26 , r22 , r2 ; { bbns r26, 3f; move r24, r3 }"
atomic_ o p 3 2 _ x c h g , 3 2 , " m o v e r24 , r2 "
atomic_ o p 3 2 _ x c h g _ a d d , 3 2 , " a d d r24 , r22 , r2 "
atomic_ o p 3 2 _ x c h g _ a d d _ u n l e s s , 3 2 , \
2010-05-28 23:09:12 -04:00
" sne r26 , r22 , r2 ; { bbns r26, 3f; add r24, r22, r3 }"
2016-06-22 11:16:49 +02:00
atomic_ o p 3 2 _ f e t c h _ o r , 3 2 , " o r r24 , r22 , r2 "
atomic_ o p 3 2 _ f e t c h _ a n d , 3 2 , " a n d r24 , r22 , r2 "
atomic_ o p 3 2 _ f e t c h _ a n d n , 3 2 , " n o r r2 , r2 , z e r o ; and r24, r22, r2"
atomic_ o p 3 2 _ f e t c h _ x o r , 3 2 , " x o r r24 , r22 , r2 "
2010-05-28 23:09:12 -04:00
atomic_ o p 6 4 _ c m p x c h g , 6 4 , " { s e q r26 , r22 , r2 ; seq r27, r23, r3 }; \
{ bbns r26 , 3 f ; move r24, r4 }; { bbns r27, 3f; move r25, r5 }"
atomic_ o p 6 4 _ x c h g , 6 4 , " { m o v e r24 , r2 ; move r25, r3 }"
atomic_ o p 6 4 _ x c h g _ a d d , 6 4 , " { a d d r24 , r22 , r2 ; add r25, r23, r3 }; \
slt_ u r26 , r24 , r22 ; add r25, r25, r26"
atomic_ o p 6 4 _ x c h g _ a d d _ u n l e s s , 6 4 , \
" { sne r26 , r22 , r2 ; sne r27, r23, r3 }; \
{ bbns r26 , 3 f ; add r24, r22, r4 }; \
{ bbns r27 , 3 f ; add r25, r23, r5 }; \
slt_ u r26 , r24 , r22 ; add r25, r25, r26"
2016-04-18 01:16:03 +02:00
atomic_ o p 6 4 _ f e t c h _ o r , 6 4 , " { o r r24 , r22 , r2 ; or r25, r23, r3 }"
atomic_ o p 6 4 _ f e t c h _ a n d , 6 4 , " { a n d r24 , r22 , r2 ; and r25, r23, r3 }"
atomic_ o p 6 4 _ f e t c h _ x o r , 6 4 , " { x o r r24 , r22 , r2 ; xor r25, r23, r3 }"
2010-05-28 23:09:12 -04:00
jrp l r / * h a p p y b a c k t r a c e r * /
ENTRY( _ _ e n d _ a t o m i c _ a s m _ c o d e )