code: 9ferno

Download patch

ref: 8bb05d8ca955ed85c7028d74371f1d23ce66a800
parent: c5b0ea7305dca401bedaa2a88a6816ff96ae51d0
author: 9ferno <gophone2015@gmail.com>
date: Sun Jan 23 14:45:14 EST 2022

forth init is a go

--- a/os/pc64/main.c
+++ b/os/pc64/main.c
@@ -7,6 +7,7 @@
 #include	"io.h"
 #include	"ureg.h"
 #include	"rebootcode.i"
+#include	"kernel.h"		/* for kopen() only */
 
 #define X86STEPPING(x)	((x) & 0x0F)
 #define X86MODEL(x)	(((x)>>4) & 0x0F)
@@ -317,7 +318,14 @@
 	}
 	kproc("alarm", alarmkproc, 0, 0);
 
+	if(kopen("#c/cons", OREAD) != 0)
+		panic("failed to make fd0 from #c/cons");
+	kopen("#c/cons", OWRITE);
+	kopen("#c/cons", OWRITE);
+
+	goforth(up->fmem);
 	/* disinit("/osinit.dis"); */
+	/* disinit("/osinit.dis"); */
 	/* init0 will never return */
 	panic("init0");
 }
@@ -329,7 +337,7 @@
 	Osenv *o;
 
 	up = nil;
-	if((p = newproc()) == nil){
+	if((p = newforthproc()) == nil){
 		panic("no procs for userinit");
 	}
 	o = p->env;
--- a/os/port/chan.c
+++ b/os/port/chan.c
@@ -1319,7 +1319,7 @@
 		error("empty file name");
 	aname = validnamedup(aname, 1);
 	if(waserror()){
-		print("namec: waserror() loop %r\n");
+		print("namec: waserror() loop pid %d %r\n", up->pid);
 		free(aname);
 		nexterror();
 	}
@@ -1382,7 +1382,7 @@
 	e.nelems = 0;
 	e.nerror = 0;
 	if(waserror()){
-print("namec: waserror() loop before parsename\n");
+print("namec: waserror() loop before parsename pid %d\n", up->pid);
 		cclose(c);
 		free(e.name);
 		free(e.elems);
@@ -1424,10 +1424,14 @@
 	}
 
 	if(walk(&c, e.elems, e.nelems, nomount, &e.nerror) < 0){
-print("namec: walk < 0 e.nerror %d\n", e.nerror);
+print("namec: walk < 0 e.nerror %d pid %d\n", e.nerror, up->pid);
 		if(e.nerror < 0 || e.nerror > e.nelems){
 			print("namec %s walk error nerror=%d\n", aname, e.nerror);
 			e.nerror = 0;
+		}else{
+			for(int j=0; j<e.nelems; j++){
+				print("	e.elems[%d] %s\n", j, e.elems[j]);
+			}
 		}
 		nexterror();
 	}
--- a/os/port/devforth.c
+++ b/os/port/devforth.c
@@ -327,19 +327,19 @@
 
 extern intptr forthmain(u8 *);
 void
-forthentry(void *fmem)
+goforth(void *fmem)
 {
 	up->type = Forth;
 
 	/* load dictionary */
 	loadforthdictionary((u8*)fmem);
-	print("forthentry pid %d forthmem 0x%zx end 0x%zx forthmem+RSTACK 0x%zx\n",
+	print("goforth pid %d forthmem 0x%zx end 0x%zx forthmem+RSTACK 0x%zx\n",
 		up->pid, (intptr)fmem, ((intptr*)fmem)[1], (intptr)fmem+RSTACK);
 	DBG("fentries[0].name %s\n", fentries[0].hdr.name);
 	DBG("fentries[1].name %s nfentries %d\n", fentries[1].hdr.name, nelem(fentries));
 	DBG("up->kstack 0x%p\n", up->kstack);
 	if(waserror()){
-		print("forthentry error: %r\n");
+		print("goforth error: %r\n");
 		nexterror();
 	}else
 		forthmain((u8*)fmem);
@@ -498,7 +498,7 @@
 	p->kparg = arg;
 	kprocchild(p, linkproc);*/
 /* this does all of the above 3 lines */
-	kprocchild(p, forthentry, p->fmem);
+	kprocchild(p, goforth, p->fmem);
 
 	p->text = forthname;
 
@@ -507,7 +507,7 @@
 /*	cycles(&p->kentry);
 	p->pcycles = -p->kentry;*/
 
-	p->fstarted = 1;
+	p->fisgo = 1;
 	qunlock(&p->debug);
 	p->psstate = nil;
 
@@ -776,7 +776,7 @@
 		freebroken();
 		resrcwait("no procs for kproc");
 	}
-	p->fstarted = 0; /* until the pctl message comes through */
+	p->fisgo = 0; /* until the pctl message comes through */
 	kstrdup(&p->env->user, up->env->user);
 
 	if(fhead == nil){
@@ -1012,7 +1012,7 @@
 	DBG("forthwrite c->path %s f->pid %d\n", chanpath(c), f->pid);
 	switch(QID(c->qid)){
 	case Qctl:
-		if(f->fstarted == 0){
+		if(f->fisgo == 0){
 			/* pctl message */
 			((char*)buf)[n] = '\0';
 			DBG("forthwrite n %d buf %s\n", n, buf);
@@ -1030,12 +1030,33 @@
 	return rv;
 }
 
+/* sets up the forth environment for the forth init */
+void
+forthinit(void)
+{
+	Proc *p;
+
+	p = up;
+	if(p == nil)
+		panic("goforth up == nil\n");
+
+	p->fmem = mallocalign(FORTHHEAPSIZE, BY2PG, 0, 0);
+	if(p->fmem == nil)
+		panic("goforth p->fmem == nil\n");
+
+	/* store the start address at that address too - magic check */
+	((intptr*)p->fmem)[0] = (intptr)p->fmem;	/* heap start */
+	((intptr*)p->fmem)[1] = (intptr)p->fmem+FORTHHEAPSIZE-1; /* heap end */
+
+	p->fisgo = 1;
+}
+
 Dev forthdevtab = {
 	'f',
 	"forth",
 
 	devreset,
-	devinit,
+	forthinit,
 	devshutdown,
 	forthattach,
 	forthwalk,
--- a/os/port/devmnt.c
+++ b/os/port/devmnt.c
@@ -1015,6 +1015,7 @@
 	int n;
 
 	while(waserror()) {
+print("mountio waserror() at the start pid %ud\n", up->pid);
 		if(m->rip == up)
 			mntgate(m);
 		if(strcmp(up->env->errstr, Eintr) != 0 || waserror()){
--- a/os/port/portdat.h
+++ b/os/port/portdat.h
@@ -763,7 +763,7 @@
 	void	*fmem;			/* forth process memory - sandboxed except for macro primitives */
 	void	*shm;		/* for devshm. move this into Osenv or maybe remove Osenv at some point to get more in sync with 9front */
 	void	*canread;	/* for devready.c */
-	u8		fstarted;	/* 0 while waiting for the pctl message */
+	u8		fisgo;	/* 0 while waiting for the pctl message */
 };
 
 enum
--- a/os/port/portfns.h
+++ b/os/port/portfns.h
@@ -122,6 +122,7 @@
 void		getcolor(u32, u32*, u32*, u32*);
 uintptr	getmalloctag(void*);
 uintptr	getrealloctag(void*);
+void	goforth(void *);
 void		gotolabel(Label*);
 char*		getconfenv(void);
 void		halt(void);
--- a/os/port/proc.c
+++ b/os/port/proc.c
@@ -717,6 +717,12 @@
 		panic("pidalloc");
 	/* addprog(p); no more dis */
 
+	/* TODO should we do this just user forth procs or does it need to be done for kproc's too */
+	memset(p->time, 0, sizeof(p->time));
+	p->time[TReal] = MACHP(0)->ticks;
+	cycles(&p->kentry);
+	p->pcycles = -p->kentry;
+
 	return p;
 }
 
--- a/os/port/sysfile.c
+++ b/os/port/sysfile.c
@@ -983,7 +983,7 @@
 	s64 off;
 
 	if(waserror()){
-		print("rread fd %d p 0x%p n %d offp %lld fdtochan failed: %r\n", fd, p, n, offp);
+		print("rread fd %d p 0x%p n %d fdtochan failed: %r\n", fd, p, n);
 		return -1;
 	}
 
@@ -990,7 +990,7 @@
 	c = fdtochan(up->env->fgrp, fd, OREAD, 1, 1);
 
 	if(waserror()){
-		print("rread fd %d p 0x%p n %d offp %lld fdtochan failed: %r\n", fd, p, n, offp);
+		print("rread fd %d p 0x%p n %d fdtochan failed: %r\n", fd, p, n);
 		cclose(c);
 		nexterror();
 	}
@@ -1052,7 +1052,23 @@
 s32
 kread(int fd, void *va, s32 n)
 {
-	return rread(fd, va, n, nil);
+	s32 rv /*, i*/;
+
+/* print("kread pid %d fd %d va 0x%p n %d\n", up->pid, fd, va, n);
+	if(up->pid == 28){
+		print("kread: started ");
+	}*/
+	rv = rread(fd, va, n, nil);
+/*	if(up->pid == 28){
+		i=rv;
+		print("returned: rv %d -", rv);
+		while(i-- > 0){
+			print("%c", ((char*)va)[i]);
+		}
+		print("-\n");
+	} */
+	/* return rread(fd, va, n, nil); */
+	return rv;
 }
 
 s32
--- a/tests/fthclose.sh
+++ b/tests/fthclose.sh
@@ -1,7 +1,7 @@
 #!/dis/sh
 
 load std
-echo my pid is ${pid}
+echo starting fthclose.sh pid ${pid}
 
 #{echo testing; sleep 1; echo exiting} | { cat - ; echo exiting the receiver }
 bind '#|' /n/ff
binary files a/tests/fthtests.sh b/tests/fthtests.sh differ
--- a/usr/inferno/lib/profile
+++ b/usr/inferno/lib/profile
@@ -5,4 +5,7 @@
 cp /locale/US_Arizona /locale/timezone
 
 echo running forth tests
+ns
+/n/remote/tests/fthclose.sh
 /n/remote/tests/fthtests.sh
+#/n/remote/tests/fthshm.sh
--- a/usr/inferno/namespace
+++ b/usr/inferno/namespace
@@ -2,5 +2,5 @@
 # /chan/foo is more natural than '#s'/foo
 bind -b '#s' /chan
 
-bind -b /usr/inferno/dis /dis
-bind -b /usr/inferno/module /module
+#bind -b /usr/inferno/dis /dis
+#bind -b /usr/inferno/module /module