:: commit c3481e12cc38fb74668eeb6adf7364bc4542ad5b

mintsuki <mintsuki@protonmail.com> — 2021-04-09 00:28

parents: 91f0600e6b

mtrr: Wholly remove MTRR support

diff --git a/stage23/entry.s2.c b/stage23/entry.s2.c
index 47003b8c..a4dbb0de 100644
--- a/stage23/entry.s2.c
+++ b/stage23/entry.s2.c
@@ -11,7 +11,6 @@
 #include <fs/file.h>
 #include <lib/elf.h>
 #include <mm/pmm.h>
-#include <mm/mtrr.h>
 #include <protos/stivale.h>
 #include <protos/stivale2.h>
 #include <protos/linux.h>
diff --git a/stage23/entry.s3.c b/stage23/entry.s3.c
index 7df046a6..d8a1c85d 100644
--- a/stage23/entry.s3.c
+++ b/stage23/entry.s3.c
@@ -11,7 +11,6 @@
 #include <fs/file.h>
 #include <lib/elf.h>
 #include <mm/pmm.h>
-#include <mm/mtrr.h>
 #include <protos/stivale.h>
 #include <protos/stivale2.h>
 #include <protos/linux.h>
@@ -96,8 +95,6 @@ void stage3_common(void) {
     print("Boot drive: %x\n", boot_volume->drive);
     print("Boot partition: %d\n", boot_volume->partition);
 
-    mtrr_save();
-
     char *cmdline;
     char *config = menu(&cmdline);
 
diff --git a/stage23/lib/gterm.c b/stage23/lib/gterm.c
index 2da56970..4a92f04f 100644
--- a/stage23/lib/gterm.c
+++ b/stage23/lib/gterm.c
@@ -7,7 +7,6 @@
 #include <lib/print.h>
 #include <lib/uri.h>
 #include <lib/fb.h>
-#include <mm/mtrr.h>
 #include <mm/pmm.h>
 
 #define VGA_FONT_WIDTH  8
@@ -390,9 +389,6 @@ bool gterm_init(int *_rows, int *_cols, uint32_t *_colours, int _margin, int _ma
     gterm_bpp         = fbinfo.framebuffer_bpp;
     gterm_pitch       = fbinfo.framebuffer_pitch;
 
-    mtrr_set_range((uint64_t)(size_t)gterm_framebuffer,
-                   (uint64_t)gterm_pitch * gterm_height, MTRR_MEMORY_TYPE_WC);
-
     if (vga_font == NULL)
         vga_font = ext_mem_alloc(VGA_FONT_MAX);
 
diff --git a/stage23/mm/mtrr.32.c b/stage23/mm/mtrr.32.c
deleted file mode 100644
index 8ea988cd..00000000
--- a/stage23/mm/mtrr.32.c
+++ /dev/null
@@ -1,28 +0,0 @@
-#include <stdint.h>
-#include <stddef.h>
-#include <mm/mtrr.h>
-#include <sys/cpu.h>
-#include <lib/blib.h>
-
-static bool mtrr_supported(void) {
-    uint32_t eax, ebx, ecx, edx;
-
-    if (!cpuid(1, 0, &eax, &ebx, &ecx, &edx))
-        return false;
-
-    return !!(edx & (1 << 12));
-}
-
-void mtrr_restore_32(struct mtrr *saved_mtrr) {
-    if (!mtrr_supported())
-        return;
-
-    uint64_t ia32_mtrrcap = rdmsr(0xfe);
-
-    uint8_t var_reg_count = ia32_mtrrcap & 0xff;
-
-    for (uint8_t i = 0; i < var_reg_count; i++) {
-        wrmsr(0x200 + i * 2,     saved_mtrr[i].base);
-        wrmsr(0x200 + i * 2 + 1, saved_mtrr[i].mask);
-    }
-}
diff --git a/stage23/mm/mtrr.c b/stage23/mm/mtrr.c
deleted file mode 100644
index 5d263e8f..00000000
--- a/stage23/mm/mtrr.c
+++ /dev/null
@@ -1,136 +0,0 @@
-#include <stdint.h>
-#include <stddef.h>
-#include <mm/mtrr.h>
-#include <mm/pmm.h>
-#include <sys/cpu.h>
-#include <lib/print.h>
-#include <lib/blib.h>
-
-static bool mtrr_supported(void) {
-    uint32_t eax, ebx, ecx, edx;
-
-    if (!cpuid(1, 0, &eax, &ebx, &ecx, &edx))
-        return false;
-
-    return !!(edx & (1 << 12));
-}
-
-static bool is_block_in_mtrr_range(struct mtrr *mtrr, uint64_t block_base, uint64_t block_size) {
-    // False if the MTRR is not valid
-    if (!(mtrr->mask & (1 << 11)))
-        return false;
-
-    uint64_t base = mtrr->base & ~((uint64_t)0xfff);
-    uint64_t mask = mtrr->mask & ~((uint64_t)0xfff);
-
-    for (uint64_t i = block_base; i < block_base + block_size; i += 4096) {
-        if ((i & mask) == (base & mask))
-            return true;
-    }
-
-    return false;
-}
-
-bool mtrr_set_range(uint64_t base, uint64_t size, uint8_t memory_type) {
-    if (!mtrr_supported())
-        return false;
-
-    uint32_t 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) & ~(size - 1);
-
-    print("mtrr: Base: %X Mask: %X\n", base, mask);
-
-    uint64_t ia32_mtrrcap = rdmsr(0xfe);
-
-    if (ia32_mtrrcap & (1 << 10)) {
-        print("mtrr: Write-combining supported\n");
-    } else {
-        if (memory_type == MTRR_MEMORY_TYPE_WC)
-            return false;
-    }
-
-    uint8_t var_reg_count = ia32_mtrrcap & 0xff;
-
-    // Check if we're not overlapping any other MTRR range
-    for (uint8_t i = 0; i < var_reg_count; i++) {
-        struct mtrr mtrr;
-        mtrr.base = rdmsr(0x200 + i * 2);
-        mtrr.mask = rdmsr(0x200 + i * 2 + 1);
-
-        if (is_block_in_mtrr_range(&mtrr, base, size))
-            return false;
-    }
-
-    print("mtrr: Block does not overlap other ranges, good to go\n");
-
-    // Find usable MTRR slot
-    for (uint8_t i = 0; i < var_reg_count; i++) {
-        struct mtrr mtrr;
-        mtrr.base = rdmsr(0x200 + i * 2);
-        mtrr.mask = rdmsr(0x200 + i * 2 + 1);
-
-        if (mtrr.mask & (1 << 11))
-            continue;
-
-        // Found
-        wrmsr(0x200 + i * 2,     base | memory_type);
-        wrmsr(0x200 + i * 2 + 1, mask | (1 << 11));
-
-        print("mtrr: Set range in variable MTRR number %u\n", i);
-        return true;
-    }
-
-    return false;
-}
-
-struct mtrr *saved_mtrr = NULL;
-
-void mtrr_save(void) {
-    if (!mtrr_supported())
-        return;
-
-    uint64_t ia32_mtrrcap = rdmsr(0xfe);
-
-    uint8_t var_reg_count = ia32_mtrrcap & 0xff;
-
-    if (!saved_mtrr)
-        saved_mtrr = ext_mem_alloc(var_reg_count * sizeof(struct mtrr));
-
-    for (uint8_t i = 0; i < var_reg_count; i++) {
-        saved_mtrr[i].base = rdmsr(0x200 + i * 2);
-        saved_mtrr[i].mask = rdmsr(0x200 + i * 2 + 1);
-    }
-}
-
-void mtrr_restore(void) {
-    if (!mtrr_supported())
-        return;
-
-    uint64_t ia32_mtrrcap = rdmsr(0xfe);
-
-    uint8_t var_reg_count = ia32_mtrrcap & 0xff;
-
-    for (uint8_t i = 0; i < var_reg_count; i++) {
-        wrmsr(0x200 + i * 2,     saved_mtrr[i].base);
-        wrmsr(0x200 + i * 2 + 1, saved_mtrr[i].mask);
-    }
-}
diff --git a/stage23/mm/mtrr.h b/stage23/mm/mtrr.h
deleted file mode 100644
index 7fae542f..00000000
--- a/stage23/mm/mtrr.h
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef __MM__MTRR_H__
-#define __MM__MTRR_H__
-
-#include <stdint.h>
-#include <stdbool.h>
-
-#define MTRR_MEMORY_TYPE_UC 0x00
-#define MTRR_MEMORY_TYPE_WC 0x01
-#define MTRR_MEMORY_TYPE_WT 0x04
-#define MTRR_MEMORY_TYPE_WP 0x05
-#define MTRR_MEMORY_TYPE_WB 0x06
-
-struct mtrr {
-    uint64_t base;
-    uint64_t mask;
-};
-
-extern struct mtrr *saved_mtrr;
-
-void mtrr_save(void);
-void mtrr_restore(void);
-void mtrr_restore_32(struct mtrr *saved_mtrr);
-bool mtrr_set_range(uint64_t base, uint64_t size, uint8_t caching_type);
-
-#endif
diff --git a/stage23/protos/chainload.c b/stage23/protos/chainload.c
index 01a57957..bc15f036 100644
--- a/stage23/protos/chainload.c
+++ b/stage23/protos/chainload.c
@@ -8,7 +8,6 @@
 #include <lib/blib.h>
 #include <drivers/disk.h>
 #include <lib/term.h>
-#include <mm/mtrr.h>
 #include <sys/idt.h>
 #include <drivers/vga_textmode.h>
 
@@ -93,8 +92,6 @@ void chainload(char *config) {
 
     volume_read(p, (void *)0x7c00, 0, 512);
 
-    mtrr_restore();
-
     spinup(drive);
 }
 
diff --git a/stage23/protos/linux.c b/stage23/protos/linux.c
index 68a61636..b87a48a2 100644
--- a/stage23/protos/linux.c
+++ b/stage23/protos/linux.c
@@ -10,7 +10,6 @@
 #include <lib/print.h>
 #include <lib/uri.h>
 #include <mm/pmm.h>
-#include <mm/mtrr.h>
 #include <sys/idt.h>
 #include <lib/fb.h>
 #include <lib/acpi.h>
@@ -484,8 +483,6 @@ void linux_load(char *config, char *cmdline) {
 
     term_deinit();
 
-    mtrr_restore();
-
     struct screen_info *screen_info = &boot_params->screen_info;
 
     int req_width = 0, req_height = 0, req_bpp = 0;
diff --git a/stage23/protos/stivale.c b/stage23/protos/stivale.c
index 7a62289a..eb1cb84a 100644
--- a/stage23/protos/stivale.c
+++ b/stage23/protos/stivale.c
@@ -19,7 +19,6 @@
 #include <fs/file.h>
 #include <mm/vmm.h>
 #include <mm/pmm.h>
-#include <mm/mtrr.h>
 #include <stivale/stivale.h>
 #include <drivers/vga_textmode.h>
 
@@ -263,8 +262,6 @@ __attribute__((noreturn)) void stivale_spinup_32(
 __attribute__((noreturn)) void stivale_spinup(
                  int bits, bool level5pg, pagemap_t *pagemap,
                  uint64_t entry_point, void *stivale_struct, uint64_t stack) {
-    mtrr_restore();
-
 #if defined (bios)
     if (bits == 64) {
         // If we're going 64, we might as well call this BIOS interrupt
diff --git a/stage23/protos/stivale2.c b/stage23/protos/stivale2.c
index 26b263ee..227aaaee 100644
--- a/stage23/protos/stivale2.c
+++ b/stage23/protos/stivale2.c
@@ -21,7 +21,6 @@
 #include <sys/lapic.h>
 #include <fs/file.h>
 #include <mm/pmm.h>
-#include <mm/mtrr.h>
 #include <stivale/stivale2.h>
 #include <pxe/tftp.h>
 #include <drivers/edid.h>
@@ -323,19 +322,6 @@ skip_modeset:;
             tag->blue_mask_shift    = fb->blue_mask_shift;
 
             append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
-
-            if (terminal_hdr_tag == NULL && get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_FB_MTRR_ID) != NULL) {
-                mtrr_restore();
-                bool ret = mtrr_set_range(tag->framebuffer_addr,
-                    (uint64_t)tag->framebuffer_pitch * tag->framebuffer_height,
-                    MTRR_MEMORY_TYPE_WC);
-                if (ret) {
-                    struct stivale2_tag *tag = ext_mem_alloc(sizeof(struct stivale2_tag));
-                    tag->identifier = STIVALE2_STRUCT_TAG_FB_MTRR_ID;
-                    append_tag(&stivale2_struct, (struct stivale2_tag *)tag);
-                }
-                mtrr_save();
-            }
         }
     } else {
 #if defined (uefi)
@@ -394,8 +380,6 @@ skip_modeset:;
     // Create SMP struct tag
     //////////////////////////////////////////////
     {
-    mtrr_restore();
-
     struct stivale2_header_tag_smp *smp_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_SMP_ID);
     if (smp_hdr_tag != NULL) {
         struct stivale2_struct_tag_smp *tag;
diff --git a/stage23/sys/smp.c b/stage23/sys/smp.c
index 90f9f930..41056f49 100644
--- a/stage23/sys/smp.c
+++ b/stage23/sys/smp.c
@@ -11,7 +11,6 @@
 #include <sys/gdt.h>
 #include <mm/vmm.h>
 #include <mm/pmm.h>
-#include <mm/mtrr.h>
 
 struct madt {
     struct sdt;
@@ -52,8 +51,6 @@ struct trampoline_passed_info {
     uint8_t  smp_tpl_booted_flag;
     uint8_t  smp_tpl_target_mode;
     uint32_t smp_tpl_pagemap;
-    uint32_t mtrr_restore_vector;
-    uint32_t saved_mtrr_ptr;
     uint32_t smp_tpl_info_struct;
     struct gdtr smp_tpl_gdt;
 } __attribute__((packed));
@@ -86,12 +83,6 @@ static bool smp_start_ap(uint32_t lapic_id, struct gdtr *gdtr,
                         | ((uint32_t)lv5 << 1)
                         | (uint32_t)longmode;
     passed_info->smp_tpl_gdt         = *gdtr;
-#if defined (bios)
-    passed_info->mtrr_restore_vector = (uint32_t)(uintptr_t)mtrr_restore;
-#elif defined (uefi)
-    passed_info->mtrr_restore_vector = (uint32_t)(uintptr_t)mtrr_restore_32;
-#endif
-    passed_info->saved_mtrr_ptr      = (uint32_t)(uintptr_t)saved_mtrr;
     passed_info->smp_tpl_booted_flag = 0;
 
     asm volatile ("" ::: "memory");
diff --git a/stage23/sys/smp_trampoline.real b/stage23/sys/smp_trampoline.real
index c7c42ca9..807b32e3 100644
--- a/stage23/sys/smp_trampoline.real
+++ b/stage23/sys/smp_trampoline.real
@@ -48,10 +48,6 @@ smp_trampoline:
   .nox2apic:
     lea esp, [ebx + temp_stack.top]
 
-    push dword [ebx + passed_info.saved_mtrr_ptr]
-    call [ebx + passed_info.mtrr_restore_vector]
-    add esp, 4
-
     test dword [ebx + passed_info.target_mode], (1 << 0)
     jz parking32
 
@@ -169,8 +165,6 @@ passed_info:
     .booted_flag db 0
     .target_mode db 0
     .pagemap dd 0
-    .mtrr_restore_vector dd 0
-    .saved_mtrr_ptr dd 0
     .smp_info_struct dd 0
     .gdtr:
         dw 0
tab: 248 wrap: offon