1 /* ===-- clear_cache.c - Implement __clear_cache ---------------------------===
3 * The LLVM Compiler Infrastructure
5 * This file is dual licensed under the MIT and the University of Illinois Open
6 * Source Licenses. See LICENSE.TXT for details.
8 * ===----------------------------------------------------------------------===
14 #include <libkern/OSCacheControl.h>
16 #if defined(__NetBSD__) && defined(__arm__)
17 #include <machine/sysarch.h>
20 #if defined(ANDROID) && defined(__mips__)
21 #include <sys/cachectl.h>
24 #if defined(ANDROID) && defined(__arm__)
25 #include <asm/unistd.h>
29 * The compiler generates calls to __clear_cache() when creating
30 * trampoline functions on the stack for use with nested functions.
31 * It is expected to invalidate the instruction cache for the
36 __clear_cache(void* start
, void* end
)
38 #if __i386__ || __x86_64__
40 * Intel processors have a unified instruction and data cache
41 * so there is nothing to do
43 #elif defined(__arm__) && !defined(__APPLE__)
44 #if defined(__NetBSD__)
45 struct arm_sync_icache_args arg
;
47 arg
.addr
= (uintptr_t)start
;
48 arg
.len
= (uintptr_t)end
- (uintptr_t)start
;
50 sysarch(ARM_SYNC_ICACHE
, &arg
);
51 #elif defined(ANDROID)
52 const register int start_reg
__asm("r0") = (int) (intptr_t) start
;
53 const register int end_reg
__asm("r1") = (int) (intptr_t) end
;
54 const register int flags
__asm("r2") = 0;
55 const register int syscall_nr
__asm("r7") = __ARM_NR_cacheflush
;
56 __asm
__volatile("svc 0x0" : "=r"(start_reg
)
57 : "r"(syscall_nr
), "r"(start_reg
), "r"(end_reg
), "r"(flags
) : "r0");
64 #elif defined(ANDROID) && defined(__mips__)
65 const uintptr_t start_int
= (uintptr_t) start
;
66 const uintptr_t end_int
= (uintptr_t) end
;
67 _flush_cache(start
, (end_int
- start_int
), BCACHE
);
68 #elif defined(__aarch64__) && !defined(__APPLE__)
69 uint64_t xstart
= (uint64_t)(uintptr_t) start
;
70 uint64_t xend
= (uint64_t)(uintptr_t) end
;
72 // Get Cache Type Info
74 __asm
__volatile("mrs %0, ctr_el0" : "=r"(ctr_el0
));
77 * dc & ic instructions must use 64bit registers so we don't use
78 * uintptr_t in case this runs in an IPL32 environment.
80 const size_t dcache_line_size
= 4 << ((ctr_el0
>> 16) & 15);
81 for (uint64_t addr
= xstart
; addr
< xend
; addr
+= dcache_line_size
)
82 __asm
__volatile("dc cvau, %0" :: "r"(addr
));
83 __asm
__volatile("dsb ish");
85 const size_t icache_line_size
= 4 << ((ctr_el0
>> 0) & 15);
86 for (uint64_t addr
= xstart
; addr
< xend
; addr
+= icache_line_size
)
87 __asm
__volatile("ic ivau, %0" :: "r"(addr
));
88 __asm
__volatile("isb sy");
91 /* On Darwin, sys_icache_invalidate() provides this functionality */
92 sys_icache_invalidate(start
, end
-start
);