2019-05-27 09:55:01 +03:00
/* SPDX-License-Identifier: GPL-2.0-or-later */
powerpc: Make the 64-bit kernel as a position-independent executable
This implements CONFIG_RELOCATABLE for 64-bit by making the kernel as
a position-independent executable (PIE) when it is set. This involves
processing the dynamic relocations in the image in the early stages of
booting, even if the kernel is being run at the address it is linked at,
since the linker does not necessarily fill in words in the image for
which there are dynamic relocations. (In fact the linker does fill in
such words for 64-bit executables, though not for 32-bit executables,
so in principle we could avoid calling relocate() entirely when we're
running a 64-bit kernel at the linked address.)
The dynamic relocations are processed by a new function relocate(addr),
where the addr parameter is the virtual address where the image will be
run. In fact we call it twice; once before calling prom_init, and again
when starting the main kernel. This means that reloc_offset() returns
0 in prom_init (since it has been relocated to the address it is running
at), which necessitated a few adjustments.
This also changes __va and __pa to use an equivalent definition that is
simpler. With the relocatable kernel, PAGE_OFFSET and MEMORY_START are
constants (for 64-bit) whereas PHYSICAL_START is a variable (and
KERNELBASE ideally should be too, but isn't yet).
With this, relocatable kernels still copy themselves down to physical
address 0 and run there.
Signed-off-by: Paul Mackerras <paulus@samba.org>
2008-08-30 05:43:47 +04:00
/ *
* Code t o p r o c e s s d y n a m i c r e l o c a t i o n s i n t h e k e r n e l .
*
* Copyright 2 0 0 8 P a u l M a c k e r r a s , I B M C o r p .
* /
# include < a s m / p p c _ a s m . h >
RELA = 7
RELACOUNT = 0 x6 f f f f f f9
R_ P P C 6 4 _ R E L A T I V E = 2 2
/ *
* r3 = d e s i r e d f i n a l a d d r e s s o f k e r n e l
* /
_ GLOBAL( r e l o c a t e )
mflr r0
bcl 2 0 ,3 1 ,$ + 4
0 : mflr r12 / * r12 h a s r u n t i m e a d d r o f l a b e l 0 * /
mtlr r0
ld r11 ,( p _ d y n - 0 b ) ( r12 )
add r11 ,r11 ,r12 / * r11 h a s r u n t i m e a d d r o f . d y n a m i c s e c t i o n * /
ld r9 ,( p _ r e l a - 0 b ) ( r12 )
add r9 ,r9 ,r12 / * r9 h a s r u n t i m e a d d r o f . r e l a . d y n s e c t i o n * /
ld r10 ,( p _ s t - 0 b ) ( r12 )
add r10 ,r10 ,r12 / * r10 h a s r u n t i m e a d d r o f _ s t e x t * /
/ *
* Scan t h e d y n a m i c s e c t i o n f o r t h e R E L A a n d R E L A C O U N T e n t r i e s .
* /
li r7 ,0
li r8 ,0
1 : ld r6 ,0 ( r11 ) / * g e t t a g * /
cmpdi r6 ,0
beq 4 f / * e n d o f l i s t * /
cmpdi r6 ,R E L A
bne 2 f
ld r7 ,8 ( r11 ) / * g e t R E L A p o i n t e r i n r7 * /
b 3 f
2 : addis r6 ,r6 ,( - R E L A C O U N T ) @ha
cmpdi r6 ,R E L A C O U N T @l
bne 3 f
ld r8 ,8 ( r11 ) / * g e t R E L A C O U N T v a l u e i n r8 * /
3 : addi r11 ,r11 ,1 6
b 1 b
4 : cmpdi r7 ,0 / * c h e c k w e h a v e b o t h R E L A a n d R E L A C O U N T * /
cmpdi c r1 ,r8 ,0
beq 6 f
beq c r1 ,6 f
/ *
* Work o u t l i n k t i m e a d d r e s s o f _ s t e x t a n d h e n c e t h e
* relocation o f f s e t t o b e a p p l i e d .
* cur_ o f f s e t [ r7 ] = r e l a . r u n [ r9 ] - r e l a . l i n k [ r7 ]
* _ stext. l i n k [ r10 ] = _ s t e x t . r u n [ r10 ] - c u r _ o f f s e t [ r7 ]
* final_ o f f s e t [ r3 ] = _ s t e x t . f i n a l [ r3 ] - _ s t e x t . l i n k [ r10 ]
* /
subf r7 ,r7 ,r9 / * c u r _ o f f s e t * /
subf r10 ,r7 ,r10
subf r3 ,r10 ,r3 / * f i n a l _ o f f s e t * /
/ *
* Run t h r o u g h t h e l i s t o f r e l o c a t i o n s a n d p r o c e s s t h e
* R_ P P C 6 4 _ R E L A T I V E o n e s .
* /
mtctr r8
2014-01-30 19:58:42 +04:00
5 : ld r0 ,8 ( 9 ) / * E L F 6 4 _ R _ T Y P E ( r e l o c - > r _ i n f o ) * /
cmpdi r0 ,R _ P P C 6 4 _ R E L A T I V E
powerpc: Make the 64-bit kernel as a position-independent executable
This implements CONFIG_RELOCATABLE for 64-bit by making the kernel as
a position-independent executable (PIE) when it is set. This involves
processing the dynamic relocations in the image in the early stages of
booting, even if the kernel is being run at the address it is linked at,
since the linker does not necessarily fill in words in the image for
which there are dynamic relocations. (In fact the linker does fill in
such words for 64-bit executables, though not for 32-bit executables,
so in principle we could avoid calling relocate() entirely when we're
running a 64-bit kernel at the linked address.)
The dynamic relocations are processed by a new function relocate(addr),
where the addr parameter is the virtual address where the image will be
run. In fact we call it twice; once before calling prom_init, and again
when starting the main kernel. This means that reloc_offset() returns
0 in prom_init (since it has been relocated to the address it is running
at), which necessitated a few adjustments.
This also changes __va and __pa to use an equivalent definition that is
simpler. With the relocatable kernel, PAGE_OFFSET and MEMORY_START are
constants (for 64-bit) whereas PHYSICAL_START is a variable (and
KERNELBASE ideally should be too, but isn't yet).
With this, relocatable kernels still copy themselves down to physical
address 0 and run there.
Signed-off-by: Paul Mackerras <paulus@samba.org>
2008-08-30 05:43:47 +04:00
bne 6 f
ld r6 ,0 ( r9 ) / * r e l o c - > r _ o f f s e t * /
ld r0 ,1 6 ( r9 ) / * r e l o c - > r _ a d d e n d * /
add r0 ,r0 ,r3
stdx r0 ,r7 ,r6
addi r9 ,r9 ,2 4
bdnz 5 b
6 : blr
2014-03-04 01:31:24 +04:00
.balign 8
2017-03-09 08:42:12 +03:00
p_dyn : .8byte _ _ d y n a m i c _ s t a r t - 0 b
p_rela : .8byte _ _ r e l a _ d y n _ s t a r t - 0 b
p_st : .8byte _ s t e x t - 0 b
powerpc: Make the 64-bit kernel as a position-independent executable
This implements CONFIG_RELOCATABLE for 64-bit by making the kernel as
a position-independent executable (PIE) when it is set. This involves
processing the dynamic relocations in the image in the early stages of
booting, even if the kernel is being run at the address it is linked at,
since the linker does not necessarily fill in words in the image for
which there are dynamic relocations. (In fact the linker does fill in
such words for 64-bit executables, though not for 32-bit executables,
so in principle we could avoid calling relocate() entirely when we're
running a 64-bit kernel at the linked address.)
The dynamic relocations are processed by a new function relocate(addr),
where the addr parameter is the virtual address where the image will be
run. In fact we call it twice; once before calling prom_init, and again
when starting the main kernel. This means that reloc_offset() returns
0 in prom_init (since it has been relocated to the address it is running
at), which necessitated a few adjustments.
This also changes __va and __pa to use an equivalent definition that is
simpler. With the relocatable kernel, PAGE_OFFSET and MEMORY_START are
constants (for 64-bit) whereas PHYSICAL_START is a variable (and
KERNELBASE ideally should be too, but isn't yet).
With this, relocatable kernels still copy themselves down to physical
address 0 and run there.
Signed-off-by: Paul Mackerras <paulus@samba.org>
2008-08-30 05:43:47 +04:00