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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <errno.h>
  22. #include <realmode.h>
  23. #include <bios.h>
  24. #include <memsizes.h>
  25. #include <ipxe/io.h>
  26. /**
  27. * @file
  28. *
  29. * Memory mapping
  30. *
  31. */
  32. /** Magic value for INT 15,e820 calls */
  33. #define SMAP ( 0x534d4150 )
  34. /** An INT 15,e820 memory map entry */
  35. struct e820_entry {
  36. /** Start of region */
  37. uint64_t start;
  38. /** Length of region */
  39. uint64_t len;
  40. /** Type of region */
  41. uint32_t type;
  42. /** Extended attributes (optional) */
  43. uint32_t attrs;
  44. } __attribute__ (( packed ));
  45. #define E820_TYPE_RAM 1 /**< Normal memory */
  46. #define E820_TYPE_RESERVED 2 /**< Reserved and unavailable */
  47. #define E820_TYPE_ACPI 3 /**< ACPI reclaim memory */
  48. #define E820_TYPE_NVS 4 /**< ACPI NVS memory */
  49. #define E820_ATTR_ENABLED 0x00000001UL
  50. #define E820_ATTR_NONVOLATILE 0x00000002UL
  51. #define E820_ATTR_UNKNOWN 0xfffffffcUL
  52. #define E820_MIN_SIZE 20
  53. /** Buffer for INT 15,e820 calls */
  54. static struct e820_entry __bss16 ( e820buf );
  55. #define e820buf __use_data16 ( e820buf )
  56. /**
  57. * Get size of extended memory via INT 15,e801
  58. *
  59. * @ret extmem Extended memory size, in kB, or 0
  60. */
  61. static unsigned int extmemsize_e801 ( void ) {
  62. uint16_t extmem_1m_to_16m_k, extmem_16m_plus_64k;
  63. uint16_t confmem_1m_to_16m_k, confmem_16m_plus_64k;
  64. unsigned int flags;
  65. unsigned int extmem;
  66. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  67. "int $0x15\n\t"
  68. "pushfw\n\t"
  69. "popw %w0\n\t" )
  70. : "=r" ( flags ),
  71. "=a" ( extmem_1m_to_16m_k ),
  72. "=b" ( extmem_16m_plus_64k ),
  73. "=c" ( confmem_1m_to_16m_k ),
  74. "=d" ( confmem_16m_plus_64k )
  75. : "a" ( 0xe801 ) );
  76. if ( flags & CF ) {
  77. DBG ( "INT 15,e801 failed with CF set\n" );
  78. return 0;
  79. }
  80. if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
  81. DBG ( "INT 15,e801 extmem=0, using confmem\n" );
  82. extmem_1m_to_16m_k = confmem_1m_to_16m_k;
  83. extmem_16m_plus_64k = confmem_16m_plus_64k;
  84. }
  85. extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
  86. DBG ( "INT 15,e801 extended memory size %d+64*%d=%d kB "
  87. "[100000,%llx)\n", extmem_1m_to_16m_k, extmem_16m_plus_64k,
  88. extmem, ( 0x100000 + ( ( ( uint64_t ) extmem ) * 1024 ) ) );
  89. /* Sanity check. Some BIOSes report the entire 4GB address
  90. * space as available, which cannot be correct (since that
  91. * would leave no address space available for 32-bit PCI
  92. * BARs).
  93. */
  94. if ( extmem == ( 0x400000 - 0x400 ) ) {
  95. DBG ( "INT 15,e801 reported whole 4GB; assuming insane\n" );
  96. return 0;
  97. }
  98. return extmem;
  99. }
  100. /**
  101. * Get size of extended memory via INT 15,88
  102. *
  103. * @ret extmem Extended memory size, in kB
  104. */
  105. static unsigned int extmemsize_88 ( void ) {
  106. uint16_t extmem;
  107. /* Ignore CF; it is not reliable for this call */
  108. __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
  109. : "=a" ( extmem ) : "a" ( 0x8800 ) );
  110. DBG ( "INT 15,88 extended memory size %d kB [100000, %x)\n",
  111. extmem, ( 0x100000 + ( extmem * 1024 ) ) );
  112. return extmem;
  113. }
  114. /**
  115. * Get size of extended memory
  116. *
  117. * @ret extmem Extended memory size, in kB
  118. *
  119. * Note that this is only an approximation; for an accurate picture,
  120. * use the E820 memory map obtained via get_memmap();
  121. */
  122. unsigned int extmemsize ( void ) {
  123. unsigned int extmem_e801;
  124. unsigned int extmem_88;
  125. /* Try INT 15,e801 first, then fall back to INT 15,88 */
  126. extmem_88 = extmemsize_88();
  127. extmem_e801 = extmemsize_e801();
  128. return ( extmem_e801 ? extmem_e801 : extmem_88 );
  129. }
  130. /**
  131. * Get e820 memory map
  132. *
  133. * @v memmap Memory map to fill in
  134. * @ret rc Return status code
  135. */
  136. static int meme820 ( struct memory_map *memmap ) {
  137. struct memory_region *region = memmap->regions;
  138. struct memory_region *prev_region = NULL;
  139. uint32_t next = 0;
  140. uint32_t smap;
  141. size_t size;
  142. unsigned int flags;
  143. unsigned int discard_D;
  144. /* Clear the E820 buffer. Do this once before starting,
  145. * rather than on each call; some BIOSes rely on the contents
  146. * being preserved between calls.
  147. */
  148. memset ( &e820buf, 0, sizeof ( e820buf ) );
  149. do {
  150. /* Some BIOSes corrupt %esi for fun. Guard against
  151. * this by telling gcc that all non-output registers
  152. * may be corrupted.
  153. */
  154. __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t"
  155. "stc\n\t"
  156. "int $0x15\n\t"
  157. "pushfw\n\t"
  158. "popw %%dx\n\t"
  159. "popl %%ebp\n\t" )
  160. : "=a" ( smap ), "=b" ( next ),
  161. "=c" ( size ), "=d" ( flags ),
  162. "=D" ( discard_D )
  163. : "a" ( 0xe820 ), "b" ( next ),
  164. "D" ( __from_data16 ( &e820buf ) ),
  165. "c" ( sizeof ( e820buf ) ),
  166. "d" ( SMAP )
  167. : "esi", "memory" );
  168. if ( smap != SMAP ) {
  169. DBG ( "INT 15,e820 failed SMAP signature check\n" );
  170. return -ENOTSUP;
  171. }
  172. if ( size < E820_MIN_SIZE ) {
  173. DBG ( "INT 15,e820 returned only %zd bytes\n", size );
  174. return -EINVAL;
  175. }
  176. if ( flags & CF ) {
  177. DBG ( "INT 15,e820 terminated on CF set\n" );
  178. break;
  179. }
  180. /* If first region is not RAM, assume map is invalid */
  181. if ( ( memmap->count == 0 ) &&
  182. ( e820buf.type != E820_TYPE_RAM ) ) {
  183. DBG ( "INT 15,e820 failed, first entry not RAM\n" );
  184. return -EINVAL;
  185. }
  186. DBG ( "INT 15,e820 region [%llx,%llx) type %d",
  187. e820buf.start, ( e820buf.start + e820buf.len ),
  188. ( int ) e820buf.type );
  189. if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
  190. DBG ( " (%s", ( ( e820buf.attrs & E820_ATTR_ENABLED )
  191. ? "enabled" : "disabled" ) );
  192. if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
  193. DBG ( ", non-volatile" );
  194. if ( e820buf.attrs & E820_ATTR_UNKNOWN )
  195. DBG ( ", other [%08x]", e820buf.attrs );
  196. DBG ( ")" );
  197. }
  198. DBG ( "\n" );
  199. /* Discard non-RAM regions */
  200. if ( e820buf.type != E820_TYPE_RAM )
  201. continue;
  202. /* Check extended attributes, if present */
  203. if ( size > offsetof ( typeof ( e820buf ), attrs ) ) {
  204. if ( ! ( e820buf.attrs & E820_ATTR_ENABLED ) )
  205. continue;
  206. if ( e820buf.attrs & E820_ATTR_NONVOLATILE )
  207. continue;
  208. }
  209. region->start = e820buf.start;
  210. region->end = e820buf.start + e820buf.len;
  211. /* Check for adjacent regions and merge them */
  212. if ( prev_region && ( region->start == prev_region->end ) ) {
  213. prev_region->end = region->end;
  214. } else {
  215. prev_region = region;
  216. region++;
  217. memmap->count++;
  218. }
  219. if ( memmap->count >= ( sizeof ( memmap->regions ) /
  220. sizeof ( memmap->regions[0] ) ) ) {
  221. DBG ( "INT 15,e820 too many regions returned\n" );
  222. /* Not a fatal error; what we've got so far at
  223. * least represents valid regions of memory,
  224. * even if we couldn't get them all.
  225. */
  226. break;
  227. }
  228. } while ( next != 0 );
  229. /* Sanity checks. Some BIOSes report complete garbage via INT
  230. * 15,e820 (especially at POST time), despite passing the
  231. * signature checks. We currently check for a base memory
  232. * region (starting at 0) and at least one high memory region
  233. * (starting at 0x100000).
  234. */
  235. if ( memmap->count < 2 ) {
  236. DBG ( "INT 15,e820 returned only %d regions; assuming "
  237. "insane\n", memmap->count );
  238. return -EINVAL;
  239. }
  240. if ( memmap->regions[0].start != 0 ) {
  241. DBG ( "INT 15,e820 region 0 starts at %llx (expected 0); "
  242. "assuming insane\n", memmap->regions[0].start );
  243. return -EINVAL;
  244. }
  245. if ( memmap->regions[1].start != 0x100000 ) {
  246. DBG ( "INT 15,e820 region 1 starts at %llx (expected 100000); "
  247. "assuming insane\n", memmap->regions[0].start );
  248. return -EINVAL;
  249. }
  250. return 0;
  251. }
  252. /**
  253. * Get memory map
  254. *
  255. * @v memmap Memory map to fill in
  256. */
  257. void x86_get_memmap ( struct memory_map *memmap ) {
  258. unsigned int basemem, extmem;
  259. int rc;
  260. DBG ( "Fetching system memory map\n" );
  261. /* Clear memory map */
  262. memset ( memmap, 0, sizeof ( *memmap ) );
  263. /* Get base and extended memory sizes */
  264. basemem = basememsize();
  265. DBG ( "FBMS base memory size %d kB [0,%x)\n",
  266. basemem, ( basemem * 1024 ) );
  267. extmem = extmemsize();
  268. /* Try INT 15,e820 first */
  269. if ( ( rc = meme820 ( memmap ) ) == 0 ) {
  270. DBG ( "Obtained system memory map via INT 15,e820\n" );
  271. return;
  272. }
  273. /* Fall back to constructing a map from basemem and extmem sizes */
  274. DBG ( "INT 15,e820 failed; constructing map\n" );
  275. memmap->regions[0].end = ( basemem * 1024 );
  276. memmap->regions[1].start = 0x100000;
  277. memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
  278. memmap->count = 2;
  279. }
  280. PROVIDE_IOAPI ( x86, get_memmap, x86_get_memmap );