xref: /openbmc/linux/arch/powerpc/kernel/syscalls.c (revision dec20c50)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
230286ef6SPaul Mackerras /*
330286ef6SPaul Mackerras  *  Implementation of various system calls for Linux/PowerPC
430286ef6SPaul Mackerras  *
530286ef6SPaul Mackerras  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
630286ef6SPaul Mackerras  *
730286ef6SPaul Mackerras  * Derived from "arch/i386/kernel/sys_i386.c"
830286ef6SPaul Mackerras  * Adapted from the i386 version by Gary Thomas
930286ef6SPaul Mackerras  * Modified by Cort Dougan (cort@cs.nmt.edu)
1030286ef6SPaul Mackerras  * and Paul Mackerras (paulus@cs.anu.edu.au).
1130286ef6SPaul Mackerras  *
1230286ef6SPaul Mackerras  * This file contains various random system calls that
1330286ef6SPaul Mackerras  * have a non-standard calling sequence on the Linux/PPC
1430286ef6SPaul Mackerras  * platform.
1530286ef6SPaul Mackerras  */
1630286ef6SPaul Mackerras 
1730286ef6SPaul Mackerras #include <linux/errno.h>
1830286ef6SPaul Mackerras #include <linux/sched.h>
1930286ef6SPaul Mackerras #include <linux/syscalls.h>
2030286ef6SPaul Mackerras #include <linux/mm.h>
214e950f6fSAlexey Dobriyan #include <linux/fs.h>
2230286ef6SPaul Mackerras #include <linux/smp.h>
2330286ef6SPaul Mackerras #include <linux/sem.h>
2430286ef6SPaul Mackerras #include <linux/msg.h>
2530286ef6SPaul Mackerras #include <linux/shm.h>
2630286ef6SPaul Mackerras #include <linux/stat.h>
2730286ef6SPaul Mackerras #include <linux/mman.h>
2830286ef6SPaul Mackerras #include <linux/sys.h>
2930286ef6SPaul Mackerras #include <linux/ipc.h>
3030286ef6SPaul Mackerras #include <linux/utsname.h>
3130286ef6SPaul Mackerras #include <linux/file.h>
3230286ef6SPaul Mackerras #include <linux/personality.h>
3330286ef6SPaul Mackerras 
347c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
35a7f31841SArnd Bergmann #include <asm/syscalls.h>
3630286ef6SPaul Mackerras #include <asm/time.h>
3730286ef6SPaul Mackerras #include <asm/unistd.h>
3830286ef6SPaul Mackerras 
do_mmap2(unsigned long addr,size_t len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long off,int shift)39b7fa9ce8SRohan McLure static long do_mmap2(unsigned long addr, size_t len,
4030286ef6SPaul Mackerras 		     unsigned long prot, unsigned long flags,
4130286ef6SPaul Mackerras 		     unsigned long fd, unsigned long off, int shift)
4230286ef6SPaul Mackerras {
439035cf9aSKhalid Aziz 	if (!arch_validate_prot(prot, addr))
44316389e9SChristophe Leroy 		return -EINVAL;
45ef3d3246SDave Kleikamp 
46316389e9SChristophe Leroy 	if (!IS_ALIGNED(off, 1 << shift))
47316389e9SChristophe Leroy 		return -EINVAL;
4830286ef6SPaul Mackerras 
49316389e9SChristophe Leroy 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, off >> shift);
5030286ef6SPaul Mackerras }
5130286ef6SPaul Mackerras 
SYSCALL_DEFINE6(mmap2,unsigned long,addr,size_t,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,unsigned long,pgoff)529c355917SBalbir Singh SYSCALL_DEFINE6(mmap2, unsigned long, addr, size_t, len,
539c355917SBalbir Singh 		unsigned long, prot, unsigned long, flags,
549c355917SBalbir Singh 		unsigned long, fd, unsigned long, pgoff)
5530286ef6SPaul Mackerras {
5630286ef6SPaul Mackerras 	return do_mmap2(addr, len, prot, flags, fd, pgoff, PAGE_SHIFT-12);
5730286ef6SPaul Mackerras }
5830286ef6SPaul Mackerras 
59b7fa9ce8SRohan McLure #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE6(mmap2,unsigned long,addr,size_t,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,unsigned long,off_4k)60b7fa9ce8SRohan McLure COMPAT_SYSCALL_DEFINE6(mmap2,
61b7fa9ce8SRohan McLure 		       unsigned long, addr, size_t, len,
62b7fa9ce8SRohan McLure 		       unsigned long, prot, unsigned long, flags,
63b7fa9ce8SRohan McLure 		       unsigned long, fd, unsigned long, off_4k)
64b7fa9ce8SRohan McLure {
65b7fa9ce8SRohan McLure 	return do_mmap2(addr, len, prot, flags, fd, off_4k, PAGE_SHIFT-12);
66b7fa9ce8SRohan McLure }
67b7fa9ce8SRohan McLure #endif
68b7fa9ce8SRohan McLure 
SYSCALL_DEFINE6(mmap,unsigned long,addr,size_t,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,off_t,offset)699c355917SBalbir Singh SYSCALL_DEFINE6(mmap, unsigned long, addr, size_t, len,
709c355917SBalbir Singh 		unsigned long, prot, unsigned long, flags,
719c355917SBalbir Singh 		unsigned long, fd, off_t, offset)
7230286ef6SPaul Mackerras {
7330286ef6SPaul Mackerras 	return do_mmap2(addr, len, prot, flags, fd, offset, PAGE_SHIFT);
7430286ef6SPaul Mackerras }
7530286ef6SPaul Mackerras 
7630286ef6SPaul Mackerras #ifdef CONFIG_PPC64
do_ppc64_personality(unsigned long personality)77ac17defbSRohan McLure static long do_ppc64_personality(unsigned long personality)
7830286ef6SPaul Mackerras {
7930286ef6SPaul Mackerras 	long ret;
8030286ef6SPaul Mackerras 
8130286ef6SPaul Mackerras 	if (personality(current->personality) == PER_LINUX32
827256a5d2SJiri Kosina 	    && personality(personality) == PER_LINUX)
837256a5d2SJiri Kosina 		personality = (personality & ~PER_MASK) | PER_LINUX32;
844df0221fSRohan McLure 	ret = ksys_personality(personality);
857256a5d2SJiri Kosina 	if (personality(ret) == PER_LINUX32)
867256a5d2SJiri Kosina 		ret = (ret & ~PER_MASK) | PER_LINUX;
8730286ef6SPaul Mackerras 	return ret;
8830286ef6SPaul Mackerras }
89*dec20c50SRohan McLure 
SYSCALL_DEFINE1(ppc64_personality,unsigned long,personality)90*dec20c50SRohan McLure SYSCALL_DEFINE1(ppc64_personality, unsigned long, personality)
91ac17defbSRohan McLure {
92ac17defbSRohan McLure 	return do_ppc64_personality(personality);
93ac17defbSRohan McLure }
9430286ef6SPaul Mackerras 
95*dec20c50SRohan McLure #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE1(ppc64_personality,unsigned long,personality)96*dec20c50SRohan McLure COMPAT_SYSCALL_DEFINE1(ppc64_personality, unsigned long, personality)
97*dec20c50SRohan McLure {
98*dec20c50SRohan McLure 	return do_ppc64_personality(personality);
99*dec20c50SRohan McLure }
100*dec20c50SRohan McLure #endif /* CONFIG_COMPAT */
101*dec20c50SRohan McLure #endif /* CONFIG_PPC64 */
102*dec20c50SRohan McLure 
SYSCALL_DEFINE6(ppc_fadvise64_64,int,fd,int,advice,u32,offset_high,u32,offset_low,u32,len_high,u32,len_low)103*dec20c50SRohan McLure SYSCALL_DEFINE6(ppc_fadvise64_64,
104*dec20c50SRohan McLure 		int, fd, int, advice, u32, offset_high, u32, offset_low,
105*dec20c50SRohan McLure 		u32, len_high, u32, len_low)
10677f543cbSPaul Mackerras {
107016ff72bSRohan McLure 	return ksys_fadvise64_64(fd, merge_64(offset_high, offset_low),
108016ff72bSRohan McLure 				 merge_64(len_high, len_low), advice);
10977f543cbSPaul Mackerras }
110529d235aSMichael Ellerman 
SYSCALL_DEFINE0(switch_endian)11181dac817SMichael Ellerman SYSCALL_DEFINE0(switch_endian)
112529d235aSMichael Ellerman {
113529d235aSMichael Ellerman 	struct thread_info *ti;
114529d235aSMichael Ellerman 
11559dc5bfcSNicholas Piggin 	regs_set_return_msr(current->thread.regs,
11659dc5bfcSNicholas Piggin 				current->thread.regs->msr ^ MSR_LE);
117529d235aSMichael Ellerman 
118529d235aSMichael Ellerman 	/*
119529d235aSMichael Ellerman 	 * Set TIF_RESTOREALL so that r3 isn't clobbered on return to
120529d235aSMichael Ellerman 	 * userspace. That also has the effect of restoring the non-volatile
121529d235aSMichael Ellerman 	 * GPRs, so we saved them on the way in here.
122529d235aSMichael Ellerman 	 */
123529d235aSMichael Ellerman 	ti = current_thread_info();
124529d235aSMichael Ellerman 	ti->flags |= _TIF_RESTOREALL;
125529d235aSMichael Ellerman 
126529d235aSMichael Ellerman 	return 0;
127529d235aSMichael Ellerman }
128