| 1 | # x86_64 architecture documentation |
| 2 | |
| 3 | This section documents coreboot's x86_64 support. When enabled, |
| 4 | every coreboot stage is built for x86_64, contrary to UEFI's implementation |
| 5 | that only runs some stages in x86_64. |
| 6 | On UEFI the PEI phase, which is x86_32, brings up DRAM and installs |
| 7 | page tables for the x86_64 DXE and BDS phases. |
| 8 | |
| 9 | ## Toolchain requirements for x86_64 support |
| 10 | * The compiler must support generating code for the *large memory model* |
| 11 | (-mcmodel=large). It's supported since GCC 4.4. |
| 12 | |
| 13 | Page tables can be used to provide security benefits, such as by marking |
| 14 | memory as non-executable or removing it entirely. This could be useful |
| 15 | for SMM to mark regular DRAM as NX. |
| 16 | |
| 17 | The large memory model causes the compiler to emit 64bit addressing |
| 18 | instructions, which increases code size. In theory, this is roughly |
| 19 | made up for by the faster execution of the x86_64 code. |
| 20 | |
| 21 | * All x86 coreboot stages and payloads must be loaded below 4GiB in |
| 22 | physical memory. When jumping to the payload coreboot will drop from |
| 23 | long mode back to protected mode to keep compatibility with these payloads. |
| 24 | |
| 25 | ## Comparison to UEFI |
| 26 | On UEFI the SEC and PEI phases (similar to coreboot's bootblock and romstage) |
| 27 | are run in x86_32 mode. The following (guessed) reasons are likely: |
| 28 | * There's no need for x86_64 as memory hasn't been trained yet. The whole 4GiB |
| 29 | address space, including CAR, memory mapped SPI flash and PCI BARs, are |
| 30 | accessible in x86_32. |
| 31 | * When the EFI specification was written compilers did not support |
| 32 | *large memory model*, required in CAR when using a 1:1 page mapping |
| 33 | * Code is 20% bigger in x86_64 due to *large memory model* where pointers and |
| 34 | function calls always use 8 byte addressing. However flash size was very |
| 35 | limited, compared to today's flash chips, when the EFI spec was written. |
| 36 | |
| 37 | ## Current software constraints for x86_64 support |
| 38 | The following constraints are coreboot limitations as it was intended to run in |
| 39 | protected mode only. The code still assumes 32bit pointers in some places and thus: |
| 40 | * The high dword of pointers must always be zero. |
| 41 | * All memory returned by malloc must be below 4GiB in physical memory. |
| 42 | * All code that is to be run must be below 4GiB in physical memory. |
| 43 | * CBMEM must reside below 4GiB in physical memory. |
| 44 | |
| 45 | Any software within coreboot must not access memory resources above 4GiB until |
| 46 | end of BS_DEV_RESOURCES in ramstage. Only at that point the full memory map is |
| 47 | known and identity mapped. |
| 48 | |
| 49 | ## Supported boards |
| 50 | On the supported boards you can enable x86_64 compilation by setting the |
| 51 | Kconfig `USE_X86_64_SUPPORT`. This config option is enabled if the SOC/CPU |
| 52 | selects `HAVE_X86_64_SUPPORT`. |
| 53 | |
| 54 | ## Protected mode wrappers |
| 55 | On some platforms binary blobs are run to initialize parts of the hardware. |
| 56 | When these binary blobs have been compiled for x86_32, then coreboot must |
| 57 | switch to protected mode in order to call and run the blobs. Once the invoked |
| 58 | blobs finish running, coreboot needs to switch back to long mode. |
| 59 | |
| 60 | Since every BLOB is different a SoC must be enabled to support x86_64 mode |
| 61 | by providing the correct wrapper around the x86_32 BLOBs. |
| 62 | |
| 63 | ## TODO |
| 64 | * Support more platforms |
| 65 | * Fix running VGA Option ROMs |
| 66 | * Fix running MRC.bin (Sandy Bridge / Haswell boards) |
| 67 | * Identity map memory above 4GiB in ramstage |
| 68 | * Fine grained page tables for SMM: |
| 69 | * Must not have execute and write permissions for the same page. |
| 70 | * Must only allow TSEG pages to be marked as executable. |
| 71 | * Must reside in SMRAM. |
| 72 | * Must be placed together with SMM rmodule. |
| 73 | * Support 64bit PCI BARs above 4GiB |
| 74 | * Jump to compatible payloads in long mode |
| 75 | |
| 76 | ## Porting other boards |
| 77 | * Fix compilation errors |
| 78 | * Test how well CAR works with x86_64 and paging |
| 79 | * Improve mode switches |
| 80 | * Test libgfxinit / VGA Option ROMs / FSP |
| 81 | |
| 82 | ## Known bugs on real hardware |
| 83 | |
| 84 | According to Intel x86_64 mode hasn't been validated in CAR environments. |
| 85 | However, coreboot developers working on x86_64 support have tried this on |
| 86 | various Intel platforms, and so far haven't found any issues with CAR when |
| 87 | running in x86_64 mode. |
| 88 | |
| 89 | ## Known bugs on KVM enabled QEMU |
| 90 | |
| 91 | The `x86_64` reference code runs fine in QEMU's soft-cpu, but has serious issues |
| 92 | when using KVM mode on some machines. This is due to various mechanisms trying |
| 93 | to accelerate the code execution. |
| 94 | |
| 95 | Known issues in QEMU: |
| 96 | * After entering long mode, the FPU doesn't work anymore, including accessing |
| 97 | MMX registers. It works fine before entering long mode. It works fine when |
| 98 | switching back to protected mode. Other registers, like SSE registers, are |
| 99 | working fine. |
| 100 | * Reading from virtual memory, when the page tables are stored in ROM, causes |
| 101 | the MMU to abort the "page table walking" mechanism when the lower address |
| 102 | bits of the virtual address to be translated have a specific pattern. |
| 103 | Instead of loading the correct physical page, the one containing the |
| 104 | page tables in ROM will be loaded and used, which breaks code and data as |
| 105 | the page table doesn't contain the expected data. This in turn leads to |
| 106 | undefined behaviour whenever the 'wrong' address is being read. |
| 107 | * Disabling paging in compatibility mode crashes the CPU. |
| 108 | * Returning from long mode to compatibility mode crashes the CPU. |
| 109 | * Entering long mode crashes on AMD host platforms. |