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

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