Skip to content

slaunch/txt: fix missing 64-bit arithmetic in MTRR calculation - #42

Open
accek-itl wants to merge 1 commit into
TrenchBoot:tb-upstream-nextfrom
accek-itl:ptl-senter-fixes
Open

slaunch/txt: fix missing 64-bit arithmetic in MTRR calculation#42
accek-itl wants to merge 1 commit into
TrenchBoot:tb-upstream-nextfrom
accek-itl:ptl-senter-fixes

Conversation

@accek-itl

Copy link
Copy Markdown

Make the range sizes 64-bit: the 32-bit ~({mtrr_s,pages_in_range} - 1) dropped PHYSMASK bits above bit 43 on parts with MAXPHYADDR >= 45.

The broken calculations (bits above 31 truncated to 0) are here:

Possible alternative version if you prefer:

diff --git a/grub-core/loader/slaunch/txt.c b/grub-core/loader/slaunch/txt.c
index 43f82d5030c5..ece0902c8e6e 100644
--- a/grub-core/loader/slaunch/txt.c
+++ b/grub-core/loader/slaunch/txt.c
@@ -424,7 +424,7 @@ set_mtrr_mem_type (struct grub_txt_acm_header *sinit, grub_uint32_t mem_type)
       grub_wrmsr (GRUB_MSR_X86_MTRR_PHYSBASE0 + ndx*2, mtrr_physbase.raw);
 
       mtrr_physmask.raw = grub_rdmsr (GRUB_MSR_X86_MTRR_PHYSMASK0 + ndx*2);
-      mtrr_physmask.mask = ~(mtrr_s - 1) & mtrr_shifted_mask;
+      mtrr_physmask.mask = ~((grub_uint64_t) mtrr_s - 1) & mtrr_shifted_mask;
       mtrr_physmask.v = 1;
       grub_wrmsr (GRUB_MSR_X86_MTRR_PHYSMASK0 + ndx*2, mtrr_physmask.raw);
 
@@ -453,7 +453,7 @@ set_mtrr_mem_type (struct grub_txt_acm_header *sinit, grub_uint32_t mem_type)
       pages_in_range = 1 << (fls (num_pages) - 1);
 
       mtrr_physmask.raw = grub_rdmsr (GRUB_MSR_X86_MTRR_PHYSMASK0 + ndx*2);
-      mtrr_physmask.mask = ~(pages_in_range - 1) & mtrr_shifted_mask;
+      mtrr_physmask.mask = ~((grub_uint64_t) pages_in_range - 1) & mtrr_shifted_mask;
       mtrr_physmask.v = 1;
       grub_wrmsr (GRUB_MSR_X86_MTRR_PHYSMASK0 + ndx*2, mtrr_physmask.raw);

@macpijan
macpijan requested a review from SergiiDmytruk August 6, 2026 13:12
@SergiiDmytruk
SergiiDmytruk changed the base branch from tb-2.12-284-v4 to tb-upstream-next August 10, 2026 13:52
@SergiiDmytruk

Copy link
Copy Markdown
Member

Made tb-upstream-next branch at the same commit as tb-2.12-284-v4 and updated the target branch here. v4 patchset has been published, so its branch shouldn't really change. I remember suggesting to use any branch, but a new one is actually better.

Possible alternative version if you prefer:

I think this version is better. The code makes a reasonable assumption that the number of pages within ranges fits into 32-bit integers. Changing types of those variables would have to also update how their values are computed to be consistent. Things go wrong only in those two expressions computing the masks and it makes sense to indicate their 64-bit nature explicitly.

Cast the range sizes to 64-bit: the 32-bit ~({mtrr_s,pages_in_range} - 1)
dropped PHYSMASK bits above bit 43 on parts with MAXPHYADDR >= 45.

Signed-off-by: Szymon "accek" Acedański <accek@invisiblethingslab.com>
@accek-itl

Copy link
Copy Markdown
Author

Ok, I agree, adjusted commit to the alternative version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants