MTRR: Ensure size of MTRR range is aligned to the closest power of 2
diff --git a/Makefile b/Makefile
index 265cc974..b023610e 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,6 @@ all: stage2 decompressor
clean: stage2-clean decompressor-clean
rm -f stage2/stage2.bin.gz
- #rm -f limine-install
stage2:
$(MAKE) -C stage2 all
@@ -36,16 +35,16 @@ test.img:
parted -s test.img mklabel msdos
parted -s test.img mkpart primary 2048s 100%
-echfs-test: limine-install test.img
+echfs-test: test.img
$(MAKE) -C test
echfs-utils -m -p0 test.img quick-format 512
echfs-utils -m -p0 test.img import test/test.elf boot/test.elf
echfs-utils -m -p0 test.img import test/limine.cfg limine.cfg
echfs-utils -m -p0 test.img import test/bg.bmp bg.bmp
./limine-install limine.bin test.img
- qemu-system-x86_64 -net none -smp 4 -hda test.img -debugcon stdio -enable-kvm
+ qemu-system-x86_64 -net none -smp 4 -hda test.img -debugcon stdio
-ext2-test: limine-install test.img
+ext2-test: test.img
$(MAKE) -C test
rm -rf test_image/
mkdir test_image
@@ -61,9 +60,9 @@ ext2-test: limine-install test.img
sudo losetup -d `cat loopback_dev`
rm -rf test_image loopback_dev
./limine-install limine.bin test.img
- qemu-system-x86_64 -hda test.img -debugcon stdio
+ qemu-system-x86_64 -hda test.img -smp 4 -debugcon stdio
-fat32-test: limine-install test.img
+fat32-test: test.img
$(MAKE) -C test
rm -rf test_image/
mkdir test_image
@@ -79,4 +78,4 @@ fat32-test: limine-install test.img
sudo losetup -d `cat loopback_dev`
rm -rf test_image loopback_dev
./limine-install limine.bin test.img
- qemu-system-x86_64 -hda test.img -debugcon stdio
+ qemu-system-x86_64 -hda test.img -smp 4 -debugcon stdio
diff --git a/limine.bin b/limine.bin
index 35db061c..af965851 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/mm/mtrr.c b/stage2/mm/mtrr.c
index 5f90c4e3..dfc333ba 100644
--- a/stage2/mm/mtrr.c
+++ b/stage2/mm/mtrr.c
@@ -30,13 +30,25 @@ static bool is_block_in_mtrr_range(struct mtrr *mtrr, uint64_t block_base, uint6
bool mtrr_set_range(uint64_t base, uint64_t size, uint8_t memory_type) {
uint32_t eax, ebx, ecx, edx;
- cpuid(0x80000008, 0, &eax, &ebx, &ecx, &edx);
+ if (cpuid(0x80000008, 0, &eax, &ebx, &ecx, &edx))
+ return false;
+
uint8_t maxphysaddr = eax & 0xff;
print("mtrr: Max phys addr: %u\n", maxphysaddr);
base = ALIGN_DOWN(base, 0x1000);
+
+ // Size must be aligned on a power of 2 (this is a slow method but this is
+ // not time sensitive)
+ for (uint64_t aligned_size = 1; ; aligned_size *= 2) {
+ if (aligned_size >= size) {
+ size = aligned_size;
+ break;
+ }
+ }
+
size = ALIGN_UP(size, 0x1000);
- uint64_t mask = (((uint64_t)1 << maxphysaddr) - 1) & ~((uint64_t)size - 1);
+ uint64_t mask = (((uint64_t)1 << maxphysaddr) - 1) & ~(size - 1);
print("mtrr: Base: %X Mask: %X\n", base, mask);
