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

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