GNU Linux-libre 4.19.245-gnu1
[releases.git] / arch / riscv / kernel / sys_riscv.c
1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  * Copyright (C) 2014 Darius Rad <darius@bluespec.com>
4  * Copyright (C) 2017 SiFive
5  *
6  *   This program is free software; you can redistribute it and/or
7  *   modify it under the terms of the GNU General Public License
8  *   as published by the Free Software Foundation, version 2.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  */
15
16 #include <linux/syscalls.h>
17 #include <asm/unistd.h>
18 #include <asm/cacheflush.h>
19 #include <asm-generic/mman-common.h>
20
21 static long riscv_sys_mmap(unsigned long addr, unsigned long len,
22                            unsigned long prot, unsigned long flags,
23                            unsigned long fd, off_t offset,
24                            unsigned long page_shift_offset)
25 {
26         if (unlikely(offset & (~PAGE_MASK >> page_shift_offset)))
27                 return -EINVAL;
28
29         if ((prot & PROT_WRITE) && (prot & PROT_EXEC))
30                 if (unlikely(!(prot & PROT_READ)))
31                         return -EINVAL;
32
33         return ksys_mmap_pgoff(addr, len, prot, flags, fd,
34                                offset >> (PAGE_SHIFT - page_shift_offset));
35 }
36
37 #ifdef CONFIG_64BIT
38 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
39         unsigned long, prot, unsigned long, flags,
40         unsigned long, fd, off_t, offset)
41 {
42         return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 0);
43 }
44 #else
45 SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
46         unsigned long, prot, unsigned long, flags,
47         unsigned long, fd, off_t, offset)
48 {
49         /*
50          * Note that the shift for mmap2 is constant (12),
51          * regardless of PAGE_SIZE
52          */
53         return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
54 }
55 #endif /* !CONFIG_64BIT */
56
57 /*
58  * Allows the instruction cache to be flushed from userspace.  Despite RISC-V
59  * having a direct 'fence.i' instruction available to userspace (which we
60  * can't trap!), that's not actually viable when running on Linux because the
61  * kernel might schedule a process on another hart.  There is no way for
62  * userspace to handle this without invoking the kernel (as it doesn't know the
63  * thread->hart mappings), so we've defined a RISC-V specific system call to
64  * flush the instruction cache.
65  *
66  * sys_riscv_flush_icache() is defined to flush the instruction cache over an
67  * address range, with the flush applying to either all threads or just the
68  * caller.  We don't currently do anything with the address range, that's just
69  * in there for forwards compatibility.
70  */
71 SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
72         uintptr_t, flags)
73 {
74         /* Check the reserved flags. */
75         if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
76                 return -EINVAL;
77
78         flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
79
80         return 0;
81 }