florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
| 2 | |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 3 | #include <setjmp.h> |
| 4 | #include <signal.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
florian | a066ccd | 2012-09-02 03:30:38 +0000 | [diff] [blame] | 8 | #include <assert.h> |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 9 | #include <ctype.h> // isspace |
| 10 | #include <fcntl.h> // open |
| 11 | #include <unistd.h> // lseek |
| 12 | #include <sys/stat.h> // S_IRUSR |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 13 | |
| 14 | // This file determines s390x features a processor supports. |
| 15 | // |
| 16 | // We return: |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 17 | // - 0 if the machine provides the asked-for feature and the cpu |
| 18 | // model, if specified, matches the machine |
| 19 | // - 1 the machine does not provide the asked-for feature or the |
| 20 | // cpu model, if specified, does not match the machine |
cborntra | 9dd35e0 | 2012-05-29 11:02:28 +0000 | [diff] [blame] | 21 | // - 1 for an unknown cpu model in /proc/cpu_info |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 22 | // - 2 if the asked-for feature isn't recognised (this will be the case for |
| 23 | // any feature if run on a non-s390x machine). |
| 24 | // - 3 if there was a usage error (it also prints an error message). |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 25 | // |
| 26 | // USAGE: |
| 27 | // |
| 28 | // s390x_features <feature> [<machine-model>] |
| 29 | // |
| 30 | // The machine_model is optional and it can be something like: |
| 31 | // |
| 32 | // z9 -- Host needs to be a z9 (and nothing else) |
| 33 | // z9: -- Host needs to be a z9 or any later model |
| 34 | // :z9 -- Host needs to be a model up to and including z9 |
| 35 | // z900:z9 -- Host needs to be at least a z900 and at most a z9. |
| 36 | // Any model in between is OK, too. |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 37 | |
| 38 | jmp_buf env; |
| 39 | |
| 40 | #if defined(VGA_s390x) |
| 41 | |
| 42 | void handle_sigill(int signum) |
| 43 | { |
| 44 | longjmp(env, 1); |
| 45 | } |
| 46 | |
| 47 | unsigned long long stfle(void) |
| 48 | { |
| 49 | |
| 50 | unsigned long long ret; |
| 51 | |
| 52 | signal(SIGILL, handle_sigill); |
| 53 | if (setjmp(env)) { |
| 54 | /* stfle not available: assume no facilities */ |
| 55 | return 0; |
| 56 | } else { |
| 57 | asm volatile("lghi 0, 0\n" |
| 58 | ".insn s,0xb2b00000,%0\n" /* stfle */ |
| 59 | : "=Q" (ret)::"0", "cc"); |
| 60 | return ret; |
| 61 | } |
| 62 | } |
| 63 | |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 64 | |
| 65 | /* Read /proc/cpuinfo. Look for lines like these |
| 66 | |
| 67 | processor 0: version = FF, identification = 0117C9, machine = 2064 |
| 68 | |
| 69 | and return the machine model or NULL on error. |
| 70 | Adapted from function VG_(get_machine_model) in coregrind/m_machine.c */ |
| 71 | |
| 72 | typedef struct { |
| 73 | const char *cpuinfo_name; |
| 74 | const char *real_name; |
| 75 | } model_info; |
| 76 | |
| 77 | /* Array needs to be sorted chronologically. Oldest to newest */ |
| 78 | model_info models[] = { |
| 79 | { "2064", "z900" }, |
| 80 | { "2066", "z800" }, |
| 81 | { "2084", "z990" }, |
| 82 | { "2086", "z890" }, |
florian | 87a25cf | 2012-08-28 13:33:10 +0000 | [diff] [blame] | 83 | { "2094", "z9-EC" }, |
| 84 | { "2096", "z9-BC" }, |
| 85 | { "2097", "z10-EC" }, |
| 86 | { "2098", "z10-BC" }, |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 87 | { "2817", "z196" }, |
cborntra | 9dd35e0 | 2012-05-29 11:02:28 +0000 | [diff] [blame] | 88 | { "2818", "z114" }, |
florian | 87a25cf | 2012-08-28 13:33:10 +0000 | [diff] [blame] | 89 | { "2827", "zEC12" }, |
florian | 3b411c1 | 2013-07-28 15:29:36 +0000 | [diff] [blame] | 90 | { "2828", "zBC12" }, |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | |
| 94 | /* Locate a machine model by name. Name can be either the cpuinfo |
| 95 | name or the external name. */ |
| 96 | static model_info *locate_model(const char *name) |
| 97 | { |
| 98 | model_info *p; |
| 99 | |
| 100 | /* Try cpuinfo name first */ |
| 101 | for (p = models; p != models + sizeof models / sizeof models[0]; ++p) { |
| 102 | if (strcmp(p->cpuinfo_name, name) == 0) return p; // found it |
| 103 | } |
| 104 | |
| 105 | /* Now try external name */ |
| 106 | for (p = models; p != models + sizeof models / sizeof models[0]; ++p) { |
| 107 | if (strcmp(p->real_name, name) == 0) return p; // found it |
| 108 | } |
| 109 | |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static model_info *get_host(void) |
| 115 | { |
| 116 | int n, fh; |
| 117 | size_t num_bytes, file_buf_size; |
| 118 | char *p, *m, *model_name, *file_buf; |
| 119 | model_info *model; |
| 120 | |
| 121 | /* Slurp contents of /proc/cpuinfo into FILE_BUF */ |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 122 | fh = open("/proc/cpuinfo", O_RDONLY, S_IRUSR); |
| 123 | if (fh < 0) return NULL; |
| 124 | |
| 125 | /* Determine the size of /proc/cpuinfo. |
| 126 | Work around broken-ness in /proc file system implementation. |
| 127 | fstat returns a zero size for /proc/cpuinfo although it is |
| 128 | claimed to be a regular file. */ |
| 129 | num_bytes = 0; |
| 130 | file_buf_size = 1000; |
| 131 | file_buf = malloc(file_buf_size + 1); |
| 132 | |
| 133 | while (42) { |
| 134 | n = read(fh, file_buf, file_buf_size); |
| 135 | if (n < 0) break; |
| 136 | |
| 137 | num_bytes += n; |
| 138 | if (n < file_buf_size) break; /* reached EOF */ |
| 139 | } |
| 140 | |
| 141 | if (n < 0) num_bytes = 0; /* read error; ignore contents */ |
| 142 | |
| 143 | if (num_bytes > file_buf_size) { |
| 144 | free(file_buf); |
| 145 | lseek(fh, 0, SEEK_SET); |
| 146 | file_buf = malloc(num_bytes + 1); |
| 147 | n = read(fh, file_buf, num_bytes); |
| 148 | if (n < 0) num_bytes = 0; |
| 149 | } |
| 150 | |
| 151 | file_buf[num_bytes] = '\0'; |
| 152 | close(fh); |
| 153 | |
| 154 | /* Parse file */ |
| 155 | model = models + sizeof models / sizeof models[0]; |
| 156 | for (p = file_buf; *p; ++p) { |
| 157 | /* Beginning of line */ |
| 158 | if (strncmp(p, "processor", sizeof "processor" - 1 ) != 0) continue; |
| 159 | |
| 160 | m = strstr(p, "machine"); |
| 161 | if (m == NULL) continue; |
| 162 | |
| 163 | p = m + sizeof "machine" - 1; |
| 164 | while (isspace(*p) || *p == '=') { |
| 165 | if (*p == '\n') goto next_line; |
| 166 | ++p; |
| 167 | } |
| 168 | |
| 169 | model_name = p; |
| 170 | for (n = 0; n < sizeof models / sizeof models[0]; ++n) { |
| 171 | model_info *mm = models + n; |
| 172 | size_t len = strlen(mm->cpuinfo_name); |
| 173 | if (strncmp(mm->cpuinfo_name, model_name, len) == 0 && |
| 174 | isspace(model_name[len])) { |
| 175 | /* In case there are different CPUs in this cluster return the |
| 176 | one with the dewest capabilities ("oldest" model). */ |
| 177 | if (mm < model) model = mm; |
| 178 | p = model_name + len; |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | /* Skip until end-of-line */ |
| 183 | while (*p != '\n') |
| 184 | ++p; |
| 185 | next_line: ; |
| 186 | } |
| 187 | |
| 188 | free(file_buf); |
| 189 | |
| 190 | if (model == models + sizeof models / sizeof models[0]) return NULL; |
| 191 | |
| 192 | return model; |
| 193 | } |
| 194 | |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 195 | |
| 196 | /* Convenience macro that maps the facility bit number as given in the |
| 197 | Principles of Ops "facility indications" section to a bit mask */ |
| 198 | #define FAC_BIT(x) (1ULL << (63 - (x))) |
| 199 | |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 200 | static int go(char *feature, char *cpu) |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 201 | { |
| 202 | unsigned long long facilities; |
| 203 | unsigned long long match; |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 204 | model_info *host, *from, *to, *p; |
| 205 | char *colon; |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 206 | |
| 207 | facilities = stfle(); |
| 208 | |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 209 | if (strcmp(feature, "s390x-zarch") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 210 | match = (facilities & FAC_BIT(1)) && (facilities & FAC_BIT(2)); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 211 | } else if (strcmp(feature, "s390x-n3") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 212 | match = facilities & FAC_BIT(0); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 213 | } else if (strcmp(feature, "s390x-stfle") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 214 | match = facilities & FAC_BIT(7); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 215 | } else if (strcmp(feature, "s390x-ldisp") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 216 | match = (facilities & FAC_BIT(18)) && (facilities & FAC_BIT(19)); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 217 | } else if (strcmp(feature, "s390x-eimm") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 218 | match = facilities & FAC_BIT(21); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 219 | } else if (strcmp(feature, "s390x-stckf") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 220 | match = facilities & FAC_BIT(25); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 221 | } else if (strcmp(feature, "s390x-genins") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 222 | match = facilities & FAC_BIT(34); |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 223 | } else if (strcmp(feature, "s390x-exrl") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 224 | match = facilities & FAC_BIT(35); |
florian | 4b345f3 | 2012-07-19 14:35:55 +0000 | [diff] [blame] | 225 | } else if (strcmp(feature, "s390x-etf3") == 0 ) { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 226 | match = facilities & FAC_BIT(30); |
florian | 370bc77 | 2012-08-30 20:30:32 +0000 | [diff] [blame] | 227 | } else if (strcmp(feature, "s390x-fpext") == 0 ) { |
| 228 | match = facilities & FAC_BIT(37); |
florian | 4682598 | 2012-11-10 22:35:24 +0000 | [diff] [blame] | 229 | } else if (strcmp(feature, "s390x-dfp") == 0 ) { |
| 230 | match = facilities & FAC_BIT(42); |
florian | 1106692 | 2013-05-11 15:05:04 +0000 | [diff] [blame] | 231 | } else if (strcmp(feature, "s390x-pfpo") == 0 ) { |
| 232 | match = facilities & FAC_BIT(44); |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 233 | } else { |
florian | 4bc35a8 | 2012-08-30 18:47:56 +0000 | [diff] [blame] | 234 | return 2; // Unrecognised feature. |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 235 | } |
| 236 | |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 237 | if (match == 0) return 1; // facility not provided |
| 238 | |
| 239 | /* Host provides facility. If no CPU was specified, we're done. */ |
| 240 | if (cpu == NULL) return 0; |
| 241 | |
| 242 | host = get_host(); |
cborntra | 9dd35e0 | 2012-05-29 11:02:28 +0000 | [diff] [blame] | 243 | if (host == NULL) return 1; // unknown model |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 244 | |
| 245 | // printf("host = %s (%s)\n", host->cpuinfo_name, host->real_name); |
| 246 | |
| 247 | /* Determine interval of models in which to search for HOST. */ |
| 248 | from = to = NULL; |
| 249 | colon = strchr(cpu, ':'); |
| 250 | |
| 251 | if (colon == NULL) { |
| 252 | // match exact |
| 253 | from = to = locate_model(cpu); |
| 254 | } else if (colon == cpu) { |
| 255 | // :NAME match machines up to and including CPU |
| 256 | from = models; |
| 257 | to = locate_model(cpu + 1); |
| 258 | } else if (colon[1] == '\0') { |
| 259 | // NAME: match machines beginning with CPU or later |
| 260 | *colon = '\0'; |
| 261 | from = locate_model(cpu); |
| 262 | to = models + sizeof models / sizeof models[0] - 1; |
| 263 | *colon = ':'; |
| 264 | } else { |
| 265 | // NAME:NAME match machines in interval |
| 266 | *colon = '\0'; |
| 267 | from = locate_model(cpu); |
| 268 | to = locate_model(colon + 1); |
| 269 | *colon = ':'; |
| 270 | } |
| 271 | |
| 272 | if (from == NULL || to == NULL || from > to) { |
| 273 | fprintf(stderr, "invalid cpu specification '%s'\n", cpu); |
| 274 | return 3; |
| 275 | } |
| 276 | |
| 277 | #if 0 |
| 278 | printf("from %s (%s) to %s (%s)\n", from->cpuinfo_name, from->real_name, |
| 279 | to->cpuinfo_name, to->real_name); |
| 280 | #endif |
| 281 | |
| 282 | /* Search for HOST. */ |
| 283 | for (p = from; p <= to; ++p) { |
| 284 | if (p == host) return 0; |
| 285 | } |
| 286 | |
| 287 | return 1; // host does not match CPU specification |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | #else |
| 291 | |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 292 | static int go(char *feature, char *cpu) |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 293 | { |
| 294 | return 2; // Feature not recognised (non-s390x machine!) |
| 295 | } |
| 296 | |
| 297 | #endif |
| 298 | |
| 299 | |
| 300 | //--------------------------------------------------------------------------- |
| 301 | // main |
| 302 | //--------------------------------------------------------------------------- |
| 303 | int main(int argc, char **argv) |
| 304 | { |
florian | a066ccd | 2012-09-02 03:30:38 +0000 | [diff] [blame] | 305 | int rc, inverted = 0; |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 306 | |
| 307 | if (argc < 2 || argc > 3) { |
| 308 | fprintf( stderr, "usage: s390x_features <feature> [<machine-model>]\n" ); |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 309 | exit(3); // Usage error. |
| 310 | } |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 311 | |
florian | a066ccd | 2012-09-02 03:30:38 +0000 | [diff] [blame] | 312 | if (argv[1][0] == '!') { |
| 313 | assert(argv[2] == NULL); // not allowed |
| 314 | inverted = 1; |
| 315 | ++argv[1]; |
| 316 | } |
| 317 | |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 318 | rc = go(argv[1], argv[2]); |
florian | a066ccd | 2012-09-02 03:30:38 +0000 | [diff] [blame] | 319 | |
| 320 | if (inverted) { |
| 321 | switch (rc) { |
| 322 | case 0: rc = 1; break; |
| 323 | case 1: rc = 0; break; |
| 324 | case 2: rc = 2; break; |
| 325 | } |
| 326 | } |
florian | ec82dd9 | 2011-07-07 22:09:41 +0000 | [diff] [blame] | 327 | |
| 328 | // printf("rc = %d\n", rc); |
| 329 | |
| 330 | return rc; |
sewardj | 02b89e8 | 2011-05-17 16:19:53 +0000 | [diff] [blame] | 331 | } |