flush-icache.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* RISC-V instruction cache flushing VDSO calls
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <dl-vdso.h>
  16. #include <stdlib.h>
  17. #include <atomic.h>
  18. #include <sys/cachectl.h>
  19. #if __has_include__ (<asm/syscalls.h>)
  20. # include <asm/syscalls.h>
  21. #else
  22. # include <asm/unistd.h>
  23. #endif
  24. typedef int (*func_type) (void *, void *, unsigned long int);
  25. static int
  26. __riscv_flush_icache_syscall (void *start, void *end, unsigned long int flags)
  27. {
  28. return INLINE_SYSCALL (riscv_flush_icache, 3, start, end, flags);
  29. }
  30. static func_type
  31. __lookup_riscv_flush_icache (void)
  32. {
  33. PREPARE_VERSION_KNOWN (linux_version, LINUX_4_15);
  34. func_type func = _dl_vdso_vsym ("__vdso_flush_icache", &linux_version);
  35. /* If there is no vDSO entry then call the system call directly. All Linux
  36. versions provide the vDSO entry, but QEMU's user-mode emulation doesn't
  37. provide a vDSO. */
  38. if (!func)
  39. func = &__riscv_flush_icache_syscall;
  40. return func;
  41. }
  42. #ifdef SHARED
  43. # define INIT_ARCH()
  44. libc_ifunc (__riscv_flush_icache, __lookup_riscv_flush_icache ())
  45. #else
  46. int
  47. __riscv_flush_icache (void *start, void *end, unsigned long int flags)
  48. {
  49. static volatile func_type cached_func;
  50. func_type func = atomic_load_relaxed (&cached_func);
  51. if (!func)
  52. {
  53. func = __lookup_riscv_flush_icache ();
  54. atomic_store_relaxed (&cached_func, func);
  55. }
  56. return func (start, end, flags);
  57. }
  58. #endif