1/*
2 * Copyright 2014, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <setjmp.h>
9#include <signal.h>
10
11#include "ebb.h"
12
13
14/*
15 * Test that closing the EBB event clears MMCR0_PMCC, preventing further access
16 * by userspace to the PMU hardware.
17 */
18
19int close_clears_pmcc(void)
20{
21	struct event event;
22
23	event_init_named(&event, 0x1001e, "cycles");
24	event_leader_ebb_init(&event);
25
26	FAIL_IF(event_open(&event));
27
28	ebb_enable_pmc_counting(1);
29	setup_ebb_handler(standard_ebb_callee);
30	ebb_global_enable();
31	FAIL_IF(ebb_event_enable(&event));
32
33	mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
34
35	while (ebb_state.stats.ebb_count < 1)
36		FAIL_IF(core_busy_loop());
37
38	ebb_global_disable();
39	event_close(&event);
40
41	FAIL_IF(ebb_state.stats.ebb_count == 0);
42
43	/* The real test is here, do we take a SIGILL when writing PMU regs now
44	 * that we have closed the event. We expect that we will. */
45
46	FAIL_IF(catch_sigill(write_pmc1));
47
48	/* We should still be able to read EBB regs though */
49	mfspr(SPRN_EBBHR);
50	mfspr(SPRN_EBBRR);
51	mfspr(SPRN_BESCR);
52
53	return 0;
54}
55
56int main(void)
57{
58	return test_harness(close_clears_pmcc, "close_clears_pmcc");
59}
60