1/* -----------------------------------------------------------------------
2   sysv.S - Copyright (c) 1998 Geoffrey Keating
3   Copyright (C) 2007 Free Software Foundation, Inc
4
5   PowerPC Assembly glue.
6
7   Permission is hereby granted, free of charge, to any person obtaining
8   a copy of this software and associated documentation files (the
9   ``Software''), to deal in the Software without restriction, including
10   without limitation the rights to use, copy, modify, merge, publish,
11   distribute, sublicense, and/or sell copies of the Software, and to
12   permit persons to whom the Software is furnished to do so, subject to
13   the following conditions:
14
15   The above copyright notice and this permission notice shall be included
16   in all copies or substantial portions of the Software.
17
18   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
19   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21   NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25   DEALINGS IN THE SOFTWARE.
26   ----------------------------------------------------------------------- */
27
28#define LIBFFI_ASM
29#include <fficonfig.h>
30#include <ffi.h>
31#include <powerpc/asm.h>
32
33#ifndef POWERPC64
34FFI_HIDDEN(ffi_call_SYSV)
35ENTRY(ffi_call_SYSV)
36	.cfi_startproc
37	/* Save the old stack pointer as AP.  */
38	mr	%r10,%r1
39	.cfi_def_cfa_register 10
40
41	/* Allocate the stack space we need.  */
42	stwux	%r1,%r1,%r8
43	/* Save registers we use.  */
44	mflr	%r9
45	stw	%r28,-16(%r10)
46	stw	%r29,-12(%r10)
47	stw	%r30, -8(%r10)
48	stw	%r31, -4(%r10)
49	stw	%r9,   4(%r10)
50	.cfi_offset 65, 4
51	.cfi_offset 31, -4
52	.cfi_offset 30, -8
53	.cfi_offset 29, -12
54	.cfi_offset 28, -16
55
56	/* Save arguments over call...  */
57	stw	%r7,   -20(%r10)	/* closure, */
58	mr	%r31,%r6		/* flags, */
59	mr	%r30,%r5		/* rvalue, */
60	mr	%r29,%r4		/* function address, */
61	mr	%r28,%r10		/* our AP. */
62	.cfi_def_cfa_register 28
63
64	/* Call ffi_prep_args_SYSV.  */
65	mr	%r4,%r1
66	bl	ffi_prep_args_SYSV@local
67
68	/* Now do the call.  */
69	/* Set up cr1 with bits 4-7 of the flags.  */
70	mtcrf	0x40,%r31
71	/* Get the address to call into CTR.  */
72	mtctr	%r29
73	/* Load all those argument registers.  */
74	lwz	%r3,-24-(8*4)(%r28)
75	lwz	%r4,-24-(7*4)(%r28)
76	lwz	%r5,-24-(6*4)(%r28)
77	lwz	%r6,-24-(5*4)(%r28)
78	bf-	5,1f
79	nop
80	lwz	%r7,-24-(4*4)(%r28)
81	lwz	%r8,-24-(3*4)(%r28)
82	lwz	%r9,-24-(2*4)(%r28)
83	lwz	%r10,-24-(1*4)(%r28)
84	nop
851:
86
87#ifndef __NO_FPRS__
88	/* Load all the FP registers.  */
89	bf-	6,2f
90	lfd	%f1,-24-(8*4)-(8*8)(%r28)
91	lfd	%f2,-24-(8*4)-(7*8)(%r28)
92	lfd	%f3,-24-(8*4)-(6*8)(%r28)
93	lfd	%f4,-24-(8*4)-(5*8)(%r28)
94	nop
95	lfd	%f5,-24-(8*4)-(4*8)(%r28)
96	lfd	%f6,-24-(8*4)-(3*8)(%r28)
97	lfd	%f7,-24-(8*4)-(2*8)(%r28)
98	lfd	%f8,-24-(8*4)-(1*8)(%r28)
99#endif
1002:
101
102	/* Make the call.  */
103	lwz	%r11, -20(%r28)
104	bctrl
105
106	/* Now, deal with the return value.  */
107	mtcrf	0x01,%r31 /* cr7  */
108	bt-	31,L(small_struct_return_value)
109	bt-	30,L(done_return_value)
110#ifndef __NO_FPRS__
111	bt-	29,L(fp_return_value)
112#endif
113	stw	%r3,0(%r30)
114	bf+	28,L(done_return_value)
115	stw	%r4,4(%r30)
116	mtcrf	0x02,%r31 /* cr6  */
117	bf	27,L(done_return_value)
118	stw     %r5,8(%r30)
119	stw	%r6,12(%r30)
120	/* Fall through...  */
121
122L(done_return_value):
123	/* Restore the registers we used and return.  */
124	lwz	%r9,   4(%r28)
125	lwz	%r31, -4(%r28)
126	mtlr	%r9
127	lwz	%r30, -8(%r28)
128	lwz	%r29,-12(%r28)
129	lwz	%r28,-16(%r28)
130	.cfi_remember_state
131	/* At this point we don't have a cfa register.  Say all our
132	   saved regs have been restored.  */
133	.cfi_same_value 65
134	.cfi_same_value 31
135	.cfi_same_value 30
136	.cfi_same_value 29
137	.cfi_same_value 28
138	/* Hopefully this works..  */
139	.cfi_def_cfa_register 1
140	.cfi_offset 1, 0
141	lwz	%r1,0(%r1)
142	.cfi_same_value 1
143	blr
144
145#ifndef __NO_FPRS__
146L(fp_return_value):
147	.cfi_restore_state
148	bf	28,L(float_return_value)
149	stfd	%f1,0(%r30)
150	mtcrf   0x02,%r31 /* cr6  */
151	bf	27,L(done_return_value)
152	stfd	%f2,8(%r30)
153	b	L(done_return_value)
154L(float_return_value):
155	stfs	%f1,0(%r30)
156	b	L(done_return_value)
157#endif
158
159L(small_struct_return_value):
160	/*
161	 * The C code always allocates a properly-aligned 8-byte bounce
162	 * buffer to make this assembly code very simple.  Just write out
163	 * r3 and r4 to the buffer to allow the C code to handle the rest.
164	 */
165	stw %r3, 0(%r30)
166	stw %r4, 4(%r30)
167	b L(done_return_value)
168	.cfi_endproc
169
170END(ffi_call_SYSV)
171
172#if defined __ELF__ && defined __linux__
173	.section	.note.GNU-stack,"",@progbits
174#endif
175#endif
176