Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/clear_cache.c
35260 views
//===-- clear_cache.c - Implement __clear_cache ---------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "int_lib.h"9#if defined(__linux__)10#include <assert.h>11#endif12#include <stddef.h>1314#if __APPLE__15#include <libkern/OSCacheControl.h>16#endif1718#if defined(_WIN32)19// Forward declare Win32 APIs since the GCC mode driver does not handle the20// newer SDKs as well as needed.21uint32_t FlushInstructionCache(uintptr_t hProcess, void *lpBaseAddress,22uintptr_t dwSize);23uintptr_t GetCurrentProcess(void);24#endif2526#if defined(__FreeBSD__) && defined(__arm__)27// clang-format off28#include <sys/types.h>29#include <machine/sysarch.h>30// clang-format on31#endif3233#if defined(__NetBSD__) && defined(__arm__)34#include <machine/sysarch.h>35#endif3637#if defined(__OpenBSD__) && (defined(__arm__) || defined(__mips__) || defined(__riscv))38// clang-format off39#include <sys/types.h>40#include <machine/sysarch.h>41// clang-format on42#endif4344#if defined(__linux__) && defined(__mips__)45#include <sys/cachectl.h>46#include <sys/syscall.h>47#include <unistd.h>48#endif4950#if defined(__linux__) && defined(__riscv)51// to get platform-specific syscall definitions52#include <linux/unistd.h>53#endif5455// The compiler generates calls to __clear_cache() when creating56// trampoline functions on the stack for use with nested functions.57// It is expected to invalidate the instruction cache for the58// specified range.5960void __clear_cache(void *start, void *end) {61#if __i386__ || __x86_64__ || defined(_M_IX86) || defined(_M_X64)62// Intel processors have a unified instruction and data cache63// so there is nothing to do64#elif defined(_WIN32) && (defined(__arm__) || defined(__aarch64__))65FlushInstructionCache(GetCurrentProcess(), start, end - start);66#elif defined(__arm__) && !defined(__APPLE__)67#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)68struct arm_sync_icache_args arg;6970arg.addr = (uintptr_t)start;71arg.len = (uintptr_t)end - (uintptr_t)start;7273sysarch(ARM_SYNC_ICACHE, &arg);74#elif defined(__linux__)75// We used to include asm/unistd.h for the __ARM_NR_cacheflush define, but76// it also brought many other unused defines, as well as a dependency on77// kernel headers to be installed.78//79// This value is stable at least since Linux 3.13 and should remain so for80// compatibility reasons, warranting it's re-definition here.81#define __ARM_NR_cacheflush 0x0f000282register int start_reg __asm("r0") = (int)(intptr_t)start;83const register int end_reg __asm("r1") = (int)(intptr_t)end;84const register int flags __asm("r2") = 0;85const register int syscall_nr __asm("r7") = __ARM_NR_cacheflush;86__asm __volatile("svc 0x0"87: "=r"(start_reg)88: "r"(syscall_nr), "r"(start_reg), "r"(end_reg), "r"(flags));89assert(start_reg == 0 && "Cache flush syscall failed.");90#else91compilerrt_abort();92#endif93#elif defined(__linux__) && defined(__loongarch__)94__asm__ volatile("ibar 0");95#elif defined(__mips__)96const uintptr_t start_int = (uintptr_t)start;97const uintptr_t end_int = (uintptr_t)end;98uintptr_t synci_step;99__asm__ volatile("rdhwr %0, $1" : "=r"(synci_step));100if (synci_step != 0) {101#if __mips_isa_rev >= 6102for (uintptr_t p = start_int; p < end_int; p += synci_step)103__asm__ volatile("synci 0(%0)" : : "r"(p));104105// The last "move $at, $0" is the target of jr.hb instead of delay slot.106__asm__ volatile(".set noat\n"107"sync\n"108"addiupc $at, 12\n"109"jr.hb $at\n"110"move $at, $0\n"111".set at");112#elif defined(__linux__) || defined(__OpenBSD__)113// Pre-R6 may not be globalized. And some implementations may give strange114// synci_step. So, let's use libc call for it.115_flush_cache(start, end_int - start_int, BCACHE);116#else117(void)start_int;118(void)end_int;119compilerrt_abort();120#endif121}122#elif defined(__aarch64__) && !defined(__APPLE__)123uint64_t xstart = (uint64_t)(uintptr_t)start;124uint64_t xend = (uint64_t)(uintptr_t)end;125126// Get Cache Type Info.127static uint64_t ctr_el0 = 0;128if (ctr_el0 == 0)129__asm __volatile("mrs %0, ctr_el0" : "=r"(ctr_el0));130131// The DC and IC instructions must use 64-bit registers so we don't use132// uintptr_t in case this runs in an IPL32 environment.133uint64_t addr;134135// If CTR_EL0.IDC is set, data cache cleaning to the point of unification136// is not required for instruction to data coherence.137if (((ctr_el0 >> 28) & 0x1) == 0x0) {138const size_t dcache_line_size = 4 << ((ctr_el0 >> 16) & 15);139for (addr = xstart & ~(dcache_line_size - 1); addr < xend;140addr += dcache_line_size)141__asm __volatile("dc cvau, %0" ::"r"(addr));142}143__asm __volatile("dsb ish");144145// If CTR_EL0.DIC is set, instruction cache invalidation to the point of146// unification is not required for instruction to data coherence.147if (((ctr_el0 >> 29) & 0x1) == 0x0) {148const size_t icache_line_size = 4 << ((ctr_el0 >> 0) & 15);149for (addr = xstart & ~(icache_line_size - 1); addr < xend;150addr += icache_line_size)151__asm __volatile("ic ivau, %0" ::"r"(addr));152__asm __volatile("dsb ish");153}154__asm __volatile("isb sy");155#elif defined(__powerpc__)156// Newer CPUs have a bigger line size made of multiple blocks, so the157// following value is a minimal common denominator for what used to be158// a single block cache line and is therefore inneficient.159const size_t line_size = 32;160const size_t len = (uintptr_t)end - (uintptr_t)start;161162const uintptr_t mask = ~(line_size - 1);163const uintptr_t start_line = ((uintptr_t)start) & mask;164const uintptr_t end_line = ((uintptr_t)start + len + line_size - 1) & mask;165166for (uintptr_t line = start_line; line < end_line; line += line_size)167__asm__ volatile("dcbf 0, %0" : : "r"(line));168__asm__ volatile("sync");169170for (uintptr_t line = start_line; line < end_line; line += line_size)171__asm__ volatile("icbi 0, %0" : : "r"(line));172__asm__ volatile("isync");173#elif defined(__sparc__)174const size_t dword_size = 8;175const size_t len = (uintptr_t)end - (uintptr_t)start;176177const uintptr_t mask = ~(dword_size - 1);178const uintptr_t start_dword = ((uintptr_t)start) & mask;179const uintptr_t end_dword = ((uintptr_t)start + len + dword_size - 1) & mask;180181for (uintptr_t dword = start_dword; dword < end_dword; dword += dword_size)182__asm__ volatile("flush %0" : : "r"(dword));183#elif defined(__riscv) && defined(__linux__)184// See: arch/riscv/include/asm/cacheflush.h, arch/riscv/kernel/sys_riscv.c185register void *start_reg __asm("a0") = start;186const register void *end_reg __asm("a1") = end;187// "0" means that we clear cache for all threads (SYS_RISCV_FLUSH_ICACHE_ALL)188const register long flags __asm("a2") = 0;189const register long syscall_nr __asm("a7") = __NR_riscv_flush_icache;190__asm __volatile("ecall"191: "=r"(start_reg)192: "r"(start_reg), "r"(end_reg), "r"(flags), "r"(syscall_nr));193assert(start_reg == 0 && "Cache flush syscall failed.");194#elif defined(__riscv) && defined(__OpenBSD__)195struct riscv_sync_icache_args arg;196197arg.addr = (uintptr_t)start;198arg.len = (uintptr_t)end - (uintptr_t)start;199200sysarch(RISCV_SYNC_ICACHE, &arg);201#elif defined(__ve__)202__asm__ volatile("fencec 2");203#else204#if __APPLE__205// On Darwin, sys_icache_invalidate() provides this functionality206sys_icache_invalidate(start, end - start);207#else208compilerrt_abort();209#endif210#endif211}212213214