blob: 11ef7b7c9cc897940ec445498f338a31a3844d28 [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>
Dominik Brodowski3e2052e2018-03-22 14:09:17 +010050#include <asm/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{
Dominik Brodowskidf260e22018-03-19 17:32:11 +010058 return ksys_truncate(filename,
59 ((loff_t) offset_high << 32) | offset_low);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +010062COMPAT_SYSCALL_DEFINE3(x86_ftruncate64, unsigned int, fd,
63 unsigned long, offset_low, unsigned long, offset_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Dominik Brodowski411d9472018-03-11 11:34:54 +010065 return ksys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Thomas Gleixnerc202f292008-01-30 13:30:08 +010068/*
69 * Another set for IA32/LFS -- x86_64 struct stat is different due to
70 * support for 64bit inode numbers.
71 */
72static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 typeof(ubuf->st_uid) uid = 0;
75 typeof(ubuf->st_gid) gid = 0;
Eric W. Biedermana7c19382012-02-09 09:10:30 -080076 SET_UID(uid, from_kuid_munged(current_user_ns(), stat->uid));
77 SET_GID(gid, from_kgid_munged(current_user_ns(), stat->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
79 __put_user(huge_encode_dev(stat->dev), &ubuf->st_dev) ||
Thomas Gleixnerc202f292008-01-30 13:30:08 +010080 __put_user(stat->ino, &ubuf->__st_ino) ||
81 __put_user(stat->ino, &ubuf->st_ino) ||
82 __put_user(stat->mode, &ubuf->st_mode) ||
83 __put_user(stat->nlink, &ubuf->st_nlink) ||
84 __put_user(uid, &ubuf->st_uid) ||
85 __put_user(gid, &ubuf->st_gid) ||
86 __put_user(huge_encode_dev(stat->rdev), &ubuf->st_rdev) ||
87 __put_user(stat->size, &ubuf->st_size) ||
88 __put_user(stat->atime.tv_sec, &ubuf->st_atime) ||
89 __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec) ||
90 __put_user(stat->mtime.tv_sec, &ubuf->st_mtime) ||
91 __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec) ||
92 __put_user(stat->ctime.tv_sec, &ubuf->st_ctime) ||
93 __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec) ||
94 __put_user(stat->blksize, &ubuf->st_blksize) ||
95 __put_user(stat->blocks, &ubuf->st_blocks))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return -EFAULT;
97 return 0;
98}
99
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100100COMPAT_SYSCALL_DEFINE2(x86_stat64, const char __user *, filename,
101 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct kstat stat;
104 int ret = vfs_stat(filename, &stat);
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!ret)
107 ret = cp_stat64(statbuf, &stat);
108 return ret;
109}
110
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100111COMPAT_SYSCALL_DEFINE2(x86_lstat64, const char __user *, filename,
112 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct kstat stat;
115 int ret = vfs_lstat(filename, &stat);
116 if (!ret)
117 ret = cp_stat64(statbuf, &stat);
118 return ret;
119}
120
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100121COMPAT_SYSCALL_DEFINE2(x86_fstat64, unsigned int, fd,
122 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct kstat stat;
125 int ret = vfs_fstat(fd, &stat);
126 if (!ret)
127 ret = cp_stat64(statbuf, &stat);
128 return ret;
129}
130
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100131COMPAT_SYSCALL_DEFINE4(x86_fstatat, unsigned int, dfd,
132 const char __user *, filename,
133 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800134{
135 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400136 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800137
Oleg Drokin0112fc22009-04-08 20:05:42 +0400138 error = vfs_fstatat(dfd, filename, &stat, flag);
139 if (error)
140 return error;
141 return cp_stat64(statbuf, &stat);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * Linux/i386 didn't use to be able to handle more than
146 * 4 system call parameters, so these system calls used a memory
147 * block for parameter passing..
148 */
149
Christoph Hellwiga4679372010-03-10 15:21:15 -0800150struct mmap_arg_struct32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned int addr;
152 unsigned int len;
153 unsigned int prot;
154 unsigned int flags;
155 unsigned int fd;
156 unsigned int offset;
157};
158
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100159COMPAT_SYSCALL_DEFINE1(x86_mmap, struct mmap_arg_struct32 __user *, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Christoph Hellwiga4679372010-03-10 15:21:15 -0800161 struct mmap_arg_struct32 a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (copy_from_user(&a, arg, sizeof(a)))
164 return -EFAULT;
165
166 if (a.offset & ~PAGE_MASK)
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100167 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Dominik Brodowskia90f5902018-03-11 11:34:46 +0100169 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100170 a.offset>>PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100173/* warning: next two assume little endian */
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100174COMPAT_SYSCALL_DEFINE5(x86_pread, unsigned int, fd, char __user *, ubuf,
175 u32, count, u32, poslo, u32, poshi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Dominik Brodowski36028d52018-03-19 17:38:31 +0100177 return ksys_pread64(fd, ubuf, count,
178 ((loff_t)AA(poshi) << 32) | AA(poslo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100181COMPAT_SYSCALL_DEFINE5(x86_pwrite, unsigned int, fd, const char __user *, ubuf,
182 u32, count, u32, poslo, u32, poshi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Dominik Brodowski36028d52018-03-19 17:38:31 +0100184 return ksys_pwrite64(fd, ubuf, count,
185 ((loff_t)AA(poshi) << 32) | AA(poslo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100190 * Some system calls that need sign extended arguments. This could be
191 * done by a generic wrapper.
192 */
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100193COMPAT_SYSCALL_DEFINE6(x86_fadvise64_64, int, fd, __u32, offset_low,
194 __u32, offset_high, __u32, len_low, __u32, len_high,
195 int, advice)
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100196{
Dominik Brodowski9d5b7c92018-03-11 11:34:45 +0100197 return ksys_fadvise64_64(fd,
198 (((u64)offset_high)<<32) | offset_low,
199 (((u64)len_high)<<32) | len_low,
200 advice);
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100203COMPAT_SYSCALL_DEFINE4(x86_readahead, int, fd, unsigned int, off_lo,
204 unsigned int, off_hi, size_t, count)
Andi Kleene412ac42007-06-20 12:23:30 +0200205{
Dominik Brodowskic7b95d52018-03-19 17:51:36 +0100206 return ksys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
Andi Kleene412ac42007-06-20 12:23:30 +0200207}
208
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100209COMPAT_SYSCALL_DEFINE6(x86_sync_file_range, int, fd, unsigned int, off_low,
210 unsigned int, off_hi, unsigned int, n_low,
211 unsigned int, n_hi, int, flags)
Andi Kleene412ac42007-06-20 12:23:30 +0200212{
Dominik Brodowski806cbae2018-03-11 11:34:47 +0100213 return ksys_sync_file_range(fd,
214 ((u64)off_hi << 32) | off_low,
215 ((u64)n_hi << 32) | n_low, flags);
Andi Kleene412ac42007-06-20 12:23:30 +0200216}
217
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100218COMPAT_SYSCALL_DEFINE5(x86_fadvise64, int, fd, unsigned int, offset_lo,
219 unsigned int, offset_hi, size_t, len, int, advice)
Andi Kleene412ac42007-06-20 12:23:30 +0200220{
Dominik Brodowski9d5b7c92018-03-11 11:34:45 +0100221 return ksys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
222 len, advice);
Andi Kleene412ac42007-06-20 12:23:30 +0200223}
Amit Arora97ac7352007-07-17 21:42:44 -0400224
Dominik Brodowski4ddb45d2018-03-06 22:18:07 +0100225COMPAT_SYSCALL_DEFINE6(x86_fallocate, int, fd, int, mode,
226 unsigned int, offset_lo, unsigned int, offset_hi,
227 unsigned int, len_lo, unsigned int, len_hi)
Amit Arora97ac7352007-07-17 21:42:44 -0400228{
Dominik Brodowskiedf292c2018-03-19 17:46:32 +0100229 return ksys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
230 ((u64)len_hi << 32) | len_lo);
Amit Arora97ac7352007-07-17 21:42:44 -0400231}
Dominik Brodowskiaf522012018-03-06 22:18:08 +0100232
233/*
234 * The 32-bit clone ABI is CONFIG_CLONE_BACKWARDS
235 */
236COMPAT_SYSCALL_DEFINE5(x86_clone, unsigned long, clone_flags,
237 unsigned long, newsp, int __user *, parent_tidptr,
238 unsigned long, tls_val, int __user *, child_tidptr)
239{
Dominik Brodowski37db2192018-03-18 08:11:26 +0100240 return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr,
Dominik Brodowskiaf522012018-03-06 22:18:08 +0100241 tls_val);
242}