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