1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
|
#include "stress-ng.h"
#include "core-arch.h"
#include "core-builtin.h"
#include "core-cpu.h"
#include "core-put.h"
static const stress_help_t help[] = {
{ NULL, "priv-instr N", "start N workers exercising privileged instruction" },
{ NULL, "priv-instr-ops N", "stop after N bogo instruction operations" },
{ NULL, NULL, NULL }
};
typedef void (*op_func_t)(void);
typedef struct {
const char *instr;
const op_func_t op_func;
bool invalid;
bool trapped;
} op_info_t;
#if defined(STRESS_ARCH_ARM) && \
defined(HAVE_ASM_ARM_TLBI)
#define HAVE_PRIV_INSTR
static void stress_arm_tlbi(void)
{
__asm__ __volatile__("tlbi vmalle1is");
}
static op_info_t op_info[] =
{
{ "tlbi", stress_arm_tlbi, false, false },
};
#endif
#if defined(STRESS_ARCH_ALPHA) && \
(defined(HAVE_ASM_ALPHA_DRAINA) || \
defined(HAVE_ASM_ALPHA_HALT))
#define HAVE_PRIV_INSTR
#define PAL_halt 0
#define PAL_draina 2
#if defined(HAVE_ASM_ALPHA_DRAINA)
static void stress_alpha_draina(void)
{
__asm__ __volatile__("call_pal %0 #draina" : : "i" (PAL_draina) : "memory");
}
#endif
#if defined(HAVE_ASM_ALPHA_HALT)
static void stress_alpha_halt(void)
{
__asm__ __volatile__("call_pal %0 #halt" : : "i" (PAL_halt));
}
#endif
static op_info_t op_info[] =
{
#if defined(HAVE_ASM_ALPHA_DRAINA)
{ "call_pal %0 #draina", stress_alpha_draina, false, false },
#endif
#if defined(HAVE_ASM_ALPHA_HALT)
{ "call_pal %0 #halt", stress_alpha_halt, false, false },
#endif
};
#endif
#if defined(STRESS_ARCH_HPPA) && \
(defined(HAVE_ASM_HPPA_DIAG) || \
defined(HAVE_ASM_HPPA_RFI))
#define HAVE_PRIV_INSTR
#if defined(HAVE_ASM_HPPA_DIAG)
static void stress_hppa_diag(void)
{
__asm__ __volatile__("diag 0");
}
#endif
#if defined(HAVE_ASM_HPPA_RFI)
static void stress_hppa_rfi(void)
{
__asm__ __volatile__("rfi");
}
#endif
static op_info_t op_info[] =
{
#if defined(HAVE_ASM_HPPA_DIAG)
{ "diag", stress_hppa_diag, false, false },
#endif
#if defined(HAVE_ASM_HPPA_RFI)
{ "rfi", stress_hppa_rfi, false, false },
#endif
};
#endif
#if defined(STRESS_ARCH_LOONG64)
#define HAVE_PRIV_INSTR
#if defined(HAVE_ASM_LOONG64_TLBRD)
static void stress_loong64_tlbrd(void)
{
__asm__ __volatile__("tlbrd");
}
#endif
#if defined(HAVE_ASM_LOONG64_TLBSRCH)
static void stress_loong64_tlbsrch(void)
{
__asm__ __volatile__("tlbsrch");
}
#endif
static op_info_t op_info[] =
{
#if defined(HAVE_ASM_LOONG64_TLBRD)
{ "tlbrd", stress_loong64_tlbrd, false, false },
#endif
#if defined(HAVE_ASM_LOONG64_TLBSRCH)
{ "tlbsrch", stress_loong64_tlbsrch, false, false },
#endif
};
#endif
#if defined(STRESS_ARCH_M68K) && \
defined(HAVE_ASM_M68K_EORI_SR)
#define HAVE_PRIV_INSTR
static void stress_m68k_sr(void)
{
__asm__ __volatile__("eori.w #0001,%sr");
}
static op_info_t op_info[] =
{
{ "eori.w #1,sr", stress_m68k_sr, false, false },
};
#endif
#if defined(STRESS_ARCH_MIPS) && \
defined(HAVE_ASM_MIPS_WAIT)
#define HAVE_PRIV_INSTR
static void stress_mips_wait(void)
{
__asm__ __volatile__("wait");
}
static op_info_t op_info[] =
{
{ "wait", stress_mips_wait, false, false },
};
#endif
#if defined(STRESS_ARCH_PPC64) && \
defined(HAVE_ASM_PPC64_TLBIE)
#define HAVE_PRIV_INSTR
#define HAVE_PRIV_PAGE
static void *page;
static void stress_ppc64_tlbie(void)
{
unsigned long int address = (unsigned long int)page;
__asm__ __volatile__("tlbie %0, 0" : : "r" (address) : "memory");
}
static op_info_t op_info[] =
{
{ "tlbie", stress_ppc64_tlbie, false, false },
};
#endif
#if defined(STRESS_ARCH_RISCV) && \
defined(HAVE_ASM_RISCV_SFENCE_VMA)
#define HAVE_PRIV_INSTR
static void stress_riscv_sfence_vma(void)
{
__asm__ __volatile__("sfence.vma" : : : "memory");
}
static op_info_t op_info[] =
{
{ "sfence.vma", stress_riscv_sfence_vma, false, false },
};
#endif
#if defined(STRESS_ARCH_S390) && \
defined(HAVE_ASM_S390_PTLB)
#define HAVE_PRIV_INSTR
static void stress_s390_ptlb(void)
{
__asm__ __volatile__("ptlb" : : : "memory");
}
static op_info_t op_info[] =
{
{ "ptlb", stress_s390_ptlb, false, false },
};
#endif
#if defined(STRESS_ARCH_SH4) && \
(defined(HAVE_ASM_SH4_RTE) || \
defined(HAVE_ASM_SH4_SLEEP))
#define HAVE_PRIV_INSTR
#if defined(HAVE_ASM_SH4_RTE)
static void stress_sh4_rte(void)
{
__asm__ __volatile__("rte");
}
#endif
#if defined(HAVE_ASM_SH4_SLEEP)
static void stress_sh4_sleep(void)
{
__asm__ __volatile__("sleep");
}
#endif
static op_info_t op_info[] =
{
#if defined(HAVE_ASM_SH4_RTE)
{ "rte", stress_sh4_rte, false, false },
#endif
#if defined(HAVE_ASM_SH4_SLEEP)
{ "sleep", stress_sh4_sleep, false, false },
#endif
};
#endif
#if defined(STRESS_ARCH_SPARC) && \
defined(HAVE_ASM_SPARC_RDPR)
#define HAVE_PRIV_INSTR
static void stress_sparc_rdpr(void)
{
unsigned long int ver;
__asm__ __volatile__("rdpr %%ver, %0" : "=r" (ver));
}
static op_info_t op_info[] =
{
{ "rdpr", stress_sparc_rdpr, false, false },
};
#endif
#if defined(STRESS_ARCH_X86) && \
(defined(HAVE_ASM_X86_CLTS) || \
defined(HAVE_ASM_X86_HLT) || \
defined(HAVE_ASM_X86_INVD) || \
defined(HAVE_ASM_X86_INVLPG) || \
defined(HAVE_ASM_X86_LGDT) || \
defined(HAVE_ASM_X86_LLDT) || \
defined(HAVE_ASM_X86_LMSW) || \
defined(HAVE_ASM_X86_MOV_CR0) || \
defined(HAVE_ASM_X86_MOV_DR0) || \
defined(HAVE_ASM_X86_RDMSR) || \
defined(HAVE_ASM_X86_RDPMC) || \
defined(HAVE_ASM_X86_WBINVD) || \
defined(HAVE_ASM_X86_WRMSR))
#define HAVE_PRIV_INSTR
#define HAVE_PRIV_PAGE
static void *page;
#if defined(HAVE_ASM_X86_CLTS)
static void stress_x86_clts(void)
{
__asm__ __volatile__("clts");
}
#endif
#if defined(HAVE_ASM_X86_HLT)
static void stress_x86_hlt(void)
{
__asm__ __volatile__("hlt");
}
#endif
#if defined(HAVE_ASM_X86_INVD)
static void stress_x86_invd(void)
{
__asm__ __volatile__("invd");
}
#endif
#if defined(HAVE_ASM_X86_INVLPG)
static void stress_x86_invlpg(void)
{
if (page != MAP_FAILED)
__asm__ __volatile__("invlpg (%0)" ::"r" (page) : "memory");
}
#endif
#if defined(HAVE_ASM_X86_LGDT)
static void stress_x86_lgdt(void)
{
if (page != MAP_FAILED)
__asm__ __volatile__("lgdt (%0)" ::"r" (page));
}
#endif
#if defined(HAVE_ASM_X86_LLDT)
static void stress_x86_lldt(void)
{
uint16_t src = 0;
__asm__ __volatile__("lldt %0" ::"r" (src));
}
#endif
#if defined(HAVE_ASM_X86_LMSW)
static void stress_x86_lmsw(void)
{
uint16_t src = 0;
__asm__ __volatile__("lmsw %0" ::"r" (src));
}
#endif
#if defined(HAVE_ASM_X86_MOV_CR0)
static void stress_x86_mov_cr0(void)
{
unsigned long int cr0;
__asm__ __volatile__("mov %%cr0, %0" : "=r"(cr0) : : "memory");
}
#endif
#if defined(HAVE_ASM_X86_MOV_DR0)
static void stress_x86_mov_dr0(void)
{
unsigned long int dr0;
__asm__ __volatile__("mov %%dr0, %0" : "=r"(dr0) : : "memory");
}
#endif
#if defined(HAVE_ASM_X86_RDMSR)
static void stress_x86_rdmsr(void)
{
uint32_t msr = 0xc0000080;
uint32_t lo;
uint32_t hi;
__asm__ __volatile__("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr));
}
#endif
#if defined(HAVE_ASM_X86_RDPMC)
static void stress_x86_rdpmc(void)
{
uint32_t lo, hi, counter = 0;
__asm__ __volatile__("rdpmc" : "=a" (lo), "=d" (hi) : "c" (counter));
}
#endif
#if defined(HAVE_ASM_X86_WBINVD)
static void stress_x86_wbinvd(void)
{
__asm__ __volatile__("wbinvd");
}
#endif
#if defined(HAVE_ASM_X86_WRMSR)
static void stress_x86_wrmsr(void)
{
uint32_t msr = 0xc0000080;
uint32_t lo = 0;
uint32_t hi = 0;
__asm__ __volatile__("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
}
#endif
static op_info_t op_info[] =
{
#if defined(HAVE_ASM_X86_CLTS)
{ "clts", stress_x86_clts, false, false },
#endif
#if defined(HAVE_ASM_X86_HLT)
{ "hlt", stress_x86_hlt, false, false },
#endif
#if defined(HAVE_ASM_X86_INVD)
{ "invd", stress_x86_invd, false, false },
#endif
#if defined(HAVE_ASM_X86_INVLPG)
{ "invlpg", stress_x86_invlpg, false, false },
#endif
#if defined(HAVE_ASM_X86_LGDT)
{ "lgdt", stress_x86_lgdt, false, false },
#endif
#if defined(HAVE_ASM_X86_LLDT)
{ "lldt", stress_x86_lldt, false, false },
#endif
#if defined(HAVE_ASM_X86_LMSW)
{ "lmsw", stress_x86_lmsw, false, false },
#endif
#if defined(HAVE_ASM_X86_MOV_CR0)
{ "mov cr0", stress_x86_mov_cr0, false, false },
#endif
#if defined(HAVE_ASM_X86_MOV_DR0)
{ "mov dr0", stress_x86_mov_dr0, false, false },
#endif
#if defined(HAVE_ASM_X86_RDMSR)
{ "rdmsr", stress_x86_rdmsr, false, false },
#endif
#if defined(HAVE_ASM_X86_RDPMC)
{ "rdpmc", stress_x86_rdpmc, false, false },
#endif
#if defined(HAVE_ASM_X86_WBINVD)
{ "wbinvd", stress_x86_wbinvd, false, false },
#endif
#if defined(HAVE_ASM_X86_WRMSR)
{ "wrmsr", stress_x86_wrmsr, false, false },
#endif
};
#endif
#if defined(HAVE_PRIV_INSTR)
static sigjmp_buf jmp_env;
static size_t idx = 0;
static double t_start, duration, count;
static inline void stress_sigsegv_handler(int signum)
{
(void)signum;
duration += stress_time_now() - t_start;
count += 1.0;
op_info[idx].trapped = true;
idx++;
siglongjmp(jmp_env, 1);
}
#if defined(SIGILL) || \
defined(SIGBUS)
static void stress_sigill_handler(int signum)
{
op_info[idx].invalid = true;
stress_sigsegv_handler(signum);
}
#endif
static int stress_priv_instr(stress_args_t *args)
{
size_t i, len;
int ret;
double rate;
idx = 0;
duration = 0.0;
count = 0.0;
#if defined(HAVE_PRIV_PAGE)
page = mmap(NULL, args->page_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if (page != MAP_FAILED)
stress_set_vma_anon_name(page, args->page_size, "priv-page");
#endif
if (stress_sighandler(args->name, SIGSEGV, stress_sigsegv_handler, NULL))
return EXIT_NO_RESOURCE;
#if defined(SIGILL)
if (stress_sighandler(args->name, SIGILL, stress_sigill_handler, NULL))
return EXIT_NO_RESOURCE;
#endif
#if defined(SIGBUS)
if (stress_sighandler(args->name, SIGBUS, stress_sigill_handler, NULL))
return EXIT_NO_RESOURCE;
#endif
for (i = 0; i < SIZEOF_ARRAY(op_info); i++) {
op_info[i].invalid = false;
op_info[i].trapped = false;
}
stress_set_proc_state(args->name, STRESS_STATE_SYNC_WAIT);
stress_sync_start_wait(args);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
ret = sigsetjmp(jmp_env, 1);
if (UNLIKELY(!stress_continue(args)))
goto finish;
} while (ret == 1);
do {
if (idx >= SIZEOF_ARRAY(op_info))
idx = 0;
stress_bogo_inc(args);
if (op_info[idx].op_func) {
t_start = stress_time_now();
op_info[idx].op_func();
}
idx++;
} while (stress_continue(args));
finish:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
for (len = 0, i = 0; i < SIZEOF_ARRAY(op_info); i++) {
if (!op_info[i].trapped)
len += strlen(op_info[i].instr) + 3;
}
if (len > 0) {
char *str;
str = (char *)calloc(len, sizeof(*str));
if (str) {
int unhandled = 0;
(void)shim_memset(str, 0, len);
for (i = 0; i < SIZEOF_ARRAY(op_info); i++) {
if (!op_info[i].trapped) {
unhandled++;
if (!*str) {
(void)shim_strscpy(str, op_info[i].instr, len);
} else {
(void)shim_strlcat(str, ", ", len);
(void)shim_strlcat(str, op_info[i].instr, len);
}
}
}
pr_inf("%s: %d unhandled instructions: %s\n", args->name, unhandled, str);
free(str);
}
}
rate = (count > 0.0) ? (duration / count) : 0.0;
stress_metrics_set(args, 0, "nanosecs per privileged op trap",
STRESS_DBL_NANOSECOND * rate, STRESS_METRIC_HARMONIC_MEAN);
#if defined(HAVE_PRIV_PAGE)
if (page != MAP_FAILED)
(void)munmap(page, args->page_size);
#endif
if ((stress_bogo_get(args) > 1) && (count < 1.0)) {
pr_fail("%s: attempted to execute %" PRIu64 " privileged instructions, trapped none.\n",
args->name, stress_bogo_get(args));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
const stressor_info_t stress_priv_instr_info = {
.stressor = stress_priv_instr,
.class = CLASS_CPU,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
const stressor_info_t stress_priv_instr_info = {
.stressor = stress_unimplemented,
.class = CLASS_CPU,
.verify = VERIFY_ALWAYS,
.help = help,
.unimplemented_reason = "no privileged op-code test for this architecture"
};
#endif
|