# 64-bit x86

---

# x86_64 Support

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> Reference for x86_64 Support.

<a id="rst-x86-64-support"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

- [General note on \[U\]EFI x86_64 support](</kernel/hardware/arch/x86-x86-64/uefi.md>)
- [Memory Management](</kernel/hardware/arch/x86-x86-64/mm.md>)
- [5-level paging](</kernel/hardware/arch/x86-x86-64/5level-paging.md>)
- [Fake NUMA For CPUSets](</kernel/hardware/arch/x86-x86-64/fake-numa-for-cpusets.md>)
- [Firmware support for CPU hotplug under Linux/x86-64](</kernel/hardware/arch/x86-x86-64/cpu-hotplug-spec.md>)
- [Configurable sysfs parameters for the x86-64 machine check code](</kernel/hardware/arch/x86-x86-64/machinecheck.md>)
- [Using FS and GS segments in user space applications](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/arch/x86/x86_64/fsgs.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>)
- [Flexible Return and Event Delivery (FRED)](</kernel/hardware/arch/x86-x86-64/fred.md>)

---

# 5-level paging

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> Original x86-64 was limited by 4-level paging to 256 TiB of virtual address space and 64 TiB of physical address space. We are already bumping into this limit: some vendors offer servers with 64 TiB of memory today.

<a id="rst-level-paging"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-overview"></a>

## Overview

Original x86-64 was limited by 4-level paging to 256 TiB of virtual address space and 64 TiB of physical address space. We are already bumping into this limit: some vendors offer servers with 64 TiB of memory today.

To overcome the limitation upcoming hardware will introduce support for 5-level paging. It is a straight-forward extension of the current page table structure adding one more layer of translation.

It bumps the limits to 128 PiB of virtual address space and 4 PiB of physical address space. This "ought to be enough for anybody" ©.

QEMU 2.9 and later support 5-level paging.

Virtual memory layout for 5-level paging is described in [Documentation/arch/x86/x86_64/mm.rst](/kernel/hardware/arch/x86-x86-64/mm.md)

<a id="rst-user-space-and-large-virtual-address-space"></a>

## User-space and large virtual address space

On x86, 5-level paging enables 56-bit userspace virtual address space. Not all user space is ready to handle wide addresses. It's known that at least some JIT compilers use higher bits in pointers to encode their information. It collides with valid pointers with 5-level paging and leads to crashes.

To mitigate this, we are not going to allocate virtual address space above 47-bit by default.

But userspace can ask for allocation from full address space by specifying hint address (with or without MAP_FIXED) above 47-bits.

If hint address set above 47-bit, but MAP_FIXED is not specified, we try to look for unmapped area by specified address. If it's already occupied, we look for unmapped area in *full* address space, rather than from 47-bit window.

A high hint address would only affect the allocation in question, but not any future mmap()s.

Specifying high hint address on older kernel or on machine without 5-level paging support is safe. The hint will be ignored and kernel will fall back to allocation from 47-bit address space.

This approach helps to easily make application's memory allocator aware about large address space without manually tracking allocated virtual address space.

One important case we need to handle here is interaction with MPX. MPX (without MAWA extension) cannot handle addresses above 47-bit, so we need to make sure that MPX cannot be enabled we already have VMA above the boundary and forbid creating such VMAs once MPX is enabled.

---

# Firmware support for CPU hotplug under Linux/x86-64

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> Linux/x86-64 supports CPU hotplug now. For various reasons Linux wants to know in advance of boot time the maximum number of CPUs that could be plugged into the system. ACPI 3.0 currently has no official way to supply this information from…

<a id="rst-firmware-support-for-cpu-hotplug-under-linux-x86-64"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

Linux/x86-64 supports CPU hotplug now. For various reasons Linux wants to know in advance of boot time the maximum number of CPUs that could be plugged into the system. ACPI 3.0 currently has no official way to supply this information from the firmware to the operating system.

In ACPI each CPU needs an LAPIC object in the MADT table (5.2.11.5 in the ACPI 3.0 specification).  ACPI already has the concept of disabled LAPIC objects by setting the Enabled bit in the LAPIC object to zero.

For CPU hotplug Linux/x86-64 expects now that any possible future hotpluggable CPU is already available in the MADT. If the CPU is not available yet it should have its LAPIC Enabled bit set to 0. Linux will use the number of disabled LAPICs to compute the maximum number of future CPUs.

In the worst case the user can overwrite this choice using a command line option (additional_cpus=...), but it is recommended to supply the correct number (or a reasonable approximation of it, with erring towards more not less) in the MADT to avoid manual configuration.

---

# Fake NUMA For CPUSets

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> Using numa=fake and CPUSets for Resource Management

<a id="rst-fake-numa-for-cpusets"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

**Author:** David Rientjes &lt;[rientjes@cs.washington.edu](<mailto:rientjes@cs.washington.edu>)&gt;

Using numa=fake and CPUSets for Resource Management

This document describes how the numa=fake x86_64 command-line option can be used in conjunction with cpusets for coarse memory management.  Using this feature, you can create fake NUMA nodes that represent contiguous chunks of memory and assign them to cpusets and their attached tasks.  This is a way of limiting the amount of system memory that are available to a certain class of tasks.

For more information on the features of cpusets, see [Documentation/admin-guide/cgroup-v1/cpusets.rst](/kernel/administration/admin-guide/cgroup-v1/cpusets.md). There are a number of different configurations you can use for your needs.  For more information on the numa=fake command line option and its various ways of configuring fake nodes, see [Documentation/admin-guide/kernel-parameters.txt](https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/admin-guide/kernel-parameters.txt?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92)

For the purposes of this introduction, we'll assume a very primitive NUMA emulation setup of "numa=fake=4\*512,".  This will split our system memory into four equal chunks of 512M each that we can now use to assign to cpusets.  As you become more familiar with using this combination for resource control, you'll determine a better setup to minimize the number of nodes you have to deal with.

A machine may be split as follows with "numa=fake=4\*512," as reported by dmesg:



```text
Faking node 0 at 0000000000000000-0000000020000000 (512MB)
Faking node 1 at 0000000020000000-0000000040000000 (512MB)
Faking node 2 at 0000000040000000-0000000060000000 (512MB)
Faking node 3 at 0000000060000000-0000000080000000 (512MB)
...
On node 0 totalpages: 130975
On node 1 totalpages: 131072
On node 2 totalpages: 131072
On node 3 totalpages: 131072
```

Now following the instructions for mounting the cpusets filesystem from [Documentation/admin-guide/cgroup-v1/cpusets.rst](/kernel/administration/admin-guide/cgroup-v1/cpusets.md), you can assign fake nodes (i.e. contiguous memory address spaces) to individual cpusets:



```text
[root@xroads /]# mkdir exampleset
[root@xroads /]# mount -t cpuset none exampleset
[root@xroads /]# mkdir exampleset/ddset
[root@xroads /]# cd exampleset/ddset
[root@xroads /exampleset/ddset]# echo 0-1 > cpus
[root@xroads /exampleset/ddset]# echo 0-1 > mems
```

Now this cpuset, 'ddset', will only allowed access to fake nodes 0 and 1 for memory allocations (1G).

You can now assign tasks to these cpusets to limit the memory resources available to them according to the fake nodes assigned as mems:



```text
[root@xroads /exampleset/ddset]# echo $$ > tasks
[root@xroads /exampleset/ddset]# dd if=/dev/zero of=tmp bs=1024 count=1G
[1] 13425
```

Notice the difference between the system memory usage as reported by /proc/meminfo between the restricted cpuset case above and the unrestricted case (i.e. running the same 'dd' command without assigning it to a fake NUMA cpuset):

>
> <table>
> <thead>
> <tr>
> <th>
>
>
> Name
>
> </th>
> <th>
>
>
> Unrestricted
>
> </th>
> <th>
>
>
> Restricted
>
> </th>
> </tr>
> </thead>
> <tbody>
> <tr>
> <td>
>
>
> MemTotal
>
> </td>
> <td>
>
>
> 3091900 kB
>
> </td>
> <td>
>
>
> 3091900 kB
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> MemFree
>
> </td>
> <td>
>
>
> 42113 kB
>
> </td>
> <td>
>
>
> 1513236 kB
>
> </td>
> </tr>
> </tbody>
> </table>

This allows for coarse memory management for the tasks you assign to particular cpusets.  Since cpusets can form a hierarchy, you can create some pretty interesting combinations of use-cases for various classes of tasks for your memory management needs.

---

# Flexible Return and Event Delivery (FRED)

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> The FRED architecture defines simple new transitions that change privilege level (ring transitions). The FRED architecture was designed with the following goals:

<a id="rst-flexible-return-and-event-delivery-fred"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-overview"></a>

## Overview

The FRED architecture defines simple new transitions that change privilege level (ring transitions). The FRED architecture was designed with the following goals:

1. Improve overall performance and response time by replacing event delivery through the interrupt descriptor table (IDT event delivery) and event return by the IRET instruction with lower latency transitions.

2. Improve software robustness by ensuring that event delivery establishes the full supervisor context and that event return establishes the full user context.

The new transitions defined by the FRED architecture are FRED event delivery and, for returning from events, two FRED return instructions. FRED event delivery can effect a transition from ring 3 to ring 0, but it is used also to deliver events incident to ring 0. One FRED instruction (ERETU) effects a return from ring 0 to ring 3, while the other (ERETS) returns while remaining in ring 0. Collectively, FRED event delivery and the FRED return instructions are FRED transitions.

In addition to these transitions, the FRED architecture defines a new instruction (LKGS) for managing the state of the GS segment register. The LKGS instruction can be used by 64-bit operating systems that do not use the new FRED transitions.

Furthermore, the FRED architecture is easy to extend for future CPU architectures.

<a id="rst-software-based-event-dispatching"></a>

## Software based event dispatching

FRED operates differently from IDT in terms of event handling. Instead of directly dispatching an event to its handler based on the event vector, FRED requires the software to dispatch an event to its handler based on both the event's type and vector. Therefore, an event dispatch framework must be implemented to facilitate the event-to-handler dispatch process. The FRED event dispatch framework takes control once an event is delivered, and employs a two-level dispatch.

The first level dispatching is event type based, and the second level dispatching is event vector based.

<a id="rst-full-supervisor-user-context"></a>

## Full supervisor/user context

FRED event delivery atomically save and restore full supervisor/user context upon event delivery and return. Thus it avoids the problem of transient states due to %cr2 and/or %dr6, and it is no longer needed to handle all the ugly corner cases caused by half baked entry states.

FRED allows explicit unblock of NMI with new event return instructions ERETS/ERETU, avoiding the mess caused by IRET which unconditionally unblocks NMI, e.g., when an exception happens during NMI handling.

FRED always restores the full value of %rsp, thus ESPFIX is no longer needed when FRED is enabled.

<a id="rst-lkgs"></a>

## LKGS

LKGS behaves like the MOV to GS instruction except that it loads the base address into the IA32_KERNEL_GS_BASE MSR instead of the GS segment’s descriptor cache. With LKGS, it ends up with avoiding mucking with kernel GS, i.e., an operating system can always operate with its own GS base address.

Because FRED event delivery from ring 3 and ERETU both swap the value of the GS base address and that of the IA32_KERNEL_GS_BASE MSR, plus the introduction of LKGS instruction, the SWAPGS instruction is no longer needed when FRED is enabled, thus is disallowed (\#UD).

<a id="rst-stack-levels"></a>

## Stack levels

4 stack levels 0\~3 are introduced to replace the nonreentrant IST for event handling, and each stack level should be configured to use a dedicated stack.

The current stack level could be unchanged or go higher upon FRED event delivery. If unchanged, the CPU keeps using the current event stack. If higher, the CPU switches to a new event stack specified by the MSR of the new stack level, i.e., MSR_IA32_FRED_RSP\[123\].

Only execution of a FRED return instruction ERET\[US\], could lower the current stack level, causing the CPU to switch back to the stack it was on before a previous event delivery that promoted the stack level.

---

# Using FS and GS segments in user space applications

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> The x86 architecture supports segmentation. Instructions which access memory can use segment register based addressing mode. The following notation is used to address a byte within a segment:

<a id="rst-using-fs-and-gs-segments-in-user-space-applications"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

The x86 architecture supports segmentation. Instructions which access memory can use segment register based addressing mode. The following notation is used to address a byte within a segment:

> Segment-register:Byte-address

The segment base address is added to the Byte-address to compute the resulting virtual address which is accessed. This allows to access multiple instances of data with the identical Byte-address, i.e. the same code. The selection of a particular instance is purely based on the base-address in the segment register.

In 32-bit mode the CPU provides 6 segments, which also support segment limits. The limits can be used to enforce address space protections.

In 64-bit mode the CS/SS/DS/ES segments are ignored and the base address is always 0 to provide a full 64bit address space. The FS and GS segments are still functional in 64-bit mode.

<a id="rst-common-fs-and-gs-usage"></a>

## Common FS and GS usage

The FS segment is commonly used to address Thread Local Storage (TLS). FS is usually managed by runtime code or a threading library. Variables declared with the '\_\_thread' storage class specifier are instantiated per thread and the compiler emits the FS: address prefix for accesses to these variables. Each thread has its own FS base address so common code can be used without complex address offset calculations to access the per thread instances. Applications should not use FS for other purposes when they use runtimes or threading libraries which manage the per thread FS.

The GS segment has no common use and can be used freely by applications. GCC and Clang support GS based addressing via address space identifiers.

<a id="rst-reading-and-writing-the-fs-gs-base-address"></a>

## Reading and writing the FS/GS base address

There exist two mechanisms to read and write the FS/GS base address:

> - the arch\_prctl() system call
>
> - the FSGSBASE instruction family

<a id="rst-accessing-fs-gs-base-with-arch-prctl"></a>

## Accessing FS/GS base with arch\_prctl()

> The arch\_prctl(2) based mechanism is available on all 64-bit CPUs and all kernel versions.
>
> Reading the base:
>
> > arch\_prctl(ARCH\_GET\_FS, &amp;fsbase); arch\_prctl(ARCH\_GET\_GS, &amp;gsbase);
>
> Writing the base:
>
> > arch\_prctl(ARCH\_SET\_FS, fsbase); arch\_prctl(ARCH\_SET\_GS, gsbase);
>
> The ARCH\_SET\_GS prctl may be disabled depending on kernel configuration and security settings.

<a id="rst-accessing-fs-gs-base-with-the-fsgsbase-instructions"></a>

## Accessing FS/GS base with the FSGSBASE instructions

> With the Ivy Bridge CPU generation Intel introduced a new set of instructions to access the FS and GS base registers directly from user space. These instructions are also supported on AMD Family 17H CPUs. The following instructions are available:
>
> >
> > <table>
> > <tbody>
> > <tr>
> > <td>
> >
> >
> > RDFSBASE %reg
> >
> > </td>
> > <td>
> >
> >
> > Read the FS base register
> >
> > </td>
> > </tr>
> > <tr>
> > <td>
> >
> >
> > RDGSBASE %reg
> >
> > </td>
> > <td>
> >
> >
> > Read the GS base register
> >
> > </td>
> > </tr>
> > <tr>
> > <td>
> >
> >
> > WRFSBASE %reg
> >
> > </td>
> > <td>
> >
> >
> > Write the FS base register
> >
> > </td>
> > </tr>
> > <tr>
> > <td>
> >
> >
> > WRGSBASE %reg
> >
> > </td>
> > <td>
> >
> >
> > Write the GS base register
> >
> > </td>
> > </tr>
> > </tbody>
> > </table>
>
> The instructions avoid the overhead of the arch\_prctl() syscall and allow more flexible usage of the FS/GS addressing modes in user space applications. This does not prevent conflicts between threading libraries and runtimes which utilize FS and applications which want to use it for their own purpose.

<a id="rst-fsgsbase-instructions-enablement"></a>

### FSGSBASE instructions enablement

> The instructions are enumerated in CPUID leaf 7, bit 0 of EBX. If available /proc/cpuinfo shows 'fsgsbase' in the flag entry of the CPUs.
>
> The availability of the instructions does not enable them automatically. The kernel has to enable them explicitly in CR4. The reason for this is that older kernels make assumptions about the values in the GS register and enforce them when GS base is set via arch\_prctl(). Allowing user space to write arbitrary values to GS base would violate these assumptions and cause malfunction.
>
> On kernels which do not enable FSGSBASE the execution of the FSGSBASE instructions will fault with a \#UD exception.
>
> The kernel provides reliable information about the enabled state in the ELF AUX vector. If the HWCAP2\_FSGSBASE bit is set in the AUX vector, the kernel has FSGSBASE instructions enabled and applications can use them. The following code example shows how this detection works:
>
>
>
> ```text
> #include <sys/auxv.h>
> #include <elf.h>
>
> /* Will be eventually in asm/hwcap.h */
> #ifndef HWCAP2_FSGSBASE
> #define HWCAP2_FSGSBASE        (1 << 1)
> #endif
>
> ....
>
> unsigned val = getauxval(AT_HWCAP2);
>
> if (val & HWCAP2_FSGSBASE)
>      printf("FSGSBASE enabled\n");
> ```

<a id="rst-fsgsbase-instructions-compiler-support"></a>

### FSGSBASE instructions compiler support

GCC version 4.6.4 and newer provide intrinsics for the FSGSBASE instructions. Clang 5 supports them as well.

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> \_readfsbase\_u64()
>
> </td>
> <td>
>
>
> Read the FS base register
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> \_readgsbase\_u64()
>
> </td>
> <td>
>
>
> Read the GS base register
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> \_writefsbase\_u64()
>
> </td>
> <td>
>
>
> Write the FS base register
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> \_writegsbase\_u64()
>
> </td>
> <td>
>
>
> Write the GS base register
>
> </td>
> </tr>
> </tbody>
> </table>

To utilize these intrinsics &lt;immintrin.h&gt; must be included in the source code and the compiler option -mfsgsbase has to be added.

<a id="rst-compiler-support-for-fs-gs-based-addressing"></a>

## Compiler support for FS/GS based addressing

GCC version 6 and newer provide support for FS/GS based addressing via Named Address Spaces. GCC implements the following address space identifiers for x86:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> \_\_seg\_fs
>
> </td>
> <td>
>
>
> Variable is addressed relative to FS
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> \_\_seg\_gs
>
> </td>
> <td>
>
>
> Variable is addressed relative to GS
>
> </td>
> </tr>
> </tbody>
> </table>

The preprocessor symbols \_\_SEG\_FS and \_\_SEG\_GS are defined when these address spaces are supported. Code which implements fallback modes should check whether these symbols are defined. Usage example:



```text
#ifdef __SEG_GS

long data0 = 0;
long data1 = 1;

long __seg_gs *ptr;

/* Check whether FSGSBASE is enabled by the kernel (HWCAP2_FSGSBASE) */
....

/* Set GS base to point to data0 */
_writegsbase_u64(&data0);

/* Access offset 0 of GS */
ptr = 0;
printf("data0 = %ld\n", *ptr);

/* Set GS base to point to data1 */
_writegsbase_u64(&data1);
/* ptr still addresses offset 0! */
printf("data1 = %ld\n", *ptr);
```

Clang does not provide the GCC address space identifiers, but it provides address spaces via an attribute based mechanism in Clang 2.6 and newer versions:

>
> <table>
> <tbody>
> <tr>
> <td>
>
>
> \_\_attribute\_\_((address\_space(256))
>
> </td>
> <td>
>
>
> Variable is addressed relative to GS
>
> </td>
> </tr>
> <tr>
> <td>
>
>
> \_\_attribute\_\_((address\_space(257))
>
> </td>
> <td>
>
>
> Variable is addressed relative to FS
>
> </td>
> </tr>
> </tbody>
> </table>

<a id="rst-fs-gs-based-addressing-with-inline-assembly"></a>

## FS/GS based addressing with inline assembly

In case the compiler does not support address spaces, inline assembly can be used for FS/GS based addressing mode:



```text
mov %fs:offset, %reg
mov %gs:offset, %reg

mov %reg, %fs:offset
mov %reg, %gs:offset
```

---

# Configurable sysfs parameters for the x86-64 machine check code

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> Machine checks report internal hardware error conditions detected by the CPU. Uncorrected errors typically cause a machine check (often with panic), corrected ones cause a machine check log entry.

<a id="rst-configurable-sysfs-parameters-for-the-x86-64-machine-check-code"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

Machine checks report internal hardware error conditions detected by the CPU. Uncorrected errors typically cause a machine check (often with panic), corrected ones cause a machine check log entry.

Machine checks are organized in banks (normally associated with a hardware subsystem) and subevents in a bank. The exact meaning of the banks and subevent is CPU specific.

mcelog knows how to decode them.

When you see the "Machine check errors logged" message in the system log then mcelog should run to collect and decode machine check entries from /dev/mcelog. Normally mcelog should be run regularly from a cronjob.

Each CPU has a directory in /sys/devices/system/machinecheck/machinecheckN (N = CPU number).

The directory contains some configurable entries. See Documentation/ABI/testing/sysfs-mce for more details.

TBD document entries for AMD threshold interrupt configuration

For more details about the x86 machine check architecture see the Intel and AMD architecture manuals from their developer websites.

For more details about the architecture see [http://one.firstfloor.org/\~andi/mce.pdf](<http://one.firstfloor.org/~andi/mce.pdf>)

---

# Memory Management

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> Architecture defines a 64-bit virtual address. Implementations can support less. Currently supported are 48- and 57-bit virtual addresses. Bits 63 through to the most-significant implemented bit are sign extended. This causes hole between…

<a id="rst-memory-management"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

<a id="rst-complete-virtual-memory-map-with-4-level-page-tables"></a>

## Complete virtual memory map with 4-level page tables

> [!NOTE]
>
> - Negative addresses such as "-23 TB" are absolute addresses in bytes, counted down from the top of the 64-bit address space. It's easier to understand the layout when seen both in absolute addresses and in distance-from-top notation.
>
>   For example 0xffffe90000000000 == -23 TB, it's 23 TB lower than the top of the 64-bit address space (ffffffffffffffff).
>
>   Note that as we get closer to the top of the address space, the notation changes from TB to GB and then MB/KB.
>
> - "16M TB" might look weird at first sight, but it's an easier way to visualize size notation than "16 EB", which few will recognize at first sight as 16 exabytes. It also shows it nicely how incredibly large 64-bit address space is.



```text
========================================================================================================================
    Start addr    |   Offset   |     End addr     |  Size   | VM area description
========================================================================================================================
                  |            |                  |         |
 0000000000000000 |    0       | 00007fffffffefff | ~128 TB | user-space virtual memory, different per mm
 00007ffffffff000 | ~128    TB | 00007fffffffffff |    4 kB | ... guard hole
__________________|____________|__________________|_________|___________________________________________________________
                  |            |                  |         |
 0000800000000000 | +128    TB | 7fffffffffffffff |   ~8 EB | ... huge, almost 63 bits wide hole of non-canonical
                  |            |                  |         |     virtual memory addresses up to the -8 EB
                  |            |                  |         |     starting offset of kernel mappings.
                  |            |                  |         |
                  |            |                  |         | LAM relaxes canonicallity check allowing to create aliases
                  |            |                  |         | for userspace memory here.
__________________|____________|__________________|_________|___________________________________________________________
                                                            |
                                                            | Kernel-space virtual memory, shared between all processes:
__________________|____________|__________________|_________|___________________________________________________________
                  |            |                  |         |
 8000000000000000 |   -8    EB | ffff7fffffffffff |   ~8 EB | ... huge, almost 63 bits wide hole of non-canonical
                  |            |                  |         |     virtual memory addresses up to the -128 TB
                  |            |                  |         |     starting offset of kernel mappings.
                  |            |                  |         |
                  |            |                  |         | LAM_SUP relaxes canonicallity check allowing to create
                  |            |                  |         | aliases for kernel memory here.
____________________________________________________________|___________________________________________________________
                  |            |                  |         |
 ffff800000000000 | -128    TB | ffff87ffffffffff |    8 TB | ... guard hole, also reserved for hypervisor
 ffff880000000000 | -120    TB | ffff887fffffffff |  0.5 TB | LDT remap for PTI
 ffff888000000000 | -119.5  TB | ffffc87fffffffff |   64 TB | direct mapping of all physical memory (page_offset_base)
 ffffc88000000000 |  -55.5  TB | ffffc8ffffffffff |  0.5 TB | ... unused hole
 ffffc90000000000 |  -55    TB | ffffe8ffffffffff |   32 TB | vmalloc/ioremap space (vmalloc_base)
 ffffe90000000000 |  -23    TB | ffffe9ffffffffff |    1 TB | ... unused hole
 ffffea0000000000 |  -22    TB | ffffeaffffffffff |    1 TB | virtual memory map (vmemmap_base)
 ffffeb0000000000 |  -21    TB | ffffebffffffffff |    1 TB | ... unused hole
 ffffec0000000000 |  -20    TB | fffffbffffffffff |   16 TB | KASAN shadow memory
__________________|____________|__________________|_________|____________________________________________________________
                                                            |
                                                            | Identical layout to the 56-bit one from here on:
____________________________________________________________|____________________________________________________________
                  |            |                  |         |
 fffffc0000000000 |   -4    TB | fffffdffffffffff |    2 TB | ... unused hole
                  |            |                  |         | vaddr_end for KASLR
 fffffe0000000000 |   -2    TB | fffffe7fffffffff |  0.5 TB | cpu_entry_area mapping
 fffffe8000000000 |   -1.5  TB | fffffeffffffffff |  0.5 TB | ... unused hole
 ffffff0000000000 |   -1    TB | ffffff7fffffffff |  0.5 TB | %esp fixup stacks
 ffffff8000000000 | -512    GB | ffffffeeffffffff |  444 GB | ... unused hole
 ffffffef00000000 |  -68    GB | fffffffeffffffff |   64 GB | EFI region mapping space
 ffffffff00000000 |   -4    GB | ffffffff7fffffff |    2 GB | ... unused hole
 ffffffff80000000 |   -2    GB | ffffffff9fffffff |  512 MB | kernel text mapping, mapped to physical address 0
 ffffffff80000000 |-2048    MB |                  |         |
 ffffffffa0000000 |-1536    MB | fffffffffeffffff | 1520 MB | module mapping space
 ffffffffff000000 |  -16    MB |                  |         |
    FIXADDR_START | ~-11    MB | ffffffffff5fffff | ~0.5 MB | kernel-internal fixmap range, variable size and offset
 ffffffffff600000 |  -10    MB | ffffffffff600fff |    4 kB | legacy vsyscall ABI
 ffffffffffe00000 |   -2    MB | ffffffffffffffff |    2 MB | ... unused hole
__________________|____________|__________________|_________|___________________________________________________________
```

<a id="rst-complete-virtual-memory-map-with-5-level-page-tables"></a>

## Complete virtual memory map with 5-level page tables

> [!NOTE]
>
> - With 56-bit addresses, user-space memory gets expanded by a factor of 512x, from 0.125 PB to 64 PB. All kernel mappings shift down to the -64 PB starting offset and many of the regions expand to support the much larger physical memory supported.



```text
========================================================================================================================
    Start addr    |   Offset   |     End addr     |  Size   | VM area description
========================================================================================================================
                  |            |                  |         |
 0000000000000000 |    0       | 00fffffffffff000 |  ~64 PB | user-space virtual memory, different per mm
 00fffffffffff000 |  ~64    PB | 00ffffffffffffff |    4 kB | ... guard hole
__________________|____________|__________________|_________|___________________________________________________________
                  |            |                  |         |
 0100000000000000 |  +64    PB | 7fffffffffffffff |   ~8 EB | ... huge, almost 63 bits wide hole of non-canonical
                  |            |                  |         |     virtual memory addresses up to the -8EB TB
                  |            |                  |         |     starting offset of kernel mappings.
                  |            |                  |         |
                  |            |                  |         | LAM relaxes canonicallity check allowing to create aliases
                  |            |                  |         | for userspace memory here.
__________________|____________|__________________|_________|___________________________________________________________
                                                            |
                                                            | Kernel-space virtual memory, shared between all processes:
____________________________________________________________|___________________________________________________________
 8000000000000000 |   -8    EB | feffffffffffffff |   ~8 EB | ... huge, almost 63 bits wide hole of non-canonical
                  |            |                  |         |     virtual memory addresses up to the -64 PB
                  |            |                  |         |     starting offset of kernel mappings.
                  |            |                  |         |
                  |            |                  |         | LAM_SUP relaxes canonicallity check allowing to create
                  |            |                  |         | aliases for kernel memory here.
____________________________________________________________|___________________________________________________________
                  |            |                  |         |
 ff00000000000000 |  -64    PB | ff0fffffffffffff |    4 PB | ... guard hole, also reserved for hypervisor
 ff10000000000000 |  -60    PB | ff10ffffffffffff | 0.25 PB | LDT remap for PTI
 ff11000000000000 |  -59.75 PB | ff90ffffffffffff |   32 PB | direct mapping of all physical memory (page_offset_base)
 ff91000000000000 |  -27.75 PB | ff9fffffffffffff | 3.75 PB | ... unused hole
 ffa0000000000000 |  -24    PB | ffd1ffffffffffff | 12.5 PB | vmalloc/ioremap space (vmalloc_base)
 ffd2000000000000 |  -11.5  PB | ffd3ffffffffffff |  0.5 PB | ... unused hole
 ffd4000000000000 |  -11    PB | ffd5ffffffffffff |  0.5 PB | virtual memory map (vmemmap_base)
 ffd6000000000000 |  -10.5  PB | ffdeffffffffffff | 2.25 PB | ... unused hole
 ffdf000000000000 |   -8.25 PB | fffffbffffffffff |   ~8 PB | KASAN shadow memory
__________________|____________|__________________|_________|____________________________________________________________
                                                            |
                                                            | Identical layout to the 47-bit one from here on:
____________________________________________________________|____________________________________________________________
                  |            |                  |         |
 fffffc0000000000 |   -4    TB | fffffdffffffffff |    2 TB | ... unused hole
                  |            |                  |         | vaddr_end for KASLR
 fffffe0000000000 |   -2    TB | fffffe7fffffffff |  0.5 TB | cpu_entry_area mapping
 fffffe8000000000 |   -1.5  TB | fffffeffffffffff |  0.5 TB | ... unused hole
 ffffff0000000000 |   -1    TB | ffffff7fffffffff |  0.5 TB | %esp fixup stacks
 ffffff8000000000 | -512    GB | ffffffeeffffffff |  444 GB | ... unused hole
 ffffffef00000000 |  -68    GB | fffffffeffffffff |   64 GB | EFI region mapping space
 ffffffff00000000 |   -4    GB | ffffffff7fffffff |    2 GB | ... unused hole
 ffffffff80000000 |   -2    GB | ffffffff9fffffff |  512 MB | kernel text mapping, mapped to physical address 0
 ffffffff80000000 |-2048    MB |                  |         |
 ffffffffa0000000 |-1536    MB | fffffffffeffffff | 1520 MB | module mapping space
 ffffffffff000000 |  -16    MB |                  |         |
    FIXADDR_START | ~-11    MB | ffffffffff5fffff | ~0.5 MB | kernel-internal fixmap range, variable size and offset
 ffffffffff600000 |  -10    MB | ffffffffff600fff |    4 kB | legacy vsyscall ABI
 ffffffffffe00000 |   -2    MB | ffffffffffffffff |    2 MB | ... unused hole
__________________|____________|__________________|_________|___________________________________________________________
```

Architecture defines a 64-bit virtual address. Implementations can support less. Currently supported are 48- and 57-bit virtual addresses. Bits 63 through to the most-significant implemented bit are sign extended. This causes hole between user space and kernel addresses if you interpret them as unsigned.

The direct mapping covers all memory in the system up to the highest memory address (this means in some cases it can also include PCI memory holes).

We map EFI runtime services in the 'efi_pgd' PGD in a 64GB large virtual memory window (this size is arbitrary, it can be raised later if needed). The mappings are not part of any other kernel PGD and are only available during EFI runtime calls.

Note that if CONFIG_RANDOMIZE_MEMORY is enabled, the direct mapping of all physical memory, vmalloc/ioremap space and virtual memory map are randomized. Their order is preserved but their base will be offset early at boot time.

Be very careful vs. KASLR when changing anything here. The KASLR address range must not overlap with anything except the KASAN shadow area, which is correct as KASAN disables KASLR.

For both 4- and 5-level layouts, the KSTACK_ERASE_POISON value in the last 2MB hole: ffffffffffff4111

---

# General note on [U]EFI x86_64 support

_Peios Kernel / Hardware and drivers / Processor architectures / 64-bit x86_

> The nomenclature EFI and UEFI are used interchangeably in this document.

<a id="rst-general-note-on-u-efi-x86-64-support"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

The nomenclature EFI and UEFI are used interchangeably in this document.

Although the tools below are \_not\_ needed for building the kernel, the needed bootloader support and associated tools for x86_64 platforms with EFI firmware and specifications are listed below.

1. UEFI specification:  [http://www.uefi.org](<http://www.uefi.org>)

2. Booting Linux kernel on UEFI x86_64 platform can either be done using the &lt;[Documentation/admin-guide/efi-stub.rst](/kernel/administration/admin-guide/boot/efi-stub.md)&gt; or using a separate bootloader.

3. x86_64 platform with EFI/UEFI firmware.

<a id="rst-mechanics"></a>

## Mechanics

Refer to &lt;[Documentation/admin-guide/efi-stub.rst](/kernel/administration/admin-guide/boot/efi-stub.md)&gt; to learn how to use the EFI stub.

Below are general EFI setup guidelines on the x86_64 platform, regardless of whether you use the EFI stub or a separate bootloader.

- Build the kernel with the following configuration:



  ```text
  CONFIG_FB_EFI=y
  CONFIG_FRAMEBUFFER_CONSOLE=y
  ```

  If EFI runtime services are expected, the following configuration should be selected:



  ```text
  CONFIG_EFI=y
  CONFIG_EFIVAR_FS=y or m         # optional
  ```

- - **Create a VFAT partition on the disk with the EFI System flag**

    You can do this with fdisk with the following commands:

    > 1. g - initialize a GPT partition table
    >
    > 2. n - create a new partition
    >
    > 3. t - change the partition type to "EFI System" (number 1)
    >
    > 4. w - write and save the changes

    Afterwards, initialize the VFAT filesystem by running mkfs:



    ```text
    mkfs.fat /dev/<your-partition>
    ```

- - **Copy the boot files to the VFAT partition:**

    If you use the EFI stub method, the kernel acts also as an EFI executable.

    You can just copy the bzImage to the EFI/boot/bootx64.efi path on the partition so that it will automatically get booted, see the &lt;[Documentation/admin-guide/efi-stub.rst](/kernel/administration/admin-guide/boot/efi-stub.md)&gt; page for additional instructions regarding passage of kernel parameters and initramfs.

    If you use a custom bootloader, refer to the relevant documentation for help on this part.

- If some or all EFI runtime services don't work, you can try following kernel command line parameters to turn off some or all EFI runtime services.

  > - **noefi**
  >
  >   turn off all EFI runtime services
  >
  > - **reboot_type=k**
  >
  >   turn off EFI reboot runtime service

- If the EFI memory map has additional entries not in the E820 map, you can include those entries in the kernels memory map of available physical RAM by using the following kernel command line parameter.

  > - **add_efi_memmap**
  >
  >   include EFI memory map of available physical RAM
