summaryrefslogtreecommitdiff
path: root/MdePkg/Library/DynamicStackCookieEntryPointLib/LoongArch64/DynamicCookieGcc.S
blob: 1bf04ca952d4abbdabef7d7cc8ab226ab52cd7c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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