blob: 7ab74315645e1c79e1033a8c3e46f0f83790fa92 [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 Kimf3536de2024-11-22 14:00:57 +0900142 * Set whether to use an alternate, hypervisor-specific authentication method
Inseob Kim06064252024-12-05 17:52:25 +0900143 * for protected VMs.
144 *
145 * This option is discouraged. Prefer to use the default authentication method, which is better
146 * tested and integrated into Android. This option must only be used from the vendor partition.
Inseob Kimf3536de2024-11-22 14:00:57 +0900147 *
148 * \return If successful, it returns 0. It returns `-ENOTSUP` if the hypervisor doesn't have an
149 * alternate auth mode.
150 */
Inseob Kim06064252024-12-05 17:52:25 +0900151int AVirtualMachineRawConfig_setHypervisorSpecificAuthMethod(
152 AVirtualMachineRawConfig* _Nonnull config, bool enable) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900153
154/**
155 * Use the specified fd as the backing memfd for a range of the guest
156 * physical memory.
157 *
158 * \param config a virtual machine config object.
159 * \param fd a memfd
Inseob Kim06064252024-12-05 17:52:25 +0900160 * \param rangeStart range start of guest memory addresses
161 * \param rangeEnd range end of guest memory addresses
Inseob Kimf3536de2024-11-22 14:00:57 +0900162 *
163 * \return If successful, it returns 0. It returns `-ENOTSUP` if the hypervisor doesn't support
164 * backing memfd.
165 */
Inseob Kim06064252024-12-05 17:52:25 +0900166int AVirtualMachineRawConfig_addCustomMemoryBackingFile(AVirtualMachineRawConfig* _Nonnull config,
167 int fd, uint64_t rangeStart,
168 uint64_t rangeEnd) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900169
170/**
171 * Represents a handle on a virtualization service, responsible for managing virtual machines.
172 */
173typedef struct AVirtualizationService AVirtualizationService;
174
175/**
176 * Spawn a new instance of `virtmgr`, a child process that will host the `VirtualizationService`
177 * service, and connect to the child process.
178 *
179 * The caller takes ownership of the returned service object, and is responsible for releasing it
180 * by calling {@link AVirtualizationService_destroy}.
181 *
Inseob Kim06064252024-12-05 17:52:25 +0900182 * \param early set to true when running a service for early virtual machines. Early VMs are
183 * specialized virtual machines that can run even before the `/data` partition is mounted.
184 * Early VMs must be pre-defined in XML files located at `{partition}/etc/avf/early_vms*.xml`, and
185 * clients of early VMs must be pre-installed under the same partition.
Inseob Kimf3536de2024-11-22 14:00:57 +0900186 * \param service an out parameter that will be set to the service handle.
187 *
188 * \return
189 * - If successful, it sets `service` and returns 0.
190 * - If it fails to spawn `virtmgr`, it leaves `service` untouched and returns a negative value
191 * representing the OS error code.
192 * - If it fails to connect to the spawned `virtmgr`, it leaves `service` untouched and returns
193 * `-ECONNREFUSED`.
194 */
Inseob Kim06064252024-12-05 17:52:25 +0900195int AVirtualizationService_create(AVirtualizationService* _Null_unspecified* _Nonnull service,
196 bool early) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900197
198/**
199 * Destroy a VirtualizationService object.
200 *
201 * `AVirtualizationService_destroy` does nothing if `service` is null. A destroyed service object
202 * must not be reused.
203 *
204 * \param service a handle on a virtualization service.
205 */
Inseob Kim06064252024-12-05 17:52:25 +0900206void AVirtualizationService_destroy(AVirtualizationService* _Nullable service) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900207
208/**
209 * Represents a handle on a virtual machine.
210 */
211typedef struct AVirtualMachine AVirtualMachine;
212
213/**
214 * The reason why a virtual machine stopped.
215 * @see AVirtualMachine_waitForStop
216 */
Inseob Kim06064252024-12-05 17:52:25 +0900217enum AVirtualMachineStopReason : int32_t {
Inseob Kimf3536de2024-11-22 14:00:57 +0900218 /**
219 * VirtualizationService died.
220 */
Inseob Kim06064252024-12-05 17:52:25 +0900221 AVIRTUAL_MACHINE_VIRTUALIZATION_SERVICE_DIED = 1,
Inseob Kimf3536de2024-11-22 14:00:57 +0900222 /**
223 * There was an error waiting for the virtual machine.
224 */
Inseob Kim06064252024-12-05 17:52:25 +0900225 AVIRTUAL_MACHINE_INFRASTRUCTURE_ERROR = 2,
Inseob Kimf3536de2024-11-22 14:00:57 +0900226 /**
227 * The virtual machine was killed.
228 */
Inseob Kim06064252024-12-05 17:52:25 +0900229 AVIRTUAL_MACHINE_KILLED = 3,
Inseob Kimf3536de2024-11-22 14:00:57 +0900230 /**
231 * The virtual machine stopped for an unknown reason.
232 */
Inseob Kim06064252024-12-05 17:52:25 +0900233 AVIRTUAL_MACHINE_UNKNOWN = 4,
Inseob Kimf3536de2024-11-22 14:00:57 +0900234 /**
235 * The virtual machine requested to shut down.
236 */
Inseob Kim06064252024-12-05 17:52:25 +0900237 AVIRTUAL_MACHINE_SHUTDOWN = 5,
Inseob Kimf3536de2024-11-22 14:00:57 +0900238 /**
239 * crosvm had an error starting the virtual machine.
240 */
Inseob Kim06064252024-12-05 17:52:25 +0900241 AVIRTUAL_MACHINE_START_FAILED = 6,
Inseob Kimf3536de2024-11-22 14:00:57 +0900242 /**
243 * The virtual machine requested to reboot, possibly as the result of a kernel panic.
244 */
Inseob Kim06064252024-12-05 17:52:25 +0900245 AVIRTUAL_MACHINE_REBOOT = 7,
Inseob Kimf3536de2024-11-22 14:00:57 +0900246 /**
247 * The virtual machine or crosvm crashed.
248 */
Inseob Kim06064252024-12-05 17:52:25 +0900249 AVIRTUAL_MACHINE_CRASH = 8,
Inseob Kimf3536de2024-11-22 14:00:57 +0900250 /**
251 * The pVM firmware failed to verify the VM because the public key doesn't match.
252 */
Inseob Kim06064252024-12-05 17:52:25 +0900253 AVIRTUAL_MACHINE_PVM_FIRMWARE_PUBLIC_KEY_MISMATCH = 9,
Inseob Kimf3536de2024-11-22 14:00:57 +0900254 /**
255 * The pVM firmware failed to verify the VM because the instance image changed.
256 */
Inseob Kim06064252024-12-05 17:52:25 +0900257 AVIRTUAL_MACHINE_PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED = 10,
Inseob Kimf3536de2024-11-22 14:00:57 +0900258 /**
259 * The virtual machine was killed due to hangup.
260 */
Inseob Kim06064252024-12-05 17:52:25 +0900261 AVIRTUAL_MACHINE_HANGUP = 11,
Inseob Kimf3536de2024-11-22 14:00:57 +0900262 /**
263 * VirtualizationService sent a stop reason which was not recognised by the client library.
264 */
Inseob Kim06064252024-12-05 17:52:25 +0900265 AVIRTUAL_MACHINE_UNRECOGNISED = 0,
Inseob Kimf3536de2024-11-22 14:00:57 +0900266};
267
268/**
269 * Create a virtual machine with given raw `config`.
270 *
271 * The created virtual machine is in stopped state. To run the created virtual machine, call
272 * {@link AVirtualMachine_start}.
273 *
274 * The caller takes ownership of the returned virtual machine object, and is responsible for
275 * releasing it by calling {@link AVirtualMachine_destroy}.
276 *
277 * \param service a handle on a virtualization service.
278 * \param config a virtual machine config object. Ownership will always be transferred from the
279 * caller, even if unsuccessful. `config` must not be reused.
280 * \param consoleOutFd a writable file descriptor for the console output, or -1. Ownership will
281 * always be transferred from the caller, even if unsuccessful.
282 * \param consoleInFd a readable file descriptor for the console input, or -1. Ownership will always
283 * be transferred from the caller, even if unsuccessful.
284 * \param logFd a writable file descriptor for the log output, or -1. Ownership will always be
285 * transferred from the caller, even if unsuccessful.
286 * \param vm an out parameter that will be set to the virtual machine handle.
287 *
288 * \return If successful, it sets `vm` and returns 0. Otherwise, it leaves `vm` untouched and
289 * returns `-EIO`.
290 */
Inseob Kim06064252024-12-05 17:52:25 +0900291int AVirtualMachine_createRaw(const AVirtualizationService* _Nonnull service,
292 AVirtualMachineRawConfig* _Nonnull config, int consoleOutFd,
293 int consoleInFd, int logFd,
294 AVirtualMachine* _Null_unspecified* _Nonnull vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900295
296/**
Inseob Kim06064252024-12-05 17:52:25 +0900297 * Start a virtual machine. `AVirtualMachine_start` is synchronous and blocks until the virtual
298 * machine is initialized and free to start executing code, or until an error happens.
Inseob Kimf3536de2024-11-22 14:00:57 +0900299 *
300 * \param vm a handle on a virtual machine.
301 *
302 * \return If successful, it returns 0. Otherwise, it returns `-EIO`.
303 */
Inseob Kim06064252024-12-05 17:52:25 +0900304int AVirtualMachine_start(AVirtualMachine* _Nonnull vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900305
306/**
Inseob Kim06064252024-12-05 17:52:25 +0900307 * Stop a virtual machine. Stopping a virtual machine is like pulling the plug on a real computer;
308 * the machine halts immediately. Software running on the virtual machine is not notified of the
309 * event, the instance might be left in an inconsistent state.
310 *
311 * For a graceful shutdown, you could request the virtual machine to exit itself, and wait for the
312 * virtual machine to stop by `AVirtualMachine_waitForStop`.
313 *
314 * A stopped virtual machine can be re-started by calling `AVirtualMachine_start`.
315 *
316 * `AVirtualMachine_stop` stops a virtual machine by sending a signal to the process. This operation
317 * is synchronous and `AVirtualMachine_stop` may block.
Inseob Kimf3536de2024-11-22 14:00:57 +0900318 *
319 * \param vm a handle on a virtual machine.
320 *
321 * \return If successful, it returns 0. Otherwise, it returns `-EIO`.
322 */
Inseob Kim06064252024-12-05 17:52:25 +0900323int AVirtualMachine_stop(AVirtualMachine* _Nonnull vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900324
325/**
Inseob Kim32e299c2024-12-09 15:27:40 +0900326 * Open a vsock connection to the VM on the given port. The caller takes ownership of the returned
327 * file descriptor, and is responsible for closing the file descriptor.
328 *
329 * This operation is synchronous and `AVirtualMachine_connectVsock` may block.
330 *
331 * \param vm a handle on a virtual machine.
332 * \param port a vsock port number.
333 *
334 * \return If successful, it returns a valid file descriptor. Otherwise, it returns `-EIO`.
335 */
336int AVirtualMachine_connectVsock(AVirtualMachine* _Nonnull vm, uint32_t port) __INTRODUCED_IN(36);
337
338/**
Inseob Kim06064252024-12-05 17:52:25 +0900339 * Wait until a virtual machine stops or the given timeout elapses.
Inseob Kimf3536de2024-11-22 14:00:57 +0900340 *
341 * \param vm a handle on a virtual machine.
Inseob Kim06064252024-12-05 17:52:25 +0900342 * \param timeout the timeout, or null to wait indefinitely.
343 * \param reason An out parameter that will be set to the reason why the virtual machine stopped.
Inseob Kimf3536de2024-11-22 14:00:57 +0900344 *
Inseob Kim06064252024-12-05 17:52:25 +0900345 * \return
346 * - If the virtual machine stops within the timeout (or indefinitely if `timeout` is null), it
347 * sets `reason` and returns true.
348 * - If the timeout expired, it returns `false`.
Inseob Kimf3536de2024-11-22 14:00:57 +0900349 */
Inseob Kim06064252024-12-05 17:52:25 +0900350bool AVirtualMachine_waitForStop(AVirtualMachine* _Nonnull vm,
351 const struct timespec* _Nullable timeout,
352 enum AVirtualMachineStopReason* _Nonnull reason)
353 __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900354
355/**
Inseob Kim06064252024-12-05 17:52:25 +0900356 * Destroy a virtual machine object. If the virtual machine is still running,
357 * `AVirtualMachine_destroy` first stops the virtual machine by sending a signal to the process.
358 * This operation is synchronous and `AVirtualMachine_destroy` may block.
Inseob Kimf3536de2024-11-22 14:00:57 +0900359 *
360 * `AVirtualMachine_destroy` does nothing if `vm` is null. A destroyed virtual machine must not be
361 * reused.
362 *
363 * \param vm a handle on a virtual machine.
364 */
Inseob Kim06064252024-12-05 17:52:25 +0900365void AVirtualMachine_destroy(AVirtualMachine* _Nullable vm) __INTRODUCED_IN(36);
Inseob Kimf3536de2024-11-22 14:00:57 +0900366
367__END_DECLS