iPXE
Macros | Functions
efi_umalloc.c File Reference

iPXE user memory allocation API for EFI More...

#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/umalloc.h>
#include <ipxe/efi/efi.h>

Go to the source code of this file.

Macros

#define UNOWHERE   ( ~UNULL )
 Equivalent of NOWHERE for user pointers. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static userptr_t efi_urealloc (userptr_t old_ptr, size_t new_size)
 Reallocate external memory. More...
 
 PROVIDE_UMALLOC (efi, urealloc, efi_urealloc)
 

Detailed Description

iPXE user memory allocation API for EFI

Definition in file efi_umalloc.c.

Macro Definition Documentation

◆ UNOWHERE

#define UNOWHERE   ( ~UNULL )

Equivalent of NOWHERE for user pointers.

Definition at line 39 of file efi_umalloc.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ efi_urealloc()

static userptr_t efi_urealloc ( userptr_t  old_ptr,
size_t  new_size 
)
static

Reallocate external memory.

Parameters
old_ptrMemory previously allocated by umalloc(), or UNULL
new_sizeRequested size
Return values
new_ptrAllocated memory, or UNULL

Calling realloc() with a new size of zero is a valid way to free a memory block.

Definition at line 51 of file efi_umalloc.c.

51  {
53  EFI_PHYSICAL_ADDRESS phys_addr;
54  unsigned int new_pages, old_pages;
55  userptr_t new_ptr = UNOWHERE;
56  size_t old_size;
57  EFI_STATUS efirc;
58  int rc;
59 
60  /* Allocate new memory if necessary. If allocation fails,
61  * return without touching the old block.
62  */
63  if ( new_size ) {
64  new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
65  if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
67  new_pages,
68  &phys_addr ) ) != 0 ) {
69  rc = -EEFI ( efirc );
70  DBG ( "EFI could not allocate %d pages: %s\n",
71  new_pages, strerror ( rc ) );
72  return UNULL;
73  }
74  assert ( phys_addr != 0 );
75  new_ptr = phys_to_user ( phys_addr + EFI_PAGE_SIZE );
76  copy_to_user ( new_ptr, -EFI_PAGE_SIZE,
77  &new_size, sizeof ( new_size ) );
78  DBG ( "EFI allocated %d pages at %llx\n",
79  new_pages, phys_addr );
80  }
81 
82  /* Copy across relevant part of the old data region (if any),
83  * then free it. Note that at this point either (a) new_ptr
84  * is valid, or (b) new_size is 0; either way, the memcpy() is
85  * valid.
86  */
87  if ( old_ptr && ( old_ptr != UNOWHERE ) ) {
88  copy_from_user ( &old_size, old_ptr, -EFI_PAGE_SIZE,
89  sizeof ( old_size ) );
90  memcpy_user ( new_ptr, 0, old_ptr, 0,
91  ( (old_size < new_size) ? old_size : new_size ));
92  old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
93  phys_addr = user_to_phys ( old_ptr, -EFI_PAGE_SIZE );
94  if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
95  rc = -EEFI ( efirc );
96  DBG ( "EFI could not free %d pages at %llx: %s\n",
97  old_pages, phys_addr, strerror ( rc ) );
98  /* Not fatal; we have leaked memory but successfully
99  * allocated (if asked to do so).
100  */
101  }
102  DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
103  }
104 
105  return new_ptr;
106 }
#define UNOWHERE
Equivalent of NOWHERE for user pointers.
Definition: efi_umalloc.c:39
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:191
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:157
static __always_inline void copy_from_user(void *dest, userptr_t src, off_t src_off, size_t len)
Copy data from user buffer.
Definition: uaccess.h:337
unsigned long user_to_phys(userptr_t userptr, off_t offset)
Convert user pointer to physical address.
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
UINT64 EFI_PHYSICAL_ADDRESS
64-bit physical memory address.
Definition: UefiBaseType.h:56
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1836
static __always_inline void copy_to_user(userptr_t dest, off_t dest_off, const void *src, size_t len)
Copy data to user buffer.
Definition: uaccess.h:324
#define EFI_SIZE_TO_PAGES(Size)
Macro that converts a size, in bytes, to a number of EFI_PAGESs.
Definition: UefiBaseType.h:206
EFI_ALLOCATE_PAGES AllocatePages
Definition: UefiSpec.h:1851
#define UNULL
Equivalent of NULL for user pointers.
Definition: uaccess.h:36
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:35
Allocate any available range of pages that satisfies the request.
Definition: UefiSpec.h:38
EFI_SYSTEM_TABLE * efi_systab
The data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot...
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
EFI_FREE_PAGES FreePages
Definition: UefiSpec.h:1852
void memcpy_user(userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, size_t len)
Copy data between user buffers.
unsigned long userptr_t
A pointer to a user buffer.
Definition: uaccess.h:33

References AllocateAnyPages, EFI_BOOT_SERVICES::AllocatePages, assert(), EFI_SYSTEM_TABLE::BootServices, copy_from_user(), copy_to_user(), DBG, EEFI, EFI_PAGE_SIZE, EFI_SIZE_TO_PAGES, efi_systab, EfiBootServicesData, EFI_BOOT_SERVICES::FreePages, memcpy_user(), phys_to_user(), rc, strerror(), UNOWHERE, UNULL, and user_to_phys().

◆ PROVIDE_UMALLOC()

PROVIDE_UMALLOC ( efi  ,
urealloc  ,
efi_urealloc   
)