cache.c 1.6 KB

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