1// SPDX-License-Identifier: GPL-2.0
2/*
3 *  S390 version
4 *    Copyright IBM Corp. 1999, 2000
5 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
6 *               Thomas Spatzier (tspat@de.ibm.com)
7 *
8 *  Derived from "arch/i386/kernel/sys_i386.c"
9 *
10 *  This file contains various random system calls that
11 *  have a non-standard calling sequence on the Linux/s390
12 *  platform.
13 */
14
15#include <linux/errno.h>
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/fs.h>
19#include <linux/smp.h>
20#include <linux/sem.h>
21#include <linux/msg.h>
22#include <linux/shm.h>
23#include <linux/stat.h>
24#include <linux/syscalls.h>
25#include <linux/mman.h>
26#include <linux/file.h>
27#include <linux/utsname.h>
28#include <linux/personality.h>
29#include <linux/unistd.h>
30#include <linux/ipc.h>
31#include <linux/uaccess.h>
32#include <linux/string.h>
33#include <linux/thread_info.h>
34#include <linux/entry-common.h>
35
36#include <asm/ptrace.h>
37#include <asm/vtime.h>
38
39#include "entry.h"
40
41/*
42 * Perform the mmap() system call. Linux for S/390 isn't able to handle more
43 * than 5 system call parameters, so this system call uses a memory block
44 * for parameter passing.
45 */
46
47struct s390_mmap_arg_struct {
48	unsigned long addr;
49	unsigned long len;
50	unsigned long prot;
51	unsigned long flags;
52	unsigned long fd;
53	unsigned long offset;
54};
55
56SYSCALL_DEFINE1(mmap2, struct s390_mmap_arg_struct __user *, arg)
57{
58	struct s390_mmap_arg_struct a;
59	int error = -EFAULT;
60
61	if (copy_from_user(&a, arg, sizeof(a)))
62		goto out;
63	error = ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd, a.offset);
64out:
65	return error;
66}
67
68#ifdef CONFIG_SYSVIPC
69/*
70 * sys_ipc() is the de-multiplexer for the SysV IPC calls.
71 */
72SYSCALL_DEFINE5(s390_ipc, uint, call, int, first, unsigned long, second,
73		unsigned long, third, void __user *, ptr)
74{
75	if (call >> 16)
76		return -EINVAL;
77	/* The s390 sys_ipc variant has only five parameters instead of six
78	 * like the generic variant. The only difference is the handling of
79	 * the SEMTIMEDOP subcall where on s390 the third parameter is used
80	 * as a pointer to a struct timespec where the generic variant uses
81	 * the fifth parameter.
82	 * Therefore we can call the generic variant by simply passing the
83	 * third parameter also as fifth parameter.
84	 */
85	return ksys_ipc(call, first, second, third, ptr, third);
86}
87#endif /* CONFIG_SYSVIPC */
88
89SYSCALL_DEFINE1(s390_personality, unsigned int, personality)
90{
91	unsigned int ret = current->personality;
92
93	if (personality(current->personality) == PER_LINUX32 &&
94	    personality(personality) == PER_LINUX)
95		personality |= PER_LINUX32;
96
97	if (personality != 0xffffffff)
98		set_personality(personality);
99
100	if (personality(ret) == PER_LINUX32)
101		ret &= ~PER_LINUX32;
102
103	return ret;
104}
105
106SYSCALL_DEFINE0(ni_syscall)
107{
108	return -ENOSYS;
109}
110
111static void do_syscall(struct pt_regs *regs)
112{
113	unsigned long nr;
114
115	nr = regs->int_code & 0xffff;
116	if (!nr) {
117		nr = regs->gprs[1] & 0xffff;
118		regs->int_code &= ~0xffffUL;
119		regs->int_code |= nr;
120	}
121
122	regs->gprs[2] = nr;
123
124	if (nr == __NR_restart_syscall && !(current->restart_block.arch_data & 1)) {
125		regs->psw.addr = current->restart_block.arch_data;
126		current->restart_block.arch_data = 1;
127	}
128	nr = syscall_enter_from_user_mode_work(regs, nr);
129
130	/*
131	 * In the s390 ptrace ABI, both the syscall number and the return value
132	 * use gpr2. However, userspace puts the syscall number either in the
133	 * svc instruction itself, or uses gpr1. To make at least skipping syscalls
134	 * work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
135	 * and if set, the syscall will be skipped.
136	 */
137
138	if (unlikely(test_and_clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET)))
139		goto out;
140	regs->gprs[2] = -ENOSYS;
141	if (likely(nr >= NR_syscalls))
142		goto out;
143	do {
144		regs->gprs[2] = current->thread.sys_call_table[nr](regs);
145	} while (test_and_clear_pt_regs_flag(regs, PIF_EXECVE_PGSTE_RESTART));
146out:
147	syscall_exit_to_user_mode_work(regs);
148}
149
150void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
151{
152	add_random_kstack_offset();
153	enter_from_user_mode(regs);
154	regs->psw = S390_lowcore.svc_old_psw;
155	regs->int_code = S390_lowcore.svc_int_code;
156	update_timer_sys();
157	if (static_branch_likely(&cpu_has_bear))
158		current->thread.last_break = regs->last_break;
159
160	local_irq_enable();
161	regs->orig_gpr2 = regs->gprs[2];
162
163	if (per_trap)
164		set_thread_flag(TIF_PER_TRAP);
165
166	regs->flags = 0;
167	set_pt_regs_flag(regs, PIF_SYSCALL);
168	do_syscall(regs);
169	exit_to_user_mode();
170}
171