1 /*- 2 * Copyright (c) 2011 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * USERBOOT interface versions 29 */ 30 #define USERBOOT_VERSION_1 1 31 #define USERBOOT_VERSION_2 2 32 #define USERBOOT_VERSION_3 3 33 34 /* 35 * Version 4 added more generic callbacks for setting up 36 * registers and descriptors. The callback structure is 37 * backward compatible (new callbacks have been added at 38 * the tail end). 39 */ 40 #define USERBOOT_VERSION_4 4 41 42 /* 43 * Version 5 added a callback for indicating that the guest 44 * should be restarted with a different interpreter. The callback 45 * structure is still backward compatible. 46 */ 47 #define USERBOOT_VERSION_5 5 48 49 /* 50 * Version 6 added a callback for indicating that the guest 51 * is OK with the existing interpreter, so the host can close associated 52 * resources. The callback structure is still backward compatible. 53 */ 54 #define USERBOOT_VERSION_6 6 55 56 /* 57 * Exit codes from the loader 58 */ 59 #define USERBOOT_EXIT_QUIT 1 60 #define USERBOOT_EXIT_REBOOT 2 61 62 struct loader_callbacks { 63 /* 64 * Console i/o 65 */ 66 67 /* 68 * Wait until a key is pressed on the console and then return it 69 */ 70 int (*getc)(void *arg); 71 72 /* 73 * Write the character ch to the console 74 */ 75 void (*putc)(void *arg, int ch); 76 77 /* 78 * Return non-zero if a key can be read from the console 79 */ 80 int (*poll)(void *arg); 81 82 /* 83 * Host filesystem i/o 84 */ 85 86 /* 87 * Open a file in the host filesystem 88 */ 89 int (*open)(void *arg, const char *filename, void **h_return); 90 91 /* 92 * Close a file 93 */ 94 int (*close)(void *arg, void *h); 95 96 /* 97 * Return non-zero if the file is a directory 98 */ 99 int (*isdir)(void *arg, void *h); 100 101 /* 102 * Read size bytes from a file. The number of bytes remaining 103 * in dst after reading is returned in *resid_return 104 */ 105 int (*read)(void *arg, void *h, void *dst, size_t size, 106 size_t *resid_return); 107 108 /* 109 * Read an entry from a directory. The entry's inode number is 110 * returned in *fileno_return, its type in *type_return and 111 * the name length in *namelen_return. The name itself is 112 * copied to the buffer name which must be at least PATH_MAX 113 * in size. 114 */ 115 int (*readdir)(void *arg, void *h, uint32_t *fileno_return, 116 uint8_t *type_return, size_t *namelen_return, char *name); 117 118 /* 119 * Seek to a location within an open file 120 */ 121 int (*seek)(void *arg, void *h, uint64_t offset, 122 int whence); 123 124 /* 125 * Return some stat(2) related information about the file 126 */ 127 int (*stat)(void *arg, void *h, struct stat *stp); 128 129 /* 130 * Disk image i/o 131 */ 132 133 /* 134 * Read from a disk image at the given offset 135 */ 136 int (*diskread)(void *arg, int unit, uint64_t offset, 137 void *dst, size_t size, size_t *resid_return); 138 139 /* 140 * Write to a disk image at the given offset 141 */ 142 int (*diskwrite)(void *arg, int unit, uint64_t offset, 143 void *src, size_t size, size_t *resid_return); 144 145 /* 146 * Guest virtual machine i/o 147 */ 148 149 /* 150 * Copy to the guest address space 151 */ 152 int (*copyin)(void *arg, const void *from, 153 uint64_t to, size_t size); 154 155 /* 156 * Copy from the guest address space 157 */ 158 int (*copyout)(void *arg, uint64_t from, 159 void *to, size_t size); 160 161 /* 162 * Set a guest register value 163 */ 164 void (*setreg)(void *arg, int, uint64_t); 165 166 /* 167 * Set a guest MSR value 168 */ 169 void (*setmsr)(void *arg, int, uint64_t); 170 171 /* 172 * Set a guest CR value 173 */ 174 void (*setcr)(void *arg, int, uint64_t); 175 176 /* 177 * Set the guest GDT address 178 */ 179 void (*setgdt)(void *arg, uint64_t, size_t); 180 181 /* 182 * Transfer control to the guest at the given address 183 */ 184 void (*exec)(void *arg, uint64_t pc); 185 186 /* 187 * Misc 188 */ 189 190 /* 191 * Sleep for usec microseconds 192 */ 193 void (*delay)(void *arg, int usec); 194 195 /* 196 * Exit with the given exit code 197 */ 198 void (*exit)(void *arg, int v); 199 200 /* 201 * Return guest physical memory map details 202 */ 203 void (*getmem)(void *arg, uint64_t *lowmem, 204 uint64_t *highmem); 205 206 /* 207 * ioctl interface to the disk device 208 */ 209 int (*diskioctl)(void *arg, int unit, u_long cmd, 210 void *data); 211 212 /* 213 * Returns an environment variable in the form "name=value". 214 * 215 * If there are no more variables that need to be set in the 216 * loader environment then return NULL. 217 * 218 * 'num' is used as a handle for the callback to identify which 219 * environment variable to return next. It will begin at 0 and 220 * each invocation will add 1 to the previous value of 'num'. 221 */ 222 char * (*getenv)(void *arg, int num); 223 224 /* 225 * Version 4 additions. 226 */ 227 int (*vm_set_register)(void *arg, int vcpu, int reg, uint64_t val); 228 int (*vm_set_desc)(void *arg, int vcpu, int reg, uint64_t base, 229 u_int limit, u_int access); 230 231 /* 232 * Version 5 addition. 233 */ 234 void (*swap_interpreter)(void *arg, const char *interp); 235 236 /* 237 * Version 6 addition. 238 */ 239 void (*accept_interpreter)(void *arg); 240 }; 241