• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "aemu/base/c_header.h"
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <inttypes.h>
22 
23 // Caching types
24 #define MAP_CACHE_MASK      0x0f
25 #define MAP_CACHE_NONE      0x00
26 #define MAP_CACHE_CACHED    0x01
27 #define MAP_CACHE_UNCACHED  0x02
28 #define MAP_CACHE_WC        0x03
29 
30 ANDROID_BEGIN_HEADER
31 // This file includes interfaces to VMMs.
32 
33 // A struct describing the information about host memory associated
34 // with a host memory id. Used with virtio-gpu-next.
35 struct HostmemEntry {
36     uint64_t id;
37     void* hva;
38     uint64_t size;
39     uint32_t caching;
40 };
41 
42 // Called by hostmemRegister(..)
43 struct MemEntry {
44     void* hva;
45     uint64_t size;
46     uint32_t register_fixed;
47     uint64_t fixed_id;
48     uint32_t caching;
49 };
50 
51 // A callback to consume a single line of output (including newline).
52 // |opque| is a handle to a context object. Functions that use this callback
53 // will also take a context handle that will be passed to the callback.
54 // |buff| contains the data to be consumed, of length |len|.
55 // The function should return the number of chars consumed successfully.
56 typedef int (*LineConsumerCallback)(void* opaque, const char* buff, int len);
57 
58 // Enumeration of various causes for shutdown. Please keep this in sync
59 // with the similar enum in include/sysem/sysemu.h.
60 typedef enum QemuShutdownCause {
61     QEMU_SHUTDOWN_CAUSE_NONE,              /* No shutdown request pending */
62     QEMU_SHUTDOWN_CAUSE_HOST_ERROR,        /* An error prevents further use of guest */
63     QEMU_SHUTDOWN_CAUSE_HOST_QMP,          /* Reaction to a QMP command, like 'quit' */
64     QEMU_SHUTDOWN_CAUSE_HOST_SIGNAL,       /* Reaction to a signal, such as SIGINT */
65     QEMU_SHUTDOWN_CAUSE_HOST_UI,           /* Reaction to UI event, like window close */
66     QEMU_SHUTDOWN_CAUSE_GUEST_SHUTDOWN,    /* Guest shutdown/suspend request, via
67                                               ACPI or other hardware-specific means */
68     QEMU_SHUTDOWN_CAUSE_GUEST_RESET,       /* Guest reset request, and command line
69                                               turns that into a shutdown */
70     QEMU_SHUTDOWN_CAUSE_GUEST_PANIC,       /* Guest panicked, and command line turns
71                                               that into a shutdown */
72     QEMU_SHUTDOWN_CAUSE__MAX,
73 } QemuShutdownCause;
74 
75 #define SHUTDOWN_CAUSE_STATIC_ASSERT_ERROR_MESSAGE "QEMU shutdown cause values differ from AndroidEmu's!" \
76 
77 #define SHUTDOWN_CAUSE_STATIC_MATCH(origCause) \
78     static_assert((int)(QEMU_##origCause) == (int)(origCause), SHUTDOWN_CAUSE_STATIC_ASSERT_ERROR_MESSAGE);
79 
80 #define STATIC_ASSERT_SHUTDOWN_CAUSE_MATCHES \
81     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_NONE) \
82     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_ERROR) \
83     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_QMP) \
84     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_SIGNAL) \
85     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_UI) \
86     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_GUEST_SHUTDOWN) \
87     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_GUEST_RESET) \
88     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_GUEST_PANIC) \
89     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE__MAX) \
90 
91 typedef struct {
92     int (*onStart)(void* opaque, const char* name);
93     void (*onEnd)(void* opaque, const char* name, int res);
94     void (*onQuickFail)(void* opaque, const char* name, int res);
95     bool (*isCanceled)(void* opaque, const char* name);
96 } SnapshotCallbackSet;
97 
98 typedef enum {
99     SNAPSHOT_SAVE,
100     SNAPSHOT_LOAD,
101     SNAPSHOT_DEL,
102     SNAPSHOT_OPS_COUNT
103 } SnapshotOperation;
104 
105 struct SnapshotRamBlock;
106 
107 typedef struct {
108     void (*registerBlock)(void* opaque,
109                           SnapshotOperation operation,
110                           const struct SnapshotRamBlock* block);
111     int (*startLoading)(void* opaque);
112     void (*savePage)(void* opaque,
113                      int64_t blockOffset,
114                      int64_t pageOffset,
115                      int32_t size);
116     int (*savingComplete)(void* opaque);
117     void (*loadRam)(void* opaque,
118                     void* hostRamPtr,
119                     uint64_t size);
120 } SnapshotRamCallbacks;
121 
122 typedef struct {
123     SnapshotCallbackSet ops[SNAPSHOT_OPS_COUNT];
124     SnapshotRamCallbacks ramOps;
125 } SnapshotCallbacks;
126 
127 typedef enum {
128     HV_UNKNOWN,
129     HV_NONE,
130     HV_KVM,
131     HV_HAXM,
132     HV_HVF,
133     HV_WHPX,
134 } VmHypervisorType;
135 
136 typedef struct {
137     VmHypervisorType hypervisorType;
138     int32_t numberOfCpuCores;
139     int64_t ramSizeBytes;
140 } VmConfiguration;
141 
142 typedef enum EmuRunState {
143     QEMU_RUN_STATE_DEBUG = 0,
144     QEMU_RUN_STATE_INMIGRATE = 1,
145     QEMU_RUN_STATE_INTERNAL_ERROR = 2,
146     QEMU_RUN_STATE_IO_ERROR = 3,
147     QEMU_RUN_STATE_PAUSED = 4,
148     QEMU_RUN_STATE_POSTMIGRATE = 5,
149     QEMU_RUN_STATE_PRELAUNCH = 6,
150     QEMU_RUN_STATE_FINISH_MIGRATE = 7,
151     QEMU_RUN_STATE_RESTORE_VM = 8,
152     QEMU_RUN_STATE_RUNNING = 9,
153     QEMU_RUN_STATE_SAVE_VM = 10,
154     QEMU_RUN_STATE_SHUTDOWN = 11,
155     QEMU_RUN_STATE_SUSPENDED = 12,
156     QEMU_RUN_STATE_WATCHDOG = 13,
157     QEMU_RUN_STATE_GUEST_PANICKED = 14,
158     QEMU_RUN_STATE_COLO = 15,
159     QEMU_RUN_STATE__MAX = 16,
160 } EmuRunState;
161 
162 typedef enum SnapshotSkipReason {
163     SNAPSHOT_SKIP_UNKNOWN = 0,
164     SNAPSHOT_SKIP_UNSUPPORTED_VK_APP = 1,
165     SNAPSHOT_SKIP_UNSUPPORTED_VK_API = 2,
166 } SnapshotSkipReason;
167 
toString_SnapshotSkipReason(SnapshotSkipReason reason)168 inline const char* toString_SnapshotSkipReason(SnapshotSkipReason reason) {
169     switch (reason) {
170         case SNAPSHOT_SKIP_UNKNOWN:
171             return "UNKNOWN";
172         case SNAPSHOT_SKIP_UNSUPPORTED_VK_APP:
173             return "UNSUPPORTED_VK_APP";
174         case SNAPSHOT_SKIP_UNSUPPORTED_VK_API:
175             return "UNSUPPORTED_VK_API";
176     }
177     return "UNKNOWN";
178 }
179 
180 // C interface to expose Qemu implementations of common VM related operations.
181 typedef struct QAndroidVmOperations {
182     bool (*vmStop)(void);
183     bool (*vmStart)(void);
184     void (*vmReset)(void);
185     void (*vmShutdown)(void);
186     bool (*vmPause)(void);
187     bool (*vmResume)(void);
188 
189     bool (*vmIsRunning)(void);
190 
191     // Snapshot-related VM operations.
192     // |outConsuer| and |errConsumer| are used to report output / error
193     // respectively. Each line of output is newline terminated and results in
194     // one call to the callback. |opaque| is passed to the callbacks as context.
195     // Returns true on success, false on failure.
196     bool (*snapshotList)(void* opaque,
197                          LineConsumerCallback outConsumer,
198                          LineConsumerCallback errConsumer);
199     bool (*snapshotSave)(const char* name,
200                          void* opaque,
201                          LineConsumerCallback errConsumer);
202     bool (*snapshotLoad)(const char* name,
203                          void* opaque,
204                          LineConsumerCallback errConsumer);
205     bool (*snapshotDelete)(const char* name,
206                            void* opaque,
207                            LineConsumerCallback errConsumer);
208     bool (*snapshotRemap)(bool shared,
209                           void* opaque,
210                           LineConsumerCallback errConsumer);
211 
212     // Export the qcow2s associated with the given snapshot to the given destination.
213     bool (*snapshotExport)(const char* snapshot,
214                            const char* dest,
215                            void* opaque,
216                            LineConsumerCallback errConsumer);
217 
218     // Get the name of the last loaded snapshot (current snapshot).
219     // Will print "(null)" if the emulator cold booted and loaded no snapshots.
220     bool (*snapshotLastLoaded)(void* opaque,
221                              LineConsumerCallback outConsumer,
222                              LineConsumerCallback errConsumer);
223 
224     // Sets a set of callback to listen for snapshot operations.
225     void (*setSnapshotCallbacks)(void* opaque,
226                                  const SnapshotCallbacks* callbacks);
227 
228     // Sets a protobuf for the snapshot save and load
229     void (*setSnapshotProtobuf)(void* pb);
230     // callbacks to "plug" and "unplug" memory into the provided address range
231     // on fly.
232     void (*mapUserBackedRam)(uint64_t gpa, void* hva, uint64_t size);
233     void (*unmapUserBackedRam)(uint64_t gpa, uint64_t size);
234 
235     // Fills in the supplied |out| with current VM configuration.
236     void (*getVmConfiguration)(VmConfiguration* out);
237 
238     // Notifies QEMU of failed operations according to our own
239     // android::snapshot::FailureReason.
240     void (*setFailureReason)(const char* name, int failureReason);
241 
242     // Notifies QEMU that the emulator is exiting, can impact how
243     // QEMU snapshot save calls work.
244     void (*setExiting)(void);
245 
246     // Allow actual audio on host through to the guest.
247     void (*allowRealAudio)(bool allow);
248 
249     // Get the host address of a guest physical address, if any.
250     void* (*physicalMemoryGetAddr)(uint64_t gpa);
251 
252     // Query whether host audio is allowed.
253     bool (*isRealAudioAllowed)(void);
254 
255     // Set whether to skip snapshotting on exit.
256     void (*setSkipSnapshotSave)(bool used);
257 
258     // Retrieve the state of whether snapshotting is skipped.
259     bool (*isSnapshotSaveSkipped)(void);
260 
261     // Create/register/getinfo for host memory Id's
262     uint64_t (*hostmemRegister)(const struct MemEntry *entry);
263     void (*hostmemUnregister)(uint64_t id);
264     struct HostmemEntry (*hostmemGetInfo)(uint64_t id);
265     EmuRunState (*getRunState)();
266 
267     // virtio display
268     bool (*setDisplay)(int32_t id, int32_t w, int32_t h, uint32_t dpi);
269 
270     // Reset the machine
271     void (*system_shutdown_request)(QemuShutdownCause reason);
272 
273     void (*vulkanInstanceRegister)(uint64_t id, const char* name);
274     void (*vulkanInstanceUnregister)(uint64_t id);
275     // get the list vk app id and name so we might be able to stop them
276     // before saving snapshot
277     void (*vulkanInstanceEnumerate)(uint32_t* pCount, uint64_t* pIds, char** pNames);
278 
279     // Set the reason to skip snapshotting on exit.
280     void (*setSkipSnapshotSaveReason)(SnapshotSkipReason reason);
281 
282     // Get the reason to skip snapshotting on exit.
283     SnapshotSkipReason (*getSkipSnapshotSaveReason)();
284 
285     // Set Vulkan snapshot is actively used, for stats.
286     void (*setStatSnapshotUseVulkan)(void);
287 
288     // Check if Vulkan snapshot is actively used, for stats.
289     bool (*snapshotUseVulkan)();
290 } QAndroidVmOperations;
291 ANDROID_END_HEADER
292