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