blob: ac46fca914b05132f16faabae14ab2ac75524a21 [file] [log] [blame]
Inseob Kimf3536de2024-11-22 14:00:57 +09001/*
2 * Copyright 2024 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#pragma once
17
18#include <stdbool.h>
19#include <stdint.h>
20#include <stdlib.h>
Inseob Kim06064252024-12-05 17:52:25 +090021#include <sys/cdefs.h>
22#include <time.h>
Inseob Kimf3536de2024-11-22 14:00:57 +090023
24__BEGIN_DECLS
25
26/**
27 * Represents a handle on a virtual machine raw config.
28 */
29typedef struct AVirtualMachineRawConfig AVirtualMachineRawConfig;
30
31/**
32 * Create a new virtual machine raw config object with no properties.
33 *
34 * This only creates the raw config object. `name` and `kernel` must be set with
35 * calls to {@link AVirtualMachineRawConfig_setName} and {@link AVirtualMachineRawConfig_setKernel}.
36 * Other properties, set by {@link AVirtualMachineRawConfig_setMemoryMib},
37 * {@link AVirtualMachineRawConfig_setInitRd}, {@link AVirtualMachineRawConfig_addDisk},
38 * {@link AVirtualMachineRawConfig_setProtectedVm}, and {@link AVirtualMachineRawConfig_setBalloon}
39 * are optional.
40 *
41 * The caller takes ownership of the returned raw config object, and is responsible for creating a
42 * VM by calling {@link AVirtualMachine_createRaw} or releasing it by calling
43 * {@link AVirtualMachineRawConfig_destroy}.
44 *
Inseob Kim06064252024-12-05 17:52:25 +090045 * \return A new virtual machine raw config object. On failure (such as out of memory), it aborts.
Inseob Kimf3536de2024-11-22 14:00:57 +090046 */
Inseob Kim06064252024-12-05 17:52:25 +090047AVirtualMachineRawConfig* _Nonnull AVirtualMachineRawConfig_create() __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +090048
49/**
50 * Destroy a virtual machine config object.
51 *
52 * \param config a virtual machine config object.
53 *
54 * `AVirtualMachineRawConfig_destroy` does nothing if `config` is null. A destroyed config object
55 * must not be reused.
56 */
Inseob Kim06064252024-12-05 17:52:25 +090057void AVirtualMachineRawConfig_destroy(AVirtualMachineRawConfig* _Nullable config)
58 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +090059
60/**
61 * Set a name of a virtual machine.
62 *
63 * \param config a virtual machine config object.
Inseob Kim06064252024-12-05 17:52:25 +090064 * \param name a pointer to a null-terminated, UTF-8 encoded string for the name.
Inseob Kimf3536de2024-11-22 14:00:57 +090065 *
Inseob Kim06064252024-12-05 17:52:25 +090066 * \return If successful, it returns 0. If `name` is not a null-terminated UTF-8 encoded string,
67 * it returns -EINVAL.
Inseob Kimf3536de2024-11-22 14:00:57 +090068 */
Inseob Kim06064252024-12-05 17:52:25 +090069int AVirtualMachineRawConfig_setName(AVirtualMachineRawConfig* _Nonnull config,
70 const char* _Nonnull name) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +090071
72/**
73 * Set an instance ID of a virtual machine.
74 *
75 * \param config a virtual machine config object.
76 * \param instanceId a pointer to a 64-byte buffer for the instance ID.
Inseob Kim06064252024-12-05 17:52:25 +090077 * \param instanceIdSize the number of bytes in `instanceId`.
Inseob Kimf3536de2024-11-22 14:00:57 +090078 *
Inseob Kim06064252024-12-05 17:52:25 +090079 * \return If successful, it returns 0. If `instanceIdSize` is incorrect, it returns -EINVAL.
Inseob Kimf3536de2024-11-22 14:00:57 +090080 */
Inseob Kim06064252024-12-05 17:52:25 +090081int AVirtualMachineRawConfig_setInstanceId(AVirtualMachineRawConfig* _Nonnull config,
82 const int8_t* _Nonnull instanceId, size_t instanceIdSize)
83 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +090084
85/**
86 * Set a kernel image of a virtual machine.
87 *
88 * \param config a virtual machine config object.
Inseob Kim06064252024-12-05 17:52:25 +090089 * \param fd a readable, seekable, and sized (i.e. report a valid size using fstat()) file
90 * descriptor containing the kernel image, or -1 to unset. `AVirtualMachineRawConfig_setKernel`
91 * takes ownership of `fd`.
Inseob Kimf3536de2024-11-22 14:00:57 +090092 */
Inseob Kim06064252024-12-05 17:52:25 +090093void AVirtualMachineRawConfig_setKernel(AVirtualMachineRawConfig* _Nonnull config, int fd)
94 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +090095
96/**
97 * Set an init rd of a virtual machine.
98 *
99 * \param config a virtual machine config object.
Inseob Kim06064252024-12-05 17:52:25 +0900100 * \param fd a readable, seekable, and sized (i.e. report a valid size using fstat()) file
101 * descriptor containing the init rd image, or -1 to unset. `AVirtualMachineRawConfig_setInitRd`
102 * takes ownership of `fd`.
Inseob Kimf3536de2024-11-22 14:00:57 +0900103 */
Inseob Kim06064252024-12-05 17:52:25 +0900104void AVirtualMachineRawConfig_setInitRd(AVirtualMachineRawConfig* _Nonnull config, int fd)
105 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900106
107/**
108 * Add a disk for a virtual machine.
109 *
110 * \param config a virtual machine config object.
Inseob Kim06064252024-12-05 17:52:25 +0900111 * \param fd a readable, seekable, and sized (i.e. report a valid size using fstat()) file
112 * descriptor containing the disk. `fd` must be writable if If `writable` is true.
113 * `AVirtualMachineRawConfig_addDisk` takes ownership of `fd`.
114 * \param writable whether this disk should be writable by the virtual machine.
Inseob Kimf3536de2024-11-22 14:00:57 +0900115 *
116 * \return If successful, it returns 0. If `fd` is invalid, it returns -EINVAL.
117 */
Inseob Kim06064252024-12-05 17:52:25 +0900118int AVirtualMachineRawConfig_addDisk(AVirtualMachineRawConfig* _Nonnull config, int fd,
119 bool writable) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900120
121/**
122 * Set how much memory will be given to a virtual machine.
123 *
124 * \param config a virtual machine config object.
125 * \param memoryMib the amount of RAM to give the virtual machine, in MiB. 0 or negative to use the
126 * default.
Inseob Kimf3536de2024-11-22 14:00:57 +0900127 */
Inseob Kim06064252024-12-05 17:52:25 +0900128void AVirtualMachineRawConfig_setMemoryMib(AVirtualMachineRawConfig* _Nonnull config,
129 int32_t memoryMib) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900130
131/**
Inseob Kim06064252024-12-05 17:52:25 +0900132 * Set whether the virtual machine's memory will be protected from the host, so the host can't
133 * access its memory.
Inseob Kimf3536de2024-11-22 14:00:57 +0900134 *
135 * \param config a virtual machine config object.
136 * \param protectedVm whether the virtual machine should be protected.
Inseob Kimf3536de2024-11-22 14:00:57 +0900137 */
Inseob Kim06064252024-12-05 17:52:25 +0900138void AVirtualMachineRawConfig_setProtectedVm(AVirtualMachineRawConfig* _Nonnull config,
139 bool protectedVm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900140
141/**
Inseob Kim06064252024-12-05 17:52:25 +0900142 * Set whether a virtual machine uses memory ballooning.
Inseob Kimf3536de2024-11-22 14:00:57 +0900143 *
144 * \param config a virtual machine config object.
145 * \param balloon whether the virtual machine should use memory ballooning.
Inseob Kimf3536de2024-11-22 14:00:57 +0900146 */
Inseob Kim06064252024-12-05 17:52:25 +0900147void AVirtualMachineRawConfig_setBalloon(AVirtualMachineRawConfig* _Nonnull config, bool balloon)
148 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900149
150/**
151 * Set whether to use an alternate, hypervisor-specific authentication method
Inseob Kim06064252024-12-05 17:52:25 +0900152 * for protected VMs.
153 *
154 * This option is discouraged. Prefer to use the default authentication method, which is better
155 * tested and integrated into Android. This option must only be used from the vendor partition.
Inseob Kimf3536de2024-11-22 14:00:57 +0900156 *
157 * \return If successful, it returns 0. It returns `-ENOTSUP` if the hypervisor doesn't have an
158 * alternate auth mode.
159 */
Inseob Kim06064252024-12-05 17:52:25 +0900160int AVirtualMachineRawConfig_setHypervisorSpecificAuthMethod(
161 AVirtualMachineRawConfig* _Nonnull config, bool enable) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900162
163/**
164 * Use the specified fd as the backing memfd for a range of the guest
165 * physical memory.
166 *
167 * \param config a virtual machine config object.
168 * \param fd a memfd
Inseob Kim06064252024-12-05 17:52:25 +0900169 * \param rangeStart range start of guest memory addresses
170 * \param rangeEnd range end of guest memory addresses
Inseob Kimf3536de2024-11-22 14:00:57 +0900171 *
172 * \return If successful, it returns 0. It returns `-ENOTSUP` if the hypervisor doesn't support
173 * backing memfd.
174 */
Inseob Kim06064252024-12-05 17:52:25 +0900175int AVirtualMachineRawConfig_addCustomMemoryBackingFile(AVirtualMachineRawConfig* _Nonnull config,
176 int fd, uint64_t rangeStart,
177 uint64_t rangeEnd) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900178
179/**
180 * Represents a handle on a virtualization service, responsible for managing virtual machines.
181 */
182typedef struct AVirtualizationService AVirtualizationService;
183
184/**
185 * Spawn a new instance of `virtmgr`, a child process that will host the `VirtualizationService`
186 * service, and connect to the child process.
187 *
188 * The caller takes ownership of the returned service object, and is responsible for releasing it
189 * by calling {@link AVirtualizationService_destroy}.
190 *
Inseob Kim06064252024-12-05 17:52:25 +0900191 * \param early set to true when running a service for early virtual machines. Early VMs are
192 * specialized virtual machines that can run even before the `/data` partition is mounted.
193 * Early VMs must be pre-defined in XML files located at `{partition}/etc/avf/early_vms*.xml`, and
194 * clients of early VMs must be pre-installed under the same partition.
Inseob Kimf3536de2024-11-22 14:00:57 +0900195 * \param service an out parameter that will be set to the service handle.
196 *
197 * \return
198 * - If successful, it sets `service` and returns 0.
199 * - If it fails to spawn `virtmgr`, it leaves `service` untouched and returns a negative value
200 * representing the OS error code.
201 * - If it fails to connect to the spawned `virtmgr`, it leaves `service` untouched and returns
202 * `-ECONNREFUSED`.
203 */
Inseob Kim06064252024-12-05 17:52:25 +0900204int AVirtualizationService_create(AVirtualizationService* _Null_unspecified* _Nonnull service,
205 bool early) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900206
207/**
208 * Destroy a VirtualizationService object.
209 *
210 * `AVirtualizationService_destroy` does nothing if `service` is null. A destroyed service object
211 * must not be reused.
212 *
213 * \param service a handle on a virtualization service.
214 */
Inseob Kim06064252024-12-05 17:52:25 +0900215void AVirtualizationService_destroy(AVirtualizationService* _Nullable service) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900216
217/**
218 * Represents a handle on a virtual machine.
219 */
220typedef struct AVirtualMachine AVirtualMachine;
221
222/**
223 * The reason why a virtual machine stopped.
224 * @see AVirtualMachine_waitForStop
225 */
Inseob Kim06064252024-12-05 17:52:25 +0900226enum AVirtualMachineStopReason : int32_t {
Inseob Kimf3536de2024-11-22 14:00:57 +0900227 /**
228 * VirtualizationService died.
229 */
Inseob Kim06064252024-12-05 17:52:25 +0900230 AVIRTUAL_MACHINE_VIRTUALIZATION_SERVICE_DIED = 1,
Inseob Kimf3536de2024-11-22 14:00:57 +0900231 /**
232 * There was an error waiting for the virtual machine.
233 */
Inseob Kim06064252024-12-05 17:52:25 +0900234 AVIRTUAL_MACHINE_INFRASTRUCTURE_ERROR = 2,
Inseob Kimf3536de2024-11-22 14:00:57 +0900235 /**
236 * The virtual machine was killed.
237 */
Inseob Kim06064252024-12-05 17:52:25 +0900238 AVIRTUAL_MACHINE_KILLED = 3,
Inseob Kimf3536de2024-11-22 14:00:57 +0900239 /**
240 * The virtual machine stopped for an unknown reason.
241 */
Inseob Kim06064252024-12-05 17:52:25 +0900242 AVIRTUAL_MACHINE_UNKNOWN = 4,
Inseob Kimf3536de2024-11-22 14:00:57 +0900243 /**
244 * The virtual machine requested to shut down.
245 */
Inseob Kim06064252024-12-05 17:52:25 +0900246 AVIRTUAL_MACHINE_SHUTDOWN = 5,
Inseob Kimf3536de2024-11-22 14:00:57 +0900247 /**
248 * crosvm had an error starting the virtual machine.
249 */
Inseob Kim06064252024-12-05 17:52:25 +0900250 AVIRTUAL_MACHINE_START_FAILED = 6,
Inseob Kimf3536de2024-11-22 14:00:57 +0900251 /**
252 * The virtual machine requested to reboot, possibly as the result of a kernel panic.
253 */
Inseob Kim06064252024-12-05 17:52:25 +0900254 AVIRTUAL_MACHINE_REBOOT = 7,
Inseob Kimf3536de2024-11-22 14:00:57 +0900255 /**
256 * The virtual machine or crosvm crashed.
257 */
Inseob Kim06064252024-12-05 17:52:25 +0900258 AVIRTUAL_MACHINE_CRASH = 8,
Inseob Kimf3536de2024-11-22 14:00:57 +0900259 /**
260 * The pVM firmware failed to verify the VM because the public key doesn't match.
261 */
Inseob Kim06064252024-12-05 17:52:25 +0900262 AVIRTUAL_MACHINE_PVM_FIRMWARE_PUBLIC_KEY_MISMATCH = 9,
Inseob Kimf3536de2024-11-22 14:00:57 +0900263 /**
264 * The pVM firmware failed to verify the VM because the instance image changed.
265 */
Inseob Kim06064252024-12-05 17:52:25 +0900266 AVIRTUAL_MACHINE_PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED = 10,
Inseob Kimf3536de2024-11-22 14:00:57 +0900267 /**
268 * The virtual machine was killed due to hangup.
269 */
Inseob Kim06064252024-12-05 17:52:25 +0900270 AVIRTUAL_MACHINE_HANGUP = 11,
Inseob Kimf3536de2024-11-22 14:00:57 +0900271 /**
272 * VirtualizationService sent a stop reason which was not recognised by the client library.
273 */
Inseob Kim06064252024-12-05 17:52:25 +0900274 AVIRTUAL_MACHINE_UNRECOGNISED = 0,
Inseob Kimf3536de2024-11-22 14:00:57 +0900275};
276
277/**
278 * Create a virtual machine with given raw `config`.
279 *
280 * The created virtual machine is in stopped state. To run the created virtual machine, call
281 * {@link AVirtualMachine_start}.
282 *
283 * The caller takes ownership of the returned virtual machine object, and is responsible for
284 * releasing it by calling {@link AVirtualMachine_destroy}.
285 *
286 * \param service a handle on a virtualization service.
287 * \param config a virtual machine config object. Ownership will always be transferred from the
288 * caller, even if unsuccessful. `config` must not be reused.
289 * \param consoleOutFd a writable file descriptor for the console output, or -1. Ownership will
290 * always be transferred from the caller, even if unsuccessful.
291 * \param consoleInFd a readable file descriptor for the console input, or -1. Ownership will always
292 * be transferred from the caller, even if unsuccessful.
293 * \param logFd a writable file descriptor for the log output, or -1. Ownership will always be
294 * transferred from the caller, even if unsuccessful.
295 * \param vm an out parameter that will be set to the virtual machine handle.
296 *
297 * \return If successful, it sets `vm` and returns 0. Otherwise, it leaves `vm` untouched and
298 * returns `-EIO`.
299 */
Inseob Kim06064252024-12-05 17:52:25 +0900300int AVirtualMachine_createRaw(const AVirtualizationService* _Nonnull service,
301 AVirtualMachineRawConfig* _Nonnull config, int consoleOutFd,
302 int consoleInFd, int logFd,
303 AVirtualMachine* _Null_unspecified* _Nonnull vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900304
305/**
Inseob Kim06064252024-12-05 17:52:25 +0900306 * Start a virtual machine. `AVirtualMachine_start` is synchronous and blocks until the virtual
307 * machine is initialized and free to start executing code, or until an error happens.
Inseob Kimf3536de2024-11-22 14:00:57 +0900308 *
309 * \param vm a handle on a virtual machine.
310 *
311 * \return If successful, it returns 0. Otherwise, it returns `-EIO`.
312 */
Inseob Kim06064252024-12-05 17:52:25 +0900313int AVirtualMachine_start(AVirtualMachine* _Nonnull vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900314
315/**
Inseob Kim06064252024-12-05 17:52:25 +0900316 * Stop a virtual machine. Stopping a virtual machine is like pulling the plug on a real computer;
317 * the machine halts immediately. Software running on the virtual machine is not notified of the
318 * event, the instance might be left in an inconsistent state.
319 *
320 * For a graceful shutdown, you could request the virtual machine to exit itself, and wait for the
321 * virtual machine to stop by `AVirtualMachine_waitForStop`.
322 *
323 * A stopped virtual machine can be re-started by calling `AVirtualMachine_start`.
324 *
325 * `AVirtualMachine_stop` stops a virtual machine by sending a signal to the process. This operation
326 * is synchronous and `AVirtualMachine_stop` may block.
Inseob Kimf3536de2024-11-22 14:00:57 +0900327 *
328 * \param vm a handle on a virtual machine.
329 *
330 * \return If successful, it returns 0. Otherwise, it returns `-EIO`.
331 */
Inseob Kim06064252024-12-05 17:52:25 +0900332int AVirtualMachine_stop(AVirtualMachine* _Nonnull vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900333
334/**
Inseob Kim06064252024-12-05 17:52:25 +0900335 * Wait until a virtual machine stops or the given timeout elapses.
Inseob Kimf3536de2024-11-22 14:00:57 +0900336 *
337 * \param vm a handle on a virtual machine.
Inseob Kim06064252024-12-05 17:52:25 +0900338 * \param timeout the timeout, or null to wait indefinitely.
339 * \param reason An out parameter that will be set to the reason why the virtual machine stopped.
Inseob Kimf3536de2024-11-22 14:00:57 +0900340 *
Inseob Kim06064252024-12-05 17:52:25 +0900341 * \return
342 * - If the virtual machine stops within the timeout (or indefinitely if `timeout` is null), it
343 * sets `reason` and returns true.
344 * - If the timeout expired, it returns `false`.
Inseob Kimf3536de2024-11-22 14:00:57 +0900345 */
Inseob Kim06064252024-12-05 17:52:25 +0900346bool AVirtualMachine_waitForStop(AVirtualMachine* _Nonnull vm,
347 const struct timespec* _Nullable timeout,
348 enum AVirtualMachineStopReason* _Nonnull reason)
349 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900350
351/**
Inseob Kim06064252024-12-05 17:52:25 +0900352 * Destroy a virtual machine object. If the virtual machine is still running,
353 * `AVirtualMachine_destroy` first stops the virtual machine by sending a signal to the process.
354 * This operation is synchronous and `AVirtualMachine_destroy` may block.
Inseob Kimf3536de2024-11-22 14:00:57 +0900355 *
356 * `AVirtualMachine_destroy` does nothing if `vm` is null. A destroyed virtual machine must not be
357 * reused.
358 *
359 * \param vm a handle on a virtual machine.
360 */
Inseob Kim06064252024-12-05 17:52:25 +0900361void AVirtualMachine_destroy(AVirtualMachine* _Nullable vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900362
363__END_DECLS