]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/i386/commpage/commpage.c
xnu-1228.9.59.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage.c
index 328e095ab65b6a0a38a5389f30fd364bcd778b49..b2bd5af50c07347ce031452dd3b3ed610e18096b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
@@ -50,6 +50,8 @@
 #include <mach/vm_map.h>
 #include <i386/machine_routines.h>
 #include <i386/misc_protos.h>
+#include <i386/tsc.h>
+#include <i386/cpu_data.h>
 #include <machine/cpu_capabilities.h>
 #include <machine/commpage.h>
 #include <machine/pmap.h>
@@ -67,8 +69,8 @@ extern        commpage_descriptor*    commpage_64_routines[];
 extern commpage_descriptor sigdata_descriptor;
 extern commpage_descriptor *ba_descriptors[];
 
-extern vm_map_t        com_region_map32;       // the shared submap, set up in vm init
-extern vm_map_t        com_region_map64;       // the shared submap, set up in vm init
+extern vm_map_t        commpage32_map; // the shared submap, set up in vm init
+extern vm_map_t        commpage64_map; // the shared submap, set up in vm init
 
 char   *commPagePtr32 = NULL;          // virtual addr in kernel map of 32-bit commpage
 char   *commPagePtr64 = NULL;          // ...and of 64-bit commpage
@@ -76,9 +78,6 @@ int     _cpu_capabilities = 0;          // define the capability vector
 
 int    noVMX = 0;              /* if true, do not set kHasAltivec in ppc _cpu_capabilities */
 
-void*  dsmos_blobs[3];         /* ptrs to the system integrity data in each commpage */
-int    dsmos_blob_count = 0;
-
 static uintptr_t next;                 // next available byte in comm page
 static int             cur_routine;            // comm page address of "current" routine
 static int             matched;                // true if we've found a match for "current" routine
@@ -86,6 +85,9 @@ static int            matched;                // true if we've found a match for "current" routine
 static char    *commPagePtr;           // virtual addr in kernel map of commpage we are working on
 static size_t  commPageBaseOffset;     // add to 32-bit runtime address to get offset in commpage
 
+static commpage_time_data      *time_data32 = NULL;
+static commpage_time_data      *time_data64 = NULL;
+
 /* Allocate the commpage and add to the shared submap created by vm:
  *     1. allocate a page in the kernel map (RW)
  *     2. wire it down
@@ -95,10 +97,10 @@ static size_t       commPageBaseOffset;     // add to 32-bit runtime address to get offset
 
 static  void*
 commpage_allocate( 
-       vm_map_t        submap,                 // com_region_map32 or com_region_map64
+       vm_map_t        submap,                 // commpage32_map or commpage_map64
        size_t          area_used )             // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
 {
-       vm_offset_t     kernel_addr;            // address of commpage in kernel map
+       vm_offset_t     kernel_addr = 0;        // address of commpage in kernel map
        vm_offset_t     zero = 0;
        vm_size_t       size = area_used;       // size actually populated
        vm_map_entry_t  entry;
@@ -107,7 +109,7 @@ commpage_allocate(
        if (submap == NULL)
                panic("commpage submap is null");
 
-       if (vm_allocate(kernel_map,&kernel_addr,area_used,VM_FLAGS_ANYWHERE))
+       if (vm_map(kernel_map,&kernel_addr,area_used,0,VM_FLAGS_ANYWHERE,NULL,0,FALSE,VM_PROT_ALL,VM_PROT_ALL,VM_INHERIT_NONE))
                panic("cannot allocate commpage");
 
        if (vm_map_wire(kernel_map,kernel_addr,kernel_addr+area_used,VM_PROT_DEFAULT,FALSE))
@@ -128,7 +130,7 @@ commpage_allocate(
        if (mach_make_memory_entry( kernel_map,         // target map
                                    &size,              // size 
                                    kernel_addr,        // offset (address in kernel map)
-                                   VM_PROT_DEFAULT,    // map it RW
+                                   VM_PROT_ALL,        // map it RWX
                                    &handle,            // this is the object handle we get
                                    NULL ))             // parent_entry (what is this?)
                panic("cannot make entry for commpage");
@@ -141,8 +143,8 @@ commpage_allocate(
                        handle,                         // port is the memory entry we just made
                        0,                              // offset (map 1st page in memory entry)
                        FALSE,                          // copy
-                       VM_PROT_READ,                   // cur_protection (R-only in user map)
-                       VM_PROT_READ,                   // max_protection
+                       VM_PROT_READ|VM_PROT_EXECUTE,   // cur_protection (R-only in user map)
+                       VM_PROT_READ|VM_PROT_EXECUTE,   // max_protection
                        VM_INHERIT_SHARE ))             // inheritance
                panic("cannot map commpage");
 
@@ -191,6 +193,12 @@ commpage_init_cpu_capabilities( void )
        ml_cpu_get_info(&cpu_info);
        
        switch (cpu_info.vector_unit) {
+               case 8:
+                       bits |= kHasSSE4_2;
+                       /* fall thru */
+               case 7:
+                       bits |= kHasSSE4_1;
+                       /* fall thru */
                case 6:
                        bits |= kHasSupplementalSSE3;
                        /* fall thru */
@@ -233,11 +241,14 @@ commpage_init_cpu_capabilities( void )
        if (cpu_mode_is64bit())                 // k64Bit means processor is 64-bit capable
                bits |= k64Bit;
 
+       if (tscFreq <= SLOW_TSC_THRESHOLD)      /* is TSC too slow for _commpage_nanotime?  */
+               bits |= kSlow;
+
        _cpu_capabilities = bits;               // set kernel version for use by drivers etc
 }
 
 int
-_get_cpu_capabilities()
+_get_cpu_capabilities(void)
 {
        return _cpu_capabilities;
 }
@@ -253,7 +264,7 @@ commpage_stuff(
     void       *dest = commpage_addr_of(address);
     
     if ((uintptr_t)dest < next)
-        panic("commpage overlap at address 0x%x, 0x%x < 0x%x", address, dest, next);
+        panic("commpage overlap at address 0x%x, %p < 0x%lx", address, dest, next);
     
     bcopy(source,dest,length);
     
@@ -305,7 +316,7 @@ commpage_stuff_routine(
     
     if (rd->commpage_address != cur_routine) {
         if ((cur_routine!=0) && (matched==0))
-            panic("commpage no match for last, next address %08x", rd->commpage_address);
+            panic("commpage no match for last, next address %08lx", rd->commpage_address);
         cur_routine = rd->commpage_address;
         matched = 0;
     }
@@ -315,7 +326,7 @@ commpage_stuff_routine(
     
     if ((must == rd->musthave) && (cant == 0)) {
         if (matched)
-            panic("commpage multiple matches for address %08x", rd->commpage_address);
+            panic("commpage multiple matches for address %08lx", rd->commpage_address);
         matched = 1;
         
         commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
@@ -329,12 +340,13 @@ commpage_stuff_routine(
 
 static void
 commpage_populate_one( 
-       vm_map_t        submap,         // com_region_map32 or com_region_map64
+       vm_map_t        submap,         // commpage32_map or compage64_map
        char **         kernAddressPtr, // &commPagePtr32 or &commPagePtr64
        size_t          area_used,      // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
        size_t          base_offset,    // will become commPageBaseOffset
        commpage_descriptor** commpage_routines, // list of routine ptrs for this commpage
        boolean_t       legacy,         // true if 32-bit commpage
+       commpage_time_data** time_data, // &time_data32 or &time_data64
        const char*     signature )     // "commpage 32-bit" or "commpage 64-bit"
 {
        short   c2;
@@ -349,6 +361,8 @@ commpage_populate_one(
        commPagePtr = (char *)commpage_allocate( submap, (vm_size_t) area_used );
        *kernAddressPtr = commPagePtr;                          // save address either in commPagePtr32 or 64
        commPageBaseOffset = base_offset;
+       
+       *time_data = commpage_addr_of( _COMM_PAGE_TIME_DATA_START );
 
        /* Stuff in the constants.  We move things into the comm page in strictly
        * ascending order, so we can check for overlap and panic if so.
@@ -390,7 +404,7 @@ commpage_populate_one(
                panic("commpage no match on last routine");
 
        if (next > (uintptr_t)_COMM_PAGE_END)
-               panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%08x", next, (uintptr_t)commPagePtr);
+               panic("commpage overflow: next = 0x%08lx, commPagePtr = 0x%08lx", next, (uintptr_t)commPagePtr);
 
        if ( legacy ) {
                next = (uintptr_t) NULL;
@@ -400,10 +414,6 @@ commpage_populate_one(
                next = (uintptr_t) NULL;
                commpage_stuff_routine(&sigdata_descriptor);
        }       
-
-       /* salt away a ptr to the system integrity data in this commpage */
-       dsmos_blobs[dsmos_blob_count++] = 
-               commpage_addr_of( _COMM_PAGE_SYSTEM_INTEGRITY );
 }
 
 
@@ -419,23 +429,27 @@ commpage_populate( void )
 {
        commpage_init_cpu_capabilities();
        
-       commpage_populate_one(  com_region_map32
+       commpage_populate_one(  commpage32_map
                                &commPagePtr32,
                                _COMM_PAGE32_AREA_USED,
                                _COMM_PAGE32_BASE_ADDRESS,
                                commpage_32_routines, 
                                TRUE,                   /* legacy (32-bit) commpage */
+                               &time_data32,
                                "commpage 32-bit");
        pmap_commpage32_init((vm_offset_t) commPagePtr32, _COMM_PAGE32_BASE_ADDRESS, 
                           _COMM_PAGE32_AREA_USED/INTEL_PGBYTES);
+                          
+       time_data64 = time_data32;                      /* if no 64-bit commpage, point to 32-bit */
 
        if (_cpu_capabilities & k64Bit) {
-               commpage_populate_one(  com_region_map64
+               commpage_populate_one(  commpage64_map
                                        &commPagePtr64,
                                        _COMM_PAGE64_AREA_USED,
                                        _COMM_PAGE32_START_ADDRESS, /* because kernel is built 32-bit */
                                        commpage_64_routines, 
                                        FALSE,          /* not a legacy commpage */
+                                       &time_data64,
                                        "commpage 64-bit");
                pmap_commpage64_init((vm_offset_t) commPagePtr64, _COMM_PAGE64_BASE_ADDRESS, 
                                   _COMM_PAGE64_AREA_USED/INTEL_PGBYTES);
@@ -443,3 +457,101 @@ commpage_populate( void )
 
        rtc_nanotime_init_commpage();
 }
+
+
+/* Update commpage nanotime information.  Note that we interleave
+ * setting the 32- and 64-bit commpages, in order to keep nanotime more
+ * nearly in sync between the two environments.
+ *
+ * This routine must be serialized by some external means, ie a lock.
+ */
+
+void
+commpage_set_nanotime(
+       uint64_t        tsc_base,
+       uint64_t        ns_base,
+       uint32_t        scale,
+       uint32_t        shift )
+{
+       commpage_time_data      *p32 = time_data32;
+       commpage_time_data      *p64 = time_data64;
+       static uint32_t generation = 0;
+       uint32_t        next_gen;
+       
+       if (p32 == NULL)                /* have commpages been allocated yet? */
+               return;
+               
+       if ( generation != p32->nt_generation )
+               panic("nanotime trouble 1");    /* possibly not serialized */
+       if ( ns_base < p32->nt_ns_base )
+               panic("nanotime trouble 2");
+       if ((shift != 32) && ((_cpu_capabilities & kSlow)==0) )
+               panic("nanotime trouble 3");
+               
+       next_gen = ++generation;
+       if (next_gen == 0)
+               next_gen = ++generation;
+       
+       p32->nt_generation = 0;         /* mark invalid, so commpage won't try to use it */
+       p64->nt_generation = 0;
+       
+       p32->nt_tsc_base = tsc_base;
+       p64->nt_tsc_base = tsc_base;
+       
+       p32->nt_ns_base = ns_base;
+       p64->nt_ns_base = ns_base;
+       
+       p32->nt_scale = scale;
+       p64->nt_scale = scale;
+       
+       p32->nt_shift = shift;
+       p64->nt_shift = shift;
+       
+       p32->nt_generation = next_gen;  /* mark data as valid */
+       p64->nt_generation = next_gen;
+}
+
+
+/* Disable commpage gettimeofday(), forcing commpage to call through to the kernel.  */
+
+void
+commpage_disable_timestamp( void )
+{
+       time_data32->gtod_generation = 0;
+       time_data64->gtod_generation = 0;
+}
+
+
+/* Update commpage gettimeofday() information.  As with nanotime(), we interleave
+ * updates to the 32- and 64-bit commpage, in order to keep time more nearly in sync 
+ * between the two environments.
+ *
+ * This routine must be serializeed by some external means, ie a lock.
+ */
+ void
+ commpage_set_timestamp(
+       uint64_t        abstime,
+       uint64_t        secs )
+{
+       commpage_time_data      *p32 = time_data32;
+       commpage_time_data      *p64 = time_data64;
+       static uint32_t generation = 0;
+       uint32_t        next_gen;
+       
+       next_gen = ++generation;
+       if (next_gen == 0)
+               next_gen = ++generation;
+       
+       p32->gtod_generation = 0;               /* mark invalid, so commpage won't try to use it */
+       p64->gtod_generation = 0;
+       
+       p32->gtod_ns_base = abstime;
+       p64->gtod_ns_base = abstime;
+       
+       p32->gtod_sec_base = secs;
+       p64->gtod_sec_base = secs;
+       
+       p32->gtod_generation = next_gen;        /* mark data as valid */
+       p64->gtod_generation = next_gen;
+}