• 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 <stdbool.h>
18 #include <stdint.h>
19 #include <inttypes.h>
20 
21 // Caching types
22 #define MAP_CACHE_MASK      0x0f
23 #define MAP_CACHE_NONE      0x00
24 #define MAP_CACHE_CACHED    0x01
25 #define MAP_CACHE_UNCACHED  0x02
26 #define MAP_CACHE_WC        0x03
27 
28 // This file includes interfaces to VMMs.
29 
30 // A struct describing the information about host memory associated
31 // with a host memory id. Used with virtio-gpu-next.
32 struct HostmemEntry {
33     uint64_t id;
34     uint64_t hva;
35     uint64_t size;
36     uint32_t caching;
37 };
38 
39 // Called by hostmemRegister(..)
40 struct MemEntry {
41     uint64_t hva;
42     uint64_t size;
43     uint32_t register_fixed;
44     uint64_t fixed_id;
45     uint32_t caching;
46 };
47 
48 // A callback to consume a single line of output (including newline).
49 // |opque| is a handle to a context object. Functions that use this callback
50 // will also take a context handle that will be passed to the callback.
51 // |buff| contains the data to be consumed, of length |len|.
52 // The function should return the number of chars consumed successfully.
53 typedef int (*LineConsumerCallback)(void* opaque, const char* buff, int len);
54 
55 // Enumeration of various causes for shutdown. Please keep this in sync
56 // with the similar enum in include/sysem/sysemu.h.
57 typedef enum QemuShutdownCause {
58     QEMU_SHUTDOWN_CAUSE_NONE,              /* No shutdown request pending */
59     QEMU_SHUTDOWN_CAUSE_HOST_ERROR,        /* An error prevents further use of guest */
60     QEMU_SHUTDOWN_CAUSE_HOST_QMP,          /* Reaction to a QMP command, like 'quit' */
61     QEMU_SHUTDOWN_CAUSE_HOST_SIGNAL,       /* Reaction to a signal, such as SIGINT */
62     QEMU_SHUTDOWN_CAUSE_HOST_UI,           /* Reaction to UI event, like window close */
63     QEMU_SHUTDOWN_CAUSE_GUEST_SHUTDOWN,    /* Guest shutdown/suspend request, via
64                                               ACPI or other hardware-specific means */
65     QEMU_SHUTDOWN_CAUSE_GUEST_RESET,       /* Guest reset request, and command line
66                                               turns that into a shutdown */
67     QEMU_SHUTDOWN_CAUSE_GUEST_PANIC,       /* Guest panicked, and command line turns
68                                               that into a shutdown */
69     QEMU_SHUTDOWN_CAUSE__MAX,
70 } QemuShutdownCause;
71 
72 #define SHUTDOWN_CAUSE_STATIC_ASSERT_ERROR_MESSAGE "QEMU shutdown cause values differ from AndroidEmu's!" \
73 
74 #define SHUTDOWN_CAUSE_STATIC_MATCH(origCause) \
75     static_assert((int)(QEMU_##origCause) == (int)(origCause), SHUTDOWN_CAUSE_STATIC_ASSERT_ERROR_MESSAGE);
76 
77 #define STATIC_ASSERT_SHUTDOWN_CAUSE_MATCHES \
78     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_NONE) \
79     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_ERROR) \
80     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_QMP) \
81     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_SIGNAL) \
82     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_HOST_UI) \
83     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_GUEST_SHUTDOWN) \
84     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_GUEST_RESET) \
85     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE_GUEST_PANIC) \
86     SHUTDOWN_CAUSE_STATIC_MATCH(SHUTDOWN_CAUSE__MAX) \
87 
88 typedef struct {
89     int (*onStart)(void* opaque, const char* name);
90     void (*onEnd)(void* opaque, const char* name, int res);
91     void (*onQuickFail)(void* opaque, const char* name, int res);
92     bool (*isCanceled)(void* opaque, const char* name);
93 } SnapshotCallbackSet;
94 
95 typedef enum {
96     SNAPSHOT_SAVE,
97     SNAPSHOT_LOAD,
98     SNAPSHOT_DEL,
99     SNAPSHOT_OPS_COUNT
100 } SnapshotOperation;
101 
102 struct SnapshotRamBlock;
103 
104 typedef struct {
105     void (*registerBlock)(void* opaque,
106                           SnapshotOperation operation,
107                           const struct SnapshotRamBlock* block);
108     int (*startLoading)(void* opaque);
109     void (*savePage)(void* opaque,
110                      int64_t blockOffset,
111                      int64_t pageOffset,
112                      int32_t size);
113     int (*savingComplete)(void* opaque);
114     void (*loadRam)(void* opaque,
115                     void* hostRamPtr,
116                     uint64_t size);
117 } SnapshotRamCallbacks;
118 
119 typedef struct {
120     SnapshotCallbackSet ops[SNAPSHOT_OPS_COUNT];
121     SnapshotRamCallbacks ramOps;
122 } SnapshotCallbacks;
123 
124 typedef enum {
125     HV_UNKNOWN,
126     HV_NONE,
127     HV_KVM,
128     HV_HAXM,
129     HV_HVF,
130     HV_WHPX,
131 } VmHypervisorType;
132 
133 typedef struct {
134     VmHypervisorType hypervisorType;
135     int32_t numberOfCpuCores;
136     int64_t ramSizeBytes;
137 } VmConfiguration;
138 
139 typedef enum EmuRunState {
140     QEMU_RUN_STATE_DEBUG = 0,
141     QEMU_RUN_STATE_INMIGRATE = 1,
142     QEMU_RUN_STATE_INTERNAL_ERROR = 2,
143     QEMU_RUN_STATE_IO_ERROR = 3,
144     QEMU_RUN_STATE_PAUSED = 4,
145     QEMU_RUN_STATE_POSTMIGRATE = 5,
146     QEMU_RUN_STATE_PRELAUNCH = 6,
147     QEMU_RUN_STATE_FINISH_MIGRATE = 7,
148     QEMU_RUN_STATE_RESTORE_VM = 8,
149     QEMU_RUN_STATE_RUNNING = 9,
150     QEMU_RUN_STATE_SAVE_VM = 10,
151     QEMU_RUN_STATE_SHUTDOWN = 11,
152     QEMU_RUN_STATE_SUSPENDED = 12,
153     QEMU_RUN_STATE_WATCHDOG = 13,
154     QEMU_RUN_STATE_GUEST_PANICKED = 14,
155     QEMU_RUN_STATE_COLO = 15,
156     QEMU_RUN_STATE__MAX = 16,
157 } EmuRunState;
158 
159 // C interface to expose Qemu implementations of common VM related operations.
160 typedef struct QAndroidVmOperations {
161     bool (*vmStop)(void);
162     bool (*vmStart)(void);
163     void (*vmReset)(void);
164     void (*vmShutdown)(void);
165     bool (*vmPause)(void);
166     bool (*vmResume)(void);
167 
168     bool (*vmIsRunning)(void);
169 
170     // Snapshot-related VM operations.
171     // |outConsuer| and |errConsumer| are used to report output / error
172     // respectively. Each line of output is newline terminated and results in
173     // one call to the callback. |opaque| is passed to the callbacks as context.
174     // Returns true on success, false on failure.
175     bool (*snapshotList)(void* opaque,
176                          LineConsumerCallback outConsumer,
177                          LineConsumerCallback errConsumer);
178     bool (*snapshotSave)(const char* name,
179                          void* opaque,
180                          LineConsumerCallback errConsumer);
181     bool (*snapshotLoad)(const char* name,
182                          void* opaque,
183                          LineConsumerCallback errConsumer);
184     bool (*snapshotDelete)(const char* name,
185                            void* opaque,
186                            LineConsumerCallback errConsumer);
187     bool (*snapshotRemap)(bool shared,
188                           void* opaque,
189                           LineConsumerCallback errConsumer);
190 
191     // Export the qcow2s associated with the given snapshot to the given destination.
192     bool (*snapshotExport)(const char* snapshot,
193                            const char* dest,
194                            void* opaque,
195                            LineConsumerCallback errConsumer);
196 
197     // Sets a set of callback to listen for snapshot operations.
198     void (*setSnapshotCallbacks)(void* opaque,
199                                  const SnapshotCallbacks* callbacks);
200 
201     // callbacks to "plug" and "unplug" memory into the provided address range
202     // on fly.
203     void (*mapUserBackedRam)(uint64_t gpa, void* hva, uint64_t size);
204     void (*unmapUserBackedRam)(uint64_t gpa, uint64_t size);
205 
206     // Fills in the supplied |out| with current VM configuration.
207     void (*getVmConfiguration)(VmConfiguration* out);
208 
209     // Notifies QEMU of failed operations according to our own
210     // android::snapshot::FailureReason.
211     void (*setFailureReason)(const char* name, int failureReason);
212 
213     // Notifies QEMU that the emulator is exiting, can impact how
214     // QEMU snapshot save calls work.
215     void (*setExiting)(void);
216 
217     // Allow actual audio on host through to the guest.
218     void (*allowRealAudio)(bool allow);
219 
220     // Get the host address of a guest physical address, if any.
221     void* (*physicalMemoryGetAddr)(uint64_t gpa);
222 
223     // Query whether host audio is allowed.
224     bool (*isRealAudioAllowed)(void);
225 
226     // Set whether to skip snapshotting on exit.
227     void (*setSkipSnapshotSave)(bool used);
228 
229     // Retrieve the state of whether snapshotting is skipped.
230     bool (*isSnapshotSaveSkipped)(void);
231 
232     // Create/register/getinfo for host memory Id's
233     uint64_t (*hostmemRegister)(const struct MemEntry *entry);
234     void (*hostmemUnregister)(uint64_t id);
235     struct HostmemEntry (*hostmemGetInfo)(uint64_t id);
236     EmuRunState (*getRunState)();
237 
238 } QAndroidVmOperations;
239 
240 #ifdef _MSC_VER
241 # ifdef BUILDING_EMUGL_COMMON_SHARED
242 #  define EMUGL_COMMON_API __declspec(dllexport)
243 # else
244 #  define EMUGL_COMMON_API __declspec(dllimport)
245 #endif
246 #else
247 # define EMUGL_COMMON_API
248 #endif
249 
250 EMUGL_COMMON_API void set_emugl_vm_operations(const QAndroidVmOperations &vm_operations);
251 
252 EMUGL_COMMON_API const QAndroidVmOperations &get_emugl_vm_operations();
253 
254 #undef EMUGL_COMMON_API
255