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

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