@ -45,8 +45,11 @@
@@ -45,8 +45,11 @@
# include " l i b c / n e x g e n 3 2 e / v i d y a . h "
# include " l i b c / n t / p e d e f . h "
# include " l i b c / n e x g e n 3 2 e / v i d y a . h "
# include " l i b c / d c e . h "
# include " l i b c / s y s v / c o n s t s / p r o t . h "
# define U S E _ S Y M B O L _ H A C K 0
.source " NOTICE"
.source " ape/ a p e . S "
.source " ape/ a p e . l d s "
@ -179,6 +182,10 @@ stub: mov $0x40,%dl # *literally* dos
@@ -179,6 +182,10 @@ stub: mov $0x40,%dl # *literally* dos
/ @noreturn
.code16
pc : cld
# if U S E _ S Y M B O L _ H A C K
.byte 0 x0 f ,0 x1 f ,0 2 0 7 # n o p r d i b i n b a s e
.short ( 0 x7 c00 - I M A G E _ B A S E _ V I R T U A L ) / 5 1 2
# endif
mov $ R E A L _ S T A C K _ F R A M E > > 4 ,% d i # w e n e e d a s t a c k
xor % c x ,% c x
rlstack % d i ,% c x
@ -197,6 +204,10 @@ pc: cld
@@ -197,6 +204,10 @@ pc: cld
xor % d i ,% d i
rep
movsb % d s : ( % s i ) ,% e s : ( % d i )
# if U S E _ S Y M B O L _ H A C K
.byte 0 x0 f ,0 x1 f ,0 2 0 7 # n o p r d i b i n b a s e
.short ( IMAGE_ B A S E _ R E A L - 0 x7 c00 ) / 5 1 2
# endif
ljmp $ 0 ,$ R E A L ( 1 f ) # l o n g j m p ( )
1 : mov $ - 5 1 2 ,% c x # m e m c p y ( ) [ r e l o c a t e t h i s f r a m e ]
rep
@ -932,9 +943,6 @@ ape.str:
@@ -932,9 +943,6 @@ ape.str:
.Lstr .long :
.asciz " nolong"
.endobj .Lstr .long
.Lstr .hello :
.asciz " hello\ n "
.endobj .Lstr .hello
.endobj ape. s t r
/ Serial L i n e C o n f i g u r a t i o n ( 8 2 5 0 U A R T 1 6 5 5 0 )
@ -964,7 +972,7 @@ sconf: .short 1843200/*hz*/ / 16/*wut*/ / 9600/*baud*/
@@ -964,7 +972,7 @@ sconf: .short 1843200/*hz*/ / 16/*wut*/ / 9600/*baud*/
.align 8
gdt : .short 2 f- 1 f # t a b l e b y t e l e n g t h
.long REAL( 1 f ) ,0 # t a b l e a d d r e s s
.align 8
.zero 2
1 :
/ โ G : granularity ( 1 โ l i m i t * = 0 x10 0 0 )
/ โ โ D/ B : d e f a u l t o p e r a t i o n s i z e ( 0 = 1 6 | 6 4 b i t , 1 = 3 2 - b i t )
@ -1047,13 +1055,8 @@ ape.grub.entry:
@@ -1047,13 +1055,8 @@ ape.grub.entry:
nop
nop
realmodeloader :
push % b p
mov % s p ,% b p
call r l i n i t
call s i n i t 4
call v i n i t
mov % e s ,X L M ( V I D E O _ P O S I T I O N _ F A R _ P O I N T E R )
mov % a x ,X L M ( V I D E O _ P O S I T I O N _ F A R _ P O I N T E R ) + 2
mov $ R E A L ( . L s t r . a p e ) ,% d i
call r v p u t s
.optfn _start16
@ -1062,38 +1065,29 @@ realmodeloader:
@@ -1062,38 +1065,29 @@ realmodeloader:
.endfn realmodeloader,g l o b l ,h i d d e n
.section .sort .text .real .init .1 , " ax" ,@progbits
.type rrinit,@function
rlinit : push % b p
mov % s p ,% b p
.previous / *
.type rlinit,@function
rlinit : .previous / *
. . .
decentralized f u n c t i o n
. . .
* / .section .sort .text .real .init .3 , " ax" ,@progbits
pop % b p
ret
.previous
/ Initializes p r e s e n t P C s e r i a l l i n e s .
sinit4 : push % b p
mov % s p ,% b p
movw $ 4 ,% c x
movw $ k B i o s D a t a A r e a X l m + C O M 1 ,% s i
0 : lodsb
mov % a l ,% d l
lodsb
mov % a l ,% d h
test % d x ,% d x
sinit4 : mov $ 4 ,% c x
mov $ k B i o s D a t a A r e a X l m + C O M 1 ,% s i
0 : lodsw
test % a x ,% a x
jz 1 f
push % c x
push % s i
mov % d x ,% d i
movw $ R E A L ( s c o n f ) ,% s i
xchg % a x ,% d i
mov $ R E A L ( s c o n f ) ,% s i
call s i n i t
pop % s i
pop % c x
1 : loop 0 b
pop % b p
ret
.endfn sinit4 ,g l o b a l ,h i d d e n
@ -1103,9 +1097,7 @@ sinit4: push %bp
@@ -1103,9 +1097,7 @@ sinit4: push %bp
/ @param char (*{es:,e,r}si)[4] register initial values
/ @mode long,legacy,real
/ @see www.lammertbies.nl/comm/info/serial-uart.html
sinit : push % b p
mov % s p ,% b p
mov % d i ,% d x
sinit : mov % d i ,% d x
test % d x ,% d x
jz 2 f
push % d x
@ -1123,8 +1115,7 @@ sinit: push %bp
@@ -1123,8 +1115,7 @@ sinit: push %bp
add $ 1 ,% d x
sub $ 1 ,% c x
jns 1 b
2 : pop % b p
ret
2 : ret
.endfn sinit,g l o b a l ,h i d d e n
/ Abnormally e x i t s p r o g r a m .
@ -1132,9 +1123,7 @@ sinit: push %bp
@@ -1132,9 +1123,7 @@ sinit: push %bp
/ @param di message
/ @mode real
/ @noreturn
rldie : push % b p
mov % s p ,% b p
call r l p u t e
rldie : call r l p u t e
call r l o f f
.endfn rldie,g l o b l ,h i d d e n
@ -1142,9 +1131,7 @@ rldie: push %bp
@@ -1142,9 +1131,7 @@ rldie: push %bp
/
/ @mode real
/ @noreturn
rloff : push % b p
mov % s p ,% b p
mov $ k B i o s D a t a A r e a X l m + C O M 1 ,% d i
rloff : mov $ k B i o s D a t a A r e a X l m + C O M 1 ,% d i
mov $ 4 ,% s i
call s f l u s h
call a p m o f f
@ -1154,9 +1141,7 @@ rloff: push %bp
@@ -1154,9 +1141,7 @@ rloff: push %bp
/
/ @param di message
/ @mode real
rlpute : push % b p
mov % s p ,% b p
mov k B i o s D a t a A r e a X l m + M E T A L _ S T D E R R ( % b x ) ,% s i
rlpute : mov k B i o s D a t a A r e a X l m + M E T A L _ S T D E R R ( % b x ) ,% s i
test % s i ,% s i
jnz 1 f
mov k B i o s D a t a A r e a X l m + M E T A L _ S T D O U T ,% s i
@ -1176,8 +1161,7 @@ rlpute: push %bp
@@ -1176,8 +1161,7 @@ rlpute: push %bp
pop % s i
call r l p u t 2
jmp 1 b
2 : pop % b p
ret
2 : ret
.endfn rlpute,g l o b l ,h i d d e n
/ Prints s t r i n g t o b o t h v i d e o a n d s e r i a l .
@ -1185,9 +1169,7 @@ rlpute: push %bp
@@ -1185,9 +1169,7 @@ rlpute: push %bp
/ @param di NUL-terminated string
/ @param si serial port
/ @mode real
rlput2 : push % b p
mov % s p ,% b p
push % d i
rlput2 : push % d i
push % s i
call r v p u t s
pop % s i
@ -1195,33 +1177,42 @@ rlput2: push %bp
@@ -1195,33 +1177,42 @@ rlput2: push %bp
test % s i ,% s i
jz 1 f
call s p u t s
1 : pop % b p
ret
1 : ret
.endfn rlput2 ,g l o b l ,h i d d e n
/ Video p u t s t r i n g .
/
/ @param di is the string
/ @mode real
rvputs : push % b p
mov % s p ,% b p
mov % d i ,% s i
les X L M ( V I D E O _ P O S I T I O N _ F A R _ P O I N T E R ) ,% d i
call v p u t s
mov % e s ,X L M ( V I D E O _ P O S I T I O N _ F A R _ P O I N T E R )
mov % a x ,X L M ( V I D E O _ P O S I T I O N _ F A R _ P O I N T E R ) + 2
pop % b p
ret
rvputs : mov % d i ,% s i
0 : lodsb
test % a l ,% a l
je 1 f
call r v p u t c
jmp 0 b
1 : ret
.endfn rvputs,g l o b l ,h i d d e n
/ Video p u t c h a r .
/
/ @param al is the char
/ @mode real
rvputc : push % b x # d o n ' t c l o b b e r b p , b x , d i , s i , c x
push % b p # o r i g i n a l i b m p c s c r o l l u p b u g
mov $ 7 ,% b x # n o r m a l m d a / c g a s t y l e p a g e z e r o
mov $ 0 x0 e ,% a h # t e l e t y p e o u t p u t a l c p 437
int $ 0 x10 # v i d y a s e r v i c e
pop % b p # p r e s e r v e s a l
pop % b x
ret
.endfn rvputc
/ Writes s t r i n g t o s e r i a l l i n e .
/
/ @param di NUL-terminated string
/ @param si serial port
/ @mode long,legacy,real
sputs : push % b p
mov % s p ,% b p
push % b x
sputs : push % b x
mov % d i ,% b x
1 : xchg % b x ,% s i
lodsb
@ -1234,7 +1225,6 @@ sputs: push %bp
@@ -1234,7 +1225,6 @@ sputs: push %bp
pop % s i
jmp 1 b
2 : pop % b x
pop % b p
ret
.endfn sputs,g l o b l
@ -1290,137 +1280,11 @@ sputc: push %ax
@@ -1290,137 +1280,11 @@ sputc: push %ax
ret
.endfn sputc,g l o b l
/ Asks B I O S t o i n i t i a l i z e M o n o c h r o m e D i s p l a y A d a p t e r .
/
/ @return es:ax start of video page
/ @mode real
vinit : push $ 7
pop % a x
int $ V I D Y A _ S E R V I C E
bbmov V I D Y A _ A D D R _ M D A > > 4 ,% a x ,% a h ,% a l
mov % a x ,% e s
xor % a x ,% a x
ret
.endfn vinit,g l o b l
/ Prints b y t e t o d i s p l a y w / t e l e t y p e e m u l a t i o n .
/
/ @param es:di screen position
/ @param sil byte
/ @return es:ax new screen position
/ @mode long,legacy,real
vputc : push % b p
mov % s p ,% b p
sub $ 1 6 ,% s p # g o a l i s t o t u r n s i l i n t o a b u f f e r
xchg % s i ,% a x # m o d r m s o o o d i f f e r e n t i n r e a l m o d e
mov % d i ,% c x
mov % s p ,% s i
mov % s p ,% d i
stosb
mov % c x ,% d i
pushpop 1 ,% d x
jmp 2 3 f
/ Prints s t r i n g t o d i s p l a y w / t e l e t y p e e m u l a t i o n .
/
/ @param es:di screen position
/ @param si NUL-terminated string
/ @return es:ax new screen position
/ @mode long,legacy,real
vputs : push % s i # i n l i n e d s t r l e n
1 : lodsb
test % a l ,% a l
jnz 1 b
mov % s i ,% d x
pop % s i
sub % s i ,% d x
/ fallthrough
/ Prints d a t a t o d i s p l a y w / t e l e t y p e e m u l a t i o n .
/
/ @param es:di screen position
/ @param si data address
/ @param dx data size in bytes
/ @return es:ax new screen position
/ @mode long,legacy,real
vtput : push % b p
mov % s p ,% b p
23 : push % b x
mov % d x ,% c x
mov % d i ,% d x
bband V I D Y A _ R E W I N D ,% d h ,% d l
bbadd V I D Y A _ S I Z E ,% d h ,% d l
bbmov V I D Y A _ C O L U M N S * 2 - 2 ,% b x ,% b h ,% b l
0 : cmp % d x ,% d i
je 6 f
ja 3 f
lodsb # t o d o : u t f 8 โ c p43 7
cmp $ ' \ n ,% a l
je 4 f
cmp $ ' \ r ,% a l
je 5 f
1 : stosb
mov $ V I D Y A _ A T T R _ N O R M A L ,% a l # t o d o : a n s i c o l o r
stosb
2 : loop 0 b
3 : mov % d i ,% a x
pop % b x
pop % b p
ret
4 : add % b x ,% d i # l i n e f e e d
jmp 2 b
5 : mov % d i ,% a x # c a r r i a g e r e t u r n
push % d x
xor % d x ,% d x
idiv % b x # t o d o : d i v i s i o n i s d e p r e c a t e d
sub % d x ,% d i
pop % d x
jmp 2 b
6 : push % a x
push % c x
push % d x
push % s i
mov % b x ,% s i
rlcall v s c r o l l
mov % a x ,% d i
pop % s i
pop % d x
pop % c x
pop % a x
jmp 2 b
.endfn vtput,g l o b l
.endfn vputs,g l o b l
.endfn vputc,g l o b l
/ Scrolls u p c o n t e n t i n d i s p l a y p a g e .
/
/ @param es:di cursor address (bytes after aren't moved)
/ @param si byte difference, e.g. VIDYA_COLUMNS*2
/ @return es:ax new cursor address (which is es:di-si)
/ @mode long,legacy,real
vscroll : not % d x
mov % d i ,% c x
bband V I D Y A _ R E W I N D ,% c h ,% c l
xchg % c x ,% d i # d i i s n o w p a g e a d d r , i . e . t o p - l e f t
sub % s i ,% c x # c x i s n o w f u t u r e c u r s o r a d d r e s s
push % c x
sub % d i ,% c x # c x i s n o w m e m c p y s i z e
mov % d i ,% s i
add % c x ,% s i
mov $ 0 ,% a x
movpp % d s ,% e s
rep m o v s b
pop % a x
ret
.endfn vscroll,g l o b l
/ Shuts d o w n p e r s o n a l c o m p u t e r .
/
/ @mode real
/ @noreturn
apmoff : push % b p
mov % s p ,% b p
mov $ 0 x53 0 0 ,% a x # a p m i n s t a l l a t i o n c h e c k
apmoff : mov $ 0 x53 0 0 ,% a x # a p m i n s t a l l a t i o n c h e c k
xor % b x ,% b x # f o r t h e a p m b i o s i t s e l f
int $ A P M _ S E R V I C E
jc 1 f
@ -1485,8 +1349,6 @@ apmoff: push %bp
@@ -1485,8 +1349,6 @@ apmoff: push %bp
long m o d e i s l o n g * /
longmodeloader :
push % b p
mov % s p ,% b p
call l c h e c k
call a20
mov $ X L M ( E 8 2 0 ) ,% d i
@ -1495,15 +1357,13 @@ longmodeloader:
@@ -1495,15 +1357,13 @@ longmodeloader:
jc 9 f
call u n r e a l
call h i l o a d
call g o l o n g
jmp g o l o n g
9 : mov $ R E A L ( . L s t r . e 8 2 0 ) ,% a x
call r l d i e
.endfn longmodeloader,g l o b l ,h i d d e n
/ Long M o d e H a r d w a r e C h e c k
lcheck : push % b p
mov % s p ,% b p
pushf # c h e c k f o r i 8086 / i 8 0 8 8 / i 8 0 1 8 6
lcheck : pushf # c h e c k f o r i 8086 / i 8 0 8 8 / i 8 0 1 8 6
pop % a x
test $ 0 x80 ,% a h # s e e i n t e l m a n u a l v o l u m e 1 2 0 . 1 . 2
jnz 9 f # w e n o w a s s u m e 32 b i t i s s u p p o r t e d
@ -1534,8 +1394,7 @@ lcheck: push %bp
@@ -1534,8 +1394,7 @@ lcheck: push %bp
cmp % e d i ,% e d x
jne 1 0 f
xor % a x ,% a x
1 : pop % b p
ret
1 : ret
9 : mov $ R E A L ( . L s t r . o l d s k o o l ) ,% a x
jmp 2 0 f
10 : mov $ R E A L ( . L s t r . l o n g ) ,% a x
@ -1587,9 +1446,7 @@ e820: push %bp
@@ -1587,9 +1446,7 @@ e820: push %bp
/ Unreal M o d e .
/ Makes 4 g b o f r e a l m e m o r y a c c e s s i b l e v i a % f s s e g m e n t .
unreal : push % b p
mov % s p ,% b p
cli
unreal : cli
lgdt R E A L ( g d t )
mov % c r0 ,% e a x
or $ C R 0 _ P E ,% a l
@ -1599,16 +1456,13 @@ unreal: push %bp
@@ -1599,16 +1456,13 @@ unreal: push %bp
mov % c x ,% f s
and $ ~ C R 0 _ P E ,% a l
mov % e a x ,% c r0
ljmpl $ 0 ,$ R E A L ( 1 f )
ljmp $ 0 ,$ R E A L ( 1 f )
1 : sti
pop % b p
ret
.endfn unreal
/ Loads r e m a i n d e r o f e x e c u t a b l e o f f d i s k .
hiload : push % b p
mov % s p ,% b p
push % b x
hiload : push % b x
mov $ I M A G E _ B A S E _ R E A L ,% e s i # r e l o c a t e , a g a i n
mov $ I M A G E _ B A S E _ P H Y S I C A L ,% e b x
mov $ v _ a p e _ r e a l s e c t o r s ,% e c x
@ -1647,7 +1501,6 @@ hiload: push %bp
@@ -1647,7 +1501,6 @@ hiload: push %bp
pop % c x
jmp 0 b
9 : pop % b x
pop % b p
ret
.endfn hiload
@ -1724,18 +1577,16 @@ a20: cli
@@ -1724,18 +1577,16 @@ a20: cli
/ stack s e g m e n t b a s e . T h i s f u n c t i o n o n l y d e f i n e s e n o u g h t a b l e s
/ to g e t u s s t a r t e d .
# define T I P R E A L _ S T A C K _ F R A M E
pinit : push % b p
mov % s p ,% b p
push % d s
pinit : push % d s
mov $ ( T I P - 0 x40 0 0 ) > > 4 ,% a x
mov % a x ,% d s
movl $ T I P - 0 x20 0 0 + P A G E _ V + P A G E _ R W ,% d s : 0 x30 0 0 # P M L 4 T โ P D P T
movl $ T I P - 0 x30 0 0 + P A G E _ V + P A G E _ R W ,% d s : 0 x20 0 0 # P D P T โ P D T
movl $ T I P - 0 x40 0 0 + P A G E _ V + P A G E _ R W ,% d s : 0 x10 0 0 # P D T โ P D
movl $ T I P - 0 x20 0 0 + P A G E _ V + P A G E _ R W ,0 x30 0 0 # P M L 4 T โ P D P T
movl $ T I P - 0 x30 0 0 + P A G E _ V + P A G E _ R W ,0 x20 0 0 # P D P T โ P D T
movl $ T I P - 0 x40 0 0 + P A G E _ V + P A G E _ R W ,0 x10 0 0 # P D T โ P D
mov $ 0 x10 0 0 0 0 / 0 x10 0 0 ,% c x # P D โ 512 k b
mov $ P A G E _ V + P A G E _ R W ,% e a x
xor % s i ,% s i
0 : mov % e a x ,% d s : ( % s i )
0 : mov % e a x ,( % s i )
add $ 0 x10 0 0 ,% e a x
add $ 8 ,% s i
loop 0 b
@ -1743,7 +1594,6 @@ pinit: push %bp
@@ -1743,7 +1594,6 @@ pinit: push %bp
movl $ T I P - 0 x40 0 0 ,X L M ( P A G E _ T A B L E _ S T A C K _ P O I N T E R ) # S T A C K โ X L M
mov $ T I P - 0 x10 0 0 ,% e a x # P M L 4 T โ C R 3
mov % e a x ,% c r3
pop % b p
ret
.endfn pinit,g l o b l ,h i d d e n
@ -1765,44 +1615,47 @@ golong: cli
@@ -1765,44 +1615,47 @@ golong: cli
or $ C R 0 _ P E | C R 0 _ P G | C R 0 _ M P ,% e a x
and $ ~ C R 0 _ E M ,% e a x
mov % e a x ,% c r0
ljmp $ G D T _ L O N G _ C O D E ,$ R E A L ( 1 f )
.code64
1 : mov $ G D T _ L O N G _ D A T A ,% e a x
mov % a x ,% d s
mov % a x ,% f s
mov % a x ,% g s
xor % e d x ,% e d x
jmp l o n g
ljmp $ G D T _ L O N G _ C O D E ,$ R E A L ( l o n g )
.endfn golong
/ Long m o d e i s l o n g .
/
/ @noreturn
long : .frame0
xor % e d i ,% e d i
call p a g e u n m a p
mov $ e 8 2 0 m a p _ x l m ,% e d i
call s m a p s o r t
mov $ e 8 2 0 m a p _ x l m ,% e d i
mov $ g _ p m l 4 t ,% e s i
mov $ g _ p t s p _ x l m ,% e d x
call f l a t t e n h i g h m e m o r y
mov $ R E A L ( . L s t r . h e l l o ) ,% e d i
mov k B i o s D a t a A r e a X l m + M E T A L _ S T D O U T ,% e s i
call s p u t s
mov $ k B i o s D a t a A r e a X l m + M E T A L _ S T D O U T ,% e d i
mov $ 4 ,% e s i
call s f l u s h
jmp t r i p l f
lea t r i p l f ( % r i p ) ,% r d x
call _ s t a r t
jmp t r i p l f
.code64
long : push $ G D T _ L O N G _ D A T A
pop % r a x
mov % e a x ,% d s
mov % e a x ,% s s
mov % e a x ,% e s
mov % e a x ,% f s
mov % e a x ,% g s
xor % e b p ,% e b p
mov $ R E A L _ S T A C K _ F R A M E + F R A M E S I Z E ,% e s p
call _ _ m a p _ i m a g e
mov $ _ m e t a l ,% e a x
jmp * % r a x
.endfn long
/ Long m o d e i n v i r t u a l a d d r e s s s p a c e .
/ @noreturn
_metal :
# if U S E _ S Y M B O L _ H A C K
.byte 0 x0 f ,0 x1 f ,0 2 0 7 # n o p r d i b i n b a s e
.long ( IMAGE_ B A S E _ V I R T U A L - I M A G E _ B A S E _ R E A L ) / 5 1 2
# endif
xor % e a x ,% e a x # c l e a r b s s
mov $ . L a p e . b s s . v a d d r ,% e d i
mov $ . L a p e . b s s . m e m s z ,% e c x
rep s t o s b
movb $ M E T A L ,h o s t o s ( % r i p )
push $ 0 # a u x v
push $ 0
push $ 0 # e n v p
push $ 0 # a u x v
push $ 0 # a r g c
xor % e d i ,% e d i
jmp _ s t a r t
.endfn _metal
/ Avoid l i n k e r s c r i p t v a r i a b l e s a p p e a r i n g a s c o d e i n o b j d u m p .
.macro .ldsvar name : req
.type \ name,@object