Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/test/asm/trampoline-ppc.pl

raw
1 #!/usr/bin/env perl
2 # Copyright (c) 2019, Google Inc.
3 #
4 # SPDX-License-Identifier: ISC
5
6 # This file defines helper functions for crypto/test/abi_test.h on ppc64le. See
7 # that header for details on how to use this.
8 #
9 # For convenience, this file is linked into libcrypto, where consuming builds
10 # already support architecture-specific sources. The static linker should drop
11 # this code in non-test binaries. This includes a shared library build of
12 # libcrypto, provided --gc-sections or equivalent is used.
13 #
14 # References:
15 #
16 # ELFv2: http://openpowerfoundation.org/wp-content/uploads/resources/leabi/leabi-20170510.pdf
17
18 use strict;
19
20 my $flavour = shift;
21 my $output = shift;
22
23 $0 =~ m/(.*[\/\\])[^\/\\]+$/;
24 my $dir = $1;
25 my $xlate;
26 ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
27 ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
28 die "can't locate ppc-xlate.pl";
29
30 open OUT, "| \"$^X\" \"$xlate\" $flavour \"$output\"";
31 *STDOUT = *OUT;
32
33 unless ($flavour =~ /linux.*64le/) {
34 die "This file only supports the ELFv2 ABI, used by ppc64le";
35 }
36
37 my $code = "";
38
39 sub load_or_store_regs {
40 # $op is "l" or "st".
41 my ($op, $base_reg, $base_offset) = @_;
42 # Vector registers.
43 foreach (20..31) {
44 my $offset = $base_offset + ($_ - 20) * 16;
45 # Vector registers only support indexed register addressing.
46 $code .= "\tli\tr11, $offset\n";
47 $code .= "\t${op}vx\tv$_, r11, $base_reg\n";
48 }
49 # Save general registers.
50 foreach (14..31) {
51 my $offset = $base_offset + 192 + ($_ - 14) * 8;
52 $code .= "\t${op}d\tr$_, $offset($base_reg)\n";
53 }
54 # Save floating point registers.
55 foreach (14..31) {
56 my $offset = $base_offset + 336 + ($_ - 14) * 8;
57 $code .= "\t${op}fd\tf$_, $offset($base_reg)\n";
58 }
59 }
60
61 sub load_regs {
62 my ($base_reg, $base_offset) = @_;
63 load_or_store_regs("l", $base_reg, $base_offset);
64 }
65
66 sub store_regs {
67 my ($base_reg, $base_offset) = @_;
68 load_or_store_regs("st", $base_reg, $base_offset);
69 }
70
71 my ($func, $state, $argv, $argc) = ("r3", "r4", "r5", "r6");
72 $code .= <<____;
73 .machine "any"
74 .text
75
76 # abi_test_trampoline loads callee-saved registers from |state|, calls |func|
77 # with |argv|, then saves the callee-saved registers into |state|. It returns
78 # the result of |func|. The |unwind| argument is unused.
79 # uint64_t abi_test_trampoline(void (*func)(...), CallerState *state,
80 # const uint64_t *argv, size_t argc,
81 # uint64_t unwind);
82 .globl abi_test_trampoline
83 .align 5
84 abi_test_trampoline:
85 # LR is saved into the caller's stack frame.
86 mflr r0
87 std r0, 16(r1)
88
89 # Allocate 66*8 = 528 bytes of stack frame. From the top of the stack
90 # to the bottom, the stack frame is:
91 #
92 # 0(r1) - Back chain pointer
93 # 8(r1) - CR save area
94 # 16(r1) - LR save area (for |func|)
95 # 24(r1) - TOC pointer save area
96 # 32(r1) - Saved copy of |state|
97 # 40(r1) - Padding
98 # 48(r1) - Vector register save area (v20-v31, 12 registers)
99 # 240(r1) - General register save area (r14-r31, 18 registers)
100 # 384(r1) - Floating point register save area (f14-f31, 18 registers)
101 #
102 # Note the layouts of the register save areas and CallerState match.
103 #
104 # In the ELFv2 ABI, the parameter save area is optional if the function
105 # is non-variadic and all parameters fit in registers. We only support
106 # such functions, so we omit it to test that |func| does not rely on it.
107 stdu r1, -528(r1)
108
109 mfcr r0
110 std r0, 8(r1) # Save CR
111 std r2, 24(r1) # Save TOC
112 std $state, 32(r1) # Save |state|
113 ____
114 # Save registers to the stack.
115 store_regs("r1", 48);
116 # Load registers from the caller.
117 load_regs($state, 0);
118 $code .= <<____;
119 # Load CR from |state|.
120 ld r0, 480($state)
121 mtcr r0
122
123 # Move parameters into temporary registers so they are not clobbered.
124 addi r11, $argv, -8 # Adjust for ldu below
125 mr r12, $func
126
127 # Load parameters into registers.
128 cmpdi $argc, 0
129 beq .Largs_done
130 mtctr $argc
131 ldu r3, 8(r11)
132 bdz .Largs_done
133 ldu r4, 8(r11)
134 bdz .Largs_done
135 ldu r5, 8(r11)
136 bdz .Largs_done
137 ldu r6, 8(r11)
138 bdz .Largs_done
139 ldu r7, 8(r11)
140 bdz .Largs_done
141 ldu r8, 8(r11)
142 bdz .Largs_done
143 ldu r9, 8(r11)
144 bdz .Largs_done
145 ldu r10, 8(r11)
146
147 .Largs_done:
148 li r2, 0 # Clear TOC to test |func|'s global entry point
149 mtctr r12
150 bctrl
151 ld r2, 24(r1) # Restore TOC
152
153 ld $state, 32(r1) # Reload |state|
154 ____
155 # Output resulting registers to the caller.
156 store_regs($state, 0);
157 # Restore registers from the stack.
158 load_regs("r1", 48);
159 $code .= <<____;
160 mfcr r0
161 std r0, 480($state) # Output CR to caller
162 ld r0, 8(r1)
163 mtcrf 0b00111000, r0 # Restore CR2-CR4
164 addi r1, r1, 528
165 ld r0, 16(r1) # Restore LR
166 mtlr r0
167 blr
168 .size abi_test_trampoline,.-abi_test_trampoline
169 ____
170
171 # abi_test_clobber_* clobbers the corresponding register. These are used to test
172 # the ABI-testing framework.
173 foreach (0..31) {
174 # r1 is the stack pointer. r13 is the thread pointer.
175 next if ($_ == 1 || $_ == 13);
176 $code .= <<____;
177 .globl abi_test_clobber_r$_
178 .align 5
179 abi_test_clobber_r$_:
180 li r$_, 0
181 blr
182 .size abi_test_clobber_r$_,.-abi_test_clobber_r$_
183 ____
184 }
185
186 foreach (0..31) {
187 $code .= <<____;
188 .globl abi_test_clobber_f$_
189 .align 4
190 abi_test_clobber_f$_:
191 li r0, 0
192 # Use the red zone.
193 std r0, -8(r1)
194 lfd f$_, -8(r1)
195 blr
196 .size abi_test_clobber_f$_,.-abi_test_clobber_f$_
197 ____
198 }
199
200 foreach (0..31) {
201 $code .= <<____;
202 .globl abi_test_clobber_v$_
203 .align 4
204 abi_test_clobber_v$_:
205 vxor v$_, v$_, v$_
206 blr
207 .size abi_test_clobber_v$_,.-abi_test_clobber_v$_
208 ____
209 }
210
211 foreach (0..7) {
212 # PPC orders CR fields in big-endian, so the mask is reversed from what one
213 # would expect.
214 my $mask = 1 << (7 - $_);
215 $code .= <<____;
216 .globl abi_test_clobber_cr$_
217 .align 4
218 abi_test_clobber_cr$_:
219 # Flip the bits on cr$_ rather than setting to zero. With a four-bit
220 # register, zeroing it will do nothing 1 in 16 times.
221 mfcr r0
222 not r0, r0
223 mtcrf $mask, r0
224 blr
225 .size abi_test_clobber_cr$_,.-abi_test_clobber_cr$_
226 ____
227 }
228
229 $code .= <<____;
230 .globl abi_test_clobber_ctr
231 .align 4
232 abi_test_clobber_ctr:
233 li r0, 0
234 mtctr r0
235 blr
236 .size abi_test_clobber_ctr,.-abi_test_clobber_ctr
237
238 .globl abi_test_clobber_lr
239 .align 4
240 abi_test_clobber_lr:
241 mflr r0
242 mtctr r0
243 li r0, 0
244 mtlr r0
245 bctr
246 .size abi_test_clobber_lr,.-abi_test_clobber_lr
247
248 ____
249
250 print $code;
251 close STDOUT or die "error closing STDOUT: $!";