選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

efi_umalloc.c 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <assert.h>
  20. #include <gpxe/umalloc.h>
  21. #include <gpxe/efi/efi.h>
  22. /** @file
  23. *
  24. * gPXE user memory allocation API for EFI
  25. *
  26. */
  27. /** Equivalent of NOWHERE for user pointers */
  28. #define UNOWHERE ( ~UNULL )
  29. /**
  30. * Reallocate external memory
  31. *
  32. * @v old_ptr Memory previously allocated by umalloc(), or UNULL
  33. * @v new_size Requested size
  34. * @ret new_ptr Allocated memory, or UNULL
  35. *
  36. * Calling realloc() with a new size of zero is a valid way to free a
  37. * memory block.
  38. */
  39. static userptr_t efi_urealloc ( userptr_t old_ptr, size_t new_size ) {
  40. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  41. EFI_PHYSICAL_ADDRESS phys_addr;
  42. unsigned int new_pages, old_pages;
  43. userptr_t new_ptr = UNOWHERE;
  44. size_t old_size;
  45. EFI_STATUS efirc;
  46. /* Allocate new memory if necessary. If allocation fails,
  47. * return without touching the old block.
  48. */
  49. if ( new_size ) {
  50. new_pages = ( EFI_SIZE_TO_PAGES ( new_size ) + 1 );
  51. if ( ( efirc = bs->AllocatePages ( AllocateAnyPages,
  52. EfiBootServicesData,
  53. new_pages,
  54. &phys_addr ) ) != 0 ) {
  55. DBG ( "EFI could not allocate %d pages: %s\n",
  56. new_pages, efi_strerror ( efirc ) );
  57. return UNULL;
  58. }
  59. assert ( phys_addr != 0 );
  60. new_ptr = phys_to_user ( phys_addr + EFI_PAGE_SIZE );
  61. copy_to_user ( new_ptr, -EFI_PAGE_SIZE,
  62. &new_size, sizeof ( new_size ) );
  63. DBG ( "EFI allocated %d pages at %llx\n",
  64. new_pages, phys_addr );
  65. }
  66. /* Copy across relevant part of the old data region (if any),
  67. * then free it. Note that at this point either (a) new_ptr
  68. * is valid, or (b) new_size is 0; either way, the memcpy() is
  69. * valid.
  70. */
  71. if ( old_ptr && ( old_ptr != UNOWHERE ) ) {
  72. copy_from_user ( &old_size, old_ptr, -EFI_PAGE_SIZE,
  73. sizeof ( old_size ) );
  74. memcpy_user ( new_ptr, 0, old_ptr, 0,
  75. ( (old_size < new_size) ? old_size : new_size ));
  76. old_pages = ( EFI_SIZE_TO_PAGES ( old_size ) + 1 );
  77. phys_addr = user_to_phys ( old_ptr, -EFI_PAGE_SIZE );
  78. if ( ( efirc = bs->FreePages ( phys_addr, old_pages ) ) != 0 ){
  79. DBG ( "EFI could not free %d pages at %llx: %s\n",
  80. old_pages, phys_addr, efi_strerror ( efirc ) );
  81. /* Not fatal; we have leaked memory but successfully
  82. * allocated (if asked to do so).
  83. */
  84. }
  85. DBG ( "EFI freed %d pages at %llx\n", old_pages, phys_addr );
  86. }
  87. return new_ptr;
  88. }
  89. PROVIDE_UMALLOC ( efi, urealloc, efi_urealloc );