Skip to content

Commit 921ebd8

Browse files
aswatermanpalmer-dabbelt
authored andcommitted
RISC-V: Allow userspace to flush the instruction cache
Despite RISC-V having a direct 'fence.i' instruction available to userspace (which we can't trap!), that's not actually viable when running on Linux because the kernel might schedule a process on another hart. There is no way for userspace to handle this without invoking the kernel (as it doesn't know the thread->hart mappings), so we've defined a RISC-V specific system call to flush the instruction cache. This patch adds both a system call and a VDSO entry. If possible, we'd like to avoid having the system call be considered part of the user-facing ABI and instead restrict that to the VDSO entry -- both just in general to avoid having additional user-visible ABI to maintain, and because we'd prefer that users just call the VDSO entry because there might be a better way to do this in the future (ie, one that doesn't require entering the kernel). Signed-off-by: Andrew Waterman <andrew@sifive.com> Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
1 parent 08f051e commit 921ebd8

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

arch/riscv/include/asm/cacheflush.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ void flush_icache_mm(struct mm_struct *mm, bool local);
5252

5353
#endif /* CONFIG_SMP */
5454

55+
/*
56+
* Bits in sys_riscv_flush_icache()'s flags argument.
57+
*/
58+
#define SYS_RISCV_FLUSH_ICACHE_LOCAL 1UL
59+
#define SYS_RISCV_FLUSH_ICACHE_ALL (SYS_RISCV_FLUSH_ICACHE_LOCAL)
60+
5561
#endif /* _ASM_RISCV_CACHEFLUSH_H */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2017 SiFive
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#ifndef _ASM_RISCV_VDSO_SYSCALLS_H
18+
#define _ASM_RISCV_VDSO_SYSCALLS_H
19+
20+
#ifdef CONFIG_SMP
21+
22+
/* These syscalls are only used by the vDSO and are not in the uapi. */
23+
#define __NR_riscv_flush_icache (__NR_arch_specific_syscall + 15)
24+
__SYSCALL(__NR_riscv_flush_icache, sys_riscv_flush_icache)
25+
26+
#endif
27+
28+
#endif /* _ASM_RISCV_VDSO_H */

arch/riscv/include/asm/vdso.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ struct vdso_data {
3838
(void __user *)((unsigned long)(base) + __vdso_##name); \
3939
})
4040

41+
#ifdef CONFIG_SMP
42+
asmlinkage long sys_riscv_flush_icache(uintptr_t, uintptr_t, uintptr_t);
43+
#endif
44+
4145
#endif /* _ASM_RISCV_VDSO_H */

arch/riscv/kernel/sys_riscv.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/syscalls.h>
1717
#include <asm/cmpxchg.h>
1818
#include <asm/unistd.h>
19+
#include <asm/cacheflush.h>
1920

2021
static long riscv_sys_mmap(unsigned long addr, unsigned long len,
2122
unsigned long prot, unsigned long flags,
@@ -47,3 +48,34 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
4748
return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
4849
}
4950
#endif /* !CONFIG_64BIT */
51+
52+
#ifdef CONFIG_SMP
53+
/*
54+
* Allows the instruction cache to be flushed from userspace. Despite RISC-V
55+
* having a direct 'fence.i' instruction available to userspace (which we
56+
* can't trap!), that's not actually viable when running on Linux because the
57+
* kernel might schedule a process on another hart. There is no way for
58+
* userspace to handle this without invoking the kernel (as it doesn't know the
59+
* thread->hart mappings), so we've defined a RISC-V specific system call to
60+
* flush the instruction cache.
61+
*
62+
* sys_riscv_flush_icache() is defined to flush the instruction cache over an
63+
* address range, with the flush applying to either all threads or just the
64+
* caller. We don't currently do anything with the address range, that's just
65+
* in there for forwards compatibility.
66+
*/
67+
SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
68+
uintptr_t, flags)
69+
{
70+
struct mm_struct *mm = current->mm;
71+
bool local = (flags & SYS_RISCV_FLUSH_ICACHE_LOCAL) != 0;
72+
73+
/* Check the reserved flags. */
74+
if (unlikely(flags & !SYS_RISCV_FLUSH_ICACHE_ALL))
75+
return -EINVAL;
76+
77+
flush_icache_mm(mm, local);
78+
79+
return 0;
80+
}
81+
#endif

arch/riscv/kernel/syscall_table.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include <linux/linkage.h>
1616
#include <linux/syscalls.h>
1717
#include <asm-generic/syscalls.h>
18+
#include <asm/vdso.h>
1819

1920
#undef __SYSCALL
2021
#define __SYSCALL(nr, call) [nr] = (call),
2122

2223
void *sys_call_table[__NR_syscalls] = {
2324
[0 ... __NR_syscalls - 1] = sys_ni_syscall,
2425
#include <asm/unistd.h>
26+
#include <asm/vdso-syscalls.h>
2527
};

arch/riscv/kernel/vdso/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ vdso-syms += gettimeofday
66
vdso-syms += clock_gettime
77
vdso-syms += clock_getres
88
vdso-syms += getcpu
9+
vdso-syms += flush_icache
910

1011
# Files to link into the vdso
1112
obj-vdso = $(patsubst %, %.o, $(vdso-syms))

arch/riscv/kernel/vdso/flush_icache.S

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2017 SiFive
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, version 2.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
14+
#include <linux/linkage.h>
15+
#include <asm/unistd.h>
16+
#include <asm/vdso-syscalls.h>
17+
18+
.text
19+
/* int __vdso_flush_icache(void *start, void *end, unsigned long flags); */
20+
ENTRY(__vdso_flush_icache)
21+
.cfi_startproc
22+
#ifdef CONFIG_SMP
23+
li a7, __NR_riscv_flush_icache
24+
ecall
25+
#else
26+
fence.i
27+
li a0, 0
28+
#endif
29+
ret
30+
.cfi_endproc
31+
ENDPROC(__vdso_flush_icache)

arch/riscv/kernel/vdso/vdso.lds.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ VERSION
7474
__vdso_clock_gettime;
7575
__vdso_clock_getres;
7676
__vdso_getcpu;
77+
__vdso_flush_icache;
7778
local: *;
7879
};
7980
}

0 commit comments

Comments
 (0)