소스 검색

mm: do not hand out highmem that the MTRRs leave uncached

A BIOS short of variable MTRRs on a 3-4 GB board can leave part of the
RAM above the PCI hole non-write-back, and ring-3 heaps come from highmem
first.  mm_init now asks mtrr_mem_type() (effective variable-range type:
UC over WT over WB, MTRRdefType where nothing matches, -1 without usable
MTRRs) for every MB of free RAM, withholds such highmem from the
allocator and logs it.  SYS_MEMINFO gains the free highmem and both
counts, and meminfo prints them.
Johannes Zellner 2 일 전
부모
커밋
682513d8aa
9개의 변경된 파일과 120개의 추가작업 그리고 2개의 파일을 삭제
  1. 8 1
      AGENTS.md
  2. 1 1
      docs/memory.html
  3. 4 0
      include/syscall.h
  4. 46 0
      kernel/mm.c
  5. 3 0
      kernel/mm.h
  6. 39 0
      kernel/mtrr.c
  7. 4 0
      kernel/mtrr.h
  8. 3 0
      kernel/sys_info.c
  9. 12 0
      user/apps/meminfo/ucmd_meminfo.c

+ 8 - 1
AGENTS.md

@@ -384,7 +384,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   is not in BSS: `mm_init` places it in the first lowmem RAM run at or above
   `0x900000` that avoids the boot pack and the LFB and marks its own pages
   used (a 512-byte static array covers the 16 MB no-E820 fallback); COM1
-  logs `mm: bitmap ... highmem free N MB`.  Allocation is lowest address
+  logs `mm: bitmap ... highmem free N MB`.  RAM the firmware's variable
+  MTRRs leave non-write-back is checked per MB (`mtrr_mem_type()` in
+  `mtrr.c`: UC beats WT beats WB, MTRRdefType where nothing matches, -1
+  without usable MTRRs): such highmem is withheld from the allocator and
+  `mm: RAM not write-back by MTRR: N MB highmem (not used), M MB lowmem`
+  is logged; `SYS_MEMINFO`/`meminfo` report both counts and the free
+  highmem.  A ring-3 heap in uncached RAM (the browser's JavaScript) runs
+  many times slower.  Allocation is lowest address
   first within a zone: a per-zone first-free word hint skips the full low
   words, single pages are found with `bsf`, and `mm_alloc_pages()` steps
   over full and empty words 32 pages at a time. `kmalloc.c/h` — a power-of-two heap

+ 1 - 1
docs/memory.html

@@ -104,7 +104,7 @@
 				<code>USER_BASE</code> = <code>0x40000000</code>; <code>paging.c</code>
 				statically asserts the two never overlap. RAM above 1 GB (up to
 				<code>MM_HIGH_LIMIT</code>, 4 GB) is highmem: tracked by the same
-				bitmap, never identity-mapped, used only for ring-3 pages. The VGA shadow reserve is conservative: the
+				bitmap, never identity-mapped, used only for ring-3 pages. Highmem that the firmware's variable MTRRs leave non-write-back (a BIOS short of MTRRs on a 3–4 GB board) is never handed out: <code>mm_init</code> asks <code>mtrr_mem_type()</code> for every MB of free RAM, withholds such highmem, logs <code>mm: RAM not write-back by MTRR</code>, and <code>meminfo</code> shows the counts. Ring-3 code whose heap sits in uncached RAM runs many times slower (a browser's JavaScript heap, for example). The VGA shadow reserve is conservative: the
 				actual buffer is smaller, but the gap keeps a future resolution bump
 				from colliding with allocated pages.
 			</figcaption>

+ 4 - 0
include/syscall.h

@@ -364,6 +364,10 @@ struct sys_meminfo {
 	unsigned int free_pages;
 	unsigned int page_size;
 	unsigned int e820_count;
+	unsigned int high_free_pages;   /* free frames above 1 GB */
+	unsigned int uncached_high_mb;  /* highmem the MTRRs leave non-WB
+					 * (withheld from the allocator) */
+	unsigned int uncached_low_mb;   /* lowmem RAM the MTRRs leave non-WB */
 };
 
 /* One live process, for `top`/`process` (PROC_NAME_LEN = 12).

+ 46 - 0
kernel/mm.c

@@ -4,6 +4,7 @@
 #include "bootinfo.h"
 #include "serial.h"
 #include "vga.h"
+#include "mtrr.h"
 
 /* One bit per 4 KB page; bit set = in use.
  *
@@ -31,6 +32,7 @@ static unsigned int bitmap_bytes;   /* page-rounded size when placed in RAM */
 static unsigned int low_words;      /* bitmap words below MM_MAX_PHYS */
 static unsigned int first_free;     /* lowest lowmem word with a free bit */
 static unsigned int first_free_high; /* same for highmem (>= low_words) */
+unsigned int mm_uncached_high_mb, mm_uncached_low_mb;
 static unsigned int free_pages;
 static unsigned int total_ram_kb;
 
@@ -278,6 +280,50 @@ void mm_init(void)
 	if (bitmap_bytes)
 		mark_used((unsigned int)bitmap, bitmap_bytes);
 
+	/* RAM the firmware's MTRRs leave uncached.  A BIOS with too few
+	 * variable ranges for a 3-4 GB board (the ION) can leave part of the
+	 * RAM above the PCI hole UC; ring-3 heaps come from highmem first, so
+	 * a page load's JavaScript then ran 100x slower.  Such highmem is not
+	 * handed out at all (lowmem still serves user pages); uncached lowmem
+	 * is only reported, the kernel cannot do without it. */
+	{
+		unsigned long long a, lim = bitmap_limit();
+		unsigned int high_uc = 0, low_uc = 0, w;
+
+		if (lim > MM_HIGH_LIMIT)
+			lim = MM_HIGH_LIMIT;
+		for (a = 0x100000ull; a < lim; a += 0x100000ull) {
+			unsigned int w0 = (unsigned int)(a >> 17);
+			int any = 0, t;
+
+			for (w = w0; w < w0 + 8 && w < bitmap_words; w++)
+				if (bitmap[w] != 0xFFFFFFFFu)
+					any = 1;
+			if (!any)
+				continue;       /* no free RAM in this MB */
+			t = mtrr_mem_type((unsigned int)a);
+			if (t < 0)
+				break;          /* no usable MTRRs */
+			if (t == 6)
+				continue;       /* write-back */
+			if (a >= MM_MAX_PHYS) {
+				mark_used(a, 0x100000ull);
+				high_uc++;
+			} else {
+				low_uc++;
+			}
+		}
+		if (high_uc || low_uc) {
+			serial_puts(SERIAL_COM1, "mm: RAM not write-back by MTRR: ");
+			serial_putdec(SERIAL_COM1, high_uc);
+			serial_puts(SERIAL_COM1, " MB highmem (not used), ");
+			serial_putdec(SERIAL_COM1, low_uc);
+			serial_puts(SERIAL_COM1, " MB lowmem\n");
+		}
+		mm_uncached_high_mb = high_uc;
+		mm_uncached_low_mb = low_uc;
+	}
+
 	/* Count free pages. */
 	free_pages = 0;
 	for (i = 0; i < bitmap_words; i++) {

+ 3 - 0
kernel/mm.h

@@ -58,6 +58,9 @@ unsigned int mm_alloc_user_page(void);
 
 /* Free pages above MM_MAX_PHYS (a slow count, for reporting). */
 unsigned int mm_free_high_pages(void);
+/* RAM the firmware's MTRRs leave non-write-back, in MB (mm_init); the
+ * highmem part is never handed out. */
+extern unsigned int mm_uncached_high_mb, mm_uncached_low_mb;
 void mm_free_page(unsigned int addr);
 
 /* Free a page-aligned boot-time reservation (never low 1 MB / the shadow). */

+ 39 - 0
kernel/mtrr.c

@@ -121,6 +121,45 @@ static const char *type_name(unsigned int t)
 	}
 }
 
+/* Effective variable-range MTRR memory type of one physical address below
+ * 4 GB (above the fixed ranges): UC wins over everything, WT over WB, and
+ * MTRRdefType applies where no entry matches.  -1 when the CPU has no MTRRs
+ * or they are disabled (then nothing can be learned from them). */
+int mtrr_mem_type(unsigned int phys)
+{
+	unsigned int lo, hi, mlo, mhi, def, vcnt, i;
+	int type = -1;
+
+	if (!cpu_has_mtrr())
+		return -1;
+	rdmsr(MSR_MTRR_DEF_TYPE, &def, &hi);
+	if (!(def & MTRR_VALID))
+		return -1;
+	rdmsr(MSR_MTRRCAP, &vcnt, &hi);
+	vcnt &= 0xFF;
+	if (vcnt > 16)
+		vcnt = 16;
+	for (i = 0; i < vcnt; i++) {
+		int t;
+
+		rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
+		rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
+		if (!(mlo & MTRR_VALID))
+			continue;
+		/* The address's bits 32+ are zero: a base with high bits
+		 * set under the mask never matches. */
+		if ((hi & mhi) != 0 ||
+		    ((phys ^ lo) & mlo & 0xFFFFF000u) != 0)
+			continue;
+		t = (int)(lo & 0xFF);
+		if (t == 0)
+			return 0;
+		if (type < 0 || (type == 6 && t == 4))
+			type = t;
+	}
+	return type < 0 ? (int)(def & 0xFF) : type;
+}
+
 /* Dump MTRRdefType and the variable entries to serial. */
 static void dump_mtrrs(unsigned int vcnt)
 {

+ 4 - 0
kernel/mtrr.h

@@ -11,4 +11,8 @@
 int mtrr_set_wc(unsigned int base, unsigned int size,
 		unsigned int *wc_base, unsigned int *wc_size);
 
+/* Effective MTRR type of a physical address (0 UC, 1 WC, 4 WT, 5 WP,
+ * 6 WB), or -1 without usable MTRRs. */
+int mtrr_mem_type(unsigned int phys);
+
 #endif

+ 3 - 0
kernel/sys_info.c

@@ -45,6 +45,9 @@ int sys_meminfo(struct sys_meminfo *out)
 	out->free_pages = mm_free_pages();
 	out->page_size = PAGE_SIZE;
 	out->e820_count = mm_e820_count();
+	out->high_free_pages = mm_free_high_pages();
+	out->uncached_high_mb = mm_uncached_high_mb;
+	out->uncached_low_mb = mm_uncached_low_mb;
 	return 0;
 }
 

+ 12 - 0
user/apps/meminfo/ucmd_meminfo.c

@@ -17,6 +17,18 @@ void _start(const char *arg)
 	u_puts(" KB free (");
 	u_putdec(m.free_pages);
 	u_puts(" pages)\n");
+	u_puts("highmem: ");
+	u_putdec(m.high_free_pages * (m.page_size / 1024));
+	u_puts(" KB free\n");
+	if (m.uncached_high_mb || m.uncached_low_mb) {
+		u_puts("not write-back (MTRR): ");
+		u_putdec(m.uncached_high_mb);
+		u_puts(" MB highmem (not used), ");
+		u_putdec(m.uncached_low_mb);
+		u_puts(" MB lowmem\n");
+	} else {
+		u_puts("all RAM is write-back cached\n");
+	}
 
 	if (m.e820_count == 0) {
 		u_puts("E820 map unavailable\n");