blob: 22f74944865ca9cc5abd8310ded933f5dfd18cfb [file] [log] [blame]
Reid Spencercbad7012004-09-11 04:59:30 +00001//===- Memory.cpp - Memory Handling Support ---------------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencercbad7012004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencercbad7012004-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. Spencer1f6efa32010-11-29 18:16:10 +000015#include "llvm/Support/Memory.h"
16#include "llvm/Support/Valgrind.h"
Reid Spencerce799952004-12-27 06:15:57 +000017#include "llvm/Config/config.h"
Reid Spencercbad7012004-09-11 04:59:30 +000018
Bruno Cardoso Lopes9d2fa872011-10-10 18:41:02 +000019#if defined(__mips__)
20#include <sys/cachectl.h>
21#endif
22
Reid Spencercbad7012004-09-11 04:59:30 +000023namespace llvm {
24using namespace sys;
Reid Spencercbad7012004-09-11 04:59:30 +000025}
26
27// Include the platform-specific parts of this class.
Reid Spencerce799952004-12-27 06:15:57 +000028#ifdef LLVM_ON_UNIX
Reid Spencerbccc8ab2005-01-09 23:29:00 +000029#include "Unix/Memory.inc"
Reid Spencerce799952004-12-27 06:15:57 +000030#endif
31#ifdef LLVM_ON_WIN32
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000032#include "Windows/Memory.inc"
Reid Spencerce799952004-12-27 06:15:57 +000033#endif
Chris Lattner93bb4aa2008-06-25 17:14:10 +000034
Chris Lattner95f39002008-06-25 17:17:53 +000035extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
36
Chris Lattner93bb4aa2008-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. Spencer1f6efa32010-11-29 18:16:10 +000042
Evan Chengc663bee2008-11-14 02:33:17 +000043// icache invalidation for PPC and ARM.
44#if defined(__APPLE__)
Daniel Dunbar299f11f2009-09-12 23:29:02 +000045
46# if (defined(__POWERPC__) || defined (__ppc__) || \
Evan Chengc663bee2008-11-14 02:33:17 +000047 defined(_POWER) || defined(_ARCH_PPC)) || defined(__arm__)
Galina Kistanovad8975992012-07-12 20:45:36 +000048 sys_icache_invalidate(const_cast<void *>(Addr), Len);
Daniel Dunbar299f11f2009-09-12 23:29:02 +000049# endif
50
Evan Chengc663bee2008-11-14 02:33:17 +000051#else
Daniel Dunbar299f11f2009-09-12 23:29:02 +000052
53# if (defined(__POWERPC__) || defined (__ppc__) || \
54 defined(_POWER) || defined(_ARCH_PPC)) && defined(__GNUC__)
Evan Chengc663bee2008-11-14 02:33:17 +000055 const size_t LineSize = 32;
Chris Lattner93bb4aa2008-06-25 17:14:10 +000056
Evan Chengc663bee2008-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 Lattner93bb4aa2008-06-25 17:14:10 +000060
Evan Chengc663bee2008-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 Lattner93bb4aa2008-06-25 17:14:10 +000064
Evan Chengc663bee2008-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 Dunbar299f11f2009-09-12 23:29:02 +000068# elif defined(__arm__) && defined(__GNUC__)
69 // FIXME: Can we safely always call this for __GNUC__ everywhere?
Galina Kistanovad8975992012-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 Lopesc4cc40c2011-09-14 03:00:41 +000073# elif defined(__mips__)
Galina Kistanovad8975992012-07-12 20:45:36 +000074 const char *Start = static_cast<const char *>(Addr);
75 cacheflush(const_cast<char *>(Start), Len, BCACHE);
Daniel Dunbar299f11f2009-09-12 23:29:02 +000076# endif
77
Evan Chengc663bee2008-11-14 02:33:17 +000078#endif // end apple
Jeffrey Yasskinf28411f2010-03-15 04:57:55 +000079
80 ValgrindDiscardTranslations(Addr, Len);
Chris Lattner95f39002008-06-25 17:17:53 +000081}