git: 9front

Download patch

ref: eb8b9ee63822d9a750a6507af00f33c5faf2d3a7
parent: 0144ea43fd45c49bee2e18e07d455b4938e8040d
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Aug 6 19:20:00 EDT 2025

kernel: leave alignment hole between TEXT and DATA unmapped

We used to pad the TEXT segment to 2MB aligment on amd64,
resulting in the segment going beyond its image file range.

This broke the new pio(), resulting in an endless loop of
zero length reads of the image.

Instead, we now round segment sizes to our effective
page size only and leave the alignment "hole" unmapped.

--- a/sys/src/9/port/fault.c
+++ b/sys/src/9/port/fault.c
@@ -62,9 +62,7 @@
 		if(ask == 0) ask = BY2PG;
 
 		daddr = soff & -ask;
-		if(daddr >= s->flen)
-			ask = 0;
-		else if((daddr+ask) > s->flen)
+		if(daddr+ask > s->flen)
 			ask = s->flen-daddr;
 		paddr = s->base + daddr;
 		daddr += s->fstart;
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -333,7 +333,7 @@
 	char *a, *e, *charp, *file;
 	int i, n, indir;
 	ulong magic, ssize, nargs, nbytes;
-	uintptr t, d, b, entry, text, data, bss, bssend, tstk, align;
+	uintptr entry, text, data, bss, adata, abss, tstk, align;
 	Segment *s, *ts;
 	Image *img;
 	Tos *tos;
@@ -427,15 +427,13 @@
 		cclose(tc);
 	}
 
-	t = (text+align) & ~align;
+	adata = (text+align) & ~align;
 	text -= UTZERO;
 	data = beswal(u.ehdr.data);
 	bss = beswal(u.ehdr.bss);
 	align = BY2PG-1;
-	d = (t + data + align) & ~align;
-	bssend = t + data + bss;
-	b = (bssend + align) & ~align;
-	if(t >= (USTKTOP-USTKSIZE) || d >= (USTKTOP-USTKSIZE) || b >= (USTKTOP-USTKSIZE))
+	abss = (adata + data + align) & ~align;
+	if(adata >= (USTKTOP-USTKSIZE) || abss >= (USTKTOP-USTKSIZE) || (abss+PGROUND(bss)) >= (USTKTOP-USTKSIZE))
 		error(Ebadexec);
 
 	/*
@@ -561,7 +559,7 @@
 
 	/* Attach text segment */
 	/* attachimage returns a locked cache image */
-	img = attachimage(tc, (b-t)>>PGSHIFT);
+	img = attachimage(tc, (PGROUND(text)+PGROUND(data))>>PGSHIFT);
 	if((ts = img->s) != nil && ts->flen == text){
 		assert(ts->image == img);
 		incref(ts);
@@ -571,7 +569,7 @@
 			putimage(img);
 			nexterror();
 		}
-		ts = newseg(SG_TEXT | SG_RONLY, UTZERO, (t-UTZERO)>>PGSHIFT);
+		ts = newseg(SG_TEXT | SG_RONLY, UTZERO, PGROUND(text)>>PGSHIFT);
 		ts->flushme = 1;
 		ts->image = img;
 		ts->fstart = 0;
@@ -610,7 +608,7 @@
 	up->seg[TSEG] = ts;
 
 	/* Data. Shared. */
-	s = newseg(SG_DATA, t, (d-t)>>PGSHIFT);
+	s = newseg(SG_DATA, adata, PGROUND(data)>>PGSHIFT);
 	s->image = img;
 	s->fstart = text;
 	s->flen = data;
@@ -618,7 +616,7 @@
 	up->seg[DSEG] = s;
 
 	/* BSS. Zero fill on demand */
-	up->seg[BSEG] = newseg(SG_BSS, d, (b-d)>>PGSHIFT);
+	up->seg[BSEG] = newseg(SG_BSS, abss, PGROUND(bss)>>PGSHIFT);
 
 	/*
 	 * Move the stack
--