iPXE
efi_umalloc.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <errno.h>
00028 #include <assert.h>
00029 #include <ipxe/umalloc.h>
00030 #include <ipxe/efi/efi.h>
00031 
00032 /** @file
00033  *
00034  * iPXE user memory allocation API for EFI
00035  *
00036  */
00037 
00038 /** Equivalent of NOWHERE for user pointers */
00039 #define UNOWHERE ( ~UNULL )
00040 
00041 /**
00042  * Reallocate external memory
00043  *
00044  * @v old_ptr           Memory previously allocated by umalloc(), or UNULL
00045  * @v new_size          Requested size
00046  * @ret new_ptr         Allocated memory, or UNULL
00047  *
00048  * Calling realloc() with a new size of zero is a valid way to free a
00049  * memory block.
00050  */
00051 static userptr_t efi_urealloc ( userptr_t old_ptr, size_t new_size ) {
00052         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00053         EFI_PHYSICAL_ADDRESS phys_addr;
00054         unsigned int new_pages, old_pages;
00055         userptr_t new_ptr = UNOWHERE;
00056         size_t old_size;
00057         EFI_STATUS efirc;
00058         int rc;
00059 
00060         /* Allocate new memory if necessary.  If allocation fails,
00061          * return without touching the old block.
00062          */
00063         if ( new_size ) {
00064                 new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
00065                 if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
00066                                                    EfiBootServicesData,
00067                                                    new_pages,
00068                                                    &phys_addr ) ) != 0 ) {
00069                         rc = -EEFI ( efirc );
00070                         DBG ( "EFI could not allocate %d pages: %s\n",
00071                               new_pages, strerror ( rc ) );
00072                         return UNULL;
00073                 }
00074                 assert ( phys_addr != 0 );
00075                 new_ptr = phys_to_user ( phys_addr + EFI_PAGE_SIZE );
00076                 copy_to_user ( new_ptr, -EFI_PAGE_SIZE,
00077                                &new_size, sizeof ( new_size ) );
00078                 DBG ( "EFI allocated %d pages at %llx\n",
00079                       new_pages, phys_addr );
00080         }
00081 
00082         /* Copy across relevant part of the old data region (if any),
00083          * then free it.  Note that at this point either (a) new_ptr
00084          * is valid, or (b) new_size is 0; either way, the memcpy() is
00085          * valid.
00086          */
00087         if ( old_ptr && ( old_ptr != UNOWHERE ) ) {
00088                 copy_from_user ( &old_size, old_ptr, -EFI_PAGE_SIZE,
00089                                  sizeof ( old_size ) );
00090                 memcpy_user ( new_ptr, 0, old_ptr, 0,
00091                               ( (old_size < new_size) ? old_size : new_size ));
00092                 old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
00093                 phys_addr = user_to_phys ( old_ptr, -EFI_PAGE_SIZE );
00094                 if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
00095                         rc = -EEFI ( efirc );
00096                         DBG ( "EFI could not free %d pages at %llx: %s\n",
00097                               old_pages, phys_addr, strerror ( rc ) );
00098                         /* Not fatal; we have leaked memory but successfully
00099                          * allocated (if asked to do so).
00100                          */
00101                 }
00102                 DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
00103         }
00104 
00105         return new_ptr;
00106 }
00107 
00108 PROVIDE_UMALLOC ( efi, urealloc, efi_urealloc );