xref: /openbmc/linux/tools/include/nolibc/arch-s390.h (revision e5242c5f)
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3  * s390 specific definitions for NOLIBC
4  */
5 
6 #ifndef _NOLIBC_ARCH_S390_H
7 #define _NOLIBC_ARCH_S390_H
8 #include <asm/signal.h>
9 #include <asm/unistd.h>
10 
11 #include "compiler.h"
12 #include "crt.h"
13 #include "std.h"
14 
15 /* Syscalls for s390:
16  *   - registers are 64-bit
17  *   - syscall number is passed in r1
18  *   - arguments are in r2-r7
19  *   - the system call is performed by calling the svc instruction
20  *   - syscall return value is in r2
21  *   - r1 and r2 are clobbered, others are preserved.
22  *
23  * Link s390 ABI: https://github.com/IBM/s390x-abi
24  *
25  */
26 
27 #define my_syscall0(num)						\
28 ({									\
29 	register long _num __asm__ ("1") = (num);			\
30 	register long _rc __asm__ ("2");				\
31 									\
32 	__asm__ volatile (						\
33 		"svc 0\n"						\
34 		: "=d"(_rc)						\
35 		: "d"(_num)						\
36 		: "memory", "cc"					\
37 		);							\
38 	_rc;								\
39 })
40 
41 #define my_syscall1(num, arg1)						\
42 ({									\
43 	register long _num __asm__ ("1") = (num);			\
44 	register long _arg1 __asm__ ("2") = (long)(arg1);		\
45 									\
46 	__asm__ volatile (						\
47 		"svc 0\n"						\
48 		: "+d"(_arg1)						\
49 		: "d"(_num)						\
50 		: "memory", "cc"					\
51 		);							\
52 	_arg1;								\
53 })
54 
55 #define my_syscall2(num, arg1, arg2)					\
56 ({									\
57 	register long _num __asm__ ("1") = (num);			\
58 	register long _arg1 __asm__ ("2") = (long)(arg1);		\
59 	register long _arg2 __asm__ ("3") = (long)(arg2);		\
60 									\
61 	__asm__ volatile (						\
62 		"svc 0\n"						\
63 		: "+d"(_arg1)						\
64 		: "d"(_arg2), "d"(_num)					\
65 		: "memory", "cc"					\
66 		);							\
67 	_arg1;								\
68 })
69 
70 #define my_syscall3(num, arg1, arg2, arg3)				\
71 ({									\
72 	register long _num __asm__ ("1") = (num);			\
73 	register long _arg1 __asm__ ("2") = (long)(arg1);		\
74 	register long _arg2 __asm__ ("3") = (long)(arg2);		\
75 	register long _arg3 __asm__ ("4") = (long)(arg3);		\
76 									\
77 	__asm__ volatile (						\
78 		"svc 0\n"						\
79 		: "+d"(_arg1)						\
80 		: "d"(_arg2), "d"(_arg3), "d"(_num)			\
81 		: "memory", "cc"					\
82 		);							\
83 	_arg1;								\
84 })
85 
86 #define my_syscall4(num, arg1, arg2, arg3, arg4)			\
87 ({									\
88 	register long _num __asm__ ("1") = (num);			\
89 	register long _arg1 __asm__ ("2") = (long)(arg1);		\
90 	register long _arg2 __asm__ ("3") = (long)(arg2);		\
91 	register long _arg3 __asm__ ("4") = (long)(arg3);		\
92 	register long _arg4 __asm__ ("5") = (long)(arg4);		\
93 									\
94 	__asm__ volatile (						\
95 		"svc 0\n"						\
96 		: "+d"(_arg1)						\
97 		: "d"(_arg2), "d"(_arg3), "d"(_arg4), "d"(_num)		\
98 		: "memory", "cc"					\
99 		);							\
100 	_arg1;								\
101 })
102 
103 #define my_syscall5(num, arg1, arg2, arg3, arg4, arg5)			\
104 ({									\
105 	register long _num __asm__ ("1") = (num);			\
106 	register long _arg1 __asm__ ("2") = (long)(arg1);		\
107 	register long _arg2 __asm__ ("3") = (long)(arg2);		\
108 	register long _arg3 __asm__ ("4") = (long)(arg3);		\
109 	register long _arg4 __asm__ ("5") = (long)(arg4);		\
110 	register long _arg5 __asm__ ("6") = (long)(arg5);		\
111 									\
112 	__asm__ volatile (						\
113 		"svc 0\n"						\
114 		: "+d"(_arg1)						\
115 		: "d"(_arg2), "d"(_arg3), "d"(_arg4), "d"(_arg5),	\
116 		  "d"(_num)						\
117 		: "memory", "cc"					\
118 		);							\
119 	_arg1;								\
120 })
121 
122 #define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6)		\
123 ({									\
124 	register long _num __asm__ ("1") = (num);			\
125 	register long _arg1 __asm__ ("2") = (long)(arg1);		\
126 	register long _arg2 __asm__ ("3") = (long)(arg2);		\
127 	register long _arg3 __asm__ ("4") = (long)(arg3);		\
128 	register long _arg4 __asm__ ("5") = (long)(arg4);		\
129 	register long _arg5 __asm__ ("6") = (long)(arg5);		\
130 	register long _arg6 __asm__ ("7") = (long)(arg6);		\
131 									\
132 	__asm__ volatile (						\
133 		"svc 0\n"						\
134 		: "+d"(_arg1)						\
135 		: "d"(_arg2), "d"(_arg3), "d"(_arg4), "d"(_arg5),	\
136 		  "d"(_arg6), "d"(_num)					\
137 		: "memory", "cc"					\
138 		);							\
139 	_arg1;								\
140 })
141 
142 /* startup code */
143 void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void)
144 {
145 	__asm__ volatile (
146 		"lgr	%r2, %r15\n"          /* save stack pointer to %r2, as arg1 of _start_c */
147 		"aghi	%r15, -160\n"         /* allocate new stackframe                        */
148 		"xc	0(8,%r15), 0(%r15)\n" /* clear backchain                                */
149 		"brasl	%r14, _start_c\n"     /* transfer to c runtime                          */
150 	);
151 	__builtin_unreachable();
152 }
153 
154 struct s390_mmap_arg_struct {
155 	unsigned long addr;
156 	unsigned long len;
157 	unsigned long prot;
158 	unsigned long flags;
159 	unsigned long fd;
160 	unsigned long offset;
161 };
162 
163 static __attribute__((unused))
164 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
165 	       off_t offset)
166 {
167 	struct s390_mmap_arg_struct args = {
168 		.addr = (unsigned long)addr,
169 		.len = (unsigned long)length,
170 		.prot = prot,
171 		.flags = flags,
172 		.fd = fd,
173 		.offset = (unsigned long)offset
174 	};
175 
176 	return (void *)my_syscall1(__NR_mmap, &args);
177 }
178 #define sys_mmap sys_mmap
179 
180 static __attribute__((unused))
181 pid_t sys_fork(void)
182 {
183 	return my_syscall5(__NR_clone, 0, SIGCHLD, 0, 0, 0);
184 }
185 #define sys_fork sys_fork
186 
187 #endif /* _NOLIBC_ARCH_S390_H */
188