18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Implementation of various system calls for Linux/PowerPC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Derived from "arch/i386/kernel/sys_i386.c"
88c2ecf20Sopenharmony_ci * Adapted from the i386 version by Gary Thomas
98c2ecf20Sopenharmony_ci * Modified by Cort Dougan (cort@cs.nmt.edu)
108c2ecf20Sopenharmony_ci * and Paul Mackerras (paulus@cs.anu.edu.au).
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * This file contains various random system calls that
138c2ecf20Sopenharmony_ci * have a non-standard calling sequence on the Linux/PPC
148c2ecf20Sopenharmony_ci * platform.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/errno.h>
188c2ecf20Sopenharmony_ci#include <linux/sched.h>
198c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
208c2ecf20Sopenharmony_ci#include <linux/mm.h>
218c2ecf20Sopenharmony_ci#include <linux/fs.h>
228c2ecf20Sopenharmony_ci#include <linux/smp.h>
238c2ecf20Sopenharmony_ci#include <linux/sem.h>
248c2ecf20Sopenharmony_ci#include <linux/msg.h>
258c2ecf20Sopenharmony_ci#include <linux/shm.h>
268c2ecf20Sopenharmony_ci#include <linux/stat.h>
278c2ecf20Sopenharmony_ci#include <linux/mman.h>
288c2ecf20Sopenharmony_ci#include <linux/sys.h>
298c2ecf20Sopenharmony_ci#include <linux/ipc.h>
308c2ecf20Sopenharmony_ci#include <linux/utsname.h>
318c2ecf20Sopenharmony_ci#include <linux/file.h>
328c2ecf20Sopenharmony_ci#include <linux/personality.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
358c2ecf20Sopenharmony_ci#include <asm/syscalls.h>
368c2ecf20Sopenharmony_ci#include <asm/time.h>
378c2ecf20Sopenharmony_ci#include <asm/unistd.h>
388c2ecf20Sopenharmony_ci#include <asm/asm-prototypes.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic inline long do_mmap2(unsigned long addr, size_t len,
418c2ecf20Sopenharmony_ci			unsigned long prot, unsigned long flags,
428c2ecf20Sopenharmony_ci			unsigned long fd, unsigned long off, int shift)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	long ret = -EINVAL;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!arch_validate_prot(prot, addr))
478c2ecf20Sopenharmony_ci		goto out;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (shift) {
508c2ecf20Sopenharmony_ci		if (off & ((1 << shift) - 1))
518c2ecf20Sopenharmony_ci			goto out;
528c2ecf20Sopenharmony_ci		off >>= shift;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	ret = ksys_mmap_pgoff(addr, len, prot, flags, fd, off);
568c2ecf20Sopenharmony_ciout:
578c2ecf20Sopenharmony_ci	return ret;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ciSYSCALL_DEFINE6(mmap2, unsigned long, addr, size_t, len,
618c2ecf20Sopenharmony_ci		unsigned long, prot, unsigned long, flags,
628c2ecf20Sopenharmony_ci		unsigned long, fd, unsigned long, pgoff)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	return do_mmap2(addr, len, prot, flags, fd, pgoff, PAGE_SHIFT-12);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciSYSCALL_DEFINE6(mmap, unsigned long, addr, size_t, len,
688c2ecf20Sopenharmony_ci		unsigned long, prot, unsigned long, flags,
698c2ecf20Sopenharmony_ci		unsigned long, fd, off_t, offset)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	return do_mmap2(addr, len, prot, flags, fd, offset, PAGE_SHIFT);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC32
758c2ecf20Sopenharmony_ci/*
768c2ecf20Sopenharmony_ci * Due to some executables calling the wrong select we sometimes
778c2ecf20Sopenharmony_ci * get wrong args.  This determines how the args are being passed
788c2ecf20Sopenharmony_ci * (a single ptr to them all args passed) then calls
798c2ecf20Sopenharmony_ci * sys_select() with the appropriate args. -- Cort
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_ciint
828c2ecf20Sopenharmony_cippc_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct __kernel_old_timeval __user *tvp)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	if ( (unsigned long)n >= 4096 )
858c2ecf20Sopenharmony_ci	{
868c2ecf20Sopenharmony_ci		unsigned long __user *buffer = (unsigned long __user *)n;
878c2ecf20Sopenharmony_ci		if (!access_ok(buffer, 5*sizeof(unsigned long))
888c2ecf20Sopenharmony_ci		    || __get_user(n, buffer)
898c2ecf20Sopenharmony_ci		    || __get_user(inp, ((fd_set __user * __user *)(buffer+1)))
908c2ecf20Sopenharmony_ci		    || __get_user(outp, ((fd_set  __user * __user *)(buffer+2)))
918c2ecf20Sopenharmony_ci		    || __get_user(exp, ((fd_set  __user * __user *)(buffer+3)))
928c2ecf20Sopenharmony_ci		    || __get_user(tvp, ((struct __kernel_old_timeval  __user * __user *)(buffer+4))))
938c2ecf20Sopenharmony_ci			return -EFAULT;
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci	return sys_select(n, inp, outp, exp, tvp);
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci#endif
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC64
1008c2ecf20Sopenharmony_cilong ppc64_personality(unsigned long personality)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	long ret;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (personality(current->personality) == PER_LINUX32
1058c2ecf20Sopenharmony_ci	    && personality(personality) == PER_LINUX)
1068c2ecf20Sopenharmony_ci		personality = (personality & ~PER_MASK) | PER_LINUX32;
1078c2ecf20Sopenharmony_ci	ret = sys_personality(personality);
1088c2ecf20Sopenharmony_ci	if (personality(ret) == PER_LINUX32)
1098c2ecf20Sopenharmony_ci		ret = (ret & ~PER_MASK) | PER_LINUX;
1108c2ecf20Sopenharmony_ci	return ret;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci#endif
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cilong ppc_fadvise64_64(int fd, int advice, u32 offset_high, u32 offset_low,
1158c2ecf20Sopenharmony_ci		      u32 len_high, u32 len_low)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	return ksys_fadvise64_64(fd, (u64)offset_high << 32 | offset_low,
1188c2ecf20Sopenharmony_ci				 (u64)len_high << 32 | len_low, advice);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciSYSCALL_DEFINE0(switch_endian)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct thread_info *ti;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	current->thread.regs->msr ^= MSR_LE;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 * Set TIF_RESTOREALL so that r3 isn't clobbered on return to
1298c2ecf20Sopenharmony_ci	 * userspace. That also has the effect of restoring the non-volatile
1308c2ecf20Sopenharmony_ci	 * GPRs, so we saved them on the way in here.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	ti = current_thread_info();
1338c2ecf20Sopenharmony_ci	ti->flags |= _TIF_RESTOREALL;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	return 0;
1368c2ecf20Sopenharmony_ci}
137