Browse Source

riscv: add half-done versions of dat.h, fns.h and mem.h

Just get it to build. It's going to be a while before we know what we want.

Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
Ronald G. Minnich 8 years ago
parent
commit
77288d8cce
3 changed files with 848 additions and 0 deletions
  1. 395 0
      sys/src/9/riscv/dat.h
  2. 303 0
      sys/src/9/riscv/fns.h
  3. 150 0
      sys/src/9/riscv/mem.h

+ 395 - 0
sys/src/9/riscv/dat.h

@@ -0,0 +1,395 @@
+/*
+ * 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.
+ */
+
+typedef struct Conf Conf;
+typedef struct Confmem Confmem;
+typedef struct Fxsave Fxsave;
+typedef struct ICC ICC;
+typedef struct ICCparms ICCparms;
+typedef struct Label Label;
+typedef struct Lock Lock;
+typedef struct MCPU MCPU;
+typedef struct MFPU MFPU;
+typedef struct MMMU MMMU;
+typedef struct NIX NIX;
+typedef struct Mach Mach;
+typedef uint64_t Mpl;
+typedef struct Page Page;
+typedef struct PFPU PFPU;
+typedef struct PmcCtr PmcCtr;
+typedef struct PmcCtl PmcCtl;
+typedef struct PmcWait PmcWait;
+typedef struct PMMU PMMU;
+typedef struct PNOTIFY PNOTIFY;
+typedef uint64_t PTE;
+typedef struct Proc Proc;
+typedef struct Sys Sys;
+typedef struct Stackframe Stackframe;
+typedef uint64_t uintmem;				/* Physical address (hideous) */
+typedef struct Ureg Ureg;
+typedef struct Vctl Vctl;
+
+#define MAXSYSARG	6	/* for mount(fd, afd, mpt, flag, arg, mntname) */
+
+/*
+ *  parameters for sysproc.c
+ */
+#define ELF_MAGIC	(ELF_MAG)
+
+/*
+ *  machine dependent definitions used by ../port/portdat.h
+ */
+
+
+struct Lock
+{
+	uint32_t	key;
+	int	isilock;
+	Mpl	pl;
+	uintptr_t	_pc;
+	Proc*	p;
+	Mach*	m;
+	uint64_t	lockcycles;
+};
+
+struct Label
+{
+	uintptr_t	sp;
+	uintptr_t	pc;
+	// TODO: how many?
+	uintptr_t	regs[64];
+};
+
+struct Fxsave {
+	int _;
+};
+
+/*
+ *  FPU stuff in Proc
+ */
+struct PFPU {
+	int	fpustate;
+	unsigned char	fxsave[sizeof(Fxsave)+15];
+	void*	fpusave;
+};
+
+/*
+ *  MMU stuff in Proc
+ */
+#define NCOLOR 1
+struct PMMU
+{
+	Page*	mmuptp[4];		/* page table pages for each level */
+};
+
+/*
+ *  things saved in the Proc structure during a notify
+ */
+struct PNOTIFY
+{
+//	void	emptiness;
+	char	emptiness;
+};
+
+struct Confmem
+{
+	uintptr_t	base;
+	usize	npage;
+	uintptr_t	kbase;
+	uintptr_t	klimit;
+};
+
+struct Conf
+{
+	uint32_t	nproc;		/* processes */
+	Confmem	mem[4];		/* physical memory */
+	uint64_t	npage;		/* total physical pages of memory */
+	usize	upages;		/* user page pool */
+	uint32_t	copymode;	/* 0 is copy on write, 1 is copy on reference */
+	uint32_t	ialloc;		/* max interrupt time allocation in bytes */
+	uint32_t	nimage;		/* number of page cache image headers */
+};
+
+enum
+{
+	NPGSZ = 4	/* # of supported  pages sizes in Mach */
+};
+
+#include "../port/portdat.h"
+
+/*
+ *  CPU stuff in Mach.
+ */
+struct MCPU
+{
+	int _;
+};
+
+/*
+ *  FPU stuff in Mach.
+ */
+struct MFPU
+{
+	int _;
+};
+
+struct NIX
+{
+	ICC*	icc;			/* inter-core call */
+	int	nixtype;
+};
+
+/*
+ *  MMU stuff in Mach.
+ */
+struct MMMU
+{
+	uintptr_t FaultingAddress;
+	Page*	Root;			/* root for this processor */
+	PTE*	pmap;			/* unused as of yet */
+
+	Page	pml4kludge;		/* NIX KLUDGE: we need a page */
+};
+
+/*
+ * Inter core calls
+ */
+enum
+{
+	ICCLNSZ =	128,	/* Cache line size for inter core calls */
+
+
+	ICCOK = 0,		/* Return codes: Ok; trap; syscall */
+	ICCTRAP,
+	ICCSYSCALL
+};
+
+struct ICC
+{
+	/* fn is kept in its own cache line */
+	union{
+		void	(*fn)(void);
+		unsigned char	_ln1_[ICCLNSZ];
+	};
+	int	flushtlb;	/* on the AC, before running fn */
+	int	rc;		/* return code from AC to TC */
+	char*	note;		/* to be posted in the TC after returning */
+	unsigned char	data[ICCLNSZ];	/* sent to the AC */
+};
+
+/*
+ * hw perf counters
+ */
+struct PmcCtl {
+	Ref r;
+	uint32_t _coreno;
+	int enab;
+	int user;
+	int os;
+	int nodesc;
+	char descstr[KNAMELEN];
+	int reset;
+};
+
+struct PmcWait{
+	Ref r;
+	Rendez rend;
+	PmcWait*	next;
+};
+
+struct PmcCtr{
+	int stale;
+	PmcWait *wq;
+	uint64_t ctr;
+	int ctrset;
+	PmcCtl PmcCtl;
+	int ctlset;
+};
+
+enum {
+	PmcMaxCtrs = 4,
+	PmcIgn = 0,
+	PmcGet = 1,
+	PmcSet = 2,
+};
+
+/*
+ * Per processor information.
+ *
+ * The offsets of the first few elements may be known
+ * to low-level assembly code, so do not re-order:
+ *	self	- machp()
+ *	splpc	- splhi, spllo, splx
+ *	proc	- syscallentry
+ *	stack	- acsyscall
+ *	externup - externup()
+ */
+struct Mach
+{
+	/* WARNING! Known to assembly! */
+	uintptr_t	self;			/* %gs:0 still gives us a Mach* */
+	uint64_t	splpc;			/* pc of last caller to splhi */
+
+	Proc*	proc;			/* current process on this processor */
+	uintptr_t	stack;		/* mach stack, kstack is in proc->kstack */
+	uintptr_t	rathole;	/* to save a reg in syscallentry */
+	Proc*	externup;		/* Forsyth recommends we replace the global up with this. */
+	/* end warning, I think */
+
+	int	machno;			/* physical id of processor */
+
+	int	online;
+
+	MMMU MMU;
+
+	uint64_t	ticks;			/* of the clock since boot time */
+	Label	sched;			/* scheduler wakeup */
+	Lock	alarmlock;		/* access to alarm list */
+	void*	alarm;			/* alarms bound to this clock */
+	int	inclockintr;
+
+	Proc*	readied;		/* old runproc, only relevant if kernel booted with nosmp (-n append) */
+	uint64_t   schedticks;		/* next forced context switch, same as above */
+	uint64_t	qstart;			/* time when up started running */
+	int	qexpired;		/* quantum expired */
+
+	int	tlbfault;
+	int	tlbpurge;
+	int	pfault;
+	int	cs;
+	int	syscall;
+	int	intr;
+	int	mmuflush;		/* make current proc flush it's mmu state */
+	int	ilockdepth;
+	Perf	perf;			/* performance counters */
+	int	inidle;			/* profiling */
+	int	lastintr;
+
+	uint64_t	cyclefreq;		/* Frequency of user readable cycle counter */
+	int64_t	cpuhz;
+	int	cpumhz;
+	uint64_t	rdtsc;
+
+	Lock	pmclock;
+	PmcCtr	pmc[PmcMaxCtrs];
+
+	MFPU FPU;
+	MCPU CPU;
+
+	NIX NIX;
+
+	/* for restoring pre-AMP scheduler */
+	Sched *sch;
+	int load;
+};
+
+struct Stackframe
+{
+	Stackframe *next;
+	uintptr_t pc;
+};
+
+/*
+ * firmware sets up the virtual memory of the kernel. What to do after that? I don't know.
+ */
+struct Sys {
+	unsigned char	machstk[MACHSTKSZ];
+
+	union {
+		Mach	mach;
+		unsigned char	machpage[MACHSZ];
+	};
+
+	union {
+		struct {
+			uint64_t	pmstart;	/* physical memory */
+			uint64_t	pmoccupied;	/* how much is occupied */
+			uint64_t	pmend;		/* total span */
+
+			uintptr_t	vmstart;	/* base address for malloc */
+			uintptr_t	vmunused;	/* 1st unused va */
+			uintptr_t	vmunmapped;	/* 1st unmapped va */
+			uintptr_t	vmend;		/* 1st unusable va */
+			uint64_t	epoch;		/* crude time synchronisation */
+
+			int		nc[NIXROLES];		/* number of online processors */
+			int		nmach;
+			int		load;
+			uint64_t	ticks;			/* of the clock since boot time */
+		};
+		unsigned char	syspage[4*KiB];
+	};
+
+	union {
+		Mach*	machptr[MACHMAX];
+		unsigned char	ptrpage[4*KiB];
+	};
+
+	uint64_t	cyclefreq;		/* Frequency of user readable cycle counter (mach 0) */
+
+	uint	pgszlg2[NPGSZ];		/* per Mach or per Sys? */
+	uint	pgszmask[NPGSZ];	/* Per sys -aki */
+	uint	pgsz[NPGSZ];
+	int	npgsz;
+
+	unsigned char	_57344_[2][4*KiB];		/* unused */
+};
+
+extern Sys *sys;
+#define MACHP(x) (sys->machptr[(x)])
+/*
+ * KMap
+ */
+typedef void KMap;
+extern KMap* kmap(Page*);
+
+#define kunmap(k)
+#define VA(k)		PTR2UINT(k)
+
+struct
+{
+	Lock l;
+	int	nonline;			/* # of active CPUs */
+	int nbooting;			/* # of CPUs waiting for the bTC to go */
+	int	exiting;			/* shutdown */
+	int	ispanic;			/* shutdown in response to a panic */
+	int	thunderbirdsarego;	/* lets the added processors continue */
+}active;
+
+/*
+ * The Mach structures must be available via the per-processor
+ * MMU information array machptr, mainly for disambiguation and access to
+ * the clock which is only maintained by the bootstrap processor (0).
+ */
+
+extern uintptr_t kseg0;
+
+extern char*rolename[];
+
+
+#pragma	varargck	type	"P"	uintmem
+
+/*
+ * Horrid.
+ */
+// HARVEY: TODO: bring this back, it's actually nice. Or do something better.
+// Talk to Ron before you condemn it.
+
+#ifdef _DBGC_
+#define DBGFLG		(dbgflg[_DBGC_])
+#else
+#define DBGFLG		(0)
+#endif /* _DBGC_ */
+
+#define DBG(...)	if(!DBGFLG){}else dbgprint(__VA_ARGS__)
+
+extern char dbgflg[256];
+
+#define dbgprint	print		/* for now */
+

+ 303 - 0
sys/src/9/riscv/fns.h

@@ -0,0 +1,303 @@
+/*
+ * 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 "../port/portfns.h"
+void	intrac(Proc*);
+void	acinit(void);
+int	acpiinit(void);
+void	actrapenable(int, char* (*)(Ureg*, void*), void*, char*);
+void	apicipi(int);
+void	apicpri(int);
+void	acsysret(void);
+void	actouser(void);
+void		runacore(void);
+void	aamloop(int);
+Dirtab*	addarchfile(char*, int,
+			   int32_t(*)(Chan*,void*,int32_t,int64_t),
+			   int32_t(*)(Chan*,void*,int32_t,int64_t));
+void	acmmuswitch(void);
+void	acmodeset(int);
+void	archfmtinstall(void);
+void	archidle(void);
+int	archmmu(void);
+int	asmfree(uintmem, uintmem, int);
+uint64_t	asmalloc(uintmem, uintmem, int, int);
+void	asminit(void);
+void	asmmapinit(uintmem, uintmem, int);
+extern void asmmodinit(uint32_t, uint32_t, char*);
+void	noerrorsleft(void);
+void	archinit(void);
+void	archreset(void);
+int64_t	archhz(void);
+int	cgaprint(int off, char *fmt, ...);
+int	cgaclearln(int off, int c);
+void	cgaconsputs(char*, int);
+void	cgainit(void);
+void	cgapost(int);
+void	checkpa(char*, uintmem);
+#define	clearmmucache()				/* x86 doesn't have one */
+void	(*coherence)(void);
+int	corecolor(int);
+uint32_t	cpuid(uint32_t, uint32_t, uint32_t[4]);
+int	dbgprint(char*, ...);
+int	decref(Ref*);
+void	delay(int);
+void	dumpmmu(Proc*);
+void	dumpmmuwalk(uint64_t pa);
+void	dumpptepg(int lvl,uintptr_t pa);
+#define	evenaddr(x)				/* x86 doesn't care */
+int	fpudevprocio(Proc*, void*, int32_t, uintptr_t, int);
+void	fpuinit(void);
+void	fpunoted(void);
+void	fpunotify(Ureg*);
+void	fpuprocrestore(Proc*);
+void	fpuprocsave(Proc*);
+void	fpusysprocsetup(Proc*);
+void	fpusysrfork(Ureg*);
+void	fpusysrforkchild(Proc*, Proc*);
+Mach*	getac(Proc*, int);
+char*	getconf(char*);
+void    gdb2ureg(uintptr_t *g, Ureg *u);
+void	halt(void);
+void	hardhalt(void);
+void	mouseenable(void);
+void	i8042systemreset(void);
+int	mousecmd(int);
+void	mouseenable(void);
+void	i8250console(char*);
+void*	i8250alloc(int, int, int);
+int64_t	i8254hz(uint32_t *info0, uint32_t *info1);
+void	idlehands(void);
+void	acidthandlers(void);
+void	idthandlers(void);
+int	inb(int);
+int	incref(Ref*);
+void	insb(int, void*, int);
+uint16_t	ins(int);
+void	inss(int, void*, int);
+uint32_t	inl(int);
+void	insl(int, void*, int);
+int	intrdisable(void*);
+void*	intrenable(int, void (*)(Ureg*, void*), void*, int, char*);
+void	invlpg(uintptr_t);
+void	iofree(int);
+void	ioinit(void);
+int	iounused(int, int);
+int	ioalloc(int, int, int, char*);
+int	ioreserve(int, int, int, char*);
+int	iprint(char*, ...);
+int	isaconfig(char*, int, ISAConf*);
+void	kexit(Ureg*);
+void	keybenable(void);
+void	keybinit(void);
+#define	kmapinval()
+void	lfence(void);
+void	links(void);
+void	machinit(void);
+void	mach0init(void);
+void	mapraminit(uint64_t, uint64_t);
+void	mapupainit(uint64_t, uint32_t);
+void	meminit(void);
+void	mfence(void);
+void	mmuflushtlb(uint64_t);
+void	mmuinit(void);
+uintptr_t	mmukmap(uintptr_t, uintptr_t, usize);
+int	mmukmapsync(uint64_t);
+uintmem	mmuphysaddr(uintptr_t);
+int	mmuwalk(PTE*, uintptr_t, int, PTE**, PTE (*)(usize));
+int	multiboot(uint32_t, uint32_t, int);
+void	ndnr(void);
+unsigned char	nvramread(int);
+void	nvramwrite(int, unsigned char);
+void	optionsinit(char*);
+void	outb(int, int);
+void	outsb(int, void*, int);
+void	outs(int, uint16_t);
+void	outss(int, void*, int);
+void	outl(int, uint32_t);
+void	outsl(int, void*, int);
+int	pcicap(Pcidev*, int);
+int	pcicfgr8(Pcidev*, int);
+int	pcicfgr16(Pcidev*, int);
+int	pcicfgr32(Pcidev*, int);
+void	pcicfgw8(Pcidev*, int, int);
+void	pcicfgw16(Pcidev*, int, int);
+void	pcicfgw32(Pcidev*, int, int);
+void	pciclrbme(Pcidev*);
+void	pciclrmwi(Pcidev*);
+int	pcigetpms(Pcidev*);
+void	pcihinv(Pcidev*);
+Pcidev*	pcimatch(Pcidev*, int, int);
+Pcidev*	pcimatchtbdf(int);
+void	pcireset(void);
+void	pcisetbme(Pcidev*);
+void	pcisetmwi(Pcidev*);
+int	pcisetpms(Pcidev*, int);
+int	pickcore(int, int);
+void	printcpufreq(void);
+void	putac(Mach*);
+void	runapcore(int);
+int	screenprint(char*, ...);			/* debugging */
+void	sfence(void);
+void	spldone(void);
+uint64_t	splhi(void);
+uint64_t	spllo(void);
+void	splx(uint64_t);
+void	splxpc(uint64_t);
+void	kstackok(void); /* panic if kstack guards garbaged, works with and without externup */
+Stackframe	*stackframe(void); /* l64v.S */
+void	stacksnippet(void);
+void	stopac(void);
+void	syncclock(void);
+void	syscall(unsigned int scallnr, Ureg *ureg);
+void*	sysexecregs(uintptr_t, uint32_t, void*);
+uintptr_t	sysexecstack(uintptr_t, int);
+void	sysprocsetup(Proc*);
+void	tssrsp0(Mach *, uint64_t);
+void	trapenable(int, void (*)(Ureg*, void*), void*, char*);
+void	trapinit(void);
+void	trap(Ureg*);
+void	umeminit(void);
+void    ureg2gdb(Ureg *u, uintptr_t *g);
+int	userureg(Ureg*);
+void*	vmap(uintptr_t, usize);
+void	vsvminit(int, int, Mach *);
+void	vunmap(void*, usize);
+
+extern uint64_t cr0get(void);
+extern void cr0put(uint64_t);
+extern uint64_t cr2get(void);
+extern uint64_t cr3get(void);
+extern void cr3put(uint64_t);
+extern uint64_t cr4get(void);
+extern void cr4put(uint64_t);
+extern void gdtget(void*);
+extern void gdtput(int, uint64_t, uint16_t);
+extern void idtput(int, uint64_t);
+extern uint64_t rdmsr(uint32_t);
+extern uint64_t rdtsc(void);
+extern void trput(uint64_t);
+extern void wrmsr(uint32_t, uint64_t);
+
+// TODO(aki): once we figure this out, these will go.
+extern int infected_with_std(void);
+extern void disinfect_std(void);
+
+extern int islo(void);
+extern void spldone(void);
+extern Mpl splhi(void);
+extern Mpl spllo(void);
+extern void splx(Mpl);
+
+int	cas32(void*, uint32_t, uint32_t);
+int	cas64(void*, uint64_t, uint64_t);
+int	tas32(void*);
+uint64_t	fas64(uint64_t*, uint64_t);
+
+#define CASU(p, e, n)	cas64((p), (uint64_t)(e), (uint64_t)(n))
+#define CASV(p, e, n)	cas64((p), (uint64_t)(e), (uint64_t)(n))
+#define CASP(p, e, n)	cas64((p), (uint64_t)(e), (uint64_t)(n))
+#define CASW(p, e, n)	cas32((p), (e), (n))
+#define TAS(addr)	tas32((addr))
+#define	FASP(p, v)	((void*)fas64((uint64_t*)(p), (uint64_t)(v)))
+
+void	touser(uintptr_t);
+void	syscallentry(void);
+void	acsyscallentry(void);
+void	syscallreturn(void);
+void	sysrforkret(void);
+
+#define	waserror()	setlabel(&up->errlab[up->nerrlab++])
+#define	poperror()	up->nerrlab--
+
+#define	dcflush(a, b)
+
+#define PTR2UINT(p)	((uintptr_t)(p))
+#define UINT2PTR(i)	((void*)(i))
+
+void*	KADDR(uintptr_t);
+uintmem	PADDR(void*);
+
+#define BIOSSEG(a)	KADDR(((uint)(a))<<4)
+
+/*
+ * apic.c
+ */
+extern int apiceoi(int);
+extern void apicipi(int);
+extern void apicinit(int, uintmem, int);
+extern int apicisr(int);
+extern int apiconline(void);
+extern void apicpri(int);
+extern void apicsipi(int, uintmem);
+
+extern void ioapicinit(int, uintmem);
+extern void ioapicintrinit(int, int, int, int, uint32_t);
+extern void ioapiconline(void);
+
+/*
+ * archamd64.c
+ */
+extern void millidelay(int);
+extern void k10mwait(void*);
+
+/*
+ * i8259.c
+ */
+extern int i8259init(int);
+extern int i8259irqdisable(int);
+extern int i8259irqenable(int);
+extern int i8259isr(int);
+
+/*
+ * mp.c
+ */
+extern void mpsinit(int);
+
+/*
+ * sipi.c
+ */
+extern void sipi(void);
+
+/*
+ * debug
+ */
+void HERE(void);
+void DONE(void);
+
+/* all these go to 0x3f8 */
+void hi(char *s);
+
+Mach *machp(void);
+Proc *externup(void);
+
+/* temporary. */
+void die(char *);
+void dumpgpr(Ureg* ureg);
+
+/* debug support. */
+int backtrace_list(uintptr_t pc, uintptr_t fp, uintptr_t *pcs, size_t nr_slots);
+
+/* horror */
+static inline void __clobber_callee_regs(void)
+{
+	asm volatile ("" : : : "rbx", "r12", "r13", "r14", "r15");
+}
+
+int slim_setlabel(Label*) __attribute__((returns_twice));
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+
+#define setlabel(label) ({int err;                                                 \
+                    __clobber_callee_regs();                               \
+                    err = slim_setlabel(label);                                     \
+                    err;})
+
+#pragma GCC diagnostic pop

+ 150 - 0
sys/src/9/riscv/mem.h

@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+
+/*
+ * Memory and machine-specific definitions.  Used in C and assembler.
+ */
+
+/*
+ * unfortunately, gas does not accept [u]l* suffixes, then we must to avoid them.
+ * https://sourceware.org/bugzilla/show_bug.cgi?id=190
+ */
+#ifndef __ASSEMBLER__
+#define KiB		1024u			/* Kibi 0x0000000000000400 */
+#define MiB		1048576u		/* Mebi 0x0000000000100000 */
+#define GiB		1073741824u		/* Gibi 000000000040000000 */
+#define TiB		1099511627776ull	/* Tebi 0x0000010000000000 */
+#define PiB		1125899906842624ull	/* Pebi 0x0004000000000000 */
+#define EiB		1152921504606846976ull	/* Exbi 0x1000000000000000 */
+#else
+#define KiB             1024
+#define MiB             1048576
+#define GiB             1073741824
+#define TiB             1099511627776
+#define PiB             1125899906842624
+#define EiB             1152921504606846976
+#endif
+
+#define HOWMANY(x, y)	(((x)+((y)-1))/(y))
+#define ROUNDUP(x, y)	(HOWMANY((x), (y))*(y))
+#define ROUNDDN(x, y)	(((x)/(y))*(y))
+#define MIN(a, b)	((a) < (b)? (a): (b))
+#define MAX(a, b)	((a) > (b)? (a): (b))
+
+#define ALIGNED(p, a)	(!(((uintptr)(p)) & ((a)-1)))
+
+/*
+ * Sizes
+ */
+#define BI2BY		8			/* bits per byte */
+#define	BI2WD		32			/* bits per word */
+#define	BY2WD		4			/* bytes per word */
+#define BY2V		8			/* bytes per double word */
+#define BY2SE		8			/* bytes per stack element */
+#define BLOCKALIGN	8
+
+/*
+ * 4K pages
+ * these defines could go.
+ */
+#define PGSZ		(4*KiB)			/* page size */
+#define PGSHFT		12			/* log(PGSZ) */
+#define PTSZ		(4*KiB)			/* page table page size */
+#define PTSHFT		9			/*  */
+
+#define MACHSZ		(4*KiB)			/* Mach+stack size */
+#define MACHMAX		32			/* max. number of cpus */
+#define MACHSTKSZ	(6*(4*KiB))		/* Mach stack size */
+
+#define KSTACK		(16*1024)		/* Size of Proc kernel stack */
+#define STACKALIGN(sp)	((sp) & ~(BY2SE-1))	/* bug: assure with alloc */
+
+#define STACKGUARD	0xdeadbeefcafebabeULL /* magic number we keep at stack bottom and check on traps */
+
+/*
+ * 2M pages
+ * these defines must go.
+ */
+#define	BIGPGSHFT	21
+#ifndef __ASSEMBLER__
+#define	BIGPGSZ		(1ull<<BIGPGSHFT)
+#else
+#define BIGPGSZ         (1<<BIGPGSHFT)
+#endif
+#define	BIGPGROUND(x)	ROUNDUP((x), BIGPGSZ)
+#define	PGROUND(x)	ROUNDUP((x), PGSZ)
+#define	PGSPERBIG	(BIGPGSZ/PGSZ)
+
+/*
+ * Time
+ */
+#define HZ		(100)			/* clock frequency */
+#define MS2HZ		(1000/HZ)		/* millisec per clock tick */
+#define TK2SEC(t)	((t)/HZ)		/* ticks to seconds */
+
+/*
+ *  Address spaces. User:
+ */
+#define UTZERO		(0+2*MiB)		/* first address in user text */
+#define UTROUND(t)	ROUNDUP((t), BIGPGSZ)
+#ifndef __ASSEMBLER__
+#define USTKTOP		(0x00007ffffffff000ull & ~(BIGPGSZ-1))
+#else
+#define USTKTOP         (0x00007ffffffff000 & ~(BIGPGSZ-1))
+#endif
+#define USTKSIZE	(16*1024*1024)		/* size of user stack */
+#define TSTKTOP		(USTKTOP-USTKSIZE)	/* end of new stack in sysexec */
+#define	NIXCALL		(TSTKTOP-USTKSIZE)	/* nix syscall queues (2MiB) */
+#ifndef __ASSEMBLER__
+#define BIGBSSTOP	((NIXCALL-BIGPGSZ) & ~(1ULL*GiB-1))
+#define BIGBSSSIZE	(32ull*GiB)			/* size of big heap segment */
+#else
+#define BIGBSSTOP       ((NIXCALL-BIGPGSZ) & ~(1*GiB-1))
+#define BIGBSSSIZE      (32*GiB)                     /* size of big heap segment */
+#endif
+#define HEAPTOP		(BIGBSSTOP-BIGBSSSIZE)	/* end of shared heap segments */
+
+
+/*
+ *  Address spaces. Kernel, sorted by address.
+ */
+
+#ifndef __ASSEMBLER__
+#define KSEG2		(0xfffffe0000000000ull)	/* 1TB - KMAP */
+/*			 0xffffff0000000000ull	end of KSEG2 */
+#define VMAP		(0xffffffffe0000000ull)
+#define VMAPSZ		(256*MiB)
+#define KSEG0		(0xffffffff80000000ull)	/* 256MB - this is confused */
+#define KZERO		(0xffffffff80000000ull)
+#define KTZERO		(KZERO+1*MiB+64*KiB)
+#define PDMAP		(0xffffffffff800000ull)
+#define PMAPADDR		(0xffffffffffe00000ull)	/* unused as of yet */
+/*			 0xffffffffffffffffull	end of KSEG0 */
+#else
+#define KSEG2           (0xfffffe0000000000)
+#define VMAPSZ          (256*MiB)
+#define VMAP            (0xffffffffe0000000)
+#define KSEG0           (0xffffffff80000000)
+#define KZERO           (0xffffffff80000000)
+#define KTZERO          (KZERO+1*MiB+64*KiB)
+#define PDMAP           (0xffffffffff800000)
+#define PMAPADDR        (0xffffffffffe00000)
+#endif
+
+/*
+ * Hierarchical Page Tables.
+ * For example, traditional IA-32 paging structures have 2 levels,
+ * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
+ * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
+ * and level 0 the PT pages. The PTLX macro gives an index into the
+ * page-table page at level 'l' for the virtual address 'v'.
+ */
+#define PTLX(v, l)	(((v)>>(((l)*PTSHFT)+PGSHFT)) & ((1<<PTSHFT)-1))
+#define PGLSZ(l)	(1<<(((l)*PTSHFT)+PGSHFT))
+