Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / efi / efi_umalloc.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <string.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <ipxe/umalloc.h>
26 #include <ipxe/efi/efi.h>
27
28 /** @file
29  *
30  * iPXE user memory allocation API for EFI
31  *
32  */
33
34 /** Equivalent of NOWHERE for user pointers */
35 #define UNOWHERE ( ~UNULL )
36
37 /**
38  * Reallocate external memory
39  *
40  * @v old_ptr           Memory previously allocated by umalloc(), or UNULL
41  * @v new_size          Requested size
42  * @ret new_ptr         Allocated memory, or UNULL
43  *
44  * Calling realloc() with a new size of zero is a valid way to free a
45  * memory block.
46  */
47 static userptr_t efi_urealloc ( userptr_t old_ptr, size_t new_size ) {
48         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
49         EFI_PHYSICAL_ADDRESS phys_addr;
50         unsigned int new_pages, old_pages;
51         userptr_t new_ptr = UNOWHERE;
52         size_t old_size;
53         EFI_STATUS efirc;
54         int rc;
55
56         /* Allocate new memory if necessary.  If allocation fails,
57          * return without touching the old block.
58          */
59         if ( new_size ) {
60                 new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
61                 if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
62                                                    EfiBootServicesData,
63                                                    new_pages,
64                                                    &phys_addr ) ) != 0 ) {
65                         rc = -EEFI ( efirc );
66                         DBG ( "EFI could not allocate %d pages: %s\n",
67                               new_pages, strerror ( rc ) );
68                         return UNULL;
69                 }
70                 assert ( phys_addr != 0 );
71                 new_ptr = phys_to_user ( phys_addr + EFI_PAGE_SIZE );
72                 copy_to_user ( new_ptr, -EFI_PAGE_SIZE,
73                                &new_size, sizeof ( new_size ) );
74                 DBG ( "EFI allocated %d pages at %llx\n",
75                       new_pages, phys_addr );
76         }
77
78         /* Copy across relevant part of the old data region (if any),
79          * then free it.  Note that at this point either (a) new_ptr
80          * is valid, or (b) new_size is 0; either way, the memcpy() is
81          * valid.
82          */
83         if ( old_ptr && ( old_ptr != UNOWHERE ) ) {
84                 copy_from_user ( &old_size, old_ptr, -EFI_PAGE_SIZE,
85                                  sizeof ( old_size ) );
86                 memcpy_user ( new_ptr, 0, old_ptr, 0,
87                               ( (old_size < new_size) ? old_size : new_size ));
88                 old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
89                 phys_addr = user_to_phys ( old_ptr, -EFI_PAGE_SIZE );
90                 if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
91                         rc = -EEFI ( efirc );
92                         DBG ( "EFI could not free %d pages at %llx: %s\n",
93                               old_pages, phys_addr, strerror ( rc ) );
94                         /* Not fatal; we have leaked memory but successfully
95                          * allocated (if asked to do so).
96                          */
97                 }
98                 DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
99         }
100
101         return new_ptr;
102 }
103
104 PROVIDE_UMALLOC ( efi, urealloc, efi_urealloc );