1 /*
2 * Copyright (C) 2017 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 
17 #pragma once
18 
19 #include "interface.h"
20 
21 #include "aemu/base/files/Stream.h"
22 
23 #include <memory>
24 #include <string>
25 #include <stdint.h>
26 
27 struct SnapshotRamBlock {
28     std::string id;
29     int64_t startOffset;
30     uint8_t* hostPtr;
31     int64_t totalSize;
32     int32_t pageSize;
33     uint32_t flags;
34     std::string path;
35     bool readonly;
36     bool needRestoreFromRamFile;
37 };
38 
39 namespace android {
40 namespace snapshot {
41 
42 class ITextureSaver;
43 class TextureSaver;
44 class ITextureLoader;
45 class TextureLoader;
46 using ITextureSaverPtr = std::shared_ptr<ITextureSaver>;
47 using ITextureLoaderPtr = std::shared_ptr<ITextureLoader>;
48 using ITextureLoaderWPtr = std::weak_ptr<ITextureLoader>;
49 
50 struct SnapshotSaveStream {
51     android::base::Stream* stream = nullptr;
52     ITextureSaverPtr textureSaver;
53 };
54 
55 struct SnapshotLoadStream {
56     android::base::Stream* stream = nullptr;
57     ITextureLoaderPtr textureLoader;
58 };
59 
60 // Taken from exec.c, these #defines
61 // are for the |flags| field in SnapshotRamBlock.
62 #define SNAPSHOT_RAM_MAPPED_SHARED (1 << 1)
63 #define SNAPSHOT_RAM_MAPPED (1 << 3)
64 #define SNAPSHOT_RAM_USER_BACKED (1 << 4)
65 
66 using RamBlock = ::SnapshotRamBlock;
67 
68 enum class IndexFlags {
69     Empty = 0,
70     CompressedPages = 0x01,
71     SeparateBackingStore = 0x02,
72 };
73 
74 enum class OperationStatus {
75     NotStarted = SNAPSHOT_STATUS_NOT_STARTED,
76     Ok = SNAPSHOT_STATUS_OK,
77     Error = SNAPSHOT_STATUS_ERROR,
78     Canceled = SNAPSHOT_STATUS_CANCELED,
79 };
80 
81 enum class FailureReason {
82     Empty = 0,
83 
84     CorruptedData,
85     NoSnapshotPb,
86     BadSnapshotPb,
87     IncompatibleVersion,
88     NoRamFile,
89     NoTexturesFile,
90     SnapshotsNotSupported,
91     Canceled,
92     Tombstone,
93 
94     UnrecoverableErrorLimit = 10000,
95 
96     NoSnapshotInImage,
97     ConfigMismatchHostHypervisor,
98     ConfigMismatchHostGpu,
99     ConfigMismatchRenderer,
100     ConfigMismatchFeatures,
101     ConfigMismatchAvd,
102     SystemImageChanged,
103 
104     ValidationErrorLimit = 20000,
105 
106     InternalError,
107     EmulationEngineFailed,
108     RamFailed,
109     TexturesFailed,
110     AdbOffline,
111     OutOfDiskSpace,
112 
113     InProgressLimit = 30000,
114 
115     UnsupportedVkApp = 30001,
116     UnsupportedVkApi = 30002,
117 
118     UnsupportedVkUsageLimit = 40000,
119 };
120 
121 FailureReason errnoToFailure(int error);
122 const char* failureReasonToString(FailureReason failure,
123                                   SnapshotOperation op);
124 
125 template <class Operation>
isComplete(const Operation & op)126 bool isComplete(const Operation& op) {
127     return op.status() != OperationStatus::NotStarted;
128 }
129 
130 bool isBufferZeroed(const void* ptr, int32_t size);
131 
132 #if defined(__APPLE__) && defined(__aarch64__)
133 constexpr int32_t kDefaultPageSize = 16384;
134 #else
135 constexpr int32_t kDefaultPageSize = 4096;
136 #endif
137 
138 constexpr int32_t kCancelTimeoutMs = 15000;
139 
140 // Size in bytes of largest in-flight RAM region for decommitting.
141 constexpr uint64_t kDecommitChunkSize = 4096 * 4096; // 16 MB
142 constexpr const char* kDefaultBootSnapshot = "default_boot";
143 constexpr const char* kRamFileName = "ram.bin";
144 constexpr const char* kTexturesFileName = "textures.bin";
145 constexpr const char* kMappedRamFileName = "ram.img";
146 constexpr const char* kMappedRamFileDirtyName = "ram.img.dirty";
147 
148 constexpr const char* kSnapshotProtobufName = "snapshot.pb";
149 
150 void resetSnapshotLiveness();
151 bool isSnapshotAlive();
152 
153 #ifdef AEMU_MIN
154 #define SNAPSHOT_METRICS 0
155 #else
156 #define SNAPSHOT_METRICS 1
157 #endif
158 
159 enum SnapshotterOperation {
160     SNAPSHOTTER_OPERATION_SAVE,
161     SNAPSHOTTER_OPERATION_LOAD,
162 };
163 
164 enum SnapshotterStage {
165     SNAPSHOTTER_STAGE_START,
166     SNAPSHOTTER_STAGE_END,
167 };
168 
169 }  // namespace snapshot
170 }  // namespace android
171