1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tools/testing/selftests/kvm/lib/x86_64/processor.c
4  *
5  * Copyright (C) 2018, Google LLC.
6  */
7 
8 #include "test_util.h"
9 #include "kvm_util.h"
10 #include "../kvm_util_internal.h"
11 #include "processor.h"
12 
13 #ifndef NUM_INTERRUPTS
14 #define NUM_INTERRUPTS 256
15 #endif
16 
17 #define DEFAULT_CODE_SELECTOR 0x8
18 #define DEFAULT_DATA_SELECTOR 0x10
19 
20 vm_vaddr_t exception_handlers;
21 
22 /* Virtual translation table structure declarations */
23 struct pageUpperEntry {
24 	uint64_t present:1;
25 	uint64_t writable:1;
26 	uint64_t user:1;
27 	uint64_t write_through:1;
28 	uint64_t cache_disable:1;
29 	uint64_t accessed:1;
30 	uint64_t ignored_06:1;
31 	uint64_t page_size:1;
32 	uint64_t ignored_11_08:4;
33 	uint64_t pfn:40;
34 	uint64_t ignored_62_52:11;
35 	uint64_t execute_disable:1;
36 };
37 
38 struct pageTableEntry {
39 	uint64_t present:1;
40 	uint64_t writable:1;
41 	uint64_t user:1;
42 	uint64_t write_through:1;
43 	uint64_t cache_disable:1;
44 	uint64_t accessed:1;
45 	uint64_t dirty:1;
46 	uint64_t reserved_07:1;
47 	uint64_t global:1;
48 	uint64_t ignored_11_09:3;
49 	uint64_t pfn:40;
50 	uint64_t ignored_62_52:11;
51 	uint64_t execute_disable:1;
52 };
53 
54 void regs_dump(FILE *stream, struct kvm_regs *regs,
55 	       uint8_t indent)
56 {
57 	fprintf(stream, "%*srax: 0x%.16llx rbx: 0x%.16llx "
58 		"rcx: 0x%.16llx rdx: 0x%.16llx\n",
59 		indent, "",
60 		regs->rax, regs->rbx, regs->rcx, regs->rdx);
61 	fprintf(stream, "%*srsi: 0x%.16llx rdi: 0x%.16llx "
62 		"rsp: 0x%.16llx rbp: 0x%.16llx\n",
63 		indent, "",
64 		regs->rsi, regs->rdi, regs->rsp, regs->rbp);
65 	fprintf(stream, "%*sr8:  0x%.16llx r9:  0x%.16llx "
66 		"r10: 0x%.16llx r11: 0x%.16llx\n",
67 		indent, "",
68 		regs->r8, regs->r9, regs->r10, regs->r11);
69 	fprintf(stream, "%*sr12: 0x%.16llx r13: 0x%.16llx "
70 		"r14: 0x%.16llx r15: 0x%.16llx\n",
71 		indent, "",
72 		regs->r12, regs->r13, regs->r14, regs->r15);
73 	fprintf(stream, "%*srip: 0x%.16llx rfl: 0x%.16llx\n",
74 		indent, "",
75 		regs->rip, regs->rflags);
76 }
77 
78 /*
79  * Segment Dump
80  *
81  * Input Args:
82  *   stream  - Output FILE stream
83  *   segment - KVM segment
84  *   indent  - Left margin indent amount
85  *
86  * Output Args: None
87  *
88  * Return: None
89  *
90  * Dumps the state of the KVM segment given by @segment, to the FILE stream
91  * given by @stream.
92  */
93 static void segment_dump(FILE *stream, struct kvm_segment *segment,
94 			 uint8_t indent)
95 {
96 	fprintf(stream, "%*sbase: 0x%.16llx limit: 0x%.8x "
97 		"selector: 0x%.4x type: 0x%.2x\n",
98 		indent, "", segment->base, segment->limit,
99 		segment->selector, segment->type);
100 	fprintf(stream, "%*spresent: 0x%.2x dpl: 0x%.2x "
101 		"db: 0x%.2x s: 0x%.2x l: 0x%.2x\n",
102 		indent, "", segment->present, segment->dpl,
103 		segment->db, segment->s, segment->l);
104 	fprintf(stream, "%*sg: 0x%.2x avl: 0x%.2x "
105 		"unusable: 0x%.2x padding: 0x%.2x\n",
106 		indent, "", segment->g, segment->avl,
107 		segment->unusable, segment->padding);
108 }
109 
110 /*
111  * dtable Dump
112  *
113  * Input Args:
114  *   stream - Output FILE stream
115  *   dtable - KVM dtable
116  *   indent - Left margin indent amount
117  *
118  * Output Args: None
119  *
120  * Return: None
121  *
122  * Dumps the state of the KVM dtable given by @dtable, to the FILE stream
123  * given by @stream.
124  */
125 static void dtable_dump(FILE *stream, struct kvm_dtable *dtable,
126 			uint8_t indent)
127 {
128 	fprintf(stream, "%*sbase: 0x%.16llx limit: 0x%.4x "
129 		"padding: 0x%.4x 0x%.4x 0x%.4x\n",
130 		indent, "", dtable->base, dtable->limit,
131 		dtable->padding[0], dtable->padding[1], dtable->padding[2]);
132 }
133 
134 void sregs_dump(FILE *stream, struct kvm_sregs *sregs,
135 		uint8_t indent)
136 {
137 	unsigned int i;
138 
139 	fprintf(stream, "%*scs:\n", indent, "");
140 	segment_dump(stream, &sregs->cs, indent + 2);
141 	fprintf(stream, "%*sds:\n", indent, "");
142 	segment_dump(stream, &sregs->ds, indent + 2);
143 	fprintf(stream, "%*ses:\n", indent, "");
144 	segment_dump(stream, &sregs->es, indent + 2);
145 	fprintf(stream, "%*sfs:\n", indent, "");
146 	segment_dump(stream, &sregs->fs, indent + 2);
147 	fprintf(stream, "%*sgs:\n", indent, "");
148 	segment_dump(stream, &sregs->gs, indent + 2);
149 	fprintf(stream, "%*sss:\n", indent, "");
150 	segment_dump(stream, &sregs->ss, indent + 2);
151 	fprintf(stream, "%*str:\n", indent, "");
152 	segment_dump(stream, &sregs->tr, indent + 2);
153 	fprintf(stream, "%*sldt:\n", indent, "");
154 	segment_dump(stream, &sregs->ldt, indent + 2);
155 
156 	fprintf(stream, "%*sgdt:\n", indent, "");
157 	dtable_dump(stream, &sregs->gdt, indent + 2);
158 	fprintf(stream, "%*sidt:\n", indent, "");
159 	dtable_dump(stream, &sregs->idt, indent + 2);
160 
161 	fprintf(stream, "%*scr0: 0x%.16llx cr2: 0x%.16llx "
162 		"cr3: 0x%.16llx cr4: 0x%.16llx\n",
163 		indent, "",
164 		sregs->cr0, sregs->cr2, sregs->cr3, sregs->cr4);
165 	fprintf(stream, "%*scr8: 0x%.16llx efer: 0x%.16llx "
166 		"apic_base: 0x%.16llx\n",
167 		indent, "",
168 		sregs->cr8, sregs->efer, sregs->apic_base);
169 
170 	fprintf(stream, "%*sinterrupt_bitmap:\n", indent, "");
171 	for (i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++) {
172 		fprintf(stream, "%*s%.16llx\n", indent + 2, "",
173 			sregs->interrupt_bitmap[i]);
174 	}
175 }
176 
177 void virt_pgd_alloc(struct kvm_vm *vm)
178 {
179 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
180 		"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
181 
182 	/* If needed, create page map l4 table. */
183 	if (!vm->pgd_created) {
184 		vm->pgd = vm_alloc_page_table(vm);
185 		vm->pgd_created = true;
186 	}
187 }
188 
189 static void *virt_get_pte(struct kvm_vm *vm, uint64_t pt_pfn, uint64_t vaddr,
190 			  int level)
191 {
192 	uint64_t *page_table = addr_gpa2hva(vm, pt_pfn << vm->page_shift);
193 	int index = vaddr >> (vm->page_shift + level * 9) & 0x1ffu;
194 
195 	return &page_table[index];
196 }
197 
198 static struct pageUpperEntry *virt_create_upper_pte(struct kvm_vm *vm,
199 						    uint64_t pt_pfn,
200 						    uint64_t vaddr,
201 						    uint64_t paddr,
202 						    int level,
203 						    enum x86_page_size page_size)
204 {
205 	struct pageUpperEntry *pte = virt_get_pte(vm, pt_pfn, vaddr, level);
206 
207 	if (!pte->present) {
208 		pte->writable = true;
209 		pte->present = true;
210 		pte->page_size = (level == page_size);
211 		if (pte->page_size)
212 			pte->pfn = paddr >> vm->page_shift;
213 		else
214 			pte->pfn = vm_alloc_page_table(vm) >> vm->page_shift;
215 	} else {
216 		/*
217 		 * Entry already present.  Assert that the caller doesn't want
218 		 * a hugepage at this level, and that there isn't a hugepage at
219 		 * this level.
220 		 */
221 		TEST_ASSERT(level != page_size,
222 			    "Cannot create hugepage at level: %u, vaddr: 0x%lx\n",
223 			    page_size, vaddr);
224 		TEST_ASSERT(!pte->page_size,
225 			    "Cannot create page table at level: %u, vaddr: 0x%lx\n",
226 			    level, vaddr);
227 	}
228 	return pte;
229 }
230 
231 void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
232 		   enum x86_page_size page_size)
233 {
234 	const uint64_t pg_size = 1ull << ((page_size * 9) + 12);
235 	struct pageUpperEntry *pml4e, *pdpe, *pde;
236 	struct pageTableEntry *pte;
237 
238 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K,
239 		    "Unknown or unsupported guest mode, mode: 0x%x", vm->mode);
240 
241 	TEST_ASSERT((vaddr % pg_size) == 0,
242 		    "Virtual address not aligned,\n"
243 		    "vaddr: 0x%lx page size: 0x%lx", vaddr, pg_size);
244 	TEST_ASSERT(sparsebit_is_set(vm->vpages_valid, (vaddr >> vm->page_shift)),
245 		    "Invalid virtual address, vaddr: 0x%lx", vaddr);
246 	TEST_ASSERT((paddr % pg_size) == 0,
247 		    "Physical address not aligned,\n"
248 		    "  paddr: 0x%lx page size: 0x%lx", paddr, pg_size);
249 	TEST_ASSERT((paddr >> vm->page_shift) <= vm->max_gfn,
250 		    "Physical address beyond maximum supported,\n"
251 		    "  paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x",
252 		    paddr, vm->max_gfn, vm->page_size);
253 
254 	/*
255 	 * Allocate upper level page tables, if not already present.  Return
256 	 * early if a hugepage was created.
257 	 */
258 	pml4e = virt_create_upper_pte(vm, vm->pgd >> vm->page_shift,
259 				      vaddr, paddr, 3, page_size);
260 	if (pml4e->page_size)
261 		return;
262 
263 	pdpe = virt_create_upper_pte(vm, pml4e->pfn, vaddr, paddr, 2, page_size);
264 	if (pdpe->page_size)
265 		return;
266 
267 	pde = virt_create_upper_pte(vm, pdpe->pfn, vaddr, paddr, 1, page_size);
268 	if (pde->page_size)
269 		return;
270 
271 	/* Fill in page table entry. */
272 	pte = virt_get_pte(vm, pde->pfn, vaddr, 0);
273 	TEST_ASSERT(!pte->present,
274 		    "PTE already present for 4k page at vaddr: 0x%lx\n", vaddr);
275 	pte->pfn = paddr >> vm->page_shift;
276 	pte->writable = true;
277 	pte->present = 1;
278 }
279 
280 void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr)
281 {
282 	__virt_pg_map(vm, vaddr, paddr, X86_PAGE_SIZE_4K);
283 }
284 
285 static struct pageTableEntry *_vm_get_page_table_entry(struct kvm_vm *vm, int vcpuid,
286 						       uint64_t vaddr)
287 {
288 	uint16_t index[4];
289 	struct pageUpperEntry *pml4e, *pdpe, *pde;
290 	struct pageTableEntry *pte;
291 	struct kvm_cpuid_entry2 *entry;
292 	struct kvm_sregs sregs;
293 	int max_phy_addr;
294 	/* Set the bottom 52 bits. */
295 	uint64_t rsvd_mask = 0x000fffffffffffff;
296 
297 	entry = kvm_get_supported_cpuid_index(0x80000008, 0);
298 	max_phy_addr = entry->eax & 0x000000ff;
299 	/* Clear the bottom bits of the reserved mask. */
300 	rsvd_mask = (rsvd_mask >> max_phy_addr) << max_phy_addr;
301 
302 	/*
303 	 * SDM vol 3, fig 4-11 "Formats of CR3 and Paging-Structure Entries
304 	 * with 4-Level Paging and 5-Level Paging".
305 	 * If IA32_EFER.NXE = 0 and the P flag of a paging-structure entry is 1,
306 	 * the XD flag (bit 63) is reserved.
307 	 */
308 	vcpu_sregs_get(vm, vcpuid, &sregs);
309 	if ((sregs.efer & EFER_NX) == 0) {
310 		rsvd_mask |= (1ull << 63);
311 	}
312 
313 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
314 		"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
315 	TEST_ASSERT(sparsebit_is_set(vm->vpages_valid,
316 		(vaddr >> vm->page_shift)),
317 		"Invalid virtual address, vaddr: 0x%lx",
318 		vaddr);
319 	/*
320 	 * Based on the mode check above there are 48 bits in the vaddr, so
321 	 * shift 16 to sign extend the last bit (bit-47),
322 	 */
323 	TEST_ASSERT(vaddr == (((int64_t)vaddr << 16) >> 16),
324 		"Canonical check failed.  The virtual address is invalid.");
325 
326 	index[0] = (vaddr >> 12) & 0x1ffu;
327 	index[1] = (vaddr >> 21) & 0x1ffu;
328 	index[2] = (vaddr >> 30) & 0x1ffu;
329 	index[3] = (vaddr >> 39) & 0x1ffu;
330 
331 	pml4e = addr_gpa2hva(vm, vm->pgd);
332 	TEST_ASSERT(pml4e[index[3]].present,
333 		"Expected pml4e to be present for gva: 0x%08lx", vaddr);
334 	TEST_ASSERT((*(uint64_t*)(&pml4e[index[3]]) &
335 		(rsvd_mask | (1ull << 7))) == 0,
336 		"Unexpected reserved bits set.");
337 
338 	pdpe = addr_gpa2hva(vm, pml4e[index[3]].pfn * vm->page_size);
339 	TEST_ASSERT(pdpe[index[2]].present,
340 		"Expected pdpe to be present for gva: 0x%08lx", vaddr);
341 	TEST_ASSERT(pdpe[index[2]].page_size == 0,
342 		"Expected pdpe to map a pde not a 1-GByte page.");
343 	TEST_ASSERT((*(uint64_t*)(&pdpe[index[2]]) & rsvd_mask) == 0,
344 		"Unexpected reserved bits set.");
345 
346 	pde = addr_gpa2hva(vm, pdpe[index[2]].pfn * vm->page_size);
347 	TEST_ASSERT(pde[index[1]].present,
348 		"Expected pde to be present for gva: 0x%08lx", vaddr);
349 	TEST_ASSERT(pde[index[1]].page_size == 0,
350 		"Expected pde to map a pte not a 2-MByte page.");
351 	TEST_ASSERT((*(uint64_t*)(&pde[index[1]]) & rsvd_mask) == 0,
352 		"Unexpected reserved bits set.");
353 
354 	pte = addr_gpa2hva(vm, pde[index[1]].pfn * vm->page_size);
355 	TEST_ASSERT(pte[index[0]].present,
356 		"Expected pte to be present for gva: 0x%08lx", vaddr);
357 
358 	return &pte[index[0]];
359 }
360 
361 uint64_t vm_get_page_table_entry(struct kvm_vm *vm, int vcpuid, uint64_t vaddr)
362 {
363 	struct pageTableEntry *pte = _vm_get_page_table_entry(vm, vcpuid, vaddr);
364 
365 	return *(uint64_t *)pte;
366 }
367 
368 void vm_set_page_table_entry(struct kvm_vm *vm, int vcpuid, uint64_t vaddr,
369 			     uint64_t pte)
370 {
371 	struct pageTableEntry *new_pte = _vm_get_page_table_entry(vm, vcpuid,
372 								  vaddr);
373 
374 	*(uint64_t *)new_pte = pte;
375 }
376 
377 void virt_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
378 {
379 	struct pageUpperEntry *pml4e, *pml4e_start;
380 	struct pageUpperEntry *pdpe, *pdpe_start;
381 	struct pageUpperEntry *pde, *pde_start;
382 	struct pageTableEntry *pte, *pte_start;
383 
384 	if (!vm->pgd_created)
385 		return;
386 
387 	fprintf(stream, "%*s                                          "
388 		"                no\n", indent, "");
389 	fprintf(stream, "%*s      index hvaddr         gpaddr         "
390 		"addr         w exec dirty\n",
391 		indent, "");
392 	pml4e_start = (struct pageUpperEntry *) addr_gpa2hva(vm, vm->pgd);
393 	for (uint16_t n1 = 0; n1 <= 0x1ffu; n1++) {
394 		pml4e = &pml4e_start[n1];
395 		if (!pml4e->present)
396 			continue;
397 		fprintf(stream, "%*spml4e 0x%-3zx %p 0x%-12lx 0x%-10lx %u "
398 			" %u\n",
399 			indent, "",
400 			pml4e - pml4e_start, pml4e,
401 			addr_hva2gpa(vm, pml4e), (uint64_t) pml4e->pfn,
402 			pml4e->writable, pml4e->execute_disable);
403 
404 		pdpe_start = addr_gpa2hva(vm, pml4e->pfn * vm->page_size);
405 		for (uint16_t n2 = 0; n2 <= 0x1ffu; n2++) {
406 			pdpe = &pdpe_start[n2];
407 			if (!pdpe->present)
408 				continue;
409 			fprintf(stream, "%*spdpe  0x%-3zx %p 0x%-12lx 0x%-10lx "
410 				"%u  %u\n",
411 				indent, "",
412 				pdpe - pdpe_start, pdpe,
413 				addr_hva2gpa(vm, pdpe),
414 				(uint64_t) pdpe->pfn, pdpe->writable,
415 				pdpe->execute_disable);
416 
417 			pde_start = addr_gpa2hva(vm, pdpe->pfn * vm->page_size);
418 			for (uint16_t n3 = 0; n3 <= 0x1ffu; n3++) {
419 				pde = &pde_start[n3];
420 				if (!pde->present)
421 					continue;
422 				fprintf(stream, "%*spde   0x%-3zx %p "
423 					"0x%-12lx 0x%-10lx %u  %u\n",
424 					indent, "", pde - pde_start, pde,
425 					addr_hva2gpa(vm, pde),
426 					(uint64_t) pde->pfn, pde->writable,
427 					pde->execute_disable);
428 
429 				pte_start = addr_gpa2hva(vm, pde->pfn * vm->page_size);
430 				for (uint16_t n4 = 0; n4 <= 0x1ffu; n4++) {
431 					pte = &pte_start[n4];
432 					if (!pte->present)
433 						continue;
434 					fprintf(stream, "%*spte   0x%-3zx %p "
435 						"0x%-12lx 0x%-10lx %u  %u "
436 						"    %u    0x%-10lx\n",
437 						indent, "",
438 						pte - pte_start, pte,
439 						addr_hva2gpa(vm, pte),
440 						(uint64_t) pte->pfn,
441 						pte->writable,
442 						pte->execute_disable,
443 						pte->dirty,
444 						((uint64_t) n1 << 27)
445 							| ((uint64_t) n2 << 18)
446 							| ((uint64_t) n3 << 9)
447 							| ((uint64_t) n4));
448 				}
449 			}
450 		}
451 	}
452 }
453 
454 /*
455  * Set Unusable Segment
456  *
457  * Input Args: None
458  *
459  * Output Args:
460  *   segp - Pointer to segment register
461  *
462  * Return: None
463  *
464  * Sets the segment register pointed to by @segp to an unusable state.
465  */
466 static void kvm_seg_set_unusable(struct kvm_segment *segp)
467 {
468 	memset(segp, 0, sizeof(*segp));
469 	segp->unusable = true;
470 }
471 
472 static void kvm_seg_fill_gdt_64bit(struct kvm_vm *vm, struct kvm_segment *segp)
473 {
474 	void *gdt = addr_gva2hva(vm, vm->gdt);
475 	struct desc64 *desc = gdt + (segp->selector >> 3) * 8;
476 
477 	desc->limit0 = segp->limit & 0xFFFF;
478 	desc->base0 = segp->base & 0xFFFF;
479 	desc->base1 = segp->base >> 16;
480 	desc->type = segp->type;
481 	desc->s = segp->s;
482 	desc->dpl = segp->dpl;
483 	desc->p = segp->present;
484 	desc->limit1 = segp->limit >> 16;
485 	desc->avl = segp->avl;
486 	desc->l = segp->l;
487 	desc->db = segp->db;
488 	desc->g = segp->g;
489 	desc->base2 = segp->base >> 24;
490 	if (!segp->s)
491 		desc->base3 = segp->base >> 32;
492 }
493 
494 
495 /*
496  * Set Long Mode Flat Kernel Code Segment
497  *
498  * Input Args:
499  *   vm - VM whose GDT is being filled, or NULL to only write segp
500  *   selector - selector value
501  *
502  * Output Args:
503  *   segp - Pointer to KVM segment
504  *
505  * Return: None
506  *
507  * Sets up the KVM segment pointed to by @segp, to be a code segment
508  * with the selector value given by @selector.
509  */
510 static void kvm_seg_set_kernel_code_64bit(struct kvm_vm *vm, uint16_t selector,
511 	struct kvm_segment *segp)
512 {
513 	memset(segp, 0, sizeof(*segp));
514 	segp->selector = selector;
515 	segp->limit = 0xFFFFFFFFu;
516 	segp->s = 0x1; /* kTypeCodeData */
517 	segp->type = 0x08 | 0x01 | 0x02; /* kFlagCode | kFlagCodeAccessed
518 					  * | kFlagCodeReadable
519 					  */
520 	segp->g = true;
521 	segp->l = true;
522 	segp->present = 1;
523 	if (vm)
524 		kvm_seg_fill_gdt_64bit(vm, segp);
525 }
526 
527 /*
528  * Set Long Mode Flat Kernel Data Segment
529  *
530  * Input Args:
531  *   vm - VM whose GDT is being filled, or NULL to only write segp
532  *   selector - selector value
533  *
534  * Output Args:
535  *   segp - Pointer to KVM segment
536  *
537  * Return: None
538  *
539  * Sets up the KVM segment pointed to by @segp, to be a data segment
540  * with the selector value given by @selector.
541  */
542 static void kvm_seg_set_kernel_data_64bit(struct kvm_vm *vm, uint16_t selector,
543 	struct kvm_segment *segp)
544 {
545 	memset(segp, 0, sizeof(*segp));
546 	segp->selector = selector;
547 	segp->limit = 0xFFFFFFFFu;
548 	segp->s = 0x1; /* kTypeCodeData */
549 	segp->type = 0x00 | 0x01 | 0x02; /* kFlagData | kFlagDataAccessed
550 					  * | kFlagDataWritable
551 					  */
552 	segp->g = true;
553 	segp->present = true;
554 	if (vm)
555 		kvm_seg_fill_gdt_64bit(vm, segp);
556 }
557 
558 vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)
559 {
560 	uint16_t index[4];
561 	struct pageUpperEntry *pml4e, *pdpe, *pde;
562 	struct pageTableEntry *pte;
563 
564 	TEST_ASSERT(vm->mode == VM_MODE_PXXV48_4K, "Attempt to use "
565 		"unknown or unsupported guest mode, mode: 0x%x", vm->mode);
566 
567 	index[0] = (gva >> 12) & 0x1ffu;
568 	index[1] = (gva >> 21) & 0x1ffu;
569 	index[2] = (gva >> 30) & 0x1ffu;
570 	index[3] = (gva >> 39) & 0x1ffu;
571 
572 	if (!vm->pgd_created)
573 		goto unmapped_gva;
574 	pml4e = addr_gpa2hva(vm, vm->pgd);
575 	if (!pml4e[index[3]].present)
576 		goto unmapped_gva;
577 
578 	pdpe = addr_gpa2hva(vm, pml4e[index[3]].pfn * vm->page_size);
579 	if (!pdpe[index[2]].present)
580 		goto unmapped_gva;
581 
582 	pde = addr_gpa2hva(vm, pdpe[index[2]].pfn * vm->page_size);
583 	if (!pde[index[1]].present)
584 		goto unmapped_gva;
585 
586 	pte = addr_gpa2hva(vm, pde[index[1]].pfn * vm->page_size);
587 	if (!pte[index[0]].present)
588 		goto unmapped_gva;
589 
590 	return (pte[index[0]].pfn * vm->page_size) + (gva & 0xfffu);
591 
592 unmapped_gva:
593 	TEST_FAIL("No mapping for vm virtual address, gva: 0x%lx", gva);
594 	exit(EXIT_FAILURE);
595 }
596 
597 static void kvm_setup_gdt(struct kvm_vm *vm, struct kvm_dtable *dt)
598 {
599 	if (!vm->gdt)
600 		vm->gdt = vm_vaddr_alloc_page(vm);
601 
602 	dt->base = vm->gdt;
603 	dt->limit = getpagesize();
604 }
605 
606 static void kvm_setup_tss_64bit(struct kvm_vm *vm, struct kvm_segment *segp,
607 				int selector)
608 {
609 	if (!vm->tss)
610 		vm->tss = vm_vaddr_alloc_page(vm);
611 
612 	memset(segp, 0, sizeof(*segp));
613 	segp->base = vm->tss;
614 	segp->limit = 0x67;
615 	segp->selector = selector;
616 	segp->type = 0xb;
617 	segp->present = 1;
618 	kvm_seg_fill_gdt_64bit(vm, segp);
619 }
620 
621 static void vcpu_setup(struct kvm_vm *vm, int vcpuid)
622 {
623 	struct kvm_sregs sregs;
624 
625 	/* Set mode specific system register values. */
626 	vcpu_sregs_get(vm, vcpuid, &sregs);
627 
628 	sregs.idt.limit = 0;
629 
630 	kvm_setup_gdt(vm, &sregs.gdt);
631 
632 	switch (vm->mode) {
633 	case VM_MODE_PXXV48_4K:
634 		sregs.cr0 = X86_CR0_PE | X86_CR0_NE | X86_CR0_PG;
635 		sregs.cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR;
636 		sregs.efer |= (EFER_LME | EFER_LMA | EFER_NX);
637 
638 		kvm_seg_set_unusable(&sregs.ldt);
639 		kvm_seg_set_kernel_code_64bit(vm, DEFAULT_CODE_SELECTOR, &sregs.cs);
640 		kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs.ds);
641 		kvm_seg_set_kernel_data_64bit(vm, DEFAULT_DATA_SELECTOR, &sregs.es);
642 		kvm_setup_tss_64bit(vm, &sregs.tr, 0x18);
643 		break;
644 
645 	default:
646 		TEST_FAIL("Unknown guest mode, mode: 0x%x", vm->mode);
647 	}
648 
649 	sregs.cr3 = vm->pgd;
650 	vcpu_sregs_set(vm, vcpuid, &sregs);
651 }
652 
653 #define CPUID_XFD_BIT (1 << 4)
654 static bool is_xfd_supported(void)
655 {
656 	int eax, ebx, ecx, edx;
657 	const int leaf = 0xd, subleaf = 0x1;
658 
659 	__asm__ __volatile__(
660 		"cpuid"
661 		: /* output */ "=a"(eax), "=b"(ebx),
662 		  "=c"(ecx), "=d"(edx)
663 		: /* input */ "0"(leaf), "2"(subleaf));
664 
665 	return !!(eax & CPUID_XFD_BIT);
666 }
667 
668 void vm_xsave_req_perm(void)
669 {
670 	unsigned long bitmask;
671 	long rc;
672 
673 	if (!is_xfd_supported())
674 		return;
675 
676 	rc = syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_GUEST_PERM,
677 		     XSTATE_XTILE_DATA_BIT);
678 	/*
679 	 * The older kernel version(<5.15) can't support
680 	 * ARCH_REQ_XCOMP_GUEST_PERM and directly return.
681 	 */
682 	if (rc)
683 		return;
684 
685 	rc = syscall(SYS_arch_prctl, ARCH_GET_XCOMP_GUEST_PERM, &bitmask);
686 	TEST_ASSERT(rc == 0, "prctl(ARCH_GET_XCOMP_GUEST_PERM) error: %ld", rc);
687 	TEST_ASSERT(bitmask & XFEATURE_XTILE_MASK,
688 		    "prctl(ARCH_REQ_XCOMP_GUEST_PERM) failure bitmask=0x%lx",
689 		    bitmask);
690 }
691 
692 void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
693 {
694 	struct kvm_mp_state mp_state;
695 	struct kvm_regs regs;
696 	vm_vaddr_t stack_vaddr;
697 	stack_vaddr = vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(),
698 				     DEFAULT_GUEST_STACK_VADDR_MIN);
699 
700 	/* Create VCPU */
701 	vm_vcpu_add(vm, vcpuid);
702 	vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
703 	vcpu_setup(vm, vcpuid);
704 
705 	/* Setup guest general purpose registers */
706 	vcpu_regs_get(vm, vcpuid, &regs);
707 	regs.rflags = regs.rflags | 0x2;
708 	regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize());
709 	regs.rip = (unsigned long) guest_code;
710 	vcpu_regs_set(vm, vcpuid, &regs);
711 
712 	/* Setup the MP state */
713 	mp_state.mp_state = 0;
714 	vcpu_set_mp_state(vm, vcpuid, &mp_state);
715 }
716 
717 /*
718  * Allocate an instance of struct kvm_cpuid2
719  *
720  * Input Args: None
721  *
722  * Output Args: None
723  *
724  * Return: A pointer to the allocated struct. The caller is responsible
725  * for freeing this struct.
726  *
727  * Since kvm_cpuid2 uses a 0-length array to allow a the size of the
728  * array to be decided at allocation time, allocation is slightly
729  * complicated. This function uses a reasonable default length for
730  * the array and performs the appropriate allocation.
731  */
732 static struct kvm_cpuid2 *allocate_kvm_cpuid2(void)
733 {
734 	struct kvm_cpuid2 *cpuid;
735 	int nent = 100;
736 	size_t size;
737 
738 	size = sizeof(*cpuid);
739 	size += nent * sizeof(struct kvm_cpuid_entry2);
740 	cpuid = malloc(size);
741 	if (!cpuid) {
742 		perror("malloc");
743 		abort();
744 	}
745 
746 	cpuid->nent = nent;
747 
748 	return cpuid;
749 }
750 
751 /*
752  * KVM Supported CPUID Get
753  *
754  * Input Args: None
755  *
756  * Output Args:
757  *
758  * Return: The supported KVM CPUID
759  *
760  * Get the guest CPUID supported by KVM.
761  */
762 struct kvm_cpuid2 *kvm_get_supported_cpuid(void)
763 {
764 	static struct kvm_cpuid2 *cpuid;
765 	int ret;
766 	int kvm_fd;
767 
768 	if (cpuid)
769 		return cpuid;
770 
771 	cpuid = allocate_kvm_cpuid2();
772 	kvm_fd = open_kvm_dev_path_or_exit();
773 
774 	ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_CPUID, cpuid);
775 	TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n",
776 		    ret, errno);
777 
778 	close(kvm_fd);
779 	return cpuid;
780 }
781 
782 /*
783  * KVM Get MSR
784  *
785  * Input Args:
786  *   msr_index - Index of MSR
787  *
788  * Output Args: None
789  *
790  * Return: On success, value of the MSR. On failure a TEST_ASSERT is produced.
791  *
792  * Get value of MSR for VCPU.
793  */
794 uint64_t kvm_get_feature_msr(uint64_t msr_index)
795 {
796 	struct {
797 		struct kvm_msrs header;
798 		struct kvm_msr_entry entry;
799 	} buffer = {};
800 	int r, kvm_fd;
801 
802 	buffer.header.nmsrs = 1;
803 	buffer.entry.index = msr_index;
804 	kvm_fd = open_kvm_dev_path_or_exit();
805 
806 	r = ioctl(kvm_fd, KVM_GET_MSRS, &buffer.header);
807 	TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n"
808 		"  rc: %i errno: %i", r, errno);
809 
810 	close(kvm_fd);
811 	return buffer.entry.data;
812 }
813 
814 /*
815  * VM VCPU CPUID Set
816  *
817  * Input Args:
818  *   vm - Virtual Machine
819  *   vcpuid - VCPU id
820  *
821  * Output Args: None
822  *
823  * Return: KVM CPUID (KVM_GET_CPUID2)
824  *
825  * Set the VCPU's CPUID.
826  */
827 struct kvm_cpuid2 *vcpu_get_cpuid(struct kvm_vm *vm, uint32_t vcpuid)
828 {
829 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
830 	struct kvm_cpuid2 *cpuid;
831 	int max_ent;
832 	int rc = -1;
833 
834 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
835 
836 	cpuid = allocate_kvm_cpuid2();
837 	max_ent = cpuid->nent;
838 
839 	for (cpuid->nent = 1; cpuid->nent <= max_ent; cpuid->nent++) {
840 		rc = ioctl(vcpu->fd, KVM_GET_CPUID2, cpuid);
841 		if (!rc)
842 			break;
843 
844 		TEST_ASSERT(rc == -1 && errno == E2BIG,
845 			    "KVM_GET_CPUID2 should either succeed or give E2BIG: %d %d",
846 			    rc, errno);
847 	}
848 
849 	TEST_ASSERT(rc == 0, "KVM_GET_CPUID2 failed, rc: %i errno: %i",
850 		    rc, errno);
851 
852 	return cpuid;
853 }
854 
855 
856 
857 /*
858  * Locate a cpuid entry.
859  *
860  * Input Args:
861  *   function: The function of the cpuid entry to find.
862  *   index: The index of the cpuid entry.
863  *
864  * Output Args: None
865  *
866  * Return: A pointer to the cpuid entry. Never returns NULL.
867  */
868 struct kvm_cpuid_entry2 *
869 kvm_get_supported_cpuid_index(uint32_t function, uint32_t index)
870 {
871 	struct kvm_cpuid2 *cpuid;
872 	struct kvm_cpuid_entry2 *entry = NULL;
873 	int i;
874 
875 	cpuid = kvm_get_supported_cpuid();
876 	for (i = 0; i < cpuid->nent; i++) {
877 		if (cpuid->entries[i].function == function &&
878 		    cpuid->entries[i].index == index) {
879 			entry = &cpuid->entries[i];
880 			break;
881 		}
882 	}
883 
884 	TEST_ASSERT(entry, "Guest CPUID entry not found: (EAX=%x, ECX=%x).",
885 		    function, index);
886 	return entry;
887 }
888 
889 
890 int __vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
891 		     struct kvm_cpuid2 *cpuid)
892 {
893 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
894 
895 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
896 
897 	return ioctl(vcpu->fd, KVM_SET_CPUID2, cpuid);
898 }
899 
900 /*
901  * VM VCPU CPUID Set
902  *
903  * Input Args:
904  *   vm - Virtual Machine
905  *   vcpuid - VCPU id
906  *   cpuid - The CPUID values to set.
907  *
908  * Output Args: None
909  *
910  * Return: void
911  *
912  * Set the VCPU's CPUID.
913  */
914 void vcpu_set_cpuid(struct kvm_vm *vm,
915 		uint32_t vcpuid, struct kvm_cpuid2 *cpuid)
916 {
917 	int rc;
918 
919 	rc = __vcpu_set_cpuid(vm, vcpuid, cpuid);
920 	TEST_ASSERT(rc == 0, "KVM_SET_CPUID2 failed, rc: %i errno: %i",
921 		    rc, errno);
922 
923 }
924 
925 /*
926  * VCPU Get MSR
927  *
928  * Input Args:
929  *   vm - Virtual Machine
930  *   vcpuid - VCPU ID
931  *   msr_index - Index of MSR
932  *
933  * Output Args: None
934  *
935  * Return: On success, value of the MSR. On failure a TEST_ASSERT is produced.
936  *
937  * Get value of MSR for VCPU.
938  */
939 uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index)
940 {
941 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
942 	struct {
943 		struct kvm_msrs header;
944 		struct kvm_msr_entry entry;
945 	} buffer = {};
946 	int r;
947 
948 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
949 	buffer.header.nmsrs = 1;
950 	buffer.entry.index = msr_index;
951 	r = ioctl(vcpu->fd, KVM_GET_MSRS, &buffer.header);
952 	TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n"
953 		"  rc: %i errno: %i", r, errno);
954 
955 	return buffer.entry.data;
956 }
957 
958 /*
959  * _VCPU Set MSR
960  *
961  * Input Args:
962  *   vm - Virtual Machine
963  *   vcpuid - VCPU ID
964  *   msr_index - Index of MSR
965  *   msr_value - New value of MSR
966  *
967  * Output Args: None
968  *
969  * Return: The result of KVM_SET_MSRS.
970  *
971  * Sets the value of an MSR for the given VCPU.
972  */
973 int _vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
974 		  uint64_t msr_value)
975 {
976 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
977 	struct {
978 		struct kvm_msrs header;
979 		struct kvm_msr_entry entry;
980 	} buffer = {};
981 	int r;
982 
983 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
984 	memset(&buffer, 0, sizeof(buffer));
985 	buffer.header.nmsrs = 1;
986 	buffer.entry.index = msr_index;
987 	buffer.entry.data = msr_value;
988 	r = ioctl(vcpu->fd, KVM_SET_MSRS, &buffer.header);
989 	return r;
990 }
991 
992 /*
993  * VCPU Set MSR
994  *
995  * Input Args:
996  *   vm - Virtual Machine
997  *   vcpuid - VCPU ID
998  *   msr_index - Index of MSR
999  *   msr_value - New value of MSR
1000  *
1001  * Output Args: None
1002  *
1003  * Return: On success, nothing. On failure a TEST_ASSERT is produced.
1004  *
1005  * Set value of MSR for VCPU.
1006  */
1007 void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
1008 	uint64_t msr_value)
1009 {
1010 	int r;
1011 
1012 	r = _vcpu_set_msr(vm, vcpuid, msr_index, msr_value);
1013 	TEST_ASSERT(r == 1, "KVM_SET_MSRS IOCTL failed,\n"
1014 		"  rc: %i errno: %i", r, errno);
1015 }
1016 
1017 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
1018 {
1019 	va_list ap;
1020 	struct kvm_regs regs;
1021 
1022 	TEST_ASSERT(num >= 1 && num <= 6, "Unsupported number of args,\n"
1023 		    "  num: %u\n",
1024 		    num);
1025 
1026 	va_start(ap, num);
1027 	vcpu_regs_get(vm, vcpuid, &regs);
1028 
1029 	if (num >= 1)
1030 		regs.rdi = va_arg(ap, uint64_t);
1031 
1032 	if (num >= 2)
1033 		regs.rsi = va_arg(ap, uint64_t);
1034 
1035 	if (num >= 3)
1036 		regs.rdx = va_arg(ap, uint64_t);
1037 
1038 	if (num >= 4)
1039 		regs.rcx = va_arg(ap, uint64_t);
1040 
1041 	if (num >= 5)
1042 		regs.r8 = va_arg(ap, uint64_t);
1043 
1044 	if (num >= 6)
1045 		regs.r9 = va_arg(ap, uint64_t);
1046 
1047 	vcpu_regs_set(vm, vcpuid, &regs);
1048 	va_end(ap);
1049 }
1050 
1051 void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, uint8_t indent)
1052 {
1053 	struct kvm_regs regs;
1054 	struct kvm_sregs sregs;
1055 
1056 	fprintf(stream, "%*scpuid: %u\n", indent, "", vcpuid);
1057 
1058 	fprintf(stream, "%*sregs:\n", indent + 2, "");
1059 	vcpu_regs_get(vm, vcpuid, &regs);
1060 	regs_dump(stream, &regs, indent + 4);
1061 
1062 	fprintf(stream, "%*ssregs:\n", indent + 2, "");
1063 	vcpu_sregs_get(vm, vcpuid, &sregs);
1064 	sregs_dump(stream, &sregs, indent + 4);
1065 }
1066 
1067 static int kvm_get_num_msrs_fd(int kvm_fd)
1068 {
1069 	struct kvm_msr_list nmsrs;
1070 	int r;
1071 
1072 	nmsrs.nmsrs = 0;
1073 	r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, &nmsrs);
1074 	TEST_ASSERT(r == -1 && errno == E2BIG, "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i",
1075 		r);
1076 
1077 	return nmsrs.nmsrs;
1078 }
1079 
1080 static int kvm_get_num_msrs(struct kvm_vm *vm)
1081 {
1082 	return kvm_get_num_msrs_fd(vm->kvm_fd);
1083 }
1084 
1085 struct kvm_msr_list *kvm_get_msr_index_list(void)
1086 {
1087 	struct kvm_msr_list *list;
1088 	int nmsrs, r, kvm_fd;
1089 
1090 	kvm_fd = open_kvm_dev_path_or_exit();
1091 
1092 	nmsrs = kvm_get_num_msrs_fd(kvm_fd);
1093 	list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
1094 	list->nmsrs = nmsrs;
1095 	r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
1096 	close(kvm_fd);
1097 
1098 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
1099 		r);
1100 
1101 	return list;
1102 }
1103 
1104 static int vcpu_save_xsave_state(struct kvm_vm *vm, struct vcpu *vcpu,
1105 				 struct kvm_x86_state *state)
1106 {
1107 	int size;
1108 
1109 	size = vm_check_cap(vm, KVM_CAP_XSAVE2);
1110 	if (!size)
1111 		size = sizeof(struct kvm_xsave);
1112 
1113 	state->xsave = malloc(size);
1114 	if (size == sizeof(struct kvm_xsave))
1115 		return ioctl(vcpu->fd, KVM_GET_XSAVE, state->xsave);
1116 	else
1117 		return ioctl(vcpu->fd, KVM_GET_XSAVE2, state->xsave);
1118 }
1119 
1120 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
1121 {
1122 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1123 	struct kvm_msr_list *list;
1124 	struct kvm_x86_state *state;
1125 	int nmsrs, r, i;
1126 	static int nested_size = -1;
1127 
1128 	if (nested_size == -1) {
1129 		nested_size = kvm_check_cap(KVM_CAP_NESTED_STATE);
1130 		TEST_ASSERT(nested_size <= sizeof(state->nested_),
1131 			    "Nested state size too big, %i > %zi",
1132 			    nested_size, sizeof(state->nested_));
1133 	}
1134 
1135 	/*
1136 	 * When KVM exits to userspace with KVM_EXIT_IO, KVM guarantees
1137 	 * guest state is consistent only after userspace re-enters the
1138 	 * kernel with KVM_RUN.  Complete IO prior to migrating state
1139 	 * to a new VM.
1140 	 */
1141 	vcpu_run_complete_io(vm, vcpuid);
1142 
1143 	nmsrs = kvm_get_num_msrs(vm);
1144 	list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
1145 	list->nmsrs = nmsrs;
1146 	r = ioctl(vm->kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
1147 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
1148 		    r);
1149 
1150 	state = malloc(sizeof(*state) + nmsrs * sizeof(state->msrs.entries[0]));
1151 	r = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, &state->events);
1152 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_VCPU_EVENTS, r: %i",
1153 		    r);
1154 
1155 	r = ioctl(vcpu->fd, KVM_GET_MP_STATE, &state->mp_state);
1156 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MP_STATE, r: %i",
1157 		    r);
1158 
1159 	r = ioctl(vcpu->fd, KVM_GET_REGS, &state->regs);
1160 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_REGS, r: %i",
1161 		    r);
1162 
1163 	r = vcpu_save_xsave_state(vm, vcpu, state);
1164 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_XSAVE, r: %i",
1165 		    r);
1166 
1167 	if (kvm_check_cap(KVM_CAP_XCRS)) {
1168 		r = ioctl(vcpu->fd, KVM_GET_XCRS, &state->xcrs);
1169 		TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_XCRS, r: %i",
1170 			    r);
1171 	}
1172 
1173 	r = ioctl(vcpu->fd, KVM_GET_SREGS, &state->sregs);
1174 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_SREGS, r: %i",
1175 		    r);
1176 
1177 	if (nested_size) {
1178 		state->nested.size = sizeof(state->nested_);
1179 		r = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, &state->nested);
1180 		TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_NESTED_STATE, r: %i",
1181 			    r);
1182 		TEST_ASSERT(state->nested.size <= nested_size,
1183 			    "Nested state size too big, %i (KVM_CHECK_CAP gave %i)",
1184 			    state->nested.size, nested_size);
1185 	} else
1186 		state->nested.size = 0;
1187 
1188 	state->msrs.nmsrs = nmsrs;
1189 	for (i = 0; i < nmsrs; i++)
1190 		state->msrs.entries[i].index = list->indices[i];
1191 	r = ioctl(vcpu->fd, KVM_GET_MSRS, &state->msrs);
1192 	TEST_ASSERT(r == nmsrs, "Unexpected result from KVM_GET_MSRS, r: %i (failed MSR was 0x%x)",
1193 		    r, r == nmsrs ? -1 : list->indices[r]);
1194 
1195 	r = ioctl(vcpu->fd, KVM_GET_DEBUGREGS, &state->debugregs);
1196 	TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_DEBUGREGS, r: %i",
1197 		    r);
1198 
1199 	free(list);
1200 	return state;
1201 }
1202 
1203 void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_x86_state *state)
1204 {
1205 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1206 	int r;
1207 
1208 	r = ioctl(vcpu->fd, KVM_SET_SREGS, &state->sregs);
1209 	TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_SREGS, r: %i",
1210 		    r);
1211 
1212 	r = ioctl(vcpu->fd, KVM_SET_MSRS, &state->msrs);
1213 	TEST_ASSERT(r == state->msrs.nmsrs,
1214 		"Unexpected result from KVM_SET_MSRS, r: %i (failed at %x)",
1215 		r, r == state->msrs.nmsrs ? -1 : state->msrs.entries[r].index);
1216 
1217 	if (kvm_check_cap(KVM_CAP_XCRS)) {
1218 		r = ioctl(vcpu->fd, KVM_SET_XCRS, &state->xcrs);
1219 		TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_XCRS, r: %i",
1220 			    r);
1221 	}
1222 
1223 	r = ioctl(vcpu->fd, KVM_SET_XSAVE, state->xsave);
1224 	TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_XSAVE, r: %i",
1225 		    r);
1226 
1227 	r = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, &state->events);
1228 	TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_VCPU_EVENTS, r: %i",
1229 		    r);
1230 
1231 	r = ioctl(vcpu->fd, KVM_SET_MP_STATE, &state->mp_state);
1232 	TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_MP_STATE, r: %i",
1233 		    r);
1234 
1235 	r = ioctl(vcpu->fd, KVM_SET_DEBUGREGS, &state->debugregs);
1236 	TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_DEBUGREGS, r: %i",
1237 		    r);
1238 
1239 	r = ioctl(vcpu->fd, KVM_SET_REGS, &state->regs);
1240 	TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_REGS, r: %i",
1241 		    r);
1242 
1243 	if (state->nested.size) {
1244 		r = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, &state->nested);
1245 		TEST_ASSERT(r == 0, "Unexpected result from KVM_SET_NESTED_STATE, r: %i",
1246 			    r);
1247 	}
1248 }
1249 
1250 void kvm_x86_state_cleanup(struct kvm_x86_state *state)
1251 {
1252 	free(state->xsave);
1253 	free(state);
1254 }
1255 
1256 static bool cpu_vendor_string_is(const char *vendor)
1257 {
1258 	const uint32_t *chunk = (const uint32_t *)vendor;
1259 	int eax, ebx, ecx, edx;
1260 	const int leaf = 0;
1261 
1262 	__asm__ __volatile__(
1263 		"cpuid"
1264 		: /* output */ "=a"(eax), "=b"(ebx),
1265 		  "=c"(ecx), "=d"(edx)
1266 		: /* input */ "0"(leaf), "2"(0));
1267 
1268 	return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
1269 }
1270 
1271 bool is_intel_cpu(void)
1272 {
1273 	return cpu_vendor_string_is("GenuineIntel");
1274 }
1275 
1276 /*
1277  * Exclude early K5 samples with a vendor string of "AMDisbetter!"
1278  */
1279 bool is_amd_cpu(void)
1280 {
1281 	return cpu_vendor_string_is("AuthenticAMD");
1282 }
1283 
1284 uint32_t kvm_get_cpuid_max_basic(void)
1285 {
1286 	return kvm_get_supported_cpuid_entry(0)->eax;
1287 }
1288 
1289 uint32_t kvm_get_cpuid_max_extended(void)
1290 {
1291 	return kvm_get_supported_cpuid_entry(0x80000000)->eax;
1292 }
1293 
1294 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
1295 {
1296 	struct kvm_cpuid_entry2 *entry;
1297 	bool pae;
1298 
1299 	/* SDM 4.1.4 */
1300 	if (kvm_get_cpuid_max_extended() < 0x80000008) {
1301 		pae = kvm_get_supported_cpuid_entry(1)->edx & (1 << 6);
1302 		*pa_bits = pae ? 36 : 32;
1303 		*va_bits = 32;
1304 	} else {
1305 		entry = kvm_get_supported_cpuid_entry(0x80000008);
1306 		*pa_bits = entry->eax & 0xff;
1307 		*va_bits = (entry->eax >> 8) & 0xff;
1308 	}
1309 }
1310 
1311 struct idt_entry {
1312 	uint16_t offset0;
1313 	uint16_t selector;
1314 	uint16_t ist : 3;
1315 	uint16_t : 5;
1316 	uint16_t type : 4;
1317 	uint16_t : 1;
1318 	uint16_t dpl : 2;
1319 	uint16_t p : 1;
1320 	uint16_t offset1;
1321 	uint32_t offset2; uint32_t reserved;
1322 };
1323 
1324 static void set_idt_entry(struct kvm_vm *vm, int vector, unsigned long addr,
1325 			  int dpl, unsigned short selector)
1326 {
1327 	struct idt_entry *base =
1328 		(struct idt_entry *)addr_gva2hva(vm, vm->idt);
1329 	struct idt_entry *e = &base[vector];
1330 
1331 	memset(e, 0, sizeof(*e));
1332 	e->offset0 = addr;
1333 	e->selector = selector;
1334 	e->ist = 0;
1335 	e->type = 14;
1336 	e->dpl = dpl;
1337 	e->p = 1;
1338 	e->offset1 = addr >> 16;
1339 	e->offset2 = addr >> 32;
1340 }
1341 
1342 void kvm_exit_unexpected_vector(uint32_t value)
1343 {
1344 	ucall(UCALL_UNHANDLED, 1, value);
1345 }
1346 
1347 void route_exception(struct ex_regs *regs)
1348 {
1349 	typedef void(*handler)(struct ex_regs *);
1350 	handler *handlers = (handler *)exception_handlers;
1351 
1352 	if (handlers && handlers[regs->vector]) {
1353 		handlers[regs->vector](regs);
1354 		return;
1355 	}
1356 
1357 	kvm_exit_unexpected_vector(regs->vector);
1358 }
1359 
1360 void vm_init_descriptor_tables(struct kvm_vm *vm)
1361 {
1362 	extern void *idt_handlers;
1363 	int i;
1364 
1365 	vm->idt = vm_vaddr_alloc_page(vm);
1366 	vm->handlers = vm_vaddr_alloc_page(vm);
1367 	/* Handlers have the same address in both address spaces.*/
1368 	for (i = 0; i < NUM_INTERRUPTS; i++)
1369 		set_idt_entry(vm, i, (unsigned long)(&idt_handlers)[i], 0,
1370 			DEFAULT_CODE_SELECTOR);
1371 }
1372 
1373 void vcpu_init_descriptor_tables(struct kvm_vm *vm, uint32_t vcpuid)
1374 {
1375 	struct kvm_sregs sregs;
1376 
1377 	vcpu_sregs_get(vm, vcpuid, &sregs);
1378 	sregs.idt.base = vm->idt;
1379 	sregs.idt.limit = NUM_INTERRUPTS * sizeof(struct idt_entry) - 1;
1380 	sregs.gdt.base = vm->gdt;
1381 	sregs.gdt.limit = getpagesize() - 1;
1382 	kvm_seg_set_kernel_data_64bit(NULL, DEFAULT_DATA_SELECTOR, &sregs.gs);
1383 	vcpu_sregs_set(vm, vcpuid, &sregs);
1384 	*(vm_vaddr_t *)addr_gva2hva(vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers;
1385 }
1386 
1387 void vm_install_exception_handler(struct kvm_vm *vm, int vector,
1388 			       void (*handler)(struct ex_regs *))
1389 {
1390 	vm_vaddr_t *handlers = (vm_vaddr_t *)addr_gva2hva(vm, vm->handlers);
1391 
1392 	handlers[vector] = (vm_vaddr_t)handler;
1393 }
1394 
1395 void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid)
1396 {
1397 	struct ucall uc;
1398 
1399 	if (get_ucall(vm, vcpuid, &uc) == UCALL_UNHANDLED) {
1400 		uint64_t vector = uc.args[0];
1401 
1402 		TEST_FAIL("Unexpected vectored event in guest (vector:0x%lx)",
1403 			  vector);
1404 	}
1405 }
1406 
1407 struct kvm_cpuid_entry2 *get_cpuid(struct kvm_cpuid2 *cpuid, uint32_t function,
1408 				   uint32_t index)
1409 {
1410 	int i;
1411 
1412 	for (i = 0; i < cpuid->nent; i++) {
1413 		struct kvm_cpuid_entry2 *cur = &cpuid->entries[i];
1414 
1415 		if (cur->function == function && cur->index == index)
1416 			return cur;
1417 	}
1418 
1419 	TEST_FAIL("CPUID function 0x%x index 0x%x not found ", function, index);
1420 
1421 	return NULL;
1422 }
1423 
1424 bool set_cpuid(struct kvm_cpuid2 *cpuid,
1425 	       struct kvm_cpuid_entry2 *ent)
1426 {
1427 	int i;
1428 
1429 	for (i = 0; i < cpuid->nent; i++) {
1430 		struct kvm_cpuid_entry2 *cur = &cpuid->entries[i];
1431 
1432 		if (cur->function != ent->function || cur->index != ent->index)
1433 			continue;
1434 
1435 		memcpy(cur, ent, sizeof(struct kvm_cpuid_entry2));
1436 		return true;
1437 	}
1438 
1439 	return false;
1440 }
1441 
1442 uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,
1443 		       uint64_t a3)
1444 {
1445 	uint64_t r;
1446 
1447 	asm volatile("vmcall"
1448 		     : "=a"(r)
1449 		     : "b"(a0), "c"(a1), "d"(a2), "S"(a3));
1450 	return r;
1451 }
1452 
1453 struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)
1454 {
1455 	static struct kvm_cpuid2 *cpuid;
1456 	int ret;
1457 	int kvm_fd;
1458 
1459 	if (cpuid)
1460 		return cpuid;
1461 
1462 	cpuid = allocate_kvm_cpuid2();
1463 	kvm_fd = open_kvm_dev_path_or_exit();
1464 
1465 	ret = ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
1466 	TEST_ASSERT(ret == 0, "KVM_GET_SUPPORTED_HV_CPUID failed %d %d\n",
1467 		    ret, errno);
1468 
1469 	close(kvm_fd);
1470 	return cpuid;
1471 }
1472 
1473 void vcpu_set_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid)
1474 {
1475 	static struct kvm_cpuid2 *cpuid_full;
1476 	struct kvm_cpuid2 *cpuid_sys, *cpuid_hv;
1477 	int i, nent = 0;
1478 
1479 	if (!cpuid_full) {
1480 		cpuid_sys = kvm_get_supported_cpuid();
1481 		cpuid_hv = kvm_get_supported_hv_cpuid();
1482 
1483 		cpuid_full = malloc(sizeof(*cpuid_full) +
1484 				    (cpuid_sys->nent + cpuid_hv->nent) *
1485 				    sizeof(struct kvm_cpuid_entry2));
1486 		if (!cpuid_full) {
1487 			perror("malloc");
1488 			abort();
1489 		}
1490 
1491 		/* Need to skip KVM CPUID leaves 0x400000xx */
1492 		for (i = 0; i < cpuid_sys->nent; i++) {
1493 			if (cpuid_sys->entries[i].function >= 0x40000000 &&
1494 			    cpuid_sys->entries[i].function < 0x40000100)
1495 				continue;
1496 			cpuid_full->entries[nent] = cpuid_sys->entries[i];
1497 			nent++;
1498 		}
1499 
1500 		memcpy(&cpuid_full->entries[nent], cpuid_hv->entries,
1501 		       cpuid_hv->nent * sizeof(struct kvm_cpuid_entry2));
1502 		cpuid_full->nent = nent + cpuid_hv->nent;
1503 	}
1504 
1505 	vcpu_set_cpuid(vm, vcpuid, cpuid_full);
1506 }
1507 
1508 struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vm *vm, uint32_t vcpuid)
1509 {
1510 	static struct kvm_cpuid2 *cpuid;
1511 
1512 	cpuid = allocate_kvm_cpuid2();
1513 
1514 	vcpu_ioctl(vm, vcpuid, KVM_GET_SUPPORTED_HV_CPUID, cpuid);
1515 
1516 	return cpuid;
1517 }
1518 
1519 unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
1520 {
1521 	const unsigned long num_ht_pages = 12 << (30 - vm->page_shift); /* 12 GiB */
1522 	unsigned long ht_gfn, max_gfn, max_pfn;
1523 	uint32_t eax, ebx, ecx, edx, max_ext_leaf;
1524 
1525 	max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
1526 
1527 	/* Avoid reserved HyperTransport region on AMD processors.  */
1528 	if (!is_amd_cpu())
1529 		return max_gfn;
1530 
1531 	/* On parts with <40 physical address bits, the area is fully hidden */
1532 	if (vm->pa_bits < 40)
1533 		return max_gfn;
1534 
1535 	/* Before family 17h, the HyperTransport area is just below 1T.  */
1536 	ht_gfn = (1 << 28) - num_ht_pages;
1537 	eax = 1;
1538 	ecx = 0;
1539 	cpuid(&eax, &ebx, &ecx, &edx);
1540 	if (x86_family(eax) < 0x17)
1541 		goto done;
1542 
1543 	/*
1544 	 * Otherwise it's at the top of the physical address space, possibly
1545 	 * reduced due to SME by bits 11:6 of CPUID[0x8000001f].EBX.  Use
1546 	 * the old conservative value if MAXPHYADDR is not enumerated.
1547 	 */
1548 	eax = 0x80000000;
1549 	cpuid(&eax, &ebx, &ecx, &edx);
1550 	max_ext_leaf = eax;
1551 	if (max_ext_leaf < 0x80000008)
1552 		goto done;
1553 
1554 	eax = 0x80000008;
1555 	cpuid(&eax, &ebx, &ecx, &edx);
1556 	max_pfn = (1ULL << ((eax & 0xff) - vm->page_shift)) - 1;
1557 	if (max_ext_leaf >= 0x8000001f) {
1558 		eax = 0x8000001f;
1559 		cpuid(&eax, &ebx, &ecx, &edx);
1560 		max_pfn >>= (ebx >> 6) & 0x3f;
1561 	}
1562 
1563 	ht_gfn = max_pfn - num_ht_pages;
1564 done:
1565 	return min(max_gfn, ht_gfn - 1);
1566 }
1567