Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/powerpc: refactor entry and rfi_flush tests

For simplicity in backporting, the original entry_flush test contained
a lot of duplicated code from the rfi_flush test. De-duplicate that code.

Signed-off-by: Daniel Axtens <dja@axtens.net>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Daniel Axtens and committed by
Michael Ellerman
0d239f3b 89a83a0c

+96 -120
+5
tools/testing/selftests/powerpc/include/utils.h
··· 42 42 int perf_event_disable(int fd); 43 43 int perf_event_reset(int fd); 44 44 45 + struct perf_event_read { 46 + __u64 nr; 47 + __u64 l1d_misses; 48 + }; 49 + 45 50 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30) 46 51 #include <unistd.h> 47 52 #include <sys/syscall.h>
+2
tools/testing/selftests/powerpc/security/Makefile
··· 11 11 12 12 $(OUTPUT)/spectre_v2: CFLAGS += -m64 13 13 $(OUTPUT)/spectre_v2: ../pmu/event.c branch_loops.S 14 + $(OUTPUT)/rfi_flush: flush_utils.c 15 + $(OUTPUT)/entry_flush: flush_utils.c
+1 -60
tools/testing/selftests/powerpc/security/entry_flush.c
··· 15 15 #include <string.h> 16 16 #include <stdio.h> 17 17 #include "utils.h" 18 - 19 - #define CACHELINE_SIZE 128 20 - 21 - struct perf_event_read { 22 - __u64 nr; 23 - __u64 l1d_misses; 24 - }; 25 - 26 - static inline __u64 load(void *addr) 27 - { 28 - __u64 tmp; 29 - 30 - asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr)); 31 - 32 - return tmp; 33 - } 34 - 35 - static void syscall_loop(char *p, unsigned long iterations, 36 - unsigned long zero_size) 37 - { 38 - for (unsigned long i = 0; i < iterations; i++) { 39 - for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE) 40 - load(p + j); 41 - getppid(); 42 - } 43 - } 44 - 45 - static void sigill_handler(int signr, siginfo_t *info, void *unused) 46 - { 47 - static int warned; 48 - ucontext_t *ctx = (ucontext_t *)unused; 49 - unsigned long *pc = &UCONTEXT_NIA(ctx); 50 - 51 - /* mtspr 3,RS to check for move to DSCR below */ 52 - if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) { 53 - if (!warned++) 54 - printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n"); 55 - *pc += 4; 56 - } else { 57 - printf("SIGILL at %p\n", pc); 58 - abort(); 59 - } 60 - } 61 - 62 - static void set_dscr(unsigned long val) 63 - { 64 - static int init; 65 - struct sigaction sa; 66 - 67 - if (!init) { 68 - memset(&sa, 0, sizeof(sa)); 69 - sa.sa_sigaction = sigill_handler; 70 - sa.sa_flags = SA_SIGINFO; 71 - if (sigaction(SIGILL, &sa, NULL)) 72 - perror("sigill_handler"); 73 - init = 1; 74 - } 75 - 76 - asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR)); 77 - } 18 + #include "flush_utils.h" 78 19 79 20 int entry_flush_test(void) 80 21 {
+70
tools/testing/selftests/powerpc/security/flush_utils.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + 3 + /* 4 + * Copyright 2018 IBM Corporation. 5 + */ 6 + 7 + #define __SANE_USERSPACE_TYPES__ 8 + 9 + #include <sys/types.h> 10 + #include <stdint.h> 11 + #include <unistd.h> 12 + #include <signal.h> 13 + #include <stdlib.h> 14 + #include <string.h> 15 + #include <stdio.h> 16 + #include "utils.h" 17 + #include "flush_utils.h" 18 + 19 + static inline __u64 load(void *addr) 20 + { 21 + __u64 tmp; 22 + 23 + asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr)); 24 + 25 + return tmp; 26 + } 27 + 28 + void syscall_loop(char *p, unsigned long iterations, 29 + unsigned long zero_size) 30 + { 31 + for (unsigned long i = 0; i < iterations; i++) { 32 + for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE) 33 + load(p + j); 34 + getppid(); 35 + } 36 + } 37 + 38 + static void sigill_handler(int signr, siginfo_t *info, void *unused) 39 + { 40 + static int warned; 41 + ucontext_t *ctx = (ucontext_t *)unused; 42 + unsigned long *pc = &UCONTEXT_NIA(ctx); 43 + 44 + /* mtspr 3,RS to check for move to DSCR below */ 45 + if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) { 46 + if (!warned++) 47 + printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n"); 48 + *pc += 4; 49 + } else { 50 + printf("SIGILL at %p\n", pc); 51 + abort(); 52 + } 53 + } 54 + 55 + void set_dscr(unsigned long val) 56 + { 57 + static int init; 58 + struct sigaction sa; 59 + 60 + if (!init) { 61 + memset(&sa, 0, sizeof(sa)); 62 + sa.sa_sigaction = sigill_handler; 63 + sa.sa_flags = SA_SIGINFO; 64 + if (sigaction(SIGILL, &sa, NULL)) 65 + perror("sigill_handler"); 66 + init = 1; 67 + } 68 + 69 + asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR)); 70 + }
+17
tools/testing/selftests/powerpc/security/flush_utils.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0+ */ 2 + 3 + /* 4 + * Copyright 2018 IBM Corporation. 5 + */ 6 + 7 + #ifndef _SELFTESTS_POWERPC_SECURITY_FLUSH_UTILS_H 8 + #define _SELFTESTS_POWERPC_SECURITY_FLUSH_UTILS_H 9 + 10 + #define CACHELINE_SIZE 128 11 + 12 + void syscall_loop(char *p, unsigned long iterations, 13 + unsigned long zero_size); 14 + 15 + void set_dscr(unsigned long val); 16 + 17 + #endif /* _SELFTESTS_POWERPC_SECURITY_FLUSH_UTILS_H */
+1 -60
tools/testing/selftests/powerpc/security/rfi_flush.c
··· 10 10 #include <stdint.h> 11 11 #include <malloc.h> 12 12 #include <unistd.h> 13 - #include <signal.h> 14 13 #include <stdlib.h> 15 14 #include <string.h> 16 15 #include <stdio.h> 17 16 #include "utils.h" 17 + #include "flush_utils.h" 18 18 19 - #define CACHELINE_SIZE 128 20 - 21 - struct perf_event_read { 22 - __u64 nr; 23 - __u64 l1d_misses; 24 - }; 25 - 26 - static inline __u64 load(void *addr) 27 - { 28 - __u64 tmp; 29 - 30 - asm volatile("ld %0,0(%1)" : "=r"(tmp) : "b"(addr)); 31 - 32 - return tmp; 33 - } 34 - 35 - static void syscall_loop(char *p, unsigned long iterations, 36 - unsigned long zero_size) 37 - { 38 - for (unsigned long i = 0; i < iterations; i++) { 39 - for (unsigned long j = 0; j < zero_size; j += CACHELINE_SIZE) 40 - load(p + j); 41 - getppid(); 42 - } 43 - } 44 - 45 - static void sigill_handler(int signr, siginfo_t *info, void *unused) 46 - { 47 - static int warned = 0; 48 - ucontext_t *ctx = (ucontext_t *)unused; 49 - unsigned long *pc = &UCONTEXT_NIA(ctx); 50 - 51 - /* mtspr 3,RS to check for move to DSCR below */ 52 - if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) { 53 - if (!warned++) 54 - printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n"); 55 - *pc += 4; 56 - } else { 57 - printf("SIGILL at %p\n", pc); 58 - abort(); 59 - } 60 - } 61 - 62 - static void set_dscr(unsigned long val) 63 - { 64 - static int init = 0; 65 - struct sigaction sa; 66 - 67 - if (!init) { 68 - memset(&sa, 0, sizeof(sa)); 69 - sa.sa_sigaction = sigill_handler; 70 - sa.sa_flags = SA_SIGINFO; 71 - if (sigaction(SIGILL, &sa, NULL)) 72 - perror("sigill_handler"); 73 - init = 1; 74 - } 75 - 76 - asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR)); 77 - } 78 19 79 20 int rfi_flush_test(void) 80 21 {