xref: /linux/arch/powerpc/include/asm/syscall.h (revision 9738280aae592b579a25b5b1b6584c894827d3c7)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Access to user system call parameters and results
4  *
5  * Copyright (C) 2008 Red Hat, Inc.  All rights reserved.
6  *
7  * See asm-generic/syscall.h for descriptions of what we must do here.
8  */
9 
10 #ifndef _ASM_SYSCALL_H
11 #define _ASM_SYSCALL_H	1
12 
13 #include <uapi/linux/audit.h>
14 #include <linux/sched.h>
15 #include <linux/thread_info.h>
16 
17 #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
18 typedef long (*syscall_fn)(const struct pt_regs *);
19 #else
20 typedef long (*syscall_fn)(unsigned long, unsigned long, unsigned long,
21 			   unsigned long, unsigned long, unsigned long);
22 #endif
23 
24 /* ftrace syscalls requires exporting the sys_call_table */
25 extern const syscall_fn sys_call_table[];
26 extern const syscall_fn compat_sys_call_table[];
27 
28 static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
29 {
30 	/*
31 	 * Note that we are returning an int here. That means 0xffffffff, ie.
32 	 * 32-bit negative 1, will be interpreted as -1 on a 64-bit kernel.
33 	 * This is important for seccomp so that compat tasks can set r0 = -1
34 	 * to reject the syscall.
35 	 */
36 	if (trap_is_syscall(regs))
37 		return regs->gpr[0];
38 	else
39 		return -1;
40 }
41 
42 static inline void syscall_set_nr(struct task_struct *task, struct pt_regs *regs, int nr)
43 {
44 	/*
45 	 * Unlike syscall_get_nr(), syscall_set_nr() can be called only when
46 	 * the target task is stopped for tracing on entering syscall, so
47 	 * there is no need to have the same check syscall_get_nr() has.
48 	 */
49 	regs->gpr[0] = nr;
50 }
51 
52 static inline void syscall_rollback(struct task_struct *task,
53 				    struct pt_regs *regs)
54 {
55 	regs->gpr[3] = regs->orig_gpr3;
56 }
57 
58 static inline long syscall_get_error(struct task_struct *task,
59 				     struct pt_regs *regs)
60 {
61 	if (trap_is_scv(regs)) {
62 		unsigned long error = regs->gpr[3];
63 
64 		return IS_ERR_VALUE(error) ? error : 0;
65 	} else {
66 		/*
67 		 * If the system call failed,
68 		 * regs->gpr[3] contains a positive ERRORCODE.
69 		 */
70 		return (regs->ccr & 0x10000000UL) ? -regs->gpr[3] : 0;
71 	}
72 }
73 
74 static inline long syscall_get_return_value(struct task_struct *task,
75 					    struct pt_regs *regs)
76 {
77 	return regs->gpr[3];
78 }
79 
80 static inline void syscall_set_return_value(struct task_struct *task,
81 					    struct pt_regs *regs,
82 					    int error, long val)
83 {
84 	if (trap_is_scv(regs)) {
85 		regs->gpr[3] = (long) error ?: val;
86 	} else {
87 		/*
88 		 * In the general case it's not obvious that we must deal with
89 		 * CCR here, as the syscall exit path will also do that for us.
90 		 * However there are some places, eg. the signal code, which
91 		 * check ccr to decide if the value in r3 is actually an error.
92 		 */
93 		if (error) {
94 			regs->ccr |= 0x10000000L;
95 			regs->gpr[3] = error;
96 		} else {
97 			regs->ccr &= ~0x10000000L;
98 			regs->gpr[3] = val;
99 		}
100 	}
101 }
102 
103 static inline void syscall_get_arguments(struct task_struct *task,
104 					 struct pt_regs *regs,
105 					 unsigned long *args)
106 {
107 	unsigned long val, mask = -1UL;
108 	unsigned int n = 6;
109 
110 	if (is_tsk_32bit_task(task))
111 		mask = 0xffffffff;
112 
113 	while (n--) {
114 		if (n == 0)
115 			val = regs->orig_gpr3;
116 		else
117 			val = regs->gpr[3 + n];
118 
119 		args[n] = val & mask;
120 	}
121 }
122 
123 static inline void syscall_set_arguments(struct task_struct *task,
124 					 struct pt_regs *regs,
125 					 const unsigned long *args)
126 {
127 	memcpy(&regs->gpr[3], args, 6 * sizeof(args[0]));
128 
129 	/* Also copy the first argument into orig_gpr3 */
130 	regs->orig_gpr3 = args[0];
131 }
132 
133 static inline int syscall_get_arch(struct task_struct *task)
134 {
135 	if (is_tsk_32bit_task(task))
136 		return AUDIT_ARCH_PPC;
137 	else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
138 		return AUDIT_ARCH_PPC64LE;
139 	else
140 		return AUDIT_ARCH_PPC64;
141 }
142 #endif	/* _ASM_SYSCALL_H */
143