mmap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Mmap management. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/machine/memory.h>
  20. #include <grub/i386/memory.h>
  21. #include <grub/memory.h>
  22. #include <grub/err.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
  26. /* Context for grub_mmap_malign_and_register. */
  27. struct grub_mmap_malign_and_register_ctx
  28. {
  29. grub_uint64_t align, size, highestlow;
  30. };
  31. /* Helper for grub_mmap_malign_and_register. */
  32. static int
  33. find_hook (grub_uint64_t start, grub_uint64_t rangesize,
  34. grub_memory_type_t memtype, void *data)
  35. {
  36. struct grub_mmap_malign_and_register_ctx *ctx = data;
  37. grub_uint64_t end = start + rangesize;
  38. if (memtype != GRUB_MEMORY_AVAILABLE)
  39. return 0;
  40. if (end > 0x100000)
  41. end = 0x100000;
  42. if (end > start + ctx->size
  43. && ctx->highestlow < ((end - ctx->size)
  44. - ((end - ctx->size) & (ctx->align - 1))))
  45. ctx->highestlow = (end - ctx->size)
  46. - ((end - ctx->size) & (ctx->align - 1));
  47. return 0;
  48. }
  49. void *
  50. grub_mmap_malign_and_register (grub_uint64_t align, grub_uint64_t size,
  51. int *handle, int type, int flags)
  52. {
  53. struct grub_mmap_malign_and_register_ctx ctx = {
  54. .align = align,
  55. .size = size,
  56. .highestlow = 0
  57. };
  58. void *ret;
  59. if (flags & GRUB_MMAP_MALLOC_LOW)
  60. {
  61. /* FIXME: use low-memory mm allocation once it's available. */
  62. grub_mmap_iterate (find_hook, &ctx);
  63. ret = (void *) (grub_addr_t) ctx.highestlow;
  64. }
  65. else
  66. ret = grub_memalign (align, size);
  67. if (! ret)
  68. {
  69. *handle = 0;
  70. return 0;
  71. }
  72. *handle = grub_mmap_register ((grub_addr_t) ret, size, type);
  73. if (! *handle)
  74. {
  75. grub_free (ret);
  76. return 0;
  77. }
  78. return ret;
  79. }
  80. void
  81. grub_mmap_free_and_unregister (int handle)
  82. {
  83. struct grub_mmap_region *cur;
  84. grub_uint64_t addr;
  85. for (cur = grub_mmap_overlays; cur; cur = cur->next)
  86. if (cur->handle == handle)
  87. break;
  88. if (! cur)
  89. return;
  90. addr = cur->start;
  91. grub_mmap_unregister (handle);
  92. if (addr >= 0x100000)
  93. grub_free ((void *) (grub_addr_t) addr);
  94. }
  95. #endif