Browse Source

Revert "Revert "ACPI: remove ACPICA source from the kernel.""

This reverts commit d056e163f082406ad50c323fa6fdcba435d7137c.
Ronald G. Minnich 7 years ago
parent
commit
d69bb730cf

+ 164 - 21
sys/src/9/amd64/acpi.h

@@ -7,6 +7,49 @@
  * in the LICENSE file.
  */
 
+/* -----------------------------------------------------------------------------
+ * ACPI is a table of tables. The tables define a hierarchy.
+ *
+ * From the hardware's perspective:
+ * Each table that we care about has a header, and the header has a
+ * length that includes the the length of all its subtables. So, even
+ * if you can't completely parse a table, you can find the next table.
+ *
+ * The process of parsing is to find the RSDP, and then for each subtable
+ * see what type it is and parse it. The process is recursive except for
+ * a few issues: The RSDP signature and header differs from the header of
+ * its subtables; their headers differ from the signatures of the tables
+ * they contain. As you walk down the tree, you need different parsers.
+ *
+ * The parser is recursive descent. Each parsing function takes a pointer
+ * to the parent of the node it is parsing and will attach itself to the parent
+ * via that pointer. Parsing functions are responsible for building the data
+ * structures that represent their node and recursive invocations of the parser
+ * for subtables.
+ *
+ * So, in this case, it's something like this:
+ *
+ * RSDP is the root. It has a standard header and size. You map that
+ * memory.  You find the first header, get its type and size, and
+ * parse as much of it as you can. Parsing will involve either a
+ * function or case statement for each element type. DMARs are complex
+ * and need functions; APICs are simple and we can get by with case
+ * statements.
+ *
+ * Each node in the tree is represented as a 'struct Atable'. This has a
+ * pointer to the actual node data, a type tag, a name, pointers to this
+ * node's children (if any) and a parent pointer. It also has a QID so that
+ * the entire structure can be exposed as a filesystem. The Atable doesn't
+ * contain any table data per se; it's metadata. The table pointer contains
+ * the table data as well as a pointer back to it's corresponding Atable.
+ *
+ * In the end we present a directory tree for #apic that looks, in this example:
+ * #acpi/DMAR/DRHD/0/{pretty,raw}
+ *
+ * 'cat pretty' will return JSON-encoded data described the element.
+ * 'cat raw' gets you the raw bytes.
+ */
+
 typedef struct Atable Atable;
 typedef struct Facs Facs;
 typedef struct Fadt Fadt;
@@ -25,7 +68,9 @@ typedef struct Apicst Apicst;
 typedef struct Srat Srat;
 typedef struct Slit Slit;
 typedef struct SlEntry SlEntry;
-
+typedef struct Dmar Dmar;
+typedef struct Drhd Drhd;
+typedef struct DevScope DevScope;
 enum
 {
 
@@ -91,24 +136,77 @@ enum
 	CMregion = 0,			/* regio name spc base len accsz*/
 	CMgpe,				/* gpe name id */
 
-	Qdir = 0,
-	Qctl,
-	Qtbl,
-	Qio,
+	/* Table types. */
+	RSDP = 0,
+	SDTH,
+	RSDT,
+	FADT,
+	FACS,
+	DSDT,
+	SSDT,
+	MADT,
+	SBST,
+	XSDT,
+	ECDT,
+	SLIT,
+	SRAT,
+	CPEP,
+	MSCT,
+	RASF,
+	MPST,
+	PMTT,
+	BGRT,
+	FPDT,
+	GTDT,
+	HPET,
+	APIC,
+	DMAR,
+	/* DMAR types */
+	DRHD,
+	RMRR,
+	ATSR,
+	RHSA,
+	ANDD,
+	NACPITBLS,			/* Number of ACPI tables */
+
+	/* Atable constants */
+	SIGSZ		= 4+1,	/* Size of the signature (including NUL) */
+	OEMIDSZ		= 6+1,	/* Size of the OEM ID (including NUL) */
+	OEMTBLIDSZ	= 8+1,	/* Size of the OEM Table ID (including NUL) */
+
 };
 
 /*
  * ACPI table (sw)
+ *
+ * This Atable struct corresponds to an interpretation of the standard header
+ * for all table types we support. It has a pointer to the converted data, i.e.
+ * the structs created by functions like acpimadt and so on. Note: althouh the
+ * various things in this are a superset of many ACPI table names (DRHD, DRHD
+ * scopes, etc). The raw data follows this header.
+ *
+ * Child entries in the table are kept in an array of pointers. Each entry has
+ * a pointer to it's logically "next" sibling, thus forming a linked list. But
+ * these lists are purely for convenience and all point to nodes within the
+ * same array.
  */
-struct Atable
-{
-	Atable*	next;		/* next table in list */
-	int	is64;		/* uses 64bits */
-	char	sig[5];		/* signature */
-	char	oemid[7];	/* oem id str. */
-	char	oemtblid[9];	/* oem tbl. id str. */
-	unsigned char* tbl;		/* pointer to table in memory */
-	int32_t	dlen;		/* size of data in table, after Stdhdr */
+struct Atable {
+	Qid qid;             /* QID corresponding to this table. */
+	Qid rqid;			/* This table's 'raw' QID. */
+	Qid pqid;			/* This table's 'pretty' QID. */
+	Qid tqid;			/* This table's 'table' QID. */
+	int type;					/* This table's type */
+	void *tbl;					/* pointer to the converted table, e.g. madt. */
+	char name[16];				/* name of this table */
+
+	Atable *parent;		/* Parent pointer */
+	Atable **children;	/* children of this node (an array). */
+	Dirtab *cdirs;		/* child directory entries of this node. */
+	size_t nchildren;			/* count of this node's children */
+	Atable *next;		/* Pointer to the next sibling. */
+
+	size_t rawsize;				/* Total size of raw table */
+	uint8_t *raw;				/* Raw data. */
 };
 
 struct Gpe
@@ -204,6 +302,8 @@ struct Sdthdr
  */
 struct Facs
 {
+	uint8_t sig[4];
+	uint8_t len[4];
 	uint32_t	hwsig;
 	uint32_t	wakingv;
 	uint32_t	glock;
@@ -218,10 +318,9 @@ struct Facs
  */
 struct Msct
 {
-	int	ndoms;		/* number of domains */
+	size_t ndoms;		/* number of discovered domains */
 	int	nclkdoms;	/* number of clock domains */
-	uint64_t	maxpa;		/* max physical address */
-
+	uint64_t	maxpa;	/* max physical address */
 	Mdom*	dom;		/* domain information list */
 };
 
@@ -231,7 +330,7 @@ struct Mdom
 	int	start;		/* start dom id */
 	int	end;		/* end dom id */
 	int	maxproc;	/* max processor capacity */
-	uint64_t	maxmem;		/* max memory capacity */
+	uint64_t maxmem;	/* max memory capacity */
 };
 
 /* Multiple APIC description table
@@ -334,7 +433,7 @@ struct Srat
 /* System locality information table
  */
 struct Slit {
-	int64_t rowlen;
+	uint64_t rowlen;
 	SlEntry **e;
 };
 
@@ -409,9 +508,53 @@ struct Fadt
  */
 struct Xsdt
 {
-	int	len;
-	int	asize;
+	size_t len;
+	size_t asize;
 	uint8_t*	p;
 };
 
+/* DMAR.
+ */
+/*
+ * Device scope.
+ */
+struct DevScope {
+	int enumeration_id;
+	int start_bus_number;
+	int npath;
+	int *paths;
+};
+/*
+ * The device scope is basic tbdf as uint32_t. There is a special value
+ * that means "everything" and if we see that we set "all" in the Drhd.
+ */
+struct Drhd {
+	int flags;
+	int segment;
+	uintptr_t rba;
+	uintptr_t all;	// This drhd scope is for everything.
+	size_t nscope;
+	struct DevScope *scopes;
+};
+
+struct Dmar {
+	int haw;
+	/*
+	 * If your firmware disables x2apic mode, you should not be here.
+	 * We ignore that bit.
+	 */
+	int intr_remap;
+};
+
+int acpiinit(void);
+Atable *mkatable(Atable *parent,
+                        int type, char *name, uint8_t *raw,
+                        size_t rawsize, size_t addsize);
+Atable *finatable(Atable *t, PSlice *slice);
+Atable *finatable_nochildren(Atable *t);
+
+extern Atable *apics;
+extern Atable *dmar;
+extern Atable *srat;
+
 extern uintmem acpimblocksize(uintmem, int*);

+ 1 - 0
sys/src/9/amd64/apic.h

@@ -21,6 +21,7 @@ typedef	struct	Apic	Apic;
 struct Ioapic {
 	Lock l;					/* IOAPIC: register access */
 	uint32_t*	addr;				/* IOAPIC: register base */
+	uintptr_t	paddr;				/* physical address */
 	int	nrdt;				/* IOAPIC: size of RDT */
 	int	gsib;				/* IOAPIC: global RDT index */
 };

+ 1 - 2
sys/src/9/amd64/core.json

@@ -52,7 +52,6 @@
 			"-Werror"
 		],
 		"Libs": [
-			"/$ARCH/lib/klibacpi.a",
 			"/$ARCH/lib/klibmemlayer.a",
 			"/$ARCH/lib/klibmemdraw.a",
 			"/$ARCH/lib/klibc.a",
@@ -81,7 +80,6 @@
 			"clean.json",
 			"../boot/bootconf.json",
 			"inith.json",
-			"/sys/src/libacpi/acpica/klibacpi.json",
 			"/sys/src/libmemdraw/klibmemdraw.json",
 			"/sys/src/libdraw/klibdraw.json"
 		],
@@ -117,6 +115,7 @@
 			"map.c",
 			"memory.c",
 			"mmu.c",
+			"mpacpi.c",
 			"mp.c",
 			"msi.c",
 			"multiboot.c",

+ 1882 - 923
sys/src/9/amd64/devacpi.c

@@ -7,1169 +7,2128 @@
  * in the LICENSE file.
  */
 
-#include	"u.h"
-#include	"../port/lib.h"
-#include	"mem.h"
-#include	"dat.h"
-#include	"fns.h"
-#include	"io.h"
-#include	"../port/error.h"
-#include "apic.h"
-#include "mp.h"
-#include <acpi/acpica/acpi.h>
+#include "u.h"
+#include "../port/lib.h"
+#include "mem.h"
+#include "dat.h"
+#include "fns.h"
+#include "io.h"
 
-enum
-{
+#include "apic.h"
+#include "acpi.h"
+
+/* -----------------------------------------------------------------------------
+ * Basic ACPI device.
+ *
+ * The qid.Path will be made unique by incrementing lastpath. lastpath starts
+ * at Qroot.
+ *
+ * Qtbl will return a pointer to the Atable, which includes the signature, OEM
+ * data, and so on.
+ *
+ * Raw, at any level, dumps the raw table at that level, which by the ACPI
+ * flattened tree layout will include all descendents.
+ *
+ * Qpretty, at any level, will print the pretty form for that level and all
+ * descendants.
+ */
+enum {
+	Qroot = 0,
 
-	Sdthdrsz	= 36,	/* size of SDT header */
+	// The type is the qid.path mod NQtypes.
 	Qdir = 0,
-	Qctl,
+	Qpretty,
+	Qraw,
 	Qtbl,
-	Qio,
+	NQtypes,
+
+	QIndexShift = 8,
+	QIndexMask = (1 << QIndexShift) - 1,
 };
 
-#if 0
-static Cmdtab ctls[] =
+/* what do we need to round up to? */
+#define ATABLEBUFSZ	ROUNDUP(sizeof(Atable), 128)
+
+static uint64_t lastpath;
+static PSlice emptyslice;
+static Atable **atableindex;
+Dev acpidevtab;
+
+static char * devname(void)
 {
-	{CMregion,	"region",	6},
-	{CMgpe,		"gpe",		3},
-};
-#endif
+	return acpidevtab.name;
+}
 
+static int devdc(void)
+{
+	return acpidevtab.dc;
+}
 
-/* 
- * This is the array of eyesores.
- * An Eyesore is an Interrupt Source Over Ride, which maps from
- * what they want to what it needs to be. You are not expected
- * to understand this.
+/*
+ * ACPI 4.0 Support.
+ * Still WIP.
+ *
+ * This driver locates tables and parses only a small subset
+ * of tables. All other tables are mapped and kept for the user-level
+ * interpreter.
  */
-static ACPI_MADT_INTERRUPT_OVERRIDE *eyesore;
-static int numeyesore;
-
-static Dirtab acpidir[]={
-	".",		{Qdir, 0, QTDIR},	0,	DMDIR|0555,
-	"acpictl",	{Qctl},			0,	0666,
-	"acpitbl",	{Qtbl},			0,	0444,
-	"acpiregio",	{Qio},			0,	0666,
+/*
+static Cmdtab ctls[] = {
+	{CMregion, "region", 6},
+	{CMgpe, "gpe", 3},
 };
-
-#if 0
-static char* regnames[] = {
+*/
+static Facs *facs;	/* Firmware ACPI control structure */
+static Fadt *fadt;	/* Fixed ACPI description to reach ACPI regs */
+static Atable *root;
+static Xsdt *xsdt;		/* XSDT table */
+static Atable *tfirst;	/* loaded DSDT/SSDT/... tables */
+//static Atable *tlast;	/* pointer to last table */
+Atable *apics; 		/* APIC info */
+Atable *srat;		/* System resource affinity used by physalloc */
+Atable *dmar;
+static Slit *slit;	/* Sys locality info table used by scheduler */
+static Atable *mscttbl;	/* Maximum system characteristics table */
+//static Reg *reg;	/* region used for I/O */
+static Gpe *gpes;	/* General purpose events */
+static int ngpes;
+
+static char *regnames[] = {
 	"mem", "io", "pcicfg", "embed",
-	"smb", "cmos", "pcibar",
+	"smb", "cmos", "pcibar", "ipmi",
 };
-#endif
 
-static void *rsd;
 /*
- * we use mp->machno (or index in Mach array) as the identifier,
- * but ACPI relies on the apic identifier.
+ * Lists to store RAM that we copy ACPI tables into. When we map a new
+ * ACPI list into the kernel, we copy it into a specifically RAM buffer
+ * (to make sure it's not coming from e.g. slow device memory). We store
+ * pointers to those buffers on these lists.
  */
-int
-corecolor(int core)
+struct Acpilist {
+	struct Acpilist *next;
+	size_t size;
+	int8_t raw[];
+};
+typedef struct Acpilist Acpilist;
+static Acpilist *acpilists;
+
+/*
+ * Produces an Atable at some level in the tree. Note that Atables are
+ * isomorphic to directories in the file system namespace; this code
+ * ensures that invariant.
+ */
+Atable *mkatable(Atable *parent,
+                        int type, char *name, uint8_t *raw,
+                        size_t rawsize, size_t addsize)
 {
-	Mach *m;
-	static int colors[32];
+	void *m;
+	Atable *t;
+
+	m = mallocz(ATABLEBUFSZ + addsize, 1);
+	if (m == nil)
+		panic("no memory for more aml tables");
+	t = m;
+	t->parent = parent;
+	t->tbl = nil;
+	if (addsize != 0)
+		t->tbl = m + ATABLEBUFSZ;
+	t->rawsize = rawsize;
+	t->raw = raw;
+	strlcpy(t->name, name, sizeof(t->name));
+	mkqid(&t->qid,  (lastpath << QIndexShift) + Qdir, 0, QTDIR);
+	mkqid(&t->rqid, (lastpath << QIndexShift) + Qraw, 0, 0);
+	mkqid(&t->pqid, (lastpath << QIndexShift) + Qpretty, 0, 0);
+	mkqid(&t->tqid, (lastpath << QIndexShift) + Qtbl, 0, 0);
+	lastpath++;
+
+	return t;
+}
 
-	if(core < 0 || core >= MACHMAX)
-		return -1;
-	m = sys->machptr[core];
-	if(m == nil)
-		return -1;
+Atable *finatable(Atable *t, PSlice *slice)
+{
+	size_t n;
+	Atable *tail;
+	Dirtab *dirs;
+
+	n = pslicelen(slice);
+	t->nchildren = n;
+	t->children = (Atable **)pslicefinalize(slice);
+	dirs = reallocarray(nil, n + NQtypes, sizeof(Dirtab));
+	assert(dirs != nil);
+	dirs[0] = (Dirtab){ ".",      t->qid,   0, 0555 };
+	dirs[1] = (Dirtab){ "pretty", t->pqid,  0, 0444 };
+	dirs[2] = (Dirtab){ "raw",    t->rqid,  0, 0444 };
+	dirs[3] = (Dirtab){ "table",  t->tqid,  0, 0444 };
+	for (size_t i = 0; i < n; i++) {
+		strlcpy(dirs[i + NQtypes].name, t->children[i]->name, KNAMELEN);
+		dirs[i + NQtypes].qid = t->children[i]->qid;
+		dirs[i + NQtypes].length = 0;
+		dirs[i + NQtypes].perm = DMDIR | 0555;
+	}
+	t->cdirs = dirs;
+	tail = nil;
+	while (n-- > 0) {
+		t->children[n]->next = tail;
+		tail = t->children[n];
+	}
 
-	if(core >= 0 && core < nelem(colors) && colors[core] != 0)
-		return colors[core] - 1;
+	return t;
+}
 
+Atable *finatable_nochildren(Atable *t)
+{
+	return finatable(t, &emptyslice);
+}
+
+static char *dumpGas(char *start, char *end, char *prefix, Gas *g);
+static void dumpxsdt(void);
+
+static char *acpiregstr(int id)
+{
+	static char buf[20];		/* BUG */
+
+	if (id >= 0 && id < nelem(regnames))
+		return regnames[id];
+	seprint(buf, buf + sizeof(buf), "spc:%#x", id);
+	return buf;
+}
+
+static int acpiregid(char *s)
+{
+	for (int i = 0; i < nelem(regnames); i++)
+		if (strcmp(regnames[i], s) == 0)
+			return i;
 	return -1;
 }
 
+/*
+ * TODO(rminnich): Fix these if we're ever on a different-endian machine.
+ * They are specific to little-endian processors and are not portable.
+ */
+static uint8_t mget8(uintptr_t p, void *unused)
+{
+	uint8_t *cp = (uint8_t *) p;
+	return *cp;
+}
 
-int
-pickcore(int mycolor, int index)
+static void mset8(uintptr_t p, uint8_t v, void *unused)
 {
-	return 0;
+	uint8_t *cp = (uint8_t *) p;
+	*cp = v;
 }
-static void*
-rsdscan(uint8_t* addr, int len, char* signature)
+
+static uint16_t mget16(uintptr_t p, void *unused)
 {
-	int sl;
-	uint8_t *e, *p;
+	uint16_t *cp = (uint16_t *) p;
+	return *cp;
+}
 
-	e = addr+len;
-	sl = strlen(signature);
-	for(p = addr; p+sl < e; p += 16){
-		if(memcmp(p, signature, sl))
-			continue;
-		return p;
-	}
+static void mset16(uintptr_t p, uint16_t v, void *unused)
+{
+	uint16_t *cp = (uint16_t *) p;
+	*cp = v;
+}
 
-	return nil;
+static uint32_t mget32(uintptr_t p, void *unused)
+{
+	uint32_t *cp = (uint32_t *) p;
+	return *cp;
 }
 
+static void mset32(uintptr_t p, uint32_t v, void *unused)
+{
+	uint32_t *cp = (uint32_t *) p;
+	*cp = v;
+}
 
-static void*
-rsdsearch(char* signature)
+static uint64_t mget64(uintptr_t p, void *unused)
 {
-	uintptr_t p;
-	uint8_t *bda;
-	void *rsd;
+	uint64_t *cp = (uint64_t *) p;
+	return *cp;
+}
 
-	/*
-	 * Search for the data structure signature:
-	 * 1) in the first KB of the EBDA;
-	 * 2) in the BIOS ROM between 0xE0000 and 0xFFFFF.
-	 */
-	if(strncmp((char*)KADDR(0xFFFD9), "EISA", 4) == 0){
-		bda = BIOSSEG(0x40);
-		if((p = (bda[0x0F]<<8)|bda[0x0E])){
-			if(rsd = rsdscan(KADDR(p), 1024, signature))
-				return rsd;
-		}
-	}
-	return rsdscan(BIOSSEG(0xE000), 0x20000, signature);
+static void mset64(uintptr_t p, uint64_t v, void *unused)
+{
+	uint64_t *cp = (uint64_t *) p;
+	*cp = v;
+}
+
+static uint8_t ioget8(uintptr_t p, void *unused)
+{
+	return inb(p);
+}
+
+static void ioset8(uintptr_t p, uint8_t v, void *unused)
+{
+	outb(p, v);
+}
+
+static uint16_t ioget16(uintptr_t p, void *unused)
+{
+	return ins(p);
+}
+
+static void ioset16(uintptr_t p, uint16_t v, void *unused)
+{
+	outs(p, v);
+}
+
+static uint32_t ioget32(uintptr_t p, void *unused)
+{
+	return inl(p);
 }
 
+static void ioset32(uintptr_t p, uint32_t v, void *unused)
+{
+	outl(p, v);
+}
+
+static uint8_t
+cfgget8(uintptr_t p, void* r)
+{
+	Reg *ro = r;
+	Pcidev d;
+
+	d.tbdf = ro->tbdf;
+	return pcicfgr8(&d, p);
+}
 
 static void
-acpirsdptr(void)
+cfgset8(uintptr_t p, uint8_t v, void* r)
 {
-	rsd = rsdsearch("RSD PTR ");
-	if (rsd == nil) {
-		print("NO RSD PTR found\n");
-		return;
-	}
-	print("Found RST PTR ta %p\n", rsd);
+	Reg *ro = r;
+	Pcidev d;
 
-#if 0
-	assert(sizeof(Sdthdr) == 36);
+	d.tbdf = ro->tbdf;
+	pcicfgw8(&d, p, v);
+}
 
-	DBG("acpi: RSD PTR@ %#p, physaddr %#x length %u %#llx rev %d\n",
-		rsd, l32get(rsd->raddr), l32get(rsd->length),
-		l64get(rsd->xaddr), rsd->revision);
+static uint16_t
+cfgget16(uintptr_t p, void* r)
+{
+	Reg *ro = r;
+	Pcidev d;
 
-	if(rsd->revision >= 2){
-		if(sdtchecksum(rsd, 36) == nil){
-			DBG("acpi: RSD: bad checksum\n");
-			return;
+	d.tbdf = ro->tbdf;
+	return pcicfgr16(&d, p);
+}
+
+static void
+cfgset16(uintptr_t p, uint16_t v, void* r)
+{
+	Reg *ro = r;
+	Pcidev d;
+
+	d.tbdf = ro->tbdf;
+	pcicfgw16(&d, p, v);
+}
+
+static uint32_t
+cfgget32(uintptr_t p, void* r)
+{
+	Reg *ro = r;
+	Pcidev d;
+
+	d.tbdf = ro->tbdf;
+	return pcicfgr32(&d, p);
+}
+
+static void
+cfgset32(uintptr_t p, uint32_t v, void* r)
+{
+	Reg *ro = r;
+	Pcidev d;
+
+	d.tbdf = ro->tbdf;
+	pcicfgw32(&d, p, v);
+}
+
+static struct Regio memio = {
+	nil,
+	mget8, mset8, mget16, mset16,
+	mget32, mset32, mget64, mset64
+};
+
+static struct Regio ioio = {
+	nil,
+	ioget8, ioset8, ioget16, ioset16,
+	ioget32, ioset32, nil, nil
+};
+
+static struct Regio cfgio = {
+	nil,
+	cfgget8, cfgset8, cfgget16, cfgset16,
+	cfgget32, cfgset32, nil, nil
+};
+
+/*
+ * Copy memory, 1/2/4/8-bytes at a time, to/from a region.
+ */
+static long
+regcpy(Regio *dio, uintptr_t da, Regio *sio,
+	   uintptr_t sa, long len, int align)
+{
+	int n, i;
+
+	print("regcpy %#p %#p %#p %#p\n", da, sa, len, align);
+	if ((len % align) != 0)
+		print("regcpy: bug: copy not aligned. truncated\n");
+	n = len / align;
+	for (i = 0; i < n; i++) {
+		switch (align) {
+			case 1:
+				print("cpy8 %#p %#p\n", da, sa);
+				dio->set8(da, sio->get8(sa, sio->arg), dio->arg);
+				break;
+			case 2:
+				print("cpy16 %#p %#p\n", da, sa);
+				dio->set16(da, sio->get16(sa, sio->arg), dio->arg);
+				break;
+			case 4:
+				print("cpy32 %#p %#p\n", da, sa);
+				dio->set32(da, sio->get32(sa, sio->arg), dio->arg);
+				break;
+			case 8:
+				print("cpy64 %#p %#p\n", da, sa);
+				print("Not doing set64 for some reason, fix me!");
+				//  dio->set64(da, sio->get64(sa, sio->arg), dio->arg);
+				break;
+			default:
+				panic("regcpy: align bug");
 		}
-		sdtpa = l64get(rsd->xaddr);
-		asize = 8;
-	}
-	else{
-		if(sdtchecksum(rsd, 20) == nil){
-			DBG("acpi: RSD: bad checksum\n");
-			return;
-		}
-		sdtpa = l32get(rsd->raddr);
-		asize = 4;
+		da += align;
+		sa += align;
 	}
+	return n * align;
+}
 
-	/*
-	 * process the RSDT or XSDT table.
-	 */
-	xsdt = malloc(sizeof(Xsdt));
-	if(xsdt == nil){
-		DBG("acpi: malloc failed\n");
-		return;
+/*
+ * Perform I/O within region in access units of accsz bytes.
+ * All units in bytes.
+ */
+static long regio(Reg *r, void *p, uint32_t len, uintptr_t off, int iswr)
+{
+	Regio rio;
+	uintptr_t rp;
+
+	print("reg%s %s %#p %#p %#lx sz=%d\n",
+		   iswr ? "out" : "in", r->name, p, off, len, r->accsz);
+	rp = 0;
+	if (off + len > r->len) {
+		print("regio: access outside limits");
+		len = r->len - off;
 	}
-	if((xsdt->p = sdtmap(sdtpa, &xsdt->len, 1)) == nil){
-		DBG("acpi: sdtmap failed\n");
-		return;
+	if (len <= 0) {
+		print("regio: zero len\n");
+		return 0;
 	}
-	if((xsdt->p[0] != 'R' && xsdt->p[0] != 'X') || memcmp(xsdt->p+1, "SDT", 3) != 0){
-		DBG("acpi: xsdt sig: %c%c%c%c\n",
-			xsdt->p[0], xsdt->p[1], xsdt->p[2], xsdt->p[3]);
-		free(xsdt);
-		xsdt = nil;
-		vunmap(xsdt, xsdt->len);
-		return;
+	switch (r->spc) {
+		case Rsysmem:
+			if (r->p == nil)
+				r->p = vmap(r->base, len);
+			if (r->p == nil)
+				error("regio: vmap/KADDR failed");
+			rp = (uintptr_t) r->p + off;
+			rio = memio;
+			break;
+		case Rsysio:
+			rp = r->base + off;
+			rio = ioio;
+			break;
+		case Rpcicfg:
+			rp = r->base + off;
+			rio = cfgio;
+			rio.arg = r;
+			break;
+		case Rpcibar:
+		case Rembed:
+		case Rsmbus:
+		case Rcmos:
+		case Ripmi:
+		case Rfixedhw:
+			print("regio: reg %s not supported\n", acpiregstr(r->spc));
+			error("region not supported");
 	}
-	xsdt->p += sizeof(Sdthdr);
-	xsdt->len -= sizeof(Sdthdr);
-	xsdt->asize = asize;
-	DBG("acpi: XSDT %#p\n", xsdt);
-	acpixsdtload(nil);
-	/* xsdt is kept and not unmapped */
-#endif
+	if (iswr)
+		regcpy(&rio, rp, &memio, (uintptr_t) p, len, r->accsz);
+	else
+		regcpy(&memio, (uintptr_t) p, &rio, rp, len, r->accsz);
+	return len;
 }
 
-static int
-acpigen(Chan *c, char* d, Dirtab *tab, int ntab, int i, Dir *dp)
+/*
+ * Compute and return SDT checksum: '0' is a correct sum.
+ */
+static uint8_t sdtchecksum(void *addr, int len)
 {
-	Qid qid;
+	uint8_t *p, sum;
+
+	sum = 0;
+print("check %p %d\n", addr, len);
+	for (p = addr; len-- > 0; p++)
+		sum += *p;
+print("sum is 0x%x\n", sum);
+	return sum;
+}
 
-	if(i == DEVDOTDOT){
-		mkqid(&qid, Qdir, 0, QTDIR);
-		devdir(c, qid, ".", 0, eve, 0555, dp);
-		return 1;
+static void *sdtmap(uintptr_t pa, size_t *n, int cksum)
+{
+	Sdthdr *sdt;
+	Acpilist *p;
+print("sdtmap %p\n", (void *)pa);
+	if (!pa) {
+		print("sdtmap: nil pa\n");
+		return nil;
+	}
+	sdt = vmap(pa, sizeof(Sdthdr));
+	if (sdt == nil) {
+		print("acpi: vmap: nil\n");
+		return nil;
+	}
+print("sdt %p\n", sdt);
+print("get it\n");
+	*n = l32get(sdt->length);
+print("*n is %d\n", *n);
+	if (!*n) {
+		print("sdt has zero length: pa = %p, sig = %.4s\n", pa, sdt->sig);
+		return nil;
+	}
+	sdt = vmap(pa, *n);
+	if (sdt == nil) {
+		print("acpi: vmap: nil\n");
+		return nil;
 	}
-	i++; /* skip first element for . itself */
-	if(tab==0 || i>=ntab)
+print("check it\n");
+	if (cksum != 0 && sdtchecksum(sdt, *n) != 0) {
+		print("acpi: SDT: bad checksum. pa = %p, len = %lu\n", pa, *n);
+		return nil;
+	}
+print("now mallocz\n");
+	p = mallocz(sizeof(Acpilist) + *n, 1);
+print("malloc'ed %p\n", p);
+	if (p == nil)
+		panic("sdtmap: memory allocation failed for %lu bytes", *n);
+print("move (%p, %p, %d)\n", p->raw, (void *)sdt, *n);
+	memmove(p->raw, (void *)sdt, *n);
+	p->size = *n;
+	p->next = acpilists;
+	acpilists = p;
+	print("sdtmap: size %d\n", *n);
+	return p->raw;
+}
+
+static int loadfacs(uintptr_t pa)
+{
+	size_t n;
+
+	facs = sdtmap(pa, &n, 0);
+	if (facs == nil)
+		return -1;
+	if (memcmp(facs->sig, "FACS", 4) != 0) {
+		facs = nil;
 		return -1;
-	tab += i;
-	qid = tab->qid;
-	qid.path &= ~Qdir;
-	qid.vers = 0;
-	devdir(c, qid, tab->name, tab->length, eve, tab->perm, dp);
-	return 1;
+	}
+
+	/* no unmap */
+	print("acpi: facs: hwsig: %#p\n", facs->hwsig);
+	print("acpi: facs: wakingv: %#p\n", facs->wakingv);
+	print("acpi: facs: flags: %#p\n", facs->flags);
+	print("acpi: facs: glock: %#p\n", facs->glock);
+	print("acpi: facs: xwakingv: %#p\n", facs->xwakingv);
+	print("acpi: facs: vers: %#p\n", facs->vers);
+	print("acpi: facs: ospmflags: %#p\n", facs->ospmflags);
+
+	return 0;
 }
 
-ACPI_STATUS
-AcpiOsInitialize(void)
+static void loaddsdt(uintptr_t pa)
 {
-	print("%s\n", __func__);
-	acpirsdptr();
-	return AE_OK;
+	size_t n;
+	uint8_t *dsdtp;
+
+	dsdtp = sdtmap(pa, &n, 1);
+print("Loaded it\n");
+	if (dsdtp == nil) {
+		print("acpi: Failed to map dsdtp.\n");
+		return;
+	}
 }
 
-ACPI_STATUS
-AcpiOsTerminate (
-	void)
+static void gasget(Gas *gas, uint8_t *p)
 {
-	print("%s\n", __func__);
-	return AE_OK;
+	gas->spc = p[0];
+	gas->len = p[1];
+	gas->off = p[2];
+	gas->accsz = p[3];
+	gas->addr = l64get(p + 4);
 }
 
-/* run AML with one integer arg. */
-static int
-run_aml_arg(char *name, int val)
+static char *dumpfadt(char *start, char *end, Fadt *fp)
 {
-    ACPI_OBJECT arg1;
-    ACPI_OBJECT_LIST args;
-    ACPI_STATUS as;
+	if (fp == nil)
+		return start;
+
+	start = seprint(start, end, "acpi: FADT@%p\n", fp);
+	start = seprint(start, end, "acpi: fadt: facs: $%p\n", fp->facs);
+	start = seprint(start, end, "acpi: fadt: dsdt: $%p\n", fp->dsdt);
+	start = seprint(start, end, "acpi: fadt: pmprofile: $%p\n", fp->pmprofile);
+	start = seprint(start, end, "acpi: fadt: sciint: $%p\n", fp->sciint);
+	start = seprint(start, end, "acpi: fadt: smicmd: $%p\n", fp->smicmd);
+	start =
+		seprint(start, end, "acpi: fadt: acpienable: $%p\n", fp->acpienable);
+	start =
+		seprint(start, end, "acpi: fadt: acpidisable: $%p\n", fp->acpidisable);
+	start = seprint(start, end, "acpi: fadt: s4biosreq: $%p\n", fp->s4biosreq);
+	start = seprint(start, end, "acpi: fadt: pstatecnt: $%p\n", fp->pstatecnt);
+	start =
+		seprint(start, end, "acpi: fadt: pm1aevtblk: $%p\n", fp->pm1aevtblk);
+	start =
+		seprint(start, end, "acpi: fadt: pm1bevtblk: $%p\n", fp->pm1bevtblk);
+	start =
+		seprint(start, end, "acpi: fadt: pm1acntblk: $%p\n", fp->pm1acntblk);
+	start =
+		seprint(start, end, "acpi: fadt: pm1bcntblk: $%p\n", fp->pm1bcntblk);
+	start = seprint(start, end, "acpi: fadt: pm2cntblk: $%p\n", fp->pm2cntblk);
+	start = seprint(start, end, "acpi: fadt: pmtmrblk: $%p\n", fp->pmtmrblk);
+	start = seprint(start, end, "acpi: fadt: gpe0blk: $%p\n", fp->gpe0blk);
+	start = seprint(start, end, "acpi: fadt: gpe1blk: $%p\n", fp->gpe1blk);
+	start = seprint(start, end, "acpi: fadt: pm1evtlen: $%p\n", fp->pm1evtlen);
+	start = seprint(start, end, "acpi: fadt: pm1cntlen: $%p\n", fp->pm1cntlen);
+	start = seprint(start, end, "acpi: fadt: pm2cntlen: $%p\n", fp->pm2cntlen);
+	start = seprint(start, end, "acpi: fadt: pmtmrlen: $%p\n", fp->pmtmrlen);
+	start =
+		seprint(start, end, "acpi: fadt: gpe0blklen: $%p\n", fp->gpe0blklen);
+	start =
+		seprint(start, end, "acpi: fadt: gpe1blklen: $%p\n", fp->gpe1blklen);
+	start = seprint(start, end, "acpi: fadt: gp1base: $%p\n", fp->gp1base);
+	start = seprint(start, end, "acpi: fadt: cstcnt: $%p\n", fp->cstcnt);
+	start = seprint(start, end, "acpi: fadt: plvl2lat: $%p\n", fp->plvl2lat);
+	start = seprint(start, end, "acpi: fadt: plvl3lat: $%p\n", fp->plvl3lat);
+	start = seprint(start, end, "acpi: fadt: flushsz: $%p\n", fp->flushsz);
+	start =
+		seprint(start, end, "acpi: fadt: flushstride: $%p\n", fp->flushstride);
+	start = seprint(start, end, "acpi: fadt: dutyoff: $%p\n", fp->dutyoff);
+	start = seprint(start, end, "acpi: fadt: dutywidth: $%p\n", fp->dutywidth);
+	start = seprint(start, end, "acpi: fadt: dayalrm: $%p\n", fp->dayalrm);
+	start = seprint(start, end, "acpi: fadt: monalrm: $%p\n", fp->monalrm);
+	start = seprint(start, end, "acpi: fadt: century: $%p\n", fp->century);
+	start =
+		seprint(start, end, "acpi: fadt: iapcbootarch: $%p\n",
+				 fp->iapcbootarch);
+	start = seprint(start, end, "acpi: fadt: flags: $%p\n", fp->flags);
+	start = dumpGas(start, end, "acpi: fadt: resetreg: ", &fp->resetreg);
+	start = seprint(start, end, "acpi: fadt: resetval: $%p\n", fp->resetval);
+	start = seprint(start, end, "acpi: fadt: xfacs: %p\n", fp->xfacs);
+	start = seprint(start, end, "acpi: fadt: xdsdt: %p\n", fp->xdsdt);
+	start = dumpGas(start, end, "acpi: fadt: xpm1aevtblk:", &fp->xpm1aevtblk);
+	start = dumpGas(start, end, "acpi: fadt: xpm1bevtblk:", &fp->xpm1bevtblk);
+	start = dumpGas(start, end, "acpi: fadt: xpm1acntblk:", &fp->xpm1acntblk);
+	start = dumpGas(start, end, "acpi: fadt: xpm1bcntblk:", &fp->xpm1bcntblk);
+	start = dumpGas(start, end, "acpi: fadt: xpm2cntblk:", &fp->xpm2cntblk);
+	start = dumpGas(start, end, "acpi: fadt: xpmtmrblk:", &fp->xpmtmrblk);
+	start = dumpGas(start, end, "acpi: fadt: xgpe0blk:", &fp->xgpe0blk);
+	start = dumpGas(start, end, "acpi: fadt: xgpe1blk:", &fp->xgpe1blk);
+	return start;
+}
+
+static Atable *parsefadt(Atable *parent,
+								char *name, uint8_t *p, size_t rawsize)
+{
+	Atable *t;
+	Fadt *fp;
 
-    arg1.Type = ACPI_TYPE_INTEGER;
-    arg1.Integer.Value = 1;
-    args.Count = 1;
-    args.Pointer = &arg1;
+	t = mkatable(parent, FADT, name, p, rawsize, sizeof(Fadt));
 
-    /* This does not work. Just leaving it here in case someone
-     * else thinks it will.
-	ACPI_STATUS as;
-	ACPI_OBJECT arg[] = {
-		{
-			.Type = ACPI_TYPE_INTEGER,
-			.Integer.Value = val
-		}
-	};
-	ACPI_OBJECT_LIST args = {
-		.Count = 1,
-		.Pointer = arg
-	};
-    */
-	as = AcpiEvaluateObject(ACPI_ROOT_OBJECT, name, &args, NULL);
-	print("run_aml_arg(%s, %d) returns %d\n", name, val, as);
-	return ACPI_SUCCESS(as);
+	if (rawsize < 116) {
+		print("ACPI: unusually short FADT, aborting!\n");
+		return t;
+	}
+	/* for now, keep the globals. We'll get rid of them later. */
+	fp = t->tbl;
+	fadt = fp;
+	fp->facs = l32get(p + 36);
+	fp->dsdt = l32get(p + 40);
+	fp->pmprofile = p[45];
+	fp->sciint = l16get(p + 46);
+	fp->smicmd = l32get(p + 48);
+	fp->acpienable = p[52];
+	fp->acpidisable = p[53];
+	fp->s4biosreq = p[54];
+	fp->pstatecnt = p[55];
+	fp->pm1aevtblk = l32get(p + 56);
+	fp->pm1bevtblk = l32get(p + 60);
+	fp->pm1acntblk = l32get(p + 64);
+	fp->pm1bcntblk = l32get(p + 68);
+	fp->pm2cntblk = l32get(p + 72);
+	fp->pmtmrblk = l32get(p + 76);
+	fp->gpe0blk = l32get(p + 80);
+	fp->gpe1blk = l32get(p + 84);
+	fp->pm1evtlen = p[88];
+	fp->pm1cntlen = p[89];
+	fp->pm2cntlen = p[90];
+	fp->pmtmrlen = p[91];
+	fp->gpe0blklen = p[92];
+	fp->gpe1blklen = p[93];
+	fp->gp1base = p[94];
+	fp->cstcnt = p[95];
+	fp->plvl2lat = l16get(p + 96);
+	fp->plvl3lat = l16get(p + 98);
+	fp->flushsz = l16get(p + 100);
+	fp->flushstride = l16get(p + 102);
+	fp->dutyoff = p[104];
+	fp->dutywidth = p[105];
+	fp->dayalrm = p[106];
+	fp->monalrm = p[107];
+	fp->century = p[108];
+	fp->iapcbootarch = l16get(p + 109);
+	fp->flags = l32get(p + 112);
+
+	/*
+	 * qemu gives us a 116 byte fadt, though i haven't seen any HW do that.
+	 * The right way to do this is to realloc the table and fake it out.
+	 */
+	if (rawsize < 244)
+		return finatable_nochildren(t);
+
+	gasget(&fp->resetreg, p + 116);
+	fp->resetval = p[128];
+	fp->xfacs = l64get(p + 132);
+	fp->xdsdt = l64get(p + 140);
+	gasget(&fp->xpm1aevtblk, p + 148);
+	gasget(&fp->xpm1bevtblk, p + 160);
+	gasget(&fp->xpm1acntblk, p + 172);
+	gasget(&fp->xpm1bcntblk, p + 184);
+	gasget(&fp->xpm2cntblk, p + 196);
+	gasget(&fp->xpmtmrblk, p + 208);
+	gasget(&fp->xgpe0blk, p + 220);
+	gasget(&fp->xgpe1blk, p + 232);
+
+	if (fp->xfacs != 0)
+		loadfacs(fp->xfacs);
+	else
+		loadfacs(fp->facs);
+
+print("x %p %p %p \n", fp, (void *)fp->xdsdt, (void *)(uint64_t)fp->dsdt);
+
+	if (fp->xdsdt == (uint64_t)fp->dsdt)	/* acpica */
+		loaddsdt(fp->xdsdt);
+	else
+		loaddsdt(fp->dsdt);
+print("y\n");
+
+	return finatable_nochildren(t);
 }
 
-static int
-set_machine_mode(void)
+static char *dumpmsct(char *start, char *end, Atable *table)
 {
-	/* we always enable the APIC. */
-	return run_aml_arg("_PIC", 1);
+	Msct *msct;
+
+	if (!table)
+		return start;
+
+	msct = table->tbl;
+	if (!msct)
+		return start;
+
+	start = seprint(start, end, "acpi: msct: %d doms %d clkdoms %#p maxpa\n",
+					 msct->ndoms, msct->nclkdoms, msct->maxpa);
+	for (int i = 0; i < table->nchildren; i++) {
+		Atable *domtbl = table->children[i]->tbl;
+		Mdom *st = domtbl->tbl;
+
+		start = seprint(start, end, "\t[%d:%d] %d maxproc %#p maxmmem\n",
+						 st->start, st->end, st->maxproc, st->maxmem);
+	}
+	start = seprint(start, end, "\n");
+
+	return start;
 }
 
-void pi(int indent)
+/*
+ * XXX: should perhaps update our idea of available memory.
+ * Else we should remove this code.
+ */
+static Atable *parsemsct(Atable *parent,
+                                char *name, uint8_t *raw, size_t rawsize)
 {
+	Atable *t;
+	uint8_t *r, *re;
+	Msct *msct;
+	size_t off, nmdom;
 	int i;
-	for(i = 0; i < indent; i++)
-		print(" ");
-}
 
-/* walk the whatever. Limited, right now. */
-static void
-objwalk(ACPI_OBJECT *p)
-{
-	static int indent;
-	int cnt;
-	ACPI_OBJECT *e;
-	pi(indent);
-	switch(p->Type) {
-	case 4: // ACPI_DESC_TYPE_STATE_PACKAGE:
-		print("Package:\n");
-		indent += 2;
-		e = p->Package.Elements;
-		for(cnt = 0; cnt < p->Package.Count; cnt++, e++){
-			objwalk(e);
-		}
-
-		indent -= 2;
-		print("\n");
-		break;
-	case 1:
-		print("Integer:0x%llx", p->Integer.Value);
-		break;
-	default:
-		print("Can't handle type %d\n", p->Type);
-		break;
+	re = raw + rawsize;
+	off = l32get(raw + 36);
+	nmdom = 0;
+	for (r = raw + off, re = raw + rawsize; r < re; r += 22)
+		nmdom++;
+	t = mkatable(parent, MSCT, name, raw, rawsize,
+	             sizeof(Msct) + nmdom * sizeof(Mdom));
+	msct = t->tbl;
+	msct->ndoms = l32get(raw + 40) + 1;
+	msct->nclkdoms = l32get(raw + 44) + 1;
+	msct->maxpa = l64get(raw + 48);
+	msct->ndoms = nmdom;
+	msct->dom = nil;
+	if (nmdom != 0)
+		msct->dom = (void *)msct + sizeof(Msct);
+	for (i = 0, r = raw; i < nmdom; i++, r += 22) {
+		msct->dom[i].start = l32get(r + 2);
+		msct->dom[i].end = l32get(r + 6);
+		msct->dom[i].maxproc = l32get(r + 10);
+		msct->dom[i].maxmem = l64get(r + 14);
 	}
+	mscttbl = finatable_nochildren(t);
 
+	return mscttbl;
 }
 
-static ACPI_STATUS
-resource(ACPI_RESOURCE *r, void *Context)
+/* TODO(rminnich): only handles on IOMMU for now. */
+static char *dumpdmar(char *start, char *end, Atable *dmar)
 {
-	ACPI_RESOURCE_IRQ *i = &r->Data.Irq;
-	print("\tACPI_RESOURCE_TYPE_%d: Length %d\n", r->Type, r->Length);
-	if (r->Type != ACPI_RESOURCE_TYPE_IRQ)
-		return 0;
-	print("\t\tIRQ Triggering %d Polarity %d Sharable %d InterruptCount %d: ", 
-	      i->Triggering, i->Polarity, i->Sharable, i->InterruptCount);
-	for(int j = 0; j < i->InterruptCount; j++)
-		print("%d,", i->Interrupts[j]);
-	print("\n");
-	/* assumptions: we assume apic 0 for now. This will need to be fixed.
-	 * We also just take the first interrupt. 
-	 */
-	uint32_t low = Im;
-	switch (i->Polarity){
-	case ACPI_ACTIVE_HIGH:
-		low |= IPhigh;
-		break;
-	case ACPI_ACTIVE_LOW:
-		low |= IPlow;
-		break;
-	case ACPI_ACTIVE_BOTH:
-		low |= IPlow | IPhigh;
-		break;
-	default:
-		print("BOTCH! i->Polarity is 0x%x and I don't do that\n", i->Polarity);
-		break;
+	Dmar *dt;
+
+	if (dmar == nil)
+		return start;
+
+	dt = dmar->tbl;
+	start = seprint(start, end, "acpi: DMAR addr %p:\n", dt);
+	start = seprint(start, end, "\tdmar: intr_remap %d haw %d\n",
+	                 dt->intr_remap, dt->haw);
+	for (int i = 0; i < dmar->nchildren; i++) {
+		Atable *at = dmar->children[i];
+		Drhd *drhd = at->tbl;
+
+		start = seprint(start, end, "\tDRHD: ");
+		start = seprint(start, end, "%s 0x%02x 0x%016x\n",
+		                 drhd->all & 1 ? "INCLUDE_PCI_ALL" : "Scoped",
+		                 drhd->segment, drhd->rba);
 	}
 
-	switch (i->Triggering) {
-	case ACPI_LEVEL_SENSITIVE:
-		low |= TMlevel;
-		break;
-	case ACPI_EDGE_SENSITIVE:
-		low |= TMedge;
-		break;
-	default:
-		print("BOTCH! i->Triggering is 0x%x and I don't do that\n", i->Triggering);
-		break;
-	}
-	print("ACPICODE: ioapicintrinit(0xff, 0x%x, 0x%x, 0x%x, 0x%x\n", 1, i->Interrupts[0], i->Interrupts[0]<<2, low);
-	return 0;
+	return start;
 }
-ACPI_STATUS
-device(ACPI_HANDLE                     Object,
-    UINT32                          NestingLevel,
-    void                            *Context,
-    void                            **ReturnValue)
 
+static char *dumpsrat(char *start, char *end, Atable *table)
 {
-	ACPI_STATUS as;
-	ACPI_DEVICE_INFO *info;
-	as = AcpiGetObjectInfo(Object, &info);
-	print("as is %d\n", as);
-	if (!ACPI_SUCCESS(as))
-		return 0;
-	ACPI_BUFFER out;
-	out.Length = ACPI_ALLOCATE_BUFFER;
-	out.Pointer = nil;
-	char n[5];
-	memmove(n, &info->Name, sizeof(info->Name));
-	n[4] = 0;
-	print("%s\n", n);
-	as = AcpiGetIrqRoutingTable(Object, &out);
-	print("get the PRT: %d\n", as);
-	print("Length is %u ptr is %p\n", out.Length, out.Pointer);
-	if (ACPI_SUCCESS(as)) {
-		void *p = (void *)out.Pointer;
-		while(((ACPI_PCI_ROUTING_TABLE*)p)->Length > 0) {
-			ACPI_PCI_ROUTING_TABLE *t = p;
-			print("%s: ", t->Source);
-			print("Pin 0x%x, Address 0x%llx, SourceIndex 0x%x\n", 
-			      t->Address, t->SourceIndex);
-			p += t->Length;
+	if (table == nil)
+		return seprint(start, end, "NO SRAT\n");
+	start = seprint(start, end, "acpi: SRAT@%p:\n", table->tbl);
+	for (; table != nil; table = table->next) {
+		Srat *st = table->tbl;
+
+		if (st == nil)
+			continue;
+		switch (st->type) {
+			case SRlapic:
+				start =
+					seprint(start, end,
+							 "\tlapic: dom %d apic %d sapic %d clk %d\n",
+							 st->lapic.dom, st->lapic.apic, st->lapic.sapic,
+							 st->lapic.clkdom);
+				break;
+			case SRmem:
+				start = seprint(start, end, "\tmem: dom %d %#p %#p %c%c\n",
+								 st->mem.dom, st->mem.addr, st->mem.len,
+								 st->mem.hplug ? 'h' : '-',
+								 st->mem.nvram ? 'n' : '-');
+				break;
+			case SRlx2apic:
+				start =
+					seprint(start, end, "\tlx2apic: dom %d apic %d clk %d\n",
+							 st->lx2apic.dom, st->lx2apic.apic,
+							 st->lx2apic.clkdom);
+				break;
+			default:
+				start = seprint(start, end, "\t<unknown srat entry>\n");
 		}
 	}
-	as = AcpiWalkResources(Object, "_CRS", resource, nil);
-	print("Walk resources: as is %d\n", as);
-#if 0
-	out.Length = ACPI_ALLOCATE_BUFFER;
-	out.Pointer = nil;
-	as = AcpiGetPossibleResources(Object, &out);
-	print("get the possible resources: %d\n", as);
-	if (ACPI_SUCCESS(as)) {
-		void *p = (void *)out.Pointer;
-		hexdump(out.Pointer, out.Length);
-		while(((ACPI_RESOURCE*)p)->Type != ACPI_RESOURCE_TYPE_END_TAG) {
-			ACPI_RESOURCE *r = p;
-			ACPI_RESOURCE_IRQ *i = p + sizeof(r->Type);
-			print("\tACPI_RESOURCE_TYPE_%d: Length %d\n", r->Type, r->Length);
-			p += r->Length;
-			if (r->Type != ACPI_RESOURCE_TYPE_IRQ)
-				continue;
-			print("\t\tIRQ Triggering %d Polarity %d Sharable %d InterruptCount %d: ", 
-			      i->Triggering, i->Polarity, i->Sharable, i->InterruptCount);
-			for(int j = 0; j < i->InterruptCount; j++)
-				print("%d,", i->Interrupts[j]);
-			print("\n");
+	start = seprint(start, end, "\n");
+	return start;
+}
+
+static Atable *parsesrat(Atable *parent,
+                                char *name, uint8_t *p, size_t rawsize)
+{
 
+	Atable *t, *tt;
+	uint8_t *pe;
+	int stlen, flags;
+	PSlice slice;
+	char buf[16];
+	int i;
+	Srat *st;
 
+	/* TODO: Parse the second SRAT */
+	if (srat != nil) {
+		print("Multiple SRATs detected and ignored!");
+		return nil;
+	}
+
+	t = mkatable(parent, SRAT, name, p, rawsize, 0);
+	psliceinit(&slice);
+	pe = p + rawsize;
+	for (p += 48, i = 0; p < pe; p += stlen, i++) {
+		snprint(buf, sizeof(buf), "%d", i);
+		stlen = p[1];
+		tt = mkatable(t, SRAT, buf, p, stlen, sizeof(Srat));
+		st = tt->tbl;
+		st->type = p[0];
+		switch (st->type) {
+			case SRlapic:
+				st->lapic.dom = p[2] | p[9] << 24 | p[10] << 16 | p[11] << 8;
+				st->lapic.apic = p[3];
+				st->lapic.sapic = p[8];
+				st->lapic.clkdom = l32get(p + 12);
+				if (l32get(p + 4) == 0) {
+					free(tt);
+					tt = nil;
+				}
+				break;
+			case SRmem:
+				st->mem.dom = l32get(p + 2);
+				st->mem.addr = l64get(p + 8);
+				st->mem.len = l64get(p + 16);
+				flags = l32get(p + 28);
+				if ((flags & 1) == 0) {	/* not enabled */
+					free(tt);
+					tt = nil;
+				} else {
+					st->mem.hplug = flags & 2;
+					st->mem.nvram = flags & 4;
+				}
+				break;
+			case SRlx2apic:
+				st->lx2apic.dom = l32get(p + 4);
+				st->lx2apic.apic = l32get(p + 8);
+				st->lx2apic.clkdom = l32get(p + 16);
+				if (l32get(p + 12) == 0) {
+					free(tt);
+					tt = nil;
+				}
+				break;
+			default:
+				print("unknown SRAT structure\n");
+				free(tt);
+				tt = nil;
+				break;
 		}
-		print("Length is %u ptr is %p\n", out.Length, out.Pointer);
+		if (tt != nil) {
+			finatable_nochildren(tt);
+			psliceappend(&slice, tt);
+		}
+	}
+	srat = finatable(t, &slice);
 
+	return srat;
+}
+
+static char *dumpslit(char *start, char *end, Slit *sl)
+{
+	int i;
+
+	if (sl == nil)
+		return start;
+	start = seprint(start, end, "acpi slit:\n");
+	for (i = 0; i < sl->rowlen * sl->rowlen; i++) {
+		start = seprint(start, end,
+						 "slit: %x\n",
+						 sl->e[i / sl->rowlen][i % sl->rowlen].dist);
 	}
+	start = seprint(start, end, "\n");
+	return start;
+}
+
+static int cmpslitent(void *v1, void *v2)
+{
+	SlEntry *se1, *se2;
+
+	se1 = v1;
+	se2 = v2;
+	return se1->dist - se2->dist;
+}
+
+static Atable *parseslit(Atable *parent,
+                                char *name, uint8_t *raw, size_t rawsize)
+{
+	Atable *t;
+	uint8_t *r, *re;
+	int i;
+	SlEntry *se;
+	size_t addsize, rowlen;
+	void *p;
+
+	addsize = sizeof(*slit);
+	rowlen = l64get(raw + 36);
+	addsize += rowlen * sizeof(SlEntry *);
+	addsize += sizeof(SlEntry) * rowlen * rowlen;
+
+	t = mkatable(parent, SLIT, name, raw, rawsize, addsize);
+	slit = t->tbl;
+	slit->rowlen = rowlen;
+	p = (void *)slit + sizeof(*slit);
+	slit->e = p;
+	p += rowlen * sizeof(SlEntry *);
+	for (i = 0; i < rowlen; i++) {
+		slit->e[i] = p;
+		p += sizeof(SlEntry) * rowlen;
+	}
+	for (i = 0, r = raw + 44, re = raw + rawsize; r < re; r++, i++) {
+		int j = i / rowlen;
+		int k = i % rowlen;
+
+		se = &slit->e[j][k];
+		se->dom = k;
+		se->dist = *r;
+	}
+
+#if 0
+	/* TODO: might need to sort this shit */
+	for (i = 0; i < slit->rowlen; i++)
+		qsort(slit->e[i], slit->rowlen, sizeof(slit->e[0][0]), cmpslitent);
 #endif
-	print("hi\n");
 
-	return 0;
+	return finatable_nochildren(t);
 }
 
+/*
+ * we use mp->machno (or index in Mach array) as the identifier,
+ * but ACPI relies on the apic identifier.
+ */
 int
-acpiinit(void)
+corecolor(int core)
 {
-	ACPI_STATUS as;
-	ACPI_TABLE_HEADER *h;
-	ACPI_BUFFER out;
-	int status;
-	int apiccnt = 1;
-	out.Length = ACPI_ALLOCATE_BUFFER;
-	out.Pointer = nil;
-	status = AcpiInitializeSubsystem();
-        if (ACPI_FAILURE(status))
-		panic("can't start acpi");
+	/* FIXME */
+	return -1;
+#if 0
+	Mach *m;
+	Srat *sl;
+	static int colors[32];
 
+	if(core < 0 || core >= MACHMAX)
+		return -1;
+	m = sys->machptr[core];
+	if(m == nil)
+		return -1;
 
-        status = AcpiInitializeTables(NULL, 16, FALSE);
-        if (ACPI_FAILURE(status))
-		panic("can't set up acpi tables");
+	if(core >= 0 && core < nelem(colors) && colors[core] != 0)
+		return colors[core] - 1;
+
+	for(sl = srat; sl != nil; sl = sl->next)
+		if(sl->type == SRlapic && sl->lapic.apic == m->apicno){
+			if(core >= 0 && core < nelem(colors))
+				colors[core] = 1 + sl->lapic.dom;
+			return sl->lapic.dom;
+		}
+	return -1;
+#endif
+}
+
+int pickcore(int mycolor, int index)
+{
+
+	if (slit == nil)
+		return 0;
+	return 0;
+#if 0
+	int color;
+	int ncorepercol;
+	ncorepercol = num_cores / slit->rowlen;
+	color = slit->e[mycolor][index / ncorepercol].dom;
+	return color * ncorepercol + index % ncorepercol;
+#endif
+}
+
+static char *polarity[4] = {
+	"polarity/trigger like in ISA",
+	"active high",
+	"BOGUS POLARITY",
+	"active low"
+};
 
-        status = AcpiLoadTables();
-        if (ACPI_FAILURE(status))
-		panic("Can't load ACPI tables");
+static char *trigger[] = {
+	"BOGUS TRIGGER",
+	"edge",
+	"BOGUS TRIGGER",
+	"level"
+};
 
-        status = AcpiEnableSubsystem(0);
-        if (ACPI_FAILURE(status))
-		panic("Can't enable ACPI subsystem");
+static char *printiflags(char *start, char *end, int flags)
+{
 
-        status = AcpiInitializeObjects(0);
-        if (ACPI_FAILURE(status))
-		panic("Can't Initialize ACPI objects");
+	return seprint(start, end, "[%s,%s]",
+					polarity[flags & AFpmask], trigger[(flags & AFtmask) >> 2]);
+}
 
-	int sublen;
-	uint8_t *p;
-	extern uint8_t *apicbase;
-	ACPI_TABLE_MADT *m;
-	status = AcpiGetTable(ACPI_SIG_MADT, apiccnt, &h);
-	if (ACPI_FAILURE(status))
-		panic("Can't find a MADT");
-	m = (ACPI_TABLE_MADT *)h;
-	print("APIC %d: %p 0x%x\n", apiccnt, (void *)(uint64_t)m->Address, m->Flags);
-	if(apicbase == nil){
-		if((apicbase = vmap((uintptr_t)m->Address, 1024)) == nil){
-			panic("%s: can't map apicbase\n", __func__);
+static char *dumpmadt(char *start, char *end, Atable *apics)
+{
+	Madt *mt;
+
+	if (apics == nil)
+		return start;
+
+	mt = apics->tbl;
+	if (mt == nil)
+		return seprint(start, end, "acpi: no MADT");
+	start = seprint(start, end, "acpi: MADT@%p: lapic paddr %p pcat %d:\n",
+	                 mt, mt->lapicpa, mt->pcat);
+	for (int i = 0; i < apics->nchildren; i++) {
+		Atable *apic = apics->children[i];
+		Apicst *st = apic->tbl;
+
+		switch (st->type) {
+			case ASlapic:
+				start =
+					seprint(start, end, "\tlapic pid %d id %d\n",
+							 st->lapic.pid, st->lapic.id);
+				break;
+			case ASioapic:
+			case ASiosapic:
+				start =
+					seprint(start, end,
+							 "\tioapic id %d addr %p ibase %d\n",
+							 st->ioapic.id, st->ioapic.addr, st->ioapic.ibase);
+				break;
+			case ASintovr:
+				start =
+					seprint(start, end, "\tintovr irq %d intr %d flags $%p",
+							 st->intovr.irq, st->intovr.intr, st->intovr.flags);
+				start = printiflags(start, end, st->intovr.flags);
+				start = seprint(start, end, "\n");
+				break;
+			case ASnmi:
+				start = seprint(start, end, "\tnmi intr %d flags $%p\n",
+								 st->nmi.intr, st->nmi.flags);
+				break;
+			case ASlnmi:
+				start =
+					seprint(start, end, "\tlnmi pid %d lint %d flags $%p\n",
+							 st->lnmi.pid, st->lnmi.lint, st->lnmi.flags);
+				break;
+			case ASlsapic:
+				start =
+					seprint(start, end,
+							 "\tlsapic pid %d id %d eid %d puid %d puids %s\n",
+							 st->lsapic.pid, st->lsapic.id, st->lsapic.eid,
+							 st->lsapic.puid, st->lsapic.puids);
+				break;
+			case ASintsrc:
+				start =
+					seprint(start, end,
+							 "\tintr type %d pid %d peid %d iosv %d intr %d %#x\n",
+							 st->type, st->intsrc.pid, st->intsrc.peid,
+							 st->intsrc.iosv, st->intsrc.intr,
+							 st->intsrc.flags);
+				start = printiflags(start, end, st->intsrc.flags);
+				start = seprint(start, end, "\n");
+				break;
+			case ASlx2apic:
+				start =
+					seprint(start, end, "\tlx2apic puid %d id %d\n",
+							 st->lx2apic.puid, st->lx2apic.id);
+				break;
+			case ASlx2nmi:
+				start =
+					seprint(start, end, "\tlx2nmi puid %d intr %d flags $%p\n",
+							 st->lx2nmi.puid, st->lx2nmi.intr,
+							 st->lx2nmi.flags);
+				break;
+			default:
+				start = seprint(start, end, "\t<unknown madt entry>\n");
 		}
-		print("%s: apicbase %#p -> %#p\n", __func__, (void *)(uint64_t)m->Address, apicbase);
-	}
-	if (! set_machine_mode()){
-		print("Set machine mode failed\n");
-		return 0;
 	}
+	start = seprint(start, end, "\n");
+	return start;
+}
 
-	p = (void*)&m[1];
-	sublen = m->Header.Length;
-	/* we only process the ones we're certain we need to. */
-	while (sublen > 0) {
-		switch(p[0]){
-		case ACPI_MADT_TYPE_LOCAL_APIC:
-		{
-			ACPI_MADT_LOCAL_APIC *l = (void *)p;
-			if (!l->LapicFlags)
+static Atable *parsemadt(Atable *parent,
+                                char *name, uint8_t *p, size_t size)
+{
+	Atable *t, *tt;
+	uint8_t *pe;
+	Madt *mt;
+	Apicst *st, *l;
+	int id;
+	size_t stlen;
+	char buf[16];
+	int i;
+	PSlice slice;
+
+	psliceinit(&slice);
+	t = mkatable(parent, MADT, name, p, size, sizeof(Madt));
+	mt = t->tbl;
+	mt->lapicpa = l32get(p + 36);
+	mt->pcat = l32get(p + 40);
+	pe = p + size;
+	for (p += 44, i = 0; p < pe; p += stlen, i++) {
+		snprint(buf, sizeof(buf), "%d", i);
+		stlen = p[1];
+		tt = mkatable(t, APIC, buf, p, stlen, sizeof(Apicst));
+		st = tt->tbl;
+		st->type = p[0];
+		switch (st->type) {
+			case ASlapic:
+				st->lapic.pid = p[2];
+				st->lapic.id = p[3];
+				if (l32get(p + 4) == 0) {
+					free(tt);
+					tt = nil;
+				}
 				break;
-			apicinit(l->Id, m->Address, apiccnt == 1);
-print("ACPICODE: apicinit(%d, %p, %d\n", l->Id, m->Address, apiccnt == 1);
-			apiccnt++;
-		}
-			break;
-		case ACPI_MADT_TYPE_IO_APIC:
-		{
-			ACPI_MADT_IO_APIC *io = (void *)p;
-			print("IOapic %d @ %p\n", io->Id, io->Address);
-			ioapicinit(io->Id, io->Address);
-print("ACPICODE: ioapicinit(%d, %p);\n", io->Id, (void*)(uint64_t)io->Address);
-		}
-			break;
-		case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
-		{
-			ACPI_MADT_INTERRUPT_OVERRIDE *e = (void *)p;
-			print("What an eyesore. Bus %d, SourceIrq %d, GlobalIrq %d, InitFlags 0x%x\n",
-			      e->Bus, e->SourceIrq, e->GlobalIrq, e->IntiFlags);
-			eyesore = realloc(eyesore, numeyesore+1);
-			if (! eyesore)
-				panic("Ran out of eyesores");
-			eyesore[numeyesore] = *e;
-			numeyesore++;
-		}
-		break;
-		case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
-		{
-			ACPI_MADT_LOCAL_APIC_NMI *nmi = (void *)p;
-			apicnmi(nmi->ProcessorId, nmi->Lint, nmi->IntiFlags);
+			case ASioapic:
+				st->ioapic.id = id = p[2];
+				st->ioapic.addr = l32get(p + 4);
+				st->ioapic.ibase = l32get(p + 8);
+				/* ioapic overrides any ioapic entry for the same id */
+				for (int i = 0; i < pslicelen(&slice); i++) {
+					l = ((Atable *)psliceget(&slice, i))->tbl;
+					if (l->type == ASiosapic && l->iosapic.id == id) {
+						st->ioapic = l->iosapic;
+						/* we leave it linked; could be removed */
+						break;
+					}
+				}
+				break;
+			case ASintovr:
+				st->intovr.irq = p[3];
+				st->intovr.intr = l32get(p + 4);
+				st->intovr.flags = l16get(p + 8);
+				break;
+			case ASnmi:
+				st->nmi.flags = l16get(p + 2);
+				st->nmi.intr = l32get(p + 4);
+				break;
+			case ASlnmi:
+				st->lnmi.pid = p[2];
+				st->lnmi.flags = l16get(p + 3);
+				st->lnmi.lint = p[5];
+				break;
+			case ASladdr:
+				/* This is for 64 bits, perhaps we should not
+				 * honor it on 32 bits.
+				 */
+				mt->lapicpa = l64get(p + 8);
+				break;
+			case ASiosapic:
+				id = st->iosapic.id = p[2];
+				st->iosapic.ibase = l32get(p + 4);
+				st->iosapic.addr = l64get(p + 8);
+				/* iosapic overrides any ioapic entry for the same id */
+				for (int i = 0; i < pslicelen(&slice); i++) {
+					l = ((Atable*)psliceget(&slice, i))->tbl;
+					if (l->type == ASioapic && l->ioapic.id == id) {
+						l->ioapic = st->iosapic;
+						free(tt);
+						tt = nil;
+						break;
+					}
+				}
+				break;
+			case ASlsapic:
+				st->lsapic.pid = p[2];
+				st->lsapic.id = p[3];
+				st->lsapic.eid = p[4];
+				st->lsapic.puid = l32get(p + 12);
+				if (l32get(p + 8) == 0) {
+					free(tt);
+					tt = nil;
+				} else
+					kstrdup(&st->lsapic.puids, (char *)p + 16);
+				break;
+			case ASintsrc:
+				st->intsrc.flags = l16get(p + 2);
+				st->type = p[4];
+				st->intsrc.pid = p[5];
+				st->intsrc.peid = p[6];
+				st->intsrc.iosv = p[7];
+				st->intsrc.intr = l32get(p + 8);
+				st->intsrc.any = l32get(p + 12);
+				break;
+			case ASlx2apic:
+				st->lx2apic.id = l32get(p + 4);
+				st->lx2apic.puid = l32get(p + 12);
+				if (l32get(p + 8) == 0) {
+					free(tt);
+					tt = nil;
+				}
+				break;
+			case ASlx2nmi:
+				st->lx2nmi.flags = l16get(p + 2);
+				st->lx2nmi.puid = l32get(p + 4);
+				st->lx2nmi.intr = p[8];
+				break;
+			default:
+				print("unknown APIC structure\n");
+				free(tt);
+				tt = nil;
 		}
-			break;
-		default:
-			print("%s: can't handle subtable type %d\n", __func__, p[0]);
-			break;
+		if (tt != nil) {
+			finatable_nochildren(tt);
+			psliceappend(&slice, tt);
 		}
-		sublen -= p[1];
-		p += p[1];
 	}
+	apics = finatable(t, &slice);
 
-	/* Get the _PRT */
-	int i;
-	for(i = 0; i < 255; i++) {
-		static char path[255];
-		snprint(path, sizeof(path), "\\_SB.PCI%d._PRT", i);
-		as = AcpiEvaluateObject(ACPI_ROOT_OBJECT, path, NULL, &out);
-		if (!ACPI_SUCCESS(as))
-			continue;
-		print("------>GOT the PRT: %d\n", i);
-		print("Length is %u ptr is %p\n", out.Length, out.Pointer);
-		hexdump(out.Pointer, out.Length);
-		objwalk(out.Pointer);
+	return apics;
+}
 
-		as = AcpiGetDevices (nil, device, nil, nil);
-		print("acpigetdevices %d\n", as);
+static Atable *parsedmar(Atable *parent,
+                                char *name, uint8_t *raw, size_t rawsize)
+{
+	Atable *t, *tt;
+	int i;
+	int baselen = MIN(rawsize, 38);
+	int nentry, nscope, npath, off, dslen, dhlen, type, flags;
+	void *pathp;
+	char buf[16];
+	PSlice drhds;
+	Drhd *drhd;
+	Dmar *dt;
+
+	/* count the entries */
+	for (nentry = 0, off = 48; off < rawsize; nentry++) {
+		dslen = l16get(raw + off + 2);
+		print("acpi DMAR: entry %d is addr %p (0x%x/0x%x)\n",
+		       nentry, raw + off, l16get(raw + off), dslen);
+		off = off + dslen;
 	}
-
-/* per device code. Not useful yet.
-
-	as = AcpiGetIrqRoutingTable(some device, &out);
-	print("get the PRT: %d\n", as);
-	print("Length is %u ptr is %p\n", out.Length, out.Pointer);
-	hexdump(out.Pointer, out.Length);
-*/
-	/* PCI devices: Walk all devices. For those with interrupts, enable them. */
-	Pcidev*pci = nil;
-	for(pci = pcimatch(pci, 0, 0); pci; pci = pcimatch(pci, 0, 0)){
-		if (!pci->intl || pci->intl == 0xff)
+	print("DMAR: %d entries\n", nentry);
+
+	t = mkatable(parent, DMAR, name, raw, rawsize, sizeof(*dmar));
+	dt = t->tbl;
+	/* The table can be only partly filled. */
+	if (baselen >= 38 && raw[37] & 1)
+		dt->intr_remap = 1;
+	if (baselen >= 37)
+		dt->haw = raw[36] + 1;
+
+	/* Now we walk all the DMAR entries. */
+	psliceinit(&drhds);
+	for (off = 48, i = 0; i < nentry; i++, off += dslen) {
+		snprint(buf, sizeof(buf), "%d", i);
+		dslen = l16get(raw + off + 2);
+		type = l16get(raw + off);
+		// TODO(dcross): Introduce sensible symbolic constants
+		// for DMAR entry types. For right now, type 0 => DRHD.
+		// We skip everything else.
+		if (type != 0)
 			continue;
-		print("Interrupt %d: \n", pci->intl);
-		pcishowdev(pci);
-		int bus = BUSBNO(pci->tbdf);
-		int apicno = 1; /* for now */
-		int low = 0x1a000; /* is PCI always this? */
-		print("ACPICODE: ioapicintrinit(%d, %d, %d, 0x%x, 0x%x);\n", bus, apicno, pci->intl, pci->tbdf, low);
-
+		npath = 0;
+		nscope = 0;
+		for (int o = off + 16; o < (off + dslen); o += dhlen) {
+			nscope++;
+			dhlen = *(raw + o + 1);	// Single byte length.
+			npath += ((dhlen - 6) / 2);
+		}
+		tt = mkatable(t, DRHD, buf, raw + off, dslen,
+		              sizeof(Drhd) + 2 * npath +
+		              nscope * sizeof(DevScope));
+		flags = *(raw + off + 4);
+		drhd = tt->tbl;
+		drhd->all = flags & 1;
+		drhd->segment = l16get(raw + off + 6);
+		drhd->rba = l64get(raw + off + 8);
+		drhd->nscope = nscope;
+		drhd->scopes = (void *)drhd + sizeof(Drhd);
+		pathp = (void *)drhd +
+		    sizeof(Drhd) + nscope * sizeof(DevScope);
+		for (int i = 0, o = off + 16; i < nscope; i++) {
+			DevScope *ds = &drhd->scopes[i];
+
+			dhlen = *(raw + o + 1);
+			ds->enumeration_id = *(raw + o + 4);
+			ds->start_bus_number = *(raw + o + 5);
+			ds->npath = (dhlen - 6) / 2;
+			ds->paths = pathp;
+			for (int j = 0; j < ds->npath; j++)
+				ds->paths[j] = l16get(raw + o + 6 + 2*j);
+			pathp += 2*ds->npath;
+			o += dhlen;
+		}
+		/*
+		 * NOTE: if all is set, there should be no scopes of type
+		 * This being ACPI, where vendors randomly copy tables
+		 * from one system to another, and creating breakage,
+		 * anything is possible. But we'll warn them.
+		 */
+		finatable_nochildren(tt);
+		psliceappend(&drhds, tt);
 	}
-	print("ACPICODE: ioapicintrinit(0xff, DONE\n");
-	return 0;
-}
+	dmar = finatable(t, &drhds);
 
-static Chan*
-acpiattach(char *spec)
-{
-	return nil;
-//	return devattach(L'α', spec);
+	return dmar;
 }
 
-static Walkqid*
-acpiwalk(Chan *c, Chan *nc, char **name, int nname)
+/*
+ * Map the table and keep it there.
+ */
+static Atable *parsessdt(Atable *parent,
+                                char *name, uint8_t *raw, size_t size)
 {
-	return devwalk(c, nc, name, nname, acpidir, nelem(acpidir), acpigen);
+	Atable *t;
+	Sdthdr *h;
+
+	/*
+	 * We found it and it is too small.
+	 * Simply return with no side effect.
+	 */
+	if (size < Sdthdrsz)
+		return nil;
+	t = mkatable(parent, SSDT, name, raw, size, 0);
+	h = (Sdthdr *)raw;
+	memmove(t->name, h->sig, sizeof(h->sig));
+	t->name[sizeof(h->sig)] = '\0';
+
+	return finatable_nochildren(t);
 }
 
-static int32_t
-acpistat(Chan *c, uint8_t *dp, int32_t n)
+static char *dumptable(char *start, char *end, char *sig, uint8_t *p, int l)
 {
-	return devstat(c, dp, n, acpidir, nelem(acpidir), acpigen);
+	int n, i;
+
+	if (2 > 1) {
+		start = seprint(start, end, "%s @ %#p\n", sig, p);
+		if (2 > 2)
+			n = l;
+		else
+			n = 256;
+		for (i = 0; i < n; i++) {
+			if ((i % 16) == 0)
+				start = seprint(start, end, "%x: ", i);
+			start = seprint(start, end, " %2.2x", p[i]);
+			if ((i % 16) == 15)
+				start = seprint(start, end, "\n");
+		}
+		start = seprint(start, end, "\n");
+		start = seprint(start, end, "\n");
+	}
+	return start;
 }
 
-static Chan*
-acpiopen(Chan *c, int omode)
+static char *seprinttable(char *s, char *e, Atable *t)
 {
-	return devopen(c, omode, acpidir, nelem(acpidir), acpigen);
+	uint8_t *p;
+	int i, n;
+
+	p = (uint8_t *)t->tbl;	/* include header */
+	n = t->rawsize;
+	s = seprint(s, e, "%s @ %#p\n", t->name, p);
+	for (i = 0; i < n; i++) {
+		if ((i % 16) == 0)
+			s = seprint(s, e, "%x: ", i);
+		s = seprint(s, e, " %2.2x", p[i]);
+		if ((i % 16) == 15)
+			s = seprint(s, e, "\n");
+	}
+	return seprint(s, e, "\n\n");
 }
 
-static void
-acpiclose(Chan *c)
+static void *rsdsearch(char *signature)
 {
+//	uintptr_t p;
+//	uint8_t *bda;
+//	void *rsd;
+
+	/*
+	 * Search for the data structure signature:
+	 * 1) in the BIOS ROM between 0xE0000 and 0xFFFFF.
+	 */
+	return sigscan(KADDR(0xE0000), 0x20000, signature);
 }
 
-#if 0
-static char*ttext;
-static int tlen;
-#endif
+/*
+ * Note: some of this comment is from the unfinished user interpreter.
+ *
+ * The DSDT is always given to the user interpreter.
+ * Tables listed here are also loaded from the XSDT:
+ * MSCT, MADT, and FADT are processed by us, because they are
+ * required to do early initialization before we have user processes.
+ * Other tables are given to the user level interpreter for
+ * execution.
+ *
+ * These historically returned a value to tell acpi whether or not it was okay
+ * to unmap the table.  (return 0 means there was no table, meaning it was okay
+ * to unmap).  We just use the kernbase mapping, so it's irrelevant.
+ *
+ * N.B. The intel source code defines the constants for ACPI in a
+ * non-endian-independent manner. Rather than bring in the huge wad o' code
+ * that represents, we just the names.
+ */
+typedef struct Parser {
+	char *sig;
+	Atable *(*parse)(Atable *parent,
+	                        char *name, uint8_t *raw, size_t rawsize);
+} Parser;
+
+
+static Parser ptable[] = {
+	{"FACP", parsefadt},
+	{"APIC", parsemadt},
+	{"DMAR", parsedmar},
+	{"SRAT", parsesrat},
+	{"SLIT", parseslit},
+	{"MSCT", parsemsct},
+	{"SSDT", parsessdt},
+//	{"HPET", parsehpet},
+};
 
-static int32_t
-acpiread(Chan *c, void *a, int32_t n, int64_t off)
+/*
+ * process xsdt table and load tables with sig, or all if nil.
+ * (XXX: should be able to search for sig, oemid, oemtblid)
+ */
+static void parsexsdt(Atable *root)
 {
-	uint64_t q;
-
-	q = c->qid.path;
-	switch(q){
-	case Qdir:
-		return devdirread(c, a, n, acpidir, nelem(acpidir), acpigen);
-	case Qtbl:
-		return -1; //readstr(off, a, n, ttext);
-	case Qio:
-		return -1; //regio(reg, a, n, off, 0);
+	Sdthdr *sdt;
+	Atable *table;
+	PSlice slice;
+	size_t l, end;
+	uintptr_t dhpa;
+	//Atable *n;
+	uint8_t *tbl;
+print("1\n");
+	psliceinit(&slice);
+print("2\n");
+print("xsdt %p\n", xsdt);
+	tbl = xsdt->p + sizeof(Sdthdr);
+	end = xsdt->len - sizeof(Sdthdr);
+	print("%s: tbl %p, end %d\n", __func__, tbl, end);
+	for (int i = 0; i < end; i += xsdt->asize) {
+		dhpa = (xsdt->asize == 8) ? l64get(tbl + i) : l32get(tbl + i);
+		sdt = sdtmap(dhpa, &l, 1);
+		print("sdt for map of %p, %d, 1 is %p\n", (void *)dhpa, l, sdt);
+		if (sdt == nil)
+			continue;
+		print("acpi: %s: addr %#p\n", __func__, sdt);
+		for (int j = 0; j < nelem(ptable); j++) {
+			print("tb sig %s\n", ptable[j].sig);
+			if (memcmp(sdt->sig, ptable[j].sig, sizeof(sdt->sig)) == 0) {
+				table = ptable[j].parse(root, ptable[j].sig, (void *)sdt, l);
+				if (table != nil)
+					psliceappend(&slice, table);
+				break;
+			}
+		}
 	}
-	error(Eperm);
-	return -1;
+	print("FINATABLE\n\n\n\n"); delay(1000);
+	finatable(root, &slice);
 }
 
-static int32_t
-acpiwrite(Chan *c, void *a, int32_t n, int64_t off)
+void makeindex(Atable *root)
 {
-	if(c->qid.path == Qio){
-		//if(reg == nil)
-		error("region not configured");
-	}
-	if(c->qid.path != Qctl)
-		error(Eperm);
+	uint64_t index;
 
-	error("NP");
-#if 0
-	cb = parsecmd(a, n);
-	if(waserror()){
-		free(cb);
-		nexterror();
-	}
-	ct = lookupcmd(cb, ctls, nelem(ctls));
-	DBG("acpi ctl %s\n", cb->f[0]);
-	switch(ct->index){
-	case CMregion:
-		r = reg;
-		if(r == nil){
-			r = smalloc(sizeof(Reg));
-			r->name = nil;
-		}
-		kstrdup(&r->name, cb->f[1]);
-		r->spc = acpiregid(cb->f[2]);
-		if(r->spc < 0){
-			free(r);
-			reg = nil;
-			error("bad region type");
-		}
-		if(r->spc == Rpcicfg || r->spc == Rpcibar){
-			rno = r->base>>Rpciregshift & Rpciregmask;
-			fun = r->base>>Rpcifunshift & Rpcifunmask;
-			dev = r->base>>Rpcidevshift & Rpcidevmask;
-			bus = r->base>>Rpcibusshift & Rpcibusmask;
-			r->tbdf = MKBUS(BusPCI, bus, dev, fun);
-			r->base = rno;	/* register ~ our base addr */
-		}
-		r->base = strtoull(cb->f[3], nil, 0);
-		r->len = strtoull(cb->f[4], nil, 0);
-		r->accsz = strtoul(cb->f[5], nil, 0);
-		if(r->accsz < 1 || r->accsz > 4){
-			free(r);
-			reg = nil;
-			error("bad region access size");
-		}
-		reg = r;
-		DBG("region %s %s %llux %llux sz%d",
-			r->name, acpiregstr(r->spc), r->base, r->len, r->accsz);
-		break;
-	case CMgpe:
-		i = strtoul(cb->f[1], nil, 0);
-		if(i >= ngpes)
-			error("gpe out of range");
-		kstrdup(&gpes[i].obj, cb->f[2]);
-		DBG("gpe %d %s\n", i, gpes[i].obj);
-		setgpeen(i, 1);
-		break;
-	default:
-		panic("acpi: unknown ctl");
-	}
-	poperror();
-	free(cb);
-	return n;
-#endif
-	return -1;
+	if (root == nil)
+		return;
+	index = root->qid.path >> QIndexShift;
+	atableindex[index] = root;
+	for (int k = 0; k < root->nchildren; k++)
+		makeindex(root->children[k]);
 }
 
+static void parsersdptr(void)
+{
+	Rsdp *rsd;
+	int asize, cksum;
+	uintptr_t sdtpa;
 
-Dev acpidevtab = {
-	.dc = L'α',
-	.name = "acpi",
-
-	.reset = devreset,
-	.init = devinit,
-	.shutdown = devshutdown,
-	.attach = acpiattach,
-	.walk = acpiwalk,
-	.stat = acpistat,
-	.open = acpiopen,
-	.create = devcreate,
-	.close = acpiclose,
-	.read = acpiread,
-	.bread = devbread,
-	.write = acpiwrite,
-	.bwrite = devbwrite,
-	.remove = devremove,
-	.wstat = devwstat,
-};
+//	static_assert(sizeof(Sdthdr) == 36);
 
-static int tbdf(ACPI_PCI_ID *p)
-{
-	return (p->Bus << 8) | (p->Device << 3) | (p->Function);
-}
-
-ACPI_STATUS
-AcpiOsReadPciConfiguration (
-    ACPI_PCI_ID             *PciId,
-    UINT32                  Reg,
-    UINT64                  *Value,
-    UINT32                  Width)
-{
-	Pcidev p;
-	p.tbdf = tbdf(PciId);
-	print("%s\n", __func__);
-	switch(Width) {
-	case 32:
-		*Value = pcicfgr32(&p, Reg);
-		break;
-	case 16:
-		*Value = pcicfgr16(&p, Reg);
-		break;
-	case 8:
-		*Value = pcicfgr8(&p, Reg);
-		break;
-	default:
-		panic("Can't read pci: bad width %d\n", Width);
+	/* Find the root pointer. */
+	rsd = rsdsearch("RSD PTR ");
+	if (rsd == nil) {
+		print("NO RSDP\n");
+		return;
 	}
 
-	return AE_OK;
-
-}
-
-ACPI_STATUS
-AcpiOsWritePciConfiguration (
-    ACPI_PCI_ID             *PciId,
-    UINT32                  Reg,
-    UINT64                  Value,
-    UINT32                  Width)
-{
-	Pcidev p;
-	p.tbdf = tbdf(PciId);
-	print("%s\n", __func__);
-	switch(Width) {
-	case 32:
-		pcicfgw32(&p, Reg, Value);
-		break;
-	case 16:
-		pcicfgw16(&p, Reg, Value);
-		break;
-	case 8:
-		pcicfgw8(&p, Reg, Value);
-		break;
-	default:
-		panic("Can't read pci: bad width %d\n", Width);
+	/*
+	 * Initialize the root of ACPI parse tree.
+	 */
+	lastpath = Qroot;
+	root = mkatable(nil, XSDT, devname(), nil, 0, sizeof(Xsdt));
+	root->parent = root;
+
+	print("/* RSDP */ Rsdp = {%08c, %x, %06c, %x, %p, %d, %p, %x}\n",
+		   rsd->signature, rsd->rchecksum, rsd->oemid, rsd->revision,
+		   *(uint32_t *)rsd->raddr, *(uint32_t *)rsd->length,
+		   *(uint32_t *)rsd->xaddr, rsd->xchecksum);
+
+	print("acpi: RSD PTR@ %#p, physaddr $%p length %ud %#llx rev %d\n",
+		   rsd, l32get(rsd->raddr), l32get(rsd->length),
+		   l64get(rsd->xaddr), rsd->revision);
+
+	if (rsd->revision >= 2) {
+		cksum = sdtchecksum(rsd, 36);
+		if (cksum != 0) {
+			print("acpi: bad RSD checksum %d, 64 bit parser aborted\n", cksum);
+			return;
+		}
+		sdtpa = l64get(rsd->xaddr);
+		asize = 8;
+	} else {
+		cksum = sdtchecksum(rsd, 20);
+		if (cksum != 0) {
+			print("acpi: bad RSD checksum %d, 32 bit parser aborted\n", cksum);
+			return;
+		}
+		sdtpa = l32get(rsd->raddr);
+		asize = 4;
 	}
 
-	return AE_OK;
+	/*
+	 * process the RSDT or XSDT table.
+	 */
+	xsdt = root->tbl;
+	xsdt->p = sdtmap(sdtpa, &xsdt->len, 1);
+	if (xsdt->p == nil) {
+		print("acpi: sdtmap failed\n");
+		return;
+	}
+	if ((xsdt->p[0] != 'R' && xsdt->p[0] != 'X')
+		|| memcmp(xsdt->p + 1, "SDT", 3) != 0) {
+		print("acpi: xsdt sig: %c%c%c%c\n",
+		       xsdt->p[0], xsdt->p[1], xsdt->p[2], xsdt->p[3]);
+		xsdt = nil;
+		return;
+	}
+	xsdt->asize = asize;
+	print("acpi: XSDT %#p\n", xsdt);
+	parsexsdt(root);
+	print("parsexdt done: lastpath %d\n", lastpath);
+	atableindex = reallocarray(nil, lastpath, sizeof(Atable *));
+	assert(atableindex != nil);
+	makeindex(root);
 }
 
 /*
- * Miscellaneous
+ * The invariant that each level in the tree has an associated
+ * Atable implies that each chan can be mapped to an Atable.
+ * The assertions here enforce that invariant.
  */
-BOOLEAN
-AcpiOsReadable (
-    void                    *Pointer,
-    ACPI_SIZE               Length)
+static Atable *genatable(Chan *c)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
-}
+	Atable *a;
+	uint64_t ai;
 
+	ai = c->qid.path >> QIndexShift;
+	assert(ai < lastpath);
+	a = atableindex[ai];
+	assert(a != nil);
 
-BOOLEAN
-AcpiOsWritable (
-    void                    *Pointer,
-    ACPI_SIZE               Length)
-{
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
+	return a;
 }
 
-
-UINT64
-AcpiOsGetTimer (
-    void)
+static int acpigen(Chan *c, char *name, Dirtab *tab, int ntab,
+		   int i, Dir *dp)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
-}
+	Atable *a = genatable(c);
 
-
-ACPI_STATUS
-AcpiOsSignal (
-    UINT32                  Function,
-    void                    *Info)
-{
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
+	if (i == DEVDOTDOT) {
+		assert((c->qid.path & QIndexMask) == Qdir);
+		devdir(c, a->parent->qid, a->parent->name, 0, eve, DMDIR|0555, dp);
+		return 1;
+	}
+	return devgen(c, name, a->cdirs, a->nchildren + NQtypes, i, dp);
 }
 
-void ACPI_INTERNAL_VAR_XFACE
-AcpiOsPrintf (
-    const char              *Format,
-    ...)
+/*
+ * Print the contents of the XSDT.
+ */
+static void dumpxsdt(void)
 {
-	va_list args;
-
-	va_start(args, Format);
-	print((char *)Format, args);
-	va_end(args);
+	print("xsdt: len = %lu, asize = %lu, p = %p\n",
+	       xsdt->len, xsdt->asize, xsdt->p);
 }
 
-void
-AcpiOsVprintf (
-    const char              *Format,
-    va_list                 Args)
+static char *dumpGas(char *start, char *end, char *prefix, Gas *g)
 {
-	/* This is a leaf function, and this function is required to implement
-	 * the va_list argument. I couldn't find any other way to do this. */
-	static char buf[1024];
-	vseprint(buf, &buf[1023], (char *)Format, Args);
-	print(buf);
+	start = seprint(start, end, "%s", prefix);
+
+	switch (g->spc) {
+		case Rsysmem:
+		case Rsysio:
+		case Rembed:
+		case Rsmbus:
+		case Rcmos:
+		case Rpcibar:
+		case Ripmi:
+			start = seprint(start, end, "[%s ", regnames[g->spc]);
+			break;
+		case Rpcicfg:
+			start = seprint(start, end, "[pci ");
+			start =
+				seprint(start, end, "dev %#p ",
+						 (uint32_t)(g->addr >> 32) & 0xFFFF);
+			start =
+				seprint(start, end, "fn %#p ",
+						 (uint32_t)(g->addr & 0xFFFF0000) >> 16);
+			start =
+				seprint(start, end, "adr %#p ", (uint32_t)(g->addr & 0xFFFF));
+			break;
+		case Rfixedhw:
+			start = seprint(start, end, "[hw ");
+			break;
+		default:
+			start = seprint(start, end, "[spc=%#p ", g->spc);
+	}
+	start = seprint(start, end, "off %d len %d addr %#p sz%d]",
+					 g->off, g->len, g->addr, g->accsz);
+	start = seprint(start, end, "\n");
+	return start;
 }
 
-void
-AcpiOsFree (
-    void *                  Memory)
+static unsigned int getbanked(uintptr_t ra, uintptr_t rb, int sz)
 {
-	//print("%s\n", __func__);
-	free(Memory);
+	unsigned int r;
+
+	r = 0;
+	switch (sz) {
+		case 1:
+			if (ra != 0)
+				r |= inb(ra);
+			if (rb != 0)
+				r |= inb(rb);
+			break;
+		case 2:
+			if (ra != 0)
+				r |= ins(ra);
+			if (rb != 0)
+				r |= ins(rb);
+			break;
+		case 4:
+			if (ra != 0)
+				r |= inl(ra);
+			if (rb != 0)
+				r |= inl(rb);
+			break;
+		default:
+			print("getbanked: wrong size\n");
+	}
+	return r;
 }
 
-void *
-AcpiOsAllocate (
-    ACPI_SIZE               Size)
+static unsigned int setbanked(uintptr_t ra, uintptr_t rb, int sz, int v)
 {
-	//print("%s\n", __func__);
-	return malloc(Size);
+	unsigned int r;
+
+	r = -1;
+	switch (sz) {
+		case 1:
+			if (ra != 0)
+				outb(ra, v);
+			if (rb != 0)
+				outb(rb, v);
+			break;
+		case 2:
+			if (ra != 0)
+				outs(ra, v);
+			if (rb != 0)
+				outs(rb, v);
+			break;
+		case 4:
+			if (ra != 0)
+				outl(ra, v);
+			if (rb != 0)
+				outl(rb, v);
+			break;
+		default:
+			print("setbanked: wrong size\n");
+	}
+	return r;
 }
 
-void *
-AcpiOsMapMemory (
-    ACPI_PHYSICAL_ADDRESS   Where,
-    ACPI_SIZE               Length)
+static unsigned int getpm1ctl(void)
 {
-	void *v = vmap(Where, Length);
-	print("%s %p = vmap(%p,0x%x)\n", __func__, v, (void*)Where, Length);
-	print("Val @ %p is 0x%x\n", v, *(int *)v);
-	return v;
+	assert(fadt != nil);
+	return getbanked(fadt->pm1acntblk, fadt->pm1bcntblk, fadt->pm1cntlen);
 }
 
-void
-AcpiOsUnmapMemory (
-    void                    *LogicalAddress,
-    ACPI_SIZE               Size)
+static void setpm1sts(unsigned int v)
 {
-	print("%s %p %d \n", __func__, LogicalAddress, Size);
-	vunmap(LogicalAddress, Size);
+	assert(fadt != nil);
+	setbanked(fadt->pm1aevtblk, fadt->pm1bevtblk, fadt->pm1evtlen / 2, v);
 }
 
-ACPI_STATUS
-AcpiOsGetPhysicalAddress (
-    void                    *LogicalAddress,
-    ACPI_PHYSICAL_ADDRESS   *PhysicalAddress)
+static unsigned int getpm1sts(void)
 {
-	ACPI_PHYSICAL_ADDRESS ret = mmuphysaddr((uintptr_t)LogicalAddress);
-	print("%s %p = mmyphysaddr(%p)", __func__, (void *)ret, LogicalAddress);
-	*PhysicalAddress = ret;
-	return AE_OK;
+	assert(fadt != nil);
+	return getbanked(fadt->pm1aevtblk, fadt->pm1bevtblk, fadt->pm1evtlen / 2);
 }
 
-/* This is the single threaded version of
- * these functions. This is now NetBSD does it. */
-ACPI_STATUS
-AcpiOsCreateSemaphore (
-    UINT32                  MaxUnits,
-    UINT32                  InitialUnits,
-    ACPI_SEMAPHORE          *OutHandle)
+static unsigned int getpm1en(void)
 {
-	//print("%s\n", __func__);
-	*OutHandle = (ACPI_SEMAPHORE) 1;
-	return AE_OK;
-}
+	int sz;
 
-ACPI_STATUS
-AcpiOsDeleteSemaphore (
-    ACPI_SEMAPHORE          Handle)
-{
-	//print("%s\n", __func__);
-	return AE_OK;
+	assert(fadt != nil);
+	sz = fadt->pm1evtlen / 2;
+	return getbanked(fadt->pm1aevtblk + sz, fadt->pm1bevtblk + sz, sz);
 }
 
-ACPI_STATUS
-AcpiOsWaitSemaphore (
-    ACPI_SEMAPHORE          Handle,
-    UINT32                  Units,
-    UINT16                  Timeout)
+static int getgpeen(int n)
 {
-	//print("%s\n", __func__);
-	return AE_OK;
+	return inb(gpes[n].enio) & 1 << gpes[n].enbit;
 }
 
-ACPI_STATUS
-AcpiOsSignalSemaphore (
-    ACPI_SEMAPHORE          Handle,
-    UINT32                  Units)
+static void setgpeen(int n, unsigned int v)
 {
-	//print("%s\n", __func__);
-	return AE_OK;
+	int old;
+
+	old = inb(gpes[n].enio);
+	if (v)
+		outb(gpes[n].enio, old | 1 << gpes[n].enbit);
+	else
+		outb(gpes[n].enio, old & ~(1 << gpes[n].enbit));
 }
 
-/* this is the single threaded case and as minix shows there is nothing to do. */
-ACPI_STATUS
-AcpiOsCreateLock (
-    ACPI_SPINLOCK           *OutHandle)
+static void clrgpests(int n)
 {
-	//print("%s\n", __func__);
-	*OutHandle = nil;
-	return AE_OK;
+	outb(gpes[n].stsio, 1 << gpes[n].stsbit);
 }
 
-void
-AcpiOsDeleteLock (
-    ACPI_SPINLOCK           Handle)
+static unsigned int getgpests(int n)
 {
-	//print("%s\n", __func__);
+	return inb(gpes[n].stsio) & 1 << gpes[n].stsbit;
 }
 
-ACPI_CPU_FLAGS
-AcpiOsAcquireLock (
-    ACPI_SPINLOCK           Handle)
+#if 0
+static void acpiintr(Ureg *, void *)
 {
-	//print("%s\n", __func__);
-	return 0;
+	int i;
+	unsigned int sts, en;
+
+	print("acpi: intr\n");
+
+	for (i = 0; i < ngpes; i++)
+		if (getgpests(i)) {
+			print("gpe %d on\n", i);
+			en = getgpeen(i);
+			setgpeen(i, 0);
+			clrgpests(i);
+			if (en != 0)
+				print("acpiitr: calling gpe %d\n", i);
+			//  queue gpe for calling gpe->ho in the
+			//  aml process.
+			//  enable it again when it returns.
+		}
+	sts = getpm1sts();
+	en = getpm1en();
+	print("acpiitr: pm1sts %#p pm1en %#p\n", sts, en);
+	if (sts & en)
+		print("have enabled events\n");
+	if (sts & 1)
+		print("power button\n");
+	// XXX serve other interrupts here.
+	setpm1sts(sts);
 }
+#endif
 
-void
-AcpiOsReleaseLock (
-    ACPI_SPINLOCK           Handle,
-    ACPI_CPU_FLAGS          Flags)
+static void initgpes(void)
 {
-	//print("%s\n", __func__);
+	int i, n0, n1;
+
+	assert(fadt != nil);
+	n0 = fadt->gpe0blklen / 2;
+	n1 = fadt->gpe1blklen / 2;
+	ngpes = n0 + n1;
+	gpes = mallocz(sizeof(Gpe) * ngpes, 1);
+	for (i = 0; i < n0; i++) {
+		gpes[i].nb = i;
+		gpes[i].stsbit = i & 7;
+		gpes[i].stsio = fadt->gpe0blk + (i >> 3);
+		gpes[i].enbit = (n0 + i) & 7;
+		gpes[i].enio = fadt->gpe0blk + ((n0 + i) >> 3);
+	}
+	for (i = 0; i + n0 < ngpes; i++) {
+		gpes[i + n0].nb = fadt->gp1base + i;
+		gpes[i + n0].stsbit = i & 7;
+		gpes[i + n0].stsio = fadt->gpe1blk + (i >> 3);
+		gpes[i + n0].enbit = (n1 + i) & 7;
+		gpes[i + n0].enio = fadt->gpe1blk + ((n1 + i) >> 3);
+	}
+	for (i = 0; i < ngpes; i++) {
+		setgpeen(i, 0);
+		clrgpests(i);
+	}
 }
 
-struct handler {
-	ACPI_OSD_HANDLER        ServiceRoutine;
-	void                    *Context;
-};
-
-/* The ACPI interrupt signature and the Harvey one are not compatible. So, we pass an arg to
- * intrenable that can in turn be used to this function to call the ACPI handler. */
-static void acpihandler(Ureg *_, void *arg)
+static void acpiioalloc(unsigned int addr, int len)
 {
-	struct handler *h = arg;
-	h->ServiceRoutine(h->Context);
+	if (addr != 0)
+		print("Just TAKING port %016lx to %016lx\n", addr, addr + len);
 }
 
-ACPI_STATUS
-AcpiOsInstallInterruptHandler (
-    UINT32                  InterruptNumber,
-    ACPI_OSD_HANDLER        ServiceRoutine,
-    void                    *Context)
+static void acpiinitonce(void)
 {
-	/* minix says "don't do it". So we don't, yet. */
-	return AE_OK;
-	struct handler *h = malloc(sizeof(*h));
-	if (! h)
-		return AE_NO_MEMORY;
-	h->ServiceRoutine = ServiceRoutine;
-	h->Context = Context;
-	print("%s %d %p %p \n", __func__, InterruptNumber, ServiceRoutine, Context);
-	/* once enabled, can't be disabled; ignore the return value unless it's nil. */
-	intrenable(InterruptNumber, acpihandler, h, 0x5, "ACPI interrupt handler");
-	return AE_OK;
+	parsersdptr();
+	if (root != nil)
+		print("ACPI initialized\n");
 }
 
-ACPI_STATUS
-AcpiOsRemoveInterruptHandler (
-    UINT32                  InterruptNumber,
-    ACPI_OSD_HANDLER        ServiceRoutine)
+int acpiinit(void)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
+	static int once = 0;
+	//die("acpiinit");
+	if (! once)
+		acpiinitonce();
+	once++;
+	return (root == nil) ? -1 : 0;
 }
 
-void
-AcpiOsWaitEventsComplete (
-	void)
+static Chan *acpiattach(char *spec)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
+	Chan *c;
+	/*
+	 * This was written for the stock kernel.
+	 * This code must use 64 registers to be acpi ready in nix.
+	 */
+	if (acpiinit() < 0)
+		error("no acpi");
+
+	/*
+	 * should use fadt->xpm* and fadt->xgpe* registers for 64 bits.
+	 * We are not ready in this kernel for that.
+	 */
+	assert(fadt != nil);
+	acpiioalloc(fadt->smicmd, 1);
+	acpiioalloc(fadt->pm1aevtblk, fadt->pm1evtlen);
+	acpiioalloc(fadt->pm1bevtblk, fadt->pm1evtlen);
+	acpiioalloc(fadt->pm1acntblk, fadt->pm1cntlen);
+	acpiioalloc(fadt->pm1bcntblk, fadt->pm1cntlen);
+	acpiioalloc(fadt->pm2cntblk, fadt->pm2cntlen);
+	acpiioalloc(fadt->pmtmrblk, fadt->pmtmrlen);
+	acpiioalloc(fadt->gpe0blk, fadt->gpe0blklen);
+	acpiioalloc(fadt->gpe1blk, fadt->gpe1blklen);
+
+	initgpes();
+#ifdef RON_SAYS_CONFIG_WE_ARE_NOT_WORTHY
+	/* this is frightening. SMI: just say no. Although we will almost
+	 * certainly find that we have no choice.
+	 *
+	 * This starts ACPI, which may require we handle
+	 * power mgmt events ourselves. Use with care.
+	 */
+	outb(fadt->smicmd, fadt->acpienable);
+	for (i = 0; i < 10; i++)
+		if (getpm1ctl() & Pm1SciEn)
+			break;
+	if (i == 10)
+		error("acpi: failed to enable\n");
+	if (fadt->sciint != 0)
+		intrenable(fadt->sciint, acpiintr, 0, BUSUNKNOWN, "acpi");
+#endif
+	c = devattach(devdc(), spec);
+
+	return c;
 }
 
-void
-AcpiOsSleep (
-    UINT64                  Milliseconds)
+static Walkqid*acpiwalk(Chan *c, Chan *nc, char **name,
+								int nname)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
+	/*
+	 * Note that devwalk hard-codes a test against the location of 'devgen',
+	 * so we pretty much have to not pass it here.
+	 */
+	return devwalk(c, nc, name, nname, nil, 0, acpigen);
 }
 
-void
-AcpiOsStall(
-    UINT32                  Microseconds)
+static int acpistat(Chan *c, uint8_t *dp, int n)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
+	Atable *a = genatable(c);
+
+	if (c->qid.type == QTDIR)
+		a = a->parent;
+	assert(a != nil);
+
+	/* TODO(dcross): make acpigen work here. */
+	return devstat(c, dp, n, a->cdirs, a->nchildren + NQtypes, devgen);
 }
 
-ACPI_THREAD_ID
-AcpiOsGetThreadId (
-    void)
+static Chan *acpiopen(Chan *c, int omode)
 {
-	/* What to do here? ACPI won't take 0 for an answer.
-	 * I guess tell it we're 1? What do we do? */
-	return 1;
-	//print("%s\n", __func__);
-	Proc *up = externup();
-	return up->pid;
+	return devopen(c, omode, nil, 0, acpigen);
 }
 
-ACPI_STATUS
-AcpiOsExecute (
-    ACPI_EXECUTE_TYPE       Type,
-    ACPI_OSD_EXEC_CALLBACK  Function,
-    void                    *Context)
+static void acpiclose(Chan *unused)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
 }
 
-ACPI_STATUS
-AcpiOsReadPort (
-    ACPI_IO_ADDRESS         Address,
-    UINT32                  *Value,
-    UINT32                  Width)
+static char *ttext;
+static int tlen;
+
+// Get the table from the qid.
+// Read that one table using the pointers.
+static int32_t acpiread(Chan *c, void *a, int32_t n, int64_t off)
 {
-	/* Ooooooookay ... ACPI specifies the IO width in *bits*. */
-	switch(Width) {
-	case 4*8:
-		*Value = inl(Address);
-		break;
-	case 2*8:
-		*Value = ins(Address);
-		break;
-	case 1*8:
-		*Value = inb(Address);
-		break;
-	default:
-		panic("%s, bad width %d", __func__, Width);
-		break;
+	long q;
+	Atable *t;
+	char *ns, *s, *e, *ntext;
+
+	if (ttext == nil) {
+		tlen = 32768;
+		ttext = mallocz(tlen, 1);
 	}
-	print("%s 0x%x 0x%x\n", __func__, Address, *Value);
-	return AE_OK;
-}
-
-ACPI_STATUS
-AcpiOsWritePort (
-    ACPI_IO_ADDRESS         Address,
-    UINT32                  Value,
-    UINT32                  Width)
-{
-	switch(Width) {
-	case 4*8:
-		outl(Address, Value);
-		break;
-	case 2*8:
-		outs(Address, Value);
-		break;
-	case 1*8:
-		outb(Address, Value);
-		break;
+	if (ttext == nil)
+		error("acpiread: no memory");
+	q = c->qid.path & QIndexMask;
+	switch (q) {
+	case Qdir:
+		return devdirread(c, a, n, nil, 0, acpigen);
+	case Qraw:
+		return readmem(off, a, n, ttext, tlen);
+	case Qtbl:
+		s = ttext;
+		e = ttext + tlen;
+		strlcpy(s, "no tables\n", tlen);
+		for (t = tfirst; t != nil; t = t->next) {
+			ns = seprinttable(s, e, t);
+			while (ns == e - 1) {
+				ntext = realloc(ttext, tlen * 2);
+				if (ntext == nil)
+					panic("acpi: no memory\n");
+				s = ntext + (ttext - s);
+				ttext = ntext;
+				tlen *= 2;
+				e = ttext + tlen;
+				ns = seprinttable(s, e, t);
+			}
+			s = ns;
+		}
+		return readstr(off, a, n, ttext);
+	case Qpretty:
+		s = ttext;
+		e = ttext + tlen;
+		s = dumpfadt(s, e, fadt);
+		s = dumpmadt(s, e, apics);
+		s = dumpslit(s, e, slit);
+		s = dumpsrat(s, e, srat);
+		s = dumpdmar(s, e, dmar);
+		dumpmsct(s, e, mscttbl);
+		return readstr(off, a, n, ttext);
 	default:
-		panic("%s, bad width %d", __func__, Width);
-		break;
+		error("acpiread: bad path");
 	}
-	print("%s 0x%x 0x%x\n", __func__, Address, Value);
-	return AE_OK;
-}
+	error("Permission denied");
 
-/*
- * Platform and hardware-independent physical memory interfaces
- */
-ACPI_STATUS
-AcpiOsReadMemory (
-    ACPI_PHYSICAL_ADDRESS   Address,
-    UINT64                  *Value,
-    UINT32                  Width)
-{
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
+	return -1;
 }
 
-ACPI_STATUS
-AcpiOsWriteMemory (
-    ACPI_PHYSICAL_ADDRESS   Address,
-    UINT64                  Value,
-    UINT32                  Width)
+static int32_t acpiwrite(Chan *c, void *a, int32_t n, int64_t off)
 {
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
-}
+	error("acpiwrite: not until we can figure out what it's for");
+	return -1;
+#if 0
+	ERRSTACK(2);
+	cmdtab *ct;
+	cmdbuf *cb;
+	Reg *r;
+	unsigned int rno, fun, dev, bus, i;
+
+	if (c->qid.path == Qio) {
+		if (reg == nil)
+			error("region not configured");
+		return regio(reg, a, n, off, 1);
+	}
+	if (c->qid.path != Qctl)
+		error(EPERM, ERROR_FIXME);
 
-/*
- * ACPI Table interfaces
- */
-ACPI_PHYSICAL_ADDRESS
-AcpiOsGetRootPointer (
-    void)
-{
-	print("%s returns %p\n", __func__, rsd);
-	return (ACPI_PHYSICAL_ADDRESS) PADDR(rsd);
+	cb = parsecmd(a, n);
+	if (waserror()) {
+		free(cb);
+		nexterror();
+	}
+	ct = lookupcmd(cb, ctls, nelem(ctls));
+	switch (ct->index) {
+		case CMregion:
+			/* TODO: this block is racy on reg (global) */
+			r = reg;
+			if (r == nil) {
+				r = mallocz(sizeof(Reg), 1);
+				r->name = nil;
+			}
+			kstrdup(&r->name, cb->f[1]);
+			r->spc = acpiregid(cb->f[2]);
+			if (r->spc < 0) {
+				free(r);
+				reg = nil;
+				error("bad region type");
+			}
+			if (r->spc == Rpcicfg || r->spc == Rpcibar) {
+				rno = r->base >> Rpciregshift & Rpciregmask;
+				fun = r->base >> Rpcifunshift & Rpcifunmask;
+				dev = r->base >> Rpcidevshift & Rpcidevmask;
+				bus = r->base >> Rpcibusshift & Rpcibusmask;
+				r->tbdf = MKBUS(BusPCI, bus, dev, fun);
+				r->base = rno;	/* register ~ our base addr */
+			}
+			r->base = strtoul(cb->f[3], nil, 0);
+			r->len = strtoul(cb->f[4], nil, 0);
+			r->accsz = strtoul(cb->f[5], nil, 0);
+			if (r->accsz < 1 || r->accsz > 4) {
+				free(r);
+				reg = nil;
+				error("bad region access size");
+			}
+			reg = r;
+			print("region %s %s %p %p sz%d",
+				   r->name, acpiregstr(r->spc), r->base, r->len, r->accsz);
+			break;
+		case CMgpe:
+			i = strtoul(cb->f[1], nil, 0);
+			if (i >= ngpes)
+				error(ERANGE, "gpe out of range");
+			kstrdup(&gpes[i].obj, cb->f[2]);
+			setgpeen(i, 1);
+			break;
+		default:
+			panic("acpi: unknown ctl");
+	}
+	poperror();
+	free(cb);
+	return n;
+#endif
 }
 
-ACPI_STATUS
-AcpiOsPredefinedOverride (
-    const ACPI_PREDEFINED_NAMES *InitVal,
-    ACPI_STRING                 *NewVal)
-{
-	print("%s\n", __func__);
-	*NewVal = nil;
-	return AE_OK;
-}
+struct {
+	char *(*pretty)(Atable *atbl, char *start, char *end, void *arg);
+} acpisw[NACPITBLS] = {
+};
 
-ACPI_STATUS
-AcpiOsTableOverride (
-    ACPI_TABLE_HEADER       *ExistingTable,
-    ACPI_TABLE_HEADER       **NewTable)
+static char *pretty(Atable *atbl, char *start, char *end, void *arg)
 {
-	print("%s\n", __func__);
-	*NewTable = nil;
-	return AE_OK;
+	int type;
+
+	type = atbl->type;
+	if (type < 0 || NACPITBLS < type)
+		return start;
+	if (acpisw[type].pretty == nil)
+		return seprint(start, end, "\"\"\n");
+	return acpisw[type].pretty(atbl, start, end, arg);
 }
 
-ACPI_STATUS
-AcpiOsPhysicalTableOverride (
-    ACPI_TABLE_HEADER       *ExistingTable,
-    ACPI_PHYSICAL_ADDRESS   *NewAddress,
-    UINT32                  *NewTableLength)
+static char *raw(Atable *atbl, char *start, char *end, void *unused_arg)
 {
-	print("%s\n", __func__);
-	*NewAddress = (ACPI_PHYSICAL_ADDRESS)nil;
-	return AE_OK;
-}
+	size_t len = MIN(end - start, atbl->rawsize);
 
-/*
- * Debug input
- */
-ACPI_STATUS
-AcpiOsGetLine (
-    char                    *Buffer,
-    UINT32                  BufferLength,
-    UINT32                  *BytesRead)
-{
-	print("%s\n", __func__);
-	panic("%s", __func__);
-	return AE_OK;
+	memmove(start, atbl->raw, len);
+
+	return start + len;
 }
 
+Dev acpidevtab = {
+	.dc = L'α',
+	.name = "acpi",
+
+	.reset = devreset,
+	.init = devinit,
+	.shutdown = devshutdown,
+	.attach = acpiattach,
+	.walk = acpiwalk,
+	.stat = acpistat,
+	.open = acpiopen,
+	.create = devcreate,
+	.close = acpiclose,
+	.read = acpiread,
+	.bread = devbread,
+	.write = acpiwrite,
+	.bwrite = devbwrite,
+	.remove = devremove,
+	.wstat = devwstat,
+};

+ 4 - 2
sys/src/9/amd64/fns.h

@@ -11,6 +11,7 @@
 void	intrac(Proc*);
 void	acinit(void);
 int	acpiinit(void);
+int	mpacpi(int);
 void	actrapenable(int, char* (*)(Ureg*, void*), void*, char*);
 void	apicipi(int);
 void	apicpri(int);
@@ -239,7 +240,7 @@ extern void apicpri(int);
 extern void apicsipi(int, uintmem);
 extern void apicnmi(int, int, int);
 
-extern void ioapicinit(int, uintmem);
+extern void ioapicinit(int, int, uintmem);
 extern void ioapicintrinit(int, int, int, int, uint32_t);
 extern void ioapiconline(void);
 
@@ -260,7 +261,8 @@ extern int i8259isr(int);
 /*
  * mp.c
  */
-extern void mpsinit(int);
+extern int mpsinit(int);
+void*sigscan(uint8_t* address, int length, char* signature);
 
 /*
  * sipi.c

+ 303 - 5
sys/src/9/amd64/ioapic.c

@@ -15,10 +15,19 @@
 
 #include "apic.h"
 #include "io.h"
+#include "acpi.h"
 
 typedef struct Rbus Rbus;
 typedef struct Rdt Rdt;
 
+/* this cross-dependency from acpi to ioapic is from akaros, and
+ * kind of breaks the clean model we had before, where table
+ * parsing and hardware were completely separate. We'll try to
+ * clean it up later.
+ */
+extern Atable *apics; 		/* APIC info */
+extern int mpisabusno;
+
 struct Rbus {
 	Rbus	*next;
 	int	devno;
@@ -29,6 +38,7 @@ struct Rdt {
 	Apic	*apic;
 	int	intin;
 	uint32_t	lo;
+	uint32_t	hi;
 
 	int	ref;				/* could map to multiple busses */
 	int	enabled;				/* times enabled */
@@ -58,6 +68,14 @@ static int idtno = IdtIOAPIC;
 
 Apic	xioapic[Napic];
 
+static int map_polarity[4] = {
+	-1, IPhigh, -1, IPlow
+};
+
+static int map_edge_level[4] = {
+	-1, TMedge, -1, TMlevel
+};
+
 static uint32_t ioapicread(Apic*apic, int reg)
 {
 	volatile uint32_t *sel = apic->Ioapic.addr+Ioregsel;
@@ -112,11 +130,28 @@ ioapicintrinit(int busno, int apicno, int intin, int devno, uint32_t lo)
 	Rdt *rdt;
 	Apic *apic;
 
-	if(busno >= Nbus || apicno >= Napic || nrdtarray >= Nrdt)
+	if(busno >= Nbus){
+		print("ioapicintrinit: botch: Busno %d >= Nbus %d\n", busno, Nbus);
 		return;
+	}
+	if (apicno >= Napic) {
+		print("ioapicintrinit: botch: acpicno %d >= Napic %d\n", apicno, Napic);
+		return;
+	}
+	if (nrdtarray >= Nrdt){
+		print("ioapicintrinit: botch: nrdtarray %d >= Nrdt %d\n", nrdtarray, Nrdt);
+		return;
+	}
+
 	apic = &xioapic[apicno];
-	if(!apic->useable || intin >= apic->Ioapic.nrdt)
+	if(!apic->useable) {
+		print("ioapicintrinit: botch: apic %d not marked usable\n", apicno);
 		return;
+	}
+	if (intin >= apic->Ioapic.nrdt){
+		print("ioapicintrinit: botch: initin %d >= apic->Ioapic.nrdt %d\n", intin, apic->Ioapic.nrdt);
+		return;
+	}
 
 	rdt = rdtlookup(apic, intin);
 	if(rdt == nil){
@@ -140,8 +175,97 @@ ioapicintrinit(int busno, int apicno, int intin, int devno, uint32_t lo)
 	rdtbus[busno] = rbus;
 }
 
+static int acpi_irq2ioapic(int irq)
+{
+	int ioapic_idx = 0;
+	Apic *apic;
+	/* with acpi, the ioapics map a global interrupt space.  each covers a
+	 * window of the space from [ibase, ibase + nrdt). */
+	for (apic = xioapic; apic < &xioapic[Napic]; apic++, ioapic_idx++) {
+		/* addr check is just for sanity */
+		if (!apic->useable || !apic->Ioapic.addr)
+			continue;
+		if ((apic->Ioapic.gsib <= irq) && (irq < apic->Ioapic.gsib + apic->Ioapic.nrdt))
+			return ioapic_idx;
+	}
+	return -1;
+}
+
+/* Build an RDT route, like we would have had from the MP tables had they been
+ * parsed, via ACPI.
+ *
+ * This only really deals with the ISA IRQs and maybe PCI ones that happen to
+ * have an override.  FWIW, on qemu the PCI NIC shows up as an ACPI intovr.
+ *
+ * From Brendan http://f.osdev.org/viewtopic.php?f=1&t=25951:
+ *
+ * 		Before parsing the MADT you should begin by assuming that redirection
+ * 		entries 0 to 15 are used for ISA IRQs 0 to 15. The MADT's "Interrupt
+ * 		Source Override Structures" will tell you when this initial/default
+ * 		assumption is wrong. For example, the MADT might tell you that ISA IRQ 9
+ * 		is connected to IO APIC 44 and is level triggered; and (in this case)
+ * 		it'd be silly to assume that ISA IRQ 9 is also connected to IO APIC
+ * 		input 9 just because IO APIC input 9 is not listed.
+ *
+ *		For PCI IRQs, the MADT tells you nothing and you can't assume anything
+ *		at all. Sadly, you have to interpret the ACPI AML to determine how PCI
+ *		IRQs are connected to IO APIC inputs (or find some other work-around;
+ *		like implementing a motherboard driver for each different motherboard,
+ *		or some complex auto-detection scheme, or just configure PCI devices to
+ *		use MSI instead). */
+static int acpi_make_rdt(int tbdf, int irq, int busno, int devno)
+{
+	Atable *at;
+	Apicst *st, *lst;
+	uint32_t lo;
+	int pol, edge_level, ioapic_nr, gsi_irq;
+
+	at = apics;
+	st = nil;
+	for (int i = 0; i < at->nchildren; i++) {
+		lst = at->children[i]->tbl;
+		if (lst->type == ASintovr) {
+			if (lst->intovr.irq == irq) {
+				st = lst;
+				break;
+			}
+		}
+	}
+	if (st) {
+		pol = map_polarity[st->intovr.flags & AFpmask];
+		if (pol < 0) {
+			print("ACPI override had bad polarity\n");
+			return -1;
+		}
+		edge_level = map_edge_level[(st->intovr.flags & AFlevel) >> 2];
+		if (edge_level < 0) {
+			print("ACPI override had bad edge/level\n");
+			return -1;
+		}
+		lo = pol | edge_level;
+		gsi_irq = st->intovr.intr;
+	} else {
+		if (BUSTYPE(tbdf) == BusISA) {
+			lo = IPhigh | TMedge;
+			gsi_irq = irq;
+		} else {
+			/* Need to query ACPI at some point to handle this */
+			print("Non-ISA IRQ %d not found in MADT, aborting\n", irq);
+			return -1;
+		}
+	}
+	ioapic_nr = acpi_irq2ioapic(gsi_irq);
+	if (ioapic_nr < 0) {
+		print("Could not find an IOAPIC for global irq %d!\n", gsi_irq);
+		return -1;
+	}
+	ioapicintrinit(busno, ioapic_nr, gsi_irq - xioapic[ioapic_nr].Ioapic.gsib,
+	               devno, lo);
+	return 0;
+}
+
 void
-ioapicinit(int id, uintptr_t pa)
+ioapicinit(int id, int ibase, uintptr_t pa)
 {
 	Apic *apic;
 
@@ -150,12 +274,14 @@ ioapicinit(int id, uintptr_t pa)
 	 * and the registers can be mapped.
 	 */
 	if(id >= Napic)
+
 		return;
 
 	apic = &xioapic[id];
 	if(apic->useable || (apic->Ioapic.addr = vmap(pa, 1024)) == nil)
 		return;
 	apic->useable = 1;
+	apic->Ioapic.paddr = pa;
 
 	/*
 	 * Initialise the I/O APIC.
@@ -164,8 +290,12 @@ ioapicinit(int id, uintptr_t pa)
 	 */
 	lock(&apic->Ioapic.l);
 	apic->Ioapic.nrdt = ((ioapicread(apic, Ioapicver)>>16) & 0xff) + 1;
-	apic->Ioapic.gsib = gsib;
-	gsib += apic->Ioapic.nrdt;
+	if (ibase == -1) {
+		apic->Ioapic.gsib = gsib;
+		gsib += apic->Ioapic.nrdt;
+	} else {
+		apic->Ioapic.gsib = ibase;
+	}
 
 	ioapicwrite(apic, Ioapicid, id<<24);
 	unlock(&apic->Ioapic.l);
@@ -502,3 +632,171 @@ ioapicintrdisable(int vecno)
 
 	return 0;
 }
+
+/* From Akaros, not sure we want this but for now ... */
+static int ioapic_exists(void)
+{
+	/* not foolproof, if we called this before parsing */
+	for (int i = 0; i < Napic; i++)
+		if (xioapic[i].useable)
+			return 1;
+	return 0;
+}
+
+Rdt *rbus_get_rdt(int busno, int devno)
+{
+	Rbus *rbus;
+	for (rbus = rdtbus[busno]; rbus != nil; rbus = rbus->next) {
+		if (rbus->devno == devno)
+			return rbus->rdt;
+	}
+	return 0;
+}
+
+/* Attempts to init a bus interrupt, initializes Vctl, and returns the IDT
+ * vector to use (-1 on error).  If routable, the IRQ will route to core 0.  The
+ * IRQ will be masked, if possible.  Call Vctl->unmask() when you're ready.
+ *
+ * This will determine the type of bus the device is on (LAPIC, IOAPIC, PIC,
+ * etc), and set the appropriate fields in isr_h.  If applicable, it'll also
+ * allocate an IDT vector, such as for an IOAPIC, and route the IOAPIC entries
+ * appropriately.
+ *
+ * Callers init Vctl->dev_irq and ->tbdf.  tbdf encodes the bus type and the
+ * classic PCI bus:dev:func.  dev_irq may be ignored based on the bus type (e.g.
+ * PCI, esp MSI).
+ *
+ * In plan9, this was ioapicintrenable(), which also unmasked.  We don't have a
+ * deinit/disable method that would tear down the route yet.  All the plan9 one
+ * did was dec enabled and mask the entry. */
+int bus_irq_setup(Vctl *v)
+{
+	//Rbus *rbus;
+	Rdt *rdt;
+	int busno = -1, devno = -1, vno;
+	Pcidev *p;
+
+       	if (!ioapic_exists()) {
+		panic("%s: no ioapics?", __func__);
+		switch (BUSTYPE(v->Vkey.tbdf)) {
+			//case BusLAPIC:
+			//case BusIPI:
+			//break;
+		default:
+			//irq_h->check_spurious = pic_check_spurious;
+			//v->eoi = pic_send_eoi;
+			//irq_h->mask = pic_mask_irq;
+			//irq_h->unmask = pic_unmask_irq;
+			//irq_h->route_irq = 0;
+			//irq_h->type = "pic";
+			/* PIC devices have vector = irq + 32 */
+			return -1; //irq_h->dev_irq + IdtPIC;
+		}
+	}
+	switch (BUSTYPE(v->Vkey.tbdf)) {
+#if 0
+	case BusLAPIC:
+		/* nxm used to set the initial 'isr' method (i think equiv to our
+		 * check_spurious) to apiceoi for non-spurious lapic vectors.  in
+		 * effect, i think they were sending the EOI early, and their eoi
+		 * method was 0.  we're not doing that (unless we have to). */
+		irq_h->check_spurious = lapic_check_spurious;
+		irq_h->eoi = lapic_send_eoi;
+		irq_h->mask = lapic_mask_irq;
+		irq_h->unmask = lapic_unmask_irq;
+		irq_h->route_irq = 0;
+		irq_h->type = "lapic";
+		/* For the LAPIC, irq == vector */
+		return irq_h->dev_irq;
+	case BusIPI:
+		/* similar to LAPIC, but we don't actually have LVT entries */
+		irq_h->check_spurious = lapic_check_spurious;
+		irq_h->eoi = lapic_send_eoi;
+		irq_h->mask = 0;
+		irq_h->unmask = 0;
+		irq_h->route_irq = 0;
+		irq_h->type = "IPI";
+		return irq_h->dev_irq;
+		case BusISA:
+			if (mpisabusno == -1)
+				panic("No ISA bus allocated");
+			busno = mpisabusno;
+			/* need to track the irq in devno in PCI interrupt assignment entry
+			 * format (see mp.c or MP spec D.3). */
+			devno = v->Vkey.irq << 2;
+			break;
+#endif
+	case BusPCI:
+		p = pcimatchtbdf(v->Vkey.tbdf);
+		if (!p) {
+			print("No PCI dev for tbdf %p!", v->Vkey.tbdf);
+			return -1;
+		}
+		if ((vno = intrenablemsi(v, p))!= -1)
+			return vno;
+		busno = BUSBNO(v->Vkey.tbdf);
+		devno = pcicfgr8(p, PciINTP);
+
+		/* this might not be a big deal - some PCI devices have no INTP.  if
+		 * so, change our devno - 1 below. */
+		if (devno == 0)
+			panic("no INTP for tbdf %p", v->Vkey.tbdf);
+		/* remember, devno is the device shifted with irq pin in bits 0-1.
+		 * we subtract 1, since the PCI intp maps 1 -> INTA, 2 -> INTB, etc,
+		 * and the MP spec uses 0 -> INTA, 1 -> INTB, etc. */
+		devno = BUSDNO(v->Vkey.tbdf) << 2 | (devno - 1);
+		break;
+	default:
+		panic("Unknown bus type, TBDF %p", v->Vkey.tbdf);
+	}
+	/* busno and devno are set, regardless of the bustype, enough to find rdt.
+	 * these may differ from the values in tbdf. */
+	rdt = rbus_get_rdt(busno, devno);
+	if (!rdt) {
+		/* second chance.  if we didn't find the item the first time, then (if
+		 * it exists at all), it wasn't in the MP tables (or we had no tables).
+		 * So maybe we can figure it out via ACPI. */
+		acpi_make_rdt(v->Vkey.tbdf, v->Vkey.irq, busno, devno);
+		rdt = rbus_get_rdt(busno, devno);
+	}
+	if (!rdt) {
+		print("Unable to build IOAPIC route for irq %d\n", v->Vkey.irq);
+		return -1;
+	}
+	/*
+	 * what to do about devices that intrenable/intrdisable frequently?
+	 * 1) there is no ioapicdisable yet;
+	 * 2) it would be good to reuse freed vectors.
+	 * Oh bugger.
+	 * brho: plus the diff btw mask/unmask and enable/disable is unclear
+	 */
+	/*
+	 * This is a low-frequency event so just lock
+	 * the whole IOAPIC to initialise the RDT entry
+	 * rather than putting a Lock in each entry.
+	 */
+	lock(&rdt->apic->Ioapic.l);
+	/* if a destination has already been picked, we store it in the lo.  this
+	 * stays around regardless of enabled/disabled, since we don't reap vectors
+	 * yet.  nor do we really mess with enabled... */
+	if ((rdt->lo & 0xff) == 0) {
+		vno = nextvec();
+		rdt->lo |= vno;
+		rdtvecno[vno] = rdt;
+	} else {
+		print("%p: mutiple irq bus %d dev %d\n", v->Vkey.tbdf, busno, devno);
+	}
+	rdt->enabled++;
+	rdt->hi = 0;			/* route to 0 by default */
+	rdt->lo |= Pm | MTf;
+	rtblput(rdt->apic, rdt->intin, rdt->hi, rdt->lo);
+	vno = rdt->lo & 0xff;
+	unlock(&rdt->apic->Ioapic.l);
+
+	v->type = "ioapic";
+
+	v->eoi = apiceoi;
+	v->vno = vno;
+	v->mask = msimask;
+	return vno;
+}

+ 13 - 20
sys/src/9/amd64/main.c

@@ -55,7 +55,7 @@ char dbgflg[256];
 static int vflag = 1;
 
 int nosmp = 1;
-int enableacpi = 0;
+int acpionly = 0;
 
 /*
  *	this may need improvement, but right now it's just for
@@ -149,7 +149,7 @@ options(int argc, char* argv[])
 	vflag = dbgflg['v'];
 	// hack.
 	nosmp = dbgflg['n'];
-	enableacpi = dbgflg['a'];
+	acpionly = dbgflg['z'];
 }
 
 void
@@ -420,8 +420,7 @@ void badcall(uint64_t where, uint64_t what)
 */
 
 void errstr(char *s, int i) {
-	print("errstr has :%s:, %d: what to do?\n", s, i);
-	//panic("errstr");
+	panic("errstr");
 }
 
 static int x = 0x123456;
@@ -569,11 +568,13 @@ main(uint32_t mbmagic, uint32_t mbaddress)
 	 * not deep in some call chain.
 	 * See next note.
 	 *
-	void *v = malloc(1234);
-	hi("v "); put64((uint64_t)v); hi("\n");
-	free(v);
-	hi("free ok\n");
 	 */
+	if (1) {
+		void *v = malloc(1234);
+		hi("allocated\n ");
+		free(v);
+		hi("free ok\n");
+	}
 
 	/*
 	 * Acpiinit will cause the first malloc
@@ -584,11 +585,7 @@ main(uint32_t mbmagic, uint32_t mbaddress)
 	 * (it's amazing how far you can get with
 	 * things like that completely broken).
 	 */
-	if (enableacpi){
-		/* If acpiinit succeeds, we leave enableacpi enabled.
-		 * This means we can always boot. */
-		enableacpi = acpiinit();
-	}
+if (1){	acpiinit(); hi("	acpiinit();\n");}
 
 	umeminit();
 	trapinit();
@@ -603,14 +600,10 @@ main(uint32_t mbmagic, uint32_t mbaddress)
 
 
 	procinit0();
-	if (! enableacpi)
-		mpsinit(maxcores);
-	print("CODE: apiconline();\n");
+	if (! acpionly)
+		maxcores = mpsinit(maxcores);
+	mpacpi(maxcores);
 	apiconline();
-	print("CODE: if(! nosmp) sipi();\n");
-	if (enableacpi){
-		die("ACPI after apiconline\n");
-	}
 	/* Forcing to single core if desired */
 	if(!nosmp) {
 		sipi();

+ 32 - 56
sys/src/9/amd64/mp.c

@@ -15,7 +15,6 @@
 
 #include "apic.h"
 
-#define ISABUSNO 0xff
 /*
  * MultiProcessor Specification Version 1.[14].
  */
@@ -56,14 +55,13 @@ static Mpbus mpbusdef[] = {
 	{ "ISA   ", IPhigh, TMedge, },
 };
 static Mpbus* mpbus[Nbus];
-static int hackisabusno = -1;
 int mpisabusno = -1;
 
 static void
 mpintrprint(char* s, uint8_t* p)
 {
 	char buf[128], *b, *e;
-	char format[] = " type %d flags %#x bus %d IRQ %d APIC %d INTIN %d\n";
+	char format[] = " type %d flags %#ux bus %d IRQ %d APIC %d INTIN %d\n";
 
 	b = buf;
 	e = b + sizeof(buf);
@@ -90,7 +88,6 @@ mpmkintr(uint8_t* p)
 	 * to imagine routing a signal to all IOAPICs, the
 	 * usual case is routing NMI and ExtINT to all LAPICs.
 	 */
-	if (p[4] == hackisabusno) p[4] = mpisabusno;
 	if(mpbus[p[4]] == nil){
 		mpintrprint("no source bus", p);
 		return 0;
@@ -185,7 +182,7 @@ mpmkintr(uint8_t* p)
 	return v;
 }
 
-static void
+static int
 mpparse(PCMP* pcmp, int maxcores)
 {
 	uint32_t lo;
@@ -200,13 +197,12 @@ mpparse(PCMP* pcmp, int maxcores)
 		for(i = 0; p < e; i++){
 			if(i && ((i & 0x0f) == 0))
 				print("\n");
-			print(" %#2.2x", *p);
+			print(" %#2.2ux", *p);
 			p++;
 		}
 		print("\n");
 		break;
 	case 0:					/* processor */
-		print("CODE: /* case 0 */\n");
 		/*
 		 * Initialise the APIC if it is enabled (p[3] & 0x01).
 		 * p[1] is the APIC ID, the memory mapped address comes
@@ -214,21 +210,15 @@ mpparse(PCMP* pcmp, int maxcores)
 		 * CPU and identical for all. Indicate whether this is
 		 * the bootstrap processor (p[3] & 0x02).
 		 */
-		DBG("mpparse: cpu %d pa %#x bp %d\n",
+		DBG("mpparse: cpu %d pa %#ux bp %d\n",
 			p[1], l32get(pcmp->apicpa), p[3] & 0x02);
-		if((p[3] & 0x01) != 0 && maxcores-- > 0) {
-			print("CODE: apicinit(%d, %p, %d); \n", p[1], (void *)(uint64_t)l32get(pcmp->apicpa), p[3]&2);
+		if((p[3] & 0x01) != 0 && maxcores-- > 0)
 			apicinit(p[1], l32get(pcmp->apicpa), p[3] & 0x02);
-		}
-print("MP: add an apic, # %d\n", p[1]);
+		maxcores--;
 		p += 20;
 		break;
 	case 1:					/* bus */
-		print("CODE: /* case 1, bus */\n");
-		if (p[1] == hackisabusno)
-				p[1] = ISABUSNO;
 		DBG("mpparse: bus: %d type %6.6s\n", p[1], (char*)p+2);
-print("MP: adda  bus %d\n", p[1]);
 		if(mpbus[p[1]] != nil){
 			print("mpparse: bus %d already allocated\n", p[1]);
 			p += 8;
@@ -243,10 +233,7 @@ print("MP: adda  bus %d\n", p[1]);
 						p[1], mpisabusno);
 					continue;
 				}
-				hackisabusno = p[1];
-				p[1] = ISABUSNO;
 				mpisabusno = p[1];
-print("CODE: mpisabusno = %d\n", p[1]);
 			}
 			mpbus[p[1]] = &mpbusdef[i];
 			break;
@@ -258,21 +245,16 @@ print("CODE: mpisabusno = %d\n", p[1]);
 		p += 8;
 		break;
 	case 2:					/* IOAPIC */
-		print("CODE: /* case 2, IOACPI */\n");
 		/*
 		 * Initialise the IOAPIC if it is enabled (p[3] & 0x01).
 		 * p[1] is the APIC ID, p[4-7] is the memory mapped address.
 		 */
-print("MP: add an IOAPIC %d\n", p[1]);
-		if(p[3] & 0x01) {
-print("CODE: ioapicinit(%d, %p);\n", p[i], l32get(p+4));
-			ioapicinit(p[1], l32get(p+4));
-		}
+		if(p[3] & 0x01)
+			ioapicinit(p[1], -1, l32get(p+4));
 
 		p += 8;
 		break;
 	case 3:					/* IOINTR */
-		print("CODE: /* case 3, IOINTR */\n");
 		/*
 		 * p[1] is the interrupt type;
 		 * p[2-3] contains the polarity and trigger mode;
@@ -303,14 +285,11 @@ print("CODE: ioapicinit(%d, %p);\n", p[i], l32get(p+4));
 		devno = p[5];
 		if(memcmp(mpbus[p[4]]->type, "PCI   ", 6) != 0)
 			devno <<= 2;
-print("CODE: ioapicintrinit(0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", p[4], p[6], p[7], devno, lo);
-		if (p[4] == hackisabusno) p[4] = mpisabusno;
 		ioapicintrinit(p[4], p[6], p[7], devno, lo);
 
 		p += 8;
 		break;
 	case 4:					/* LINTR */
-		print("CODE: /* case 3, LINTR */\n");
 		/*
 		 * Format is the same as IOINTR above.
 		 */
@@ -329,14 +308,10 @@ print("CODE: ioapicintrinit(0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", p[4], p[6], p[7], d
 				if(!xlapic[i].useable || xlapic[i].Ioapic.addr != nil)
 					continue;
 				xlapic[i].Lapic.lvt[p[7]] = lo;
-print("CODE: xlapic[0x%x].Lapic.lvt[0x%x] = 0x%x\n", i, p[7], lo);
-print("MP: add LINTR %d\n", i);
 			}
 		}
-		else {
+		else
 			xlapic[p[6]].Lapic.lvt[p[7]] = lo;
-print("CODE: xlapic[0x%x].Lapic.lvt[0x%x] = 0x%x\n", i, p[7], lo);
-		}
 		p += 8;
 		break;
 	}
@@ -354,14 +329,14 @@ print("CODE: xlapic[0x%x].Lapic.lvt[0x%x] = 0x%x\n", i, p[7], lo);
 		for(i = 0; i < n; i++){
 			if(i && ((i & 0x0f) == 0))
 				print("\n");
-			print(" %#2.2x", *p);
+			print(" %#2.2ux", *p);
 			p++;
 		}
 		print("\n");
 		break;
 	case 128:
 		DBG("address space mapping\n");
-		DBG(" bus %d type %d base %#llx length %#llx\n",
+		DBG(" bus %d type %d base %#llux length %#llux\n",
 			p[2], p[3], l64get(p+4), l64get(p+12));
 		p += p[1];
 		break;
@@ -378,6 +353,7 @@ print("CODE: xlapic[0x%x].Lapic.lvt[0x%x] = 0x%x\n", i, p[7], lo);
 		p += p[1];
 		break;
 	}
+	return maxcores;
 }
 
 static int
@@ -392,7 +368,7 @@ sigchecksum(void* address, int length)
 	return sum;
 }
 
-static void*
+void*
 sigscan(uint8_t* address, int length, char* signature)
 {
 	uint8_t *e, *p;
@@ -437,7 +413,7 @@ sigsearch(char* signature)
 	return sigscan(BIOSSEG(0xe000), 0x20000, signature);
 }
 
-void
+int
 mpsinit(int maxcores)
 {
 	uint8_t *p;
@@ -445,54 +421,53 @@ mpsinit(int maxcores)
 	_MP_ *mp;
 	PCMP *pcmp;
 
-	if((mp = sigsearch("_MP_")) == nil) {
-		panic("NO _MP_ table");
-	}
+	if((mp = sigsearch("_MP_")) == nil)
+		return maxcores;
 	if(DBGFLG){
-		DBG("_MP_ @ %#p, addr %#x length %u rev %d",
+		DBG("_MP_ @ %#p, addr %#ux length %ud rev %d",
 			mp, l32get(mp->addr), mp->length, mp->revision);
 		for(i = 0; i < sizeof(mp->feature); i++)
-			DBG(" %2.2#x", mp->feature[i]);
+			DBG(" %2.2#ux", mp->feature[i]);
 		DBG("\n");
 	}
 	if(mp->revision != 1 && mp->revision != 4)
-		return;
+		return maxcores;
 	if(sigchecksum(mp, mp->length*16) != 0)
-		return;
+		return maxcores;
 
 	if((pcmp = vmap(l32get(mp->addr), sizeof(PCMP))) == nil)
-		return;
+		return maxcores;
 	if(pcmp->revision != 1 && pcmp->revision != 4){
 		vunmap(pcmp, sizeof(PCMP));
-		return;
+		return maxcores;
 	}
 	n = l16get(pcmp->length) + l16get(pcmp->xlength);
 	vunmap(pcmp, sizeof(PCMP));
 	if((pcmp = vmap(l32get(mp->addr), n)) == nil)
-		return;
+		return maxcores;
 	if(sigchecksum(pcmp, l16get(pcmp->length)) != 0){
 		vunmap(pcmp, n);
-		return;
+		return maxcores;
 	}
 	if(DBGFLG){
-		DBG("PCMP @ %#p length %#x revision %d\n",
+		DBG("PCMP @ %#p length %#ux revision %d\n",
 			pcmp, l16get(pcmp->length), pcmp->revision);
-		DBG(" %20.20s oaddr %#x olength %#x\n",
+		DBG(" %20.20s oaddr %#ux olength %#ux\n",
 			(char*)pcmp->string, l32get(pcmp->oaddr),
 			l16get(pcmp->olength));
-		DBG(" entry %d apicpa %#x\n",
+		DBG(" entry %d apicpa %#ux\n",
 			l16get(pcmp->entry), l32get(pcmp->apicpa));
 
-		DBG(" xlength %#x xchecksum %#x\n",
+		DBG(" xlength %#ux xchecksum %#ux\n",
     			l16get(pcmp->xlength), pcmp->xchecksum);
 	}
 	if(pcmp->xchecksum != 0){
 		p = ((uint8_t*)pcmp) + l16get(pcmp->length);
 		i = sigchecksum(p, l16get(pcmp->xlength));
 		if(((i+pcmp->xchecksum) & 0xff) != 0){
-			print("extended table checksums to %#x\n", i);
+			print("extended table checksums to %#ux\n", i);
 			vunmap(pcmp, n);
-			return;
+			return maxcores;
 		}
 	}
 
@@ -501,8 +476,9 @@ mpsinit(int maxcores)
 	 * for later interrupt enabling and application processor
 	 * startup.
 	 */
-	mpparse(pcmp, maxcores);
+	maxcores = mpparse(pcmp, maxcores);
 
 	apicdump();
 	ioapicdump();
+	return maxcores;
 }

+ 88 - 0
sys/src/9/amd64/mpacpi.c

@@ -0,0 +1,88 @@
+/* This file is part of the UCB release of Plan 9. It is subject to the license
+ * terms in the LICENSE file found in the top-level directory of this
+ * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
+ * part of the UCB release of Plan 9, including this file, may be copied,
+ * modified, propagated, or distributed except according to the terms contained
+ * in the LICENSE file. */
+
+#include "u.h"
+#include "../port/lib.h"
+#include "mem.h"
+#include "dat.h"
+#include "fns.h"
+
+#include "apic.h"
+#include "acpi.h"
+
+extern int mpisabusno;
+
+int mpacpi(int ncleft)
+{
+	char *already;
+	int np, bp;
+	Apic *apic;
+	Apicst *st;
+	Madt *mt;
+
+	/* If we don't have an mpisabusno yet, it's because the MP tables failed to
+	 * parse.  So we'll just take the last one available.  I think we're
+	 * supposed to parse the ACPI shit with the AML to figure out the buses and
+	 * find a clear one, but fuck that.  Note this busno is just for our own
+	 * RDT/Rbus bookkeeping. */
+	if (mpisabusno == -1)
+		mpisabusno = Nbus - 1;
+
+	if (apics == nil)
+		return ncleft;
+	mt = apics->tbl;
+	if (mt == nil)
+		return ncleft;
+
+	print("APIC lapic paddr %#.8llux, flags %#.8ux\n",
+		   mt->lapicpa, mt->pcat);
+	np = 0;
+	//print("apics->st %p\n", apics->st);
+	for (int i = 0; i < apics->nchildren; i++) {
+		st = apics->children[i]->tbl;
+		already = "";
+		switch (st->type) {
+			case ASlapic:
+				print("ASlapic %d\n", st->lapic.id);
+				/* this table is supposed to have all of them if it exists */
+				if (st->lapic.id > Napic)
+					break;
+				apic = xlapic + st->lapic.id;
+				bp = (np++ == 0);
+				if (apic->useable) {
+					already = "(mp)";
+				} else if (ncleft != 0) {
+					ncleft--;
+					apicinit(st->lapic.id, mt->lapicpa, bp);
+				} else
+					already = "(off)";
+
+				print("apic proc %d/%d apicid %d %s\n", np - 1, apic->Lapic.machno,
+					   st->lapic.id, already);
+				break;
+			case ASioapic:
+				print("ASioapic %d\n", st->ioapic.id);
+				if (st->ioapic.id > Napic){
+					print("ASioapic: %d is > %d, ignoring\n", st->ioapic.id, Napic);
+					break;
+				}
+				apic = xioapic + st->ioapic.id;
+				if (apic->useable) {
+					already = "(mp)";
+					goto pr1;
+				}
+				ioapicinit(st->ioapic.id, st->ioapic.ibase, st->ioapic.addr);
+pr1:
+				apic->Ioapic.gsib = st->ioapic.ibase;
+				print("ioapic %d ", st->ioapic.id);
+				print("addr %p ibase %d %s\n", st->ioapic.addr, st->ioapic.ibase,
+					   already);
+				break;
+		}
+	}
+	return ncleft;
+}

+ 11 - 0
sys/src/9/port/devcons.c

@@ -419,6 +419,17 @@ readnum(uint32_t off, char *buf, uint32_t n, uint32_t val, int size)
 	return n;
 }
 
+int32_t
+readmem(int32_t offset, void *buf, int32_t n, void *v, int32_t size)
+{
+	if(offset >= size)
+		return 0;
+	if(offset+n > size)
+		n = size-offset;
+	memmove(buf, v+offset, n);
+	return n;
+}
+
 int32_t
 readstr(int32_t offset, char *buf, int32_t n, char *str)
 {

+ 21 - 0
sys/src/9/port/lib.h

@@ -7,6 +7,7 @@
  * in the LICENSE file.
  */
 
+/* TODO: it really ought to be possible to include <libc.h>, not "../port/lib.h". */
 /*
  * functions (possibly) linked in, complete, from libc.
  */
@@ -32,6 +33,7 @@ extern	int	strcmp(char*, char*);
 extern	char*	strcpy(char*, char*);
 extern	char*	strecpy(char*, char*, char*);
 extern	char*	strncat(char*, char*, int32_t);
+extern	char*	strlcpy(char*, char*, int32_t);
 extern	char*	strncpy(char*, char*, int32_t);
 extern	int	strncmp(char*, char*, int32_t);
 extern	char*	strrchr(char*, int);
@@ -73,6 +75,8 @@ extern	void	setrealloctag(void*, uint32_t);
 extern	uint32_t	getmalloctag(void*);
 extern	uint32_t	getrealloctag(void*);
 extern	void*	realloc(void *, uint32_t);
+/* from BSD */
+void* reallocarray(void *base, size_t nel, size_t size);
 
 /*
  * print routines
@@ -315,3 +319,20 @@ void set_printx(int mode);
 #       endif
 #endif
 
+typedef struct PSlice PSlice;
+
+struct PSlice {
+	void **ptrs;
+	size_t len;
+	size_t capacity;
+};
+
+void psliceinit(PSlice *slice);
+void psliceclear(PSlice *slice);
+void *psliceget(PSlice *slice, size_t i);
+int psliceput(PSlice *slice, size_t i, void *p);
+int pslicedel(PSlice *slice, size_t i);
+void psliceappend(PSlice *s, void *p);
+size_t pslicelen(PSlice *slice);
+void **pslicefinalize(PSlice *slice);
+void pslicedestroy(PSlice *slice);

+ 1 - 0
sys/src/9/port/portfns.h

@@ -328,6 +328,7 @@ uint32_t		urandomread(void*, uint32_t);
 void		rdb(void);
 int		readnum(uint32_t, char*, uint32_t, uint32_t, int);
 int32_t		readstr(int32_t, char*, int32_t, char*);
+int32_t		readmem(int32_t, void*, int32_t, void*, int32_t);
 void		ready(Proc*);
 int32_t		readzio(Kzio[], int, void*, int32_t);
 void		reboot(void*, void*, int32_t);

+ 0 - 1
sys/src/klibs.json

@@ -1,7 +1,6 @@
 {
 	"klibs": {
 		"Projects": [
-			"/sys/src/libacpi/acpica/klibacpi.json",
 			"/sys/src/libc/klibc.json",
 			"/sys/src/libip/klibip.json",
 			"/sys/src/libdraw/klibdraw.json",

+ 0 - 35
sys/src/libacpi/BUILD

@@ -1,40 +1,5 @@
 
 
-cc_library(
-    name="klibacpi",
-    includes=[
-        "//sys/include/acpi/acpica",
-        "//sys/src/9",
-        "//sys/include",
-        "//amd64/include",
-    ],
-    srcs=glob(["acpica/components/*/*.c"], exclude=["acpica/components/disassembler"]),
-    copts=[
-        "-DACPI_DEBUGGER",
-        "-DACPI_DEBUG_OUTPUT",
-        "-D__HARVEY__",
-        "-D__KERNEL__",
-        "-U_LINUX",
-        "-U__linux__",
-        "-Wno-unused-function",
-        "-Wno-unused-variable",
-        "-mcmodel=kernel",
-        "-O0",
-        "-mno-red-zone",
-        "-ffreestanding",
-        "-fno-builtin",
-        "-Wall",
-        "-Wno-missing-braces",
-        "-Wno-parentheses",
-        "-Wno-unknown-pragmas",
-        "-Wuninitialized",
-        "-include", "u.h",
-        "-include", "libc.h",
-        "-include", "ctype.h",
-        "-g",
-        ],
-   alwayslink=true,
-)
 cc_library(
     name="libacpi",
     includes=[

+ 0 - 40
sys/src/libacpi/acpica/kacpiflags.json

@@ -1,40 +0,0 @@
-{
-	"kacpiflags": {
-		"Cflags": [
-		        "-DACPI_DEBUGGER",
-		        "-DACPI_DEBUG_OUTPUT",
-		        "-D__HARVEY__",
-		        "-D__KERNEL__",
-		        "-U_LINUX",
-		        "-U__linux__",
-			"-Wno-unused-function",
-			"-Wno-unused-variable",
-			"-I", "/sys/include/acpi/acpica",
-			"-I", "/sys/src/9",
-			"-mcmodel=kernel",
-			"-O0",
-			"-mno-red-zone",
-			"-ffreestanding",
-			"-fno-builtin",
-			"-Wall",
-		        "-Wno-unused-but-set-variable",
-			"-Wno-missing-braces",
-			"-Wno-parentheses",
-			"-Wno-unknown-pragmas",
-			"-Wuninitialized",
-			"-include", "u.h",
-			"-include", "libc.h",
-			"-include", "ctype.h",
-			"-g"
-		],
-		"Install": "/$ARCH/lib/",
-		"Library": "klibacpi.a",
-		"NoCflags": [
-			"-include", "/$ARCH/include/mach_acpi.h",
-			"-Werror"
-		],
-		"IncludeCanNotUsedWUnusedYet": [
-			"../../lib.json"
-		]
-		}
-}

+ 0 - 10
sys/src/libacpi/acpica/klibacpi.json

@@ -1,10 +0,0 @@
-{
-	"Klibacpi": {
-		"Projects": [
-			"components/kbuild.json"
-		],
-		"NInstall": "/$ARCH/lib/",
-		"NLibrary": "klibacpi.a"
-		}
-}
-	

+ 8 - 2
sys/src/libc/port/slice.c

@@ -71,7 +71,10 @@ psliceappend(PSlice *s, void *p)
 			s->capacity = 4;
 		s->capacity *= 2;
 		ps = reallocarray(s->ptrs, s->capacity, sizeof(void *));
-		assert(ps != nil);		/* XXX: if size*sizeof(void*) overflows. */
+		if (ps == nil) {
+			print("realloc at %p failed, cap %d, size %d\n", s->ptrs, s->capacity, sizeof(void *));
+			assert(0);
+		}
 		s->ptrs = ps;
 	}
 	s->ptrs[s->len] = p;
@@ -90,7 +93,10 @@ pslicefinalize(PSlice *slice)
 	void **ps;
 
 	ps = reallocarray(slice->ptrs, slice->len, sizeof(void *));
-	assert(ps != nil);
+	if (ps == nil) {
+		print("realloc at %p failed, cap %d, size %d\n", slice->ptrs, slice->len, sizeof(void *));
+		assert(0);
+	}
 	slice->len = 0;
 	slice->capacity = 0;
 	slice->ptrs = nil;