1//===-- xray_trampoline_loongarch64.s ---------------------------*- ASM -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of XRay, a dynamic runtime instrumentation system.
10//
11// This implements the loongarch-specific assembler for the trampolines.
12//
13//===----------------------------------------------------------------------===//
14
15#include "../sanitizer_common/sanitizer_asm.h"
16
17#define FROM_0_TO_7 0,1,2,3,4,5,6,7
18#define FROM_7_TO_0 7,6,5,4,3,2,1,0
19
20.macro SAVE_ARG_REGISTERS
21  .irp i,FROM_7_TO_0
22    st.d  $a\i, $sp, (8 * 8 + 8 * \i)
23  .endr
24  .irp i,FROM_7_TO_0
25    fst.d $f\i, $sp, (8 * \i)
26  .endr
27.endm
28
29.macro RESTORE_ARG_REGISTERS
30  .irp i,FROM_0_TO_7
31    fld.d $f\i, $sp, (8 * \i)
32  .endr
33  .irp i,FROM_0_TO_7
34    ld.d  $a\i, $sp, (8 * 8 + 8 * \i)
35  .endr
36.endm
37
38.macro SAVE_RET_REGISTERS
39  st.d    $a1, $sp, 24
40  st.d    $a0, $sp, 16
41  fst.d   $f1, $sp, 8
42  fst.d   $f0, $sp, 0
43.endm
44
45.macro RESTORE_RET_REGISTERS
46  fld.d   $f0, $sp, 0
47  fld.d   $f1, $sp, 8
48  ld.d    $a0, $sp, 16
49  ld.d    $a1, $sp, 24
50.endm
51
52  .text
53  .file "xray_trampoline_loongarch64.S"
54  .globl ASM_SYMBOL(__xray_FunctionEntry)
55  ASM_HIDDEN(__xray_FunctionEntry)
56  .p2align 2
57  ASM_TYPE_FUNCTION(__xray_FunctionEntry)
58ASM_SYMBOL(__xray_FunctionEntry):
59  .cfi_startproc
60  // Save argument registers before doing any actual work.
61  .cfi_def_cfa_offset 136
62  addi.d  $sp, $sp, -136
63  st.d    $ra, $sp, 128
64  .cfi_offset 1, -8
65  SAVE_ARG_REGISTERS
66
67  la.got  $t2, ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)
68  ld.d    $t2, $t2, 0
69
70  beqz    $t2, FunctionEntry_restore
71
72  // a1=0 means that we are tracing an entry event.
73  move    $a1, $zero
74  // Function ID is in t1 (the first parameter).
75  move    $a0, $t1
76  jirl    $ra, $t2, 0
77
78FunctionEntry_restore:
79  // Restore argument registers.
80  RESTORE_ARG_REGISTERS
81  ld.d    $ra, $sp, 128
82  addi.d  $sp, $sp, 136
83  ret
84FunctionEntry_end:
85  ASM_SIZE(__xray_FunctionEntry)
86  .cfi_endproc
87
88  .text
89  .globl ASM_SYMBOL(__xray_FunctionExit)
90  ASM_HIDDEN(__xray_FunctionExit)
91  .p2align 2
92  ASM_TYPE_FUNCTION(__xray_FunctionExit)
93ASM_SYMBOL(__xray_FunctionExit):
94  .cfi_startproc
95  // Save return registers before doing any actual work.
96  .cfi_def_cfa_offset 48
97  addi.d  $sp, $sp, -48
98  st.d    $ra, $sp, 40
99  .cfi_offset 1, -8
100  st.d    $fp, $sp, 32
101  SAVE_RET_REGISTERS
102
103  la.got  $t2, ASM_SYMBOL(_ZN6__xray19XRayPatchedFunctionE)
104  ld.d    $t2, $t2, 0
105
106  beqz    $t2, FunctionExit_restore
107
108  // a1=1 means that we are tracing an exit event.
109  li.w    $a1, 1
110  // Function ID is in t1 (the first parameter).
111  move    $a0, $t1
112  jirl    $ra, $t2, 0
113
114FunctionExit_restore:
115  // Restore return registers.
116  RESTORE_RET_REGISTERS
117  ld.d    $fp, $sp, 32
118  ld.d    $ra, $sp, 40
119  addi.d  $sp, $sp, 48
120  ret
121
122FunctionExit_end:
123  ASM_SIZE(__xray_FunctionExit)
124  .cfi_endproc
125