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.

memmap.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <errno.h>
  20. #include <realmode.h>
  21. #include <bios.h>
  22. #include <memmap.h>
  23. /**
  24. * @file
  25. *
  26. * Memory mapping
  27. *
  28. */
  29. /** Magic value for INT 15,e820 calls */
  30. #define SMAP ( 0x534d4150 )
  31. /** An INT 15,e820 memory map entry */
  32. struct e820_entry {
  33. /** Start of region */
  34. uint64_t start;
  35. /** Length of region */
  36. uint64_t len;
  37. /** Type of region */
  38. uint32_t type;
  39. } __attribute__ (( packed ));
  40. #define E820_TYPE_RAM 1 /**< Normal memory */
  41. #define E820_TYPE_RESERVED 2 /**< Reserved and unavailable */
  42. #define E820_TYPE_ACPI 3 /**< ACPI reclaim memory */
  43. #define E820_TYPE_NVS 4 /**< ACPI NVS memory */
  44. /** Buffer for INT 15,e820 calls */
  45. static struct e820_entry __data16 ( e820buf );
  46. #define e820buf __use_data16 ( e820buf )
  47. /**
  48. * Get size of base memory from BIOS free base memory counter
  49. *
  50. * @ret basemem Base memory size, in kB
  51. */
  52. static unsigned int basememsize ( void ) {
  53. uint16_t basemem;
  54. get_real ( basemem, BDA_SEG, 0x0013 );
  55. DBG ( "Base memory size %dkB\n", basemem );
  56. return basemem;
  57. }
  58. /**
  59. * Get size of extended memory via INT 15,e801
  60. *
  61. * @ret extmem Extended memory size, in kB, or 0
  62. */
  63. static unsigned int extmemsize_e801 ( void ) {
  64. uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
  65. uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
  66. unsigned int flags;
  67. unsigned int extmem;
  68. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  69. "int $0x15\n\t"
  70. "pushfw\n\t"
  71. "popw %w0\n\t" )
  72. : "=r" ( flags ),
  73. "=a" ( extmem_1m_to_16m_k ),
  74. "=b" ( extmem_16m_plus_64k ),
  75. "=c" ( confmem_1m_to_16m_k ),
  76. "=d" ( confmem_16m_plus_64k )
  77. : "a" ( 0xe801 ) );
  78. if ( flags & CF )
  79. return 0;
  80. if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
  81. extmem_1m_to_16m_k = confmem_1m_to_16m_k;
  82. extmem_16m_plus_64k = confmem_16m_plus_64k;
  83. }
  84. extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
  85. DBG ( "Extended memory size %d+64*%d=%d kB\n",
  86. extmem_1m_to_16m_k, extmem_16m_plus_64k, extmem );
  87. return extmem;
  88. }
  89. /**
  90. * Get size of extended memory via INT 15,88
  91. *
  92. * @ret extmem Extended memory size, in kB
  93. */
  94. static unsigned int extmemsize_88 ( void ) {
  95. uint16_t extmem;
  96. /* Ignore CF; it is not reliable for this call */
  97. __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
  98. : "=a" ( extmem ) : "a" ( 0x8800 ) );
  99. DBG ( "Extended memory size %d kB\n", extmem );
  100. return extmem;
  101. }
  102. /**
  103. * Get size of extended memory
  104. *
  105. * @ret extmem Extended memory size, in kB
  106. */
  107. static unsigned int extmemsize ( void ) {
  108. unsigned int extmem;
  109. /* Try INT 15,e801 first, then fall back to INT 15,88 */
  110. extmem = extmemsize_e801();
  111. if ( ! extmem )
  112. extmem = extmemsize_88();
  113. return extmem;
  114. }
  115. /**
  116. * Get e820 memory map
  117. *
  118. * @v memmap Memory map to fill in
  119. * @ret rc Return status code
  120. */
  121. static int meme820 ( struct memory_map *memmap ) {
  122. struct memory_region *region = memmap->regions;
  123. uint32_t next = 0;
  124. uint32_t smap;
  125. unsigned int flags;
  126. unsigned int discard_c, discard_d, discard_D;
  127. do {
  128. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  129. "int $0x15\n\t"
  130. "pushfw\n\t"
  131. "popw %w0\n\t" )
  132. : "=r" ( flags ), "=a" ( smap ),
  133. "=b" ( next ), "=D" ( discard_D ),
  134. "=c" ( discard_c ), "=d" ( discard_d )
  135. : "a" ( 0xe820 ), "b" ( next ),
  136. "D" ( &__from_data16 ( e820buf ) ),
  137. "c" ( sizeof ( e820buf ) ),
  138. "d" ( SMAP )
  139. : "memory" );
  140. if ( smap != SMAP )
  141. return -ENOTSUP;
  142. if ( flags & CF )
  143. break;
  144. DBG ( "E820 region [%llx,%llx) type %d\n", e820buf.start,
  145. ( e820buf.start + e820buf.len ), ( int ) e820buf.type );
  146. if ( e820buf.type != E820_TYPE_RAM )
  147. continue;
  148. region->start = e820buf.start;
  149. region->end = e820buf.start + e820buf.len;
  150. region++;
  151. memmap->count++;
  152. } while ( ( next != 0 ) &&
  153. ( memmap->count < ( sizeof ( memmap->regions ) /
  154. sizeof ( memmap->regions[0] ) ) ) );
  155. return 0;
  156. }
  157. /**
  158. * Get memory map
  159. *
  160. * @v memmap Memory map to fill in
  161. */
  162. void get_memmap ( struct memory_map *memmap ) {
  163. unsigned int basemem, extmem;
  164. int rc;
  165. /* Clear memory map */
  166. memset ( memmap, 0, sizeof ( *memmap ) );
  167. /* Get base and extended memory sizes */
  168. basemem = basememsize();
  169. extmem = extmemsize();
  170. /* Try INT 15,e820 first */
  171. if ( ( rc = meme820 ( memmap ) ) == 0 )
  172. return;
  173. /* Fall back to constructing a map from basemem and extmem sizes */
  174. memmap->regions[0].end = ( basemem * 1024 );
  175. memmap->regions[1].start = 0x100000;
  176. memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
  177. memmap->count = 2;
  178. }