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 }
26 
27 void for_each_guest_mode(void (*func)(enum vm_guest_mode, void *), void *arg)
28 {
29 	int i;
30 
31 	for (i = 0; i < NUM_VM_MODES; ++i) {
32 		if (!guest_modes[i].enabled)
33 			continue;
34 		TEST_ASSERT(guest_modes[i].supported,
35 			    "Guest mode ID %d (%s) not supported.",
36 			    i, vm_guest_mode_string(i));
37 		func(i, arg);
38 	}
39 }
40 
41 void guest_modes_help(void)
42 {
43 	int i;
44 
45 	printf(" -m: specify the guest mode ID to test\n"
46 	       "     (default: test all supported modes)\n"
47 	       "     This option may be used multiple times.\n"
48 	       "     Guest mode IDs:\n");
49 	for (i = 0; i < NUM_VM_MODES; ++i) {
50 		printf("         %d:    %s%s\n", i, vm_guest_mode_string(i),
51 		       guest_modes[i].supported ? " (supported)" : "");
52 	}
53 }
54 
55 void guest_modes_cmdline(const char *arg)
56 {
57 	static bool mode_selected;
58 	unsigned int mode;
59 	int i;
60 
61 	if (!mode_selected) {
62 		for (i = 0; i < NUM_VM_MODES; ++i)
63 			guest_modes[i].enabled = false;
64 		mode_selected = true;
65 	}
66 
67 	mode = strtoul(optarg, NULL, 10);
68 	TEST_ASSERT(mode < NUM_VM_MODES, "Guest mode ID %d too big", mode);
69 	guest_modes[mode].enabled = true;
70 }
71