]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/perfmon.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / i386 / perfmon.c
index 5d6c0bc69729ed99b6196fc481b5a95481e92f85..a918f1fcbfac43dfe7b590598dec9bb215404229 100644 (file)
@@ -1,24 +1,31 @@
 /*
  * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
  * 
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apple Public Source License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
+ * This file contains Original Code and/or Modifications of Original Code 
+ * as defined in and that are subject to the Apple Public Source License 
+ * Version 2.0 (the 'License'). You may not use this file except in 
+ * compliance with the License.  The rights granted to you under the 
+ * License may not be used to create, or enable the creation or 
+ * redistribution of, unlawful or unlicensed copies of an Apple operating 
+ * system, or to circumvent, violate, or enable the circumvention or 
+ * violation of, any terms of an Apple operating system software license 
+ * agreement.
+ *
+ * Please obtain a copy of the License at 
+ * http://www.opensource.apple.com/apsl/ and read it before using this 
  * file.
- * 
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
+ *
+ * The Original Code and all software distributed under the License are 
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
+ * Please see the License for the specific language governing rights and 
  * limitations under the License.
- * 
- * @APPLE_LICENSE_HEADER_END@
+ *
+ * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
  */
 
 #include <mach/std_types.h>
 #include <i386/cpuid.h>
 #include <i386/lock.h>
 #include <vm/vm_kern.h>
+#include <kern/task.h>
 
-#ifdef DEBUG
+#if DEBUG
 #define        DBG(x...)       kprintf(x)
 #else
 #define        DBG(x...)
 #endif
 
+decl_simple_lock_data(,pmc_lock)
+static task_t  pmc_owner = TASK_NULL;
+static int     pmc_thread_count = 0;
+
+/* PMC Facility Owner:
+ * TASK_NULL - no one owns it
+ * kernel_task - owned by pmc
+ * other task - owned by another task
+ */
+
 /*
  * Table of ESCRs and addresses associated with performance counters/CCCRs.
  * See Intel SDM Vol 3, Table 15-4 (section 15.9):
@@ -275,24 +293,18 @@ pmc_p6_intr(void *state)
                        (*pmc_table->ovf_func[id])(id, state);
 }
 
-int
-pmc_init(void)
+void *
+pmc_alloc(void)
 {
        int             ret;
-       cpu_core_t      *my_core;
        pmc_table_t     *pmc_table;
        pmc_machine_t   pmc_type;
 
-       my_core = cpu_core();
-       assert(my_core);
-
        pmc_type = _pmc_machine_type();
        if (pmc_type == pmc_none) {
-               return KERN_FAILURE;
+               return NULL;
        }
        
-       pmc_table = (pmc_table_t *) my_core->pmc;
-       if (pmc_table == NULL) {
                ret = kmem_alloc(kernel_map,
                        (void *) &pmc_table, sizeof(pmc_table_t));
                if (ret != KERN_SUCCESS)
@@ -316,18 +328,10 @@ pmc_init(void)
                default:
                        break;
                }
-               if (!atomic_cmpxchg((uint32_t *) &my_core->pmc,
-                                   0, (uint32_t) pmc_table)) {
-                       kmem_free(kernel_map,
-                                 (vm_offset_t) pmc_table, sizeof(pmc_table_t));
-               }
-       }
-       DBG("pmc_init() done for cpu %d my_core->pmc=0x%x type=%d\n",
-               cpu_number(), my_core->pmc, pmc_type);
-
-       return KERN_SUCCESS;
+       return (void *) pmc_table;
 }
 
+
 static inline pmc_table_t *
 pmc_table_valid(pmc_id_t id)
 {
@@ -551,3 +555,81 @@ pmc_set_ovf_func(pmc_id_t id, pmc_ovf_func_t func)
 
        return KERN_SUCCESS;
 }
+
+int
+pmc_acquire(task_t task)
+{
+       kern_return_t retval = KERN_SUCCESS;
+  
+       simple_lock(&pmc_lock);
+  
+       if(pmc_owner == task) {
+               DBG("pmc_acquire - "
+                   "ACQUIRED: already owner\n");
+               retval = KERN_SUCCESS;
+               /* already own it */
+       } else if(pmc_owner == TASK_NULL) { /* no one owns it */
+               pmc_owner = task;
+               pmc_thread_count = 0;
+               DBG("pmc_acquire - "
+                   "ACQUIRED: no current owner - made new owner\n");
+               retval = KERN_SUCCESS;
+       } else { /* someone already owns it */
+               if(pmc_owner == kernel_task) {
+                       if(pmc_thread_count == 0) {
+                               /* kernel owns it but no threads using it */
+                               pmc_owner = task;
+                               pmc_thread_count = 0;
+                               DBG("pmc_acquire - "
+                                   "ACQUIRED: owned by kernel, no threads\n");
+                               retval = KERN_SUCCESS;
+                       } else {
+                               DBG("pmc_acquire - "
+                                   "DENIED: owned by kernel, in use\n");
+                               retval = KERN_RESOURCE_SHORTAGE;
+                       }
+               } else { /* non-kernel owner */
+                       DBG("pmc_acquire - "    
+                           "DENIED: owned by another task\n");
+                       retval = KERN_RESOURCE_SHORTAGE;
+               }
+       }
+  
+       simple_unlock(&pmc_lock);
+       return retval;
+}
+
+int
+pmc_release(task_t task)
+{
+       kern_return_t retval = KERN_SUCCESS;
+       task_t old_pmc_owner = pmc_owner;
+  
+       simple_lock(&pmc_lock);
+  
+       if(task != pmc_owner) {
+               retval = KERN_NO_ACCESS;
+       } else {
+               if(old_pmc_owner == kernel_task) {
+                       if(pmc_thread_count>0) {
+                               DBG("pmc_release - "
+                                   "NOT RELEASED: owned by kernel, in use\n");
+                               retval = KERN_NO_ACCESS;
+                       } else {
+                               DBG("pmc_release - "
+                                   "RELEASED: was owned by kernel\n");
+                               pmc_owner = TASK_NULL;
+                               retval = KERN_SUCCESS;
+                       }
+               } else {
+                       DBG("pmc_release - "
+                           "RELEASED: was owned by user\n");
+                       pmc_owner = TASK_NULL;
+                       retval = KERN_SUCCESS;
+               }
+       }
+
+       simple_unlock(&pmc_lock);
+       return retval;
+}
+