clear_cache.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* ===-- clear_cache.c - Implement __clear_cache ---------------------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is dual licensed under the MIT and the University of Illinois Open
  6. * Source Licenses. See LICENSE.TXT for details.
  7. *
  8. * ===----------------------------------------------------------------------===
  9. */
  10. #include "int_lib.h"
  11. #include <stddef.h>
  12. #if __APPLE__
  13. #include <libkern/OSCacheControl.h>
  14. #endif
  15. #if (defined(__FreeBSD__) || defined(__Bitrig__)) && defined(__arm__)
  16. #include <sys/types.h>
  17. #include <machine/sysarch.h>
  18. #endif
  19. #if defined(__NetBSD__) && defined(__arm__)
  20. #include <machine/sysarch.h>
  21. #endif
  22. #if defined(__mips__)
  23. #include <sys/cachectl.h>
  24. #include <sys/syscall.h>
  25. #include <unistd.h>
  26. #if defined(__ANDROID__) && defined(__LP64__)
  27. /*
  28. * clear_mips_cache - Invalidates instruction cache for Mips.
  29. */
  30. static void clear_mips_cache(const void* Addr, size_t Size) {
  31. asm volatile (
  32. ".set push\n"
  33. ".set noreorder\n"
  34. ".set noat\n"
  35. "beq %[Size], $zero, 20f\n" /* If size == 0, branch around. */
  36. "nop\n"
  37. "daddu %[Size], %[Addr], %[Size]\n" /* Calculate end address + 1 */
  38. "rdhwr $v0, $1\n" /* Get step size for SYNCI.
  39. $1 is $HW_SYNCI_Step */
  40. "beq $v0, $zero, 20f\n" /* If no caches require
  41. synchronization, branch
  42. around. */
  43. "nop\n"
  44. "10:\n"
  45. "synci 0(%[Addr])\n" /* Synchronize all caches around
  46. address. */
  47. "daddu %[Addr], %[Addr], $v0\n" /* Add step size. */
  48. "sltu $at, %[Addr], %[Size]\n" /* Compare current with end
  49. address. */
  50. "bne $at, $zero, 10b\n" /* Branch if more to do. */
  51. "nop\n"
  52. "sync\n" /* Clear memory hazards. */
  53. "20:\n"
  54. "bal 30f\n"
  55. "nop\n"
  56. "30:\n"
  57. "daddiu $ra, $ra, 12\n" /* $ra has a value of $pc here.
  58. Add offset of 12 to point to the
  59. instruction after the last nop.
  60. */
  61. "jr.hb $ra\n" /* Return, clearing instruction
  62. hazards. */
  63. "nop\n"
  64. ".set pop\n"
  65. : [Addr] "+r"(Addr), [Size] "+r"(Size)
  66. :: "at", "ra", "v0", "memory"
  67. );
  68. }
  69. #endif
  70. #endif
  71. #if defined(__ANDROID__) && defined(__arm__)
  72. #include <asm/unistd.h>
  73. #endif
  74. /*
  75. * The compiler generates calls to __clear_cache() when creating
  76. * trampoline functions on the stack for use with nested functions.
  77. * It is expected to invalidate the instruction cache for the
  78. * specified range.
  79. */
  80. void __clear_cache(void *start, void *end) {
  81. #if __i386__ || __x86_64__
  82. /*
  83. * Intel processors have a unified instruction and data cache
  84. * so there is nothing to do
  85. */
  86. #elif defined(__arm__) && !defined(__APPLE__)
  87. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__Bitrig__)
  88. struct arm_sync_icache_args arg;
  89. arg.addr = (uintptr_t)start;
  90. arg.len = (uintptr_t)end - (uintptr_t)start;
  91. sysarch(ARM_SYNC_ICACHE, &arg);
  92. #elif defined(__ANDROID__)
  93. int start_reg __asm("r0") = (int) (intptr_t) start;
  94. const register int end_reg __asm("r1") = (int) (intptr_t) end;
  95. const register int flags __asm("r2") = 0;
  96. const register int syscall_nr __asm("r7") = __ARM_NR_cacheflush;
  97. __asm __volatile("svc 0x0" : "=r"(start_reg)
  98. : "r"(syscall_nr), "r"(start_reg), "r"(end_reg), "r"(flags) : "r0");
  99. if (start_reg != 0) {
  100. compilerrt_abort();
  101. }
  102. #else
  103. compilerrt_abort();
  104. #endif
  105. #elif defined(__mips__)
  106. const uintptr_t start_int = (uintptr_t) start;
  107. const uintptr_t end_int = (uintptr_t) end;
  108. #if defined(__ANDROID__) && defined(__LP64__)
  109. // Call synci implementation for short address range.
  110. const uintptr_t address_range_limit = 256;
  111. if ((end_int - start_int) <= address_range_limit) {
  112. clear_mips_cache(start, (end_int - start_int));
  113. } else {
  114. syscall(__NR_cacheflush, start, (end_int - start_int), BCACHE);
  115. }
  116. #else
  117. syscall(__NR_cacheflush, start, (end_int - start_int), BCACHE);
  118. #endif
  119. #elif defined(__aarch64__) && !defined(__APPLE__)
  120. uint64_t xstart = (uint64_t)(uintptr_t) start;
  121. uint64_t xend = (uint64_t)(uintptr_t) end;
  122. uint64_t addr;
  123. // Get Cache Type Info
  124. uint64_t ctr_el0;
  125. __asm __volatile("mrs %0, ctr_el0" : "=r"(ctr_el0));
  126. /*
  127. * dc & ic instructions must use 64bit registers so we don't use
  128. * uintptr_t in case this runs in an IPL32 environment.
  129. */
  130. const size_t dcache_line_size = 4 << ((ctr_el0 >> 16) & 15);
  131. for (addr = xstart; addr < xend; addr += dcache_line_size)
  132. __asm __volatile("dc cvau, %0" :: "r"(addr));
  133. __asm __volatile("dsb ish");
  134. const size_t icache_line_size = 4 << ((ctr_el0 >> 0) & 15);
  135. for (addr = xstart; addr < xend; addr += icache_line_size)
  136. __asm __volatile("ic ivau, %0" :: "r"(addr));
  137. __asm __volatile("isb sy");
  138. #else
  139. #if __APPLE__
  140. /* On Darwin, sys_icache_invalidate() provides this functionality */
  141. sys_icache_invalidate(start, end-start);
  142. #else
  143. compilerrt_abort();
  144. #endif
  145. #endif
  146. }