Home | History | Annotate | Line # | Download | only in x86
nvmm_x86_svm.c revision 1.46.4.13
      1  1.46.4.13  martin /*	$NetBSD: nvmm_x86_svm.c,v 1.46.4.13 2020/09/13 11:56:44 martin Exp $	*/
      2        1.1    maxv 
      3        1.1    maxv /*
      4   1.46.4.2  martin  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
      5        1.1    maxv  * All rights reserved.
      6        1.1    maxv  *
      7        1.1    maxv  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1    maxv  * by Maxime Villard.
      9        1.1    maxv  *
     10        1.1    maxv  * Redistribution and use in source and binary forms, with or without
     11        1.1    maxv  * modification, are permitted provided that the following conditions
     12        1.1    maxv  * are met:
     13        1.1    maxv  * 1. Redistributions of source code must retain the above copyright
     14        1.1    maxv  *    notice, this list of conditions and the following disclaimer.
     15        1.1    maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1    maxv  *    notice, this list of conditions and the following disclaimer in the
     17        1.1    maxv  *    documentation and/or other materials provided with the distribution.
     18        1.1    maxv  *
     19        1.1    maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1    maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1    maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1    maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1    maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1    maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1    maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1    maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1    maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1    maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1    maxv  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1    maxv  */
     31        1.1    maxv 
     32        1.1    maxv #include <sys/cdefs.h>
     33  1.46.4.13  martin __KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.46.4.13 2020/09/13 11:56:44 martin Exp $");
     34        1.1    maxv 
     35        1.1    maxv #include <sys/param.h>
     36        1.1    maxv #include <sys/systm.h>
     37        1.1    maxv #include <sys/kernel.h>
     38        1.1    maxv #include <sys/kmem.h>
     39        1.1    maxv #include <sys/cpu.h>
     40        1.1    maxv #include <sys/xcall.h>
     41       1.35    maxv #include <sys/mman.h>
     42        1.1    maxv 
     43        1.1    maxv #include <uvm/uvm.h>
     44        1.1    maxv #include <uvm/uvm_page.h>
     45        1.1    maxv 
     46        1.1    maxv #include <x86/cputypes.h>
     47        1.1    maxv #include <x86/specialreg.h>
     48        1.1    maxv #include <x86/pmap.h>
     49        1.1    maxv #include <x86/dbregs.h>
     50       1.24    maxv #include <x86/cpu_counter.h>
     51        1.1    maxv #include <machine/cpuvar.h>
     52        1.1    maxv 
     53        1.1    maxv #include <dev/nvmm/nvmm.h>
     54        1.1    maxv #include <dev/nvmm/nvmm_internal.h>
     55        1.1    maxv #include <dev/nvmm/x86/nvmm_x86.h>
     56        1.1    maxv 
     57        1.1    maxv int svm_vmrun(paddr_t, uint64_t *);
     58        1.1    maxv 
     59        1.1    maxv #define	MSR_VM_HSAVE_PA	0xC0010117
     60        1.1    maxv 
     61        1.1    maxv /* -------------------------------------------------------------------------- */
     62        1.1    maxv 
     63        1.1    maxv #define VMCB_EXITCODE_CR0_READ		0x0000
     64        1.1    maxv #define VMCB_EXITCODE_CR1_READ		0x0001
     65        1.1    maxv #define VMCB_EXITCODE_CR2_READ		0x0002
     66        1.1    maxv #define VMCB_EXITCODE_CR3_READ		0x0003
     67        1.1    maxv #define VMCB_EXITCODE_CR4_READ		0x0004
     68        1.1    maxv #define VMCB_EXITCODE_CR5_READ		0x0005
     69        1.1    maxv #define VMCB_EXITCODE_CR6_READ		0x0006
     70        1.1    maxv #define VMCB_EXITCODE_CR7_READ		0x0007
     71        1.1    maxv #define VMCB_EXITCODE_CR8_READ		0x0008
     72        1.1    maxv #define VMCB_EXITCODE_CR9_READ		0x0009
     73        1.1    maxv #define VMCB_EXITCODE_CR10_READ		0x000A
     74        1.1    maxv #define VMCB_EXITCODE_CR11_READ		0x000B
     75        1.1    maxv #define VMCB_EXITCODE_CR12_READ		0x000C
     76        1.1    maxv #define VMCB_EXITCODE_CR13_READ		0x000D
     77        1.1    maxv #define VMCB_EXITCODE_CR14_READ		0x000E
     78        1.1    maxv #define VMCB_EXITCODE_CR15_READ		0x000F
     79        1.1    maxv #define VMCB_EXITCODE_CR0_WRITE		0x0010
     80        1.1    maxv #define VMCB_EXITCODE_CR1_WRITE		0x0011
     81        1.1    maxv #define VMCB_EXITCODE_CR2_WRITE		0x0012
     82        1.1    maxv #define VMCB_EXITCODE_CR3_WRITE		0x0013
     83        1.1    maxv #define VMCB_EXITCODE_CR4_WRITE		0x0014
     84        1.1    maxv #define VMCB_EXITCODE_CR5_WRITE		0x0015
     85        1.1    maxv #define VMCB_EXITCODE_CR6_WRITE		0x0016
     86        1.1    maxv #define VMCB_EXITCODE_CR7_WRITE		0x0017
     87        1.1    maxv #define VMCB_EXITCODE_CR8_WRITE		0x0018
     88        1.1    maxv #define VMCB_EXITCODE_CR9_WRITE		0x0019
     89        1.1    maxv #define VMCB_EXITCODE_CR10_WRITE	0x001A
     90        1.1    maxv #define VMCB_EXITCODE_CR11_WRITE	0x001B
     91        1.1    maxv #define VMCB_EXITCODE_CR12_WRITE	0x001C
     92        1.1    maxv #define VMCB_EXITCODE_CR13_WRITE	0x001D
     93        1.1    maxv #define VMCB_EXITCODE_CR14_WRITE	0x001E
     94        1.1    maxv #define VMCB_EXITCODE_CR15_WRITE	0x001F
     95        1.1    maxv #define VMCB_EXITCODE_DR0_READ		0x0020
     96        1.1    maxv #define VMCB_EXITCODE_DR1_READ		0x0021
     97        1.1    maxv #define VMCB_EXITCODE_DR2_READ		0x0022
     98        1.1    maxv #define VMCB_EXITCODE_DR3_READ		0x0023
     99        1.1    maxv #define VMCB_EXITCODE_DR4_READ		0x0024
    100        1.1    maxv #define VMCB_EXITCODE_DR5_READ		0x0025
    101        1.1    maxv #define VMCB_EXITCODE_DR6_READ		0x0026
    102        1.1    maxv #define VMCB_EXITCODE_DR7_READ		0x0027
    103        1.1    maxv #define VMCB_EXITCODE_DR8_READ		0x0028
    104        1.1    maxv #define VMCB_EXITCODE_DR9_READ		0x0029
    105        1.1    maxv #define VMCB_EXITCODE_DR10_READ		0x002A
    106        1.1    maxv #define VMCB_EXITCODE_DR11_READ		0x002B
    107        1.1    maxv #define VMCB_EXITCODE_DR12_READ		0x002C
    108        1.1    maxv #define VMCB_EXITCODE_DR13_READ		0x002D
    109        1.1    maxv #define VMCB_EXITCODE_DR14_READ		0x002E
    110        1.1    maxv #define VMCB_EXITCODE_DR15_READ		0x002F
    111        1.1    maxv #define VMCB_EXITCODE_DR0_WRITE		0x0030
    112        1.1    maxv #define VMCB_EXITCODE_DR1_WRITE		0x0031
    113        1.1    maxv #define VMCB_EXITCODE_DR2_WRITE		0x0032
    114        1.1    maxv #define VMCB_EXITCODE_DR3_WRITE		0x0033
    115        1.1    maxv #define VMCB_EXITCODE_DR4_WRITE		0x0034
    116        1.1    maxv #define VMCB_EXITCODE_DR5_WRITE		0x0035
    117        1.1    maxv #define VMCB_EXITCODE_DR6_WRITE		0x0036
    118        1.1    maxv #define VMCB_EXITCODE_DR7_WRITE		0x0037
    119        1.1    maxv #define VMCB_EXITCODE_DR8_WRITE		0x0038
    120        1.1    maxv #define VMCB_EXITCODE_DR9_WRITE		0x0039
    121        1.1    maxv #define VMCB_EXITCODE_DR10_WRITE	0x003A
    122        1.1    maxv #define VMCB_EXITCODE_DR11_WRITE	0x003B
    123        1.1    maxv #define VMCB_EXITCODE_DR12_WRITE	0x003C
    124        1.1    maxv #define VMCB_EXITCODE_DR13_WRITE	0x003D
    125        1.1    maxv #define VMCB_EXITCODE_DR14_WRITE	0x003E
    126        1.1    maxv #define VMCB_EXITCODE_DR15_WRITE	0x003F
    127        1.1    maxv #define VMCB_EXITCODE_EXCP0		0x0040
    128        1.1    maxv #define VMCB_EXITCODE_EXCP1		0x0041
    129        1.1    maxv #define VMCB_EXITCODE_EXCP2		0x0042
    130        1.1    maxv #define VMCB_EXITCODE_EXCP3		0x0043
    131        1.1    maxv #define VMCB_EXITCODE_EXCP4		0x0044
    132        1.1    maxv #define VMCB_EXITCODE_EXCP5		0x0045
    133        1.1    maxv #define VMCB_EXITCODE_EXCP6		0x0046
    134        1.1    maxv #define VMCB_EXITCODE_EXCP7		0x0047
    135        1.1    maxv #define VMCB_EXITCODE_EXCP8		0x0048
    136        1.1    maxv #define VMCB_EXITCODE_EXCP9		0x0049
    137        1.1    maxv #define VMCB_EXITCODE_EXCP10		0x004A
    138        1.1    maxv #define VMCB_EXITCODE_EXCP11		0x004B
    139        1.1    maxv #define VMCB_EXITCODE_EXCP12		0x004C
    140        1.1    maxv #define VMCB_EXITCODE_EXCP13		0x004D
    141        1.1    maxv #define VMCB_EXITCODE_EXCP14		0x004E
    142        1.1    maxv #define VMCB_EXITCODE_EXCP15		0x004F
    143        1.1    maxv #define VMCB_EXITCODE_EXCP16		0x0050
    144        1.1    maxv #define VMCB_EXITCODE_EXCP17		0x0051
    145        1.1    maxv #define VMCB_EXITCODE_EXCP18		0x0052
    146        1.1    maxv #define VMCB_EXITCODE_EXCP19		0x0053
    147        1.1    maxv #define VMCB_EXITCODE_EXCP20		0x0054
    148        1.1    maxv #define VMCB_EXITCODE_EXCP21		0x0055
    149        1.1    maxv #define VMCB_EXITCODE_EXCP22		0x0056
    150        1.1    maxv #define VMCB_EXITCODE_EXCP23		0x0057
    151        1.1    maxv #define VMCB_EXITCODE_EXCP24		0x0058
    152        1.1    maxv #define VMCB_EXITCODE_EXCP25		0x0059
    153        1.1    maxv #define VMCB_EXITCODE_EXCP26		0x005A
    154        1.1    maxv #define VMCB_EXITCODE_EXCP27		0x005B
    155        1.1    maxv #define VMCB_EXITCODE_EXCP28		0x005C
    156        1.1    maxv #define VMCB_EXITCODE_EXCP29		0x005D
    157        1.1    maxv #define VMCB_EXITCODE_EXCP30		0x005E
    158        1.1    maxv #define VMCB_EXITCODE_EXCP31		0x005F
    159        1.1    maxv #define VMCB_EXITCODE_INTR		0x0060
    160        1.1    maxv #define VMCB_EXITCODE_NMI		0x0061
    161        1.1    maxv #define VMCB_EXITCODE_SMI		0x0062
    162        1.1    maxv #define VMCB_EXITCODE_INIT		0x0063
    163        1.1    maxv #define VMCB_EXITCODE_VINTR		0x0064
    164        1.1    maxv #define VMCB_EXITCODE_CR0_SEL_WRITE	0x0065
    165        1.1    maxv #define VMCB_EXITCODE_IDTR_READ		0x0066
    166        1.1    maxv #define VMCB_EXITCODE_GDTR_READ		0x0067
    167        1.1    maxv #define VMCB_EXITCODE_LDTR_READ		0x0068
    168        1.1    maxv #define VMCB_EXITCODE_TR_READ		0x0069
    169        1.1    maxv #define VMCB_EXITCODE_IDTR_WRITE	0x006A
    170        1.1    maxv #define VMCB_EXITCODE_GDTR_WRITE	0x006B
    171        1.1    maxv #define VMCB_EXITCODE_LDTR_WRITE	0x006C
    172        1.1    maxv #define VMCB_EXITCODE_TR_WRITE		0x006D
    173        1.1    maxv #define VMCB_EXITCODE_RDTSC		0x006E
    174        1.1    maxv #define VMCB_EXITCODE_RDPMC		0x006F
    175        1.1    maxv #define VMCB_EXITCODE_PUSHF		0x0070
    176        1.1    maxv #define VMCB_EXITCODE_POPF		0x0071
    177        1.1    maxv #define VMCB_EXITCODE_CPUID		0x0072
    178        1.1    maxv #define VMCB_EXITCODE_RSM		0x0073
    179        1.1    maxv #define VMCB_EXITCODE_IRET		0x0074
    180        1.1    maxv #define VMCB_EXITCODE_SWINT		0x0075
    181        1.1    maxv #define VMCB_EXITCODE_INVD		0x0076
    182        1.1    maxv #define VMCB_EXITCODE_PAUSE		0x0077
    183        1.1    maxv #define VMCB_EXITCODE_HLT		0x0078
    184        1.1    maxv #define VMCB_EXITCODE_INVLPG		0x0079
    185        1.1    maxv #define VMCB_EXITCODE_INVLPGA		0x007A
    186        1.1    maxv #define VMCB_EXITCODE_IOIO		0x007B
    187        1.1    maxv #define VMCB_EXITCODE_MSR		0x007C
    188        1.1    maxv #define VMCB_EXITCODE_TASK_SWITCH	0x007D
    189        1.1    maxv #define VMCB_EXITCODE_FERR_FREEZE	0x007E
    190        1.1    maxv #define VMCB_EXITCODE_SHUTDOWN		0x007F
    191        1.1    maxv #define VMCB_EXITCODE_VMRUN		0x0080
    192        1.1    maxv #define VMCB_EXITCODE_VMMCALL		0x0081
    193        1.1    maxv #define VMCB_EXITCODE_VMLOAD		0x0082
    194        1.1    maxv #define VMCB_EXITCODE_VMSAVE		0x0083
    195        1.1    maxv #define VMCB_EXITCODE_STGI		0x0084
    196        1.1    maxv #define VMCB_EXITCODE_CLGI		0x0085
    197        1.1    maxv #define VMCB_EXITCODE_SKINIT		0x0086
    198        1.1    maxv #define VMCB_EXITCODE_RDTSCP		0x0087
    199        1.1    maxv #define VMCB_EXITCODE_ICEBP		0x0088
    200        1.1    maxv #define VMCB_EXITCODE_WBINVD		0x0089
    201        1.1    maxv #define VMCB_EXITCODE_MONITOR		0x008A
    202        1.1    maxv #define VMCB_EXITCODE_MWAIT		0x008B
    203        1.1    maxv #define VMCB_EXITCODE_MWAIT_CONDITIONAL	0x008C
    204        1.1    maxv #define VMCB_EXITCODE_XSETBV		0x008D
    205   1.46.4.1  martin #define VMCB_EXITCODE_RDPRU		0x008E
    206        1.1    maxv #define VMCB_EXITCODE_EFER_WRITE_TRAP	0x008F
    207        1.1    maxv #define VMCB_EXITCODE_CR0_WRITE_TRAP	0x0090
    208        1.1    maxv #define VMCB_EXITCODE_CR1_WRITE_TRAP	0x0091
    209        1.1    maxv #define VMCB_EXITCODE_CR2_WRITE_TRAP	0x0092
    210        1.1    maxv #define VMCB_EXITCODE_CR3_WRITE_TRAP	0x0093
    211        1.1    maxv #define VMCB_EXITCODE_CR4_WRITE_TRAP	0x0094
    212        1.1    maxv #define VMCB_EXITCODE_CR5_WRITE_TRAP	0x0095
    213        1.1    maxv #define VMCB_EXITCODE_CR6_WRITE_TRAP	0x0096
    214        1.1    maxv #define VMCB_EXITCODE_CR7_WRITE_TRAP	0x0097
    215        1.1    maxv #define VMCB_EXITCODE_CR8_WRITE_TRAP	0x0098
    216        1.1    maxv #define VMCB_EXITCODE_CR9_WRITE_TRAP	0x0099
    217        1.1    maxv #define VMCB_EXITCODE_CR10_WRITE_TRAP	0x009A
    218        1.1    maxv #define VMCB_EXITCODE_CR11_WRITE_TRAP	0x009B
    219        1.1    maxv #define VMCB_EXITCODE_CR12_WRITE_TRAP	0x009C
    220        1.1    maxv #define VMCB_EXITCODE_CR13_WRITE_TRAP	0x009D
    221        1.1    maxv #define VMCB_EXITCODE_CR14_WRITE_TRAP	0x009E
    222        1.1    maxv #define VMCB_EXITCODE_CR15_WRITE_TRAP	0x009F
    223   1.46.4.8  martin #define VMCB_EXITCODE_INVLPGB		0x00A0
    224   1.46.4.8  martin #define VMCB_EXITCODE_INVLPGB_ILLEGAL	0x00A1
    225   1.46.4.8  martin #define VMCB_EXITCODE_INVPCID		0x00A2
    226   1.46.4.1  martin #define VMCB_EXITCODE_MCOMMIT		0x00A3
    227   1.46.4.8  martin #define VMCB_EXITCODE_TLBSYNC		0x00A4
    228        1.1    maxv #define VMCB_EXITCODE_NPF		0x0400
    229        1.1    maxv #define VMCB_EXITCODE_AVIC_INCOMP_IPI	0x0401
    230        1.1    maxv #define VMCB_EXITCODE_AVIC_NOACCEL	0x0402
    231        1.1    maxv #define VMCB_EXITCODE_VMGEXIT		0x0403
    232   1.46.4.8  martin #define VMCB_EXITCODE_BUSY		-2ULL
    233   1.46.4.7  martin #define VMCB_EXITCODE_INVALID		-1ULL
    234        1.1    maxv 
    235        1.1    maxv /* -------------------------------------------------------------------------- */
    236        1.1    maxv 
    237        1.1    maxv struct vmcb_ctrl {
    238        1.1    maxv 	uint32_t intercept_cr;
    239        1.1    maxv #define VMCB_CTRL_INTERCEPT_RCR(x)	__BIT( 0 + x)
    240        1.1    maxv #define VMCB_CTRL_INTERCEPT_WCR(x)	__BIT(16 + x)
    241        1.1    maxv 
    242        1.1    maxv 	uint32_t intercept_dr;
    243        1.1    maxv #define VMCB_CTRL_INTERCEPT_RDR(x)	__BIT( 0 + x)
    244        1.1    maxv #define VMCB_CTRL_INTERCEPT_WDR(x)	__BIT(16 + x)
    245        1.1    maxv 
    246        1.1    maxv 	uint32_t intercept_vec;
    247        1.1    maxv #define VMCB_CTRL_INTERCEPT_VEC(x)	__BIT(x)
    248        1.1    maxv 
    249        1.1    maxv 	uint32_t intercept_misc1;
    250        1.1    maxv #define VMCB_CTRL_INTERCEPT_INTR	__BIT(0)
    251        1.1    maxv #define VMCB_CTRL_INTERCEPT_NMI		__BIT(1)
    252        1.1    maxv #define VMCB_CTRL_INTERCEPT_SMI		__BIT(2)
    253        1.1    maxv #define VMCB_CTRL_INTERCEPT_INIT	__BIT(3)
    254        1.1    maxv #define VMCB_CTRL_INTERCEPT_VINTR	__BIT(4)
    255        1.1    maxv #define VMCB_CTRL_INTERCEPT_CR0_SPEC	__BIT(5)
    256        1.1    maxv #define VMCB_CTRL_INTERCEPT_RIDTR	__BIT(6)
    257        1.1    maxv #define VMCB_CTRL_INTERCEPT_RGDTR	__BIT(7)
    258        1.1    maxv #define VMCB_CTRL_INTERCEPT_RLDTR	__BIT(8)
    259        1.1    maxv #define VMCB_CTRL_INTERCEPT_RTR		__BIT(9)
    260        1.1    maxv #define VMCB_CTRL_INTERCEPT_WIDTR	__BIT(10)
    261        1.1    maxv #define VMCB_CTRL_INTERCEPT_WGDTR	__BIT(11)
    262        1.1    maxv #define VMCB_CTRL_INTERCEPT_WLDTR	__BIT(12)
    263        1.1    maxv #define VMCB_CTRL_INTERCEPT_WTR		__BIT(13)
    264        1.1    maxv #define VMCB_CTRL_INTERCEPT_RDTSC	__BIT(14)
    265        1.1    maxv #define VMCB_CTRL_INTERCEPT_RDPMC	__BIT(15)
    266        1.1    maxv #define VMCB_CTRL_INTERCEPT_PUSHF	__BIT(16)
    267        1.1    maxv #define VMCB_CTRL_INTERCEPT_POPF	__BIT(17)
    268        1.1    maxv #define VMCB_CTRL_INTERCEPT_CPUID	__BIT(18)
    269        1.1    maxv #define VMCB_CTRL_INTERCEPT_RSM		__BIT(19)
    270        1.1    maxv #define VMCB_CTRL_INTERCEPT_IRET	__BIT(20)
    271        1.1    maxv #define VMCB_CTRL_INTERCEPT_INTN	__BIT(21)
    272        1.1    maxv #define VMCB_CTRL_INTERCEPT_INVD	__BIT(22)
    273        1.1    maxv #define VMCB_CTRL_INTERCEPT_PAUSE	__BIT(23)
    274        1.1    maxv #define VMCB_CTRL_INTERCEPT_HLT		__BIT(24)
    275        1.1    maxv #define VMCB_CTRL_INTERCEPT_INVLPG	__BIT(25)
    276        1.1    maxv #define VMCB_CTRL_INTERCEPT_INVLPGA	__BIT(26)
    277        1.1    maxv #define VMCB_CTRL_INTERCEPT_IOIO_PROT	__BIT(27)
    278        1.1    maxv #define VMCB_CTRL_INTERCEPT_MSR_PROT	__BIT(28)
    279        1.1    maxv #define VMCB_CTRL_INTERCEPT_TASKSW	__BIT(29)
    280        1.1    maxv #define VMCB_CTRL_INTERCEPT_FERR_FREEZE	__BIT(30)
    281        1.1    maxv #define VMCB_CTRL_INTERCEPT_SHUTDOWN	__BIT(31)
    282        1.1    maxv 
    283        1.1    maxv 	uint32_t intercept_misc2;
    284        1.1    maxv #define VMCB_CTRL_INTERCEPT_VMRUN	__BIT(0)
    285        1.1    maxv #define VMCB_CTRL_INTERCEPT_VMMCALL	__BIT(1)
    286        1.1    maxv #define VMCB_CTRL_INTERCEPT_VMLOAD	__BIT(2)
    287        1.1    maxv #define VMCB_CTRL_INTERCEPT_VMSAVE	__BIT(3)
    288        1.1    maxv #define VMCB_CTRL_INTERCEPT_STGI	__BIT(4)
    289        1.1    maxv #define VMCB_CTRL_INTERCEPT_CLGI	__BIT(5)
    290        1.1    maxv #define VMCB_CTRL_INTERCEPT_SKINIT	__BIT(6)
    291        1.1    maxv #define VMCB_CTRL_INTERCEPT_RDTSCP	__BIT(7)
    292        1.1    maxv #define VMCB_CTRL_INTERCEPT_ICEBP	__BIT(8)
    293        1.1    maxv #define VMCB_CTRL_INTERCEPT_WBINVD	__BIT(9)
    294        1.1    maxv #define VMCB_CTRL_INTERCEPT_MONITOR	__BIT(10)
    295   1.46.4.1  martin #define VMCB_CTRL_INTERCEPT_MWAIT	__BIT(11)
    296   1.46.4.1  martin #define VMCB_CTRL_INTERCEPT_MWAIT_ARMED	__BIT(12)
    297        1.1    maxv #define VMCB_CTRL_INTERCEPT_XSETBV	__BIT(13)
    298   1.46.4.1  martin #define VMCB_CTRL_INTERCEPT_RDPRU	__BIT(14)
    299        1.1    maxv #define VMCB_CTRL_INTERCEPT_EFER_SPEC	__BIT(15)
    300        1.1    maxv #define VMCB_CTRL_INTERCEPT_WCR_SPEC(x)	__BIT(16 + x)
    301        1.1    maxv 
    302   1.46.4.1  martin 	uint32_t intercept_misc3;
    303   1.46.4.8  martin #define VMCB_CTRL_INTERCEPT_INVLPGB_ALL	__BIT(0)
    304   1.46.4.8  martin #define VMCB_CTRL_INTERCEPT_INVLPGB_ILL	__BIT(1)
    305   1.46.4.8  martin #define VMCB_CTRL_INTERCEPT_PCID	__BIT(2)
    306   1.46.4.1  martin #define VMCB_CTRL_INTERCEPT_MCOMMIT	__BIT(3)
    307   1.46.4.8  martin #define VMCB_CTRL_INTERCEPT_TLBSYNC	__BIT(4)
    308   1.46.4.1  martin 
    309   1.46.4.1  martin 	uint8_t  rsvd1[36];
    310        1.1    maxv 	uint16_t pause_filt_thresh;
    311        1.1    maxv 	uint16_t pause_filt_cnt;
    312        1.1    maxv 	uint64_t iopm_base_pa;
    313        1.1    maxv 	uint64_t msrpm_base_pa;
    314        1.1    maxv 	uint64_t tsc_offset;
    315        1.1    maxv 	uint32_t guest_asid;
    316        1.1    maxv 
    317        1.1    maxv 	uint32_t tlb_ctrl;
    318        1.1    maxv #define VMCB_CTRL_TLB_CTRL_FLUSH_ALL			0x01
    319        1.1    maxv #define VMCB_CTRL_TLB_CTRL_FLUSH_GUEST			0x03
    320        1.1    maxv #define VMCB_CTRL_TLB_CTRL_FLUSH_GUEST_NONGLOBAL	0x07
    321        1.1    maxv 
    322        1.1    maxv 	uint64_t v;
    323       1.34    maxv #define VMCB_CTRL_V_TPR			__BITS(3,0)
    324        1.1    maxv #define VMCB_CTRL_V_IRQ			__BIT(8)
    325        1.1    maxv #define VMCB_CTRL_V_VGIF		__BIT(9)
    326        1.1    maxv #define VMCB_CTRL_V_INTR_PRIO		__BITS(19,16)
    327        1.1    maxv #define VMCB_CTRL_V_IGN_TPR		__BIT(20)
    328        1.1    maxv #define VMCB_CTRL_V_INTR_MASKING	__BIT(24)
    329        1.1    maxv #define VMCB_CTRL_V_GUEST_VGIF		__BIT(25)
    330        1.1    maxv #define VMCB_CTRL_V_AVIC_EN		__BIT(31)
    331        1.1    maxv #define VMCB_CTRL_V_INTR_VECTOR		__BITS(39,32)
    332        1.1    maxv 
    333        1.1    maxv 	uint64_t intr;
    334        1.1    maxv #define VMCB_CTRL_INTR_SHADOW		__BIT(0)
    335   1.46.4.8  martin #define VMCB_CTRL_INTR_MASK		__BIT(1)
    336        1.1    maxv 
    337        1.1    maxv 	uint64_t exitcode;
    338        1.1    maxv 	uint64_t exitinfo1;
    339        1.1    maxv 	uint64_t exitinfo2;
    340        1.1    maxv 
    341        1.1    maxv 	uint64_t exitintinfo;
    342        1.1    maxv #define VMCB_CTRL_EXITINTINFO_VECTOR	__BITS(7,0)
    343        1.1    maxv #define VMCB_CTRL_EXITINTINFO_TYPE	__BITS(10,8)
    344        1.1    maxv #define VMCB_CTRL_EXITINTINFO_EV	__BIT(11)
    345        1.1    maxv #define VMCB_CTRL_EXITINTINFO_V		__BIT(31)
    346        1.1    maxv #define VMCB_CTRL_EXITINTINFO_ERRORCODE	__BITS(63,32)
    347        1.1    maxv 
    348        1.1    maxv 	uint64_t enable1;
    349        1.1    maxv #define VMCB_CTRL_ENABLE_NP		__BIT(0)
    350        1.1    maxv #define VMCB_CTRL_ENABLE_SEV		__BIT(1)
    351        1.1    maxv #define VMCB_CTRL_ENABLE_ES_SEV		__BIT(2)
    352   1.46.4.1  martin #define VMCB_CTRL_ENABLE_GMET		__BIT(3)
    353   1.46.4.1  martin #define VMCB_CTRL_ENABLE_VTE		__BIT(5)
    354        1.1    maxv 
    355        1.1    maxv 	uint64_t avic;
    356        1.1    maxv #define VMCB_CTRL_AVIC_APIC_BAR		__BITS(51,0)
    357        1.1    maxv 
    358        1.1    maxv 	uint64_t ghcb;
    359        1.1    maxv 
    360        1.1    maxv 	uint64_t eventinj;
    361        1.1    maxv #define VMCB_CTRL_EVENTINJ_VECTOR	__BITS(7,0)
    362        1.1    maxv #define VMCB_CTRL_EVENTINJ_TYPE		__BITS(10,8)
    363        1.1    maxv #define VMCB_CTRL_EVENTINJ_EV		__BIT(11)
    364        1.1    maxv #define VMCB_CTRL_EVENTINJ_V		__BIT(31)
    365        1.1    maxv #define VMCB_CTRL_EVENTINJ_ERRORCODE	__BITS(63,32)
    366        1.1    maxv 
    367        1.1    maxv 	uint64_t n_cr3;
    368        1.1    maxv 
    369        1.1    maxv 	uint64_t enable2;
    370        1.1    maxv #define VMCB_CTRL_ENABLE_LBR		__BIT(0)
    371        1.1    maxv #define VMCB_CTRL_ENABLE_VVMSAVE	__BIT(1)
    372        1.1    maxv 
    373        1.1    maxv 	uint32_t vmcb_clean;
    374        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_I		__BIT(0)
    375        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_IOPM	__BIT(1)
    376        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_ASID	__BIT(2)
    377        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_TPR	__BIT(3)
    378        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_NP		__BIT(4)
    379        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_CR		__BIT(5)
    380        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_DR		__BIT(6)
    381        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_DT		__BIT(7)
    382        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_SEG	__BIT(8)
    383        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_CR2	__BIT(9)
    384        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_LBR	__BIT(10)
    385        1.1    maxv #define VMCB_CTRL_VMCB_CLEAN_AVIC	__BIT(11)
    386        1.1    maxv 
    387        1.1    maxv 	uint32_t rsvd2;
    388        1.1    maxv 	uint64_t nrip;
    389        1.1    maxv 	uint8_t	inst_len;
    390        1.1    maxv 	uint8_t	inst_bytes[15];
    391       1.11    maxv 	uint64_t avic_abpp;
    392       1.11    maxv 	uint64_t rsvd3;
    393       1.11    maxv 	uint64_t avic_ltp;
    394       1.11    maxv 
    395       1.11    maxv 	uint64_t avic_phys;
    396       1.11    maxv #define VMCB_CTRL_AVIC_PHYS_TABLE_PTR	__BITS(51,12)
    397       1.11    maxv #define VMCB_CTRL_AVIC_PHYS_MAX_INDEX	__BITS(7,0)
    398       1.11    maxv 
    399       1.11    maxv 	uint64_t rsvd4;
    400   1.46.4.8  martin 	uint64_t vmsa_ptr;
    401       1.11    maxv 
    402       1.11    maxv 	uint8_t	pad[752];
    403        1.1    maxv } __packed;
    404        1.1    maxv 
    405        1.1    maxv CTASSERT(sizeof(struct vmcb_ctrl) == 1024);
    406        1.1    maxv 
    407        1.1    maxv struct vmcb_segment {
    408        1.1    maxv 	uint16_t selector;
    409        1.1    maxv 	uint16_t attrib;	/* hidden */
    410        1.1    maxv 	uint32_t limit;		/* hidden */
    411        1.1    maxv 	uint64_t base;		/* hidden */
    412        1.1    maxv } __packed;
    413        1.1    maxv 
    414        1.1    maxv CTASSERT(sizeof(struct vmcb_segment) == 16);
    415        1.1    maxv 
    416        1.1    maxv struct vmcb_state {
    417        1.1    maxv 	struct   vmcb_segment es;
    418        1.1    maxv 	struct   vmcb_segment cs;
    419        1.1    maxv 	struct   vmcb_segment ss;
    420        1.1    maxv 	struct   vmcb_segment ds;
    421        1.1    maxv 	struct   vmcb_segment fs;
    422        1.1    maxv 	struct   vmcb_segment gs;
    423        1.1    maxv 	struct   vmcb_segment gdt;
    424        1.1    maxv 	struct   vmcb_segment ldt;
    425        1.1    maxv 	struct   vmcb_segment idt;
    426        1.1    maxv 	struct   vmcb_segment tr;
    427        1.1    maxv 	uint8_t	 rsvd1[43];
    428        1.1    maxv 	uint8_t	 cpl;
    429        1.1    maxv 	uint8_t  rsvd2[4];
    430        1.1    maxv 	uint64_t efer;
    431        1.1    maxv 	uint8_t	 rsvd3[112];
    432        1.1    maxv 	uint64_t cr4;
    433        1.1    maxv 	uint64_t cr3;
    434        1.1    maxv 	uint64_t cr0;
    435        1.1    maxv 	uint64_t dr7;
    436        1.1    maxv 	uint64_t dr6;
    437        1.1    maxv 	uint64_t rflags;
    438        1.1    maxv 	uint64_t rip;
    439        1.1    maxv 	uint8_t	 rsvd4[88];
    440        1.1    maxv 	uint64_t rsp;
    441        1.1    maxv 	uint8_t	 rsvd5[24];
    442        1.1    maxv 	uint64_t rax;
    443        1.1    maxv 	uint64_t star;
    444        1.1    maxv 	uint64_t lstar;
    445        1.1    maxv 	uint64_t cstar;
    446        1.1    maxv 	uint64_t sfmask;
    447        1.1    maxv 	uint64_t kernelgsbase;
    448        1.1    maxv 	uint64_t sysenter_cs;
    449        1.1    maxv 	uint64_t sysenter_esp;
    450        1.1    maxv 	uint64_t sysenter_eip;
    451        1.1    maxv 	uint64_t cr2;
    452        1.1    maxv 	uint8_t	 rsvd6[32];
    453        1.1    maxv 	uint64_t g_pat;
    454        1.1    maxv 	uint64_t dbgctl;
    455        1.1    maxv 	uint64_t br_from;
    456        1.1    maxv 	uint64_t br_to;
    457        1.1    maxv 	uint64_t int_from;
    458        1.1    maxv 	uint64_t int_to;
    459        1.1    maxv 	uint8_t	 pad[2408];
    460        1.1    maxv } __packed;
    461        1.1    maxv 
    462        1.1    maxv CTASSERT(sizeof(struct vmcb_state) == 0xC00);
    463        1.1    maxv 
    464        1.1    maxv struct vmcb {
    465        1.1    maxv 	struct vmcb_ctrl ctrl;
    466        1.1    maxv 	struct vmcb_state state;
    467        1.1    maxv } __packed;
    468        1.1    maxv 
    469        1.1    maxv CTASSERT(sizeof(struct vmcb) == PAGE_SIZE);
    470        1.1    maxv CTASSERT(offsetof(struct vmcb, state) == 0x400);
    471        1.1    maxv 
    472        1.1    maxv /* -------------------------------------------------------------------------- */
    473        1.1    maxv 
    474       1.43    maxv static void svm_vcpu_state_provide(struct nvmm_cpu *, uint64_t);
    475       1.43    maxv static void svm_vcpu_state_commit(struct nvmm_cpu *);
    476       1.43    maxv 
    477        1.1    maxv struct svm_hsave {
    478        1.1    maxv 	paddr_t pa;
    479        1.1    maxv };
    480        1.1    maxv 
    481        1.1    maxv static struct svm_hsave hsave[MAXCPUS];
    482        1.1    maxv 
    483        1.1    maxv static uint8_t *svm_asidmap __read_mostly;
    484        1.1    maxv static uint32_t svm_maxasid __read_mostly;
    485        1.1    maxv static kmutex_t svm_asidlock __cacheline_aligned;
    486        1.1    maxv 
    487        1.1    maxv static bool svm_decode_assist __read_mostly;
    488        1.1    maxv static uint32_t svm_ctrl_tlb_flush __read_mostly;
    489        1.1    maxv 
    490        1.1    maxv #define SVM_XCR0_MASK_DEFAULT	(XCR0_X87|XCR0_SSE)
    491        1.1    maxv static uint64_t svm_xcr0_mask __read_mostly;
    492        1.1    maxv 
    493        1.1    maxv #define SVM_NCPUIDS	32
    494        1.1    maxv 
    495        1.1    maxv #define VMCB_NPAGES	1
    496        1.1    maxv 
    497        1.1    maxv #define MSRBM_NPAGES	2
    498        1.1    maxv #define MSRBM_SIZE	(MSRBM_NPAGES * PAGE_SIZE)
    499        1.1    maxv 
    500        1.1    maxv #define IOBM_NPAGES	3
    501        1.1    maxv #define IOBM_SIZE	(IOBM_NPAGES * PAGE_SIZE)
    502        1.1    maxv 
    503        1.1    maxv /* Does not include EFER_LMSLE. */
    504        1.1    maxv #define EFER_VALID \
    505        1.1    maxv 	(EFER_SCE|EFER_LME|EFER_LMA|EFER_NXE|EFER_SVME|EFER_FFXSR|EFER_TCE)
    506        1.1    maxv 
    507        1.1    maxv #define EFER_TLB_FLUSH \
    508        1.1    maxv 	(EFER_NXE|EFER_LMA|EFER_LME)
    509        1.1    maxv #define CR0_TLB_FLUSH \
    510        1.1    maxv 	(CR0_PG|CR0_WP|CR0_CD|CR0_NW)
    511        1.1    maxv #define CR4_TLB_FLUSH \
    512  1.46.4.10  martin 	(CR4_PSE|CR4_PAE|CR4_PGE|CR4_PCIDE|CR4_SMEP)
    513        1.1    maxv 
    514        1.1    maxv /* -------------------------------------------------------------------------- */
    515        1.1    maxv 
    516        1.1    maxv struct svm_machdata {
    517       1.29    maxv 	volatile uint64_t mach_htlb_gen;
    518        1.1    maxv };
    519        1.1    maxv 
    520   1.46.4.2  martin static const size_t svm_vcpu_conf_sizes[NVMM_X86_VCPU_NCONF] = {
    521   1.46.4.2  martin 	[NVMM_VCPU_CONF_MD(NVMM_VCPU_CONF_CPUID)] =
    522   1.46.4.2  martin 	    sizeof(struct nvmm_vcpu_conf_cpuid),
    523   1.46.4.2  martin 	[NVMM_VCPU_CONF_MD(NVMM_VCPU_CONF_TPR)] =
    524   1.46.4.2  martin 	    sizeof(struct nvmm_vcpu_conf_tpr)
    525        1.1    maxv };
    526        1.1    maxv 
    527        1.1    maxv struct svm_cpudata {
    528        1.1    maxv 	/* General */
    529        1.1    maxv 	bool shared_asid;
    530       1.28    maxv 	bool gtlb_want_flush;
    531       1.36    maxv 	bool gtsc_want_update;
    532       1.29    maxv 	uint64_t vcpu_htlb_gen;
    533        1.1    maxv 
    534        1.1    maxv 	/* VMCB */
    535        1.1    maxv 	struct vmcb *vmcb;
    536        1.1    maxv 	paddr_t vmcb_pa;
    537        1.1    maxv 
    538        1.1    maxv 	/* I/O bitmap */
    539        1.1    maxv 	uint8_t *iobm;
    540        1.1    maxv 	paddr_t iobm_pa;
    541        1.1    maxv 
    542        1.1    maxv 	/* MSR bitmap */
    543        1.1    maxv 	uint8_t *msrbm;
    544        1.1    maxv 	paddr_t msrbm_pa;
    545        1.1    maxv 
    546        1.1    maxv 	/* Host state */
    547       1.13    maxv 	uint64_t hxcr0;
    548        1.1    maxv 	uint64_t star;
    549        1.1    maxv 	uint64_t lstar;
    550        1.1    maxv 	uint64_t cstar;
    551        1.1    maxv 	uint64_t sfmask;
    552       1.14    maxv 	uint64_t fsbase;
    553       1.14    maxv 	uint64_t kernelgsbase;
    554        1.1    maxv 	bool ts_set;
    555       1.16    maxv 	struct xsave_header hfpu __aligned(64);
    556        1.1    maxv 
    557       1.37    maxv 	/* Intr state */
    558       1.10    maxv 	bool int_window_exit;
    559       1.10    maxv 	bool nmi_window_exit;
    560       1.37    maxv 	bool evt_pending;
    561       1.10    maxv 
    562        1.1    maxv 	/* Guest state */
    563       1.13    maxv 	uint64_t gxcr0;
    564       1.13    maxv 	uint64_t gprs[NVMM_X64_NGPR];
    565       1.13    maxv 	uint64_t drs[NVMM_X64_NDR];
    566       1.36    maxv 	uint64_t gtsc;
    567       1.16    maxv 	struct xsave_header gfpu __aligned(64);
    568   1.46.4.2  martin 
    569   1.46.4.2  martin 	/* VCPU configuration. */
    570   1.46.4.2  martin 	bool cpuidpresent[SVM_NCPUIDS];
    571   1.46.4.2  martin 	struct nvmm_vcpu_conf_cpuid cpuid[SVM_NCPUIDS];
    572        1.1    maxv };
    573        1.1    maxv 
    574       1.12    maxv static void
    575       1.12    maxv svm_vmcb_cache_default(struct vmcb *vmcb)
    576       1.12    maxv {
    577       1.12    maxv 	vmcb->ctrl.vmcb_clean =
    578       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_I |
    579       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_IOPM |
    580       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_ASID |
    581       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_TPR |
    582       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_NP |
    583       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_CR |
    584       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_DR |
    585       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_DT |
    586       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_SEG |
    587       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_CR2 |
    588       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_LBR |
    589       1.12    maxv 	    VMCB_CTRL_VMCB_CLEAN_AVIC;
    590       1.12    maxv }
    591       1.12    maxv 
    592       1.12    maxv static void
    593       1.12    maxv svm_vmcb_cache_update(struct vmcb *vmcb, uint64_t flags)
    594       1.12    maxv {
    595       1.12    maxv 	if (flags & NVMM_X64_STATE_SEGS) {
    596       1.12    maxv 		vmcb->ctrl.vmcb_clean &=
    597       1.12    maxv 		    ~(VMCB_CTRL_VMCB_CLEAN_SEG | VMCB_CTRL_VMCB_CLEAN_DT);
    598       1.12    maxv 	}
    599       1.12    maxv 	if (flags & NVMM_X64_STATE_CRS) {
    600       1.12    maxv 		vmcb->ctrl.vmcb_clean &=
    601       1.13    maxv 		    ~(VMCB_CTRL_VMCB_CLEAN_CR | VMCB_CTRL_VMCB_CLEAN_CR2 |
    602       1.13    maxv 		      VMCB_CTRL_VMCB_CLEAN_TPR);
    603       1.12    maxv 	}
    604       1.12    maxv 	if (flags & NVMM_X64_STATE_DRS) {
    605       1.12    maxv 		vmcb->ctrl.vmcb_clean &= ~VMCB_CTRL_VMCB_CLEAN_DR;
    606       1.12    maxv 	}
    607       1.12    maxv 	if (flags & NVMM_X64_STATE_MSRS) {
    608       1.12    maxv 		/* CR for EFER, NP for PAT. */
    609       1.12    maxv 		vmcb->ctrl.vmcb_clean &=
    610       1.12    maxv 		    ~(VMCB_CTRL_VMCB_CLEAN_CR | VMCB_CTRL_VMCB_CLEAN_NP);
    611       1.12    maxv 	}
    612       1.12    maxv }
    613       1.12    maxv 
    614       1.12    maxv static inline void
    615       1.12    maxv svm_vmcb_cache_flush(struct vmcb *vmcb, uint64_t flags)
    616       1.12    maxv {
    617       1.12    maxv 	vmcb->ctrl.vmcb_clean &= ~flags;
    618       1.12    maxv }
    619       1.12    maxv 
    620       1.12    maxv static inline void
    621       1.12    maxv svm_vmcb_cache_flush_all(struct vmcb *vmcb)
    622       1.12    maxv {
    623       1.12    maxv 	vmcb->ctrl.vmcb_clean = 0;
    624       1.12    maxv }
    625       1.12    maxv 
    626        1.1    maxv #define SVM_EVENT_TYPE_HW_INT	0
    627        1.1    maxv #define SVM_EVENT_TYPE_NMI	2
    628        1.1    maxv #define SVM_EVENT_TYPE_EXC	3
    629        1.1    maxv #define SVM_EVENT_TYPE_SW_INT	4
    630        1.1    maxv 
    631        1.1    maxv static void
    632       1.10    maxv svm_event_waitexit_enable(struct nvmm_cpu *vcpu, bool nmi)
    633        1.1    maxv {
    634       1.10    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
    635       1.10    maxv 	struct vmcb *vmcb = cpudata->vmcb;
    636       1.10    maxv 
    637        1.1    maxv 	if (nmi) {
    638        1.1    maxv 		vmcb->ctrl.intercept_misc1 |= VMCB_CTRL_INTERCEPT_IRET;
    639       1.10    maxv 		cpudata->nmi_window_exit = true;
    640        1.1    maxv 	} else {
    641        1.1    maxv 		vmcb->ctrl.intercept_misc1 |= VMCB_CTRL_INTERCEPT_VINTR;
    642       1.10    maxv 		vmcb->ctrl.v |= (VMCB_CTRL_V_IRQ | VMCB_CTRL_V_IGN_TPR);
    643       1.12    maxv 		svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_TPR);
    644       1.10    maxv 		cpudata->int_window_exit = true;
    645        1.1    maxv 	}
    646       1.12    maxv 
    647       1.12    maxv 	svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_I);
    648        1.1    maxv }
    649        1.1    maxv 
    650        1.1    maxv static void
    651       1.10    maxv svm_event_waitexit_disable(struct nvmm_cpu *vcpu, bool nmi)
    652        1.1    maxv {
    653       1.10    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
    654       1.10    maxv 	struct vmcb *vmcb = cpudata->vmcb;
    655       1.10    maxv 
    656        1.1    maxv 	if (nmi) {
    657        1.1    maxv 		vmcb->ctrl.intercept_misc1 &= ~VMCB_CTRL_INTERCEPT_IRET;
    658       1.10    maxv 		cpudata->nmi_window_exit = false;
    659        1.1    maxv 	} else {
    660        1.1    maxv 		vmcb->ctrl.intercept_misc1 &= ~VMCB_CTRL_INTERCEPT_VINTR;
    661       1.10    maxv 		vmcb->ctrl.v &= ~(VMCB_CTRL_V_IRQ | VMCB_CTRL_V_IGN_TPR);
    662       1.12    maxv 		svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_TPR);
    663       1.10    maxv 		cpudata->int_window_exit = false;
    664        1.1    maxv 	}
    665       1.12    maxv 
    666       1.12    maxv 	svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_I);
    667        1.1    maxv }
    668        1.1    maxv 
    669  1.46.4.13  martin static inline bool
    670  1.46.4.13  martin svm_excp_has_rf(uint8_t vector)
    671  1.46.4.13  martin {
    672  1.46.4.13  martin 	switch (vector) {
    673  1.46.4.13  martin 	case 1:		/* #DB */
    674  1.46.4.13  martin 	case 4:		/* #OF */
    675  1.46.4.13  martin 	case 8:		/* #DF */
    676  1.46.4.13  martin 	case 18:	/* #MC */
    677  1.46.4.13  martin 		return false;
    678  1.46.4.13  martin 	default:
    679  1.46.4.13  martin 		return true;
    680  1.46.4.13  martin 	}
    681  1.46.4.13  martin }
    682  1.46.4.13  martin 
    683        1.1    maxv static inline int
    684  1.46.4.13  martin svm_excp_has_error(uint8_t vector)
    685        1.1    maxv {
    686        1.1    maxv 	switch (vector) {
    687        1.1    maxv 	case 8:		/* #DF */
    688        1.1    maxv 	case 10:	/* #TS */
    689        1.1    maxv 	case 11:	/* #NP */
    690        1.1    maxv 	case 12:	/* #SS */
    691        1.1    maxv 	case 13:	/* #GP */
    692        1.1    maxv 	case 14:	/* #PF */
    693        1.1    maxv 	case 17:	/* #AC */
    694        1.1    maxv 	case 30:	/* #SX */
    695        1.1    maxv 		return 1;
    696        1.1    maxv 	default:
    697        1.1    maxv 		return 0;
    698        1.1    maxv 	}
    699        1.1    maxv }
    700        1.1    maxv 
    701        1.1    maxv static int
    702       1.45    maxv svm_vcpu_inject(struct nvmm_cpu *vcpu)
    703        1.1    maxv {
    704       1.45    maxv 	struct nvmm_comm_page *comm = vcpu->comm;
    705        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
    706        1.1    maxv 	struct vmcb *vmcb = cpudata->vmcb;
    707   1.46.4.2  martin 	u_int evtype;
    708   1.46.4.2  martin 	uint8_t vector;
    709   1.46.4.2  martin 	uint64_t error;
    710        1.1    maxv 	int type = 0, err = 0;
    711        1.1    maxv 
    712       1.45    maxv 	evtype = comm->event.type;
    713       1.45    maxv 	vector = comm->event.vector;
    714   1.46.4.2  martin 	error = comm->event.u.excp.error;
    715       1.45    maxv 	__insn_barrier();
    716       1.45    maxv 
    717       1.45    maxv 	switch (evtype) {
    718   1.46.4.2  martin 	case NVMM_VCPU_EVENT_EXCP:
    719        1.1    maxv 		type = SVM_EVENT_TYPE_EXC;
    720       1.45    maxv 		if (vector == 2 || vector >= 32)
    721        1.1    maxv 			return EINVAL;
    722       1.45    maxv 		if (vector == 3 || vector == 0)
    723       1.22    maxv 			return EINVAL;
    724  1.46.4.13  martin 		if (svm_excp_has_rf(vector)) {
    725  1.46.4.13  martin 			vmcb->state.rflags |= PSL_RF;
    726  1.46.4.13  martin 		}
    727  1.46.4.13  martin 		err = svm_excp_has_error(vector);
    728        1.1    maxv 		break;
    729   1.46.4.2  martin 	case NVMM_VCPU_EVENT_INTR:
    730   1.46.4.2  martin 		type = SVM_EVENT_TYPE_HW_INT;
    731   1.46.4.2  martin 		if (vector == 2) {
    732   1.46.4.2  martin 			type = SVM_EVENT_TYPE_NMI;
    733   1.46.4.2  martin 			svm_event_waitexit_enable(vcpu, true);
    734   1.46.4.2  martin 		}
    735   1.46.4.2  martin 		err = 0;
    736   1.46.4.2  martin 		break;
    737        1.1    maxv 	default:
    738        1.1    maxv 		return EINVAL;
    739        1.1    maxv 	}
    740        1.1    maxv 
    741        1.1    maxv 	vmcb->ctrl.eventinj =
    742   1.46.4.2  martin 	    __SHIFTIN((uint64_t)vector, VMCB_CTRL_EVENTINJ_VECTOR) |
    743   1.46.4.2  martin 	    __SHIFTIN((uint64_t)type, VMCB_CTRL_EVENTINJ_TYPE) |
    744   1.46.4.2  martin 	    __SHIFTIN((uint64_t)err, VMCB_CTRL_EVENTINJ_EV) |
    745   1.46.4.2  martin 	    __SHIFTIN((uint64_t)1, VMCB_CTRL_EVENTINJ_V) |
    746   1.46.4.2  martin 	    __SHIFTIN((uint64_t)error, VMCB_CTRL_EVENTINJ_ERRORCODE);
    747        1.1    maxv 
    748       1.37    maxv 	cpudata->evt_pending = true;
    749       1.37    maxv 
    750        1.1    maxv 	return 0;
    751        1.1    maxv }
    752        1.1    maxv 
    753        1.1    maxv static void
    754       1.45    maxv svm_inject_ud(struct nvmm_cpu *vcpu)
    755        1.1    maxv {
    756       1.45    maxv 	struct nvmm_comm_page *comm = vcpu->comm;
    757        1.1    maxv 	int ret __diagused;
    758        1.1    maxv 
    759   1.46.4.2  martin 	comm->event.type = NVMM_VCPU_EVENT_EXCP;
    760       1.45    maxv 	comm->event.vector = 6;
    761   1.46.4.2  martin 	comm->event.u.excp.error = 0;
    762        1.1    maxv 
    763       1.45    maxv 	ret = svm_vcpu_inject(vcpu);
    764        1.1    maxv 	KASSERT(ret == 0);
    765        1.1    maxv }
    766        1.1    maxv 
    767        1.1    maxv static void
    768       1.45    maxv svm_inject_gp(struct nvmm_cpu *vcpu)
    769        1.1    maxv {
    770       1.45    maxv 	struct nvmm_comm_page *comm = vcpu->comm;
    771        1.1    maxv 	int ret __diagused;
    772        1.1    maxv 
    773   1.46.4.2  martin 	comm->event.type = NVMM_VCPU_EVENT_EXCP;
    774       1.45    maxv 	comm->event.vector = 13;
    775   1.46.4.2  martin 	comm->event.u.excp.error = 0;
    776        1.1    maxv 
    777       1.45    maxv 	ret = svm_vcpu_inject(vcpu);
    778        1.1    maxv 	KASSERT(ret == 0);
    779        1.1    maxv }
    780        1.1    maxv 
    781       1.45    maxv static inline int
    782       1.45    maxv svm_vcpu_event_commit(struct nvmm_cpu *vcpu)
    783       1.45    maxv {
    784       1.45    maxv 	if (__predict_true(!vcpu->comm->event_commit)) {
    785       1.45    maxv 		return 0;
    786       1.45    maxv 	}
    787       1.45    maxv 	vcpu->comm->event_commit = false;
    788       1.45    maxv 	return svm_vcpu_inject(vcpu);
    789       1.45    maxv }
    790       1.45    maxv 
    791       1.17    maxv static inline void
    792       1.17    maxv svm_inkernel_advance(struct vmcb *vmcb)
    793        1.1    maxv {
    794       1.17    maxv 	/*
    795       1.17    maxv 	 * Maybe we should also apply single-stepping and debug exceptions.
    796       1.17    maxv 	 * Matters for guest-ring3, because it can execute 'cpuid' under a
    797       1.17    maxv 	 * debugger.
    798       1.17    maxv 	 */
    799       1.17    maxv 	vmcb->state.rip = vmcb->ctrl.nrip;
    800  1.46.4.13  martin 	vmcb->state.rflags &= ~PSL_RF;
    801       1.17    maxv 	vmcb->ctrl.intr &= ~VMCB_CTRL_INTR_SHADOW;
    802        1.1    maxv }
    803        1.1    maxv 
    804   1.46.4.9  martin #define SVM_CPUID_MAX_BASIC		0xD
    805   1.46.4.5  martin #define SVM_CPUID_MAX_HYPERVISOR	0x40000000
    806   1.46.4.9  martin #define SVM_CPUID_MAX_EXTENDED		0x8000001F
    807   1.46.4.9  martin static uint32_t svm_cpuid_max_basic __read_mostly;
    808   1.46.4.9  martin static uint32_t svm_cpuid_max_extended __read_mostly;
    809   1.46.4.9  martin 
    810   1.46.4.9  martin static void
    811   1.46.4.9  martin svm_inkernel_exec_cpuid(struct svm_cpudata *cpudata, uint64_t eax, uint64_t ecx)
    812   1.46.4.9  martin {
    813   1.46.4.9  martin 	u_int descs[4];
    814   1.46.4.9  martin 
    815   1.46.4.9  martin 	x86_cpuid2(eax, ecx, descs);
    816   1.46.4.9  martin 	cpudata->vmcb->state.rax = descs[0];
    817   1.46.4.9  martin 	cpudata->gprs[NVMM_X64_GPR_RBX] = descs[1];
    818   1.46.4.9  martin 	cpudata->gprs[NVMM_X64_GPR_RCX] = descs[2];
    819   1.46.4.9  martin 	cpudata->gprs[NVMM_X64_GPR_RDX] = descs[3];
    820   1.46.4.9  martin }
    821   1.46.4.5  martin 
    822        1.1    maxv static void
    823        1.1    maxv svm_inkernel_handle_cpuid(struct nvmm_cpu *vcpu, uint64_t eax, uint64_t ecx)
    824        1.1    maxv {
    825        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
    826       1.25    maxv 	uint64_t cr4;
    827        1.1    maxv 
    828   1.46.4.9  martin 	if (eax < 0x40000000) {
    829   1.46.4.9  martin 		if (__predict_false(eax > svm_cpuid_max_basic)) {
    830   1.46.4.9  martin 			eax = svm_cpuid_max_basic;
    831   1.46.4.9  martin 			svm_inkernel_exec_cpuid(cpudata, eax, ecx);
    832   1.46.4.9  martin 		}
    833   1.46.4.9  martin 	} else if (eax < 0x80000000) {
    834   1.46.4.9  martin 		if (__predict_false(eax > SVM_CPUID_MAX_HYPERVISOR)) {
    835   1.46.4.9  martin 			eax = svm_cpuid_max_basic;
    836   1.46.4.9  martin 			svm_inkernel_exec_cpuid(cpudata, eax, ecx);
    837   1.46.4.9  martin 		}
    838   1.46.4.9  martin 	} else {
    839   1.46.4.9  martin 		if (__predict_false(eax > svm_cpuid_max_extended)) {
    840   1.46.4.9  martin 			eax = svm_cpuid_max_basic;
    841   1.46.4.9  martin 			svm_inkernel_exec_cpuid(cpudata, eax, ecx);
    842   1.46.4.9  martin 		}
    843   1.46.4.9  martin 	}
    844   1.46.4.9  martin 
    845        1.1    maxv 	switch (eax) {
    846   1.46.4.9  martin 	case 0x00000000:
    847   1.46.4.9  martin 		cpudata->vmcb->state.rax = svm_cpuid_max_basic;
    848   1.46.4.9  martin 		break;
    849       1.25    maxv 	case 0x00000001:
    850       1.33    maxv 		cpudata->vmcb->state.rax &= nvmm_cpuid_00000001.eax;
    851       1.33    maxv 
    852       1.13    maxv 		cpudata->gprs[NVMM_X64_GPR_RBX] &= ~CPUID_LOCAL_APIC_ID;
    853       1.13    maxv 		cpudata->gprs[NVMM_X64_GPR_RBX] |= __SHIFTIN(vcpu->cpuid,
    854        1.1    maxv 		    CPUID_LOCAL_APIC_ID);
    855       1.25    maxv 
    856       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RCX] &= nvmm_cpuid_00000001.ecx;
    857       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RCX] |= CPUID2_RAZ;
    858       1.33    maxv 
    859       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RDX] &= nvmm_cpuid_00000001.edx;
    860       1.33    maxv 
    861       1.25    maxv 		/* CPUID2_OSXSAVE depends on CR4. */
    862       1.25    maxv 		cr4 = cpudata->vmcb->state.cr4;
    863       1.25    maxv 		if (!(cr4 & CR4_OSXSAVE)) {
    864       1.25    maxv 			cpudata->gprs[NVMM_X64_GPR_RCX] &= ~CPUID2_OSXSAVE;
    865       1.25    maxv 		}
    866        1.1    maxv 		break;
    867   1.46.4.5  martin 	case 0x00000002: /* Empty */
    868   1.46.4.5  martin 	case 0x00000003: /* Empty */
    869   1.46.4.5  martin 	case 0x00000004: /* Empty */
    870   1.46.4.5  martin 	case 0x00000005: /* Monitor/MWait */
    871   1.46.4.5  martin 	case 0x00000006: /* Power Management Related Features */
    872       1.33    maxv 		cpudata->vmcb->state.rax = 0;
    873       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    874       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    875       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    876       1.33    maxv 		break;
    877   1.46.4.5  martin 	case 0x00000007: /* Structured Extended Features */
    878   1.46.4.9  martin 		switch (ecx) {
    879   1.46.4.9  martin 		case 0:
    880   1.46.4.9  martin 			cpudata->vmcb->state.rax = 0;
    881   1.46.4.9  martin 			cpudata->gprs[NVMM_X64_GPR_RBX] &= nvmm_cpuid_00000007.ebx;
    882   1.46.4.9  martin 			cpudata->gprs[NVMM_X64_GPR_RCX] &= nvmm_cpuid_00000007.ecx;
    883   1.46.4.9  martin 			cpudata->gprs[NVMM_X64_GPR_RDX] &= nvmm_cpuid_00000007.edx;
    884   1.46.4.9  martin 			break;
    885   1.46.4.9  martin 		default:
    886   1.46.4.9  martin 			cpudata->vmcb->state.rax = 0;
    887   1.46.4.9  martin 			cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    888   1.46.4.9  martin 			cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    889   1.46.4.9  martin 			cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    890   1.46.4.9  martin 			break;
    891   1.46.4.9  martin 		}
    892       1.33    maxv 		break;
    893   1.46.4.5  martin 	case 0x00000008: /* Empty */
    894   1.46.4.5  martin 	case 0x00000009: /* Empty */
    895   1.46.4.5  martin 	case 0x0000000A: /* Empty */
    896   1.46.4.5  martin 	case 0x0000000B: /* Empty */
    897   1.46.4.5  martin 	case 0x0000000C: /* Empty */
    898   1.46.4.5  martin 		cpudata->vmcb->state.rax = 0;
    899   1.46.4.5  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    900   1.46.4.5  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    901   1.46.4.5  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    902   1.46.4.5  martin 		break;
    903   1.46.4.5  martin 	case 0x0000000D: /* Processor Extended State Enumeration */
    904       1.25    maxv 		if (svm_xcr0_mask == 0) {
    905        1.1    maxv 			break;
    906        1.1    maxv 		}
    907       1.25    maxv 		switch (ecx) {
    908       1.25    maxv 		case 0:
    909       1.26    maxv 			cpudata->vmcb->state.rax = svm_xcr0_mask & 0xFFFFFFFF;
    910       1.25    maxv 			if (cpudata->gxcr0 & XCR0_SSE) {
    911       1.25    maxv 				cpudata->gprs[NVMM_X64_GPR_RBX] = sizeof(struct fxsave);
    912       1.25    maxv 			} else {
    913       1.25    maxv 				cpudata->gprs[NVMM_X64_GPR_RBX] = sizeof(struct save87);
    914       1.25    maxv 			}
    915       1.25    maxv 			cpudata->gprs[NVMM_X64_GPR_RBX] += 64; /* XSAVE header */
    916       1.39    maxv 			cpudata->gprs[NVMM_X64_GPR_RCX] = sizeof(struct fxsave) + 64;
    917       1.25    maxv 			cpudata->gprs[NVMM_X64_GPR_RDX] = svm_xcr0_mask >> 32;
    918       1.25    maxv 			break;
    919       1.25    maxv 		case 1:
    920   1.46.4.3  martin 			cpudata->vmcb->state.rax &=
    921   1.46.4.3  martin 			    (CPUID_PES1_XSAVEOPT | CPUID_PES1_XSAVEC |
    922   1.46.4.3  martin 			     CPUID_PES1_XGETBV);
    923   1.46.4.3  martin 			cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    924   1.46.4.3  martin 			cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    925   1.46.4.3  martin 			cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    926   1.46.4.3  martin 			break;
    927   1.46.4.3  martin 		default:
    928   1.46.4.3  martin 			cpudata->vmcb->state.rax = 0;
    929   1.46.4.3  martin 			cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    930   1.46.4.3  martin 			cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    931   1.46.4.3  martin 			cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    932       1.25    maxv 			break;
    933        1.1    maxv 		}
    934        1.1    maxv 		break;
    935   1.46.4.5  martin 
    936   1.46.4.5  martin 	case 0x40000000: /* Hypervisor Information */
    937   1.46.4.5  martin 		cpudata->vmcb->state.rax = SVM_CPUID_MAX_HYPERVISOR;
    938       1.16    maxv 		cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    939       1.16    maxv 		cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    940       1.16    maxv 		cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    941       1.13    maxv 		memcpy(&cpudata->gprs[NVMM_X64_GPR_RBX], "___ ", 4);
    942       1.13    maxv 		memcpy(&cpudata->gprs[NVMM_X64_GPR_RCX], "NVMM", 4);
    943       1.13    maxv 		memcpy(&cpudata->gprs[NVMM_X64_GPR_RDX], " ___", 4);
    944       1.10    maxv 		break;
    945   1.46.4.5  martin 
    946   1.46.4.9  martin 	case 0x80000000:
    947   1.46.4.9  martin 		cpudata->vmcb->state.rax = svm_cpuid_max_extended;
    948   1.46.4.9  martin 		break;
    949       1.25    maxv 	case 0x80000001:
    950       1.33    maxv 		cpudata->vmcb->state.rax &= nvmm_cpuid_80000001.eax;
    951       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RBX] &= nvmm_cpuid_80000001.ebx;
    952       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RCX] &= nvmm_cpuid_80000001.ecx;
    953       1.33    maxv 		cpudata->gprs[NVMM_X64_GPR_RDX] &= nvmm_cpuid_80000001.edx;
    954       1.10    maxv 		break;
    955   1.46.4.9  martin 	case 0x80000002: /* Extended Processor Name String */
    956   1.46.4.9  martin 	case 0x80000003: /* Extended Processor Name String */
    957   1.46.4.9  martin 	case 0x80000004: /* Extended Processor Name String */
    958   1.46.4.9  martin 	case 0x80000005: /* L1 Cache and TLB Information */
    959   1.46.4.9  martin 	case 0x80000006: /* L2 Cache and TLB and L3 Cache Information */
    960   1.46.4.9  martin 		break;
    961   1.46.4.9  martin 	case 0x80000007: /* Processor Power Management and RAS Capabilities */
    962   1.46.4.9  martin 		cpudata->vmcb->state.rax &= nvmm_cpuid_80000007.eax;
    963   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] &= nvmm_cpuid_80000007.ebx;
    964   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] &= nvmm_cpuid_80000007.ecx;
    965   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] &= nvmm_cpuid_80000007.edx;
    966   1.46.4.9  martin 		break;
    967   1.46.4.9  martin 	case 0x80000008: /* Processor Capacity Parameters and Ext Feat Ident */
    968   1.46.4.9  martin 		cpudata->vmcb->state.rax &= nvmm_cpuid_80000008.eax;
    969   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] &= nvmm_cpuid_80000008.ebx;
    970   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] &= nvmm_cpuid_80000008.ecx;
    971   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] &= nvmm_cpuid_80000008.edx;
    972   1.46.4.9  martin 		break;
    973   1.46.4.9  martin 	case 0x80000009: /* Empty */
    974   1.46.4.9  martin 	case 0x8000000A: /* SVM Features */
    975   1.46.4.9  martin 	case 0x8000000B: /* Empty */
    976   1.46.4.9  martin 	case 0x8000000C: /* Empty */
    977   1.46.4.9  martin 	case 0x8000000D: /* Empty */
    978   1.46.4.9  martin 	case 0x8000000E: /* Empty */
    979   1.46.4.9  martin 	case 0x8000000F: /* Empty */
    980   1.46.4.9  martin 	case 0x80000010: /* Empty */
    981   1.46.4.9  martin 	case 0x80000011: /* Empty */
    982   1.46.4.9  martin 	case 0x80000012: /* Empty */
    983   1.46.4.9  martin 	case 0x80000013: /* Empty */
    984   1.46.4.9  martin 	case 0x80000014: /* Empty */
    985   1.46.4.9  martin 	case 0x80000015: /* Empty */
    986   1.46.4.9  martin 	case 0x80000016: /* Empty */
    987   1.46.4.9  martin 	case 0x80000017: /* Empty */
    988   1.46.4.9  martin 	case 0x80000018: /* Empty */
    989   1.46.4.9  martin 		cpudata->vmcb->state.rax = 0;
    990   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
    991   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
    992   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
    993   1.46.4.9  martin 		break;
    994   1.46.4.9  martin 	case 0x80000019: /* TLB Characteristics for 1GB pages */
    995   1.46.4.9  martin 	case 0x8000001A: /* Instruction Optimizations */
    996   1.46.4.9  martin 		break;
    997   1.46.4.9  martin 	case 0x8000001B: /* Instruction-Based Sampling Capabilities */
    998   1.46.4.9  martin 	case 0x8000001C: /* Lightweight Profiling Capabilities */
    999   1.46.4.9  martin 		cpudata->vmcb->state.rax = 0;
   1000   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
   1001   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
   1002   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
   1003   1.46.4.9  martin 		break;
   1004   1.46.4.9  martin 	case 0x8000001D: /* Cache Topology Information */
   1005   1.46.4.9  martin 	case 0x8000001E: /* Processor Topology Information */
   1006   1.46.4.9  martin 		break; /* TODO? */
   1007   1.46.4.9  martin 	case 0x8000001F: /* Encrypted Memory Capabilities */
   1008   1.46.4.9  martin 		cpudata->vmcb->state.rax = 0;
   1009   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] = 0;
   1010   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] = 0;
   1011   1.46.4.9  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] = 0;
   1012   1.46.4.9  martin 		break;
   1013   1.46.4.9  martin 
   1014        1.1    maxv 	default:
   1015        1.1    maxv 		break;
   1016        1.1    maxv 	}
   1017        1.1    maxv }
   1018        1.1    maxv 
   1019        1.1    maxv static void
   1020   1.46.4.2  martin svm_exit_insn(struct vmcb *vmcb, struct nvmm_vcpu_exit *exit, uint64_t reason)
   1021   1.46.4.2  martin {
   1022   1.46.4.2  martin 	exit->u.insn.npc = vmcb->ctrl.nrip;
   1023   1.46.4.2  martin 	exit->reason = reason;
   1024   1.46.4.2  martin }
   1025   1.46.4.2  martin 
   1026   1.46.4.2  martin static void
   1027        1.1    maxv svm_exit_cpuid(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1028   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1029        1.1    maxv {
   1030        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1031   1.46.4.2  martin 	struct nvmm_vcpu_conf_cpuid *cpuid;
   1032        1.1    maxv 	uint64_t eax, ecx;
   1033        1.1    maxv 	size_t i;
   1034        1.1    maxv 
   1035        1.1    maxv 	eax = cpudata->vmcb->state.rax;
   1036       1.13    maxv 	ecx = cpudata->gprs[NVMM_X64_GPR_RCX];
   1037  1.46.4.10  martin 	svm_inkernel_exec_cpuid(cpudata, eax, ecx);
   1038       1.38    maxv 	svm_inkernel_handle_cpuid(vcpu, eax, ecx);
   1039       1.38    maxv 
   1040        1.1    maxv 	for (i = 0; i < SVM_NCPUIDS; i++) {
   1041   1.46.4.2  martin 		if (!cpudata->cpuidpresent[i]) {
   1042        1.1    maxv 			continue;
   1043        1.1    maxv 		}
   1044   1.46.4.2  martin 		cpuid = &cpudata->cpuid[i];
   1045        1.1    maxv 		if (cpuid->leaf != eax) {
   1046        1.1    maxv 			continue;
   1047        1.1    maxv 		}
   1048        1.1    maxv 
   1049   1.46.4.2  martin 		if (cpuid->exit) {
   1050   1.46.4.2  martin 			svm_exit_insn(cpudata->vmcb, exit, NVMM_VCPU_EXIT_CPUID);
   1051   1.46.4.2  martin 			return;
   1052   1.46.4.2  martin 		}
   1053   1.46.4.2  martin 		KASSERT(cpuid->mask);
   1054   1.46.4.2  martin 
   1055        1.1    maxv 		/* del */
   1056   1.46.4.2  martin 		cpudata->vmcb->state.rax &= ~cpuid->u.mask.del.eax;
   1057   1.46.4.2  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] &= ~cpuid->u.mask.del.ebx;
   1058   1.46.4.2  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] &= ~cpuid->u.mask.del.ecx;
   1059   1.46.4.2  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] &= ~cpuid->u.mask.del.edx;
   1060        1.1    maxv 
   1061        1.1    maxv 		/* set */
   1062   1.46.4.2  martin 		cpudata->vmcb->state.rax |= cpuid->u.mask.set.eax;
   1063   1.46.4.2  martin 		cpudata->gprs[NVMM_X64_GPR_RBX] |= cpuid->u.mask.set.ebx;
   1064   1.46.4.2  martin 		cpudata->gprs[NVMM_X64_GPR_RCX] |= cpuid->u.mask.set.ecx;
   1065   1.46.4.2  martin 		cpudata->gprs[NVMM_X64_GPR_RDX] |= cpuid->u.mask.set.edx;
   1066        1.1    maxv 
   1067        1.1    maxv 		break;
   1068        1.1    maxv 	}
   1069        1.1    maxv 
   1070       1.17    maxv 	svm_inkernel_advance(cpudata->vmcb);
   1071   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_NONE;
   1072        1.1    maxv }
   1073        1.1    maxv 
   1074       1.10    maxv static void
   1075       1.10    maxv svm_exit_hlt(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1076   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1077       1.10    maxv {
   1078       1.10    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1079       1.17    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1080       1.10    maxv 
   1081       1.17    maxv 	if (cpudata->int_window_exit && (vmcb->state.rflags & PSL_I)) {
   1082       1.17    maxv 		svm_event_waitexit_disable(vcpu, false);
   1083       1.17    maxv 	}
   1084       1.17    maxv 
   1085       1.17    maxv 	svm_inkernel_advance(cpudata->vmcb);
   1086   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_HALTED;
   1087       1.10    maxv }
   1088       1.10    maxv 
   1089        1.1    maxv #define SVM_EXIT_IO_PORT	__BITS(31,16)
   1090        1.1    maxv #define SVM_EXIT_IO_SEG		__BITS(12,10)
   1091        1.1    maxv #define SVM_EXIT_IO_A64		__BIT(9)
   1092        1.1    maxv #define SVM_EXIT_IO_A32		__BIT(8)
   1093        1.1    maxv #define SVM_EXIT_IO_A16		__BIT(7)
   1094        1.1    maxv #define SVM_EXIT_IO_SZ32	__BIT(6)
   1095        1.1    maxv #define SVM_EXIT_IO_SZ16	__BIT(5)
   1096        1.1    maxv #define SVM_EXIT_IO_SZ8		__BIT(4)
   1097        1.1    maxv #define SVM_EXIT_IO_REP		__BIT(3)
   1098        1.1    maxv #define SVM_EXIT_IO_STR		__BIT(2)
   1099        1.4    maxv #define SVM_EXIT_IO_IN		__BIT(0)
   1100        1.1    maxv 
   1101        1.1    maxv static void
   1102        1.1    maxv svm_exit_io(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1103   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1104        1.1    maxv {
   1105        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1106        1.1    maxv 	uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
   1107        1.1    maxv 	uint64_t nextpc = cpudata->vmcb->ctrl.exitinfo2;
   1108        1.1    maxv 
   1109   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_IO;
   1110        1.1    maxv 
   1111   1.46.4.2  martin 	exit->u.io.in = (info & SVM_EXIT_IO_IN) != 0;
   1112        1.1    maxv 	exit->u.io.port = __SHIFTOUT(info, SVM_EXIT_IO_PORT);
   1113        1.1    maxv 
   1114        1.1    maxv 	if (svm_decode_assist) {
   1115        1.1    maxv 		KASSERT(__SHIFTOUT(info, SVM_EXIT_IO_SEG) < 6);
   1116       1.32    maxv 		exit->u.io.seg = __SHIFTOUT(info, SVM_EXIT_IO_SEG);
   1117        1.1    maxv 	} else {
   1118        1.8    maxv 		exit->u.io.seg = -1;
   1119        1.1    maxv 	}
   1120        1.1    maxv 
   1121        1.1    maxv 	if (info & SVM_EXIT_IO_A64) {
   1122        1.1    maxv 		exit->u.io.address_size = 8;
   1123        1.1    maxv 	} else if (info & SVM_EXIT_IO_A32) {
   1124        1.1    maxv 		exit->u.io.address_size = 4;
   1125        1.1    maxv 	} else if (info & SVM_EXIT_IO_A16) {
   1126        1.1    maxv 		exit->u.io.address_size = 2;
   1127        1.1    maxv 	}
   1128        1.1    maxv 
   1129        1.1    maxv 	if (info & SVM_EXIT_IO_SZ32) {
   1130        1.1    maxv 		exit->u.io.operand_size = 4;
   1131        1.1    maxv 	} else if (info & SVM_EXIT_IO_SZ16) {
   1132        1.1    maxv 		exit->u.io.operand_size = 2;
   1133        1.1    maxv 	} else if (info & SVM_EXIT_IO_SZ8) {
   1134        1.1    maxv 		exit->u.io.operand_size = 1;
   1135        1.1    maxv 	}
   1136        1.1    maxv 
   1137        1.1    maxv 	exit->u.io.rep = (info & SVM_EXIT_IO_REP) != 0;
   1138        1.1    maxv 	exit->u.io.str = (info & SVM_EXIT_IO_STR) != 0;
   1139        1.1    maxv 	exit->u.io.npc = nextpc;
   1140       1.43    maxv 
   1141       1.43    maxv 	svm_vcpu_state_provide(vcpu,
   1142       1.43    maxv 	    NVMM_X64_STATE_GPRS | NVMM_X64_STATE_SEGS |
   1143       1.43    maxv 	    NVMM_X64_STATE_CRS | NVMM_X64_STATE_MSRS);
   1144        1.1    maxv }
   1145        1.1    maxv 
   1146       1.10    maxv static const uint64_t msr_ignore_list[] = {
   1147       1.10    maxv 	0xc0010055, /* MSR_CMPHALT */
   1148       1.10    maxv 	MSR_DE_CFG,
   1149       1.10    maxv 	MSR_IC_CFG,
   1150       1.10    maxv 	MSR_UCODE_AMD_PATCHLEVEL
   1151       1.10    maxv };
   1152       1.10    maxv 
   1153        1.1    maxv static bool
   1154        1.1    maxv svm_inkernel_handle_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1155   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1156        1.1    maxv {
   1157        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1158       1.19    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1159       1.10    maxv 	uint64_t val;
   1160       1.10    maxv 	size_t i;
   1161        1.1    maxv 
   1162   1.46.4.2  martin 	if (exit->reason == NVMM_VCPU_EXIT_RDMSR) {
   1163  1.46.4.12  martin 		if (exit->u.rdmsr.msr == MSR_EFER) {
   1164  1.46.4.12  martin 			val = vmcb->state.efer & ~EFER_SVME;
   1165  1.46.4.12  martin 			vmcb->state.rax = (val & 0xFFFFFFFF);
   1166  1.46.4.12  martin 			cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
   1167  1.46.4.12  martin 			goto handled;
   1168  1.46.4.12  martin 		}
   1169   1.46.4.2  martin 		if (exit->u.rdmsr.msr == MSR_NB_CFG) {
   1170       1.10    maxv 			val = NB_CFG_INITAPICCPUIDLO;
   1171       1.19    maxv 			vmcb->state.rax = (val & 0xFFFFFFFF);
   1172       1.13    maxv 			cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
   1173       1.10    maxv 			goto handled;
   1174       1.10    maxv 		}
   1175       1.10    maxv 		for (i = 0; i < __arraycount(msr_ignore_list); i++) {
   1176   1.46.4.2  martin 			if (msr_ignore_list[i] != exit->u.rdmsr.msr)
   1177       1.10    maxv 				continue;
   1178       1.10    maxv 			val = 0;
   1179       1.19    maxv 			vmcb->state.rax = (val & 0xFFFFFFFF);
   1180       1.13    maxv 			cpudata->gprs[NVMM_X64_GPR_RDX] = (val >> 32);
   1181        1.1    maxv 			goto handled;
   1182        1.1    maxv 		}
   1183   1.46.4.2  martin 	} else {
   1184   1.46.4.2  martin 		if (exit->u.wrmsr.msr == MSR_EFER) {
   1185   1.46.4.2  martin 			if (__predict_false(exit->u.wrmsr.val & ~EFER_VALID)) {
   1186       1.19    maxv 				goto error;
   1187        1.1    maxv 			}
   1188   1.46.4.2  martin 			if ((vmcb->state.efer ^ exit->u.wrmsr.val) &
   1189        1.1    maxv 			     EFER_TLB_FLUSH) {
   1190       1.28    maxv 				cpudata->gtlb_want_flush = true;
   1191        1.1    maxv 			}
   1192   1.46.4.2  martin 			vmcb->state.efer = exit->u.wrmsr.val | EFER_SVME;
   1193       1.24    maxv 			svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_CR);
   1194       1.24    maxv 			goto handled;
   1195       1.24    maxv 		}
   1196   1.46.4.2  martin 		if (exit->u.wrmsr.msr == MSR_TSC) {
   1197   1.46.4.2  martin 			cpudata->gtsc = exit->u.wrmsr.val;
   1198       1.36    maxv 			cpudata->gtsc_want_update = true;
   1199        1.1    maxv 			goto handled;
   1200        1.1    maxv 		}
   1201       1.10    maxv 		for (i = 0; i < __arraycount(msr_ignore_list); i++) {
   1202   1.46.4.2  martin 			if (msr_ignore_list[i] != exit->u.wrmsr.msr)
   1203       1.10    maxv 				continue;
   1204       1.10    maxv 			goto handled;
   1205       1.10    maxv 		}
   1206        1.1    maxv 	}
   1207        1.1    maxv 
   1208        1.1    maxv 	return false;
   1209        1.1    maxv 
   1210        1.1    maxv handled:
   1211       1.17    maxv 	svm_inkernel_advance(cpudata->vmcb);
   1212        1.1    maxv 	return true;
   1213       1.19    maxv 
   1214       1.19    maxv error:
   1215       1.45    maxv 	svm_inject_gp(vcpu);
   1216       1.19    maxv 	return true;
   1217        1.1    maxv }
   1218        1.1    maxv 
   1219   1.46.4.2  martin static inline void
   1220   1.46.4.2  martin svm_exit_rdmsr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1221   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1222        1.1    maxv {
   1223        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1224        1.1    maxv 
   1225   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_RDMSR;
   1226   1.46.4.2  martin 	exit->u.rdmsr.msr = (cpudata->gprs[NVMM_X64_GPR_RCX] & 0xFFFFFFFF);
   1227   1.46.4.2  martin 	exit->u.rdmsr.npc = cpudata->vmcb->ctrl.nrip;
   1228   1.46.4.2  martin 
   1229   1.46.4.2  martin 	if (svm_inkernel_handle_msr(mach, vcpu, exit)) {
   1230   1.46.4.2  martin 		exit->reason = NVMM_VCPU_EXIT_NONE;
   1231   1.46.4.2  martin 		return;
   1232        1.1    maxv 	}
   1233        1.1    maxv 
   1234   1.46.4.2  martin 	svm_vcpu_state_provide(vcpu, NVMM_X64_STATE_GPRS);
   1235   1.46.4.2  martin }
   1236        1.1    maxv 
   1237   1.46.4.2  martin static inline void
   1238   1.46.4.2  martin svm_exit_wrmsr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1239   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1240   1.46.4.2  martin {
   1241   1.46.4.2  martin 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1242   1.46.4.2  martin 	uint64_t rdx, rax;
   1243   1.46.4.2  martin 
   1244   1.46.4.2  martin 	rdx = cpudata->gprs[NVMM_X64_GPR_RDX];
   1245   1.46.4.2  martin 	rax = cpudata->vmcb->state.rax;
   1246   1.46.4.2  martin 
   1247   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_WRMSR;
   1248   1.46.4.2  martin 	exit->u.wrmsr.msr = (cpudata->gprs[NVMM_X64_GPR_RCX] & 0xFFFFFFFF);
   1249   1.46.4.2  martin 	exit->u.wrmsr.val = (rdx << 32) | (rax & 0xFFFFFFFF);
   1250   1.46.4.2  martin 	exit->u.wrmsr.npc = cpudata->vmcb->ctrl.nrip;
   1251        1.1    maxv 
   1252        1.1    maxv 	if (svm_inkernel_handle_msr(mach, vcpu, exit)) {
   1253   1.46.4.2  martin 		exit->reason = NVMM_VCPU_EXIT_NONE;
   1254        1.1    maxv 		return;
   1255        1.1    maxv 	}
   1256        1.1    maxv 
   1257       1.43    maxv 	svm_vcpu_state_provide(vcpu, NVMM_X64_STATE_GPRS);
   1258        1.1    maxv }
   1259        1.1    maxv 
   1260        1.1    maxv static void
   1261   1.46.4.2  martin svm_exit_msr(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1262   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1263   1.46.4.2  martin {
   1264   1.46.4.2  martin 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1265   1.46.4.2  martin 	uint64_t info = cpudata->vmcb->ctrl.exitinfo1;
   1266   1.46.4.2  martin 
   1267   1.46.4.2  martin 	if (info == 0) {
   1268   1.46.4.2  martin 		svm_exit_rdmsr(mach, vcpu, exit);
   1269   1.46.4.2  martin 	} else {
   1270   1.46.4.2  martin 		svm_exit_wrmsr(mach, vcpu, exit);
   1271   1.46.4.2  martin 	}
   1272   1.46.4.2  martin }
   1273   1.46.4.2  martin 
   1274   1.46.4.2  martin static void
   1275        1.1    maxv svm_exit_npf(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1276   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1277        1.1    maxv {
   1278        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1279        1.1    maxv 	gpaddr_t gpa = cpudata->vmcb->ctrl.exitinfo2;
   1280        1.1    maxv 
   1281   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_MEMORY;
   1282       1.27    maxv 	if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_W)
   1283       1.35    maxv 		exit->u.mem.prot = PROT_WRITE;
   1284       1.27    maxv 	else if (cpudata->vmcb->ctrl.exitinfo1 & PGEX_X)
   1285       1.35    maxv 		exit->u.mem.prot = PROT_EXEC;
   1286       1.27    maxv 	else
   1287       1.35    maxv 		exit->u.mem.prot = PROT_READ;
   1288       1.27    maxv 	exit->u.mem.gpa = gpa;
   1289       1.27    maxv 	exit->u.mem.inst_len = cpudata->vmcb->ctrl.inst_len;
   1290       1.27    maxv 	memcpy(exit->u.mem.inst_bytes, cpudata->vmcb->ctrl.inst_bytes,
   1291       1.27    maxv 	    sizeof(exit->u.mem.inst_bytes));
   1292       1.43    maxv 
   1293       1.43    maxv 	svm_vcpu_state_provide(vcpu,
   1294       1.43    maxv 	    NVMM_X64_STATE_GPRS | NVMM_X64_STATE_SEGS |
   1295       1.43    maxv 	    NVMM_X64_STATE_CRS | NVMM_X64_STATE_MSRS);
   1296        1.1    maxv }
   1297        1.1    maxv 
   1298        1.1    maxv static void
   1299        1.1    maxv svm_exit_xsetbv(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1300   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1301        1.1    maxv {
   1302        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1303        1.1    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1304        1.1    maxv 	uint64_t val;
   1305        1.1    maxv 
   1306   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_NONE;
   1307        1.1    maxv 
   1308       1.13    maxv 	val = (cpudata->gprs[NVMM_X64_GPR_RDX] << 32) |
   1309        1.3    maxv 	    (vmcb->state.rax & 0xFFFFFFFF);
   1310        1.1    maxv 
   1311       1.13    maxv 	if (__predict_false(cpudata->gprs[NVMM_X64_GPR_RCX] != 0)) {
   1312        1.1    maxv 		goto error;
   1313        1.1    maxv 	} else if (__predict_false(vmcb->state.cpl != 0)) {
   1314        1.1    maxv 		goto error;
   1315        1.1    maxv 	} else if (__predict_false((val & ~svm_xcr0_mask) != 0)) {
   1316        1.1    maxv 		goto error;
   1317        1.1    maxv 	} else if (__predict_false((val & XCR0_X87) == 0)) {
   1318        1.1    maxv 		goto error;
   1319        1.1    maxv 	}
   1320        1.1    maxv 
   1321       1.13    maxv 	cpudata->gxcr0 = val;
   1322        1.1    maxv 
   1323       1.17    maxv 	svm_inkernel_advance(cpudata->vmcb);
   1324        1.1    maxv 	return;
   1325        1.1    maxv 
   1326        1.1    maxv error:
   1327       1.45    maxv 	svm_inject_gp(vcpu);
   1328        1.1    maxv }
   1329        1.1    maxv 
   1330       1.40    maxv static void
   1331   1.46.4.2  martin svm_exit_invalid(struct nvmm_vcpu_exit *exit, uint64_t code)
   1332       1.40    maxv {
   1333       1.40    maxv 	exit->u.inv.hwcode = code;
   1334   1.46.4.2  martin 	exit->reason = NVMM_VCPU_EXIT_INVALID;
   1335       1.40    maxv }
   1336       1.40    maxv 
   1337       1.29    maxv /* -------------------------------------------------------------------------- */
   1338       1.29    maxv 
   1339        1.1    maxv static void
   1340        1.1    maxv svm_vcpu_guest_fpu_enter(struct nvmm_cpu *vcpu)
   1341        1.1    maxv {
   1342        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1343        1.1    maxv 
   1344       1.16    maxv 	cpudata->ts_set = (rcr0() & CR0_TS) != 0;
   1345       1.16    maxv 
   1346       1.16    maxv 	fpu_area_save(&cpudata->hfpu, svm_xcr0_mask);
   1347       1.16    maxv 	fpu_area_restore(&cpudata->gfpu, svm_xcr0_mask);
   1348       1.16    maxv 
   1349       1.16    maxv 	if (svm_xcr0_mask != 0) {
   1350       1.13    maxv 		cpudata->hxcr0 = rdxcr(0);
   1351       1.13    maxv 		wrxcr(0, cpudata->gxcr0);
   1352        1.1    maxv 	}
   1353        1.1    maxv }
   1354        1.1    maxv 
   1355        1.1    maxv static void
   1356        1.1    maxv svm_vcpu_guest_fpu_leave(struct nvmm_cpu *vcpu)
   1357        1.1    maxv {
   1358        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1359        1.1    maxv 
   1360       1.16    maxv 	if (svm_xcr0_mask != 0) {
   1361       1.16    maxv 		cpudata->gxcr0 = rdxcr(0);
   1362       1.16    maxv 		wrxcr(0, cpudata->hxcr0);
   1363       1.16    maxv 	}
   1364       1.16    maxv 
   1365       1.16    maxv 	fpu_area_save(&cpudata->gfpu, svm_xcr0_mask);
   1366       1.16    maxv 	fpu_area_restore(&cpudata->hfpu, svm_xcr0_mask);
   1367        1.1    maxv 
   1368        1.1    maxv 	if (cpudata->ts_set) {
   1369        1.1    maxv 		stts();
   1370        1.1    maxv 	}
   1371        1.1    maxv }
   1372        1.1    maxv 
   1373        1.1    maxv static void
   1374        1.1    maxv svm_vcpu_guest_dbregs_enter(struct nvmm_cpu *vcpu)
   1375        1.1    maxv {
   1376        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1377        1.1    maxv 
   1378        1.1    maxv 	x86_dbregs_save(curlwp);
   1379        1.1    maxv 
   1380       1.15    maxv 	ldr7(0);
   1381       1.15    maxv 
   1382       1.13    maxv 	ldr0(cpudata->drs[NVMM_X64_DR_DR0]);
   1383       1.13    maxv 	ldr1(cpudata->drs[NVMM_X64_DR_DR1]);
   1384       1.13    maxv 	ldr2(cpudata->drs[NVMM_X64_DR_DR2]);
   1385       1.13    maxv 	ldr3(cpudata->drs[NVMM_X64_DR_DR3]);
   1386        1.1    maxv }
   1387        1.1    maxv 
   1388        1.1    maxv static void
   1389        1.1    maxv svm_vcpu_guest_dbregs_leave(struct nvmm_cpu *vcpu)
   1390        1.1    maxv {
   1391        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1392        1.1    maxv 
   1393       1.13    maxv 	cpudata->drs[NVMM_X64_DR_DR0] = rdr0();
   1394       1.13    maxv 	cpudata->drs[NVMM_X64_DR_DR1] = rdr1();
   1395       1.13    maxv 	cpudata->drs[NVMM_X64_DR_DR2] = rdr2();
   1396       1.13    maxv 	cpudata->drs[NVMM_X64_DR_DR3] = rdr3();
   1397        1.1    maxv 
   1398        1.1    maxv 	x86_dbregs_restore(curlwp);
   1399        1.1    maxv }
   1400        1.1    maxv 
   1401        1.1    maxv static void
   1402        1.1    maxv svm_vcpu_guest_misc_enter(struct nvmm_cpu *vcpu)
   1403        1.1    maxv {
   1404        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1405        1.1    maxv 
   1406       1.14    maxv 	cpudata->fsbase = rdmsr(MSR_FSBASE);
   1407       1.14    maxv 	cpudata->kernelgsbase = rdmsr(MSR_KERNELGSBASE);
   1408        1.1    maxv }
   1409        1.1    maxv 
   1410        1.1    maxv static void
   1411        1.1    maxv svm_vcpu_guest_misc_leave(struct nvmm_cpu *vcpu)
   1412        1.1    maxv {
   1413        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1414        1.1    maxv 
   1415        1.1    maxv 	wrmsr(MSR_STAR, cpudata->star);
   1416        1.1    maxv 	wrmsr(MSR_LSTAR, cpudata->lstar);
   1417        1.1    maxv 	wrmsr(MSR_CSTAR, cpudata->cstar);
   1418        1.1    maxv 	wrmsr(MSR_SFMASK, cpudata->sfmask);
   1419       1.14    maxv 	wrmsr(MSR_FSBASE, cpudata->fsbase);
   1420       1.14    maxv 	wrmsr(MSR_KERNELGSBASE, cpudata->kernelgsbase);
   1421        1.1    maxv }
   1422        1.1    maxv 
   1423       1.28    maxv /* -------------------------------------------------------------------------- */
   1424       1.28    maxv 
   1425       1.28    maxv static inline void
   1426       1.28    maxv svm_gtlb_catchup(struct nvmm_cpu *vcpu, int hcpu)
   1427       1.28    maxv {
   1428       1.28    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1429       1.28    maxv 
   1430       1.28    maxv 	if (vcpu->hcpu_last != hcpu || cpudata->shared_asid) {
   1431       1.28    maxv 		cpudata->gtlb_want_flush = true;
   1432       1.28    maxv 	}
   1433       1.28    maxv }
   1434       1.28    maxv 
   1435       1.29    maxv static inline void
   1436       1.29    maxv svm_htlb_catchup(struct nvmm_cpu *vcpu, int hcpu)
   1437       1.29    maxv {
   1438       1.29    maxv 	/*
   1439       1.29    maxv 	 * Nothing to do. If an hTLB flush was needed, either the VCPU was
   1440       1.29    maxv 	 * executing on this hCPU and the hTLB already got flushed, or it
   1441       1.29    maxv 	 * was executing on another hCPU in which case the catchup is done
   1442       1.29    maxv 	 * in svm_gtlb_catchup().
   1443       1.29    maxv 	 */
   1444       1.29    maxv }
   1445       1.29    maxv 
   1446       1.29    maxv static inline uint64_t
   1447       1.29    maxv svm_htlb_flush(struct svm_machdata *machdata, struct svm_cpudata *cpudata)
   1448       1.29    maxv {
   1449       1.29    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1450       1.29    maxv 	uint64_t machgen;
   1451       1.29    maxv 
   1452       1.29    maxv 	machgen = machdata->mach_htlb_gen;
   1453       1.29    maxv 	if (__predict_true(machgen == cpudata->vcpu_htlb_gen)) {
   1454       1.29    maxv 		return machgen;
   1455       1.29    maxv 	}
   1456       1.29    maxv 
   1457       1.29    maxv 	vmcb->ctrl.tlb_ctrl = svm_ctrl_tlb_flush;
   1458       1.29    maxv 	return machgen;
   1459       1.29    maxv }
   1460       1.29    maxv 
   1461       1.29    maxv static inline void
   1462       1.29    maxv svm_htlb_flush_ack(struct svm_cpudata *cpudata, uint64_t machgen)
   1463       1.29    maxv {
   1464       1.29    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1465       1.29    maxv 
   1466       1.29    maxv 	if (__predict_true(vmcb->ctrl.exitcode != VMCB_EXITCODE_INVALID)) {
   1467       1.29    maxv 		cpudata->vcpu_htlb_gen = machgen;
   1468       1.29    maxv 	}
   1469       1.29    maxv }
   1470       1.29    maxv 
   1471       1.41    maxv static inline void
   1472       1.41    maxv svm_exit_evt(struct svm_cpudata *cpudata, struct vmcb *vmcb)
   1473       1.41    maxv {
   1474       1.41    maxv 	cpudata->evt_pending = false;
   1475       1.41    maxv 
   1476       1.41    maxv 	if (__predict_false(vmcb->ctrl.exitintinfo & VMCB_CTRL_EXITINTINFO_V)) {
   1477       1.41    maxv 		vmcb->ctrl.eventinj = vmcb->ctrl.exitintinfo;
   1478       1.41    maxv 		cpudata->evt_pending = true;
   1479       1.41    maxv 	}
   1480       1.41    maxv }
   1481       1.41    maxv 
   1482        1.1    maxv static int
   1483        1.1    maxv svm_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
   1484   1.46.4.2  martin     struct nvmm_vcpu_exit *exit)
   1485        1.1    maxv {
   1486       1.43    maxv 	struct nvmm_comm_page *comm = vcpu->comm;
   1487       1.29    maxv 	struct svm_machdata *machdata = mach->machdata;
   1488        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1489        1.1    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1490       1.29    maxv 	uint64_t machgen;
   1491        1.1    maxv 	int hcpu, s;
   1492        1.1    maxv 
   1493  1.46.4.13  martin 	svm_vcpu_state_commit(vcpu);
   1494  1.46.4.13  martin 	comm->state_cached = 0;
   1495  1.46.4.13  martin 
   1496       1.45    maxv 	if (__predict_false(svm_vcpu_event_commit(vcpu) != 0)) {
   1497       1.45    maxv 		return EINVAL;
   1498       1.45    maxv 	}
   1499       1.43    maxv 
   1500        1.1    maxv 	kpreempt_disable();
   1501        1.1    maxv 	hcpu = cpu_number();
   1502        1.1    maxv 
   1503       1.28    maxv 	svm_gtlb_catchup(vcpu, hcpu);
   1504       1.29    maxv 	svm_htlb_catchup(vcpu, hcpu);
   1505        1.1    maxv 
   1506        1.1    maxv 	if (vcpu->hcpu_last != hcpu) {
   1507       1.12    maxv 		svm_vmcb_cache_flush_all(vmcb);
   1508       1.36    maxv 		cpudata->gtsc_want_update = true;
   1509        1.1    maxv 	}
   1510        1.1    maxv 
   1511        1.1    maxv 	svm_vcpu_guest_dbregs_enter(vcpu);
   1512        1.1    maxv 	svm_vcpu_guest_misc_enter(vcpu);
   1513        1.1    maxv 
   1514        1.1    maxv 	while (1) {
   1515       1.28    maxv 		if (cpudata->gtlb_want_flush) {
   1516       1.20    maxv 			vmcb->ctrl.tlb_ctrl = svm_ctrl_tlb_flush;
   1517       1.20    maxv 		} else {
   1518       1.20    maxv 			vmcb->ctrl.tlb_ctrl = 0;
   1519       1.20    maxv 		}
   1520       1.20    maxv 
   1521       1.36    maxv 		if (__predict_false(cpudata->gtsc_want_update)) {
   1522       1.36    maxv 			vmcb->ctrl.tsc_offset = cpudata->gtsc - rdtsc();
   1523       1.36    maxv 			svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_I);
   1524       1.36    maxv 		}
   1525       1.36    maxv 
   1526        1.1    maxv 		s = splhigh();
   1527       1.29    maxv 		machgen = svm_htlb_flush(machdata, cpudata);
   1528        1.1    maxv 		svm_vcpu_guest_fpu_enter(vcpu);
   1529       1.13    maxv 		svm_vmrun(cpudata->vmcb_pa, cpudata->gprs);
   1530        1.1    maxv 		svm_vcpu_guest_fpu_leave(vcpu);
   1531       1.29    maxv 		svm_htlb_flush_ack(cpudata, machgen);
   1532        1.1    maxv 		splx(s);
   1533        1.1    maxv 
   1534        1.1    maxv 		svm_vmcb_cache_default(vmcb);
   1535        1.1    maxv 
   1536        1.1    maxv 		if (vmcb->ctrl.exitcode != VMCB_EXITCODE_INVALID) {
   1537       1.28    maxv 			cpudata->gtlb_want_flush = false;
   1538       1.36    maxv 			cpudata->gtsc_want_update = false;
   1539        1.1    maxv 			vcpu->hcpu_last = hcpu;
   1540        1.1    maxv 		}
   1541       1.41    maxv 		svm_exit_evt(cpudata, vmcb);
   1542        1.1    maxv 
   1543        1.1    maxv 		switch (vmcb->ctrl.exitcode) {
   1544        1.1    maxv 		case VMCB_EXITCODE_INTR:
   1545        1.1    maxv 		case VMCB_EXITCODE_NMI:
   1546   1.46.4.2  martin 			exit->reason = NVMM_VCPU_EXIT_NONE;
   1547        1.1    maxv 			break;
   1548        1.1    maxv 		case VMCB_EXITCODE_VINTR:
   1549       1.10    maxv 			svm_event_waitexit_disable(vcpu, false);
   1550   1.46.4.2  martin 			exit->reason = NVMM_VCPU_EXIT_INT_READY;
   1551        1.1    maxv 			break;
   1552        1.1    maxv 		case VMCB_EXITCODE_IRET:
   1553       1.10    maxv 			svm_event_waitexit_disable(vcpu, true);
   1554   1.46.4.2  martin 			exit->reason = NVMM_VCPU_EXIT_NMI_READY;
   1555        1.1    maxv 			break;
   1556        1.1    maxv 		case VMCB_EXITCODE_CPUID:
   1557        1.1    maxv 			svm_exit_cpuid(mach, vcpu, exit);
   1558        1.1    maxv 			break;
   1559        1.1    maxv 		case VMCB_EXITCODE_HLT:
   1560       1.10    maxv 			svm_exit_hlt(mach, vcpu, exit);
   1561        1.1    maxv 			break;
   1562        1.1    maxv 		case VMCB_EXITCODE_IOIO:
   1563        1.1    maxv 			svm_exit_io(mach, vcpu, exit);
   1564        1.1    maxv 			break;
   1565        1.1    maxv 		case VMCB_EXITCODE_MSR:
   1566        1.1    maxv 			svm_exit_msr(mach, vcpu, exit);
   1567        1.1    maxv 			break;
   1568        1.1    maxv 		case VMCB_EXITCODE_SHUTDOWN:
   1569   1.46.4.2  martin 			exit->reason = NVMM_VCPU_EXIT_SHUTDOWN;
   1570        1.1    maxv 			break;
   1571        1.1    maxv 		case VMCB_EXITCODE_RDPMC:
   1572        1.1    maxv 		case VMCB_EXITCODE_RSM:
   1573        1.1    maxv 		case VMCB_EXITCODE_INVLPGA:
   1574        1.1    maxv 		case VMCB_EXITCODE_VMRUN:
   1575        1.1    maxv 		case VMCB_EXITCODE_VMMCALL:
   1576        1.1    maxv 		case VMCB_EXITCODE_VMLOAD:
   1577        1.1    maxv 		case VMCB_EXITCODE_VMSAVE:
   1578        1.1    maxv 		case VMCB_EXITCODE_STGI:
   1579        1.1    maxv 		case VMCB_EXITCODE_CLGI:
   1580        1.1    maxv 		case VMCB_EXITCODE_SKINIT:
   1581        1.1    maxv 		case VMCB_EXITCODE_RDTSCP:
   1582   1.46.4.8  martin 		case VMCB_EXITCODE_RDPRU:
   1583   1.46.4.8  martin 		case VMCB_EXITCODE_INVLPGB:
   1584   1.46.4.8  martin 		case VMCB_EXITCODE_INVPCID:
   1585   1.46.4.8  martin 		case VMCB_EXITCODE_MCOMMIT:
   1586   1.46.4.8  martin 		case VMCB_EXITCODE_TLBSYNC:
   1587       1.45    maxv 			svm_inject_ud(vcpu);
   1588   1.46.4.2  martin 			exit->reason = NVMM_VCPU_EXIT_NONE;
   1589        1.1    maxv 			break;
   1590        1.1    maxv 		case VMCB_EXITCODE_MONITOR:
   1591   1.46.4.2  martin 			svm_exit_insn(vmcb, exit, NVMM_VCPU_EXIT_MONITOR);
   1592        1.1    maxv 			break;
   1593        1.1    maxv 		case VMCB_EXITCODE_MWAIT:
   1594        1.1    maxv 		case VMCB_EXITCODE_MWAIT_CONDITIONAL:
   1595   1.46.4.2  martin 			svm_exit_insn(vmcb, exit, NVMM_VCPU_EXIT_MWAIT);
   1596        1.1    maxv 			break;
   1597        1.1    maxv 		case VMCB_EXITCODE_XSETBV:
   1598        1.1    maxv 			svm_exit_xsetbv(mach, vcpu, exit);
   1599        1.1    maxv 			break;
   1600        1.1    maxv 		case VMCB_EXITCODE_NPF:
   1601        1.1    maxv 			svm_exit_npf(mach, vcpu, exit);
   1602        1.1    maxv 			break;
   1603        1.1    maxv 		case VMCB_EXITCODE_FERR_FREEZE: /* ? */
   1604        1.1    maxv 		default:
   1605       1.40    maxv 			svm_exit_invalid(exit, vmcb->ctrl.exitcode);
   1606        1.1    maxv 			break;
   1607        1.1    maxv 		}
   1608        1.1    maxv 
   1609        1.1    maxv 		/* If no reason to return to userland, keep rolling. */
   1610   1.46.4.6  martin 		if (nvmm_return_needed()) {
   1611       1.10    maxv 			break;
   1612       1.10    maxv 		}
   1613   1.46.4.2  martin 		if (exit->reason != NVMM_VCPU_EXIT_NONE) {
   1614        1.1    maxv 			break;
   1615        1.1    maxv 		}
   1616        1.1    maxv 	}
   1617        1.1    maxv 
   1618       1.36    maxv 	cpudata->gtsc = rdtsc() + vmcb->ctrl.tsc_offset;
   1619       1.36    maxv 
   1620        1.1    maxv 	svm_vcpu_guest_misc_leave(vcpu);
   1621        1.1    maxv 	svm_vcpu_guest_dbregs_leave(vcpu);
   1622        1.1    maxv 
   1623        1.1    maxv 	kpreempt_enable();
   1624        1.1    maxv 
   1625   1.46.4.2  martin 	exit->exitstate.rflags = vmcb->state.rflags;
   1626   1.46.4.2  martin 	exit->exitstate.cr8 = __SHIFTOUT(vmcb->ctrl.v, VMCB_CTRL_V_TPR);
   1627   1.46.4.2  martin 	exit->exitstate.int_shadow =
   1628       1.10    maxv 	    ((vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0);
   1629   1.46.4.2  martin 	exit->exitstate.int_window_exiting = cpudata->int_window_exit;
   1630   1.46.4.2  martin 	exit->exitstate.nmi_window_exiting = cpudata->nmi_window_exit;
   1631   1.46.4.2  martin 	exit->exitstate.evt_pending = cpudata->evt_pending;
   1632       1.10    maxv 
   1633        1.1    maxv 	return 0;
   1634        1.1    maxv }
   1635        1.1    maxv 
   1636        1.1    maxv /* -------------------------------------------------------------------------- */
   1637        1.1    maxv 
   1638        1.1    maxv static int
   1639        1.1    maxv svm_memalloc(paddr_t *pa, vaddr_t *va, size_t npages)
   1640        1.1    maxv {
   1641        1.1    maxv 	struct pglist pglist;
   1642        1.1    maxv 	paddr_t _pa;
   1643        1.1    maxv 	vaddr_t _va;
   1644        1.1    maxv 	size_t i;
   1645        1.1    maxv 	int ret;
   1646        1.1    maxv 
   1647        1.1    maxv 	ret = uvm_pglistalloc(npages * PAGE_SIZE, 0, ~0UL, PAGE_SIZE, 0,
   1648        1.1    maxv 	    &pglist, 1, 0);
   1649        1.1    maxv 	if (ret != 0)
   1650        1.1    maxv 		return ENOMEM;
   1651   1.46.4.7  martin 	_pa = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
   1652        1.1    maxv 	_va = uvm_km_alloc(kernel_map, npages * PAGE_SIZE, 0,
   1653        1.1    maxv 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
   1654        1.1    maxv 	if (_va == 0)
   1655        1.1    maxv 		goto error;
   1656        1.1    maxv 
   1657        1.1    maxv 	for (i = 0; i < npages; i++) {
   1658        1.1    maxv 		pmap_kenter_pa(_va + i * PAGE_SIZE, _pa + i * PAGE_SIZE,
   1659        1.1    maxv 		    VM_PROT_READ | VM_PROT_WRITE, PMAP_WRITE_BACK);
   1660        1.1    maxv 	}
   1661        1.5    maxv 	pmap_update(pmap_kernel());
   1662        1.1    maxv 
   1663        1.1    maxv 	memset((void *)_va, 0, npages * PAGE_SIZE);
   1664        1.1    maxv 
   1665        1.1    maxv 	*pa = _pa;
   1666        1.1    maxv 	*va = _va;
   1667        1.1    maxv 	return 0;
   1668        1.1    maxv 
   1669        1.1    maxv error:
   1670        1.1    maxv 	for (i = 0; i < npages; i++) {
   1671        1.1    maxv 		uvm_pagefree(PHYS_TO_VM_PAGE(_pa + i * PAGE_SIZE));
   1672        1.1    maxv 	}
   1673        1.1    maxv 	return ENOMEM;
   1674        1.1    maxv }
   1675        1.1    maxv 
   1676        1.1    maxv static void
   1677        1.1    maxv svm_memfree(paddr_t pa, vaddr_t va, size_t npages)
   1678        1.1    maxv {
   1679        1.1    maxv 	size_t i;
   1680        1.1    maxv 
   1681        1.1    maxv 	pmap_kremove(va, npages * PAGE_SIZE);
   1682        1.1    maxv 	pmap_update(pmap_kernel());
   1683        1.1    maxv 	uvm_km_free(kernel_map, va, npages * PAGE_SIZE, UVM_KMF_VAONLY);
   1684        1.1    maxv 	for (i = 0; i < npages; i++) {
   1685        1.1    maxv 		uvm_pagefree(PHYS_TO_VM_PAGE(pa + i * PAGE_SIZE));
   1686        1.1    maxv 	}
   1687        1.1    maxv }
   1688        1.1    maxv 
   1689        1.1    maxv /* -------------------------------------------------------------------------- */
   1690        1.1    maxv 
   1691        1.1    maxv #define SVM_MSRBM_READ	__BIT(0)
   1692        1.1    maxv #define SVM_MSRBM_WRITE	__BIT(1)
   1693        1.1    maxv 
   1694        1.1    maxv static void
   1695        1.1    maxv svm_vcpu_msr_allow(uint8_t *bitmap, uint64_t msr, bool read, bool write)
   1696        1.1    maxv {
   1697        1.1    maxv 	uint64_t byte;
   1698        1.1    maxv 	uint8_t bitoff;
   1699        1.1    maxv 
   1700        1.1    maxv 	if (msr < 0x00002000) {
   1701        1.1    maxv 		/* Range 1 */
   1702        1.1    maxv 		byte = ((msr - 0x00000000) >> 2UL) + 0x0000;
   1703        1.1    maxv 	} else if (msr >= 0xC0000000 && msr < 0xC0002000) {
   1704        1.1    maxv 		/* Range 2 */
   1705        1.1    maxv 		byte = ((msr - 0xC0000000) >> 2UL) + 0x0800;
   1706        1.1    maxv 	} else if (msr >= 0xC0010000 && msr < 0xC0012000) {
   1707        1.1    maxv 		/* Range 3 */
   1708        1.1    maxv 		byte = ((msr - 0xC0010000) >> 2UL) + 0x1000;
   1709        1.1    maxv 	} else {
   1710        1.1    maxv 		panic("%s: wrong range", __func__);
   1711        1.1    maxv 	}
   1712        1.1    maxv 
   1713        1.1    maxv 	bitoff = (msr & 0x3) << 1;
   1714        1.1    maxv 
   1715        1.1    maxv 	if (read) {
   1716        1.1    maxv 		bitmap[byte] &= ~(SVM_MSRBM_READ << bitoff);
   1717        1.1    maxv 	}
   1718        1.1    maxv 	if (write) {
   1719        1.1    maxv 		bitmap[byte] &= ~(SVM_MSRBM_WRITE << bitoff);
   1720        1.1    maxv 	}
   1721        1.1    maxv }
   1722        1.1    maxv 
   1723       1.32    maxv #define SVM_SEG_ATTRIB_TYPE		__BITS(3,0)
   1724       1.32    maxv #define SVM_SEG_ATTRIB_S		__BIT(4)
   1725        1.1    maxv #define SVM_SEG_ATTRIB_DPL		__BITS(6,5)
   1726        1.1    maxv #define SVM_SEG_ATTRIB_P		__BIT(7)
   1727        1.1    maxv #define SVM_SEG_ATTRIB_AVL		__BIT(8)
   1728       1.32    maxv #define SVM_SEG_ATTRIB_L		__BIT(9)
   1729       1.32    maxv #define SVM_SEG_ATTRIB_DEF		__BIT(10)
   1730       1.32    maxv #define SVM_SEG_ATTRIB_G		__BIT(11)
   1731        1.1    maxv 
   1732        1.1    maxv static void
   1733       1.30    maxv svm_vcpu_setstate_seg(const struct nvmm_x64_state_seg *seg,
   1734       1.30    maxv     struct vmcb_segment *vseg)
   1735        1.1    maxv {
   1736        1.1    maxv 	vseg->selector = seg->selector;
   1737        1.1    maxv 	vseg->attrib =
   1738        1.1    maxv 	    __SHIFTIN(seg->attrib.type, SVM_SEG_ATTRIB_TYPE) |
   1739       1.32    maxv 	    __SHIFTIN(seg->attrib.s, SVM_SEG_ATTRIB_S) |
   1740        1.1    maxv 	    __SHIFTIN(seg->attrib.dpl, SVM_SEG_ATTRIB_DPL) |
   1741        1.1    maxv 	    __SHIFTIN(seg->attrib.p, SVM_SEG_ATTRIB_P) |
   1742        1.1    maxv 	    __SHIFTIN(seg->attrib.avl, SVM_SEG_ATTRIB_AVL) |
   1743       1.32    maxv 	    __SHIFTIN(seg->attrib.l, SVM_SEG_ATTRIB_L) |
   1744       1.32    maxv 	    __SHIFTIN(seg->attrib.def, SVM_SEG_ATTRIB_DEF) |
   1745       1.32    maxv 	    __SHIFTIN(seg->attrib.g, SVM_SEG_ATTRIB_G);
   1746        1.1    maxv 	vseg->limit = seg->limit;
   1747        1.1    maxv 	vseg->base = seg->base;
   1748        1.1    maxv }
   1749        1.1    maxv 
   1750        1.1    maxv static void
   1751        1.1    maxv svm_vcpu_getstate_seg(struct nvmm_x64_state_seg *seg, struct vmcb_segment *vseg)
   1752        1.1    maxv {
   1753        1.1    maxv 	seg->selector = vseg->selector;
   1754        1.1    maxv 	seg->attrib.type = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_TYPE);
   1755       1.32    maxv 	seg->attrib.s = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_S);
   1756        1.1    maxv 	seg->attrib.dpl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DPL);
   1757        1.1    maxv 	seg->attrib.p = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_P);
   1758        1.1    maxv 	seg->attrib.avl = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_AVL);
   1759       1.32    maxv 	seg->attrib.l = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_L);
   1760       1.32    maxv 	seg->attrib.def = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_DEF);
   1761       1.32    maxv 	seg->attrib.g = __SHIFTOUT(vseg->attrib, SVM_SEG_ATTRIB_G);
   1762        1.1    maxv 	seg->limit = vseg->limit;
   1763        1.1    maxv 	seg->base = vseg->base;
   1764        1.1    maxv }
   1765        1.1    maxv 
   1766       1.13    maxv static inline bool
   1767       1.30    maxv svm_state_tlb_flush(const struct vmcb *vmcb, const struct nvmm_x64_state *state,
   1768       1.13    maxv     uint64_t flags)
   1769        1.1    maxv {
   1770        1.1    maxv 	if (flags & NVMM_X64_STATE_CRS) {
   1771       1.13    maxv 		if ((vmcb->state.cr0 ^
   1772       1.13    maxv 		     state->crs[NVMM_X64_CR_CR0]) & CR0_TLB_FLUSH) {
   1773        1.1    maxv 			return true;
   1774        1.1    maxv 		}
   1775       1.13    maxv 		if (vmcb->state.cr3 != state->crs[NVMM_X64_CR_CR3]) {
   1776        1.1    maxv 			return true;
   1777        1.1    maxv 		}
   1778       1.13    maxv 		if ((vmcb->state.cr4 ^
   1779       1.13    maxv 		     state->crs[NVMM_X64_CR_CR4]) & CR4_TLB_FLUSH) {
   1780        1.1    maxv 			return true;
   1781        1.1    maxv 		}
   1782        1.1    maxv 	}
   1783        1.1    maxv 
   1784        1.1    maxv 	if (flags & NVMM_X64_STATE_MSRS) {
   1785       1.13    maxv 		if ((vmcb->state.efer ^
   1786       1.13    maxv 		     state->msrs[NVMM_X64_MSR_EFER]) & EFER_TLB_FLUSH) {
   1787        1.1    maxv 			return true;
   1788        1.1    maxv 		}
   1789        1.1    maxv 	}
   1790        1.1    maxv 
   1791        1.1    maxv 	return false;
   1792        1.1    maxv }
   1793        1.1    maxv 
   1794        1.1    maxv static void
   1795       1.43    maxv svm_vcpu_setstate(struct nvmm_cpu *vcpu)
   1796        1.1    maxv {
   1797       1.43    maxv 	struct nvmm_comm_page *comm = vcpu->comm;
   1798       1.43    maxv 	const struct nvmm_x64_state *state = &comm->state;
   1799        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1800        1.1    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1801        1.1    maxv 	struct fxsave *fpustate;
   1802       1.43    maxv 	uint64_t flags;
   1803       1.43    maxv 
   1804       1.43    maxv 	flags = comm->state_wanted;
   1805        1.1    maxv 
   1806       1.13    maxv 	if (svm_state_tlb_flush(vmcb, state, flags)) {
   1807       1.28    maxv 		cpudata->gtlb_want_flush = true;
   1808        1.1    maxv 	}
   1809        1.1    maxv 
   1810        1.1    maxv 	if (flags & NVMM_X64_STATE_SEGS) {
   1811       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_CS],
   1812        1.1    maxv 		    &vmcb->state.cs);
   1813       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_DS],
   1814        1.1    maxv 		    &vmcb->state.ds);
   1815       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_ES],
   1816        1.1    maxv 		    &vmcb->state.es);
   1817       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_FS],
   1818        1.1    maxv 		    &vmcb->state.fs);
   1819       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_GS],
   1820        1.1    maxv 		    &vmcb->state.gs);
   1821       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_SS],
   1822        1.1    maxv 		    &vmcb->state.ss);
   1823       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_GDT],
   1824        1.1    maxv 		    &vmcb->state.gdt);
   1825       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_IDT],
   1826        1.1    maxv 		    &vmcb->state.idt);
   1827       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_LDT],
   1828        1.1    maxv 		    &vmcb->state.ldt);
   1829       1.13    maxv 		svm_vcpu_setstate_seg(&state->segs[NVMM_X64_SEG_TR],
   1830        1.1    maxv 		    &vmcb->state.tr);
   1831       1.23    maxv 
   1832       1.23    maxv 		vmcb->state.cpl = state->segs[NVMM_X64_SEG_SS].attrib.dpl;
   1833        1.1    maxv 	}
   1834        1.1    maxv 
   1835       1.13    maxv 	CTASSERT(sizeof(cpudata->gprs) == sizeof(state->gprs));
   1836        1.1    maxv 	if (flags & NVMM_X64_STATE_GPRS) {
   1837       1.13    maxv 		memcpy(cpudata->gprs, state->gprs, sizeof(state->gprs));
   1838        1.1    maxv 
   1839       1.13    maxv 		vmcb->state.rip = state->gprs[NVMM_X64_GPR_RIP];
   1840       1.13    maxv 		vmcb->state.rsp = state->gprs[NVMM_X64_GPR_RSP];
   1841       1.13    maxv 		vmcb->state.rax = state->gprs[NVMM_X64_GPR_RAX];
   1842       1.13    maxv 		vmcb->state.rflags = state->gprs[NVMM_X64_GPR_RFLAGS];
   1843        1.1    maxv 	}
   1844        1.1    maxv 
   1845        1.1    maxv 	if (flags & NVMM_X64_STATE_CRS) {
   1846       1.13    maxv 		vmcb->state.cr0 = state->crs[NVMM_X64_CR_CR0];
   1847       1.13    maxv 		vmcb->state.cr2 = state->crs[NVMM_X64_CR_CR2];
   1848       1.13    maxv 		vmcb->state.cr3 = state->crs[NVMM_X64_CR_CR3];
   1849       1.13    maxv 		vmcb->state.cr4 = state->crs[NVMM_X64_CR_CR4];
   1850        1.1    maxv 
   1851        1.1    maxv 		vmcb->ctrl.v &= ~VMCB_CTRL_V_TPR;
   1852       1.13    maxv 		vmcb->ctrl.v |= __SHIFTIN(state->crs[NVMM_X64_CR_CR8],
   1853        1.1    maxv 		    VMCB_CTRL_V_TPR);
   1854        1.1    maxv 
   1855        1.1    maxv 		if (svm_xcr0_mask != 0) {
   1856       1.16    maxv 			/* Clear illegal XCR0 bits, set mandatory X87 bit. */
   1857       1.13    maxv 			cpudata->gxcr0 = state->crs[NVMM_X64_CR_XCR0];
   1858       1.13    maxv 			cpudata->gxcr0 &= svm_xcr0_mask;
   1859       1.13    maxv 			cpudata->gxcr0 |= XCR0_X87;
   1860        1.1    maxv 		}
   1861        1.1    maxv 	}
   1862        1.1    maxv 
   1863       1.13    maxv 	CTASSERT(sizeof(cpudata->drs) == sizeof(state->drs));
   1864        1.1    maxv 	if (flags & NVMM_X64_STATE_DRS) {
   1865       1.13    maxv 		memcpy(cpudata->drs, state->drs, sizeof(state->drs));
   1866        1.1    maxv 
   1867       1.13    maxv 		vmcb->state.dr6 = state->drs[NVMM_X64_DR_DR6];
   1868       1.13    maxv 		vmcb->state.dr7 = state->drs[NVMM_X64_DR_DR7];
   1869        1.1    maxv 	}
   1870        1.1    maxv 
   1871        1.1    maxv 	if (flags & NVMM_X64_STATE_MSRS) {
   1872       1.30    maxv 		/*
   1873       1.30    maxv 		 * EFER_SVME is mandatory.
   1874       1.30    maxv 		 */
   1875       1.13    maxv 		vmcb->state.efer = state->msrs[NVMM_X64_MSR_EFER] | EFER_SVME;
   1876       1.13    maxv 		vmcb->state.star = state->msrs[NVMM_X64_MSR_STAR];
   1877       1.13    maxv 		vmcb->state.lstar = state->msrs[NVMM_X64_MSR_LSTAR];
   1878       1.13    maxv 		vmcb->state.cstar = state->msrs[NVMM_X64_MSR_CSTAR];
   1879       1.13    maxv 		vmcb->state.sfmask = state->msrs[NVMM_X64_MSR_SFMASK];
   1880        1.1    maxv 		vmcb->state.kernelgsbase =
   1881       1.13    maxv 		    state->msrs[NVMM_X64_MSR_KERNELGSBASE];
   1882        1.1    maxv 		vmcb->state.sysenter_cs =
   1883       1.13    maxv 		    state->msrs[NVMM_X64_MSR_SYSENTER_CS];
   1884        1.1    maxv 		vmcb->state.sysenter_esp =
   1885       1.13    maxv 		    state->msrs[NVMM_X64_MSR_SYSENTER_ESP];
   1886        1.1    maxv 		vmcb->state.sysenter_eip =
   1887       1.13    maxv 		    state->msrs[NVMM_X64_MSR_SYSENTER_EIP];
   1888       1.13    maxv 		vmcb->state.g_pat = state->msrs[NVMM_X64_MSR_PAT];
   1889       1.36    maxv 
   1890       1.36    maxv 		cpudata->gtsc = state->msrs[NVMM_X64_MSR_TSC];
   1891       1.36    maxv 		cpudata->gtsc_want_update = true;
   1892        1.1    maxv 	}
   1893        1.1    maxv 
   1894       1.37    maxv 	if (flags & NVMM_X64_STATE_INTR) {
   1895       1.37    maxv 		if (state->intr.int_shadow) {
   1896       1.10    maxv 			vmcb->ctrl.intr |= VMCB_CTRL_INTR_SHADOW;
   1897       1.10    maxv 		} else {
   1898       1.10    maxv 			vmcb->ctrl.intr &= ~VMCB_CTRL_INTR_SHADOW;
   1899       1.10    maxv 		}
   1900       1.10    maxv 
   1901       1.37    maxv 		if (state->intr.int_window_exiting) {
   1902       1.10    maxv 			svm_event_waitexit_enable(vcpu, false);
   1903       1.10    maxv 		} else {
   1904       1.10    maxv 			svm_event_waitexit_disable(vcpu, false);
   1905       1.10    maxv 		}
   1906       1.10    maxv 
   1907       1.37    maxv 		if (state->intr.nmi_window_exiting) {
   1908       1.10    maxv 			svm_event_waitexit_enable(vcpu, true);
   1909       1.10    maxv 		} else {
   1910       1.10    maxv 			svm_event_waitexit_disable(vcpu, true);
   1911       1.10    maxv 		}
   1912        1.1    maxv 	}
   1913        1.1    maxv 
   1914       1.13    maxv 	CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(state->fpu));
   1915        1.1    maxv 	if (flags & NVMM_X64_STATE_FPU) {
   1916       1.13    maxv 		memcpy(cpudata->gfpu.xsh_fxsave, &state->fpu,
   1917       1.13    maxv 		    sizeof(state->fpu));
   1918        1.1    maxv 
   1919        1.1    maxv 		fpustate = (struct fxsave *)cpudata->gfpu.xsh_fxsave;
   1920        1.1    maxv 		fpustate->fx_mxcsr_mask &= x86_fpu_mxcsr_mask;
   1921        1.1    maxv 		fpustate->fx_mxcsr &= fpustate->fx_mxcsr_mask;
   1922       1.16    maxv 
   1923       1.16    maxv 		if (svm_xcr0_mask != 0) {
   1924       1.16    maxv 			/* Reset XSTATE_BV, to force a reload. */
   1925       1.16    maxv 			cpudata->gfpu.xsh_xstate_bv = svm_xcr0_mask;
   1926       1.16    maxv 		}
   1927        1.1    maxv 	}
   1928       1.12    maxv 
   1929       1.12    maxv 	svm_vmcb_cache_update(vmcb, flags);
   1930       1.43    maxv 
   1931       1.43    maxv 	comm->state_wanted = 0;
   1932       1.43    maxv 	comm->state_cached |= flags;
   1933        1.1    maxv }
   1934        1.1    maxv 
   1935        1.1    maxv static void
   1936       1.43    maxv svm_vcpu_getstate(struct nvmm_cpu *vcpu)
   1937        1.1    maxv {
   1938       1.43    maxv 	struct nvmm_comm_page *comm = vcpu->comm;
   1939       1.43    maxv 	struct nvmm_x64_state *state = &comm->state;
   1940        1.1    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   1941        1.1    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   1942       1.43    maxv 	uint64_t flags;
   1943       1.43    maxv 
   1944       1.43    maxv 	flags = comm->state_wanted;
   1945        1.1    maxv 
   1946        1.1    maxv 	if (flags & NVMM_X64_STATE_SEGS) {
   1947       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_CS],
   1948        1.1    maxv 		    &vmcb->state.cs);
   1949       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_DS],
   1950        1.1    maxv 		    &vmcb->state.ds);
   1951       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_ES],
   1952        1.1    maxv 		    &vmcb->state.es);
   1953       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_FS],
   1954        1.1    maxv 		    &vmcb->state.fs);
   1955       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_GS],
   1956        1.1    maxv 		    &vmcb->state.gs);
   1957       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_SS],
   1958        1.1    maxv 		    &vmcb->state.ss);
   1959       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_GDT],
   1960        1.1    maxv 		    &vmcb->state.gdt);
   1961       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_IDT],
   1962        1.1    maxv 		    &vmcb->state.idt);
   1963       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_LDT],
   1964        1.1    maxv 		    &vmcb->state.ldt);
   1965       1.13    maxv 		svm_vcpu_getstate_seg(&state->segs[NVMM_X64_SEG_TR],
   1966        1.1    maxv 		    &vmcb->state.tr);
   1967       1.23    maxv 
   1968       1.23    maxv 		state->segs[NVMM_X64_SEG_SS].attrib.dpl = vmcb->state.cpl;
   1969        1.1    maxv 	}
   1970        1.1    maxv 
   1971       1.13    maxv 	CTASSERT(sizeof(cpudata->gprs) == sizeof(state->gprs));
   1972        1.1    maxv 	if (flags & NVMM_X64_STATE_GPRS) {
   1973       1.13    maxv 		memcpy(state->gprs, cpudata->gprs, sizeof(state->gprs));
   1974        1.1    maxv 
   1975       1.13    maxv 		state->gprs[NVMM_X64_GPR_RIP] = vmcb->state.rip;
   1976       1.13    maxv 		state->gprs[NVMM_X64_GPR_RSP] = vmcb->state.rsp;
   1977       1.13    maxv 		state->gprs[NVMM_X64_GPR_RAX] = vmcb->state.rax;
   1978       1.13    maxv 		state->gprs[NVMM_X64_GPR_RFLAGS] = vmcb->state.rflags;
   1979        1.1    maxv 	}
   1980        1.1    maxv 
   1981        1.1    maxv 	if (flags & NVMM_X64_STATE_CRS) {
   1982       1.13    maxv 		state->crs[NVMM_X64_CR_CR0] = vmcb->state.cr0;
   1983       1.13    maxv 		state->crs[NVMM_X64_CR_CR2] = vmcb->state.cr2;
   1984       1.13    maxv 		state->crs[NVMM_X64_CR_CR3] = vmcb->state.cr3;
   1985       1.13    maxv 		state->crs[NVMM_X64_CR_CR4] = vmcb->state.cr4;
   1986       1.13    maxv 		state->crs[NVMM_X64_CR_CR8] = __SHIFTOUT(vmcb->ctrl.v,
   1987        1.1    maxv 		    VMCB_CTRL_V_TPR);
   1988       1.13    maxv 		state->crs[NVMM_X64_CR_XCR0] = cpudata->gxcr0;
   1989        1.1    maxv 	}
   1990        1.1    maxv 
   1991       1.13    maxv 	CTASSERT(sizeof(cpudata->drs) == sizeof(state->drs));
   1992        1.1    maxv 	if (flags & NVMM_X64_STATE_DRS) {
   1993       1.13    maxv 		memcpy(state->drs, cpudata->drs, sizeof(state->drs));
   1994        1.1    maxv 
   1995       1.13    maxv 		state->drs[NVMM_X64_DR_DR6] = vmcb->state.dr6;
   1996       1.13    maxv 		state->drs[NVMM_X64_DR_DR7] = vmcb->state.dr7;
   1997        1.1    maxv 	}
   1998        1.1    maxv 
   1999        1.1    maxv 	if (flags & NVMM_X64_STATE_MSRS) {
   2000       1.13    maxv 		state->msrs[NVMM_X64_MSR_EFER] = vmcb->state.efer;
   2001       1.13    maxv 		state->msrs[NVMM_X64_MSR_STAR] = vmcb->state.star;
   2002       1.13    maxv 		state->msrs[NVMM_X64_MSR_LSTAR] = vmcb->state.lstar;
   2003       1.13    maxv 		state->msrs[NVMM_X64_MSR_CSTAR] = vmcb->state.cstar;
   2004       1.13    maxv 		state->msrs[NVMM_X64_MSR_SFMASK] = vmcb->state.sfmask;
   2005       1.13    maxv 		state->msrs[NVMM_X64_MSR_KERNELGSBASE] =
   2006        1.1    maxv 		    vmcb->state.kernelgsbase;
   2007       1.13    maxv 		state->msrs[NVMM_X64_MSR_SYSENTER_CS] =
   2008        1.1    maxv 		    vmcb->state.sysenter_cs;
   2009       1.13    maxv 		state->msrs[NVMM_X64_MSR_SYSENTER_ESP] =
   2010        1.1    maxv 		    vmcb->state.sysenter_esp;
   2011       1.13    maxv 		state->msrs[NVMM_X64_MSR_SYSENTER_EIP] =
   2012        1.1    maxv 		    vmcb->state.sysenter_eip;
   2013       1.13    maxv 		state->msrs[NVMM_X64_MSR_PAT] = vmcb->state.g_pat;
   2014       1.36    maxv 		state->msrs[NVMM_X64_MSR_TSC] = cpudata->gtsc;
   2015        1.1    maxv 
   2016        1.1    maxv 		/* Hide SVME. */
   2017       1.13    maxv 		state->msrs[NVMM_X64_MSR_EFER] &= ~EFER_SVME;
   2018        1.1    maxv 	}
   2019        1.1    maxv 
   2020       1.37    maxv 	if (flags & NVMM_X64_STATE_INTR) {
   2021       1.37    maxv 		state->intr.int_shadow =
   2022       1.10    maxv 		    (vmcb->ctrl.intr & VMCB_CTRL_INTR_SHADOW) != 0;
   2023       1.37    maxv 		state->intr.int_window_exiting = cpudata->int_window_exit;
   2024       1.37    maxv 		state->intr.nmi_window_exiting = cpudata->nmi_window_exit;
   2025       1.37    maxv 		state->intr.evt_pending = cpudata->evt_pending;
   2026        1.1    maxv 	}
   2027        1.1    maxv 
   2028       1.13    maxv 	CTASSERT(sizeof(cpudata->gfpu.xsh_fxsave) == sizeof(state->fpu));
   2029        1.1    maxv 	if (flags & NVMM_X64_STATE_FPU) {
   2030       1.13    maxv 		memcpy(&state->fpu, cpudata->gfpu.xsh_fxsave,
   2031       1.13    maxv 		    sizeof(state->fpu));
   2032        1.1    maxv 	}
   2033       1.43    maxv 
   2034       1.43    maxv 	comm->state_wanted = 0;
   2035       1.43    maxv 	comm->state_cached |= flags;
   2036       1.43    maxv }
   2037       1.43    maxv 
   2038       1.43    maxv static void
   2039       1.43    maxv svm_vcpu_state_provide(struct nvmm_cpu *vcpu, uint64_t flags)
   2040       1.43    maxv {
   2041       1.43    maxv 	vcpu->comm->state_wanted = flags;
   2042       1.43    maxv 	svm_vcpu_getstate(vcpu);
   2043       1.43    maxv }
   2044       1.43    maxv 
   2045       1.43    maxv static void
   2046       1.43    maxv svm_vcpu_state_commit(struct nvmm_cpu *vcpu)
   2047       1.43    maxv {
   2048       1.43    maxv 	vcpu->comm->state_wanted = vcpu->comm->state_commit;
   2049       1.43    maxv 	vcpu->comm->state_commit = 0;
   2050       1.43    maxv 	svm_vcpu_setstate(vcpu);
   2051        1.1    maxv }
   2052        1.1    maxv 
   2053        1.1    maxv /* -------------------------------------------------------------------------- */
   2054        1.1    maxv 
   2055        1.1    maxv static void
   2056       1.30    maxv svm_asid_alloc(struct nvmm_cpu *vcpu)
   2057       1.30    maxv {
   2058       1.30    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   2059       1.30    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   2060       1.30    maxv 	size_t i, oct, bit;
   2061       1.30    maxv 
   2062       1.30    maxv 	mutex_enter(&svm_asidlock);
   2063       1.30    maxv 
   2064       1.30    maxv 	for (i = 0; i < svm_maxasid; i++) {
   2065       1.30    maxv 		oct = i / 8;
   2066       1.30    maxv 		bit = i % 8;
   2067       1.30    maxv 
   2068       1.30    maxv 		if (svm_asidmap[oct] & __BIT(bit)) {
   2069       1.30    maxv 			continue;
   2070       1.30    maxv 		}
   2071       1.30    maxv 
   2072       1.30    maxv 		svm_asidmap[oct] |= __BIT(bit);
   2073       1.30    maxv 		vmcb->ctrl.guest_asid = i;
   2074       1.30    maxv 		mutex_exit(&svm_asidlock);
   2075       1.30    maxv 		return;
   2076       1.30    maxv 	}
   2077       1.30    maxv 
   2078       1.30    maxv 	/*
   2079       1.30    maxv 	 * No free ASID. Use the last one, which is shared and requires
   2080       1.30    maxv 	 * special TLB handling.
   2081       1.30    maxv 	 */
   2082       1.30    maxv 	cpudata->shared_asid = true;
   2083       1.30    maxv 	vmcb->ctrl.guest_asid = svm_maxasid - 1;
   2084       1.30    maxv 	mutex_exit(&svm_asidlock);
   2085       1.30    maxv }
   2086       1.30    maxv 
   2087       1.30    maxv static void
   2088       1.30    maxv svm_asid_free(struct nvmm_cpu *vcpu)
   2089       1.30    maxv {
   2090       1.30    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   2091       1.30    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   2092       1.30    maxv 	size_t oct, bit;
   2093       1.30    maxv 
   2094       1.30    maxv 	if (cpudata->shared_asid) {
   2095       1.30    maxv 		return;
   2096       1.30    maxv 	}
   2097       1.30    maxv 
   2098       1.30    maxv 	oct = vmcb->ctrl.guest_asid / 8;
   2099       1.30    maxv 	bit = vmcb->ctrl.guest_asid % 8;
   2100       1.30    maxv 
   2101       1.30    maxv 	mutex_enter(&svm_asidlock);
   2102       1.30    maxv 	svm_asidmap[oct] &= ~__BIT(bit);
   2103       1.30    maxv 	mutex_exit(&svm_asidlock);
   2104       1.30    maxv }
   2105       1.30    maxv 
   2106       1.30    maxv static void
   2107       1.30    maxv svm_vcpu_init(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
   2108       1.30    maxv {
   2109       1.30    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   2110       1.30    maxv 	struct vmcb *vmcb = cpudata->vmcb;
   2111       1.30    maxv 
   2112       1.30    maxv 	/* Allow reads/writes of Control Registers. */
   2113       1.30    maxv 	vmcb->ctrl.intercept_cr = 0;
   2114       1.30    maxv 
   2115       1.30    maxv 	/* Allow reads/writes of Debug Registers. */
   2116       1.30    maxv 	vmcb->ctrl.intercept_dr = 0;
   2117       1.30    maxv 
   2118       1.30    maxv 	/* Allow exceptions 0 to 31. */
   2119       1.30    maxv 	vmcb->ctrl.intercept_vec = 0;
   2120       1.30    maxv 
   2121       1.30    maxv 	/*
   2122       1.30    maxv 	 * Allow:
   2123       1.30    maxv 	 *  - SMI [smm interrupts]
   2124       1.30    maxv 	 *  - VINTR [virtual interrupts]
   2125       1.30    maxv 	 *  - CR0_SPEC [CR0 writes changing other fields than CR0.TS or CR0.MP]
   2126       1.30    maxv 	 *  - RIDTR [reads of IDTR]
   2127       1.30    maxv 	 *  - RGDTR [reads of GDTR]
   2128       1.30    maxv 	 *  - RLDTR [reads of LDTR]
   2129       1.30    maxv 	 *  - RTR [reads of TR]
   2130       1.30    maxv 	 *  - WIDTR [writes of IDTR]
   2131       1.30    maxv 	 *  - WGDTR [writes of GDTR]
   2132       1.30    maxv 	 *  - WLDTR [writes of LDTR]
   2133       1.30    maxv 	 *  - WTR [writes of TR]
   2134       1.30    maxv 	 *  - RDTSC [rdtsc instruction]
   2135       1.30    maxv 	 *  - PUSHF [pushf instruction]
   2136       1.30    maxv 	 *  - POPF [popf instruction]
   2137       1.30    maxv 	 *  - IRET [iret instruction]
   2138       1.30    maxv 	 *  - INTN [int $n instructions]
   2139       1.30    maxv 	 *  - PAUSE [pause instruction]
   2140       1.30    maxv 	 *  - INVLPG [invplg instruction]
   2141       1.30    maxv 	 *  - TASKSW [task switches]
   2142       1.30    maxv 	 *
   2143       1.30    maxv 	 * Intercept the rest below.
   2144       1.30    maxv 	 */
   2145       1.30    maxv 	vmcb->ctrl.intercept_misc1 =
   2146       1.30    maxv 	    VMCB_CTRL_INTERCEPT_INTR |
   2147       1.30    maxv 	    VMCB_CTRL_INTERCEPT_NMI |
   2148       1.30    maxv 	    VMCB_CTRL_INTERCEPT_INIT |
   2149       1.30    maxv 	    VMCB_CTRL_INTERCEPT_RDPMC |
   2150       1.30    maxv 	    VMCB_CTRL_INTERCEPT_CPUID |
   2151       1.30    maxv 	    VMCB_CTRL_INTERCEPT_RSM |
   2152  1.46.4.10  martin 	    VMCB_CTRL_INTERCEPT_INVD |
   2153       1.30    maxv 	    VMCB_CTRL_INTERCEPT_HLT |
   2154       1.30    maxv 	    VMCB_CTRL_INTERCEPT_INVLPGA |
   2155       1.30    maxv 	    VMCB_CTRL_INTERCEPT_IOIO_PROT |
   2156       1.30    maxv 	    VMCB_CTRL_INTERCEPT_MSR_PROT |
   2157       1.30    maxv 	    VMCB_CTRL_INTERCEPT_FERR_FREEZE |
   2158       1.30    maxv 	    VMCB_CTRL_INTERCEPT_SHUTDOWN;
   2159       1.30    maxv 
   2160       1.30    maxv 	/*
   2161       1.30    maxv 	 * Allow:
   2162       1.30    maxv 	 *  - ICEBP [icebp instruction]
   2163       1.30    maxv 	 *  - WBINVD [wbinvd instruction]
   2164       1.30    maxv 	 *  - WCR_SPEC(0..15) [writes of CR0-15, received after instruction]
   2165       1.30    maxv 	 *
   2166       1.30    maxv 	 * Intercept the rest below.
   2167       1.30    maxv 	 */
   2168       1.30    maxv 	vmcb->ctrl.intercept_misc2 =
   2169       1.30    maxv 	    VMCB_CTRL_INTERCEPT_VMRUN |
   2170       1.30    maxv 	    VMCB_CTRL_INTERCEPT_VMMCALL |
   2171       1.30    maxv 	    VMCB_CTRL_INTERCEPT_VMLOAD |
   2172       1.30    maxv 	    VMCB_CTRL_INTERCEPT_VMSAVE |
   2173       1.30    maxv 	    VMCB_CTRL_INTERCEPT_STGI |
   2174       1.30    maxv 	    VMCB_CTRL_INTERCEPT_CLGI |
   2175       1.30    maxv 	    VMCB_CTRL_INTERCEPT_SKINIT |
   2176       1.30    maxv 	    VMCB_CTRL_INTERCEPT_RDTSCP |
   2177       1.30    maxv 	    VMCB_CTRL_INTERCEPT_MONITOR |
   2178       1.30    maxv 	    VMCB_CTRL_INTERCEPT_MWAIT |
   2179   1.46.4.8  martin 	    VMCB_CTRL_INTERCEPT_XSETBV |
   2180   1.46.4.8  martin 	    VMCB_CTRL_INTERCEPT_RDPRU;
   2181   1.46.4.8  martin 
   2182   1.46.4.8  martin 	/*
   2183   1.46.4.8  martin 	 * Intercept everything.
   2184   1.46.4.8  martin 	 */
   2185   1.46.4.8  martin 	vmcb->ctrl.intercept_misc3 =
   2186   1.46.4.8  martin 	    VMCB_CTRL_INTERCEPT_INVLPGB_ALL |
   2187   1.46.4.8  martin 	    VMCB_CTRL_INTERCEPT_PCID |
   2188   1.46.4.8  martin 	    VMCB_CTRL_INTERCEPT_MCOMMIT |
   2189   1.46.4.8  martin 	    VMCB_CTRL_INTERCEPT_TLBSYNC;
   2190       1.30    maxv 
   2191       1.30    maxv 	/* Intercept all I/O accesses. */
   2192       1.30    maxv 	memset(cpudata->iobm, 0xFF, IOBM_SIZE);
   2193       1.30    maxv 	vmcb->ctrl.iopm_base_pa = cpudata->iobm_pa;
   2194       1.30    maxv 
   2195       1.30    maxv 	/* Allow direct access to certain MSRs. */
   2196       1.30    maxv 	memset(cpudata->msrbm, 0xFF, MSRBM_SIZE);
   2197       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_STAR, true, true);
   2198       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_LSTAR, true, true);
   2199       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_CSTAR, true, true);
   2200       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_SFMASK, true, true);
   2201       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_KERNELGSBASE, true, true);
   2202       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_CS, true, true);
   2203       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_ESP, true, true);
   2204       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_SYSENTER_EIP, true, true);
   2205       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_FSBASE, true, true);
   2206       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_GSBASE, true, true);
   2207       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_CR_PAT, true, true);
   2208       1.30    maxv 	svm_vcpu_msr_allow(cpudata->msrbm, MSR_TSC, true, false);
   2209       1.30    maxv 	vmcb->ctrl.msrpm_base_pa = cpudata->msrbm_pa;
   2210       1.30    maxv 
   2211       1.30    maxv 	/* Generate ASID. */
   2212       1.30    maxv 	svm_asid_alloc(vcpu);
   2213       1.30    maxv 
   2214       1.30    maxv 	/* Virtual TPR. */
   2215       1.30    maxv 	vmcb->ctrl.v = VMCB_CTRL_V_INTR_MASKING;
   2216       1.30    maxv 
   2217       1.30    maxv 	/* Enable Nested Paging. */
   2218       1.30    maxv 	vmcb->ctrl.enable1 = VMCB_CTRL_ENABLE_NP;
   2219       1.30    maxv 	vmcb->ctrl.n_cr3 = mach->vm->vm_map.pmap->pm_pdirpa[0];
   2220       1.30    maxv 
   2221       1.30    maxv 	/* Init XSAVE header. */
   2222       1.30    maxv 	cpudata->gfpu.xsh_xstate_bv = svm_xcr0_mask;
   2223       1.30    maxv 	cpudata->gfpu.xsh_xcomp_bv = 0;
   2224       1.30    maxv 
   2225       1.30    maxv 	/* These MSRs are static. */
   2226       1.30    maxv 	cpudata->star = rdmsr(MSR_STAR);
   2227       1.30    maxv 	cpudata->lstar = rdmsr(MSR_LSTAR);
   2228       1.30    maxv 	cpudata->cstar = rdmsr(MSR_CSTAR);
   2229       1.30    maxv 	cpudata->sfmask = rdmsr(MSR_SFMASK);
   2230       1.31    maxv 
   2231       1.31    maxv 	/* Install the RESET state. */
   2232       1.43    maxv 	memcpy(&vcpu->comm->state, &nvmm_x86_reset_state,
   2233       1.43    maxv 	    sizeof(nvmm_x86_reset_state));
   2234       1.43    maxv 	vcpu->comm->state_wanted = NVMM_X64_STATE_ALL;
   2235       1.43    maxv 	vcpu->comm->state_cached = 0;
   2236       1.43    maxv 	svm_vcpu_setstate(vcpu);
   2237       1.30    maxv }
   2238       1.30    maxv 
   2239       1.30    maxv static int
   2240       1.30    maxv svm_vcpu_create(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
   2241       1.30    maxv {
   2242       1.30    maxv 	struct svm_cpudata *cpudata;
   2243       1.30    maxv 	int error;
   2244       1.30    maxv 
   2245       1.30    maxv 	/* Allocate the SVM cpudata. */
   2246       1.30    maxv 	cpudata = (struct svm_cpudata *)uvm_km_alloc(kernel_map,
   2247       1.30    maxv 	    roundup(sizeof(*cpudata), PAGE_SIZE), 0,
   2248       1.30    maxv 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
   2249       1.30    maxv 	vcpu->cpudata = cpudata;
   2250       1.30    maxv 
   2251       1.30    maxv 	/* VMCB */
   2252       1.30    maxv 	error = svm_memalloc(&cpudata->vmcb_pa, (vaddr_t *)&cpudata->vmcb,
   2253       1.30    maxv 	    VMCB_NPAGES);
   2254       1.30    maxv 	if (error)
   2255       1.30    maxv 		goto error;
   2256       1.30    maxv 
   2257       1.30    maxv 	/* I/O Bitmap */
   2258       1.30    maxv 	error = svm_memalloc(&cpudata->iobm_pa, (vaddr_t *)&cpudata->iobm,
   2259       1.30    maxv 	    IOBM_NPAGES);
   2260       1.30    maxv 	if (error)
   2261       1.30    maxv 		goto error;
   2262       1.30    maxv 
   2263       1.30    maxv 	/* MSR Bitmap */
   2264       1.30    maxv 	error = svm_memalloc(&cpudata->msrbm_pa, (vaddr_t *)&cpudata->msrbm,
   2265       1.30    maxv 	    MSRBM_NPAGES);
   2266       1.30    maxv 	if (error)
   2267       1.30    maxv 		goto error;
   2268       1.30    maxv 
   2269       1.30    maxv 	/* Init the VCPU info. */
   2270       1.30    maxv 	svm_vcpu_init(mach, vcpu);
   2271       1.30    maxv 
   2272       1.30    maxv 	return 0;
   2273       1.30    maxv 
   2274       1.30    maxv error:
   2275       1.30    maxv 	if (cpudata->vmcb_pa) {
   2276       1.30    maxv 		svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb,
   2277       1.30    maxv 		    VMCB_NPAGES);
   2278       1.30    maxv 	}
   2279       1.30    maxv 	if (cpudata->iobm_pa) {
   2280       1.30    maxv 		svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm,
   2281       1.30    maxv 		    IOBM_NPAGES);
   2282       1.30    maxv 	}
   2283       1.30    maxv 	if (cpudata->msrbm_pa) {
   2284       1.30    maxv 		svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm,
   2285       1.30    maxv 		    MSRBM_NPAGES);
   2286       1.30    maxv 	}
   2287       1.30    maxv 	uvm_km_free(kernel_map, (vaddr_t)cpudata,
   2288       1.30    maxv 	    roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
   2289       1.30    maxv 	return error;
   2290       1.30    maxv }
   2291       1.30    maxv 
   2292       1.30    maxv static void
   2293       1.30    maxv svm_vcpu_destroy(struct nvmm_machine *mach, struct nvmm_cpu *vcpu)
   2294       1.30    maxv {
   2295       1.30    maxv 	struct svm_cpudata *cpudata = vcpu->cpudata;
   2296       1.30    maxv 
   2297       1.30    maxv 	svm_asid_free(vcpu);
   2298       1.30    maxv 
   2299       1.30    maxv 	svm_memfree(cpudata->vmcb_pa, (vaddr_t)cpudata->vmcb, VMCB_NPAGES);
   2300       1.30    maxv 	svm_memfree(cpudata->iobm_pa, (vaddr_t)cpudata->iobm, IOBM_NPAGES);
   2301       1.30    maxv 	svm_memfree(cpudata->msrbm_pa, (vaddr_t)cpudata->msrbm, MSRBM_NPAGES);
   2302       1.30    maxv 
   2303       1.30    maxv 	uvm_km_free(kernel_map, (vaddr_t)cpudata,
   2304       1.30    maxv 	    roundup(sizeof(*cpudata), PAGE_SIZE), UVM_KMF_WIRED);
   2305       1.30    maxv }
   2306       1.30    maxv 
   2307       1.30    maxv /* -------------------------------------------------------------------------- */
   2308       1.30    maxv 
   2309   1.46.4.2  martin static int
   2310   1.46.4.2  martin svm_vcpu_configure_cpuid(struct svm_cpudata *cpudata, void *data)
   2311   1.46.4.2  martin {
   2312   1.46.4.2  martin 	struct nvmm_vcpu_conf_cpuid *cpuid = data;
   2313   1.46.4.2  martin 	size_t i;
   2314   1.46.4.2  martin 
   2315   1.46.4.2  martin 	if (__predict_false(cpuid->mask && cpuid->exit)) {
   2316   1.46.4.2  martin 		return EINVAL;
   2317   1.46.4.2  martin 	}
   2318   1.46.4.2  martin 	if (__predict_false(cpuid->mask &&
   2319   1.46.4.2  martin 	    ((cpuid->u.mask.set.eax & cpuid->u.mask.del.eax) ||
   2320   1.46.4.2  martin 	     (cpuid->u.mask.set.ebx & cpuid->u.mask.del.ebx) ||
   2321   1.46.4.2  martin 	     (cpuid->u.mask.set.ecx & cpuid->u.mask.del.ecx) ||
   2322   1.46.4.2  martin 	     (cpuid->u.mask.set.edx & cpuid->u.mask.del.edx)))) {
   2323   1.46.4.2  martin 		return EINVAL;
   2324   1.46.4.2  martin 	}
   2325   1.46.4.2  martin 
   2326   1.46.4.2  martin 	/* If unset, delete, to restore the default behavior. */
   2327   1.46.4.2  martin 	if (!cpuid->mask && !cpuid->exit) {
   2328   1.46.4.2  martin 		for (i = 0; i < SVM_NCPUIDS; i++) {
   2329   1.46.4.2  martin 			if (!cpudata->cpuidpresent[i]) {
   2330   1.46.4.2  martin 				continue;
   2331   1.46.4.2  martin 			}
   2332   1.46.4.2  martin 			if (cpudata->cpuid[i].leaf == cpuid->leaf) {
   2333   1.46.4.2  martin 				cpudata->cpuidpresent[i] = false;
   2334   1.46.4.2  martin 			}
   2335   1.46.4.2  martin 		}
   2336   1.46.4.2  martin 		return 0;
   2337   1.46.4.2  martin 	}
   2338   1.46.4.2  martin 
   2339   1.46.4.2  martin 	/* If already here, replace. */
   2340   1.46.4.2  martin 	for (i = 0; i < SVM_NCPUIDS; i++) {
   2341   1.46.4.2  martin 		if (!cpudata->cpuidpresent[i]) {
   2342   1.46.4.2  martin 			continue;
   2343   1.46.4.2  martin 		}
   2344   1.46.4.2  martin 		if (cpudata->cpuid[i].leaf == cpuid->leaf) {
   2345   1.46.4.2  martin 			memcpy(&cpudata->cpuid[i], cpuid,
   2346   1.46.4.2  martin 			    sizeof(struct nvmm_vcpu_conf_cpuid));
   2347   1.46.4.2  martin 			return 0;
   2348   1.46.4.2  martin 		}
   2349   1.46.4.2  martin 	}
   2350   1.46.4.2  martin 
   2351   1.46.4.2  martin 	/* Not here, insert. */
   2352   1.46.4.2  martin 	for (i = 0; i < SVM_NCPUIDS; i++) {
   2353   1.46.4.2  martin 		if (!cpudata->cpuidpresent[i]) {
   2354   1.46.4.2  martin 			cpudata->cpuidpresent[i] = true;
   2355   1.46.4.2  martin 			memcpy(&cpudata->cpuid[i], cpuid,
   2356   1.46.4.2  martin 			    sizeof(struct nvmm_vcpu_conf_cpuid));
   2357   1.46.4.2  martin 			return 0;
   2358   1.46.4.2  martin 		}
   2359   1.46.4.2  martin 	}
   2360   1.46.4.2  martin 
   2361   1.46.4.2  martin 	return ENOBUFS;
   2362   1.46.4.2  martin }
   2363   1.46.4.2  martin 
   2364   1.46.4.2  martin static int
   2365   1.46.4.2  martin svm_vcpu_configure(struct nvmm_cpu *vcpu, uint64_t op, void *data)
   2366   1.46.4.2  martin {
   2367   1.46.4.2  martin 	struct svm_cpudata *cpudata = vcpu->cpudata;
   2368   1.46.4.2  martin 
   2369   1.46.4.2  martin 	switch (op) {
   2370   1.46.4.2  martin 	case NVMM_VCPU_CONF_MD(NVMM_VCPU_CONF_CPUID):
   2371   1.46.4.2  martin 		return svm_vcpu_configure_cpuid(cpudata, data);
   2372   1.46.4.2  martin 	default:
   2373   1.46.4.2  martin 		return EINVAL;
   2374   1.46.4.2  martin 	}
   2375   1.46.4.2  martin }
   2376   1.46.4.2  martin 
   2377   1.46.4.2  martin /* -------------------------------------------------------------------------- */
   2378   1.46.4.2  martin 
   2379       1.30    maxv static void
   2380        1.1    maxv svm_tlb_flush(struct pmap *pm)
   2381        1.1    maxv {
   2382        1.1    maxv 	struct nvmm_machine *mach = pm->pm_data;
   2383       1.29    maxv 	struct svm_machdata *machdata = mach->machdata;
   2384       1.29    maxv 
   2385       1.29    maxv 	atomic_inc_64(&machdata->mach_htlb_gen);
   2386        1.1    maxv 
   2387       1.29    maxv 	/* Generates IPIs, which cause #VMEXITs. */
   2388   1.46.4.1  martin 	pmap_tlb_shootdown(pmap_kernel(), -1, PTE_G, TLBSHOOT_UPDATE);
   2389        1.1    maxv }
   2390        1.1    maxv 
   2391        1.1    maxv static void
   2392        1.1    maxv svm_machine_create(struct nvmm_machine *mach)
   2393        1.1    maxv {
   2394       1.29    maxv 	struct svm_machdata *machdata;
   2395       1.29    maxv 
   2396        1.1    maxv 	/* Fill in pmap info. */
   2397        1.1    maxv 	mach->vm->vm_map.pmap->pm_data = (void *)mach;
   2398        1.1    maxv 	mach->vm->vm_map.pmap->pm_tlb_flush = svm_tlb_flush;
   2399        1.1    maxv 
   2400       1.29    maxv 	machdata = kmem_zalloc(sizeof(struct svm_machdata), KM_SLEEP);
   2401       1.29    maxv 	mach->machdata = machdata;
   2402       1.29    maxv 
   2403       1.29    maxv 	/* Start with an hTLB flush everywhere. */
   2404       1.29    maxv 	machdata->mach_htlb_gen = 1;
   2405        1.1    maxv }
   2406        1.1    maxv 
   2407        1.1    maxv static void
   2408        1.1    maxv svm_machine_destroy(struct nvmm_machine *mach)
   2409        1.1    maxv {
   2410        1.1    maxv 	kmem_free(mach->machdata, sizeof(struct svm_machdata));
   2411        1.1    maxv }
   2412        1.1    maxv 
   2413        1.1    maxv static int
   2414        1.1    maxv svm_machine_configure(struct nvmm_machine *mach, uint64_t op, void *data)
   2415        1.1    maxv {
   2416   1.46.4.2  martin 	panic("%s: impossible", __func__);
   2417        1.1    maxv }
   2418        1.1    maxv 
   2419        1.1    maxv /* -------------------------------------------------------------------------- */
   2420        1.1    maxv 
   2421        1.1    maxv static bool
   2422        1.1    maxv svm_ident(void)
   2423        1.1    maxv {
   2424        1.1    maxv 	u_int descs[4];
   2425        1.1    maxv 	uint64_t msr;
   2426        1.1    maxv 
   2427        1.1    maxv 	if (cpu_vendor != CPUVENDOR_AMD) {
   2428        1.1    maxv 		return false;
   2429        1.1    maxv 	}
   2430        1.1    maxv 	if (!(cpu_feature[3] & CPUID_SVM)) {
   2431   1.46.4.4  martin 		printf("NVMM: SVM not supported\n");
   2432        1.1    maxv 		return false;
   2433        1.1    maxv 	}
   2434        1.1    maxv 
   2435        1.1    maxv 	if (curcpu()->ci_max_ext_cpuid < 0x8000000a) {
   2436   1.46.4.4  martin 		printf("NVMM: CPUID leaf not available\n");
   2437        1.1    maxv 		return false;
   2438        1.1    maxv 	}
   2439        1.1    maxv 	x86_cpuid(0x8000000a, descs);
   2440        1.1    maxv 
   2441  1.46.4.11  martin 	/* Expect revision 1. */
   2442  1.46.4.11  martin 	if (__SHIFTOUT(descs[0], CPUID_AMD_SVM_REV) != 1) {
   2443  1.46.4.11  martin 		printf("NVMM: SVM revision not supported\n");
   2444  1.46.4.11  martin 		return false;
   2445  1.46.4.11  martin 	}
   2446  1.46.4.11  martin 
   2447        1.1    maxv 	/* Want Nested Paging. */
   2448        1.1    maxv 	if (!(descs[3] & CPUID_AMD_SVM_NP)) {
   2449   1.46.4.4  martin 		printf("NVMM: SVM-NP not supported\n");
   2450        1.1    maxv 		return false;
   2451        1.1    maxv 	}
   2452        1.1    maxv 
   2453        1.1    maxv 	/* Want nRIP. */
   2454        1.1    maxv 	if (!(descs[3] & CPUID_AMD_SVM_NRIPS)) {
   2455   1.46.4.4  martin 		printf("NVMM: SVM-NRIPS not supported\n");
   2456        1.1    maxv 		return false;
   2457        1.1    maxv 	}
   2458        1.1    maxv 
   2459        1.1    maxv 	svm_decode_assist = (descs[3] & CPUID_AMD_SVM_DecodeAssist) != 0;
   2460        1.1    maxv 
   2461        1.1    maxv 	msr = rdmsr(MSR_VMCR);
   2462        1.1    maxv 	if ((msr & VMCR_SVMED) && (msr & VMCR_LOCK)) {
   2463   1.46.4.4  martin 		printf("NVMM: SVM disabled in BIOS\n");
   2464        1.1    maxv 		return false;
   2465        1.1    maxv 	}
   2466        1.1    maxv 
   2467        1.1    maxv 	return true;
   2468        1.1    maxv }
   2469        1.1    maxv 
   2470        1.1    maxv static void
   2471        1.1    maxv svm_init_asid(uint32_t maxasid)
   2472        1.1    maxv {
   2473        1.1    maxv 	size_t i, j, allocsz;
   2474        1.1    maxv 
   2475        1.1    maxv 	mutex_init(&svm_asidlock, MUTEX_DEFAULT, IPL_NONE);
   2476        1.1    maxv 
   2477        1.1    maxv 	/* Arbitrarily limit. */
   2478        1.1    maxv 	maxasid = uimin(maxasid, 8192);
   2479        1.1    maxv 
   2480        1.1    maxv 	svm_maxasid = maxasid;
   2481        1.1    maxv 	allocsz = roundup(maxasid, 8) / 8;
   2482        1.1    maxv 	svm_asidmap = kmem_zalloc(allocsz, KM_SLEEP);
   2483        1.1    maxv 
   2484        1.1    maxv 	/* ASID 0 is reserved for the host. */
   2485        1.1    maxv 	svm_asidmap[0] |= __BIT(0);
   2486        1.1    maxv 
   2487        1.1    maxv 	/* ASID n-1 is special, we share it. */
   2488        1.1    maxv 	i = (maxasid - 1) / 8;
   2489        1.1    maxv 	j = (maxasid - 1) % 8;
   2490        1.1    maxv 	svm_asidmap[i] |= __BIT(j);
   2491        1.1    maxv }
   2492        1.1    maxv 
   2493        1.1    maxv static void
   2494        1.1    maxv svm_change_cpu(void *arg1, void *arg2)
   2495        1.1    maxv {
   2496   1.46.4.7  martin 	bool enable = arg1 != NULL;
   2497        1.1    maxv 	uint64_t msr;
   2498        1.1    maxv 
   2499        1.1    maxv 	msr = rdmsr(MSR_VMCR);
   2500        1.1    maxv 	if (msr & VMCR_SVMED) {
   2501        1.1    maxv 		wrmsr(MSR_VMCR, msr & ~VMCR_SVMED);
   2502        1.1    maxv 	}
   2503        1.1    maxv 
   2504        1.1    maxv 	if (!enable) {
   2505        1.1    maxv 		wrmsr(MSR_VM_HSAVE_PA, 0);
   2506        1.1    maxv 	}
   2507        1.1    maxv 
   2508        1.1    maxv 	msr = rdmsr(MSR_EFER);
   2509        1.1    maxv 	if (enable) {
   2510        1.1    maxv 		msr |= EFER_SVME;
   2511        1.1    maxv 	} else {
   2512        1.1    maxv 		msr &= ~EFER_SVME;
   2513        1.1    maxv 	}
   2514        1.1    maxv 	wrmsr(MSR_EFER, msr);
   2515        1.1    maxv 
   2516        1.1    maxv 	if (enable) {
   2517        1.1    maxv 		wrmsr(MSR_VM_HSAVE_PA, hsave[cpu_index(curcpu())].pa);
   2518        1.1    maxv 	}
   2519        1.1    maxv }
   2520        1.1    maxv 
   2521        1.1    maxv static void
   2522        1.1    maxv svm_init(void)
   2523        1.1    maxv {
   2524        1.1    maxv 	CPU_INFO_ITERATOR cii;
   2525        1.1    maxv 	struct cpu_info *ci;
   2526        1.1    maxv 	struct vm_page *pg;
   2527        1.1    maxv 	u_int descs[4];
   2528        1.1    maxv 	uint64_t xc;
   2529        1.1    maxv 
   2530        1.1    maxv 	x86_cpuid(0x8000000a, descs);
   2531        1.1    maxv 
   2532        1.1    maxv 	/* The guest TLB flush command. */
   2533        1.1    maxv 	if (descs[3] & CPUID_AMD_SVM_FlushByASID) {
   2534        1.1    maxv 		svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_GUEST;
   2535        1.1    maxv 	} else {
   2536        1.1    maxv 		svm_ctrl_tlb_flush = VMCB_CTRL_TLB_CTRL_FLUSH_ALL;
   2537        1.1    maxv 	}
   2538        1.1    maxv 
   2539        1.1    maxv 	/* Init the ASID. */
   2540        1.1    maxv 	svm_init_asid(descs[1]);
   2541        1.1    maxv 
   2542        1.1    maxv 	/* Init the XCR0 mask. */
   2543        1.1    maxv 	svm_xcr0_mask = SVM_XCR0_MASK_DEFAULT & x86_xsave_features;
   2544        1.1    maxv 
   2545   1.46.4.9  martin 	/* Init the max basic CPUID leaf. */
   2546   1.46.4.9  martin 	svm_cpuid_max_basic = uimin(cpuid_level, SVM_CPUID_MAX_BASIC);
   2547   1.46.4.9  martin 
   2548   1.46.4.9  martin 	/* Init the max extended CPUID leaf. */
   2549   1.46.4.9  martin 	x86_cpuid(0x80000000, descs);
   2550   1.46.4.9  martin 	svm_cpuid_max_extended = uimin(descs[0], SVM_CPUID_MAX_EXTENDED);
   2551   1.46.4.9  martin 
   2552        1.1    maxv 	memset(hsave, 0, sizeof(hsave));
   2553        1.1    maxv 	for (CPU_INFO_FOREACH(cii, ci)) {
   2554        1.1    maxv 		pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_ZERO);
   2555        1.1    maxv 		hsave[cpu_index(ci)].pa = VM_PAGE_TO_PHYS(pg);
   2556        1.1    maxv 	}
   2557        1.1    maxv 
   2558        1.1    maxv 	xc = xc_broadcast(0, svm_change_cpu, (void *)true, NULL);
   2559        1.1    maxv 	xc_wait(xc);
   2560        1.1    maxv }
   2561        1.1    maxv 
   2562        1.1    maxv static void
   2563        1.1    maxv svm_fini_asid(void)
   2564        1.1    maxv {
   2565        1.1    maxv 	size_t allocsz;
   2566        1.1    maxv 
   2567        1.1    maxv 	allocsz = roundup(svm_maxasid, 8) / 8;
   2568        1.1    maxv 	kmem_free(svm_asidmap, allocsz);
   2569        1.1    maxv 
   2570        1.1    maxv 	mutex_destroy(&svm_asidlock);
   2571        1.1    maxv }
   2572        1.1    maxv 
   2573        1.1    maxv static void
   2574        1.1    maxv svm_fini(void)
   2575        1.1    maxv {
   2576        1.1    maxv 	uint64_t xc;
   2577        1.1    maxv 	size_t i;
   2578        1.1    maxv 
   2579        1.1    maxv 	xc = xc_broadcast(0, svm_change_cpu, (void *)false, NULL);
   2580        1.1    maxv 	xc_wait(xc);
   2581        1.1    maxv 
   2582        1.1    maxv 	for (i = 0; i < MAXCPUS; i++) {
   2583        1.1    maxv 		if (hsave[i].pa != 0)
   2584        1.1    maxv 			uvm_pagefree(PHYS_TO_VM_PAGE(hsave[i].pa));
   2585        1.1    maxv 	}
   2586        1.1    maxv 
   2587        1.1    maxv 	svm_fini_asid();
   2588        1.1    maxv }
   2589        1.1    maxv 
   2590        1.1    maxv static void
   2591        1.1    maxv svm_capability(struct nvmm_capability *cap)
   2592        1.1    maxv {
   2593   1.46.4.2  martin 	cap->arch.mach_conf_support = 0;
   2594   1.46.4.2  martin 	cap->arch.vcpu_conf_support =
   2595   1.46.4.2  martin 	    NVMM_CAP_ARCH_VCPU_CONF_CPUID;
   2596       1.42    maxv 	cap->arch.xcr0_mask = svm_xcr0_mask;
   2597       1.42    maxv 	cap->arch.mxcsr_mask = x86_fpu_mxcsr_mask;
   2598       1.42    maxv 	cap->arch.conf_cpuid_maxops = SVM_NCPUIDS;
   2599        1.1    maxv }
   2600        1.1    maxv 
   2601        1.1    maxv const struct nvmm_impl nvmm_x86_svm = {
   2602   1.46.4.6  martin 	.name = "x86-svm",
   2603        1.1    maxv 	.ident = svm_ident,
   2604        1.1    maxv 	.init = svm_init,
   2605        1.1    maxv 	.fini = svm_fini,
   2606        1.1    maxv 	.capability = svm_capability,
   2607   1.46.4.2  martin 	.mach_conf_max = NVMM_X86_MACH_NCONF,
   2608   1.46.4.2  martin 	.mach_conf_sizes = NULL,
   2609   1.46.4.2  martin 	.vcpu_conf_max = NVMM_X86_VCPU_NCONF,
   2610   1.46.4.2  martin 	.vcpu_conf_sizes = svm_vcpu_conf_sizes,
   2611        1.1    maxv 	.state_size = sizeof(struct nvmm_x64_state),
   2612        1.1    maxv 	.machine_create = svm_machine_create,
   2613        1.1    maxv 	.machine_destroy = svm_machine_destroy,
   2614        1.1    maxv 	.machine_configure = svm_machine_configure,
   2615        1.1    maxv 	.vcpu_create = svm_vcpu_create,
   2616        1.1    maxv 	.vcpu_destroy = svm_vcpu_destroy,
   2617   1.46.4.2  martin 	.vcpu_configure = svm_vcpu_configure,
   2618        1.1    maxv 	.vcpu_setstate = svm_vcpu_setstate,
   2619        1.1    maxv 	.vcpu_getstate = svm_vcpu_getstate,
   2620        1.1    maxv 	.vcpu_inject = svm_vcpu_inject,
   2621        1.1    maxv 	.vcpu_run = svm_vcpu_run
   2622        1.1    maxv };
   2623