Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

linux_umalloc.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
  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 St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. FILE_LICENCE(GPL2_OR_LATER);
  19. #include <valgrind/memcheck.h>
  20. /** @file
  21. *
  22. * iPXE user memory allocation API for linux
  23. *
  24. */
  25. #include <assert.h>
  26. #include <ipxe/umalloc.h>
  27. #include <linux_api.h>
  28. /** Special address returned for empty allocations */
  29. #define NOWHERE ((void *)-1)
  30. /** Poison to make the metadata more unique */
  31. #define POISON 0xa5a5a5a5
  32. #define min(a,b) (((a)<(b))?(a):(b))
  33. /** Metadata stored at the beginning of all allocations */
  34. struct metadata
  35. {
  36. unsigned poison;
  37. size_t size;
  38. };
  39. #define SIZE_MD (sizeof(struct metadata))
  40. /** Simple realloc which passes most of the work to mmap(), mremap() and munmap() */
  41. static void * linux_realloc(void *ptr, size_t size)
  42. {
  43. struct metadata md = {0, 0};
  44. struct metadata * mdptr = NULL;
  45. DBG2("linux_realloc(%p, %zd)\n", ptr, size);
  46. /* Check whether we have a valid pointer */
  47. if (ptr != NULL && ptr != NOWHERE) {
  48. mdptr = ptr - SIZE_MD;
  49. VALGRIND_MAKE_MEM_DEFINED(mdptr, SIZE_MD);
  50. md = *mdptr;
  51. VALGRIND_MAKE_MEM_NOACCESS(mdptr, SIZE_MD);
  52. /* Check for poison in the metadata */
  53. if (md.poison != POISON) {
  54. DBG("linux_realloc bad poison: 0x%x (expected 0x%x)\n", md.poison, POISON);
  55. return NULL;
  56. }
  57. } else {
  58. /* Handle NOWHERE as NULL */
  59. ptr = NULL;
  60. }
  61. /*
  62. * At this point, ptr is either NULL or pointing to a region allocated by us.
  63. * In the latter case mdptr is pointing to a valid metadata, otherwise it is NULL.
  64. */
  65. /* Handle deallocation or allocation of size 0 */
  66. if (size == 0) {
  67. if (mdptr) {
  68. if (linux_munmap(mdptr, md.size))
  69. DBG("linux_realloc munmap failed: %s\n", linux_strerror(linux_errno));
  70. VALGRIND_FREELIKE_BLOCK(ptr, sizeof(*mdptr));
  71. }
  72. return NOWHERE;
  73. }
  74. if (ptr) {
  75. char *vbits = NULL;
  76. if (RUNNING_ON_VALGRIND > 0)
  77. vbits = linux_realloc(NULL, min(size, md.size));
  78. /* prevent an unused variable warning when building w/o valgrind support */
  79. #ifndef NVALGRIND
  80. VALGRIND_GET_VBITS(ptr, vbits, min(size, md.size));
  81. #endif
  82. VALGRIND_FREELIKE_BLOCK(ptr, SIZE_MD);
  83. mdptr = linux_mremap(mdptr, md.size + SIZE_MD, size + SIZE_MD, MREMAP_MAYMOVE);
  84. if (mdptr == MAP_FAILED) {
  85. DBG("linux_realloc mremap failed: %s\n", linux_strerror(linux_errno));
  86. return NULL;
  87. }
  88. ptr = ((void *)mdptr) + SIZE_MD;
  89. VALGRIND_MALLOCLIKE_BLOCK(ptr, size, SIZE_MD, 0);
  90. /* prevent an unused variable warning when building w/o valgrind support */
  91. #ifndef NVALGRIND
  92. VALGRIND_SET_VBITS(ptr, vbits, min(size, md.size));
  93. #endif
  94. if (RUNNING_ON_VALGRIND > 0)
  95. linux_realloc(vbits, 0);
  96. } else {
  97. mdptr = linux_mmap(NULL, size + SIZE_MD, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  98. if (mdptr == MAP_FAILED) {
  99. DBG("linux_realloc mmap failed: %s\n", linux_strerror(linux_errno));
  100. return NULL;
  101. }
  102. ptr = ((void *)mdptr) + SIZE_MD;
  103. VALGRIND_MALLOCLIKE_BLOCK(ptr, size, SIZE_MD, 0);
  104. }
  105. /* Update the metadata */
  106. VALGRIND_MAKE_MEM_DEFINED(mdptr, SIZE_MD);
  107. mdptr->poison = POISON;
  108. mdptr->size = size;
  109. VALGRIND_MAKE_MEM_NOACCESS(mdptr, SIZE_MD);
  110. // VALGRIND_MALLOCLIKE_BLOCK ignores redzones currently, make our own
  111. VALGRIND_MAKE_MEM_NOACCESS(ptr + size, SIZE_MD);
  112. return ptr;
  113. }
  114. /**
  115. * Reallocate external memory
  116. *
  117. * @v old_ptr Memory previously allocated by umalloc(), or UNULL
  118. * @v new_size Requested size
  119. * @ret new_ptr Allocated memory, or UNULL
  120. *
  121. * Calling realloc() with a new size of zero is a valid way to free a
  122. * memory block.
  123. */
  124. static userptr_t linux_urealloc(userptr_t old_ptr, size_t new_size)
  125. {
  126. return (userptr_t)linux_realloc((void *)old_ptr, new_size);
  127. }
  128. PROVIDE_UMALLOC(linux, urealloc, linux_urealloc);