memory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* -*- linux-c -*- ------------------------------------------------------- *
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright 2007 rPath, Inc. - All Rights Reserved
  6. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  7. *
  8. * ----------------------------------------------------------------------- */
  9. /*
  10. * Memory detection code
  11. */
  12. #include "boot.h"
  13. #define SMAP 0x534d4150 /* ASCII "SMAP" */
  14. static void detect_memory_e820(void)
  15. {
  16. int count = 0;
  17. struct biosregs ireg, oreg;
  18. struct boot_e820_entry *desc = boot_params.e820_table;
  19. static struct boot_e820_entry buf; /* static so it is zeroed */
  20. initregs(&ireg);
  21. ireg.ax = 0xe820;
  22. ireg.cx = sizeof(buf);
  23. ireg.edx = SMAP;
  24. ireg.di = (size_t)&buf;
  25. /*
  26. * Note: at least one BIOS is known which assumes that the
  27. * buffer pointed to by one e820 call is the same one as
  28. * the previous call, and only changes modified fields. Therefore,
  29. * we use a temporary buffer and copy the results entry by entry.
  30. *
  31. * This routine deliberately does not try to account for
  32. * ACPI 3+ extended attributes. This is because there are
  33. * BIOSes in the field which report zero for the valid bit for
  34. * all ranges, and we don't currently make any use of the
  35. * other attribute bits. Revisit this if we see the extended
  36. * attribute bits deployed in a meaningful way in the future.
  37. */
  38. do {
  39. intcall(0x15, &ireg, &oreg);
  40. ireg.ebx = oreg.ebx; /* for next iteration... */
  41. /* BIOSes which terminate the chain with CF = 1 as opposed
  42. to %ebx = 0 don't always report the SMAP signature on
  43. the final, failing, probe. */
  44. if (oreg.eflags & X86_EFLAGS_CF)
  45. break;
  46. /* Some BIOSes stop returning SMAP in the middle of
  47. the search loop. We don't know exactly how the BIOS
  48. screwed up the map at that point, we might have a
  49. partial map, the full map, or complete garbage, so
  50. just return failure. */
  51. if (oreg.eax != SMAP) {
  52. count = 0;
  53. break;
  54. }
  55. *desc++ = buf;
  56. count++;
  57. } while (ireg.ebx && count < ARRAY_SIZE(boot_params.e820_table));
  58. boot_params.e820_entries = count;
  59. }
  60. static void detect_memory_e801(void)
  61. {
  62. struct biosregs ireg, oreg;
  63. initregs(&ireg);
  64. ireg.ax = 0xe801;
  65. intcall(0x15, &ireg, &oreg);
  66. if (oreg.eflags & X86_EFLAGS_CF)
  67. return;
  68. /* Do we really need to do this? */
  69. if (oreg.cx || oreg.dx) {
  70. oreg.ax = oreg.cx;
  71. oreg.bx = oreg.dx;
  72. }
  73. if (oreg.ax > 15*1024) {
  74. return; /* Bogus! */
  75. } else if (oreg.ax == 15*1024) {
  76. boot_params.alt_mem_k = (oreg.bx << 6) + oreg.ax;
  77. } else {
  78. /*
  79. * This ignores memory above 16MB if we have a memory
  80. * hole there. If someone actually finds a machine
  81. * with a memory hole at 16MB and no support for
  82. * 0E820h they should probably generate a fake e820
  83. * map.
  84. */
  85. boot_params.alt_mem_k = oreg.ax;
  86. }
  87. }
  88. static void detect_memory_88(void)
  89. {
  90. struct biosregs ireg, oreg;
  91. initregs(&ireg);
  92. ireg.ah = 0x88;
  93. intcall(0x15, &ireg, &oreg);
  94. boot_params.screen_info.ext_mem_k = oreg.ax;
  95. }
  96. void detect_memory(void)
  97. {
  98. detect_memory_e820();
  99. detect_memory_e801();
  100. detect_memory_88();
  101. }