blob: 23a5260eae677be4787f94059b290b6ca6b50844 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
Thomas Gleixnerc202f292008-01-30 13:30:08 +01004 * sys_sparc32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Copyright (C) 2000 VA Linux Co
7 * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
Thomas Gleixnerc202f292008-01-30 13:30:08 +01008 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2000 Hewlett-Packard Co.
12 * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
Thomas Gleixnerc202f292008-01-30 13:30:08 +010013 * Copyright (C) 2000,2001,2002 Andi Kleen, SuSE Labs (x86-64 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * These routines maintain argument size conversion between 32bit and 64bit
Thomas Gleixnerc202f292008-01-30 13:30:08 +010016 * environment. In 2.5 most of this should be moved to a generic directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * This file assumes that there is a hole at the end of user address space.
Thomas Gleixnerc202f292008-01-30 13:30:08 +010019 *
20 * Some of the functions are LE specific currently. These are
21 * hopefully all marked. This should be fixed.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/sched.h>
Thomas Gleixnerc202f292008-01-30 13:30:08 +010026#include <linux/fs.h>
27#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/signal.h>
29#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/times.h>
31#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/poll.h>
35#include <linux/personality.h>
36#include <linux/stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/compat.h>
39#include <linux/vfs.h>
40#include <linux/ptrace.h>
41#include <linux/highuid.h>
Andi Kleenddb15ec2006-09-26 10:52:33 +020042#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Dominik Brodowski37db2192018-03-18 08:11:26 +010044#include <linux/sched/task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/mman.h>
46#include <asm/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080047#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070048#include <linux/atomic.h>
Arnaldo Carvalho de Melo28d23122008-02-07 21:03:04 -020049#include <asm/vgtod.h>
Jaswinder Singh Rajput2f06de02008-12-27 21:37:10 +053050#include <asm/sys_ia32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#define AA(__x) ((unsigned long)(__x))
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +010055COMPAT_SYSCALL_DEFINE3(x86_truncate64, const char __user *, filename,
56 unsigned long, offset_low, unsigned long, offset_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
59}
60
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +010061COMPAT_SYSCALL_DEFINE3(x86_ftruncate64, unsigned int, fd,
62 unsigned long, offset_low, unsigned long, offset_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
65}
66
Thomas Gleixnerc202f292008-01-30 13:30:08 +010067/*
68 * Another set for IA32/LFS -- x86_64 struct stat is different due to
69 * support for 64bit inode numbers.
70 */
71static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 typeof(ubuf->st_uid) uid = 0;
74 typeof(ubuf->st_gid) gid = 0;
Eric W. Biedermana7c19382012-02-09 09:10:30 -080075 SET_UID(uid, from_kuid_munged(current_user_ns(), stat->uid));
76 SET_GID(gid, from_kgid_munged(current_user_ns(), stat->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
78 __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
Thomas Gleixnerc202f292008-01-30 13:30:08 +010079 __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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -EFAULT;
96 return 0;
97}
98
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +010099COMPAT_SYSCALL_DEFINE2(x86_stat64, const char __user *, filename,
100 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct kstat stat;
103 int ret = vfs_stat(filename, &stat);
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!ret)
106 ret = cp_stat64(statbuf, &stat);
107 return ret;
108}
109
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100110COMPAT_SYSCALL_DEFINE2(x86_lstat64, const char __user *, filename,
111 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
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
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100120COMPAT_SYSCALL_DEFINE2(x86_fstat64, unsigned int, fd,
121 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct kstat stat;
124 int ret = vfs_fstat(fd, &stat);
125 if (!ret)
126 ret = cp_stat64(statbuf, &stat);
127 return ret;
128}
129
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100130COMPAT_SYSCALL_DEFINE4(x86_fstatat, unsigned int, dfd,
131 const char __user *, filename,
132 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800133{
134 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400135 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800136
Oleg Drokin0112fc22009-04-08 20:05:42 +0400137 error = vfs_fstatat(dfd, filename, &stat, flag);
138 if (error)
139 return error;
140 return cp_stat64(statbuf, &stat);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * Linux/i386 didn't use to be able to handle more than
145 * 4 system call parameters, so these system calls used a memory
146 * block for parameter passing..
147 */
148
Christoph Hellwiga4679372010-03-10 15:21:15 -0800149struct mmap_arg_struct32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned int addr;
151 unsigned int len;
152 unsigned int prot;
153 unsigned int flags;
154 unsigned int fd;
155 unsigned int offset;
156};
157
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100158COMPAT_SYSCALL_DEFINE1(x86_mmap, struct mmap_arg_struct32 __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Christoph Hellwiga4679372010-03-10 15:21:15 -0800160 struct mmap_arg_struct32 a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (copy_from_user(&a, arg, sizeof(a)))
163 return -EFAULT;
164
165 if (a.offset & ~PAGE_MASK)
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100166 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Al Virof8b72562009-11-30 17:37:04 -0500168 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100169 a.offset>>PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100172/* warning: next two assume little endian */
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100173COMPAT_SYSCALL_DEFINE5(x86_pread, unsigned int, fd, char __user *, ubuf,
174 u32, count, u32, poslo, u32, poshi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 return sys_pread64(fd, ubuf, count,
177 ((loff_t)AA(poshi) << 32) | AA(poslo));
178}
179
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100180COMPAT_SYSCALL_DEFINE5(x86_pwrite, unsigned int, fd, const char __user *, ubuf,
181 u32, count, u32, poslo, u32, poshi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 return sys_pwrite64(fd, ubuf, count,
184 ((loff_t)AA(poshi) << 32) | AA(poslo));
185}
186
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100189 * Some system calls that need sign extended arguments. This could be
190 * done by a generic wrapper.
191 */
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100192COMPAT_SYSCALL_DEFINE6(x86_fadvise64_64, int, fd, __u32, offset_low,
193 __u32, offset_high, __u32, len_low, __u32, len_high,
194 int, advice)
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return sys_fadvise64_64(fd,
197 (((u64)offset_high)<<32) | offset_low,
198 (((u64)len_high)<<32) | len_low,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100199 advice);
200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100202COMPAT_SYSCALL_DEFINE4(x86_readahead, int, fd, unsigned int, off_lo,
203 unsigned int, off_hi, size_t, count)
Andi Kleene412ac42007-06-20 12:23:30 +0200204{
205 return sys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
206}
207
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100208COMPAT_SYSCALL_DEFINE6(x86_sync_file_range, int, fd, unsigned int, off_low,
209 unsigned int, off_hi, unsigned int, n_low,
210 unsigned int, n_hi, int, flags)
Andi Kleene412ac42007-06-20 12:23:30 +0200211{
212 return sys_sync_file_range(fd,
213 ((u64)off_hi << 32) | off_low,
214 ((u64)n_hi << 32) | n_low, flags);
215}
216
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100217COMPAT_SYSCALL_DEFINE5(x86_fadvise64, int, fd, unsigned int, offset_lo,
218 unsigned int, offset_hi, size_t, len, int, advice)
Andi Kleene412ac42007-06-20 12:23:30 +0200219{
220 return sys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
221 len, advice);
222}
Amit Arora97ac7352007-07-17 21:42:44 -0400223
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100224COMPAT_SYSCALL_DEFINE6(x86_fallocate, int, fd, int, mode,
225 unsigned int, offset_lo, unsigned int, offset_hi,
226 unsigned int, len_lo, unsigned int, len_hi)
Amit Arora97ac7352007-07-17 21:42:44 -0400227{
228 return sys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
229 ((u64)len_hi << 32) | len_lo);
230}
Dominik Brodowskiaf522012018-03-06 22:18:08 +0100231
232/*
233 * The 32-bit clone ABI is CONFIG_CLONE_BACKWARDS
234 */
235COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
236 unsigned long, newsp, int __user *, parent_tidptr,
237 unsigned long, tls_val, int __user *, child_tidptr)
238{
Dominik Brodowski37db2192018-03-18 08:11:26 +0100239 return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr,
Dominik Brodowskiaf522012018-03-06 22:18:08 +0100240 tls_val);
241}