This test uses the PMU to count branch prediction hits/misses for a
known loop, and compare the result to the reported spectre v2
mitigation.
This gives us a way of sanity checking that the reported mitigation is
actually in effect.
Sample output for some cases, eg:
Power9:
sysfs reports: 'Vulnerable'
PM_BR_PRED_CCACHE: result 368 running/enabled 5792777124
PM_BR_MPRED_CCACHE: result 319 running/enabled 5792775546
PM_BR_PRED_PCACHE: result 2147483281 running/enabled 5792773128
PM_BR_MPRED_PCACHE: result 213604201 running/enabled 5792771640
Miss percent 9 %
OK - Measured branch prediction rates match reported spectre v2 mitigation.
sysfs reports: 'Mitigation: Indirect branch serialisation (kernel only)'
PM_BR_PRED_CCACHE: result 895 running/enabled 5780320920
PM_BR_MPRED_CCACHE: result 822 running/enabled 5780312414
PM_BR_PRED_PCACHE: result 2147482754 running/enabled 5780308836
PM_BR_MPRED_PCACHE: result 213639731 running/enabled 5780307912
Miss percent 9 %
OK - Measured branch prediction rates match reported spectre v2 mitigation.
sysfs reports: 'Mitigation: Indirect branch cache disabled'
PM_BR_PRED_CCACHE: result 2147483649 running/enabled 20540186160
PM_BR_MPRED_CCACHE: result 2147483649 running/enabled 20540180056
PM_BR_PRED_PCACHE: result 0 running/enabled 20540176090
PM_BR_MPRED_PCACHE: result 0 running/enabled 20540174182
Miss percent 100 %
OK - Measured branch prediction rates match reported spectre v2 mitigation.
Power8:
sysfs reports: 'Vulnerable'
PM_BR_PRED_CCACHE: result 2147483649 running/enabled 3505888142
PM_BR_MPRED_CCACHE: result 9 running/enabled 3505882788
Miss percent 0 %
OK - Measured branch prediction rates match reported spectre v2 mitigation.
sysfs reports: 'Mitigation: Indirect branch cache disabled'
PM_BR_PRED_CCACHE: result 2147483649 running/enabled 16931421988
PM_BR_MPRED_CCACHE: result 2147483649 running/enabled 16931416478
Miss percent 100 %
OK - Measured branch prediction rates match reported spectre v2 mitigation.
success: spectre_v2
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20190520105520.22274-1-mpe@ellerman.id.au
115 lines
2.8 KiB
C
115 lines
2.8 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
/*
|
|
* Copyright 2013, Michael Ellerman, IBM Corp.
|
|
*/
|
|
|
|
#ifndef _SELFTESTS_POWERPC_UTILS_H
|
|
#define _SELFTESTS_POWERPC_UTILS_H
|
|
|
|
#define __cacheline_aligned __attribute__((aligned(128)))
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <linux/auxvec.h>
|
|
#include <linux/perf_event.h>
|
|
#include "reg.h"
|
|
|
|
/* Avoid headaches with PRI?64 - just use %ll? always */
|
|
typedef unsigned long long u64;
|
|
typedef signed long long s64;
|
|
|
|
/* Just for familiarity */
|
|
typedef uint32_t u32;
|
|
typedef uint16_t u16;
|
|
typedef uint8_t u8;
|
|
|
|
void test_harness_set_timeout(uint64_t time);
|
|
int test_harness(int (test_function)(void), char *name);
|
|
|
|
int read_auxv(char *buf, ssize_t buf_size);
|
|
void *find_auxv_entry(int type, char *auxv);
|
|
void *get_auxv_entry(int type);
|
|
|
|
int pick_online_cpu(void);
|
|
|
|
int read_debugfs_file(char *debugfs_file, int *result);
|
|
int write_debugfs_file(char *debugfs_file, int result);
|
|
int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
|
|
void set_dscr(unsigned long val);
|
|
int perf_event_open_counter(unsigned int type,
|
|
unsigned long config, int group_fd);
|
|
int perf_event_enable(int fd);
|
|
int perf_event_disable(int fd);
|
|
int perf_event_reset(int fd);
|
|
|
|
static inline bool have_hwcap(unsigned long ftr)
|
|
{
|
|
return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
|
|
}
|
|
|
|
#ifdef AT_HWCAP2
|
|
static inline bool have_hwcap2(unsigned long ftr2)
|
|
{
|
|
return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
|
|
}
|
|
#else
|
|
static inline bool have_hwcap2(unsigned long ftr2)
|
|
{
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
bool is_ppc64le(void);
|
|
|
|
/* Yes, this is evil */
|
|
#define FAIL_IF(x) \
|
|
do { \
|
|
if ((x)) { \
|
|
fprintf(stderr, \
|
|
"[FAIL] Test FAILED on line %d\n", __LINE__); \
|
|
return 1; \
|
|
} \
|
|
} while (0)
|
|
|
|
/* The test harness uses this, yes it's gross */
|
|
#define MAGIC_SKIP_RETURN_VALUE 99
|
|
|
|
#define SKIP_IF(x) \
|
|
do { \
|
|
if ((x)) { \
|
|
fprintf(stderr, \
|
|
"[SKIP] Test skipped on line %d\n", __LINE__); \
|
|
return MAGIC_SKIP_RETURN_VALUE; \
|
|
} \
|
|
} while (0)
|
|
|
|
#define SKIP_IF_MSG(x, msg) \
|
|
do { \
|
|
if ((x)) { \
|
|
fprintf(stderr, \
|
|
"[SKIP] Test skipped on line %d: %s\n", \
|
|
__LINE__, msg); \
|
|
return MAGIC_SKIP_RETURN_VALUE; \
|
|
} \
|
|
} while (0)
|
|
|
|
#define _str(s) #s
|
|
#define str(s) _str(s)
|
|
|
|
/* POWER9 feature */
|
|
#ifndef PPC_FEATURE2_ARCH_3_00
|
|
#define PPC_FEATURE2_ARCH_3_00 0x00800000
|
|
#endif
|
|
|
|
#if defined(__powerpc64__)
|
|
#define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
|
|
#define UCONTEXT_MSR(UC) (UC)->uc_mcontext.gp_regs[PT_MSR]
|
|
#elif defined(__powerpc__)
|
|
#define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
|
|
#define UCONTEXT_MSR(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
|
|
#else
|
|
#error implement UCONTEXT_NIA
|
|
#endif
|
|
|
|
#endif /* _SELFTESTS_POWERPC_UTILS_H */
|