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

basemem.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "stdint.h"
  2. #include "stddef.h"
  3. #include "memsizes.h"
  4. #include "etherboot.h"
  5. #include "basemem.h"
  6. /* Routines to allocate base memory in a BIOS-compatible way, by
  7. * updating the Free Base Memory Size counter at 40:13h.
  8. *
  9. * Michael Brown <mbrown@fensystems.co.uk> (mcb30)
  10. *
  11. * We no longer have anything to do with the real-mode stack. The
  12. * only code that can end up creating a huge bubble of wasted base
  13. * memory is the UNDI driver, so we make it the responsibility of the
  14. * UNDI driver to reallocate the real-mode stack if required.
  15. */
  16. /* "fbms" is an alias to the BIOS FBMS counter at 40:13, and acts just
  17. * like any other uint16_t. We can't be used under -DKEEP_IT_REAL
  18. * anyway, so we may as well be efficient.
  19. */
  20. #define fbms ( * ( ( uint16_t * ) phys_to_virt ( 0x413 ) ) )
  21. #define FBMS_MAX ( 640 )
  22. /* Local prototypes */
  23. static void free_unused_base_memory ( void );
  24. #undef DBG
  25. #ifdef DEBUG_BASEMEM
  26. #define DBG(...) printf ( __VA_ARGS__ )
  27. #else
  28. #define DBG(...)
  29. #endif
  30. /*
  31. * Return amount of free base memory in bytes
  32. *
  33. */
  34. uint32_t get_free_base_memory ( void ) {
  35. return fbms << 10;
  36. }
  37. /* Allocate N bytes of base memory. Amount allocated will be rounded
  38. * up to the nearest kB, since that's the granularity of the BIOS FBMS
  39. * counter. Returns NULL if memory cannot be allocated.
  40. *
  41. */
  42. void * alloc_base_memory ( size_t size ) {
  43. uint16_t size_kb = ( size + 1023 ) >> 10;
  44. void *ptr;
  45. DBG ( "Trying to allocate %d bytes of base memory from %d kB free\n",
  46. size, fbms );
  47. /* Free up any unused memory before we start */
  48. free_unused_base_memory();
  49. /* Check available base memory */
  50. if ( size_kb > fbms ) {
  51. DBG ( "Could not allocate %d kB of base memory: "
  52. "only %d kB free\n", size_kb, fbms );
  53. return NULL;
  54. }
  55. /* Reduce available base memory */
  56. fbms -= size_kb;
  57. /* Calculate address of memory allocated */
  58. ptr = phys_to_virt ( fbms << 10 );
  59. /* Zero out memory. We do this so that allocation of
  60. * already-used space will show up in the form of a crash as
  61. * soon as possible.
  62. *
  63. * Update: there's another reason for doing this. If we don't
  64. * zero the contents, then they could still retain our "free
  65. * block" markers and be liable to being freed whenever a
  66. * base-memory allocation routine is next called.
  67. */
  68. memset ( ptr, 0, size_kb << 10 );
  69. DBG ( "Allocated %d kB of base memory at [%hx:0000,%hx:0000), "
  70. "%d kB now free\n", size_kb,
  71. ( virt_to_phys ( ptr ) >> 4 ),
  72. ( ( virt_to_phys ( ptr ) + ( size_kb << 10 ) ) >> 4 ), fbms );
  73. /* Update our memory map */
  74. get_memsizes();
  75. return ptr;
  76. }
  77. /* Free base memory allocated by alloc_base_memory. The BIOS provides
  78. * nothing better than a LIFO mechanism for freeing memory (i.e. it
  79. * just has the single "total free memory" counter), but we improve
  80. * upon this slightly; as long as you free all the allocated blocks, it
  81. * doesn't matter what order you free them in. (This will only work
  82. * for blocks that are freed via free_base_memory()).
  83. *
  84. * Yes, it's annoying that you have to remember the size of the blocks
  85. * you've allocated. However, since our granularity of allocation is
  86. * 1K, the alternative is to risk wasting the occasional kB of base
  87. * memory, which is a Bad Thing. Really, you should be using as
  88. * little base memory as possible, so consider the awkwardness of the
  89. * API to be a feature! :-)
  90. *
  91. */
  92. void free_base_memory ( void *ptr, size_t size ) {
  93. uint16_t remainder = virt_to_phys ( ptr ) & 1023;
  94. uint16_t size_kb = ( size + remainder + 1023 ) >> 10;
  95. union free_base_memory_block *free_block =
  96. ( ( void * ) ( ptr - remainder ) );
  97. if ( ( ptr == NULL ) || ( size == 0 ) ) {
  98. return;
  99. }
  100. DBG ( "Trying to free %d bytes base memory at %hx:%hx "
  101. "from %d kB free\n", size,
  102. ( virt_to_phys ( ptr - remainder ) >> 4 ),
  103. ( virt_to_phys ( ptr - remainder ) & 0xf ) + remainder,
  104. fbms );
  105. /* Mark every kilobyte within this block as free. This is
  106. * overkill for normal purposes, but helps when something has
  107. * allocated base memory with a granularity finer than the
  108. * BIOS granularity of 1kB. PXE ROMs tend to do this when
  109. * they allocate their own memory. This method allows us to
  110. * free their blocks (admittedly in a rather dangerous,
  111. * tread-on-anything-either-side sort of way, but there's no
  112. * other way to do it).
  113. *
  114. * Since we're marking every kB as free, there's actually no
  115. * need for recording the size of the blocks. However, we
  116. * keep this in so that debug messages are friendlier. It
  117. * probably adds around 8 bytes to the overall code size.
  118. */
  119. for ( ; size_kb > 0 ; free_block++, size_kb-- ) {
  120. /* Mark this block as unused */
  121. free_block->magic = FREE_BLOCK_MAGIC;
  122. free_block->size_kb = size_kb;
  123. }
  124. /* Free up unused base memory */
  125. free_unused_base_memory();
  126. /* Update our memory map */
  127. get_memsizes();
  128. }
  129. /* Do the actual freeing of memory. This is split out from
  130. * free_base_memory() so that it may be called separately. It
  131. * should be called whenever base memory is deallocated by an external
  132. * entity (if we can detect that it has done so) so that we get the
  133. * chance to free up our own blocks.
  134. */
  135. static void free_unused_base_memory ( void ) {
  136. union free_base_memory_block *free_block;
  137. /* Try to release memory back to the BIOS. Free all
  138. * consecutive blocks marked as free.
  139. */
  140. while ( 1 ) {
  141. /* Calculate address of next potential free block */
  142. free_block = phys_to_virt ( fbms << 10 );
  143. /* Stop processing if we're all the way up to 640K or
  144. * if this is not a free block
  145. */
  146. if ( ( fbms == FBMS_MAX ) ||
  147. ( free_block->magic != FREE_BLOCK_MAGIC ) ) {
  148. break;
  149. }
  150. /* Return memory to BIOS */
  151. fbms += free_block->size_kb;
  152. DBG ( "Freed %d kB of base memory at [%hx:0000,%hx:0000), "
  153. "%d kB now free\n",
  154. free_block->size_kb,
  155. ( virt_to_phys ( free_block ) >> 4 ),
  156. ( ( virt_to_phys ( free_block ) +
  157. ( free_block->size_kb << 10 ) ) >> 4 ),
  158. fbms );
  159. /* Do not zero out the freed block, because it might
  160. * be the one containing librm, in which case we're
  161. * going to have severe problems the next time we use
  162. * DBG() or, failing that, call get_memsizes().
  163. */
  164. }
  165. }