| 1 | /*	$NetBSD: nvmm.h,v 1.16 2021/03/26 15:59:53 reinoud 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_H_ |
| 32 | #define _NVMM_H_ |
| 33 | |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #ifndef _KERNEL |
| 37 | #include <stdbool.h> |
| 38 | #endif |
| 39 | |
| 40 | typedef uint64_t	gpaddr_t; |
| 41 | typedef uint64_t	gvaddr_t; |
| 42 | |
| 43 | typedef uint32_t	nvmm_machid_t; |
| 44 | typedef uint32_t	nvmm_cpuid_t; |
| 45 | |
| 46 | #if defined(__x86_64__) |
| 47 | #include <dev/nvmm/x86/nvmm_x86.h> |
| 48 | #endif |
| 49 | |
| 50 | #define NVMM_KERN_VERSION		2 |
| 51 | |
| 52 | /* |
| 53 | * Version 1 - Initial release in NetBSD 9.0. |
| 54 | * Version 2 - Added nvmm_vcpu::stop. |
| 55 | */ |
| 56 | |
| 57 | struct nvmm_capability { |
| 58 | 	uint32_t version; |
| 59 | 	uint32_t state_size; |
| 60 | 	uint32_t max_machines; |
| 61 | 	uint32_t max_vcpus; |
| 62 | 	uint64_t max_ram; |
| 63 | 	struct nvmm_cap_md arch; |
| 64 | }; |
| 65 | |
| 66 | /* Machine configuration slots. */ |
| 67 | #define NVMM_MACH_CONF_LIBNVMM_BEGIN	0 |
| 68 | #define NVMM_MACH_CONF_MI_BEGIN		100 |
| 69 | #define NVMM_MACH_CONF_MD_BEGIN		200 |
| 70 | #define NVMM_MACH_CONF_MD(op)		(op - NVMM_MACH_CONF_MD_BEGIN) |
| 71 | |
| 72 | /* VCPU configuration slots. */ |
| 73 | #define NVMM_VCPU_CONF_LIBNVMM_BEGIN	0 |
| 74 | #define NVMM_VCPU_CONF_MI_BEGIN		100 |
| 75 | #define NVMM_VCPU_CONF_MD_BEGIN		200 |
| 76 | #define NVMM_VCPU_CONF_MD(op)		(op - NVMM_VCPU_CONF_MD_BEGIN) |
| 77 | |
| 78 | struct nvmm_comm_page { |
| 79 | 	/* State. */ |
| 80 | 	uint64_t state_wanted; |
| 81 | 	uint64_t state_cached; |
| 82 | 	uint64_t state_commit; |
| 83 | 	struct nvmm_vcpu_state state; |
| 84 | |
| 85 | 	/* Event. */ |
| 86 | 	bool event_commit; |
| 87 | 	struct nvmm_vcpu_event event; |
| 88 | |
| 89 | 	/* Race-free exit from nvmm_vcpu_run() without signals. */ |
| 90 | 	volatile int stop; |
| 91 | }; |
| 92 | |
| 93 | /* |
| 94 | * Bits 20:27 -> machid |
| 95 | * Bits 12:19 -> cpuid |
| 96 | */ |
| 97 | #define NVMM_COMM_OFF(machid, cpuid)	\ |
| 98 | 	((((uint64_t)machid & 0xFFULL) << 20) | (((uint64_t)cpuid & 0xFFULL) << 12)) |
| 99 | |
| 100 | #define NVMM_COMM_MACHID(off)		\ |
| 101 | 	((off >> 20) & 0xFF) |
| 102 | |
| 103 | #define NVMM_COMM_CPUID(off)		\ |
| 104 | 	((off >> 12) & 0xFF) |
| 105 | |
| 106 | #ifdef _KERNEL |
| 107 | /* At most one page, for the NVMM_COMM_* macros. */ |
| 108 | CTASSERT(sizeof(struct nvmm_comm_page) <= PAGE_SIZE); |
| 109 | #endif |
| 110 | |
| 111 | #endif |