GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / x86 / ia32 / sys_ia32.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
4  *             sys_sparc32
5  *
6  * Copyright (C) 2000           VA Linux Co
7  * Copyright (C) 2000           Don Dugger <n0ano@valinux.com>
8  * Copyright (C) 1999           Arun Sharma <arun.sharma@intel.com>
9  * Copyright (C) 1997,1998      Jakub Jelinek (jj@sunsite.mff.cuni.cz)
10  * Copyright (C) 1997           David S. Miller (davem@caip.rutgers.edu)
11  * Copyright (C) 2000           Hewlett-Packard Co.
12  * Copyright (C) 2000           David Mosberger-Tang <davidm@hpl.hp.com>
13  * Copyright (C) 2000,2001,2002 Andi Kleen, SuSE Labs (x86-64 port)
14  *
15  * These routines maintain argument size conversion between 32bit and 64bit
16  * environment. In 2.5 most of this should be moved to a generic directory.
17  *
18  * This file assumes that there is a hole at the end of user address space.
19  *
20  * Some of the functions are LE specific currently. These are
21  * hopefully all marked.  This should be fixed.
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/fs.h>
27 #include <linux/file.h>
28 #include <linux/signal.h>
29 #include <linux/syscalls.h>
30 #include <linux/times.h>
31 #include <linux/utsname.h>
32 #include <linux/mm.h>
33 #include <linux/uio.h>
34 #include <linux/poll.h>
35 #include <linux/personality.h>
36 #include <linux/stat.h>
37 #include <linux/rwsem.h>
38 #include <linux/compat.h>
39 #include <linux/vfs.h>
40 #include <linux/ptrace.h>
41 #include <linux/highuid.h>
42 #include <linux/sysctl.h>
43 #include <linux/slab.h>
44 #include <asm/mman.h>
45 #include <asm/types.h>
46 #include <linux/uaccess.h>
47 #include <linux/atomic.h>
48 #include <asm/vgtod.h>
49 #include <asm/sys_ia32.h>
50
51 #define AA(__x)         ((unsigned long)(__x))
52
53
54 asmlinkage long sys32_truncate64(const char __user *filename,
55                                  unsigned long offset_low,
56                                  unsigned long offset_high)
57 {
58        return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
59 }
60
61 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long offset_low,
62                                   unsigned long offset_high)
63 {
64        return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
65 }
66
67 /*
68  * Another set for IA32/LFS -- x86_64 struct stat is different due to
69  * support for 64bit inode numbers.
70  */
71 static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
72 {
73         typeof(ubuf->st_uid) uid = 0;
74         typeof(ubuf->st_gid) gid = 0;
75         SET_UID(uid, from_kuid_munged(current_user_ns(), stat->uid));
76         SET_GID(gid, from_kgid_munged(current_user_ns(), stat->gid));
77         if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
78             __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
79             __put_user(stat->ino, &ubuf->__st_ino) ||
80             __put_user(stat->ino, &ubuf->st_ino) ||
81             __put_user(stat->mode, &ubuf->st_mode) ||
82             __put_user(stat->nlink, &ubuf->st_nlink) ||
83             __put_user(uid, &ubuf->st_uid) ||
84             __put_user(gid, &ubuf->st_gid) ||
85             __put_user(huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
86             __put_user(stat->size, &ubuf->st_size) ||
87             __put_user(stat->atime.tv_sec, &ubuf->st_atime) ||
88             __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
89             __put_user(stat->mtime.tv_sec, &ubuf->st_mtime) ||
90             __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
91             __put_user(stat->ctime.tv_sec, &ubuf->st_ctime) ||
92             __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
93             __put_user(stat->blksize, &ubuf->st_blksize) ||
94             __put_user(stat->blocks, &ubuf->st_blocks))
95                 return -EFAULT;
96         return 0;
97 }
98
99 asmlinkage long sys32_stat64(const char __user *filename,
100                              struct stat64 __user *statbuf)
101 {
102         struct kstat stat;
103         int ret = vfs_stat(filename, &stat);
104
105         if (!ret)
106                 ret = cp_stat64(statbuf, &stat);
107         return ret;
108 }
109
110 asmlinkage long sys32_lstat64(const char __user *filename,
111                               struct stat64 __user *statbuf)
112 {
113         struct kstat stat;
114         int ret = vfs_lstat(filename, &stat);
115         if (!ret)
116                 ret = cp_stat64(statbuf, &stat);
117         return ret;
118 }
119
120 asmlinkage long sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
121 {
122         struct kstat stat;
123         int ret = vfs_fstat(fd, &stat);
124         if (!ret)
125                 ret = cp_stat64(statbuf, &stat);
126         return ret;
127 }
128
129 asmlinkage long sys32_fstatat(unsigned int dfd, const char __user *filename,
130                               struct stat64 __user *statbuf, int flag)
131 {
132         struct kstat stat;
133         int error;
134
135         error = vfs_fstatat(dfd, filename, &stat, flag);
136         if (error)
137                 return error;
138         return cp_stat64(statbuf, &stat);
139 }
140
141 /*
142  * Linux/i386 didn't use to be able to handle more than
143  * 4 system call parameters, so these system calls used a memory
144  * block for parameter passing..
145  */
146
147 struct mmap_arg_struct32 {
148         unsigned int addr;
149         unsigned int len;
150         unsigned int prot;
151         unsigned int flags;
152         unsigned int fd;
153         unsigned int offset;
154 };
155
156 asmlinkage long sys32_mmap(struct mmap_arg_struct32 __user *arg)
157 {
158         struct mmap_arg_struct32 a;
159
160         if (copy_from_user(&a, arg, sizeof(a)))
161                 return -EFAULT;
162
163         if (a.offset & ~PAGE_MASK)
164                 return -EINVAL;
165
166         return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
167                                a.offset>>PAGE_SHIFT);
168 }
169
170 asmlinkage long sys32_waitpid(compat_pid_t pid, unsigned int __user *stat_addr,
171                               int options)
172 {
173         return compat_sys_wait4(pid, stat_addr, options, NULL);
174 }
175
176 /* warning: next two assume little endian */
177 asmlinkage long sys32_pread(unsigned int fd, char __user *ubuf, u32 count,
178                             u32 poslo, u32 poshi)
179 {
180         return sys_pread64(fd, ubuf, count,
181                          ((loff_t)AA(poshi) << 32) | AA(poslo));
182 }
183
184 asmlinkage long sys32_pwrite(unsigned int fd, const char __user *ubuf,
185                              u32 count, u32 poslo, u32 poshi)
186 {
187         return sys_pwrite64(fd, ubuf, count,
188                           ((loff_t)AA(poshi) << 32) | AA(poslo));
189 }
190
191
192 /*
193  * Some system calls that need sign extended arguments. This could be
194  * done by a generic wrapper.
195  */
196 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
197                         __u32 len_low, __u32 len_high, int advice)
198 {
199         return sys_fadvise64_64(fd,
200                                (((u64)offset_high)<<32) | offset_low,
201                                (((u64)len_high)<<32) | len_low,
202                                 advice);
203 }
204
205 asmlinkage ssize_t sys32_readahead(int fd, unsigned off_lo, unsigned off_hi,
206                                    size_t count)
207 {
208         return sys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
209 }
210
211 asmlinkage long sys32_sync_file_range(int fd, unsigned off_low, unsigned off_hi,
212                                       unsigned n_low, unsigned n_hi,  int flags)
213 {
214         return sys_sync_file_range(fd,
215                                    ((u64)off_hi << 32) | off_low,
216                                    ((u64)n_hi << 32) | n_low, flags);
217 }
218
219 asmlinkage long sys32_fadvise64(int fd, unsigned offset_lo, unsigned offset_hi,
220                                 size_t len, int advice)
221 {
222         return sys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
223                                 len, advice);
224 }
225
226 asmlinkage long sys32_fallocate(int fd, int mode, unsigned offset_lo,
227                                 unsigned offset_hi, unsigned len_lo,
228                                 unsigned len_hi)
229 {
230         return sys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
231                              ((u64)len_hi << 32) | len_lo);
232 }