iPXE
Macros | Functions | Variables
virt_offset.h File Reference

Virtual offset memory model. More...

#include <bits/virt_offset.h>

Go to the source code of this file.

Macros

#define UACCESS_PREFIX_offset   __offset_
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static __always_inline void * UACCESS_INLINE (offset, phys_to_virt)(unsigned long phys)
 Allow for architecture-specific overrides of virt_offset. More...
 
static __always_inline physaddr_t UACCESS_INLINE (offset, virt_to_phys)(volatile const void *virt)
 Convert virtual address to physical address. More...
 

Variables

const unsigned long virt_offset
 Virtual address offset. More...
 

Detailed Description

Virtual offset memory model.

No currently supported machine provides a full 64 bits of physical address space. When we have ownership of the page tables (or segmentation mechanism), we can therefore use the following model:

In both cases, we can define "virt_offset" as "the value to be added to an address within iPXE's own image in order to obtain its physical address". With this definition:

For x86_64-pcbios, we identity-map the low 4GB of address space since the only accesses required above 4GB are for MMIO (typically PCI devices with large memory BARs).

For riscv64-sbi, we identity-map as much of the physical address space as can be mapped by the paging model (Sv39, Sv48, or Sv57) and create a coherent DMA mapping of the low 4GB.

Definition in file virt_offset.h.

Macro Definition Documentation

◆ UACCESS_PREFIX_offset

#define UACCESS_PREFIX_offset   __offset_

Definition at line 54 of file virt_offset.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ UACCESS_INLINE() [1/2]

static __always_inline void* UACCESS_INLINE ( offset  ,
phys_to_virt   
)
inlinestatic

Allow for architecture-specific overrides of virt_offset.

Convert physical address to virtual address

Parameters
physPhysical address
Return values
virtVirtual address

Definition at line 75 of file virt_offset.h.

75  {
76 
77  /* In a 64-bit build, any valid physical address is directly
78  * usable as a virtual address, since physical addresses are
79  * identity-mapped.
80  */
81  if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) )
82  return ( ( void * ) phys );
83 
84  /* In a 32-bit build: subtract virt_offset */
85  return ( ( void * ) ( phys - virt_offset ) );
86 }
static signed char phys[4]
Definition: epic100.c:88
const unsigned long virt_offset
Virtual address offset.
unsigned int uint32_t
Definition: stdint.h:12
unsigned long physaddr_t
Definition: stdint.h:20

References phys, and virt_offset.

◆ UACCESS_INLINE() [2/2]

static __always_inline physaddr_t UACCESS_INLINE ( offset  ,
virt_to_phys   
) const volatile
inlinestatic

Convert virtual address to physical address.

Parameters
virtVirtual address
Return values
physPhysical address

Definition at line 95 of file virt_offset.h.

95  {
96  const physaddr_t msb = ( 1ULL << ( 8 * sizeof ( physaddr_t ) - 1 ) );
97  physaddr_t addr = ( ( physaddr_t ) virt );
98 
99  /* In a 64-bit build, any valid virtual address with the MSB
100  * clear is directly usable as a physical address, since it
101  * must lie within the identity-mapped portion.
102  *
103  * This test will typically reduce to a single "branch if less
104  * than zero" instruction.
105  */
106  if ( ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) &&
107  ( ! ( addr & msb ) ) ) {
108  return addr;
109  }
110 
111  /* In a 32-bit build or in a 64-bit build with a virtual
112  * address with the MSB set: add virt_offset
113  */
114  addr += virt_offset;
115 
116  /* In a 64-bit build with an address that still has the MSB
117  * set after adding virt_offset: truncate the original virtual
118  * address to form a 32-bit physical address.
119  *
120  * This test will also typically reduce to a single "branch if
121  * less than zero" instruction.
122  */
123  if ( ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) &&
124  ( addr & msb ) ) {
125  return ( ( uint32_t ) ( physaddr_t ) virt );
126  }
127 
128  return addr;
129 }
uint32_t addr
Buffer address.
Definition: dwmac.h:20
const unsigned long virt_offset
Virtual address offset.
unsigned int uint32_t
Definition: stdint.h:12
unsigned long physaddr_t
Definition: stdint.h:20

References addr, and virt_offset.

Variable Documentation

◆ virt_offset

const unsigned long virt_offset

Virtual address offset.

This is defined to be the value to be added to an address within iPXE's own image in order to obtain its physical address, as described above.

Referenced by UACCESS_INLINE().