Home | History | Annotate | Line # | Download | only in nvmm
nvmm_internal.h revision 1.18
      1 /*	$NetBSD: nvmm_internal.h,v 1.18 2020/09/05 07:22:25 maxv 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 #define NVMM_MAX_MACHINES	128
     35 #define NVMM_MAX_VCPUS		256
     36 #define NVMM_MAX_HMAPPINGS	32
     37 #define NVMM_MAX_RAM		(128ULL * (1 << 30))
     38 
     39 struct nvmm_owner {
     40 	pid_t pid;
     41 };
     42 
     43 struct nvmm_cpu {
     44 	/* Shared. */
     45 	bool present;
     46 	nvmm_cpuid_t cpuid;
     47 	kmutex_t lock;
     48 
     49 	/* Comm page. */
     50 	struct nvmm_comm_page *comm;
     51 
     52 	/* Last host CPU on which the VCPU ran. */
     53 	int hcpu_last;
     54 
     55 	/* Implementation-specific. */
     56 	void *cpudata;
     57 };
     58 
     59 struct nvmm_hmapping {
     60 	bool present;
     61 	uintptr_t hva;
     62 	size_t size;
     63 	struct uvm_object *uobj;
     64 };
     65 
     66 struct nvmm_machine {
     67 	bool present;
     68 	nvmm_machid_t machid;
     69 	time_t time;
     70 	struct nvmm_owner *owner;
     71 	krwlock_t lock;
     72 
     73 	/* Comm */
     74 	struct uvm_object *commuobj;
     75 
     76 	/* Kernel */
     77 	struct vmspace *vm;
     78 	gpaddr_t gpa_begin;
     79 	gpaddr_t gpa_end;
     80 
     81 	/* Host Mappings */
     82 	struct nvmm_hmapping hmap[NVMM_MAX_HMAPPINGS];
     83 
     84 	/* CPU */
     85 	volatile unsigned int ncpus;
     86 	struct nvmm_cpu cpus[NVMM_MAX_VCPUS];
     87 
     88 	/* Implementation-specific */
     89 	void *machdata;
     90 };
     91 
     92 struct nvmm_impl {
     93 	const char *name;
     94 	bool (*ident)(void);
     95 	void (*init)(void);
     96 	void (*fini)(void);
     97 	void (*capability)(struct nvmm_capability *);
     98 
     99 	size_t mach_conf_max;
    100 	const size_t *mach_conf_sizes;
    101 
    102 	size_t vcpu_conf_max;
    103 	const size_t *vcpu_conf_sizes;
    104 
    105 	size_t state_size;
    106 
    107 	void (*machine_create)(struct nvmm_machine *);
    108 	void (*machine_destroy)(struct nvmm_machine *);
    109 	int (*machine_configure)(struct nvmm_machine *, uint64_t, void *);
    110 
    111 	int (*vcpu_create)(struct nvmm_machine *, struct nvmm_cpu *);
    112 	void (*vcpu_destroy)(struct nvmm_machine *, struct nvmm_cpu *);
    113 	int (*vcpu_configure)(struct nvmm_cpu *, uint64_t, void *);
    114 	void (*vcpu_setstate)(struct nvmm_cpu *);
    115 	void (*vcpu_getstate)(struct nvmm_cpu *);
    116 	int (*vcpu_inject)(struct nvmm_cpu *);
    117 	int (*vcpu_run)(struct nvmm_machine *, struct nvmm_cpu *,
    118 	    struct nvmm_vcpu_exit *);
    119 };
    120 
    121 #if defined(__x86_64__)
    122 extern const struct nvmm_impl nvmm_x86_svm;
    123 extern const struct nvmm_impl nvmm_x86_vmx;
    124 #endif
    125 
    126 static inline bool
    127 nvmm_return_needed(void)
    128 {
    129 	if (preempt_needed()) {
    130 		return true;
    131 	}
    132 	if (curlwp->l_flag & LW_USERRET) {
    133 		return true;
    134 	}
    135 	return false;
    136 }
    137 
    138 #endif /* _NVMM_INTERNAL_H_ */
    139