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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <assert.h>
00022 #include <gpxe/umalloc.h>
00023 #include <gpxe/efi/efi.h>
00024 
00025 /** @file
00026  *
00027  * gPXE user memory allocation API for EFI
00028  *
00029  */
00030 
00031 /** Equivalent of NOWHERE for user pointers */
00032 #define UNOWHERE ( ~UNULL )
00033 
00034 /**
00035  * Reallocate external memory
00036  *
00037  * @v old_ptr           Memory previously allocated by umalloc(), or UNULL
00038  * @v new_size          Requested size
00039  * @ret new_ptr         Allocated memory, or UNULL
00040  *
00041  * Calling realloc() with a new size of zero is a valid way to free a
00042  * memory block.
00043  */
00044 static userptr_t efi_urealloc ( userptr_t old_ptr, size_t new_size ) {
00045         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00046         EFI_PHYSICAL_ADDRESS phys_addr;
00047         unsigned int new_pages, old_pages;
00048         userptr_t new_ptr = UNOWHERE;
00049         size_t old_size;
00050         EFI_STATUS efirc;
00051 
00052         /* Allocate new memory if necessary.  If allocation fails,
00053          * return without touching the old block.
00054          */
00055         if ( new_size ) {
00056                 new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
00057                 if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
00058                                                    EfiBootServicesData,
00059                                                    new_pages,
00060                                                    &phys_addr ) ) != 0 ) {
00061                         DBG ( "EFI could not allocate %d pages: %s\n",
00062                               new_pages, efi_strerror ( efirc ) );
00063                         return UNULL;
00064                 }
00065                 assert ( phys_addr != 0 );
00066                 new_ptr = phys_to_user ( phys_addr + EFI_PAGE_SIZE );
00067                 copy_to_user ( new_ptr, -EFI_PAGE_SIZE,
00068                                &new_size, sizeof ( new_size ) );
00069                 DBG ( "EFI allocated %d pages at %llx\n",
00070                       new_pages, phys_addr );
00071         }
00072 
00073         /* Copy across relevant part of the old data region (if any),
00074          * then free it.  Note that at this point either (a) new_ptr
00075          * is valid, or (b) new_size is 0; either way, the memcpy() is
00076          * valid.
00077          */
00078         if ( old_ptr && ( old_ptr != UNOWHERE ) ) {
00079                 copy_from_user ( &old_size, old_ptr, -EFI_PAGE_SIZE,
00080                                  sizeof ( old_size ) );
00081                 memcpy_user ( new_ptr, 0, old_ptr, 0,
00082                               ( (old_size < new_size) ? old_size : new_size ));
00083                 old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
00084                 phys_addr = user_to_phys ( old_ptr, -EFI_PAGE_SIZE );
00085                 if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
00086                         DBG ( "EFI could not free %d pages at %llx: %s\n",
00087                               old_pages, phys_addr, efi_strerror ( efirc ) );
00088                         /* Not fatal; we have leaked memory but successfully
00089                          * allocated (if asked to do so).
00090                          */
00091                 }
00092                 DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
00093         }
00094 
00095         return new_ptr;
00096 }
00097 
00098 PROVIDE_UMALLOC ( efi, urealloc, efi_urealloc );

Generated on Tue Apr 6 20:01:09 2010 for gPXE by  doxygen 1.5.7.1