xref: /openbmc/linux/arch/arm64/kernel/sys_compat.c (revision b732539e)
1 /*
2  * Based on arch/arm/kernel/sys_arm.c
3  *
4  * Copyright (C) People who wrote linux/arch/i386/kernel/sys_i386.c
5  * Copyright (C) 1995, 1996 Russell King.
6  * Copyright (C) 2012 ARM Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <linux/compat.h>
22 #include <linux/personality.h>
23 #include <linux/sched.h>
24 #include <linux/sched/signal.h>
25 #include <linux/slab.h>
26 #include <linux/syscalls.h>
27 #include <linux/uaccess.h>
28 
29 #include <asm/cacheflush.h>
30 #include <asm/system_misc.h>
31 #include <asm/unistd.h>
32 
33 static long
34 __do_compat_cache_op(unsigned long start, unsigned long end)
35 {
36 	long ret;
37 
38 	do {
39 		unsigned long chunk = min(PAGE_SIZE, end - start);
40 
41 		if (fatal_signal_pending(current))
42 			return 0;
43 
44 		ret = __flush_cache_user_range(start, start + chunk);
45 		if (ret)
46 			return ret;
47 
48 		cond_resched();
49 		start += chunk;
50 	} while (start < end);
51 
52 	return 0;
53 }
54 
55 static inline long
56 do_compat_cache_op(unsigned long start, unsigned long end, int flags)
57 {
58 	if (end < start || flags)
59 		return -EINVAL;
60 
61 	if (!access_ok(VERIFY_READ, (const void __user *)start, end - start))
62 		return -EFAULT;
63 
64 	return __do_compat_cache_op(start, end);
65 }
66 /*
67  * Handle all unrecognised system calls.
68  */
69 long compat_arm_syscall(struct pt_regs *regs)
70 {
71 	siginfo_t info;
72 	unsigned int no = regs->regs[7];
73 
74 	switch (no) {
75 	/*
76 	 * Flush a region from virtual address 'r0' to virtual address 'r1'
77 	 * _exclusive_.  There is no alignment requirement on either address;
78 	 * user space does not need to know the hardware cache layout.
79 	 *
80 	 * r2 contains flags.  It should ALWAYS be passed as ZERO until it
81 	 * is defined to be something else.  For now we ignore it, but may
82 	 * the fires of hell burn in your belly if you break this rule. ;)
83 	 *
84 	 * (at a later date, we may want to allow this call to not flush
85 	 * various aspects of the cache.  Passing '0' will guarantee that
86 	 * everything necessary gets flushed to maintain consistency in
87 	 * the specified region).
88 	 */
89 	case __ARM_NR_compat_cacheflush:
90 		return do_compat_cache_op(regs->regs[0], regs->regs[1], regs->regs[2]);
91 
92 	case __ARM_NR_compat_set_tls:
93 		current->thread.uw.tp_value = regs->regs[0];
94 
95 		/*
96 		 * Protect against register corruption from context switch.
97 		 * See comment in tls_thread_flush.
98 		 */
99 		barrier();
100 		write_sysreg(regs->regs[0], tpidrro_el0);
101 		return 0;
102 
103 	default:
104 		/*
105 		 * Calls 9f00xx..9f07ff are defined to return -ENOSYS
106 		 * if not implemented, rather than raising SIGILL. This
107 		 * way the calling program can gracefully determine whether
108 		 * a feature is supported.
109 		 */
110 		if ((no & 0xffff) <= 0x7ff)
111 			return -ENOSYS;
112 		break;
113 	}
114 
115 	info.si_signo = SIGILL;
116 	info.si_errno = 0;
117 	info.si_code  = ILL_ILLTRP;
118 	info.si_addr  = (void __user *)instruction_pointer(regs) -
119 			 (compat_thumb_mode(regs) ? 2 : 4);
120 
121 	arm64_notify_die("Oops - bad compat syscall(2)", regs, &info, no);
122 	return 0;
123 }
124