Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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