Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 1 | /* |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 21 | #include <sys/cdefs.h> |
| 22 | #include <time.h> |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 23 | |
| 24 | __BEGIN_DECLS |
| 25 | |
| 26 | /** |
| 27 | * Represents a handle on a virtual machine raw config. |
| 28 | */ |
| 29 | typedef 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 45 | * \return A new virtual machine raw config object. On failure (such as out of memory), it aborts. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 46 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 47 | AVirtualMachineRawConfig* _Nonnull AVirtualMachineRawConfig_create() __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 48 | |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 57 | void AVirtualMachineRawConfig_destroy(AVirtualMachineRawConfig* _Nullable config) |
| 58 | __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Set a name of a virtual machine. |
| 62 | * |
| 63 | * \param config a virtual machine config object. |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 64 | * \param name a pointer to a null-terminated, UTF-8 encoded string for the name. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 65 | * |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 66 | * \return If successful, it returns 0. If `name` is not a null-terminated UTF-8 encoded string, |
| 67 | * it returns -EINVAL. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 68 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 69 | int AVirtualMachineRawConfig_setName(AVirtualMachineRawConfig* _Nonnull config, |
| 70 | const char* _Nonnull name) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 71 | |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 77 | * \param instanceIdSize the number of bytes in `instanceId`. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 78 | * |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 79 | * \return If successful, it returns 0. If `instanceIdSize` is incorrect, it returns -EINVAL. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 80 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 81 | int AVirtualMachineRawConfig_setInstanceId(AVirtualMachineRawConfig* _Nonnull config, |
| 82 | const int8_t* _Nonnull instanceId, size_t instanceIdSize) |
| 83 | __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * Set a kernel image of a virtual machine. |
| 87 | * |
| 88 | * \param config a virtual machine config object. |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 89 | * \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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 92 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 93 | void AVirtualMachineRawConfig_setKernel(AVirtualMachineRawConfig* _Nonnull config, int fd) |
| 94 | __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Set an init rd of a virtual machine. |
| 98 | * |
| 99 | * \param config a virtual machine config object. |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 100 | * \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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 103 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 104 | void AVirtualMachineRawConfig_setInitRd(AVirtualMachineRawConfig* _Nonnull config, int fd) |
| 105 | __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Add a disk for a virtual machine. |
| 109 | * |
| 110 | * \param config a virtual machine config object. |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 111 | * \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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 115 | * |
| 116 | * \return If successful, it returns 0. If `fd` is invalid, it returns -EINVAL. |
| 117 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 118 | int AVirtualMachineRawConfig_addDisk(AVirtualMachineRawConfig* _Nonnull config, int fd, |
| 119 | bool writable) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 120 | |
| 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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 127 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 128 | void AVirtualMachineRawConfig_setMemoryMib(AVirtualMachineRawConfig* _Nonnull config, |
| 129 | int32_t memoryMib) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 130 | |
| 131 | /** |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 132 | * Set whether the virtual machine's memory will be protected from the host, so the host can't |
| 133 | * access its memory. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 134 | * |
| 135 | * \param config a virtual machine config object. |
| 136 | * \param protectedVm whether the virtual machine should be protected. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 137 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 138 | void AVirtualMachineRawConfig_setProtectedVm(AVirtualMachineRawConfig* _Nonnull config, |
| 139 | bool protectedVm) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 140 | |
| 141 | /** |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 142 | * Set whether a virtual machine uses memory ballooning. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 143 | * |
| 144 | * \param config a virtual machine config object. |
| 145 | * \param balloon whether the virtual machine should use memory ballooning. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 146 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 147 | void AVirtualMachineRawConfig_setBalloon(AVirtualMachineRawConfig* _Nonnull config, bool balloon) |
| 148 | __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Set whether to use an alternate, hypervisor-specific authentication method |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 152 | * 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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 156 | * |
| 157 | * \return If successful, it returns 0. It returns `-ENOTSUP` if the hypervisor doesn't have an |
| 158 | * alternate auth mode. |
| 159 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 160 | int AVirtualMachineRawConfig_setHypervisorSpecificAuthMethod( |
| 161 | AVirtualMachineRawConfig* _Nonnull config, bool enable) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 162 | |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 169 | * \param rangeStart range start of guest memory addresses |
| 170 | * \param rangeEnd range end of guest memory addresses |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 171 | * |
| 172 | * \return If successful, it returns 0. It returns `-ENOTSUP` if the hypervisor doesn't support |
| 173 | * backing memfd. |
| 174 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 175 | int AVirtualMachineRawConfig_addCustomMemoryBackingFile(AVirtualMachineRawConfig* _Nonnull config, |
| 176 | int fd, uint64_t rangeStart, |
| 177 | uint64_t rangeEnd) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Represents a handle on a virtualization service, responsible for managing virtual machines. |
| 181 | */ |
| 182 | typedef 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 191 | * \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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 195 | * \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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 204 | int AVirtualizationService_create(AVirtualizationService* _Null_unspecified* _Nonnull service, |
| 205 | bool early) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 206 | |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 215 | void AVirtualizationService_destroy(AVirtualizationService* _Nullable service) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Represents a handle on a virtual machine. |
| 219 | */ |
| 220 | typedef struct AVirtualMachine AVirtualMachine; |
| 221 | |
| 222 | /** |
| 223 | * The reason why a virtual machine stopped. |
| 224 | * @see AVirtualMachine_waitForStop |
| 225 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 226 | enum AVirtualMachineStopReason : int32_t { |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 227 | /** |
| 228 | * VirtualizationService died. |
| 229 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 230 | AVIRTUAL_MACHINE_VIRTUALIZATION_SERVICE_DIED = 1, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 231 | /** |
| 232 | * There was an error waiting for the virtual machine. |
| 233 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 234 | AVIRTUAL_MACHINE_INFRASTRUCTURE_ERROR = 2, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 235 | /** |
| 236 | * The virtual machine was killed. |
| 237 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 238 | AVIRTUAL_MACHINE_KILLED = 3, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 239 | /** |
| 240 | * The virtual machine stopped for an unknown reason. |
| 241 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 242 | AVIRTUAL_MACHINE_UNKNOWN = 4, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 243 | /** |
| 244 | * The virtual machine requested to shut down. |
| 245 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 246 | AVIRTUAL_MACHINE_SHUTDOWN = 5, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 247 | /** |
| 248 | * crosvm had an error starting the virtual machine. |
| 249 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 250 | AVIRTUAL_MACHINE_START_FAILED = 6, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 251 | /** |
| 252 | * The virtual machine requested to reboot, possibly as the result of a kernel panic. |
| 253 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 254 | AVIRTUAL_MACHINE_REBOOT = 7, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 255 | /** |
| 256 | * The virtual machine or crosvm crashed. |
| 257 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 258 | AVIRTUAL_MACHINE_CRASH = 8, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 259 | /** |
| 260 | * The pVM firmware failed to verify the VM because the public key doesn't match. |
| 261 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 262 | AVIRTUAL_MACHINE_PVM_FIRMWARE_PUBLIC_KEY_MISMATCH = 9, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 263 | /** |
| 264 | * The pVM firmware failed to verify the VM because the instance image changed. |
| 265 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 266 | AVIRTUAL_MACHINE_PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED = 10, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 267 | /** |
| 268 | * The virtual machine was killed due to hangup. |
| 269 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 270 | AVIRTUAL_MACHINE_HANGUP = 11, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 271 | /** |
| 272 | * VirtualizationService sent a stop reason which was not recognised by the client library. |
| 273 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 274 | AVIRTUAL_MACHINE_UNRECOGNISED = 0, |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 275 | }; |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 300 | int 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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 304 | |
| 305 | /** |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 306 | * 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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 308 | * |
| 309 | * \param vm a handle on a virtual machine. |
| 310 | * |
| 311 | * \return If successful, it returns 0. Otherwise, it returns `-EIO`. |
| 312 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 313 | int AVirtualMachine_start(AVirtualMachine* _Nonnull vm) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 314 | |
| 315 | /** |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 316 | * 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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 327 | * |
| 328 | * \param vm a handle on a virtual machine. |
| 329 | * |
| 330 | * \return If successful, it returns 0. Otherwise, it returns `-EIO`. |
| 331 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 332 | int AVirtualMachine_stop(AVirtualMachine* _Nonnull vm) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 333 | |
| 334 | /** |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 335 | * Wait until a virtual machine stops or the given timeout elapses. |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 336 | * |
| 337 | * \param vm a handle on a virtual machine. |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 338 | * \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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 340 | * |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 341 | * \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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 345 | */ |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 346 | bool AVirtualMachine_waitForStop(AVirtualMachine* _Nonnull vm, |
| 347 | const struct timespec* _Nullable timeout, |
| 348 | enum AVirtualMachineStopReason* _Nonnull reason) |
| 349 | __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 350 | |
| 351 | /** |
Inseob Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 352 | * 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 Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 355 | * |
| 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 Kim | 0606425 | 2024-12-05 17:52:25 +0900 | [diff] [blame^] | 361 | void AVirtualMachine_destroy(AVirtualMachine* _Nullable vm) __INTRODUCED_IN(36); |
Inseob Kim | f3536de | 2024-11-22 14:00:57 +0900 | [diff] [blame] | 362 | |
| 363 | __END_DECLS |