render / rpms / edk2

Forked from rpms/edk2 2 months ago
Clone

Blame SOURCES/ovmf-whitepaper-c770f8c.txt

7439a5
Open Virtual Machine Firmware (OVMF) Status Report
7439a5
July 2014 (with updates in August 2014 - January 2015)
7439a5
7439a5
Author: Laszlo Ersek <lersek@redhat.com>
7439a5
Copyright (C) 2014-2015, Red Hat, Inc.
7439a5
CC BY-SA 4.0 <http://creativecommons.org/licenses/by-sa/4.0/>
7439a5
7439a5
Abstract
7439a5
--------
7439a5
7439a5
The Unified Extensible Firmware Interface (UEFI) is a specification that
7439a5
defines a software interface between an operating system and platform firmware.
7439a5
UEFI is designed to replace the Basic Input/Output System (BIOS) firmware
7439a5
interface.
7439a5
7439a5
Hardware platform vendors have been increasingly adopting the UEFI
7439a5
Specification to govern their boot firmware developments. OVMF (Open Virtual
7439a5
Machine Firmware), a sub-project of Intel's EFI Development Kit II (edk2),
7439a5
enables UEFI support for Ia32 and X64 Virtual Machines.
7439a5
7439a5
This paper reports on the status of the OVMF project, treats features and
7439a5
limitations, gives end-user hints, and examines some areas in-depth.
7439a5
7439a5
Keywords: ACPI, boot options, CSM, edk2, firmware, flash, fw_cfg, KVM, memory
7439a5
map, non-volatile variables, OVMF, PCD, QEMU, reset vector, S3, Secure Boot,
7439a5
Smbios, SMM, TianoCore, UEFI, VBE shim, Virtio
7439a5
7439a5
Table of Contents
7439a5
-----------------
7439a5
7439a5
- Motivation
7439a5
- Scope
7439a5
- Example qemu invocation
7439a5
- Installation of OVMF guests with virt-manager and virt-install
7439a5
- Supported guest operating systems
7439a5
- Compatibility Support Module (CSM)
7439a5
- Phases of the boot process
7439a5
- Project structure
7439a5
- Platform Configuration Database (PCD)
7439a5
- Firmware image structure
7439a5
- S3 (suspend to RAM and resume)
7439a5
- A comprehensive memory map of OVMF
7439a5
- Known Secure Boot limitations
7439a5
- Variable store and LockBox in SMRAM
7439a5
- Select features
7439a5
  - X64-specific reset vector for OVMF
7439a5
  - Client library for QEMU's firmware configuration interface
7439a5
  - Guest ACPI tables
7439a5
  - Guest SMBIOS tables
7439a5
  - Platform-specific boot policy
7439a5
  - Virtio drivers
7439a5
  - Platform Driver
7439a5
  - Video driver
7439a5
- Afterword
7439a5
7439a5
Motivation
7439a5
----------
7439a5
7439a5
OVMF extends the usual benefits of virtualization to UEFI. Reasons to use OVMF
7439a5
include:
7439a5
7439a5
- Legacy-free guests. A UEFI-based environment eliminates dependencies on
7439a5
  legacy address spaces and devices. This is especially beneficial when used
7439a5
  with physically assigned devices where the legacy operating mode is
7439a5
  troublesome to support, ex. assigned graphics cards operating in legacy-free,
7439a5
  non-VGA mode in the guest.
7439a5
7439a5
- Future proof guests. The x86 market is steadily moving towards a legacy-free
7439a5
  platform and guest operating systems may eventually require a UEFI
7439a5
  environment. OVMF provides that next generation firmware support for such
7439a5
  applications.
7439a5
7439a5
- GUID partition tables (GPTs). MBR partition tables represent partition
7439a5
  offsets and sizes with 32-bit integers, in units of 512 byte sectors. This
7439a5
  limits the addressable portion of the disk to 2 TB. GPT represents logical
7439a5
  block addresses with 64 bits.
7439a5
7439a5
- Liberating boot loader binaries from residing in contested and poorly defined
7439a5
  space between the partition table and the partitions.
7439a5
7439a5
- Support for booting off disks (eg. pass-through physical SCSI devices) with a
7439a5
  4kB physical and logical sector size, i.e. which don't have 512-byte block
7439a5
  emulation.
7439a5
7439a5
- Development and testing of Secure Boot-related features in guest operating
7439a5
  systems. Although OVMF's Secure Boot implementation is currently not secure
7439a5
  against malicious UEFI drivers, UEFI applications, and guest kernels,
7439a5
  trusted guest code that only uses standard UEFI interfaces will find a valid
7439a5
  Secure Boot environment under OVMF, with working key enrollment and signature
7439a5
  validation. This enables development and testing of portable, Secure
7439a5
  Boot-related guest code.
7439a5
7439a5
- Presence of non-volatile UEFI variables. This furthers development and
7439a5
  testing of OS installers, UEFI boot loaders, and unique, dependent guest OS
7439a5
  features. For example, an efivars-backed pstore (persistent storage)
7439a5
  file system works under Linux.
7439a5
7439a5
- Altogether, a near production-level UEFI environment for virtual machines
7439a5
  when Secure Boot is not required.
7439a5
7439a5
Scope
7439a5
-----
7439a5
7439a5
UEFI and especially Secure Boot have been topics fraught with controversy and
7439a5
political activism. This paper sidesteps these aspects and strives to focus on
7439a5
use cases, hands-on information for end users, and technical details.
7439a5
7439a5
Unless stated otherwise, the expression "X supports Y" means "X is technically
7439a5
compatible with interfaces provided or required by Y". It does not imply
7439a5
support as an activity performed by natural persons or companies.
7439a5
7439a5
We discuss the status of OVMF at a state no earlier than edk2 SVN revision
7439a5
16158. The paper concentrates on upstream projects and communities, but
7439a5
occasionally it pans out about OVMF as it is planned to be shipped (as
7439a5
Technical Preview) in Red Hat Enterprise Linux 7.1. Such digressions are marked
7439a5
with the [RHEL] margin notation.
7439a5
7439a5
Although other VMMs and accelerators are known to support (or plan to support)
7439a5
OVMF to various degrees -- for example, VirtualBox, Xen, BHyVe --, we'll
7439a5
emphasize OVMF on qemu/KVM, because QEMU and KVM have always been Red Hat's
7439a5
focus wrt. OVMF.
7439a5
7439a5
The recommended upstream QEMU version is 2.1+. The recommended host Linux
7439a5
kernel (KVM) version is 3.10+. The recommended QEMU machine type is
7439a5
"qemu-system-x86_64 -M pc-i440fx-2.1" or later.
7439a5
7439a5
The term "TianoCore" is used interchangeably with "edk2" in this paper.
7439a5
7439a5
Example qemu invocation
7439a5
-----------------------
7439a5
7439a5
The following commands give a quick foretaste of installing a UEFI operating
7439a5
system on OVMF, relying only on upstream edk2 and qemu.
7439a5
7439a5
- Clone and build OVMF:
7439a5
7439a5
  git clone https://github.com/tianocore/edk2.git
7439a5
  cd edk2
7439a5
  nice OvmfPkg/build.sh -a X64 -n $(getconf _NPROCESSORS_ONLN)
7439a5
7439a5
  (Note that this ad-hoc build will not include the Secure Boot feature.)
7439a5
7439a5
- The build output file, "OVMF.fd", includes not only the executable firmware
7439a5
  code, but the non-volatile variable store as well. For this reason, make a
7439a5
  VM-specific copy of the build output (the variable store should be private to
7439a5
  the virtual machine):
7439a5
7439a5
  cp Build/OvmfX64/DEBUG_GCC4?/FV/OVMF.fd fedora.flash
7439a5
7439a5
  (The variable store and the firmware executable are also available in the
7439a5
  build output as separate files: "OVMF_VARS.fd" and "OVMF_CODE.fd". This
7439a5
  enables central management and updates of the firmware executable, while each
7439a5
  virtual machine can retain its own variable store.)
7439a5
7439a5
- Download a Fedora LiveCD:
7439a5
7439a5
  wget https://dl.fedoraproject.org/pub/fedora/linux/releases/20/Live/x86_64/Fedora-Live-Xfce-x86_64-20-1.iso
7439a5
7439a5
- Create a virtual disk (qcow2 format, 20 GB in size):
7439a5
7439a5
  qemu-img create -f qcow2 fedora.img 20G
7439a5
7439a5
- Create the following qemu wrapper script under the name "fedora.sh":
7439a5
7439a5
  # Basic virtual machine properties: a recent i440fx machine type, KVM
7439a5
  # acceleration, 2048 MB RAM, two VCPUs.
7439a5
  OPTS="-M pc-i440fx-2.1 -enable-kvm -m 2048 -smp 2"
7439a5
7439a5
  # The OVMF binary, including the non-volatile variable store, appears as a
7439a5
  # "normal" qemu drive on the host side, and it is exposed to the guest as a
7439a5
  # persistent flash device.
7439a5
  OPTS="$OPTS -drive if=pflash,format=raw,file=fedora.flash"
7439a5
7439a5
  # The hard disk is exposed to the guest as a virtio-block device. OVMF has a
7439a5
  # driver stack that supports such a disk. We specify this disk as first boot
7439a5
  # option. OVMF recognizes the boot order specification.
7439a5
  OPTS="$OPTS -drive id=disk0,if=none,format=qcow2,file=fedora.img"
7439a5
  OPTS="$OPTS -device virtio-blk-pci,drive=disk0,bootindex=0"
7439a5
7439a5
  # The Fedora installer disk appears as an IDE CD-ROM in the guest. This is
7439a5
  # the 2nd boot option.
7439a5
  OPTS="$OPTS -drive id=cd0,if=none,format=raw,readonly"
7439a5
  OPTS="$OPTS,file=Fedora-Live-Xfce-x86_64-20-1.iso"
7439a5
  OPTS="$OPTS -device ide-cd,bus=ide.1,drive=cd0,bootindex=1"
7439a5
7439a5
  # The following setting enables S3 (suspend to RAM). OVMF supports S3
7439a5
  # suspend/resume.
7439a5
  OPTS="$OPTS -global PIIX4_PM.disable_s3=0"
7439a5
7439a5
  # OVMF emits a number of info / debug messages to the QEMU debug console, at
7439a5
  # ioport 0x402. We configure qemu so that the debug console is indeed
7439a5
  # available at that ioport. We redirect the host side of the debug console to
7439a5
  # a file.
7439a5
  OPTS="$OPTS -global isa-debugcon.iobase=0x402 -debugcon file:fedora.ovmf.log"
7439a5
7439a5
  # QEMU accepts various commands and queries from the user on the monitor
7439a5
  # interface. Connect the monitor with the qemu process's standard input and
7439a5
  # output.
7439a5
  OPTS="$OPTS -monitor stdio"
7439a5
7439a5
  # A USB tablet device in the guest allows for accurate pointer tracking
7439a5
  # between the host and the guest.
7439a5
  OPTS="$OPTS -device piix3-usb-uhci -device usb-tablet"
7439a5
7439a5
  # Provide the guest with a virtual network card (virtio-net).
7439a5
  #
7439a5
  # Normally, qemu provides the guest with a UEFI-conformant network driver
7439a5
  # from the iPXE project, in the form of a PCI expansion ROM. For this test,
7439a5
  # we disable the expansion ROM and allow OVMF's built-in virtio-net driver to
7439a5
  # take effect.
7439a5
  #
7439a5
  # On the host side, we use the SLIRP ("user") network backend, which has
7439a5
  # relatively low performance, but it doesn't require extra privileges from
7439a5
  # the user executing qemu.
7439a5
  OPTS="$OPTS -netdev id=net0,type=user"
7439a5
  OPTS="$OPTS -device virtio-net-pci,netdev=net0,romfile="
7439a5
7439a5
  # A Spice QXL GPU is recommended as the primary VGA-compatible display
7439a5
  # device. It is a full-featured virtual video card, with great operating
7439a5
  # system driver support. OVMF supports it too.
7439a5
  OPTS="$OPTS -device qxl-vga"
7439a5
7439a5
  qemu-system-x86_64 $OPTS
7439a5
7439a5
- Start the Fedora guest:
7439a5
7439a5
  sh fedora.sh
7439a5
7439a5
- The above command can be used for both installation and later boots of the
7439a5
  Fedora guest.
7439a5
7439a5
- In order to verify basic OVMF network connectivity:
7439a5
7439a5
  - Assuming that the non-privileged user running qemu belongs to group G
7439a5
    (where G is a numeric identifier), ensure as root on the host that the
7439a5
    group range in file "/proc/sys/net/ipv4/ping_group_range" includes G.
7439a5
7439a5
  - As the non-privileged user, boot the guest as usual.
7439a5
7439a5
  - On the TianoCore splash screen, press ESC.
7439a5
7439a5
  - Navigate to Boot Manager | EFI Internal Shell
7439a5
7439a5
  - In the UEFI Shell, issue the following commands:
7439a5
7439a5
    ifconfig -s eth0 dhcp
7439a5
    ping A.B.C.D
7439a5
7439a5
    where A.B.C.D is a public IPv4 address in dotted decimal notation that your
7439a5
    host can reach.
7439a5
7439a5
  - Type "quit" at the (qemu) monitor prompt.
7439a5
7439a5
Installation of OVMF guests with virt-manager and virt-install
7439a5
--------------------------------------------------------------
7439a5
7439a5
(1) Assuming OVMF has been installed on the host with the following files:
7439a5
    - /usr/share/OVMF/OVMF_CODE.fd
7439a5
    - /usr/share/OVMF/OVMF_VARS.fd
7439a5
7439a5
    locate the "nvram" stanza in "/etc/libvirt/qemu.conf", and edit it as
7439a5
    follows:
7439a5
7439a5
    nvram = [ "/usr/share/OVMF/OVMF_CODE.fd:/usr/share/OVMF/OVMF_VARS.fd" ]
7439a5
7439a5
(2) Restart libvirtd with your Linux distribution's service management tool;
7439a5
    for example,
7439a5
7439a5
    systemctl restart libvirtd
7439a5
7439a5
(3) In virt-manager, proceed with the guest installation as usual:
7439a5
    - select File | New Virtual Machine,
7439a5
    - advance to Step 5 of 5,
7439a5
    - in Step 5, check "Customize configuration before install",
7439a5
    - click Finish;
7439a5
    - in the customization dialog, select Overview | Firmware, and choose UEFI,
7439a5
    - click Apply and Begin Installation.
7439a5
7439a5
(4) With virt-install:
7439a5
7439a5
    LDR="loader=/usr/share/OVMF/OVMF_CODE.fd,loader_ro=yes,loader_type=pflash"
7439a5
    virt-install \
7439a5
      --name fedora20 \
7439a5
      --memory 2048 \
7439a5
      --vcpus 2 \
7439a5
      --os-variant fedora20 \
7439a5
      --boot hd,cdrom,$LDR \
7439a5
      --disk size=20 \
7439a5
      --disk path=Fedora-Live-Xfce-x86_64-20-1.iso,device=cdrom,bus=scsi
7439a5
7439a5
(5) A popular, distribution-independent, bleeding-edge OVMF package is
7439a5
    available under <https://www.kraxel.org/repos/>, courtesy of Gerd Hoffmann.
7439a5
7439a5
    The "edk2.git-ovmf-x64" package provides the following files, among others:
7439a5
    - /usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd
7439a5
    - /usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd
7439a5
7439a5
    When using this package, adapt steps (1) and (4) accordingly.
7439a5
7439a5
(6) Additionally, the "edk2.git-ovmf-x64" package seeks to simplify the
7439a5
    enablement of Secure Boot in a virtual machine (strictly for development
7439a5
    and testing purposes).
7439a5
7439a5
    - Boot the virtual machine off the CD-ROM image called
7439a5
      "/usr/share/edk2.git/ovmf-x64/UefiShell.iso"; before or after installing
7439a5
      the main guest operating system.
7439a5
7439a5
    - When the UEFI shell appears, issue the following commands:
7439a5
7439a5
      EnrollDefaultKeys.efi
7439a5
      reset -s
7439a5
7439a5
    - The EnrollDefaultKeys.efi utility enrolls the following keys:
7439a5
7439a5
      - A static example X.509 certificate (CN=TestCommonName) as Platform Key
7439a5
        and first Key Exchange Key.
7439a5
7439a5
        The private key matching this certificate has been destroyed (but you
7439a5
        shouldn't trust this statement).
7439a5
7439a5
      - "Microsoft Corporation KEK CA 2011" as second Key Exchange Key
7439a5
        (SHA1: 31:59:0b:fd:89:c9:d7:4e:d0:87:df:ac:66:33:4b:39:31:25:4b:30).
7439a5
7439a5
      - "Microsoft Windows Production PCA 2011" as first DB entry
7439a5
        (SHA1: 58:0a:6f:4c:c4:e4:b6:69:b9:eb:dc:1b:2b:3e:08:7b:80:d0:67:8d).
7439a5
7439a5
      - "Microsoft Corporation UEFI CA 2011" as second DB entry
7439a5
        (SHA1: 46:de:f6:3b:5c:e6:1c:f8:ba:0d:e2:e6:63:9c:10:19:d0:ed:14:f3).
7439a5
7439a5
      These keys suffice to boot released versions of popular Linux
7439a5
      distributions (through the shim.efi utility), and Windows 8 and Windows
7439a5
      Server 2012 R2, in Secure Boot mode.
7439a5
7439a5
Supported guest operating systems
7439a5
---------------------------------
7439a5
7439a5
Upstream OVMF does not favor some guest operating systems over others for
7439a5
political or ideological reasons. However, some operating systems are harder to
7439a5
obtain and/or technically more difficult to support. The general expectation is
7439a5
that recent UEFI OSes should just work. Please consult the "OvmfPkg/README"
7439a5
file.
7439a5
7439a5
The following guest OSes were tested with OVMF:
7439a5
- Red Hat Enterprise Linux 6
7439a5
- Red Hat Enterprise Linux 7
7439a5
- Fedora 18
7439a5
- Fedora 19
7439a5
- Fedora 20
7439a5
- Windows Server 2008 R2 SP1
7439a5
- Windows Server 2012
7439a5
- Windows 8
7439a5
7439a5
Notes about Windows Server 2008 R2 (paraphrasing the "OvmfPkg/README" file):
7439a5
7439a5
- QEMU should be started with one of the "-device qxl-vga" and "-device VGA"
7439a5
  options.
7439a5
7439a5
- Only one video mode, 1024x768x32, is supported at OS runtime.
7439a5
7439a5
  Please refer to the section about QemuVideoDxe (OVMF's built-in video driver)
7439a5
  for more details on this limitation.
7439a5
7439a5
- The qxl-vga video card is recommended ("-device qxl-vga"). After booting the
7439a5
  installed guest OS, select the video card in Device Manager, and upgrade the
7439a5
  video driver to the QXL XDDM one.
7439a5
7439a5
  The QXL XDDM driver can be downloaded from
7439a5
  <http://www.spice-space.org/download.html>, under Guest | Windows binaries.
7439a5
7439a5
  This driver enables additional graphics resolutions at OS runtime, and
7439a5
  provides S3 (suspend/resume) capability.
7439a5
7439a5
Notes about Windows Server 2012 and Windows 8:
7439a5
7439a5
- QEMU should be started with the "-device qxl-vga,revision=4" option (or a
7439a5
  later revision, if available).
7439a5
7439a5
- The guest OS's builtin video driver inherits the video mode / frame buffer
7439a5
  from OVMF. There's no way to change the resolution at OS runtime.
7439a5
7439a5
  For this reason, a platform driver has been developed for OVMF, which allows
7439a5
  users to change the preferred video mode in the firmware. Please refer to the
7439a5
  section about PlatformDxe for details.
7439a5
7439a5
- It is recommended to upgrade the guest OS's video driver to the QXL WDDM one,
7439a5
  via Device Manager.
7439a5
7439a5
  Binaries for the QXL WDDM driver can be found at
7439a5
  <http://people.redhat.com/~vrozenfe/qxlwddm> (pick a version greater than or
7439a5
  equal to 0.6), while the source code resides at
7439a5
  <https://github.com/vrozenfe/qxl-dod>.
7439a5
7439a5
  This driver enables additional graphics resolutions at OS runtime, and
7439a5
  provides S3 (suspend/resume) capability.
7439a5
7439a5
Compatibility Support Module (CSM)
7439a5
----------------------------------
7439a5
7439a5
Collaboration between SeaBIOS and OVMF developers has enabled SeaBIOS to be
7439a5
built as a Compatibility Support Module, and OVMF to embed and use it.
7439a5
7439a5
Benefits of a SeaBIOS CSM include:
7439a5
7439a5
- The ability to boot legacy (non-UEFI) operating systems, such as legacy Linux
7439a5
  systems, Windows 7, OpenBSD 5.2, FreeBSD 8/9, NetBSD, DragonflyBSD, Solaris
7439a5
  10/11.
7439a5
7439a5
- Legacy (non-UEFI-compliant) PCI expansion ROMs, such as a VGA BIOS, mapped by
7439a5
  QEMU in emulated devices' ROM BARs, are loaded and executed by OVMF.
7439a5
7439a5
  For example, this grants the Windows Server 2008 R2 SP1 guest's native,
7439a5
  legacy video driver access to all modes of all QEMU video cards.
7439a5
7439a5
Building the CSM target of the SeaBIOS source tree is out of scope for this
7439a5
report. Additionally, upstream OVMF does not enable the CSM by default.
7439a5
7439a5
Interested users and developers should look for OVMF's "-D CSM_ENABLE"
7439a5
build-time option, and check out the <https://www.kraxel.org/repos/> continuous
7439a5
integration repository, which provides CSM-enabled OVMF builds.
7439a5
7439a5
[RHEL] The "OVMF_CODE.fd" firmware image made available on the Red Hat
7439a5
       Enterprise Linux 7.1 host does not include a Compatibility Support
7439a5
       Module, for the following reasons:
7439a5
7439a5
       - Virtual machines running officially supported, legacy guest operating
7439a5
         systems should just use the standalone SeaBIOS firmware. Firmware
7439a5
         selection is flexible in virtualization, see eg. "Installation of OVMF
7439a5
         guests with virt-manager and virt-install" above.
7439a5
7439a5
       - The 16-bit thunking interface between OVMF and SeaBIOS is very complex
7439a5
         and presents a large debugging and support burden, based on past
7439a5
         experience.
7439a5
7439a5
       - Secure Boot is incompatible with CSM.
7439a5
7439a5
       - Inter-project dependencies should be minimized whenever possible.
7439a5
7439a5
       - Using the default QXL video card, the Windows 2008 R2 SP1 guest can be
7439a5
         installed with its built-in, legacy video driver. Said driver will
7439a5
         select the only available video mode, 1024x768x32. After installation,
7439a5
         the video driver can be upgraded to the full-featured QXL XDDM driver.
7439a5
7439a5
Phases of the boot process
7439a5
--------------------------
7439a5
7439a5
The PI and UEFI specifications, and Intel's UEFI and EDK II Learning and
7439a5
Development materials provide ample information on PI and UEFI concepts. The
7439a5
following is an absolutely minimal, rough glossary that is included only to
7439a5
help readers new to PI and UEFI understand references in later, OVMF-specific
7439a5
sections. We defer heavily to the official specifications and the training
7439a5
materials, and frequently quote them below.
7439a5
7439a5
A central concept to mention early is the GUID -- globally unique identifier. A
7439a5
GUID is a 128-bit number, written as XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX,
7439a5
where each X stands for a hexadecimal nibble. GUIDs are used to name everything
7439a5
in PI and in UEFI. Programmers introduce new GUIDs with the "uuidgen" utility,
7439a5
and standards bodies standardize well-known services by positing their GUIDs.
7439a5
7439a5
The boot process is roughly divided in the following phases:
7439a5
7439a5
- Reset vector code.
7439a5
7439a5
- SEC: Security phase. This phase is the root of firmware integrity.
7439a5
7439a5
- PEI: Pre-EFI Initialization. This phase performs "minimal processor, chipset
7439a5
  and platform configuration for the purpose of discovering memory". Modules in
7439a5
  PEI collectively save their findings about the platform in a list of HOBs
7439a5
  (hand-off blocks).
7439a5
7439a5
  When developing PEI code, the Platform Initialization (PI) specification
7439a5
  should be consulted.
7439a5
7439a5
- DXE: Driver eXecution Environment, pronounced as "Dixie". This "is the phase
7439a5
  where the bulk of the booting occurs: devices are enumerated and initialized,
7439a5
  UEFI services are supported, and protocols and drivers are implemented. Also,
7439a5
  the tables that create the UEFI interface are produced".
7439a5
7439a5
  On the PEI/DXE boundary, the HOBs produced by PEI are consumed. For example,
7439a5
  this is how the memory space map is configured initially.
7439a5
7439a5
- BDS: Boot Device Selection. It is "responsible for determining how and where
7439a5
  you want to boot the operating system".
7439a5
7439a5
  When developing DXE and BDS code, it is mainly the UEFI specification that
7439a5
  should be consulted. When speaking about DXE, BDS is frequently considered to
7439a5
  be a part of it.
7439a5
7439a5
The following concepts are tied to specific boot process phases:
7439a5
7439a5
- PEIM: a PEI Module (pronounced "PIM"). A binary module running in the PEI
7439a5
  phase, consuming some PPIs and producing other PPIs, and producing HOBs.
7439a5
7439a5
- PPI: PEIM-to-PEIM interface. A structure of function pointers and related
7439a5
  data members that establishes a PEI service, or an instance of a PEI service.
7439a5
  PPIs are identified by GUID.
7439a5
7439a5
  An example is EFI_PEI_S3_RESUME2_PPI (6D582DBC-DB85-4514-8FCC-5ADF6227B147).
7439a5
7439a5
- DXE driver: a binary module running in the DXE and BDS phases, consuming some
7439a5
  protocols and producing other protocols.
7439a5
7439a5
- Protocol: A structure of function pointers and related data members that
7439a5
  establishes a DXE service, or an instance of a DXE service. Protocols are
7439a5
  identified by GUID.
7439a5
7439a5
  An example is EFI_BLOCK_IO_PROTOCOL (964E5B21-6459-11D2-8E39-00A0C969723B).
7439a5
7439a5
- Architectural protocols: a set of standard protocols that are foundational to
7439a5
  the working of a UEFI system. Each architectural protocol has at most one
7439a5
  instance. Architectural protocols are implemented by a subset of DXE drivers.
7439a5
  DXE drivers explicitly list the set of protocols (including architectural
7439a5
  protocols) that they need to work. UEFI drivers can only be loaded once all
7439a5
  architectural protocols have become available during the DXE phase.
7439a5
7439a5
  An example is EFI_VARIABLE_WRITE_ARCH_PROTOCOL
7439a5
  (6441F818-6362-4E44-B570-7DBA31DD2453).
7439a5
7439a5
Project structure
7439a5
-----------------
7439a5
7439a5
The term "OVMF" usually denotes the project (community and development effort)
7439a5
that provide and maintain the subject matter UEFI firmware for virtual
7439a5
machines. However the term is also frequently applied to the firmware binary
7439a5
proper that a virtual machine executes.
7439a5
7439a5
OVMF emerges as a compilation of several modules from the edk2 source
7439a5
repository. "edk2" stands for EFI Development Kit II; it is a "modern,
7439a5
feature-rich, cross-platform firmware development environment for the UEFI and
7439a5
PI specifications".
7439a5
7439a5
The composition of OVMF is dictated by the following build control files:
7439a5
7439a5
  OvmfPkg/OvmfPkgIa32.dsc
7439a5
  OvmfPkg/OvmfPkgIa32.fdf
7439a5
7439a5
  OvmfPkg/OvmfPkgIa32X64.dsc
7439a5
  OvmfPkg/OvmfPkgIa32X64.fdf
7439a5
7439a5
  OvmfPkg/OvmfPkgX64.dsc
7439a5
  OvmfPkg/OvmfPkgX64.fdf
7439a5
7439a5
The format of these files is described in the edk2 DSC and FDF specifications.
7439a5
Roughly, the DSC file determines:
7439a5
- library instance resolutions for library class requirements presented by the
7439a5
  modules to be compiled,
7439a5
- the set of modules to compile.
7439a5
7439a5
The FDF file roughly determines:
7439a5
- what binary modules (compilation output files, precompiled binaries, graphics
7439a5
  image files, verbatim binary sections) to include in the firmware image,
7439a5
- how to lay out the firmware image.
7439a5
7439a5
The Ia32 flavor of these files builds a firmware where both PEI and DXE phases
7439a5
are 32-bit. The Ia32X64 flavor builds a firmware where the PEI phase consists
7439a5
of 32-bit modules, and the DXE phase is 64-bit. The X64 flavor builds a purely
7439a5
64-bit firmware.
7439a5
7439a5
The word size of the DXE phase must match the word size of the runtime OS -- a
7439a5
32-bit DXE can't cooperate with a 64-bit OS, and a 64-bit DXE can't work a
7439a5
32-bit OS.
7439a5
7439a5
OVMF pulls together modules from across the edk2 tree. For example:
7439a5
7439a5
- common drivers and libraries that are platform independent are usually
7439a5
  located under MdeModulePkg and MdePkg,
7439a5
7439a5
- common but hardware-specific drivers and libraries that match QEMU's
7439a5
  pc-i440fx-* machine type are pulled in from IntelFrameworkModulePkg,
7439a5
  PcAtChipsetPkg and UefiCpuPkg,
7439a5
7439a5
- the platform independent UEFI Shell is built from ShellPkg,
7439a5
7439a5
- OvmfPkg includes drivers and libraries that are useful for virtual machines
7439a5
  and may or may not be specific to QEMU's pc-i440fx-* machine type.
7439a5
7439a5
Platform Configuration Database (PCD)
7439a5
-------------------------------------
7439a5
7439a5
Like the "Phases of the boot process" section, this one introduces a concept in
7439a5
very raw form. We defer to the PCD related edk2 specifications, and we won't
7439a5
discuss implementation details here. Our purpose is only to offer the reader a
7439a5
usable (albeit possibly inaccurate) definition, so that we can refer to PCDs
7439a5
later on.
7439a5
7439a5
Colloquially, when we say "PCD", we actually mean "PCD entry"; that is, an
7439a5
entry stored in the Platform Configuration Database.
7439a5
7439a5
The Platform Configuration Database is
7439a5
- a firmware-wide
7439a5
- name-value store
7439a5
- of scalars and buffers
7439a5
- where each entry may be
7439a5
  - build-time constant, or
7439a5
  - run-time dynamic, or
7439a5
  - theoretically, a middle option: patchable in the firmware file itself,
7439a5
    using a dedicated tool. (OVMF does not utilize externally patchable
7439a5
    entries.)
7439a5
7439a5
A PCD entry is declared in the DEC file of the edk2 top-level Package directory
7439a5
whose modules (drivers and libraries) are the primary consumers of the PCD
7439a5
entry. (See for example OvmfPkg/OvmfPkg.dec). Basically, a PCD in a DEC file
7439a5
exposes a simple customization point.
7439a5
7439a5
Interest in a PCD entry is communicated to the build system by naming the PCD
7439a5
entry in the INF file of the interested module (application, driver or
7439a5
library). The module may read and -- dependent on the PCD entry's category --
7439a5
write the PCD entry.
7439a5
7439a5
Let's investigate the characteristics of the Database and the PCD entries.
7439a5
7439a5
- Firmware-wide: technically, all modules may access all entries they are
7439a5
  interested in, assuming they advertise their interest in their INF files.
7439a5
  With careful design, PCDs enable inter-driver propagation of (simple) system
7439a5
  configuration. PCDs are available in both PEI and DXE.
7439a5
7439a5
  (UEFI drivers meant to be portable (ie. from third party vendors) are not
7439a5
  supposed to use PCDs, since PCDs qualify internal to the specific edk2
7439a5
  firmware in question.)
7439a5
7439a5
- Name-value store of scalars and buffers: each PCD has a symbolic name, and a
7439a5
  fixed scalar type (UINT16, UINT32 etc), or VOID* for buffers. Each PCD entry
7439a5
  belongs to a namespace, where a namespace is (obviously) a GUID, defined in
7439a5
  the DEC file.
7439a5
7439a5
- A DEC file can permit several categories for a PCD:
7439a5
  - build-time constant ("FixedAtBuild"),
7439a5
  - patchable in the firmware image ("PatchableInModule", unused in OVMF),
7439a5
  - runtime modifiable ("Dynamic").
7439a5
7439a5
The platform description file (DSC) of a top-level Package directory may choose
7439a5
the exact category for a given PCD entry that its modules wish to use, and
7439a5
assign a default (or constant) initial value to it.
7439a5
7439a5
In addition, the edk2 build system too can initialize PCD entries to values
7439a5
that it calculates while laying out the flash device image. Such PCD
7439a5
assignments are described in the FDF control file.
7439a5
7439a5
Firmware image structure
7439a5
------------------------
7439a5
7439a5
(We assume the common X64 choice for both PEI and DXE, and the default DEBUG
7439a5
build target.)
7439a5
7439a5
The OvmfPkg/OvmfPkgX64.fdf file defines the following layout for the flash
7439a5
device image "OVMF.fd":
7439a5
7439a5
  Description                     Compression type        Size
7439a5
  ------------------------------  ----------------------  -------
7439a5
  Non-volatile data storage       open-coded binary data   128 KB
7439a5
    Variable store                                          56 KB
7439a5
    Event log                                                4 KB
7439a5
    Working block                                            4 KB
7439a5
    Spare area                                              64 KB
7439a5
7439a5
  FVMAIN_COMPACT                  uncompressed            1712 KB
7439a5
    FV Firmware File System file  LZMA compressed
7439a5
      PEIFV                       uncompressed             896 KB
7439a5
        individual PEI modules    uncompressed
7439a5
      DXEFV                       uncompressed            8192 KB
7439a5
        individual DXE modules    uncompressed
7439a5
7439a5
  SECFV                           uncompressed             208 KB
7439a5
    SEC driver
7439a5
    reset vector code
7439a5
7439a5
The top-level image consists of three regions (three firmware volumes):
7439a5
- non-volatile data store (128 KB),
7439a5
- main firmware volume (FVMAIN_COMPACT, 1712 KB),
7439a5
- firmware volume containing the reset vector code and the SEC phase code (208
7439a5
  KB).
7439a5
7439a5
In total, the OVMF.fd file has size 128 KB + 1712 KB + 208 KB == 2 MB.
7439a5
7439a5
(1) The firmware volume with non-volatile data store (128 KB) has the following
7439a5
    internal structure, in blocks of 4 KB:
7439a5
7439a5
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  L: event log
7439a5
       LIVE | varstore                  |L|W|  W: working block
7439a5
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
7439a5
7439a5
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
7439a5
      SPARE |                               |
7439a5
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
7439a5
7439a5
    The first half of this firmware volume is "live", while the second half is
7439a5
    "spare". The spare half is important when the variable driver reclaims
7439a5
    unused storage and reorganizes the variable store.
7439a5
7439a5
    The live half dedicates 14 blocks (56 KB) to the variable store itself. On
7439a5
    top of those, one block is set aside for an event log, and one block is
7439a5
    used as the working block of the fault tolerant write protocol. Fault
7439a5
    tolerant writes are used to recover from an occasional (virtual) power loss
7439a5
    during variable updates.
7439a5
7439a5
    The blocks in this firmware volume are accessed, in stacking order from
7439a5
    least abstract to most abstract, by:
7439a5
7439a5
    - EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL (provided by
7439a5
      OvmfPkg/QemuFlashFvbServicesRuntimeDxe),
7439a5
7439a5
    - EFI_FAULT_TOLERANT_WRITE_PROTOCOL (provided by
7439a5
      MdeModulePkg/Universal/FaultTolerantWriteDxe),
7439a5
7439a5
    - architectural protocols instrumental to the runtime UEFI variable
7439a5
      services:
7439a5
      - EFI_VARIABLE_ARCH_PROTOCOL,
7439a5
      - EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
7439a5
7439a5
      In a non-secure boot build, the DXE driver providing these architectural
7439a5
      protocols is MdeModulePkg/Universal/Variable/RuntimeDxe. In a secure boot
7439a5
      build, where authenticated variables are available, the DXE driver
7439a5
      offering these protocols is SecurityPkg/VariableAuthenticated/RuntimeDxe.
7439a5
7439a5
(2) The main firmware volume (FVMAIN_COMPACT, 1712 KB) embeds further firmware
7439a5
    volumes. The outermost layer is a Firmware File System (FFS), carrying a
7439a5
    single file. This file holds an LZMA-compressed section, which embeds two
7439a5
    firmware volumes: PEIFV (896 KB) with PEIMs, and DXEFV (8192 KB) with DXE
7439a5
    and UEFI drivers.
7439a5
7439a5
    This scheme enables us to build 896 KB worth of PEI drivers and 8192 KB
7439a5
    worth of DXE and UEFI drivers, compress them all with LZMA in one go, and
7439a5
    store the compressed result in 1712 KB, saving room in the flash device.
7439a5
7439a5
(3) The SECFV firmware volume (208 KB) is not compressed. It carries the
7439a5
    "volume top file" with the reset vector code, to end at 4 GB in
7439a5
    guest-physical address space, and the SEC phase driver (OvmfPkg/Sec).
7439a5
7439a5
    The last 16 bytes of the volume top file (mapped directly under 4 GB)
7439a5
    contain a NOP slide and a jump instruction. This is where QEMU starts
7439a5
    executing the firmware, at address 0xFFFF_FFF0. The reset vector and the
7439a5
    SEC driver run from flash directly.
7439a5
7439a5
    The SEC driver locates FVMAIN_COMPACT in the flash, and decompresses the
7439a5
    main firmware image to RAM. The rest of OVMF (PEI, DXE, BDS phases) run
7439a5
    from RAM.
7439a5
7439a5
As already mentioned, the OVMF.fd file is mapped by qemu's
7439a5
"hw/block/pflash_cfi01.c" device just under 4 GB in guest-physical address
7439a5
space, according to the command line option
7439a5
7439a5
  -drive if=pflash,format=raw,file=fedora.flash
7439a5
7439a5
(refer to the Example qemu invocation). This is a "ROMD device", which can
7439a5
switch out of "ROMD mode" and back into it.
7439a5
7439a5
Namely, in the default ROMD mode, the guest-physical address range backed by
7439a5
the flash device reads and executes as ROM (it does not trap from KVM to QEMU).
7439a5
The first write access in this mode traps to QEMU, and flips the device out of
7439a5
ROMD mode.
7439a5
7439a5
In non-ROMD mode, the flash chip is programmed by storing CFI (Common Flash
7439a5
Interface) command values at the flash-covered addresses; both reads and writes
7439a5
trap to QEMU, and the flash contents are modified and synchronized to the
7439a5
host-side file. A special CFI command flips the flash device back to ROMD mode.
7439a5
7439a5
Qemu implements the above based on the KVM_CAP_READONLY_MEM / KVM_MEM_READONLY
7439a5
KVM features, and OVMF puts it to use in its EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
7439a5
implementation, under "OvmfPkg/QemuFlashFvbServicesRuntimeDxe".
7439a5
7439a5
IMPORTANT: Never pass OVMF.fd to qemu with the -bios option. That option maps
7439a5
the firmware image as ROM into the guest's address space, and forces OVMF to
7439a5
emulate non-volatile variables with a fallback driver that is bound to have
7439a5
insufficient and confusing semantics.
7439a5
7439a5
The 128 KB firmware volume with the variable store, discussed under (1), is
7439a5
also built as a separate host-side file, named "OVMF_VARS.fd". The "rest" is
7439a5
built into a third file, "OVMF_CODE.fd", which is only 1920 KB in size. The
7439a5
variable store is mapped into its usual location, at 4 GB - 2 MB = 0xFFE0_0000,
7439a5
through the following qemu options:
7439a5
7439a5
  -drive if=pflash,format=raw,readonly,file=OVMF_CODE.fd   \
7439a5
  -drive if=pflash,format=raw,file=fedora.varstore.fd
7439a5
7439a5
This way qemu configures two flash chips consecutively, with start addresses
7439a5
growing downwards, which is transparent to OVMF.
7439a5
7439a5
[RHEL] Red Hat Enterprise Linux 7.1 ships a Secure Boot-enabled, X64, DEBUG
7439a5
       firmware only. Furthermore, only the split files ("OVMF_VARS.fd" and
7439a5
       "OVMF_CODE.fd") are available.
7439a5
7439a5
S3 (suspend to RAM and resume)
7439a5
------------------------------
7439a5
7439a5
As noted in Example qemu invocation, the
7439a5
7439a5
  -global PIIX4_PM.disable_s3=0
7439a5
7439a5
command line option tells qemu and OVMF if the user would like to enable S3
7439a5
support. (This is corresponds to the /domain/pm/suspend-to-mem/@enabled libvirt
7439a5
domain XML attribute.)
7439a5
7439a5
Implementing / orchestrating S3 was a considerable community effort in OVMF. A
7439a5
detailed description exceeds the scope of this report; we only make a few
7439a5
statements.
7439a5
7439a5
(1) S3-related PPIs and protocols are well documented in the PI specification.
7439a5
7439a5
(2) Edk2 contains most modules that are needed to implement S3 on a given
7439a5
    platform. One abstraction that is central to the porting / extending of the
7439a5
    S3-related modules to a new platform is the LockBox library interface,
7439a5
    which a specific platform can fill in by implementing its own LockBox
7439a5
    library instance.
7439a5
7439a5
    The LockBox library provides a privileged name-value store (to be addressed
7439a5
    by GUIDs). The privilege separation stretches between the firmware and the
7439a5
    operating system. That is, the S3-related machinery of the firmware saves
7439a5
    some items in the LockBox securely, under well-known GUIDs, before booting
7439a5
    the operating system. During resume (which is a form of warm reset), the
7439a5
    firmware is activated again, and retrieves items from the LockBox. Before
7439a5
    jumping to the OS's resume vector, the LockBox is secured again.
7439a5
7439a5
    We'll return to this later when we separately discuss SMRAM and SMM.
7439a5
7439a5
(3) During resume, the DXE and later phases are never reached; only the reset
7439a5
    vector, and the SEC and PEI phases of the firmware run. The platform is
7439a5
    supposed to detect a resume in progress during PEI, and to store that fact
7439a5
    in the BootMode field of the Phase Handoff Information Table (PHIT) HOB.
7439a5
    OVMF keys this off the CMOS, see OvmfPkg/PlatformPei.
7439a5
7439a5
    At the end of PEI, the DXE IPL PEIM (Initial Program Load PEI Module, see
7439a5
    MdeModulePkg/Core/DxeIplPeim) examines the Boot Mode, and if it says "S3
7439a5
    resume in progress", then the IPL branches to the PEIM that exports
7439a5
    EFI_PEI_S3_RESUME2_PPI (provided by UefiCpuPkg/Universal/Acpi/S3Resume2Pei)
7439a5
    rather than loading the DXE core.
7439a5
7439a5
    S3Resume2Pei executes the technical steps of the resumption, relying on the
7439a5
    contents of the LockBox.
7439a5
7439a5
(4) During first boot (or after a normal platform reset), when DXE does run,
7439a5
    hardware drivers in the DXE phase are encouraged to "stash" their hardware
7439a5
    configuration steps (eg. accesses to PCI config space, I/O ports, memory
7439a5
    mapped addresses, and so on) in a centrally maintained, so called "S3 boot
7439a5
    script". Hardware accesses are represented with opcodes of a special binary
7439a5
    script language.
7439a5
7439a5
    This boot script is to be replayed during resume, by S3Resume2Pei. The
7439a5
    general goal is to bring back hardware devices -- which have been powered
7439a5
    off during suspend -- to their original after-first-boot state, and in
7439a5
    particular, to do so quickly.
7439a5
7439a5
    At the moment, OVMF saves only one opcode in the S3 resume boot script: an
7439a5
    INFORMATION opcode, with contents 0xDEADBEEF (in network byte order). The
7439a5
    consensus between Linux developers seems to be that boot firmware is only
7439a5
    responsible for restoring basic chipset state, which OVMF does during PEI
7439a5
    anyway, independently of S3 vs. normal reset. (One example is the power
7439a5
    management registers of the i440fx chipset.) Device and peripheral state is
7439a5
    the responsibility of the runtime operating system.
7439a5
7439a5
    Although an experimental OVMF S3 boot script was at one point captured for
7439a5
    the virtual Cirrus VGA card, such a boot script cannot follow eg. video
7439a5
    mode changes effected by the OS. Hence the operating system can never avoid
7439a5
    restoring device state, and most Linux display drivers (eg. stdvga, QXL)
7439a5
    already cover S3 resume fully.
7439a5
7439a5
    The XDDM and WDDM driver models used under Windows OSes seem to recognize
7439a5
    this notion of runtime OS responsibility as well. (See the list of OSes
7439a5
    supported by OVMF in a separate section.)
7439a5
7439a5
(5) The S3 suspend/resume data flow in OVMF is included here tersely, for
7439a5
    interested developers.
7439a5
7439a5
    (a) BdsLibBootViaBootOption()
7439a5
          EFI_ACPI_S3_SAVE_PROTOCOL [AcpiS3SaveDxe]
7439a5
          - saves ACPI S3 Context to LockBox  ---------------------+
7439a5
            (including FACS address -- FACS ACPI table             |
7439a5
            contains OS waking vector)                             |
7439a5
                                                                   |
7439a5
          - prepares boot script:                                  |
7439a5
            EFI_S3_SAVE_STATE_PROTOCOL.Write() [S3SaveStateDxe]    |
7439a5
              S3BootScriptLib [PiDxeS3BootScriptLib]               |
7439a5
              - opcodes & arguments are saved in NVS.  --+         |
7439a5
                                                         |         |
7439a5
          - issues a notification by installing          |         |
7439a5
            EFI_DXE_SMM_READY_TO_LOCK_PROTOCOL           |         |
7439a5
                                                         |         |
7439a5
    (b) EFI_S3_SAVE_STATE_PROTOCOL [S3SaveStateDxe]      |         |
7439a5
          S3BootScriptLib [PiDxeS3BootScriptLib]         |         |
7439a5
          - closes script with special opcode  <---------+         |
7439a5
          - script is available in non-volatile memory             |
7439a5
            via PcdS3BootScriptTablePrivateDataPtr  --+            |
7439a5
                                                      |            |
7439a5
        BootScriptExecutorDxe                         |            |
7439a5
          S3BootScriptLib [PiDxeS3BootScriptLib]      |            |
7439a5
          - Knows about boot script location by  <----+            |
7439a5
            synchronizing with the other library                   |
7439a5
            instance via                                           |
7439a5
            PcdS3BootScriptTablePrivateDataPtr.                    |
7439a5
          - Copies relocated image of itself to                    |
7439a5
            reserved memory. --------------------------------+     |
7439a5
          - Saved image contains pointer to boot script.  ---|--+  |
7439a5
                                                             |  |  |
7439a5
    Runtime:                                                 |  |  |
7439a5
                                                             |  |  |
7439a5
    (c) OS is booted, writes OS waking vector to FACS,       |  |  |
7439a5
        suspends machine                                     |  |  |
7439a5
                                                             |  |  |
7439a5
    S3 Resume (PEI):                                         |  |  |
7439a5
                                                             |  |  |
7439a5
    (d) PlatformPei sets S3 Boot Mode based on CMOS          |  |  |
7439a5
                                                             |  |  |
7439a5
    (e) DXE core is skipped and EFI_PEI_S3_RESUME2 is        |  |  |
7439a5
        called as last step of PEI                           |  |  |
7439a5
                                                             |  |  |
7439a5
    (f) S3Resume2Pei retrieves from LockBox:                 |  |  |
7439a5
        - ACPI S3 Context (path to FACS)  <------------------|--|--+
7439a5
                                          |                  |  |
7439a5
                                          +------------------|--|--+
7439a5
        - Boot Script Executor Image  <----------------------+  |  |
7439a5
                                                                |  |
7439a5
    (g) BootScriptExecutorDxe                                   |  |
7439a5
          S3BootScriptLib [PiDxeS3BootScriptLib]                |  |
7439a5
          - executes boot script  <-----------------------------+  |
7439a5
                                                                   |
7439a5
    (h) OS waking vector available from ACPI S3 Context / FACS  <--+
7439a5
        is called
7439a5
7439a5
A comprehensive memory map of OVMF
7439a5
----------------------------------
7439a5
7439a5
The following section gives a detailed analysis of memory ranges below 4 GB
7439a5
that OVMF statically uses.
7439a5
7439a5
In the rightmost column, the PCD entry is identified by which the source refers
7439a5
to the address or size in question.
7439a5
7439a5
The flash-covered range has been discussed previously in "Firmware image
7439a5
structure", therefore we include it only for completeness. Due to the fact that
7439a5
this range is always backed by a memory mapped device (and never RAM), it is
7439a5
unaffected by S3 (suspend to RAM and resume).
7439a5
7439a5
+--------------------------+ 4194304 KB
7439a5
|                          |
7439a5
|          SECFV           | size: 208 KB
7439a5
|                          |
7439a5
+--------------------------+ 4194096 KB
7439a5
|                          |
7439a5
|      FVMAIN_COMPACT      | size: 1712 KB
7439a5
|                          |
7439a5
+--------------------------+ 4192384 KB
7439a5
|                          |
7439a5
|      variable store      | size: 64 KB   PcdFlashNvStorageFtwSpareSize
7439a5
|        spare area        |
7439a5
|                          |
7439a5
+--------------------------+ 4192320 KB    PcdOvmfFlashNvStorageFtwSpareBase
7439a5
|                          |
7439a5
|    FTW working block     | size: 4 KB    PcdFlashNvStorageFtwWorkingSize
7439a5
|                          |
7439a5
+--------------------------+ 4192316 KB    PcdOvmfFlashNvStorageFtwWorkingBase
7439a5
|                          |
7439a5
|       Event log of       | size: 4 KB    PcdOvmfFlashNvStorageEventLogSize
7439a5
|   non-volatile storage   |
7439a5
|                          |
7439a5
+--------------------------+ 4192312 KB    PcdOvmfFlashNvStorageEventLogBase
7439a5
|                          |
7439a5
|      variable store      | size: 56 KB   PcdFlashNvStorageVariableSize
7439a5
|                          |
7439a5
+--------------------------+ 4192256 KB    PcdOvmfFlashNvStorageVariableBase
7439a5
7439a5
The flash-mapped image of OVMF.fd covers the entire structure above (2048 KB).
7439a5
7439a5
When using the split files, the address 4192384 KB
7439a5
(PcdOvmfFlashNvStorageFtwSpareBase + PcdFlashNvStorageFtwSpareSize) is the
7439a5
boundary between the mapped images of OVMF_VARS.fd (56 KB + 4 KB + 4 KB + 64 KB
7439a5
= 128 KB) and OVMF_CODE.fd (1712 KB + 208 KB = 1920 KB).
7439a5
7439a5
With regard to RAM that is statically used by OVMF, S3 (suspend to RAM and
7439a5
resume) complicates matters. Many ranges have been introduced only to support
7439a5
S3, hence for all ranges below, the following questions will be audited:
7439a5
7439a5
(a) when and how a given range is initialized after first boot of the VM,
7439a5
(b) how it is protected from memory allocations during DXE,
7439a5
(c) how it is protected from the OS,
7439a5
(d) how it is accessed on the S3 resume path,
7439a5
(e) how it is accessed on the warm reset path.
7439a5
7439a5
Importantly, the term "protected" is meant as protection against inadvertent
7439a5
reallocations and overwrites by co-operating DXE and OS modules. It does not
7439a5
imply security against malicious code.
7439a5
7439a5
+--------------------------+ 17408 KB
7439a5
|                          |
7439a5
|DXEFV from FVMAIN_COMPACT | size: 8192 KB PcdOvmfDxeMemFvSize
7439a5
|  decompressed firmware   |
7439a5
| volume with DXE modules  |
7439a5
|                          |
7439a5
+--------------------------+ 9216 KB       PcdOvmfDxeMemFvBase
7439a5
|                          |
7439a5
|PEIFV from FVMAIN_COMPACT | size: 896 KB  PcdOvmfPeiMemFvSize
7439a5
|  decompressed firmware   |
7439a5
| volume with PEI modules  |
7439a5
|                          |
7439a5
+--------------------------+ 8320 KB       PcdOvmfPeiMemFvBase
7439a5
|                          |
7439a5
| permanent PEI memory for | size: 32 KB   PcdS3AcpiReservedMemorySize
7439a5
|   the S3 resume path     |
7439a5
|                          |
7439a5
+--------------------------+ 8288 KB       PcdS3AcpiReservedMemoryBase
7439a5
|                          |
7439a5
|  temporary SEC/PEI heap  | size: 32 KB   PcdOvmfSecPeiTempRamSize
7439a5
|         and stack        |
7439a5
|                          |
7439a5
+--------------------------+ 8256 KB       PcdOvmfSecPeiTempRamBase
7439a5
|                          |
7439a5
|          unused          | size: 32 KB
7439a5
|                          |
7439a5
+--------------------------+ 8224 KB
7439a5
|                          |
7439a5
|      SEC's table of      | size: 4 KB    PcdGuidedExtractHandlerTableSize
7439a5
| GUIDed section handlers  |
7439a5
|                          |
7439a5
+--------------------------+ 8220 KB       PcdGuidedExtractHandlerTableAddress
7439a5
|                          |
7439a5
|     LockBox storage      | size: 4 KB    PcdOvmfLockBoxStorageSize
7439a5
|                          |
7439a5
+--------------------------+ 8216 KB       PcdOvmfLockBoxStorageBase
7439a5
|                          |
7439a5
| early page tables on X64 | size: 24 KB   PcdOvmfSecPageTablesSize
7439a5
|                          |
7439a5
+--------------------------+ 8192 KB       PcdOvmfSecPageTablesBase
7439a5
7439a5
(1) Early page tables on X64:
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    The range is filled in during the SEC phase
7439a5
    [OvmfPkg/ResetVector/Ia32/PageTables64.asm]. The CR3 register is verified
7439a5
    against the base address in SecCoreStartupWithStack()
7439a5
    [OvmfPkg/Sec/SecMain.c].
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    If S3 was enabled on the QEMU command line (see "-global
7439a5
    PIIX4_PM.disable_s3=0" earlier), then InitializeRamRegions()
7439a5
    [OvmfPkg/PlatformPei/MemDetect.c] protects the range with an AcpiNVS memory
7439a5
    allocation HOB, in PEI.
7439a5
7439a5
    If S3 was disabled, then this range is not protected. DXE's own page tables
7439a5
    are first built while still in PEI (see HandOffToDxeCore()
7439a5
    [MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c]). Those tables are located
7439a5
    in permanent PEI memory. After CR3 is switched over to them (which occurs
7439a5
    before jumping to the DXE core entry point), we don't have to preserve the
7439a5
    initial tables.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    If S3 is enabled, then (1b) reserves it from the OS too.
7439a5
7439a5
    If S3 is disabled, then the range needs no protection.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    It is rewritten same as in (1a), which is fine because (1c) reserved it.
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    It is rewritten same as in (1a).
7439a5
7439a5
(2) LockBox storage:
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    InitializeRamRegions() [OvmfPkg/PlatformPei/MemDetect.c] zeroes out the
7439a5
    area during PEI. This is correct but not strictly necessary, since on first
7439a5
    boot the area is zero-filled anyway.
7439a5
7439a5
    The LockBox signature of the area is filled in by the PEI module or DXE
7439a5
    driver that has been linked against OVMF's LockBoxLib and is run first. The
7439a5
    signature is written in LockBoxLibInitialize()
7439a5
    [OvmfPkg/Library/LockBoxLib/LockBoxLib.c].
7439a5
7439a5
    Any module calling SaveLockBox() [OvmfPkg/Library/LockBoxLib/LockBoxLib.c]
7439a5
    will co-populate this area.
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    If S3 is enabled, then InitializeRamRegions()
7439a5
    [OvmfPkg/PlatformPei/MemDetect.c] protects the range as AcpiNVS.
7439a5
7439a5
    Otherwise, the range is covered with a BootServicesData memory allocation
7439a5
    HOB.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    If S3 is enabled, then (2b) protects it sufficiently.
7439a5
7439a5
    Otherwise the range requires no runtime protection, and the
7439a5
    BootServicesData allocation type from (2b) ensures that the range will be
7439a5
    released to the OS.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    The S3 Resume PEIM restores data from the LockBox, which has been correctly
7439a5
    protected in (2c).
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    InitializeRamRegions() [OvmfPkg/PlatformPei/MemDetect.c] zeroes out the
7439a5
    range during PEI, effectively emptying the LockBox. Modules will
7439a5
    re-populate the LockBox as described in (2a).
7439a5
7439a5
(3) SEC's table of GUIDed section handlers
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    The following two library instances are linked into SecMain:
7439a5
    - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib,
7439a5
    - MdePkg/Library/BaseExtractGuidedSectionLib.
7439a5
7439a5
    The first library registers its LZMA decompressor plugin (which is a called
7439a5
    a "section handler") by calling the second library:
7439a5
7439a5
    LzmaDecompressLibConstructor() [GuidedSectionExtraction.c]
7439a5
      ExtractGuidedSectionRegisterHandlers() [BaseExtractGuidedSectionLib.c]
7439a5
7439a5
    The second library maintains its table of registered "section handlers", to
7439a5
    be indexed by GUID, in this fixed memory area, independently of S3
7439a5
    enablement.
7439a5
7439a5
    (The decompression of FVMAIN_COMPACT's FFS file section that contains the
7439a5
    PEIFV and DXEFV firmware volumes occurs with the LZMA decompressor
7439a5
    registered above. See (6) and (7) below.)
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    There is no need to protect this area from DXE: because nothing else in
7439a5
    OVMF links against BaseExtractGuidedSectionLib, the area loses its
7439a5
    significance as soon as OVMF progresses from SEC to PEI, therefore DXE is
7439a5
    allowed to overwrite the region.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    When S3 is enabled, we cover the range with an AcpiNVS memory allocation
7439a5
    HOB in InitializeRamRegions().
7439a5
7439a5
    When S3 is disabled, the range is not protected.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    The table of registered section handlers is again managed by
7439a5
    BaseExtractGuidedSectionLib linked into SecMain exclusively. Section
7439a5
    handler registrations update the table in-place (based on GUID matches).
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    If S3 is enabled, then the OS won't damage the table (due to (3c)), thus
7439a5
    see (3d).
7439a5
7439a5
    If S3 is disabled, then the OS has most probably overwritten the range with
7439a5
    its own data, hence (3a) -- complete reinitialization -- will come into
7439a5
    effect, based on the table signature check in BaseExtractGuidedSectionLib.
7439a5
7439a5
(4) temporary SEC/PEI heap and stack
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    The range is configured in [OvmfPkg/Sec/X64/SecEntry.S] and
7439a5
    SecCoreStartupWithStack() [OvmfPkg/Sec/SecMain.c]. The stack half is read &
7439a5
    written by the CPU transparently. The heap half is used for memory
7439a5
    allocations during PEI.
7439a5
7439a5
    Data is migrated out (to permanent PEI stack & memory) in (or soon after)
7439a5
    PublishPeiMemory() [OvmfPkg/PlatformPei/MemDetect.c].
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    It is not necessary to protect this range during DXE because its use ends
7439a5
    still in PEI.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    If S3 is enabled, then InitializeRamRegions()
7439a5
    [OvmfPkg/PlatformPei/MemDetect.c] reserves it as AcpiNVS.
7439a5
7439a5
    If S3 is disabled, then the range doesn't require protection.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    Same as in (4a), except the target area of the migration triggered by
7439a5
    PublishPeiMemory() [OvmfPkg/PlatformPei/MemDetect.c] is different -- see
7439a5
    (5).
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    Same as in (4a). The stack and heap halves both may contain garbage, but it
7439a5
    doesn't matter.
7439a5
7439a5
(5) permanent PEI memory for the S3 resume path
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    No particular initialization or use.
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    We don't need to protect this area during DXE.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    When S3 is enabled, InitializeRamRegions()
7439a5
    [OvmfPkg/PlatformPei/MemDetect.c] makes sure the OS stays away by covering
7439a5
    the range with an AcpiNVS memory allocation HOB.
7439a5
7439a5
    When S3 is disabled, the range needs no protection.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    PublishPeiMemory() installs the range as permanent RAM for PEI. The range
7439a5
    will serve as stack and will satisfy allocation requests during the rest of
7439a5
    PEI. OS data won't overlap due to (5c).
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    Same as (5a).
7439a5
7439a5
(6) PEIFV -- decompressed firmware volume with PEI modules
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    DecompressMemFvs() [OvmfPkg/Sec/SecMain.c] populates the area, by
7439a5
    decompressing the flash-mapped FVMAIN_COMPACT volume's contents. (Refer to
7439a5
    "Firmware image structure".)
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    When S3 is disabled, PeiFvInitialization() [OvmfPkg/PlatformPei/Fv.c]
7439a5
    covers the range with a BootServicesData memory allocation HOB.
7439a5
7439a5
    When S3 is enabled, the same is coverage is ensured, just with the stronger
7439a5
    AcpiNVS memory allocation type.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    When S3 is disabled, it is not necessary to keep the range from the OS.
7439a5
7439a5
    Otherwise the AcpiNVS type allocation from (6b) provides coverage.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    Rather than decompressing it again from FVMAIN_COMPACT, GetS3ResumePeiFv()
7439a5
    [OvmfPkg/Sec/SecMain.c] reuses the protected area for parsing / execution
7439a5
    from (6c).
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    Same as (6a).
7439a5
7439a5
(7) DXEFV -- decompressed firmware volume with DXE modules
7439a5
7439a5
  (a) when and how it is initialized after first boot of the VM
7439a5
7439a5
    Same as (6a).
7439a5
7439a5
  (b) how it is protected from memory allocations during DXE
7439a5
7439a5
    PeiFvInitialization() [OvmfPkg/PlatformPei/Fv.c] covers the range with a
7439a5
    BootServicesData memory allocation HOB.
7439a5
7439a5
  (c) how it is protected from the OS
7439a5
7439a5
    The OS is allowed to release and reuse this range.
7439a5
7439a5
  (d) how it is accessed on the S3 resume path
7439a5
7439a5
    It's not; DXE never runs during S3 resume.
7439a5
7439a5
  (e) how it is accessed on the warm reset path
7439a5
7439a5
    Same as in (7a).
7439a5
7439a5
Known Secure Boot limitations
7439a5
-----------------------------
7439a5
7439a5
Under "Motivation" we've mentioned that OVMF's Secure Boot implementation is
7439a5
not suitable for production use yet -- it's only good for development and
7439a5
testing of standards-conformant, non-malicious guest code (UEFI and operating
7439a5
system alike).
7439a5
7439a5
Now that we've examined the persistent flash device, the workings of S3, and
7439a5
the memory map, we can discuss two currently known shortcomings of OVMF's
7439a5
Secure Boot that in fact make it insecure. (Clearly problems other than these
7439a5
two might exist; the set of issues considered here is not meant to be
7439a5
exhaustive.)
7439a5
7439a5
One trait of Secure Boot is tamper-evidence. Secure Boot may not prevent
7439a5
malicious modification of software components (for example, operating system
7439a5
drivers), but by being the root of integrity on a platform, it can catch (or
7439a5
indirectly contribute to catching) unauthorized changes, by way of signature
7439a5
and certificate checks at the earliest phases of boot.
7439a5
7439a5
If an attacker can tamper with key material stored in authenticated and/or
7439a5
boot-time only persistent variables (for example, PK, KEK, db, dbt, dbx), then
7439a5
the intended security of this scheme is compromised. The UEFI 2.4A
7439a5
specification says
7439a5
7439a5
- in section 28.3.4:
7439a5
7439a5
  Platform Keys:
7439a5
7439a5
    The public key must be stored in non-volatile storage which is tamper and
7439a5
    delete resistant.
7439a5
7439a5
  Key Exchange Keys:
7439a5
7439a5
    The public key must be stored in non-volatile storage which is tamper
7439a5
    resistant.
7439a5
7439a5
- in section 28.6.1:
7439a5
7439a5
  The signature database variables db, dbt, and dbx must be stored in
7439a5
  tamper-resistant non-volatile storage.
7439a5
7439a5
(1) The combination of QEMU, KVM, and OVMF does not provide this kind of
7439a5
    resistance. The variable store in the emulated flash chip is directly
7439a5
    accessible to, and reprogrammable by, UEFI drivers, applications, and
7439a5
    operating systems.
7439a5
7439a5
(2) Under "S3 (suspend to RAM and resume)" we pointed out that the LockBox
7439a5
    storage must be similarly secure and tamper-resistant.
7439a5
7439a5
    On the S3 resume path, the PEIM providing EFI_PEI_S3_RESUME2_PPI
7439a5
    (UefiCpuPkg/Universal/Acpi/S3Resume2Pei) restores and interprets data from
7439a5
    the LockBox that has been saved there during boot. This PEIM, being part of
7439a5
    the firmware, has full access to the platform. If an operating system can
7439a5
    tamper with the contents of the LockBox, then at the next resume the
7439a5
    platform's integrity might be subverted.
7439a5
7439a5
    OVMF stores the LockBox in normal guest RAM (refer to the memory map
7439a5
    section above). Operating systems and third party UEFI drivers and UEFI
7439a5
    applications that respect the UEFI memory map will not inadvertently
7439a5
    overwrite the LockBox storage, but there's nothing to prevent eg. a
7439a5
    malicious kernel from modifying the LockBox.
7439a5
7439a5
One means to address these issues is SMM and SMRAM (System Management Mode and
7439a5
System Management RAM).
7439a5
7439a5
During boot and resume, the firmware can enter and leave SMM and access SMRAM.
7439a5
Before the DXE phase is left, and control is transferred to the BDS phase (when
7439a5
third party UEFI drivers and applications can be loaded, and an operating
7439a5
system can be loaded), SMRAM is locked in hardware, and subsequent modules
7439a5
cannot access it directly. (See EFI_DXE_SMM_READY_TO_LOCK_PROTOCOL.)
7439a5
7439a5
Once SMRAM has been locked, UEFI drivers and the operating system can enter SMM
7439a5
by raising a System Management Interrupt (SMI), at which point trusted code
7439a5
(part of the platform firmware) takes control. SMRAM is also unlocked by
7439a5
platform reset, at which point the boot firmware takes control again.
7439a5
7439a5
Variable store and LockBox in SMRAM
7439a5
-----------------------------------
7439a5
7439a5
Edk2 provides almost all components to implement the variable store and the
7439a5
LockBox in SMRAM. In this section we summarize ideas for utilizing those
7439a5
facilities.
7439a5
7439a5
The SMRAM and SMM infrastructure in edk2 is built up as follows:
7439a5
7439a5
(1) The platform hardware provides SMM / SMI / SMRAM.
7439a5
7439a5
    Qemu/KVM doesn't support these features currently and should implement them
7439a5
    in the longer term.
7439a5
7439a5
(2) The platform vendor (in this case, OVMF developers) implement device
7439a5
    drivers for the platform's System Management Mode:
7439a5
7439a5
    - EFI_SMM_CONTROL2_PROTOCOL: for raising a synchronous (and/or) periodic
7439a5
      SMI(s); that is, for entering SMM.
7439a5
7439a5
    - EFI_SMM_ACCESS2_PROTOCOL: for describing and accessing SMRAM.
7439a5
7439a5
    These protocols are documented in the PI Specification, Volume 4.
7439a5
7439a5
(3) The platform DSC file is to include the following platform-independent
7439a5
    modules:
7439a5
7439a5
    - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf: SMM Initial Program Load
7439a5
    - MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf: SMM Core
7439a5
7439a5
(4) At this point, modules of type DXE_SMM_DRIVER can be loaded.
7439a5
7439a5
    Such drivers are privileged. They run in SMM, have access to SMRAM, and are
7439a5
    separated and switched from other drivers through SMIs. Secure
7439a5
    communication between unprivileged (non-SMM) and privileged (SMM) drivers
7439a5
    happens through EFI_SMM_COMMUNICATION_PROTOCOL (implemented by the SMM
7439a5
    Core, see (3)).
7439a5
7439a5
    DXE_SMM_DRIVER modules must sanitize their input (coming from unprivileged
7439a5
    drivers) carefully.
7439a5
7439a5
(5) The authenticated runtime variable services driver (for Secure Boot builds)
7439a5
    is located under "SecurityPkg/VariableAuthenticated/RuntimeDxe". OVMF
7439a5
    currently builds the driver (a DXE_RUNTIME_DRIVER module) with the
7439a5
    "VariableRuntimeDxe.inf" control file (refer to "OvmfPkg/OvmfPkgX64.dsc"),
7439a5
    which does not use SMM.
7439a5
7439a5
    The directory includes two more INF files:
7439a5
7439a5
    - VariableSmm.inf -- module type: DXE_SMM_DRIVER. A privileged driver that
7439a5
      runs in SMM and has access to SMRAM.
7439a5
7439a5
    - VariableSmmRuntimeDxe.inf -- module type: DXE_RUNTIME_DRIVER. A
7439a5
      non-privileged driver that implements the variable runtime services
7439a5
      (replacing the current "VariableRuntimeDxe.inf" file) by communicating
7439a5
      with the above privileged SMM half via EFI_SMM_COMMUNICATION_PROTOCOL.
7439a5
7439a5
(6) An SMRAM-based LockBox implementation needs to be discussed in two parts,
7439a5
    because the LockBox is accessed in both PEI and DXE.
7439a5
7439a5
    (a) During DXE, drivers save data in the LockBox. A save operation is
7439a5
        layered as follows:
7439a5
7439a5
        - The unprivileged driver wishing to store data in the LockBox links
7439a5
          against the "MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf"
7439a5
          library instance.
7439a5
7439a5
          The library allows the unprivileged driver to format requests for the
7439a5
          privileged SMM LockBox driver (see below), and to parse responses.
7439a5
7439a5
        - The privileged SMM LockBox driver is built from
7439a5
          "MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf". This
7439a5
          driver has module type DXE_SMM_DRIVER and can access SMRAM.
7439a5
7439a5
          The driver delegates command parsing and response formatting to
7439a5
          "MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf".
7439a5
7439a5
        - The above two halves (unprivileged and privileged) mirror what we've
7439a5
          seen in case of the variable service drivers, under (5).
7439a5
7439a5
    (b) In PEI, the S3 Resume PEIM (UefiCpuPkg/Universal/Acpi/S3Resume2Pei)
7439a5
        retrieves data from the LockBox.
7439a5
7439a5
        Presumably, S3Resume2Pei should be considered an "unprivileged PEIM",
7439a5
        and the SMRAM access should be layered as seen in DXE. Unfortunately,
7439a5
        edk2 does not implement all of the layers in PEI -- the code either
7439a5
        doesn't exist, or it is not open source:
7439a5
7439a5
  role         | DXE: protocol/module           | PEI: PPI/module
7439a5
  -------------+--------------------------------+------------------------------
7439a5
  unprivileged | any                            | S3Resume2Pei.inf
7439a5
  driver       |                                |
7439a5
  -------------+--------------------------------+------------------------------
7439a5
  command      | LIBRARY_CLASS = LockBoxLib     | LIBRARY_CLASS = LockBoxLib
7439a5
  formatting   |                                |
7439a5
  and response | SmmLockBoxDxeLib.inf           | SmmLockBoxPeiLib.inf
7439a5
  parsing      |                                |
7439a5
  -------------+--------------------------------+------------------------------
7439a5
  privilege    | EFI_SMM_COMMUNICATION_PROTOCOL | EFI_PEI_SMM_COMMUNICATION_PPI
7439a5
  separation   |                                |
7439a5
               | PiSmmCore.inf                  | missing!
7439a5
  -------------+--------------------------------+------------------------------
7439a5
  platform SMM | EFI_SMM_CONTROL2_PROTOCOL      | PEI_SMM_CONTROL_PPI
7439a5
  and SMRAM    | EFI_SMM_ACCESS2_PROTOCOL       | PEI_SMM_ACCESS_PPI
7439a5
  access       |                                |
7439a5
               | to be done in OVMF             | to be done in OVMF
7439a5
  -------------+--------------------------------+------------------------------
7439a5
  command      | LIBRARY_CLASS = LockBoxLib     | LIBRARY_CLASS = LockBoxLib
7439a5
  parsing and  |                                |
7439a5
  response     | SmmLockBoxSmmLib.inf           | missing!
7439a5
  formatting   |                                |
7439a5
  -------------+--------------------------------+------------------------------
7439a5
  privileged   | SmmLockBox.inf                 | missing!
7439a5
  LockBox      |                                |
7439a5
  driver       |                                |
7439a5
7439a5
        Alternatively, in the future OVMF might be able to provide a LockBoxLib
7439a5
        instance (an SmmLockBoxPeiLib substitute) for S3Resume2Pei that
7439a5
        accesses SMRAM directly, eliminating the need for deeper layers in the
7439a5
        stack (that is, EFI_PEI_SMM_COMMUNICATION_PPI and deeper).
7439a5
7439a5
        In fact, a "thin" EFI_PEI_SMM_COMMUNICATION_PPI implementation whose
7439a5
        sole Communicate() member invariably returns EFI_NOT_STARTED would
7439a5
        cause the current SmmLockBoxPeiLib library instance to directly perform
7439a5
        full-depth SMRAM access and LockBox search, obviating the "missing"
7439a5
        cells. (With reference to A Tour Beyond BIOS: Implementing S3 Resume
7439a5
        with EDK2, by Jiewen Yao and Vincent Zimmer, October 2014.)
7439a5
7439a5
Select features
7439a5
---------------
7439a5
7439a5
In this section we'll browse the top-level "OvmfPkg" package directory, and
7439a5
discuss the more interesting drivers and libraries that have not been mentioned
7439a5
thus far.
7439a5
7439a5
X64-specific reset vector for OVMF
7439a5
..................................
7439a5
7439a5
The "OvmfPkg/ResetVector" directory customizes the reset vector (found in
7439a5
"UefiCpuPkg/ResetVector/Vtf0") for "OvmfPkgX64.fdf", that is, when the SEC/PEI
7439a5
phases run in 64-bit (ie. long) mode.
7439a5
7439a5
The reset vector's control flow looks roughly like:
7439a5
7439a5
  resetVector                               [Ia16/ResetVectorVtf0.asm]
7439a5
  EarlyBspInitReal16                        [Ia16/Init16.asm]
7439a5
  Main16                                    [Main.asm]
7439a5
    EarlyInit16                             [Ia16/Init16.asm]
7439a5
7439a5
    ; Transition the processor from
7439a5
    ; 16-bit real mode to 32-bit flat mode
7439a5
    TransitionFromReal16To32BitFlat         [Ia16/Real16ToFlat32.asm]
7439a5
7439a5
    ; Search for the
7439a5
    ; Boot Firmware Volume (BFV)
7439a5
    Flat32SearchForBfvBase                  [Ia32/SearchForBfvBase.asm]
7439a5
7439a5
    ; Search for the SEC entry point
7439a5
    Flat32SearchForSecEntryPoint            [Ia32/SearchForSecEntry.asm]
7439a5
7439a5
    %ifdef ARCH_IA32
7439a5
      ; Jump to the 32-bit SEC entry point
7439a5
    %else
7439a5
      ; Transition the processor
7439a5
      ; from 32-bit flat mode
7439a5
      ; to 64-bit flat mode
7439a5
      Transition32FlatTo64Flat              [Ia32/Flat32ToFlat64.asm]
7439a5
7439a5
        SetCr3ForPageTables64               [Ia32/PageTables64.asm]
7439a5
          ; set CR3 to page tables
7439a5
          ; built into the ROM image
7439a5
7439a5
        ; enable PAE
7439a5
        ; set LME
7439a5
        ; enable paging
7439a5
7439a5
      ; Jump to the 64-bit SEC entry point
7439a5
    %endif
7439a5
7439a5
On physical platforms, the initial page tables referenced by
7439a5
SetCr3ForPageTables64 are built statically into the flash device image, and are
7439a5
present in ROM at runtime. This is fine on physical platforms because the
7439a5
pre-built page table entries have the Accessed and Dirty bits set from the
7439a5
start.
7439a5
7439a5
Accordingly, for OVMF running in long mode on qemu/KVM, the initial page tables
7439a5
were mapped as a KVM_MEM_READONLY slot, as part of QEMU's pflash device (refer
7439a5
to "Firmware image structure" above).
7439a5
7439a5
In spite of the Accessed and Dirty bits being pre-set in the read-only,
7439a5
in-flash PTEs, in a virtual machine attempts are made to update said PTE bits,
7439a5
differently from physical hardware. The component attempting to update the
7439a5
read-only PTEs can be one of the following:
7439a5
7439a5
- The processor itself, if it supports nested paging, and the user enables that
7439a5
  processor feature,
7439a5
7439a5
- KVM code implementing shadow paging, otherwise.
7439a5
7439a5
The first case presents no user-visible symptoms, but the second case (KVM,
7439a5
shadow paging) used to cause a triple fault, prior to Linux commit ba6a354
7439a5
("KVM: mmu: allow page tables to be in read-only slots").
7439a5
7439a5
For compatibility with earlier KVM versions, the OvmfPkg/ResetVector directory
7439a5
adapts the generic reset vector code as follows:
7439a5
7439a5
      Transition32FlatTo64Flat         [UefiCpuPkg/.../Ia32/Flat32ToFlat64.asm]
7439a5
7439a5
        SetCr3ForPageTables64       [OvmfPkg/ResetVector/Ia32/PageTables64.asm]
7439a5
7439a5
          ; dynamically build the initial page tables in RAM, at address
7439a5
          ; PcdOvmfSecPageTablesBase (refer to the memory map above),
7439a5
          ; identity-mapping the first 4 GB of address space
7439a5
7439a5
          ; set CR3 to PcdOvmfSecPageTablesBase
7439a5
7439a5
        ; enable PAE
7439a5
        ; set LME
7439a5
        ; enable paging
7439a5
7439a5
This way the PTEs that earlier KVM versions try to update (during shadow
7439a5
paging) are located in a read-write memory slot, and the write attempts
7439a5
succeed.
7439a5
7439a5
Client library for QEMU's firmware configuration interface
7439a5
..........................................................
7439a5
7439a5
QEMU provides a write-only, 16-bit wide control port, and a read-write, 8-bit
7439a5
wide data port for exchanging configuration elements with the firmware.
7439a5
7439a5
The firmware writes a selector (a key) to the control port (0x510), and then
7439a5
reads the corresponding configuration data (produced by QEMU) from the data
7439a5
port (0x511).
7439a5
7439a5
If the selected entry is writable, the firmware may overwrite it. If QEMU has
7439a5
associated a callback with the entry, then when the entry is completely
7439a5
rewritten, QEMU runs the callback. (OVMF does not rewrite any entries at the
7439a5
moment.)
7439a5
7439a5
A number of selector values (keys) are predefined. In particular, key 0x19
7439a5
selects (returns) a directory of { name, selector, size } triplets, roughly
7439a5
speaking.
7439a5
7439a5
The firmware can request configuration elements by well-known name as well, by
7439a5
looking up the selector value first in the directory, by name, and then writing
7439a5
the selector to the control port. The number of bytes to read subsequently from
7439a5
the data port is known from the directory entry's "size" field.
7439a5
7439a5
By convention, directory entries (well-known symbolic names of configuration
7439a5
elements) are formatted as POSIX pathnames. For example, the array selected by
7439a5
the "etc/system-states" name indicates (among other things) whether the user
7439a5
enabled S3 support in QEMU.
7439a5
7439a5
The above interface is called "fw_cfg".
7439a5
7439a5
The binary data associated with a symbolic name is called an "fw_cfg file".
7439a5
7439a5
OVMF's fw_cfg client library is found in "OvmfPkg/Library/QemuFwCfgLib". OVMF
7439a5
discovers many aspects of the virtual system with it; we refer to a few
7439a5
examples below.
7439a5
7439a5
Guest ACPI tables
7439a5
.................
7439a5
7439a5
An operating system discovers a good amount of its hardware by parsing ACPI
7439a5
tables, and by interpreting ACPI objects and methods. On physical hardware, the
7439a5
platform vendor's firmware installs ACPI tables in memory that match both the
7439a5
hardware present in the system and the user's firmware configuration ("BIOS
7439a5
setup").
7439a5
7439a5
Under qemu/KVM, the owner of the (virtual) hardware configuration is QEMU.
7439a5
Hardware can easily be reconfigured on the command line. Furthermore, features
7439a5
like CPU hotplug, PCI hotplug, memory hotplug are continuously developed for
7439a5
QEMU, and operating systems need direct ACPI support to exploit these features.
7439a5
7439a5
For this reason, QEMU builds its own ACPI tables dynamically, in a
7439a5
self-descriptive manner, and exports them to the firmware through a complex,
7439a5
multi-file fw_cfg interface. It is rooted in the "etc/table-loader" fw_cfg
7439a5
file. (Further details of this interface are out of scope for this report.)
7439a5
7439a5
OVMF's AcpiPlatformDxe driver fetches the ACPI tables, and installs them for
7439a5
the guest OS with the EFI_ACPI_TABLE_PROTOCOL (which is in turn provided by the
7439a5
generic "MdeModulePkg/Universal/Acpi/AcpiTableDxe" driver).
7439a5
7439a5
For earlier QEMU versions and machine types (which we generally don't recommend
7439a5
for OVMF; see "Scope"), the "OvmfPkg/AcpiTables" directory contains a few
7439a5
static ACPI table templates. When the "etc/table-loader" fw_cfg file is
7439a5
unavailable, AcpiPlatformDxe installs these default tables (with a little bit
7439a5
of dynamic patching).
7439a5
7439a5
When OVMF runs in a Xen domU, AcpiTableDxe also installs ACPI tables that
7439a5
originate from the hypervisor's environment.
7439a5
7439a5
Guest SMBIOS tables
7439a5
...................
7439a5
7439a5
Quoting the SMBIOS Reference Specification,
7439a5
7439a5
  [...] the System Management BIOS Reference Specification addresses how
7439a5
  motherboard and system vendors present management information about their
7439a5
  products in a standard format [...]
7439a5
7439a5
In practice SMBIOS tables are just another set of tables that the platform
7439a5
vendor's firmware installs in RAM for the operating system, and, importantly,
7439a5
for management applications running on the OS. Without rehashing the "Guest
7439a5
ACPI tables" section in full, let's map the OVMF roles seen there from ACPI to
7439a5
SMBIOS:
7439a5
7439a5
  role                     | ACPI                    | SMBIOS
7439a5
  -------------------------+-------------------------+-------------------------
7439a5
  fw_cfg file              | etc/table-loader        | etc/smbios/smbios-tables
7439a5
  -------------------------+-------------------------+-------------------------
7439a5
  OVMF driver              | AcpiPlatformDxe         | SmbiosPlatformDxe
7439a5
  under "OvmfPkg"          |                         |
7439a5
  -------------------------+-------------------------+-------------------------
7439a5
  Underlying protocol,     | EFI_ACPI_TABLE_PROTOCOL | EFI_SMBIOS_PROTOCOL
7439a5
  implemented by generic   |                         |
7439a5
  driver under             | Acpi/AcpiTableDxe       | SmbiosDxe
7439a5
  "MdeModulePkg/Universal" |                         |
7439a5
  -------------------------+-------------------------+-------------------------
7439a5
  default tables available | yes                     | [RHEL] yes, Type0 and
7439a5
  for earlier QEMU machine |                         |        Type1 tables
7439a5
  types, with hot-patching |                         |
7439a5
  -------------------------+-------------------------+-------------------------
7439a5
  tables fetched in Xen    | yes                     | yes
7439a5
  domUs                    |                         |
7439a5
7439a5
Platform-specific boot policy
7439a5
.............................
7439a5
7439a5
OVMF's BDS (Boot Device Selection) phase is implemented by
7439a5
IntelFrameworkModulePkg/Universal/BdsDxe. Roughly speaking, this large driver:
7439a5
7439a5
- provides the EFI BDS architectural protocol (which DXE transfers control to
7439a5
  after dispatching all DXE drivers),
7439a5
7439a5
- connects drivers to devices,
7439a5
7439a5
- enumerates boot devices,
7439a5
7439a5
- auto-generates boot options,
7439a5
7439a5
- provides "BIOS setup" screens, such as:
7439a5
7439a5
  - Boot Manager, for booting an option,
7439a5
7439a5
  - Boot Maintenance Manager, for adding, deleting, and reordering boot
7439a5
    options, changing console properties etc,
7439a5
7439a5
  - Device Manager, where devices can register configuration forms, including
7439a5
7439a5
    - Secure Boot configuration forms,
7439a5
7439a5
    - OVMF's Platform Driver form (see under PlatformDxe).
7439a5
7439a5
Firmware that includes the "IntelFrameworkModulePkg/Universal/BdsDxe" driver
7439a5
can customize its behavior by providing an instance of the PlatformBdsLib
7439a5
library class. The driver links against this platform library, and the
7439a5
platform library can call Intel's BDS utility functions from
7439a5
"IntelFrameworkModulePkg/Library/GenericBdsLib".
7439a5
7439a5
OVMF's PlatformBdsLib instance can be found in
7439a5
"OvmfPkg/Library/PlatformBdsLib". The main function where the BdsDxe driver
7439a5
enters the library is PlatformBdsPolicyBehavior(). We mention two OVMF
7439a5
particulars here.
7439a5
7439a5
(1) OVMF is capable of loading kernel images directly from fw_cfg, matching
7439a5
    QEMU's -kernel, -initrd, and -append command line options. This feature is
7439a5
    useful for rapid, repeated Linux kernel testing, and is implemented in the
7439a5
    following call tree:
7439a5
7439a5
    PlatformBdsPolicyBehavior() [OvmfPkg/Library/PlatformBdsLib/BdsPlatform.c]
7439a5
      TryRunningQemuKernel() [OvmfPkg/Library/PlatformBdsLib/QemuKernel.c]
7439a5
        LoadLinux*() [OvmfPkg/Library/LoadLinuxLib/Linux.c]
7439a5
7439a5
    OvmfPkg/Library/LoadLinuxLib ports the efilinux bootloader project into
7439a5
    OvmfPkg.
7439a5
7439a5
(2) OVMF seeks to comply with the boot order specification passed down by QEMU
7439a5
    over fw_cfg.
7439a5
7439a5
    (a) About Boot Modes
7439a5
7439a5
      During the PEI phase, OVMF determines and stores the Boot Mode in the
7439a5
      PHIT HOB (already mentioned in "S3 (suspend to RAM and resume)"). The
7439a5
      boot mode is supposed to influence the rest of the system, for example it
7439a5
      distinguishes S3 resume (BOOT_ON_S3_RESUME) from a "normal" boot.
7439a5
7439a5
      In general, "normal" boots can be further differentiated from each other;
7439a5
      for example for speed reasons. When the firmware can tell during PEI that
7439a5
      the chassis has not been opened since last power-up, then it might want
7439a5
      to save time by not connecting all devices and not enumerating all boot
7439a5
      options from scratch; it could just rely on the stored results of the
7439a5
      last enumeration. The matching BootMode value, to be set during PEI,
7439a5
      would be BOOT_ASSUMING_NO_CONFIGURATION_CHANGES.
7439a5
7439a5
      OVMF only sets one of the following two boot modes, based on CMOS
7439a5
      contents:
7439a5
      - BOOT_ON_S3_RESUME,
7439a5
      - BOOT_WITH_FULL_CONFIGURATION.
7439a5
7439a5
      For BOOT_ON_S3_RESUME, please refer to "S3 (suspend to RAM and resume)".
7439a5
      The other boot mode supported by OVMF, BOOT_WITH_FULL_CONFIGURATION, is
7439a5
      an appropriate "catch-all" for a virtual machine, where hardware can
7439a5
      easily change from boot to boot.
7439a5
7439a5
    (b) Auto-generation of boot options
7439a5
7439a5
      Accordingly, when not resuming from S3 sleep (*), OVMF always connects
7439a5
      all devices, and enumerates all bootable devices as new boot options
7439a5
      (non-volatile variables called Boot####).
7439a5
7439a5
      (*) During S3 resume, DXE is not reached, hence BDS isn't either.
7439a5
7439a5
      The auto-enumerated boot options are stored in the BootOrder non-volatile
7439a5
      variable after any preexistent options. (Boot options may exist before
7439a5
      auto-enumeration eg. because the user added them manually with the Boot
7439a5
      Maintenance Manager or the efibootmgr utility. They could also originate
7439a5
      from an earlier auto-enumeration.)
7439a5
7439a5
      PlatformBdsPolicyBehavior()                   [OvmfPkg/.../BdsPlatform.c]
7439a5
        TryRunningQemuKernel()                       [OvmfPkg/.../QemuKernel.c]
7439a5
        BdsLibConnectAll()           [IntelFrameworkModulePkg/.../BdsConnect.c]
7439a5
        BdsLibEnumerateAllBootOption()  [IntelFrameworkModulePkg/.../BdsBoot.c]
7439a5
          BdsLibBuildOptionFromHandle() [IntelFrameworkModulePkg/.../BdsBoot.c]
7439a5
            BdsLibRegisterNewOption()   [IntelFrameworkModulePkg/.../BdsMisc.c]
7439a5
              //
7439a5
              // Append the new option number to the original option order
7439a5
              //
7439a5
7439a5
    (c) Relative UEFI device paths in boot options
7439a5
7439a5
      The handling of relative ("short-form") UEFI device paths is best
7439a5
      demonstrated through an example, and by quoting the UEFI 2.4A
7439a5
      specification.
7439a5
7439a5
      A short-form hard drive UEFI device path could be (displaying each device
7439a5
      path node on a separate line for readability):
7439a5
7439a5
        HD(1,GPT,14DD1CC5-D576-4BBF-8858-BAF877C8DF61,0x800,0x64000)/
7439a5
        \EFI\fedora\shim.efi
7439a5
7439a5
      This device path lacks prefix nodes (eg. hardware or messaging type
7439a5
      nodes) that would lead to the hard drive. During load option processing,
7439a5
      the above short-form or relative device path could be matched against the
7439a5
      following absolute device path:
7439a5
7439a5
        PciRoot(0x0)/
7439a5
        Pci(0x4,0x0)/
7439a5
        HD(1,GPT,14DD1CC5-D576-4BBF-8858-BAF877C8DF61,0x800,0x64000)/
7439a5
        \EFI\fedora\shim.efi
7439a5
7439a5
      The motivation for this type of device path matching / completion is to
7439a5
      allow the user to move around the hard drive (for example, to plug a
7439a5
      controller in a different PCI slot, or to expose the block device on a
7439a5
      different iSCSI path) and still enable the firmware to find the hard
7439a5
      drive.
7439a5
7439a5
      The UEFI specification says,
7439a5
7439a5
        9.3.6 Media Device Path
7439a5
        9.3.6.1 Hard Drive
7439a5
7439a5
          [...] Section 3.1.2 defines special rules for processing the Hard
7439a5
          Drive Media Device Path. These special rules enable a disk's location
7439a5
          to change and still have the system boot from the disk. [...]
7439a5
7439a5
        3.1.2 Load Option Processing
7439a5
7439a5
          [...] The boot manager must [...] support booting from a short-form
7439a5
          device path that starts with the first element being a hard drive
7439a5
          media device path [...]. The boot manager must use the GUID or
7439a5
          signature and partition number in the hard drive device path to match
7439a5
          it to a device in the system. If the drive supports the GPT
7439a5
          partitioning scheme the GUID in the hard drive media device path is
7439a5
          compared with the UniquePartitionGuid field of the GUID Partition
7439a5
          Entry [...]. If the drive supports the PC-AT MBR scheme the signature
7439a5
          in the hard drive media device path is compared with the
7439a5
          UniqueMBRSignature in the Legacy Master Boot Record [...]. If a
7439a5
          signature match is made, then the partition number must also be
7439a5
          matched. The hard drive device path can be appended to the matching
7439a5
          hardware device path and normal boot behavior can then be used. If
7439a5
          more than one device matches the hard drive device path, the boot
7439a5
          manager will pick one arbitrarily. Thus the operating system must
7439a5
          ensure the uniqueness of the signatures on hard drives to guarantee
7439a5
          deterministic boot behavior.
7439a5
7439a5
      Edk2 implements and exposes the device path completion logic in the
7439a5
      already referenced "IntelFrameworkModulePkg/Library/GenericBdsLib"
7439a5
      library, in the BdsExpandPartitionPartialDevicePathToFull() function.
7439a5
7439a5
    (d) Filtering and reordering the boot options based on fw_cfg
7439a5
7439a5
      Once we have an "all-inclusive", partly preexistent, partly freshly
7439a5
      auto-generated boot option list from bullet (b), OVMF loads QEMU's
7439a5
      requested boot order from fw_cfg, and filters and reorders the list from
7439a5
      (b) with it:
7439a5
7439a5
      PlatformBdsPolicyBehavior()                   [OvmfPkg/.../BdsPlatform.c]
7439a5
        TryRunningQemuKernel()                       [OvmfPkg/.../QemuKernel.c]
7439a5
        BdsLibConnectAll()           [IntelFrameworkModulePkg/.../BdsConnect.c]
7439a5
        BdsLibEnumerateAllBootOption()  [IntelFrameworkModulePkg/.../BdsBoot.c]
7439a5
        SetBootOrderFromQemu()                    [OvmfPkg/.../QemuBootOrder.c]
7439a5
7439a5
      According to the (preferred) "-device ...,bootindex=N" and the (legacy)
7439a5
      '-boot order=drives' command line options, QEMU requests a boot order
7439a5
      from the firmware through the "bootorder" fw_cfg file. (For a bootindex
7439a5
      example, refer to the "Example qemu invocation" section.)
7439a5
7439a5
      This fw_cfg file consists of OpenFirmware (OFW) device paths -- note: not
7439a5
      UEFI device paths! --, one per line. An example list is:
7439a5
7439a5
        /pci@i0cf8/scsi@4/disk@0,0
7439a5
        /pci@i0cf8/ide@1,1/drive@1/disk@0
7439a5
        /pci@i0cf8/ethernet@3/ethernet-phy@0
7439a5
7439a5
      OVMF filters and reorders the boot option list from bullet (b) with the
7439a5
      following nested loops algorithm:
7439a5
7439a5
        new_uefi_order := <empty>
7439a5
        for each qemu_ofw_path in QEMU's OpenFirmware device path list:
7439a5
          qemu_uefi_path_prefix := translate(qemu_ofw_path)
7439a5
7439a5
          for each boot_option in current_uefi_order:
7439a5
            full_boot_option := complete(boot_option)
7439a5
7439a5
            if match(qemu_uefi_path_prefix, full_boot_option):
7439a5
              append(new_uefi_order, boot_option)
7439a5
              break
7439a5
7439a5
        for each unmatched boot_option in current_uefi_order:
7439a5
          if survives(boot_option):
7439a5
            append(new_uefi_order, boot_option)
7439a5
7439a5
        current_uefi_order := new_uefi_order
7439a5
7439a5
      OVMF iterates over QEMU's OFW device paths in order, translates each to a
7439a5
      UEFI device path prefix, tries to match the translated prefix against the
7439a5
      UEFI boot options (which are completed from relative form to absolute
7439a5
      form for the purpose of prefix matching), and if there's a match, the
7439a5
      matching boot option is appended to the new boot order (which starts out
7439a5
      empty).
7439a5
7439a5
      (We elaborate on the translate() function under bullet (e). The
7439a5
      complete() function has been explained in bullet (c).)
7439a5
7439a5
      In addition, UEFI boot options that remain unmatched after filtering and
7439a5
      reordering are post-processed, and some of them "survive". Due to the
7439a5
      fact that OpenFirmware device paths have less expressive power than their
7439a5
      UEFI counterparts, some UEFI boot options are simply inexpressible (hence
7439a5
      unmatchable) by the nested loops algorithm.
7439a5
7439a5
      An important example is the memory-mapped UEFI shell, whose UEFI device
7439a5
      path is inexpressible by QEMU's OFW device paths:
7439a5
7439a5
        MemoryMapped(0xB,0x900000,0x10FFFFF)/
7439a5
        FvFile(7C04A583-9E3E-4F1C-AD65-E05268D0B4D1)
7439a5
7439a5
      (Side remark: notice that the address range visible in the MemoryMapped()
7439a5
      node corresponds to DXEFV under "comprehensive memory map of OVMF"! In
7439a5
      addition, the FvFile() node's GUID originates from the FILE_GUID entry of
7439a5
      "ShellPkg/Application/Shell/Shell.inf".)
7439a5
7439a5
      The UEFI shell can be booted by pressing ESC in OVMF on the TianoCore
7439a5
      splash screen, and navigating to Boot Manager | EFI Internal Shell. If
7439a5
      the "survival policy" was not implemented, the UEFI shell's boot option
7439a5
      would always be filtered out.
7439a5
7439a5
      The current "survival policy" preserves all boot options that start with
7439a5
      neither PciRoot() nor HD().
7439a5
7439a5
    (e) Translating QEMU's OpenFirmware device paths to UEFI device path
7439a5
        prefixes
7439a5
7439a5
      In this section we list the (strictly heuristical) mappings currently
7439a5
      performed by OVMF.
7439a5
7439a5
      The "prefix only" nature of the translation output is rooted minimally in
7439a5
      the fact that QEMU's OpenFirmware device paths cannot carry pathnames
7439a5
      within filesystems. There's no way to specify eg.
7439a5
7439a5
        \EFI\fedora\shim.efi
7439a5
7439a5
      in an OFW device path, therefore a UEFI device path translated from an
7439a5
      OFW device path can at best be a prefix (not a full match) of a UEFI
7439a5
      device path that ends with "\EFI\fedora\shim.efi".
7439a5
7439a5
      - IDE disk, IDE CD-ROM:
7439a5
7439a5
        OpenFirmware device path:
7439a5
7439a5
          /pci@i0cf8/ide@1,1/drive@0/disk@0
7439a5
               ^         ^ ^       ^      ^
7439a5
               |         | |       |      master or slave
7439a5
               |         | |       primary or secondary
7439a5
               |         PCI slot & function holding IDE controller
7439a5
               PCI root at system bus port, PIO
7439a5
7439a5
        UEFI device path prefix:
7439a5
7439a5
          PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)
7439a5
                                                       ^
7439a5
                                                       fixed LUN
7439a5
7439a5
      - Floppy disk:
7439a5
7439a5
        OpenFirmware device path:
7439a5
7439a5
          /pci@i0cf8/isa@1/fdc@03f0/floppy@0
7439a5
               ^         ^     ^           ^
7439a5
               |         |     |           A: or B:
7439a5
               |         |     ISA controller io-port (hex)
7439a5
               |         PCI slot holding ISA controller
7439a5
               PCI root at system bus port, PIO
7439a5
7439a5
        UEFI device path prefix:
7439a5
7439a5
          PciRoot(0x0)/Pci(0x1,0x0)/Floppy(0x0)
7439a5
                                           ^
7439a5
                                           ACPI UID (A: or B:)
7439a5
7439a5
      - Virtio-block disk:
7439a5
7439a5
        OpenFirmware device path:
7439a5
7439a5
          /pci@i0cf8/scsi@6[,3]/disk@0,0
7439a5
               ^          ^  ^       ^ ^
7439a5
               |          |  |       fixed
7439a5
               |          |  PCI function corresponding to disk (optional)
7439a5
               |          PCI slot holding disk
7439a5
               PCI root at system bus port, PIO
7439a5
7439a5
        UEFI device path prefixes (dependent on the presence of a nonzero PCI
7439a5
        function in the OFW device path):
7439a5
7439a5
          PciRoot(0x0)/Pci(0x6,0x0)/HD(
7439a5
          PciRoot(0x0)/Pci(0x6,0x3)/HD(
7439a5
7439a5
      - Virtio-scsi disk and virtio-scsi passthrough:
7439a5
7439a5
        OpenFirmware device path:
7439a5
7439a5
          /pci@i0cf8/scsi@7[,3]/channel@0/disk@2,3
7439a5
               ^          ^             ^      ^ ^
7439a5
               |          |             |      | LUN
7439a5
               |          |             |      target
7439a5
               |          |             channel (unused, fixed 0)
7439a5
               |          PCI slot[, function] holding SCSI controller
7439a5
               PCI root at system bus port, PIO
7439a5
7439a5
        UEFI device path prefixes (dependent on the presence of a nonzero PCI
7439a5
        function in the OFW device path):
7439a5
7439a5
          PciRoot(0x0)/Pci(0x7,0x0)/Scsi(0x2,0x3)
7439a5
          PciRoot(0x0)/Pci(0x7,0x3)/Scsi(0x2,0x3)
7439a5
7439a5
      - Emulated and passed-through (physical) network cards:
7439a5
7439a5
        OpenFirmware device path:
7439a5
7439a5
          /pci@i0cf8/ethernet@3[,2]
7439a5
               ^              ^
7439a5
               |              PCI slot[, function] holding Ethernet card
7439a5
               PCI root at system bus port, PIO
7439a5
7439a5
        UEFI device path prefixes (dependent on the presence of a nonzero PCI
7439a5
        function in the OFW device path):
7439a5
7439a5
          PciRoot(0x0)/Pci(0x3,0x0)
7439a5
          PciRoot(0x0)/Pci(0x3,0x2)
7439a5
7439a5
Virtio drivers
7439a5
..............
7439a5
7439a5
UEFI abstracts various types of hardware resources into protocols, and allows
7439a5
firmware developers to implement those protocols in device drivers. The Virtio
7439a5
Specification defines various types of virtual hardware for virtual machines.
7439a5
Connecting the two specifications, OVMF provides UEFI drivers for QEMU's
7439a5
virtio-block, virtio-scsi, and virtio-net devices.
7439a5
7439a5
The following diagram presents the protocol and driver stack related to Virtio
7439a5
devices in edk2 and OVMF. Each node in the graph identifies a protocol and/or
7439a5
the edk2 driver that produces it. Nodes on the top are more abstract.
7439a5
7439a5
  EFI_BLOCK_IO_PROTOCOL                             EFI_SIMPLE_NETWORK_PROTOCOL
7439a5
  [OvmfPkg/VirtioBlkDxe]                              [OvmfPkg/VirtioNetDxe]
7439a5
             |                                                   |
7439a5
             |         EFI_EXT_SCSI_PASS_THRU_PROTOCOL           |
7439a5
             |             [OvmfPkg/VirtioScsiDxe]               |
7439a5
             |                        |                          |
7439a5
             +------------------------+--------------------------+
7439a5
                                      |
7439a5
                           VIRTIO_DEVICE_PROTOCOL
7439a5
                                      |
7439a5
                +---------------------+---------------------+
7439a5
                |                                           |
7439a5
  [OvmfPkg/VirtioPciDeviceDxe]                  [custom platform drivers]
7439a5
                |                                           |
7439a5
                |                                           |
7439a5
       EFI_PCI_IO_PROTOCOL                [OvmfPkg/Library/VirtioMmioDeviceLib]
7439a5
 [MdeModulePkg/Bus/Pci/PciBusDxe]              direct MMIO register access
7439a5
7439a5
The top three drivers produce standard UEFI abstractions: the Block IO
7439a5
Protocol, the Extended SCSI Pass Thru Protocol, and the Simple Network
7439a5
Protocol, for virtio-block, virtio-scsi, and virtio-net devices, respectively.
7439a5
7439a5
Comparing these device-specific virtio drivers to each other, we can determine:
7439a5
7439a5
- They all conform to the UEFI Driver Model. This means that their entry point
7439a5
  functions don't immediately start to search for devices and to drive them,
7439a5
  they only register instances of the EFI_DRIVER_BINDING_PROTOCOL. The UEFI
7439a5
  Driver Model then enumerates devices and chains matching drivers
7439a5
  automatically.
7439a5
7439a5
- They are as minimal as possible, while remaining correct (refer to source
7439a5
  code comments for details). For example, VirtioBlkDxe and VirtioScsiDxe both
7439a5
  support only one request in flight.
7439a5
7439a5
  In theory, VirtioBlkDxe could implement EFI_BLOCK_IO2_PROTOCOL, which allows
7439a5
  queueing. Similarly, VirtioScsiDxe does not support the non-blocking mode of
7439a5
  EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru(). (Which is permitted by the UEFI
7439a5
  specification.) Both VirtioBlkDxe and VirtioScsiDxe delegate synchronous
7439a5
  request handling to "OvmfPkg/Library/VirtioLib". This limitation helps keep
7439a5
  the implementation simple, and testing thus far seems to imply satisfactory
7439a5
  performance, for a virtual boot firmware.
7439a5
7439a5
  VirtioNetDxe cannot avoid queueing, because EFI_SIMPLE_NETWORK_PROTOCOL
7439a5
  requires it on the interface level. Consequently, VirtioNetDxe is
7439a5
  significantly more complex than VirtioBlkDxe and VirtioScsiDxe. Technical
7439a5
  notes are provided in "OvmfPkg/VirtioNetDxe/TechNotes.txt".
7439a5
7439a5
- None of these drivers access hardware directly. Instead, the Virtio Device
7439a5
  Protocol (OvmfPkg/Include/Protocol/VirtioDevice.h) collects / extracts virtio
7439a5
  operations defined in the Virtio Specification, and these backend-independent
7439a5
  virtio device drivers go through the abstract VIRTIO_DEVICE_PROTOCOL.
7439a5
7439a5
  IMPORTANT: the VIRTIO_DEVICE_PROTOCOL is not a standard UEFI protocol. It is
7439a5
  internal to edk2 and not described in the UEFI specification. It should only
7439a5
  be used by drivers and applications that live inside the edk2 source tree.
7439a5
7439a5
Currently two providers exist for VIRTIO_DEVICE_PROTOCOL:
7439a5
7439a5
- The first one is the "more traditional" virtio-pci backend, implemented by
7439a5
  OvmfPkg/VirtioPciDeviceDxe. This driver also complies with the UEFI Driver
7439a5
  Model. It consumes an instance of the EFI_PCI_IO_PROTOCOL, and, if the PCI
7439a5
  device/function under probing appears to be a virtio device, it produces a
7439a5
  Virtio Device Protocol instance for it. The driver translates abstract virtio
7439a5
  operations to PCI accesses.
7439a5
7439a5
- The second provider, the virtio-mmio backend, is a library, not a driver,
7439a5
  living in OvmfPkg/Library/VirtioMmioDeviceLib. This library translates
7439a5
  abstract virtio operations to MMIO accesses.
7439a5
7439a5
  The virtio-mmio backend is only a library -- rather than a standalone, UEFI
7439a5
  Driver Model-compliant driver -- because the type of resource it consumes, an
7439a5
  MMIO register block base address, is not enumerable.
7439a5
7439a5
  In other words, while the PCI root bridge driver and the PCI bus driver
7439a5
  produce instances of EFI_PCI_IO_PROTOCOL automatically, thereby enabling the
7439a5
  UEFI Driver Model to probe devices and stack up drivers automatically, no
7439a5
  such enumeration exists for MMIO register blocks.
7439a5
7439a5
  For this reason, VirtioMmioDeviceLib needs to be linked into thin, custom
7439a5
  platform drivers that dispose over this kind of information. As soon as a
7439a5
  driver knows about the MMIO register block base addresses, it can pass each
7439a5
  to the library, and then the VIRTIO_DEVICE_PROTOCOL will be instantiated
7439a5
  (assuming a valid virtio-mmio register block of course). From that point on
7439a5
  the UEFI Driver Model again takes care of the chaining.
7439a5
7439a5
  Typically, such a custom driver does not conform to the UEFI Driver Model
7439a5
  (because that would presuppose auto-enumeration for MMIO register blocks).
7439a5
  Hence it has the following responsibilities:
7439a5
7439a5
  - it shall behave as a "wrapper" UEFI driver around the library,
7439a5
7439a5
  - it shall know virtio-mmio base addresses,
7439a5
7439a5
  - in its entry point function, it shall create a new UEFI handle with an
7439a5
    instance of the EFI_DEVICE_PATH_PROTOCOL for each virtio-mmio device it
7439a5
    knows the base address for,
7439a5
7439a5
  - it shall call VirtioMmioInstallDevice() on those handles, with the
7439a5
    corresponding base addresses.
7439a5
7439a5
  OVMF itself does not employ VirtioMmioDeviceLib. However, the library is used
7439a5
  (or has been tested as Proof-of-Concept) in the following 64-bit and 32-bit
7439a5
  ARM emulator setups:
7439a5
7439a5
  - in "RTSM_VE_FOUNDATIONV8_EFI.fd" and "FVP_AARCH64_EFI.fd", on ARM Holdings'
7439a5
    ARM(R) v8-A Foundation Model and ARM(R) AEMv8-A Base Platform FVP
7439a5
    emulators, respectively:
7439a5
7439a5
                           EFI_BLOCK_IO_PROTOCOL
7439a5
                           [OvmfPkg/VirtioBlkDxe]
7439a5
                                      |
7439a5
                           VIRTIO_DEVICE_PROTOCOL
7439a5
        [ArmPlatformPkg/ArmVExpressPkg/ArmVExpressDxe/ArmFvpDxe.inf]
7439a5
                                      |
7439a5
                    [OvmfPkg/Library/VirtioMmioDeviceLib]
7439a5
                         direct MMIO register access
7439a5
7439a5
  - in "RTSM_VE_CORTEX-A15_EFI.fd" and "RTSM_VE_CORTEX-A15_MPCORE_EFI.fd", on
7439a5
    "qemu-system-arm -M vexpress-a15":
7439a5
7439a5
        EFI_BLOCK_IO_PROTOCOL            EFI_SIMPLE_NETWORK_PROTOCOL
7439a5
        [OvmfPkg/VirtioBlkDxe]             [OvmfPkg/VirtioNetDxe]
7439a5
                   |                                  |
7439a5
                   +------------------+---------------+
7439a5
                                      |
7439a5
                           VIRTIO_DEVICE_PROTOCOL
7439a5
        [ArmPlatformPkg/ArmVExpressPkg/ArmVExpressDxe/ArmFvpDxe.inf]
7439a5
                                      |
7439a5
                    [OvmfPkg/Library/VirtioMmioDeviceLib]
7439a5
                         direct MMIO register access
7439a5
7439a5
  In the above ARM / VirtioMmioDeviceLib configurations, VirtioBlkDxe was
7439a5
  tested with booting Linux distributions, while VirtioNetDxe was tested with
7439a5
  pinging public IPv4 addresses from the UEFI shell.
7439a5
7439a5
Platform Driver
7439a5
...............
7439a5
7439a5
Sometimes, elements of persistent firmware configuration are best exposed to
7439a5
the user in a friendly way. OVMF's platform driver (OvmfPkg/PlatformDxe)
7439a5
presents such settings on the "OVMF Platform Configuration" dialog:
7439a5
7439a5
- Press ESC on the TianoCore splash screen,
7439a5
- Navigate to Device Manager | OVMF Platform Configuration.
7439a5
7439a5
At the moment, OVMF's platform driver handles only one setting: the preferred
7439a5
graphics resolution. This is useful for two purposes:
7439a5
7439a5
- Some UEFI shell commands, like DRIVERS and DEVICES, benefit from a wide
7439a5
  display. Using the MODE shell command, the user can switch to a larger text
7439a5
  resolution (limited by the graphics resolution), and see the command output
7439a5
  in a more easily consumable way.
7439a5
7439a5
  [RHEL] The list of text modes available to the MODE command is also limited
7439a5
         by ConSplitterDxe (found under MdeModulePkg/Universal/Console).
7439a5
         ConSplitterDxe builds an intersection of text modes that are
7439a5
         simultaneously supported by all consoles that ConSplitterDxe
7439a5
         multiplexes console output to.
7439a5
7439a5
         In practice, the strongest text mode restriction comes from
7439a5
         TerminalDxe, which provides console I/O on serial ports. TerminalDxe
7439a5
         has a very limited built-in list of text modes, heavily pruning the
7439a5
         intersection built by ConSplitterDxe, and made available to the MODE
7439a5
         command.
7439a5
7439a5
         On the Red Hat Enterprise Linux 7.1 host, TerminalDxe's list of modes
7439a5
         has been extended with text resolutions that match the Spice QXL GPU's
7439a5
         common graphics resolutions. This way a "full screen" text mode should
7439a5
         always be available in the MODE command.
7439a5
7439a5
- The other advantage of controlling the graphics resolution lies with UEFI
7439a5
  operating systems that don't (yet) have a native driver for QEMU's virtual
7439a5
  video cards  -- eg. the Spice QXL GPU. Such OSes may choose to inherit the
7439a5
  properties of OVMF's EFI_GRAPHICS_OUTPUT_PROTOCOL (provided by
7439a5
  OvmfPkg/QemuVideoDxe, see later).
7439a5
7439a5
  Although the display can be used at runtime in such cases, by direct
7439a5
  framebuffer access, its properties, for example, the resolution, cannot be
7439a5
  modified. The platform driver allows the user to select the preferred GOP
7439a5
  resolution, reboot, and let the guest OS inherit that preferred resolution.
7439a5
7439a5
The platform driver has three access points: the "normal" driver entry point, a
7439a5
set of HII callbacks, and a GOP installation callback.
7439a5
7439a5
(1) Driver entry point: the PlatformInit() function.
7439a5
7439a5
    (a) First, this function loads any available settings, and makes them take
7439a5
        effect. For the preferred graphics resolution in particular, this means
7439a5
        setting the following PCDs:
7439a5
7439a5
          gEfiMdeModulePkgTokenSpaceGuid.PcdVideoHorizontalResolution
7439a5
          gEfiMdeModulePkgTokenSpaceGuid.PcdVideoVerticalResolution
7439a5
7439a5
        These PCDs influence the GraphicsConsoleDxe driver (located under
7439a5
        MdeModulePkg/Universal/Console), which switches to the preferred
7439a5
        graphics mode, and produces EFI_SIMPLE_TEXT_OUTPUT_PROTOCOLs on GOPs:
7439a5
7439a5
                    EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
7439a5
          [MdeModulePkg/Universal/Console/GraphicsConsoleDxe]
7439a5
                                   |
7439a5
                      EFI_GRAPHICS_OUTPUT_PROTOCOL
7439a5
                         [OvmfPkg/QemuVideoDxe]
7439a5
                                   |
7439a5
                          EFI_PCI_IO_PROTOCOL
7439a5
                   [MdeModulePkg/Bus/Pci/PciBusDxe]
7439a5
7439a5
  (b) Second, the driver entry point registers the user interface, including
7439a5
      HII callbacks.
7439a5
7439a5
  (c) Third, the driver entry point registers a GOP installation callback.
7439a5
7439a5
(2) HII callbacks and the user interface.
7439a5
7439a5
    The Human Interface Infrastructure (HII) "is a set of protocols that allow
7439a5
    a UEFI driver to provide the ability to register user interface and
7439a5
    configuration content with the platform firmware".
7439a5
7439a5
    OVMF's platform driver:
7439a5
7439a5
    - provides a static, basic, visual form (PlatformForms.vfr), written in the
7439a5
      Visual Forms Representation language,
7439a5
7439a5
    - includes a UCS-16 encoded message catalog (Platform.uni),
7439a5
7439a5
    - includes source code that dynamically populates parts of the form, with
7439a5
      the help of MdeModulePkg/Library/UefiHiiLib -- this library simplifies
7439a5
      the handling of IFR (Internal Forms Representation) opcodes,
7439a5
7439a5
    - processes form actions that the user takes (Callback() function),
7439a5
7439a5
    - loads and saves platform configuration in a private, non-volatile
7439a5
      variable (ExtractConfig() and RouteConfig() functions).
7439a5
7439a5
    The ExtractConfig() HII callback implements the following stack of
7439a5
    conversions, for loading configuration and presenting it to the user:
7439a5
7439a5
          MultiConfigAltResp       -- form engine / HII communication
7439a5
                  ^
7439a5
                  |
7439a5
           [BlockToConfig]
7439a5
                  |
7439a5
           MAIN_FORM_STATE         -- binary representation of form/widget
7439a5
                  ^                   state
7439a5
                  |
7439a5
      [PlatformConfigToFormState]
7439a5
                  |
7439a5
           PLATFORM_CONFIG         -- accessible to DXE and UEFI drivers
7439a5
                  ^
7439a5
                  |
7439a5
         [PlatformConfigLoad]
7439a5
                  |
7439a5
        UEFI non-volatile variable -- accessible to external utilities
7439a5
7439a5
    The layers are very similar for the reverse direction, ie. when taking
7439a5
    input from the user, and saving the configuration (RouteConfig() HII
7439a5
    callback):
7439a5
7439a5
             ConfigResp            -- form engine / HII communication
7439a5
                  |
7439a5
           [ConfigToBlock]
7439a5
                  |
7439a5
                  v
7439a5
           MAIN_FORM_STATE         -- binary representation of form/widget
7439a5
                  |                   state
7439a5
      [FormStateToPlatformConfig]
7439a5
                  |
7439a5
                  v
7439a5
           PLATFORM_CONFIG         -- accessible to DXE and UEFI drivers
7439a5
                  |
7439a5
         [PlatformConfigSave]
7439a5
                  |
7439a5
                  v
7439a5
        UEFI non-volatile variable -- accessible to external utilities
7439a5
7439a5
(3) When the platform driver starts, a GOP may not be available yet. Thus the
7439a5
    driver entry point registers a callback (the GopInstalled() function) for
7439a5
    GOP installations.
7439a5
7439a5
    When the first GOP is produced (usually by QemuVideoDxe, or potentially by
7439a5
    a third party video driver), PlatformDxe retrieves the list of graphics
7439a5
    modes the GOP supports, and dynamically populates the drop-down list of
7439a5
    available resolutions on the form. The GOP installation callback is then
7439a5
    removed.
7439a5
7439a5
Video driver
7439a5
............
7439a5
7439a5
OvmfPkg/QemuVideoDxe is OVMF's built-in video driver. We can divide its
7439a5
services in two parts: graphics output protocol (primary), and Int10h (VBE)
7439a5
shim (secondary).
7439a5
7439a5
(1) QemuVideoDxe conforms to the UEFI Driver Model; it produces an instance of
7439a5
    the EFI_GRAPHICS_OUTPUT_PROTOCOL (GOP) on each PCI display that it supports
7439a5
    and is connected to:
7439a5
7439a5
                      EFI_GRAPHICS_OUTPUT_PROTOCOL
7439a5
                         [OvmfPkg/QemuVideoDxe]
7439a5
                                   |
7439a5
                          EFI_PCI_IO_PROTOCOL
7439a5
                   [MdeModulePkg/Bus/Pci/PciBusDxe]
7439a5
7439a5
    It supports the following QEMU video cards:
7439a5
7439a5
    - Cirrus 5430 ("-device cirrus-vga"),
7439a5
    - Standard VGA ("-device VGA"),
7439a5
    - QXL VGA ("-device qxl-vga", "-device qxl").
7439a5
7439a5
    For Cirrus the following resolutions and color depths are available:
7439a5
    640x480x32, 800x600x32, 1024x768x24. On stdvga and QXL a long list of
7439a5
    resolutions is available. The list is filtered against the frame buffer
7439a5
    size during initialization.
7439a5
7439a5
    The size of the QXL VGA compatibility framebuffer can be changed with the
7439a5
7439a5
      -device qxl-vga,vgamem_mb=$NUM_MB
7439a5
7439a5
    QEMU option. If $NUM_MB exceeds 32, then the following is necessary
7439a5
    instead:
7439a5
7439a5
      -device qxl-vga,vgamem_mb=$NUM_MB,ram_size_mb=$((NUM_MB*2))
7439a5
7439a5
    because the compatibility framebuffer can't cover more than half of PCI BAR
7439a5
    #0. The latter defaults to 64MB in size, and is controlled by the
7439a5
    "ram_size_mb" property.
7439a5
7439a5
(2) When QemuVideoDxe binds the first Standard VGA or QXL VGA device, and there
7439a5
    is no real VGA BIOS present in the C to F segments (which could originate
7439a5
    from a legacy PCI option ROM -- refer to "Compatibility Support Module
7439a5
    (CSM)"), then QemuVideoDxe installs a minimal, "fake" VGA BIOS -- an Int10h
7439a5
    (VBE) "shim".
7439a5
7439a5
    The shim is implemented in 16-bit assembly in
7439a5
    "OvmfPkg/QemuVideoDxe/VbeShim.asm". The "VbeShim.sh" shell script assembles
7439a5
    it and formats it as a C array ("VbeShim.h") with the help of the "nasm"
7439a5
    utility. The driver's InstallVbeShim() function copies the shim in place
7439a5
    (the C segment), and fills in the VBE Info and VBE Mode Info structures.
7439a5
    The real-mode 10h interrupt vector is pointed to the shim's handler.
7439a5
7439a5
    The shim is (correctly) irrelevant and invisible for all UEFI operating
7439a5
    systems we know about -- except Windows Server 2008 R2 and other Windows
7439a5
    operating systems in that family.
7439a5
7439a5
    Namely, the Windows 2008 R2 SP1 (and Windows 7) UEFI guest's default video
7439a5
    driver dereferences the real mode Int10h vector, loads the pointed-to
7439a5
    handler code, and executes what it thinks to be VGA BIOS services in an
7439a5
    internal real-mode emulator. Consequently, video mode switching used not to
7439a5
    work in Windows 2008 R2 SP1 when it ran on the "pure UEFI" build of OVMF,
7439a5
    making the guest uninstallable. Hence the (otherwise optional, non-default)
7439a5
    Compatibility Support Module (CSM) ended up a requirement for running such
7439a5
    guests.
7439a5
7439a5
    The hard dependency on the sophisticated SeaBIOS CSM and the complex
7439a5
    supporting edk2 infrastructure, for enabling this family of guests, was
7439a5
    considered suboptimal by some members of the upstream community,
7439a5
7439a5
    [RHEL] and was certainly considered a serious maintenance disadvantage for
7439a5
           Red Hat Enterprise Linux 7.1 hosts.
7439a5
7439a5
    Thus, the shim has been collaboratively developed for the Windows 7 /
7439a5
    Windows Server 2008 R2 family. The shim provides a real stdvga / QXL
7439a5
    implementation for the few services that are in fact necessary for the
7439a5
    Windows 2008 R2 SP1 (and Windows 7) UEFI guest, plus some "fakes" that the
7439a5
    guest invokes but whose effect is not important. The only supported mode is
7439a5
    1024x768x32, which is enough to install the guest and then upgrade its
7439a5
    video driver to the full-featured QXL XDDM one.
7439a5
7439a5
    The C segment is not present in the UEFI memory map prepared by OVMF.
7439a5
    Memory space that would cover it is never added (either in PEI, in the form
7439a5
    of memory resource descriptor HOBs, or in DXE, via gDS->AddMemorySpace()).
7439a5
    This way the handler body is invisible to all other UEFI guests, and the
7439a5
    rest of edk2.
7439a5
7439a5
    The Int10h real-mode IVT entry is covered with a Boot Services Code page,
7439a5
    making that too inaccessible to the rest of edk2. Due to the allocation
7439a5
    type, UEFI guest OSes different from the Windows Server 2008 family can
7439a5
    reclaim the page at zero. (The Windows 2008 family accesses that page
7439a5
    regardless of the allocation type.)
7439a5
7439a5
Afterword
7439a5
---------
7439a5
7439a5
After the bulk of this document was written in July 2014, OVMF development has
7439a5
not stopped. To name two significant code contributions from the community: in
7439a5
January 2015, OVMF runs on the "q35" machine type of QEMU, and it features a
7439a5
driver for Xen paravirtual block devices (and another for the underlying Xen
7439a5
bus).
7439a5
7439a5
Furthermore, a dedicated virtualization platform has been contributed to
7439a5
ArmPlatformPkg that plays a role parallel to OvmfPkg's. It targets the "virt"
7439a5
machine type of qemu-system-arm and qemu-system-aarch64. Parts of OvmfPkg are
7439a5
being refactored and modularized so they can be reused in
7439a5
"ArmPlatformPkg/ArmVirtualizationPkg/ArmVirtualizationQemu.dsc".