Subject: Re: [9front] [PATCH] mk for only supported archs

From: [email-masked]
Date: Sat, 21 Aug 2021 01:27:45 -0700

The mtx and ppc (ucu config) kernel now build.  I've also added a
Readme for the ppc kernel.  I gleaned the Readme and some code from
the Plan 9 kernel found on github; I assume it was updated after
9front had forked.  I made some minor edits to Readme, but otherwise I
left the code received as-is (namely, ppc/^(trap.c l.s devce.c) ).  I did
not see any manual page for devce.  As for (emulated) hardware, QEMU
emulates the G3 but not the G2.

'mk kernels' now completes without failure.

Quoth [email-masked]:
> I'm testing that all the "supported" archs build.  This is the rc
> script I use:
> 
> #!/bin/rc
> echo Updating system
> sysupdate || exit 'sysupdate failed'
> . /sys/lib/rootstub
> cd /sys/src
> objtypes_default=`{cat mkfile.proto| grep 'CPUS=' | sed -e 's/CPUS=//'}
> if (~ $objtypes '')
> 	objtypes=($cputype `{{for (f in $cputype $objtypes_default) echo $f} | sort | uniq -u})
> for (objtype in $objtypes)
> 	for (mk in nuke all install clean)
> 		mk $mk || exit 'mk '^$mk^' failed for '^$objtype
> 
> cd /sys/man
> mk || exit '/sys/man failed'
> 
> cd /sys/doc
> mk || exit '/sys/doc failed'
> 
> cd /sys/src
> mk kernels || exit 'mk kernels failed'
> 

--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/dat.h
+++ /sys/src/9/mtx/dat.h
@@ -71,6 +71,8 @@
 	FPinit,
 	FPactive,
 	FPinactive,
+
+	FPillegal= 0x100,
 };
 
 struct Confmem
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/devarch.c
+++ /sys/src/9/mtx/devarch.c
@@ -130,7 +130,6 @@
 	uchar *cp;
 	ushort *sp;
 	ulong *lp;
-	IOMap *m;
 	Rdwrfn *fn;
 
 	switch((ulong)c->qid.path){
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/fns.h
+++ /sys/src/9/mtx/fns.h
@@ -47,7 +47,6 @@
 void	ioinit(void);
 int	iprint(char*, ...);
 int	isaconfig(char*, int, ISAConf*);
-#define kexit(a)
 #define	kmapinval()
 void	links(void);
 void	mmuinit(void);
@@ -88,6 +87,10 @@
 void	tlbflushall(void);
 #define	userureg(ur) (((ur)->status & MSR_PR) != 0)
 void	watchreset(void);
+
+/* devarch */
+Dirtab*	addarchfile(char*, int, long(*)(Chan*, void*, long, vlong), 
+	long(*)(Chan*, void*, long, vlong));
 
 #define	waserror()	(up->nerrlab++, setlabel(&up->errlab[up->nerrlab-1]))
 #define KADDR(a)	((void*)((ulong)(a)|KZERO))
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/mem.h
+++ /sys/src/9/mtx/mem.h
@@ -2,6 +2,9 @@
  * Memory and machine-specific definitions.  Used in C and assembler.
  */
 
+#define MIN(a, b)	((a) < (b)? (a): (b))
+#define MAX(a, b)	((a) > (b)? (a): (b))
+
 /*
  * Sizes
  */
@@ -31,7 +34,8 @@
 /*
  * Time
  */
-#define	HZ		100			/* clock frequency */
+#define	HZ		(100)			/* clock frequency */
+#define	MS2HZ		(1000/HZ)		/* millisec per clock tick */
 #define	TK2SEC(t)	((t)/HZ)		/* ticks to seconds */
 
 /*
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/mkfile
+++ /sys/src/9/mtx/mkfile
@@ -58,8 +58,8 @@
 	/$objtype/lib/libmemdraw.a\
 	/$objtype/lib/libdraw.a\
 	/$objtype/lib/libip.a\
-	/$objtype/lib/libc.a\
 	/$objtype/lib/libsec.a\
+	/$objtype/lib/libc.a\
 
 ETHER=`{echo devether.c ether*.c | sed 's/\.c/.'$O'/g'}
 VGA=`{echo devvga.c screen.c vga*.c | sed 's/\.c/.'$O'/g'}
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/mtx
+++ /sys/src/9/mtx/mtx
@@ -9,6 +9,7 @@
 	proc
 	mnt
 	srv
+	shr
 	dup
 	ssl
 	cap
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/pcimtx.c
+++ /sys/src/9/mtx/pcimtx.c
@@ -115,7 +115,7 @@
 int
 pcicfgrw8(int tbdf, int rno, int data, int read)
 {
-	int o, x;
+	int o;
 
 	switch(pcicfgmode){
 	case 1:
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/raven.c
+++ /sys/src/9/mtx/raven.c
@@ -3,6 +3,7 @@
  */
 #include	"u.h"
 #include	"../port/lib.h"
+#include	"../port/pci.h"
 #include	"mem.h"
 #include	"dat.h"
 #include	"fns.h"
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/9/mtx/trap.c
+++ /sys/src/9/mtx/trap.c
@@ -1,5 +1,6 @@
 #include	"u.h"
 #include	"../port/lib.h"
+#include	"../port/pci.h"
 #include	"mem.h"
 #include	"dat.h"
 #include	"fns.h"
@@ -632,7 +633,7 @@
 		up->s = *((Sargs*)(sp+BY2WD));
 		up->psstate = sysctab[scallnr];
 
-		ret = systab[scallnr](up->s.args);
+		ret = systab[scallnr]((char *)up->s.args);
 		poperror();
 	}else{
 		/* failure: save the error buffer for errstr */
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/boot/zynq/mkfile
+++ /sys/src/boot/zynq/mkfile
@@ -7,6 +7,9 @@
 
 all:V: $TARG
 
+install:V:
+	echo install not supported >[1=2]
+
 clean:V:
 	rm -rf $TARG *.$O
 	@{objtype=$cputype mk -f mkfile.port clean}
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/cmd/mkfile
+++ /sys/src/cmd/mkfile
@@ -8,8 +8,9 @@
 YFLAGS=-d
 
 NOTSYS=sml|dup|.+\..+
+NOTPOWER=|dtracy
 PCONLY=|vmx
-NOMK=$NOTSYS`{~ $objtype (386 amd64) || echo $PCONLY}
+NOMK=$NOTSYS$NOTPOWER`{~ $objtype (386 amd64) || echo $PCONLY}
 
 cpuobjtype=`{sed -n 's/^O=//p' /$cputype/mkfile}
 DIRS=`{echo */mkfile | sed 's,/mkfile *,\n,g' | grep -v '^('$NOMK')$'}
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree//sys/src/mkfile.proto
+++ /sys/src/mkfile.proto
@@ -2,8 +2,8 @@
 # common mkfile parameters shared by all architectures
 #
 
-OS=05678qv
-CPUS=spim arm arm64 amd64 386 power mips
+OS=5678qv
+CPUS=arm arm64 amd64 386 power mips
 CFLAGS=-FTVw
 LEX=lex
 YACC=yacc
--- /tmp/diff100001999533
+++ sys/src/9/ppc/Readme
@@ -1,0 +1,26 @@
+This directory contains source code for two PowerPC ports,
+
+The kernel made with
+	mk 'CONF=blast'
+is an 8260 kernel.  The 8260 (PowerQUICC II, e.g.  Apple G2) is an
+embedded PowerPC with on-chip DMA, Uart, Ethernet, ATM controllers and
+a few other things.
+
+The 8260-specific code is in m8260.c (no surprises there) and in the
+device drivers for SMC uart (uartsmc.c) and FCC ethernet (etherfcc.c).
+
+The kernel made with
+	mk 'CONF=ucu'
+is a PowerPC 750/755 (e.g., Apple G3) kernel using the saturn chip for
+Ether and Uart.
+
+We run both kernels on Lucent proprietary boards, but the kernels are
+pretty generic.  On a different board, the ports may be hooked up differently,
+the clocks may be running at different speeds, the memory banks may be
+configured differently and this will require tweaking the Ethernet setup,
+Uart setup, Memory bank setup and several other things as well.
+
+There is 8260 and Ucu specific code in l.s too (lucu.s and lblast.s).
+These differences primarily concern the state of the system at bootup.
+In the case of the ucu, virtual memory is already on when the kernel
+starts.
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/clock.c
+++ sys/src/9/ppc/clock.c
@@ -82,6 +82,11 @@
 }
 
 ulong
+µs(void)
+{
+	return fastticks2us(m->ticks);
+}
+ulong
 perfticks(void)
 {
 	return (ulong)fastticks(nil);
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/dat.h
+++ sys/src/9/ppc/dat.h
@@ -15,7 +15,7 @@
 typedef struct Sys	Sys;
 typedef struct Ureg	Ureg;
 typedef struct Vctl	Vctl;
-typedef long		Tval;
+typedef uvlong		Tval;
 
 #pragma incomplete Ureg
 #pragma incomplete Imap
@@ -37,8 +37,9 @@
 	ulong	sr;
 	ulong	pc;
 	Proc	*p;
-	ulong	pid;
+	Mach	*m;
 	ushort	isilock;
+	long	lockcycles;
 };
 
 struct Label
--- /tmp/diff100001999542
+++ sys/src/9/ppc/devce.c
@@ -1,0 +1,191 @@
+//
+// channel element driver
+//
+#include "u.h"
+#include "../port/lib.h"
+#include "mem.h"
+#include "dat.h"
+#include "fns.h"
+#include "io.h"
+#include "../port/error.h"
+#include "msaturn.h"
+
+enum{
+	Ucuunknown = 0,
+	Ucu64,
+
+	Ndsp = 16,
+
+	Cedead = 0,
+	Cereset,
+	Celoaded,
+	Cerunning,
+	
+	Qdir = 0,
+	Qctl,
+	Qce,
+
+	Cpldbase = Saturn + 0x6000000,
+		Cplducuversion_ucu = 0x1c,
+	Cebase = Saturn + 0x3000000,
+	Cesize = 0x100000,
+};
+
+typedef struct Cpld Cpld;
+struct Cpld{
+	uchar	led;
+	uchar	fpga1;
+	uchar	slotid;
+	uchar	version;
+	uchar	watchdog;
+	uchar	spi;
+	uchar	asicreset;
+	uchar	dspreset;
+	uchar	generalreset;
+	uchar	ucuversion;
+	uchar	fpga2;
+};
+
+typedef struct Circbuf Circbuf;
+struct Circbuf{
+	uchar	*nextin;
+	uchar	*nextout;
+	uchar	*start;
+	uchar	*end;
+};
+
+typedef struct Dsp Dsp;
+struct Dsp{
+	Ref;
+	int		state;
+	Circbuf	*cb;
+};
+
+typedef struct Ce Ce;
+struct Ce{
+	int		ucutype;
+	Ce		*ces[Ndsp];
+};
+
+static Cpld*cpld = (Cpld*)Cpldbase;
+static Ce ce;
+
+static void
+ceinit(void)
+{
+	if(cpld->ucuversion & Cplducuversion_ucu)
+		ce.ucutype = Ucu64;
+	else{
+		print("ceinit: unsuppoerted UCU\n");
+		return;
+	}
+
+}
+
+static Chan*
+ceattach(char*spec)
+{
+	return devattach('C', spec);
+}
+
+#define DEV(q)			((int)(((q).path >> 8) & 0xff))
+#define TYPE(q)			((int)((q).path & 0xff))
+#define QID(d, t)		((((d) & 0xff) << 8) | (t))
+
+static int
+cegen(Chan*c, char*, Dirtab*, int, int i, Dir*dp)
+{
+	Qid qid;
+
+	switch(TYPE(c->qid)){
+	case Qdir:
+		if(i == DEVDOTDOT){
+			mkqid(&qid, QID(0, Qdir), 0, QTDIR);
+			devdir(c, qid, "#C", 0, eve, 0555, dp);
+			return 1;
+		}
+
+		if(i == 0){
+			mkqid(&qid, QID(-1, Qctl), 0, QTFILE);
+			devdir(c, qid, "cectl", 0, eve, 0644, dp);
+			return 1;
+		}
+
+		if (--i >= Ndsp)
+			return -1;
+	
+		mkqid(&qid, QID(Qce, i), 0, QTFILE);
+		snprint(up->genbuf, sizeof(up->genbuf), "ce%d", i);
+		devdir(c, qid, up->genbuf, 0, eve, 0644, dp);
+		return 1;
+
+	default:
+		return -1;
+	}
+}
+
+static Walkqid *
+cewalk(Chan*c, Chan*nc, char**name, int nname)
+{
+	return devwalk(c, nc, name, nname, 0, 0, cegen);
+}
+
+static int
+cestat(Chan*c, uchar*db, int n)
+{
+	return devstat(c, db, n, 0, 0, cegen);
+}
+
+static Chan*
+ceopen(Chan*c, int omode)
+{
+	c->mode = openmode(omode);
+	c->flag |= COPEN;
+	c->offset = 0;
+	return c;
+}
+
+static void
+ceclose(Chan*)
+{}
+
+static long
+ceread(Chan*c, void*a, long n, vlong)
+{
+	switch(TYPE(c->qid)){
+	case Qdir:
+		return devdirread(c, a, n, 0, 0, cegen);
+	
+	default:
+		error("unsupported operation");
+	}
+	return 0;
+}
+
+static long
+cewrite(Chan*, void*, long, vlong)
+{
+	return 0;
+}
+
+Dev cedevtab = {
+	'C',
+	"channel element",
+
+	devreset,
+	ceinit,
+	devshutdown,
+	ceattach,
+	cewalk,
+	cestat,
+	ceopen,
+	devcreate,
+	ceclose,
+	ceread,
+	devbread,
+	cewrite,
+	devbwrite,
+	devremove,
+	devwstat,
+	devpower,
+};
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/fns.h
+++ sys/src/9/ppc/fns.h
@@ -1,7 +1,9 @@
 #include "../port/portfns.h"
 
+ulong	cankaddr(ulong);
 void	clockinit(void);
 void	clockintr(Ureg*);
+int	cmpswap(long*, long, long);
 void	cpuidprint(void);
 void	cycles(uvlong*);
 void	dbgputc(int c);
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/l.s
+++ sys/src/9/ppc/l.s
@@ -346,7 +346,6 @@
 	RETURN
 
 TEXT tas(SB), $0
-TEXT _tas(SB), $0
 	MOVW	R3, R4
 	MOVW	$0xdead, R5
 tas1:
@@ -360,6 +359,42 @@
 	EIEIO
 tas0:
 	SYNC
+	RETURN
+
+TEXT _xinc(SB), $0			/* void _xinc(long *); */
+	MOVW	R3, R4
+xincloop:
+	DCBF	(R4)			/* fix for 603x bug */
+	LWAR	(R4), R3
+	ADD	$1, R3
+	STWCCC	R3, (R4)
+	BNE	xincloop
+	RETURN
+
+TEXT _xdec(SB), $0			/* long _xdec(long *); */
+	MOVW	R3, R4
+xdecloop:
+	DCBF	(R4)			/* fix for 603x bug */
+	LWAR	(R4), R3
+	ADD	$-1, R3
+	STWCCC	R3, (R4)
+	BNE	xdecloop
+	RETURN
+
+TEXT cmpswap(SB),$0	/* int cmpswap(long*, long, long) */
+	MOVW	R3, R4	/* addr */
+	MOVW	old+4(FP), R5
+	MOVW	new+8(FP), R6
+	DCBF	(R4)		/* fix for 603x bug? */
+	LWAR	(R4), R3
+	CMP	R3, R5
+	BNE fail
+	STWCCC	R6, (R4)
+	BNE fail
+	MOVW $1, R3
+	RETURN
+fail:
+	MOVW $0, R3
 	RETURN
 
 TEXT tlbflushall(SB), $0
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/mem.h
+++ sys/src/9/ppc/mem.h
@@ -8,6 +8,9 @@
 #include "blast.h"
 #endif
 
+#define MIN(a, b)	((a) < (b)? (a): (b))
+#define MAX(a, b)	((a) > (b)? (a): (b))
+
 /*
  * Sizes
  */
@@ -37,7 +40,8 @@
 /*
  * Time
  */
-#define	HZ		1000			/* clock frequency */
+#define	HZ		(1000)			/* clock frequency */
+#define	MS2HZ		(HZ)		/* millisec per clock tick */
 #define	TK2SEC(t)	((t)/HZ)		/* ticks to seconds */
 
 /*
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/mkfile
+++ sys/src/9/ppc/mkfile
@@ -37,6 +37,7 @@
 	tod.$O\
 	xalloc.$O\
 	random.$O\
+	userinit.$O\
 
 OBJ=\
 	l.$O\
@@ -60,8 +61,9 @@
 	/$objtype/lib/libmemlayer.a\
 	/$objtype/lib/libmemdraw.a\
 	/$objtype/lib/libdraw.a\
-	/$objtype/lib/libc.a\
 	/$objtype/lib/libsec.a\
+	/$objtype/lib/libip.a\
+	/$objtype/lib/libc.a\
 
 ETHER=`{echo devether.c ether*.c | sed 's/\.c/.'$O'/g'}
 VGA=`{echo devvga.c screen.c vga*.c | sed 's/\.c/.'$O'/g'}
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/saturntimer.c
+++ sys/src/9/ppc/saturntimer.c
@@ -72,7 +72,7 @@
 }
 
 void
-timerset(uvlong next)
+timerset(Tval next)
 {
 	ulong offset;
 	uvlong now;
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/trap.c
+++ sys/src/9/ppc/trap.c
@@ -11,6 +11,14 @@
 static Lock vctllock;
 Vctl *vctl[256];
 
+int nintr[10];
+int nintro;
+int dblintr[64];
+ulong thisto[32];
+ulong thistoo;
+vlong vnot[64];
+ulong vnon[64];
+
 void
 intrenable(int irq, void (*f)(Ureg*, void*), void* a, char *name)
 {
@@ -368,6 +376,62 @@
 		vp[n] = 0x4e800021;		/* BL (LR) */
 	}else
 		vp[n] = (18<<26)|(pa&0x3FFFFFC)|3;	/* bla */
+}
+
+void
+intr(Ureg *ureg)
+{
+	int vno, pvno, i;
+	Vctl *ctl, *v;
+	void (*pt)(Proc*, int, vlong);
+	uvlong tt, x;
+
+	cycles(&tt);
+	pt = proctrace;
+	pvno = -1;
+	for(i = 0; i < 64; i++){
+		vno = intvec();
+		if(vno == 0)
+			break;
+		cycles(&x);
+		vnot[vno] -= x;
+		if(vno == pvno)
+			dblintr[vno]++;
+		pvno = vno;
+		if(pt && up && up->trace)
+			pt(up, (vno << 16) | SInts, 0);
+	
+		if(vno > nelem(vctl) || (ctl = vctl[vno]) == 0) {
+			iprint("spurious intr %d\n", vno);
+			return;
+		}
+
+		for(v = ctl; v != nil; v = v->next)
+			if(v->f)
+				v->f(ureg, v->a);
+
+		intend(vno);	/* reenable the interrupt */
+
+		if(pt && up && up->trace)
+			pt(up, (vno << 16) | SInte, 0);
+		cycles(&x);
+		vnot[vno] += x;
+		vnon[vno]++;
+	}
+	if(i < nelem(nintr))
+		nintr[i]++;
+	else
+		nintro++;
+	cycles(&x);
+	tt = x - tt;
+	i = tt / 3600;	 /* 100 microseconds units */
+	if(i < nelem(thisto))
+		thisto[i]++;
+	else
+		thistoo++;
+
+	if(up)
+		preempted();
 }
 
 char*
--- //.git/fs/object/8c796bf8b9c10a0845717c47af908e47dfe14482/tree/sys/src/9/ppc/ucu
+++ sys/src/9/ppc/ucu
@@ -1,11 +1,13 @@
 dev
 	root
 	cons
+	swap
 	env
 	pipe
 	proc
 	mnt
 	srv
+	shr
 	dup
 	ssl
 	cap

---

From: [email-masked]
Date: Sun, 22 Aug 2021 16:33:37 +0200

also, can we separate the /sys/src/mkfile.proto and /sys/src/cmd/mkfile 
and /sys/src/9/zynq changes from the mtx kernel changes?

why are we removing spim from archs?

--
cinap

---

From: [email-masked]
Date: Sun, 22 Aug 2021 16:15:33 +0200

the patch is mangled by the mailinglist. please send it as an attachment,
and not inline for application. preferrably in a way where i can just
git/import it without having to recreating your commit message.

thank you.

--
cinap

---

From: Amavect <[email-masked]>
Date: Sun, 22 Aug 2021 10:13:09 -0500

> why are we removing spim from archs?

spim does not build, and we have no working spim kernel.
Thus, there is no reason to include spim for mk installall.
Attached is my changes.

Thanks,
Amavect

From: Amavect <[email-masked]>
Date: Sun, 22 Aug 2021 15:05:38 +0000
Subject: [PATCH] mkfile.proto: remove spim and $MK, organize $CPUS

`objtype=spim mk install` does not build, and we have no working kernel.
$MK is useless, and is not used anywhere in the system.
Reorder $CPUS in lexographic order for fun.
---
diff 8c796bf8b9c10a0845717c47af908e47dfe14482
2106158cb0e529e550a65e51b21b66cc12d580b0 ---
a/sys/src/mkfile.proto	Thu Aug 19 23:07:37 2021 +++
b/sys/src/mkfile.proto	Sun Aug 22 10:05:38 2021 @@ -3,11 +3,10 @@
 #
 
 OS=05678qv
-CPUS=spim arm arm64 amd64 386 power mips
+CPUS=386 amd64 arm arm64 mips power
 CFLAGS=-FTVw
 LEX=lex
 YACC=yacc
-MK=/bin/mk
 
 # recursive mk will have these set from the parent
 # this is never what we want.  clear them
from postmaster@1ess:
The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-patch
	Content-Transfer-Encoding: 7bit
	Content-Disposition: attachment; filename=mkfileproto.diff