2005-04-16 15:20:36 -07:00
/ *
NetWinder F l o a t i n g P o i n t E m u l a t o r
( c) R e b e l . C O M , 1 9 9 8
( c) P h i l i p B l u n d e l l 1 9 9 8 - 1 9 9 9
Direct q u e s t i o n s , c o m m e n t s t o S c o t t B a m b r o u g h < s c o t t b @netwinder.org>
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
it 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 a s p u b l i s h e d b y
the F r e e S o f t w a r e F o u n d a t i o n ; either version 2 of the License, or
( at y o u r o p t i o n ) a n y l a t e r v e r s i o n .
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 ,
but W I T H O U T 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 . S e e t h e
GNU G e n e r a l P u b l i c L i c e n s e f o r m o r e d e t a i l s .
You s h o u l d h a v e r e c e i v e d a c o p y 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
along w i t h t h i s p r o g r a m ; if not, write to the Free Software
Foundation, I n c . , 6 7 5 M a s s A v e , C a m b r i d g e , M A 0 2 1 3 9 , U S A .
* /
2005-09-09 21:08:59 +02:00
# include < a s m / a s m - o f f s e t s . h >
2005-04-16 15:20:36 -07:00
/ * This i s t h e k e r n e l ' s e n t r y p o i n t i n t o t h e f l o a t i n g p o i n t e m u l a t o r .
It i s c a l l e d f r o m t h e k e r n e l w i t h c o d e s i m i l a r t o t h i s :
mov f p , #0
2006-06-25 12:01:48 +01:00
teqp p c , #P S R _ I _ B I T | S V C _ M O D E
2005-04-16 15:20:36 -07:00
ldr r4 , . L C 2
ldr p c , [ r4 ] @ Call FP module USR entry point
The k e r n e l e x p e c t s t h e e m u l a t o r t o r e t u r n v i a o n e o f t w o p o s s i b l e
points o f r e t u r n i t p a s s e s t o t h e e m u l a t o r . T h e e m u l a t o r , i f
successful i n i t s e m u l a t i o n , j u m p s t o r e t _ f r o m _ e x c e p t i o n a n d t h e
kernel t a k e s c a r e o f r e t u r n i n g c o n t r o l f r o m t h e t r a p t o t h e u s e r c o d e .
If t h e e m u l a t o r i s u n a b l e t o e m u l a t e t h e i n s t r u c t i o n , i t r e t u r n s t o
fpundefinstr a n d t h e k e r n e l h a l t s t h e u s e r p r o g r a m w i t h a c o r e d u m p .
This r o u t i n e d o e s f o u r t h i n g s :
1 ) It s a v e s S P i n t o a v a r i a b l e c a l l e d u s e r R e g i s t e r s . T h e k e r n e l h a s
created a s t r u c t p t _ r e g s o n t h e s t a c k a n d s a v e d t h e u s e r r e g i s t e r s
into i t . S e e / u s r / i n c l u d e / a s m / p r o c / p t r a c e . h f o r d e t a i l s . T h e
emulator c o d e u s e s u s e r R e g i s t e r s a s t h e b a s e o f a n a r r a y o f w o r d s f r o m
which t h e c o n t e n t s o f t h e r e g i s t e r s c a n b e e x t r a c t e d .
2 ) It l o c a t e s t h e F P e m u l a t o r w o r k a r e a w i t h i n t h e T S S s t r u c t u r e a n d
points ` f p a11 ' t o i t .
3 ) It c a l l s E m u l a t e A l l t o e m u l a t e a f l o a t i n g p o i n t i n s t r u c t i o n .
EmulateAll r e t u r n s 1 i f t h e e m u l a t i o n w a s s u c c e s s f u l , o r 0 i f n o t .
4 ) If a n i n s t r u c t i o n h a s b e e n e m u l a t e d s u c c e s s f u l l y , i t l o o k s a h e a d a t
the n e x t i n s t r u c t i o n . I f i t i s a f l o a t i n g p o i n t i n s t r u c t i o n , i t
executes t h e i n s t r u c t i o n , w i t h o u t r e t u r n i n g t o u s e r s p a c e . I n t h i s
way i t r e p e a t e d l y l o o k s a h e a d a n d e x e c u t e s f l o a t i n g p o i n t i n s t r u c t i o n s
until i t e n c o u n t e r s a n o n f l o a t i n g p o i n t i n s t r u c t i o n , a t w h i c h t i m e i t
returns v i a _ f p r e t u r n .
This i s d o n e t o r e d u c e t h e e f f e c t o f t h e t r a p o v e r h e a d o n e a c h
floating p o i n t i n s t r u c t i o n s . G C C a t t e m p t s t o g r o u p f l o a t i n g p o i n t
instructions t o a l l o w t h e e m u l a t o r t o s p r e a d t h e c o s t o f t h e t r a p o v e r
several f l o a t i n g p o i n t i n s t r u c t i o n s . * /
.globl nwfpe_enter
nwfpe_enter :
mov s l , s p
ldr r5 , [ s p , #60 ] @ get contents of PC
bic r5 , r5 , #0xfc000003
ldr r0 , [ r5 , #- 4 ] @ get actual instruction into r0
bl E m u l a t e A l l @ emulate the instruction
1 : cmp r0 , #0 @ was emulation successful
beq f p u n d e f i n s t r @ no, return failure
next :
.Lx1 : ldrt r6 , [ r5 ] , #4 @ get the next instruction and
@ increment PC
and r2 , r6 , #0x0F000000 @ test for FP insns
teq r2 , #0x0C000000
teqne r2 , #0x0D000000
teqne r2 , #0x0E000000
bne r e t _ f r o m _ e x c e p t i o n @ return ok if not a fp insn
ldr r9 , [ s p , #60 ] @ get new condition codes
and r9 , r9 , #0xfc000003
orr r7 , r5 , r9
str r7 , [ s p , #60 ] @ update PC copy in regs
mov r0 , r6 @ save a copy
mov r1 , r9 @ fetch the condition codes
bl c h e c k C o n d i t i o n @ check the condition
cmp r0 , #0 @ r0 = 0 ==> condition failed
@ if condition code failed to match, next insn
beq n e x t @ get the next instruction;
mov r0 , r6 @ prepare for EmulateAll()
adr l r , 1 b
orr l r , l r , #3
b E m u l a t e A l l @ if r0 != 0, goto EmulateAll
.Lret : b r e t _ f r o m _ e x c e p t i o n @ let the user eat segfaults
@ We need to be prepared for the instruction at .Lx1 to fault.
@ Emit the appropriate exception gunk to fix things up.
.section _ _ ex_ t a b l e ," a "
.align 3
.long .Lx1
ldr l r , [ l r , $ ( . L r e t - . L x1 ) / 4 ]
.previous