Skip to contentSkip to conversation

EFI: always map EfiRuntimeServices{Code,Data}

PATCH In review Jan Beulich 3 participants 5 messages Jan 12, 2022 – Jan 31, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 EFI: always map EfiRuntimeServices{Code,Data}

From: Sergey Temerkhanov <[email protected]>

This helps overcome problems observed with some UEFI implementations which don't set the Attributes field in memery descriptors properly.

---
v4: Drop EFI_MEMORY_CACHEABILITY_MASK. Fold with pre-existing if() (into

    switch()). Style.

---
I guess "map_bs" would also want honoring in efi_exit_boot(), but that's yet another patch then I suppose.

xen/common/efi/boot.c +35 -6
@@ -1094,7 +1094,13 @@ static void __init efi_exit_boot(EFI_HAN
     {
         EFI_MEMORY_DESCRIPTOR *desc = efi_memmap + i;
 
-        if ( desc->Attribute & EFI_MEMORY_RUNTIME )
+        /*
+         * Runtime services regions are always mapped here.
+         * Attributes may be adjusted in efi_init_memory().
+         */
+        if ( (desc->Attribute & EFI_MEMORY_RUNTIME) ||
+             desc->Type == EfiRuntimeServicesCode ||
+             desc->Type == EfiRuntimeServicesData )
             desc->VirtualStart = desc->PhysicalStart;
         else
             desc->VirtualStart = INVALID_VIRTUAL_ADDRESS;
@@ -1545,13 +1551,36 @@ void __init efi_init_memory(void)
                     ROUNDUP(desc->PhysicalStart + len, PAGE_SIZE));
         }
 
-        if ( !efi_enabled(EFI_RS) ||
-             (!(desc->Attribute & EFI_MEMORY_RUNTIME) &&
-              (!map_bs ||
-               (desc->Type != EfiBootServicesCode &&
-                desc->Type != EfiBootServicesData))) )
+        if ( !efi_enabled(EFI_RS) )
             continue;
 
+        if ( !(desc->Attribute & EFI_MEMORY_RUNTIME) )
+        {
+            switch ( desc->Type )
+            {
+            default:
+                continue;
+
+            /*
+             * Adjust runtime services regions. Keep in sync with
+             * efi_exit_boot().
+             */
+            case EfiRuntimeServicesCode:
+            case EfiRuntimeServicesData:
+                printk(XENLOG_WARNING
+                       "Setting RUNTIME attribute for %013" PRIx64 "-%013" PRIx64 "\n",
+                       desc->PhysicalStart, desc->PhysicalStart + len - 1);
+                desc->Attribute |= EFI_MEMORY_RUNTIME;
+                break;
+
+            case EfiBootServicesCode:
+            case EfiBootServicesData:
+                if ( !map_bs )
+                    continue;
+                break;
+            }
+        }
+
         desc->VirtualStart = INVALID_VIRTUAL_ADDRESS;
 
         smfn = PFN_DOWN(desc->PhysicalStart);
Reviewed-by Tested-by on v4 1/1
#1 lore raw
Quoted 11 lines · Jan Beulich

From: Sergey Temerkhanov <[email protected]>

This helps overcome problems observed with some UEFI implementations which don't set the Attributes field in memery descriptors properly.

Hi,

I’ve tested this patch on an arm machine with UEFI boot and it works fine.

Cheers,
Luca

Quoted 68 lines

---
v4: Drop EFI_MEMORY_CACHEABILITY_MASK. Fold with pre-existing if() (into
switch()). Style.
---
I guess "map_bs" would also want honoring in efi_exit_boot(), but that's yet another patch then I suppose.

xen/common/efi/boot.c +35 -6
@@ -1094,7 +1094,13 @@ static void __init efi_exit_boot(EFI_HAN
    {
        EFI_MEMORY_DESCRIPTOR *desc = efi_memmap + i;
 
-        if ( desc->Attribute & EFI_MEMORY_RUNTIME )
+        /*
+         * Runtime services regions are always mapped here.
+         * Attributes may be adjusted in efi_init_memory().
+         */
+        if ( (desc->Attribute & EFI_MEMORY_RUNTIME) ||
+             desc->Type == EfiRuntimeServicesCode ||
+             desc->Type == EfiRuntimeServicesData )
            desc->VirtualStart = desc->PhysicalStart;
        else
            desc->VirtualStart = INVALID_VIRTUAL_ADDRESS;
@@ -1545,13 +1551,36 @@ void __init efi_init_memory(void)
                    ROUNDUP(desc->PhysicalStart + len, PAGE_SIZE));
        }
 
-        if ( !efi_enabled(EFI_RS) ||
-             (!(desc->Attribute & EFI_MEMORY_RUNTIME) &&
-              (!map_bs ||
-               (desc->Type != EfiBootServicesCode &&
-                desc->Type != EfiBootServicesData))) )
+        if ( !efi_enabled(EFI_RS) )
            continue;
 
+        if ( !(desc->Attribute & EFI_MEMORY_RUNTIME) )
+        {
+            switch ( desc->Type )
+            {
+            default:
+                continue;
+
+            /*
+             * Adjust runtime services regions. Keep in sync with
+             * efi_exit_boot().
+             */
+            case EfiRuntimeServicesCode:
+            case EfiRuntimeServicesData:
+                printk(XENLOG_WARNING
+                       "Setting RUNTIME attribute for %013" PRIx64 "-%013" PRIx64 "\n",
+                       desc->PhysicalStart, desc->PhysicalStart + len - 1);
+                desc->Attribute |= EFI_MEMORY_RUNTIME;
+                break;
+
+            case EfiBootServicesCode:
+            case EfiBootServicesData:
+                if ( !map_bs )
+                    continue;
+                break;
+            }
+        }
+
        desc->VirtualStart = INVALID_VIRTUAL_ADDRESS;
 
        smfn = PFN_DOWN(desc->PhysicalStart);
#2 lore raw
In reply to Jan Beulich

Hi Jan,

Quoted 3 lines · Jan Beulich

From: Sergey Temerkhanov <[email protected]>

This helps overcome problems observed with some UEFI implementations

Would it be possible to provide some details about the platform? This is helpful to track why a workaround is present.

Quoted 1 line

which don't set the Attributes field in memery descriptors properly.

typo: s/memery/memory/

Quoted 9 lines

---
v4: Drop EFI_MEMORY_CACHEABILITY_MASK. Fold with pre-existing if() (into

     switch()). Style.

---
I guess "map_bs" would also want honoring in efi_exit_boot(), but that's yet another patch then I suppose.

IIUC, we would need to invalidate the mapping when map_bs was used. Is it correct? If so, then I agree this sounds separate to the issue you are describing here.

Quoted 5 lines
xen/common/efi/boot.c +0 -0
@@ -1094,7 +1094,13 @@ static void __init efi_exit_boot(EFI_HAN
      {

xen/common/efi/boot.c static void __init efi_exit_boot(EFI_HAN · line 1103

Quoted 10 lines
xen/common/efi/boot.c +7 -1
          EFI_MEMORY_DESCRIPTOR *desc = efi_memmap + i;
 
-        if ( desc->Attribute & EFI_MEMORY_RUNTIME )
+        /*
+         * Runtime services regions are always mapped here.
+         * Attributes may be adjusted in efi_init_memory().
+         */
+        if ( (desc->Attribute & EFI_MEMORY_RUNTIME) ||
+             desc->Type == EfiRuntimeServicesCode ||
+             desc->Type == EfiRuntimeServicesData )

My knowledge with the stub is limited. Would you be able to explain why we need to map the runtime services even with !efi_enabled(EFI_RS)?

The reason I am asking is if we don't need it, then it would be sufficient to rely on desc->Attribute because you updated it in efi_init_memory().

Quoted 31 lines
xen/common/efi/boot.c +17 -5
              desc->VirtualStart = desc->PhysicalStart;
          else
              desc->VirtualStart = INVALID_VIRTUAL_ADDRESS;
@@ -1545,13 +1551,36 @@ void __init efi_init_memory(void)
                      ROUNDUP(desc->PhysicalStart + len, PAGE_SIZE));
          }
 
-        if ( !efi_enabled(EFI_RS) ||
-             (!(desc->Attribute & EFI_MEMORY_RUNTIME) &&
-              (!map_bs ||
-               (desc->Type != EfiBootServicesCode &&
-                desc->Type != EfiBootServicesData))) )
+        if ( !efi_enabled(EFI_RS) )
              continue;
 
+        if ( !(desc->Attribute & EFI_MEMORY_RUNTIME) )
+        {
+            switch ( desc->Type )
+            {
+            default:
+                continue;
+
+            /*
+             * Adjust runtime services regions. Keep in sync with
+             * efi_exit_boot().
+             */
+            case EfiRuntimeServicesCode:
+            case EfiRuntimeServicesData:
+                printk(XENLOG_WARNING
+                       "Setting RUNTIME attribute for %013" PRIx64 "-%013" PRIx64 "\n",
+                       desc->PhysicalStart, desc->PhysicalStart + len - 1);

xen/common/efi/boot.c void __init efi_init_memory(void) · line 1582

Quoted 10 lines
xen/common/efi/boot.c +10 -0
+                desc->Attribute |= EFI_MEMORY_RUNTIME;
+                break;
+
+            case EfiBootServicesCode:
+            case EfiBootServicesData:
+                if ( !map_bs )
+                    continue;
+                break;
+            }
+        }

The new code is much easier to read :).

xen/common/efi/boot.c void __init efi_init_memory(void) · line 1586

Quoted 5 lines
xen/common/efi/boot.c +1 -0
+
          desc->VirtualStart = INVALID_VIRTUAL_ADDRESS;
 
          smfn = PFN_DOWN(desc->PhysicalStart);

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw
Quoted 7 lines · Julien Grall
Quoted 3 lines · Jan Beulich

From: Sergey Temerkhanov <[email protected]>

This helps overcome problems observed with some UEFI implementations

Would it be possible to provide some details about the platform? This is helpful to track why a workaround is present.

I cannot provide any details. I took over the patch after pinging Sergey more than once, without any reaction. It was him to actually run into the problem in the wild.

Quoted 14 lines
Quoted 1 line

which don't set the Attributes field in memery descriptors properly.

typo: s/memery/memory/

Quoted 9 lines

---
v4: Drop EFI_MEMORY_CACHEABILITY_MASK. Fold with pre-existing if() (into

     switch()). Style.

---
I guess "map_bs" would also want honoring in efi_exit_boot(), but that's yet another patch then I suppose.

Quoted 3 lines

IIUC, we would need to invalidate the mapping when map_bs was used. Is it correct? If so, then I agree this sounds separate to the issue you are describing here.

No, the other way around: We'd also need to put in a valid virtual address there for EfiBootServices{Code,Data}. I expect this wasn't done when map_bs was introduced because iirc at that time the code fragment in efi_exit_boot() was entirely dead, present merely for documentation purposes.

Quoted 6 lines
Quoted 6 lines
xen/common/efi/boot.c +0 -0
@@ -1094,7 +1094,13 @@ static void __init efi_exit_boot(EFI_HAN
      {
          EFI_MEMORY_DESCRIPTOR *desc = efi_memmap + i;
 
Quoted 11 lines

xen/common/efi/boot.c static void __init efi_exit_boot(EFI_HAN · line 1103

Quoted 8 lines
xen/common/efi/boot.c +7 -1
-        if ( desc->Attribute & EFI_MEMORY_RUNTIME )
+        /*
+         * Runtime services regions are always mapped here.
+         * Attributes may be adjusted in efi_init_memory().
+         */
+        if ( (desc->Attribute & EFI_MEMORY_RUNTIME) ||
+             desc->Type == EfiRuntimeServicesCode ||
+             desc->Type == EfiRuntimeServicesData )

My knowledge with the stub is limited. Would you be able to explain why we need to map the runtime services even with !efi_enabled(EFI_RS)?

In principle we wouldn't need to, but the final setting of this bit isn't known yet at this point - it'll be known only after parsing the command line (see parse_efi_param()).

Jan

Reviewed-by
#4 lore raw

Hi Jan,

Quoted 4 lines · Jan Beulich
Quoted 3 lines · Julien Grall
Quoted 2 lines · Jan Beulich

From: Sergey Temerkhanov <[email protected]>

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This helps overcome problems observed with some UEFI implementations

Would it be possible to provide some details about the platform? This is helpful to track why a workaround is present.

I cannot provide any details. I took over the patch after pinging Sergey more than once, without any reaction. It was him to actually run into the problem in the wild.

This is a quite unfortunate.

Quoted 43 lines
Quoted 15 lines
Quoted 2 lines

which don't set the Attributes field in memery descriptors properly.

typo: s/memery/memory/

Quoted 9 lines

---
v4: Drop EFI_MEMORY_CACHEABILITY_MASK. Fold with pre-existing if() (into

      switch()). Style.

---
I guess "map_bs" would also want honoring in efi_exit_boot(), but that's yet another patch then I suppose.

Quoted 3 lines

IIUC, we would need to invalidate the mapping when map_bs was used. Is it correct? If so, then I agree this sounds separate to the issue you are describing here.

No, the other way around: We'd also need to put in a valid virtual address there for EfiBootServices{Code,Data}. I expect this wasn't done when map_bs was introduced because iirc at that time the code fragment in efi_exit_boot() was entirely dead, present merely for documentation purposes.

Quoted 6 lines
Quoted 6 lines
xen/common/efi/boot.c +0 -0
@@ -1094,7 +1094,13 @@ static void __init efi_exit_boot(EFI_HAN
       {
           EFI_MEMORY_DESCRIPTOR *desc = efi_memmap + i;
 
Quoted 11 lines

xen/common/efi/boot.c static void __init efi_exit_boot(EFI_HAN · line 1103

Quoted 8 lines
xen/common/efi/boot.c +7 -1
-        if ( desc->Attribute & EFI_MEMORY_RUNTIME )
+        /*
+         * Runtime services regions are always mapped here.
+         * Attributes may be adjusted in efi_init_memory().
+         */
+        if ( (desc->Attribute & EFI_MEMORY_RUNTIME) ||
+             desc->Type == EfiRuntimeServicesCode ||
+             desc->Type == EfiRuntimeServicesData )

My knowledge with the stub is limited. Would you be able to explain why we need to map the runtime services even with !efi_enabled(EFI_RS)?

Quoted 3 lines

In principle we wouldn't need to, but the final setting of this bit isn't known yet at this point - it'll be known only after parsing the command line (see parse_efi_param()).

Oh. I thought efi_exit_boot() was called *after* efi_init_memory(). Your answer about 'map_bs' also make more sense.

Thanks for the explanation!

Cheers,

Signature · 1 line
Julien Grall