ACPIStaticParser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/ACPI/ACPIStaticParser.h>
  27. #include <Kernel/VM/MemoryManager.h>
  28. #include <LibBareMetal/IO.h>
  29. #include <LibBareMetal/StdLib.h>
  30. //#define ACPI_DEBUG
  31. namespace Kernel {
  32. namespace ACPI {
  33. void StaticParser::initialize(PhysicalAddress rsdp)
  34. {
  35. if (!Parser::is_initialized()) {
  36. new StaticParser(rsdp);
  37. }
  38. }
  39. void StaticParser::initialize_without_rsdp()
  40. {
  41. if (!Parser::is_initialized()) {
  42. new StaticParser();
  43. }
  44. }
  45. bool StaticParser::is_initialized()
  46. {
  47. return Parser::is_initialized();
  48. }
  49. void StaticParser::locate_static_data()
  50. {
  51. locate_main_system_description_table();
  52. initialize_main_system_description_table();
  53. init_fadt();
  54. init_facs();
  55. }
  56. PhysicalAddress StaticParser::find_table(const char* sig)
  57. {
  58. #ifdef ACPI_DEBUG
  59. dbg() << "ACPI: Calling Find Table method!";
  60. #endif
  61. for (auto p_sdt : m_sdt_pointers) {
  62. auto region = MM.allocate_kernel_region(p_sdt.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser Tables Finding", Region::Access::Read);
  63. auto* sdt = (const Structures::SDTHeader*)region->vaddr().offset(p_sdt.offset_in_page().get()).as_ptr();
  64. #ifdef ACPI_DEBUG
  65. dbg() << "ACPI: Examining Table @ P " << physical_sdt_ptr;
  66. #endif
  67. if (!strncmp(sdt->sig, sig, 4)) {
  68. #ifdef ACPI_DEBUG
  69. dbg() << "ACPI: Found Table @ P " << physical_sdt_ptr;
  70. #endif
  71. return p_sdt;
  72. }
  73. }
  74. return {};
  75. }
  76. void StaticParser::init_facs()
  77. {
  78. m_facs = find_table("FACS");
  79. }
  80. void StaticParser::init_fadt()
  81. {
  82. klog() << "ACPI: Initializing Fixed ACPI data";
  83. klog() << "ACPI: Searching for the Fixed ACPI Data Table";
  84. m_fadt = find_table("FACP");
  85. ASSERT(!m_fadt.is_null());
  86. auto checkup_region = MM.allocate_kernel_region(m_fadt.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read);
  87. auto* sdt = (const Structures::SDTHeader*)checkup_region->vaddr().offset(m_fadt.offset_in_page().get()).as_ptr();
  88. #ifdef ACPI_DEBUG
  89. dbg() << "ACPI: FADT @ V " << sdt << ", P " << (void*)fadt.as_ptr();
  90. #endif
  91. klog() << "ACPI: Fixed ACPI data, Revision " << sdt->revision << ", Length " << sdt->length << " bytes";
  92. }
  93. void StaticParser::do_acpi_reboot()
  94. {
  95. // FIXME: Determine if we need to do MMIO/PCI/IO access to reboot, according to ACPI spec 6.2, Section 4.8.3.6
  96. #ifdef ACPI_DEBUG
  97. dbg() << "ACPI: Rebooting, Probing FADT (P @ " << m_fadt.ptr() << ")";
  98. #endif
  99. auto region = MM.allocate_kernel_region(m_fadt.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read);
  100. auto* fadt = (const Structures::FADT*)region->vaddr().offset(m_fadt.offset_in_page().get()).as_ptr();
  101. if (fadt->h.revision >= 2) {
  102. klog() << "ACPI: Reboot, Sending value 0x" << String::format("%x", fadt->reset_value) << " to Port 0x" << String::format("%x", fadt->reset_reg.address);
  103. IO::out8(fadt->reset_reg.address, fadt->reset_value);
  104. } else {
  105. klog() << "ACPI: Reboot, Not supported!";
  106. }
  107. ASSERT_NOT_REACHED(); /// If rebooting didn't work, halt.
  108. }
  109. void StaticParser::do_acpi_shutdown()
  110. {
  111. klog() << "ACPI: Shutdown is not supported with the current configuration, Abort!";
  112. ASSERT_NOT_REACHED();
  113. }
  114. size_t StaticParser::get_table_size(PhysicalAddress table_header)
  115. {
  116. InterruptDisabler disabler;
  117. #ifdef ACPI_DEBUG
  118. dbg() << "ACPI: Checking SDT Length";
  119. #endif
  120. auto region = MM.allocate_kernel_region(table_header.page_base(), (PAGE_SIZE * 2), "ACPI get_table_size()", Region::Access::Read);
  121. auto* sdt = (volatile Structures::SDTHeader*)region->vaddr().offset(table_header.offset_in_page().get()).as_ptr();
  122. return sdt->length;
  123. }
  124. u8 StaticParser::get_table_revision(PhysicalAddress table_header)
  125. {
  126. InterruptDisabler disabler;
  127. #ifdef ACPI_DEBUG
  128. dbg() << "ACPI: Checking SDT Revision";
  129. #endif
  130. auto region = MM.allocate_kernel_region(table_header.page_base(), (PAGE_SIZE * 2), "ACPI get_table_revision()", Region::Access::Read);
  131. auto* sdt = (volatile Structures::SDTHeader*)region->vaddr().offset(table_header.offset_in_page().get()).as_ptr();
  132. return sdt->revision;
  133. }
  134. void StaticParser::initialize_main_system_description_table()
  135. {
  136. #ifdef ACPI_DEBUG
  137. dbg() << "ACPI: Checking Main SDT Length to choose the correct mapping size";
  138. #endif
  139. ASSERT(!m_main_system_description_table.is_null());
  140. auto length = get_table_size(m_main_system_description_table);
  141. auto revision = get_table_revision(m_main_system_description_table);
  142. auto main_sdt_region = MM.allocate_kernel_region(m_main_system_description_table.page_base(), PAGE_ROUND_UP(length) + PAGE_SIZE, "ACPI Static Parser Initialization", Region::Access::Read, false, true);
  143. auto* sdt = (volatile Structures::SDTHeader*)main_sdt_region->vaddr().offset(m_main_system_description_table.offset_in_page().get()).as_ptr();
  144. klog() << "ACPI: Main Description Table valid? " << StaticParsing::validate_table(const_cast<Structures::SDTHeader&>(*sdt), length);
  145. if (m_xsdt_supported) {
  146. volatile auto* xsdt = (volatile Structures::XSDT*)sdt;
  147. klog() << "ACPI: Using XSDT, Enumerating tables @ P " << String::format("%p", m_main_system_description_table.get());
  148. klog() << "ACPI: XSDT Revision " << revision << ", Total length - " << length;
  149. #ifdef ACPI_DEBUG
  150. dbg() << "ACPI: XSDT pointer @ V " << xsdt;
  151. #endif
  152. for (u32 i = 0; i < ((length - sizeof(Structures::SDTHeader)) / sizeof(u64)); i++) {
  153. #ifdef ACPI_DEBUG
  154. dbg() << "ACPI: Found new table [" << i << "], @ V 0x" << String::format("%x", &xsdt->table_ptrs[i]) << " - P 0x" << String::format("%x", xsdt->table_ptrs[i]);
  155. #endif
  156. m_sdt_pointers.append(PhysicalAddress(xsdt->table_ptrs[i]));
  157. }
  158. } else {
  159. volatile auto* rsdt = (volatile Structures::RSDT*)sdt;
  160. klog() << "ACPI: Using RSDT, Enumerating tables @ P " << String::format("%p", m_main_system_description_table.get());
  161. klog() << "ACPI: RSDT Revision " << revision << ", Total length - " << length;
  162. #ifdef ACPI_DEBUG
  163. dbg() << "ACPI: RSDT pointer @ V " << rsdt;
  164. #endif
  165. for (u32 i = 0; i < ((length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
  166. #ifdef ACPI_DEBUG
  167. dbg() << "ACPI: Found new table [" << i << "], @ V 0x" << String::format("%x", &rsdt->table_ptrs[i]) << " - P 0x" << String::format("%x", rsdt->table_ptrs[i]);
  168. #endif
  169. m_sdt_pointers.append(PhysicalAddress(rsdt->table_ptrs[i]));
  170. }
  171. }
  172. }
  173. void StaticParser::locate_main_system_description_table()
  174. {
  175. auto rsdp_region = MM.allocate_kernel_region(m_rsdp.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser Initialization", Region::Access::Read, false, true);
  176. volatile auto* rsdp = (Structures::RSDPDescriptor20*)rsdp_region->vaddr().offset(m_rsdp.offset_in_page().get()).as_ptr();
  177. if (rsdp->base.revision == 0) {
  178. m_xsdt_supported = false;
  179. } else if (rsdp->base.revision >= 2) {
  180. if (rsdp->xsdt_ptr != (u64) nullptr) {
  181. m_xsdt_supported = true;
  182. } else {
  183. m_xsdt_supported = false;
  184. }
  185. }
  186. if (!m_xsdt_supported) {
  187. m_main_system_description_table = PhysicalAddress(rsdp->base.rsdt_ptr);
  188. } else {
  189. m_main_system_description_table = PhysicalAddress(rsdp->xsdt_ptr);
  190. }
  191. }
  192. StaticParser::StaticParser()
  193. : Parser(true)
  194. , m_rsdp(StaticParsing::search_rsdp())
  195. {
  196. if (!m_rsdp.is_null()) {
  197. klog() << "ACPI: Using RSDP @ P " << String::format("%p", m_rsdp);
  198. m_operable = true;
  199. locate_static_data();
  200. } else {
  201. m_operable = false;
  202. klog() << "ACPI: Disabled, due to RSDP being absent";
  203. }
  204. }
  205. StaticParser::StaticParser(PhysicalAddress rsdp)
  206. : Parser(true)
  207. , m_rsdp(rsdp)
  208. {
  209. klog() << "ACPI: Using RSDP @ P " << String::format("%p", rsdp.get());
  210. m_operable = true;
  211. locate_static_data();
  212. }
  213. PhysicalAddress StaticParsing::search_rsdp_in_ebda(u16 ebda_segment)
  214. {
  215. auto rsdp_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)(ebda_segment << 4))), PAGE_ROUND_UP(1024), "ACPI Static Parser RSDP Finding #1", Region::Access::Read, false, true);
  216. char* p_rsdp_str = (char*)(PhysicalAddress(ebda_segment << 4).as_ptr());
  217. for (char* rsdp_str = (char*)rsdp_region->vaddr().offset(offset_in_page((u32)(ebda_segment << 4))).as_ptr(); rsdp_str < (char*)(rsdp_region->vaddr().offset(offset_in_page((u32)(ebda_segment << 4))).get() + 1024); rsdp_str += 16) {
  218. #ifdef ACPI_DEBUG
  219. dbg() << "ACPI: Looking for RSDP in EBDA @ V " << (void*)rsdp_str << ", P " << (void*)p_rsdp_str;
  220. #endif
  221. if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
  222. return PhysicalAddress((uintptr_t)p_rsdp_str);
  223. p_rsdp_str += 16;
  224. }
  225. return {};
  226. }
  227. PhysicalAddress StaticParsing::search_rsdp_in_bios_area()
  228. {
  229. auto rsdp_region = MM.allocate_kernel_region(PhysicalAddress(0xE0000), PAGE_ROUND_UP(0xFFFFF - 0xE0000), "ACPI Static Parser RSDP Finding #2", Region::Access::Read, false, true);
  230. char* p_rsdp_str = (char*)(PhysicalAddress(0xE0000).as_ptr());
  231. for (char* rsdp_str = (char*)rsdp_region->vaddr().offset(offset_in_page((u32)(0xE0000))).as_ptr(); rsdp_str < (char*)(rsdp_region->vaddr().offset(offset_in_page((u32)(0xE0000))).get() + (0xFFFFF - 0xE0000)); rsdp_str += 16) {
  232. #ifdef ACPI_DEBUG
  233. dbg() << "ACPI: Looking for RSDP in BIOS ROM area @ V " << (void*)rsdp_str << ", P " << (void*)p_rsdp_str;
  234. #endif
  235. if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
  236. return PhysicalAddress((uintptr_t)p_rsdp_str);
  237. p_rsdp_str += 16;
  238. }
  239. return {};
  240. }
  241. inline bool StaticParsing::validate_table(Structures::SDTHeader& v_header, size_t length)
  242. {
  243. u8 checksum = 0;
  244. auto* sdt = (u8*)&v_header;
  245. for (size_t i = 0; i < length; i++)
  246. checksum += sdt[i];
  247. if (checksum == 0)
  248. return true;
  249. return false;
  250. }
  251. PhysicalAddress StaticParsing::search_rsdp()
  252. {
  253. PhysicalAddress rsdp;
  254. auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "ACPI RSDP Searching", Region::Access::Read);
  255. u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
  256. klog() << "ACPI: Probing EBDA, Segment 0x" << String::format("%x", ebda_seg);
  257. rsdp = search_rsdp_in_ebda(ebda_seg);
  258. if (!rsdp.is_null())
  259. return rsdp;
  260. return search_rsdp_in_bios_area();
  261. }
  262. PhysicalAddress StaticParsing::search_table(PhysicalAddress rsdp, const char* signature)
  263. {
  264. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  265. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  266. ASSERT(strlen(signature) == 4);
  267. auto rsdp_region = MM.allocate_kernel_region(rsdp.page_base(), (PAGE_SIZE * 2), "ACPI Static Parsing search_table()", Region::Access::Read, false, true);
  268. volatile auto* rsdp_ptr = (Structures::RSDPDescriptor20*)rsdp_region->vaddr().offset(rsdp.offset_in_page().get()).as_ptr();
  269. if (rsdp_ptr->base.revision == 0) {
  270. return search_table_in_rsdt(PhysicalAddress(rsdp_ptr->base.rsdt_ptr), signature);
  271. }
  272. if (rsdp_ptr->base.revision >= 2) {
  273. if (rsdp_ptr->xsdt_ptr != (u64) nullptr)
  274. return search_table_in_xsdt(PhysicalAddress(rsdp_ptr->xsdt_ptr), signature);
  275. return search_table_in_rsdt(PhysicalAddress(rsdp_ptr->base.rsdt_ptr), signature);
  276. }
  277. ASSERT_NOT_REACHED();
  278. }
  279. PhysicalAddress StaticParsing::search_table_in_xsdt(PhysicalAddress xsdt, const char* signature)
  280. {
  281. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  282. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  283. ASSERT(strlen(signature) == 4);
  284. auto main_sdt_region = MM.allocate_kernel_region(xsdt.page_base(), PAGE_SIZE, "ACPI Static Parsing search_table_in_xsdt()", Region::Access::Read, false, true);
  285. auto* xsdt_ptr = (volatile Structures::XSDT*)main_sdt_region->vaddr().offset(xsdt.offset_in_page().get()).as_ptr();
  286. for (u32 i = 0; i < ((xsdt_ptr->h.length - sizeof(Structures::SDTHeader)) / sizeof(u64)); i++) {
  287. if (match_table_signature(PhysicalAddress((uintptr_t)xsdt_ptr->table_ptrs[i]), signature))
  288. return PhysicalAddress((uintptr_t)xsdt_ptr->table_ptrs[i]);
  289. }
  290. return {};
  291. }
  292. bool StaticParsing::match_table_signature(PhysicalAddress table_header, const char* signature)
  293. {
  294. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  295. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  296. ASSERT(strlen(signature) == 4);
  297. auto main_sdt_region = MM.allocate_kernel_region(table_header.page_base(), PAGE_SIZE, "ACPI Static Parsing match_table_signature()", Region::Access::Read, false, true);
  298. auto* table_ptr = (volatile Structures::RSDT*)main_sdt_region->vaddr().offset(table_header.offset_in_page().get()).as_ptr();
  299. return !strncmp(const_cast<const char*>(table_ptr->h.sig), signature, 4);
  300. }
  301. PhysicalAddress StaticParsing::search_table_in_rsdt(PhysicalAddress rsdt, const char* signature)
  302. {
  303. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  304. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  305. ASSERT(strlen(signature) == 4);
  306. auto main_sdt_region = MM.allocate_kernel_region(rsdt.page_base(), PAGE_SIZE, "ACPI Static Parsing search_table_in_rsdt()", Region::Access::Read, false, true);
  307. auto* rsdt_ptr = (volatile Structures::RSDT*)main_sdt_region->vaddr().offset(rsdt.offset_in_page().get()).as_ptr();
  308. for (u32 i = 0; i < ((rsdt_ptr->h.length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
  309. if (match_table_signature(PhysicalAddress((uintptr_t)rsdt_ptr->table_ptrs[i]), signature))
  310. return PhysicalAddress((uintptr_t)rsdt_ptr->table_ptrs[i]);
  311. }
  312. return {};
  313. }
  314. }
  315. }