Subject: msgbuf_vaddr etc.
To: None <port-i386@netbsd.org>
From: enami tsugutomo <enami@sm.sony.co.jp>
List: port-i386
Date: 11/20/2001 18:19:45
Hi.

I recently found msgbuf_vaddr is initialized twice.  Once in pmap.c
and once in machdep.c.  I guess this means that we are wasting few
pages in kernel virtual space.

If we use the one in stolen in pmap.c, we can initialize msgbuf a bit
earlier.  This allows us to see output from some earlier function like
identifycpu() in msgbuf.  I think this is useful.

If we steal physical pages also in pmap.c, we can initialize msgbuf
before loading physical pages into vm system.  This may be useful if
we're defining DEBUG_MEMLOAD, but system often won't boot when we need
it to debug :-).  So, I'm not sure if this is worth having (especialy
since allocating physical pages other than end of core has some
disadvantage).

enami.
Index: conf/files.i386
===================================================================
RCS file: /h/cvsroot/syssrc/sys/arch/i386/conf/files.i386,v
retrieving revision 1.198
diff -u -r1.198 files.i386
--- conf/files.i386	2001/09/28 02:06:55	1.198
+++ conf/files.i386	2001/11/20 07:57:41
@@ -43,6 +43,7 @@
 
 # The REAL{BASE,EXT}MEM options
 defopt	opt_realmem.h	REALBASEMEM REALEXTMEM
+defopt	DEBUG_MEMLOAD
 
 # understand boot device passed by pre-1.3 bootblocks
 defopt	COMPAT_OLDBOOT
Index: i386/pmap.c
===================================================================
RCS file: /h/cvsroot/syssrc/sys/arch/i386/i386/pmap.c,v
retrieving revision 1.131
diff -u -r1.131 pmap.c
--- i386/pmap.c	2001/11/15 07:03:30	1.131
+++ i386/pmap.c	2001/11/20 08:03:46
@@ -65,6 +65,7 @@
 #include "opt_cputype.h"
 #include "opt_user_ldt.h"
 #include "opt_largepages.h"
+#include "opt_debug_memload.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -817,6 +818,10 @@
 
 	msgbuf_vaddr = virtual_avail;			/* don't need pte */
 	virtual_avail += round_page(MSGBUFSIZE); pte++;
+#ifdef DEBUG_MEMLOAD
+	msgbuf_paddr = avail_start;			/* steal a page */
+	avail_start += round_page(MSGBUFSIZE);
+#endif
 
 	idt_vaddr = virtual_avail;			/* don't need pte */
 	virtual_avail += PAGE_SIZE; pte++;
Index: i386/machdep.c
===================================================================
RCS file: /h/cvsroot/syssrc/sys/arch/i386/i386/machdep.c,v
retrieving revision 1.463
diff -u -r1.463 machdep.c
--- i386/machdep.c	2001/11/20 07:44:17	1.463
+++ i386/machdep.c	2001/11/20 08:13:26
@@ -90,6 +90,7 @@
 #include "opt_realmem.h"
 #include "opt_compat_mach.h"	/* need to get the right segment def */
 #include "opt_mtrr.h"
+#include "opt_debug_memload.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -252,6 +253,7 @@
 void	dumpsys __P((void));
 void	identifycpu __P((struct cpu_info *));
 void	init386 __P((paddr_t));
+void	i386_initmsgbuf __P((void));
 
 #if !defined(REALBASEMEM) && !defined(REALEXTMEM)
 void	add_mem_cluster	__P((u_int64_t, u_int64_t, u_int32_t));
@@ -302,7 +304,7 @@
 {
 	struct cpu_info *ci = curcpu();
 	caddr_t v;
-	int sz, x;
+	int sz;
 	vaddr_t minaddr, maxaddr;
 	vsize_t size;
 	char buf[160];				/* about 2 line */
@@ -310,21 +312,6 @@
 	char cbuf[7];
 	int bigcache, cachesize;
 
-	/*
-	 * Initialize error message buffer (et end of core).
-	 */
-	msgbuf_vaddr = uvm_km_valloc(kernel_map, i386_round_page(MSGBUFSIZE));
-	if (msgbuf_vaddr == 0)
-		panic("failed to valloc msgbuf_vaddr");
-
-	/* msgbuf_paddr was init'd in pmap */
-	for (x = 0; x < btoc(MSGBUFSIZE); x++)
-		pmap_kenter_pa((vaddr_t)msgbuf_vaddr + x * PAGE_SIZE,
-		    msgbuf_paddr + x * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
-	pmap_update(pmap_kernel());
-
-	initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
-
 	printf("%s", version);
 
 	printf("cpu0: %s", cpu_model);
@@ -2628,6 +2615,22 @@
 #endif /* !defined(REALBASEMEM) && !defined(REALEXTMEM) */
 
 void
+i386_initmsgbuf(void)
+{
+	int x;
+
+	/*
+	 * Initialize error message buffer.
+	 */
+	for (x = 0; x < btoc(MSGBUFSIZE); x++)
+		pmap_kenter_pa((vaddr_t)msgbuf_vaddr + x * PAGE_SIZE,
+		    msgbuf_paddr + x * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
+	pmap_update(pmap_kernel());
+
+	initmsgbuf((caddr_t)msgbuf_vaddr, round_page(MSGBUFSIZE));
+}
+
+void
 init386(first_avail)
 	vaddr_t first_avail;
 {
@@ -2691,6 +2694,10 @@
 	 */
 	pmap_bootstrap((vaddr_t)atdevbase + IOM_SIZE);
 
+#ifdef DEBUG_MEMLOAD
+	i386_initmsgbuf();
+#endif
+
 #if !defined(REALBASEMEM) && !defined(REALEXTMEM)
 	/*
 	 * Check to see if we have a memory map from the BIOS (passed
@@ -2932,8 +2939,11 @@
 		}
 	}
 
+#ifndef DEBUG_MEMLOAD
 	/*
-	 * Steal memory for the message buffer (at end of core).
+	 * Steal memory for the message buffer (at end of core
+	 * so that it will remain across reboot with different
+	 * size of kernel).
 	 */
 	{
 		struct vm_physseg *vps;
@@ -2972,7 +2982,10 @@
 		if (sz != reqsz)
 			printf("WARNING: %ld bytes not available for msgbuf "
 			    "in last cluster (%ld used)\n", reqsz, sz);
+
+		i386_initmsgbuf();
 	}
+#endif
 
 #if NBIOSCALL > 0
 	/* install page 2 (reserved above) as PT page for first 4M */