Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

memmap.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. return 0;
  70. if ( ! ( extmem_1m_to_16m_k | extmem_16m_plus_64k ) ) {
  71. extmem_1m_to_16m_k = confmem_1m_to_16m_k;
  72. extmem_16m_plus_64k = confmem_16m_plus_64k;
  73. }
  74. extmem = ( extmem_1m_to_16m_k + ( extmem_16m_plus_64k * 64 ) );
  75. DBG ( "Extended memory size %d+64*%d=%d kB\n",
  76. extmem_1m_to_16m_k, extmem_16m_plus_64k, extmem );
  77. return extmem;
  78. }
  79. /**
  80. * Get size of extended memory via INT 15,88
  81. *
  82. * @ret extmem Extended memory size, in kB
  83. */
  84. static unsigned int extmemsize_88 ( void ) {
  85. uint16_t extmem;
  86. /* Ignore CF; it is not reliable for this call */
  87. __asm__ __volatile__ ( REAL_CODE ( "int $0x15" )
  88. : "=a" ( extmem ) : "a" ( 0x8800 ) );
  89. DBG ( "Extended memory size %d kB\n", extmem );
  90. return extmem;
  91. }
  92. /**
  93. * Get size of extended memory
  94. *
  95. * @ret extmem Extended memory size, in kB
  96. *
  97. * Note that this is only an approximation; for an accurate picture,
  98. * use the E820 memory map obtained via get_memmap();
  99. */
  100. unsigned int extmemsize ( void ) {
  101. unsigned int extmem;
  102. /* Try INT 15,e801 first, then fall back to INT 15,88 */
  103. extmem = extmemsize_e801();
  104. if ( ! extmem )
  105. extmem = extmemsize_88();
  106. return extmem;
  107. }
  108. /**
  109. * Get e820 memory map
  110. *
  111. * @v memmap Memory map to fill in
  112. * @ret rc Return status code
  113. */
  114. static int meme820 ( struct memory_map *memmap ) {
  115. struct memory_region *region = memmap->regions;
  116. uint32_t next = 0;
  117. uint32_t smap;
  118. unsigned int flags;
  119. unsigned int discard_c, discard_d, discard_D;
  120. do {
  121. __asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
  122. "int $0x15\n\t"
  123. "pushfw\n\t"
  124. "popw %w0\n\t" )
  125. : "=r" ( flags ), "=a" ( smap ),
  126. "=b" ( next ), "=D" ( discard_D ),
  127. "=c" ( discard_c ), "=d" ( discard_d )
  128. : "a" ( 0xe820 ), "b" ( next ),
  129. "D" ( &__from_data16 ( e820buf ) ),
  130. "c" ( sizeof ( e820buf ) ),
  131. "d" ( SMAP )
  132. : "memory" );
  133. if ( smap != SMAP )
  134. return -ENOTSUP;
  135. if ( flags & CF )
  136. break;
  137. DBG ( "E820 region [%llx,%llx) type %d\n", e820buf.start,
  138. ( e820buf.start + e820buf.len ), ( int ) e820buf.type );
  139. if ( e820buf.type != E820_TYPE_RAM )
  140. continue;
  141. region->start = e820buf.start;
  142. region->end = e820buf.start + e820buf.len;
  143. region++;
  144. memmap->count++;
  145. } while ( ( next != 0 ) &&
  146. ( memmap->count < ( sizeof ( memmap->regions ) /
  147. sizeof ( memmap->regions[0] ) ) ) );
  148. return 0;
  149. }
  150. /**
  151. * Get memory map
  152. *
  153. * @v memmap Memory map to fill in
  154. */
  155. void get_memmap ( struct memory_map *memmap ) {
  156. unsigned int basemem, extmem;
  157. int rc;
  158. /* Clear memory map */
  159. memset ( memmap, 0, sizeof ( *memmap ) );
  160. /* Get base and extended memory sizes */
  161. basemem = basememsize();
  162. extmem = extmemsize();
  163. /* Try INT 15,e820 first */
  164. if ( ( rc = meme820 ( memmap ) ) == 0 )
  165. return;
  166. /* Fall back to constructing a map from basemem and extmem sizes */
  167. memmap->regions[0].end = ( basemem * 1024 );
  168. memmap->regions[1].start = 0x100000;
  169. memmap->regions[1].end = 0x100000 + ( extmem * 1024 );
  170. memmap->count = 2;
  171. }