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.5KB

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