]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/Diagnostics.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / osfmk / i386 / Diagnostics.c
index 3023aeef9cd98eab84a68bbd90658f59924b82cc..34acf876fda93824f8b22fdcb9569b4aa72d6478 100644 (file)
@@ -71,7 +71,6 @@
 #include <mach/i386/syscall_sw.h>
 #include <kern/kalloc.h>
 #include <sys/kdebug.h>
-
 #include <i386/machine_cpu.h>
 #include <i386/misc_protos.h>
 #include <i386/cpuid.h>
@@ -80,7 +79,7 @@
 
 diagWork        dgWork;
 uint64_t        lastRuptClear = 0ULL;
-
+boolean_t      diag_pmc_enabled = FALSE;
 void cpu_powerstats(void *);
 
 typedef struct {
@@ -98,6 +97,7 @@ typedef struct {
 } core_energy_stat_t;
 
 typedef struct {
+       uint64_t pkes_version;
        uint64_t pkg_cres[2][7];
        uint64_t pkg_power_unit;
        uint64_t pkg_energy;
@@ -130,6 +130,7 @@ diagCall64(x86_saved_state_t * state)
 
        assert(is_saved_state64(state));
        regs = saved_state64(state);
+
        diagflag = ((dgWork.dgFlags & enaDiagSCs) != 0);
        selector = regs->rdi;
 
@@ -154,7 +155,6 @@ diagCall64(x86_saved_state_t * state)
 
                (void) copyout((char *) &real_ncpus, data, sizeof(real_ncpus)); /* Copy out number of
                                                                                 * processors */
-
                currNap = mach_absolute_time(); /* Get the time now */
                durNap = currNap - lastRuptClear;       /* Get the last interval
                                                         * duration */
@@ -176,6 +176,7 @@ diagCall64(x86_saved_state_t * state)
                }
                rval = 1;
                break;
+
        case dgPowerStat:
        {
                uint32_t c2l = 0, c2h = 0, c3l = 0, c3h = 0, c6l = 0, c6h = 0, c7l = 0, c7h = 0;
@@ -187,6 +188,7 @@ diagCall64(x86_saved_state_t * state)
                bzero(&pkes, sizeof(pkes));
                bzero(&cest, sizeof(cest));
 
+               pkes.pkes_version = 1ULL;
                rdmsr_carefully(MSR_IA32_PKG_C2_RESIDENCY, &c2l, &c2h);
                rdmsr_carefully(MSR_IA32_PKG_C3_RESIDENCY, &c3l, &c3h);
                rdmsr_carefully(MSR_IA32_PKG_C6_RESIDENCY, &c6l, &c6h);
@@ -283,21 +285,27 @@ diagCall64(x86_saved_state_t * state)
        case dgEnaPMC:
        {
                boolean_t enable = TRUE;
-               mp_cpus_call(CPUMASK_ALL, ASYNC, cpu_pmc_control, &enable);
+               uint32_t cpuinfo[4];
+               /* Require architectural PMC v2 or higher, corresponding to
+                * Merom+, or equivalent virtualised facility.
+                */
+               do_cpuid(0xA, &cpuinfo[0]);
+               if ((cpuinfo[0] & 0xFF) >= 2) {
+                       mp_cpus_call(CPUMASK_ALL, ASYNC, cpu_pmc_control, &enable);
+                       diag_pmc_enabled = TRUE;
+               }
                rval = 1;
        }
        break;
-
 #if    DEBUG
        case dgGzallocTest:
        {
                (void) ml_set_interrupts_enabled(TRUE);
-               if (diagflag == 0)
-                       break;
-
-               unsigned *ptr = (unsigned *)kalloc(1024);
-               kfree(ptr, 1024);
-               *ptr = 0x42;
+               if (diagflag) {
+                       unsigned *ptr = (unsigned *)kalloc(1024);
+                       kfree(ptr, 1024);
+                       *ptr = 0x42;
+               }
        }
        break;
 #endif
@@ -306,21 +314,18 @@ diagCall64(x86_saved_state_t * state)
        case    dgPermCheck:
        {
                (void) ml_set_interrupts_enabled(TRUE);
-               if (diagflag == 0)
-                       break;
-
-               rval = pmap_permissions_verify(kernel_pmap, kernel_map, 0, ~0ULL);
+               if (diagflag)
+                       rval = pmap_permissions_verify(kernel_pmap, kernel_map, 0, ~0ULL);
        }
                break;
 #endif /* PERMIT_PERMCHECK */
-
        default:                /* Handle invalid ones */
                rval = 0;       /* Return an exception */
        }
 
        regs->rax = rval;
 
-       return rval;            /* Normal non-ast check return */
+       return rval;
 }
 
 void cpu_powerstats(__unused void *arg) {
@@ -346,13 +351,15 @@ void cpu_powerstats(__unused void *arg) {
 
        rdmsr_carefully(MSR_IA32_CORE_C7_RESIDENCY, &cl, &ch);
        cdp->cpu_c7res = ((uint64_t)ch << 32) | cl;
-       
-       uint64_t insns = read_pmc(FIXED_PMC0);
-       uint64_t ucc = read_pmc(FIXED_PMC1);
-       uint64_t urc = read_pmc(FIXED_PMC2);
-       cdp->cpu_cur_insns = insns;
-       cdp->cpu_cur_ucc = ucc;
-       cdp->cpu_cur_urc = urc;
+
+       if (diag_pmc_enabled) {
+               uint64_t insns = read_pmc(FIXED_PMC0);
+               uint64_t ucc = read_pmc(FIXED_PMC1);
+               uint64_t urc = read_pmc(FIXED_PMC2);
+               cdp->cpu_cur_insns = insns;
+               cdp->cpu_cur_ucc = ucc;
+               cdp->cpu_cur_urc = urc;
+       }
 }
 
 void cpu_pmc_control(void *enablep) {