xref: /openbmc/linux/arch/sparc/kernel/sys_sparc32.c (revision 680ef72a)
1 // SPDX-License-Identifier: GPL-2.0
2 /* sys_sparc32.c: Conversion between 32bit and 64bit native syscalls.
3  *
4  * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  * Copyright (C) 1997, 2007 David S. Miller (davem@davemloft.net)
6  *
7  * These routines maintain argument size conversion between 32bit and 64bit
8  * environment.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/capability.h>
14 #include <linux/fs.h>
15 #include <linux/mm.h>
16 #include <linux/file.h>
17 #include <linux/signal.h>
18 #include <linux/resource.h>
19 #include <linux/times.h>
20 #include <linux/smp.h>
21 #include <linux/sem.h>
22 #include <linux/msg.h>
23 #include <linux/shm.h>
24 #include <linux/uio.h>
25 #include <linux/nfs_fs.h>
26 #include <linux/quota.h>
27 #include <linux/poll.h>
28 #include <linux/personality.h>
29 #include <linux/stat.h>
30 #include <linux/filter.h>
31 #include <linux/highmem.h>
32 #include <linux/highuid.h>
33 #include <linux/mman.h>
34 #include <linux/ipv6.h>
35 #include <linux/in.h>
36 #include <linux/icmpv6.h>
37 #include <linux/syscalls.h>
38 #include <linux/sysctl.h>
39 #include <linux/binfmts.h>
40 #include <linux/dnotify.h>
41 #include <linux/security.h>
42 #include <linux/compat.h>
43 #include <linux/vfs.h>
44 #include <linux/ptrace.h>
45 #include <linux/slab.h>
46 
47 #include <asm/types.h>
48 #include <linux/uaccess.h>
49 #include <asm/fpumacro.h>
50 #include <asm/mmu_context.h>
51 #include <asm/compat_signal.h>
52 
53 #include "systbls.h"
54 
55 asmlinkage long sys32_truncate64(const char __user * path, unsigned long high, unsigned long low)
56 {
57 	if ((int)high < 0)
58 		return -EINVAL;
59 	else
60 		return sys_truncate(path, (high << 32) | low);
61 }
62 
63 asmlinkage long sys32_ftruncate64(unsigned int fd, unsigned long high, unsigned long low)
64 {
65 	if ((int)high < 0)
66 		return -EINVAL;
67 	else
68 		return sys_ftruncate(fd, (high << 32) | low);
69 }
70 
71 static int cp_compat_stat64(struct kstat *stat,
72 			    struct compat_stat64 __user *statbuf)
73 {
74 	int err;
75 
76 	err  = put_user(huge_encode_dev(stat->dev), &statbuf->st_dev);
77 	err |= put_user(stat->ino, &statbuf->st_ino);
78 	err |= put_user(stat->mode, &statbuf->st_mode);
79 	err |= put_user(stat->nlink, &statbuf->st_nlink);
80 	err |= put_user(from_kuid_munged(current_user_ns(), stat->uid), &statbuf->st_uid);
81 	err |= put_user(from_kgid_munged(current_user_ns(), stat->gid), &statbuf->st_gid);
82 	err |= put_user(huge_encode_dev(stat->rdev), &statbuf->st_rdev);
83 	err |= put_user(0, (unsigned long __user *) &statbuf->__pad3[0]);
84 	err |= put_user(stat->size, &statbuf->st_size);
85 	err |= put_user(stat->blksize, &statbuf->st_blksize);
86 	err |= put_user(0, (unsigned int __user *) &statbuf->__pad4[0]);
87 	err |= put_user(0, (unsigned int __user *) &statbuf->__pad4[4]);
88 	err |= put_user(stat->blocks, &statbuf->st_blocks);
89 	err |= put_user(stat->atime.tv_sec, &statbuf->st_atime);
90 	err |= put_user(stat->atime.tv_nsec, &statbuf->st_atime_nsec);
91 	err |= put_user(stat->mtime.tv_sec, &statbuf->st_mtime);
92 	err |= put_user(stat->mtime.tv_nsec, &statbuf->st_mtime_nsec);
93 	err |= put_user(stat->ctime.tv_sec, &statbuf->st_ctime);
94 	err |= put_user(stat->ctime.tv_nsec, &statbuf->st_ctime_nsec);
95 	err |= put_user(0, &statbuf->__unused4);
96 	err |= put_user(0, &statbuf->__unused5);
97 
98 	return err;
99 }
100 
101 asmlinkage long compat_sys_stat64(const char __user * filename,
102 		struct compat_stat64 __user *statbuf)
103 {
104 	struct kstat stat;
105 	int error = vfs_stat(filename, &stat);
106 
107 	if (!error)
108 		error = cp_compat_stat64(&stat, statbuf);
109 	return error;
110 }
111 
112 asmlinkage long compat_sys_lstat64(const char __user * filename,
113 		struct compat_stat64 __user *statbuf)
114 {
115 	struct kstat stat;
116 	int error = vfs_lstat(filename, &stat);
117 
118 	if (!error)
119 		error = cp_compat_stat64(&stat, statbuf);
120 	return error;
121 }
122 
123 asmlinkage long compat_sys_fstat64(unsigned int fd,
124 		struct compat_stat64 __user * statbuf)
125 {
126 	struct kstat stat;
127 	int error = vfs_fstat(fd, &stat);
128 
129 	if (!error)
130 		error = cp_compat_stat64(&stat, statbuf);
131 	return error;
132 }
133 
134 asmlinkage long compat_sys_fstatat64(unsigned int dfd,
135 		const char __user *filename,
136 		struct compat_stat64 __user * statbuf, int flag)
137 {
138 	struct kstat stat;
139 	int error;
140 
141 	error = vfs_fstatat(dfd, filename, &stat, flag);
142 	if (error)
143 		return error;
144 	return cp_compat_stat64(&stat, statbuf);
145 }
146 
147 COMPAT_SYSCALL_DEFINE3(sparc_sigaction, int, sig,
148 			struct compat_old_sigaction __user *,act,
149 			struct compat_old_sigaction __user *,oact)
150 {
151 	WARN_ON_ONCE(sig >= 0);
152 	return compat_sys_sigaction(-sig, act, oact);
153 }
154 
155 COMPAT_SYSCALL_DEFINE5(rt_sigaction, int, sig,
156 			struct compat_sigaction __user *,act,
157 			struct compat_sigaction __user *,oact,
158 			void __user *,restorer,
159 			compat_size_t,sigsetsize)
160 {
161         struct k_sigaction new_ka, old_ka;
162         int ret;
163 
164         /* XXX: Don't preclude handling different sized sigset_t's.  */
165         if (sigsetsize != sizeof(compat_sigset_t))
166                 return -EINVAL;
167 
168         if (act) {
169 		u32 u_handler, u_restorer;
170 
171 		new_ka.ka_restorer = restorer;
172 		ret = get_user(u_handler, &act->sa_handler);
173 		new_ka.sa.sa_handler =  compat_ptr(u_handler);
174 		ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
175 		ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
176 		ret |= get_user(u_restorer, &act->sa_restorer);
177 		new_ka.sa.sa_restorer = compat_ptr(u_restorer);
178                 if (ret)
179                 	return -EFAULT;
180 	}
181 
182 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
183 
184 	if (!ret && oact) {
185 		ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), &oact->sa_handler);
186 		ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
187 					 sizeof(oact->sa_mask));
188 		ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
189 		ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer), &oact->sa_restorer);
190 		if (ret)
191 			ret = -EFAULT;
192         }
193 
194         return ret;
195 }
196 
197 asmlinkage compat_ssize_t sys32_pread64(unsigned int fd,
198 					char __user *ubuf,
199 					compat_size_t count,
200 					unsigned long poshi,
201 					unsigned long poslo)
202 {
203 	return sys_pread64(fd, ubuf, count, (poshi << 32) | poslo);
204 }
205 
206 asmlinkage compat_ssize_t sys32_pwrite64(unsigned int fd,
207 					 char __user *ubuf,
208 					 compat_size_t count,
209 					 unsigned long poshi,
210 					 unsigned long poslo)
211 {
212 	return sys_pwrite64(fd, ubuf, count, (poshi << 32) | poslo);
213 }
214 
215 asmlinkage long compat_sys_readahead(int fd,
216 				     unsigned long offhi,
217 				     unsigned long offlo,
218 				     compat_size_t count)
219 {
220 	return sys_readahead(fd, (offhi << 32) | offlo, count);
221 }
222 
223 long compat_sys_fadvise64(int fd,
224 			  unsigned long offhi,
225 			  unsigned long offlo,
226 			  compat_size_t len, int advice)
227 {
228 	return sys_fadvise64_64(fd, (offhi << 32) | offlo, len, advice);
229 }
230 
231 long compat_sys_fadvise64_64(int fd,
232 			     unsigned long offhi, unsigned long offlo,
233 			     unsigned long lenhi, unsigned long lenlo,
234 			     int advice)
235 {
236 	return sys_fadvise64_64(fd,
237 				(offhi << 32) | offlo,
238 				(lenhi << 32) | lenlo,
239 				advice);
240 }
241 
242 long sys32_sync_file_range(unsigned int fd, unsigned long off_high, unsigned long off_low, unsigned long nb_high, unsigned long nb_low, unsigned int flags)
243 {
244 	return sys_sync_file_range(fd,
245 				   (off_high << 32) | off_low,
246 				   (nb_high << 32) | nb_low,
247 				   flags);
248 }
249 
250 asmlinkage long compat_sys_fallocate(int fd, int mode, u32 offhi, u32 offlo,
251 				     u32 lenhi, u32 lenlo)
252 {
253 	return sys_fallocate(fd, mode, ((loff_t)offhi << 32) | offlo,
254 			     ((loff_t)lenhi << 32) | lenlo);
255 }
256