2022-05-31 13:04:11 +03:00
/* SPDX-License-Identifier: GPL-2.0 */
/ *
* Copyright ( C ) 2 0 2 0 - 2 0 2 2 L o o n g s o n T e c h n o l o g y C o r p o r a t i o n L i m i t e d
* /
# include < l i n u x / i n i t . h >
# include < l i n u x / t h r e a d s . h >
# include < a s m / a d d r s p a c e . h >
# include < a s m / a s m . h >
# include < a s m / a s m m a c r o . h >
2022-10-12 11:36:19 +03:00
# include < a s m / b u g . h >
2022-05-31 13:04:11 +03:00
# include < a s m / r e g d e f . h >
# include < a s m / l o o n g a r c h . h >
# include < a s m / s t a c k f r a m e . h >
efi/loongarch: Add efistub booting support
This patch adds efistub booting support, which is the standard UEFI boot
protocol for LoongArch to use.
We use generic efistub, which means we can pass boot information (i.e.,
system table, memory map, kernel command line, initrd) via a light FDT
and drop a lot of non-standard code.
We use a flat mapping to map the efi runtime in the kernel's address
space. In efi, VA = PA; in kernel, VA = PA + PAGE_OFFSET. As a result,
flat mapping is not identity mapping, SetVirtualAddressMap() is still
needed for the efi runtime.
Tested-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
[ardb: change fpic to fpie as suggested by Xi Ruoyao]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-19 13:20:37 +03:00
# ifdef C O N F I G _ E F I _ S T U B
# include " e f i - h e a d e r . S "
_ _ HEAD
_head :
.word MZ_MAGIC /* "MZ", MS-DOS header */
2022-10-12 11:36:19 +03:00
.org 0x8
.dword kernel_entry /* Kernel entry point */
.dword _end - _ text / * K e r n e l i m a g e e f f e c t i v e s i z e * /
.quad 0 /* Kernel image load offset from start of RAM */
efi: Put Linux specific magic number in the DOS header
GRUB currently relies on the magic number in the image header of ARM and
arm64 EFI kernel images to decide whether or not the image in question
is a bootable kernel.
However, the purpose of the magic number is to identify the image as one
that implements the bare metal boot protocol, and so GRUB, which only
does EFI boot, is limited unnecessarily to booting images that could
potentially be booted in a non-EFI manner as well.
This is problematic for the new zboot decompressor image format, as it
can only boot in EFI mode, and must therefore not use the bare metal
boot magic number in its header.
For this reason, the strict magic number was dropped from GRUB, to
permit essentially any kind of EFI executable to be booted via the
'linux' command, blurring the line between the linux loader and the
chainloader.
So let's use the same field in the DOS header that RISC-V and arm64
already use for their 'bare metal' magic numbers to store a 'generic
Linux kernel' magic number, which can be used to identify bootable
kernel images in PE format which don't necessarily implement a bare
metal boot protocol in the same binary. Note that, in the context of
EFI, the MS-DOS header is only described in terms of the fields that it
shares with the hybrid PE/COFF image format, (i.e., the MS-DOS EXE magic
number at offset #0 and the PE header offset at byte offset #0x3c).
Since we aim for compatibility with EFI only, and not with MS-DOS or
MS-Windows, we can use the remaining space in the MS-DOS header however
we want.
Let's set the generic magic number for x86 images as well: existing
bootloaders already have their own methods to identify x86 Linux images
that can be booted in a non-EFI manner, and having the magic number in
place there will ease any future transitions in loader implementations
to merge the x86 and non-x86 EFI boot paths.
Note that 32-bit ARM already uses the same location in the header for a
different purpose, but the ARM support is already widely implemented and
the EFI zboot decompressor is not available on ARM anyway, so we just
disregard it here.
Acked-by: Leif Lindholm <quic_llindhol@quicinc.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-11-09 17:16:11 +03:00
.org 0x38 /* 0x20 ~ 0x37 reserved */
.long LINUX_PE_MAGIC
efi/loongarch: Add efistub booting support
This patch adds efistub booting support, which is the standard UEFI boot
protocol for LoongArch to use.
We use generic efistub, which means we can pass boot information (i.e.,
system table, memory map, kernel command line, initrd) via a light FDT
and drop a lot of non-standard code.
We use a flat mapping to map the efi runtime in the kernel's address
space. In efi, VA = PA; in kernel, VA = PA + PAGE_OFFSET. As a result,
flat mapping is not identity mapping, SetVirtualAddressMap() is still
needed for the efi runtime.
Tested-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
[ardb: change fpic to fpie as suggested by Xi Ruoyao]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-08-19 13:20:37 +03:00
.long pe_header - _ head / * O f f s e t t o t h e P E h e a d e r * /
pe_header :
_ _ EFI_ P E _ H E A D E R
SYM_ D A T A ( k e r n e l _ a s i z e , . l o n g _ e n d - _ t e x t ) ;
SYM_ D A T A ( k e r n e l _ f s i z e , . l o n g _ e d a t a - _ t e x t ) ;
SYM_ D A T A ( k e r n e l _ o f f s e t , . l o n g k e r n e l _ o f f s e t - _ t e x t ) ;
# endif
2022-05-31 13:04:11 +03:00
_ _ REF
2022-09-26 17:25:29 +03:00
.align 12
2022-05-31 13:04:11 +03:00
SYM_ C O D E _ S T A R T ( k e r n e l _ e n t r y ) # k e r n e l e n t r y p o i n t
/* Config direct window and set PG */
li. d t 0 , C S R _ D M W 0 _ I N I T # U C , P L V 0 , 0 x80 0 0 x x x x x x x x x x x x
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 0
li. d t 0 , C S R _ D M W 1 _ I N I T # C A , P L V 0 , 0 x90 0 0 x x x x x x x x x x x x
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 1
2022-08-11 15:52:12 +03:00
/ * We m i g h t n o t g e t l a u n c h e d a t t h e a d d r e s s t h e k e r n e l i s l i n k e d t o ,
so w e j u m p t h e r e . * /
la. a b s t 0 , 0 f
jr t 0
0 :
2022-05-31 13:04:11 +03:00
/* Enable PG */
li. w t 0 , 0 x b0 # P L V = 0 , I E =0 , P G =1
csrwr t 0 , L O O N G A R C H _ C S R _ C R M D
li. w t 0 , 0 x04 # P L V = 0 , P I E =1 , P W E =0
csrwr t 0 , L O O N G A R C H _ C S R _ P R M D
li. w t 0 , 0 x00 # F P E = 0 , S X E =0 , A S X E =0 , B T E =0
csrwr t 0 , L O O N G A R C H _ C S R _ E U E N
2022-10-12 11:36:08 +03:00
la. p c r e l t 0 , _ _ b s s _ s t a r t # c l e a r . b s s
2022-05-31 13:04:11 +03:00
st. d z e r o , t 0 , 0
2022-10-12 11:36:08 +03:00
la. p c r e l t 1 , _ _ b s s _ s t o p - L O N G S I Z E
2022-05-31 13:04:11 +03:00
1 :
addi. d t 0 , t 0 , L O N G S I Z E
st. d z e r o , t 0 , 0
bne t 0 , t 1 , 1 b
2022-10-12 11:36:08 +03:00
la. p c r e l t 0 , f w _ a r g 0
2022-05-31 13:04:11 +03:00
st. d a0 , t 0 , 0 # f i r m w a r e a r g u m e n t s
2022-10-12 11:36:08 +03:00
la. p c r e l t 0 , f w _ a r g 1
2022-05-31 13:04:11 +03:00
st. d a1 , t 0 , 0
2022-10-12 11:36:08 +03:00
la. p c r e l t 0 , f w _ a r g 2
efi/loongarch: libstub: remove dependency on flattened DT
LoongArch does not use FDT or DT natively [yet], and the only reason it
currently uses it is so that it can reuse the existing EFI stub code.
Overloading the DT with data passed between the EFI stub and the core
kernel has been a source of problems: there is the overlap between
information provided by EFI which DT can also provide (initrd base/size,
command line, memory descriptions), requiring us to reason about which
is which and what to prioritize. It has also resulted in ABI leaks,
i.e., internal ABI being promoted to external ABI inadvertently because
the bootloader can set the EFI stub's DT properties as well (e.g.,
"kaslr-seed"). This has become especially problematic with boot
environments that want to pretend that EFI boot is being done (to access
ACPI and SMBIOS tables, for instance) but have no ability to execute the
EFI stub, and so the environment that the EFI stub creates is emulated
[poorly, in some cases].
Another downside of treating DT like this is that the DT binary that the
kernel receives is different from the one created by the firmware, which
is undesirable in the context of secure and measured boot.
Given that LoongArch support in Linux is brand new, we can avoid these
pitfalls, and treat the DT strictly as a hardware description, and use a
separate handover method between the EFI stub and the kernel. Now that
initrd loading and passing the EFI memory map have been refactored into
pure EFI routines that use EFI configuration tables, the only thing we
need to pass directly is the kernel command line (even if we could pass
this via a config table as well, it is used extremely early, so passing
it directly is preferred in this case.)
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Acked-by: Huacai Chen <chenhuacai@loongson.cn>
2022-09-16 20:48:53 +03:00
st. d a2 , t 0 , 0
2022-05-31 13:04:11 +03:00
/* KSave3 used for percpu base, initialized as 0 */
csrwr z e r o , P E R C P U _ B A S E _ K S
/* GPR21 used for percpu base (runtime), initialized as 0 */
2022-07-26 18:57:18 +03:00
move u 0 , z e r o
2022-05-31 13:04:11 +03:00
2022-10-12 11:36:08 +03:00
la. p c r e l t p , i n i t _ t h r e a d _ u n i o n
2022-05-31 13:04:11 +03:00
/* Set the SP after an empty pt_regs. */
2022-10-29 11:29:31 +03:00
PTR_ L I s p , ( _ T H R E A D _ S I Z E - P T _ S I Z E )
2022-05-31 13:04:11 +03:00
PTR_ A D D s p , s p , t p
set_ s a v e d _ s p s p , t 0 , t 1
bl s t a r t _ k e r n e l
2022-10-12 11:36:19 +03:00
ASM_ B U G ( )
2022-05-31 13:04:11 +03:00
SYM_ C O D E _ E N D ( k e r n e l _ e n t r y )
2022-05-31 13:04:12 +03:00
# ifdef C O N F I G _ S M P
/ *
* SMP s l a v e c p u s e n t r y p o i n t . B o a r d s p e c i f i c c o d e f o r b o o t s t r a p c a l l s t h i s
* function a f t e r s e t t i n g u p t h e s t a c k a n d t p r e g i s t e r s .
* /
SYM_ C O D E _ S T A R T ( s m p b o o t _ e n t r y )
li. d t 0 , C S R _ D M W 0 _ I N I T # U C , P L V 0
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 0
li. d t 0 , C S R _ D M W 1 _ I N I T # C A , P L V 0
csrwr t 0 , L O O N G A R C H _ C S R _ D M W I N 1
2022-08-11 15:52:12 +03:00
la. a b s t 0 , 0 f
jr t 0
0 :
/* Enable PG */
2022-05-31 13:04:12 +03:00
li. w t 0 , 0 x b0 # P L V = 0 , I E =0 , P G =1
csrwr t 0 , L O O N G A R C H _ C S R _ C R M D
li. w t 0 , 0 x04 # P L V = 0 , P I E =1 , P W E =0
csrwr t 0 , L O O N G A R C H _ C S R _ P R M D
li. w t 0 , 0 x00 # F P E = 0 , S X E =0 , A S X E =0 , B T E =0
csrwr t 0 , L O O N G A R C H _ C S R _ E U E N
2023-02-25 10:52:56 +03:00
la. p c r e l t 0 , c p u b o o t _ d a t a
2022-05-31 13:04:12 +03:00
ld. d s p , t 0 , C P U _ B O O T _ S T A C K
ld. d t p , t 0 , C P U _ B O O T _ T I N F O
bl s t a r t _ s e c o n d a r y
2022-10-12 11:36:19 +03:00
ASM_ B U G ( )
2022-05-31 13:04:12 +03:00
SYM_ C O D E _ E N D ( s m p b o o t _ e n t r y )
# endif / * C O N F I G _ S M P * /
2022-05-31 13:04:11 +03:00
SYM_ E N T R Y ( k e r n e l _ e n t r y _ e n d , S Y M _ L _ G L O B A L , S Y M _ A _ N O N E )