Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2014, Michael Ellerman, IBM Corp.
0004  */
0005 
0006 #include <sched.h>
0007 #include <signal.h>
0008 #include <stdbool.h>
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 
0012 #include "ebb.h"
0013 
0014 
0015 /*
0016  * Test that the kernel properly handles PMAE across context switches.
0017  *
0018  * We test this by calling into the kernel inside our EBB handler, where PMAE
0019  * is clear. A cpu eater companion thread is running on the same CPU as us to
0020  * encourage the scheduler to switch us.
0021  *
0022  * The kernel must make sure that when it context switches us back in, it
0023  * honours the fact that we had PMAE clear.
0024  *
0025  * Observed to hit the failing case on the first EBB with a broken kernel.
0026  */
0027 
0028 static bool mmcr0_mismatch;
0029 static uint64_t before, after;
0030 
0031 static void syscall_ebb_callee(void)
0032 {
0033     uint64_t val;
0034 
0035     val = mfspr(SPRN_BESCR);
0036     if (!(val & BESCR_PMEO)) {
0037         ebb_state.stats.spurious++;
0038         goto out;
0039     }
0040 
0041     ebb_state.stats.ebb_count++;
0042     count_pmc(1, sample_period);
0043 
0044     before = mfspr(SPRN_MMCR0);
0045 
0046     /* Try and get ourselves scheduled, to force a PMU context switch */
0047     sched_yield();
0048 
0049     after = mfspr(SPRN_MMCR0);
0050     if (before != after)
0051         mmcr0_mismatch = true;
0052 
0053 out:
0054     reset_ebb();
0055 }
0056 
0057 static int test_body(void)
0058 {
0059     struct event event;
0060 
0061     SKIP_IF(!ebb_is_supported());
0062 
0063     event_init_named(&event, 0x1001e, "cycles");
0064     event_leader_ebb_init(&event);
0065 
0066     event.attr.exclude_kernel = 1;
0067     event.attr.exclude_hv = 1;
0068     event.attr.exclude_idle = 1;
0069 
0070     FAIL_IF(event_open(&event));
0071 
0072     setup_ebb_handler(syscall_ebb_callee);
0073     ebb_global_enable();
0074 
0075     FAIL_IF(ebb_event_enable(&event));
0076 
0077     mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
0078 
0079     while (ebb_state.stats.ebb_count < 20 && !mmcr0_mismatch)
0080         FAIL_IF(core_busy_loop());
0081 
0082     ebb_global_disable();
0083     ebb_freeze_pmcs();
0084 
0085     dump_ebb_state();
0086 
0087     if (mmcr0_mismatch)
0088         printf("Saw MMCR0 before 0x%lx after 0x%lx\n", before, after);
0089 
0090     event_close(&event);
0091 
0092     FAIL_IF(ebb_state.stats.ebb_count == 0);
0093     FAIL_IF(mmcr0_mismatch);
0094 
0095     return 0;
0096 }
0097 
0098 int pmae_handling(void)
0099 {
0100     return eat_cpu(test_body);
0101 }
0102 
0103 int main(void)
0104 {
0105     return test_harness(pmae_handling, "pmae_handling");
0106 }