+ next = address + length;
+}
+
+/* Copy a routine into comm page if it matches running machine.
+ */
+static void
+commpage_stuff_routine(
+ commpage_descriptor *rd )
+{
+ commpage_stuff(rd->commpage_address,rd->code_address,rd->code_length);
+}
+
+/* Fill in the 32- or 64-bit commpage. Called once for each.
+ */
+
+static void
+commpage_populate_one(
+ 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
+ commpage_address_t base_offset, // will become commPageBaseOffset
+ commpage_time_data** time_data, // &time_data32 or &time_data64
+ const char* signature, // "commpage 32-bit" or "commpage 64-bit"
+ vm_prot_t uperm)
+{
+ uint8_t c1;
+ uint16_t c2;
+ int c4;
+ uint64_t c8;
+ uint32_t cfamily;
+ short version = _COMM_PAGE_THIS_VERSION;
+
+ next = 0;
+ commPagePtr = (char *)commpage_allocate( submap, (vm_size_t) area_used, uperm );
+ *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.
+ * Note: the 32-bit cpu_capabilities vector is retained in addition to
+ * the expanded 64-bit vector.
+ */
+ commpage_stuff(_COMM_PAGE_SIGNATURE,signature,(int)MIN(_COMM_PAGE_SIGNATURELEN, strlen(signature)));
+ commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES64,&_cpu_capabilities,sizeof(_cpu_capabilities));
+ commpage_stuff(_COMM_PAGE_VERSION,&version,sizeof(short));
+ commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES,&_cpu_capabilities,sizeof(uint32_t));
+
+ c2 = 32; // default
+ if (_cpu_capabilities & kCache64)
+ c2 = 64;
+ else if (_cpu_capabilities & kCache128)
+ c2 = 128;
+ commpage_stuff(_COMM_PAGE_CACHE_LINESIZE,&c2,2);
+
+ c4 = MP_SPIN_TRIES;
+ commpage_stuff(_COMM_PAGE_SPIN_COUNT,&c4,4);
+
+ /* machine_info valid after ml_get_max_cpus() */
+ c1 = machine_info.physical_cpu_max;
+ commpage_stuff(_COMM_PAGE_PHYSICAL_CPUS,&c1,1);
+ c1 = machine_info.logical_cpu_max;
+ commpage_stuff(_COMM_PAGE_LOGICAL_CPUS,&c1,1);
+
+ c8 = ml_cpu_cache_size(0);
+ commpage_stuff(_COMM_PAGE_MEMORY_SIZE, &c8, 8);
+
+ cfamily = cpuid_info()->cpuid_cpufamily;
+ commpage_stuff(_COMM_PAGE_CPUFAMILY, &cfamily, 4);
+
+ if (next > _COMM_PAGE_END)
+ panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%p", next, commPagePtr);
+
+}
+
+
+/* Fill in commpages: called once, during kernel initialization, from the
+ * startup thread before user-mode code is running.
+ *
+ * See the top of this file for a list of what you have to do to add
+ * a new routine to the commpage.
+ */
+
+void
+commpage_populate( void )
+{
+ commpage_init_cpu_capabilities();
+
+ commpage_populate_one( commpage32_map,
+ &commPagePtr32,
+ _COMM_PAGE32_AREA_USED,
+ _COMM_PAGE32_BASE_ADDRESS,
+ &time_data32,
+ "commpage 32-bit",
+ VM_PROT_READ);
+#ifndef __LP64__
+ pmap_commpage32_init((vm_offset_t) commPagePtr32, _COMM_PAGE32_BASE_ADDRESS,
+ _COMM_PAGE32_AREA_USED/INTEL_PGBYTES);
+#endif
+ time_data64 = time_data32; /* if no 64-bit commpage, point to 32-bit */
+
+ if (_cpu_capabilities & k64Bit) {
+ commpage_populate_one( commpage64_map,
+ &commPagePtr64,
+ _COMM_PAGE64_AREA_USED,
+ _COMM_PAGE32_START_ADDRESS, /* commpage address are relative to 32-bit commpage placement */
+ &time_data64,
+ "commpage 64-bit",
+ VM_PROT_READ);
+#ifndef __LP64__
+ pmap_commpage64_init((vm_offset_t) commPagePtr64, _COMM_PAGE64_BASE_ADDRESS,
+ _COMM_PAGE64_AREA_USED/INTEL_PGBYTES);
+#endif
+ }
+
+ simple_lock_init(&commpage_active_cpus_lock, 0);
+
+ commpage_update_active_cpus();
+ commpage_mach_approximate_time_init();
+ commpage_mach_continuous_time_init();
+ commpage_boottime_init();
+ rtc_nanotime_init_commpage();
+ commpage_update_kdebug_state();
+#if CONFIG_ATM
+ commpage_update_atm_diagnostic_config(atm_get_diagnostic_config());
+#endif
+}
+
+/* Fill in the common routines during kernel initialization.
+ * This is called before user-mode code is running.
+ */
+void commpage_text_populate( void ){
+ commpage_descriptor **rd;
+
+ next = 0;
+ commPagePtr = (char *) commpage_allocate(commpage_text32_map, (vm_size_t) _COMM_PAGE_TEXT_AREA_USED, VM_PROT_READ | VM_PROT_EXECUTE);
+ commPageTextPtr32 = commPagePtr;
+
+ char *cptr = commPagePtr;
+ int i=0;
+ for(; i< _COMM_PAGE_TEXT_AREA_USED; i++){
+ cptr[i]=0xCC;
+ }
+
+ commPageBaseOffset = _COMM_PAGE_TEXT_START;
+ for (rd = commpage_32_routines; *rd != NULL; rd++) {
+ commpage_stuff_routine(*rd);
+ }
+
+#ifndef __LP64__
+ pmap_commpage32_init((vm_offset_t) commPageTextPtr32, _COMM_PAGE_TEXT_START,
+ _COMM_PAGE_TEXT_AREA_USED/INTEL_PGBYTES);
+#endif
+
+ if (_cpu_capabilities & k64Bit) {
+ next = 0;
+ commPagePtr = (char *) commpage_allocate(commpage_text64_map, (vm_size_t) _COMM_PAGE_TEXT_AREA_USED, VM_PROT_READ | VM_PROT_EXECUTE);
+ commPageTextPtr64 = commPagePtr;
+
+ cptr=commPagePtr;
+ for(i=0; i<_COMM_PAGE_TEXT_AREA_USED; i++){
+ cptr[i]=0xCC;
+ }
+
+ for (rd = commpage_64_routines; *rd !=NULL; rd++) {
+ commpage_stuff_routine(*rd);
+ }
+
+#ifndef __LP64__
+ pmap_commpage64_init((vm_offset_t) commPageTextPtr64, _COMM_PAGE_TEXT_START,
+ _COMM_PAGE_TEXT_AREA_USED/INTEL_PGBYTES);
+#endif
+ }
+
+ if (next > _COMM_PAGE_TEXT_END)
+ panic("commpage text overflow: next=0x%08x, commPagePtr=%p", next, commPagePtr);
+
+}
+
+/* Update commpage nanotime information.
+ *
+ * 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 != 0) && ((_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;
+}
+
+
+/* Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure() */
+
+void
+commpage_set_memory_pressure(
+ unsigned int pressure )
+{
+ char *cp;
+ uint32_t *ip;
+
+ cp = commPagePtr32;
+ if ( cp ) {
+ cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_BASE_ADDRESS);
+ ip = (uint32_t*) (void *) cp;
+ *ip = (uint32_t) pressure;
+ }
+
+ cp = commPagePtr64;
+ if ( cp ) {
+ cp += (_COMM_PAGE_MEMORY_PRESSURE - _COMM_PAGE32_START_ADDRESS);
+ ip = (uint32_t*) (void *) cp;
+ *ip = (uint32_t) pressure;
+ }
+