Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 20 | #include "arch/instruction_set.h" |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 21 | #include "gc/accounting/space_bitmap.h" |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 22 | #include "runtime.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 23 | #include "space.h" |
| 24 | |
| 25 | namespace art { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 26 | |
| 27 | class OatFile; |
| 28 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | namespace gc { |
| 30 | namespace space { |
| 31 | |
| 32 | // An image space is a space backed with a memory mapped image. |
| 33 | class ImageSpace : public MemMapSpace { |
| 34 | public: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 35 | SpaceType GetType() const { |
| 36 | return kSpaceTypeImageSpace; |
| 37 | } |
| 38 | |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 39 | // Load boot image spaces from a primary image file for a specified instruction set. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 40 | // |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 41 | // On successful return, the loaded spaces are added to boot_image_spaces (which must be |
| 42 | // empty on entry) and oat_file_end is updated with the (page-aligned) end of the last |
| 43 | // oat file. |
| 44 | static bool LoadBootImage(const std::string& image_file_name, |
| 45 | const InstructionSet image_instruction_set, |
| 46 | std::vector<space::ImageSpace*>* boot_image_spaces, |
| 47 | uint8_t** oat_file_end) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 48 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 49 | |
| 50 | // Try to open an existing app image space. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 51 | static std::unique_ptr<ImageSpace> CreateFromAppImage(const char* image, |
| 52 | const OatFile* oat_file, |
| 53 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 54 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 55 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 56 | // Reads the image header from the specified image location for the |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 57 | // instruction set image_isa. Returns null on failure, with |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 58 | // reason in error_msg. |
| 59 | static ImageHeader* ReadImageHeader(const char* image_location, |
| 60 | InstructionSet image_isa, |
| 61 | std::string* error_msg); |
| 62 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 63 | // Give access to the OatFile. |
| 64 | const OatFile* GetOatFile() const; |
| 65 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 66 | // Releases the OatFile from the ImageSpace so it can be transfer to |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 67 | // the caller, presumably the OatFileManager. |
| 68 | std::unique_ptr<const OatFile> ReleaseOatFile(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 69 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 70 | void VerifyImageAllocations() |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 71 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 72 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 73 | const ImageHeader& GetImageHeader() const { |
| 74 | return *reinterpret_cast<ImageHeader*>(Begin()); |
| 75 | } |
| 76 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 77 | // Actual filename where image was loaded from. |
| 78 | // For example: /data/dalvik-cache/arm/system@framework@boot.art |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 79 | const std::string GetImageFilename() const { |
| 80 | return GetName(); |
| 81 | } |
| 82 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 83 | // Symbolic location for image. |
| 84 | // For example: /system/framework/boot.art |
| 85 | const std::string GetImageLocation() const { |
| 86 | return image_location_; |
| 87 | } |
| 88 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 89 | accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 90 | return live_bitmap_.get(); |
| 91 | } |
| 92 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 93 | accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 94 | // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of |
| 95 | // special cases to test against. |
| 96 | return live_bitmap_.get(); |
| 97 | } |
| 98 | |
| 99 | void Dump(std::ostream& os) const; |
| 100 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 101 | // Sweeping image spaces is a NOP. |
| 102 | void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) { |
| 103 | } |
| 104 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 105 | bool CanMoveObjects() const OVERRIDE { |
| 106 | return false; |
| 107 | } |
| 108 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 109 | // Returns the filename of the image corresponding to |
| 110 | // requested image_location, or the filename where a new image |
| 111 | // should be written if one doesn't exist. Looks for a generated |
| 112 | // image in the specified location and then in the dalvik-cache. |
| 113 | // |
| 114 | // Returns true if an image was found, false otherwise. |
| 115 | static bool FindImageFilename(const char* image_location, |
| 116 | InstructionSet image_isa, |
| 117 | std::string* system_location, |
| 118 | bool* has_system, |
| 119 | std::string* data_location, |
| 120 | bool* dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 121 | bool* has_data, |
| 122 | bool *is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 123 | |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 124 | // Use the input image filename to adapt the names in the given boot classpath to establish |
| 125 | // complete locations for secondary images. |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 126 | static void ExtractMultiImageLocations(const std::string& input_image_file_name, |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 127 | const std::string& boot_classpath, |
| 128 | std::vector<std::string>* image_filenames); |
| 129 | |
Mathieu Chartier | 866d874 | 2016-09-21 15:24:18 -0700 | [diff] [blame] | 130 | static std::string GetMultiImageBootClassPath(const std::vector<const char*>& dex_locations, |
| 131 | const std::vector<const char*>& oat_filenames, |
| 132 | const std::vector<const char*>& image_filenames); |
| 133 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 134 | // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields. |
| 135 | uint8_t* GetImageEnd() const { |
| 136 | return Begin() + GetImageHeader().GetImageSize(); |
| 137 | } |
| 138 | |
| 139 | // Return the start of the associated oat file. |
| 140 | uint8_t* GetOatFileBegin() const { |
| 141 | return GetImageHeader().GetOatFileBegin(); |
| 142 | } |
| 143 | |
| 144 | // Return the end of the associated oat file. |
| 145 | uint8_t* GetOatFileEnd() const { |
| 146 | return GetImageHeader().GetOatFileEnd(); |
| 147 | } |
| 148 | |
Mathieu Chartier | d5f3f32 | 2016-03-21 14:05:56 -0700 | [diff] [blame] | 149 | void DumpSections(std::ostream& os) const; |
| 150 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 151 | protected: |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 152 | // Tries to initialize an ImageSpace from the given image path, returning null on error. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 153 | // |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 154 | // If validate_oat_file is false (for /system), do not verify that image's OatFile is up-to-date |
| 155 | // relative to its DexFile inputs. Otherwise (for /data), validate the inputs and generate the |
| 156 | // OatFile in /data/dalvik-cache if necessary. If the oat_file is null, it uses the oat file from |
| 157 | // the image. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 158 | static std::unique_ptr<ImageSpace> Init(const char* image_filename, |
| 159 | const char* image_location, |
| 160 | bool validate_oat_file, |
| 161 | const OatFile* oat_file, |
| 162 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 163 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 164 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 165 | static Atomic<uint32_t> bitmap_index_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 166 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 167 | std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 168 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 169 | ImageSpace(const std::string& name, |
| 170 | const char* image_location, |
| 171 | MemMap* mem_map, |
| 172 | accounting::ContinuousSpaceBitmap* live_bitmap, |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 173 | uint8_t* end); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 174 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 175 | // The OatFile associated with the image during early startup to |
| 176 | // reserve space contiguous to the image. It is later released to |
| 177 | // the ClassLinker during it's initialization. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 178 | std::unique_ptr<OatFile> oat_file_; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 179 | |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 180 | // There are times when we need to find the boot image oat file. As |
| 181 | // we release ownership during startup, keep a non-owned reference. |
| 182 | const OatFile* oat_file_non_owned_; |
| 183 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 184 | const std::string image_location_; |
| 185 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 186 | friend class ImageSpaceLoader; |
| 187 | friend class Space; |
| 188 | |
Mathieu Chartier | 763a31e | 2015-11-16 16:05:55 -0800 | [diff] [blame] | 189 | private: |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 190 | // Create a boot image space from an image file for a specified instruction |
| 191 | // set. Cannot be used for future allocation or collected. |
| 192 | // |
| 193 | // Create also opens the OatFile associated with the image file so |
| 194 | // that it be contiguously allocated with the image before the |
| 195 | // creation of the alloc space. The ReleaseOatFile will later be |
| 196 | // used to transfer ownership of the OatFile to the ClassLinker when |
| 197 | // it is initialized. |
| 198 | static std::unique_ptr<ImageSpace> CreateBootImage(const char* image, |
| 199 | InstructionSet image_isa, |
| 200 | bool secondary_image, |
| 201 | std::string* error_msg) |
| 202 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 203 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 204 | DISALLOW_COPY_AND_ASSIGN(ImageSpace); |
| 205 | }; |
| 206 | |
| 207 | } // namespace space |
| 208 | } // namespace gc |
| 209 | } // namespace art |
| 210 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 211 | #endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ |