memory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  6. *
  7. * This file is part of the Linux kernel, and is made available under
  8. * the terms of the GNU General Public License version 2.
  9. *
  10. * ----------------------------------------------------------------------- */
  11. /*
  12. * Memory detection code
  13. */
  14. #include "boot.h"
  15. #define SMAP 0x534d4150 /* ASCII "SMAP" */
  16. struct e820_ext_entry {
  17. struct e820entry std;
  18. u32 ext_flags;
  19. } __attribute__((packed));
  20. static int detect_memory_e820(void)
  21. {
  22. int count = 0;
  23. u32 next = 0;
  24. u32 size, id, edi;
  25. u8 err;
  26. struct e820entry *desc = boot_params.e820_map;
  27. static struct e820_ext_entry buf; /* static so it is zeroed */
  28. do {
  29. size = sizeof buf;
  30. /* Important: %edx and %esi are clobbered by some BIOSes,
  31. so they must be either used for the error output
  32. or explicitly marked clobbered. Given that, assume there
  33. is something out there clobbering %ebp and %edi, too. */
  34. asm("pushl %%ebp; int $0x15; popl %%ebp; setc %0"
  35. : "=d" (err), "+b" (next), "=a" (id), "+c" (size),
  36. "=D" (edi), "+m" (buf)
  37. : "D" (&buf), "d" (SMAP), "a" (0xe820)
  38. : "esi");
  39. /* BIOSes which terminate the chain with CF = 1 as opposed
  40. to %ebx = 0 don't always report the SMAP signature on
  41. the final, failing, probe. */
  42. if (err)
  43. break;
  44. /* Some BIOSes stop returning SMAP in the middle of
  45. the search loop. We don't know exactly how the BIOS
  46. screwed up the map at that point, we might have a
  47. partial map, the full map, or complete garbage, so
  48. just return failure. */
  49. if (id != SMAP) {
  50. count = 0;
  51. break;
  52. }
  53. /* ACPI 3.0 added the extended flags support. If bit 0
  54. in the extended flags is zero, we're supposed to simply
  55. ignore the entry -- a backwards incompatible change! */
  56. if (size > 20 && !(buf.ext_flags & 1))
  57. continue;
  58. *desc++ = buf.std;
  59. count++;
  60. } while (next && count < ARRAY_SIZE(boot_params.e820_map));
  61. return boot_params.e820_entries = count;
  62. }
  63. static int detect_memory_e801(void)
  64. {
  65. u16 ax, bx, cx, dx;
  66. u8 err;
  67. bx = cx = dx = 0;
  68. ax = 0xe801;
  69. asm("stc; int $0x15; setc %0"
  70. : "=m" (err), "+a" (ax), "+b" (bx), "+c" (cx), "+d" (dx));
  71. if (err)
  72. return -1;
  73. /* Do we really need to do this? */
  74. if (cx || dx) {
  75. ax = cx;
  76. bx = dx;
  77. }
  78. if (ax > 15*1024)
  79. return -1; /* Bogus! */
  80. /* This ignores memory above 16MB if we have a memory hole
  81. there. If someone actually finds a machine with a memory
  82. hole at 16MB and no support for 0E820h they should probably
  83. generate a fake e820 map. */
  84. boot_params.alt_mem_k = (ax == 15*1024) ? (dx << 6)+ax : ax;
  85. return 0;
  86. }
  87. static int detect_memory_88(void)
  88. {
  89. u16 ax;
  90. u8 err;
  91. ax = 0x8800;
  92. asm("stc; int $0x15; setc %0" : "=bcdm" (err), "+a" (ax));
  93. boot_params.screen_info.ext_mem_k = ax;
  94. return -err;
  95. }
  96. int detect_memory(void)
  97. {
  98. int err = -1;
  99. if (detect_memory_e820() > 0)
  100. err = 0;
  101. if (!detect_memory_e801())
  102. err = 0;
  103. if (!detect_memory_88())
  104. err = 0;
  105. return err;
  106. }