WIP-cta-hacks
diff --git a/kern/arch/x86/apic.c b/kern/arch/x86/apic.c
index 9eadd5b..bdf71f5 100644
--- a/kern/arch/x86/apic.c
+++ b/kern/arch/x86/apic.c
@@ -142,6 +142,15 @@
 	 * the desired time. */
 	uint64_t ticks64 = (usec * __proc_global_info.bus_freq)
 	                   / LAPIC_TIMER_DIVISOR_VAL / 1000000;
+
+	// XXX had a bus_freq that was too small.  we used
+	// LAPIC_TIMER_DIVISOR_VAL and whatnot for the highest granularity or
+	// whatever, but this is really supposed to be based on bus freq. 
+	// 	actually, 32 was supposed to be reasonable
+	// 	12505216 = 12 MHz.  that is probably wrong... (computed)
+	if (!ticks64)
+		ticks64 = 1;
+
 	uint32_t ticks32 = ((ticks64 >> 32) ? 0xffffffff : ticks64);
 
 	assert(ticks32 > 0);
diff --git a/kern/arch/x86/apic9.c b/kern/arch/x86/apic9.c
index e5eb11e..49ab773 100644
--- a/kern/arch/x86/apic9.c
+++ b/kern/arch/x86/apic9.c
@@ -172,6 +172,7 @@
 		printd("apicinit%d: already initialised\n", apicno);
 		return;
 	}
+	// XXX we don't even use the old LAPIC_BASE anymore...?
 	assert(pa == LAPIC_PBASE);
 	apic->useable = 1;
 
@@ -181,6 +182,14 @@
 	 * akaros does its own remapping of hw <-> os coreid during smp_boot */
 
 	//X2APIC INIT
+	//XXX i think this is useless and wrong
+	//uh  which core are we running on?  this is core 0...  and all this
+	//function does is mark the apic usable if it wasn't before?
+	//also, this is the first time we jhaven't had an mp table, right?
+	//which might fuck up all of the shit
+	//	i.e. first time called from mpacpi or whatever
+	//	looks OK
+	//apiconline does this too, which is the real one
 	msr_val = read_msr(IA32_APIC_BASE);
 	write_msr(IA32_APIC_BASE, msr_val | (3<<10));
 }
@@ -248,11 +257,14 @@
 		printk("Bad apicno %d on HW core %d!!\n", apicno, hw_core_id());
 		return 0;
 	}
+	// XXX if we don't get this, we're fucked.  how are we even running?
+	// 	leftover turds from pre-x2apic?
+	// 	(do we need the lapic again, for AMD?
 	apic = &xlapic[apicno];
 	/* The addr check tells us if it is an IOAPIC or not... */
 	if (!apic->useable || apic->addr) {
-		printk("Unsuitable apicno %d on HW core %d!!\n", apicno,
-		       hw_core_id());
+		printk("Bad apic struct for apicno %d on HW core %d!! (useable %d, addr %p)\n",
+		       apicno, hw_core_id(), apic->useable, apic->addr);
 		return 0;
 	}
 	/* Things that can only be done when on the processor owning the APIC,
diff --git a/kern/arch/x86/mp.c b/kern/arch/x86/mp.c
index 5733be9..4312287 100644
--- a/kern/arch/x86/mp.c
+++ b/kern/arch/x86/mp.c
@@ -455,6 +455,7 @@
 		return r;
 #endif
 	r = sigscan(KADDR(0xe0000), 0x20000, signature);
+	// XXX found at 0
 	printk("Found MP table at %p\n", r);
 	if (r != NULL)
 		return r;
diff --git a/kern/arch/x86/mpacpi.c b/kern/arch/x86/mpacpi.c
index a5407a0..2fbaf7e 100644
--- a/kern/arch/x86/mpacpi.c
+++ b/kern/arch/x86/mpacpi.c
@@ -69,6 +69,28 @@
 			printd("apic proc %d/%d apicid %d %s\n", np - 1,
 			       apic->machno, st->lapic.id, already);
 			break;
+		case ASlx2apic:
+			// XXX hacked version of above
+			printd("ASlx2apic %d\n", st->lx2apic.id);
+			/* this table is supposed to have all of them if it
+			 * exists */
+			if (st->lx2apic.id > MaxAPICNO)
+				break;
+			apic = xlapic + st->lx2apic.id;
+			bp = (np++ == 0);
+			if (apic->useable) {
+				already = "(mp)";
+			} else if (ncleft != 0) {
+				ncleft--;
+				apicinit(st->lx2apic.id, mt->lapicpa, bp);
+			} else
+				already = "(off)";
+
+			// XXX machno is 0.  looks like an old plan9 thing
+			printd("apic proc %d/%d apicid %d %s\n", np - 1,
+			       apic->machno, st->lx2apic.id, already);
+			break;
+
 		case ASioapic:
 			printd("ASioapic %d\n", st->ioapic.id);
 			if (st->ioapic.id > Napic)
diff --git a/kern/arch/x86/topology.c b/kern/arch/x86/topology.c
index ea2a8c0..08101cf 100644
--- a/kern/arch/x86/topology.c
+++ b/kern/arch/x86/topology.c
@@ -110,11 +110,16 @@
 	for (int i = 0; i < apics->nchildren; i++) {
 		struct Apicst *temp = apics->children[i]->tbl;
 
-		if (temp != NULL && temp->type == ASlapic)
+		// XXX
+		if (temp != NULL && (temp->type == ASlapic || temp->type == ASlx2apic))
 			num_cores++;
 	}
-	if (num_cores < old_num_cores)
-		warn("Topology found less cores than early MADT parsing!");
+	if (num_cores < old_num_cores) {
+		warn("Topology found less cores (%d) than early MADT parsing! (%d)", num_cores, old_num_cores);
+		// XXX fucked
+		if (!num_cores)
+			num_cores = 2;
+	}
 	/* Too many cores will be a problem for some data structures. */
 	if (num_cores > old_num_cores)
 		panic("Topology found more cores than early MADT parsing!");
@@ -174,9 +179,15 @@
 	for (int i = 0; i < apics->nchildren; i++) {
 		struct Apicst *temp = apics->children[i]->tbl;
 
-		if (temp->type == ASlapic) {
+		switch (temp->type) {
+		case ASlapic:
 			if (temp->lapic.id > max_apic_id)
 				max_apic_id = temp->lapic.id;
+			break;
+		case ASlx2apic:
+			if (temp->lx2apic.id > max_apic_id)
+				max_apic_id = temp->lx2apic.id;
+			break;
 		}
 	}
 }
@@ -201,8 +212,14 @@
 	for (int i = 0; i < apics->nchildren; i++) {
 		struct Apicst *temp = apics->children[i]->tbl;
 
-		if (temp->type == ASlapic)
+		switch (temp->type) {
+		case ASlapic:
 			os_coreid_lookup[temp->lapic.id] = 0;
+			break;
+		case ASlx2apic:
+			os_coreid_lookup[temp->lx2apic.id] = 0;
+			break;
+		}
 	}
 	int os_coreid = 0;
 
@@ -380,6 +397,7 @@
 		build_flat_topology();
 }
 
+// XXX busted on cta9.  should be 2 sockets.  probably ACPI vs cpuid?
 void print_cpu_topology(void)
 {
 	printk("num_numa: %d, num_sockets: %d, num_cpus: %d, num_cores: %d\n",
diff --git a/kern/drivers/dev/acpi.c b/kern/drivers/dev/acpi.c
index cdac431..e4719db 100644
--- a/kern/drivers/dev/acpi.c
+++ b/kern/drivers/dev/acpi.c
@@ -1672,6 +1672,7 @@
 	return NULL;
 }
 
+// XXX wrong comment
 /* This may be an overestimate, if some LAPICS are present but disabled */
 static int madt_get_nr_cores(struct Sdthdr *madt)
 {
@@ -1684,7 +1685,15 @@
 	for (p += 44; p < madt_end; p += entry_len) {
 		entry_len = p[1];
 		switch (p[0]) {
+			// XXX NASTY
 		case ASlapic:
+			if (l32get(p + 4) == 0)
+				break;
+			nr_cores++;
+			break;
+		case ASlx2apic:
+			if (l32get(p + 8) == 0)
+				break;
 			nr_cores++;
 			break;
 		default:
diff --git a/kern/src/init.c b/kern/src/init.c
index a5e6349..83d54b5 100644
--- a/kern/src/init.c
+++ b/kern/src/init.c
@@ -133,6 +133,8 @@
 
 	exception_table_init();
 	num_cores = get_early_num_cores();
+	// XXX
+	printk("EARLY NUM CORES %d\n", num_cores);
 	pmem_init(multiboot_kaddr);
 	kmalloc_init();
 	vmap_init();
diff --git a/kern/src/ns/devtab.c b/kern/src/ns/devtab.c
index e88df66..660c671 100644
--- a/kern/src/ns/devtab.c
+++ b/kern/src/ns/devtab.c
@@ -27,10 +27,12 @@
 		return;
 	}
 	for (i = 0; &devtab[i] < __devtabend; i++) {
+		printk("%s: #%s\n", __func__, devtab[i].name);
 		if (devtab[i].reset)
 			devtab[i].reset();
 	}
 	poperror();
+	printk("%s complete\n", __func__);
 }
 
 void devtabinit(void)
@@ -48,10 +50,12 @@
 		 */
 		printd("i %d, '%s', dev %p, init %p\n", i, devtab[i].name,
 		       &devtab[i], devtab[i].init);
+		printk("%s: #%s\n", __func__, devtab[i].name);
 		if (devtab[i].init)
 			devtab[i].init();
 	}
 	poperror();
+	printk("%s complete\n", __func__);
 }
 
 void devtabshutdown(void)