MdePkg/DynamicStackCookieEntryPointLib: Add LoongArch64 support

Added LoongArch64 support. Currently, LoongArch64 doesn't supports TRNG,
it uses the `RDTIME` instruction and a xorshif64 alorithms to compose a
PRNG(RngLib).
It may supports the SE TRNG in the future, and will change to the TRNG
when SE support becomes available.

Signed-off-by: Chao Li <lichao@loongson.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
This commit is contained in:
Chao Li 2026-07-06 11:09:10 +08:00 committed by mergify[bot]
parent 8756401a02
commit 42fde88a42
5 changed files with 99 additions and 4 deletions

View file

@ -19,7 +19,7 @@
#
# VALID_ARCHITECTURES = IA32 X64 AARCH64
# VALID_ARCHITECTURES = IA32 X64 AARCH64 LOONGARCH64
#
[Sources]
@ -36,6 +36,9 @@
[Sources.AARCH64]
AArch64/DynamicCookieGcc.S | GCC
[Sources.LOONGARCH64]
LoongArch64/DynamicCookieGcc.S | GCC
[Packages]
MdePkg/MdePkg.dec
@ -43,3 +46,6 @@
BaseLib
DebugLib
StackCheckLib
[LibraryClasses.LOONGARCH64]
RngLib

View file

@ -0,0 +1,71 @@
#------------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation.
# Copyright (c) 2026, Loongson Technology Corporation Limited. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
# Module Name:
#
# DynamicCookieGcc.S
#
# Abstract:
#
# Generates random number through the RdTime instruction on a 64-bit LOONGARCH64 platform
# to store a random value in the GCC __stack_check_guard stack cookie.
# The first byte is 0'd to prevent string copy functions from clobbering
# the stack cookie.
#
# Notes:
#
# Use the RngLib library to get random numbers. LoongArch64 currently does not support like
# `RdRand` or `RNDR` instructions, it can use the `RdTime` and a xorshift64 algorithms to
# generate pseudo-random numbers.
#
#------------------------------------------------------------------------------
#include <Library/RngLib.h>
ASM_GLOBAL ASM_PFX (_ModuleEntryPoint)
#------------------------------------------------------------------------------
# VOID
# EFIAPI
# _ModuleEntryPoint (
# The parameters are saved to the stack when it calls other functions.
# )
#------------------------------------------------------------------------------
ASM_PFX(_ModuleEntryPoint):
addi.d $sp, $sp, -0x20
st.d $a0, $sp, 0x0
st.d $a1, $sp, 0x8
st.d $ra, $sp, 0x10
//
// Use the stack to pass parameters.
//
addi.d $a0, $sp, 0x18
bl GetRandomNumber64
//
// Load the random number from the stack.
//
ld.d $t0, $sp, 0x18
//
// Zero the first byte of the random value
//
bstrins.d $t0, $zero, 7, 0
la.local $t1, __stack_chk_guard
st.d $t0, $t1, 0
dbar 0
ld.d $a0, $sp, 0x0
ld.d $a1, $sp, 0x8
ld.d $ra, $sp, 0x10
addi.d $sp, $sp, 0x20
c_entry:
b _CModuleEntryPoint // Jump to the C module entry point
.end

View file

@ -23,7 +23,7 @@
#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = X64 AARCH64
# VALID_ARCHITECTURES = X64 AARCH64 LOONGARCH64
#
[Sources]
@ -40,6 +40,9 @@
[Sources.AARCH64]
AArch64/DynamicCookieGcc.S | GCC
[Sources.LOONGARCH64]
LoongArch64/DynamicCookieGcc.S | GCC
[Packages]
MdePkg/MdePkg.dec
@ -49,5 +52,8 @@
MmServicesTableLib
StackCheckLib
[LibraryClasses.LOONGARCH64]
RngLib
[Protocols]
gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES

View file

@ -18,7 +18,7 @@
LIBRARY_CLASS = UefiApplicationEntryPoint|UEFI_APPLICATION
#
# VALID_ARCHITECTURES = IA32 X64 AARCH64
# VALID_ARCHITECTURES = IA32 X64 AARCH64 LOONGARCH64
#
[Sources]
@ -35,6 +35,9 @@
[Sources.AARCH64]
AArch64/DynamicCookieGcc.S | GCC
[Sources.LOONGARCH64]
LoongArch64/DynamicCookieGcc.S | GCC
[Packages]
MdePkg/MdePkg.dec
@ -43,3 +46,6 @@
DebugLib
BaseLib
StackCheckLib
[LibraryClasses.LOONGARCH64]
RngLib

View file

@ -20,7 +20,7 @@
#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 AARCH64
# VALID_ARCHITECTURES = IA32 X64 AARCH64 LOONGARCH64
#
[Sources]
@ -40,12 +40,18 @@
[Sources.AARCH64]
AArch64/DynamicCookieGcc.S | GCC
[Sources.LOONGARCH64]
LoongArch64/DynamicCookieGcc.S | GCC
[LibraryClasses]
UefiBootServicesTableLib
DebugLib
BaseLib
StackCheckLib
[LibraryClasses.LOONGARCH64]
RngLib
[Protocols]
gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES