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.

basemem.c 6.0KB

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