1 /* RISC-V instruction cache flushing VDSO calls
2    Copyright (C) 2017-2020 Free Software Foundation, Inc.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 2.1 of the
7    License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <stdlib.h>
19 #include <atomic.h>
20 #include <sys/syscall.h>
21 
22 #ifndef __NR_riscv_flush_icache
23 #define __NR_riscv_flush_icache 259
24 #endif
25 
26 typedef int (*func_type) (void *, void *, unsigned long int);
27 
28 static int
__riscv_flush_icache_syscall(void * start,void * end,unsigned long int flags)29 __riscv_flush_icache_syscall (void *start, void *end, unsigned long int flags)
30 {
31   return INLINE_SYSCALL (riscv_flush_icache, 3, start, end, flags);
32 }
33 
34 static func_type
__lookup_riscv_flush_icache(void)35 __lookup_riscv_flush_icache (void)
36 {
37   /* always call the system call directly.*/
38   return &__riscv_flush_icache_syscall;
39 }
40 
41 int
__riscv_flush_icache(void * start,void * end,unsigned long int flags)42 __riscv_flush_icache (void *start, void *end, unsigned long int flags)
43 {
44   static volatile func_type cached_func;
45 
46   func_type func = atomic_load_relaxed (&cached_func);
47 
48   if (!func)
49     {
50       func = __lookup_riscv_flush_icache ();
51       atomic_store_relaxed (&cached_func, func);
52     }
53 
54   return func (start, end, flags);
55 }
56