Merge branch 'for-5.14/intel-ish' into for-linus
[linux-2.6-microblaze.git] / arch / s390 / kernel / syscall.c
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
47 struct 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
56 SYSCALL_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);
64 out:
65         return error;
66 }
67
68 #ifdef CONFIG_SYSVIPC
69 /*
70  * sys_ipc() is the de-multiplexer for the SysV IPC calls.
71  */
72 SYSCALL_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
89 SYSCALL_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
106 SYSCALL_DEFINE0(ni_syscall)
107 {
108         return -ENOSYS;
109 }
110
111 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         nr = syscall_enter_from_user_mode_work(regs, nr);
125
126         /*
127          * In the s390 ptrace ABI, both the syscall number and the return value
128          * use gpr2. However, userspace puts the syscall number either in the
129          * svc instruction itself, or uses gpr1. To make at least skipping syscalls
130          * work, the ptrace code sets PIF_SYSCALL_RET_SET, which is checked here
131          * and if set, the syscall will be skipped.
132          */
133         if (!test_pt_regs_flag(regs, PIF_SYSCALL_RET_SET)) {
134                 regs->gprs[2] = -ENOSYS;
135                 if (likely(nr < NR_syscalls))
136                         regs->gprs[2] = current->thread.sys_call_table[nr](regs);
137         } else {
138                 clear_pt_regs_flag(regs, PIF_SYSCALL_RET_SET);
139         }
140         syscall_exit_to_user_mode_work(regs);
141 }
142
143 void noinstr __do_syscall(struct pt_regs *regs, int per_trap)
144 {
145         add_random_kstack_offset();
146         enter_from_user_mode(regs);
147
148         memcpy(&regs->gprs[8], S390_lowcore.save_area_sync, 8 * sizeof(unsigned long));
149         memcpy(&regs->int_code, &S390_lowcore.svc_ilc, sizeof(regs->int_code));
150         regs->psw = S390_lowcore.svc_old_psw;
151
152         update_timer_sys();
153
154         local_irq_enable();
155         regs->orig_gpr2 = regs->gprs[2];
156
157         if (per_trap)
158                 set_thread_flag(TIF_PER_TRAP);
159
160         for (;;) {
161                 regs->flags = 0;
162                 set_pt_regs_flag(regs, PIF_SYSCALL);
163                 do_syscall(regs);
164                 if (!test_pt_regs_flag(regs, PIF_SYSCALL_RESTART))
165                         break;
166                 local_irq_enable();
167         }
168         exit_to_user_mode();
169 }