1/* -----------------------------------------------------------------------
2   sysv.S - Copyright (c) 2014 Sebastian Macke <sebastian@macke.de>
3
4   OpenRISC Foreign Function Interface
5
6   Permission is hereby granted, free of charge, to any person obtaining
7   a copy of this software and associated documentation files (the
8   ``Software''), to deal in the Software without restriction, including
9   without limitation the rights to use, copy, modify, merge, publish,
10   distribute, sublicense, and/or sell copies of the Software, and to
11   permit persons to whom the Software is furnished to do so, subject to
12   the following conditions:
13
14   The above copyright notice and this permission notice shall be included
15   in all copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
18   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20   NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24   DEALINGS IN THE SOFTWARE.
25   ----------------------------------------------------------------------- */
26
27#define LIBFFI_ASM
28#include <fficonfig.h>
29#include <ffi.h>
30
31.text
32	.globl ffi_call_SYSV
33	.type ffi_call_SYSV, @function
34/*
35  r3: size to allocate on stack
36  r4: extended cif structure
37  r5: function pointer ffi_prep_args
38  r6: ret address
39  r7: function to call
40  r8: flag for return type
41*/
42
43ffi_call_SYSV:
44	/* Store registers used on stack */
45	l.sw -4(r1), r9 /* return address */
46	l.sw -8(r1), r1 /* stack address */
47	l.sw -12(r1), r14 /* callee saved registers */
48	l.sw -16(r1), r16
49	l.sw -20(r1), r18
50	l.sw -24(r1), r20
51
52	l.ori r14, r1, 0x0 /* save stack pointer */
53	l.addi r1, r1, -24
54
55	l.ori r16, r7, 0x0 /* save function address */
56	l.ori r18, r6, 0x0 /* save ret address */
57	l.ori r20, r8, 0x0 /* save flag */
58
59	l.sub r1, r1, r3 /* reserve space on stack */
60
61	/* Call ffi_prep_args */
62	l.ori r3, r1, 0x0  /* first argument stack address, second already ecif */
63	l.jalr r5
64	l.nop
65
66	/* Load register arguments and call*/
67
68	l.lwz r3, 0(r1)
69	l.lwz r4, 4(r1)
70	l.lwz r5, 8(r1)
71	l.lwz r6, 12(r1)
72	l.lwz r7, 16(r1)
73	l.lwz r8, 20(r1)
74	l.ori r1, r11, 0x0 /* new stack pointer */
75	l.jalr r16
76	l.nop
77
78	/* handle return values */
79
80	l.sfeqi r20, FFI_TYPE_STRUCT
81	l.bf ret  /* structs don't return an rvalue */
82	l.nop
83
84	/* copy ret address */
85
86	l.sfeqi r20, FFI_TYPE_UINT64
87	l.bnf four_byte_ret  /* 8 byte value is returned */
88	l.nop
89
90	l.sw 4(r18), r12
91
92four_byte_ret:
93	l.sw 0(r18), r11
94
95ret:
96	/* return */
97	l.ori r1, r14, 0x0 /* reset stack pointer */
98	l.lwz r9, -4(r1)
99	l.lwz r1, -8(r1)
100	l.lwz r14, -12(r1)
101	l.lwz r16, -16(r1)
102	l.lwz r18, -20(r1)
103	l.lwz r20, -24(r1)
104	l.jr r9
105	l.nop
106
107.size ffi_call_SYSV, .-ffi_call_SYSV
108