18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Access to user system call parameters and results
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 Red Hat, Inc.  All rights reserved.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * See asm-generic/syscall.h for descriptions of what we must do here.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _ASM_SYSCALL_H
118c2ecf20Sopenharmony_ci#define _ASM_SYSCALL_H	1
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <uapi/linux/audit.h>
148c2ecf20Sopenharmony_ci#include <linux/sched.h>
158c2ecf20Sopenharmony_ci#include <linux/thread_info.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* ftrace syscalls requires exporting the sys_call_table */
188c2ecf20Sopenharmony_ciextern const unsigned long sys_call_table[];
198c2ecf20Sopenharmony_ciextern const unsigned long compat_sys_call_table[];
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	/*
248c2ecf20Sopenharmony_ci	 * Note that we are returning an int here. That means 0xffffffff, ie.
258c2ecf20Sopenharmony_ci	 * 32-bit negative 1, will be interpreted as -1 on a 64-bit kernel.
268c2ecf20Sopenharmony_ci	 * This is important for seccomp so that compat tasks can set r0 = -1
278c2ecf20Sopenharmony_ci	 * to reject the syscall.
288c2ecf20Sopenharmony_ci	 */
298c2ecf20Sopenharmony_ci	if (trap_is_syscall(regs))
308c2ecf20Sopenharmony_ci		return regs->gpr[0];
318c2ecf20Sopenharmony_ci	else
328c2ecf20Sopenharmony_ci		return -1;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic inline void syscall_rollback(struct task_struct *task,
368c2ecf20Sopenharmony_ci				    struct pt_regs *regs)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	regs->gpr[3] = regs->orig_gpr3;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic inline long syscall_get_error(struct task_struct *task,
428c2ecf20Sopenharmony_ci				     struct pt_regs *regs)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	if (trap_is_scv(regs)) {
458c2ecf20Sopenharmony_ci		unsigned long error = regs->gpr[3];
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci		return IS_ERR_VALUE(error) ? error : 0;
488c2ecf20Sopenharmony_ci	} else {
498c2ecf20Sopenharmony_ci		/*
508c2ecf20Sopenharmony_ci		 * If the system call failed,
518c2ecf20Sopenharmony_ci		 * regs->gpr[3] contains a positive ERRORCODE.
528c2ecf20Sopenharmony_ci		 */
538c2ecf20Sopenharmony_ci		return (regs->ccr & 0x10000000UL) ? -regs->gpr[3] : 0;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic inline long syscall_get_return_value(struct task_struct *task,
588c2ecf20Sopenharmony_ci					    struct pt_regs *regs)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return regs->gpr[3];
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic inline void syscall_set_return_value(struct task_struct *task,
648c2ecf20Sopenharmony_ci					    struct pt_regs *regs,
658c2ecf20Sopenharmony_ci					    int error, long val)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	if (trap_is_scv(regs)) {
688c2ecf20Sopenharmony_ci		regs->gpr[3] = (long) error ?: val;
698c2ecf20Sopenharmony_ci	} else {
708c2ecf20Sopenharmony_ci		/*
718c2ecf20Sopenharmony_ci		 * In the general case it's not obvious that we must deal with
728c2ecf20Sopenharmony_ci		 * CCR here, as the syscall exit path will also do that for us.
738c2ecf20Sopenharmony_ci		 * However there are some places, eg. the signal code, which
748c2ecf20Sopenharmony_ci		 * check ccr to decide if the value in r3 is actually an error.
758c2ecf20Sopenharmony_ci		 */
768c2ecf20Sopenharmony_ci		if (error) {
778c2ecf20Sopenharmony_ci			regs->ccr |= 0x10000000L;
788c2ecf20Sopenharmony_ci			regs->gpr[3] = error;
798c2ecf20Sopenharmony_ci		} else {
808c2ecf20Sopenharmony_ci			regs->ccr &= ~0x10000000L;
818c2ecf20Sopenharmony_ci			regs->gpr[3] = val;
828c2ecf20Sopenharmony_ci		}
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic inline void syscall_get_arguments(struct task_struct *task,
878c2ecf20Sopenharmony_ci					 struct pt_regs *regs,
888c2ecf20Sopenharmony_ci					 unsigned long *args)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	unsigned long val, mask = -1UL;
918c2ecf20Sopenharmony_ci	unsigned int n = 6;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
948c2ecf20Sopenharmony_ci	if (test_tsk_thread_flag(task, TIF_32BIT))
958c2ecf20Sopenharmony_ci		mask = 0xffffffff;
968c2ecf20Sopenharmony_ci#endif
978c2ecf20Sopenharmony_ci	while (n--) {
988c2ecf20Sopenharmony_ci		if (n == 0)
998c2ecf20Sopenharmony_ci			val = regs->orig_gpr3;
1008c2ecf20Sopenharmony_ci		else
1018c2ecf20Sopenharmony_ci			val = regs->gpr[3 + n];
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		args[n] = val & mask;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic inline void syscall_set_arguments(struct task_struct *task,
1088c2ecf20Sopenharmony_ci					 struct pt_regs *regs,
1098c2ecf20Sopenharmony_ci					 const unsigned long *args)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	memcpy(&regs->gpr[3], args, 6 * sizeof(args[0]));
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* Also copy the first argument into orig_gpr3 */
1148c2ecf20Sopenharmony_ci	regs->orig_gpr3 = args[0];
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic inline int syscall_get_arch(struct task_struct *task)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	int arch;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC64) && !test_tsk_thread_flag(task, TIF_32BIT))
1228c2ecf20Sopenharmony_ci		arch = AUDIT_ARCH_PPC64;
1238c2ecf20Sopenharmony_ci	else
1248c2ecf20Sopenharmony_ci		arch = AUDIT_ARCH_PPC;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#ifdef __LITTLE_ENDIAN__
1278c2ecf20Sopenharmony_ci	arch |= __AUDIT_ARCH_LE;
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_ci	return arch;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci#endif	/* _ASM_SYSCALL_H */
132