Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 1 | //===- Memory.cpp - Memory Handling Support ---------------------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines some helpful functions for allocating memory and dealing |
| 11 | // with memory mapped files |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/System/Memory.h" |
Reid Spencer | ce79995 | 2004-12-27 06:15:57 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 17 | |
| 18 | namespace llvm { |
| 19 | using namespace sys; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | // Include the platform-specific parts of this class. |
Reid Spencer | ce79995 | 2004-12-27 06:15:57 +0000 | [diff] [blame] | 23 | #ifdef LLVM_ON_UNIX |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 24 | #include "Unix/Memory.inc" |
Reid Spencer | ce79995 | 2004-12-27 06:15:57 +0000 | [diff] [blame] | 25 | #endif |
| 26 | #ifdef LLVM_ON_WIN32 |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 27 | #include "Win32/Memory.inc" |
Reid Spencer | ce79995 | 2004-12-27 06:15:57 +0000 | [diff] [blame] | 28 | #endif |
Chris Lattner | 93bb4aa | 2008-06-25 17:14:10 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 95f3900 | 2008-06-25 17:17:53 +0000 | [diff] [blame] | 30 | extern "C" void sys_icache_invalidate(const void *Addr, size_t len); |
| 31 | |
Chris Lattner | 93bb4aa | 2008-06-25 17:14:10 +0000 | [diff] [blame] | 32 | /// InvalidateInstructionCache - Before the JIT can run a block of code |
| 33 | /// that has been emitted it must invalidate the instruction cache on some |
| 34 | /// platforms. |
| 35 | void llvm::sys::Memory::InvalidateInstructionCache(const void *Addr, |
| 36 | size_t Len) { |
| 37 | |
| 38 | // icache invalidation for PPC. |
| 39 | #if (defined(__POWERPC__) || defined (__ppc__) || \ |
| 40 | defined(_POWER) || defined(_ARCH_PPC)) |
| 41 | #if defined(__APPLE__) |
Chris Lattner | 95f3900 | 2008-06-25 17:17:53 +0000 | [diff] [blame] | 42 | sys_icache_invalidate(Addr, Len); |
Chris Lattner | 93bb4aa | 2008-06-25 17:14:10 +0000 | [diff] [blame] | 43 | #elif defined(__GNUC__) |
| 44 | const size_t LineSize = 32; |
| 45 | |
| 46 | const intptr_t Mask = ~(LineSize - 1); |
| 47 | const intptr_t StartLine = ((intptr_t) Addr) & Mask; |
Chris Lattner | 95f3900 | 2008-06-25 17:17:53 +0000 | [diff] [blame] | 48 | const intptr_t EndLine = ((intptr_t) Addr + Len + LineSize - 1) & Mask; |
Chris Lattner | 93bb4aa | 2008-06-25 17:14:10 +0000 | [diff] [blame] | 49 | |
| 50 | for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize) |
| 51 | asm volatile("dcbf 0, %0" : : "r"(Line)); |
| 52 | asm volatile("sync"); |
| 53 | |
| 54 | for (intptr_t Line = StartLine; Line < EndLine; Line += LineSize) |
| 55 | asm volatile("icbi 0, %0" : : "r"(Line)); |
| 56 | asm volatile("isync"); |
| 57 | #endif |
| 58 | #endif // end PPC |
| 59 | |
Chris Lattner | 95f3900 | 2008-06-25 17:17:53 +0000 | [diff] [blame] | 60 | } |
Evan Cheng | bc4707a | 2008-09-18 07:54:21 +0000 | [diff] [blame^] | 61 | |
| 62 | bool llvm::sys::Memory::SetRXPrivilege(const void *Addr, size_t Size) { |
| 63 | #if defined(__APPLE__) && defined(__arm__) |
| 64 | kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr, |
| 65 | (vm_size_t)Size, 0, |
| 66 | VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY); |
| 67 | return KERN_SUCCESS == kr; |
| 68 | #else |
| 69 | return true; |
| 70 | #endif |
| 71 | } |