xref: /openbmc/linux/arch/s390/kernel/compat_linux.c (revision 2cf1c348)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  S390 version
4  *    Copyright IBM Corp. 2000
5  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
6  *               Gerhard Tonn (ton@de.ibm.com)
7  *               Thomas Spatzier (tspat@de.ibm.com)
8  *
9  *  Conversion between 31bit and 64bit native syscalls.
10  *
11  * Heavily inspired by the 32-bit Sparc compat code which is
12  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
13  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
14  *
15  */
16 
17 
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/fs.h>
21 #include <linux/mm.h>
22 #include <linux/file.h>
23 #include <linux/signal.h>
24 #include <linux/resource.h>
25 #include <linux/times.h>
26 #include <linux/smp.h>
27 #include <linux/sem.h>
28 #include <linux/msg.h>
29 #include <linux/shm.h>
30 #include <linux/uio.h>
31 #include <linux/quota.h>
32 #include <linux/poll.h>
33 #include <linux/personality.h>
34 #include <linux/stat.h>
35 #include <linux/filter.h>
36 #include <linux/highmem.h>
37 #include <linux/mman.h>
38 #include <linux/ipv6.h>
39 #include <linux/in.h>
40 #include <linux/icmpv6.h>
41 #include <linux/syscalls.h>
42 #include <linux/sysctl.h>
43 #include <linux/binfmts.h>
44 #include <linux/capability.h>
45 #include <linux/compat.h>
46 #include <linux/vfs.h>
47 #include <linux/ptrace.h>
48 #include <linux/fadvise.h>
49 #include <linux/ipc.h>
50 #include <linux/slab.h>
51 
52 #include <asm/types.h>
53 #include <linux/uaccess.h>
54 
55 #include <net/scm.h>
56 #include <net/sock.h>
57 
58 #include "compat_linux.h"
59 
60 #ifdef CONFIG_SYSVIPC
61 COMPAT_SYSCALL_DEFINE5(s390_ipc, uint, call, int, first, compat_ulong_t, second,
62 		compat_ulong_t, third, compat_uptr_t, ptr)
63 {
64 	if (call >> 16)		/* hack for backward compatibility */
65 		return -EINVAL;
66 	return compat_ksys_ipc(call, first, second, third, ptr, third);
67 }
68 #endif
69 
70 COMPAT_SYSCALL_DEFINE3(s390_truncate64, const char __user *, path, u32, high, u32, low)
71 {
72 	return ksys_truncate(path, (unsigned long)high << 32 | low);
73 }
74 
75 COMPAT_SYSCALL_DEFINE3(s390_ftruncate64, unsigned int, fd, u32, high, u32, low)
76 {
77 	return ksys_ftruncate(fd, (unsigned long)high << 32 | low);
78 }
79 
80 COMPAT_SYSCALL_DEFINE5(s390_pread64, unsigned int, fd, char __user *, ubuf,
81 		       compat_size_t, count, u32, high, u32, low)
82 {
83 	if ((compat_ssize_t) count < 0)
84 		return -EINVAL;
85 	return ksys_pread64(fd, ubuf, count, (unsigned long)high << 32 | low);
86 }
87 
88 COMPAT_SYSCALL_DEFINE5(s390_pwrite64, unsigned int, fd, const char __user *, ubuf,
89 		       compat_size_t, count, u32, high, u32, low)
90 {
91 	if ((compat_ssize_t) count < 0)
92 		return -EINVAL;
93 	return ksys_pwrite64(fd, ubuf, count, (unsigned long)high << 32 | low);
94 }
95 
96 COMPAT_SYSCALL_DEFINE4(s390_readahead, int, fd, u32, high, u32, low, s32, count)
97 {
98 	return ksys_readahead(fd, (unsigned long)high << 32 | low, count);
99 }
100 
101 struct stat64_emu31 {
102 	unsigned long long  st_dev;
103 	unsigned int    __pad1;
104 #define STAT64_HAS_BROKEN_ST_INO        1
105 	u32             __st_ino;
106 	unsigned int    st_mode;
107 	unsigned int    st_nlink;
108 	u32             st_uid;
109 	u32             st_gid;
110 	unsigned long long  st_rdev;
111 	unsigned int    __pad3;
112 	long            st_size;
113 	u32             st_blksize;
114 	unsigned char   __pad4[4];
115 	u32             __pad5;     /* future possible st_blocks high bits */
116 	u32             st_blocks;  /* Number 512-byte blocks allocated. */
117 	u32             st_atime;
118 	u32             __pad6;
119 	u32             st_mtime;
120 	u32             __pad7;
121 	u32             st_ctime;
122 	u32             __pad8;     /* will be high 32 bits of ctime someday */
123 	unsigned long   st_ino;
124 };
125 
126 static int cp_stat64(struct stat64_emu31 __user *ubuf, struct kstat *stat)
127 {
128 	struct stat64_emu31 tmp;
129 
130 	memset(&tmp, 0, sizeof(tmp));
131 
132 	tmp.st_dev = huge_encode_dev(stat->dev);
133 	tmp.st_ino = stat->ino;
134 	tmp.__st_ino = (u32)stat->ino;
135 	tmp.st_mode = stat->mode;
136 	tmp.st_nlink = (unsigned int)stat->nlink;
137 	tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
138 	tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
139 	tmp.st_rdev = huge_encode_dev(stat->rdev);
140 	tmp.st_size = stat->size;
141 	tmp.st_blksize = (u32)stat->blksize;
142 	tmp.st_blocks = (u32)stat->blocks;
143 	tmp.st_atime = (u32)stat->atime.tv_sec;
144 	tmp.st_mtime = (u32)stat->mtime.tv_sec;
145 	tmp.st_ctime = (u32)stat->ctime.tv_sec;
146 
147 	return copy_to_user(ubuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
148 }
149 
150 COMPAT_SYSCALL_DEFINE2(s390_stat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
151 {
152 	struct kstat stat;
153 	int ret = vfs_stat(filename, &stat);
154 	if (!ret)
155 		ret = cp_stat64(statbuf, &stat);
156 	return ret;
157 }
158 
159 COMPAT_SYSCALL_DEFINE2(s390_lstat64, const char __user *, filename, struct stat64_emu31 __user *, statbuf)
160 {
161 	struct kstat stat;
162 	int ret = vfs_lstat(filename, &stat);
163 	if (!ret)
164 		ret = cp_stat64(statbuf, &stat);
165 	return ret;
166 }
167 
168 COMPAT_SYSCALL_DEFINE2(s390_fstat64, unsigned int, fd, struct stat64_emu31 __user *, statbuf)
169 {
170 	struct kstat stat;
171 	int ret = vfs_fstat(fd, &stat);
172 	if (!ret)
173 		ret = cp_stat64(statbuf, &stat);
174 	return ret;
175 }
176 
177 COMPAT_SYSCALL_DEFINE4(s390_fstatat64, unsigned int, dfd, const char __user *, filename,
178 		       struct stat64_emu31 __user *, statbuf, int, flag)
179 {
180 	struct kstat stat;
181 	int error;
182 
183 	error = vfs_fstatat(dfd, filename, &stat, flag);
184 	if (error)
185 		return error;
186 	return cp_stat64(statbuf, &stat);
187 }
188 
189 /*
190  * Linux/i386 didn't use to be able to handle more than
191  * 4 system call parameters, so these system calls used a memory
192  * block for parameter passing..
193  */
194 
195 struct mmap_arg_struct_emu31 {
196 	compat_ulong_t addr;
197 	compat_ulong_t len;
198 	compat_ulong_t prot;
199 	compat_ulong_t flags;
200 	compat_ulong_t fd;
201 	compat_ulong_t offset;
202 };
203 
204 COMPAT_SYSCALL_DEFINE1(s390_old_mmap, struct mmap_arg_struct_emu31 __user *, arg)
205 {
206 	struct mmap_arg_struct_emu31 a;
207 
208 	if (copy_from_user(&a, arg, sizeof(a)))
209 		return -EFAULT;
210 	if (a.offset & ~PAGE_MASK)
211 		return -EINVAL;
212 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
213 			       a.offset >> PAGE_SHIFT);
214 }
215 
216 COMPAT_SYSCALL_DEFINE1(s390_mmap2, struct mmap_arg_struct_emu31 __user *, arg)
217 {
218 	struct mmap_arg_struct_emu31 a;
219 
220 	if (copy_from_user(&a, arg, sizeof(a)))
221 		return -EFAULT;
222 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
223 }
224 
225 COMPAT_SYSCALL_DEFINE3(s390_read, unsigned int, fd, char __user *, buf, compat_size_t, count)
226 {
227 	if ((compat_ssize_t) count < 0)
228 		return -EINVAL;
229 
230 	return ksys_read(fd, buf, count);
231 }
232 
233 COMPAT_SYSCALL_DEFINE3(s390_write, unsigned int, fd, const char __user *, buf, compat_size_t, count)
234 {
235 	if ((compat_ssize_t) count < 0)
236 		return -EINVAL;
237 
238 	return ksys_write(fd, buf, count);
239 }
240 
241 /*
242  * 31 bit emulation wrapper functions for sys_fadvise64/fadvise64_64.
243  * These need to rewrite the advise values for POSIX_FADV_{DONTNEED,NOREUSE}
244  * because the 31 bit values differ from the 64 bit values.
245  */
246 
247 COMPAT_SYSCALL_DEFINE5(s390_fadvise64, int, fd, u32, high, u32, low, compat_size_t, len, int, advise)
248 {
249 	if (advise == 4)
250 		advise = POSIX_FADV_DONTNEED;
251 	else if (advise == 5)
252 		advise = POSIX_FADV_NOREUSE;
253 	return ksys_fadvise64_64(fd, (unsigned long)high << 32 | low, len,
254 				 advise);
255 }
256 
257 struct fadvise64_64_args {
258 	int fd;
259 	long long offset;
260 	long long len;
261 	int advice;
262 };
263 
264 COMPAT_SYSCALL_DEFINE1(s390_fadvise64_64, struct fadvise64_64_args __user *, args)
265 {
266 	struct fadvise64_64_args a;
267 
268 	if ( copy_from_user(&a, args, sizeof(a)) )
269 		return -EFAULT;
270 	if (a.advice == 4)
271 		a.advice = POSIX_FADV_DONTNEED;
272 	else if (a.advice == 5)
273 		a.advice = POSIX_FADV_NOREUSE;
274 	return ksys_fadvise64_64(a.fd, a.offset, a.len, a.advice);
275 }
276 
277 COMPAT_SYSCALL_DEFINE6(s390_sync_file_range, int, fd, u32, offhigh, u32, offlow,
278 		       u32, nhigh, u32, nlow, unsigned int, flags)
279 {
280 	return ksys_sync_file_range(fd, ((loff_t)offhigh << 32) + offlow,
281 				   ((u64)nhigh << 32) + nlow, flags);
282 }
283 
284 COMPAT_SYSCALL_DEFINE6(s390_fallocate, int, fd, int, mode, u32, offhigh, u32, offlow,
285 		       u32, lenhigh, u32, lenlow)
286 {
287 	return ksys_fallocate(fd, mode, ((loff_t)offhigh << 32) + offlow,
288 			      ((u64)lenhigh << 32) + lenlow);
289 }
290