1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2022, Madhavan Srinivasan, IBM Corp.
4  */
5 
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 
11 #include "../event.h"
12 #include "misc.h"
13 #include "utils.h"
14 
15 extern void thirty_two_instruction_loop(int loops);
16 
17 static bool is_hv;
18 
sig_usr2_handler(int signum,siginfo_t * info,void * data)19 static void sig_usr2_handler(int signum, siginfo_t *info, void *data)
20 {
21 	ucontext_t *uctx = data;
22 
23 	is_hv = !!(uctx->uc_mcontext.gp_regs[PT_MSR] & MSR_HV);
24 }
25 
26 /*
27  * A perf sampling test for mmcr2
28  * fields : fcs, fch.
29  */
mmcr2_fcs_fch(void)30 static int mmcr2_fcs_fch(void)
31 {
32 	struct sigaction sigact = {
33 		.sa_sigaction = sig_usr2_handler,
34 		.sa_flags = SA_SIGINFO
35 	};
36 	struct event event;
37 	u64 *intr_regs;
38 
39 	FAIL_IF(sigaction(SIGUSR2, &sigact, NULL));
40 	FAIL_IF(kill(getpid(), SIGUSR2));
41 
42 	/* Check for platform support for the test */
43 	SKIP_IF(check_pvr_for_sampling_tests());
44 
45 	/* Init the event for the sampling test */
46 	event_init_sampling(&event, 0x1001e);
47 	event.attr.sample_regs_intr = platform_extended_mask;
48 	event.attr.exclude_kernel = 1;
49 	FAIL_IF(event_open(&event));
50 	event.mmap_buffer = event_sample_buf_mmap(event.fd, 1);
51 
52 	FAIL_IF(event_enable(&event));
53 
54 	/* workload to make the event overflow */
55 	thirty_two_instruction_loop(10000);
56 
57 	FAIL_IF(event_disable(&event));
58 
59 	/* Check for sample count */
60 	FAIL_IF(!collect_samples(event.mmap_buffer));
61 
62 	intr_regs = get_intr_regs(&event, event.mmap_buffer);
63 
64 	/* Check for intr_regs */
65 	FAIL_IF(!intr_regs);
66 
67 	/*
68 	 * Verify that fcs and fch field of MMCR2 match
69 	 * with corresponding modifier fields.
70 	 */
71 	if (is_hv)
72 		FAIL_IF(event.attr.exclude_kernel !=
73 			get_mmcr2_fch(get_reg_value(intr_regs, "MMCR2"), 1));
74 	else
75 		FAIL_IF(event.attr.exclude_kernel !=
76 			get_mmcr2_fcs(get_reg_value(intr_regs, "MMCR2"), 1));
77 
78 	event_close(&event);
79 	return 0;
80 }
81 
main(void)82 int main(void)
83 {
84 	return test_harness(mmcr2_fcs_fch, "mmcr2_fcs_fch");
85 }
86