xref: /linux/arch/loongarch/kernel/syscall.c (revision 7a5f1cd22d47f8ca4b760b6334378ae42c1bd24b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Author: Hanlu Li <lihanlu@loongson.cn>
4  *         Huacai Chen <chenhuacai@loongson.cn>
5  *
6  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7  */
8 #include <linux/capability.h>
9 #include <linux/entry-common.h>
10 #include <linux/errno.h>
11 #include <linux/linkage.h>
12 #include <linux/nospec.h>
13 #include <linux/objtool.h>
14 #include <linux/randomize_kstack.h>
15 #include <linux/syscalls.h>
16 #include <linux/unistd.h>
17 
18 #include <asm/asm.h>
19 #include <asm/exception.h>
20 #include <asm/loongarch.h>
21 #include <asm/signal.h>
22 #include <asm/switch_to.h>
23 #include <asm-generic/syscalls.h>
24 
25 #undef __SYSCALL
26 #define __SYSCALL(nr, call)	[nr] = (call),
27 #define __SYSCALL_WITH_COMPAT(nr, native, compat) __SYSCALL(nr, native)
28 
29 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len, unsigned long,
30 		prot, unsigned long, flags, unsigned long, fd, unsigned long, offset)
31 {
32 	if (offset & ~PAGE_MASK)
33 		return -EINVAL;
34 
35 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >> PAGE_SHIFT);
36 }
37 
38 SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len, unsigned long,
39 		 prot, unsigned long, flags, unsigned long, fd, unsigned long, offset)
40 {
41 	if (offset & (~PAGE_MASK >> 12))
42 		return -EINVAL;
43 
44 	return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >> (PAGE_SHIFT - 12));
45 }
46 
47 void *sys_call_table[__NR_syscalls] = {
48 	[0 ... __NR_syscalls - 1] = sys_ni_syscall,
49 #ifdef CONFIG_32BIT
50 #include <asm/syscall_table_32.h>
51 #else
52 #include <asm/syscall_table_64.h>
53 #endif
54 };
55 
56 typedef long (*sys_call_fn)(unsigned long, unsigned long,
57 	unsigned long, unsigned long, unsigned long, unsigned long);
58 
59 void noinstr __no_stack_protector do_syscall(struct pt_regs *regs)
60 {
61 	unsigned long nr;
62 	sys_call_fn syscall_fn;
63 
64 	nr = regs->regs[11];
65 	/* Set for syscall restarting */
66 	if (nr < NR_syscalls)
67 		regs->regs[0] = nr + 1;
68 
69 	regs->csr_era += 4;
70 	regs->orig_a0 = regs->regs[4];
71 	regs->regs[4] = -ENOSYS;
72 
73 	nr = syscall_enter_from_user_mode(regs, nr);
74 
75 	add_random_kstack_offset();
76 
77 	if (nr < NR_syscalls) {
78 		syscall_fn = sys_call_table[array_index_nospec(nr, NR_syscalls)];
79 		regs->regs[4] = syscall_fn(regs->orig_a0, regs->regs[5], regs->regs[6],
80 					   regs->regs[7], regs->regs[8], regs->regs[9]);
81 	}
82 
83 	syscall_exit_to_user_mode(regs);
84 }
85