blob: 96cd33bbfc85494f52e4131f50b37129393d3b8a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/mman.h>
45#include <asm/types.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070047#include <linux/atomic.h>
Arnaldo Carvalho de Melo28d23122008-02-07 21:03:04 -020048#include <asm/vgtod.h>
Jaswinder Singh Rajput2f06de02008-12-27 21:37:10 +053049#include <asm/sys_ia32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define AA(__x) ((unsigned long)(__x))
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David Howellsc7887322010-08-11 11:26:22 +010054asmlinkage long sys32_truncate64(const char __user *filename,
Thomas Gleixnerc202f292008-01-30 13:30:08 +010055 unsigned long offset_low,
56 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
Thomas Gleixnerc202f292008-01-30 13:30:08 +010061asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long offset_low,
62 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
David Howellsc7887322010-08-11 11:26:22 +010099asmlinkage long sys32_stat64(const char __user *filename,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100100 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
David Howellsc7887322010-08-11 11:26:22 +0100110asmlinkage long sys32_lstat64(const char __user *filename,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100111 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
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100120asmlinkage long sys32_fstat64(unsigned int fd, struct stat64 __user *statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
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
David Howellsc7887322010-08-11 11:26:22 +0100129asmlinkage long sys32_fstatat(unsigned int dfd, const char __user *filename,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100130 struct stat64 __user *statbuf, int flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800131{
132 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400133 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800134
Oleg Drokin0112fc22009-04-08 20:05:42 +0400135 error = vfs_fstatat(dfd, filename, &stat, flag);
136 if (error)
137 return error;
138 return cp_stat64(statbuf, &stat);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
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
Christoph Hellwiga4679372010-03-10 15:21:15 -0800147struct mmap_arg_struct32 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 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
Christoph Hellwiga4679372010-03-10 15:21:15 -0800156asmlinkage long sys32_mmap(struct mmap_arg_struct32 __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Christoph Hellwiga4679372010-03-10 15:21:15 -0800158 struct mmap_arg_struct32 a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (copy_from_user(&a, arg, sizeof(a)))
161 return -EFAULT;
162
163 if (a.offset & ~PAGE_MASK)
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Al Virof8b72562009-11-30 17:37:04 -0500166 return sys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100167 a.offset>>PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Mathias Krausef0002622012-09-02 23:31:40 +0200170asmlinkage long sys32_waitpid(compat_pid_t pid, unsigned int __user *stat_addr,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100171 int options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 return compat_sys_wait4(pid, stat_addr, options, NULL);
174}
175
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100176/* warning: next two assume little endian */
177asmlinkage long sys32_pread(unsigned int fd, char __user *ubuf, u32 count,
178 u32 poslo, u32 poshi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 return sys_pread64(fd, ubuf, count,
181 ((loff_t)AA(poshi) << 32) | AA(poslo));
182}
183
David Howellsc7887322010-08-11 11:26:22 +0100184asmlinkage long sys32_pwrite(unsigned int fd, const char __user *ubuf,
185 u32 count, u32 poslo, u32 poshi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 return sys_pwrite64(fd, ubuf, count,
188 ((loff_t)AA(poshi) << 32) | AA(poslo));
189}
190
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100193 * Some system calls that need sign extended arguments. This could be
194 * done by a generic wrapper.
195 */
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100196long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 __u32 len_low, __u32 len_high, int advice)
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return sys_fadvise64_64(fd,
200 (((u64)offset_high)<<32) | offset_low,
201 (((u64)len_high)<<32) | len_low,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100202 advice);
203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100205asmlinkage ssize_t sys32_readahead(int fd, unsigned off_lo, unsigned off_hi,
206 size_t count)
Andi Kleene412ac42007-06-20 12:23:30 +0200207{
208 return sys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
209}
210
211asmlinkage long sys32_sync_file_range(int fd, unsigned off_low, unsigned off_hi,
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100212 unsigned n_low, unsigned n_hi, int flags)
Andi Kleene412ac42007-06-20 12:23:30 +0200213{
214 return sys_sync_file_range(fd,
215 ((u64)off_hi << 32) | off_low,
216 ((u64)n_hi << 32) | n_low, flags);
217}
218
Thomas Gleixnerc202f292008-01-30 13:30:08 +0100219asmlinkage long sys32_fadvise64(int fd, unsigned offset_lo, unsigned offset_hi,
220 size_t len, int advice)
Andi Kleene412ac42007-06-20 12:23:30 +0200221{
222 return sys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
223 len, advice);
224}
Amit Arora97ac7352007-07-17 21:42:44 -0400225
226asmlinkage 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}