Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

memmap.c 7.0KB

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