]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - lib/libvmmapi/vmmapi.c
Fix possible login(1) argument injection in telnetd(8). [SA-16:36]
[FreeBSD/releng/10.2.git] / lib / libvmmapi / vmmapi.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
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 NETAPP, INC ``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 NETAPP, INC 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  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/_iovec.h>
37 #include <sys/cpuset.h>
38
39 #include <x86/segments.h>
40 #include <machine/specialreg.h>
41 #include <machine/param.h>
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <assert.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50
51 #include <libutil.h>
52
53 #include <machine/vmm.h>
54 #include <machine/vmm_dev.h>
55
56 #include "vmmapi.h"
57
58 #define MB      (1024 * 1024UL)
59 #define GB      (1024 * 1024 * 1024UL)
60
61 struct vmctx {
62         int     fd;
63         uint32_t lowmem_limit;
64         enum vm_mmap_style vms;
65         int     memflags;
66         size_t  lowmem;
67         char    *lowmem_addr;
68         size_t  highmem;
69         char    *highmem_addr;
70         char    *name;
71 };
72
73 #define CREATE(x)  sysctlbyname("hw.vmm.create", NULL, NULL, (x), strlen((x)))
74 #define DESTROY(x) sysctlbyname("hw.vmm.destroy", NULL, NULL, (x), strlen((x)))
75
76 static int
77 vm_device_open(const char *name)
78 {
79         int fd, len;
80         char *vmfile;
81
82         len = strlen("/dev/vmm/") + strlen(name) + 1;
83         vmfile = malloc(len);
84         assert(vmfile != NULL);
85         snprintf(vmfile, len, "/dev/vmm/%s", name);
86
87         /* Open the device file */
88         fd = open(vmfile, O_RDWR, 0);
89
90         free(vmfile);
91         return (fd);
92 }
93
94 int
95 vm_create(const char *name)
96 {
97
98         return (CREATE((char *)name));
99 }
100
101 struct vmctx *
102 vm_open(const char *name)
103 {
104         struct vmctx *vm;
105
106         vm = malloc(sizeof(struct vmctx) + strlen(name) + 1);
107         assert(vm != NULL);
108
109         vm->fd = -1;
110         vm->memflags = 0;
111         vm->lowmem_limit = 3 * GB;
112         vm->name = (char *)(vm + 1);
113         strcpy(vm->name, name);
114
115         if ((vm->fd = vm_device_open(vm->name)) < 0)
116                 goto err;
117
118         return (vm);
119 err:
120         vm_destroy(vm);
121         return (NULL);
122 }
123
124 void
125 vm_destroy(struct vmctx *vm)
126 {
127         assert(vm != NULL);
128
129         if (vm->fd >= 0)
130                 close(vm->fd);
131         DESTROY(vm->name);
132
133         free(vm);
134 }
135
136 int
137 vm_parse_memsize(const char *optarg, size_t *ret_memsize)
138 {
139         char *endptr;
140         size_t optval;
141         int error;
142
143         optval = strtoul(optarg, &endptr, 0);
144         if (*optarg != '\0' && *endptr == '\0') {
145                 /*
146                  * For the sake of backward compatibility if the memory size
147                  * specified on the command line is less than a megabyte then
148                  * it is interpreted as being in units of MB.
149                  */
150                 if (optval < MB)
151                         optval *= MB;
152                 *ret_memsize = optval;
153                 error = 0;
154         } else
155                 error = expand_number(optarg, ret_memsize);
156
157         return (error);
158 }
159
160 int
161 vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len,
162                   int *wired)
163 {
164         int error;
165         struct vm_memory_segment seg;
166
167         bzero(&seg, sizeof(seg));
168         seg.gpa = gpa;
169         error = ioctl(ctx->fd, VM_GET_MEMORY_SEG, &seg);
170         *ret_len = seg.len;
171         if (wired != NULL)
172                 *wired = seg.wired;
173         return (error);
174 }
175
176 uint32_t
177 vm_get_lowmem_limit(struct vmctx *ctx)
178 {
179
180         return (ctx->lowmem_limit);
181 }
182
183 void
184 vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit)
185 {
186
187         ctx->lowmem_limit = limit;
188 }
189
190 void
191 vm_set_memflags(struct vmctx *ctx, int flags)
192 {
193
194         ctx->memflags = flags;
195 }
196
197 static int
198 setup_memory_segment(struct vmctx *ctx, vm_paddr_t gpa, size_t len, char **addr)
199 {
200         int error, mmap_flags;
201         struct vm_memory_segment seg;
202
203         /*
204          * Create and optionally map 'len' bytes of memory at guest
205          * physical address 'gpa'
206          */
207         bzero(&seg, sizeof(seg));
208         seg.gpa = gpa;
209         seg.len = len;
210         error = ioctl(ctx->fd, VM_MAP_MEMORY, &seg);
211         if (error == 0 && addr != NULL) {
212                 mmap_flags = MAP_SHARED;
213                 if ((ctx->memflags & VM_MEM_F_INCORE) == 0)
214                         mmap_flags |= MAP_NOCORE;
215                 *addr = mmap(NULL, len, PROT_READ | PROT_WRITE, mmap_flags,
216                     ctx->fd, gpa);
217         }
218         return (error);
219 }
220
221 int
222 vm_setup_memory(struct vmctx *ctx, size_t memsize, enum vm_mmap_style vms)
223 {
224         char **addr;
225         int error;
226
227         /* XXX VM_MMAP_SPARSE not implemented yet */
228         assert(vms == VM_MMAP_NONE || vms == VM_MMAP_ALL);
229         ctx->vms = vms;
230
231         /*
232          * If 'memsize' cannot fit entirely in the 'lowmem' segment then
233          * create another 'highmem' segment above 4GB for the remainder.
234          */
235         if (memsize > ctx->lowmem_limit) {
236                 ctx->lowmem = ctx->lowmem_limit;
237                 ctx->highmem = memsize - ctx->lowmem;
238         } else {
239                 ctx->lowmem = memsize;
240                 ctx->highmem = 0;
241         }
242
243         if (ctx->lowmem > 0) {
244                 addr = (vms == VM_MMAP_ALL) ? &ctx->lowmem_addr : NULL;
245                 error = setup_memory_segment(ctx, 0, ctx->lowmem, addr);
246                 if (error)
247                         return (error);
248         }
249
250         if (ctx->highmem > 0) {
251                 addr = (vms == VM_MMAP_ALL) ? &ctx->highmem_addr : NULL;
252                 error = setup_memory_segment(ctx, 4*GB, ctx->highmem, addr);
253                 if (error)
254                         return (error);
255         }
256
257         return (0);
258 }
259
260 void *
261 vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len)
262 {
263
264         /* XXX VM_MMAP_SPARSE not implemented yet */
265         assert(ctx->vms == VM_MMAP_ALL);
266
267         if (gaddr < ctx->lowmem && len <= ctx->lowmem &&
268             gaddr + len <= ctx->lowmem)
269                 return ((void *)(ctx->lowmem_addr + gaddr));
270
271         if (gaddr >= 4*GB) {
272                 gaddr -= 4*GB;
273                 if (gaddr < ctx->highmem && len <= ctx->highmem &&
274                     gaddr + len <= ctx->highmem)
275                         return ((void *)(ctx->highmem_addr + gaddr));
276         }
277
278         return (NULL);
279 }
280
281 size_t
282 vm_get_lowmem_size(struct vmctx *ctx)
283 {
284
285         return (ctx->lowmem);
286 }
287
288 size_t
289 vm_get_highmem_size(struct vmctx *ctx)
290 {
291
292         return (ctx->highmem);
293 }
294
295 int
296 vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
297             uint64_t base, uint32_t limit, uint32_t access)
298 {
299         int error;
300         struct vm_seg_desc vmsegdesc;
301
302         bzero(&vmsegdesc, sizeof(vmsegdesc));
303         vmsegdesc.cpuid = vcpu;
304         vmsegdesc.regnum = reg;
305         vmsegdesc.desc.base = base;
306         vmsegdesc.desc.limit = limit;
307         vmsegdesc.desc.access = access;
308
309         error = ioctl(ctx->fd, VM_SET_SEGMENT_DESCRIPTOR, &vmsegdesc);
310         return (error);
311 }
312
313 int
314 vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
315             uint64_t *base, uint32_t *limit, uint32_t *access)
316 {
317         int error;
318         struct vm_seg_desc vmsegdesc;
319
320         bzero(&vmsegdesc, sizeof(vmsegdesc));
321         vmsegdesc.cpuid = vcpu;
322         vmsegdesc.regnum = reg;
323
324         error = ioctl(ctx->fd, VM_GET_SEGMENT_DESCRIPTOR, &vmsegdesc);
325         if (error == 0) {
326                 *base = vmsegdesc.desc.base;
327                 *limit = vmsegdesc.desc.limit;
328                 *access = vmsegdesc.desc.access;
329         }
330         return (error);
331 }
332
333 int
334 vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg, struct seg_desc *seg_desc)
335 {
336         int error;
337
338         error = vm_get_desc(ctx, vcpu, reg, &seg_desc->base, &seg_desc->limit,
339             &seg_desc->access);
340         return (error);
341 }
342
343 int
344 vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val)
345 {
346         int error;
347         struct vm_register vmreg;
348
349         bzero(&vmreg, sizeof(vmreg));
350         vmreg.cpuid = vcpu;
351         vmreg.regnum = reg;
352         vmreg.regval = val;
353
354         error = ioctl(ctx->fd, VM_SET_REGISTER, &vmreg);
355         return (error);
356 }
357
358 int
359 vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *ret_val)
360 {
361         int error;
362         struct vm_register vmreg;
363
364         bzero(&vmreg, sizeof(vmreg));
365         vmreg.cpuid = vcpu;
366         vmreg.regnum = reg;
367
368         error = ioctl(ctx->fd, VM_GET_REGISTER, &vmreg);
369         *ret_val = vmreg.regval;
370         return (error);
371 }
372
373 int
374 vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
375 {
376         int error;
377         struct vm_run vmrun;
378
379         bzero(&vmrun, sizeof(vmrun));
380         vmrun.cpuid = vcpu;
381
382         error = ioctl(ctx->fd, VM_RUN, &vmrun);
383         bcopy(&vmrun.vm_exit, vmexit, sizeof(struct vm_exit));
384         return (error);
385 }
386
387 int
388 vm_suspend(struct vmctx *ctx, enum vm_suspend_how how)
389 {
390         struct vm_suspend vmsuspend;
391
392         bzero(&vmsuspend, sizeof(vmsuspend));
393         vmsuspend.how = how;
394         return (ioctl(ctx->fd, VM_SUSPEND, &vmsuspend));
395 }
396
397 int
398 vm_reinit(struct vmctx *ctx)
399 {
400
401         return (ioctl(ctx->fd, VM_REINIT, 0));
402 }
403
404 int
405 vm_inject_exception(struct vmctx *ctx, int vcpu, int vector, int errcode_valid,
406     uint32_t errcode, int restart_instruction)
407 {
408         struct vm_exception exc;
409
410         exc.cpuid = vcpu;
411         exc.vector = vector;
412         exc.error_code = errcode;
413         exc.error_code_valid = errcode_valid;
414         exc.restart_instruction = restart_instruction;
415
416         return (ioctl(ctx->fd, VM_INJECT_EXCEPTION, &exc));
417 }
418
419 int
420 vm_apicid2vcpu(struct vmctx *ctx, int apicid)
421 {
422         /*
423          * The apic id associated with the 'vcpu' has the same numerical value
424          * as the 'vcpu' itself.
425          */
426         return (apicid);
427 }
428
429 int
430 vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector)
431 {
432         struct vm_lapic_irq vmirq;
433
434         bzero(&vmirq, sizeof(vmirq));
435         vmirq.cpuid = vcpu;
436         vmirq.vector = vector;
437
438         return (ioctl(ctx->fd, VM_LAPIC_IRQ, &vmirq));
439 }
440
441 int
442 vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector)
443 {
444         struct vm_lapic_irq vmirq;
445
446         bzero(&vmirq, sizeof(vmirq));
447         vmirq.cpuid = vcpu;
448         vmirq.vector = vector;
449
450         return (ioctl(ctx->fd, VM_LAPIC_LOCAL_IRQ, &vmirq));
451 }
452
453 int
454 vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg)
455 {
456         struct vm_lapic_msi vmmsi;
457
458         bzero(&vmmsi, sizeof(vmmsi));
459         vmmsi.addr = addr;
460         vmmsi.msg = msg;
461
462         return (ioctl(ctx->fd, VM_LAPIC_MSI, &vmmsi));
463 }
464
465 int
466 vm_ioapic_assert_irq(struct vmctx *ctx, int irq)
467 {
468         struct vm_ioapic_irq ioapic_irq;
469
470         bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
471         ioapic_irq.irq = irq;
472
473         return (ioctl(ctx->fd, VM_IOAPIC_ASSERT_IRQ, &ioapic_irq));
474 }
475
476 int
477 vm_ioapic_deassert_irq(struct vmctx *ctx, int irq)
478 {
479         struct vm_ioapic_irq ioapic_irq;
480
481         bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
482         ioapic_irq.irq = irq;
483
484         return (ioctl(ctx->fd, VM_IOAPIC_DEASSERT_IRQ, &ioapic_irq));
485 }
486
487 int
488 vm_ioapic_pulse_irq(struct vmctx *ctx, int irq)
489 {
490         struct vm_ioapic_irq ioapic_irq;
491
492         bzero(&ioapic_irq, sizeof(struct vm_ioapic_irq));
493         ioapic_irq.irq = irq;
494
495         return (ioctl(ctx->fd, VM_IOAPIC_PULSE_IRQ, &ioapic_irq));
496 }
497
498 int
499 vm_ioapic_pincount(struct vmctx *ctx, int *pincount)
500 {
501
502         return (ioctl(ctx->fd, VM_IOAPIC_PINCOUNT, pincount));
503 }
504
505 int
506 vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
507 {
508         struct vm_isa_irq isa_irq;
509
510         bzero(&isa_irq, sizeof(struct vm_isa_irq));
511         isa_irq.atpic_irq = atpic_irq;
512         isa_irq.ioapic_irq = ioapic_irq;
513
514         return (ioctl(ctx->fd, VM_ISA_ASSERT_IRQ, &isa_irq));
515 }
516
517 int
518 vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
519 {
520         struct vm_isa_irq isa_irq;
521
522         bzero(&isa_irq, sizeof(struct vm_isa_irq));
523         isa_irq.atpic_irq = atpic_irq;
524         isa_irq.ioapic_irq = ioapic_irq;
525
526         return (ioctl(ctx->fd, VM_ISA_DEASSERT_IRQ, &isa_irq));
527 }
528
529 int
530 vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq)
531 {
532         struct vm_isa_irq isa_irq;
533
534         bzero(&isa_irq, sizeof(struct vm_isa_irq));
535         isa_irq.atpic_irq = atpic_irq;
536         isa_irq.ioapic_irq = ioapic_irq;
537
538         return (ioctl(ctx->fd, VM_ISA_PULSE_IRQ, &isa_irq));
539 }
540
541 int
542 vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
543     enum vm_intr_trigger trigger)
544 {
545         struct vm_isa_irq_trigger isa_irq_trigger;
546
547         bzero(&isa_irq_trigger, sizeof(struct vm_isa_irq_trigger));
548         isa_irq_trigger.atpic_irq = atpic_irq;
549         isa_irq_trigger.trigger = trigger;
550
551         return (ioctl(ctx->fd, VM_ISA_SET_IRQ_TRIGGER, &isa_irq_trigger));
552 }
553
554 int
555 vm_inject_nmi(struct vmctx *ctx, int vcpu)
556 {
557         struct vm_nmi vmnmi;
558
559         bzero(&vmnmi, sizeof(vmnmi));
560         vmnmi.cpuid = vcpu;
561
562         return (ioctl(ctx->fd, VM_INJECT_NMI, &vmnmi));
563 }
564
565 static struct {
566         const char      *name;
567         int             type;
568 } capstrmap[] = {
569         { "hlt_exit",           VM_CAP_HALT_EXIT },
570         { "mtrap_exit",         VM_CAP_MTRAP_EXIT },
571         { "pause_exit",         VM_CAP_PAUSE_EXIT },
572         { "unrestricted_guest", VM_CAP_UNRESTRICTED_GUEST },
573         { "enable_invpcid",     VM_CAP_ENABLE_INVPCID },
574         { 0 }
575 };
576
577 int
578 vm_capability_name2type(const char *capname)
579 {
580         int i;
581
582         for (i = 0; capstrmap[i].name != NULL && capname != NULL; i++) {
583                 if (strcmp(capstrmap[i].name, capname) == 0)
584                         return (capstrmap[i].type);
585         }
586
587         return (-1);
588 }
589
590 const char *
591 vm_capability_type2name(int type)
592 {
593         int i;
594
595         for (i = 0; capstrmap[i].name != NULL; i++) {
596                 if (capstrmap[i].type == type)
597                         return (capstrmap[i].name);
598         }
599
600         return (NULL);
601 }
602
603 int
604 vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
605                   int *retval)
606 {
607         int error;
608         struct vm_capability vmcap;
609
610         bzero(&vmcap, sizeof(vmcap));
611         vmcap.cpuid = vcpu;
612         vmcap.captype = cap;
613
614         error = ioctl(ctx->fd, VM_GET_CAPABILITY, &vmcap);
615         *retval = vmcap.capval;
616         return (error);
617 }
618
619 int
620 vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap, int val)
621 {
622         struct vm_capability vmcap;
623
624         bzero(&vmcap, sizeof(vmcap));
625         vmcap.cpuid = vcpu;
626         vmcap.captype = cap;
627         vmcap.capval = val;
628         
629         return (ioctl(ctx->fd, VM_SET_CAPABILITY, &vmcap));
630 }
631
632 int
633 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
634 {
635         struct vm_pptdev pptdev;
636
637         bzero(&pptdev, sizeof(pptdev));
638         pptdev.bus = bus;
639         pptdev.slot = slot;
640         pptdev.func = func;
641
642         return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
643 }
644
645 int
646 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
647 {
648         struct vm_pptdev pptdev;
649
650         bzero(&pptdev, sizeof(pptdev));
651         pptdev.bus = bus;
652         pptdev.slot = slot;
653         pptdev.func = func;
654
655         return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
656 }
657
658 int
659 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
660                    vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
661 {
662         struct vm_pptdev_mmio pptmmio;
663
664         bzero(&pptmmio, sizeof(pptmmio));
665         pptmmio.bus = bus;
666         pptmmio.slot = slot;
667         pptmmio.func = func;
668         pptmmio.gpa = gpa;
669         pptmmio.len = len;
670         pptmmio.hpa = hpa;
671
672         return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
673 }
674
675 int
676 vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
677     uint64_t addr, uint64_t msg, int numvec)
678 {
679         struct vm_pptdev_msi pptmsi;
680
681         bzero(&pptmsi, sizeof(pptmsi));
682         pptmsi.vcpu = vcpu;
683         pptmsi.bus = bus;
684         pptmsi.slot = slot;
685         pptmsi.func = func;
686         pptmsi.msg = msg;
687         pptmsi.addr = addr;
688         pptmsi.numvec = numvec;
689
690         return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
691 }
692
693 int     
694 vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot, int func,
695     int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
696 {
697         struct vm_pptdev_msix pptmsix;
698
699         bzero(&pptmsix, sizeof(pptmsix));
700         pptmsix.vcpu = vcpu;
701         pptmsix.bus = bus;
702         pptmsix.slot = slot;
703         pptmsix.func = func;
704         pptmsix.idx = idx;
705         pptmsix.msg = msg;
706         pptmsix.addr = addr;
707         pptmsix.vector_control = vector_control;
708
709         return ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix);
710 }
711
712 uint64_t *
713 vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
714              int *ret_entries)
715 {
716         int error;
717
718         static struct vm_stats vmstats;
719
720         vmstats.cpuid = vcpu;
721
722         error = ioctl(ctx->fd, VM_STATS, &vmstats);
723         if (error == 0) {
724                 if (ret_entries)
725                         *ret_entries = vmstats.num_entries;
726                 if (ret_tv)
727                         *ret_tv = vmstats.tv;
728                 return (vmstats.statbuf);
729         } else
730                 return (NULL);
731 }
732
733 const char *
734 vm_get_stat_desc(struct vmctx *ctx, int index)
735 {
736         static struct vm_stat_desc statdesc;
737
738         statdesc.index = index;
739         if (ioctl(ctx->fd, VM_STAT_DESC, &statdesc) == 0)
740                 return (statdesc.desc);
741         else
742                 return (NULL);
743 }
744
745 int
746 vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *state)
747 {
748         int error;
749         struct vm_x2apic x2apic;
750
751         bzero(&x2apic, sizeof(x2apic));
752         x2apic.cpuid = vcpu;
753
754         error = ioctl(ctx->fd, VM_GET_X2APIC_STATE, &x2apic);
755         *state = x2apic.state;
756         return (error);
757 }
758
759 int
760 vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state state)
761 {
762         int error;
763         struct vm_x2apic x2apic;
764
765         bzero(&x2apic, sizeof(x2apic));
766         x2apic.cpuid = vcpu;
767         x2apic.state = state;
768
769         error = ioctl(ctx->fd, VM_SET_X2APIC_STATE, &x2apic);
770
771         return (error);
772 }
773
774 /*
775  * From Intel Vol 3a:
776  * Table 9-1. IA-32 Processor States Following Power-up, Reset or INIT
777  */
778 int
779 vcpu_reset(struct vmctx *vmctx, int vcpu)
780 {
781         int error;
782         uint64_t rflags, rip, cr0, cr4, zero, desc_base, rdx;
783         uint32_t desc_access, desc_limit;
784         uint16_t sel;
785
786         zero = 0;
787
788         rflags = 0x2;
789         error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RFLAGS, rflags);
790         if (error)
791                 goto done;
792
793         rip = 0xfff0;
794         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RIP, rip)) != 0)
795                 goto done;
796
797         cr0 = CR0_NE;
798         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR0, cr0)) != 0)
799                 goto done;
800
801         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR3, zero)) != 0)
802                 goto done;
803         
804         cr4 = 0;
805         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CR4, cr4)) != 0)
806                 goto done;
807
808         /*
809          * CS: present, r/w, accessed, 16-bit, byte granularity, usable
810          */
811         desc_base = 0xffff0000;
812         desc_limit = 0xffff;
813         desc_access = 0x0093;
814         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_CS,
815                             desc_base, desc_limit, desc_access);
816         if (error)
817                 goto done;
818
819         sel = 0xf000;
820         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_CS, sel)) != 0)
821                 goto done;
822
823         /*
824          * SS,DS,ES,FS,GS: present, r/w, accessed, 16-bit, byte granularity
825          */
826         desc_base = 0;
827         desc_limit = 0xffff;
828         desc_access = 0x0093;
829         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_SS,
830                             desc_base, desc_limit, desc_access);
831         if (error)
832                 goto done;
833
834         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_DS,
835                             desc_base, desc_limit, desc_access);
836         if (error)
837                 goto done;
838
839         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_ES,
840                             desc_base, desc_limit, desc_access);
841         if (error)
842                 goto done;
843
844         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_FS,
845                             desc_base, desc_limit, desc_access);
846         if (error)
847                 goto done;
848
849         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GS,
850                             desc_base, desc_limit, desc_access);
851         if (error)
852                 goto done;
853
854         sel = 0;
855         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_SS, sel)) != 0)
856                 goto done;
857         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_DS, sel)) != 0)
858                 goto done;
859         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_ES, sel)) != 0)
860                 goto done;
861         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_FS, sel)) != 0)
862                 goto done;
863         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_GS, sel)) != 0)
864                 goto done;
865
866         /* General purpose registers */
867         rdx = 0xf00;
868         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RAX, zero)) != 0)
869                 goto done;
870         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBX, zero)) != 0)
871                 goto done;
872         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RCX, zero)) != 0)
873                 goto done;
874         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDX, rdx)) != 0)
875                 goto done;
876         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSI, zero)) != 0)
877                 goto done;
878         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RDI, zero)) != 0)
879                 goto done;
880         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RBP, zero)) != 0)
881                 goto done;
882         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_RSP, zero)) != 0)
883                 goto done;
884
885         /* GDTR, IDTR */
886         desc_base = 0;
887         desc_limit = 0xffff;
888         desc_access = 0;
889         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_GDTR,
890                             desc_base, desc_limit, desc_access);
891         if (error != 0)
892                 goto done;
893
894         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_IDTR,
895                             desc_base, desc_limit, desc_access);
896         if (error != 0)
897                 goto done;
898
899         /* TR */
900         desc_base = 0;
901         desc_limit = 0xffff;
902         desc_access = 0x0000008b;
903         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_TR, 0, 0, desc_access);
904         if (error)
905                 goto done;
906
907         sel = 0;
908         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_TR, sel)) != 0)
909                 goto done;
910
911         /* LDTR */
912         desc_base = 0;
913         desc_limit = 0xffff;
914         desc_access = 0x00000082;
915         error = vm_set_desc(vmctx, vcpu, VM_REG_GUEST_LDTR, desc_base,
916                             desc_limit, desc_access);
917         if (error)
918                 goto done;
919
920         sel = 0;
921         if ((error = vm_set_register(vmctx, vcpu, VM_REG_GUEST_LDTR, 0)) != 0)
922                 goto done;
923
924         /* XXX cr2, debug registers */
925
926         error = 0;
927 done:
928         return (error);
929 }
930
931 int
932 vm_get_gpa_pmap(struct vmctx *ctx, uint64_t gpa, uint64_t *pte, int *num)
933 {
934         int error, i;
935         struct vm_gpa_pte gpapte;
936
937         bzero(&gpapte, sizeof(gpapte));
938         gpapte.gpa = gpa;
939
940         error = ioctl(ctx->fd, VM_GET_GPA_PMAP, &gpapte);
941
942         if (error == 0) {
943                 *num = gpapte.ptenum;
944                 for (i = 0; i < gpapte.ptenum; i++)
945                         pte[i] = gpapte.pte[i];
946         }
947
948         return (error);
949 }
950
951 int
952 vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities)
953 {
954         int error;
955         struct vm_hpet_cap cap;
956
957         bzero(&cap, sizeof(struct vm_hpet_cap));
958         error = ioctl(ctx->fd, VM_GET_HPET_CAPABILITIES, &cap);
959         if (capabilities != NULL)
960                 *capabilities = cap.capabilities;
961         return (error);
962 }
963
964 int
965 vm_gla2gpa(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
966     uint64_t gla, int prot, uint64_t *gpa, int *fault)
967 {
968         struct vm_gla2gpa gg;
969         int error;
970
971         bzero(&gg, sizeof(struct vm_gla2gpa));
972         gg.vcpuid = vcpu;
973         gg.prot = prot;
974         gg.gla = gla;
975         gg.paging = *paging;
976
977         error = ioctl(ctx->fd, VM_GLA2GPA, &gg);
978         if (error == 0) {
979                 *fault = gg.fault;
980                 *gpa = gg.gpa;
981         }
982         return (error);
983 }
984
985 #ifndef min
986 #define min(a,b)        (((a) < (b)) ? (a) : (b))
987 #endif
988
989 int
990 vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *paging,
991     uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
992     int *fault)
993 {
994         void *va;
995         uint64_t gpa;
996         int error, i, n, off;
997
998         for (i = 0; i < iovcnt; i++) {
999                 iov[i].iov_base = 0;
1000                 iov[i].iov_len = 0;
1001         }
1002
1003         while (len) {
1004                 assert(iovcnt > 0);
1005                 error = vm_gla2gpa(ctx, vcpu, paging, gla, prot, &gpa, fault);
1006                 if (error || *fault)
1007                         return (error);
1008
1009                 off = gpa & PAGE_MASK;
1010                 n = min(len, PAGE_SIZE - off);
1011
1012                 va = vm_map_gpa(ctx, gpa, n);
1013                 if (va == NULL)
1014                         return (EFAULT);
1015
1016                 iov->iov_base = va;
1017                 iov->iov_len = n;
1018                 iov++;
1019                 iovcnt--;
1020
1021                 gla += n;
1022                 len -= n;
1023         }
1024         return (0);
1025 }
1026
1027 void
1028 vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov, int iovcnt)
1029 {
1030
1031         return;
1032 }
1033
1034 void
1035 vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *iov, void *vp, size_t len)
1036 {
1037         const char *src;
1038         char *dst;
1039         size_t n;
1040
1041         dst = vp;
1042         while (len) {
1043                 assert(iov->iov_len);
1044                 n = min(len, iov->iov_len);
1045                 src = iov->iov_base;
1046                 bcopy(src, dst, n);
1047
1048                 iov++;
1049                 dst += n;
1050                 len -= n;
1051         }
1052 }
1053
1054 void
1055 vm_copyout(struct vmctx *ctx, int vcpu, const void *vp, struct iovec *iov,
1056     size_t len)
1057 {
1058         const char *src;
1059         char *dst;
1060         size_t n;
1061
1062         src = vp;
1063         while (len) {
1064                 assert(iov->iov_len);
1065                 n = min(len, iov->iov_len);
1066                 dst = iov->iov_base;
1067                 bcopy(src, dst, n);
1068
1069                 iov++;
1070                 src += n;
1071                 len -= n;
1072         }
1073 }
1074
1075 static int
1076 vm_get_cpus(struct vmctx *ctx, int which, cpuset_t *cpus)
1077 {
1078         struct vm_cpuset vm_cpuset;
1079         int error;
1080
1081         bzero(&vm_cpuset, sizeof(struct vm_cpuset));
1082         vm_cpuset.which = which;
1083         vm_cpuset.cpusetsize = sizeof(cpuset_t);
1084         vm_cpuset.cpus = cpus;
1085
1086         error = ioctl(ctx->fd, VM_GET_CPUS, &vm_cpuset);
1087         return (error);
1088 }
1089
1090 int
1091 vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus)
1092 {
1093
1094         return (vm_get_cpus(ctx, VM_ACTIVE_CPUS, cpus));
1095 }
1096
1097 int
1098 vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus)
1099 {
1100
1101         return (vm_get_cpus(ctx, VM_SUSPENDED_CPUS, cpus));
1102 }
1103
1104 int
1105 vm_activate_cpu(struct vmctx *ctx, int vcpu)
1106 {
1107         struct vm_activate_cpu ac;
1108         int error;
1109
1110         bzero(&ac, sizeof(struct vm_activate_cpu));
1111         ac.vcpuid = vcpu;
1112         error = ioctl(ctx->fd, VM_ACTIVATE_CPU, &ac);
1113         return (error);
1114 }
1115
1116 int
1117 vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *info1, uint64_t *info2)
1118 {
1119         struct vm_intinfo vmii;
1120         int error;
1121
1122         bzero(&vmii, sizeof(struct vm_intinfo));
1123         vmii.vcpuid = vcpu;
1124         error = ioctl(ctx->fd, VM_GET_INTINFO, &vmii);
1125         if (error == 0) {
1126                 *info1 = vmii.info1;
1127                 *info2 = vmii.info2;
1128         }
1129         return (error);
1130 }
1131
1132 int
1133 vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t info1)
1134 {
1135         struct vm_intinfo vmii;
1136         int error;
1137
1138         bzero(&vmii, sizeof(struct vm_intinfo));
1139         vmii.vcpuid = vcpu;
1140         vmii.info1 = info1;
1141         error = ioctl(ctx->fd, VM_SET_INTINFO, &vmii);
1142         return (error);
1143 }
1144
1145 int
1146 vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value)
1147 {
1148         struct vm_rtc_data rtcdata;
1149         int error;
1150
1151         bzero(&rtcdata, sizeof(struct vm_rtc_data));
1152         rtcdata.offset = offset;
1153         rtcdata.value = value;
1154         error = ioctl(ctx->fd, VM_RTC_WRITE, &rtcdata);
1155         return (error);
1156 }
1157
1158 int
1159 vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval)
1160 {
1161         struct vm_rtc_data rtcdata;
1162         int error;
1163
1164         bzero(&rtcdata, sizeof(struct vm_rtc_data));
1165         rtcdata.offset = offset;
1166         error = ioctl(ctx->fd, VM_RTC_READ, &rtcdata);
1167         if (error == 0)
1168                 *retval = rtcdata.value;
1169         return (error);
1170 }
1171
1172 int
1173 vm_rtc_settime(struct vmctx *ctx, time_t secs)
1174 {
1175         struct vm_rtc_time rtctime;
1176         int error;
1177
1178         bzero(&rtctime, sizeof(struct vm_rtc_time));
1179         rtctime.secs = secs;
1180         error = ioctl(ctx->fd, VM_RTC_SETTIME, &rtctime);
1181         return (error);
1182 }
1183
1184 int
1185 vm_rtc_gettime(struct vmctx *ctx, time_t *secs)
1186 {
1187         struct vm_rtc_time rtctime;
1188         int error;
1189
1190         bzero(&rtctime, sizeof(struct vm_rtc_time));
1191         error = ioctl(ctx->fd, VM_RTC_GETTIME, &rtctime);
1192         if (error == 0)
1193                 *secs = rtctime.secs;
1194         return (error);
1195 }
1196
1197 int
1198 vm_restart_instruction(void *arg, int vcpu)
1199 {
1200         struct vmctx *ctx = arg;
1201
1202         return (ioctl(ctx->fd, VM_RESTART_INSTRUCTION, &vcpu));
1203 }