ACPIStaticParser.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  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. bool StaticParser::can_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. auto region = MM.allocate_kernel_region(m_fadt.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read);
  97. auto* fadt = (const Structures::FADT*)region->vaddr().offset(m_fadt.offset_in_page().get()).as_ptr();
  98. return fadt->h.revision >= 2;
  99. }
  100. void StaticParser::try_acpi_reboot()
  101. {
  102. // FIXME: Determine if we need to do MMIO/PCI/IO access to reboot, according to ACPI spec 6.2, Section 4.8.3.6
  103. #ifdef ACPI_DEBUG
  104. dbg() << "ACPI: Rebooting, Probing FADT (" << m_fadt << ")";
  105. #endif
  106. auto region = MM.allocate_kernel_region(m_fadt.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser", Region::Access::Read);
  107. auto* fadt = (const Structures::FADT*)region->vaddr().offset(m_fadt.offset_in_page().get()).as_ptr();
  108. if (fadt->h.revision >= 2) {
  109. klog() << "ACPI: Reboot, Sending value 0x" << String::format("%x", fadt->reset_value) << " to Port 0x" << String::format("%x", fadt->reset_reg.address);
  110. IO::out8(fadt->reset_reg.address, fadt->reset_value);
  111. }
  112. klog() << "ACPI: Reboot, Not supported!";
  113. }
  114. void StaticParser::try_acpi_shutdown()
  115. {
  116. klog() << "ACPI: Shutdown is not supported with the current configuration, Abort!";
  117. }
  118. size_t StaticParser::get_table_size(PhysicalAddress table_header)
  119. {
  120. InterruptDisabler disabler;
  121. #ifdef ACPI_DEBUG
  122. dbg() << "ACPI: Checking SDT Length";
  123. #endif
  124. auto region = MM.allocate_kernel_region(table_header.page_base(), (PAGE_SIZE * 2), "ACPI get_table_size()", Region::Access::Read);
  125. auto* sdt = (volatile Structures::SDTHeader*)region->vaddr().offset(table_header.offset_in_page().get()).as_ptr();
  126. return sdt->length;
  127. }
  128. u8 StaticParser::get_table_revision(PhysicalAddress table_header)
  129. {
  130. InterruptDisabler disabler;
  131. #ifdef ACPI_DEBUG
  132. dbg() << "ACPI: Checking SDT Revision";
  133. #endif
  134. auto region = MM.allocate_kernel_region(table_header.page_base(), (PAGE_SIZE * 2), "ACPI get_table_revision()", Region::Access::Read);
  135. auto* sdt = (volatile Structures::SDTHeader*)region->vaddr().offset(table_header.offset_in_page().get()).as_ptr();
  136. return sdt->revision;
  137. }
  138. void StaticParser::initialize_main_system_description_table()
  139. {
  140. #ifdef ACPI_DEBUG
  141. dbg() << "ACPI: Checking Main SDT Length to choose the correct mapping size";
  142. #endif
  143. ASSERT(!m_main_system_description_table.is_null());
  144. auto length = get_table_size(m_main_system_description_table);
  145. auto revision = get_table_revision(m_main_system_description_table);
  146. 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);
  147. auto* sdt = (volatile Structures::SDTHeader*)main_sdt_region->vaddr().offset(m_main_system_description_table.offset_in_page().get()).as_ptr();
  148. klog() << "ACPI: Main Description Table valid? " << StaticParsing::validate_table(const_cast<Structures::SDTHeader&>(*sdt), length);
  149. if (m_xsdt_supported) {
  150. volatile auto* xsdt = (volatile Structures::XSDT*)sdt;
  151. klog() << "ACPI: Using XSDT, Enumerating tables @ " << m_main_system_description_table;
  152. klog() << "ACPI: XSDT Revision " << revision << ", Total length - " << length;
  153. #ifdef ACPI_DEBUG
  154. dbg() << "ACPI: XSDT pointer @ V " << xsdt;
  155. #endif
  156. for (u32 i = 0; i < ((length - sizeof(Structures::SDTHeader)) / sizeof(u64)); i++) {
  157. #ifdef ACPI_DEBUG
  158. dbg() << "ACPI: Found new table [" << i << "], @ V 0x" << String::format("%x", &xsdt->table_ptrs[i]) << " - P 0x" << String::format("%x", xsdt->table_ptrs[i]);
  159. #endif
  160. m_sdt_pointers.append(PhysicalAddress(xsdt->table_ptrs[i]));
  161. }
  162. } else {
  163. volatile auto* rsdt = (volatile Structures::RSDT*)sdt;
  164. klog() << "ACPI: Using RSDT, Enumerating tables @ " << m_main_system_description_table;
  165. klog() << "ACPI: RSDT Revision " << revision << ", Total length - " << length;
  166. #ifdef ACPI_DEBUG
  167. dbg() << "ACPI: RSDT pointer @ V " << rsdt;
  168. #endif
  169. for (u32 i = 0; i < ((length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
  170. #ifdef ACPI_DEBUG
  171. dbg() << "ACPI: Found new table [" << i << "], @ V 0x" << String::format("%x", &rsdt->table_ptrs[i]) << " - P 0x" << String::format("%x", rsdt->table_ptrs[i]);
  172. #endif
  173. m_sdt_pointers.append(PhysicalAddress(rsdt->table_ptrs[i]));
  174. }
  175. }
  176. }
  177. void StaticParser::locate_main_system_description_table()
  178. {
  179. auto rsdp_region = MM.allocate_kernel_region(m_rsdp.page_base(), (PAGE_SIZE * 2), "ACPI Static Parser Initialization", Region::Access::Read, false, true);
  180. volatile auto* rsdp = (Structures::RSDPDescriptor20*)rsdp_region->vaddr().offset(m_rsdp.offset_in_page().get()).as_ptr();
  181. if (rsdp->base.revision == 0) {
  182. m_xsdt_supported = false;
  183. } else if (rsdp->base.revision >= 2) {
  184. if (rsdp->xsdt_ptr != (u64) nullptr) {
  185. m_xsdt_supported = true;
  186. } else {
  187. m_xsdt_supported = false;
  188. }
  189. }
  190. if (!m_xsdt_supported) {
  191. m_main_system_description_table = PhysicalAddress(rsdp->base.rsdt_ptr);
  192. } else {
  193. m_main_system_description_table = PhysicalAddress(rsdp->xsdt_ptr);
  194. }
  195. }
  196. StaticParser::StaticParser()
  197. : Parser(true)
  198. , m_rsdp(StaticParsing::search_rsdp())
  199. {
  200. if (!m_rsdp.is_null()) {
  201. klog() << "ACPI: Using RSDP @ " << m_rsdp;
  202. m_operable = true;
  203. locate_static_data();
  204. } else {
  205. m_operable = false;
  206. klog() << "ACPI: Disabled, due to RSDP being absent";
  207. }
  208. }
  209. StaticParser::StaticParser(PhysicalAddress rsdp)
  210. : Parser(true)
  211. , m_rsdp(rsdp)
  212. {
  213. klog() << "ACPI: Using RSDP @ " << rsdp;
  214. m_operable = true;
  215. locate_static_data();
  216. }
  217. PhysicalAddress StaticParsing::search_rsdp_in_ebda(u16 ebda_segment)
  218. {
  219. 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);
  220. char* p_rsdp_str = (char*)(PhysicalAddress(ebda_segment << 4).as_ptr());
  221. 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) {
  222. #ifdef ACPI_DEBUG
  223. dbg() << "ACPI: Looking for RSDP in EBDA @ V " << (void*)rsdp_str << ", P " << (void*)p_rsdp_str;
  224. #endif
  225. if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
  226. return PhysicalAddress((FlatPtr)p_rsdp_str);
  227. p_rsdp_str += 16;
  228. }
  229. return {};
  230. }
  231. PhysicalAddress StaticParsing::search_rsdp_in_bios_area()
  232. {
  233. 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);
  234. char* p_rsdp_str = (char*)(PhysicalAddress(0xE0000).as_ptr());
  235. 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) {
  236. #ifdef ACPI_DEBUG
  237. dbg() << "ACPI: Looking for RSDP in BIOS ROM area @ V " << (void*)rsdp_str << ", P " << (void*)p_rsdp_str;
  238. #endif
  239. if (!strncmp("RSD PTR ", rsdp_str, strlen("RSD PTR ")))
  240. return PhysicalAddress((FlatPtr)p_rsdp_str);
  241. p_rsdp_str += 16;
  242. }
  243. return {};
  244. }
  245. inline bool StaticParsing::validate_table(Structures::SDTHeader& v_header, size_t length)
  246. {
  247. u8 checksum = 0;
  248. auto* sdt = (u8*)&v_header;
  249. for (size_t i = 0; i < length; i++)
  250. checksum += sdt[i];
  251. if (checksum == 0)
  252. return true;
  253. return false;
  254. }
  255. PhysicalAddress StaticParsing::search_rsdp()
  256. {
  257. PhysicalAddress rsdp;
  258. auto region = MM.allocate_kernel_region(PhysicalAddress(0), PAGE_SIZE, "ACPI RSDP Searching", Region::Access::Read);
  259. u16 ebda_seg = (u16) * ((uint16_t*)((region->vaddr().get() & PAGE_MASK) + 0x40e));
  260. klog() << "ACPI: Probing EBDA, Segment 0x" << String::format("%x", ebda_seg);
  261. rsdp = search_rsdp_in_ebda(ebda_seg);
  262. if (!rsdp.is_null())
  263. return rsdp;
  264. return search_rsdp_in_bios_area();
  265. }
  266. PhysicalAddress StaticParsing::search_table(PhysicalAddress rsdp, const char* signature)
  267. {
  268. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  269. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  270. ASSERT(strlen(signature) == 4);
  271. auto rsdp_region = MM.allocate_kernel_region(rsdp.page_base(), (PAGE_SIZE * 2), "ACPI Static Parsing search_table()", Region::Access::Read, false, true);
  272. volatile auto* rsdp_ptr = (Structures::RSDPDescriptor20*)rsdp_region->vaddr().offset(rsdp.offset_in_page().get()).as_ptr();
  273. if (rsdp_ptr->base.revision == 0) {
  274. return search_table_in_rsdt(PhysicalAddress(rsdp_ptr->base.rsdt_ptr), signature);
  275. }
  276. if (rsdp_ptr->base.revision >= 2) {
  277. if (rsdp_ptr->xsdt_ptr != (u64) nullptr)
  278. return search_table_in_xsdt(PhysicalAddress(rsdp_ptr->xsdt_ptr), signature);
  279. return search_table_in_rsdt(PhysicalAddress(rsdp_ptr->base.rsdt_ptr), signature);
  280. }
  281. ASSERT_NOT_REACHED();
  282. }
  283. PhysicalAddress StaticParsing::search_table_in_xsdt(PhysicalAddress xsdt, const char* signature)
  284. {
  285. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  286. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  287. ASSERT(strlen(signature) == 4);
  288. 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);
  289. auto* xsdt_ptr = (volatile Structures::XSDT*)main_sdt_region->vaddr().offset(xsdt.offset_in_page().get()).as_ptr();
  290. for (u32 i = 0; i < ((xsdt_ptr->h.length - sizeof(Structures::SDTHeader)) / sizeof(u64)); i++) {
  291. if (match_table_signature(PhysicalAddress((FlatPtr)xsdt_ptr->table_ptrs[i]), signature))
  292. return PhysicalAddress((FlatPtr)xsdt_ptr->table_ptrs[i]);
  293. }
  294. return {};
  295. }
  296. bool StaticParsing::match_table_signature(PhysicalAddress table_header, const char* signature)
  297. {
  298. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  299. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  300. ASSERT(strlen(signature) == 4);
  301. 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);
  302. auto* table_ptr = (volatile Structures::RSDT*)main_sdt_region->vaddr().offset(table_header.offset_in_page().get()).as_ptr();
  303. return !strncmp(const_cast<const char*>(table_ptr->h.sig), signature, 4);
  304. }
  305. PhysicalAddress StaticParsing::search_table_in_rsdt(PhysicalAddress rsdt, const char* signature)
  306. {
  307. // FIXME: There's no validation of ACPI tables here. Use the checksum to validate the tables.
  308. // FIXME: Don't blindly use PAGE_SIZE here, but probe the actual length.
  309. ASSERT(strlen(signature) == 4);
  310. 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);
  311. auto* rsdt_ptr = (volatile Structures::RSDT*)main_sdt_region->vaddr().offset(rsdt.offset_in_page().get()).as_ptr();
  312. for (u32 i = 0; i < ((rsdt_ptr->h.length - sizeof(Structures::SDTHeader)) / sizeof(u32)); i++) {
  313. if (match_table_signature(PhysicalAddress((FlatPtr)rsdt_ptr->table_ptrs[i]), signature))
  314. return PhysicalAddress((FlatPtr)rsdt_ptr->table_ptrs[i]);
  315. }
  316. return {};
  317. }
  318. }
  319. }