xref: /linux/arch/x86/kernel/sys_ia32.c (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
1121b32a5SBrian Gerst // SPDX-License-Identifier: GPL-2.0
2121b32a5SBrian Gerst /*
3121b32a5SBrian Gerst  * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
4121b32a5SBrian Gerst  *             sys_sparc32
5121b32a5SBrian Gerst  *
6121b32a5SBrian Gerst  * Copyright (C) 2000		VA Linux Co
7121b32a5SBrian Gerst  * Copyright (C) 2000		Don Dugger <n0ano@valinux.com>
8121b32a5SBrian Gerst  * Copyright (C) 1999		Arun Sharma <arun.sharma@intel.com>
9121b32a5SBrian Gerst  * Copyright (C) 1997,1998	Jakub Jelinek (jj@sunsite.mff.cuni.cz)
10121b32a5SBrian Gerst  * Copyright (C) 1997		David S. Miller (davem@caip.rutgers.edu)
11121b32a5SBrian Gerst  * Copyright (C) 2000		Hewlett-Packard Co.
12121b32a5SBrian Gerst  * Copyright (C) 2000		David Mosberger-Tang <davidm@hpl.hp.com>
13121b32a5SBrian Gerst  * Copyright (C) 2000,2001,2002	Andi Kleen, SuSE Labs (x86-64 port)
14121b32a5SBrian Gerst  *
15121b32a5SBrian Gerst  * These routines maintain argument size conversion between 32bit and 64bit
16121b32a5SBrian Gerst  * environment. In 2.5 most of this should be moved to a generic directory.
17121b32a5SBrian Gerst  *
18121b32a5SBrian Gerst  * This file assumes that there is a hole at the end of user address space.
19121b32a5SBrian Gerst  *
20121b32a5SBrian Gerst  * Some of the functions are LE specific currently. These are
21121b32a5SBrian Gerst  * hopefully all marked.  This should be fixed.
22121b32a5SBrian Gerst  */
23121b32a5SBrian Gerst 
24121b32a5SBrian Gerst #include <linux/kernel.h>
25121b32a5SBrian Gerst #include <linux/sched.h>
26121b32a5SBrian Gerst #include <linux/fs.h>
27121b32a5SBrian Gerst #include <linux/file.h>
28121b32a5SBrian Gerst #include <linux/signal.h>
29121b32a5SBrian Gerst #include <linux/syscalls.h>
30121b32a5SBrian Gerst #include <linux/times.h>
31121b32a5SBrian Gerst #include <linux/utsname.h>
32121b32a5SBrian Gerst #include <linux/mm.h>
33121b32a5SBrian Gerst #include <linux/uio.h>
34121b32a5SBrian Gerst #include <linux/poll.h>
35121b32a5SBrian Gerst #include <linux/personality.h>
36121b32a5SBrian Gerst #include <linux/stat.h>
37121b32a5SBrian Gerst #include <linux/rwsem.h>
38121b32a5SBrian Gerst #include <linux/compat.h>
39121b32a5SBrian Gerst #include <linux/vfs.h>
40121b32a5SBrian Gerst #include <linux/ptrace.h>
41121b32a5SBrian Gerst #include <linux/highuid.h>
42121b32a5SBrian Gerst #include <linux/sysctl.h>
43121b32a5SBrian Gerst #include <linux/slab.h>
44121b32a5SBrian Gerst #include <linux/sched/task.h>
45121b32a5SBrian Gerst #include <asm/mman.h>
46121b32a5SBrian Gerst #include <asm/types.h>
47121b32a5SBrian Gerst #include <linux/uaccess.h>
48121b32a5SBrian Gerst #include <linux/atomic.h>
49121b32a5SBrian Gerst #include <asm/vgtod.h>
50121b32a5SBrian Gerst #include <asm/ia32.h>
51121b32a5SBrian Gerst 
52121b32a5SBrian Gerst #define AA(__x)		((unsigned long)(__x))
53121b32a5SBrian Gerst 
SYSCALL_DEFINE3(ia32_truncate64,const char __user *,filename,unsigned long,offset_low,unsigned long,offset_high)54121b32a5SBrian Gerst SYSCALL_DEFINE3(ia32_truncate64, const char __user *, filename,
55121b32a5SBrian Gerst 		unsigned long, offset_low, unsigned long, offset_high)
56121b32a5SBrian Gerst {
57121b32a5SBrian Gerst 	return ksys_truncate(filename,
58121b32a5SBrian Gerst 			    ((loff_t) offset_high << 32) | offset_low);
59121b32a5SBrian Gerst }
60121b32a5SBrian Gerst 
SYSCALL_DEFINE3(ia32_ftruncate64,unsigned int,fd,unsigned long,offset_low,unsigned long,offset_high)61121b32a5SBrian Gerst SYSCALL_DEFINE3(ia32_ftruncate64, unsigned int, fd,
62121b32a5SBrian Gerst 		unsigned long, offset_low, unsigned long, offset_high)
63121b32a5SBrian Gerst {
64121b32a5SBrian Gerst 	return ksys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
65121b32a5SBrian Gerst }
66121b32a5SBrian Gerst 
67121b32a5SBrian Gerst /* warning: next two assume little endian */
SYSCALL_DEFINE5(ia32_pread64,unsigned int,fd,char __user *,ubuf,u32,count,u32,poslo,u32,poshi)68121b32a5SBrian Gerst SYSCALL_DEFINE5(ia32_pread64, unsigned int, fd, char __user *, ubuf,
69121b32a5SBrian Gerst 		u32, count, u32, poslo, u32, poshi)
70121b32a5SBrian Gerst {
71121b32a5SBrian Gerst 	return ksys_pread64(fd, ubuf, count,
72121b32a5SBrian Gerst 			    ((loff_t)AA(poshi) << 32) | AA(poslo));
73121b32a5SBrian Gerst }
74121b32a5SBrian Gerst 
SYSCALL_DEFINE5(ia32_pwrite64,unsigned int,fd,const char __user *,ubuf,u32,count,u32,poslo,u32,poshi)75121b32a5SBrian Gerst SYSCALL_DEFINE5(ia32_pwrite64, unsigned int, fd, const char __user *, ubuf,
76121b32a5SBrian Gerst 		u32, count, u32, poslo, u32, poshi)
77121b32a5SBrian Gerst {
78121b32a5SBrian Gerst 	return ksys_pwrite64(fd, ubuf, count,
79121b32a5SBrian Gerst 			     ((loff_t)AA(poshi) << 32) | AA(poslo));
80121b32a5SBrian Gerst }
81121b32a5SBrian Gerst 
82121b32a5SBrian Gerst 
83121b32a5SBrian Gerst /*
84121b32a5SBrian Gerst  * Some system calls that need sign extended arguments. This could be
85121b32a5SBrian Gerst  * done by a generic wrapper.
86121b32a5SBrian Gerst  */
SYSCALL_DEFINE6(ia32_fadvise64_64,int,fd,__u32,offset_low,__u32,offset_high,__u32,len_low,__u32,len_high,int,advice)87121b32a5SBrian Gerst SYSCALL_DEFINE6(ia32_fadvise64_64, int, fd, __u32, offset_low,
88121b32a5SBrian Gerst 		__u32, offset_high, __u32, len_low, __u32, len_high,
89121b32a5SBrian Gerst 		int, advice)
90121b32a5SBrian Gerst {
91121b32a5SBrian Gerst 	return ksys_fadvise64_64(fd,
92121b32a5SBrian Gerst 				 (((u64)offset_high)<<32) | offset_low,
93121b32a5SBrian Gerst 				 (((u64)len_high)<<32) | len_low,
94121b32a5SBrian Gerst 				 advice);
95121b32a5SBrian Gerst }
96121b32a5SBrian Gerst 
SYSCALL_DEFINE4(ia32_readahead,int,fd,unsigned int,off_lo,unsigned int,off_hi,size_t,count)97121b32a5SBrian Gerst SYSCALL_DEFINE4(ia32_readahead, int, fd, unsigned int, off_lo,
98121b32a5SBrian Gerst 		unsigned int, off_hi, size_t, count)
99121b32a5SBrian Gerst {
100121b32a5SBrian Gerst 	return ksys_readahead(fd, ((u64)off_hi << 32) | off_lo, count);
101121b32a5SBrian Gerst }
102121b32a5SBrian Gerst 
SYSCALL_DEFINE6(ia32_sync_file_range,int,fd,unsigned int,off_low,unsigned int,off_hi,unsigned int,n_low,unsigned int,n_hi,int,flags)103121b32a5SBrian Gerst SYSCALL_DEFINE6(ia32_sync_file_range, int, fd, unsigned int, off_low,
104121b32a5SBrian Gerst 		unsigned int, off_hi, unsigned int, n_low,
105121b32a5SBrian Gerst 		unsigned int, n_hi, int, flags)
106121b32a5SBrian Gerst {
107121b32a5SBrian Gerst 	return ksys_sync_file_range(fd,
108121b32a5SBrian Gerst 				    ((u64)off_hi << 32) | off_low,
109121b32a5SBrian Gerst 				    ((u64)n_hi << 32) | n_low, flags);
110121b32a5SBrian Gerst }
111121b32a5SBrian Gerst 
SYSCALL_DEFINE5(ia32_fadvise64,int,fd,unsigned int,offset_lo,unsigned int,offset_hi,size_t,len,int,advice)112121b32a5SBrian Gerst SYSCALL_DEFINE5(ia32_fadvise64, int, fd, unsigned int, offset_lo,
113121b32a5SBrian Gerst 		unsigned int, offset_hi, size_t, len, int, advice)
114121b32a5SBrian Gerst {
115121b32a5SBrian Gerst 	return ksys_fadvise64_64(fd, ((u64)offset_hi << 32) | offset_lo,
116121b32a5SBrian Gerst 				 len, advice);
117121b32a5SBrian Gerst }
118121b32a5SBrian Gerst 
SYSCALL_DEFINE6(ia32_fallocate,int,fd,int,mode,unsigned int,offset_lo,unsigned int,offset_hi,unsigned int,len_lo,unsigned int,len_hi)119121b32a5SBrian Gerst SYSCALL_DEFINE6(ia32_fallocate, int, fd, int, mode,
120121b32a5SBrian Gerst 		unsigned int, offset_lo, unsigned int, offset_hi,
121121b32a5SBrian Gerst 		unsigned int, len_lo, unsigned int, len_hi)
122121b32a5SBrian Gerst {
123121b32a5SBrian Gerst 	return ksys_fallocate(fd, mode, ((u64)offset_hi << 32) | offset_lo,
124121b32a5SBrian Gerst 			      ((u64)len_hi << 32) | len_lo);
125121b32a5SBrian Gerst }
126121b32a5SBrian Gerst 
127121b32a5SBrian Gerst #ifdef CONFIG_IA32_EMULATION
128121b32a5SBrian Gerst /*
129121b32a5SBrian Gerst  * Another set for IA32/LFS -- x86_64 struct stat is different due to
130121b32a5SBrian Gerst  * support for 64bit inode numbers.
131121b32a5SBrian Gerst  */
cp_stat64(struct stat64 __user * ubuf,struct kstat * stat)132121b32a5SBrian Gerst static int cp_stat64(struct stat64 __user *ubuf, struct kstat *stat)
133121b32a5SBrian Gerst {
134121b32a5SBrian Gerst 	typeof(ubuf->st_uid) uid = 0;
135121b32a5SBrian Gerst 	typeof(ubuf->st_gid) gid = 0;
136121b32a5SBrian Gerst 	SET_UID(uid, from_kuid_munged(current_user_ns(), stat->uid));
137121b32a5SBrian Gerst 	SET_GID(gid, from_kgid_munged(current_user_ns(), stat->gid));
138c120f3b8SAl Viro 	if (!user_write_access_begin(ubuf, sizeof(struct stat64)))
139121b32a5SBrian Gerst 		return -EFAULT;
140c120f3b8SAl Viro 	unsafe_put_user(huge_encode_dev(stat->dev), &ubuf->st_dev, Efault);
141c120f3b8SAl Viro 	unsafe_put_user(stat->ino, &ubuf->__st_ino, Efault);
142c120f3b8SAl Viro 	unsafe_put_user(stat->ino, &ubuf->st_ino, Efault);
143c120f3b8SAl Viro 	unsafe_put_user(stat->mode, &ubuf->st_mode, Efault);
144c120f3b8SAl Viro 	unsafe_put_user(stat->nlink, &ubuf->st_nlink, Efault);
145c120f3b8SAl Viro 	unsafe_put_user(uid, &ubuf->st_uid, Efault);
146c120f3b8SAl Viro 	unsafe_put_user(gid, &ubuf->st_gid, Efault);
147c120f3b8SAl Viro 	unsafe_put_user(huge_encode_dev(stat->rdev), &ubuf->st_rdev, Efault);
148c120f3b8SAl Viro 	unsafe_put_user(stat->size, &ubuf->st_size, Efault);
149c120f3b8SAl Viro 	unsafe_put_user(stat->atime.tv_sec, &ubuf->st_atime, Efault);
150c120f3b8SAl Viro 	unsafe_put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec, Efault);
151c120f3b8SAl Viro 	unsafe_put_user(stat->mtime.tv_sec, &ubuf->st_mtime, Efault);
152c120f3b8SAl Viro 	unsafe_put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec, Efault);
153c120f3b8SAl Viro 	unsafe_put_user(stat->ctime.tv_sec, &ubuf->st_ctime, Efault);
154c120f3b8SAl Viro 	unsafe_put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec, Efault);
155c120f3b8SAl Viro 	unsafe_put_user(stat->blksize, &ubuf->st_blksize, Efault);
156c120f3b8SAl Viro 	unsafe_put_user(stat->blocks, &ubuf->st_blocks, Efault);
157c120f3b8SAl Viro 	user_access_end();
158121b32a5SBrian Gerst 	return 0;
159c120f3b8SAl Viro Efault:
160c120f3b8SAl Viro 	user_write_access_end();
161c120f3b8SAl Viro 	return -EFAULT;
162121b32a5SBrian Gerst }
163121b32a5SBrian Gerst 
COMPAT_SYSCALL_DEFINE2(ia32_stat64,const char __user *,filename,struct stat64 __user *,statbuf)164121b32a5SBrian Gerst COMPAT_SYSCALL_DEFINE2(ia32_stat64, const char __user *, filename,
165121b32a5SBrian Gerst 		       struct stat64 __user *, statbuf)
166121b32a5SBrian Gerst {
167121b32a5SBrian Gerst 	struct kstat stat;
168121b32a5SBrian Gerst 	int ret = vfs_stat(filename, &stat);
169121b32a5SBrian Gerst 
170121b32a5SBrian Gerst 	if (!ret)
171121b32a5SBrian Gerst 		ret = cp_stat64(statbuf, &stat);
172121b32a5SBrian Gerst 	return ret;
173121b32a5SBrian Gerst }
174121b32a5SBrian Gerst 
COMPAT_SYSCALL_DEFINE2(ia32_lstat64,const char __user *,filename,struct stat64 __user *,statbuf)175121b32a5SBrian Gerst COMPAT_SYSCALL_DEFINE2(ia32_lstat64, const char __user *, filename,
176121b32a5SBrian Gerst 		       struct stat64 __user *, statbuf)
177121b32a5SBrian Gerst {
178121b32a5SBrian Gerst 	struct kstat stat;
179121b32a5SBrian Gerst 	int ret = vfs_lstat(filename, &stat);
180121b32a5SBrian Gerst 	if (!ret)
181121b32a5SBrian Gerst 		ret = cp_stat64(statbuf, &stat);
182121b32a5SBrian Gerst 	return ret;
183121b32a5SBrian Gerst }
184121b32a5SBrian Gerst 
COMPAT_SYSCALL_DEFINE2(ia32_fstat64,unsigned int,fd,struct stat64 __user *,statbuf)185121b32a5SBrian Gerst COMPAT_SYSCALL_DEFINE2(ia32_fstat64, unsigned int, fd,
186121b32a5SBrian Gerst 		       struct stat64 __user *, statbuf)
187121b32a5SBrian Gerst {
188121b32a5SBrian Gerst 	struct kstat stat;
189121b32a5SBrian Gerst 	int ret = vfs_fstat(fd, &stat);
190121b32a5SBrian Gerst 	if (!ret)
191121b32a5SBrian Gerst 		ret = cp_stat64(statbuf, &stat);
192121b32a5SBrian Gerst 	return ret;
193121b32a5SBrian Gerst }
194121b32a5SBrian Gerst 
COMPAT_SYSCALL_DEFINE4(ia32_fstatat64,unsigned int,dfd,const char __user *,filename,struct stat64 __user *,statbuf,int,flag)195121b32a5SBrian Gerst COMPAT_SYSCALL_DEFINE4(ia32_fstatat64, unsigned int, dfd,
196121b32a5SBrian Gerst 		       const char __user *, filename,
197121b32a5SBrian Gerst 		       struct stat64 __user *, statbuf, int, flag)
198121b32a5SBrian Gerst {
199121b32a5SBrian Gerst 	struct kstat stat;
200121b32a5SBrian Gerst 	int error;
201121b32a5SBrian Gerst 
202121b32a5SBrian Gerst 	error = vfs_fstatat(dfd, filename, &stat, flag);
203121b32a5SBrian Gerst 	if (error)
204121b32a5SBrian Gerst 		return error;
205121b32a5SBrian Gerst 	return cp_stat64(statbuf, &stat);
206121b32a5SBrian Gerst }
207121b32a5SBrian Gerst 
208121b32a5SBrian Gerst /*
209121b32a5SBrian Gerst  * Linux/i386 didn't use to be able to handle more than
210121b32a5SBrian Gerst  * 4 system call parameters, so these system calls used a memory
211121b32a5SBrian Gerst  * block for parameter passing..
212121b32a5SBrian Gerst  */
213121b32a5SBrian Gerst 
214121b32a5SBrian Gerst struct mmap_arg_struct32 {
215121b32a5SBrian Gerst 	unsigned int addr;
216121b32a5SBrian Gerst 	unsigned int len;
217121b32a5SBrian Gerst 	unsigned int prot;
218121b32a5SBrian Gerst 	unsigned int flags;
219121b32a5SBrian Gerst 	unsigned int fd;
220121b32a5SBrian Gerst 	unsigned int offset;
221121b32a5SBrian Gerst };
222121b32a5SBrian Gerst 
COMPAT_SYSCALL_DEFINE1(ia32_mmap,struct mmap_arg_struct32 __user *,arg)223121b32a5SBrian Gerst COMPAT_SYSCALL_DEFINE1(ia32_mmap, struct mmap_arg_struct32 __user *, arg)
224121b32a5SBrian Gerst {
225121b32a5SBrian Gerst 	struct mmap_arg_struct32 a;
226121b32a5SBrian Gerst 
227121b32a5SBrian Gerst 	if (copy_from_user(&a, arg, sizeof(a)))
228121b32a5SBrian Gerst 		return -EFAULT;
229121b32a5SBrian Gerst 
230121b32a5SBrian Gerst 	if (a.offset & ~PAGE_MASK)
231121b32a5SBrian Gerst 		return -EINVAL;
232121b32a5SBrian Gerst 
233121b32a5SBrian Gerst 	return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
234121b32a5SBrian Gerst 			       a.offset>>PAGE_SHIFT);
235121b32a5SBrian Gerst }
236121b32a5SBrian Gerst 
237121b32a5SBrian Gerst /*
238121b32a5SBrian Gerst  * The 32-bit clone ABI is CONFIG_CLONE_BACKWARDS
239121b32a5SBrian Gerst  */
COMPAT_SYSCALL_DEFINE5(ia32_clone,unsigned long,clone_flags,unsigned long,newsp,int __user *,parent_tidptr,unsigned long,tls_val,int __user *,child_tidptr)240121b32a5SBrian Gerst COMPAT_SYSCALL_DEFINE5(ia32_clone, unsigned long, clone_flags,
241121b32a5SBrian Gerst 		       unsigned long, newsp, int __user *, parent_tidptr,
242121b32a5SBrian Gerst 		       unsigned long, tls_val, int __user *, child_tidptr)
243121b32a5SBrian Gerst {
244121b32a5SBrian Gerst 	struct kernel_clone_args args = {
245121b32a5SBrian Gerst 		.flags		= (clone_flags & ~CSIGNAL),
246121b32a5SBrian Gerst 		.pidfd		= parent_tidptr,
247121b32a5SBrian Gerst 		.child_tid	= child_tidptr,
248121b32a5SBrian Gerst 		.parent_tid	= parent_tidptr,
249121b32a5SBrian Gerst 		.exit_signal	= (clone_flags & CSIGNAL),
250121b32a5SBrian Gerst 		.stack		= newsp,
251121b32a5SBrian Gerst 		.tls		= tls_val,
252121b32a5SBrian Gerst 	};
253121b32a5SBrian Gerst 
254*c723523bSChristian Brauner 	return kernel_clone(&args);
255121b32a5SBrian Gerst }
256121b32a5SBrian Gerst #endif /* CONFIG_IA32_EMULATION */
257