Home | History | Annotate | Line # | Download | only in nvmm
nvmm_internal.h revision 1.19.4.1
      1 /*	$NetBSD: nvmm_internal.h,v 1.19.4.1 2021/04/03 21:44:51 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
      5  * All rights reserved.
      6  *
      7  * This code is part of the NVMM hypervisor.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #ifndef _NVMM_INTERNAL_H_
     32 #define _NVMM_INTERNAL_H_
     33 
     34 #include <sys/types.h>
     35 
     36 #include <sys/lwp.h>
     37 #include <sys/mutex.h>
     38 #include <sys/rwlock.h>
     39 #include <sys/sched.h>
     40 
     41 #include <dev/nvmm/nvmm.h>
     42 
     43 struct uvm_object;
     44 struct vmspace;
     45 
     46 #define NVMM_MAX_MACHINES	128
     47 #define NVMM_MAX_VCPUS		256
     48 #define NVMM_MAX_HMAPPINGS	32
     49 #define NVMM_MAX_RAM		(128ULL * (1 << 30))
     50 
     51 struct nvmm_owner {
     52 	pid_t pid;
     53 };
     54 
     55 struct nvmm_cpu {
     56 	/* Shared. */
     57 	bool present;
     58 	nvmm_cpuid_t cpuid;
     59 	kmutex_t lock;
     60 
     61 	/* Comm page. */
     62 	struct nvmm_comm_page *comm;
     63 
     64 	/* Last host CPU on which the VCPU ran. */
     65 	int hcpu_last;
     66 
     67 	/* Implementation-specific. */
     68 	void *cpudata;
     69 };
     70 
     71 struct nvmm_hmapping {
     72 	bool present;
     73 	uintptr_t hva;
     74 	size_t size;
     75 	struct uvm_object *uobj;
     76 };
     77 
     78 struct nvmm_machine {
     79 	bool present;
     80 	nvmm_machid_t machid;
     81 	time_t time;
     82 	struct nvmm_owner *owner;
     83 	krwlock_t lock;
     84 
     85 	/* Comm */
     86 	struct uvm_object *commuobj;
     87 
     88 	/* Kernel */
     89 	struct vmspace *vm;
     90 	gpaddr_t gpa_begin;
     91 	gpaddr_t gpa_end;
     92 
     93 	/* Host Mappings */
     94 	struct nvmm_hmapping hmap[NVMM_MAX_HMAPPINGS];
     95 
     96 	/* CPU */
     97 	volatile unsigned int ncpus;
     98 	struct nvmm_cpu cpus[NVMM_MAX_VCPUS];
     99 
    100 	/* Implementation-specific */
    101 	void *machdata;
    102 };
    103 
    104 struct nvmm_impl {
    105 	const char *name;
    106 	bool (*ident)(void);
    107 	void (*init)(void);
    108 	void (*fini)(void);
    109 	void (*capability)(struct nvmm_capability *);
    110 
    111 	size_t mach_conf_max;
    112 	const size_t *mach_conf_sizes;
    113 
    114 	size_t vcpu_conf_max;
    115 	const size_t *vcpu_conf_sizes;
    116 
    117 	size_t state_size;
    118 
    119 	void (*machine_create)(struct nvmm_machine *);
    120 	void (*machine_destroy)(struct nvmm_machine *);
    121 	int (*machine_configure)(struct nvmm_machine *, uint64_t, void *);
    122 
    123 	int (*vcpu_create)(struct nvmm_machine *, struct nvmm_cpu *);
    124 	void (*vcpu_destroy)(struct nvmm_machine *, struct nvmm_cpu *);
    125 	int (*vcpu_configure)(struct nvmm_cpu *, uint64_t, void *);
    126 	void (*vcpu_setstate)(struct nvmm_cpu *);
    127 	void (*vcpu_getstate)(struct nvmm_cpu *);
    128 	int (*vcpu_inject)(struct nvmm_cpu *);
    129 	int (*vcpu_run)(struct nvmm_machine *, struct nvmm_cpu *,
    130 	    struct nvmm_vcpu_exit *);
    131 };
    132 
    133 #if defined(__x86_64__)
    134 extern const struct nvmm_impl nvmm_x86_svm;
    135 extern const struct nvmm_impl nvmm_x86_vmx;
    136 #endif
    137 
    138 static inline bool
    139 nvmm_return_needed(struct nvmm_cpu *vcpu, struct nvmm_vcpu_exit *exit)
    140 {
    141 
    142 	if (preempt_needed()) {
    143 		exit->reason = NVMM_VCPU_EXIT_NONE;
    144 		return true;
    145 	}
    146 	if (curlwp->l_flag & LW_USERRET) {
    147 		exit->reason = NVMM_VCPU_EXIT_NONE;
    148 		return true;
    149 	}
    150 	if (vcpu->comm->stop) {
    151 		exit->reason = NVMM_VCPU_EXIT_STOPPED;
    152 		return true;
    153 	}
    154 
    155 	return false;
    156 }
    157 
    158 #endif /* _NVMM_INTERNAL_H_ */
    159