shithub: drawcpu

Download patch

ref: b0a436ba688da0ecc7c224ecfdf94287a01b1107
parent: 013b71c36516a6feddf4a9c792dd9fe2f7f1f475
author: halfwit <michaelmisch1985@gmail.com>
date: Tue Jan 6 11:44:05 EST 2026

Slight modifications to try to fix fwstat

--- a/include/u.h
+++ b/include/u.h
@@ -29,9 +29,9 @@
 #undef iounit
 #undef wait
 
-//#define DSEGM 1     /* Debug segments */
-//#define DINSTR 1    /* Debug Arm instructions */
-//#define DSYSCALL 1  /* Debug syscalls */
-//#define DTRACE 1    /* Debug program flow */
-//#define DVADDR  1    /* Debug vaddr calls */
-//#define DREGS 1     /* Debug registers */
+//#define DSEGM     1   /* Debug segments */
+//#define DINSTR    1   /* Debug Arm instructions */
+//#define DSYSCALL  1   /* Debug syscalls */
+//#define DTRACE    1   /* Debug program flow */
+//#define DVADDR    1   /* Debug vaddr calls */
+//#define DREGS     1   /* Debug registers */
--- a/kern/arm.c
+++ b/kern/arm.c
@@ -33,7 +33,7 @@
 void
 invalid(u32int instr)
 {
-    print("%s: undefined instruction %.8ux @ %.8ux", (char*)up->arg, instr, up->R[15] - 4);
+    print("%s: undefined instruction %.8ux @ %.8ux pid=%d", (char*)up->arg, instr, up->R[15] - 4, up->pid);
     pexit("invalid", 1);
 }
 
@@ -175,8 +175,7 @@
     Rd = up->R + ((instr >> 12) & 15);
     Rn = up->R + ((instr >> 16) & 15);
 #ifdef DINSTR
-    if(DEBUG&INSTR)
-        print("swap: Rm=%d Rd=%d Rn=%d pid=%d\n", (instr & 15), ((instr >> 12) & 15), ((instr >> 16) & 15), up->pid);
+    print("swap: Rm=%d Rd=%d Rn=%d pid=%d\n", (instr & 15), ((instr >> 12) & 15), ((instr >> 16) & 15), up->pid);
 #endif
     if(Rm == up->R + 15 || Rd == up->R + 15 || Rn == up->R + 15)
         invalid(instr);
@@ -425,7 +424,7 @@
     u32int *RdH, *RdL, *Rs, *Rm;
     u64int res;
 #ifdef DINSTR
-    print("multiplylong\n");
+    print("multiplylong pid=%d\n", up->pid);
 #endif
     Rm = up->R + (instr & 15);
     Rs = up->R + ((instr >> 8) & 15);
@@ -488,7 +487,7 @@
 clrex(void)
 {
 #ifdef DINSTR
-    print("clrex\n");
+    print("clrex: pid=%d\n", up->pid);
 #endif
     up->lladdr = 0;
     up->llval = 0;
@@ -498,7 +497,7 @@
 barrier(void)
 {
 #ifdef DINSTR
-    print("barrier\n");
+    print("barrier: pid=%d\n", up->pid);
 #endif
     static Lock l;
     lock(&l);
--- a/kern/dat.h
+++ b/kern/dat.h
@@ -348,6 +348,7 @@
 struct Rgrp
 {
 	Ref ref;
+	Lock 	lk;
 	Proc	*rendhash[RENDHASH];	/* Rendezvous tag hash */
 };
 
@@ -378,7 +379,7 @@
 	int	nfd;			/* number allocated */
 	int	maxfd;			/* highest fd in use */
 	int	exceed;			/* debugging */
-	RWLock lk;
+	Lock lk;
 };
 
 enum
--- a/kern/devproc.c
+++ b/kern/devproc.c
@@ -178,22 +178,22 @@
 		error(Eprocdied);
 
 	incref(&f->ref);
-	lock(&f->lk.lk);
+	lock(&f->lk);
 	while(fd <= f->maxfd){
 		c = f->fd[fd];
 		if(c != nil){
 			f->fd[fd] = nil;
-			unlock(&f->lk.lk);
+			unlock(&f->lk);
 			qunlock(&p->debug);
 			cclose(c);
 			qlock(&p->debug);
-			lock(&f->lk.lk);
+			lock(&f->lk);
 		}
 		if(!all)
 			break;
 		fd++;
 	}
-	unlock(&f->lk.lk);
+	unlock(&f->lk);
 	closefgrp(f);
 }
 
--- a/kern/pgrp.c
+++ b/kern/pgrp.c
@@ -140,36 +140,42 @@
 	Chan *c;
 	int i;
 
-	new = smalloc(sizeof(Fgrp));
+	new = malloc(sizeof(Fgrp));
+	if(new == nil)
+		error(Enomem);
+	new->ref.ref = 1;
 	if(f == nil){
-		new->fd = smalloc(DELTAFD*sizeof(Chan*));
 		new->nfd = DELTAFD;
-		new->ref.ref = 1;
+		new->fd = malloc(DELTAFD*sizeof(new->fd[0]));
+		if(new->fd == nil){
+			free(new->fd);
+			free(new);
+			error(Enomem);
+		}
 		return new;
 	}
 
-	lock(&f->ref.lk);
+	lock(&f->lk);
 	/* Make new fd list shorter if possible, preserving quantization */
 	new->nfd = f->maxfd+1;
 	i = new->nfd%DELTAFD;
 	if(i != 0)
 		new->nfd += DELTAFD - i;
-	new->fd = malloc(new->nfd*sizeof(Chan*));
+	new->fd = malloc(new->nfd*sizeof(new->fd[0]));
 	if(new->fd == nil){
-		unlock(&f->ref.lk);
+		unlock(&f->lk);
+		free(new->fd);
 		free(new);
-		error("no memory for fgrp");
+		error(Enomem);
 	}
-	new->ref.ref = 1;
-
 	new->maxfd = f->maxfd;
 	for(i = 0; i <= f->maxfd; i++) {
 		if((c = f->fd[i]) != nil){
-			incref(&c->ref);
 			new->fd[i] = c;
+			incref(&c->ref);
 		}
 	}
-	unlock(&f->ref.lk);
+	unlock(&f->lk);
 
 	return new;
 }
--- a/kern/seg.c
+++ b/kern/seg.c
@@ -64,11 +64,14 @@
 vaddr(u32int addr, u32int len, Segment **seg)
 {
 	Segment **ss, *s;
+#ifdef DVADDR
 	int count = 0;
-	
+#endif
 	qlock(&up->seglock);
 	for (ss = up->seg; ss < up->seg + NSEG; ss++) {
+#ifdef DVADDR
 		count++;
+#endif
 		if (*ss == nil)
 			continue;
 		s = *ss;
--- a/kern/syscall.c
+++ b/kern/syscall.c
@@ -73,7 +73,7 @@
 	namet = copyifnec(name, -1, &copied);
 	fd = open(namet, flags);
 #ifdef DSYSCALL
-	print("sysopen: fd=%lux name=%s flags=%lux\n", fd, namet, flags);
+	print("sysopen: pid=%d, fd=%lux name=%s flags=%lux\n", up->pid, fd, namet, flags);
 #endif
 	if(copied)
 		free(namet);
@@ -99,7 +99,7 @@
 	namet = copyifnec(name, -1, &copied);
 	fd = create(namet, flags, perm);
 #ifdef DSYSCALL
-	print("syscreate: fd=%d name=%s\n", fd, namet);
+	print("syscreate: pid=%d, fd=%d name=%s\n", up->pid, fd, namet);
 #endif
 	if(copied)
 		free(namet);
@@ -119,7 +119,7 @@
 	
 	fd = arg(0);
 #ifdef DSYSCALL
-	print("sysclose: fd=%lux\n", fd);
+	print("sysclose: pid=%d, fd=%lux\n",up->pid, fd);
 #endif
 	up->R[0] = noteerr(close(fd), 0);
 	if((fd & (1<<31)) == 0)
@@ -139,7 +139,7 @@
 	size = arg(2);
 	off = argv(3);
 #ifdef DSYSCALL
-	print("syspread: fd=%d size=0x%lux off=0x%lux\n", fd, size, off);
+	print("syspread: pid=%d, fd=%d size=0x%lux off=0x%lux\n", up->pid, fd, size, off);
 #endif
 	targ = bufifnec(buf, size, &buffered);
 	up->R[0] = noteerr(pread(fd, targ, size, off), size);
@@ -161,7 +161,7 @@
 	off = argv(3);
 	buft = copyifnec(buf, size, &copied);
 #ifdef DSYSCALL
-	print("syspwrite: fd=%ux buf=%s size=%lux off=%lux\n", fd, buft, size, off);
+	print("syspwrite: pid=%d, fd=%ux buf=%s size=%lux off=%lux\n", up->pid, fd, buft, size, off);
 #endif
 	up->R[0] = noteerr(pwrite(fd, buft, size, off), size);
 	if(copied)
@@ -180,7 +180,7 @@
 	n = argv(2);
 	type = arg(4);
 #ifdef DSYSCALL
-	print("sysseek: to=%d whence=%lux\n", n, type);
+	print("sysseek: pid=%d, to=%d whence=%lux\n", up->pid, n, type);
 #endif
 	*ret = seek(fd, n, type);
 	if(*ret < 0) noteerr(0, 1);
@@ -220,7 +220,7 @@
     nedir = arg(2);
 	edirt = bufifnec(edir, nedir, &buffered);
 #ifdef DSYSCALL
-	print("sysstat: edir=%s, nedir=%d\n", edirt, nedir);
+	print("sysstat: pid=%d, name=%s, edir=%s, nedir=%d\n", up->pid, namet, edirt, nedir);
 #endif
     up->R[0] = noteerr(stat(namet, edirt, nedir), nedir);
     if (copied)
@@ -241,7 +241,7 @@
 	nedir = arg(2);
 	edirt = bufifnec(edir, nedir, &buffered);
 #ifdef DSYSCALL
-	print("sysfstat: edir=%s, nedir=%d\n", edirt, nedir);
+	print("sysfstat: pid=%d edir=%s, nedir=%d\n", up->pid, edirt, nedir);
 #endif
 	up->R[0] = noteerr(fstat(fd, edirt, nedir), nedir);
 	if(buffered)
@@ -262,7 +262,7 @@
 	nedir = arg(2);
 	edirt = copyifnec(edir, nedir, &copied2);
 #ifdef DSYSCALL
-	print("syswstate dir=%s, name=%s\n", edirt, namet);
+	print("syswstate: pid=%d, dir=%s, name=%s\n", up->pid, edirt, namet);
 #endif
 	up->R[0] = noteerr(wstat(namet, edirt, nedir), nedir);
 	if(copied)
@@ -281,10 +281,9 @@
 	fd = arg(0);
 	edir = arg(1);
 	nedir = arg(2);
-	
 	edirt = copyifnec(edir, nedir, &copied);
 #ifdef DSYSCALL
-	print("sysfwstat: fd=0x%ux edir=%us nedir=%d copied=%d\n", fd, edirt, nedir, copied);
+	print("sysfwstat: pid=%d, fd=%d, edir=%us, nedir=%d, copied=%d\n", up->pid, fd, edirt, nedir, copied);
 #endif
 	up->R[0] = noteerr(fwstat(fd, edirt, nedir), nedir);
 	if(copied)
@@ -296,12 +295,12 @@
 {
 	if(arg(0) == 0) {
 #ifdef DSYSCALL
-		print("sysexits\n");
+		print("sysexits: pid=%d\n", up->pid);
 #endif
 		pexit("", 0);
 	} else {
 #ifdef DSYSCALL
-		print("sysexits: %s\n", vaddrnol(arg(0), 0));
+		print("sysexits: pid=%d, status=%s\n", up->pid, vaddrnol(arg(0), 0));
 #endif
 		pexit(vaddrnol(arg(0), 0), 0);		
 	}
@@ -317,7 +316,7 @@
 	v = arg(0);
 	v = v + 7 & -8;
 #ifdef DSYSCALL
-	print("sysbrk v=%#lux\n", v);
+	print("sysbrk: pid=%d v=%#lux\n", up->pid, v);
 #endif
 	if(v >= up->seg[ESEG]->start)
 		panic("bss > stack, wtf?");
@@ -358,7 +357,7 @@
 	if(copied)
 		copyback(src, len, srct);
 #ifdef DSYSCALL
-	print("syserrstr buf=%s\n", buf);
+	print("syserrstr: pid=%d, buf=%s\n", up->pid, buf);
 #endif
 	up->R[0] = 0;
 }
@@ -373,7 +372,7 @@
 	dir = arg(0);
 	dirt = copyifnec(dir, -1, &copied);
 #ifdef DSYSCALL
-	print("syschdir\n");
+	print("syschdir: pid=%d\n", up->pid);
 #endif
 	up->R[0] = noteerr(chdir(dirt), 0);
 	if(copied)
@@ -385,7 +384,7 @@
 {
 	u32int handler;
 #ifdef DSYSCALL
-	print("sysnotify\n");
+	print("sysnotify: pid=%d\n", up->pid);
 #endif
 	handler = arg(0);
 	up->notehandler = handler;
@@ -399,7 +398,7 @@
 	
 	v = arg(0);
 #ifdef DSYSCALL
-	print("sysnoted\n");
+	print("sysnoted: pid=%d\n", up->pid);
 #endif
 	if(up->innote)
 		longjmp(up->notejmp, v + 1);
@@ -433,7 +432,7 @@
 	segunlock(seg1);
 	argvt = vaddr(argv, 0, &seg1);
 #ifdef DSYSCALL
-	print("sysexec(%#ux=\"%s\", %#ux)\n", name, namet, argv);
+	print("sysexec(%#ux=\"%s\", %#ux) pid=%d\n", name, namet, argv, up->pid);
 #endif
 	for(argc = 0; argvt[argc]; argc++)
 		;
@@ -455,6 +454,90 @@
 }
 
 static void
+_syssegbrk(void)
+{
+    uintptr newbrk;
+    Segment *s;
+    uintptr base, oldbrk, oldsize, newsize;
+    Ref *newdref;
+
+    newbrk = arg(0);  /* requested new break address */
+
+    /* Case: segbrk(0) → return current break (like sbrk(0)) */
+    if(newbrk == 0) {
+        s = up->seg[BSEG];
+        if(s == nil || s->start == 0) {
+            werrstr("no data segment");
+            up->R[0] = (int)-1;
+            return;
+        }
+        up->R[0] = s->start + s->size;
+        return;
+    }
+
+    /*
+     * Find the segment containing the address 'newbrk'.
+     * We reuse vaddr() with len=1 — it locks the segment if needed.
+     */
+    s = nil;
+    vaddr(newbrk, 1, &s);  /* will panic on segfault, which is fine */
+
+    /* Now s is valid and properly locked (rlock if SEGFLLOCK) */
+
+    /* Only allow growing BSS or attached memory segments */
+    if((s->type & SG_TYPE) != SG_BSS && s->type != SG_PHYSICAL) {
+        segunlock(s);
+        werrstr("segment not growable");
+        up->R[0] = (int)-1;
+        return;
+    }
+
+    base = s->start;
+    oldbrk = base + s->size;
+    oldsize = s->size;
+
+    /* Must be trying to grow (or stay the same) */
+    if(newbrk < oldbrk) {
+        segunlock(s);
+        werrstr("shrinking not supported");
+        up->R[0] = (int)-1;
+        return;
+    }
+
+    if(newbrk == oldbrk) {
+        /* No change — just return current break */
+        up->R[0] = oldbrk;
+        segunlock(s);
+        return;
+    }
+
+    /* Round up to 8-byte alignment (like your original _sysbrk) */
+    newsize = (newbrk - base + 7) & ~7;
+
+    /* Reallocate the data buffer */
+    newdref = realloc(s->dref, newsize + sizeof(Ref));
+    if(newdref == nil) {
+        segunlock(s);
+        werrstr("out of memory");
+        up->R[0] = (int)-1;
+        return;
+    }
+
+    /* Zero the new portion */
+    memset((char*)(newdref + 1) + oldsize, 0, newsize - oldsize);
+
+    /* Update segment */
+    s->dref = newdref;
+    s->data = newdref + 1;
+    s->size = newsize;
+
+    /* Success: return old break */
+    up->R[0] = oldbrk;
+
+    segunlock(s);
+}
+
+static void
 _sysawait(void)
 {
 	u32int s, n;
@@ -465,7 +548,7 @@
 	n = arg(1);
 	st = bufifnec(s, n, &buffered);
 #ifdef DSYSCALL
-	print("sysawait\n");
+	print("sysawait: pid=%d\n", up->pid);
 #endif
 	up->R[0] = noteerr(await(st, n), 0);
 	if(buffered)
@@ -481,7 +564,7 @@
 	fd = arg(0);
 	fdt = bufifnec(fd, 8, &buffered);
 #ifdef DSYSCALL
-	print("syspipe\n");
+	print("syspipe: pid=%d\n", up->pid);
 #endif
 	up->R[0] = noteerr(pipe((int *) fdt), 0);
 	if(buffered)
@@ -496,7 +579,7 @@
 	oldfd = arg(0);
 	newfd = arg(1);
 #ifdef DSYSCALL
-	print("sysdup\n");
+	print("sysdup: pid=%d\n", up->pid);
 #endif
 	up->R[0] = noteerr(dup(oldfd, newfd), 0);
 }
@@ -508,7 +591,7 @@
 	
 	n = arg(0);
 #ifdef DSYSCALL
-	print("syssleep: nsec=%.8ux\n", n);
+	print("syssleep: pid=%d nsec=%.8ux\n", up->pid, n);
 #endif
 	osmsleep(n);
 	up->R[0] = 0;
@@ -522,7 +605,7 @@
 	tag = arg(0);
 	value = arg(1);
 #ifdef DSYSCALL
-	print("sysrendezvous: tag=%.8ux, value=%.8ux\n", tag, value);
+	print("sysrendezvous: pid=%d, tag=%.8ux, value=%.8ux\n", up->pid, tag, value);
 #endif
 	up->R[0] = (u32int)(uintptr_t)(rendezvous((void*)(uintptr_t)tag, (void*)(uintptr_t)value));
 	if(up->R[0] == ~0)
@@ -547,13 +630,13 @@
 		if(!anamet) 
 			anamet = "";
 #ifdef DSYSCALL
-		print("sysmount: aname=%s\n", anamet);
+		print("sysmount: pid=%d, fd=%d, afd=%d, aname=%s\n", up->pid, fd, afd, anamet);
 #endif
 	} else {
 		anamet = nil;
 		copiedaname = 0;
 #ifdef DSYSCALL
-		print("sysmount\n");
+		print("sysmount: pid=%d, fd=%d, afd=%d\n", fd, afd, up->pid);
 #endif
 	}
 	up->R[0] = noteerr(mount(fd, afd, oldt, flag, anamet), 0);
@@ -576,7 +659,7 @@
 	namet = copyifnec(name, -1, &copiedname);
 	oldt = copyifnec(old, -1, &copiedold);
 #ifdef DSYSCALL
-	print("sysbind: name=%s, old=%s\n", namet, oldt);
+	print("sysbind: pid=%d, name=%s, old=%s\n", up->pid, namet, oldt);
 #endif
 	up->R[0] = noteerr(bind(namet, oldt, flags), 0);
 	if(copiedname)
@@ -596,7 +679,7 @@
 	old = arg(1);
 	oldt = copyifnec(old, -1, &copiedold);
 #ifdef DSYSCALL
-	print("sysunmount: name=%s, old=%s\n", name, oldt);
+	print("sysunmount: pid=%d, name=%s, old=%s\n", up->pid, name, oldt);
 #endif
 	if(name == 0) {
 		namet = nil;
@@ -640,7 +723,7 @@
 		nexterror();
 	}
 #ifdef DSYSCALL
-	print("sysfauth\n");
+	print("sysfauth: pid=%d\n", up->pid);
 #endif
 	ac = mntauth(c, aname);
 	/* at this point ac is responsible for keeping c alive */
@@ -668,7 +751,7 @@
 	file = arg(0);
 	filet = copyifnec(file, -1, &copied);
 #ifdef DSYSCALL
-	print("sysremove: file=%s\n", filet);
+	print("sysremove: pid=%d, file=%s\n", up->pid, filet);
 #endif
 	up->R[0] = noteerr(remove(filet), 0);
 	if(copied)
@@ -682,7 +765,7 @@
 	
 	msec = arg(0);
 #ifdef DSYSCALL
-	print("sysalarm: msec=%.8ux\n", msec);
+	print("sysalarm: pid=%d, msec=%.8ux\n", up->pid, msec);
 #endif
 	up->R[0] = alarm(msec);
 }
@@ -697,7 +780,7 @@
 	block = arg(1);
 	addrt = vaddrnol(addr, 4);
 #ifdef DSYSCALL
-	print("syssemacquire: addr=%s, block=%.8ux\n", addrt, block);
+	print("syssemacquire: pid=%d, addr=%s, block=%.8ux\n", up->pid, addrt, block);
 #endif
 	up->R[0] = noteerr(semacquire(addrt, block), 0);
 }
@@ -713,7 +796,7 @@
 	count = arg(1);
 	addrt = vaddr(addr, 4, &seg);
 #ifdef DSYSCALL
-		print("syssemrelease: addr=%s, count=%.8ux\n", addrt, count);
+		print("syssemrelease: pid=%d, addr=%s, count=%.8ux\n", up->pid, addrt, count);
 #endif
 	up->R[0] = noteerr(semrelease(addrt, count), 0);
 	segunlock(seg);
@@ -737,7 +820,7 @@
 		[_FSESSION]		= _sys_fsession,
 		[FAUTH]			= _sysfauth,
 		//[_FSTAT]		= _sys_fstat,
-		//[SEGBRK]			= _syssegbrk,
+		[SEGBRK]		= _syssegbrk,
 		//[_MOUNT]		= _sys_mount,
 		[OPEN]			= _sysopen,
 		//[_READ]			= _sys_read,
--- a/kern/sysfile.c
+++ b/kern/sysfile.c
@@ -131,6 +131,54 @@
 	return 0;
 }
 
+static char*
+pathlast(Path *p)
+{
+	char *s;
+
+	if(p == nil)
+		return nil;
+	if(p->len == 0)
+		return nil;
+	s = strrchr(p->s, '/');
+	if(s != nil)
+		return s+1;
+	return p->s;
+}
+
+static char*
+dirname(uchar *p, int *n)
+{
+	p += BIT16SZ+BIT16SZ+BIT32SZ+BIT8SZ+BIT32SZ+BIT64SZ
+		+ BIT32SZ+BIT32SZ+BIT32SZ+BIT64SZ;
+	*n = GBIT16(p);
+	return (char*)p+BIT16SZ;
+}
+
+static long
+dirsetname(char *name, int len, uchar *p, long n, long maxn)
+{
+	char *oname;
+	int olen;
+	long nn;
+
+	if(n == BIT16SZ)
+		return BIT16SZ;
+
+	oname = dirname(p, &olen);
+
+	nn = n+len-olen;
+	PBIT16(p, nn-BIT16SZ);
+	if(nn > maxn)
+		return BIT16SZ;
+
+	if(len != olen)
+		memmove(oname+len, oname+olen, p+n-(uchar*)(oname+olen));
+	PBIT16((uchar*)(oname-2), len);
+	memmove(oname, name, len);
+	return nn;
+}
+
 Chan*
 fdtochan(int fd, int mode, int chkmnt, int iref)
 {
@@ -603,7 +651,8 @@
 _sysstat(char *name, void *buf, long n)
 {
 	Chan *c;
-	uint l;
+	uchar *s;
+	uint l, r;
 
 	l = n;
 	validaddr(buf, l, 1);
@@ -614,6 +663,8 @@
 		nexterror();
 	}
 	l = devtab[c->type]->stat(c, buf, l);
+	if((name = pathlast(c->path)) != nil)
+		r = dirsetname(name, strlen(name), s, r, l);
 	poperror();
 	cclose(c);
 	return l;
--- a/kern/sysproc.c
+++ b/kern/sysproc.c
@@ -266,6 +266,9 @@
     switch(rc = setjmp(up->notejmp) - 1) {
     case -1:
         for(;;) {
+#ifdef DREGS
+			dump();
+#endif
             step();
         }
     case NDFLT:
@@ -570,7 +573,7 @@
 	}
 	else {
 		p->fgrp = up->fgrp;
-		incref(&p->fgrp->ref);
+		incref(&up->fgrp->ref);
 	}
 
 	/* Process groups */
--