shithub: riscv

Download patch

ref: 3b36daa2bb8c0f4169455baf829b9695b520c5fc
parent: 157d7ebdbd7479b271e7b1df744b2dfc6b5816e9
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon May 13 15:12:41 EDT 2019

bcm, bcm64: preserve memsize across reboots, avoid trashing atags while parsing cmdline

we override atag memory on reboot, so preserve
the memsize learned from atag as *maxmem plan9
variable. the global memsize variable is not
needed anymore.

avoid trashing the following atag when zero
terminating the cmdline string.

zero memory after plan9.ini variables.

--- a/sys/src/9/bcm/bootargs.c
+++ b/sys/src/9/bcm/bootargs.c
@@ -88,34 +88,37 @@
 void
 bootargsinit(void)
 {
+	static char maxmem[11];
+	char x, *e;
 	Atag *a;
-	int n;
 
-	a = (Atag*)BOOTARGS;
+	e = BOOTARGS;
+	a = (Atag*)e;
 	if(a->tag != AtagCore){
-		plan9iniinit((char*)a, 0);
+		plan9iniinit(e, 0);
 		return;
 	}
-	while(a->tag != AtagNone && a->size != 0){
+	while(a->tag != AtagNone){
+		e += a->size * sizeof(u32int);
+		if(a->size < 2 || e < (char*)a || e > &BOOTARGS[BOOTARGSLEN])
+			break;
 		switch(a->tag){
 		case AtagMem:
-			/* use only first bank */
-			if(conf.mem[0].limit == 0 && a->mem.size != 0){
-				memsize = a->mem.size;
-				conf.mem[0].base = a->mem.base;
-				conf.mem[0].limit = a->mem.base + memsize;
+			if(findconf("*maxmem") < 0){
+				snprint(maxmem, sizeof(maxmem), "%ud", a->mem.base+a->mem.size);
+				addconf("*maxmem", maxmem);
 			}
 			break;
 		case AtagCmdline:
-			n = (a->size * sizeof(u32int)) - offsetof(Atag, cmdline[0]);
-			if(a->cmdline + n < BOOTARGS + BOOTARGSLEN)
-				a->cmdline[n] = 0;
-			else
-				BOOTARGS[BOOTARGSLEN-1] = 0;
+			x = *e;
+			*e = 0;
 			plan9iniinit(a->cmdline, 1);
+			*e = x;
 			break;
 		}
-		a = (Atag*)((u32int*)a + a->size);
+		if(e > &BOOTARGS[BOOTARGSLEN-8])
+			break;
+		a = (Atag*)e;
 	}
 }
 
@@ -164,6 +167,7 @@
 	if(n >= BOOTARGSLEN)
 		error("kernel configuration too large");
 	memmove(BOOTARGS, p, n);
+	memset(BOOTARGS+n, 0, BOOTARGSLEN-n);
 	poperror();
 	free(p);
 }
--- a/sys/src/9/bcm/dat.h
+++ b/sys/src/9/bcm/dat.h
@@ -227,7 +227,6 @@
 extern register Proc* up;			/* R9 */
 extern uintptr kseg0;
 extern Mach* machaddr[MAXMACH];
-extern ulong memsize;
 extern int normalprint;
 
 /*
--- a/sys/src/9/bcm/main.c
+++ b/sys/src/9/bcm/main.c
@@ -18,7 +18,6 @@
 uintptr kseg0 = KZERO;
 Mach*	machaddr[MAXMACH];
 Conf	conf;
-ulong	memsize = 128*1024*1024;
 
 void
 machinit(void)
@@ -241,7 +240,7 @@
 confinit(void)
 {
 	int i, userpcnt;
-	ulong kpages;
+	ulong kpages, memsize = 0;
 	uintptr pa;
 	char *p;
 
@@ -257,12 +256,10 @@
 	else
 		userpcnt = 0;
 
-	if((p = getconf("*maxmem")) != nil){
+	if(p = getconf("*maxmem"))
 		memsize = strtoul(p, 0, 0) - PHYSDRAM;
-		if (memsize < 16*MB)		/* sanity */
-			memsize = 16*MB;
-	}
-
+	if (memsize < 16*MB)		/* sanity */
+		memsize = 16*MB;
 	getramsize(&conf.mem[0]);
 	if(conf.mem[0].limit == 0){
 		conf.mem[0].base = PHYSDRAM;
--- a/sys/src/9/bcm/mmu.c
+++ b/sys/src/9/bcm/mmu.c
@@ -304,8 +304,8 @@
 uintptr
 cankaddr(uintptr pa)
 {
-	if(pa < PHYSDRAM + memsize)		/* assumes PHYSDRAM is 0 */
-		return PHYSDRAM + memsize - pa;
+	if(pa < PHYSDRAM+soc.dramsize)
+		return PHYSDRAM+soc.dramsize - pa;
 	return 0;
 }