1db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom/*
2db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * Copyright (C) 2008 The Android Open Source Project
3db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom *
4db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * you may not use this file except in compliance with the License.
6db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * You may obtain a copy of the License at
7db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom *
8db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom *
10db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
11db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * See the License for the specific language governing permissions and
14db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom * limitations under the License.
15db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom */
16db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_MEM_MAP_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_MEM_MAP_H_
19db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
203eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi#include "base/mutex.h"
213eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi
221c23e1edb7361bbaec6e57fca86d8d3797960ad2Mathieu Chartier#include <string>
233eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi#include <map>
241c23e1edb7361bbaec6e57fca86d8d3797960ad2Mathieu Chartier
2527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include <stddef.h>
26a168c83a1d247094e9efb1244b0f73a5f1e1ed97Elliott Hughes#include <sys/mman.h>  // For the PROT_* and MAP_* constants.
2727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include <sys/types.h>
28db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
295369c40f75fdcb1be7a7c06db212ce965c83a164Mathieu Chartier#include "base/allocator.h"
3027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include "globals.h"
31db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
32db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstromnamespace art {
33db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
34c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#if defined(__LP64__) && (!defined(__x86_64__) || defined(__APPLE__))
35c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#define USE_ART_LOW_4G_ALLOCATOR 1
36c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#else
37c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#define USE_ART_LOW_4G_ALLOCATOR 0
38c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#endif
39c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers
40c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers#ifdef __linux__
41c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogersstatic constexpr bool kMadviseZeroes = true;
42c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers#else
43c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogersstatic constexpr bool kMadviseZeroes = false;
44c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers#endif
45c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers
46db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom// Used to keep track of mmap segments.
47d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe//
48d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// On 64b systems not supporting MAP_32BIT, the implementation of MemMap will do a linear scan
49d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// for free pages. For security, the start of this scan should be randomized. This requires a
50d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// dynamic initializer.
51d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// For this to work, it is paramount that there are no other static initializers that access MemMap.
52d8f26dbebe72c1cbdfa85bdeeb003283c7435db3Andreas Gampe// Otherwise, calls might see uninitialized values.
53db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstromclass MemMap {
54db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom public:
55ecd3a6fc6e29d7dc4cb825c5282a8054ac52b8cfElliott Hughes  // Request an anonymous region of length 'byte_count' and a requested base address.
566c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes  // Use NULL as the requested base address if you don't care.
576c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes  //
586c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes  // The word "anonymous" in this context means "not backed by a file". The supplied
596c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes  // 'ashmem_name' will be used -- on systems that support it -- to give the mapping
606c9c06dbb0b16714079afaedbebd3d548aa832b2Elliott Hughes  // a name.
614a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  //
624a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
638d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers  static MemMap* MapAnonymous(const char* ashmem_name, byte* addr, size_t byte_count, int prot,
64ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                              bool low_4gb, std::string* error_msg);
65db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
66db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom  // Map part of a file, taking care of non-page aligned offsets.  The
67db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom  // "start" offset is absolute, not relative.
68db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom  //
69db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom  // On success, returns returns a MemMap instance.  On failure, returns a NULL;
708d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers  static MemMap* MapFile(size_t byte_count, int prot, int flags, int fd, off_t start,
718d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers                         const char* filename, std::string* error_msg) {
728d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    return MapFileAtAddress(NULL, byte_count, prot, flags, fd, start, false, filename, error_msg);
734a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  }
744a289ed61242964b921434de7d375f46480472a1Brian Carlstrom
754a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  // Map part of a file, taking care of non-page aligned offsets.  The
764a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  // "start" offset is absolute, not relative. This version allows
77a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  // requesting a specific address for the base of the
78a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  // mapping. "reuse" allows us to create a view into an existing
79a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  // mapping where we do not take ownership of the memory.
804a289ed61242964b921434de7d375f46480472a1Brian Carlstrom  //
81bddaea2b88b0a19d9cc7a4dea772af8e829323b3Narayan Kamath  // On success, returns returns a MemMap instance.  On failure, returns a
82bddaea2b88b0a19d9cc7a4dea772af8e829323b3Narayan Kamath  // nullptr;
838d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers  static MemMap* MapFileAtAddress(byte* addr, size_t byte_count, int prot, int flags, int fd,
848d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers                                  off_t start, bool reuse, const char* filename,
858d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers                                  std::string* error_msg);
86db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
8727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  // Releases the memory mapping
883eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  ~MemMap() LOCKS_EXCLUDED(Locks::mem_maps_lock_);
89db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
900d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  const std::string& GetName() const {
910d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom    return name_;
920d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  }
930d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom
94d88fa26892ad5ddebb7ab3407cdc574c54ed8258Logan Chien  bool Protect(int prot);
95d88fa26892ad5ddebb7ab3407cdc574c54ed8258Logan Chien
96c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers  void MadviseDontNeedAndZero();
97c5f17732d8144491c642776b6b48c85dfadf4b52Ian Rogers
981c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers  int GetProtect() const {
991c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers    return prot_;
1001c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers  }
1011c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers
10230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  byte* Begin() const {
10330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    return begin_;
104db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom  }
105db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
10630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t Size() const {
10730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    return size_;
108db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom  }
109db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
11030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  byte* End() const {
1112fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier    return Begin() + Size();
1122fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier  }
1132fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier
1140d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  void* BaseBegin() const {
1150d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom    return base_begin_;
1160d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  }
1170d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom
1180d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  size_t BaseSize() const {
1190d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom    return base_size_;
1200d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  }
1210d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom
1220d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  void* BaseEnd() const {
1230d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom    return reinterpret_cast<byte*>(BaseBegin()) + BaseSize();
1240d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom  }
1250d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstrom
1262fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier  bool HasAddress(const void* addr) const {
1272fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier    return Begin() <= addr && addr < End();
128b765be0d656c3073402693aeaf64e95a0e49f218Brian Carlstrom  }
129b765be0d656c3073402693aeaf64e95a0e49f218Brian Carlstrom
130fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi  // Unmap the pages at end and remap them to create another memory map.
131fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi  MemMap* RemapAtEnd(byte* new_end, const char* tail_name, int tail_prot,
132fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi                     std::string* error_msg);
133cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier
1343eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  static bool CheckNoGaps(MemMap* begin_map, MemMap* end_map)
1353eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi      LOCKS_EXCLUDED(Locks::mem_maps_lock_);
1363eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  static void DumpMaps(std::ostream& os)
1373eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi      LOCKS_EXCLUDED(Locks::mem_maps_lock_);
1383eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi
1395369c40f75fdcb1be7a7c06db212ce965c83a164Mathieu Chartier  typedef AllocationTrackingMultiMap<void*, MemMap*, kAllocatorTagMaps> Maps;
1405369c40f75fdcb1be7a7c06db212ce965c83a164Mathieu Chartier
141c54e12a413e16f90526318f1f466a900a717fbb0Mathieu Chartier  static void Init() LOCKS_EXCLUDED(Locks::mem_maps_lock_);
142c54e12a413e16f90526318f1f466a900a717fbb0Mathieu Chartier  static void Shutdown() LOCKS_EXCLUDED(Locks::mem_maps_lock_);
143c54e12a413e16f90526318f1f466a900a717fbb0Mathieu Chartier
144db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom private:
1451c23e1edb7361bbaec6e57fca86d8d3797960ad2Mathieu Chartier  MemMap(const std::string& name, byte* begin, size_t size, void* base_begin, size_t base_size,
146a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo         int prot, bool reuse) LOCKS_EXCLUDED(Locks::mem_maps_lock_);
1473eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi
1485369c40f75fdcb1be7a7c06db212ce965c83a164Mathieu Chartier  static void DumpMapsLocked(std::ostream& os)
1493eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi      EXCLUSIVE_LOCKS_REQUIRED(Locks::mem_maps_lock_);
1503eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  static bool HasMemMap(MemMap* map)
1513eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi      EXCLUSIVE_LOCKS_REQUIRED(Locks::mem_maps_lock_);
1523eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  static MemMap* GetLargestMemMapAt(void* address)
1533eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi      EXCLUSIVE_LOCKS_REQUIRED(Locks::mem_maps_lock_);
154db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
155a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  const std::string name_;
1561c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers  byte* const begin_;  // Start of data.
157cc236d74772dda5a4161d9bc5f497fd3d956eb87Mathieu Chartier  size_t size_;  // Length of data.
158db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
1591c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers  void* const base_begin_;  // Page-aligned base address.
160fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi  size_t base_size_;  // Length of mapping. May be changed by RemapAtEnd (ie Zygote).
1611c849e5badc85b6753dee0c0487729b2c0529f51Ian Rogers  int prot_;  // Protection of the map.
162fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi
163a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  // When reuse_ is true, this is just a view of an existing mapping
164a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  // and we do not take ownership and are not responsible for
165a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  // unmapping.
166a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo  const bool reuse_;
167a62a588a9202f69e53fbeb3045ea8ea5ec2587f8Jim_Guo
168c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers#if USE_ART_LOW_4G_ALLOCATOR
169c3ccc1039e0bbc0744f958cb8719cf96bce5b853Ian Rogers  static uintptr_t next_mem_pos_;   // Next memory location to check for low_4g extent.
1708dba5aaaffc0bc2b2580bf02f0d9095c00d26a17Stuart Monteith#endif
1718dba5aaaffc0bc2b2580bf02f0d9095c00d26a17Stuart Monteith
1723eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi  // All the non-empty MemMaps. Use a multimap as we do a reserve-and-divide (eg ElfMap::Load()).
173c54e12a413e16f90526318f1f466a900a717fbb0Mathieu Chartier  static Maps* maps_ GUARDED_BY(Locks::mem_maps_lock_);
1743eed93dd5be03e5539827bebf0f414251a12e15eHiroshi Yamauchi
175fd7e7f1253927c8d7f17e7cbc259daaf51868bd3Hiroshi Yamauchi  friend class MemMapTest;  // To allow access to base_begin_ and base_size_.
176db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom};
1770d6adac2550113da33d42e88f0d87a57b25c5a60Brian Carlstromstd::ostream& operator<<(std::ostream& os, const MemMap& mem_map);
178db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
179db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom}  // namespace art
180db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
181fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_MEM_MAP_H_
182