1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020, Red Hat, Inc.
4  */
5 #include "guest_modes.h"
6 
7 struct guest_mode guest_modes[NUM_VM_MODES];
8 
9 void guest_modes_append_default(void)
10 {
11 	guest_mode_append(VM_MODE_DEFAULT, true, true);
12 
13 #ifdef __aarch64__
14 	guest_mode_append(VM_MODE_P40V48_64K, true, true);
15 	{
16 		unsigned int limit = kvm_check_cap(KVM_CAP_ARM_VM_IPA_SIZE);
17 		if (limit >= 52)
18 			guest_mode_append(VM_MODE_P52V48_64K, true, true);
19 		if (limit >= 48) {
20 			guest_mode_append(VM_MODE_P48V48_4K, true, true);
21 			guest_mode_append(VM_MODE_P48V48_64K, true, true);
22 		}
23 	}
24 #endif
25 #ifdef __s390x__
26 	{
27 		int kvm_fd, vm_fd;
28 		struct kvm_s390_vm_cpu_processor info;
29 
30 		kvm_fd = open_kvm_dev_path_or_exit();
31 		vm_fd = ioctl(kvm_fd, KVM_CREATE_VM, 0);
32 		kvm_device_access(vm_fd, KVM_S390_VM_CPU_MODEL,
33 				  KVM_S390_VM_CPU_PROCESSOR, &info, false);
34 		close(vm_fd);
35 		close(kvm_fd);
36 		/* Starting with z13 we have 47bits of physical address */
37 		if (info.ibc >= 0x30)
38 			guest_mode_append(VM_MODE_P47V64_4K, true, true);
39 	}
40 #endif
41 #ifdef __riscv
42 	{
43 		unsigned int sz = kvm_check_cap(KVM_CAP_VM_GPA_BITS);
44 
45 		if (sz >= 52)
46 			guest_mode_append(VM_MODE_P52V48_4K, true, true);
47 		if (sz >= 48)
48 			guest_mode_append(VM_MODE_P48V48_4K, true, true);
49 	}
50 #endif
51 }
52 
53 void for_each_guest_mode(void (*func)(enum vm_guest_mode, void *), void *arg)
54 {
55 	int i;
56 
57 	for (i = 0; i < NUM_VM_MODES; ++i) {
58 		if (!guest_modes[i].enabled)
59 			continue;
60 		TEST_ASSERT(guest_modes[i].supported,
61 			    "Guest mode ID %d (%s) not supported.",
62 			    i, vm_guest_mode_string(i));
63 		func(i, arg);
64 	}
65 }
66 
67 void guest_modes_help(void)
68 {
69 	int i;
70 
71 	printf(" -m: specify the guest mode ID to test\n"
72 	       "     (default: test all supported modes)\n"
73 	       "     This option may be used multiple times.\n"
74 	       "     Guest mode IDs:\n");
75 	for (i = 0; i < NUM_VM_MODES; ++i) {
76 		printf("         %d:    %s%s\n", i, vm_guest_mode_string(i),
77 		       guest_modes[i].supported ? " (supported)" : "");
78 	}
79 }
80 
81 void guest_modes_cmdline(const char *arg)
82 {
83 	static bool mode_selected;
84 	unsigned int mode;
85 	int i;
86 
87 	if (!mode_selected) {
88 		for (i = 0; i < NUM_VM_MODES; ++i)
89 			guest_modes[i].enabled = false;
90 		mode_selected = true;
91 	}
92 
93 	mode = strtoul(optarg, NULL, 10);
94 	TEST_ASSERT(mode < NUM_VM_MODES, "Guest mode ID %d too big", mode);
95 	guest_modes[mode].enabled = true;
96 }
97