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-2009 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_X86_SYSCALL_H
118c2ecf20Sopenharmony_ci#define _ASM_X86_SYSCALL_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <uapi/linux/audit.h>
148c2ecf20Sopenharmony_ci#include <linux/sched.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <asm/thread_info.h>	/* for TS_COMPAT */
178c2ecf20Sopenharmony_ci#include <asm/unistd.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_citypedef long (*sys_call_ptr_t)(const struct pt_regs *);
208c2ecf20Sopenharmony_ciextern const sys_call_ptr_t sys_call_table[];
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#if defined(CONFIG_X86_32)
238c2ecf20Sopenharmony_ci#define ia32_sys_call_table sys_call_table
248c2ecf20Sopenharmony_ci#endif
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#if defined(CONFIG_IA32_EMULATION)
278c2ecf20Sopenharmony_ciextern const sys_call_ptr_t ia32_sys_call_table[];
288c2ecf20Sopenharmony_ci#endif
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_X32_ABI
318c2ecf20Sopenharmony_ciextern const sys_call_ptr_t x32_sys_call_table[];
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Only the low 32 bits of orig_ax are meaningful, so we return int.
368c2ecf20Sopenharmony_ci * This importantly ignores the high bits on 64-bit, so comparisons
378c2ecf20Sopenharmony_ci * sign-extend the low 32 bits.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_cistatic inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	return regs->orig_ax;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic inline void syscall_rollback(struct task_struct *task,
458c2ecf20Sopenharmony_ci				    struct pt_regs *regs)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	regs->ax = regs->orig_ax;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic inline long syscall_get_error(struct task_struct *task,
518c2ecf20Sopenharmony_ci				     struct pt_regs *regs)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	unsigned long error = regs->ax;
548c2ecf20Sopenharmony_ci#ifdef CONFIG_IA32_EMULATION
558c2ecf20Sopenharmony_ci	/*
568c2ecf20Sopenharmony_ci	 * TS_COMPAT is set for 32-bit syscall entries and then
578c2ecf20Sopenharmony_ci	 * remains set until we return to user mode.
588c2ecf20Sopenharmony_ci	 */
598c2ecf20Sopenharmony_ci	if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
608c2ecf20Sopenharmony_ci		/*
618c2ecf20Sopenharmony_ci		 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
628c2ecf20Sopenharmony_ci		 * and will match correctly in comparisons.
638c2ecf20Sopenharmony_ci		 */
648c2ecf20Sopenharmony_ci		error = (long) (int) error;
658c2ecf20Sopenharmony_ci#endif
668c2ecf20Sopenharmony_ci	return IS_ERR_VALUE(error) ? error : 0;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic inline long syscall_get_return_value(struct task_struct *task,
708c2ecf20Sopenharmony_ci					    struct pt_regs *regs)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	return regs->ax;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic inline void syscall_set_return_value(struct task_struct *task,
768c2ecf20Sopenharmony_ci					    struct pt_regs *regs,
778c2ecf20Sopenharmony_ci					    int error, long val)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	regs->ax = (long) error ?: val;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic inline void syscall_get_arguments(struct task_struct *task,
858c2ecf20Sopenharmony_ci					 struct pt_regs *regs,
868c2ecf20Sopenharmony_ci					 unsigned long *args)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	memcpy(args, &regs->bx, 6 * sizeof(args[0]));
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic inline void syscall_set_arguments(struct task_struct *task,
928c2ecf20Sopenharmony_ci					 struct pt_regs *regs,
938c2ecf20Sopenharmony_ci					 unsigned int i, unsigned int n,
948c2ecf20Sopenharmony_ci					 const unsigned long *args)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	BUG_ON(i + n > 6);
978c2ecf20Sopenharmony_ci	memcpy(&regs->bx + i, args, n * sizeof(args[0]));
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic inline int syscall_get_arch(struct task_struct *task)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return AUDIT_ARCH_I386;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#else	 /* CONFIG_X86_64 */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic inline void syscall_get_arguments(struct task_struct *task,
1088c2ecf20Sopenharmony_ci					 struct pt_regs *regs,
1098c2ecf20Sopenharmony_ci					 unsigned long *args)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci# ifdef CONFIG_IA32_EMULATION
1128c2ecf20Sopenharmony_ci	if (task->thread_info.status & TS_COMPAT) {
1138c2ecf20Sopenharmony_ci		*args++ = regs->bx;
1148c2ecf20Sopenharmony_ci		*args++ = regs->cx;
1158c2ecf20Sopenharmony_ci		*args++ = regs->dx;
1168c2ecf20Sopenharmony_ci		*args++ = regs->si;
1178c2ecf20Sopenharmony_ci		*args++ = regs->di;
1188c2ecf20Sopenharmony_ci		*args   = regs->bp;
1198c2ecf20Sopenharmony_ci	} else
1208c2ecf20Sopenharmony_ci# endif
1218c2ecf20Sopenharmony_ci	{
1228c2ecf20Sopenharmony_ci		*args++ = regs->di;
1238c2ecf20Sopenharmony_ci		*args++ = regs->si;
1248c2ecf20Sopenharmony_ci		*args++ = regs->dx;
1258c2ecf20Sopenharmony_ci		*args++ = regs->r10;
1268c2ecf20Sopenharmony_ci		*args++ = regs->r8;
1278c2ecf20Sopenharmony_ci		*args   = regs->r9;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline void syscall_set_arguments(struct task_struct *task,
1328c2ecf20Sopenharmony_ci					 struct pt_regs *regs,
1338c2ecf20Sopenharmony_ci					 const unsigned long *args)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci# ifdef CONFIG_IA32_EMULATION
1368c2ecf20Sopenharmony_ci	if (task->thread_info.status & TS_COMPAT) {
1378c2ecf20Sopenharmony_ci		regs->bx = *args++;
1388c2ecf20Sopenharmony_ci		regs->cx = *args++;
1398c2ecf20Sopenharmony_ci		regs->dx = *args++;
1408c2ecf20Sopenharmony_ci		regs->si = *args++;
1418c2ecf20Sopenharmony_ci		regs->di = *args++;
1428c2ecf20Sopenharmony_ci		regs->bp = *args;
1438c2ecf20Sopenharmony_ci	} else
1448c2ecf20Sopenharmony_ci# endif
1458c2ecf20Sopenharmony_ci	{
1468c2ecf20Sopenharmony_ci		regs->di = *args++;
1478c2ecf20Sopenharmony_ci		regs->si = *args++;
1488c2ecf20Sopenharmony_ci		regs->dx = *args++;
1498c2ecf20Sopenharmony_ci		regs->r10 = *args++;
1508c2ecf20Sopenharmony_ci		regs->r8 = *args++;
1518c2ecf20Sopenharmony_ci		regs->r9 = *args;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic inline int syscall_get_arch(struct task_struct *task)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	/* x32 tasks should be considered AUDIT_ARCH_X86_64. */
1588c2ecf20Sopenharmony_ci	return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
1598c2ecf20Sopenharmony_ci		task->thread_info.status & TS_COMPAT)
1608c2ecf20Sopenharmony_ci		? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_civoid do_syscall_64(unsigned long nr, struct pt_regs *regs);
1648c2ecf20Sopenharmony_civoid do_int80_syscall_32(struct pt_regs *regs);
1658c2ecf20Sopenharmony_cilong do_fast_syscall_32(struct pt_regs *regs);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#endif	/* CONFIG_X86_32 */
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#endif	/* _ASM_X86_SYSCALL_H */
170