You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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)\n",
  70. size_kb, ( fbms << 6 ), ( ( fbms + size_kb ) << 6 ) );
  71. /* Update our memory map */
  72. get_memsizes();
  73. return ptr;
  74. }
  75. /* Free base memory allocated by alloc_base_memory. The BIOS provides
  76. * nothing better than a LIFO mechanism for freeing memory (i.e. it
  77. * just has the single "total free memory" counter), but we improve
  78. * upon this slightly; as long as you free all the allocated blocks, it
  79. * doesn't matter what order you free them in. (This will only work
  80. * for blocks that are freed via free_base_memory()).
  81. *
  82. * Yes, it's annoying that you have to remember the size of the blocks
  83. * you've allocated. However, since our granularity of allocation is
  84. * 1K, the alternative is to risk wasting the occasional kB of base
  85. * memory, which is a Bad Thing. Really, you should be using as
  86. * little base memory as possible, so consider the awkwardness of the
  87. * API to be a feature! :-)
  88. *
  89. */
  90. void free_base_memory ( void *ptr, size_t size ) {
  91. uint16_t remainder = virt_to_phys ( ptr ) & 1023;
  92. uint16_t size_kb = ( size + remainder + 1023 ) >> 10;
  93. union free_base_memory_block *free_block =
  94. ( ( void * ) ( ptr - remainder ) );
  95. if ( ( ptr == NULL ) || ( size == 0 ) ) {
  96. return;
  97. }
  98. DBG ( "Trying to free %d bytes base memory at %hx:%hx\n", size,
  99. ( virt_to_phys ( ptr - remainder ) >> 4 ),
  100. ( virt_to_phys ( ptr - remainder ) & 0xf ) + remainder );
  101. /* Mark every kilobyte within this block as free. This is
  102. * overkill for normal purposes, but helps when something has
  103. * allocated base memory with a granularity finer than the
  104. * BIOS granularity of 1kB. PXE ROMs tend to do this when
  105. * they allocate their own memory. This method allows us to
  106. * free their blocks (admittedly in a rather dangerous,
  107. * tread-on-anything-either-side sort of way, but there's no
  108. * other way to do it).
  109. *
  110. * Since we're marking every kB as free, there's actually no
  111. * need for recording the size of the blocks. However, we
  112. * keep this in so that debug messages are friendlier. It
  113. * probably adds around 8 bytes to the overall code size.
  114. */
  115. for ( ; size_kb > 0 ; free_block++, size_kb-- ) {
  116. /* Mark this block as unused */
  117. free_block->magic = FREE_BLOCK_MAGIC;
  118. free_block->size_kb = size_kb;
  119. }
  120. /* Free up unused base memory */
  121. free_unused_base_memory();
  122. /* Update our memory map */
  123. get_memsizes();
  124. DBG ( "%d kB of base memory now free\n", fbms );
  125. }
  126. /* Do the actual freeing of memory. This is split out from
  127. * free_base_memory() so that it may be called separately. It
  128. * should be called whenever base memory is deallocated by an external
  129. * entity (if we can detect that it has done so) so that we get the
  130. * chance to free up our own blocks.
  131. */
  132. static void free_unused_base_memory ( void ) {
  133. union free_base_memory_block *free_block;
  134. /* Try to release memory back to the BIOS. Free all
  135. * consecutive blocks marked as free.
  136. */
  137. while ( 1 ) {
  138. /* Calculate address of next potential free block */
  139. free_block = phys_to_virt ( fbms << 10 );
  140. /* Stop processing if we're all the way up to 640K or
  141. * if this is not a free block
  142. */
  143. if ( ( fbms == FBMS_MAX ) ||
  144. ( free_block->magic != FREE_BLOCK_MAGIC ) ) {
  145. break;
  146. }
  147. /* Return memory to BIOS */
  148. fbms += free_block->size_kb;
  149. DBG ( "Freed %d kB of base memory at [%hx:0000,%hx:0000)\n",
  150. free_block->size_kb, ( fbms << 6 ),
  151. ( fbms + free_block->size_kb ) << 6 );
  152. /* Do not zero out the freed block, because it might
  153. * be the one containing librm, in which case we're
  154. * going to have severe problems the next time we use
  155. * DBG() or, failing that, call get_memsizes().
  156. */
  157. }
  158. }