Print this page
13275 bhyve needs richer INIT/SIPI support
Reviewed by: Robert Mustacchi <rm@fingolfin.org>
Approved by: Gordon Ross <gordon.w.ross@gmail.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libvmmapi/common/vmmapi.c
+++ new/usr/src/lib/libvmmapi/common/vmmapi.c
1 1 /*-
2 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 3 *
4 4 * Copyright (c) 2011 NetApp, Inc.
5 5 * All rights reserved.
6 6 *
7 7 * Redistribution and use in source and binary forms, with or without
8 8 * modification, are permitted provided that the following conditions
9 9 * are met:
10 10 * 1. Redistributions of source code must retain the above copyright
11 11 * notice, this list of conditions and the following disclaimer.
12 12 * 2. Redistributions in binary form must reproduce the above copyright
13 13 * notice, this list of conditions and the following disclaimer in the
14 14 * documentation and/or other materials provided with the distribution.
15 15 *
16 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 26 * SUCH DAMAGE.
27 27 *
28 28 * $FreeBSD$
29 29 */
30 30 /*
31 31 * This file and its contents are supplied under the terms of the
32 32 * Common Development and Distribution License ("CDDL"), version 1.0.
33 33 * You may only use this file in accordance with the terms of version
34 34 * 1.0 of the CDDL.
35 35 *
36 36 * A full copy of the text of the CDDL should have accompanied this
37 37 * source. A copy of the CDDL is also available via the Internet at
38 38 * http://www.illumos.org/license/CDDL.
39 39 *
40 40 * Copyright 2015 Pluribus Networks Inc.
41 41 * Copyright 2019 Joyent, Inc.
42 42 * Copyright 2020 Oxide Computer Company
43 43 */
44 44
45 45 #include <sys/cdefs.h>
46 46 __FBSDID("$FreeBSD$");
47 47
48 48 #include <sys/param.h>
49 49 #include <sys/sysctl.h>
50 50 #include <sys/ioctl.h>
51 51 #ifdef __FreeBSD__
52 52 #include <sys/linker.h>
53 53 #endif
54 54 #include <sys/mman.h>
55 55 #include <sys/module.h>
56 56 #include <sys/_iovec.h>
57 57 #include <sys/cpuset.h>
58 58
59 59 #include <x86/segments.h>
60 60 #include <machine/specialreg.h>
61 61
62 62 #include <errno.h>
63 63 #include <stdio.h>
64 64 #include <stdlib.h>
65 65 #include <assert.h>
66 66 #include <string.h>
67 67 #include <fcntl.h>
68 68 #include <unistd.h>
69 69
70 70 #include <libutil.h>
71 71
72 72 #include <machine/vmm.h>
73 73 #include <machine/vmm_dev.h>
74 74
75 75 #include "vmmapi.h"
76 76
77 77 #define MB (1024 * 1024UL)
78 78 #define GB (1024 * 1024 * 1024UL)
79 79
80 80 #ifndef __FreeBSD__
81 81 /* shim to no-op for now */
82 82 #define MAP_NOCORE 0
83 83 #define MAP_ALIGNED_SUPER 0
84 84
85 85 /* Rely on PROT_NONE for guard purposes */
86 86 #define MAP_GUARD (MAP_PRIVATE | MAP_ANON | MAP_NORESERVE)
87 87 #endif
88 88
89 89 /*
90 90 * Size of the guard region before and after the virtual address space
91 91 * mapping the guest physical memory. This must be a multiple of the
92 92 * superpage size for performance reasons.
93 93 */
94 94 #define VM_MMAP_GUARD_SIZE (4 * MB)
95 95
96 96 #define PROT_RW (PROT_READ | PROT_WRITE)
97 97 #define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
98 98
99 99 struct vmctx {
100 100 int fd;
101 101 uint32_t lowmem_limit;
102 102 int memflags;
103 103 size_t lowmem;
104 104 size_t highmem;
105 105 char *baseaddr;
106 106 char *name;
107 107 };
108 108
109 109 #ifdef __FreeBSD__
110 110 #define CREATE(x) sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
111 111 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
112 112 #else
113 113 #define CREATE(x) vm_do_ctl(VMM_CREATE_VM, (x))
114 114 #define DESTROY(x) vm_do_ctl(VMM_DESTROY_VM, (x))
115 115
116 116 static int
117 117 vm_do_ctl(int cmd, const char *name)
118 118 {
119 119 int ctl_fd;
120 120
121 121 ctl_fd = open(VMM_CTL_DEV, O_EXCL | O_RDWR);
122 122 if (ctl_fd < 0) {
123 123 return (-1);
124 124 }
125 125
126 126 if (ioctl(ctl_fd, cmd, name) == -1) {
127 127 int err = errno;
128 128
129 129 /* Do not lose ioctl errno through the close(2) */
130 130 (void) close(ctl_fd);
131 131 errno = err;
132 132 return (-1);
133 133 }
134 134 (void) close(ctl_fd);
135 135
136 136 return (0);
137 137 }
138 138 #endif
139 139
140 140 static int
141 141 vm_device_open(const char *name)
142 142 {
143 143 int fd, len;
144 144 char *vmfile;
145 145
146 146 len = strlen("/dev/vmm/") + strlen(name) + 1;
147 147 vmfile = malloc(len);
148 148 assert(vmfile != NULL);
149 149 snprintf(vmfile, len, "/dev/vmm/%s", name);
150 150
151 151 /* Open the device file */
152 152 fd = open(vmfile, O_RDWR, 0);
153 153
154 154 free(vmfile);
155 155 return (fd);
156 156 }
157 157
158 158 int
159 159 vm_create(const char *name)
160 160 {
161 161 #ifdef __FreeBSD__
162 162 /* Try to load vmm(4) module before creating a guest. */
163 163 if (modfind("vmm") < 0)
164 164 kldload("vmm");
165 165 #endif
166 166 return (CREATE((char *)name));
167 167 }
168 168
169 169 struct vmctx *
170 170 vm_open(const char *name)
171 171 {
172 172 struct vmctx *vm;
173 173
174 174 vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
175 175 assert(vm != NULL);
176 176
177 177 vm->fd = -1;
178 178 vm->memflags = 0;
179 179 vm->lowmem_limit = 3 * GB;
180 180 vm->name = (char *)(vm + 1);
181 181 strcpy(vm->name, name);
182 182
183 183 if ((vm->fd = vm_device_open(vm->name)) < 0)
184 184 goto err;
185 185
186 186 return (vm);
187 187 err:
188 188 #ifdef __FreeBSD__
189 189 vm_destroy(vm);
190 190 #else
191 191 /*
192 192 * As libvmmapi is used by other programs to query and control bhyve
193 193 * VMs, destroying a VM just because the open failed isn't useful. We
194 194 * have to free what we have allocated, though.
195 195 */
196 196 free(vm);
197 197 #endif
198 198 return (NULL);
199 199 }
200 200
201 201 #ifndef __FreeBSD__
202 202 void
203 203 vm_close(struct vmctx *vm)
204 204 {
205 205 assert(vm != NULL);
206 206 assert(vm->fd >= 0);
207 207
208 208 (void) close(vm->fd);
209 209
210 210 free(vm);
211 211 }
212 212 #endif
213 213
214 214 void
215 215 vm_destroy(struct vmctx *vm)
216 216 {
217 217 assert(vm != NULL);
218 218
219 219 if (vm->fd >= 0)
220 220 close(vm->fd);
221 221 DESTROY(vm->name);
222 222
223 223 free(vm);
224 224 }
225 225
226 226 int
227 227 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
228 228 {
229 229 char *endptr;
230 230 size_t optval;
231 231 int error;
232 232
233 233 optval = strtoul(optarg, &endptr, 0);
234 234 if (*optarg != '\0' && *endptr == '\0') {
235 235 /*
236 236 * For the sake of backward compatibility if the memory size
237 237 * specified on the command line is less than a megabyte then
238 238 * it is interpreted as being in units of MB.
239 239 */
240 240 if (optval < MB)
241 241 optval *= MB;
242 242 *ret_memsize = optval;
243 243 error = 0;
244 244 } else
245 245 error = expand_number(optarg, ret_memsize);
246 246
247 247 return (error);
248 248 }
249 249
250 250 uint32_t
251 251 vm_get_lowmem_limit(struct vmctx *ctx)
252 252 {
253 253
254 254 return (ctx->lowmem_limit);
255 255 }
256 256
257 257 void
258 258 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
259 259 {
260 260
261 261 ctx->lowmem_limit = limit;
262 262 }
263 263
264 264 void
265 265 vm_set_memflags(struct vmctx *ctx, int flags)
266 266 {
267 267
268 268 ctx->memflags = flags;
269 269 }
270 270
271 271 int
272 272 vm_get_memflags(struct vmctx *ctx)
273 273 {
274 274
275 275 return (ctx->memflags);
276 276 }
277 277
278 278 /*
279 279 * Map segment 'segid' starting at 'off' into guest address range [gpa,gpa+len).
280 280 */
281 281 int
282 282 vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, vm_ooffset_t off,
283 283 size_t len, int prot)
284 284 {
285 285 struct vm_memmap memmap;
286 286 int error, flags;
287 287
288 288 memmap.gpa = gpa;
289 289 memmap.segid = segid;
290 290 memmap.segoff = off;
291 291 memmap.len = len;
292 292 memmap.prot = prot;
293 293 memmap.flags = 0;
294 294
295 295 if (ctx->memflags & VM_MEM_F_WIRED)
296 296 memmap.flags |= VM_MEMMAP_F_WIRED;
297 297
298 298 /*
299 299 * If this mapping already exists then don't create it again. This
300 300 * is the common case for SYSMEM mappings created by bhyveload(8).
301 301 */
302 302 error = vm_mmap_getnext(ctx, &gpa, &segid, &off, &len, &prot, &flags);
303 303 if (error == 0 && gpa == memmap.gpa) {
304 304 if (segid != memmap.segid || off != memmap.segoff ||
305 305 prot != memmap.prot || flags != memmap.flags) {
306 306 errno = EEXIST;
307 307 return (-1);
308 308 } else {
309 309 return (0);
310 310 }
311 311 }
312 312
313 313 error = ioctl(ctx->fd, VM_MMAP_MEMSEG, &memmap);
314 314 return (error);
315 315 }
316 316
317 317 int
318 318 vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
319 319 vm_ooffset_t *segoff, size_t *len, int *prot, int *flags)
320 320 {
321 321 struct vm_memmap memmap;
322 322 int error;
323 323
324 324 bzero(&memmap, sizeof(struct vm_memmap));
325 325 memmap.gpa = *gpa;
326 326 error = ioctl(ctx->fd, VM_MMAP_GETNEXT, &memmap);
327 327 if (error == 0) {
328 328 *gpa = memmap.gpa;
329 329 *segid = memmap.segid;
330 330 *segoff = memmap.segoff;
331 331 *len = memmap.len;
332 332 *prot = memmap.prot;
333 333 *flags = memmap.flags;
334 334 }
335 335 return (error);
336 336 }
337 337
338 338 /*
339 339 * Return 0 if the segments are identical and non-zero otherwise.
340 340 *
341 341 * This is slightly complicated by the fact that only device memory segments
342 342 * are named.
343 343 */
344 344 static int
345 345 cmpseg(size_t len, const char *str, size_t len2, const char *str2)
346 346 {
347 347
348 348 if (len == len2) {
349 349 if ((!str && !str2) || (str && str2 && !strcmp(str, str2)))
350 350 return (0);
351 351 }
352 352 return (-1);
353 353 }
354 354
355 355 static int
356 356 vm_alloc_memseg(struct vmctx *ctx, int segid, size_t len, const char *name)
357 357 {
358 358 struct vm_memseg memseg;
359 359 size_t n;
360 360 int error;
361 361
362 362 /*
363 363 * If the memory segment has already been created then just return.
364 364 * This is the usual case for the SYSMEM segment created by userspace
365 365 * loaders like bhyveload(8).
366 366 */
367 367 error = vm_get_memseg(ctx, segid, &memseg.len, memseg.name,
368 368 sizeof(memseg.name));
369 369 if (error)
370 370 return (error);
371 371
372 372 if (memseg.len != 0) {
373 373 if (cmpseg(len, name, memseg.len, VM_MEMSEG_NAME(&memseg))) {
374 374 errno = EINVAL;
375 375 return (-1);
376 376 } else {
377 377 return (0);
378 378 }
379 379 }
380 380
381 381 bzero(&memseg, sizeof(struct vm_memseg));
382 382 memseg.segid = segid;
383 383 memseg.len = len;
384 384 if (name != NULL) {
385 385 n = strlcpy(memseg.name, name, sizeof(memseg.name));
386 386 if (n >= sizeof(memseg.name)) {
387 387 errno = ENAMETOOLONG;
388 388 return (-1);
389 389 }
390 390 }
391 391
392 392 error = ioctl(ctx->fd, VM_ALLOC_MEMSEG, &memseg);
393 393 return (error);
394 394 }
395 395
396 396 int
397 397 vm_get_memseg(struct vmctx *ctx, int segid, size_t *lenp, char *namebuf,
398 398 size_t bufsize)
399 399 {
400 400 struct vm_memseg memseg;
401 401 size_t n;
402 402 int error;
403 403
404 404 memseg.segid = segid;
405 405 error = ioctl(ctx->fd, VM_GET_MEMSEG, &memseg);
406 406 if (error == 0) {
407 407 *lenp = memseg.len;
408 408 n = strlcpy(namebuf, memseg.name, bufsize);
409 409 if (n >= bufsize) {
410 410 errno = ENAMETOOLONG;
411 411 error = -1;
412 412 }
413 413 }
414 414 return (error);
415 415 }
416 416
417 417 static int
418 418 #ifdef __FreeBSD__
419 419 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char *base)
420 420 #else
421 421 setup_memory_segment(struct vmctx *ctx, int segid, vm_paddr_t gpa, size_t len,
422 422 char *base)
423 423 #endif
424 424 {
425 425 char *ptr;
426 426 int error, flags;
427 427
428 428 /* Map 'len' bytes starting at 'gpa' in the guest address space */
429 429 #ifdef __FreeBSD__
430 430 error = vm_mmap_memseg(ctx, gpa, VM_SYSMEM, gpa, len, PROT_ALL);
431 431 #else
432 432 /*
433 433 * As we use two segments for lowmem/highmem the offset within the
434 434 * segment is 0 on illumos.
435 435 */
436 436 error = vm_mmap_memseg(ctx, gpa, segid, 0, len, PROT_ALL);
437 437 #endif
438 438 if (error)
439 439 return (error);
440 440
441 441 flags = MAP_SHARED | MAP_FIXED;
442 442 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
443 443 flags |= MAP_NOCORE;
444 444
445 445 /* mmap into the process address space on the host */
446 446 ptr = mmap(base + gpa, len, PROT_RW, flags, ctx->fd, gpa);
447 447 if (ptr == MAP_FAILED)
448 448 return (-1);
449 449
450 450 return (0);
451 451 }
452 452
453 453 int
454 454 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
455 455 {
456 456 size_t objsize, len;
457 457 vm_paddr_t gpa;
458 458 char *baseaddr, *ptr;
459 459 int error;
460 460
461 461 assert(vms == VM_MMAP_ALL);
462 462
463 463 /*
464 464 * If 'memsize' cannot fit entirely in the 'lowmem' segment then
465 465 * create another 'highmem' segment above 4GB for the remainder.
466 466 */
467 467 if (memsize > ctx->lowmem_limit) {
468 468 ctx->lowmem = ctx->lowmem_limit;
469 469 ctx->highmem = memsize - ctx->lowmem_limit;
470 470 objsize = 4*GB + ctx->highmem;
471 471 } else {
472 472 ctx->lowmem = memsize;
473 473 ctx->highmem = 0;
474 474 objsize = ctx->lowmem;
475 475 }
476 476
477 477 #ifdef __FreeBSD__
478 478 error = vm_alloc_memseg(ctx, VM_SYSMEM, objsize, NULL);
479 479 if (error)
480 480 return (error);
481 481 #endif
482 482
483 483 /*
484 484 * Stake out a contiguous region covering the guest physical memory
485 485 * and the adjoining guard regions.
486 486 */
487 487 len = VM_MMAP_GUARD_SIZE + objsize + VM_MMAP_GUARD_SIZE;
488 488 ptr = mmap(NULL, len, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1, 0);
489 489 if (ptr == MAP_FAILED)
490 490 return (-1);
491 491
492 492 baseaddr = ptr + VM_MMAP_GUARD_SIZE;
493 493
494 494 #ifdef __FreeBSD__
495 495 if (ctx->highmem > 0) {
496 496 gpa = 4*GB;
497 497 len = ctx->highmem;
498 498 error = setup_memory_segment(ctx, gpa, len, baseaddr);
499 499 if (error)
500 500 return (error);
501 501 }
502 502
503 503 if (ctx->lowmem > 0) {
504 504 gpa = 0;
505 505 len = ctx->lowmem;
506 506 error = setup_memory_segment(ctx, gpa, len, baseaddr);
507 507 if (error)
508 508 return (error);
509 509 }
510 510 #else
511 511 if (ctx->highmem > 0) {
512 512 error = vm_alloc_memseg(ctx, VM_HIGHMEM, ctx->highmem, NULL);
513 513 if (error)
514 514 return (error);
515 515 gpa = 4*GB;
516 516 len = ctx->highmem;
517 517 error = setup_memory_segment(ctx, VM_HIGHMEM, gpa, len, baseaddr);
518 518 if (error)
519 519 return (error);
520 520 }
521 521
522 522 if (ctx->lowmem > 0) {
523 523 error = vm_alloc_memseg(ctx, VM_LOWMEM, ctx->lowmem, NULL);
524 524 if (error)
525 525 return (error);
526 526 gpa = 0;
527 527 len = ctx->lowmem;
528 528 error = setup_memory_segment(ctx, VM_LOWMEM, gpa, len, baseaddr);
529 529 if (error)
530 530 return (error);
531 531 }
532 532 #endif
533 533
534 534 ctx->baseaddr = baseaddr;
535 535
536 536 return (0);
537 537 }
538 538
539 539 /*
540 540 * Returns a non-NULL pointer if [gaddr, gaddr+len) is entirely contained in
541 541 * the lowmem or highmem regions.
542 542 *
543 543 * In particular return NULL if [gaddr, gaddr+len) falls in guest MMIO region.
544 544 * The instruction emulation code depends on this behavior.
545 545 */
546 546 void *
547 547 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
548 548 {
549 549
550 550 if (ctx->lowmem > 0) {
551 551 if (gaddr < ctx->lowmem && len <= ctx->lowmem &&
552 552 gaddr + len <= ctx->lowmem)
553 553 return (ctx->baseaddr + gaddr);
554 554 }
555 555
556 556 if (ctx->highmem > 0) {
557 557 if (gaddr >= 4*GB) {
558 558 if (gaddr < 4*GB + ctx->highmem &&
559 559 len <= ctx->highmem &&
560 560 gaddr + len <= 4*GB + ctx->highmem)
561 561 return (ctx->baseaddr + gaddr);
562 562 }
563 563 }
564 564
565 565 return (NULL);
566 566 }
567 567
568 568 size_t
569 569 vm_get_lowmem_size(struct vmctx *ctx)
570 570 {
571 571
572 572 return (ctx->lowmem);
573 573 }
574 574
575 575 size_t
576 576 vm_get_highmem_size(struct vmctx *ctx)
577 577 {
578 578
579 579 return (ctx->highmem);
580 580 }
581 581
582 582 #ifndef __FreeBSD__
583 583 int
584 584 vm_get_devmem_offset(struct vmctx *ctx, int segid, off_t *mapoff)
585 585 {
586 586 struct vm_devmem_offset vdo;
587 587 int error;
588 588
589 589 vdo.segid = segid;
590 590 error = ioctl(ctx->fd, VM_DEVMEM_GETOFFSET, &vdo);
591 591 if (error == 0)
592 592 *mapoff = vdo.offset;
593 593
594 594 return (error);
595 595 }
596 596 #endif
597 597
598 598 void *
599 599 vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len)
600 600 {
601 601 #ifdef __FreeBSD__
602 602 char pathname[MAXPATHLEN];
603 603 #endif
604 604 size_t len2;
605 605 char *base, *ptr;
606 606 int fd, error, flags;
607 607 off_t mapoff;
608 608
609 609 fd = -1;
610 610 ptr = MAP_FAILED;
611 611 if (name == NULL || strlen(name) == 0) {
612 612 errno = EINVAL;
613 613 goto done;
614 614 }
615 615
616 616 error = vm_alloc_memseg(ctx, segid, len, name);
617 617 if (error)
618 618 goto done;
619 619
620 620 #ifdef __FreeBSD__
621 621 strlcpy(pathname, "/dev/vmm.io/", sizeof(pathname));
622 622 strlcat(pathname, ctx->name, sizeof(pathname));
623 623 strlcat(pathname, ".", sizeof(pathname));
624 624 strlcat(pathname, name, sizeof(pathname));
625 625
626 626 fd = open(pathname, O_RDWR);
627 627 if (fd < 0)
628 628 goto done;
629 629 #else
630 630 if (vm_get_devmem_offset(ctx, segid, &mapoff) != 0)
631 631 goto done;
632 632 #endif
633 633
634 634 /*
635 635 * Stake out a contiguous region covering the device memory and the
636 636 * adjoining guard regions.
637 637 */
638 638 len2 = VM_MMAP_GUARD_SIZE + len + VM_MMAP_GUARD_SIZE;
639 639 base = mmap(NULL, len2, PROT_NONE, MAP_GUARD | MAP_ALIGNED_SUPER, -1,
640 640 0);
641 641 if (base == MAP_FAILED)
642 642 goto done;
643 643
644 644 flags = MAP_SHARED | MAP_FIXED;
645 645 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
646 646 flags |= MAP_NOCORE;
647 647
648 648 #ifdef __FreeBSD__
649 649 /* mmap the devmem region in the host address space */
650 650 ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, fd, 0);
651 651 #else
652 652 /* mmap the devmem region in the host address space */
653 653 ptr = mmap(base + VM_MMAP_GUARD_SIZE, len, PROT_RW, flags, ctx->fd,
654 654 mapoff);
655 655 #endif
656 656 done:
657 657 if (fd >= 0)
658 658 close(fd);
659 659 return (ptr);
660 660 }
661 661
662 662 int
663 663 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
664 664 uint64_t base, uint32_t limit, uint32_t access)
665 665 {
666 666 int error;
667 667 struct vm_seg_desc vmsegdesc;
668 668
669 669 bzero(&vmsegdesc, sizeof(vmsegdesc));
670 670 vmsegdesc.cpuid = vcpu;
671 671 vmsegdesc.regnum = reg;
672 672 vmsegdesc.desc.base = base;
673 673 vmsegdesc.desc.limit = limit;
674 674 vmsegdesc.desc.access = access;
675 675
676 676 error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
677 677 return (error);
678 678 }
679 679
680 680 int
681 681 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
682 682 uint64_t *base, uint32_t *limit, uint32_t *access)
683 683 {
684 684 int error;
685 685 struct vm_seg_desc vmsegdesc;
686 686
687 687 bzero(&vmsegdesc, sizeof(vmsegdesc));
688 688 vmsegdesc.cpuid = vcpu;
689 689 vmsegdesc.regnum = reg;
690 690
691 691 error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
692 692 if (error == 0) {
693 693 *base = vmsegdesc.desc.base;
694 694 *limit = vmsegdesc.desc.limit;
695 695 *access = vmsegdesc.desc.access;
696 696 }
697 697 return (error);
698 698 }
699 699
700 700 int
701 701 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc)
702 702 {
703 703 int error;
704 704
705 705 error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit,
706 706 &seg_desc->access);
707 707 return (error);
708 708 }
709 709
710 710 int
711 711 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
712 712 {
713 713 int error;
714 714 struct vm_register vmreg;
715 715
716 716 bzero(&vmreg, sizeof(vmreg));
717 717 vmreg.cpuid = vcpu;
718 718 vmreg.regnum = reg;
719 719 vmreg.regval = val;
720 720
721 721 error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
722 722 return (error);
723 723 }
724 724
725 725 int
726 726 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
727 727 {
728 728 int error;
729 729 struct vm_register vmreg;
730 730
731 731 bzero(&vmreg, sizeof(vmreg));
732 732 vmreg.cpuid = vcpu;
733 733 vmreg.regnum = reg;
734 734
735 735 error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
736 736 *ret_val = vmreg.regval;
737 737 return (error);
738 738 }
739 739
740 740 int
741 741 vm_set_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
742 742 const int *regnums, uint64_t *regvals)
743 743 {
744 744 int error;
745 745 struct vm_register_set vmregset;
746 746
747 747 bzero(&vmregset, sizeof(vmregset));
748 748 vmregset.cpuid = vcpu;
749 749 vmregset.count = count;
750 750 vmregset.regnums = regnums;
751 751 vmregset.regvals = regvals;
752 752
753 753 error = ioctl(ctx->fd, VM_SET_REGISTER_SET, &vmregset);
754 754 return (error);
755 755 }
756 756
757 757 int
758 758 vm_get_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
759 759 const int *regnums, uint64_t *regvals)
760 760 {
761 761 int error;
762 762 struct vm_register_set vmregset;
763 763
764 764 bzero(&vmregset, sizeof(vmregset));
765 765 vmregset.cpuid = vcpu;
766 766 vmregset.count = count;
767 767 vmregset.regnums = regnums;
768 768 vmregset.regvals = regvals;
769 769
770 770 error = ioctl(ctx->fd, VM_GET_REGISTER_SET, &vmregset);
771 771 return (error);
772 772 }
773 773
774 774 int
775 775 vm_run(struct vmctx *ctx, int vcpu, const struct vm_entry *vm_entry,
776 776 struct vm_exit *vm_exit)
777 777 {
778 778 struct vm_entry entry;
779 779
780 780 bcopy(vm_entry, &entry, sizeof (entry));
781 781 entry.cpuid = vcpu;
782 782 entry.exit_data = vm_exit;
783 783
784 784 return (ioctl(ctx->fd, VM_RUN, &entry));
785 785 }
786 786
787 787 int
788 788 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
789 789 {
790 790 struct vm_suspend vmsuspend;
791 791
792 792 bzero(&vmsuspend, sizeof(vmsuspend));
793 793 vmsuspend.how = how;
794 794 return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
795 795 }
796 796
797 797 int
798 798 vm_reinit(struct vmctx *ctx)
799 799 {
800 800
801 801 return (ioctl(ctx->fd, VM_REINIT, 0));
802 802 }
803 803
804 804 int
805 805 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
806 806 uint32_t errcode, int restart_instruction)
807 807 {
808 808 struct vm_exception exc;
809 809
810 810 exc.cpuid = vcpu;
811 811 exc.vector = vector;
812 812 exc.error_code = errcode;
813 813 exc.error_code_valid = errcode_valid;
814 814 exc.restart_instruction = restart_instruction;
815 815
816 816 return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
817 817 }
818 818
819 819 #ifndef __FreeBSD__
820 820 void
821 821 vm_inject_fault(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
822 822 int errcode)
823 823 {
824 824 int error;
825 825 struct vm_exception exc;
826 826
827 827 exc.cpuid = vcpu;
828 828 exc.vector = vector;
829 829 exc.error_code = errcode;
830 830 exc.error_code_valid = errcode_valid;
831 831 exc.restart_instruction = 1;
832 832 error = ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc);
833 833
834 834 assert(error == 0);
835 835 }
836 836 #endif /* __FreeBSD__ */
837 837
838 838 int
839 839 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
840 840 {
841 841 /*
842 842 * The apic id associated with the 'vcpu' has the same numerical value
843 843 * as the 'vcpu' itself.
844 844 */
845 845 return (apicid);
846 846 }
847 847
848 848 int
849 849 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
850 850 {
851 851 struct vm_lapic_irq vmirq;
852 852
853 853 bzero(&vmirq, sizeof(vmirq));
854 854 vmirq.cpuid = vcpu;
855 855 vmirq.vector = vector;
856 856
857 857 return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
858 858 }
859 859
860 860 int
861 861 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
862 862 {
863 863 struct vm_lapic_irq vmirq;
864 864
865 865 bzero(&vmirq, sizeof(vmirq));
866 866 vmirq.cpuid = vcpu;
867 867 vmirq.vector = vector;
868 868
869 869 return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
870 870 }
871 871
872 872 int
873 873 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
874 874 {
875 875 struct vm_lapic_msi vmmsi;
876 876
877 877 bzero(&vmmsi, sizeof(vmmsi));
878 878 vmmsi.addr = addr;
879 879 vmmsi.msg = msg;
880 880
881 881 return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
882 882 }
883 883
884 884 int
885 885 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
886 886 {
887 887 struct vm_ioapic_irq ioapic_irq;
888 888
889 889 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
890 890 ioapic_irq.irq = irq;
891 891
892 892 return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
893 893 }
894 894
895 895 int
896 896 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
897 897 {
898 898 struct vm_ioapic_irq ioapic_irq;
899 899
900 900 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
901 901 ioapic_irq.irq = irq;
902 902
903 903 return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
904 904 }
905 905
906 906 int
907 907 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
908 908 {
909 909 struct vm_ioapic_irq ioapic_irq;
910 910
911 911 bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
912 912 ioapic_irq.irq = irq;
913 913
914 914 return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
915 915 }
916 916
917 917 int
918 918 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
919 919 {
920 920
921 921 return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
922 922 }
923 923
924 924 int
925 925 vm_readwrite_kernemu_device(struct vmctx *ctx, int vcpu, vm_paddr_t gpa,
926 926 bool write, int size, uint64_t *value)
927 927 {
928 928 struct vm_readwrite_kernemu_device irp = {
929 929 .vcpuid = vcpu,
930 930 .access_width = fls(size) - 1,
931 931 .gpa = gpa,
932 932 .value = write ? *value : ~0ul,
933 933 };
934 934 long cmd = (write ? VM_SET_KERNEMU_DEV : VM_GET_KERNEMU_DEV);
935 935 int rc;
936 936
937 937 rc = ioctl(ctx->fd, cmd, &irp);
938 938 if (rc == 0 && !write)
939 939 *value = irp.value;
940 940 return (rc);
941 941 }
942 942
943 943 int
944 944 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
945 945 {
946 946 struct vm_isa_irq isa_irq;
947 947
948 948 bzero(&isa_irq, sizeof(struct vm_isa_irq));
949 949 isa_irq.atpic_irq = atpic_irq;
950 950 isa_irq.ioapic_irq = ioapic_irq;
951 951
952 952 return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
953 953 }
954 954
955 955 int
956 956 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
957 957 {
958 958 struct vm_isa_irq isa_irq;
959 959
960 960 bzero(&isa_irq, sizeof(struct vm_isa_irq));
961 961 isa_irq.atpic_irq = atpic_irq;
962 962 isa_irq.ioapic_irq = ioapic_irq;
963 963
964 964 return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
965 965 }
966 966
967 967 int
968 968 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
969 969 {
970 970 struct vm_isa_irq isa_irq;
971 971
972 972 bzero(&isa_irq, sizeof(struct vm_isa_irq));
973 973 isa_irq.atpic_irq = atpic_irq;
974 974 isa_irq.ioapic_irq = ioapic_irq;
975 975
976 976 return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
977 977 }
978 978
979 979 int
980 980 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
981 981 enum vm_intr_trigger trigger)
982 982 {
983 983 struct vm_isa_irq_trigger isa_irq_trigger;
984 984
985 985 bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
986 986 isa_irq_trigger.atpic_irq = atpic_irq;
987 987 isa_irq_trigger.trigger = trigger;
988 988
989 989 return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
990 990 }
991 991
992 992 int
993 993 vm_inject_nmi(struct vmctx *ctx, int vcpu)
994 994 {
995 995 struct vm_nmi vmnmi;
996 996
997 997 bzero(&vmnmi, sizeof(vmnmi));
998 998 vmnmi.cpuid = vcpu;
999 999
1000 1000 return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
1001 1001 }
1002 1002
1003 1003 static const char *capstrmap[] = {
1004 1004 [VM_CAP_HALT_EXIT] = "hlt_exit",
1005 1005 [VM_CAP_MTRAP_EXIT] = "mtrap_exit",
1006 1006 [VM_CAP_PAUSE_EXIT] = "pause_exit",
1007 1007 #ifdef __FreeBSD__
1008 1008 [VM_CAP_UNRESTRICTED_GUEST] = "unrestricted_guest",
1009 1009 #endif
1010 1010 [VM_CAP_ENABLE_INVPCID] = "enable_invpcid",
1011 1011 [VM_CAP_BPT_EXIT] = "bpt_exit",
1012 1012 };
1013 1013
1014 1014 int
1015 1015 vm_capability_name2type(const char *capname)
1016 1016 {
1017 1017 int i;
1018 1018
1019 1019 for (i = 0; i < nitems(capstrmap); i++) {
1020 1020 if (strcmp(capstrmap[i], capname) == 0)
1021 1021 return (i);
1022 1022 }
1023 1023
1024 1024 return (-1);
1025 1025 }
1026 1026
1027 1027 const char *
1028 1028 vm_capability_type2name(int type)
1029 1029 {
1030 1030 if (type >= 0 && type < nitems(capstrmap))
1031 1031 return (capstrmap[type]);
1032 1032
1033 1033 return (NULL);
1034 1034 }
1035 1035
1036 1036 int
1037 1037 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
1038 1038 int *retval)
1039 1039 {
1040 1040 int error;
1041 1041 struct vm_capability vmcap;
1042 1042
1043 1043 bzero(&vmcap, sizeof(vmcap));
1044 1044 vmcap.cpuid = vcpu;
1045 1045 vmcap.captype = cap;
1046 1046
1047 1047 error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
1048 1048 *retval = vmcap.capval;
1049 1049 return (error);
1050 1050 }
1051 1051
1052 1052 int
1053 1053 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
1054 1054 {
1055 1055 struct vm_capability vmcap;
1056 1056
1057 1057 bzero(&vmcap, sizeof(vmcap));
1058 1058 vmcap.cpuid = vcpu;
1059 1059 vmcap.captype = cap;
1060 1060 vmcap.capval = val;
1061 1061
1062 1062 return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
1063 1063 }
1064 1064
1065 1065 #ifdef __FreeBSD__
1066 1066 int
1067 1067 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
1068 1068 {
1069 1069 struct vm_pptdev pptdev;
1070 1070
1071 1071 bzero(&pptdev, sizeof(pptdev));
1072 1072 pptdev.bus = bus;
1073 1073 pptdev.slot = slot;
1074 1074 pptdev.func = func;
1075 1075
1076 1076 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
1077 1077 }
1078 1078
1079 1079 int
1080 1080 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
1081 1081 {
1082 1082 struct vm_pptdev pptdev;
1083 1083
1084 1084 bzero(&pptdev, sizeof(pptdev));
1085 1085 pptdev.bus = bus;
1086 1086 pptdev.slot = slot;
1087 1087 pptdev.func = func;
1088 1088
1089 1089 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
1090 1090 }
1091 1091
1092 1092 int
1093 1093 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
1094 1094 vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
1095 1095 {
1096 1096 struct vm_pptdev_mmio pptmmio;
1097 1097
1098 1098 bzero(&pptmmio, sizeof(pptmmio));
1099 1099 pptmmio.bus = bus;
1100 1100 pptmmio.slot = slot;
1101 1101 pptmmio.func = func;
1102 1102 pptmmio.gpa = gpa;
1103 1103 pptmmio.len = len;
1104 1104 pptmmio.hpa = hpa;
1105 1105
1106 1106 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
1107 1107 }
1108 1108
1109 1109 int
1110 1110 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
1111 1111 uint64_t addr, uint64_t msg, int numvec)
1112 1112 {
1113 1113 struct vm_pptdev_msi pptmsi;
1114 1114
1115 1115 bzero(&pptmsi, sizeof(pptmsi));
1116 1116 pptmsi.vcpu = vcpu;
1117 1117 pptmsi.bus = bus;
1118 1118 pptmsi.slot = slot;
1119 1119 pptmsi.func = func;
1120 1120 pptmsi.msg = msg;
1121 1121 pptmsi.addr = addr;
1122 1122 pptmsi.numvec = numvec;
1123 1123
1124 1124 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
1125 1125 }
1126 1126
1127 1127 int
1128 1128 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
1129 1129 int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
1130 1130 {
1131 1131 struct vm_pptdev_msix pptmsix;
1132 1132
1133 1133 bzero(&pptmsix, sizeof(pptmsix));
1134 1134 pptmsix.vcpu = vcpu;
1135 1135 pptmsix.bus = bus;
1136 1136 pptmsix.slot = slot;
1137 1137 pptmsix.func = func;
1138 1138 pptmsix.idx = idx;
1139 1139 pptmsix.msg = msg;
1140 1140 pptmsix.addr = addr;
1141 1141 pptmsix.vector_control = vector_control;
1142 1142
1143 1143 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
1144 1144 }
1145 1145
1146 1146 int
1147 1147 vm_get_pptdev_limits(struct vmctx *ctx, int bus, int slot, int func,
1148 1148 int *msi_limit, int *msix_limit)
1149 1149 {
1150 1150 struct vm_pptdev_limits pptlimits;
1151 1151 int error;
1152 1152
1153 1153 bzero(&pptlimits, sizeof (pptlimits));
1154 1154 pptlimits.bus = bus;
1155 1155 pptlimits.slot = slot;
1156 1156 pptlimits.func = func;
1157 1157
1158 1158 error = ioctl(ctx->fd, VM_GET_PPTDEV_LIMITS, &pptlimits);
1159 1159
1160 1160 *msi_limit = pptlimits.msi_limit;
1161 1161 *msix_limit = pptlimits.msix_limit;
1162 1162
1163 1163 return (error);
1164 1164 }
1165 1165 #else /* __FreeBSD__ */
1166 1166 int
1167 1167 vm_assign_pptdev(struct vmctx *ctx, int pptfd)
1168 1168 {
1169 1169 struct vm_pptdev pptdev;
1170 1170
1171 1171 pptdev.pptfd = pptfd;
1172 1172 return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
1173 1173 }
1174 1174
1175 1175 int
1176 1176 vm_unassign_pptdev(struct vmctx *ctx, int pptfd)
1177 1177 {
1178 1178 struct vm_pptdev pptdev;
1179 1179
1180 1180 pptdev.pptfd = pptfd;
1181 1181 return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
1182 1182 }
1183 1183
1184 1184 int
1185 1185 vm_map_pptdev_mmio(struct vmctx *ctx, int pptfd, vm_paddr_t gpa, size_t len,
1186 1186 vm_paddr_t hpa)
1187 1187 {
1188 1188 struct vm_pptdev_mmio pptmmio;
1189 1189
1190 1190 pptmmio.pptfd = pptfd;
1191 1191 pptmmio.gpa = gpa;
1192 1192 pptmmio.len = len;
1193 1193 pptmmio.hpa = hpa;
1194 1194 return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
1195 1195 }
1196 1196
1197 1197 int
1198 1198 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int pptfd, uint64_t addr,
1199 1199 uint64_t msg, int numvec)
1200 1200 {
1201 1201 struct vm_pptdev_msi pptmsi;
1202 1202
1203 1203 pptmsi.vcpu = vcpu;
1204 1204 pptmsi.pptfd = pptfd;
1205 1205 pptmsi.msg = msg;
1206 1206 pptmsi.addr = addr;
1207 1207 pptmsi.numvec = numvec;
1208 1208 return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
1209 1209 }
1210 1210
1211 1211 int
1212 1212 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int pptfd, int idx,
1213 1213 uint64_t addr, uint64_t msg, uint32_t vector_control)
1214 1214 {
1215 1215 struct vm_pptdev_msix pptmsix;
1216 1216
1217 1217 pptmsix.vcpu = vcpu;
1218 1218 pptmsix.pptfd = pptfd;
1219 1219 pptmsix.idx = idx;
1220 1220 pptmsix.msg = msg;
1221 1221 pptmsix.addr = addr;
1222 1222 pptmsix.vector_control = vector_control;
1223 1223 return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
1224 1224 }
1225 1225
1226 1226 int
1227 1227 vm_get_pptdev_limits(struct vmctx *ctx, int pptfd, int *msi_limit,
1228 1228 int *msix_limit)
1229 1229 {
1230 1230 struct vm_pptdev_limits pptlimits;
1231 1231 int error;
1232 1232
1233 1233 bzero(&pptlimits, sizeof (pptlimits));
1234 1234 pptlimits.pptfd = pptfd;
1235 1235 error = ioctl(ctx->fd, VM_GET_PPTDEV_LIMITS, &pptlimits);
1236 1236
1237 1237 *msi_limit = pptlimits.msi_limit;
1238 1238 *msix_limit = pptlimits.msix_limit;
1239 1239 return (error);
1240 1240 }
1241 1241 #endif /* __FreeBSD__ */
1242 1242
1243 1243 uint64_t *
1244 1244 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
1245 1245 int *ret_entries)
1246 1246 {
1247 1247 int error;
1248 1248
1249 1249 static struct vm_stats vmstats;
1250 1250
1251 1251 vmstats.cpuid = vcpu;
1252 1252
1253 1253 error = ioctl(ctx->fd, VM_STATS_IOC, &vmstats);
1254 1254 if (error == 0) {
1255 1255 if (ret_entries)
1256 1256 *ret_entries = vmstats.num_entries;
1257 1257 if (ret_tv)
1258 1258 *ret_tv = vmstats.tv;
1259 1259 return (vmstats.statbuf);
1260 1260 } else
1261 1261 return (NULL);
1262 1262 }
1263 1263
1264 1264 const char *
1265 1265 vm_get_stat_desc(struct vmctx *ctx, int index)
1266 1266 {
1267 1267 static struct vm_stat_desc statdesc;
1268 1268
1269 1269 statdesc.index = index;
1270 1270 if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
1271 1271 return (statdesc.desc);
1272 1272 else
1273 1273 return (NULL);
1274 1274 }
1275 1275
1276 1276 int
1277 1277 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
1278 1278 {
1279 1279 int error;
1280 1280 struct vm_x2apic x2apic;
1281 1281
1282 1282 bzero(&x2apic, sizeof(x2apic));
1283 1283 x2apic.cpuid = vcpu;
1284 1284
1285 1285 error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
1286 1286 *state = x2apic.state;
1287 1287 return (error);
1288 1288 }
1289 1289
1290 1290 int
1291 1291 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
1292 1292 {
1293 1293 int error;
1294 1294 struct vm_x2apic x2apic;
|
↓ open down ↓ |
1294 lines elided |
↑ open up ↑ |
1295 1295
1296 1296 bzero(&x2apic, sizeof(x2apic));
1297 1297 x2apic.cpuid = vcpu;
1298 1298 x2apic.state = state;
1299 1299
1300 1300 error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
1301 1301
1302 1302 return (error);
1303 1303 }
1304 1304
1305 +#ifndef __FreeBSD__
1306 +int
1307 +vcpu_reset(struct vmctx *vmctx, int vcpu)
1308 +{
1309 + struct vm_vcpu_reset vvr;
1310 +
1311 + vvr.vcpuid = vcpu;
1312 + vvr.kind = VRK_RESET;
1313 +
1314 + return (ioctl(vmctx->fd, VM_RESET_CPU, &vvr));
1315 +}
1316 +#else /* __FreeBSD__ */
1305 1317 /*
1306 1318 * From Intel Vol 3a:
1307 1319 * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
1308 1320 */
1309 1321 int
1310 1322 vcpu_reset(struct vmctx *vmctx, int vcpu)
1311 1323 {
1312 1324 int error;
1313 1325 uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
1314 1326 uint32_t desc_access, desc_limit;
1315 1327 uint16_t sel;
1316 1328
1317 1329 zero = 0;
1318 1330
1319 1331 rflags = 0x2;
1320 1332 error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
1321 1333 if (error)
1322 1334 goto done;
1323 1335
1324 1336 rip = 0xfff0;
1325 1337 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
1326 1338 goto done;
1327 1339
1328 1340 cr0 = CR0_NE;
1329 1341 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
1330 1342 goto done;
1331 1343
1332 1344 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
1333 1345 goto done;
1334 1346
1335 1347 cr4 = 0;
1336 1348 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
1337 1349 goto done;
1338 1350
1339 1351 /*
1340 1352 * CS: present, r/w, accessed, 16-bit, byte granularity, usable
1341 1353 */
1342 1354 desc_base = 0xffff0000;
1343 1355 desc_limit = 0xffff;
1344 1356 desc_access = 0x0093;
1345 1357 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
1346 1358 desc_base, desc_limit, desc_access);
1347 1359 if (error)
1348 1360 goto done;
1349 1361
1350 1362 sel = 0xf000;
1351 1363 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
1352 1364 goto done;
1353 1365
1354 1366 /*
1355 1367 * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
1356 1368 */
1357 1369 desc_base = 0;
1358 1370 desc_limit = 0xffff;
1359 1371 desc_access = 0x0093;
1360 1372 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
1361 1373 desc_base, desc_limit, desc_access);
1362 1374 if (error)
1363 1375 goto done;
1364 1376
1365 1377 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
1366 1378 desc_base, desc_limit, desc_access);
1367 1379 if (error)
1368 1380 goto done;
1369 1381
1370 1382 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
1371 1383 desc_base, desc_limit, desc_access);
1372 1384 if (error)
1373 1385 goto done;
1374 1386
1375 1387 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
1376 1388 desc_base, desc_limit, desc_access);
1377 1389 if (error)
1378 1390 goto done;
1379 1391
1380 1392 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
1381 1393 desc_base, desc_limit, desc_access);
1382 1394 if (error)
1383 1395 goto done;
1384 1396
1385 1397 sel = 0;
1386 1398 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
1387 1399 goto done;
1388 1400 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
1389 1401 goto done;
1390 1402 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
1391 1403 goto done;
1392 1404 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
1393 1405 goto done;
1394 1406 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
1395 1407 goto done;
1396 1408
1397 1409 /* General purpose registers */
1398 1410 rdx = 0xf00;
1399 1411 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
1400 1412 goto done;
1401 1413 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
1402 1414 goto done;
1403 1415 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
1404 1416 goto done;
1405 1417 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
1406 1418 goto done;
1407 1419 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
1408 1420 goto done;
1409 1421 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
1410 1422 goto done;
1411 1423 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
1412 1424 goto done;
1413 1425 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
1414 1426 goto done;
1415 1427
1416 1428 /* GDTR, IDTR */
1417 1429 desc_base = 0;
1418 1430 desc_limit = 0xffff;
1419 1431 desc_access = 0;
1420 1432 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
1421 1433 desc_base, desc_limit, desc_access);
1422 1434 if (error != 0)
1423 1435 goto done;
1424 1436
1425 1437 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
1426 1438 desc_base, desc_limit, desc_access);
1427 1439 if (error != 0)
1428 1440 goto done;
1429 1441
1430 1442 /* TR */
1431 1443 desc_base = 0;
1432 1444 desc_limit = 0xffff;
1433 1445 desc_access = 0x0000008b;
1434 1446 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
1435 1447 if (error)
1436 1448 goto done;
1437 1449
1438 1450 sel = 0;
1439 1451 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
1440 1452 goto done;
1441 1453
1442 1454 /* LDTR */
1443 1455 desc_base = 0;
1444 1456 desc_limit = 0xffff;
1445 1457 desc_access = 0x00000082;
1446 1458 error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
1447 1459 desc_limit, desc_access);
1448 1460 if (error)
1449 1461 goto done;
1450 1462
|
↓ open down ↓ |
136 lines elided |
↑ open up ↑ |
1451 1463 sel = 0;
1452 1464 if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
1453 1465 goto done;
1454 1466
1455 1467 /* XXX cr2, debug registers */
1456 1468
1457 1469 error = 0;
1458 1470 done:
1459 1471 return (error);
1460 1472 }
1473 +#endif /* __FreeBSD__ */
1461 1474
1462 1475 int
1463 1476 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
1464 1477 {
1465 1478 int error, i;
1466 1479 struct vm_gpa_pte gpapte;
1467 1480
1468 1481 bzero(&gpapte, sizeof(gpapte));
1469 1482 gpapte.gpa = gpa;
1470 1483
1471 1484 error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
1472 1485
1473 1486 if (error == 0) {
1474 1487 *num = gpapte.ptenum;
1475 1488 for (i = 0; i < gpapte.ptenum; i++)
1476 1489 pte[i] = gpapte.pte[i];
1477 1490 }
1478 1491
1479 1492 return (error);
1480 1493 }
1481 1494
1482 1495 int
1483 1496 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
1484 1497 {
1485 1498 int error;
1486 1499 struct vm_hpet_cap cap;
1487 1500
1488 1501 bzero(&cap, sizeof(struct vm_hpet_cap));
1489 1502 error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
1490 1503 if (capabilities != NULL)
1491 1504 *capabilities = cap.capabilities;
1492 1505 return (error);
1493 1506 }
1494 1507
1495 1508 int
1496 1509 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1497 1510 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1498 1511 {
1499 1512 struct vm_gla2gpa gg;
1500 1513 int error;
1501 1514
1502 1515 bzero(&gg, sizeof(struct vm_gla2gpa));
1503 1516 gg.vcpuid = vcpu;
1504 1517 gg.prot = prot;
1505 1518 gg.gla = gla;
1506 1519 gg.paging = *paging;
1507 1520
1508 1521 error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
1509 1522 if (error == 0) {
1510 1523 *fault = gg.fault;
1511 1524 *gpa = gg.gpa;
1512 1525 }
1513 1526 return (error);
1514 1527 }
1515 1528
1516 1529 int
1517 1530 vm_gla2gpa_nofault(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1518 1531 uint64_t gla, int prot, uint64_t *gpa, int *fault)
1519 1532 {
1520 1533 struct vm_gla2gpa gg;
1521 1534 int error;
1522 1535
1523 1536 bzero(&gg, sizeof(struct vm_gla2gpa));
1524 1537 gg.vcpuid = vcpu;
1525 1538 gg.prot = prot;
1526 1539 gg.gla = gla;
1527 1540 gg.paging = *paging;
1528 1541
1529 1542 error = ioctl(ctx->fd, VM_GLA2GPA_NOFAULT, &gg);
1530 1543 if (error == 0) {
1531 1544 *fault = gg.fault;
1532 1545 *gpa = gg.gpa;
1533 1546 }
1534 1547 return (error);
1535 1548 }
1536 1549
1537 1550 #ifndef min
1538 1551 #define min(a,b) (((a) < (b)) ? (a) : (b))
1539 1552 #endif
1540 1553
1541 1554 int
1542 1555 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
1543 1556 uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1544 1557 int *fault)
1545 1558 {
1546 1559 void *va;
1547 1560 uint64_t gpa;
1548 1561 int error, i, n, off;
1549 1562
1550 1563 for (i = 0; i < iovcnt; i++) {
1551 1564 iov[i].iov_base = 0;
1552 1565 iov[i].iov_len = 0;
1553 1566 }
1554 1567
1555 1568 while (len) {
1556 1569 assert(iovcnt > 0);
1557 1570 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1558 1571 if (error || *fault)
1559 1572 return (error);
1560 1573
1561 1574 off = gpa & PAGE_MASK;
1562 1575 n = min(len, PAGE_SIZE - off);
1563 1576
1564 1577 va = vm_map_gpa(ctx, gpa, n);
1565 1578 if (va == NULL)
1566 1579 return (EFAULT);
1567 1580
1568 1581 iov->iov_base = va;
1569 1582 iov->iov_len = n;
1570 1583 iov++;
1571 1584 iovcnt--;
1572 1585
1573 1586 gla += n;
1574 1587 len -= n;
1575 1588 }
1576 1589 return (0);
1577 1590 }
1578 1591
1579 1592 void
1580 1593 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt)
1581 1594 {
1582 1595
1583 1596 return;
1584 1597 }
1585 1598
1586 1599 void
1587 1600 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1588 1601 {
1589 1602 const char *src;
1590 1603 char *dst;
1591 1604 size_t n;
1592 1605
1593 1606 dst = vp;
1594 1607 while (len) {
1595 1608 assert(iov->iov_len);
1596 1609 n = min(len, iov->iov_len);
1597 1610 src = iov->iov_base;
1598 1611 bcopy(src, dst, n);
1599 1612
1600 1613 iov++;
1601 1614 dst += n;
1602 1615 len -= n;
1603 1616 }
1604 1617 }
1605 1618
1606 1619 void
1607 1620 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1608 1621 size_t len)
1609 1622 {
1610 1623 const char *src;
1611 1624 char *dst;
1612 1625 size_t n;
1613 1626
1614 1627 src = vp;
1615 1628 while (len) {
1616 1629 assert(iov->iov_len);
1617 1630 n = min(len, iov->iov_len);
1618 1631 dst = iov->iov_base;
1619 1632 bcopy(src, dst, n);
1620 1633
1621 1634 iov++;
1622 1635 src += n;
1623 1636 len -= n;
1624 1637 }
1625 1638 }
1626 1639
1627 1640 static int
1628 1641 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1629 1642 {
1630 1643 struct vm_cpuset vm_cpuset;
1631 1644 int error;
1632 1645
1633 1646 bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1634 1647 vm_cpuset.which = which;
1635 1648 vm_cpuset.cpusetsize = sizeof(cpuset_t);
1636 1649 vm_cpuset.cpus = cpus;
1637 1650
1638 1651 error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1639 1652 return (error);
1640 1653 }
1641 1654
1642 1655 int
1643 1656 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1644 1657 {
1645 1658
1646 1659 return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1647 1660 }
1648 1661
1649 1662 int
1650 1663 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1651 1664 {
1652 1665
1653 1666 return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1654 1667 }
1655 1668
1656 1669 int
1657 1670 vm_debug_cpus(struct vmctx *ctx, cpuset_t *cpus)
1658 1671 {
1659 1672
1660 1673 return (vm_get_cpus(ctx, VM_DEBUG_CPUS, cpus));
1661 1674 }
1662 1675
1663 1676 int
1664 1677 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1665 1678 {
1666 1679 struct vm_activate_cpu ac;
1667 1680 int error;
1668 1681
1669 1682 bzero(&ac, sizeof(struct vm_activate_cpu));
1670 1683 ac.vcpuid = vcpu;
1671 1684 error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1672 1685 return (error);
1673 1686 }
1674 1687
1675 1688 int
1676 1689 vm_suspend_cpu(struct vmctx *ctx, int vcpu)
1677 1690 {
1678 1691 struct vm_activate_cpu ac;
1679 1692 int error;
1680 1693
1681 1694 bzero(&ac, sizeof(struct vm_activate_cpu));
1682 1695 ac.vcpuid = vcpu;
1683 1696 error = ioctl(ctx->fd, VM_SUSPEND_CPU, &ac);
1684 1697 return (error);
1685 1698 }
1686 1699
1687 1700 int
1688 1701 vm_resume_cpu(struct vmctx *ctx, int vcpu)
1689 1702 {
1690 1703 struct vm_activate_cpu ac;
1691 1704 int error;
1692 1705
1693 1706 bzero(&ac, sizeof(struct vm_activate_cpu));
1694 1707 ac.vcpuid = vcpu;
1695 1708 error = ioctl(ctx->fd, VM_RESUME_CPU, &ac);
1696 1709 return (error);
1697 1710 }
1698 1711
1699 1712 int
1700 1713 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1701 1714 {
1702 1715 struct vm_intinfo vmii;
1703 1716 int error;
1704 1717
1705 1718 bzero(&vmii, sizeof(struct vm_intinfo));
1706 1719 vmii.vcpuid = vcpu;
1707 1720 error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1708 1721 if (error == 0) {
1709 1722 *info1 = vmii.info1;
1710 1723 *info2 = vmii.info2;
1711 1724 }
1712 1725 return (error);
1713 1726 }
1714 1727
1715 1728 int
1716 1729 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1717 1730 {
1718 1731 struct vm_intinfo vmii;
1719 1732 int error;
1720 1733
1721 1734 bzero(&vmii, sizeof(struct vm_intinfo));
1722 1735 vmii.vcpuid = vcpu;
1723 1736 vmii.info1 = info1;
1724 1737 error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1725 1738 return (error);
1726 1739 }
1727 1740
1728 1741 int
1729 1742 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1730 1743 {
1731 1744 struct vm_rtc_data rtcdata;
1732 1745 int error;
1733 1746
1734 1747 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1735 1748 rtcdata.offset = offset;
1736 1749 rtcdata.value = value;
1737 1750 error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1738 1751 return (error);
1739 1752 }
1740 1753
1741 1754 int
1742 1755 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1743 1756 {
1744 1757 struct vm_rtc_data rtcdata;
1745 1758 int error;
1746 1759
1747 1760 bzero(&rtcdata, sizeof(struct vm_rtc_data));
1748 1761 rtcdata.offset = offset;
1749 1762 error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1750 1763 if (error == 0)
1751 1764 *retval = rtcdata.value;
1752 1765 return (error);
1753 1766 }
1754 1767
1755 1768 int
1756 1769 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1757 1770 {
1758 1771 struct vm_rtc_time rtctime;
1759 1772 int error;
1760 1773
1761 1774 bzero(&rtctime, sizeof(struct vm_rtc_time));
1762 1775 rtctime.secs = secs;
1763 1776 error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1764 1777 return (error);
1765 1778 }
1766 1779
1767 1780 int
1768 1781 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1769 1782 {
1770 1783 struct vm_rtc_time rtctime;
1771 1784 int error;
1772 1785
1773 1786 bzero(&rtctime, sizeof(struct vm_rtc_time));
1774 1787 error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1775 1788 if (error == 0)
1776 1789 *secs = rtctime.secs;
1777 1790 return (error);
1778 1791 }
1779 1792
1780 1793 int
1781 1794 vm_restart_instruction(void *arg, int vcpu)
1782 1795 {
1783 1796 struct vmctx *ctx = arg;
1784 1797
1785 1798 return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));
1786 1799 }
1787 1800
1788 1801 int
1789 1802 vm_set_topology(struct vmctx *ctx,
1790 1803 uint16_t sockets, uint16_t cores, uint16_t threads, uint16_t maxcpus)
1791 1804 {
1792 1805 struct vm_cpu_topology topology;
1793 1806
1794 1807 bzero(&topology, sizeof (struct vm_cpu_topology));
1795 1808 topology.sockets = sockets;
1796 1809 topology.cores = cores;
1797 1810 topology.threads = threads;
1798 1811 topology.maxcpus = maxcpus;
1799 1812 return (ioctl(ctx->fd, VM_SET_TOPOLOGY, &topology));
1800 1813 }
1801 1814
1802 1815 int
1803 1816 vm_get_topology(struct vmctx *ctx,
1804 1817 uint16_t *sockets, uint16_t *cores, uint16_t *threads, uint16_t *maxcpus)
1805 1818 {
1806 1819 struct vm_cpu_topology topology;
1807 1820 int error;
1808 1821
1809 1822 bzero(&topology, sizeof (struct vm_cpu_topology));
1810 1823 error = ioctl(ctx->fd, VM_GET_TOPOLOGY, &topology);
1811 1824 if (error == 0) {
1812 1825 *sockets = topology.sockets;
1813 1826 *cores = topology.cores;
1814 1827 *threads = topology.threads;
1815 1828 *maxcpus = topology.maxcpus;
1816 1829 }
1817 1830 return (error);
1818 1831 }
1819 1832
1820 1833 int
1821 1834 vm_get_device_fd(struct vmctx *ctx)
1822 1835 {
1823 1836
1824 1837 return (ctx->fd);
1825 1838 }
1826 1839
1827 1840 #ifndef __FreeBSD__
1828 1841 int
1829 1842 vm_pmtmr_set_location(struct vmctx *ctx, uint16_t ioport)
1830 1843 {
1831 1844 return (ioctl(ctx->fd, VM_PMTMR_LOCATE, ioport));
|
↓ open down ↓ |
361 lines elided |
↑ open up ↑ |
1832 1845 }
1833 1846
1834 1847 int
1835 1848 vm_wrlock_cycle(struct vmctx *ctx)
1836 1849 {
1837 1850 if (ioctl(ctx->fd, VM_WRLOCK_CYCLE, 0) != 0) {
1838 1851 return (errno);
1839 1852 }
1840 1853 return (0);
1841 1854 }
1855 +
1856 +int
1857 +vm_get_run_state(struct vmctx *ctx, int vcpu, enum vcpu_run_state *state,
1858 + uint8_t *sipi_vector)
1859 +{
1860 + struct vm_run_state data;
1861 +
1862 + data.vcpuid = vcpu;
1863 + if (ioctl(ctx->fd, VM_GET_RUN_STATE, &data) != 0) {
1864 + return (errno);
1865 + }
1866 +
1867 + *state = data.state;
1868 + *sipi_vector = data.sipi_vector;
1869 + return (0);
1870 +}
1871 +
1872 +int
1873 +vm_set_run_state(struct vmctx *ctx, int vcpu, enum vcpu_run_state state,
1874 + uint8_t sipi_vector)
1875 +{
1876 + struct vm_run_state data;
1877 +
1878 + data.vcpuid = vcpu;
1879 + data.state = state;
1880 + data.sipi_vector = sipi_vector;
1881 + if (ioctl(ctx->fd, VM_SET_RUN_STATE, &data) != 0) {
1882 + return (errno);
1883 + }
1884 +
1885 + return (0);
1886 +}
1887 +
1842 1888 #endif /* __FreeBSD__ */
1843 1889
1844 1890 #ifdef __FreeBSD__
1845 1891 const cap_ioctl_t *
1846 1892 vm_get_ioctls(size_t *len)
1847 1893 {
1848 1894 cap_ioctl_t *cmds;
1849 1895 /* keep in sync with machine/vmm_dev.h */
1850 1896 static const cap_ioctl_t vm_ioctl_cmds[] = { VM_RUN, VM_SUSPEND, VM_REINIT,
1851 1897 VM_ALLOC_MEMSEG, VM_GET_MEMSEG, VM_MMAP_MEMSEG, VM_MMAP_MEMSEG,
1852 1898 VM_MMAP_GETNEXT, VM_SET_REGISTER, VM_GET_REGISTER,
1853 1899 VM_SET_SEGMENT_DESCRIPTOR, VM_GET_SEGMENT_DESCRIPTOR,
1854 1900 VM_SET_REGISTER_SET, VM_GET_REGISTER_SET,
1855 1901 VM_SET_KERNEMU_DEV, VM_GET_KERNEMU_DEV,
1856 1902 VM_INJECT_EXCEPTION, VM_LAPIC_IRQ, VM_LAPIC_LOCAL_IRQ,
1857 1903 VM_LAPIC_MSI, VM_IOAPIC_ASSERT_IRQ, VM_IOAPIC_DEASSERT_IRQ,
1858 1904 VM_IOAPIC_PULSE_IRQ, VM_IOAPIC_PINCOUNT, VM_ISA_ASSERT_IRQ,
1859 1905 VM_ISA_DEASSERT_IRQ, VM_ISA_PULSE_IRQ, VM_ISA_SET_IRQ_TRIGGER,
1860 1906 VM_SET_CAPABILITY, VM_GET_CAPABILITY, VM_BIND_PPTDEV,
1861 1907 VM_UNBIND_PPTDEV, VM_MAP_PPTDEV_MMIO, VM_PPTDEV_MSI,
1862 1908 VM_PPTDEV_MSIX, VM_INJECT_NMI, VM_STATS, VM_STAT_DESC,
1863 1909 VM_SET_X2APIC_STATE, VM_GET_X2APIC_STATE,
1864 1910 VM_GET_HPET_CAPABILITIES, VM_GET_GPA_PMAP, VM_GLA2GPA,
1865 1911 VM_GLA2GPA_NOFAULT,
1866 1912 VM_ACTIVATE_CPU, VM_GET_CPUS, VM_SUSPEND_CPU, VM_RESUME_CPU,
1867 1913 VM_SET_INTINFO, VM_GET_INTINFO,
1868 1914 VM_RTC_WRITE, VM_RTC_READ, VM_RTC_SETTIME, VM_RTC_GETTIME,
1869 1915 VM_RESTART_INSTRUCTION, VM_SET_TOPOLOGY, VM_GET_TOPOLOGY };
1870 1916
1871 1917 if (len == NULL) {
1872 1918 cmds = malloc(sizeof(vm_ioctl_cmds));
1873 1919 if (cmds == NULL)
1874 1920 return (NULL);
1875 1921 bcopy(vm_ioctl_cmds, cmds, sizeof(vm_ioctl_cmds));
1876 1922 return (cmds);
1877 1923 }
1878 1924
1879 1925 *len = nitems(vm_ioctl_cmds);
1880 1926 return (NULL);
1881 1927 }
1882 1928 #endif /* __FreeBSD__ */
|
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX