1 /* RISC-V instruction cache flushing VDSO calls
2    Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 
4    This file is part of the GNU C Library.
5 
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of the
9    License, or (at your option) any later version.
10 
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <https://www.gnu.org/licenses/>.  */
19 
20 #include <dl-vdso.h>
21 #include <stdlib.h>
22 #include <atomic.h>
23 #include <sys/cachectl.h>
24 #if __has_include (<asm/syscalls.h>)
25 # include <asm/syscalls.h>
26 #else
27 # include <asm/unistd.h>
28 #endif
29 #include <sys/syscall.h>
30 
31 typedef int (*func_type) (void *, void *, unsigned long int);
32 
33 static int
__riscv_flush_icache_syscall(void * start,void * end,unsigned long int flags)34 __riscv_flush_icache_syscall (void *start, void *end, unsigned long int flags)
35 {
36   return INLINE_SYSCALL (riscv_flush_icache, 3, start, end, flags);
37 }
38 
39 static func_type
__lookup_riscv_flush_icache(void)40 __lookup_riscv_flush_icache (void)
41 {
42   func_type func = dl_vdso_vsym ("__vdso_flush_icache");
43 
44   /* If there is no vDSO entry then call the system call directly.  All Linux
45      versions provide the vDSO entry, but QEMU's user-mode emulation doesn't
46      provide a vDSO.  */
47   if (!func)
48     func = &__riscv_flush_icache_syscall;
49 
50   return func;
51 }
52 
53 #ifdef SHARED
54 
55 # define INIT_ARCH()
56 libc_ifunc (__riscv_flush_icache, __lookup_riscv_flush_icache ())
57 
58 #else
59 
60 int
61 __riscv_flush_icache (void *start, void *end, unsigned long int flags)
62 {
63   static volatile func_type cached_func;
64 
65   func_type func = atomic_load_relaxed (&cached_func);
66 
67   if (!func)
68     {
69       func = __lookup_riscv_flush_icache ();
70       atomic_store_relaxed (&cached_func, func);
71     }
72 
73   return func (start, end, flags);
74 }
75 
76 #endif
77