xref: /linux/arch/s390/tools/gen_bitmaps.c (revision f9ef2f6c877a9faef8528058eb62fcefde83ed9a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Simple program to generate bitmaps, for example facility lists.
4  *
5  *    Copyright IBM Corp. 2015, 2018
6  *
7  */
8 
9 #include <getopt.h>
10 #include <strings.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16 
17 struct bitmap_def {
18 	char *name;
19 	int *bits;
20 };
21 
22 static struct bitmap_def facility_defs[] = {
23 	{
24 		/*
25 		 * FACILITIES_ALS contains the list of facilities that are
26 		 * required to run a kernel that is compiled e.g. with
27 		 * -march=<machine>.
28 		 */
29 		.name = "FACILITIES_ALS",
30 		.bits = (int[]){
31 			0,  /* N3 instructions */
32 			1,  /* z/Arch mode installed */
33 			3,  /* dat-enhancement 1 */
34 			18, /* long displacement facility */
35 			21, /* extended-immediate facility */
36 			25, /* store clock fast */
37 			27, /* mvcos */
38 			32, /* compare and swap and store */
39 			33, /* compare and swap and store 2 */
40 			34, /* general instructions extension */
41 			35, /* execute extensions */
42 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
43 			45, /* fast-BCR, etc. */
44 #endif
45 #ifdef CONFIG_HAVE_MARCH_ZEC12_FEATURES
46 			49, /* misc-instruction-extensions */
47 			52, /* interlocked facility 2 */
48 #endif
49 #ifdef CONFIG_HAVE_MARCH_Z13_FEATURES
50 			53, /* load-and-zero-rightmost-byte, etc. */
51 			129, /* vector */
52 #endif
53 #ifdef CONFIG_HAVE_MARCH_Z14_FEATURES
54 			58, /* miscellaneous-instruction-extension 2 */
55 #endif
56 #ifdef CONFIG_HAVE_MARCH_Z15_FEATURES
57 			61, /* miscellaneous-instruction-extension 3 */
58 #endif
59 #ifdef CONFIG_HAVE_MARCH_Z17_FEATURES
60 			84, /* miscellaneous-instruction-extension 4 */
61 #endif
62 			-1 /* END */
63 		}
64 	},
65 	{
66 		/*
67 		 * FACILITIES_KVM contains the list of facilities that are part
68 		 * of the default facility mask and list that are passed to the
69 		 * initial CPU model. If no CPU model is used, this, together
70 		 * with the non-hypervisor managed bits, is the maximum list of
71 		 * guest facilities supported by KVM.
72 		 */
73 		.name = "FACILITIES_KVM",
74 		.bits = (int[]){
75 			0,  /* N3 instructions */
76 			1,  /* z/Arch mode installed */
77 			2,  /* z/Arch mode active */
78 			3,  /* DAT-enhancement */
79 			4,  /* idte segment table */
80 			5,  /* idte region table */
81 			6,  /* ASN-and-LX reuse */
82 			7,  /* stfle */
83 			8,  /* enhanced-DAT 1 */
84 			9,  /* sense-running-status */
85 			10, /* conditional sske */
86 			13, /* ipte-range */
87 			14, /* nonquiescing key-setting */
88 			73, /* transactional execution */
89 			75, /* access-exception-fetch/store indication */
90 			76, /* msa extension 3 */
91 			77, /* msa extension 4 */
92 			78, /* enhanced-DAT 2 */
93 			130, /* instruction-execution-protection */
94 			131, /* enhanced-SOP 2 and side-effect */
95 			139, /* multiple epoch facility */
96 			146, /* msa extension 8 */
97 			150, /* enhanced sort */
98 			151, /* deflate conversion */
99 			155, /* msa extension 9 */
100 			165, /* nnpa facility */
101 			170, /* ineffective-nonconstrained-transaction facility */
102 			193, /* bear enhancement facility */
103 			194, /* rdp enhancement facility */
104 			196, /* processor activity instrumentation facility */
105 			197, /* processor activity instrumentation extension 1 */
106 			201, /* concurrent-functions facility */
107 			-1  /* END */
108 		}
109 	},
110 	{
111 		/*
112 		 * FACILITIES_KVM_CPUMODEL contains the list of facilities
113 		 * that can be enabled by CPU model code if the host supports
114 		 * it. These facilities are not passed to the guest without
115 		 * CPU model support.
116 		 */
117 
118 		.name = "FACILITIES_KVM_CPUMODEL",
119 		.bits = (int[]){
120 			12, /* AP Query Configuration Information */
121 			15, /* AP Facilities Test */
122 			156, /* etoken facility */
123 			-1  /* END */
124 		}
125 	},
126 };
127 
128 static void print_bitmap(struct bitmap_def *def)
129 {
130 	unsigned int high, bit, dword, i;
131 	unsigned long long *array;
132 
133 	array = calloc(1, 8);
134 	if (!array)
135 		exit(EXIT_FAILURE);
136 	high = 0;
137 	for (i = 0; def->bits[i] != -1; i++) {
138 		bit = 63 - (def->bits[i] & 63);
139 		dword = def->bits[i] / 64;
140 		if (dword > high) {
141 			array = realloc(array, (dword + 1) * 8);
142 			if (!array)
143 				exit(EXIT_FAILURE);
144 			memset(array + high + 1, 0, (dword - high) * 8);
145 			high = dword;
146 		}
147 		array[dword] |= 1ULL << bit;
148 	}
149 	printf("#define %s ", def->name);
150 	for (i = 0; i <= high; i++)
151 		printf("_AC(0x%016llx,UL)%c", array[i], i < high ? ',' : '\n');
152 	free(array);
153 }
154 
155 static void print_header(const char *guard, struct bitmap_def *defs, size_t num)
156 {
157 	unsigned int i;
158 
159 	printf("#ifndef __ASM_S390_%s_DEFS__\n", guard);
160 	printf("#define __ASM_S390_%s_DEFS__\n", guard);
161 	printf("/*\n");
162 	printf(" * DO NOT MODIFY.\n");
163 	printf(" *\n");
164 	printf(" * This file was generated by %s\n", __FILE__);
165 	printf(" */\n\n");
166 	printf("#include <linux/const.h>\n\n");
167 	for (i = 0; i < num; i++)
168 		print_bitmap(&defs[i]);
169 	printf("\n#endif\n");
170 }
171 
172 static const struct option long_options[] = {
173 	{ "facility", no_argument, NULL, 'f' },
174 	{ NULL, 0, NULL, 0 }
175 };
176 
177 int main(int argc, char **argv)
178 {
179 	int opt;
180 
181 	opt = getopt_long(argc, argv, "", long_options, NULL);
182 	if (opt == -1)
183 		return 1;
184 	switch (opt) {
185 	case 'f':
186 		print_header("FACILITY", facility_defs, ARRAY_SIZE(facility_defs));
187 		break;
188 	default:
189 		return 1;
190 	}
191 	return 0;
192 }
193