]>
git.proxmox.com Git - rustc.git/blob - vendor/compiler_builtins/compiler-rt/lib/builtins/clear_cache.c
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 * ===----------------------------------------------------------------------===
16 #include <libkern/OSCacheControl.h>
20 /* Forward declare Win32 APIs since the GCC mode driver does not handle the
21 newer SDKs as well as needed. */
22 uint32_t FlushInstructionCache(uintptr_t hProcess
, void *lpBaseAddress
,
24 uintptr_t GetCurrentProcess(void);
27 #if defined(__FreeBSD__) && defined(__arm__)
28 #include <sys/types.h>
29 #include <machine/sysarch.h>
32 #if defined(__NetBSD__) && defined(__arm__)
33 #include <machine/sysarch.h>
36 #if defined(__OpenBSD__) && defined(__mips__)
37 #include <sys/types.h>
38 #include <machine/sysarch.h>
41 #if defined(__linux__) && defined(__mips__)
42 #include <sys/cachectl.h>
43 #include <sys/syscall.h>
45 #if defined(__ANDROID__) && defined(__LP64__)
47 * clear_mips_cache - Invalidates instruction cache for Mips.
49 static void clear_mips_cache(const void* Addr
, size_t Size
) {
54 "beq %[Size], $zero, 20f\n" /* If size == 0, branch around. */
56 "daddu %[Size], %[Addr], %[Size]\n" /* Calculate end address + 1 */
57 "rdhwr $v0, $1\n" /* Get step size for SYNCI.
58 $1 is $HW_SYNCI_Step */
59 "beq $v0, $zero, 20f\n" /* If no caches require
60 synchronization, branch
64 "synci 0(%[Addr])\n" /* Synchronize all caches around
66 "daddu %[Addr], %[Addr], $v0\n" /* Add step size. */
67 "sltu $at, %[Addr], %[Size]\n" /* Compare current with end
69 "bne $at, $zero, 10b\n" /* Branch if more to do. */
71 "sync\n" /* Clear memory hazards. */
76 "daddiu $ra, $ra, 12\n" /* $ra has a value of $pc here.
77 Add offset of 12 to point to the
78 instruction after the last nop.
80 "jr.hb $ra\n" /* Return, clearing instruction
84 : [Addr
] "+r"(Addr
), [Size
] "+r"(Size
)
85 :: "at", "ra", "v0", "memory"
92 * The compiler generates calls to __clear_cache() when creating
93 * trampoline functions on the stack for use with nested functions.
94 * It is expected to invalidate the instruction cache for the
98 void __clear_cache(void *start
, void *end
) {
99 #if __i386__ || __x86_64__ || defined(_M_IX86) || defined(_M_X64)
101 * Intel processors have a unified instruction and data cache
102 * so there is nothing to do
104 #elif defined(_WIN32) && (defined(__arm__) || defined(__aarch64__))
105 FlushInstructionCache(GetCurrentProcess(), start
, end
- start
);
106 #elif defined(__arm__) && !defined(__APPLE__)
107 #if defined(__FreeBSD__) || defined(__NetBSD__)
108 struct arm_sync_icache_args arg
;
110 arg
.addr
= (uintptr_t)start
;
111 arg
.len
= (uintptr_t)end
- (uintptr_t)start
;
113 sysarch(ARM_SYNC_ICACHE
, &arg
);
114 #elif defined(__linux__)
116 * We used to include asm/unistd.h for the __ARM_NR_cacheflush define, but
117 * it also brought many other unused defines, as well as a dependency on
118 * kernel headers to be installed.
120 * This value is stable at least since Linux 3.13 and should remain so for
121 * compatibility reasons, warranting it's re-definition here.
123 #define __ARM_NR_cacheflush 0x0f0002
124 register int start_reg
__asm("r0") = (int) (intptr_t) start
;
125 const register int end_reg
__asm("r1") = (int) (intptr_t) end
;
126 const register int flags
__asm("r2") = 0;
127 const register int syscall_nr
__asm("r7") = __ARM_NR_cacheflush
;
128 __asm
__volatile("svc 0x0"
130 : "r"(syscall_nr
), "r"(start_reg
), "r"(end_reg
),
132 assert(start_reg
== 0 && "Cache flush syscall failed.");
136 #elif defined(__linux__) && defined(__mips__)
137 const uintptr_t start_int
= (uintptr_t) start
;
138 const uintptr_t end_int
= (uintptr_t) end
;
139 #if defined(__ANDROID__) && defined(__LP64__)
140 // Call synci implementation for short address range.
141 const uintptr_t address_range_limit
= 256;
142 if ((end_int
- start_int
) <= address_range_limit
) {
143 clear_mips_cache(start
, (end_int
- start_int
));
145 syscall(__NR_cacheflush
, start
, (end_int
- start_int
), BCACHE
);
148 syscall(__NR_cacheflush
, start
, (end_int
- start_int
), BCACHE
);
150 #elif defined(__mips__) && defined(__OpenBSD__)
151 cacheflush(start
, (uintptr_t)end
- (uintptr_t)start
, BCACHE
);
152 #elif defined(__aarch64__) && !defined(__APPLE__)
153 uint64_t xstart
= (uint64_t)(uintptr_t) start
;
154 uint64_t xend
= (uint64_t)(uintptr_t) end
;
157 // Get Cache Type Info
159 __asm
__volatile("mrs %0, ctr_el0" : "=r"(ctr_el0
));
162 * dc & ic instructions must use 64bit registers so we don't use
163 * uintptr_t in case this runs in an IPL32 environment.
165 const size_t dcache_line_size
= 4 << ((ctr_el0
>> 16) & 15);
166 for (addr
= xstart
& ~(dcache_line_size
- 1); addr
< xend
;
167 addr
+= dcache_line_size
)
168 __asm
__volatile("dc cvau, %0" :: "r"(addr
));
169 __asm
__volatile("dsb ish");
171 const size_t icache_line_size
= 4 << ((ctr_el0
>> 0) & 15);
172 for (addr
= xstart
& ~(icache_line_size
- 1); addr
< xend
;
173 addr
+= icache_line_size
)
174 __asm
__volatile("ic ivau, %0" :: "r"(addr
));
175 __asm
__volatile("isb sy");
176 #elif defined (__powerpc64__)
177 const size_t line_size
= 32;
178 const size_t len
= (uintptr_t)end
- (uintptr_t)start
;
180 const uintptr_t mask
= ~(line_size
- 1);
181 const uintptr_t start_line
= ((uintptr_t)start
) & mask
;
182 const uintptr_t end_line
= ((uintptr_t)start
+ len
+ line_size
- 1) & mask
;
184 for (uintptr_t line
= start_line
; line
< end_line
; line
+= line_size
)
185 __asm__
volatile("dcbf 0, %0" : : "r"(line
));
186 __asm__
volatile("sync");
188 for (uintptr_t line
= start_line
; line
< end_line
; line
+= line_size
)
189 __asm__
volatile("icbi 0, %0" : : "r"(line
));
190 __asm__
volatile("isync");
193 /* On Darwin, sys_icache_invalidate() provides this functionality */
194 sys_icache_invalidate(start
, end
-start
);