blob: 22f74944865ca9cc5abd8310ded933f5dfd18cfb [file] [log] [blame]
Reid Spencer566ac282004-09-11 04:59:30 +00001//===- Memory.cpp - Memory Handling Support ---------------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer566ac282004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer566ac282004-09-11 04:59:30 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines some helpful functions for allocating memory and dealing
11// with memory mapped files
12//
13//===----------------------------------------------------------------------===//
14
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/Memory.h"
16#include "llvm/Support/Valgrind.h"
Reid Spencer71383fb2004-12-27 06:15:57 +000017#include "llvm/Config/config.h"
Reid Spencer566ac282004-09-11 04:59:30 +000018
Bruno Cardoso Lopescc6659b2011-10-10 18:41:02 +000019#if defined(__mips__)
20#include <sys/cachectl.h>
21#endif
22
Reid Spencer566ac282004-09-11 04:59:30 +000023namespace llvm {
24using namespace sys;
Reid Spencer566ac282004-09-11 04:59:30 +000025}
26
27// Include the platform-specific parts of this class.
Reid Spencer71383fb2004-12-27 06:15:57 +000028#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000029#include "Unix/Memory.inc"
Reid Spencer71383fb2004-12-27 06:15:57 +000030#endif
31#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000032#include "Windows/Memory.inc"
Reid Spencer71383fb2004-12-27 06:15:57 +000033#endif
Chris Lattner881d5372008-06-25 17:14:10 +000034
Chris Lattnerb4325a82008-06-25 17:17:53 +000035extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
36
Chris Lattner881d5372008-06-25 17:14:10 +000037/// InvalidateInstructionCache - Before the JIT can run a block of code
38/// that has been emitted it must invalidate the instruction cache on some
39/// platforms.
40void llvm::sys::Memory::InvalidateInstructionCache(const void *Addr,
41 size_t Len) {
Michael J. Spencer447762d2010-11-29 18:16:10 +000042
Evan Cheng889b79b2008-11-14 02:33:17 +000043// icache invalidation for PPC and ARM.
44#if defined(__APPLE__)
Daniel Dunbara2263782009-09-12 23:29:02 +000045
46# if (defined(__POWERPC__) || defined (__ppc__) || \
Evan Cheng889b79b2008-11-14 02:33:17 +000047 defined(_POWER) || defined(_ARCH_PPC)) || defined(__arm__)
Galina Kistanova7da65782012-07-12 20:45:36 +000048 sys_icache_invalidate(const_cast<void *>(Addr), Len);
Daniel Dunbara2263782009-09-12 23:29:02 +000049# endif
50
Evan Cheng889b79b2008-11-14 02:33:17 +000051#else
Daniel Dunbara2263782009-09-12 23:29:02 +000052
53# if (defined(__POWERPC__) || defined (__ppc__) || \
54 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
Evan Cheng889b79b2008-11-14 02:33:17 +000055 const size_t LineSize = 32;
Chris Lattner881d5372008-06-25 17:14:10 +000056
Evan Cheng889b79b2008-11-14 02:33:17 +000057 const intptr_t Mask = ~(LineSize - 1);
58 const intptr_t StartLine = ((intptr_t) Addr) & Mask;
59 const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask;
Chris Lattner881d5372008-06-25 17:14:10 +000060
Evan Cheng889b79b2008-11-14 02:33:17 +000061 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
62 asm volatile("dcbf 0, %0" : : "r"(Line));
63 asm volatile("sync");
Chris Lattner881d5372008-06-25 17:14:10 +000064
Evan Cheng889b79b2008-11-14 02:33:17 +000065 for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize)
66 asm volatile("icbi 0, %0" : : "r"(Line));
67 asm volatile("isync");
Daniel Dunbara2263782009-09-12 23:29:02 +000068# elif defined(__arm__) && defined(__GNUC__)
69 // FIXME: Can we safely always call this for __GNUC__ everywhere?
Galina Kistanova7da65782012-07-12 20:45:36 +000070 const char *Start = static_cast<const char *>(Addr);
71 const char *End = Start + Len;
72 __clear_cache(const_cast<char *>(Start), const_cast<char *>(End));
Bruno Cardoso Lopes483c2692011-09-14 03:00:41 +000073# elif defined(__mips__)
Galina Kistanova7da65782012-07-12 20:45:36 +000074 const char *Start = static_cast<const char *>(Addr);
75 cacheflush(const_cast<char *>(Start), Len, BCACHE);
Daniel Dunbara2263782009-09-12 23:29:02 +000076# endif
77
Evan Cheng889b79b2008-11-14 02:33:17 +000078#endif // end apple
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +000079
80 ValgrindDiscardTranslations(Addr, Len);
Chris Lattnerb4325a82008-06-25 17:17:53 +000081}