mmap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/memory.h>
  21. #include <grub/err.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. GRUB_EXPORT(grub_mmap_free_and_unregister);
  25. GRUB_EXPORT(grub_mmap_malign_and_register);
  26. #ifndef GRUB_MMAP_REGISTER_BY_FIRMWARE
  27. struct grub_mmap_malign_and_register_closure
  28. {
  29. grub_uint64_t align;
  30. grub_uint64_t size;
  31. grub_uint64_t min;
  32. grub_uint64_t max;
  33. grub_uint64_t highestlow;
  34. };
  35. static int
  36. find_hook (grub_uint64_t start, grub_uint64_t rangesize,
  37. grub_uint32_t memtype, void *closure)
  38. {
  39. struct grub_mmap_malign_and_register_closure *c = closure;
  40. grub_uint64_t end = start + rangesize;
  41. grub_uint64_t addr;
  42. if (memtype != GRUB_MACHINE_MEMORY_AVAILABLE)
  43. return 0;
  44. if (end > c->max)
  45. end = c->max;
  46. if (end < c->size)
  47. return 0;
  48. if (start < c->min)
  49. start = c->min;
  50. addr = (end - c->size) - ((end - c->size) & (c->align - 1));
  51. if (addr >= start && c->highestlow < addr)
  52. c->highestlow = addr;
  53. return 0;
  54. }
  55. void *
  56. grub_mmap_malign_and_register (grub_uint64_t align, grub_uint64_t size,
  57. int *handle, int type, int flags)
  58. {
  59. void *ret;
  60. if (flags & (GRUB_MMAP_MALLOC_LOW | GRUB_MMAP_MALLOC_HIGH))
  61. {
  62. struct grub_mmap_malign_and_register_closure c;
  63. c.align = align;
  64. c.size = size;
  65. c.highestlow = 0;
  66. if (flags & GRUB_MMAP_MALLOC_LOW)
  67. {
  68. c.min = 0;
  69. c.max = 0x100000;
  70. }
  71. else
  72. {
  73. c.min = grub_mmap_high;
  74. c.max = 0x100000000ll;
  75. }
  76. /* FIXME: use low-memory mm allocation once it's available. */
  77. grub_mmap_iterate (find_hook, &c);
  78. ret = UINT_TO_PTR (c.highestlow);
  79. }
  80. else
  81. ret = grub_memalign (align, size);
  82. if (! ret)
  83. {
  84. *handle = 0;
  85. return 0;
  86. }
  87. *handle = grub_mmap_register (PTR_TO_UINT64 (ret), size, type);
  88. if (! *handle)
  89. {
  90. grub_free (ret);
  91. return 0;
  92. }
  93. return ret;
  94. }
  95. void
  96. grub_mmap_free_and_unregister (int handle)
  97. {
  98. struct grub_mmap_region *cur;
  99. grub_uint64_t addr;
  100. for (cur = grub_mmap_overlays; cur; cur = cur->next)
  101. if (cur->handle == handle)
  102. break;
  103. if (! cur)
  104. return;
  105. addr = cur->start;
  106. grub_mmap_unregister (handle);
  107. if (addr >= 0x100000)
  108. grub_free (UINT_TO_PTR (addr));
  109. }
  110. #endif