]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmVirtPkg/QemuVirtMemInfoLib: trim the MMIO region mapping
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Fri, 30 Nov 2018 11:28:29 +0000 (12:28 +0100)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Mon, 3 Dec 2018 14:56:39 +0000 (15:56 +0100)
QEMU/mach-virt is rather unhelpful when it comes to tracking down
NULL pointer dereferences that occur while running in UEFI: since
we have NOR flash mapped at address 0x0, inadvertent reads go
unnoticed, and even most writes are silently dropped, unless you're
unlucky and the instruction in question is one that KVM cannot
emulate, in which case you end up with a QEMU crash like this:

  error: kvm run failed Function not implemented
   PC=000000013f7ff804 X00=000000013f7ab108 X01=0000000000000064
  X02=000000013f801988 X03=00000000800003c4 X04=0000000000000000
  X05=0000000096000044 X06=fffffffffffd8270 X07=000000013f7ab4a0
  X08=0000000000000001 X09=000000013f803b88 X10=000000013f7e88d0
  X11=0000000000000009 X12=000000013f7ab554 X13=0000000000000008
  X14=0000000000000002 X15=0000000000000000 X16=0000000000000000
  X17=0000000000000000 X18=0000000000000000 X19=0000000000000000
  X20=000000013f81c000 X21=000000013f7ab170 X22=000000013f81c000
  X23=0000000009000018 X24=000000013f407020 X25=000000013f81c000
  X26=000000013f803530 X27=000000013f802000 X28=000000013f7ab270
  X29=000000013f7ab0d0 X30=000000013f7fee10  SP=000000013f7a6f30
  PSTATE=800003c5 N--- EL1h

and a warning in the host kernel log that load/store instruction
decoding is not supported by KVM.

Given that the first page of the flash device is not actually
used anyway, let's reduce the mappings of the peripheral space
and the flash device (both of which cover page #0) to only cover
what is actually required:

  ArmVirtQemu.fdf:
  > 0x00001000|0x001ff000
  > gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize

  ArmVirtQemuKernel.fdf:
  > 0x00008000|0x001f8000
  > gArmTokenSpaceGuid.PcdFvBaseAddress|gArmTokenSpaceGuid.PcdFvSize

For ArmVirtQemu, the resulting virtual mapping looks roughly like:
- [0, 4K)       : flash, unmapped
- [4K, 2M)      : flash, mapped as WB+X RAM
- [2M, 64M)     : flash, unmapped
- [64M, 128M)   : varstore flash, will be mapped by the NOR flash driver
- [128M, 256M)  : peripherals, mapped as device
- [256M, 1GB)   : 32-bit MMIO aperture, translated IO aperture, ECAM,
                  will be mapped by the PCI host bridge driver
- [1GB, ...)    : RAM, mapped.

After this change, any inadvertent read or write from/to the first
physical page will trigger a translation fault inside the guest,
regardless of the nature of the instruction, without crashing QEMU.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.c
ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoLib.inf
ArmVirtPkg/Library/QemuVirtMemInfoLib/QemuVirtMemInfoPeiLib.inf

index 0285a11b1d7701a568274cf72417bce0141fb61b..a26b2fbad9be05bece1f63dd0349c73d0023ceb3 100644 (file)
 // Number of Virtual Memory Map Descriptors\r
 #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS          5\r
 \r
 // Number of Virtual Memory Map Descriptors\r
 #define MAX_VIRTUAL_MEMORY_MAP_DESCRIPTORS          5\r
 \r
+//\r
+// mach-virt's core peripherals such as the UART, the GIC and the RTC are\r
+// all mapped in the 'miscellaneous device I/O' region, which we just map\r
+// in its entirety rather than device by device. Note that it does not\r
+// cover any of the NOR flash banks or PCI resource windows.\r
+//\r
+#define MACH_VIRT_PERIPH_BASE       0x08000000\r
+#define MACH_VIRT_PERIPH_SIZE       SIZE_128MB\r
+\r
 /**\r
   Return the Virtual Memory Map of your platform\r
 \r
 /**\r
   Return the Virtual Memory Map of your platform\r
 \r
@@ -66,16 +75,16 @@ ArmVirtGetMemoryMap (
       VirtualMemoryTable[0].VirtualBase,\r
       VirtualMemoryTable[0].Length));\r
 \r
       VirtualMemoryTable[0].VirtualBase,\r
       VirtualMemoryTable[0].Length));\r
 \r
-  // Peripheral space before DRAM\r
-  VirtualMemoryTable[1].PhysicalBase = 0x0;\r
-  VirtualMemoryTable[1].VirtualBase  = 0x0;\r
-  VirtualMemoryTable[1].Length       = VirtualMemoryTable[0].PhysicalBase;\r
+  // Memory mapped peripherals (UART, RTC, GIC, virtio-mmio, etc)\r
+  VirtualMemoryTable[1].PhysicalBase = MACH_VIRT_PERIPH_BASE;\r
+  VirtualMemoryTable[1].VirtualBase  = MACH_VIRT_PERIPH_BASE;\r
+  VirtualMemoryTable[1].Length       = MACH_VIRT_PERIPH_SIZE;\r
   VirtualMemoryTable[1].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;\r
 \r
   VirtualMemoryTable[1].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_DEVICE;\r
 \r
-  // Remap the FD region as normal executable memory\r
-  VirtualMemoryTable[2].PhysicalBase = PcdGet64 (PcdFdBaseAddress);\r
+  // Map the FV region as normal executable memory\r
+  VirtualMemoryTable[2].PhysicalBase = PcdGet64 (PcdFvBaseAddress);\r
   VirtualMemoryTable[2].VirtualBase  = VirtualMemoryTable[2].PhysicalBase;\r
   VirtualMemoryTable[2].VirtualBase  = VirtualMemoryTable[2].PhysicalBase;\r
-  VirtualMemoryTable[2].Length       = FixedPcdGet32 (PcdFdSize);\r
+  VirtualMemoryTable[2].Length       = FixedPcdGet32 (PcdFvSize);\r
   VirtualMemoryTable[2].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;\r
 \r
   // End of Table\r
   VirtualMemoryTable[2].Attributes   = ARM_MEMORY_REGION_ATTRIBUTE_WRITE_BACK;\r
 \r
   // End of Table\r
index 5c5b841051adf9fbce9a7ab9a124b7f2219ba48d..b6abc52531a8f561204f4f3aea603a232b66eb4e 100644 (file)
@@ -39,9 +39,9 @@
   PcdLib\r
 \r
 [Pcd]\r
   PcdLib\r
 \r
 [Pcd]\r
-  gArmTokenSpaceGuid.PcdFdBaseAddress\r
+  gArmTokenSpaceGuid.PcdFvBaseAddress\r
   gArmTokenSpaceGuid.PcdSystemMemoryBase\r
   gArmTokenSpaceGuid.PcdSystemMemorySize\r
 \r
 [FixedPcd]\r
   gArmTokenSpaceGuid.PcdSystemMemoryBase\r
   gArmTokenSpaceGuid.PcdSystemMemorySize\r
 \r
 [FixedPcd]\r
-  gArmTokenSpaceGuid.PcdFdSize\r
+  gArmTokenSpaceGuid.PcdFvSize\r
index d12089760b2224cc98280f5a6427c6c9c7e82caa..16802c5c414bce9b86375bde9a7c6a03f1e5e56d 100644 (file)
 \r
 [Pcd]\r
   gArmTokenSpaceGuid.PcdFdBaseAddress\r
 \r
 [Pcd]\r
   gArmTokenSpaceGuid.PcdFdBaseAddress\r
+  gArmTokenSpaceGuid.PcdFvBaseAddress\r
   gArmTokenSpaceGuid.PcdSystemMemoryBase\r
   gArmTokenSpaceGuid.PcdSystemMemorySize\r
 \r
 [FixedPcd]\r
   gArmTokenSpaceGuid.PcdFdSize\r
   gArmTokenSpaceGuid.PcdSystemMemoryBase\r
   gArmTokenSpaceGuid.PcdSystemMemorySize\r
 \r
 [FixedPcd]\r
   gArmTokenSpaceGuid.PcdFdSize\r
+  gArmTokenSpaceGuid.PcdFvSize\r
   gArmVirtTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress\r
   gArmVirtTokenSpaceGuid.PcdDeviceTreeInitialBaseAddress\r