From cbc51f523ec8c8b31c87798b735eefcdcc557d03 Mon Sep 17 00:00:00 2001 From: bdbrd Date: Wed, 22 Oct 2025 15:51:24 +0200 Subject: [PATCH] Initial commit --- LICENSE | 674 +++ Makefile | 102 + README.md | 21 + bochsrc | 5 + bx_enh_dbg.ini | 26 + compile_flags.txt | 4 + limine.conf | 20 + linker.ld | 69 + src/drivers/ahci.c | 75 + src/drivers/ahci.h | 1 + src/drivers/pmt.c | 79 + src/drivers/pmt.h | 3 + src/drivers/rtc.c | 0 src/drivers/serial.c | 76 + src/drivers/serial.h | 8 + src/elf/elf.c | 24 + src/elf/elf.h | 43 + src/elf/elftest.c | 5 + src/hal/apic.c | 137 + src/hal/apic.h | 4 + src/hal/gdt.asm | 34 + src/hal/gdt.c | 33 + src/hal/gdt.h | 17 + src/hal/idt.asm | 330 ++ src/hal/idt.c | 230 + src/hal/idt.h | 37 + src/hal/ioapic.c | 62 + src/hal/ioapic.h | 13 + src/hal/smp.c | 112 + src/hal/smp.h | 23 + src/hal/timer.c | 24 + src/hal/timer.h | 11 + src/hal/tsc.c | 77 + src/hal/tsc.h | 6 + src/include/SFB25.h | 18 + src/include/error.h | 14 + src/include/io.h | 12 + src/include/limine.h | 621 +++ src/include/lock.h | 9 + src/include/stdio.h | 41 + src/include/string.h | 16 + src/include/uacpi/acpi.h | 1430 +++++ src/include/uacpi/context.h | 45 + src/include/uacpi/event.h | 282 + src/include/uacpi/helpers.h | 16 + src/include/uacpi/internal/compiler.h | 3 + src/include/uacpi/internal/context.h | 143 + src/include/uacpi/internal/dynamic_array.h | 147 + src/include/uacpi/internal/event.h | 25 + src/include/uacpi/internal/helpers.h | 7 + src/include/uacpi/internal/interpreter.h | 20 + src/include/uacpi/internal/io.h | 31 + src/include/uacpi/internal/log.h | 22 + src/include/uacpi/internal/mutex.h | 78 + src/include/uacpi/internal/namespace.h | 119 + src/include/uacpi/internal/notify.h | 9 + src/include/uacpi/internal/opcodes.h | 1329 +++++ src/include/uacpi/internal/opregion.h | 42 + src/include/uacpi/internal/osi.h | 8 + src/include/uacpi/internal/registers.h | 54 + src/include/uacpi/internal/resources.h | 323 ++ src/include/uacpi/internal/shareable.h | 21 + src/include/uacpi/internal/stdlib.h | 87 + src/include/uacpi/internal/tables.h | 66 + src/include/uacpi/internal/types.h | 311 ++ src/include/uacpi/internal/utilities.h | 45 + src/include/uacpi/io.h | 15 + src/include/uacpi/kernel_api.h | 289 + src/include/uacpi/namespace.h | 136 + src/include/uacpi/notify.h | 26 + src/include/uacpi/opregion.h | 43 + src/include/uacpi/osi.h | 121 + src/include/uacpi/platform/arch_helpers.h | 38 + src/include/uacpi/platform/atomic.h | 129 + src/include/uacpi/platform/compiler.h | 82 + src/include/uacpi/platform/config.h | 130 + src/include/uacpi/platform/libc.h | 25 + src/include/uacpi/platform/types.h | 64 + src/include/uacpi/resources.h | 674 +++ src/include/uacpi/sleep.h | 63 + src/include/uacpi/status.h | 57 + src/include/uacpi/tables.h | 130 + src/include/uacpi/types.h | 458 ++ src/include/uacpi/uacpi.h | 258 + src/include/uacpi/utilities.h | 184 + src/lib/io.c | 63 + src/lib/spinlock.c | 15 + src/lib/stdio.c | 468 ++ src/lib/string.c | 82 + src/main.c | 136 + src/mm/kmalloc.c | 167 + src/mm/kmalloc.h | 10 + src/mm/pmm.c | 110 + src/mm/pmm.h | 14 + src/mm/vmm.c | 306 + src/mm/vmm.h | 23 + src/scheduler/sched.asm | 47 + src/scheduler/sched.c | 123 + src/scheduler/sched.h | 30 + src/sys/acpi.c | 121 + src/sys/acpi.h | 196 + src/sys/pci.c | 251 + src/sys/pci.h | 102 + src/sys/time.c | 16 + src/uacpi/default_handlers.c | 353 ++ src/uacpi/event.c | 2424 ++++++++ src/uacpi/files.cmake | 21 + src/uacpi/interpreter.c | 5822 ++++++++++++++++++++ src/uacpi/io.c | 672 +++ src/uacpi/mutex.c | 392 ++ src/uacpi/namespace.c | 1020 ++++ src/uacpi/notify.c | 251 + src/uacpi/opcodes.c | 260 + src/uacpi/opregion.c | 881 +++ src/uacpi/osi.c | 384 ++ src/uacpi/registers.c | 439 ++ src/uacpi/resources.c | 2500 +++++++++ src/uacpi/shareable.c | 66 + src/uacpi/sleep.c | 606 ++ src/uacpi/stdlib.c | 711 +++ src/uacpi/tables.c | 1350 +++++ src/uacpi/types.c | 1460 +++++ src/uacpi/uacpi.c | 944 ++++ src/uacpi/utilities.c | 1139 ++++ src/uacpi_kernel_api.c | 141 + 125 files changed, 34817 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 bochsrc create mode 100644 bx_enh_dbg.ini create mode 100644 compile_flags.txt create mode 100644 limine.conf create mode 100644 linker.ld create mode 100644 src/drivers/ahci.c create mode 100644 src/drivers/ahci.h create mode 100644 src/drivers/pmt.c create mode 100644 src/drivers/pmt.h create mode 100644 src/drivers/rtc.c create mode 100644 src/drivers/serial.c create mode 100644 src/drivers/serial.h create mode 100644 src/elf/elf.c create mode 100644 src/elf/elf.h create mode 100644 src/elf/elftest.c create mode 100644 src/hal/apic.c create mode 100644 src/hal/apic.h create mode 100644 src/hal/gdt.asm create mode 100644 src/hal/gdt.c create mode 100644 src/hal/gdt.h create mode 100644 src/hal/idt.asm create mode 100644 src/hal/idt.c create mode 100644 src/hal/idt.h create mode 100644 src/hal/ioapic.c create mode 100644 src/hal/ioapic.h create mode 100644 src/hal/smp.c create mode 100644 src/hal/smp.h create mode 100644 src/hal/timer.c create mode 100644 src/hal/timer.h create mode 100644 src/hal/tsc.c create mode 100644 src/hal/tsc.h create mode 100644 src/include/SFB25.h create mode 100644 src/include/error.h create mode 100644 src/include/io.h create mode 100644 src/include/limine.h create mode 100644 src/include/lock.h create mode 100644 src/include/stdio.h create mode 100644 src/include/string.h create mode 100644 src/include/uacpi/acpi.h create mode 100644 src/include/uacpi/context.h create mode 100644 src/include/uacpi/event.h create mode 100644 src/include/uacpi/helpers.h create mode 100644 src/include/uacpi/internal/compiler.h create mode 100644 src/include/uacpi/internal/context.h create mode 100644 src/include/uacpi/internal/dynamic_array.h create mode 100644 src/include/uacpi/internal/event.h create mode 100644 src/include/uacpi/internal/helpers.h create mode 100644 src/include/uacpi/internal/interpreter.h create mode 100644 src/include/uacpi/internal/io.h create mode 100644 src/include/uacpi/internal/log.h create mode 100644 src/include/uacpi/internal/mutex.h create mode 100644 src/include/uacpi/internal/namespace.h create mode 100644 src/include/uacpi/internal/notify.h create mode 100644 src/include/uacpi/internal/opcodes.h create mode 100644 src/include/uacpi/internal/opregion.h create mode 100644 src/include/uacpi/internal/osi.h create mode 100644 src/include/uacpi/internal/registers.h create mode 100644 src/include/uacpi/internal/resources.h create mode 100644 src/include/uacpi/internal/shareable.h create mode 100644 src/include/uacpi/internal/stdlib.h create mode 100644 src/include/uacpi/internal/tables.h create mode 100644 src/include/uacpi/internal/types.h create mode 100644 src/include/uacpi/internal/utilities.h create mode 100644 src/include/uacpi/io.h create mode 100644 src/include/uacpi/kernel_api.h create mode 100644 src/include/uacpi/namespace.h create mode 100644 src/include/uacpi/notify.h create mode 100644 src/include/uacpi/opregion.h create mode 100644 src/include/uacpi/osi.h create mode 100644 src/include/uacpi/platform/arch_helpers.h create mode 100644 src/include/uacpi/platform/atomic.h create mode 100644 src/include/uacpi/platform/compiler.h create mode 100644 src/include/uacpi/platform/config.h create mode 100644 src/include/uacpi/platform/libc.h create mode 100644 src/include/uacpi/platform/types.h create mode 100644 src/include/uacpi/resources.h create mode 100644 src/include/uacpi/sleep.h create mode 100644 src/include/uacpi/status.h create mode 100644 src/include/uacpi/tables.h create mode 100644 src/include/uacpi/types.h create mode 100644 src/include/uacpi/uacpi.h create mode 100644 src/include/uacpi/utilities.h create mode 100644 src/lib/io.c create mode 100644 src/lib/spinlock.c create mode 100644 src/lib/stdio.c create mode 100644 src/lib/string.c create mode 100644 src/main.c create mode 100644 src/mm/kmalloc.c create mode 100644 src/mm/kmalloc.h create mode 100644 src/mm/pmm.c create mode 100644 src/mm/pmm.h create mode 100644 src/mm/vmm.c create mode 100644 src/mm/vmm.h create mode 100644 src/scheduler/sched.asm create mode 100644 src/scheduler/sched.c create mode 100644 src/scheduler/sched.h create mode 100644 src/sys/acpi.c create mode 100644 src/sys/acpi.h create mode 100644 src/sys/pci.c create mode 100644 src/sys/pci.h create mode 100644 src/sys/time.c create mode 100644 src/uacpi/default_handlers.c create mode 100644 src/uacpi/event.c create mode 100644 src/uacpi/files.cmake create mode 100644 src/uacpi/interpreter.c create mode 100644 src/uacpi/io.c create mode 100644 src/uacpi/mutex.c create mode 100644 src/uacpi/namespace.c create mode 100644 src/uacpi/notify.c create mode 100644 src/uacpi/opcodes.c create mode 100644 src/uacpi/opregion.c create mode 100644 src/uacpi/osi.c create mode 100644 src/uacpi/registers.c create mode 100644 src/uacpi/resources.c create mode 100644 src/uacpi/shareable.c create mode 100644 src/uacpi/sleep.c create mode 100644 src/uacpi/stdlib.c create mode 100644 src/uacpi/tables.c create mode 100644 src/uacpi/types.c create mode 100644 src/uacpi/uacpi.c create mode 100644 src/uacpi/utilities.c create mode 100644 src/uacpi_kernel_api.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f288702 --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cb8b5b5 --- /dev/null +++ b/Makefile @@ -0,0 +1,102 @@ +BUILD_DIR=build +CC = gcc +AS = nasm +LD = ld +CFLAGS += -Wall \ + -Wextra \ + -std=gnu11 \ + -ffreestanding \ + -fno-stack-protector \ + -fno-stack-check \ + -fno-lto \ + -fPIE \ + -m64 \ + -march=x86-64 \ + -mno-80387 \ + -mno-mmx \ + -mno-sse \ + -mno-sse2 \ + -mno-red-zone \ + -I src/include \ + -O0 \ + -ggdb3 \ + -g +CDEBUG = -g +LDFLAGS += -m elf_x86_64 \ + -nostdlib \ + -static \ + -pie \ + --no-dynamic-linker \ + -z text \ + -z max-page-size=0x1000 \ + -T linker.ld +NASMFLAGS = -f elf64 +dependencies: + # build limine + rm -rf limine + git clone https://github.com/limine-bootloader/limine.git --branch=v8.x-binary --depth=1 + make -C limine + # clone flanterm + rm -rf src/flanterm + + git clone https://codeberg.org/mintsuki/flanterm src/flanterm +all: + # make build directory + mkdir -p $(BUILD_DIR) || true + # build & link boot and kernel files + $(CC) -c src/main.c -o $(BUILD_DIR)/main.o $(CFLAGS) + $(CC) -c src/flanterm/src/flanterm.c -o $(BUILD_DIR)/flanterm.o $(CFLAGS) + $(CC) -c src/flanterm/src/flanterm_backends/fb.c -o $(BUILD_DIR)/fb.o $(CFLAGS) + $(CC) -c src/lib/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) + $(CC) -c src/lib/stdio.c -o $(BUILD_DIR)/stdio.o $(CFLAGS) + $(CC) -c src/lib/io.c -o $(BUILD_DIR)/io.o $(CFLAGS) + $(CC) -c src/lib/spinlock.c -o $(BUILD_DIR)/spinlock.o $(CFLAGS) + + $(CC) -c src/hal/gdt.c -o $(BUILD_DIR)/gdt.o $(CFLAGS) + $(AS) src/hal/gdt.asm -o $(BUILD_DIR)/gdt_asm.o $(NASMFLAGS) + $(CC) -c src/hal/idt.c -o $(BUILD_DIR)/idt.o $(CFLAGS) + $(AS) src/hal/idt.asm -o $(BUILD_DIR)/idt_asm.o $(NASMFLAGS) + $(CC) -c src/hal/apic.c -o $(BUILD_DIR)/apic.o $(CFLAGS) + $(CC) -c src/hal/ioapic.c -o $(BUILD_DIR)/ioapic.o $(CFLAGS) + $(CC) -c src/hal/timer.c -o $(BUILD_DIR)/timer.o $(CFLAGS) + $(CC) -c src/hal/smp.c -o $(BUILD_DIR)/smp.o $(CFLAGS) + $(CC) -c src/hal/tsc.c -o $(BUILD_DIR)/tsc.o $(CFLAGS) + $(CC) -c src/mm/pmm.c -o $(BUILD_DIR)/pmm.o $(CFLAGS) + $(CC) -c src/mm/vmm.c -o $(BUILD_DIR)/vmm.o $(CFLAGS) + $(CC) -c src/mm/kmalloc.c -o $(BUILD_DIR)/kmalloc.o $(CFLAGS) + $(CC) -c src/sys/acpi.c -o $(BUILD_DIR)/acpi.o $(CFLAGS) + $(CC) -c src/sys/pci.c -o $(BUILD_DIR)/pci.o $(CFLAGS) + + $(CC) -c src/drivers/serial.c -o $(BUILD_DIR)/serial.o $(CFLAGS) + $(CC) -c src/drivers/pmt.c -o $(BUILD_DIR)/pmt.o $(CFLAGS) + $(CC) -c src/drivers/ahci.c -o $(BUILD_DIR)/ahci.o $(CFLAGS) + + $(CC) -c src/scheduler/sched.c -o $(BUILD_DIR)/sched.o $(CFLAGS) + $(AS) src/scheduler/sched.asm -o $(BUILD_DIR)/sched_asm.o $(NASMFLAGS) + + + + + # link everything to an elf + $(LD) -o $(BUILD_DIR)/SFB25.elf $(BUILD_DIR)/*.o $(LDFLAGS) + # Create a directory which will be our ISO root. + mkdir -p iso_root + # Copy the relevant files over. + cp -v $(BUILD_DIR)/SFB25.elf limine.conf limine/limine-bios.sys \ + limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/ + # Create the EFI boot tree and copy Limine's EFI executables over. + mkdir -p iso_root/EFI/BOOT + cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/ + cp -v limine/BOOTIA32.EFI iso_root/EFI/BOOT/ + # Create the bootable ISO. + xorriso -as mkisofs -b limine-bios-cd.bin \ + -no-emul-boot -boot-load-size 4 -boot-info-table \ + --efi-boot limine-uefi-cd.bin \ + -efi-boot-part --efi-boot-image --protective-msdos-label \ + iso_root -o $(BUILD_DIR)/SFB25.iso + # Install Limine stage 1 and 2 for legacy BIOS boot. + ./limine/limine bios-install $(BUILD_DIR)/SFB25.iso +disk: + dd if=/dev/zero of=disk.img bs=1M count=128 +elftest: + $(CC) src/elf/elftest.c -o $(BUILD_DIR)/elftest -ffreestanding -Isrc/include -static -fPIE -nostdlib diff --git a/README.md b/README.md new file mode 100644 index 0000000..18c8329 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# SFB/25 + +Hobby operating system for the x86_64 architecture written in C. Licensed under GPLv3 + +## How to build + +First run `make dependencies` to clone and build Limine and Flanterm + +Then run `make all` - make sure to adjust the `CC`, `AS` and `LD` flags to match your cross-compiling toolchain + +in the `build` folder you should have a `SFB25.iso` file. + +To try out SFB/25 you can use QEMU: + +`qemu-system-x86_64 build/SFB25.iso -m 512M` + +## External projects + +- [Limine bootloader](https://github.com/limine-bootloader/limine) for the bootloader +- [Flanterm](https://github.com/mintsuki/flanterm) for the terminal +- [uACPI](https://github.com/uacpi/uacpi) for the AML interpreter and other ACPI stuff \ No newline at end of file diff --git a/bochsrc b/bochsrc new file mode 100644 index 0000000..a4c69b1 --- /dev/null +++ b/bochsrc @@ -0,0 +1,5 @@ +display_library: x, options="gui_debug" +ata0-master: type=cdrom, path="build/SFB25.iso", status=inserted +boot: cdrom +memory: guest=512, host=512 +cpu: count=3, ips=95000000 diff --git a/bx_enh_dbg.ini b/bx_enh_dbg.ini new file mode 100644 index 0000000..7a02f4f --- /dev/null +++ b/bx_enh_dbg.ini @@ -0,0 +1,26 @@ +# bx_enh_dbg_ini +SeeReg[0] = TRUE +SeeReg[1] = TRUE +SeeReg[2] = TRUE +SeeReg[3] = TRUE +SeeReg[4] = FALSE +SeeReg[5] = FALSE +SeeReg[6] = FALSE +SeeReg[7] = FALSE +SingleCPU = FALSE +ShowIOWindows = TRUE +ShowButtons = TRUE +SeeRegColors = TRUE +ignoreNxtT = TRUE +ignSSDisasm = TRUE +UprCase = 0 +DumpInAsciiMode = 3 +isLittleEndian = TRUE +DefaultAsmLines = 512 +DumpWSIndex = 0 +DockOrder = 0x123 +ListWidthPix[0] = 158 +ListWidthPix[1] = 218 +ListWidthPix[2] = 250 +MainWindow = 0, 0, 714, 500 +FontName = Normal diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..e771016 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,4 @@ +-I./src/include +-Wall +-Wno-incompatible-library-redeclaration +-Wextra \ No newline at end of file diff --git a/limine.conf b/limine.conf new file mode 100644 index 0000000..39732a6 --- /dev/null +++ b/limine.conf @@ -0,0 +1,20 @@ +# Timeout in seconds that Limine will use before automatically booting. +timeout: 5 + +# The entry name that will be displayed in the boot menu. +/SFB25 (KASLR on) + # We use the Limine boot protocol. + protocol: limine + + # Path to the kernel to boot. boot:/// represents the partition on which limine.cfg is located. + kernel_path: boot():/SFB25.elf + +# Same thing, but without KASLR. +/SFB25 (KASLR off) + # We use the Limine boot protocol. + protocol: limine + + kaslr: no + + # Path to the kernel to boot. boot:/// represents the partition on which limine.cfg is located. + kernel_path: boot():/SFB25.elf \ No newline at end of file diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..51dc81c --- /dev/null +++ b/linker.ld @@ -0,0 +1,69 @@ +OUTPUT_FORMAT(elf64-x86-64) +OUTPUT_ARCH(i386:x86-64) + +/* We want the symbol _start to be our entry point */ +ENTRY(_start) + +/* Define the program headers we want so the bootloader gives us the right */ +/* MMU permissions */ +PHDRS +{ + text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ + rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ + data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ + dynamic PT_DYNAMIC FLAGS((1 << 1) | (1 << 2)) ; /* Dynamic PHDR for relocations */ +} + +SECTIONS +{ + /* We wanna be placed in the topmost 2GiB of the address space, for optimisations */ + /* and because that is what the Limine spec mandates. */ + /* Any address in this region will do, but often 0xffffffff80000000 is chosen as */ + /* that is the beginning of the region. */ + . = 0xffffffff80000000; + + text_start_addr = .; + + .text : { + *(.text .text.*) + } :text + + text_end_addr = .; + /* Move to the next memory page for .rodata */ + . += CONSTANT(MAXPAGESIZE); + + . = ALIGN(0x1000); + rodata_start_addr = .; + .rodata : { + *(.rodata .rodata.*) + } :rodata + rodata_end_addr = .; + /* Move to the next memory page for .data */ + . += CONSTANT(MAXPAGESIZE); + + . = ALIGN(0x1000); + data_start_addr = .; + .data : { + *(.data .data.*) + } :data + + /* Dynamic section for relocations, both in its own PHDR and inside data PHDR */ + .dynamic : { + *(.dynamic) + } :data :dynamic + + /* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */ + /* unnecessary zeros will be written to the binary. */ + /* If you need, for example, .init_array and .fini_array, those should be placed */ + /* above this. */ + .bss : { + *(.bss .bss.*) + *(COMMON) + } :data + data_end_addr = .; + /* Discard .note.* and .eh_frame since they may cause issues on some hosts. */ + /DISCARD/ : { + *(.eh_frame) + *(.note .note.*) + } +} \ No newline at end of file diff --git a/src/drivers/ahci.c b/src/drivers/ahci.c new file mode 100644 index 0000000..4ecdb89 --- /dev/null +++ b/src/drivers/ahci.c @@ -0,0 +1,75 @@ + +#include +#include +#include "../hal/apic.h" +#include "../sys/pci.h" +#include "../mm/vmm.h" +#include "../hal/idt.h" + +#define AHCI_MSE 0x02 +#define AHCI_BME 0x03 +#define AHCI_INT_ENABLED (1 << 10) + +#define AHCI_CLASS_ID 0x01 +#define AHCI_SUBCLASS_ID 0x06 + +#define AHCI_HOST_CAP_REG 0x00 +#define AHCI_GHC_REG 0X04 +#define AHCI_INT_STATUS_REG 0x08 +#define AHCI_PORTS_IMPL_REG 0x0C +#define AHCI_BOHC_REG 0x28 + +uint64_t ahci_base_address = 0; + +uint32_t ahci_read_reg(uint16_t reg){ + return *(uint32_t*)((uint64_t)ahci_base_address + reg); +} + +void ahci_write_reg(uint16_t reg, uint32_t data){ + *(uint32_t*)((uint64_t)ahci_base_address + reg) = data; +} + + +void ahci_init(){ + pci_header_0_t *header = (pci_header_0_t *)pci_find_device(AHCI_CLASS_ID, AHCI_SUBCLASS_ID); + + if(!header){ + klog(LOG_ERROR, __func__, "AHCI controller not found!"); + kkill(); + } + + kprintf("size of header: 0x{xn}", sizeof(pci_header_0_t)); + + extern uint64_t hhdmoffset; + + ahci_base_address = header->bar5 & 0xfffff000; + + kprintf("ahci: 0x{x}\n", ahci_base_address); + + /* Enable bus master, memory space and interrupts */ + header->header.command |= AHCI_MSE | AHCI_BME | AHCI_INT_ENABLED; + + /* Map the AHCI registers */ + kernel_map_pages((uint64_t*)ahci_base_address, 1, PTE_BIT_RW | PTE_BIT_NX | PTE_BIT_UNCACHABLE); + + ahci_base_address += hhdmoffset; + + /* BIOS/OS Handoff */ + kprintf("ahci: Performing BIOS/OS handoff\n"); + ahci_write_reg(AHCI_BOHC_REG, ahci_read_reg(AHCI_BOHC_REG) | 0x2); // Set the OS Owned Semaphore bit - OS now owns the HBA + + uint32_t bohc = ahci_read_reg(AHCI_BOHC_REG); + + /* Wait for the handoff to complete*/ + while((bohc & 0b01) != 1 && (bohc & 0b1) != 0){ + apic_sleep(200); + bohc = ahci_read_reg(AHCI_BOHC_REG); + } + + /* Reset the controller */ + ahci_write_reg(AHCI_GHC_REG, 1); + + /* Set the IRQ and enable interrupts */ + kprintf("ahci: Requesting pin {d}\n", header->interrupt_pin); + +} \ No newline at end of file diff --git a/src/drivers/ahci.h b/src/drivers/ahci.h new file mode 100644 index 0000000..3128642 --- /dev/null +++ b/src/drivers/ahci.h @@ -0,0 +1 @@ +void ahci_init(); \ No newline at end of file diff --git a/src/drivers/pmt.c b/src/drivers/pmt.c new file mode 100644 index 0000000..9ef5656 --- /dev/null +++ b/src/drivers/pmt.c @@ -0,0 +1,79 @@ +#include "../sys/acpi.h" +#include +#include +#include + + +#define PMT_TIMER_RATE 3579545 + +#define X_PMT_TMR_BLOCK_OFFSET 208 + +fadt_t *fadt; +extern uint64_t hhdmoffset; + +/* Use extended address fields instead */ +bool use_ext = false; + +uint64_t pmt_read_reg(gas_t X_PMTimerBlock){ + + /* TO FIX - address space id invalid in Bochs */ + + /* Check address space ID field to understand how to access the register */ + if(X_PMTimerBlock.address_space_id == 0x00){ + /* Access through memory */ + return *((uint64_t*)X_PMTimerBlock.address); + }else if (X_PMTimerBlock.address_space_id == 0x01){ + /* Access through I/O port */ + return inl(X_PMTimerBlock.address); + }else{ + serial_kprintf("address id: 0x{xn}", X_PMTimerBlock.address_space_id); + klog(LOG_ERROR, __func__, "X_PMTimerBlock address space id isn't supported!"); + return 0; + } +} + +int pmt_init(){ + fadt = (fadt_t*)((uint64_t)find_acpi_table("FACP")); + + if(!fadt){ + klog(LOG_ERROR, __func__, "Didn't find FADT table"); + kkill(); + } + + fadt = (fadt_t*)((uint64_t)fadt + hhdmoffset); + + /* Check if timer exists */ + if(fadt->PMTimerLength == 0){ + return -1; + } + + /* If ACPI revision is over or equal 2 and if X_PMTimerBlock isnt 0, then use X_PMTimerBlock */ + if(fadt->header.revision >= 2 && fadt->X_PMTimerBlock.address != 0 && (fadt->header.length >= X_PMT_TMR_BLOCK_OFFSET)){ + serial_kprintf("pmt: Using the X_PMTimerBlock\n"); + + use_ext = true; + } + + return 0; +} + +void pmt_delay(uint64_t us){ + uint64_t count; + + if(!use_ext){ + count = inl(fadt->PMTimerBlock); + }else{ + count = pmt_read_reg(fadt->X_PMTimerBlock); + } + + uint64_t target = (us * PMT_TIMER_RATE) / 1000000; + uint64_t current = 0; + + while(current < target){ + if(!use_ext){ + current = ((inl(fadt->PMTimerBlock) - count) & 0xffffff); + }else{ + current = (pmt_read_reg(fadt->X_PMTimerBlock) - count) & 0xffffff; + } + } +} \ No newline at end of file diff --git a/src/drivers/pmt.h b/src/drivers/pmt.h new file mode 100644 index 0000000..9570c85 --- /dev/null +++ b/src/drivers/pmt.h @@ -0,0 +1,3 @@ +#include +int pmt_init(); +void pmt_delay(uint64_t us); \ No newline at end of file diff --git a/src/drivers/rtc.c b/src/drivers/rtc.c new file mode 100644 index 0000000..e69de29 diff --git a/src/drivers/serial.c b/src/drivers/serial.c new file mode 100644 index 0000000..a18caf6 --- /dev/null +++ b/src/drivers/serial.c @@ -0,0 +1,76 @@ +#include "../sys/acpi.h" +#include "../hal/ioapic.h" +#include +#include + +#define COM1 0x3F8 + +#define LINE_CTRL_REG 0x3 +#define LINE_STAT_REG 0x5 +#define INT_ENABLE_REG 0x1 +#define MDM_CTRL_REG 0x4 + +#define DIVISOR 0x12 // We want a baud rate of 9600, so the divisor here is 12 + +bool serial_enabled = false; + +void serial_init(){ + + /* Disable interrupts */ + outb(COM1 + INT_ENABLE_REG, 0); + + /* Set the DLAB bit */ + outb(COM1 + LINE_CTRL_REG, (1 << 7)); + + /* Send least significant byte of divisor */ + outb(COM1 + 1, 0); + + /* Send most significant byte of divisor */ + outb(COM1, 12); + + /* Clear the DLAB bit */ + outb(COM1 + LINE_CTRL_REG, (0 << 7)); + + /* Set the character length to 8 bits, parity to none and stop bit to 1 */ + outb(COM1 + LINE_CTRL_REG, 0b00000011); + + /* Set DTR, RTS and enables IRQ */ + outb(COM1 + MDM_CTRL_REG, 0b00001101); + + /* Set loopback testing mode to see if UART werks */ + outb(COM1 + MDM_CTRL_REG, 0b00011110); + + outb(COM1, 0xAE); + + if(inb(COM1) != 0xAE){ + klog(LOG_WARN, __func__, "Serial controller failed test, serial output will not work"); + return; + } + + /* Disable loopback and set DTR bit */ + outb(COM1 + MDM_CTRL_REG, 0b00001111); + + serial_enabled = true; + +} + +uint8_t serial_read(){ + while((inb(COM1 + LINE_STAT_REG) & 0x1) == 0){ asm("nop"); } + + return inb(COM1); +} + +void serial_write(uint8_t data){ + while((inb(COM1 + LINE_STAT_REG) & (1 << 5)) == 0){ asm("nop"); } + + outb(COM1, data); +} + +void serial_print(char *str){ + uint64_t i = 0; + while (str[i] != '\0') { + serial_write(str[i]); + i++; + } +} + diff --git a/src/drivers/serial.h b/src/drivers/serial.h new file mode 100644 index 0000000..e0cdd45 --- /dev/null +++ b/src/drivers/serial.h @@ -0,0 +1,8 @@ +#include + +void serial_write(uint8_t data); +uint8_t serial_read(); + +void serial_print(char *str); + +void serial_init(); \ No newline at end of file diff --git a/src/elf/elf.c b/src/elf/elf.c new file mode 100644 index 0000000..8530ccd --- /dev/null +++ b/src/elf/elf.c @@ -0,0 +1,24 @@ +#include +#include +#include "elf.h" + +kstatus check_elf(elf64_ehdr *ehdr){ + if(!ehdr){ + return KERNEL_STATUS_ERROR; + } + + if( ehdr->e_ident[0] == ELFMAG0 && ehdr->e_ident[1] == ELFMAG1 && + ehdr->e_ident[2] == ELFMAG2 && ehdr->e_ident[3] == ELFMAG3){ + return KERNEL_STATUS_SUCCESS; + } + + return KERNEL_STATUS_ERROR; +} + +kstatus kernel_load_elf64(elf64_ehdr *ehdr){ + if(!check_elf(ehdr)){ + return KERNEL_STATUS_ERROR; + } + + +} \ No newline at end of file diff --git a/src/elf/elf.h b/src/elf/elf.h new file mode 100644 index 0000000..b68ff86 --- /dev/null +++ b/src/elf/elf.h @@ -0,0 +1,43 @@ +#include +typedef uint64_t elf64_addr; +typedef uint64_t elf64_off; +typedef uint16_t elf64_half; +typedef uint32_t elf64_word; +typedef int32_t elf64_sword; +typedef uint64_t elf64_xword; +typedef int64_t elf64_sxword; + +# define ELFMAG0 0x7F // e_ident[EI_MAG0] +# define ELFMAG1 'E' // e_ident[EI_MAG1] +# define ELFMAG2 'L' // e_ident[EI_MAG2] +# define ELFMAG3 'F' // e_ident[EI_MAG3] + +typedef struct elf64_ehdr { + uint8_t e_ident[16]; + elf64_half e_type; + elf64_half e_machine; + elf64_word e_version; + elf64_addr e_entry; + elf64_off e_phoff; + elf64_off e_shoff; + elf64_word e_flags; + elf64_half e_ehsize; + elf64_half e_phentsize; + elf64_half e_phnum; + elf64_half e_shentsize; + elf64_half e_shnum; + elf64_half e_shstrndx; +}__attribute((packed))elf64_ehdr; + +enum e_ident{ + EI_MAG0 = 0, // 0x7F + EI_MAG1 = 1, // 'E' + EI_MAG2 = 2, // 'L' + EI_MAG3 = 3, // 'F' + EI_CLASS = 4, // Architecture (32/64) + EI_DATA = 5, // Byte Order + EI_VERSION = 6, // ELF Version + EI_OSABI = 7, // OS Specific + EI_ABIVERSION = 8, // OS Specific + EI_PAD = 9 // Padding +}; \ No newline at end of file diff --git a/src/elf/elftest.c b/src/elf/elftest.c new file mode 100644 index 0000000..ab72522 --- /dev/null +++ b/src/elf/elftest.c @@ -0,0 +1,5 @@ + + +int _start(){ + return 123; +} \ No newline at end of file diff --git a/src/hal/apic.c b/src/hal/apic.c new file mode 100644 index 0000000..d09af97 --- /dev/null +++ b/src/hal/apic.c @@ -0,0 +1,137 @@ +#include "../sys/acpi.h" +#include "../drivers/pmt.h" +#include "smp.h" +#include "timer.h" +#include "ioapic.h" +#include +#include +#include +#include // GCC specific + +#define LAPIC_ID_REG 0x020 +#define LAPIC_EOI_REG 0x0B0 +#define LAPIC_SPURIOUS_REG 0x0F0 +#define LAPIC_ERR_REG 0x280 + +#define LAPIC_LINT0_REG 0x350 +#define LAPIC_LINT1_REG 0x360 + +#define LAPIC_ICR_REG 0x300 + +#define LAPIC_LVT_TIMER_REG 0x320 +#define LAPIC_TIMER_INITIAL_CNT_REG 0x380 +#define LAPIC_TIMER_CURRENT_CNT_REG 0x390 +#define LAPIC_TIMER_DIVIDER_REG 0x3E0 + +#define LAPIC_TIMER_MASK (1 << 16) +#define LAPIC_TIMER_UNMASK 0xFFFEFFFF + +#define LAPIC_TIMER_PERIODIC (1 << 17) +#define LAPIC_TIMER_VECTOR 69 + + +extern madt_t *madt; +extern uint64_t hhdmoffset; + +uint64_t lapic_address = 0; +uint64_t timer_speed_us = 0; + +void lapic_write_reg(uint32_t reg, uint32_t data){ + *((uint32_t*)(lapic_address+reg)) = data; +} + +uint32_t lapic_read_reg(uint32_t reg){ + return(*((uint32_t*)(lapic_address+reg))); +} + +/* Assumes single-threaded*/ +void apic_sleep(uint64_t ms){ + uint64_t lapic_timer_ticks = get_cpu_struct()->lapic_timer_ticks; + uint64_t curcnt = get_cpu_struct()->lapic_timer_ticks; + while (lapic_timer_ticks - curcnt < ms) { + lapic_timer_ticks = get_cpu_struct()->lapic_timer_ticks; + } +} + +atomic_flag lapic_timer_flag = ATOMIC_FLAG_INIT; +void lapic_timer_init(int us){ + acquire_lock(&lapic_timer_flag); + /* Stop the APIC timer */ + lapic_write_reg(LAPIC_TIMER_INITIAL_CNT_REG, 0); + + /* Set the divisor to 16 */ + lapic_write_reg(LAPIC_TIMER_DIVIDER_REG, 0b11); + + /* Set the intial count to max */ + lapic_write_reg(LAPIC_TIMER_INITIAL_CNT_REG, 0xffffffff); + + /* Call a delay function based on the available timer */ + pmt_delay(us); + + /* Mask the timer (prevents interrupts) */ + lapic_write_reg(LAPIC_LVT_TIMER_REG, LAPIC_TIMER_MASK); + + /* Determine the inital count to be used for a delay set by `timer_speed_us` */ + uint32_t calibration = 0xffffffff - lapic_read_reg(LAPIC_TIMER_CURRENT_CNT_REG); + + /* Set the timer interrupt vector and put the timer into periodic mode */ + lapic_write_reg(LAPIC_LVT_TIMER_REG, LAPIC_TIMER_VECTOR | LAPIC_TIMER_PERIODIC); + + /* Set the inital count to the calibration */ + lapic_write_reg(LAPIC_TIMER_INITIAL_CNT_REG, calibration); + + free_lock(&lapic_timer_flag); + +} + +void apic_init(void){ + asm("cli"); + + lapic_address = madt->lic_address + hhdmoffset; + + lapic_ao_t *lapic_ao = (lapic_ao_t*) find_ics(0x5); // Local APIC Address Override + + /* If there is a lapic address override present then use that instead */ + if(lapic_ao){ + /* Check that the field isnt 0 */ + if(lapic_ao->lapic_address != 0){ + lapic_address = lapic_ao->lapic_address + hhdmoffset; + } + } + + /* Enable the lapic and set the spurious interrupt vector to 0xFF */ + lapic_write_reg(LAPIC_SPURIOUS_REG, 0x1FF); + + /* Initialize the IOAPIC */ + ioapic_init(); + + /* Start the timers for calibration of the APIC timer */ + timer_init(); + + /* Start the APIC timer with 10ms timer */ + lapic_timer_init(10000); + + asm("sti"); +} + +void ap_apic_init(){ + asm("cli"); + /* Enable the lapic and set the spurious interrupt vector to 0xFF */ + lapic_write_reg(LAPIC_SPURIOUS_REG, 0x1FF); + + /* Start the APIC timer */ + lapic_timer_init(10000); + + asm("sti"); +} + +void apic_timer_handler(){ + lapic_write_reg(LAPIC_EOI_REG, 0); + if(get_cpu_struct_initialized()){ + get_cpu_struct()->lapic_timer_ticks++; + } +} + +void apic_send_ipi(uint8_t dest_field, uint8_t dest_shorthand, uint8_t trigger, uint8_t level, uint8_t status, uint8_t destination, uint8_t delivery_mode, uint8_t vector){ + +} \ No newline at end of file diff --git a/src/hal/apic.h b/src/hal/apic.h new file mode 100644 index 0000000..0f34a0b --- /dev/null +++ b/src/hal/apic.h @@ -0,0 +1,4 @@ + +void apic_init(void); +void ap_apic_init(); +void apic_sleep(int ms); \ No newline at end of file diff --git a/src/hal/gdt.asm b/src/hal/gdt.asm new file mode 100644 index 0000000..03f9075 --- /dev/null +++ b/src/hal/gdt.asm @@ -0,0 +1,34 @@ +[bits 64] + +default rel + +extern gdtr + +global s_load_gdt + +s_load_gdt: + + lgdt [gdtr] + + ; move kernel data offset into data registers + mov ax, 0x10 + mov ds, ax + mov es, ax + mov ss, ax + + ; zero the optional data registers + xor ax, ax + mov fs, ax + mov gs, ax + + ; pop the return instruction pointer from the stack + pop rax + + ; first push the segment selector we will far return to (0x08 is the code segment) + push 0x08 + + ; then push the return instruction pointer + push rax + + ; and finally far return + retfq \ No newline at end of file diff --git a/src/hal/gdt.c b/src/hal/gdt.c new file mode 100644 index 0000000..6275900 --- /dev/null +++ b/src/hal/gdt.c @@ -0,0 +1,33 @@ +#include "gdt.h" +#include + +gdt_descriptor gdt[5] = {0}; + +gdt_register gdtr = {sizeof(gdt)-1, (uint64_t)(&gdt)}; + +extern void s_load_gdt(); + +void gdt_set_entry(int num, unsigned long long base, unsigned long long limit, unsigned char access, unsigned char granularity){ + // descriptor base access + gdt[num].base_low = (base & 0xFFFF); + gdt[num].base_middle = (base >> 16) & 0xFF; + gdt[num].base_high = (base >> 24) & 0xFF; + + // descriptor limits + gdt[num].limit_low = (limit & 0xFFFF); + gdt[num].granularity = ((limit >> 16) & 0x0F); + + // granularity and access flag + gdt[num].granularity |= (granularity & 0xF) << 4; + gdt[num].access = access; +} + +void set_gdt(void){ + gdt_set_entry(0, 0, 0, 0, 0); // null segment offset 0x00 + gdt_set_entry(1, 0, 0xFFFFF, 0x9A, 0xA); // kernel code offset 0x08 + gdt_set_entry(2, 0, 0xFFFFF, 0x92, 0xA); // kernel data offset 0x10 + gdt_set_entry(3, 0, 0xFFFFF, 0xFA, 0xA); // userspace code offset 0x18 + gdt_set_entry(4, 0, 0xFFFFF, 0xF2, 0xA); // userspace data offset 0x20 + s_load_gdt(); + +} \ No newline at end of file diff --git a/src/hal/gdt.h b/src/hal/gdt.h new file mode 100644 index 0000000..a30975a --- /dev/null +++ b/src/hal/gdt.h @@ -0,0 +1,17 @@ +#include + +typedef struct gdt_descriptor { + uint16_t limit_low; + uint16_t base_low; + uint8_t base_middle; + uint8_t access; + uint8_t granularity; + uint8_t base_high; +} __attribute((packed)) gdt_descriptor; + +typedef struct gdt_register { + uint16_t limit; + uint64_t base_address; +} __attribute((packed)) gdt_register; + +void set_gdt(void); \ No newline at end of file diff --git a/src/hal/idt.asm b/src/hal/idt.asm new file mode 100644 index 0000000..cc84c13 --- /dev/null +++ b/src/hal/idt.asm @@ -0,0 +1,330 @@ +default rel + +extern interrupt_handler + +extern idtr + +global s_isr0 +global s_isr1 +global s_isr2 +global s_isr3 +global s_isr4 +global s_isr5 +global s_isr6 +global s_isr7 +global s_isr8 +global s_isr9 +global s_isr10 +global s_isr11 +global s_isr12 +global s_isr13 +global s_isr14 +global s_isr15 +global s_isr16 +global s_isr17 +global s_isr18 +global s_isr19 +global s_isr20 +global s_isr21 +global s_isr22 +global s_isr23 +global s_isr24 +global s_isr25 +global s_isr26 +global s_isr27 +global s_isr28 +global s_isr29 +global s_isr30 +global s_isr31 + +global s_isr44 + +global s_isr69 + +global s_isr70 + +global s_isr255 + +global s_load_idt + +s_isr0: + ; + push qword 0 ; dummy + push qword 0 ; isr num + jmp isr_handler + +s_isr1: + + push qword 0 ; dummy + push qword 1 ; isr num + jmp isr_handler + +s_isr2: + + push qword 0 ; dummy + push qword 2 ; isr num + jmp isr_handler + +s_isr3: + + push qword 0 ; dummy + push qword 3 ; isr num + jmp isr_handler + +s_isr4: + + push qword 0 ; dummy + push qword 4 ; isr num + jmp isr_handler + +s_isr5: + + push qword 0 ; dummy + push qword 5 ; isr num + jmp isr_handler + +s_isr6: + + push qword 0 ; dummy + push qword 6 ; isr num + jmp isr_handler + +s_isr7: + + push qword 0 ; dummy + push qword 7 ; isr num + jmp isr_handler + +s_isr8: + + ; dont push dummy as it already pushes one + push qword 8 ; isr num + jmp isr_handler + +s_isr9: + + push qword 0 ; dummy + push qword 9 ; isr num + jmp isr_handler + +s_isr10: + + ; dont push dummy as it already pushes one + push qword 10 ; isr num + jmp isr_handler + +s_isr11: + + ; dont push dummy as it already pushes one + push qword 11 ; isr num + jmp isr_handler + +s_isr12: + + ; dont push dummy as it already pushes one + push qword 12 ; isr num + jmp isr_handler + +s_isr13: + + ; dont push dummy as it already pushes one + push qword 13 ; isr num + jmp isr_handler + +s_isr14: + + ; dont push dummy as it already pushes one + push qword 14 ; isr num + jmp isr_handler + +s_isr15: + + push qword 0 ; dummy + push qword 15 ; isr num + jmp isr_handler + +s_isr16: + + push qword 0 ; dummy + push qword 16 ; isr num + jmp isr_handler + +s_isr17: + + push qword 0 ; dummy + push qword 17 ; isr num + jmp isr_handler + +s_isr18: + + push qword 0 ; dummy + push qword 18 ; isr num + jmp isr_handler + +; 19: Reserved +s_isr19: + + push qword 0 + push qword 19 + jmp isr_handler + +; 20: Reserved +s_isr20: + + push qword 0 + push qword 20 + jmp isr_handler + +; 21: Reserved +s_isr21: + + push qword 0 + push qword 21 + jmp isr_handler + +; 22: Reserved +s_isr22: + + push qword 0 + push qword 22 + jmp isr_handler + +; 23: Reserved +s_isr23: + + push qword 0 + push qword 23 + jmp isr_handler + +; 24: Reserved +s_isr24: + + push qword 0 + push qword 24 + jmp isr_handler + +; 25: Reserved +s_isr25: + + push qword 0 + push qword 25 + jmp isr_handler + +; 26: Reserved +s_isr26: + + push qword 0 + push qword 26 + jmp isr_handler + +; 27: Reserved +s_isr27: + + push qword 0 + push qword 27 + jmp isr_handler + +; 28: Reserved +s_isr28: + + push qword 0 + push qword 28 + jmp isr_handler + +; 29: Reserved +s_isr29: + + push qword 0 + push qword 29 + jmp isr_handler + +; 30: Reserved +s_isr30: + + push qword 0 + push qword 30 + jmp isr_handler + +; 31: Reserved +s_isr31: + + push qword 0 + push qword 31 + jmp isr_handler + +s_isr44: + + push qword 0 + push qword 44 + jmp isr_handler + +; 69 - APIC timer +s_isr69: + + push qword 0 + push qword 69 + jmp isr_handler + +; 70 - Kernel panic +s_isr70: + + push qword 0 + push qword 70 + jmp isr_handler + +s_isr255: + + push qword 0 + push qword 255 + jmp isr_handler + +%macro pushaq 0 + push rax + push rbx + push rcx + push rdx + push rbp + push rsi + push rdi + push r8 + push r9 + push r10 + push r11 + push r12 + push r13 + push r14 + push r15 + +%endmacro + +%macro popaq 0 + pop r15 + pop r14 + pop r13 + pop r12 + pop r11 + pop r10 + pop r9 + pop r8 + pop rdi + pop rsi + pop rbp + pop rdx + pop rcx + pop rbx + pop rax +%endmacro + +isr_handler: + pushaq + mov rdi, rsp ; put stack frame as parameter for interrupt_handler + call interrupt_handler + popaq + add rsp, 16 ; remove vector and error code from the stack + iretq + +s_load_idt: + lidt [idtr] + sti + ret + diff --git a/src/hal/idt.c b/src/hal/idt.c new file mode 100644 index 0000000..6ab37fe --- /dev/null +++ b/src/hal/idt.c @@ -0,0 +1,230 @@ +#include "idt.h" +#include "error.h" +#include "timer.h" +#include +#include +#include +idt_descriptor idt[256] = {0}; + +idt_register idtr = {sizeof(idt)-1, (uint64_t)(&idt)}; + +/* Expand if needed */ +#define MAX_IRQ 256 + +/* IRQ structure list, eventually restructure to support IRQs on multiple cores */ +irq_t irq_list[MAX_IRQ] = {0}; + +extern void s_isr0(); +extern void s_isr1(); +extern void s_isr2(); +extern void s_isr3(); +extern void s_isr4(); +extern void s_isr5(); +extern void s_isr6(); +extern void s_isr7(); +extern void s_isr8(); +extern void s_isr9(); +extern void s_isr10(); +extern void s_isr11(); +extern void s_isr12(); +extern void s_isr13(); +extern void s_isr14(); +extern void s_isr15(); +extern void s_isr16(); +extern void s_isr17(); +extern void s_isr18(); +extern void s_isr19(); +extern void s_isr20(); +extern void s_isr21(); +extern void s_isr22(); +extern void s_isr23(); +extern void s_isr24(); +extern void s_isr25(); +extern void s_isr26(); +extern void s_isr27(); +extern void s_isr28(); +extern void s_isr29(); +extern void s_isr30(); +extern void s_isr31(); + +extern void s_isr44(); + +extern void s_isr69(); +extern void s_isr70(); + +extern void s_isr255(); + +extern void s_load_idt(); + +atomic_flag irq_register_lock = ATOMIC_FLAG_INIT; + +/* Registers an IRQ with the specified vector. */ +kstatus register_irq_vector(uint8_t vector, void *base, uint8_t flags){ + acquire_lock(&irq_register_lock); + + if(!irq_list[vector].in_use){ + free_lock(&irq_register_lock); + return KERNEL_STATUS_ERROR; + } + + set_idt_descriptor(vector, base, flags); + + irq_list[vector].base = base; + irq_list[vector].in_use = true; + + s_load_idt(); + + free_lock(&irq_register_lock); + + return KERNEL_STATUS_SUCCESS; +} + +/* Registers an IRQ and returns the vector */ +int register_irq(void *base, uint8_t flags){ + acquire_lock(&irq_register_lock); + + for(size_t i = 0; i < MAX_IRQ; i++){ + if(!irq_list[i].in_use) { + set_idt_descriptor(i, base, flags); + irq_list[i].base = base; + irq_list[i].in_use = true; + free_lock(&irq_register_lock); + s_load_idt(); + return i; + } + } + + free_lock(&irq_register_lock); + + return -1; +} + +void set_idt_descriptor(uint8_t vector, void *base, uint8_t flags){ + idt[vector].offset_low = ((uint64_t)base & 0xffff); + + idt[vector].segment_sel = 0x08; // kernel code segment + + idt[vector].ist = 0; + idt[vector].attributes = flags; + + idt[vector].offset_high = ((uint64_t)base >> 16) & 0xffff; + idt[vector].offset_higher = ((uint64_t)base >> 32) & 0xffffffff; + + idt[vector].reserved = 0; +} + +void set_idt(void){ + + /* Set all the reserved vectors as used */ + for(size_t i = 0; i < 32; i++){ + irq_list[i].in_use = true; + irq_list[i].base = NULL; + } + + set_idt_descriptor(0, s_isr0, 0x8E); + set_idt_descriptor(1, s_isr1, 0x8E); + set_idt_descriptor(2, s_isr2, 0x8E); + set_idt_descriptor(3, s_isr3, 0x8E); + set_idt_descriptor(4, s_isr4, 0x8E); + set_idt_descriptor(5, s_isr5, 0x8E); + set_idt_descriptor(6, s_isr6, 0x8E); + set_idt_descriptor(7, s_isr7, 0x8E); + set_idt_descriptor(8, s_isr8, 0x8E); + set_idt_descriptor(9, s_isr9, 0x8E); + set_idt_descriptor(10, s_isr10, 0x8E); + set_idt_descriptor(11, s_isr11, 0x8E); + set_idt_descriptor(12, s_isr12, 0x8E); + set_idt_descriptor(13, s_isr13, 0x8E); + set_idt_descriptor(14, s_isr14, 0x8E); + set_idt_descriptor(15, s_isr15, 0x8E); + set_idt_descriptor(16, s_isr16, 0x8E); + set_idt_descriptor(17, s_isr17, 0x8E); + set_idt_descriptor(18, s_isr18, 0x8E); + set_idt_descriptor(19, s_isr19, 0x8E); + set_idt_descriptor(20, s_isr20, 0x8E); + set_idt_descriptor(21, s_isr21, 0x8E); + set_idt_descriptor(22, s_isr22, 0x8E); + set_idt_descriptor(23, s_isr23, 0x8E); + set_idt_descriptor(24, s_isr24, 0x8E); + set_idt_descriptor(25, s_isr25, 0x8E); + set_idt_descriptor(26, s_isr26, 0x8E); + set_idt_descriptor(27, s_isr27, 0x8E); + set_idt_descriptor(28, s_isr28, 0x8E); + set_idt_descriptor(29, s_isr29, 0x8E); + set_idt_descriptor(30, s_isr30, 0x8E); + set_idt_descriptor(31, s_isr31, 0x8E); + + set_idt_descriptor(44, 0, 0x8E); + set_idt_descriptor(69, s_isr69, 0x8E); + set_idt_descriptor(70, s_isr70, 0x8E); + set_idt_descriptor(255, s_isr255, 0x8E); + + s_load_idt(); +} + +char *exception_messages[] = +{ + "Division Error", + "Debug", + "Non Maskable Interrupt", + "Breakpoint", + "Into Detected Overflow", + "Out of Bounds", + "Invalid Opcode", + "Device not available", + + "Double Fault", + "Coprocessor Segment Overrun", + "Invalid TSS", + "Segment Not Present", + "Stack Fault", + "General Protection Fault", + "Page Fault", + "x87 FPU Floating-point error", + + "Alignment Check", + "Machine Check", + "SIMD Floating-point exception", + "Virtualization exception", + "Control Protection", + "Reserved", + "Reserved", + + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved", + "Reserved" +}; + +void interrupt_handler(interrupt_frame *r){ + + if(r->int_no < 32){ + kprintf("\nOh no! Received interrupt {d}, '{s}'. Below is the provided stack frame{n}{n}", r->int_no, exception_messages[r->int_no]); + kprintf("error code 0x{xn}", r->err); + kprintf("rax 0x{x} | rbx 0x{x} | rcx 0x{x} | rdx 0x{xn}", r->rax, r->rbx, r->rcx, r->rdx); + kprintf("rdi 0x{x} | rsi 0x{x} | rbp 0x{xn}", r->rdi, r->rsi, r->rbp); + kprintf("r8 0x{x} | r9 0x{x} | r10 0x{x} | r11 0x{x} | r12 0x{x} | r13 0x{x} | r14 0x{x} | r15 0x{xn}", r->r8, r->r9, r->r10, r->r11, r->r12, r->r13, r->r14, r->r15); + kprintf("rip 0x{x} | cs 0x{x} | ss 0x{x} | rsp 0x{x} | rflags 0x{xn}", r->rip, r->cs, r->ss, r->rsp, r->rflags); + kkill(); + for(;;); + } + + if(r->int_no == 255){ + kprintf("hey"); + } + + if(r->int_no == 69){ + apic_timer_handler(); + } + if(r->int_no == 70){ + for(;;){ + asm("cli;hlt"); + } + } + + return; +} \ No newline at end of file diff --git a/src/hal/idt.h b/src/hal/idt.h new file mode 100644 index 0000000..9b7e3a9 --- /dev/null +++ b/src/hal/idt.h @@ -0,0 +1,37 @@ +#include "error.h" +#include +#include + +typedef struct idt_descriptor { + uint16_t offset_low; + uint16_t segment_sel; + uint8_t ist; + uint8_t attributes; + uint16_t offset_high; + uint32_t offset_higher; + uint32_t reserved; +} __attribute((packed))idt_descriptor; + +typedef struct idt_register { + uint16_t limit; + uint64_t base_address; +} __attribute((packed)) idt_register; + +typedef struct interrupt_frame { + uint64_t r15, r14, r13, r12, r11, r10, r9, r8, rdi, rsi, rbp, rdx, rcx, rbx, rax; + uint64_t int_no, err; + uint64_t rip, cs, rflags, rsp, ss; +} __attribute((packed)) interrupt_frame; + +typedef struct irq_t { + void *base; + bool in_use; +}irq_t; + +void set_idt_descriptor(uint8_t vector, void *base, uint8_t flags); + +kstatus register_irq_vector(uint8_t vector, void *base, uint8_t flags); + +int register_irq(void *base, uint8_t flags); + +void set_idt(void); \ No newline at end of file diff --git a/src/hal/ioapic.c b/src/hal/ioapic.c new file mode 100644 index 0000000..f98c569 --- /dev/null +++ b/src/hal/ioapic.c @@ -0,0 +1,62 @@ + +#include +#include +#include +#include "../sys/acpi.h" +#include "error.h" + +#define IOREGSEL 0x0 +#define IOWIN 0x10 + +#define IOAPICID 0x0 +#define IOAPICVER 0x1 +#define IOAPICARB 0x2 +#define IOREDTBL(x) (0x10 + (x * 2)) // 0-23 registers + +extern uint64_t hhdmoffset; + +extern madt_t *madt; + +uint64_t ioapic_address; + +void ioapic_write_reg(uint8_t reg, uint32_t data){ + /* First we load IOREGSEL with the register we want to access */ + *(uint32_t*)(ioapic_address + IOREGSEL) = (uint8_t) reg; + + /* Then we write the data to the IOWIN register */ + *(uint32_t*)(ioapic_address + IOWIN) = (uint32_t) data; +} + +uint32_t ioapic_read_reg(uint8_t reg){ + *(uint32_t*)(ioapic_address + IOREGSEL) = reg; + + return *(uint32_t*)(ioapic_address + IOWIN); +} + +void write_redir_entry(uint8_t reg, uint64_t data){ + /* First write lower 32-bits of the data to the specified IOREDTBL register */ + ioapic_write_reg(IOREDTBL(reg), (uint32_t)(data & 0xFFFFFFFF)); + + /* Then write the upper 32-bits */ + ioapic_write_reg(IOREDTBL(reg)+1, (uint32_t)(data >> 32)); +} + +kstatus set_redir_entry(uint8_t pin, uint8_t vector, uint8_t delivery, uint8_t trigger, uint8_t destination_field, uint8_t destination_mode){ + uint64_t data = ((uint64_t)destination_field << 56) | (uint64_t)trigger << 15 | (uint64_t)destination_mode << 11 | (uint64_t)delivery << 8 | vector; + write_redir_entry(pin, data); + return KERNEL_STATUS_SUCCESS; +} + + +void ioapic_init(void){ + + ioapic_t *ioapic = (ioapic_t*) find_ics(0x1); + + if(!ioapic){ + klog(LOG_ERROR, __func__, "IOAPIC ICS not found\n"); + kkill(); + } + + ioapic_address = ioapic->ioapic_address + hhdmoffset; + +} \ No newline at end of file diff --git a/src/hal/ioapic.h b/src/hal/ioapic.h new file mode 100644 index 0000000..bb16e21 --- /dev/null +++ b/src/hal/ioapic.h @@ -0,0 +1,13 @@ +#include "error.h" +#include +void ioapic_init(void); +void write_redir_entry(uint8_t reg, uint64_t data); +kstatus set_redir_entry(uint8_t pin, uint8_t vector, uint8_t delivery, uint8_t trigger, uint8_t destination_field, uint8_t destination_mode); + +#define IOREGSEL 0x0 +#define IOWIN 0x10 + +#define IOAPICID 0x0 +#define IOAPICVER 0x1 +#define IOAPICARB 0x2 +#define IOREDTBL(x) (0x10 + (x * 2)) // 0-23 registers \ No newline at end of file diff --git a/src/hal/smp.c b/src/hal/smp.c new file mode 100644 index 0000000..773f8f2 --- /dev/null +++ b/src/hal/smp.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include "gdt.h" +#include "smp.h" +#include "apic.h" +#include "idt.h" +#include "../mm/vmm.h" +#include +#include +#include + +static volatile struct limine_smp_request smp_request = { + .id = LIMINE_SMP_REQUEST, + .revision = 0, +}; + +extern void s_load_idt(); +extern void s_load_gdt(); +extern uint64_t hhdmoffset; + +/* Returns the CPU structure for this particular CPU */ +cpu_state *get_cpu_struct(){ + return (cpu_state*)rdmsr(GSBASE); +} + +uint64_t get_cpu_count(){ + return smp_request.response->cpu_count; +} + +bool get_cpu_struct_initialized(){ + if(rdmsr(GSBASE) < hhdmoffset){ + return false; + } + + return true; +} + +atomic_flag ap_init_lock = ATOMIC_FLAG_INIT; + +void ap_init(struct limine_smp_info *smp_info){ + + acquire_lock(&ap_init_lock); + + /* Load the GDT */ + s_load_gdt(); + + /* Load the IDT */ + s_load_idt(); + + /* Set the CR3 context */ + extern uint64_t *kernel_page_map; + + vmm_set_ctx(kernel_page_map); + + asm volatile( + "movq %%cr3, %%rax\n\ + movq %%rax, %%cr3\n" + : : : "rax" + ); + + cpu_state *cpu_struct = (cpu_state*)kmalloc(sizeof(cpu_state)); + memset(cpu_struct, 0, sizeof(cpu_state)); + cpu_struct->lapic_id = smp_info->lapic_id; + + wrmsr(KERNELGSBASE, (uint64_t)cpu_struct); + wrmsr(GSBASE, (uint64_t)cpu_struct); + + /* Initialize APIC & APIC timer */ + ap_apic_init(); + + free_lock(&ap_init_lock); + + for(;;); + + scheduler_init(); +} + +void smp_init(){ + + if(!smp_request.response){ + klog(LOG_ERROR, __func__, "Failed to get SMP request"); + kkill(); + } + + struct limine_smp_response *smp_response = smp_request.response; + + kprintf("smp: {d} CPUs\n", smp_response->cpu_count); + + for(uint64_t i = 0; i < smp_response->cpu_count; i++){ + /* Pointer to smp_info is passed in RDI by Limine, so no need to pass any arguments here */ + smp_response->cpus[i]->goto_address = &ap_init; + } + + /* -- Setup CPU structure for BSP -- */ + + /* Allocate CPU structure */ + cpu_state *cpu_struct = (cpu_state*)kmalloc(sizeof(cpu_state)); + cpu_struct->lapic_id = smp_response->cpus[0]->lapic_id; + + wrmsr(KERNELGSBASE, (uint64_t)cpu_struct); + wrmsr(GSBASE, (uint64_t)cpu_struct); + + /* If one of the APs has halted, then halt the BSP */ + extern bool kernel_killed; + if(kernel_killed == true){ + kkill(); + } + +} \ No newline at end of file diff --git a/src/hal/smp.h b/src/hal/smp.h new file mode 100644 index 0000000..0c96151 --- /dev/null +++ b/src/hal/smp.h @@ -0,0 +1,23 @@ +#include +#include +#include "../scheduler/sched.h" + +#pragma once + +#define GSBASE 0xC0000101 +#define KERNELGSBASE 0xC0000102 + +typedef struct cpu_state { + uint32_t lapic_id; + uint64_t lapic_timer_ticks; + proc process_list[PROC_MAX]; + proc *current_process; + uint16_t process_count; + context scheduler_context; +}__attribute((packed))cpu_state; + +void smp_init(); +cpu_state *get_cpu_struct(); +uint64_t get_cpu_count(); +bool get_cpu_struct_initialized(); + diff --git a/src/hal/timer.c b/src/hal/timer.c new file mode 100644 index 0000000..0106eb5 --- /dev/null +++ b/src/hal/timer.c @@ -0,0 +1,24 @@ +#include "../sys/acpi.h" +#include "../hal/ioapic.h" +#include "../hal/apic.h" +#include "../drivers/pmt.h" +#include "timer.h" +#include +#include + +/* Determines which timer will be used for calibration */ +int calibration_timer = -1; + +void timer_init(void){ + if(pmt_init() == -1){ + klog(LOG_INFO, __func__, "PMT Timer not found, falling back"); + /* Fall back to PIT */ + }else{ + calibration_timer = PMT; + } +} + +void sleep(int ms){ + /* Eventually fix this */ + apic_sleep(ms); +} \ No newline at end of file diff --git a/src/hal/timer.h b/src/hal/timer.h new file mode 100644 index 0000000..aed2234 --- /dev/null +++ b/src/hal/timer.h @@ -0,0 +1,11 @@ +#include + +enum USABLE_TIMERS { + HPET = 0, + PMT, + PIT, +}; + +void timer_init(void); +void apic_timer_handler(void); +void sleep(int ms); \ No newline at end of file diff --git a/src/hal/tsc.c b/src/hal/tsc.c new file mode 100644 index 0000000..62b3bc9 --- /dev/null +++ b/src/hal/tsc.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include "error.h" +#include "../drivers/pmt.h" + +uint32_t core_crystal_clock = 0; + +void enable_tsc(){ + asm(".intel_syntax noprefix\n\ + mov rax, cr4\n\ + or rax, 0b10\n\ + mov cr4, rax\n\ + .att_syntax prefix"); +} + +void disable_tsc(){ + asm(".intel_syntax noprefix\n\ + mov rax, cr4\n\ + and rax, 0xFFFFFFFFFFFFFFFD\n\ + mov cr4, rax\n\ + .att_syntax prefix"); +} + +uint64_t read_tsc(){ + uint32_t eax, edx; + asm("rdtsc" :"=a"(eax),"=d"(edx)); + return ((uint64_t)edx << 32 | eax); +} + +kstatus tsc_init(){ + + uint32_t edx, unused; + + /* Check if there is an invariant TSC */ + __get_cpuid(0x80000007, &unused, &unused, &unused, &edx); + + if((edx & (1 << 8)) == 0){ + return KERNEL_STATUS_ERROR; + } + + kprintf("tsc: Invariant TSC found\n"); + + /* Get the core crystal clock so we can determine TSC speed */ + __get_cpuid(0x15, &unused, &unused, &core_crystal_clock, &unused); + + if(core_crystal_clock != 0){ + kprintf("cpuid 15h supported!\n"); + + /* Make it so that it ticks every millisecond */ + core_crystal_clock *= 1000; + + }else{ + /* Calibrate using the PMT */ + enable_tsc(); + uint32_t read1 = read_tsc(); + pmt_delay(1000); + uint32_t read2 = read_tsc(); + disable_tsc(); + core_crystal_clock = read2 - read1; + } + + kprintf("Core crystal clock: {d}\n", core_crystal_clock); + + enable_tsc(); + + return KERNEL_STATUS_SUCCESS; + +} + +uint64_t tsc_get_timestamp(){ + if(core_crystal_clock == 0){ + return 0; + } + uint64_t read = read_tsc(); + return read / core_crystal_clock; +} \ No newline at end of file diff --git a/src/hal/tsc.h b/src/hal/tsc.h new file mode 100644 index 0000000..9db15d0 --- /dev/null +++ b/src/hal/tsc.h @@ -0,0 +1,6 @@ +#include "error.h" +#include + +kstatus tsc_init(); + +uint64_t tsc_get_timestamp(); \ No newline at end of file diff --git a/src/include/SFB25.h b/src/include/SFB25.h new file mode 100644 index 0000000..1617261 --- /dev/null +++ b/src/include/SFB25.h @@ -0,0 +1,18 @@ +#include +void kkill(void); + +typedef char link_symbol_ptr[]; + +#define ALIGN_UP_MASK(x, mask) (((x) + (mask)) & ~(mask)) +#define ALIGN_UP(x, val) ALIGN_UP_MASK(x, (typeof(x))(val) - 1) + +#define ALIGN_DOWN_MASK(x, mask) ((x) & ~(mask)) +#define ALIGN_DOWN(x, val) ALIGN_DOWN_MASK(x, (typeof(x))(val) - 1) + +#define IS_ALIGNED_MASK(x, mask) (((x) & (mask)) == 0) +#define IS_ALIGNED(x, val) IS_ALIGNED_MASK(x, (typeof(x))(val) - 1) + +#define PAGE_ROUND_UP(size) ALIGN_UP(size, PAGE_SIZE) +#define PAGE_ROUND_DOWN(size) ALIGN_DOWN(size, PAGE_SIZE) + +void *kmalloc(uint64_t size); \ No newline at end of file diff --git a/src/include/error.h b/src/include/error.h new file mode 100644 index 0000000..87c2e26 --- /dev/null +++ b/src/include/error.h @@ -0,0 +1,14 @@ +#ifndef ERROR_H +#define ERROR_H + +typedef enum { + /* Success */ + KERNEL_STATUS_SUCCESS, + + /* General error */ + KERNEL_STATUS_ERROR, +} kstatus; + + + +#endif \ No newline at end of file diff --git a/src/include/io.h b/src/include/io.h new file mode 100644 index 0000000..4315d3e --- /dev/null +++ b/src/include/io.h @@ -0,0 +1,12 @@ +#include + +void outb(uint16_t port, uint8_t val); +void outw(uint16_t port, uint16_t val); +void outl(uint16_t port, uint32_t val); + +uint8_t inb(uint16_t port); +uint16_t inw(uint16_t port); +uint32_t inl(uint16_t port); + +void wrmsr(uint64_t msr, uint64_t value); +uint64_t rdmsr(uint64_t msr); diff --git a/src/include/limine.h b/src/include/limine.h new file mode 100644 index 0000000..b4b49c5 --- /dev/null +++ b/src/include/limine.h @@ -0,0 +1,621 @@ +/* BSD Zero Clause License */ + +/* Copyright (C) 2022-2024 mintsuki and contributors. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef LIMINE_H +#define LIMINE_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Misc */ + +#ifdef LIMINE_NO_POINTERS +# define LIMINE_PTR(TYPE) uint64_t +#else +# define LIMINE_PTR(TYPE) TYPE +#endif + +#ifdef __GNUC__ +# define LIMINE_DEPRECATED __attribute__((__deprecated__)) +# define LIMINE_DEPRECATED_IGNORE_START \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +# define LIMINE_DEPRECATED_IGNORE_END \ + _Pragma("GCC diagnostic pop") +#else +# define LIMINE_DEPRECATED +# define LIMINE_DEPRECATED_IGNORE_START +# define LIMINE_DEPRECATED_IGNORE_END +#endif + +#define LIMINE_REQUESTS_START_MARKER \ + uint64_t limine_requests_start_marker[4] = { 0xf6b8f4b39de7d1ae, 0xfab91a6940fcb9cf, \ + 0x785c6ed015d3e316, 0x181e920a7852b9d9 }; +#define LIMINE_REQUESTS_END_MARKER \ + uint64_t limine_requests_end_marker[2] = { 0xadc0e0531bb10d03, 0x9572709f31764c62 }; + +#define LIMINE_REQUESTS_DELIMITER LIMINE_REQUESTS_END_MARKER + +#define LIMINE_BASE_REVISION(N) \ + uint64_t limine_base_revision[3] = { 0xf9562b2d5c95a6c8, 0x6a7b384944536bdc, (N) }; + +#define LIMINE_BASE_REVISION_SUPPORTED (limine_base_revision[2] == 0) + +#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b + +struct limine_uuid { + uint32_t a; + uint16_t b; + uint16_t c; + uint8_t d[8]; +}; + +#define LIMINE_MEDIA_TYPE_GENERIC 0 +#define LIMINE_MEDIA_TYPE_OPTICAL 1 +#define LIMINE_MEDIA_TYPE_TFTP 2 + +struct limine_file { + uint64_t revision; + LIMINE_PTR(void *) address; + uint64_t size; + LIMINE_PTR(char *) path; + LIMINE_PTR(char *) cmdline; + uint32_t media_type; + uint32_t unused; + uint32_t tftp_ip; + uint32_t tftp_port; + uint32_t partition_index; + uint32_t mbr_disk_id; + struct limine_uuid gpt_disk_uuid; + struct limine_uuid gpt_part_uuid; + struct limine_uuid part_uuid; +}; + +/* Boot info */ + +#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 } + +struct limine_bootloader_info_response { + uint64_t revision; + LIMINE_PTR(char *) name; + LIMINE_PTR(char *) version; +}; + +struct limine_bootloader_info_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_bootloader_info_response *) response; +}; + +/* Firmware type */ + +#define LIMINE_FIRMWARE_TYPE_REQUEST { LIMINE_COMMON_MAGIC, 0x8c2f75d90bef28a8, 0x7045a4688eac00c3 } + +#define LIMINE_FIRMWARE_TYPE_X86BIOS 0 +#define LIMINE_FIRMWARE_TYPE_UEFI32 1 +#define LIMINE_FIRMWARE_TYPE_UEFI64 2 + +struct limine_firmware_type_response { + uint64_t revision; + uint64_t firmware_type; +}; + +struct limine_firmware_type_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_firmware_type_response *) response; +}; + +/* Stack size */ + +#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d } + +struct limine_stack_size_response { + uint64_t revision; +}; + +struct limine_stack_size_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_stack_size_response *) response; + uint64_t stack_size; +}; + +/* HHDM */ + +#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b } + +struct limine_hhdm_response { + uint64_t revision; + uint64_t offset; +}; + +struct limine_hhdm_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_hhdm_response *) response; +}; + +/* Framebuffer */ + +#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b } + +#define LIMINE_FRAMEBUFFER_RGB 1 + +struct limine_video_mode { + uint64_t pitch; + uint64_t width; + uint64_t height; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; +}; + +struct limine_framebuffer { + LIMINE_PTR(void *) address; + uint64_t width; + uint64_t height; + uint64_t pitch; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; + uint8_t unused[7]; + uint64_t edid_size; + LIMINE_PTR(void *) edid; + /* Response revision 1 */ + uint64_t mode_count; + LIMINE_PTR(struct limine_video_mode **) modes; +}; + +struct limine_framebuffer_response { + uint64_t revision; + uint64_t framebuffer_count; + LIMINE_PTR(struct limine_framebuffer **) framebuffers; +}; + +struct limine_framebuffer_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_framebuffer_response *) response; +}; + +/* Terminal */ + +#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 } + +#define LIMINE_TERMINAL_CB_DEC 10 +#define LIMINE_TERMINAL_CB_BELL 20 +#define LIMINE_TERMINAL_CB_PRIVATE_ID 30 +#define LIMINE_TERMINAL_CB_STATUS_REPORT 40 +#define LIMINE_TERMINAL_CB_POS_REPORT 50 +#define LIMINE_TERMINAL_CB_KBD_LEDS 60 +#define LIMINE_TERMINAL_CB_MODE 70 +#define LIMINE_TERMINAL_CB_LINUX 80 + +#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1)) +#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2)) +#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3)) +#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4)) + +/* Response revision 1 */ +#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10)) +#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11)) + +#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2) +#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6) +#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7) + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_terminal; + +typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t); +typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t); + +struct LIMINE_DEPRECATED limine_terminal { + uint64_t columns; + uint64_t rows; + LIMINE_PTR(struct limine_framebuffer *) framebuffer; +}; + +struct LIMINE_DEPRECATED limine_terminal_response { + uint64_t revision; + uint64_t terminal_count; + LIMINE_PTR(struct limine_terminal **) terminals; + LIMINE_PTR(limine_terminal_write) write; +}; + +struct LIMINE_DEPRECATED limine_terminal_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_terminal_response *) response; + LIMINE_PTR(limine_terminal_callback) callback; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* Paging mode */ + +#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a } + +#if defined (__x86_64__) || defined (__i386__) +#define LIMINE_PAGING_MODE_X86_64_4LVL 0 +#define LIMINE_PAGING_MODE_X86_64_5LVL 1 +#define LIMINE_PAGING_MODE_MIN LIMINE_PAGING_MODE_X86_64_4LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL +#elif defined (__aarch64__) +#define LIMINE_PAGING_MODE_AARCH64_4LVL 0 +#define LIMINE_PAGING_MODE_AARCH64_5LVL 1 +#define LIMINE_PAGING_MODE_MIN LIMINE_PAGING_MODE_AARCH64_4LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL +#elif defined (__riscv) && (__riscv_xlen == 64) +#define LIMINE_PAGING_MODE_RISCV_SV39 0 +#define LIMINE_PAGING_MODE_RISCV_SV48 1 +#define LIMINE_PAGING_MODE_RISCV_SV57 2 +#define LIMINE_PAGING_MODE_MIN LIMINE_PAGING_MODE_RISCV_SV39 +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48 +#elif defined (__loongarch__) && (__loongarch_grlen == 64) +#define LIMINE_PAGING_MODE_LOONGARCH64_4LVL 0 +#define LIMINE_PAGING_MODE_MIN LIMINE_PAGING_MODE_LOONGARCH64_4LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_LOONGARCH64_4LVL +#else +#error Unknown architecture +#endif + +struct limine_paging_mode_response { + uint64_t revision; + uint64_t mode; +}; + +struct limine_paging_mode_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_paging_mode_response *) response; + uint64_t mode; + uint64_t max_mode; + uint64_t min_mode; +}; + +/* 5-level paging */ + +#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 } + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_5_level_paging_response { + uint64_t revision; +}; + +struct LIMINE_DEPRECATED limine_5_level_paging_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_5_level_paging_response *) response; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* SMP */ + +#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 } + +struct limine_smp_info; + +typedef void (*limine_goto_address)(struct limine_smp_info *); + +#if defined (__x86_64__) || defined (__i386__) + +#define LIMINE_SMP_X2APIC (1 << 0) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t lapic_id; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint32_t flags; + uint32_t bsp_lapic_id; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__aarch64__) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t reserved1; + uint64_t mpidr; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_mpidr; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__riscv) && (__riscv_xlen == 64) + +struct limine_smp_info { + uint64_t processor_id; + uint64_t hartid; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_hartid; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__loongarch__) && (__loongarch_grlen == 64) + +struct limine_smp_info { + uint64_t reserved; +}; + +struct limine_smp_response { + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#else +#error Unknown architecture +#endif + +struct limine_smp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smp_response *) response; + uint64_t flags; +}; + +/* Memory map */ + +#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 } + +#define LIMINE_MEMMAP_USABLE 0 +#define LIMINE_MEMMAP_RESERVED 1 +#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2 +#define LIMINE_MEMMAP_ACPI_NVS 3 +#define LIMINE_MEMMAP_BAD_MEMORY 4 +#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5 +#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6 +#define LIMINE_MEMMAP_FRAMEBUFFER 7 + +struct limine_memmap_entry { + uint64_t base; + uint64_t length; + uint64_t type; +}; + +struct limine_memmap_response { + uint64_t revision; + uint64_t entry_count; + LIMINE_PTR(struct limine_memmap_entry **) entries; +}; + +struct limine_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_memmap_response *) response; +}; + +/* Entry point */ + +#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a } + +typedef void (*limine_entry_point)(void); + +struct limine_entry_point_response { + uint64_t revision; +}; + +struct limine_entry_point_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_entry_point_response *) response; + LIMINE_PTR(limine_entry_point) entry; +}; + +/* Kernel File */ + +#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 } + +struct limine_kernel_file_response { + uint64_t revision; + LIMINE_PTR(struct limine_file *) kernel_file; +}; + +struct limine_kernel_file_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_file_response *) response; +}; + +/* Module */ + +#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee } + +#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) +#define LIMINE_INTERNAL_MODULE_COMPRESSED (1 << 1) + +struct limine_internal_module { + LIMINE_PTR(const char *) path; + LIMINE_PTR(const char *) cmdline; + uint64_t flags; +}; + +struct limine_module_response { + uint64_t revision; + uint64_t module_count; + LIMINE_PTR(struct limine_file **) modules; +}; + +struct limine_module_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_module_response *) response; + + /* Request revision 1 */ + uint64_t internal_module_count; + LIMINE_PTR(struct limine_internal_module **) internal_modules; +}; + +/* RSDP */ + +#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c } + +struct limine_rsdp_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_rsdp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_rsdp_response *) response; +}; + +/* SMBIOS */ + +#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee } + +struct limine_smbios_response { + uint64_t revision; + LIMINE_PTR(void *) entry_32; + LIMINE_PTR(void *) entry_64; +}; + +struct limine_smbios_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smbios_response *) response; +}; + +/* EFI system table */ + +#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc } + +struct limine_efi_system_table_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_efi_system_table_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_system_table_response *) response; +}; + +/* EFI memory map */ + +#define LIMINE_EFI_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x7df62a431d6872d5, 0xa4fcdfb3e57306c8 } + +struct limine_efi_memmap_response { + uint64_t revision; + LIMINE_PTR(void *) memmap; + uint64_t memmap_size; + uint64_t desc_size; + uint64_t desc_version; +}; + +struct limine_efi_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_memmap_response *) response; +}; + +/* Boot time */ + +#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 } + +struct limine_boot_time_response { + uint64_t revision; + int64_t boot_time; +}; + +struct limine_boot_time_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_boot_time_response *) response; +}; + +/* Kernel address */ + +#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 } + +struct limine_kernel_address_response { + uint64_t revision; + uint64_t physical_base; + uint64_t virtual_base; +}; + +struct limine_kernel_address_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_address_response *) response; +}; + +/* Device Tree Blob */ + +#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 } + +struct limine_dtb_response { + uint64_t revision; + LIMINE_PTR(void *) dtb_ptr; +}; + +struct limine_dtb_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_dtb_response *) response; +}; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/include/lock.h b/src/include/lock.h new file mode 100644 index 0000000..18ef1cf --- /dev/null +++ b/src/include/lock.h @@ -0,0 +1,9 @@ +#include + +#ifndef SPINLOCK_H +#define SPINLOCK_H + +void acquire_lock(atomic_flag *lock); +void free_lock(atomic_flag *lock); + +#endif \ No newline at end of file diff --git a/src/include/stdio.h b/src/include/stdio.h new file mode 100644 index 0000000..2f1fdf8 --- /dev/null +++ b/src/include/stdio.h @@ -0,0 +1,41 @@ +#include +#include "../flanterm/src/flanterm.h" + +enum { + LOG_INFO = 0, + LOG_WARN, + LOG_ERROR, + LOG_SUCCESS, +}; + +void klog(int level, const char *func, const char *msg); + +int kprintf(const char *format_string, ...); + +int serial_kprintf(const char *format_string, ...); + +void print_char(struct flanterm_context *ft_ctx, char c); +void print_str(struct flanterm_context *ft_ctx, char *str); +void print_int(struct flanterm_context *ft_ctx, uint64_t i); +void print_hex(struct flanterm_context *ft_ctx, uint64_t num); +void print_bin(struct flanterm_context *ft_ctx, uint64_t num); + +void serial_print_char(char c); +void serial_print_int(uint64_t i); +void serial_print_hex(uint64_t num); +void serial_print_bin(uint64_t num); + +void kernel_framebuffer_print(char *buffer, size_t n); +void kernel_serial_print(char *buffer, size_t n); + +char toupper(char c); +char dtoc(int digit); + + +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" +#define ANSI_COLOR_RESET "\x1b[0m" \ No newline at end of file diff --git a/src/include/string.h b/src/include/string.h new file mode 100644 index 0000000..665ae57 --- /dev/null +++ b/src/include/string.h @@ -0,0 +1,16 @@ +#ifndef STRING_H +#define STRING_H + +#include + +void *memset(void *addr, int c, uint64_t n); + +void *memcpy(void *dest, void *src, uint64_t n); + +void *memmove(void *dest, const void *src, uint64_t n); + +int memcmp(const void *s1, const void *s2, uint64_t n); + +uint64_t strlen(const char* str); + +#endif \ No newline at end of file diff --git a/src/include/uacpi/acpi.h b/src/include/uacpi/acpi.h new file mode 100644 index 0000000..3f2e629 --- /dev/null +++ b/src/include/uacpi/acpi.h @@ -0,0 +1,1430 @@ +#pragma once + +#include +#include +#include + +/* + * ----------------------------------------------------- + * Common structures provided by the ACPI specification + * ----------------------------------------------------- + */ + +#define ACPI_RSDP_SIGNATURE "RSD PTR " +#define ACPI_RSDT_SIGNATURE "RSDT" +#define ACPI_XSDT_SIGNATURE "XSDT" +#define ACPI_MADT_SIGNATURE "APIC" +#define ACPI_FADT_SIGNATURE "FACP" +#define ACPI_FACS_SIGNATURE "FACS" +#define ACPI_MCFG_SIGNATURE "MCFG" +#define ACPI_HPET_SIGNATURE "HPET" +#define ACPI_SRAT_SIGNATURE "SRAT" +#define ACPI_SLIT_SIGNATURE "SLIT" +#define ACPI_DSDT_SIGNATURE "DSDT" +#define ACPI_SSDT_SIGNATURE "SSDT" +#define ACPI_PSDT_SIGNATURE "PSDT" +#define ACPI_ECDT_SIGNATURE "ECDT" +#define ACPI_RHCT_SIGNATURE "RHCT" + +#define ACPI_AS_ID_SYS_MEM 0x00 +#define ACPI_AS_ID_SYS_IO 0x01 +#define ACPI_AS_ID_PCI_CFG_SPACE 0x02 +#define ACPI_AS_ID_EC 0x03 +#define ACPI_AS_ID_SMBUS 0x04 +#define ACPI_AS_ID_SYS_CMOS 0x05 +#define ACPI_AS_ID_PCI_BAR_TGT 0x06 +#define ACPI_AS_ID_IPMI 0x07 +#define ACPI_AS_ID_GP_IO 0x08 +#define ACPI_AS_ID_GENERIC_SBUS 0x09 +#define ACPI_AS_ID_PCC 0x0A +#define ACPI_AS_ID_FFH 0x7F +#define ACPI_AS_ID_OEM_BASE 0xC0 +#define ACPI_AS_ID_OEM_END 0xFF + +#define ACPI_ACCESS_UD 0 +#define ACPI_ACCESS_BYTE 1 +#define ACPI_ACCESS_WORD 2 +#define ACPI_ACCESS_DWORD 3 +#define ACPI_ACCESS_QWORD 4 + +UACPI_PACKED(struct acpi_gas { + uacpi_u8 address_space_id; + uacpi_u8 register_bit_width; + uacpi_u8 register_bit_offset; + uacpi_u8 access_size; + uacpi_u64 address; +}) +UACPI_EXPECT_SIZEOF(struct acpi_gas, 12); + +UACPI_PACKED(struct acpi_rsdp { + uacpi_char signature[8]; + uacpi_u8 checksum; + uacpi_char oemid[6]; + uacpi_u8 revision; + uacpi_u32 rsdt_addr; + + // vvvv available if .revision >= 2.0 only + uacpi_u32 length; + uacpi_u64 xsdt_addr; + uacpi_u8 extended_checksum; + uacpi_u8 rsvd[3]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rsdp, 36); + +UACPI_PACKED(struct acpi_sdt_hdr { + uacpi_char signature[4]; + uacpi_u32 length; + uacpi_u8 revision; + uacpi_u8 checksum; + uacpi_char oemid[6]; + uacpi_char oem_table_id[8]; + uacpi_u32 oem_revision; + uacpi_u32 creator_id; + uacpi_u32 creator_revision; +}) +UACPI_EXPECT_SIZEOF(struct acpi_sdt_hdr, 36); + +UACPI_PACKED(struct acpi_rsdt { + struct acpi_sdt_hdr hdr; + uacpi_u32 entries[]; +}) + +UACPI_PACKED(struct acpi_xsdt { + struct acpi_sdt_hdr hdr; + uacpi_u64 entries[]; +}) + +UACPI_PACKED(struct acpi_entry_hdr { + /* + * - acpi_madt_entry_type for the APIC table + * - acpi_srat_entry_type for the SRAT table + */ + uacpi_u8 type; + uacpi_u8 length; +}) + +// acpi_madt->flags +#define ACPI_PCAT_COMPAT (1 << 0) + +enum acpi_madt_entry_type { + ACPI_MADT_ENTRY_TYPE_LAPIC = 0, + ACPI_MADT_ENTRY_TYPE_IOAPIC = 1, + ACPI_MADT_ENTRY_TYPE_INTERRUPT_SOURCE_OVERRIDE = 2, + ACPI_MADT_ENTRY_TYPE_NMI_SOURCE = 3, + ACPI_MADT_ENTRY_TYPE_LAPIC_NMI = 4, + ACPI_MADT_ENTRY_TYPE_LAPIC_ADDRESS_OVERRIDE = 5, + ACPI_MADT_ENTRY_TYPE_IOSAPIC = 6, + ACPI_MADT_ENTRY_TYPE_LSAPIC = 7, + ACPI_MADT_ENTRY_TYPE_PLATFORM_INTERRUPT_SOURCES = 8, + ACPI_MADT_ENTRY_TYPE_LOCAL_X2APIC = 9, + ACPI_MADT_ENTRY_TYPE_LOCAL_X2APIC_NMI = 0xA, + ACPI_MADT_ENTRY_TYPE_GICC = 0xB, + ACPI_MADT_ENTRY_TYPE_GICD = 0xC, + ACPI_MADT_ENTRY_TYPE_GIC_MSI_FRAME = 0xD, + ACPI_MADT_ENTRY_TYPE_GICR = 0xE, + ACPI_MADT_ENTRY_TYPE_GIC_ITS = 0xF, + ACPI_MADT_ENTRY_TYPE_MULTIPROCESSOR_WAKEUP = 0x10, + ACPI_MADT_ENTRY_TYPE_CORE_PIC = 0x11, + ACPI_MADT_ENTRY_TYPE_LIO_PIC = 0x12, + ACPI_MADT_ENTRY_TYPE_HT_PIC = 0x13, + ACPI_MADT_ENTRY_TYPE_EIO_PIC = 0x14, + ACPI_MADT_ENTRY_TYPE_MSI_PIC = 0x15, + ACPI_MADT_ENTRY_TYPE_BIO_PIC = 0x16, + ACPI_MADT_ENTRY_TYPE_LPC_PIC = 0x17, + ACPI_MADT_ENTRY_TYPE_RINTC = 0x18, + ACPI_MADT_ENTRY_TYPE_IMSIC = 0x19, + ACPI_MADT_ENTRY_TYPE_APLIC = 0x1A, + ACPI_MADT_ENTRY_TYPE_PLIC = 0x1B, + ACPI_MADT_ENTRY_TYPE_RESERVED = 0x1C, // 0x1C..0x7F + ACPI_MADT_ENTRY_TYPE_OEM = 0x80, // 0x80..0xFF +}; + +UACPI_PACKED(struct acpi_madt { + struct acpi_sdt_hdr hdr; + uacpi_u32 local_interrupt_controller_address; + uacpi_u32 flags; + struct acpi_entry_hdr entries[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt, 44); + +/* + * - acpi_madt_lapic->flags + * - acpi_madt_lsapic->flags + * - acpi_madt_x2apic->flags + */ +#define ACPI_PIC_ENABLED (1 << 0) +#define ACPI_PIC_ONLINE_CAPABLE (1 << 1) + +UACPI_PACKED(struct acpi_madt_lapic { + struct acpi_entry_hdr hdr; + uacpi_u8 uid; + uacpi_u8 id; + uacpi_u32 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_lapic, 8); + +UACPI_PACKED(struct acpi_madt_ioapic { + struct acpi_entry_hdr hdr; + uacpi_u8 id; + uacpi_u8 rsvd; + uacpi_u32 address; + uacpi_u32 gsi_base; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_ioapic, 12); + +/* + * - acpi_madt_interrupt_source_override->flags + * - acpi_madt_nmi_source->flags + * - acpi_madt_lapic_nmi->flags + * - acpi_madt_platform_interrupt_source->flags + * - acpi_madt_x2apic_nmi->flags + */ +#define ACPI_MADT_POLARITY_MASK 0b11 +#define ACPI_MADT_POLARITY_CONFORMING 0b00 +#define ACPI_MADT_POLARITY_ACTIVE_HIGH 0b01 +#define ACPI_MADT_POLARITY_ACTIVE_LOW 0b11 + +#define ACPI_MADT_TRIGGERING_MASK 0b1100 +#define ACPI_MADT_TRIGGERING_CONFORMING 0b0000 +#define ACPI_MADT_TRIGGERING_EDGE 0b0100 +#define ACPI_MADT_TRIGGERING_LEVEL 0b1100 + +UACPI_PACKED(struct acpi_madt_interrupt_source_override { + struct acpi_entry_hdr hdr; + uacpi_u8 bus; + uacpi_u8 source; + uacpi_u32 gsi; + uacpi_u16 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_interrupt_source_override, 10); + +UACPI_PACKED(struct acpi_madt_nmi_source { + struct acpi_entry_hdr hdr; + uacpi_u16 flags; + uacpi_u32 gsi; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_nmi_source, 8); + +UACPI_PACKED(struct acpi_madt_lapic_nmi { + struct acpi_entry_hdr hdr; + uacpi_u8 uid; + uacpi_u16 flags; + uacpi_u8 lint; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_lapic_nmi, 6); + +UACPI_PACKED(struct acpi_madt_lapic_address_override { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd; + uacpi_u64 address; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_lapic_address_override, 12); + +UACPI_PACKED(struct acpi_madt_iosapic { + struct acpi_entry_hdr hdr; + uacpi_u8 id; + uacpi_u8 rsvd; + uacpi_u32 gsi_base; + uacpi_u64 address; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_iosapic, 16); + +UACPI_PACKED(struct acpi_madt_lsapic { + struct acpi_entry_hdr hdr; + uacpi_u8 acpi_id; + uacpi_u8 id; + uacpi_u8 eid; + uacpi_u8 reserved[3]; + uacpi_u32 flags; + uacpi_u32 uid; + uacpi_char uid_string[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_lsapic, 16); + +// acpi_madt_platform_interrupt_source->platform_flags +#define ACPI_CPEI_PROCESSOR_OVERRIDE (1 << 0) + +UACPI_PACKED(struct acpi_madt_platform_interrupt_source { + struct acpi_entry_hdr hdr; + uacpi_u16 flags; + uacpi_u8 type; + uacpi_u8 processor_id; + uacpi_u8 processor_eid; + uacpi_u8 iosapic_vector; + uacpi_u32 gsi; + uacpi_u32 platform_flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_platform_interrupt_source, 16); + +UACPI_PACKED(struct acpi_madt_x2apic { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd; + uacpi_u32 id; + uacpi_u32 flags; + uacpi_u32 uid; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_x2apic, 16); + +UACPI_PACKED(struct acpi_madt_x2apic_nmi { + struct acpi_entry_hdr hdr; + uacpi_u16 flags; + uacpi_u32 uid; + uacpi_u8 lint; + uacpi_u8 reserved[3]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_x2apic_nmi, 12); + +// acpi_madt_gicc->flags +#define ACPI_GICC_ENABLED (1 << 0) +#define ACPI_GICC_PERF_INTERRUPT_MODE (1 << 1) +#define ACPI_GICC_VGIC_MAINTENANCE_INTERRUPT_MODE (1 << 2) +#define ACPI_GICC_ONLINE_CAPABLE (1 << 3) + +// ACPI_GICC_*_INTERRUPT_MODE +#define ACPI_GICC_TRIGGERING_EDGE 1 +#define ACPI_GICC_TRIGGERING_LEVEL 0 + +UACPI_PACKED(struct acpi_madt_gicc { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd0; + uacpi_u32 interface_number; + uacpi_u32 acpi_id; + uacpi_u32 flags; + uacpi_u32 parking_protocol_version; + uacpi_u32 perf_interrupt_gsiv; + uacpi_u64 parked_address; + uacpi_u64 address; + uacpi_u64 gicv; + uacpi_u64 gich; + uacpi_u32 vgic_maitenante_interrupt; + uacpi_u64 gicr_base_address; + uacpi_u64 mpidr; + uacpi_u8 power_efficiency_class; + uacpi_u8 rsvd1; + uacpi_u16 spe_overflow_interrupt; + uacpi_u16 trbe_interrupt; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_gicc, 82); + +UACPI_PACKED(struct acpi_madt_gicd { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd0; + uacpi_u32 id; + uacpi_u64 address; + uacpi_u32 system_vector_base; + uacpi_u8 gic_version; + uacpi_u8 reserved1[3]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_gicd, 24); + +// acpi_madt_gic_msi_frame->flags +#define ACPI_SPI_SELECT (1 << 0) + +UACPI_PACKED(struct acpi_madt_gic_msi_frame { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd; + uacpi_u32 id; + uacpi_u64 address; + uacpi_u32 flags; + uacpi_u16 spi_count; + uacpi_u16 spi_base; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_gic_msi_frame, 24); + +UACPI_PACKED(struct acpi_madt_gicr { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd; + uacpi_u64 address; + uacpi_u32 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_gicr, 16); + +UACPI_PACKED(struct acpi_madt_gic_its { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd0; + uacpi_u32 id; + uacpi_u64 address; + uacpi_u32 rsvd1; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_gic_its, 20); + +UACPI_PACKED(struct acpi_madt_multiprocessor_wakeup { + struct acpi_entry_hdr hdr; + uacpi_u16 mailbox_version; + uacpi_u32 rsvd; + uacpi_u64 mailbox_address; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_multiprocessor_wakeup, 16); + +#define ACPI_CORE_PIC_ENABLED (1 << 0) + +UACPI_PACKED(struct acpi_madt_core_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u32 acpi_id; + uacpi_u32 id; + uacpi_u32 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_core_pic, 15); + +UACPI_PACKED(struct acpi_madt_lio_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u64 address; + uacpi_u16 size; + uacpi_u16 cascade_vector; + uacpi_u64 cascade_vector_mapping; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_lio_pic, 23); + +UACPI_PACKED(struct acpi_madt_ht_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u64 address; + uacpi_u16 size; + uacpi_u64 cascade_vector; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_ht_pic, 21); + +UACPI_PACKED(struct acpi_madt_eio_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u8 cascade_vector; + uacpi_u8 node; + uacpi_u64 node_map; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_eio_pic, 13); + +UACPI_PACKED(struct acpi_madt_msi_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u64 address; + uacpi_u32 start; + uacpi_u32 count; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_msi_pic, 19); + +UACPI_PACKED(struct acpi_madt_bio_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u64 address; + uacpi_u16 size; + uacpi_u16 hardware_id; + uacpi_u16 gsi_base; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_bio_pic, 17); + +UACPI_PACKED(struct acpi_madt_lpc_pic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u64 address; + uacpi_u16 size; + uacpi_u16 cascade_vector; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_lpc_pic, 15); + +UACPI_PACKED(struct acpi_madt_rintc { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u8 rsvd; + uacpi_u32 flags; + uacpi_u64 hart_id; + uacpi_u32 uid; + uacpi_u32 ext_intc_id; + uacpi_u64 address; + uacpi_u32 size; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_rintc, 36); + +UACPI_PACKED(struct acpi_madt_imsic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u8 rsvd; + uacpi_u32 flags; + uacpi_u16 num_ids; + uacpi_u16 num_guest_ids; + uacpi_u8 guest_index_bits; + uacpi_u8 hart_index_bits; + uacpi_u8 group_index_bits; + uacpi_u8 group_index_shift; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_imsic, 16); + +UACPI_PACKED(struct acpi_madt_aplic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u8 id; + uacpi_u32 flags; + uacpi_u64 hardware_id; + uacpi_u16 idc_count; + uacpi_u16 sources_count; + uacpi_u32 gsi_base; + uacpi_u64 address; + uacpi_u32 size; +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_aplic, 36); + +UACPI_PACKED(struct acpi_madt_plic { + struct acpi_entry_hdr hdr; + uacpi_u8 version; + uacpi_u8 id; + uacpi_u64 hardware_id; + uacpi_u16 sources_count; + uacpi_u16 max_priority; + uacpi_u32 flags; + uacpi_u32 size; + uacpi_u64 address; + uacpi_u32 gsi_base; + +}) +UACPI_EXPECT_SIZEOF(struct acpi_madt_plic, 36); + +enum acpi_srat_entry_type { + ACPI_SRAT_ENTRY_TYPE_PROCESSOR_AFFINITY = 0, + ACPI_SRAT_ENTRY_TYPE_MEMORY_AFFINITY = 1, + ACPI_SRAT_ENTRY_TYPE_X2APIC_AFFINITY = 2, + ACPI_SRAT_ENTRY_TYPE_GICC_AFFINITY = 3, + ACPI_SRAT_ENTRY_TYPE_GIC_ITS_AFFINITY = 4, + ACPI_SRAT_ENTRY_TYPE_GENERIC_INITIATOR_AFFINITY = 5, + ACPI_SRAT_ENTRY_TYPE_GENERIC_PORT_AFFINITY = 6, + ACPI_SRAT_ENTRY_TYPE_RINTC_AFFINITY = 7, +}; + +UACPI_PACKED(struct acpi_srat { + struct acpi_sdt_hdr hdr; + uacpi_u32 rsvd0; + uacpi_u64 rsvd1; + struct acpi_entry_hdr entries[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat, 48); + +/* + * acpi_srat_processor_affinity->flags + * acpi_srat_x2apic_affinity->flags + */ +#define ACPI_SRAT_PROCESSOR_ENABLED (1 << 0) + +UACPI_PACKED(struct acpi_srat_processor_affinity { + struct acpi_entry_hdr hdr; + uacpi_u8 proximity_domain_low; + uacpi_u8 id; + uacpi_u32 flags; + uacpi_u8 eid; + uacpi_u8 proximity_domain_high[3]; + uacpi_u32 clock_domain; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_processor_affinity, 16); + +// acpi_srat_memory_affinity->flags +#define ACPI_SRAT_MEMORY_ENABLED (1 << 0) +#define ACPI_SRAT_MEMORY_HOTPLUGGABLE (1 << 1) +#define ACPI_SRAT_MEMORY_NON_VOLATILE (1 << 2) + +UACPI_PACKED(struct acpi_srat_memory_affinity { + struct acpi_entry_hdr hdr; + uacpi_u32 proximity_domain; + uacpi_u16 rsvd0; + uacpi_u64 address; + uacpi_u64 length; + uacpi_u32 rsvd1; + uacpi_u32 flags; + uacpi_u64 rsdv2; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_memory_affinity, 40); + +UACPI_PACKED(struct acpi_srat_x2apic_affinity { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd0; + uacpi_u32 proximity_domain; + uacpi_u32 id; + uacpi_u32 flags; + uacpi_u32 clock_domain; + uacpi_u32 rsvd1; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_x2apic_affinity, 24); + +// acpi_srat_gicc_affinity->flags +#define ACPI_SRAT_GICC_ENABLED (1 << 0) + +UACPI_PACKED(struct acpi_srat_gicc_affinity { + struct acpi_entry_hdr hdr; + uacpi_u32 proximity_domain; + uacpi_u32 uid; + uacpi_u32 flags; + uacpi_u32 clock_domain; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_gicc_affinity, 18); + +UACPI_PACKED(struct acpi_srat_gic_its_affinity { + struct acpi_entry_hdr hdr; + uacpi_u32 proximity_domain; + uacpi_u16 rsvd; + uacpi_u32 id; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_gic_its_affinity, 12); + +// acpi_srat_generic_affinity->flags +#define ACPI_GENERIC_AFFINITY_ENABLED (1 << 0) +#define ACPI_GENERIC_AFFINITY_ARCH_TRANSACTIONS (1 << 1) + +UACPI_PACKED(struct acpi_srat_generic_affinity { + struct acpi_entry_hdr hdr; + uacpi_u8 rsvd0; + uacpi_u8 handle_type; + uacpi_u32 proximity_domain; + uacpi_u8 handle[16]; + uacpi_u32 flags; + uacpi_u32 rsvd1; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_generic_affinity, 32); + +// acpi_srat_rintc_affinity->flags +#define ACPI_SRAT_RINTC_AFFINITY_ENABLED (1 << 0) + +UACPI_PACKED(struct acpi_srat_rintc_affinity { + struct acpi_entry_hdr hdr; + uacpi_u16 rsvd; + uacpi_u32 proximity_domain; + uacpi_u32 uid; + uacpi_u32 flags; + uacpi_u32 clock_domain; +}) +UACPI_EXPECT_SIZEOF(struct acpi_srat_rintc_affinity, 20); + +UACPI_PACKED(struct acpi_slit { + struct acpi_sdt_hdr hdr; + uacpi_u64 num_localities; + uacpi_u8 matrix[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_slit, 44); + +/* + * acpi_gtdt->el*_flags + * acpi_gtdt_timer_entry->physical_flags + * acpi_gtdt_timer_entry->virtual_flags + * acpi_gtdt_watchdog->flags + */ +#define ACPI_GTDT_TRIGGERING (1 << 0) +#define ACPI_GTDT_TRIGGERING_EDGE 1 +#define ACPI_GTDT_TRIGGERING_LEVEL 0 + +/* + * acpi_gtdt->el*_flags + * acpi_gtdt_timer_entry->physical_flags + * acpi_gtdt_timer_entry->virtual_flags + * acpi_gtdt_watchdog->flags + */ +#define ACPI_GTDT_POLARITY (1 << 1) +#define ACPI_GTDT_POLARITY_ACTIVE_LOW 1 +#define ACPI_GTDT_POLARITY_ACTIVE_HIGH 0 + +// acpi_gtdt->el*_flags +#define ACPI_GTDT_ALWAYS_ON_CAPABLE (1 << 2) + +UACPI_PACKED(struct acpi_gtdt { + struct acpi_sdt_hdr hdr; + uacpi_u64 cnt_control_base; + uacpi_u32 rsvd; + uacpi_u32 el1_secure_gsiv; + uacpi_u32 el1_secure_flags; + uacpi_u32 el1_non_secure_gsiv; + uacpi_u32 el1_non_secure_flags; + uacpi_u32 el1_virtual_gsiv; + uacpi_u32 el1_virtual_flags; + uacpi_u32 el2_gsiv; + uacpi_u32 el2_flags; + uacpi_u64 cnt_read_base; + uacpi_u32 platform_timer_count; + uacpi_u32 platform_timer_offset; + + // revision >= 3 + uacpi_u32 el2_virtual_gsiv; + uacpi_u32 el2_virtual_flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_gtdt, 104); + +enum acpi_gtdt_entry_type { + ACPI_GTDT_ENTRY_TYPE_TIMER = 0, + ACPI_GTDT_ENTRY_TYPE_WATCHDOG = 1, +}; + +UACPI_PACKED(struct acpi_gtdt_entry_hdr { + uacpi_u8 type; + uacpi_u16 length; +}) + +UACPI_PACKED(struct acpi_gtdt_timer { + struct acpi_gtdt_entry_hdr hdr; + uacpi_u8 rsvd; + uacpi_u64 cnt_ctl_base; + uacpi_u32 timer_count; + uacpi_u32 timer_offset; +}) +UACPI_EXPECT_SIZEOF(struct acpi_gtdt_timer, 20); + +// acpi_gtdt_timer_entry->common_flags +#define ACPI_GTDT_TIMER_ENTRY_SECURE (1 << 0) +#define ACPI_GTDT_TIMER_ENTRY_ALWAYS_ON_CAPABLE (1 << 1) + +UACPI_PACKED(struct acpi_gtdt_timer_entry { + uacpi_u8 frame_number; + uacpi_u8 rsvd[3]; + uacpi_u64 cnt_base; + uacpi_u64 el0_cnt_base; + uacpi_u32 physical_gsiv; + uacpi_u32 physical_flags; + uacpi_u32 virtual_gsiv; + uacpi_u32 virtual_flags; + uacpi_u32 common_flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_gtdt_timer_entry, 40); + +// acpi_gtdt_watchdog->flags +#define ACPI_GTDT_WATCHDOG_SECURE (1 << 2) + +UACPI_PACKED(struct acpi_gtdt_watchdog { + struct acpi_gtdt_entry_hdr hdr; + uacpi_u8 rsvd; + uacpi_u64 refresh_frame; + uacpi_u64 control_frame; + uacpi_u32 gsiv; + uacpi_u32 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_gtdt_watchdog, 28); + +// acpi_fdt->iapc_flags +#define ACPI_IA_PC_LEGACY_DEVS (1 << 0) +#define ACPI_IA_PC_8042 (1 << 1) +#define ACPI_IA_PC_NO_VGA (1 << 2) +#define ACPI_IA_PC_NO_MSI (1 << 3) +#define ACPI_IA_PC_NO_PCIE_ASPM (1 << 4) +#define ACPI_IA_PC_NO_CMOS_RTC (1 << 5) + +// acpi_fdt->flags +#define ACPI_WBINVD (1 << 0) +#define ACPI_WBINVD_FLUSH (1 << 1) +#define ACPI_PROC_C1 (1 << 2) +#define ACPI_P_LVL2_UP (1 << 3) +#define ACPI_PWR_BUTTON (1 << 4) +#define ACPI_SLP_BUTTON (1 << 5) +#define ACPI_FIX_RTC (1 << 6) +#define ACPI_RTC_S4 (1 << 7) +#define ACPI_TMR_VAL_EXT (1 << 8) +#define ACPI_DCK_CAP (1 << 9) +#define ACPI_RESET_REG_SUP (1 << 10) +#define ACPI_SEALED_CASE (1 << 11) +#define ACPI_HEADLESS (1 << 12) +#define ACPI_CPU_SW_SLP (1 << 13) +#define ACPI_PCI_EXP_WAK (1 << 14) +#define ACPI_USE_PLATFORM_CLOCK (1 << 15) +#define ACPI_S4_RTC_STS_VALID (1 << 16) +#define ACPI_REMOTE_POWER_ON_CAPABLE (1 << 17) +#define ACPI_FORCE_APIC_CLUSTER_MODEL (1 << 18) +#define ACPI_FORCE_APIC_PHYS_DEST_MODE (1 << 19) +#define ACPI_HW_REDUCED_ACPI (1 << 20) +#define ACPI_LOW_POWER_S0_IDLE_CAPABLE (1 << 21) + +// acpi_fdt->arm_flags +#define ACPI_ARM_PSCI_COMPLIANT (1 << 0) +#define ACPI_ARM_PSCI_USE_HVC (1 << 1) + +UACPI_PACKED(struct acpi_fadt { + struct acpi_sdt_hdr hdr; + uacpi_u32 firmware_ctrl; + uacpi_u32 dsdt; + uacpi_u8 int_model; + uacpi_u8 preferred_pm_profile; + uacpi_u16 sci_int; + uacpi_u32 smi_cmd; + uacpi_u8 acpi_enable; + uacpi_u8 acpi_disable; + uacpi_u8 s4bios_req; + uacpi_u8 pstate_cnt; + uacpi_u32 pm1a_evt_blk; + uacpi_u32 pm1b_evt_blk; + uacpi_u32 pm1a_cnt_blk; + uacpi_u32 pm1b_cnt_blk; + uacpi_u32 pm2_cnt_blk; + uacpi_u32 pm_tmr_blk; + uacpi_u32 gpe0_blk; + uacpi_u32 gpe1_blk; + uacpi_u8 pm1_evt_len; + uacpi_u8 pm1_cnt_len; + uacpi_u8 pm2_cnt_len; + uacpi_u8 pm_tmr_len; + uacpi_u8 gpe0_blk_len; + uacpi_u8 gpe1_blk_len; + uacpi_u8 gpe1_base; + uacpi_u8 cst_cnt; + uacpi_u16 p_lvl2_lat; + uacpi_u16 p_lvl3_lat; + uacpi_u16 flush_size; + uacpi_u16 flush_stride; + uacpi_u8 duty_offset; + uacpi_u8 duty_width; + uacpi_u8 day_alrm; + uacpi_u8 mon_alrm; + uacpi_u8 century; + uacpi_u16 iapc_boot_arch; + uacpi_u8 rsvd; + uacpi_u32 flags; + struct acpi_gas reset_reg; + uacpi_u8 reset_value; + uacpi_u16 arm_boot_arch; + uacpi_u8 fadt_minor_verison; + uacpi_u64 x_firmware_ctrl; + uacpi_u64 x_dsdt; + struct acpi_gas x_pm1a_evt_blk; + struct acpi_gas x_pm1b_evt_blk; + struct acpi_gas x_pm1a_cnt_blk; + struct acpi_gas x_pm1b_cnt_blk; + struct acpi_gas x_pm2_cnt_blk; + struct acpi_gas x_pm_tmr_blk; + struct acpi_gas x_gpe0_blk; + struct acpi_gas x_gpe1_blk; + struct acpi_gas sleep_control_reg; + struct acpi_gas sleep_status_reg; + uacpi_u64 hypervisor_vendor_identity; +}) +UACPI_EXPECT_SIZEOF(struct acpi_fadt, 276); + +// acpi_facs->flags +#define ACPI_S4BIOS_F (1 << 0) +#define ACPI_64BIT_WAKE_SUPPORTED_F (1 << 1) + +// acpi_facs->ospm_flags +#define ACPI_64BIT_WAKE_F (1 << 0) + +struct acpi_facs { + uacpi_char signature[4]; + uacpi_u32 length; + uacpi_u32 hardware_signature; + uacpi_u32 firmware_waking_vector; + uacpi_u32 global_lock; + uacpi_u32 flags; + uacpi_u64 x_firmware_waking_vector; + uacpi_u8 version; + uacpi_char rsvd0[3]; + uacpi_u32 ospm_flags; + uacpi_char rsvd1[24]; +}; +UACPI_EXPECT_SIZEOF(struct acpi_facs, 64); + +UACPI_PACKED(struct acpi_mcfg_allocation { + uacpi_u64 address; + uacpi_u16 segment; + uacpi_u8 start_bus; + uacpi_u8 end_bus; + uacpi_u32 rsvd; +}) +UACPI_EXPECT_SIZEOF(struct acpi_mcfg_allocation, 16); + +UACPI_PACKED(struct acpi_mcfg { + struct acpi_sdt_hdr hdr; + uacpi_u64 rsvd; + struct acpi_mcfg_allocation entries[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_mcfg, 44); + +// acpi_hpet->block_id +#define ACPI_HPET_PCI_VENDOR_ID_SHIFT 16 +#define ACPI_HPET_LEGACY_REPLACEMENT_IRQ_ROUTING_CAPABLE (1 << 15) +#define ACPI_HPET_COUNT_SIZE_CAP (1 << 13) +#define ACPI_HPET_NUMBER_OF_COMPARATORS_SHIFT 8 +#define ACPI_HPET_NUMBER_OF_COMPARATORS_MASK 0b11111 +#define ACPI_HPET_HARDWARE_REV_ID_MASK 0b11111111 + +// acpi_hpet->flags +#define ACPI_HPET_PAGE_PROTECTION_MASK 0b11 +#define ACPI_HPET_PAGE_NO_PROTECTION 0 +#define ACPI_HPET_PAGE_4K_PROTECTED 1 +#define ACPI_HPET_PAGE_64K_PROTECTED 2 + +UACPI_PACKED(struct acpi_hpet { + struct acpi_sdt_hdr hdr; + uacpi_u32 block_id; + struct acpi_gas address; + uacpi_u8 number; + uacpi_u16 min_clock_tick; + uacpi_u8 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_hpet, 56); + +// PM1{a,b}_STS +#define ACPI_PM1_STS_TMR_STS_IDX 0 +#define ACPI_PM1_STS_BM_STS_IDX 4 +#define ACPI_PM1_STS_GBL_STS_IDX 5 +#define ACPI_PM1_STS_PWRBTN_STS_IDX 8 +#define ACPI_PM1_STS_SLPBTN_STS_IDX 9 +#define ACPI_PM1_STS_RTC_STS_IDX 10 +#define ACPI_PM1_STS_IGN0_IDX 11 +#define ACPI_PM1_STS_PCIEXP_WAKE_STS_IDX 14 +#define ACPI_PM1_STS_WAKE_STS_IDX 15 + +#define ACPI_PM1_STS_TMR_STS_MASK (1 << ACPI_PM1_STS_TMR_STS_IDX) +#define ACPI_PM1_STS_BM_STS_MASK (1 << ACPI_PM1_STS_BM_STS_IDX) +#define ACPI_PM1_STS_GBL_STS_MASK (1 << ACPI_PM1_STS_GBL_STS_IDX) +#define ACPI_PM1_STS_PWRBTN_STS_MASK (1 << ACPI_PM1_STS_PWRBTN_STS_IDX) +#define ACPI_PM1_STS_SLPBTN_STS_MASK (1 << ACPI_PM1_STS_SLPBTN_STS_IDX) +#define ACPI_PM1_STS_RTC_STS_MASK (1 << ACPI_PM1_STS_RTC_STS_IDX) +#define ACPI_PM1_STS_IGN0_MASK (1 << ACPI_PM1_STS_IGN0_IDX) +#define ACPI_PM1_STS_PCIEXP_WAKE_STS_MASK (1 << ACPI_PM1_STS_PCIEXP_WAKE_STS_IDX) +#define ACPI_PM1_STS_WAKE_STS_MASK (1 << ACPI_PM1_STS_WAKE_STS_IDX) + +#define ACPI_PM1_STS_CLEAR 1 + +// PM1{a,b}_EN +#define ACPI_PM1_EN_TMR_EN_IDX 0 +#define ACPI_PM1_EN_GBL_EN_IDX 5 +#define ACPI_PM1_EN_PWRBTN_EN_IDX 8 +#define ACPI_PM1_EN_SLPBTN_EN_IDX 9 +#define ACPI_PM1_EN_RTC_EN_IDX 10 +#define ACPI_PM1_EN_PCIEXP_WAKE_DIS_IDX 14 + +#define ACPI_PM1_EN_TMR_EN_MASK (1 << ACPI_PM1_EN_TMR_EN_IDX) +#define ACPI_PM1_EN_GBL_EN_MASK (1 << ACPI_PM1_EN_GBL_EN_IDX) +#define ACPI_PM1_EN_PWRBTN_EN_MASK (1 << ACPI_PM1_EN_PWRBTN_EN_IDX) +#define ACPI_PM1_EN_SLPBTN_EN_MASK (1 << ACPI_PM1_EN_SLPBTN_EN_IDX) +#define ACPI_PM1_EN_RTC_EN_MASK (1 << ACPI_PM1_EN_RTC_EN_IDX) +#define ACPI_PM1_EN_PCIEXP_WAKE_DIS_MASK (1 << ACPI_PM1_EN_PCIEXP_WAKE_DIS_IDX) + +// PM1{a,b}_CNT_BLK +#define ACPI_PM1_CNT_SCI_EN_IDX 0 +#define ACPI_PM1_CNT_BM_RLD_IDX 1 +#define ACPI_PM1_CNT_GBL_RLS_IDX 2 +#define ACPI_PM1_CNT_RSVD0_IDX 3 +#define ACPI_PM1_CNT_RSVD1_IDX 4 +#define ACPI_PM1_CNT_RSVD2_IDX 5 +#define ACPI_PM1_CNT_RSVD3_IDX 6 +#define ACPI_PM1_CNT_RSVD4_IDX 7 +#define ACPI_PM1_CNT_RSVD5_IDX 8 +#define ACPI_PM1_CNT_IGN0_IDX 9 +#define ACPI_PM1_CNT_SLP_TYP_IDX 10 +#define ACPI_PM1_CNT_SLP_EN_IDX 13 +#define ACPI_PM1_CNT_RSVD6_IDX 14 +#define ACPI_PM1_CNT_RSVD7_IDX 15 + +#define ACPI_SLP_TYP_MAX 0b111 + +#define ACPI_PM1_CNT_SCI_EN_MASK (1 << ACPI_PM1_CNT_SCI_EN_IDX) +#define ACPI_PM1_CNT_BM_RLD_MASK (1 << ACPI_PM1_CNT_BM_RLD_IDX) +#define ACPI_PM1_CNT_GBL_RLS_MASK (1 << ACPI_PM1_CNT_GBL_RLS_IDX) +#define ACPI_PM1_CNT_SLP_TYP_MASK (ACPI_SLP_TYP_MAX << ACPI_PM1_CNT_SLP_TYP_IDX) +#define ACPI_PM1_CNT_SLP_EN_MASK (1 << ACPI_PM1_CNT_SLP_EN_IDX) + +/* + * SCI_EN is not in this mask even though the spec says it must be preserved. + * This is because it's known to be bugged on some hardware that relies on + * software writing 1 to it after resume (as indicated by a similar comment in + * ACPICA) + */ +#define ACPI_PM1_CNT_PRESERVE_MASK ( \ + (1 << ACPI_PM1_CNT_RSVD0_IDX) | \ + (1 << ACPI_PM1_CNT_RSVD1_IDX) | \ + (1 << ACPI_PM1_CNT_RSVD2_IDX) | \ + (1 << ACPI_PM1_CNT_RSVD3_IDX) | \ + (1 << ACPI_PM1_CNT_RSVD4_IDX) | \ + (1 << ACPI_PM1_CNT_RSVD5_IDX) | \ + (1 << ACPI_PM1_CNT_IGN0_IDX ) | \ + (1 << ACPI_PM1_CNT_RSVD6_IDX) | \ + (1 << ACPI_PM1_CNT_RSVD7_IDX) \ +) + +// PM2_CNT +#define ACPI_PM2_CNT_ARB_DIS_IDX 0 +#define ACPI_PM2_CNT_ARB_DIS_MASK (1 << ACPI_PM2_CNT_ARB_DIS_IDX) + +// All bits are reserved but this first one +#define ACPI_PM2_CNT_PRESERVE_MASK (~((uacpi_u64)ACPI_PM2_CNT_ARB_DIS_MASK)) + +// SLEEP_CONTROL_REG +#define ACPI_SLP_CNT_RSVD0_IDX 0 +#define ACPI_SLP_CNT_IGN0_IDX 1 +#define ACPI_SLP_CNT_SLP_TYP_IDX 2 +#define ACPI_SLP_CNT_SLP_EN_IDX 5 +#define ACPI_SLP_CNT_RSVD1_IDX 6 +#define ACPI_SLP_CNT_RSVD2_IDX 7 + +#define ACPI_SLP_CNT_SLP_TYP_MASK (ACPI_SLP_TYP_MAX << ACPI_SLP_CNT_SLP_TYP_IDX) +#define ACPI_SLP_CNT_SLP_EN_MASK (1 << ACPI_SLP_CNT_SLP_EN_IDX) + +#define ACPI_SLP_CNT_PRESERVE_MASK ( \ + (1 << ACPI_SLP_CNT_RSVD0_IDX) | \ + (1 << ACPI_SLP_CNT_IGN0_IDX) | \ + (1 << ACPI_SLP_CNT_RSVD1_IDX) | \ + (1 << ACPI_SLP_CNT_RSVD2_IDX) \ +) + +// SLEEP_STATUS_REG +#define ACPI_SLP_STS_WAK_STS_IDX 7 + +#define ACPI_SLP_STS_WAK_STS_MASK (1 << ACPI_SLP_STS_WAK_STS_IDX) + +// All bits are reserved but this last one +#define ACPI_SLP_STS_PRESERVE_MASK (~((uacpi_u64)ACPI_SLP_STS_WAK_STS_MASK)) + +#define ACPI_SLP_STS_CLEAR 1 + +UACPI_PACKED(struct acpi_dsdt { + struct acpi_sdt_hdr hdr; + uacpi_u8 definition_block[]; +}) + +UACPI_PACKED(struct acpi_ssdt { + struct acpi_sdt_hdr hdr; + uacpi_u8 definition_block[]; +}) + +/* + * ACPI 6.5 specification: + * Bit [0] - Set if the device is present. + * Bit [1] - Set if the device is enabled and decoding its resources. + * Bit [2] - Set if the device should be shown in the UI. + * Bit [3] - Set if the device is functioning properly (cleared if device + * failed its diagnostics). + * Bit [4] - Set if the battery is present. + */ +#define ACPI_STA_RESULT_DEVICE_PRESENT (1 << 0) +#define ACPI_STA_RESULT_DEVICE_ENABLED (1 << 1) +#define ACPI_STA_RESULT_DEVICE_SHOWN_IN_UI (1 << 2) +#define ACPI_STA_RESULT_DEVICE_FUNCTIONING (1 << 3) +#define ACPI_STA_RESULT_DEVICE_BATTERY_PRESENT (1 << 4) + +#define ACPI_REG_DISCONNECT 0 +#define ACPI_REG_CONNECT 1 + +UACPI_PACKED(struct acpi_ecdt { + struct acpi_sdt_hdr hdr; + struct acpi_gas ec_control; + struct acpi_gas ec_data; + uacpi_u32 uid; + uacpi_u8 gpe_bit; + uacpi_char ec_id[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_ecdt, 65); + +UACPI_PACKED(struct acpi_rhct_hdr { + uacpi_u16 type; + uacpi_u16 length; + uacpi_u16 revision; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rhct_hdr, 6); + +// acpi_rhct->flags +#define ACPI_TIMER_CANNOT_WAKE_CPU (1 << 0) + +UACPI_PACKED(struct acpi_rhct { + struct acpi_sdt_hdr hdr; + uacpi_u32 flags; + uacpi_u64 timebase_frequency; + uacpi_u32 node_count; + uacpi_u32 nodes_offset; + struct acpi_rhct_hdr entries[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rhct, 56); + +enum acpi_rhct_entry_type { + ACPI_RHCT_ENTRY_TYPE_ISA_STRING = 0, + ACPI_RHCT_ENTRY_TYPE_CMO = 1, + ACPI_RHCT_ENTRY_TYPE_MMU = 2, + ACPI_RHCT_ENTRY_TYPE_HART_INFO = 65535, +}; + +UACPI_PACKED(struct acpi_rhct_isa_string { + struct acpi_rhct_hdr hdr; + uacpi_u16 length; + uacpi_u8 isa[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rhct_isa_string, 8); + +UACPI_PACKED(struct acpi_rhct_cmo { + struct acpi_rhct_hdr hdr; + uacpi_u8 rsvd; + uacpi_u8 cbom_size; + uacpi_u8 cbop_size; + uacpi_u8 cboz_size; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rhct_cmo, 10); + +enum acpi_rhct_mmu_type { + ACPI_RHCT_MMU_TYPE_SV39 = 0, + ACPI_RHCT_MMU_TYPE_SV48 = 1, + ACPI_RHCT_MMU_TYPE_SV57 = 2, +}; + +UACPI_PACKED(struct acpi_rhct_mmu { + struct acpi_rhct_hdr hdr; + uacpi_u8 rsvd; + uacpi_u8 type; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rhct_mmu, 8); + +UACPI_PACKED(struct acpi_rhct_hart_info { + struct acpi_rhct_hdr hdr; + uacpi_u16 offset_count; + uacpi_u32 uid; + uacpi_u32 offsets[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_rhct_hart_info, 12); + +#define ACPI_LARGE_ITEM (1 << 7) + +#define ACPI_SMALL_ITEM_NAME_IDX 3 +#define ACPI_SMALL_ITEM_NAME_MASK 0b1111 +#define ACPI_SMALL_ITEM_LENGTH_MASK 0b111 + +#define ACPI_LARGE_ITEM_NAME_MASK 0b1111111 + +// Small items +#define ACPI_RESOURCE_IRQ 0x04 +#define ACPI_RESOURCE_DMA 0x05 +#define ACPI_RESOURCE_START_DEPENDENT 0x06 +#define ACPI_RESOURCE_END_DEPENDENT 0x07 +#define ACPI_RESOURCE_IO 0x08 +#define ACPI_RESOURCE_FIXED_IO 0x09 +#define ACPI_RESOURCE_FIXED_DMA 0x0A +#define ACPI_RESOURCE_VENDOR_TYPE0 0x0E +#define ACPI_RESOURCE_END_TAG 0x0F + +// Large items +#define ACPI_RESOURCE_MEMORY24 0x01 +#define ACPI_RESOURCE_GENERIC_REGISTER 0x02 +#define ACPI_RESOURCE_VENDOR_TYPE1 0x04 +#define ACPI_RESOURCE_MEMORY32 0x05 +#define ACPI_RESOURCE_FIXED_MEMORY32 0x06 +#define ACPI_RESOURCE_ADDRESS32 0x07 +#define ACPI_RESOURCE_ADDRESS16 0x08 +#define ACPI_RESOURCE_EXTENDED_IRQ 0x09 +#define ACPI_RESOURCE_ADDRESS64 0x0A +#define ACPI_RESOURCE_ADDRESS64_EXTENDED 0x0B +#define ACPI_RESOURCE_GPIO_CONNECTION 0x0C +#define ACPI_RESOURCE_PIN_FUNCTION 0x0D +#define ACPI_RESOURCE_SERIAL_CONNECTION 0x0E +#define ACPI_RESOURCE_PIN_CONFIGURATION 0x0F +#define ACPI_RESOURCE_PIN_GROUP 0x10 +#define ACPI_RESOURCE_PIN_GROUP_FUNCTION 0x11 +#define ACPI_RESOURCE_PIN_GROUP_CONFIGURATION 0x12 +#define ACPI_RESOURCE_CLOCK_INPUT 0x13 + +/* + * Resources as encoded by the raw AML byte stream. + * For decode API & human usable structures refer to uacpi/resources.h + */ +UACPI_PACKED(struct acpi_small_item { + uacpi_u8 type_and_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_small_item, 1); + +UACPI_PACKED(struct acpi_resource_irq { + struct acpi_small_item common; + uacpi_u16 irq_mask; + uacpi_u8 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_irq, 4); + +UACPI_PACKED(struct acpi_resource_dma { + struct acpi_small_item common; + uacpi_u8 channel_mask; + uacpi_u8 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_dma, 3); + +UACPI_PACKED(struct acpi_resource_start_dependent { + struct acpi_small_item common; + uacpi_u8 flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_start_dependent, 2); + +UACPI_PACKED(struct acpi_resource_end_dependent { + struct acpi_small_item common; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_end_dependent, 1); + +UACPI_PACKED(struct acpi_resource_io { + struct acpi_small_item common; + uacpi_u8 information; + uacpi_u16 minimum; + uacpi_u16 maximum; + uacpi_u8 alignment; + uacpi_u8 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_io, 8); + +UACPI_PACKED(struct acpi_resource_fixed_io { + struct acpi_small_item common; + uacpi_u16 address; + uacpi_u8 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_fixed_io, 4); + +UACPI_PACKED(struct acpi_resource_fixed_dma { + struct acpi_small_item common; + uacpi_u16 request_line; + uacpi_u16 channel; + uacpi_u8 transfer_width; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_fixed_dma, 6); + +UACPI_PACKED(struct acpi_resource_vendor_defined_type0 { + struct acpi_small_item common; + uacpi_u8 byte_data[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_vendor_defined_type0, 1); + +UACPI_PACKED(struct acpi_resource_end_tag { + struct acpi_small_item common; + uacpi_u8 checksum; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_end_tag, 2); + +UACPI_PACKED(struct acpi_large_item { + uacpi_u8 type; + uacpi_u16 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_large_item, 3); + +UACPI_PACKED(struct acpi_resource_memory24 { + struct acpi_large_item common; + uacpi_u8 information; + uacpi_u16 minimum; + uacpi_u16 maximum; + uacpi_u16 alignment; + uacpi_u16 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_memory24, 12); + +UACPI_PACKED(struct acpi_resource_vendor_defined_type1 { + struct acpi_large_item common; + uacpi_u8 byte_data[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_vendor_defined_type1, 3); + +UACPI_PACKED(struct acpi_resource_memory32 { + struct acpi_large_item common; + uacpi_u8 information; + uacpi_u32 minimum; + uacpi_u32 maximum; + uacpi_u32 alignment; + uacpi_u32 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_memory32, 20); + +UACPI_PACKED(struct acpi_resource_fixed_memory32 { + struct acpi_large_item common; + uacpi_u8 information; + uacpi_u32 address; + uacpi_u32 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_fixed_memory32, 12); + +UACPI_PACKED(struct acpi_resource_address { + struct acpi_large_item common; + uacpi_u8 type; + uacpi_u8 flags; + uacpi_u8 type_flags; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_address, 6); + +UACPI_PACKED(struct acpi_resource_address64 { + struct acpi_resource_address common; + uacpi_u64 granularity; + uacpi_u64 minimum; + uacpi_u64 maximum; + uacpi_u64 translation_offset; + uacpi_u64 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_address64, 46); + +UACPI_PACKED(struct acpi_resource_address32 { + struct acpi_resource_address common; + uacpi_u32 granularity; + uacpi_u32 minimum; + uacpi_u32 maximum; + uacpi_u32 translation_offset; + uacpi_u32 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_address32, 26); + +UACPI_PACKED(struct acpi_resource_address16 { + struct acpi_resource_address common; + uacpi_u16 granularity; + uacpi_u16 minimum; + uacpi_u16 maximum; + uacpi_u16 translation_offset; + uacpi_u16 length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_address16, 16); + +UACPI_PACKED(struct acpi_resource_address64_extended { + struct acpi_resource_address common; + uacpi_u8 revision_id; + uacpi_u8 rsvd; + uacpi_u64 granularity; + uacpi_u64 minimum; + uacpi_u64 maximum; + uacpi_u64 translation_offset; + uacpi_u64 length; + uacpi_u64 attributes; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_address64_extended, 56); + +UACPI_PACKED(struct acpi_resource_extended_irq { + struct acpi_large_item common; + uacpi_u8 flags; + uacpi_u8 num_irqs; + uacpi_u32 irqs[]; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_extended_irq, 5); + +UACPI_PACKED(struct acpi_resource_generic_register { + struct acpi_large_item common; + uacpi_u8 address_space_id; + uacpi_u8 bit_width; + uacpi_u8 bit_offset; + uacpi_u8 access_size; + uacpi_u64 address; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_generic_register, 15); + +UACPI_PACKED(struct acpi_resource_gpio_connection { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u8 type; + uacpi_u16 general_flags; + uacpi_u16 connection_flags; + uacpi_u8 pull_configuration; + uacpi_u16 drive_strength; + uacpi_u16 debounce_timeout; + uacpi_u16 pin_table_offset; + uacpi_u8 source_index; + uacpi_u16 source_offset; + uacpi_u16 vendor_data_offset; + uacpi_u16 vendor_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_gpio_connection, 23); + +#define ACPI_SERIAL_TYPE_I2C 1 +#define ACPI_SERIAL_TYPE_SPI 2 +#define ACPI_SERIAL_TYPE_UART 3 +#define ACPI_SERIAL_TYPE_CSI2 4 +#define ACPI_SERIAL_TYPE_MAX ACPI_SERIAL_TYPE_CSI2 + +UACPI_PACKED(struct acpi_resource_serial { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u8 source_index; + uacpi_u8 type; + uacpi_u8 flags; + uacpi_u16 type_specific_flags; + uacpi_u8 type_specific_revision_id; + uacpi_u16 type_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_serial, 12); + +UACPI_PACKED(struct acpi_resource_serial_i2c { + struct acpi_resource_serial common; + uacpi_u32 connection_speed; + uacpi_u16 slave_address; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_serial_i2c, 18); + +UACPI_PACKED(struct acpi_resource_serial_spi { + struct acpi_resource_serial common; + uacpi_u32 connection_speed; + uacpi_u8 data_bit_length; + uacpi_u8 phase; + uacpi_u8 polarity; + uacpi_u16 device_selection; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_serial_spi, 21); + +UACPI_PACKED(struct acpi_resource_serial_uart { + struct acpi_resource_serial common; + uacpi_u32 baud_rate; + uacpi_u16 rx_fifo; + uacpi_u16 tx_fifo; + uacpi_u8 parity; + uacpi_u8 lines_enabled; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_serial_uart, 22); + +UACPI_PACKED(struct acpi_resource_serial_csi2 { + struct acpi_resource_serial common; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_serial_csi2, 12); + +UACPI_PACKED(struct acpi_resource_pin_function { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u16 flags; + uacpi_u8 pull_configuration; + uacpi_u16 function_number; + uacpi_u16 pin_table_offset; + uacpi_u8 source_index; + uacpi_u16 source_offset; + uacpi_u16 vendor_data_offset; + uacpi_u16 vendor_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_pin_function, 18); + +UACPI_PACKED(struct acpi_resource_pin_configuration { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u16 flags; + uacpi_u8 type; + uacpi_u32 value; + uacpi_u16 pin_table_offset; + uacpi_u8 source_index; + uacpi_u16 source_offset; + uacpi_u16 vendor_data_offset; + uacpi_u16 vendor_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_pin_configuration, 20); + +UACPI_PACKED(struct acpi_resource_pin_group { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u16 flags; + uacpi_u16 pin_table_offset; + uacpi_u16 source_lable_offset; + uacpi_u16 vendor_data_offset; + uacpi_u16 vendor_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_pin_group, 14); + +UACPI_PACKED(struct acpi_resource_pin_group_function { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u16 flags; + uacpi_u16 function; + uacpi_u8 source_index; + uacpi_u16 source_offset; + uacpi_u16 source_lable_offset; + uacpi_u16 vendor_data_offset; + uacpi_u16 vendor_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_pin_group_function, 17); + +UACPI_PACKED(struct acpi_resource_pin_group_configuration { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u16 flags; + uacpi_u8 type; + uacpi_u32 value; + uacpi_u8 source_index; + uacpi_u16 source_offset; + uacpi_u16 source_lable_offset; + uacpi_u16 vendor_data_offset; + uacpi_u16 vendor_data_length; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_pin_group_configuration, 20); + +UACPI_PACKED(struct acpi_resource_clock_input { + struct acpi_large_item common; + uacpi_u8 revision_id; + uacpi_u16 flags; + uacpi_u16 divisor; + uacpi_u32 numerator; + uacpi_u8 source_index; +}) +UACPI_EXPECT_SIZEOF(struct acpi_resource_clock_input, 13); diff --git a/src/include/uacpi/context.h b/src/include/uacpi/context.h new file mode 100644 index 0000000..ce76c9e --- /dev/null +++ b/src/include/uacpi/context.h @@ -0,0 +1,45 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Set the minimum log level to be accepted by the logging facilities. Any logs + * below this level are discarded and not passed to uacpi_kernel_log, etc. + * + * 0 is treated as a special value that resets the setting to the default value. + * + * E.g. for a log level of UACPI_LOG_INFO: + * UACPI_LOG_DEBUG -> discarded + * UACPI_LOG_TRACE -> discarded + * UACPI_LOG_INFO -> allowed + * UACPI_LOG_WARN -> allowed + * UACPI_LOG_ERROR -> allowed + */ +void uacpi_context_set_log_level(uacpi_log_level); + +/* + * Set the maximum number of seconds a While loop is allowed to run for before + * getting timed out. + * + * 0 is treated a special value that resets the setting to the default value. + */ +void uacpi_context_set_loop_timeout(uacpi_u32 seconds); + +/* + * Set the maximum call stack depth AML can reach before getting aborted. + * + * 0 is treated as a special value that resets the setting to the default value. + */ +void uacpi_context_set_max_call_stack_depth(uacpi_u32 depth); + +uacpi_u32 uacpi_context_get_loop_timeout(void); + +void uacpi_context_set_proactive_table_checksum(uacpi_bool); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/event.h b/src/include/uacpi/event.h new file mode 100644 index 0000000..7d005a9 --- /dev/null +++ b/src/include/uacpi/event.h @@ -0,0 +1,282 @@ +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum uacpi_fixed_event { + UACPI_FIXED_EVENT_TIMER_STATUS = 1, + UACPI_FIXED_EVENT_POWER_BUTTON, + UACPI_FIXED_EVENT_SLEEP_BUTTON, + UACPI_FIXED_EVENT_RTC, + UACPI_FIXED_EVENT_MAX = UACPI_FIXED_EVENT_RTC, +} uacpi_fixed_event; + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_install_fixed_event_handler( + uacpi_fixed_event event, uacpi_interrupt_handler handler, uacpi_handle user +)) + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_uninstall_fixed_event_handler( + uacpi_fixed_event event +)) + +/* + * Enable/disable a fixed event. Note that the event is automatically enabled + * upon installing a handler to it. + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_enable_fixed_event(uacpi_fixed_event event) +) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_disable_fixed_event(uacpi_fixed_event event) +) + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_clear_fixed_event(uacpi_fixed_event event) +) + +typedef enum uacpi_event_info { + // Event is enabled in software + UACPI_EVENT_INFO_ENABLED = (1 << 0), + + // Event is enabled in software (only for wake) + UACPI_EVENT_INFO_ENABLED_FOR_WAKE = (1 << 1), + + // Event is masked + UACPI_EVENT_INFO_MASKED = (1 << 2), + + // Event has a handler attached + UACPI_EVENT_INFO_HAS_HANDLER = (1 << 3), + + // Hardware enable bit is set + UACPI_EVENT_INFO_HW_ENABLED = (1 << 4), + + // Hardware status bit is set + UACPI_EVENT_INFO_HW_STATUS = (1 << 5), +} uacpi_event_info; + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_fixed_event_info( + uacpi_fixed_event event, uacpi_event_info *out_info +)) + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_gpe_info( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_event_info *out_info +)) + +// Set if the handler wishes to reenable the GPE it just handled +#define UACPI_GPE_REENABLE (1 << 7) + +typedef uacpi_interrupt_ret (*uacpi_gpe_handler)( + uacpi_handle ctx, uacpi_namespace_node *gpe_device, uacpi_u16 idx +); + +typedef enum uacpi_gpe_triggering { + UACPI_GPE_TRIGGERING_LEVEL = 0, + UACPI_GPE_TRIGGERING_EDGE = 1, + UACPI_GPE_TRIGGERING_MAX = UACPI_GPE_TRIGGERING_EDGE, +} uacpi_gpe_triggering; + +const uacpi_char *uacpi_gpe_triggering_to_string( + uacpi_gpe_triggering triggering +); + +/* + * Installs a handler to the provided GPE at 'idx' controlled by device + * 'gpe_device'. The GPE is automatically disabled & cleared according to the + * configured triggering upon invoking the handler. The event is optionally + * re-enabled (by returning UACPI_GPE_REENABLE from the handler) + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_install_gpe_handler( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_gpe_triggering triggering, uacpi_gpe_handler handler, uacpi_handle ctx +)) + +/* + * Installs a raw handler to the provided GPE at 'idx' controlled by device + * 'gpe_device'. The handler is dispatched immediately after the event is + * received, status & enable bits are untouched. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_install_gpe_handler_raw( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_gpe_triggering triggering, uacpi_gpe_handler handler, uacpi_handle ctx +)) + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_uninstall_gpe_handler( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, uacpi_gpe_handler handler +)) + +/* + * Marks the GPE 'idx' managed by 'gpe_device' as wake-capable. 'wake_device' is + * optional and configures the GPE to generate an implicit notification whenever + * an event occurs. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_setup_gpe_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_namespace_node *wake_device +)) + +/* + * Mark a GPE managed by 'gpe_device' as enabled/disabled for wake. The GPE must + * have previously been marked by calling uacpi_gpe_setup_for_wake. This + * function only affects the GPE enable register state following the call to + * uacpi_gpe_enable_all_for_wake. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_enable_gpe_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_disable_gpe_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) + +/* + * Finalize GPE initialization by enabling all GPEs not configured for wake and + * having a matching AML handler detected. + * + * This should be called after the kernel power managment subsystem has + * enumerated all of the devices, executing their _PRW methods etc., and + * marking those it wishes to use for wake by calling uacpi_setup_gpe_for_wake + * or uacpi_mark_gpe_for_wake. + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_finalize_gpe_initialization(void) +) + +/* + * Enable/disable a general purpose event managed by 'gpe_device'. Internally + * this uses reference counting to make sure a GPE is not disabled until all + * possible users of it do so. GPEs not marked for wake are enabled + * automatically so this API is only needed for wake events or those that don't + * have a corresponding AML handler. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_enable_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_disable_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) + +/* + * Clear the status bit of the event 'idx' managed by 'gpe_device'. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_clear_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) + +/* + * Suspend/resume a general purpose event managed by 'gpe_device'. This bypasses + * the reference counting mechanism and unconditionally clears/sets the + * corresponding bit in the enable registers. This is used for switching the GPE + * to poll mode. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_suspend_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_resume_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) + +/* + * Finish handling the GPE managed by 'gpe_device' at 'idx'. This clears the + * status registers if it hasn't been cleared yet and re-enables the event if + * it was enabled before. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_finish_handling_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) + +/* + * Hard mask/umask a general purpose event at 'idx' managed by 'gpe_device'. + * This is used to permanently silence an event so that further calls to + * enable/disable as well as suspend/resume get ignored. This might be necessary + * for GPEs that cause an event storm due to the kernel's inability to properly + * handle them. The only way to enable a masked event is by a call to unmask. + * + * NOTE: 'gpe_device' may be null for GPEs managed by \_GPE + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_mask_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_unmask_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +)) + +/* + * Disable all GPEs currently set up on the system. + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_disable_all_gpes(void) +) + +/* + * Enable all GPEs not marked as wake. This is only needed after the system + * wakes from a shallow sleep state and is called automatically by wake code. + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_enable_all_runtime_gpes(void) +) + +/* + * Enable all GPEs marked as wake. This is only needed before the system goes + * to sleep is called automatically by sleep code. + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_enable_all_wake_gpes(void) +) + +/* + * Install/uninstall a new GPE block, usually defined by a device in the + * namespace with a _HID of ACPI0006. + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_install_gpe_block( + uacpi_namespace_node *gpe_device, uacpi_u64 address, + uacpi_address_space address_space, uacpi_u16 num_registers, + uacpi_u32 irq +)) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_uninstall_gpe_block( + uacpi_namespace_node *gpe_device +)) + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/helpers.h b/src/include/uacpi/helpers.h new file mode 100644 index 0000000..f45c583 --- /dev/null +++ b/src/include/uacpi/helpers.h @@ -0,0 +1,16 @@ +#pragma once + +#ifdef __cplusplus +#define UACPI_STATIC_ASSERT static_assert +#else +#define UACPI_STATIC_ASSERT _Static_assert +#endif + +#define UACPI_BUILD_BUG_ON_WITH_MSG(expr, msg) UACPI_STATIC_ASSERT(!(expr), msg) + +#define UACPI_BUILD_BUG_ON(expr) \ + UACPI_BUILD_BUG_ON_WITH_MSG(expr, "BUILD BUG: " #expr " evaluated to true") + +#define UACPI_EXPECT_SIZEOF(type, size) \ + UACPI_BUILD_BUG_ON_WITH_MSG(sizeof(type) != size, \ + "BUILD BUG: invalid type size") diff --git a/src/include/uacpi/internal/compiler.h b/src/include/uacpi/internal/compiler.h new file mode 100644 index 0000000..68033fd --- /dev/null +++ b/src/include/uacpi/internal/compiler.h @@ -0,0 +1,3 @@ +#pragma once + +#include diff --git a/src/include/uacpi/internal/context.h b/src/include/uacpi/internal/context.h new file mode 100644 index 0000000..1b34d86 --- /dev/null +++ b/src/include/uacpi/internal/context.h @@ -0,0 +1,143 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +struct uacpi_runtime_context { + /* + * A local copy of FADT that has been verified & converted to most optimal + * format for faster access to the registers. + */ + struct acpi_fadt fadt; + + /* + * A cached pointer to FACS so that we don't have to look it up in interrupt + * contexts as we can't take mutexes. + */ + struct acpi_facs *facs; + + /* + * pm1{a,b}_evt_blk split into two registers for convenience + */ + struct acpi_gas pm1a_status_blk; + struct acpi_gas pm1b_status_blk; + struct acpi_gas pm1a_enable_blk; + struct acpi_gas pm1b_enable_blk; + + uacpi_u64 flags; + +#define UACPI_SLEEP_TYP_INVALID 0xFF + uacpi_u8 last_sleep_typ_a; + uacpi_u8 last_sleep_typ_b; + + uacpi_u8 s0_sleep_typ_a; + uacpi_u8 s0_sleep_typ_b; + + /* + * This is a per-table value but we mimic the NT implementation: + * treat all other definition blocks as if they were the same revision + * as DSDT. + */ + uacpi_bool is_rev1; + uacpi_bool global_lock_acquired; + +#ifndef UACPI_REDUCED_HARDWARE + uacpi_bool is_hardware_reduced; + uacpi_bool was_in_legacy_mode; + uacpi_bool has_global_lock; + uacpi_bool sci_handle_valid; + uacpi_handle sci_handle; +#endif + uacpi_u64 opcodes_executed; + + uacpi_u32 loop_timeout_seconds; + uacpi_u32 max_call_stack_depth; + + uacpi_u32 global_lock_seq_num; + + /* + * These are stored here to protect against stuff like: + * - CopyObject(JUNK, \) + * - CopyObject(JUNK, \_GL) + */ + uacpi_mutex *global_lock_mutex; + uacpi_object *root_object; + +#ifndef UACPI_REDUCED_HARDWARE + uacpi_handle *global_lock_event; + uacpi_handle *global_lock_spinlock; + uacpi_bool global_lock_pending; +#endif + + uacpi_u8 log_level; + uacpi_u8 init_level; +}; + +static inline const uacpi_char *uacpi_init_level_to_string(uacpi_u8 lvl) +{ + switch (lvl) { + case UACPI_INIT_LEVEL_EARLY: + return "early"; + case UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED: + return "subsystem initialized"; + case UACPI_INIT_LEVEL_NAMESPACE_LOADED: + return "namespace loaded"; + case UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED: + return "namespace initialized"; + default: + return ""; + } +} + +#define UACPI_ENSURE_INIT_LEVEL_AT_LEAST(lvl) \ + do { \ + if (uacpi_unlikely(g_uacpi_rt_ctx.init_level < lvl)) { \ + uacpi_error( \ + "while evaluating %s: init level %d (%s) is too low, " \ + "expected at least %d (%s)\n", __FUNCTION__, \ + g_uacpi_rt_ctx.init_level, \ + uacpi_init_level_to_string(g_uacpi_rt_ctx.init_level), lvl, \ + uacpi_init_level_to_string(lvl) \ + ); \ + return UACPI_STATUS_INIT_LEVEL_MISMATCH; \ + } \ + } while (0) + +#define UACPI_ENSURE_INIT_LEVEL_IS(lvl) \ + do { \ + if (uacpi_unlikely(g_uacpi_rt_ctx.init_level != lvl)) { \ + uacpi_error( \ + "while evaluating %s: invalid init level %d (%s), " \ + "expected %d (%s)\n", __FUNCTION__, \ + g_uacpi_rt_ctx.init_level, \ + uacpi_init_level_to_string(g_uacpi_rt_ctx.init_level), lvl, \ + uacpi_init_level_to_string(lvl) \ + ); \ + return UACPI_STATUS_INIT_LEVEL_MISMATCH; \ + } \ + } while (0) + +extern struct uacpi_runtime_context g_uacpi_rt_ctx; + +static inline uacpi_bool uacpi_check_flag(uacpi_u64 flag) +{ + return (g_uacpi_rt_ctx.flags & flag) == flag; +} + +static inline uacpi_bool uacpi_should_log(enum uacpi_log_level lvl) +{ + return lvl <= g_uacpi_rt_ctx.log_level; +} + +static inline uacpi_bool uacpi_is_hardware_reduced(void) +{ +#ifndef UACPI_REDUCED_HARDWARE + return g_uacpi_rt_ctx.is_hardware_reduced; +#else + return UACPI_TRUE; +#endif +} diff --git a/src/include/uacpi/internal/dynamic_array.h b/src/include/uacpi/internal/dynamic_array.h new file mode 100644 index 0000000..e67a5b9 --- /dev/null +++ b/src/include/uacpi/internal/dynamic_array.h @@ -0,0 +1,147 @@ +#pragma once + +#include +#include +#include + +#define DYNAMIC_ARRAY_WITH_INLINE_STORAGE(name, type, inline_capacity) \ + struct name { \ + type inline_storage[inline_capacity]; \ + type *dynamic_storage; \ + uacpi_size dynamic_capacity; \ + uacpi_size size_including_inline; \ + }; \ + +#define DYNAMIC_ARRAY_SIZE(arr) ((arr)->size_including_inline) + +#define DYNAMIC_ARRAY_WITH_INLINE_STORAGE_EXPORTS(name, type, prefix) \ + prefix uacpi_size name##_inline_capacity(struct name *arr); \ + prefix type *name##_at(struct name *arr, uacpi_size idx); \ + prefix type *name##_alloc(struct name *arr); \ + prefix type *name##_calloc(struct name *arr); \ + prefix void name##_pop(struct name *arr); \ + prefix uacpi_size name##_size(struct name *arr); \ + prefix type *name##_last(struct name *arr) \ + prefix void name##_clear(struct name *arr); + +#define DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL(name, type, prefix) \ + UACPI_MAYBE_UNUSED \ + prefix uacpi_size name##_inline_capacity(struct name *arr) \ + { \ + return sizeof(arr->inline_storage) / sizeof(arr->inline_storage[0]); \ + } \ + \ + UACPI_MAYBE_UNUSED \ + prefix uacpi_size name##_capacity(struct name *arr) \ + { \ + return name##_inline_capacity(arr) + arr->dynamic_capacity; \ + } \ + \ + prefix type *name##_at(struct name *arr, uacpi_size idx) \ + { \ + if (idx >= arr->size_including_inline) \ + return UACPI_NULL; \ + \ + if (idx < name##_inline_capacity(arr)) \ + return &arr->inline_storage[idx]; \ + \ + return &arr->dynamic_storage[idx - name##_inline_capacity(arr)]; \ + } \ + \ + UACPI_MAYBE_UNUSED \ + prefix type *name##_alloc(struct name *arr) \ + { \ + uacpi_size inline_cap; \ + type *out_ptr; \ + \ + inline_cap = name##_inline_capacity(arr); \ + \ + if (arr->size_including_inline >= inline_cap) { \ + uacpi_size dynamic_size; \ + \ + dynamic_size = arr->size_including_inline - inline_cap; \ + if (dynamic_size == arr->dynamic_capacity) { \ + uacpi_size bytes, type_size; \ + void *new_buf; \ + \ + type_size = sizeof(*arr->dynamic_storage); \ + \ + if (arr->dynamic_capacity == 0) { \ + bytes = type_size * inline_cap; \ + } else { \ + bytes = (arr->dynamic_capacity / 2) * type_size; \ + if (bytes == 0) \ + bytes += type_size; \ + \ + bytes += arr->dynamic_capacity * type_size; \ + } \ + \ + new_buf = uacpi_kernel_alloc(bytes); \ + if (uacpi_unlikely(new_buf == UACPI_NULL)) \ + return UACPI_NULL; \ + \ + arr->dynamic_capacity = bytes / type_size; \ + \ + if (arr->dynamic_storage) { \ + uacpi_memcpy(new_buf, arr->dynamic_storage, \ + dynamic_size * type_size); \ + } \ + uacpi_free(arr->dynamic_storage, dynamic_size * type_size); \ + arr->dynamic_storage = new_buf; \ + } \ + \ + out_ptr = &arr->dynamic_storage[dynamic_size]; \ + goto ret; \ + } \ + \ + \ + out_ptr = &arr->inline_storage[arr->size_including_inline]; \ + \ + ret: \ + arr->size_including_inline++; \ + return out_ptr; \ + } \ + \ + UACPI_MAYBE_UNUSED \ + prefix type *name##_calloc(struct name *arr) \ + { \ + type *ret; \ + \ + ret = name##_alloc(arr); \ + if (ret) \ + uacpi_memzero(ret, sizeof(*ret)); \ + \ + return ret; \ + } \ + \ + UACPI_MAYBE_UNUSED \ + prefix void name##_pop(struct name *arr) \ + { \ + if (arr->size_including_inline == 0) \ + return; \ + \ + arr->size_including_inline--; \ + } \ + \ + UACPI_MAYBE_UNUSED \ + prefix uacpi_size name##_size(struct name *arr) \ + { \ + return arr->size_including_inline; \ + } \ + \ + UACPI_MAYBE_UNUSED \ + prefix type *name##_last(struct name *arr) \ + { \ + return name##_at(arr, arr->size_including_inline - 1); \ + } \ + \ + prefix void name##_clear(struct name *arr) \ + { \ + uacpi_free( \ + arr->dynamic_storage, \ + arr->dynamic_capacity * sizeof(*arr->dynamic_storage) \ + ); \ + arr->size_including_inline = 0; \ + arr->dynamic_capacity = 0; \ + arr->dynamic_storage = UACPI_NULL; \ + } diff --git a/src/include/uacpi/internal/event.h b/src/include/uacpi/internal/event.h new file mode 100644 index 0000000..40ced0d --- /dev/null +++ b/src/include/uacpi/internal/event.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +// This fixed event is internal-only, and we don't expose it in the enum +#define UACPI_FIXED_EVENT_GLOBAL_LOCK 0 + +UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_initialize_events_early(void) +) + +UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_initialize_events(void) +) +UACPI_STUB_IF_REDUCED_HARDWARE( + void uacpi_deinitialize_events(void) +) + +UACPI_STUB_IF_REDUCED_HARDWARE( + void uacpi_events_match_post_dynamic_table_load(void) +) + +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_clear_all_events(void) +) diff --git a/src/include/uacpi/internal/helpers.h b/src/include/uacpi/internal/helpers.h new file mode 100644 index 0000000..f02b589 --- /dev/null +++ b/src/include/uacpi/internal/helpers.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +#define UACPI_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +#define UACPI_UNUSED(x) (void)(x) diff --git a/src/include/uacpi/internal/interpreter.h b/src/include/uacpi/internal/interpreter.h new file mode 100644 index 0000000..3f180d6 --- /dev/null +++ b/src/include/uacpi/internal/interpreter.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include + +enum uacpi_table_load_cause { + UACPI_TABLE_LOAD_CAUSE_LOAD_OP, + UACPI_TABLE_LOAD_CAUSE_LOAD_TABLE_OP, + UACPI_TABLE_LOAD_CAUSE_INIT, + UACPI_TABLE_LOAD_CAUSE_HOST, +}; + +uacpi_status uacpi_execute_table(void*, enum uacpi_table_load_cause cause); +uacpi_status uacpi_osi(uacpi_handle handle, uacpi_object *retval); + +uacpi_status uacpi_execute_control_method( + uacpi_namespace_node *scope, uacpi_control_method *method, + const uacpi_object_array *args, uacpi_object **ret +); diff --git a/src/include/uacpi/internal/io.h b/src/include/uacpi/internal/io.h new file mode 100644 index 0000000..d9153db --- /dev/null +++ b/src/include/uacpi/internal/io.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include + +uacpi_size uacpi_round_up_bits_to_bytes(uacpi_size bit_length); + +void uacpi_read_buffer_field( + const uacpi_buffer_field *field, void *dst +); +void uacpi_write_buffer_field( + uacpi_buffer_field *field, const void *src, uacpi_size size +); + +uacpi_status uacpi_read_field_unit( + uacpi_field_unit *field, void *dst, uacpi_size size +); +uacpi_status uacpi_write_field_unit( + uacpi_field_unit *field, const void *src, uacpi_size size +); + +uacpi_status uacpi_system_io_read( + uacpi_io_addr address, uacpi_u8 width, uacpi_u64 *out +); +uacpi_status uacpi_system_io_write( + uacpi_io_addr address, uacpi_u8 width, uacpi_u64 in +); + +uacpi_status uacpi_system_memory_read(void *ptr, uacpi_u8 width, uacpi_u64 *out); +uacpi_status uacpi_system_memory_write(void *ptr, uacpi_u8 width, uacpi_u64 in); diff --git a/src/include/uacpi/internal/log.h b/src/include/uacpi/internal/log.h new file mode 100644 index 0000000..006fb73 --- /dev/null +++ b/src/include/uacpi/internal/log.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#ifdef UACPI_FORMATTED_LOGGING +#define uacpi_log uacpi_kernel_log +#else +UACPI_PRINTF_DECL(2, 3) +void uacpi_log(uacpi_log_level, const uacpi_char*, ...); +#endif + +#define uacpi_log_lvl(lvl, ...) \ + do { if (uacpi_should_log(lvl)) uacpi_log(lvl, __VA_ARGS__); } while (0) + +#define uacpi_debug(...) uacpi_log_lvl(UACPI_LOG_DEBUG, __VA_ARGS__) +#define uacpi_trace(...) uacpi_log_lvl(UACPI_LOG_TRACE, __VA_ARGS__) +#define uacpi_info(...) uacpi_log_lvl(UACPI_LOG_INFO, __VA_ARGS__) +#define uacpi_warn(...) uacpi_log_lvl(UACPI_LOG_WARN, __VA_ARGS__) +#define uacpi_error(...) uacpi_log_lvl(UACPI_LOG_ERROR, __VA_ARGS__) + +void uacpi_logger_initialize(void); diff --git a/src/include/uacpi/internal/mutex.h b/src/include/uacpi/internal/mutex.h new file mode 100644 index 0000000..a1680bd --- /dev/null +++ b/src/include/uacpi/internal/mutex.h @@ -0,0 +1,78 @@ +#pragma once + +#include +#include + +uacpi_bool uacpi_this_thread_owns_aml_mutex(uacpi_mutex*); + +uacpi_status uacpi_acquire_aml_mutex(uacpi_mutex*, uacpi_u16 timeout); +uacpi_status uacpi_release_aml_mutex(uacpi_mutex*); + +static inline uacpi_status uacpi_acquire_native_mutex(uacpi_handle mtx) +{ + if (uacpi_unlikely(mtx == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + return uacpi_kernel_acquire_mutex(mtx, 0xFFFF); +} + +uacpi_status uacpi_acquire_native_mutex_with_timeout( + uacpi_handle mtx, uacpi_u16 timeout +); + +static inline uacpi_status uacpi_release_native_mutex(uacpi_handle mtx) +{ + if (uacpi_unlikely(mtx == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + uacpi_kernel_release_mutex(mtx); + return UACPI_STATUS_OK; +} + +static inline uacpi_status uacpi_acquire_native_mutex_may_be_null( + uacpi_handle mtx +) +{ + if (mtx == UACPI_NULL) + return UACPI_STATUS_OK; + + return uacpi_kernel_acquire_mutex(mtx, 0xFFFF); +} + +static inline uacpi_status uacpi_release_native_mutex_may_be_null( + uacpi_handle mtx +) +{ + if (mtx == UACPI_NULL) + return UACPI_STATUS_OK; + + uacpi_kernel_release_mutex(mtx); + return UACPI_STATUS_OK; +} + +struct uacpi_recursive_lock { + uacpi_handle mutex; + uacpi_size depth; + uacpi_thread_id owner; +}; + +uacpi_status uacpi_recursive_lock_init(struct uacpi_recursive_lock *lock); +uacpi_status uacpi_recursive_lock_deinit(struct uacpi_recursive_lock *lock); + +uacpi_status uacpi_recursive_lock_acquire(struct uacpi_recursive_lock *lock); +uacpi_status uacpi_recursive_lock_release(struct uacpi_recursive_lock *lock); + +struct uacpi_rw_lock { + uacpi_handle read_mutex; + uacpi_handle write_mutex; + uacpi_size num_readers; +}; + +uacpi_status uacpi_rw_lock_init(struct uacpi_rw_lock *lock); +uacpi_status uacpi_rw_lock_deinit(struct uacpi_rw_lock *lock); + +uacpi_status uacpi_rw_lock_read(struct uacpi_rw_lock *lock); +uacpi_status uacpi_rw_unlock_read(struct uacpi_rw_lock *lock); + +uacpi_status uacpi_rw_lock_write(struct uacpi_rw_lock *lock); +uacpi_status uacpi_rw_unlock_write(struct uacpi_rw_lock *lock); diff --git a/src/include/uacpi/internal/namespace.h b/src/include/uacpi/internal/namespace.h new file mode 100644 index 0000000..a9cba1c --- /dev/null +++ b/src/include/uacpi/internal/namespace.h @@ -0,0 +1,119 @@ +#pragma once + +#include +#include +#include +#include + +#define UACPI_NAMESPACE_NODE_FLAG_ALIAS (1 << 0) + +/* + * This node has been uninstalled and has no object associated with it. + * + * This is used to handle edge cases where an object needs to reference + * a namespace node, where the node might end up going out of scope before + * the object lifetime ends. + */ +#define UACPI_NAMESPACE_NODE_FLAG_DANGLING (1u << 1) + +/* + * This node is method-local and must not be exposed via public API as its + * lifetime is limited. + */ +#define UACPI_NAMESPACE_NODE_FLAG_TEMPORARY (1u << 2) + +#define UACPI_NAMESPACE_NODE_PREDEFINED (1u << 31) + +typedef struct uacpi_namespace_node { + struct uacpi_shareable shareable; + uacpi_object_name name; + uacpi_u32 flags; + uacpi_object *object; + struct uacpi_namespace_node *parent; + struct uacpi_namespace_node *child; + struct uacpi_namespace_node *next; +} uacpi_namespace_node; + +uacpi_status uacpi_initialize_namespace(void); +void uacpi_deinitialize_namespace(void); + +uacpi_namespace_node *uacpi_namespace_node_alloc(uacpi_object_name name); +void uacpi_namespace_node_unref(uacpi_namespace_node *node); + + +uacpi_status uacpi_namespace_node_type_unlocked( + const uacpi_namespace_node *node, uacpi_object_type *out_type +); +uacpi_status uacpi_namespace_node_is_one_of_unlocked( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask, + uacpi_bool *out +); + +uacpi_object *uacpi_namespace_node_get_object(const uacpi_namespace_node *node); + +uacpi_object *uacpi_namespace_node_get_object_typed( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask +); + +uacpi_status uacpi_namespace_node_acquire_object( + const uacpi_namespace_node *node, uacpi_object **out_obj +); +uacpi_status uacpi_namespace_node_acquire_object_typed( + const uacpi_namespace_node *node, uacpi_object_type_bits, + uacpi_object **out_obj +); + +uacpi_status uacpi_namespace_node_reacquire_object( + uacpi_object *obj +); +uacpi_status uacpi_namespace_node_release_object( + uacpi_object *obj +); + +uacpi_status uacpi_namespace_node_install( + uacpi_namespace_node *parent, uacpi_namespace_node *node +); +uacpi_status uacpi_namespace_node_uninstall(uacpi_namespace_node *node); + +uacpi_namespace_node *uacpi_namespace_node_find_sub_node( + uacpi_namespace_node *parent, + uacpi_object_name name +); + +enum uacpi_may_search_above_parent { + UACPI_MAY_SEARCH_ABOVE_PARENT_NO, + UACPI_MAY_SEARCH_ABOVE_PARENT_YES, +}; + +enum uacpi_permanent_only { + UACPI_PERMANENT_ONLY_NO, + UACPI_PERMANENT_ONLY_YES, +}; + +enum uacpi_should_lock { + UACPI_SHOULD_LOCK_NO, + UACPI_SHOULD_LOCK_YES, +}; + +uacpi_status uacpi_namespace_node_resolve( + uacpi_namespace_node *scope, const uacpi_char *path, enum uacpi_should_lock, + enum uacpi_may_search_above_parent, enum uacpi_permanent_only, + uacpi_namespace_node **out_node +); + +uacpi_status uacpi_namespace_do_for_each_child( + uacpi_namespace_node *parent, uacpi_iteration_callback descending_callback, + uacpi_iteration_callback ascending_callback, + uacpi_object_type_bits, uacpi_u32 max_depth, enum uacpi_should_lock, + enum uacpi_permanent_only, void *user +); + +uacpi_bool uacpi_namespace_node_is_dangling(uacpi_namespace_node *node); +uacpi_bool uacpi_namespace_node_is_temporary(uacpi_namespace_node *node); +uacpi_bool uacpi_namespace_node_is_predefined(uacpi_namespace_node *node); + +uacpi_status uacpi_namespace_read_lock(void); +uacpi_status uacpi_namespace_read_unlock(void); + +uacpi_status uacpi_namespace_write_lock(void); +uacpi_status uacpi_namespace_write_unlock(void); diff --git a/src/include/uacpi/internal/notify.h b/src/include/uacpi/internal/notify.h new file mode 100644 index 0000000..27cb21c --- /dev/null +++ b/src/include/uacpi/internal/notify.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +uacpi_status uacpi_initialize_notify(void); +void uacpi_deinitialize_notify(void); + +uacpi_status uacpi_notify_all(uacpi_namespace_node *node, uacpi_u64 value); diff --git a/src/include/uacpi/internal/opcodes.h b/src/include/uacpi/internal/opcodes.h new file mode 100644 index 0000000..0f46cd8 --- /dev/null +++ b/src/include/uacpi/internal/opcodes.h @@ -0,0 +1,1329 @@ +#pragma once + +#include + +typedef uacpi_u16 uacpi_aml_op; + +#define UACPI_EXT_PREFIX 0x5B +#define UACPI_EXT_OP(op) ((UACPI_EXT_PREFIX << 8) | (op)) + +#define UACPI_DUAL_NAME_PREFIX 0x2E +#define UACPI_MULTI_NAME_PREFIX 0x2F +#define UACPI_NULL_NAME 0x00 + +/* + * Opcodes that tell the parser VM how to take apart every AML instruction. + * Every AML opcode has a list of these that is executed by the parser. + */ +enum uacpi_parse_op { + UACPI_PARSE_OP_END = 0, + + /* + * End the execution of the current instruction with a warning if the item + * at decode_ops[pc + 1] is NULL. + */ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, + + // SimpleName := NameString | ArgObj | LocalObj + UACPI_PARSE_OP_SIMPLE_NAME, + + // SuperName := SimpleName | DebugObj | ReferenceTypeOpcode + UACPI_PARSE_OP_SUPERNAME, + // The resulting item will be set to null if name couldn't be resolved + UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED, + + // TermArg := ExpressionOpcode | DataObject | ArgObj | LocalObj + UACPI_PARSE_OP_TERM_ARG, + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, + + /* + * Same as TERM_ARG, but named references are passed as-is. + * This means methods are not invoked, fields are not read, etc. + */ + UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT, + + /* + * Same as UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT but allows unresolved + * name strings. + */ + UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED, + + // Operand := TermArg => Integer + UACPI_PARSE_OP_OPERAND, + + // TermArg => String + UACPI_PARSE_OP_STRING, + + /* + * ComputationalData := ByteConst | WordConst | DWordConst | QWordConst | + * String | ConstObj | RevisionOp | DefBuffer + */ + UACPI_PARSE_OP_COMPUTATIONAL_DATA, + + // Target := SuperName | NullName + UACPI_PARSE_OP_TARGET, + + // Parses a pkglen + UACPI_PARSE_OP_PKGLEN, + + /* + * Parses a pkglen and records it, the end of this pkglen is considered + * the end of the instruction. The PC is always set to the end of this + * package once parser reaches UACPI_PARSE_OP_END. + */ + UACPI_PARSE_OP_TRACKED_PKGLEN, + + /* + * Parse a NameString and create the last nameseg. + * Note that this errors out if last nameseg already exists. + */ + UACPI_PARSE_OP_CREATE_NAMESTRING, + + /* + * same as UACPI_PARSE_OP_CREATE_NAMESTRING, but attempting to create an + * already existing object is not fatal if currently loading a table. + */ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, + + /* + * Parse a NameString and put the node into the ready parts array. + * Note that this errors out if the referenced node doesn't exist. + */ + UACPI_PARSE_OP_EXISTING_NAMESTRING, + + /* + * Same as UACPI_PARSE_OP_EXISTING_NAMESTRING except the op doesn't error + * out if namestring couldn't be resolved. + */ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL, + + /* + * Same as UACPI_PARSE_OP_EXISTING_NAMESTRING, but undefined references + * are not fatal if currently loading a table. + */ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD, + + // Invoke a handler at op_handlers[spec->code] + UACPI_PARSE_OP_INVOKE_HANDLER, + + // Allocate an object an put it at the front of the item list + UACPI_PARSE_OP_OBJECT_ALLOC, + + UACPI_PARSE_OP_EMPTY_OBJECT_ALLOC, + + // Convert last item into a shallow/deep copy of itself + UACPI_PARSE_OP_OBJECT_CONVERT_TO_SHALLOW_COPY, + UACPI_PARSE_OP_OBJECT_CONVERT_TO_DEEP_COPY, + + /* + * Same as UACPI_PARSE_OP_OBJECT_ALLOC except the type of the allocated + * object is specified at decode_ops[pc + 1] + */ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, + + // Record current AML program counter as a QWORD immediate + UACPI_PARSE_OP_RECORD_AML_PC, + + // Load a QWORD immediate located at decode_ops[pc + 1] + UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT, + + // Load a decode_ops[pc + 1] byte imm at decode_ops[pc + 2] + UACPI_PARSE_OP_LOAD_INLINE_IMM, + + // Load a QWORD zero immediate + UACPI_PARSE_OP_LOAD_ZERO_IMM, + + // Load a decode_ops[pc + 1] byte imm from the instructions stream + UACPI_PARSE_OP_LOAD_IMM, + + // Same as UACPI_PARSE_OP_LOAD_IMM, expect the resulting value is an object + UACPI_PARSE_OP_LOAD_IMM_AS_OBJECT, + + // Create & Load an integer constant representing either true or false + UACPI_PARSE_OP_LOAD_FALSE_OBJECT, + UACPI_PARSE_OP_LOAD_TRUE_OBJECT, + + // Truncate the last item in the list if needed + UACPI_PARSE_OP_TRUNCATE_NUMBER, + + // Ensure the type of item is decode_ops[pc + 1] + UACPI_PARSE_OP_TYPECHECK, + + // Install the namespace node specified in items[decode_ops[pc + 1]] + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, + + // Move item to the previous (preempted) op + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, + + /* + * Same as UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, but the object + * is copied instead. (Useful when dealing with multiple targets) + * TODO: optimize this so that we can optionally move the object + * if target was a null target. + */ + UACPI_PARSE_OP_OBJECT_COPY_TO_PREV, + + // Store the last item to the target at items[decode_ops[pc + 1]] + UACPI_PARSE_OP_STORE_TO_TARGET, + + /* + * Store the item at items[decode_ops[pc + 2]] to target + * at items[decode_ops[pc + 1]] + */ + UACPI_PARSE_OP_STORE_TO_TARGET_INDIRECT, + + /* + * Error if reached. Should be used for opcodes that are supposed to be + * converted at op parse time, e.g. invoking a method or referring to + * a named object. + */ + UACPI_PARSE_OP_UNREACHABLE, + + // Invalid opcode, should never be encountered in the stream + UACPI_PARSE_OP_BAD_OPCODE, + + // Decrement the current AML instruction pointer + UACPI_PARSE_OP_AML_PC_DECREMENT, + + // Decrement the immediate at decode_ops[pc + 1] + UACPI_PARSE_OP_IMM_DECREMENT, + + // Remove the last item off the item stack + UACPI_PARSE_OP_ITEM_POP, + + // Dispatch the method call from items[0] and return from current op_exec + UACPI_PARSE_OP_DISPATCH_METHOD_CALL, + + /* + * Dispatch a table load with scope node at items[0] and method at items[1]. + * The last item is expected to be an integer object that is set to 0 in + * case load fails. + */ + UACPI_PARSE_OP_DISPATCH_TABLE_LOAD, + + /* + * Convert the current resolved namestring to either a method call + * or a named object reference. + */ + UACPI_PARSE_OP_CONVERT_NAMESTRING, + + /* + * Execute the next instruction only if currently tracked package still + * has data left, otherwise skip decode_ops[pc + 1] bytes. + */ + UACPI_PARSE_OP_IF_HAS_DATA, + + /* + * Execute the next instruction only if the handle at + * items[decode_ops[pc + 1]] is null. Otherwise skip + * decode_ops[pc + 2] bytes. + */ + UACPI_PARSE_OP_IF_NULL, + + // The inverse of UACPI_PARSE_OP_IF_NULL + UACPI_PARSE_OP_IF_NOT_NULL, + + /* + * Execute the next instruction only if the last immediate is equal to + * decode_ops[pc + 1], otherwise skip decode_ops[pc + 2] bytes. + */ + UACPI_PARSE_OP_IF_EQUALS, + + /* + * pc = decode_ops[pc + 1] + */ + UACPI_PARSE_OP_JMP, + UACPI_PARSE_OP_MAX = UACPI_PARSE_OP_JMP, +}; +const uacpi_char *uacpi_parse_op_to_string(enum uacpi_parse_op op); + +/* + * A few notes about op properties: + * Technically the spec says that RefOfOp is considered a SuperName, but NT + * disagrees about this. For example Store(..., RefOf) fails with + * "Invalid SuperName". MethodInvocation could also technically be considered + * a SuperName, but NT doesn't allow that either: Store(..., MethodInvocation) + * fails with "Invalid Target Method, expected a DataObject" error. + */ + +enum uacpi_op_property { + UACPI_OP_PROPERTY_TERM_ARG = 1, + UACPI_OP_PROPERTY_SUPERNAME = 2, + UACPI_OP_PROPERTY_SIMPLE_NAME = 4, + UACPI_OP_PROPERTY_TARGET = 8, + + // The ops to execute are pointed to by indirect_decode_ops + UACPI_OP_PROPERTY_OUT_OF_LINE = 16, + + // Error if encountered in the AML byte strem + UACPI_OP_PROPERTY_RESERVED = 128, +}; + +struct uacpi_op_spec { + uacpi_char *name; + union { + uacpi_u8 decode_ops[16]; + uacpi_u8 *indirect_decode_ops; + }; + uacpi_u8 properties; + uacpi_aml_op code; +}; + +const struct uacpi_op_spec *uacpi_get_op_spec(uacpi_aml_op); + +#define UACPI_INTERNAL_OP(code) \ + UACPI_OP(Internal_##code, code, { UACPI_PARSE_OP_UNREACHABLE }) + +#define UACPI_BAD_OPCODE(code) \ + UACPI_OP(Reserved_##code, code, { UACPI_PARSE_OP_BAD_OPCODE }) + +#define UACPI_METHOD_CALL_OPCODE(nargs) \ + UACPI_OP( \ + InternalOpMethodCall##nargs##Args, 0xF7 + nargs, \ + { \ + UACPI_PARSE_OP_LOAD_INLINE_IMM, 1, nargs, \ + UACPI_PARSE_OP_IF_NOT_NULL, 1, 6, \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_OBJECT_CONVERT_TO_SHALLOW_COPY, \ + UACPI_PARSE_OP_IMM_DECREMENT, 1, \ + UACPI_PARSE_OP_JMP, 3, \ + UACPI_PARSE_OP_OBJECT_ALLOC, \ + UACPI_PARSE_OP_DISPATCH_METHOD_CALL, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_RESERVED \ + ) + +/* + * ------------------------------------------------------------- + * RootChar := ‘\’ + * ParentPrefixChar := ‘^’ + * ‘\’ := 0x5C + * ‘^’ := 0x5E + * MultiNamePrefix := 0x2F + * DualNamePrefix := 0x2E + * ------------------------------------------------------------ + * ‘A’-‘Z’ := 0x41 - 0x5A + * ‘_’ := 0x5F + * LeadNameChar := ‘A’-‘Z’ | ‘_’ + * NameSeg := + * NameString := | + * PrefixPath := Nothing | <’^’ prefixpath> + * DualNamePath := DualNamePrefix NameSeg NameSeg + * MultiNamePath := MultiNamePrefix SegCount NameSeg(SegCount) + */ +#define UACPI_UNRESOLVED_NAME_STRING_OP(character, code) \ + UACPI_OP( \ + UACPI_InternalOpUnresolvedNameString_##character, code, \ + { \ + UACPI_PARSE_OP_AML_PC_DECREMENT, \ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL, \ + UACPI_PARSE_OP_CONVERT_NAMESTRING, \ + }, \ + UACPI_OP_PROPERTY_SIMPLE_NAME | \ + UACPI_OP_PROPERTY_SUPERNAME | \ + UACPI_OP_PROPERTY_TERM_ARG \ + ) + +#define UACPI_BUILD_LOCAL_OR_ARG_OP(prefix, base, offset) \ +UACPI_OP( \ + prefix##offset##Op, base + offset, \ + { \ + UACPI_PARSE_OP_EMPTY_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_SUPERNAME | \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_SIMPLE_NAME \ +) \ + +#define UACPI_LOCALX_OP(idx) UACPI_BUILD_LOCAL_OR_ARG_OP(Local, 0x60, idx) +#define UACPI_ARGX_OP(idx) UACPI_BUILD_LOCAL_OR_ARG_OP(Arg, 0x68, idx) + +#define UACPI_BUILD_PACKAGE_OP(name, code, jmp_off, ...) \ +UACPI_OP( \ + name##Op, code, \ + { \ + UACPI_PARSE_OP_TRACKED_PKGLEN, \ + ##__VA_ARGS__, \ + UACPI_PARSE_OP_IF_HAS_DATA, 4, \ + UACPI_PARSE_OP_RECORD_AML_PC, \ + UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED, \ + UACPI_PARSE_OP_JMP, jmp_off, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_PACKAGE, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +#define UACPI_BUILD_BINARY_MATH_OP(prefix, code) \ +UACPI_OP( \ + prefix##Op, code, \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_TRUNCATE_NUMBER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 2, \ + UACPI_PARSE_OP_OBJECT_COPY_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +#define UACPI_BUILD_UNARY_MATH_OP(type, code) \ +UACPI_OP( \ + type##Op, code, \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 1, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +#define UACPI_DO_BUILD_BUFFER_FIELD_OP(type, code, node_idx, ...) \ +UACPI_OP( \ + type##FieldOp, code, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_TYPECHECK, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_OPERAND, \ + ##__VA_ARGS__, \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, node_idx, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_BUFFER_FIELD, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, node_idx, \ + } \ +) + +#define UACPI_BUILD_BUFFER_FIELD_OP(type, code) \ + UACPI_DO_BUILD_BUFFER_FIELD_OP(Create##type, code, 2) + +#define UACPI_INTEGER_LITERAL_OP(type, code, bytes) \ +UACPI_OP( \ + type##Prefix, code, \ + { \ + UACPI_PARSE_OP_LOAD_IMM_AS_OBJECT, bytes, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ + +#define UACPI_BUILD_BINARY_LOGIC_OP(type, code) \ +UACPI_OP( \ + type##Op, code, \ + { \ + UACPI_PARSE_OP_COMPUTATIONAL_DATA, \ + UACPI_PARSE_OP_COMPUTATIONAL_DATA, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +#define UACPI_BUILD_TO_OP(kind, code, dst_type) \ +UACPI_OP( \ + To##kind##Op, code, \ + { \ + UACPI_PARSE_OP_COMPUTATIONAL_DATA, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, dst_type, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 1, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +#define UACPI_BUILD_INC_DEC_OP(prefix, code) \ +UACPI_OP( \ + prefix##Op, code, \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_TRUNCATE_NUMBER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 0, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ + +#define UACPI_ENUMERATE_OPCODES \ +UACPI_OP( \ + ZeroOp, 0x00, \ + { \ + UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TARGET | \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + OneOp, 0x01, \ + { \ + UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT, \ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BAD_OPCODE(0x02) \ +UACPI_BAD_OPCODE(0x03) \ +UACPI_BAD_OPCODE(0x04) \ +UACPI_BAD_OPCODE(0x05) \ +UACPI_OP( \ + AliasOp, 0x06, \ + { \ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 1, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 1, \ + } \ +) \ +UACPI_BAD_OPCODE(0x07) \ +UACPI_OP( \ + NameOp, 0x08, \ + { \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_OBJECT_CONVERT_TO_DEEP_COPY, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 0, \ + } \ +) \ +UACPI_BAD_OPCODE(0x09) \ +UACPI_INTEGER_LITERAL_OP(Byte, 0x0A, 1) \ +UACPI_INTEGER_LITERAL_OP(Word, 0x0B, 2) \ +UACPI_INTEGER_LITERAL_OP(DWord, 0x0C, 4) \ +UACPI_OP( \ + StringPrefix, 0x0D, \ + { \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_STRING, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_INTEGER_LITERAL_OP(QWord, 0x0E, 8) \ +UACPI_BAD_OPCODE(0x0F) \ +UACPI_OP( \ + ScopeOp, 0x10, \ + { \ + UACPI_PARSE_OP_TRACKED_PKGLEN, \ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 1, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + BufferOp, 0x11, \ + { \ + UACPI_PARSE_OP_TRACKED_PKGLEN, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_RECORD_AML_PC, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_PACKAGE_OP( \ + Package, 0x12, 3, \ + UACPI_PARSE_OP_LOAD_IMM, 1 \ +) \ +UACPI_BUILD_PACKAGE_OP( \ + VarPackage, 0x13, 2, \ + UACPI_PARSE_OP_OPERAND \ +) \ +UACPI_OP( \ + MethodOp, 0x14, \ + { \ + UACPI_PARSE_OP_TRACKED_PKGLEN, \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 1, \ + UACPI_PARSE_OP_RECORD_AML_PC, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_METHOD, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 1, \ + } \ +) \ +UACPI_OP( \ + ExternalOp, 0x15, \ + { \ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + } \ +) \ +UACPI_BAD_OPCODE(0x16) \ +UACPI_BAD_OPCODE(0x17) \ +UACPI_BAD_OPCODE(0x18) \ +UACPI_BAD_OPCODE(0x19) \ +UACPI_BAD_OPCODE(0x1A) \ +UACPI_BAD_OPCODE(0x1B) \ +UACPI_BAD_OPCODE(0x1C) \ +UACPI_BAD_OPCODE(0x1D) \ +UACPI_BAD_OPCODE(0x1E) \ +UACPI_BAD_OPCODE(0x1F) \ +UACPI_BAD_OPCODE(0x20) \ +UACPI_BAD_OPCODE(0x21) \ +UACPI_BAD_OPCODE(0x22) \ +UACPI_BAD_OPCODE(0x23) \ +UACPI_BAD_OPCODE(0x24) \ +UACPI_BAD_OPCODE(0x25) \ +UACPI_BAD_OPCODE(0x26) \ +UACPI_BAD_OPCODE(0x27) \ +UACPI_BAD_OPCODE(0x28) \ +UACPI_BAD_OPCODE(0x29) \ +UACPI_BAD_OPCODE(0x2A) \ +UACPI_BAD_OPCODE(0x2B) \ +UACPI_BAD_OPCODE(0x2C) \ +UACPI_BAD_OPCODE(0x2D) \ +UACPI_UNRESOLVED_NAME_STRING_OP(DualNamePrefix, 0x2E) \ +UACPI_UNRESOLVED_NAME_STRING_OP(MultiNamePrefix, 0x2F) \ +UACPI_INTERNAL_OP(0x30) \ +UACPI_INTERNAL_OP(0x31) \ +UACPI_INTERNAL_OP(0x32) \ +UACPI_INTERNAL_OP(0x33) \ +UACPI_INTERNAL_OP(0x34) \ +UACPI_INTERNAL_OP(0x35) \ +UACPI_INTERNAL_OP(0x36) \ +UACPI_INTERNAL_OP(0x37) \ +UACPI_INTERNAL_OP(0x38) \ +UACPI_INTERNAL_OP(0x39) \ +UACPI_BAD_OPCODE(0x3A) \ +UACPI_BAD_OPCODE(0x3B) \ +UACPI_BAD_OPCODE(0x3C) \ +UACPI_BAD_OPCODE(0x3D) \ +UACPI_BAD_OPCODE(0x3E) \ +UACPI_BAD_OPCODE(0x3F) \ +UACPI_BAD_OPCODE(0x40) \ +UACPI_UNRESOLVED_NAME_STRING_OP(A, 0x41) \ +UACPI_UNRESOLVED_NAME_STRING_OP(B, 0x42) \ +UACPI_UNRESOLVED_NAME_STRING_OP(C, 0x43) \ +UACPI_UNRESOLVED_NAME_STRING_OP(D, 0x44) \ +UACPI_UNRESOLVED_NAME_STRING_OP(E, 0x45) \ +UACPI_UNRESOLVED_NAME_STRING_OP(F, 0x46) \ +UACPI_UNRESOLVED_NAME_STRING_OP(G, 0x47) \ +UACPI_UNRESOLVED_NAME_STRING_OP(H, 0x48) \ +UACPI_UNRESOLVED_NAME_STRING_OP(I, 0x49) \ +UACPI_UNRESOLVED_NAME_STRING_OP(J, 0x4A) \ +UACPI_UNRESOLVED_NAME_STRING_OP(K, 0x4B) \ +UACPI_UNRESOLVED_NAME_STRING_OP(L, 0x4C) \ +UACPI_UNRESOLVED_NAME_STRING_OP(M, 0x4D) \ +UACPI_UNRESOLVED_NAME_STRING_OP(N, 0x4E) \ +UACPI_UNRESOLVED_NAME_STRING_OP(O, 0x4F) \ +UACPI_UNRESOLVED_NAME_STRING_OP(P, 0x50) \ +UACPI_UNRESOLVED_NAME_STRING_OP(Q, 0x51) \ +UACPI_UNRESOLVED_NAME_STRING_OP(R, 0x52) \ +UACPI_UNRESOLVED_NAME_STRING_OP(S, 0x53) \ +UACPI_UNRESOLVED_NAME_STRING_OP(T, 0x54) \ +UACPI_UNRESOLVED_NAME_STRING_OP(U, 0x55) \ +UACPI_UNRESOLVED_NAME_STRING_OP(V, 0x56) \ +UACPI_UNRESOLVED_NAME_STRING_OP(W, 0x57) \ +UACPI_UNRESOLVED_NAME_STRING_OP(X, 0x58) \ +UACPI_UNRESOLVED_NAME_STRING_OP(Y, 0x59) \ +UACPI_UNRESOLVED_NAME_STRING_OP(Z, 0x5A) \ +UACPI_INTERNAL_OP(0x5B) \ +UACPI_UNRESOLVED_NAME_STRING_OP(RootChar, 0x5C) \ +UACPI_BAD_OPCODE(0x5D) \ +UACPI_UNRESOLVED_NAME_STRING_OP(ParentPrefixChar, 0x5E) \ +UACPI_UNRESOLVED_NAME_STRING_OP(Underscore, 0x5F) \ +UACPI_LOCALX_OP(0) \ +UACPI_LOCALX_OP(1) \ +UACPI_LOCALX_OP(2) \ +UACPI_LOCALX_OP(3) \ +UACPI_LOCALX_OP(4) \ +UACPI_LOCALX_OP(5) \ +UACPI_LOCALX_OP(6) \ +UACPI_LOCALX_OP(7) \ +UACPI_ARGX_OP(0) \ +UACPI_ARGX_OP(1) \ +UACPI_ARGX_OP(2) \ +UACPI_ARGX_OP(3) \ +UACPI_ARGX_OP(4) \ +UACPI_ARGX_OP(5) \ +UACPI_ARGX_OP(6) \ +UACPI_BAD_OPCODE(0x6F) \ +UACPI_OP( \ + StoreOp, 0x70, \ + { \ + UACPI_PARSE_OP_TERM_ARG, \ + UACPI_PARSE_OP_OBJECT_COPY_TO_PREV, \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + RefOfOp, 0x71, \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BINARY_MATH_OP(Add, 0x72) \ +UACPI_OP( \ + ConcatOp, 0x73, \ + { \ + UACPI_PARSE_OP_COMPUTATIONAL_DATA, \ + UACPI_PARSE_OP_COMPUTATIONAL_DATA, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 2, \ + UACPI_PARSE_OP_OBJECT_COPY_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BINARY_MATH_OP(Subtract, 0x74) \ +UACPI_BUILD_INC_DEC_OP(Increment, 0x75) \ +UACPI_BUILD_INC_DEC_OP(Decrement, 0x76) \ +UACPI_BUILD_BINARY_MATH_OP(Multiply, 0x77) \ +UACPI_OP( \ + DivideOp, 0x78, \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 3, \ + UACPI_PARSE_OP_OBJECT_COPY_TO_PREV, \ + UACPI_PARSE_OP_STORE_TO_TARGET_INDIRECT, 2, 4, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BINARY_MATH_OP(ShiftLeft, 0x79) \ +UACPI_BUILD_BINARY_MATH_OP(ShiftRight, 0x7A) \ +UACPI_BUILD_BINARY_MATH_OP(And, 0x7B) \ +UACPI_BUILD_BINARY_MATH_OP(Nand, 0x7C) \ +UACPI_BUILD_BINARY_MATH_OP(Or, 0x7D) \ +UACPI_BUILD_BINARY_MATH_OP(Nor, 0x7E) \ +UACPI_BUILD_BINARY_MATH_OP(Xor, 0x7F) \ +UACPI_BUILD_UNARY_MATH_OP(Not, 0x80) \ +UACPI_BUILD_UNARY_MATH_OP(FindSetLeftBit, 0x81) \ +UACPI_BUILD_UNARY_MATH_OP(FindSetRightBit, 0x82) \ +UACPI_OP( \ + DerefOfOp, 0x83, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + ConcatResOp, 0x84, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_TYPECHECK, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_TYPECHECK, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 2, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BINARY_MATH_OP(Mod, 0x85) \ +UACPI_OP( \ + NotifyOp, 0x86, \ + { \ + /* This is technically wrong according to spec but I was */ \ + /* unable to find any examples of anything else after */ \ + /* inspecting about 500 AML dumps. Spec says this is a */ \ + /* SuperName that must evaluate to Device/ThermalZone or */ \ + /* Processor, just ignore for now. */ \ + UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + SizeOfOp, 0x87, \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + IndexOp, 0x88, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_EMPTY_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 2, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_SUPERNAME | \ + UACPI_OP_PROPERTY_SIMPLE_NAME \ +) \ +UACPI_OP( \ + MatchOp, 0x89, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_TYPECHECK, UACPI_OBJECT_PACKAGE, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BUFFER_FIELD_OP(DWord, 0x8A) \ +UACPI_BUILD_BUFFER_FIELD_OP(Word, 0x8B) \ +UACPI_BUILD_BUFFER_FIELD_OP(Byte, 0x8C) \ +UACPI_BUILD_BUFFER_FIELD_OP(Bit, 0x8D) \ +UACPI_OP( \ + ObjectTypeOp, 0x8E, \ + { \ + UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BUFFER_FIELD_OP(QWord, 0x8F) \ +UACPI_BUILD_BINARY_LOGIC_OP(Land, 0x90) \ +UACPI_BUILD_BINARY_LOGIC_OP(Lor, 0x91) \ +UACPI_OP( \ + LnotOp, 0x92, \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_BUILD_BINARY_LOGIC_OP(LEqual, 0x93) \ +UACPI_BUILD_BINARY_LOGIC_OP(LGreater, 0x94) \ +UACPI_BUILD_BINARY_LOGIC_OP(LLess, 0x95) \ +UACPI_BUILD_TO_OP(Buffer, 0x96, UACPI_OBJECT_BUFFER) \ +UACPI_BUILD_TO_OP(DecimalString, 0x97, UACPI_OBJECT_STRING) \ +UACPI_BUILD_TO_OP(HexString, 0x98, UACPI_OBJECT_STRING) \ +UACPI_BUILD_TO_OP(Integer, 0x99, UACPI_OBJECT_INTEGER) \ +UACPI_BAD_OPCODE(0x9A) \ +UACPI_BAD_OPCODE(0x9B) \ +UACPI_OP( \ + ToStringOp, 0x9C, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_TYPECHECK, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_STRING, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 2, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + CopyObjectOp, 0x9D, \ + { \ + UACPI_PARSE_OP_TERM_ARG, \ + UACPI_PARSE_OP_OBJECT_COPY_TO_PREV, \ + UACPI_PARSE_OP_SIMPLE_NAME, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + MidOp, 0x9E, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 3, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + ContinueOp, 0x9F, \ + { \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + IfOp, 0xA0, \ + { \ + UACPI_PARSE_OP_PKGLEN, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + ElseOp, 0xA1, \ + { \ + UACPI_PARSE_OP_PKGLEN, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + WhileOp, 0xA2, \ + { \ + UACPI_PARSE_OP_PKGLEN, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + NoopOp, 0xA3, \ + { \ + UACPI_PARSE_OP_END, \ + } \ +) \ +UACPI_OP( \ + ReturnOp, 0xA4, \ + { \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + BreakOp, 0xA5, \ + { \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_BAD_OPCODE(0xA6) \ +UACPI_BAD_OPCODE(0xA7) \ +UACPI_BAD_OPCODE(0xA8) \ +UACPI_BAD_OPCODE(0xA9) \ +UACPI_BAD_OPCODE(0xAA) \ +UACPI_BAD_OPCODE(0xAB) \ +UACPI_BAD_OPCODE(0xAC) \ +UACPI_BAD_OPCODE(0xAD) \ +UACPI_BAD_OPCODE(0xAE) \ +UACPI_BAD_OPCODE(0xAF) \ +UACPI_BAD_OPCODE(0xB0) \ +UACPI_BAD_OPCODE(0xB1) \ +UACPI_BAD_OPCODE(0xB2) \ +UACPI_BAD_OPCODE(0xB3) \ +UACPI_BAD_OPCODE(0xB4) \ +UACPI_BAD_OPCODE(0xB5) \ +UACPI_BAD_OPCODE(0xB6) \ +UACPI_BAD_OPCODE(0xB7) \ +UACPI_BAD_OPCODE(0xB8) \ +UACPI_BAD_OPCODE(0xB9) \ +UACPI_BAD_OPCODE(0xBA) \ +UACPI_BAD_OPCODE(0xBB) \ +UACPI_BAD_OPCODE(0xBC) \ +UACPI_BAD_OPCODE(0xBD) \ +UACPI_BAD_OPCODE(0xBE) \ +UACPI_BAD_OPCODE(0xBF) \ +UACPI_BAD_OPCODE(0xC0) \ +UACPI_BAD_OPCODE(0xC1) \ +UACPI_BAD_OPCODE(0xC2) \ +UACPI_BAD_OPCODE(0xC3) \ +UACPI_BAD_OPCODE(0xC4) \ +UACPI_BAD_OPCODE(0xC5) \ +UACPI_BAD_OPCODE(0xC6) \ +UACPI_BAD_OPCODE(0xC7) \ +UACPI_BAD_OPCODE(0xC8) \ +UACPI_BAD_OPCODE(0xC9) \ +UACPI_BAD_OPCODE(0xCA) \ +UACPI_BAD_OPCODE(0xCB) \ +UACPI_OP( \ + BreakPointOp, 0xCC, \ + { \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_BAD_OPCODE(0xCD) \ +UACPI_BAD_OPCODE(0xCE) \ +UACPI_BAD_OPCODE(0xCF) \ +UACPI_BAD_OPCODE(0xD0) \ +UACPI_BAD_OPCODE(0xD1) \ +UACPI_BAD_OPCODE(0xD2) \ +UACPI_BAD_OPCODE(0xD3) \ +UACPI_BAD_OPCODE(0xD4) \ +UACPI_BAD_OPCODE(0xD5) \ +UACPI_BAD_OPCODE(0xD6) \ +UACPI_BAD_OPCODE(0xD7) \ +UACPI_BAD_OPCODE(0xD8) \ +UACPI_BAD_OPCODE(0xD9) \ +UACPI_BAD_OPCODE(0xDA) \ +UACPI_BAD_OPCODE(0xDB) \ +UACPI_BAD_OPCODE(0xDC) \ +UACPI_BAD_OPCODE(0xDD) \ +UACPI_BAD_OPCODE(0xDE) \ +UACPI_BAD_OPCODE(0xDF) \ +UACPI_BAD_OPCODE(0xE0) \ +UACPI_BAD_OPCODE(0xE1) \ +UACPI_BAD_OPCODE(0xE2) \ +UACPI_BAD_OPCODE(0xE3) \ +UACPI_BAD_OPCODE(0xE4) \ +UACPI_BAD_OPCODE(0xE5) \ +UACPI_BAD_OPCODE(0xE6) \ +UACPI_BAD_OPCODE(0xE7) \ +UACPI_BAD_OPCODE(0xE8) \ +UACPI_BAD_OPCODE(0xE9) \ +UACPI_BAD_OPCODE(0xEA) \ +UACPI_BAD_OPCODE(0xEB) \ +UACPI_BAD_OPCODE(0xEC) \ +UACPI_BAD_OPCODE(0xED) \ +UACPI_BAD_OPCODE(0xEE) \ +UACPI_BAD_OPCODE(0xEF) \ +UACPI_BAD_OPCODE(0xF0) \ +UACPI_BAD_OPCODE(0xF1) \ +UACPI_BAD_OPCODE(0xF2) \ +UACPI_BAD_OPCODE(0xF3) \ +UACPI_OP( \ + InternalOpReadFieldAsBuffer, 0xF4, \ + { \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_RESERVED \ +) \ +UACPI_OP( \ + InternalOpReadFieldAsInteger, 0xF5, \ + { \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_RESERVED \ +) \ +UACPI_OP( \ + InternalOpNamedObject, 0xF6, \ + { \ + UACPI_PARSE_OP_EMPTY_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_SIMPLE_NAME | \ + UACPI_OP_PROPERTY_SUPERNAME | \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_RESERVED \ +) \ +UACPI_METHOD_CALL_OPCODE(0) \ +UACPI_METHOD_CALL_OPCODE(1) \ +UACPI_METHOD_CALL_OPCODE(2) \ +UACPI_METHOD_CALL_OPCODE(3) \ +UACPI_METHOD_CALL_OPCODE(4) \ +UACPI_METHOD_CALL_OPCODE(5) \ +UACPI_METHOD_CALL_OPCODE(6) \ +UACPI_METHOD_CALL_OPCODE(7) \ +UACPI_OP( \ + OnesOp, 0xFF, \ + { \ + UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT, \ + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ + UACPI_PARSE_OP_TRUNCATE_NUMBER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +extern uacpi_u8 uacpi_field_op_decode_ops[]; +extern uacpi_u8 uacpi_index_field_op_decode_ops[]; +extern uacpi_u8 uacpi_bank_field_op_decode_ops[]; +extern uacpi_u8 uacpi_load_op_decode_ops[]; +extern uacpi_u8 uacpi_load_table_op_decode_ops[]; + +#define UACPI_BUILD_NAMED_SCOPE_OBJECT_OP(name, code, type, ...) \ +UACPI_OP( \ + name##Op, UACPI_EXT_OP(code), \ + { \ + UACPI_PARSE_OP_TRACKED_PKGLEN, \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + ##__VA_ARGS__, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 1, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, type, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 1, \ + } \ +) + +#define UACPI_BUILD_TO_FROM_BCD(type, code) \ +UACPI_OP( \ + type##BCDOp, UACPI_EXT_OP(code), \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 1, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) + +#define UACPI_ENUMERATE_EXT_OPCODES \ +UACPI_OP( \ + ReservedExtOp, UACPI_EXT_OP(0x00), \ + { \ + UACPI_PARSE_OP_BAD_OPCODE, \ + } \ +) \ +UACPI_OP( \ + MutexOp, UACPI_EXT_OP(0x01), \ + { \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_MUTEX, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 0, \ + } \ +) \ +UACPI_OP( \ + EventOp, UACPI_EXT_OP(0x02), \ + { \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_EVENT, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 0, \ + } \ +) \ +UACPI_OP( \ + CondRefOfOp, UACPI_EXT_OP(0x12), \ + { \ + UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED, \ + UACPI_PARSE_OP_TARGET, \ + UACPI_PARSE_OP_IF_NULL, 0, 3, \ + UACPI_PARSE_OP_LOAD_FALSE_OBJECT, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + UACPI_PARSE_OP_END, \ + UACPI_PARSE_OP_OBJECT_ALLOC, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_STORE_TO_TARGET, 1, \ + UACPI_PARSE_OP_LOAD_TRUE_OBJECT, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_DO_BUILD_BUFFER_FIELD_OP( \ + Create, UACPI_EXT_OP(0x13), 3, \ + UACPI_PARSE_OP_OPERAND \ +) \ +UACPI_OUT_OF_LINE_OP( \ + LoadTableOp, UACPI_EXT_OP(0x1F), \ + uacpi_load_table_op_decode_ops, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_OUT_OF_LINE \ +) \ +UACPI_OUT_OF_LINE_OP( \ + LoadOp, UACPI_EXT_OP(0x20), \ + uacpi_load_op_decode_ops, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_OUT_OF_LINE \ +) \ +UACPI_OP( \ + StallOp, UACPI_EXT_OP(0x21), \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + SleepOp, UACPI_EXT_OP(0x22), \ + { \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + AcquireOp, UACPI_EXT_OP(0x23), \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_LOAD_IMM, 2, \ + UACPI_PARSE_OP_LOAD_TRUE_OBJECT, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + SignalOp, UACPI_EXT_OP(0x24), \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + WaitOp, UACPI_EXT_OP(0x25), \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_LOAD_TRUE_OBJECT, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + ResetOp, UACPI_EXT_OP(0x26), \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + ReleaseOp, UACPI_EXT_OP(0x27), \ + { \ + UACPI_PARSE_OP_SUPERNAME, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_BUILD_TO_FROM_BCD(From, 0x28) \ +UACPI_BUILD_TO_FROM_BCD(To, 0x29) \ +UACPI_OP( \ + RevisionOp, UACPI_EXT_OP(0x30), \ + { \ + UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT, \ + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + DebugOp, UACPI_EXT_OP(0x31), \ + { \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_DEBUG, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG | \ + UACPI_OP_PROPERTY_SUPERNAME | \ + UACPI_OP_PROPERTY_TARGET \ +) \ +UACPI_OP( \ + FatalOp, UACPI_EXT_OP(0x32), \ + { \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 4, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + } \ +) \ +UACPI_OP( \ + TimerOp, UACPI_EXT_OP(0x33), \ + { \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_INTEGER, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, \ + }, \ + UACPI_OP_PROPERTY_TERM_ARG \ +) \ +UACPI_OP( \ + OpRegionOp, UACPI_EXT_OP(0x80), \ + { \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_OPERAND, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_OPERATION_REGION, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 0, \ + } \ +) \ +UACPI_OUT_OF_LINE_OP( \ + FieldOp, UACPI_EXT_OP(0x81), \ + uacpi_field_op_decode_ops, \ + UACPI_OP_PROPERTY_OUT_OF_LINE \ +) \ +UACPI_BUILD_NAMED_SCOPE_OBJECT_OP( \ + Device, 0x82, UACPI_OBJECT_DEVICE \ +) \ +UACPI_BUILD_NAMED_SCOPE_OBJECT_OP( \ + Processor, 0x83, UACPI_OBJECT_PROCESSOR, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 4, \ + UACPI_PARSE_OP_LOAD_IMM, 1 \ +) \ +UACPI_BUILD_NAMED_SCOPE_OBJECT_OP( \ + PowerRes, 0x84, UACPI_OBJECT_POWER_RESOURCE, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 2 \ +) \ +UACPI_BUILD_NAMED_SCOPE_OBJECT_OP( \ + ThermalZone, 0x85, UACPI_OBJECT_THERMAL_ZONE \ +) \ +UACPI_OUT_OF_LINE_OP( \ + IndexFieldOp, UACPI_EXT_OP(0x86), \ + uacpi_index_field_op_decode_ops, \ + UACPI_OP_PROPERTY_OUT_OF_LINE \ +) \ +UACPI_OUT_OF_LINE_OP( \ + BankFieldOp, UACPI_EXT_OP(0x87), \ + uacpi_bank_field_op_decode_ops, \ + UACPI_OP_PROPERTY_OUT_OF_LINE \ +) \ +UACPI_OP( \ + DataRegionOp, UACPI_EXT_OP(0x88), \ + { \ + UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD, \ + UACPI_PARSE_OP_STRING, \ + UACPI_PARSE_OP_STRING, \ + UACPI_PARSE_OP_STRING, \ + UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL, 0, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, \ + UACPI_OBJECT_OPERATION_REGION, \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE, 0, \ + } \ +) + +enum uacpi_aml_op { +#define UACPI_OP(name, code, ...) UACPI_AML_OP_##name = code, +#define UACPI_OUT_OF_LINE_OP(name, code, ...) UACPI_AML_OP_##name = code, + UACPI_ENUMERATE_OPCODES + UACPI_ENUMERATE_EXT_OPCODES +#undef UACPI_OP +#undef UACPI_OUT_OF_LINE_OP +}; diff --git a/src/include/uacpi/internal/opregion.h b/src/include/uacpi/internal/opregion.h new file mode 100644 index 0000000..fe44f66 --- /dev/null +++ b/src/include/uacpi/internal/opregion.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include + +uacpi_status uacpi_initialize_opregion(void); +void uacpi_deinitialize_opregion(void); + +void uacpi_trace_region_error( + uacpi_namespace_node *node, uacpi_char *message, uacpi_status ret +); +void uacpi_trace_region_io( + uacpi_namespace_node *node, uacpi_address_space space, uacpi_region_op op, + uacpi_u64 offset, uacpi_u8 byte_size, uacpi_u64 ret +); + +uacpi_status uacpi_install_address_space_handler_with_flags( + uacpi_namespace_node *device_node, enum uacpi_address_space space, + uacpi_region_handler handler, uacpi_handle handler_context, + uacpi_u16 flags +); + +void uacpi_opregion_uninstall_handler(uacpi_namespace_node *node); + +uacpi_bool uacpi_address_space_handler_is_default( + uacpi_address_space_handler *handler +); + +uacpi_address_space_handlers *uacpi_node_get_address_space_handlers( + uacpi_namespace_node *node +); + +uacpi_status uacpi_initialize_opregion_node(uacpi_namespace_node *node); + +uacpi_status uacpi_opregion_attach(uacpi_namespace_node *node); + +void uacpi_install_default_address_space_handlers(void); + +uacpi_status uacpi_dispatch_opregion_io( + uacpi_namespace_node *region_node, uacpi_u32 offset, uacpi_u8 byte_width, + uacpi_region_op op, uacpi_u64 *in_out +); diff --git a/src/include/uacpi/internal/osi.h b/src/include/uacpi/internal/osi.h new file mode 100644 index 0000000..6d7b0db --- /dev/null +++ b/src/include/uacpi/internal/osi.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +uacpi_status uacpi_initialize_interfaces(void); +void uacpi_deinitialize_interfaces(void); + +uacpi_status uacpi_handle_osi(const uacpi_char *string, uacpi_bool *out_value); diff --git a/src/include/uacpi/internal/registers.h b/src/include/uacpi/internal/registers.h new file mode 100644 index 0000000..7db0061 --- /dev/null +++ b/src/include/uacpi/internal/registers.h @@ -0,0 +1,54 @@ +#pragma once + +#include + +uacpi_status uacpi_ininitialize_registers(void); +void uacpi_deininitialize_registers(void); + +enum uacpi_register { + UACPI_REGISTER_PM1_STS = 0, + UACPI_REGISTER_PM1_EN, + UACPI_REGISTER_PM1_CNT, + UACPI_REGISTER_PM_TMR, + UACPI_REGISTER_PM2_CNT, + UACPI_REGISTER_SLP_CNT, + UACPI_REGISTER_SLP_STS, + UACPI_REGISTER_RESET, + UACPI_REGISTER_SMI_CMD, + UACPI_REGISTER_MAX = UACPI_REGISTER_SMI_CMD, +}; + +uacpi_status uacpi_read_register(enum uacpi_register, uacpi_u64*); + +uacpi_status uacpi_write_register(enum uacpi_register, uacpi_u64); +uacpi_status uacpi_write_registers(enum uacpi_register, uacpi_u64, uacpi_u64); + +enum uacpi_register_field { + UACPI_REGISTER_FIELD_TMR_STS = 0, + UACPI_REGISTER_FIELD_BM_STS, + UACPI_REGISTER_FIELD_GBL_STS, + UACPI_REGISTER_FIELD_PWRBTN_STS, + UACPI_REGISTER_FIELD_SLPBTN_STS, + UACPI_REGISTER_FIELD_RTC_STS, + UACPI_REGISTER_FIELD_PCIEX_WAKE_STS, + UACPI_REGISTER_FIELD_HWR_WAK_STS, + UACPI_REGISTER_FIELD_WAK_STS, + UACPI_REGISTER_FIELD_TMR_EN, + UACPI_REGISTER_FIELD_GBL_EN, + UACPI_REGISTER_FIELD_PWRBTN_EN, + UACPI_REGISTER_FIELD_SLPBTN_EN, + UACPI_REGISTER_FIELD_RTC_EN, + UACPI_REGISTER_FIELD_PCIEXP_WAKE_DIS, + UACPI_REGISTER_FIELD_SCI_EN, + UACPI_REGISTER_FIELD_BM_RLD, + UACPI_REGISTER_FIELD_GBL_RLS, + UACPI_REGISTER_FIELD_SLP_TYP, + UACPI_REGISTER_FIELD_HWR_SLP_TYP, + UACPI_REGISTER_FIELD_SLP_EN, + UACPI_REGISTER_FIELD_HWR_SLP_EN, + UACPI_REGISTER_FIELD_ARB_DIS, + UACPI_REGISTER_FIELD_MAX = UACPI_REGISTER_FIELD_ARB_DIS, +}; + +uacpi_status uacpi_read_register_field(enum uacpi_register_field, uacpi_u64*); +uacpi_status uacpi_write_register_field(enum uacpi_register_field, uacpi_u64); diff --git a/src/include/uacpi/internal/resources.h b/src/include/uacpi/internal/resources.h new file mode 100644 index 0000000..3d0b48a --- /dev/null +++ b/src/include/uacpi/internal/resources.h @@ -0,0 +1,323 @@ +#pragma once + +#include +#include + +enum uacpi_aml_resource { + UACPI_AML_RESOURCE_TYPE_INVALID = 0, + + // Small resources + UACPI_AML_RESOURCE_IRQ, + UACPI_AML_RESOURCE_DMA, + UACPI_AML_RESOURCE_START_DEPENDENT, + UACPI_AML_RESOURCE_END_DEPENDENT, + UACPI_AML_RESOURCE_IO, + UACPI_AML_RESOURCE_FIXED_IO, + UACPI_AML_RESOURCE_FIXED_DMA, + UACPI_AML_RESOURCE_VENDOR_TYPE0, + UACPI_AML_RESOURCE_END_TAG, + + // Large resources + UACPI_AML_RESOURCE_MEMORY24, + UACPI_AML_RESOURCE_GENERIC_REGISTER, + UACPI_AML_RESOURCE_VENDOR_TYPE1, + UACPI_AML_RESOURCE_MEMORY32, + UACPI_AML_RESOURCE_FIXED_MEMORY32, + UACPI_AML_RESOURCE_ADDRESS32, + UACPI_AML_RESOURCE_ADDRESS16, + UACPI_AML_RESOURCE_EXTENDED_IRQ, + UACPI_AML_RESOURCE_ADDRESS64, + UACPI_AML_RESOURCE_ADDRESS64_EXTENDED, + UACPI_AML_RESOURCE_GPIO_CONNECTION, + UACPI_AML_RESOURCE_PIN_FUNCTION, + UACPI_AML_RESOURCE_SERIAL_CONNECTION, + UACPI_AML_RESOURCE_PIN_CONFIGURATION, + UACPI_AML_RESOURCE_PIN_GROUP, + UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION, + UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION, + UACPI_AML_RESOURCE_CLOCK_INPUT, + UACPI_AML_RESOURCE_MAX = UACPI_AML_RESOURCE_CLOCK_INPUT, +}; + +enum uacpi_aml_resource_size_kind { + UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + UACPI_AML_RESOURCE_SIZE_KIND_FIXED_OR_ONE_LESS, + UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, +}; + +enum uacpi_aml_resource_kind { + UACPI_AML_RESOURCE_KIND_SMALL = 0, + UACPI_AML_RESOURCE_KIND_LARGE, +}; + +enum uacpi_resource_convert_opcode { + UACPI_RESOURCE_CONVERT_OPCODE_END = 0, + + /* + * AML -> native: + * Take the mask at 'aml_offset' and convert to an array of uacpi_u8 + * at 'native_offset' with the value corresponding to the bit index. + * The array size is written to the byte at offset 'arg2'. + * + * native -> AML: + * Walk each element of the array at 'native_offset' and set the + * corresponding bit in the mask at 'aml_offset' to 1. The array size is + * read from the byte at offset 'arg2'. + */ + UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_8, + UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_16, + + /* + * AML -> native: + * Grab the bits at the byte at 'aml_offset' + 'bit_index', and copy its + * value into the byte at 'native_offset'. + * + * native -> AML: + * Grab first N bits at 'native_offset' and copy to 'aml_offset' starting + * at the 'bit_index'. + * + * NOTE: + * These must be contiguous in this order. + */ + UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_1, + UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_2, + UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_3, + UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_6 = + UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_3 + 3, + + /* + * AML -> native: + * Copy N bytes at 'aml_offset' to 'native_offset'. + * + * native -> AML: + * Copy N bytes at 'native_offset' to 'aml_offset'. + * + * 'imm' is added to the accumulator. + * + * NOTE: These are affected by the current value in the accumulator. If it's + * set to 0 at the time of evalution, this is executed once, N times + * otherwise. 0xFF is considered a special value, which resets the + * accumulator to 0 unconditionally. + */ + UACPI_RESOURCE_CONVERT_OPCODE_FIELD_8, + UACPI_RESOURCE_CONVERT_OPCODE_FIELD_16, + UACPI_RESOURCE_CONVERT_OPCODE_FIELD_32, + UACPI_RESOURCE_CONVERT_OPCODE_FIELD_64, + + /* + * If the length of the current resource is less than 'arg0', then skip + * 'imm' instructions. + */ + UACPI_RESOURCE_CONVERT_OPCODE_SKIP_IF_AML_SIZE_LESS_THAN, + + /* + * Skip 'imm' instructions if 'arg0' is not equal to the value in the + * accumulator. + */ + UACPI_RESOURCE_CONVERT_OPCODE_SKIP_IF_NOT_EQUALS, + + /* + * AML -> native: + * Set the byte at 'native_offset' to 'imm'. + * + * native -> AML: + * Set the byte at 'aml_offset' to 'imm'. + */ + UACPI_RESOURCE_CONVERT_OPCODE_SET_TO_IMM, + + /* + * AML -> native: + * Load the AML resoruce length into the accumulator as well as the field at + * 'native_offset' of width N. + * + * native -> AML: + * Load the resource length into the accumulator. + */ + UACPI_RESOURCE_CONVERT_OPCODE_LOAD_AML_SIZE_32, + + /* + * AML -> native: + * Load the 8 bit field at 'aml_offset' into the accumulator and store at + * 'native_offset'. + * + * native -> AML: + * Load the 8 bit field at 'native_offset' into the accumulator and store + * at 'aml_offset'. + * + * The accumulator is multiplied by 'imm' unless it's set to zero. + */ + UACPI_RESOURCE_CONVERT_OPCODE_LOAD_8_STORE, + + /* + * Load the N bit field at 'native_offset' into the accumulator + */ + UACPI_RESOURCE_CONVERT_OPCODE_LOAD_8_NATIVE, + UACPI_RESOURCE_CONVERT_OPCODE_LOAD_16_NATIVE, + + /* + * Load 'imm' into the accumulator. + */ + UACPI_RESOURCE_CONVERT_OPCODE_LOAD_IMM, + + /* + * AML -> native: + * Load the resource source at offset = aml size + accumulator into the + * uacpi_resource_source struct at 'native_offset'. The string bytes are + * written to the offset at resource size + accumulator. The presence is + * detected by comparing the length of the resource to the offset, + * 'arg2' optionally specifies the offset to the upper bound of the string. + * + * native -> AML: + * Load the resource source from the uacpi_resource_source struct at + * 'native_offset' to aml_size + accumulator. aml_size + accumulator is + * optionally written to 'aml_offset' if it's specified. + */ + UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE, + UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE_NO_INDEX, + UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_LABEL, + + /* + * AML -> native: + * Load the pin table with upper bound specified at 'aml_offset'. + * The table length is calculated by subtracting the upper bound from + * aml_size and is written into the accumulator. + * + * native -> AML: + * Load the pin table length from 'native_offset' and multiply by 2, store + * the result in the accumulator. + */ + UACPI_RESOURCE_CONVERT_OPCODE_LOAD_PIN_TABLE_LENGTH, + + /* + * AML -> native: + * Store the accumulator divided by 2 at 'native_offset'. + * The table is copied to the offset at resource size from offset at + * aml_size with the pointer written to the offset at 'arg2'. + * + * native -> AML: + * Read the pin table from resource size offset, write aml_size to + * 'aml_offset'. Copy accumulator bytes to the offset at aml_size. + */ + UACPI_RESOURCE_CONVERT_OPCODE_PIN_TABLE, + + /* + * AML -> native: + * Load vendor data with offset stored at 'aml_offset'. The length is + * calculated as aml_size - aml_offset and is written to 'native_offset'. + * The data is written to offset - aml_size with the pointer written back + * to the offset at 'arg2'. + * + * native -> AML: + * Read vendor data from the pointer at offset 'arg2' and size at + * 'native_offset', the offset to write to is calculated as the difference + * between the data pointer and the native resource end pointer. + * offset + aml_size is written to 'aml_offset' and the data is copied + * there as well. + */ + UACPI_RESOURCE_CONVERT_OPCODE_VENDOR_DATA, + + /* + * AML -> native: + * Read the serial type from the byte at 'aml_offset' and write it to the + * type field of the uacpi_resource_serial_bus_common structure. Convert + * the serial type to native and set the resource type to it. Copy the + * vendor data to the offset at native size, the length is calculated + * as type_data_length - extra-type-specific-size, and is written to + * vendor_data_length, as well as the accumulator. The data pointer is + * written to vendor_data. + * + * native -> AML: + * Set the serial type at 'aml_offset' to the value stored at + * 'native_offset'. Load the vendor data to the offset at aml_size, + * the length is read from 'vendor_data_length', and the data is copied from + * 'vendor_data'. + */ + UACPI_RESOURCE_CONVERT_OPCODE_SERIAL_TYPE_SPECIFIC, + + /* + * Produces an error if encountered in the instruction stream. + * Used to trap invalid/unexpected code flow. + */ + UACPI_RESOURCE_CONVERT_OPCODE_UNREACHABLE, +}; + +struct uacpi_resource_convert_instruction { + uacpi_u8 code; + + union { + uacpi_u8 aml_offset; + uacpi_u8 arg0; + }; + + union { + uacpi_u8 native_offset; + uacpi_u8 arg1; + }; + + union { + uacpi_u8 imm; + uacpi_u8 bit_index; + uacpi_u8 arg2; + }; +}; + +struct uacpi_resource_spec { + uacpi_u8 type : 5; + uacpi_u8 native_type : 5; + uacpi_u8 resource_kind : 1; + uacpi_u8 size_kind : 2; + + /* + * Size of the resource as appears in the AML byte stream, for variable + * length resources this is the minimum. + */ + uacpi_u16 aml_size; + + /* + * Size of the native human-readable uacpi resource, for variable length + * resources this is the minimum. The final length is this field plus the + * result of extra_size_for_native(). + */ + uacpi_u16 native_size; + + /* + * Calculate the amount of extra bytes that must be allocated for a specific + * native resource given the AML counterpart. This being NULL means no extra + * bytes are needed, aka native resources is always the same size. + */ + uacpi_size (*extra_size_for_native)( + const struct uacpi_resource_spec*, void*, uacpi_size + ); + + /* + * Calculate the number of bytes needed to represent a native resource as + * AML. The 'aml_size' field is used if this is NULL. + */ + uacpi_size (*size_for_aml)( + const struct uacpi_resource_spec*, uacpi_resource* + ); + + const struct uacpi_resource_convert_instruction *to_native; + const struct uacpi_resource_convert_instruction *to_aml; +}; + +typedef uacpi_iteration_decision (*uacpi_aml_resource_iteration_callback)( + void*, uacpi_u8 *data, uacpi_u16 resource_size, + const struct uacpi_resource_spec* +); + +uacpi_status uacpi_for_each_aml_resource( + uacpi_buffer *buffer, uacpi_aml_resource_iteration_callback cb, void *user +); + +uacpi_status uacpi_find_aml_resource_end_tag( + uacpi_buffer *buffer, uacpi_size *out_offset +); + +uacpi_status uacpi_native_resources_from_aml( + uacpi_buffer *aml_buffer, uacpi_resources **out_resources +); + +uacpi_status uacpi_native_resources_to_aml( + uacpi_resources *resources, uacpi_object **out_template +); diff --git a/src/include/uacpi/internal/shareable.h b/src/include/uacpi/internal/shareable.h new file mode 100644 index 0000000..e00d850 --- /dev/null +++ b/src/include/uacpi/internal/shareable.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +struct uacpi_shareable { + uacpi_u32 reference_count; +}; + +void uacpi_shareable_init(uacpi_handle); + +uacpi_bool uacpi_bugged_shareable(uacpi_handle); +void uacpi_make_shareable_bugged(uacpi_handle); + +uacpi_u32 uacpi_shareable_ref(uacpi_handle); +uacpi_u32 uacpi_shareable_unref(uacpi_handle); + +void uacpi_shareable_unref_and_delete_if_last( + uacpi_handle, void (*do_free)(uacpi_handle) +); + +uacpi_u32 uacpi_shareable_refcount(uacpi_handle); diff --git a/src/include/uacpi/internal/stdlib.h b/src/include/uacpi/internal/stdlib.h new file mode 100644 index 0000000..1ff7e34 --- /dev/null +++ b/src/include/uacpi/internal/stdlib.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include + +#ifndef uacpi_memcpy +void *uacpi_memcpy(void *dest, const void *src, uacpi_size count); +#endif + +#ifndef uacpi_memmove +void *uacpi_memmove(void *dest, const void *src, uacpi_size count); +#endif + +#ifndef uacpi_memset +void *uacpi_memset(void *dest, uacpi_i32 ch, uacpi_size count); +#endif + +#ifndef uacpi_memcmp +uacpi_i32 uacpi_memcmp(const void *lhs, const void *rhs, uacpi_size count); +#endif + +#ifndef uacpi_strlen +uacpi_size uacpi_strlen(const uacpi_char *str); +#endif + +#ifndef uacpi_strnlen +uacpi_size uacpi_strnlen(const uacpi_char *str, uacpi_size max); +#endif + +#ifndef uacpi_strcmp +uacpi_i32 uacpi_strcmp(const uacpi_char *lhs, const uacpi_char *rhs); +#endif + +#ifndef uacpi_snprintf +UACPI_PRINTF_DECL(3, 4) +uacpi_i32 uacpi_snprintf( + uacpi_char *buffer, uacpi_size capacity, const uacpi_char *fmt, ... +); +#endif + +#ifndef uacpi_vsnprintf +uacpi_i32 uacpi_vsnprintf( + uacpi_char *buffer, uacpi_size capacity, const uacpi_char *fmt, + uacpi_va_list vlist +); +#endif + +#ifdef UACPI_SIZED_FREES +#define uacpi_free(mem, size) uacpi_kernel_free(mem, size) +#else +#define uacpi_free(mem, _) uacpi_kernel_free(mem) +#endif + +#define uacpi_memzero(ptr, size) uacpi_memset(ptr, 0, size) + +#define UACPI_COMPARE(x, y, op) ((x) op (y) ? (x) : (y)) +#define UACPI_MIN(x, y) UACPI_COMPARE(x, y, <) +#define UACPI_MAX(x, y) UACPI_COMPARE(x, y, >) + +#define UACPI_ALIGN_UP_MASK(x, mask) (((x) + (mask)) & ~(mask)) +#define UACPI_ALIGN_UP(x, val, type) UACPI_ALIGN_UP_MASK(x, (type)(val) - 1) + +#define UACPI_ALIGN_DOWN_MASK(x, mask) ((x) & ~(mask)) +#define UACPI_ALIGN_DOWN(x, val, type) UACPI_ALIGN_DOWN_MASK(x, (type)(val) - 1) + +#define UACPI_IS_ALIGNED_MASK(x, mask) (((x) & (mask)) == 0) +#define UACPI_IS_ALIGNED(x, val, type) UACPI_IS_ALIGNED_MASK(x, (type)(val) - 1) + +#define UACPI_IS_POWER_OF_TWO(x, type) UACPI_IS_ALIGNED(x, x, type) + +void uacpi_memcpy_zerout(void *dst, const void *src, + uacpi_size dst_size, uacpi_size src_size); + +// Returns the one-based bit location of LSb or 0 +uacpi_u8 uacpi_bit_scan_forward(uacpi_u64); + +// Returns the one-based bit location of MSb or 0 +uacpi_u8 uacpi_bit_scan_backward(uacpi_u64); + +uacpi_u8 uacpi_popcount(uacpi_u64); + +#ifndef UACPI_NATIVE_ALLOC_ZEROED +void *uacpi_builtin_alloc_zeroed(uacpi_size size); +#define uacpi_kernel_alloc_zeroed uacpi_builtin_alloc_zeroed +#endif diff --git a/src/include/uacpi/internal/tables.h b/src/include/uacpi/internal/tables.h new file mode 100644 index 0000000..35e1b16 --- /dev/null +++ b/src/include/uacpi/internal/tables.h @@ -0,0 +1,66 @@ +#pragma once + +#include +#include +#include +#include +#include + +enum uacpi_table_origin { + UACPI_TABLE_ORIGIN_FIRMWARE_VIRTUAL = 0, + UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL, + + UACPI_TABLE_ORIGIN_HOST_VIRTUAL, + UACPI_TABLE_ORIGIN_HOST_PHYSICAL, +}; + +struct uacpi_installed_table { + uacpi_phys_addr phys_addr; + struct acpi_sdt_hdr hdr; + void *ptr; + + uacpi_u16 reference_count; + +#define UACPI_TABLE_LOADED (1 << 0) +#define UACPI_TABLE_CSUM_VERIFIED (1 << 1) +#define UACPI_TABLE_INVALID (1 << 2) + uacpi_u8 flags; + uacpi_u8 origin; +}; + +uacpi_status uacpi_initialize_tables(void); +void uacpi_deinitialize_tables(void); + +uacpi_bool uacpi_signatures_match(const void *const lhs, const void *const rhs); +uacpi_status uacpi_check_table_signature(void *table, const uacpi_char *expect); +uacpi_status uacpi_verify_table_checksum(void *table, uacpi_size size); + +uacpi_status uacpi_table_install_physical_with_origin( + uacpi_phys_addr phys, enum uacpi_table_origin origin, uacpi_table *out_table +); +uacpi_status uacpi_table_install_with_origin( + void *virt, enum uacpi_table_origin origin, uacpi_table *out_table +); + +void uacpi_table_mark_as_loaded(uacpi_size idx); + +uacpi_status uacpi_table_load_with_cause( + uacpi_size idx, enum uacpi_table_load_cause cause +); + +typedef uacpi_iteration_decision (*uacpi_table_iteration_callback) + (void *user, struct uacpi_installed_table *tbl, uacpi_size idx); + +uacpi_status uacpi_for_each_table( + uacpi_size base_idx, uacpi_table_iteration_callback, void *user +); + +typedef uacpi_bool (*uacpi_table_match_callback) + (struct uacpi_installed_table *tbl); + +uacpi_status uacpi_table_match( + uacpi_size base_idx, uacpi_table_match_callback, uacpi_table *out_table +); + +#define UACPI_PRI_TBL_HDR "'%.4s' (OEM ID '%.6s' OEM Table ID '%.8s')" +#define UACPI_FMT_TBL_HDR(hdr) (hdr)->signature, (hdr)->oemid, (hdr)->oem_table_id diff --git a/src/include/uacpi/internal/types.h b/src/include/uacpi/internal/types.h new file mode 100644 index 0000000..bbc9f61 --- /dev/null +++ b/src/include/uacpi/internal/types.h @@ -0,0 +1,311 @@ +#pragma once + +#include +#include +#include + +// object->flags field if object->type == UACPI_OBJECT_REFERENCE +enum uacpi_reference_kind { + UACPI_REFERENCE_KIND_REFOF = 0, + UACPI_REFERENCE_KIND_LOCAL = 1, + UACPI_REFERENCE_KIND_ARG = 2, + UACPI_REFERENCE_KIND_NAMED = 3, + UACPI_REFERENCE_KIND_PKG_INDEX = 4, +}; + +// object->flags field if object->type == UACPI_OBJECT_STRING +enum uacpi_string_kind { + UACPI_STRING_KIND_NORMAL = 0, + UACPI_STRING_KIND_PATH, +}; + +typedef struct uacpi_buffer { + struct uacpi_shareable shareable; + union { + void *data; + uacpi_u8 *byte_data; + uacpi_char *text; + }; + uacpi_size size; +} uacpi_buffer; + +typedef struct uacpi_package { + struct uacpi_shareable shareable; + uacpi_object **objects; + uacpi_size count; +} uacpi_package; + +typedef struct uacpi_buffer_field { + uacpi_buffer *backing; + uacpi_size bit_index; + uacpi_u32 bit_length; + uacpi_bool force_buffer; +} uacpi_buffer_field; + +typedef struct uacpi_buffer_index { + uacpi_size idx; + uacpi_buffer *buffer; +} uacpi_buffer_index; + +typedef struct uacpi_mutex { + struct uacpi_shareable shareable; + uacpi_handle handle; + uacpi_thread_id owner; + uacpi_u16 depth; + uacpi_u8 sync_level; +} uacpi_mutex; + +typedef struct uacpi_event { + struct uacpi_shareable shareable; + uacpi_handle handle; +} uacpi_event; + +typedef struct uacpi_address_space_handler { + struct uacpi_shareable shareable; + uacpi_region_handler callback; + uacpi_handle user_context; + struct uacpi_address_space_handler *next; + struct uacpi_operation_region *regions; + uacpi_u16 space; + +#define UACPI_ADDRESS_SPACE_HANDLER_DEFAULT (1 << 0) + uacpi_u16 flags; +} uacpi_address_space_handler; + +/* + * NOTE: These are common object headers. + * Any changes to these structs must be propagated to all objects. + * ============================================================== + * Common for the following objects: + * - UACPI_OBJECT_OPERATION_REGION + * - UACPI_OBJECT_PROCESSOR + * - UACPI_OBJECT_DEVICE + * - UACPI_OBJECT_THERMAL_ZONE + */ +typedef struct uacpi_address_space_handlers { + struct uacpi_shareable shareable; + uacpi_address_space_handler *head; +} uacpi_address_space_handlers; + +typedef struct uacpi_device_notify_handler { + uacpi_notify_handler callback; + uacpi_handle user_context; + struct uacpi_device_notify_handler *next; +} uacpi_device_notify_handler; + +/* + * Common for the following objects: + * - UACPI_OBJECT_PROCESSOR + * - UACPI_OBJECT_DEVICE + * - UACPI_OBJECT_THERMAL_ZONE + */ +typedef struct uacpi_handlers { + struct uacpi_shareable shareable; + uacpi_address_space_handler *address_space_head; + uacpi_device_notify_handler *notify_head; +} uacpi_handlers; + +// This region has a corresponding _REG method that was succesfully executed +#define UACPI_OP_REGION_STATE_REG_EXECUTED (1 << 0) + +// This region was successfully attached to a handler +#define UACPI_OP_REGION_STATE_ATTACHED (1 << 1) + +typedef struct uacpi_operation_region { + struct uacpi_shareable shareable; + uacpi_address_space_handler *handler; + uacpi_handle user_context; + uacpi_u16 space; + uacpi_u8 state_flags; + uacpi_u64 offset; + uacpi_u64 length; + + // If space == TABLE_DATA + uacpi_u64 table_idx; + + // Used to link regions sharing the same handler + struct uacpi_operation_region *next; +} uacpi_operation_region; + +typedef struct uacpi_device { + struct uacpi_shareable shareable; + uacpi_address_space_handler *address_space_handlers; + uacpi_device_notify_handler *notify_handlers; +} uacpi_device; + +typedef struct uacpi_processor { + struct uacpi_shareable shareable; + uacpi_address_space_handler *address_space_handlers; + uacpi_device_notify_handler *notify_handlers; + uacpi_u8 id; + uacpi_u32 block_address; + uacpi_u8 block_length; +} uacpi_processor; + +typedef struct uacpi_thermal_zone { + struct uacpi_shareable shareable; + uacpi_address_space_handler *address_space_handlers; + uacpi_device_notify_handler *notify_handlers; +} uacpi_thermal_zone; + +typedef struct uacpi_power_resource { + uacpi_u8 system_level; + uacpi_u16 resource_order; +} uacpi_power_resource; + +typedef uacpi_status (*uacpi_native_call_handler)( + uacpi_handle ctx, uacpi_object *retval +); + +typedef struct uacpi_control_method { + struct uacpi_shareable shareable; + union { + uacpi_u8 *code; + uacpi_native_call_handler handler; + }; + uacpi_mutex *mutex; + uacpi_u32 size; + uacpi_u8 sync_level : 4; + uacpi_u8 args : 3; + uacpi_u8 is_serialized : 1; + uacpi_u8 named_objects_persist: 1; + uacpi_u8 native_call : 1; + uacpi_u8 owns_code : 1; +} uacpi_control_method; + +typedef enum uacpi_access_type { + UACPI_ACCESS_TYPE_ANY = 0, + UACPI_ACCESS_TYPE_BYTE = 1, + UACPI_ACCESS_TYPE_WORD = 2, + UACPI_ACCESS_TYPE_DWORD = 3, + UACPI_ACCESS_TYPE_QWORD = 4, + UACPI_ACCESS_TYPE_BUFFER = 5, +} uacpi_access_type; + +typedef enum uacpi_access_attributes { + UACPI_ACCESS_ATTRIBUTE_QUICK = 0x02, + UACPI_ACCESS_ATTRIBUTE_SEND_RECEIVE = 0x04, + UACPI_ACCESS_ATTRIBUTE_BYTE = 0x06, + UACPI_ACCESS_ATTRIBUTE_WORD = 0x08, + UACPI_ACCESS_ATTRIBUTE_BLOCK = 0x0A, + UACPI_ACCESS_ATTRIBUTE_BYTES = 0x0B, + UACPI_ACCESS_ATTRIBUTE_PROCESS_CALL = 0x0C, + UACPI_ACCESS_ATTRIBUTE_BLOCK_PROCESS_CALL = 0x0D, + UACPI_ACCESS_ATTRIBUTE_RAW_BYTES = 0x0E, + UACPI_ACCESS_ATTRIBUTE_RAW_PROCESS_BYTES = 0x0F, +} uacpi_access_attributes; + +typedef enum uacpi_lock_rule { + UACPI_LOCK_RULE_NO_LOCK = 0, + UACPI_LOCK_RULE_LOCK = 1, +} uacpi_lock_rule; + +typedef enum uacpi_update_rule { + UACPI_UPDATE_RULE_PRESERVE = 0, + UACPI_UPDATE_RULE_WRITE_AS_ONES = 1, + UACPI_UPDATE_RULE_WRITE_AS_ZEROES = 2, +} uacpi_update_rule; + +typedef enum uacpi_field_unit_kind { + UACPI_FIELD_UNIT_KIND_NORMAL = 0, + UACPI_FIELD_UNIT_KIND_INDEX = 1, + UACPI_FIELD_UNIT_KIND_BANK = 2, +} uacpi_field_unit_kind; + +typedef struct uacpi_field_unit { + struct uacpi_shareable shareable; + + union { + // UACPI_FIELD_UNIT_KIND_NORMAL + struct { + uacpi_namespace_node *region; + }; + + // UACPI_FIELD_UNIT_KIND_INDEX + struct { + struct uacpi_field_unit *index; + struct uacpi_field_unit *data; + }; + + // UACPI_FIELD_UNIT_KIND_BANK + struct { + uacpi_namespace_node *bank_region; + struct uacpi_field_unit *bank_selection; + uacpi_u64 bank_value; + }; + }; + + uacpi_object *connection; + + uacpi_u32 byte_offset; + uacpi_u32 bit_length; + uacpi_u8 bit_offset_within_first_byte; + uacpi_u8 access_width_bytes; + uacpi_u8 access_length; + + uacpi_u8 attributes : 4; + uacpi_u8 update_rule : 2; + uacpi_u8 kind : 2; + uacpi_u8 lock_rule : 1; +} uacpi_field_unit; + +typedef struct uacpi_object { + struct uacpi_shareable shareable; + uacpi_u8 type; + uacpi_u8 flags; + + union { + uacpi_u64 integer; + uacpi_package *package; + uacpi_buffer_field buffer_field; + uacpi_object *inner_object; + uacpi_control_method *method; + uacpi_buffer *buffer; + uacpi_mutex *mutex; + uacpi_event *event; + uacpi_buffer_index buffer_index; + uacpi_operation_region *op_region; + uacpi_device *device; + uacpi_processor *processor; + uacpi_thermal_zone *thermal_zone; + uacpi_address_space_handlers *address_space_handlers; + uacpi_handlers *handlers; + uacpi_power_resource power_resource; + uacpi_field_unit *field_unit; + }; +} uacpi_object; + +uacpi_object *uacpi_create_object(uacpi_object_type type); + +enum uacpi_assign_behavior { + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY, + UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY, +}; + +uacpi_status uacpi_object_assign(uacpi_object *dst, uacpi_object *src, + enum uacpi_assign_behavior); + +void uacpi_object_attach_child(uacpi_object *parent, uacpi_object *child); +void uacpi_object_detach_child(uacpi_object *parent); + +struct uacpi_object *uacpi_create_internal_reference( + enum uacpi_reference_kind kind, uacpi_object *child +); +uacpi_object *uacpi_unwrap_internal_reference(uacpi_object *object); + +enum uacpi_prealloc_objects { + UACPI_PREALLOC_OBJECTS_NO, + UACPI_PREALLOC_OBJECTS_YES, +}; + +uacpi_bool uacpi_package_fill( + uacpi_package *pkg, uacpi_size num_elements, + enum uacpi_prealloc_objects prealloc_objects +); + +uacpi_mutex *uacpi_create_mutex(void); +void uacpi_mutex_unref(uacpi_mutex*); + +void uacpi_method_unref(uacpi_control_method*); + +void uacpi_address_space_handler_unref(uacpi_address_space_handler *handler); diff --git a/src/include/uacpi/internal/utilities.h b/src/include/uacpi/internal/utilities.h new file mode 100644 index 0000000..606ec92 --- /dev/null +++ b/src/include/uacpi/internal/utilities.h @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include + +static inline uacpi_phys_addr uacpi_truncate_phys_addr_with_warn(uacpi_u64 large_addr) +{ + if (sizeof(uacpi_phys_addr) < 8 && large_addr > 0xFFFFFFFF) { + uacpi_warn( + "truncating a physical address 0x%"UACPI_PRIX64 + " outside of address space\n", UACPI_FMT64(large_addr) + ); + } + + return (uacpi_phys_addr)large_addr; +} + +#define UACPI_PTR_TO_VIRT_ADDR(ptr) ((uacpi_virt_addr)(ptr)) +#define UACPI_VIRT_ADDR_TO_PTR(vaddr) ((void*)(vaddr)) + +#define UACPI_PTR_ADD(ptr, value) ((void*)(((uacpi_u8*)(ptr)) + value)) + +/* + * Target buffer must have a length of at least 8 bytes. + */ +void uacpi_eisa_id_to_string(uacpi_u32, uacpi_char *out_string); + +enum uacpi_base { + UACPI_BASE_AUTO, + UACPI_BASE_OCT = 8, + UACPI_BASE_DEC = 10, + UACPI_BASE_HEX = 16, +}; +uacpi_status uacpi_string_to_integer( + const uacpi_char *str, uacpi_size max_chars, enum uacpi_base base, + uacpi_u64 *out_value +); + +uacpi_bool uacpi_is_valid_nameseg(uacpi_u8 *nameseg); + +void uacpi_free_dynamic_string(const uacpi_char *str); + +#define UACPI_NANOSECONDS_PER_SEC (1000ull * 1000ull * 1000ull) diff --git a/src/include/uacpi/io.h b/src/include/uacpi/io.h new file mode 100644 index 0000000..0d6c3c7 --- /dev/null +++ b/src/include/uacpi/io.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uacpi_status uacpi_gas_read(const struct acpi_gas *gas, uacpi_u64 *value); +uacpi_status uacpi_gas_write(const struct acpi_gas *gas, uacpi_u64 value); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/kernel_api.h b/src/include/uacpi/kernel_api.h new file mode 100644 index 0000000..f07f6cb --- /dev/null +++ b/src/include/uacpi/kernel_api.h @@ -0,0 +1,289 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Convenience initialization/deinitialization hooks that will be called by + * uACPI automatically when appropriate if compiled-in. + */ +#ifdef UACPI_KERNEL_INITIALIZATION +/* + * This API is invoked for each initialization level so that appropriate parts + * of the host kernel and/or glue code can be initialized at different stages. + * + * uACPI API that triggers calls to uacpi_kernel_initialize and the respective + * 'current_init_lvl' passed to the hook at that stage: + * 1. uacpi_initialize() -> UACPI_INIT_LEVEL_EARLY + * 2. uacpi_namespace_load() -> UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED + * 3. (start of) uacpi_namespace_initialize() -> UACPI_INIT_LEVEL_NAMESPACE_LOADED + * 4. (end of) uacpi_namespace_initialize() -> UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED + */ +uacpi_status uacpi_kernel_initialize(uacpi_init_level current_init_lvl); +void uacpi_kernel_deinitialize(void); +#endif + +// Returns the PHYSICAL address of the RSDP structure via *out_rsdp_address. +uacpi_status uacpi_kernel_get_rsdp(uacpi_phys_addr *out_rsdp_address); + +/* + * Open a PCI device at 'address' for reading & writing. + * + * The handle returned via 'out_handle' is used to perform IO on the + * configuration space of the device. + */ +uacpi_status uacpi_kernel_pci_device_open( + uacpi_pci_address address, uacpi_handle *out_handle +); +void uacpi_kernel_pci_device_close(uacpi_handle); + +/* + * Read & write the configuration space of a previously open PCI device. + * + * NOTE: + * 'byte_width' is ALWAYS one of 1, 2, 4. Since PCI registers are 32 bits wide + * this must be able to handle e.g. a 1-byte access by reading at the nearest + * 4-byte aligned offset below, then masking the value to select the target + * byte. + */ +uacpi_status uacpi_kernel_pci_read( + uacpi_handle device, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 *value +); +uacpi_status uacpi_kernel_pci_write( + uacpi_handle device, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 value +); + +/* + * Map a SystemIO address at [base, base + len) and return a kernel-implemented + * handle that can be used for reading and writing the IO range. + */ +uacpi_status uacpi_kernel_io_map( + uacpi_io_addr base, uacpi_size len, uacpi_handle *out_handle +); +void uacpi_kernel_io_unmap(uacpi_handle handle); + +/* + * Read/Write the IO range mapped via uacpi_kernel_io_map + * at a 0-based 'offset' within the range. + * + * NOTE: + * 'byte_width' is ALWAYS one of 1, 2, 4. You are NOT allowed to break e.g. a + * 4-byte access into four 1-byte accesses. Hardware ALWAYS expects accesses to + * be of the exact width. + */ +uacpi_status uacpi_kernel_io_read( + uacpi_handle, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 *value +); +uacpi_status uacpi_kernel_io_write( + uacpi_handle, uacpi_size offset, + uacpi_u8 byte_width, uacpi_u64 value +); + +void *uacpi_kernel_map(uacpi_phys_addr addr, uacpi_size len); +void uacpi_kernel_unmap(void *addr, uacpi_size len); + +/* + * Allocate a block of memory of 'size' bytes. + * The contents of the allocated memory are unspecified. + */ +void *uacpi_kernel_alloc(uacpi_size size); + +#ifdef UACPI_NATIVE_ALLOC_ZEROED +/* + * Allocate a block of memory of 'size' bytes. + * The returned memory block is expected to be zero-filled. + */ +void *uacpi_kernel_alloc_zeroed(uacpi_size size); +#endif + +/* + * Free a previously allocated memory block. + * + * 'mem' might be a NULL pointer. In this case, the call is assumed to be a + * no-op. + * + * An optionally enabled 'size_hint' parameter contains the size of the original + * allocation. Note that in some scenarios this incurs additional cost to + * calculate the object size. + */ +#ifndef UACPI_SIZED_FREES +void uacpi_kernel_free(void *mem); +#else +void uacpi_kernel_free(void *mem, uacpi_size size_hint); +#endif + +#ifndef UACPI_FORMATTED_LOGGING +void uacpi_kernel_log(uacpi_log_level, const uacpi_char*); +#else +UACPI_PRINTF_DECL(2, 3) +void uacpi_kernel_log(uacpi_log_level, const uacpi_char*, ...); +void uacpi_kernel_vlog(uacpi_log_level, const uacpi_char*, uacpi_va_list); +#endif + +/* + * Returns the number of nanosecond ticks elapsed since boot, + * strictly monotonic. + */ +uacpi_u64 uacpi_kernel_get_nanoseconds_since_boot(void); + +/* + * Spin for N microseconds. + */ +void uacpi_kernel_stall(uacpi_u8 usec); + +/* + * Sleep for N milliseconds. + */ +void uacpi_kernel_sleep(uacpi_u64 msec); + +/* + * Create/free an opaque non-recursive kernel mutex object. + */ +uacpi_handle uacpi_kernel_create_mutex(void); +void uacpi_kernel_free_mutex(uacpi_handle); + +/* + * Create/free an opaque kernel (semaphore-like) event object. + */ +uacpi_handle uacpi_kernel_create_event(void); +void uacpi_kernel_free_event(uacpi_handle); + +/* + * Returns a unique identifier of the currently executing thread. + * + * The returned thread id cannot be UACPI_THREAD_ID_NONE. + */ +uacpi_thread_id uacpi_kernel_get_thread_id(void); + +/* + * Try to acquire the mutex with a millisecond timeout. + * + * The timeout value has the following meanings: + * 0x0000 - Attempt to acquire the mutex once, in a non-blocking manner + * 0x0001...0xFFFE - Attempt to acquire the mutex for at least 'timeout' + * milliseconds + * 0xFFFF - Infinite wait, block until the mutex is acquired + * + * The following are possible return values: + * 1. UACPI_STATUS_OK - successful acquire operation + * 2. UACPI_STATUS_TIMEOUT - timeout reached while attempting to acquire (or the + * single attempt to acquire was not successful for + * calls with timeout=0) + * 3. Any other value - signifies a host internal error and is treated as such + */ +uacpi_status uacpi_kernel_acquire_mutex(uacpi_handle, uacpi_u16); +void uacpi_kernel_release_mutex(uacpi_handle); + +/* + * Try to wait for an event (counter > 0) with a millisecond timeout. + * A timeout value of 0xFFFF implies infinite wait. + * + * The internal counter is decremented by 1 if wait was successful. + * + * A successful wait is indicated by returning UACPI_TRUE. + */ +uacpi_bool uacpi_kernel_wait_for_event(uacpi_handle, uacpi_u16); + +/* + * Signal the event object by incrementing its internal counter by 1. + * + * This function may be used in interrupt contexts. + */ +void uacpi_kernel_signal_event(uacpi_handle); + +/* + * Reset the event counter to 0. + */ +void uacpi_kernel_reset_event(uacpi_handle); + +/* + * Handle a firmware request. + * + * Currently either a Breakpoint or Fatal operators. + */ +uacpi_status uacpi_kernel_handle_firmware_request(uacpi_firmware_request*); + +/* + * Install an interrupt handler at 'irq', 'ctx' is passed to the provided + * handler for every invocation. + * + * 'out_irq_handle' is set to a kernel-implemented value that can be used to + * refer to this handler from other API. + */ +uacpi_status uacpi_kernel_install_interrupt_handler( + uacpi_u32 irq, uacpi_interrupt_handler, uacpi_handle ctx, + uacpi_handle *out_irq_handle +); + +/* + * Uninstall an interrupt handler. 'irq_handle' is the value returned via + * 'out_irq_handle' during installation. + */ +uacpi_status uacpi_kernel_uninstall_interrupt_handler( + uacpi_interrupt_handler, uacpi_handle irq_handle +); + +/* + * Create/free a kernel spinlock object. + * + * Unlike other types of locks, spinlocks may be used in interrupt contexts. + */ +uacpi_handle uacpi_kernel_create_spinlock(void); +void uacpi_kernel_free_spinlock(uacpi_handle); + +/* + * Lock/unlock helpers for spinlocks. + * + * These are expected to disable interrupts, returning the previous state of cpu + * flags, that can be used to possibly re-enable interrupts if they were enabled + * before. + * + * Note that lock is infalliable. + */ +uacpi_cpu_flags uacpi_kernel_lock_spinlock(uacpi_handle); +void uacpi_kernel_unlock_spinlock(uacpi_handle, uacpi_cpu_flags); + +typedef enum uacpi_work_type { + /* + * Schedule a GPE handler method for execution. + * This should be scheduled to run on CPU0 to avoid potential SMI-related + * firmware bugs. + */ + UACPI_WORK_GPE_EXECUTION, + + /* + * Schedule a Notify(device) firmware request for execution. + * This can run on any CPU. + */ + UACPI_WORK_NOTIFICATION, +} uacpi_work_type; + +typedef void (*uacpi_work_handler)(uacpi_handle); + +/* + * Schedules deferred work for execution. + * Might be invoked from an interrupt context. + */ +uacpi_status uacpi_kernel_schedule_work( + uacpi_work_type, uacpi_work_handler, uacpi_handle ctx +); + +/* + * Waits for two types of work to finish: + * 1. All in-flight interrupts installed via uacpi_kernel_install_interrupt_handler + * 2. All work scheduled via uacpi_kernel_schedule_work + * + * Note that the waits must be done in this order specifically. + */ +uacpi_status uacpi_kernel_wait_for_work_completion(void); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/namespace.h b/src/include/uacpi/namespace.h new file mode 100644 index 0000000..3a94429 --- /dev/null +++ b/src/include/uacpi/namespace.h @@ -0,0 +1,136 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct uacpi_namespace_node uacpi_namespace_node; + +uacpi_namespace_node *uacpi_namespace_root(void); + +typedef enum uacpi_predefined_namespace { + UACPI_PREDEFINED_NAMESPACE_ROOT = 0, + UACPI_PREDEFINED_NAMESPACE_GPE, + UACPI_PREDEFINED_NAMESPACE_PR, + UACPI_PREDEFINED_NAMESPACE_SB, + UACPI_PREDEFINED_NAMESPACE_SI, + UACPI_PREDEFINED_NAMESPACE_TZ, + UACPI_PREDEFINED_NAMESPACE_GL, + UACPI_PREDEFINED_NAMESPACE_OS, + UACPI_PREDEFINED_NAMESPACE_OSI, + UACPI_PREDEFINED_NAMESPACE_REV, + UACPI_PREDEFINED_NAMESPACE_MAX = UACPI_PREDEFINED_NAMESPACE_REV, +} uacpi_predefined_namespace; +uacpi_namespace_node *uacpi_namespace_get_predefined( + uacpi_predefined_namespace +); + +/* + * Returns UACPI_TRUE if the provided 'node' is an alias. + */ +uacpi_bool uacpi_namespace_node_is_alias(uacpi_namespace_node *node); + +uacpi_object_name uacpi_namespace_node_name(const uacpi_namespace_node *node); + +/* + * Returns the type of object stored at the namespace node. + * + * NOTE: due to the existance of the CopyObject operator in AML, the + * return value of this function is subject to TOCTOU bugs. + */ +uacpi_status uacpi_namespace_node_type( + const uacpi_namespace_node *node, uacpi_object_type *out_type +); + +/* + * Returns UACPI_TRUE via 'out' if the type of the object stored at the + * namespace node matches the provided value, UACPI_FALSE otherwise. + * + * NOTE: due to the existance of the CopyObject operator in AML, the + * return value of this function is subject to TOCTOU bugs. + */ +uacpi_status uacpi_namespace_node_is( + const uacpi_namespace_node *node, uacpi_object_type type, uacpi_bool *out +); + +/* + * Returns UACPI_TRUE via 'out' if the type of the object stored at the + * namespace node matches any of the type bits in the provided value, + * UACPI_FALSE otherwise. + * + * NOTE: due to the existance of the CopyObject operator in AML, the + * return value of this function is subject to TOCTOU bugs. + */ +uacpi_status uacpi_namespace_node_is_one_of( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask, + uacpi_bool *out +); + +uacpi_size uacpi_namespace_node_depth(const uacpi_namespace_node *node); + +uacpi_namespace_node *uacpi_namespace_node_parent( + uacpi_namespace_node *node +); + +uacpi_status uacpi_namespace_node_find( + uacpi_namespace_node *parent, + const uacpi_char *path, + uacpi_namespace_node **out_node +); + +/* + * Same as uacpi_namespace_node_find, except the search recurses upwards when + * the namepath consists of only a single nameseg. Usually, this behavior is + * only desired if resolving a namepath specified in an aml-provided object, + * such as a package element. + */ +uacpi_status uacpi_namespace_node_resolve_from_aml_namepath( + uacpi_namespace_node *scope, + const uacpi_char *path, + uacpi_namespace_node **out_node +); + +typedef uacpi_iteration_decision (*uacpi_iteration_callback) ( + void *user, uacpi_namespace_node *node, uacpi_u32 node_depth +); + +#define UACPI_MAX_DEPTH_ANY 0xFFFFFFFF + +/* + * Depth-first iterate the namespace starting at the first child of 'parent'. + */ +uacpi_status uacpi_namespace_for_each_child_simple( + uacpi_namespace_node *parent, uacpi_iteration_callback callback, void *user +); + +/* + * Depth-first iterate the namespace starting at the first child of 'parent'. + * + * 'descending_callback' is invoked the first time a node is visited when + * walking down. 'ascending_callback' is invoked the second time a node is + * visited after we reach the leaf node without children and start walking up. + * Either of the callbacks may be NULL, but not both at the same time. + * + * Only nodes matching 'type_mask' are passed to the callbacks. + * + * 'max_depth' is used to limit the maximum reachable depth from 'parent', + * where 1 is only direct children of 'parent', 2 is children of first-level + * children etc. Use UACPI_MAX_DEPTH_ANY or -1 to specify infinite depth. + */ +uacpi_status uacpi_namespace_for_each_child( + uacpi_namespace_node *parent, uacpi_iteration_callback descending_callback, + uacpi_iteration_callback ascending_callback, + uacpi_object_type_bits type_mask, uacpi_u32 max_depth, void *user +); + +const uacpi_char *uacpi_namespace_node_generate_absolute_path( + const uacpi_namespace_node *node +); +void uacpi_free_absolute_path(const uacpi_char *path); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/notify.h b/src/include/uacpi/notify.h new file mode 100644 index 0000000..3360d4f --- /dev/null +++ b/src/include/uacpi/notify.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Install a Notify() handler to a device node. + * A handler installed to the root node will receive all notifications, even if + * a device already has a dedicated Notify handler. + * 'handler_context' is passed to the handler on every invocation. + */ +uacpi_status uacpi_install_notify_handler( + uacpi_namespace_node *node, uacpi_notify_handler handler, + uacpi_handle handler_context +); + +uacpi_status uacpi_uninstall_notify_handler( + uacpi_namespace_node *node, uacpi_notify_handler handler +); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/opregion.h b/src/include/uacpi/opregion.h new file mode 100644 index 0000000..01e984d --- /dev/null +++ b/src/include/uacpi/opregion.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Install an address space handler to a device node. + * The handler is recursively connected to all of the operation regions of + * type 'space' underneath 'device_node'. Note that this recursion stops as + * soon as another device node that already has an address space handler of + * this type installed is encountered. + */ +uacpi_status uacpi_install_address_space_handler( + uacpi_namespace_node *device_node, enum uacpi_address_space space, + uacpi_region_handler handler, uacpi_handle handler_context +); + +/* + * Uninstall the handler of type 'space' from a given device node. + */ +uacpi_status uacpi_uninstall_address_space_handler( + uacpi_namespace_node *device_node, + enum uacpi_address_space space +); + +/* + * Execute _REG(space, ACPI_REG_CONNECT) for all of the opregions with this + * address space underneath this device. This should only be called manually + * if you want to register an early handler that must be available before the + * call to uacpi_namespace_initialize(). + */ +uacpi_status uacpi_reg_all_opregions( + uacpi_namespace_node *device_node, + enum uacpi_address_space space +); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/osi.h b/src/include/uacpi/osi.h new file mode 100644 index 0000000..29197e8 --- /dev/null +++ b/src/include/uacpi/osi.h @@ -0,0 +1,121 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum uacpi_vendor_interface { + UACPI_VENDOR_INTERFACE_NONE = 0, + UACPI_VENDOR_INTERFACE_WINDOWS_2000, + UACPI_VENDOR_INTERFACE_WINDOWS_XP, + UACPI_VENDOR_INTERFACE_WINDOWS_XP_SP1, + UACPI_VENDOR_INTERFACE_WINDOWS_SERVER_2003, + UACPI_VENDOR_INTERFACE_WINDOWS_XP_SP2, + UACPI_VENDOR_INTERFACE_WINDOWS_SERVER_2003_SP1, + UACPI_VENDOR_INTERFACE_WINDOWS_VISTA, + UACPI_VENDOR_INTERFACE_WINDOWS_SERVER_2008, + UACPI_VENDOR_INTERFACE_WINDOWS_VISTA_SP1, + UACPI_VENDOR_INTERFACE_WINDOWS_VISTA_SP2, + UACPI_VENDOR_INTERFACE_WINDOWS_7, + UACPI_VENDOR_INTERFACE_WINDOWS_8, + UACPI_VENDOR_INTERFACE_WINDOWS_8_1, + UACPI_VENDOR_INTERFACE_WINDOWS_10, + UACPI_VENDOR_INTERFACE_WINDOWS_10_RS1, + UACPI_VENDOR_INTERFACE_WINDOWS_10_RS2, + UACPI_VENDOR_INTERFACE_WINDOWS_10_RS3, + UACPI_VENDOR_INTERFACE_WINDOWS_10_RS4, + UACPI_VENDOR_INTERFACE_WINDOWS_10_RS5, + UACPI_VENDOR_INTERFACE_WINDOWS_10_19H1, + UACPI_VENDOR_INTERFACE_WINDOWS_10_20H1, + UACPI_VENDOR_INTERFACE_WINDOWS_11, + UACPI_VENDOR_INTERFACE_WINDOWS_11_22H2, +} uacpi_vendor_interface; + +/* + * Returns the "latest" AML-queried _OSI vendor interface. + * + * E.g. for the following AML code: + * _OSI("Windows 2021") + * _OSI("Windows 2000") + * + * This function will return UACPI_VENDOR_INTERFACE_WINDOWS_11, since this is + * the latest version of the interface the code queried, even though the + * "Windows 2000" query came after "Windows 2021". + */ +uacpi_vendor_interface uacpi_latest_queried_vendor_interface(void); + +typedef enum uacpi_interface_kind { + UACPI_INTERFACE_KIND_VENDOR = (1 << 0), + UACPI_INTERFACE_KIND_FEATURE = (1 << 1), + UACPI_INTERFACE_KIND_ALL = UACPI_INTERFACE_KIND_VENDOR | + UACPI_INTERFACE_KIND_FEATURE, +} uacpi_interface_kind; + +/* + * Install or uninstall an interface. + * + * The interface kind is used for matching during interface enumeration in + * uacpi_bulk_configure_interfaces(). + * + * After installing an interface, all _OSI queries report it as supported. + */ +uacpi_status uacpi_install_interface( + const uacpi_char *name, uacpi_interface_kind +); +uacpi_status uacpi_uninstall_interface(const uacpi_char *name); + +typedef enum uacpi_host_interface { + UACPI_HOST_INTERFACE_MODULE_DEVICE = 1, + UACPI_HOST_INTERFACE_PROCESSOR_DEVICE, + UACPI_HOST_INTERFACE_3_0_THERMAL_MODEL, + UACPI_HOST_INTERFACE_3_0_SCP_EXTENSIONS, + UACPI_HOST_INTERFACE_PROCESSOR_AGGREGATOR_DEVICE, +} uacpi_host_interface; + +/* + * Same as install/uninstall interface, but comes with an enum of known + * interfaces defined by the ACPI specification. These are disabled by default + * as they depend on the host kernel support. + */ +uacpi_status uacpi_enable_host_interface(uacpi_host_interface); +uacpi_status uacpi_disable_host_interface(uacpi_host_interface); + +typedef uacpi_bool (*uacpi_interface_handler) + (const uacpi_char *name, uacpi_bool supported); + +/* + * Set a custom interface query (_OSI) handler. + * + * This callback will be invoked for each _OSI query with the value + * passed in the _OSI, as well as whether the interface was detected as + * supported. The callback is able to override the return value dynamically + * or leave it untouched if desired (e.g. if it simply wants to log something or + * do internal bookkeeping of some kind). + */ +uacpi_status uacpi_set_interface_query_handler(uacpi_interface_handler); + +typedef enum uacpi_interface_action { + UACPI_INTERFACE_ACTION_DISABLE = 0, + UACPI_INTERFACE_ACTION_ENABLE, +} uacpi_interface_action; + +/* + * Bulk interface configuration, used to disable or enable all interfaces that + * match 'kind'. + * + * This is generally only needed to work around buggy hardware, for example if + * requested from the kernel command line. + * + * By default, all vendor strings (like "Windows 2000") are enabled, and all + * host features (like "3.0 Thermal Model") are disabled. + */ +uacpi_status uacpi_bulk_configure_interfaces( + uacpi_interface_action action, uacpi_interface_kind kind +); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/platform/arch_helpers.h b/src/include/uacpi/platform/arch_helpers.h new file mode 100644 index 0000000..2e566c4 --- /dev/null +++ b/src/include/uacpi/platform/arch_helpers.h @@ -0,0 +1,38 @@ +#pragma once + +#ifdef UACPI_OVERRIDE_ARCH_HELPERS +#include "uacpi_arch_helpers.h" +#else + +#include + +#ifndef UACPI_ARCH_FLUSH_CPU_CACHE +#define UACPI_ARCH_FLUSH_CPU_CACHE() do {} while (0) +#endif + +typedef unsigned long uacpi_cpu_flags; + +typedef void *uacpi_thread_id; + +/* + * Replace as needed depending on your platform's way to represent thread ids. + * uACPI offers a few more helpers like uacpi_atomic_{load,store}{8,16,32,64,ptr} + * (or you could provide your own helpers) + */ +#ifndef UACPI_ATOMIC_LOAD_THREAD_ID +#define UACPI_ATOMIC_LOAD_THREAD_ID(ptr) ((uacpi_thread_id)uacpi_atomic_load_ptr(ptr)) +#endif + +#ifndef UACPI_ATOMIC_STORE_THREAD_ID +#define UACPI_ATOMIC_STORE_THREAD_ID(ptr, value) uacpi_atomic_store_ptr(ptr, value) +#endif + +/* + * A sentinel value that the kernel promises to NEVER return from + * uacpi_kernel_get_current_thread_id or this will break + */ +#ifndef UACPI_THREAD_ID_NONE +#define UACPI_THREAD_ID_NONE ((uacpi_thread_id)-1) +#endif + +#endif diff --git a/src/include/uacpi/platform/atomic.h b/src/include/uacpi/platform/atomic.h new file mode 100644 index 0000000..20db940 --- /dev/null +++ b/src/include/uacpi/platform/atomic.h @@ -0,0 +1,129 @@ +#pragma once + +/* + * Most of this header is a giant workaround for MSVC to make atomics into a + * somewhat unified interface with how GCC and Clang handle them. + * + * We don't use the absolutely disgusting C11 stdatomic.h header because it is + * unable to operate on non _Atomic types, which enforce implicit sequential + * consistency and alter the behavior of the standard C binary/unary operators. + * + * The strictness of the atomic helpers defined here is assumed to be at least + * acquire for loads and release for stores. Cmpxchg uses the standard acq/rel + * for success, acq for failure, and is assumed to be strong. + */ + +#ifdef UACPI_OVERRIDE_ATOMIC +#include "uacpi_atomic.h" +#else + +#include + +#ifdef _MSC_VER + +#include + +// mimic __atomic_compare_exchange_n that doesn't exist on MSVC +#define UACPI_MAKE_MSVC_CMPXCHG(width, type, suffix) \ + static inline int uacpi_do_atomic_cmpxchg##width( \ + type volatile *ptr, type volatile *expected, type desired \ + ) \ + { \ + type current; \ + \ + current = _InterlockedCompareExchange##suffix(ptr, *expected, desired); \ + if (current != *expected) { \ + *expected = current; \ + return 0; \ + } \ + return 1; \ + } + +#define UACPI_MSVC_CMPXCHG_INVOKE(ptr, expected, desired, width, type) \ + uacpi_do_atomic_cmpxchg##width( \ + (type volatile*)ptr, (type volatile*)expected, desired \ + ) + +#define UACPI_MSVC_ATOMIC_STORE(ptr, value, type, width) \ + _InterlockedExchange##width((type volatile*)(ptr), (type)(value)) + +#define UACPI_MSVC_ATOMIC_LOAD(ptr, type, width) \ + _InterlockedOr##width((type volatile*)(ptr), 0) + +#define UACPI_MSVC_ATOMIC_INC(ptr, type, width) \ + _InterlockedIncrement##width((type volatile*)(ptr)) + +#define UACPI_MSVC_ATOMIC_DEC(ptr, type, width) \ + _InterlockedDecrement##width((type volatile*)(ptr)) + +UACPI_MAKE_MSVC_CMPXCHG(64, __int64, 64) +UACPI_MAKE_MSVC_CMPXCHG(32, long,) +UACPI_MAKE_MSVC_CMPXCHG(16, short, 16) + +#define uacpi_atomic_cmpxchg16(ptr, expected, desired) \ + UACPI_MSVC_CMPXCHG_INVOKE(ptr, expected, desired, 16, short) + +#define uacpi_atomic_cmpxchg32(ptr, expected, desired) \ + UACPI_MSVC_CMPXCHG_INVOKE(ptr, expected, desired, 32, long) + +#define uacpi_atomic_cmpxchg64(ptr, expected, desired) \ + UACPI_MSVC_CMPXCHG_INVOKE(ptr, expected, desired, 64, __int64) + +#define uacpi_atomic_load8(ptr) UACPI_MSVC_ATOMIC_LOAD(ptr, char, 8) +#define uacpi_atomic_load16(ptr) UACPI_MSVC_ATOMIC_LOAD(ptr, short, 16) +#define uacpi_atomic_load32(ptr) UACPI_MSVC_ATOMIC_LOAD(ptr, long,) +#define uacpi_atomic_load64(ptr) UACPI_MSVC_ATOMIC_LOAD(ptr, __int64, 64) + +#define uacpi_atomic_store8(ptr, value) UACPI_MSVC_ATOMIC_STORE(ptr, value, char, 8) +#define uacpi_atomic_store16(ptr, value) UACPI_MSVC_ATOMIC_STORE(ptr, value, short, 16) +#define uacpi_atomic_store32(ptr, value) UACPI_MSVC_ATOMIC_STORE(ptr, value, long,) +#define uacpi_atomic_store64(ptr, value) UACPI_MSVC_ATOMIC_STORE(ptr, value, __int64, 64) + +#define uacpi_atomic_inc16(ptr) UACPI_MSVC_ATOMIC_INC(ptr, short, 16) +#define uacpi_atomic_inc32(ptr) UACPI_MSVC_ATOMIC_INC(ptr, long,) +#define uacpi_atomic_inc64(ptr) UACPI_MSVC_ATOMIC_INC(ptr, __int64, 64) + +#define uacpi_atomic_dec16(ptr) UACPI_MSVC_ATOMIC_DEC(ptr, short, 16) +#define uacpi_atomic_dec32(ptr) UACPI_MSVC_ATOMIC_DEC(ptr, long,) +#define uacpi_atomic_dec64(ptr) UACPI_MSVC_ATOMIC_DEC(ptr, __int64, 64) +#else + +#define UACPI_DO_CMPXCHG(ptr, expected, desired) \ + __atomic_compare_exchange_n(ptr, expected, desired, 0, \ + __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE) + +#define uacpi_atomic_cmpxchg16(ptr, expected, desired) \ + UACPI_DO_CMPXCHG(ptr, expected, desired) +#define uacpi_atomic_cmpxchg32(ptr, expected, desired) \ + UACPI_DO_CMPXCHG(ptr, expected, desired) +#define uacpi_atomic_cmpxchg64(ptr, expected, desired) \ + UACPI_DO_CMPXCHG(ptr, expected, desired) + +#define uacpi_atomic_load8(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE) +#define uacpi_atomic_load16(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE) +#define uacpi_atomic_load32(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE) +#define uacpi_atomic_load64(ptr) __atomic_load_n(ptr, __ATOMIC_ACQUIRE) + +#define uacpi_atomic_store8(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_RELEASE) +#define uacpi_atomic_store16(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_RELEASE) +#define uacpi_atomic_store32(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_RELEASE) +#define uacpi_atomic_store64(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_RELEASE) + +#define uacpi_atomic_inc16(ptr) __atomic_add_fetch(ptr, 1, __ATOMIC_ACQ_REL) +#define uacpi_atomic_inc32(ptr) __atomic_add_fetch(ptr, 1, __ATOMIC_ACQ_REL) +#define uacpi_atomic_inc64(ptr) __atomic_add_fetch(ptr, 1, __ATOMIC_ACQ_REL) + +#define uacpi_atomic_dec16(ptr) __atomic_sub_fetch(ptr, 1, __ATOMIC_ACQ_REL) +#define uacpi_atomic_dec32(ptr) __atomic_sub_fetch(ptr, 1, __ATOMIC_ACQ_REL) +#define uacpi_atomic_dec64(ptr) __atomic_sub_fetch(ptr, 1, __ATOMIC_ACQ_REL) +#endif + +#if UACPI_POINTER_SIZE == 4 +#define uacpi_atomic_load_ptr(ptr_to_ptr) uacpi_atomic_load32(ptr_to_ptr) +#define uacpi_atomic_store_ptr(ptr_to_ptr, value) uacpi_atomic_store32(ptr_to_ptr, value) +#else +#define uacpi_atomic_load_ptr(ptr_to_ptr) uacpi_atomic_load64(ptr_to_ptr) +#define uacpi_atomic_store_ptr(ptr_to_ptr, value) uacpi_atomic_store64(ptr_to_ptr, value) +#endif + +#endif diff --git a/src/include/uacpi/platform/compiler.h b/src/include/uacpi/platform/compiler.h new file mode 100644 index 0000000..e3dd1c5 --- /dev/null +++ b/src/include/uacpi/platform/compiler.h @@ -0,0 +1,82 @@ +#pragma once + +/* + * Compiler-specific attributes/macros go here. This is the default placeholder + * that should work for MSVC/GCC/clang. + */ + +#ifdef UACPI_OVERRIDE_COMPILER +#include "uacpi_compiler.h" +#else + +#define UACPI_ALIGN(x) __declspec(align(x)) + +#ifdef _MSC_VER + #include + + #define UACPI_ALWAYS_INLINE __forceinline + + #define UACPI_PACKED(decl) \ + __pragma(pack(push, 1)) \ + decl; \ + __pragma(pack(pop)) +#else + #define UACPI_ALWAYS_INLINE inline __attribute__((always_inline)) + #define UACPI_PACKED(decl) decl __attribute__((packed)); +#endif + +#ifdef __GNUC__ + #define uacpi_unlikely(expr) __builtin_expect(!!(expr), 0) + #define uacpi_likely(expr) __builtin_expect(!!(expr), 1) + + #if __has_attribute(__fallthrough__) + #define UACPI_FALLTHROUGH __attribute__((__fallthrough__)) + #endif + + #define UACPI_MAYBE_UNUSED __attribute__ ((unused)) + + #define UACPI_NO_UNUSED_PARAMETER_WARNINGS_BEGIN \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wunused-parameter\"") + + #define UACPI_NO_UNUSED_PARAMETER_WARNINGS_END \ + _Pragma("GCC diagnostic pop") + + #ifdef __clang__ + #define UACPI_PRINTF_DECL(fmt_idx, args_idx) \ + __attribute__((format(printf, fmt_idx, args_idx))) + #else + #define UACPI_PRINTF_DECL(fmt_idx, args_idx) \ + __attribute__((format(gnu_printf, fmt_idx, args_idx))) + #endif +#else + #define uacpi_unlikely(expr) expr + #define uacpi_likely(expr) expr + + #define UACPI_MAYBE_UNUSED + + #define UACPI_NO_UNUSED_PARAMETER_WARNINGS_BEGIN + #define UACPI_NO_UNUSED_PARAMETER_WARNINGS_END + + #define UACPI_PRINTF_DECL(fmt_idx, args_idx) +#endif + +#ifndef UACPI_FALLTHROUGH + #define UACPI_FALLTHROUGH do {} while (0) +#endif + +#ifndef UACPI_POINTER_SIZE + #ifdef _WIN32 + #ifdef _WIN64 + #define UACPI_POINTER_SIZE 8 + #else + #define UACPI_POINTER_SIZE 4 + #endif + #elif defined(__GNUC__) + #define UACPI_POINTER_SIZE __SIZEOF_POINTER__ + #else + #error Failed to detect pointer size + #endif +#endif + +#endif diff --git a/src/include/uacpi/platform/config.h b/src/include/uacpi/platform/config.h new file mode 100644 index 0000000..b8ca130 --- /dev/null +++ b/src/include/uacpi/platform/config.h @@ -0,0 +1,130 @@ +#pragma once + +#ifdef UACPI_OVERRIDE_CONFIG +#include "uacpi_config.h" +#else + +#include +#include + +/* + * ======================= + * Context-related options + * ======================= + */ +#ifndef UACPI_DEFAULT_LOG_LEVEL + #define UACPI_DEFAULT_LOG_LEVEL UACPI_LOG_INFO +#endif + +UACPI_BUILD_BUG_ON_WITH_MSG( + UACPI_DEFAULT_LOG_LEVEL < UACPI_LOG_ERROR || + UACPI_DEFAULT_LOG_LEVEL > UACPI_LOG_DEBUG, + "configured default log level is invalid" +); + +#ifndef UACPI_DEFAULT_LOOP_TIMEOUT_SECONDS + #define UACPI_DEFAULT_LOOP_TIMEOUT_SECONDS 30 +#endif + +UACPI_BUILD_BUG_ON_WITH_MSG( + UACPI_DEFAULT_LOOP_TIMEOUT_SECONDS < 1, + "configured default loop timeout is invalid (expecting at least 1 second)" +); + +#ifndef UACPI_DEFAULT_MAX_CALL_STACK_DEPTH + #define UACPI_DEFAULT_MAX_CALL_STACK_DEPTH 256 +#endif + +UACPI_BUILD_BUG_ON_WITH_MSG( + UACPI_DEFAULT_MAX_CALL_STACK_DEPTH < 4, + "configured default max call stack depth is invalid " + "(expecting at least 4 frames)" +); + +/* + * =================== + * Kernel-api options + * =================== + */ + +/* + * Convenience initialization/deinitialization hooks that will be called by + * uACPI automatically when appropriate if compiled-in. + */ +// #define UACPI_KERNEL_INITIALIZATION + +/* + * Makes kernel api logging callbacks work with unformatted printf-style + * strings and va_args instead of a pre-formatted string. Can be useful if + * your native logging is implemented in terms of this format as well. + */ +// #define UACPI_FORMATTED_LOGGING + +/* + * Makes uacpi_kernel_free take in an additional 'size_hint' parameter, which + * contains the size of the original allocation. Note that this comes with a + * performance penalty in some cases. + */ +// #define UACPI_SIZED_FREES + + +/* + * Makes uacpi_kernel_alloc_zeroed mandatory to implement by the host, uACPI + * will not provide a default implementation if this is enabled. + */ +// #define UACPI_NATIVE_ALLOC_ZEROED + +/* + * ========================= + * Platform-specific options + * ========================= + */ + +/* + * Turns uacpi_phys_addr and uacpi_io_addr into a 32-bit type, and adds extra + * code for address truncation. Needed for e.g. i686 platforms without PAE + * support. + */ +// #define UACPI_PHYS_ADDR_IS_32BITS + +/* + * Switches uACPI into reduced-hardware-only mode. Strips all full-hardware + * ACPI support code at compile-time, including the event subsystem, the global + * lock, and other full-hardware features. + */ +// #define UACPI_REDUCED_HARDWARE + +/* + * ============= + * Misc. options + * ============= + */ + +/* + * If UACPI_FORMATTED_LOGGING is not enabled, this is the maximum length of the + * pre-formatted message that is passed to the logging callback. + */ +#ifndef UACPI_PLAIN_LOG_BUFFER_SIZE + #define UACPI_PLAIN_LOG_BUFFER_SIZE 128 +#endif + +UACPI_BUILD_BUG_ON_WITH_MSG( + UACPI_PLAIN_LOG_BUFFER_SIZE < 16, + "configured log buffer size is too small (expecting at least 16 bytes)" +); + +/* + * The size of the table descriptor inline storage. All table descriptors past + * this length will be stored in a dynamically allocated heap array. The size + * of one table descriptor is approximately 56 bytes. + */ +#ifndef UACPI_STATIC_TABLE_ARRAY_LEN + #define UACPI_STATIC_TABLE_ARRAY_LEN 16 +#endif + +UACPI_BUILD_BUG_ON_WITH_MSG( + UACPI_STATIC_TABLE_ARRAY_LEN < 1, + "configured static table array length is too small (expecting at least 1)" +); + +#endif diff --git a/src/include/uacpi/platform/libc.h b/src/include/uacpi/platform/libc.h new file mode 100644 index 0000000..9fab216 --- /dev/null +++ b/src/include/uacpi/platform/libc.h @@ -0,0 +1,25 @@ +#pragma once + +#ifdef UACPI_OVERRIDE_LIBC +#include "uacpi_libc.h" +#else +/* + * The following libc functions are used internally by uACPI and have a default + * (sub-optimal) implementation: + * - memcpy + * - memset + * - memcmp + * - strcmp + * - memmove + * - strnlen + * - strlen + * - snprintf + * - vsnprintf + * + * In case your platform happens to implement optimized verisons of the helpers + * above, you are able to make uACPI use those instead by overriding them like so: + * + * #define uacpi_memcpy my_fast_memcpy + * #define uacpi_snprintf my_fast_snprintf + */ +#endif diff --git a/src/include/uacpi/platform/types.h b/src/include/uacpi/platform/types.h new file mode 100644 index 0000000..f4a7cf9 --- /dev/null +++ b/src/include/uacpi/platform/types.h @@ -0,0 +1,64 @@ +#pragma once + +/* + * Platform-specific types go here. This is the default placeholder using + * types from the standard headers. + */ + +#ifdef UACPI_OVERRIDE_TYPES +#include "uacpi_types.h" +#else + +#include +#include +#include +#include + +#include + +typedef uint8_t uacpi_u8; +typedef uint16_t uacpi_u16; +typedef uint32_t uacpi_u32; +typedef uint64_t uacpi_u64; + +typedef int8_t uacpi_i8; +typedef int16_t uacpi_i16; +typedef int32_t uacpi_i32; +typedef int64_t uacpi_i64; + +#define UACPI_TRUE true +#define UACPI_FALSE false +typedef bool uacpi_bool; + +#define UACPI_NULL NULL + +typedef uintptr_t uacpi_uintptr; +typedef uacpi_uintptr uacpi_virt_addr; +typedef size_t uacpi_size; + +typedef va_list uacpi_va_list; +#define uacpi_va_start va_start +#define uacpi_va_end va_end +#define uacpi_va_arg va_arg + +typedef char uacpi_char; + +#define uacpi_offsetof offsetof + +/* + * We use unsignd long long for 64-bit number formatting because 64-bit types + * don't have a standard way to format them. The inttypes.h header is not + * freestanding therefore it's not practical to force the user to define the + * corresponding PRI macros. Moreover, unsignd long long is required to be + * at least 64-bits as per C99. + */ +UACPI_BUILD_BUG_ON_WITH_MSG( + sizeof(unsigned long long) < 8, + "unsigned long long must be at least 64 bits large as per C99" +); +#define UACPI_PRIu64 "llu" +#define UACPI_PRIx64 "llx" +#define UACPI_PRIX64 "llX" +#define UACPI_FMT64(val) ((unsigned long long)(val)) + +#endif diff --git a/src/include/uacpi/resources.h b/src/include/uacpi/resources.h new file mode 100644 index 0000000..2d4b4d8 --- /dev/null +++ b/src/include/uacpi/resources.h @@ -0,0 +1,674 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum uacpi_resource_type { + UACPI_RESOURCE_TYPE_IRQ, + UACPI_RESOURCE_TYPE_EXTENDED_IRQ, + + UACPI_RESOURCE_TYPE_DMA, + UACPI_RESOURCE_TYPE_FIXED_DMA, + + UACPI_RESOURCE_TYPE_IO, + UACPI_RESOURCE_TYPE_FIXED_IO, + + UACPI_RESOURCE_TYPE_ADDRESS16, + UACPI_RESOURCE_TYPE_ADDRESS32, + UACPI_RESOURCE_TYPE_ADDRESS64, + UACPI_RESOURCE_TYPE_ADDRESS64_EXTENDED, + + UACPI_RESOURCE_TYPE_MEMORY24, + UACPI_RESOURCE_TYPE_MEMORY32, + UACPI_RESOURCE_TYPE_FIXED_MEMORY32, + + UACPI_RESOURCE_TYPE_START_DEPENDENT, + UACPI_RESOURCE_TYPE_END_DEPENDENT, + + // Up to 7 bytes + UACPI_RESOURCE_TYPE_VENDOR_SMALL, + + // Up to 2^16 - 1 bytes + UACPI_RESOURCE_TYPE_VENDOR_LARGE, + + UACPI_RESOURCE_TYPE_GENERIC_REGISTER, + UACPI_RESOURCE_TYPE_GPIO_CONNECTION, + + // These must always be contiguous in this order + UACPI_RESOURCE_TYPE_SERIAL_I2C_CONNECTION, + UACPI_RESOURCE_TYPE_SERIAL_SPI_CONNECTION, + UACPI_RESOURCE_TYPE_SERIAL_UART_CONNECTION, + UACPI_RESOURCE_TYPE_SERIAL_CSI2_CONNECTION, + + UACPI_RESOURCE_TYPE_PIN_FUNCTION, + UACPI_RESOURCE_TYPE_PIN_CONFIGURATION, + UACPI_RESOURCE_TYPE_PIN_GROUP, + UACPI_RESOURCE_TYPE_PIN_GROUP_FUNCTION, + UACPI_RESOURCE_TYPE_PIN_GROUP_CONFIGURATION, + + UACPI_RESOURCE_TYPE_CLOCK_INPUT, + + UACPI_RESOURCE_TYPE_END_TAG, + UACPI_RESOURCE_TYPE_MAX = UACPI_RESOURCE_TYPE_END_TAG, +} uacpi_resource_type; + +typedef struct uacpi_resource_source { + uacpi_u8 index; + uacpi_bool index_present; + uacpi_u16 length; + uacpi_char *string; +} uacpi_resource_source; + +/* + * This applies to IRQ & StartDependent resources only. The DONT_CARE value is + * used for deserialization into the AML format to signify that the serializer + * is allowed to optimize the length down if possible. Note that this is + * generally not allowed unless the resource is generated by the caller: + * + * -- ACPI 6.5 ------------------------------------------------------------ + * The resource descriptors in the byte stream argument must be specified + * exactly as listed in the _CRS byte stream - meaning that the identical + * resource descriptors must appear in the identical order, resulting in a + * buffer of exactly the same length. Optimizations such as changing an + * IRQ descriptor to an IRQNoFlags descriptor (or vice-versa) must not be + * performed. Similarly, changing StartDependentFn to StartDependentFnNoPri + * is not allowed. + * ------------------------------------------------------------------------ + */ +enum uacpi_resource_length_kind { + UACPI_RESOURCE_LENGTH_KIND_DONT_CARE = 0, + UACPI_RESOURCE_LENGTH_KIND_ONE_LESS, + UACPI_RESOURCE_LENGTH_KIND_FULL, +}; + +// triggering fields +#define UACPI_TRIGGERING_EDGE 1 +#define UACPI_TRIGGERING_LEVEL 0 + +// polarity +#define UACPI_POLARITY_ACTIVE_HIGH 0 +#define UACPI_POLARITY_ACTIVE_LOW 1 +#define UACPI_POLARITY_ACTIVE_BOTH 2 + +// sharing +#define UACPI_EXCLUSIVE 0 +#define UACPI_SHARED 1 + +// wake_capability +#define UACPI_WAKE_CAPABLE 1 +#define UACPI_NOT_WAKE_CAPABLE 0 + +typedef struct uacpi_resource_irq { + uacpi_u8 length_kind; + uacpi_u8 triggering; + uacpi_u8 polarity; + uacpi_u8 sharing; + uacpi_u8 wake_capability; + uacpi_u8 num_irqs; + uacpi_u8 irqs[]; +} uacpi_resource_irq; + +typedef struct uacpi_resource_extended_irq { + uacpi_u8 direction; + uacpi_u8 triggering; + uacpi_u8 polarity; + uacpi_u8 sharing; + uacpi_u8 wake_capability; + uacpi_u8 num_irqs; + uacpi_resource_source source; + uacpi_u32 irqs[]; +} uacpi_resource_extended_irq; + +// transfer_type +#define UACPI_TRANSFER_TYPE_8_BIT 0b00 +#define UACPI_TRANSFER_TYPE_8_AND_16_BIT 0b01 +#define UACPI_TRANSFER_TYPE_16_BIT 0b10 + +// bus_master_status +#define UACPI_BUS_MASTER 0b1 + +// channel_speed +#define UACPI_DMA_COMPATIBILITY 0b00 +#define UACPI_DMA_TYPE_A 0b01 +#define UACPI_DMA_TYPE_B 0b10 +#define UACPI_DMA_TYPE_F 0b11 + +// transfer_width +#define UACPI_TRANSFER_WIDTH_8 0x00 +#define UACPI_TRANSFER_WIDTH_16 0x01 +#define UACPI_TRANSFER_WIDTH_32 0x02 +#define UACPI_TRANSFER_WIDTH_64 0x03 +#define UACPI_TRANSFER_WIDTH_128 0x04 +#define UACPI_TRANSFER_WIDTH_256 0x05 + +typedef struct uacpi_resource_dma { + uacpi_u8 transfer_type; + uacpi_u8 bus_master_status; + uacpi_u8 channel_speed; + uacpi_u8 num_channels; + uacpi_u8 channels[]; +} uacpi_resource_dma; + +typedef struct uacpi_resource_fixed_dma { + uacpi_u16 request_line; + uacpi_u16 channel; + uacpi_u8 transfer_width; +} uacpi_resource_fixed_dma; + +// decode_type +#define UACPI_DECODE_16 0b1 +#define UACPI_DECODE_10 0b0 + +typedef struct uacpi_resource_io { + uacpi_u8 decode_type; + uacpi_u16 minimum; + uacpi_u16 maximum; + uacpi_u8 alignment; + uacpi_u8 length; +} uacpi_resource_io; + +typedef struct uacpi_resource_fixed_io { + uacpi_u16 address; + uacpi_u8 length; +} uacpi_resource_fixed_io; + +// write_status +#define UACPI_NON_WRITABLE 0 +#define UACPI_WRITABLE 1 + +// caching +#define UACPI_NON_CACHEABLE 0 +#define UACPI_CACHEABLE 1 +#define UACPI_CACHEABLE_WRITE_COMBINING 2 +#define UACPI_PREFETCHABLE 3 + +// range_type +#define UACPI_RANGE_TYPE_MEMORY 0 +#define UACPI_RANGE_TYPE_RESERVED 1 +#define UACPI_RANGE_TYPE_ACPI 2 +#define UACPI_RANGE_TYPE_NVS 3 + +// address_common->type +#define UACPI_RANGE_MEMORY 0 +#define UACPI_RANGE_IO 1 +#define UACPI_RANGE_BUS 2 + +// translation +#define UACPI_IO_MEM_TRANSLATION 1 +#define UACPI_IO_MEM_STATIC 0 + +// translation_type +#define UACPI_TRANSLATION_DENSE 0 +#define UACPI_TRANSLATION_SPARSE 1 + +// direction +#define UACPI_PRODUCER 0 +#define UACPI_CONSUMER 1 + +// decode_type +#define UACPI_POISITIVE_DECODE 0 +#define UACPI_SUBTRACTIVE_DECODE 1 + +// fixed_min_address & fixed_max_address +#define UACPI_ADDRESS_NOT_FIXED 0 +#define UACPI_ADDRESS_FIXED 1 + +typedef struct uacpi_memory_attribute { + uacpi_u8 write_status; + uacpi_u8 caching; + uacpi_u8 range_type; + uacpi_u8 translation; +} uacpi_memory_attribute; + +typedef struct uacpi_io_attribute { + uacpi_u8 range_type; + uacpi_u8 translation; + uacpi_u8 translation_type; +} uacpi_io_attribute; + +typedef union uacpi_address_attribute { + uacpi_memory_attribute memory; + uacpi_io_attribute io; + uacpi_u8 type_specific; +} uacpi_address_attribute; + +typedef struct uacpi_resource_address_common { + uacpi_address_attribute attribute; + uacpi_u8 type; + uacpi_u8 direction; + uacpi_u8 decode_type; + uacpi_u8 fixed_min_address; + uacpi_u8 fixed_max_address; +} uacpi_resource_address_common; + +typedef struct uacpi_resource_address16 { + uacpi_resource_address_common common; + uacpi_u16 granularity; + uacpi_u16 minimum; + uacpi_u16 maximum; + uacpi_u16 translation_offset; + uacpi_u16 address_length; + uacpi_resource_source source; +} uacpi_resource_address16; + +typedef struct uacpi_resource_address32 { + uacpi_resource_address_common common; + uacpi_u32 granularity; + uacpi_u32 minimum; + uacpi_u32 maximum; + uacpi_u32 translation_offset; + uacpi_u32 address_length; + uacpi_resource_source source; +} uacpi_resource_address32; + +typedef struct uacpi_resource_address64 { + uacpi_resource_address_common common; + uacpi_u64 granularity; + uacpi_u64 minimum; + uacpi_u64 maximum; + uacpi_u64 translation_offset; + uacpi_u64 address_length; + uacpi_resource_source source; +} uacpi_resource_address64; + +typedef struct uacpi_resource_address64_extended { + uacpi_resource_address_common common; + uacpi_u8 revision_id; + uacpi_u64 granularity; + uacpi_u64 minimum; + uacpi_u64 maximum; + uacpi_u64 translation_offset; + uacpi_u64 address_length; + uacpi_u64 attributes; +} uacpi_resource_address64_extended; + +typedef struct uacpi_resource_memory24 { + uacpi_u8 write_status; + uacpi_u16 minimum; + uacpi_u16 maximum; + uacpi_u16 alignment; + uacpi_u16 length; +} uacpi_resource_memory24; + +typedef struct uacpi_resource_memory32 { + uacpi_u8 write_status; + uacpi_u32 minimum; + uacpi_u32 maximum; + uacpi_u32 alignment; + uacpi_u32 length; +} uacpi_resource_memory32; + +typedef struct uacpi_resource_fixed_memory32 { + uacpi_u8 write_status; + uacpi_u32 address; + uacpi_u32 length; +} uacpi_resource_fixed_memory32; + +// compatibility & performance +#define UACPI_GOOD 0 +#define UACPI_ACCEPTABLE 1 +#define UACPI_SUB_OPTIMAL 2 + +typedef struct uacpi_resource_start_dependent { + uacpi_u8 length_kind; + uacpi_u8 compatibility; + uacpi_u8 performance; +} uacpi_resource_start_dependent; + +typedef struct uacpi_resource_vendor_defined { + uacpi_u8 length; + uacpi_u8 data[]; +} uacpi_resource_vendor; + +typedef struct uacpi_resource_vendor_typed { + uacpi_u16 length; + uacpi_u8 sub_type; + uacpi_u8 uuid[16]; + uacpi_u8 data[]; +} uacpi_resource_vendor_typed; + +typedef struct uacpi_resource_generic_register { + uacpi_u8 address_space_id; + uacpi_u8 bit_width; + uacpi_u8 bit_offset; + uacpi_u8 access_size; + uacpi_u64 address; +} uacpi_resource_generic_register; + +// type +#define UACPI_GPIO_CONNECTION_INTERRUPT 0x00 +#define UACPI_GPIO_CONNECTION_IO 0x01 + +typedef struct uacpi_interrupt_connection_flags { + uacpi_u8 triggering; + uacpi_u8 polarity; + uacpi_u8 sharing; + uacpi_u8 wake_capability; +} uacpi_interrupt_connection_flags; + +// restriction +#define UACPI_IO_RESTRICTION_NONE 0x0 +#define UACPI_IO_RESTRICTION_INPUT 0x1 +#define UACPI_IO_RESTRICTION_OUTPUT 0x2 +#define UACPI_IO_RESTRICTION_NONE_PRESERVE 0x3 + +typedef struct uacpi_io_connection_flags { + uacpi_u8 restriction; + uacpi_u8 sharing; +} uacpi_io_connection_flags; + +// pull_configuration +#define UACPI_PIN_CONFIG_DEFAULT 0x00 +#define UACPI_PIN_CONFIG_PULL_UP 0x01 +#define UACPI_PIN_CONFIG_PULL_DOWN 0x02 +#define UACPI_PIN_CONFIG_NO_PULL 0x03 + +typedef struct uacpi_resource_gpio_connection { + uacpi_u8 revision_id; + uacpi_u8 type; + uacpi_u8 direction; + + union { + uacpi_interrupt_connection_flags interrupt; + uacpi_io_connection_flags io; + uacpi_u16 type_specific; + }; + + uacpi_u8 pull_configuration; + uacpi_u16 drive_strength; + uacpi_u16 debounce_timeout; + uacpi_u16 vendor_data_length; + uacpi_u16 pin_table_length; + uacpi_resource_source source; + uacpi_u16 *pin_table; + uacpi_u8 *vendor_data; +} uacpi_resource_gpio_connection; + +// mode +#define UACPI_MODE_CONTROLLER_INITIATED 0x0 +#define UACPI_MODE_DEVICE_INITIATED 0x1 + +typedef struct uacpi_resource_serial_bus_common { + uacpi_u8 revision_id; + uacpi_u8 type; + uacpi_u8 mode; + uacpi_u8 direction; + uacpi_u8 sharing; + uacpi_u8 type_revision_id; + uacpi_u16 type_data_length; + uacpi_u16 vendor_data_length; + uacpi_resource_source source; + uacpi_u8 *vendor_data; +} uacpi_resource_serial_bus_common; + +// addressing_mode +#define UACPI_I2C_7BIT 0x0 +#define UACPI_I2C_10BIT 0x1 + +typedef struct uacpi_resource_i2c_connection { + uacpi_resource_serial_bus_common common; + uacpi_u8 addressing_mode; + uacpi_u16 slave_address; + uacpi_u32 connection_speed; +} uacpi_resource_i2c_connection; + +// wire_mode +#define UACPI_SPI_4_WIRES 0 +#define UACPI_SPI_3_WIRES 1 + +// device_polarity +#define UACPI_SPI_ACTIVE_LOW 0 +#define UACPI_SPI_ACTIVE_HIGH 1 + +// phase +#define UACPI_SPI_PHASE_FIRST 0 +#define UACPI_SPI_PHASE_SECOND 0 + +// polarity +#define UACPI_SPI_START_LOW 0 +#define UACPI_SPI_START_HIGH 1 + +typedef struct uacpi_resource_spi_connection { + uacpi_resource_serial_bus_common common; + uacpi_u8 wire_mode; + uacpi_u8 device_polarity; + uacpi_u8 data_bit_length; + uacpi_u8 phase; + uacpi_u8 polarity; + uacpi_u16 device_selection; + uacpi_u32 connection_speed; +} uacpi_resource_spi_connection; + +// stop_bits +#define UACPI_UART_STOP_BITS_NONE 0b00 +#define UACPI_UART_STOP_BITS_1 0b01 +#define UACPI_UART_STOP_BITS_1_5 0b10 +#define UACPI_UART_STOP_BITS_2 0b11 + +// data_bits +#define UACPI_UART_DATA_5BITS 0b000 +#define UACPI_UART_DATA_6BITS 0b001 +#define UACPI_UART_DATA_7BITS 0b010 +#define UACPI_UART_DATA_8BITS 0b011 +#define UACPI_UART_DATA_9BITS 0b100 + +// endianness +#define UACPI_UART_LITTLE_ENDIAN 0 +#define UACPI_UART_BIG_ENDIAN 1 + +// parity +#define UACPI_UART_PARITY_NONE 0x00 +#define UACPI_UART_PARITY_EVEN 0x01 +#define UACPI_UART_PARITY_ODD 0x02 +#define UACPI_UART_PARITY_MARK 0x03 +#define UACPI_UART_PARITY_SPACE 0x04 + +// lines_enabled +#define UACPI_UART_DATA_CARRIER_DETECT (1 << 2) +#define UACPI_UART_RING_INDICATOR (1 << 3) +#define UACPI_UART_DATA_SET_READY (1 << 4) +#define UACPI_UART_DATA_TERMINAL_READY (1 << 5) +#define UACPI_UART_CLEAR_TO_SEND (1 << 6) +#define UACPI_UART_REQUEST_TO_SEND (1 << 7) + +// flow_control +#define UACPI_UART_FLOW_CONTROL_NONE 0b00 +#define UACPI_UART_FLOW_CONTROL_HW 0b01 +#define UACPI_UART_FLOW_CONTROL_XON_XOFF 0b10 + +typedef struct uacpi_resource_uart_connection { + uacpi_resource_serial_bus_common common; + uacpi_u8 stop_bits; + uacpi_u8 data_bits; + uacpi_u8 endianness; + uacpi_u8 parity; + uacpi_u8 lines_enabled; + uacpi_u8 flow_control; + uacpi_u32 baud_rate; + uacpi_u16 rx_fifo; + uacpi_u16 tx_fifo; +} uacpi_resource_uart_connection; + +// phy_type +#define UACPI_CSI2_PHY_C 0b00 +#define UACPI_CSI2_PHY_D 0b01 + +typedef struct uacpi_resource_csi2_connection { + uacpi_resource_serial_bus_common common; + uacpi_u8 phy_type; + uacpi_u8 local_port; +} uacpi_resource_csi2_connection; + +typedef struct uacpi_resource_pin_function { + uacpi_u8 revision_id; + uacpi_u8 sharing; + uacpi_u8 pull_configuration; + uacpi_u16 function_number; + uacpi_u16 pin_table_length; + uacpi_u16 vendor_data_length; + uacpi_resource_source source; + uacpi_u16 *pin_table; + uacpi_u8 *vendor_data; +} uacpi_resource_pin_function; + +// type +#define UACPI_PIN_CONFIG_DEFAULT 0x00 +#define UACPI_PIN_CONFIG_BIAS_PULL_UP 0x01 +#define UACPI_PIN_CONFIG_BIAS_PULL_DOWN 0x02 +#define UACPI_PIN_CONFIG_BIAS_DEFAULT 0x03 +#define UACPI_PIN_CONFIG_BIAS_DISABLE 0x04 +#define UACPI_PIN_CONFIG_BIAS_HIGH_IMPEDANCE 0x05 +#define UACPI_PIN_CONFIG_BIAS_BUS_HOLD 0x06 +#define UACPI_PIN_CONFIG_DRIVE_OPEN_DRAIN 0x07 +#define UACPI_PIN_CONFIG_DRIVE_OPEN_SOURCE 0x08 +#define UACPI_PIN_CONFIG_DRIVE_PUSH_PULL 0x09 +#define UACPI_PIN_CONFIG_DRIVE_STRENGTH 0x0A +#define UACPI_PIN_CONFIG_SLEW_RATE 0x0B +#define UACPI_PIN_CONFIG_INPUT_DEBOUNCE 0x0C +#define UACPI_PIN_CONFIG_INPUT_SCHMITT_TRIGGER 0x0D + +typedef struct uacpi_resource_pin_configuration { + uacpi_u8 revision_id; + uacpi_u8 sharing; + uacpi_u8 direction; + uacpi_u8 type; + uacpi_u32 value; + uacpi_u16 pin_table_length; + uacpi_u16 vendor_data_length; + uacpi_resource_source source; + uacpi_u16 *pin_table; + uacpi_u8 *vendor_data; +} uacpi_resource_pin_configuration; + +typedef struct uacpi_resource_label { + uacpi_u16 length; + const uacpi_char *string; +} uacpi_resource_label; + +typedef struct uacpi_resource_pin_group { + uacpi_u8 revision_id; + uacpi_u8 direction; + uacpi_u16 pin_table_length; + uacpi_u16 vendor_data_length; + uacpi_resource_label label; + uacpi_u16 *pin_table; + uacpi_u8 *vendor_data; +} uacpi_resource_pin_group; + +typedef struct uacpi_resource_pin_group_function { + uacpi_u8 revision_id; + uacpi_u8 sharing; + uacpi_u8 direction; + uacpi_u16 function; + uacpi_u16 vendor_data_length; + uacpi_resource_source source; + uacpi_resource_label label; + uacpi_u8 *vendor_data; +} uacpi_resource_pin_group_function; + +typedef struct uacpi_resource_pin_group_configuration { + uacpi_u8 revision_id; + uacpi_u8 sharing; + uacpi_u8 direction; + uacpi_u8 type; + uacpi_u32 value; + uacpi_u16 vendor_data_length; + uacpi_resource_source source; + uacpi_resource_label label; + uacpi_u8 *vendor_data; +} uacpi_resource_pin_group_configuration; + +// scale +#define UACPI_SCALE_HZ 0b00 +#define UACPI_SCALE_KHZ 0b01 +#define UACPI_SCALE_MHZ 0b10 + +// frequency +#define UACPI_FREQUENCY_FIXED 0x0 +#define UACPI_FREQUENCY_VARIABLE 0x1 + +typedef struct uacpi_resource_clock_input { + uacpi_u8 revision_id; + uacpi_u8 frequency; + uacpi_u8 scale; + uacpi_u16 divisor; + uacpi_u32 numerator; + uacpi_resource_source source; +} uacpi_resource_clock_input; + +typedef struct uacpi_resource { + uacpi_u32 type; + uacpi_u32 length; + + union { + uacpi_resource_irq irq; + uacpi_resource_extended_irq extended_irq; + uacpi_resource_dma dma; + uacpi_resource_fixed_dma fixed_dma; + uacpi_resource_io io; + uacpi_resource_fixed_io fixed_io; + uacpi_resource_address16 address16; + uacpi_resource_address32 address32; + uacpi_resource_address64 address64; + uacpi_resource_address64_extended address64_extended; + uacpi_resource_memory24 memory24; + uacpi_resource_memory32 memory32; + uacpi_resource_fixed_memory32 fixed_memory32; + uacpi_resource_start_dependent start_dependent; + uacpi_resource_vendor vendor; + uacpi_resource_vendor_typed vendor_typed; + uacpi_resource_generic_register generic_register; + uacpi_resource_gpio_connection gpio_connection; + uacpi_resource_serial_bus_common serial_bus_common; + uacpi_resource_i2c_connection i2c_connection; + uacpi_resource_spi_connection spi_connection; + uacpi_resource_uart_connection uart_connection; + uacpi_resource_csi2_connection csi2_connection; + uacpi_resource_pin_function pin_function; + uacpi_resource_pin_configuration pin_configuration; + uacpi_resource_pin_group pin_group; + uacpi_resource_pin_group_function pin_group_function; + uacpi_resource_pin_group_configuration pin_group_configuration; + uacpi_resource_clock_input clock_input; + }; +} uacpi_resource; + +#define UACPI_NEXT_RESOURCE(cur) \ + ((uacpi_resource*)((uacpi_u8*)(cur) + (cur)->length)) + +typedef struct uacpi_resources { + uacpi_size length; + uacpi_resource *entries; +} uacpi_resources; +void uacpi_free_resources(uacpi_resources*); + +typedef uacpi_iteration_decision (*uacpi_resource_iteration_callback) + (void *user, uacpi_resource *resource); + +uacpi_status uacpi_get_current_resources( + uacpi_namespace_node *device, uacpi_resources **out_resources +); + +uacpi_status uacpi_get_possible_resources( + uacpi_namespace_node *device, uacpi_resources **out_resources +); + +uacpi_status uacpi_set_resources( + uacpi_namespace_node *device, uacpi_resources *resources +); + +uacpi_status uacpi_for_each_resource( + uacpi_resources *resources, uacpi_resource_iteration_callback cb, void *user +); + +uacpi_status uacpi_for_each_device_resource( + uacpi_namespace_node *device, const uacpi_char *method, + uacpi_resource_iteration_callback cb, void *user +); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/sleep.h b/src/include/uacpi/sleep.h new file mode 100644 index 0000000..f505b04 --- /dev/null +++ b/src/include/uacpi/sleep.h @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Set the firmware waking vector in FACS. + * + * 'addr32' is the real mode entry-point address + * 'addr64' is the protected mode entry-point address + */ +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( +uacpi_status uacpi_set_waking_vector( + uacpi_phys_addr addr32, uacpi_phys_addr addr64 +)) + +typedef enum uacpi_sleep_state { + UACPI_SLEEP_STATE_S0 = 0, + UACPI_SLEEP_STATE_S1, + UACPI_SLEEP_STATE_S2, + UACPI_SLEEP_STATE_S3, + UACPI_SLEEP_STATE_S4, + UACPI_SLEEP_STATE_S5, + UACPI_SLEEP_STATE_MAX = UACPI_SLEEP_STATE_S5, +} uacpi_sleep_state; + +/* + * Prepare for a given sleep state. + * Must be caled with interrupts ENABLED. + */ +uacpi_status uacpi_prepare_for_sleep_state(uacpi_sleep_state); + +/* + * Enter the given sleep state after preparation. + * Must be called with interrupts DISABLED. + */ +uacpi_status uacpi_enter_sleep_state(uacpi_sleep_state); + +/* + * Prepare to leave the given sleep state. + * Must be called with interrupts DISABLED. + */ +uacpi_status uacpi_prepare_for_wake_from_sleep_state(uacpi_sleep_state); + +/* + * Wake from the given sleep state. + * Must be called with interrupts ENABLED. + */ +uacpi_status uacpi_wake_from_sleep_state(uacpi_sleep_state); + +/* + * Attempt reset via the FADT reset register. + */ +uacpi_status uacpi_reboot(void); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/status.h b/src/include/uacpi/status.h new file mode 100644 index 0000000..5c09508 --- /dev/null +++ b/src/include/uacpi/status.h @@ -0,0 +1,57 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum uacpi_status { + UACPI_STATUS_OK = 0, + UACPI_STATUS_MAPPING_FAILED = 1, + UACPI_STATUS_OUT_OF_MEMORY = 2, + UACPI_STATUS_BAD_CHECKSUM = 3, + UACPI_STATUS_INVALID_SIGNATURE = 4, + UACPI_STATUS_INVALID_TABLE_LENGTH = 5, + UACPI_STATUS_NOT_FOUND = 6, + UACPI_STATUS_INVALID_ARGUMENT = 7, + UACPI_STATUS_UNIMPLEMENTED = 8, + UACPI_STATUS_ALREADY_EXISTS = 9, + UACPI_STATUS_INTERNAL_ERROR = 10, + UACPI_STATUS_TYPE_MISMATCH = 11, + UACPI_STATUS_INIT_LEVEL_MISMATCH = 12, + UACPI_STATUS_NAMESPACE_NODE_DANGLING = 13, + UACPI_STATUS_NO_HANDLER = 14, + UACPI_STATUS_NO_RESOURCE_END_TAG = 15, + UACPI_STATUS_COMPILED_OUT = 16, + UACPI_STATUS_HARDWARE_TIMEOUT = 17, + UACPI_STATUS_TIMEOUT = 18, + UACPI_STATUS_OVERRIDDEN = 19, + UACPI_STATUS_DENIED = 20, + + // All errors that have bytecode-related origin should go here + UACPI_STATUS_AML_UNDEFINED_REFERENCE = 0x0EFF0000, + UACPI_STATUS_AML_INVALID_NAMESTRING = 0x0EFF0001, + UACPI_STATUS_AML_OBJECT_ALREADY_EXISTS = 0x0EFF0002, + UACPI_STATUS_AML_INVALID_OPCODE = 0x0EFF0003, + UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE = 0x0EFF0004, + UACPI_STATUS_AML_BAD_ENCODING = 0x0EFF0005, + UACPI_STATUS_AML_OUT_OF_BOUNDS_INDEX = 0x0EFF0006, + UACPI_STATUS_AML_SYNC_LEVEL_TOO_HIGH = 0x0EFF0007, + UACPI_STATUS_AML_INVALID_RESOURCE = 0x0EFF0008, + UACPI_STATUS_AML_LOOP_TIMEOUT = 0x0EFF0009, + UACPI_STATUS_AML_CALL_STACK_DEPTH_LIMIT = 0x0EFF000A, +} uacpi_status; + +const uacpi_char *uacpi_status_to_string(uacpi_status); + +#define uacpi_unlikely_error(expr) uacpi_unlikely((expr) != UACPI_STATUS_OK) +#define uacpi_likely_error(expr) uacpi_likely((expr) != UACPI_STATUS_OK) + +#define uacpi_unlikely_success(expr) uacpi_unlikely((expr) == UACPI_STATUS_OK) +#define uacpi_likely_success(expr) uacpi_likely((expr) == UACPI_STATUS_OK) + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/tables.h b/src/include/uacpi/tables.h new file mode 100644 index 0000000..e256d89 --- /dev/null +++ b/src/include/uacpi/tables.h @@ -0,0 +1,130 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Forward-declared to avoid including the entire acpi.h here +struct acpi_fadt; + +typedef struct uacpi_table_identifiers { + uacpi_object_name signature; + + // if oemid[0] == 0 this field is ignored + char oemid[6]; + + // if oem_table_id[0] == 0 this field is ignored + char oem_table_id[8]; +} uacpi_table_identifiers; + +typedef struct uacpi_table { + union { + uacpi_virt_addr virt_addr; + void *ptr; + struct acpi_sdt_hdr *hdr; + }; + + // Index number used to identify this table internally + uacpi_size index; +} uacpi_table; + +/* + * Install a table from either a virtual or a physical address. + * The table is simply stored in the internal table array, and not loaded by + * the interpreter (see uacpi_table_load). + * + * The table is optionally returned via 'out_table'. + * + * Manual calls to uacpi_table_install are not subject to filtering via the + * table installation callback (if any). + */ +uacpi_status uacpi_table_install( + void*, uacpi_table *out_table +); +uacpi_status uacpi_table_install_physical( + uacpi_phys_addr, uacpi_table *out_table +); + +/* + * Load a previously installed table by feeding it to the interpreter. + */ +uacpi_status uacpi_table_load(uacpi_size index); + +/* + * Helpers for finding tables. + * + * NOTE: + * The returned table's reference count is incremented by 1, which keeps its + * mapping alive forever unless uacpi_table_unref() is called for this table + * later on. Calling uacpi_table_find_next_with_same_signature() on a table also + * drops its reference count by 1, so if you want to keep it mapped you must + * manually call uacpi_table_ref() beforehand. + */ +uacpi_status uacpi_table_find_by_signature( + const uacpi_char *signature, uacpi_table *out_table +); +uacpi_status uacpi_table_find_next_with_same_signature( + uacpi_table *in_out_table +); +uacpi_status uacpi_table_find( + const uacpi_table_identifiers *id, uacpi_table *out_table +); + +/* + * Increment/decrement a table's reference count. + * The table is unmapped when the reference count drops to 0. + */ +uacpi_status uacpi_table_ref(uacpi_table*); +uacpi_status uacpi_table_unref(uacpi_table*); + +/* + * Returns the pointer to a sanitized internal version of FADT. + * + * The revision is guaranteed to be correct. All of the registers are converted + * to GAS format. Fields that might contain garbage are cleared. + */ +uacpi_status uacpi_table_fadt(struct acpi_fadt**); + +typedef enum uacpi_table_installation_disposition { + // Allow the table to be installed as-is + UACPI_TABLE_INSTALLATION_DISPOSITON_ALLOW = 0, + + /* + * Deny the table from being installed completely. This is useful for + * debugging various problems, e.g. AML loading bad SSDTs that cause the + * system to hang or enter an undesired state. + */ + UACPI_TABLE_INSTALLATION_DISPOSITON_DENY, + + /* + * Override the table being installed with the table at the virtual address + * returned in 'out_override_address'. + */ + UACPI_TABLE_INSTALLATION_DISPOSITON_VIRTUAL_OVERRIDE, + + /* + * Override the table being installed with the table at the physical address + * returned in 'out_override_address'. + */ + UACPI_TABLE_INSTALLATION_DISPOSITON_PHYSICAL_OVERRIDE, +} uacpi_table_installation_disposition; + +typedef uacpi_table_installation_disposition (*uacpi_table_installation_handler) + (struct acpi_sdt_hdr *hdr, uacpi_u64 *out_override_address); + +/* + * Set a handler that is invoked for each table before it gets installed. + * + * Depending on the return value, the table is either allowed to be installed + * as-is, denied, or overriden with a new one. + */ +uacpi_status uacpi_set_table_installation_handler( + uacpi_table_installation_handler handler +); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/types.h b/src/include/uacpi/types.h new file mode 100644 index 0000000..a79744c --- /dev/null +++ b/src/include/uacpi/types.h @@ -0,0 +1,458 @@ +#pragma once +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum uacpi_init_level { + // Reboot state, nothing is available + UACPI_INIT_LEVEL_EARLY = 0, + + /* + * State after a successfull call to uacpi_initialize. Table API and + * other helpers that don't depend on the ACPI namespace may be used. + */ + UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED = 1, + + /* + * State after a successfull call to uacpi_namespace_load. Most API may be + * used, namespace can be iterated, etc. + */ + UACPI_INIT_LEVEL_NAMESPACE_LOADED = 2, + + /* + * The final initialization stage, this is entered after the call to + * uacpi_namespace_initialize. All API is available to use. + */ + UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED = 3, +} uacpi_init_level; + +typedef enum uacpi_log_level { + /* + * Super verbose logging, every op & uop being processed is logged. + * Mostly useful for tracking down hangs/lockups. + */ + UACPI_LOG_DEBUG = 5, + + /* + * A little verbose, every operation region access is traced with a bit of + * extra information on top. + */ + UACPI_LOG_TRACE = 4, + + /* + * Only logs the bare minimum information about state changes and/or + * initialization progress. + */ + UACPI_LOG_INFO = 3, + + /* + * Logs recoverable errors and/or non-important aborts. + */ + UACPI_LOG_WARN = 2, + + /* + * Logs only critical errors that might affect the ability to initialize or + * prevent stable runtime. + */ + UACPI_LOG_ERROR = 1, +} uacpi_log_level; + +#if UACPI_POINTER_SIZE == 4 && defined(UACPI_PHYS_ADDR_IS_32BITS) +typedef uacpi_u32 uacpi_phys_addr; +typedef uacpi_u32 uacpi_io_addr; +#else +typedef uacpi_u64 uacpi_phys_addr; +typedef uacpi_u64 uacpi_io_addr; +#endif + +typedef struct uacpi_pci_address { + uacpi_u16 segment; + uacpi_u8 bus; + uacpi_u8 device; + uacpi_u8 function; +} uacpi_pci_address; + +typedef struct uacpi_data_view { + union { + uacpi_u8 *bytes; + const uacpi_u8 *const_bytes; + + uacpi_char *text; + const uacpi_char *const_text; + }; + uacpi_size length; +} uacpi_data_view; + +typedef void *uacpi_handle; +typedef struct uacpi_namespace_node uacpi_namespace_node; + +typedef enum uacpi_object_type { + UACPI_OBJECT_UNINITIALIZED = 0, + UACPI_OBJECT_INTEGER = 1, + UACPI_OBJECT_STRING = 2, + UACPI_OBJECT_BUFFER = 3, + UACPI_OBJECT_PACKAGE = 4, + UACPI_OBJECT_FIELD_UNIT = 5, + UACPI_OBJECT_DEVICE = 6, + UACPI_OBJECT_EVENT = 7, + UACPI_OBJECT_METHOD = 8, + UACPI_OBJECT_MUTEX = 9, + UACPI_OBJECT_OPERATION_REGION = 10, + UACPI_OBJECT_POWER_RESOURCE = 11, + UACPI_OBJECT_PROCESSOR = 12, + UACPI_OBJECT_THERMAL_ZONE = 13, + UACPI_OBJECT_BUFFER_FIELD = 14, + UACPI_OBJECT_DEBUG = 16, + + UACPI_OBJECT_REFERENCE = 20, + UACPI_OBJECT_BUFFER_INDEX = 21, + UACPI_OBJECT_MAX_TYPE_VALUE = UACPI_OBJECT_BUFFER_INDEX +} uacpi_object_type; + +// Type bits for API requiring a bit mask, e.g. uacpi_eval_typed +typedef enum uacpi_object_type_bits { + UACPI_OBJECT_INTEGER_BIT = (1 << UACPI_OBJECT_INTEGER), + UACPI_OBJECT_STRING_BIT = (1 << UACPI_OBJECT_STRING), + UACPI_OBJECT_BUFFER_BIT = (1 << UACPI_OBJECT_BUFFER), + UACPI_OBJECT_PACKAGE_BIT = (1 << UACPI_OBJECT_PACKAGE), + UACPI_OBJECT_FIELD_UNIT_BIT = (1 << UACPI_OBJECT_FIELD_UNIT), + UACPI_OBJECT_DEVICE_BIT = (1 << UACPI_OBJECT_DEVICE), + UACPI_OBJECT_EVENT_BIT = (1 << UACPI_OBJECT_EVENT), + UACPI_OBJECT_METHOD_BIT = (1 << UACPI_OBJECT_METHOD), + UACPI_OBJECT_MUTEX_BIT = (1 << UACPI_OBJECT_MUTEX), + UACPI_OBJECT_OPERATION_REGION_BIT = (1 << UACPI_OBJECT_OPERATION_REGION), + UACPI_OBJECT_POWER_RESOURCE_BIT = (1 << UACPI_OBJECT_POWER_RESOURCE), + UACPI_OBJECT_PROCESSOR_BIT = (1 << UACPI_OBJECT_PROCESSOR), + UACPI_OBJECT_THERMAL_ZONE_BIT = (1 << UACPI_OBJECT_THERMAL_ZONE), + UACPI_OBJECT_BUFFER_FIELD_BIT = (1 << UACPI_OBJECT_BUFFER_FIELD), + UACPI_OBJECT_DEBUG_BIT = (1 << UACPI_OBJECT_DEBUG), + UACPI_OBJECT_REFERENCE_BIT = (1 << UACPI_OBJECT_REFERENCE), + UACPI_OBJECT_BUFFER_INDEX_BIT = (1 << UACPI_OBJECT_BUFFER_INDEX), + UACPI_OBJECT_ANY_BIT = 0xFFFFFFFF, +} uacpi_object_type_bits; + +typedef struct uacpi_object uacpi_object; + +void uacpi_object_ref(uacpi_object *obj); +void uacpi_object_unref(uacpi_object *obj); + +uacpi_object_type uacpi_object_get_type(uacpi_object*); +uacpi_object_type_bits uacpi_object_get_type_bit(uacpi_object*); + +/* + * Returns UACPI_TRUE if the provided object's type matches this type. + */ +uacpi_bool uacpi_object_is(uacpi_object*, uacpi_object_type); + +/* + * Returns UACPI_TRUE if the provided object's type is one of the values + * specified in the 'type_mask' of UACPI_OBJECT_*_BIT. + */ +uacpi_bool uacpi_object_is_one_of( + uacpi_object*, uacpi_object_type_bits type_mask +); + +const uacpi_char *uacpi_object_type_to_string(uacpi_object_type); + +/* + * Create an uninitialized object. The object can be further overwritten via + * uacpi_object_assign_* to anything. + */ +uacpi_object *uacpi_object_create_uninitialized(void); + +/* + * Create an integer object with the value provided. + */ +uacpi_object *uacpi_object_create_integer(uacpi_u64); + +typedef enum uacpi_overflow_behavior { + UACPI_OVERFLOW_ALLOW = 0, + UACPI_OVERFLOW_TRUNCATE, + UACPI_OVERFLOW_DISALLOW, +} uacpi_overflow_behavior; + +/* + * Same as uacpi_object_create_integer, but introduces additional ways to + * control what happens if the provided integer is larger than 32-bits, and the + * AML code expects 32-bit integers. + * + * - UACPI_OVERFLOW_ALLOW -> do nothing, same as the vanilla helper + * - UACPI_OVERFLOW_TRUNCATE -> truncate the integer to 32-bits if it happens to + * be larger than allowed by the DSDT + * - UACPI_OVERFLOW_DISALLOW -> fail object creation with + * UACPI_STATUS_INVALID_ARGUMENT if the provided + * value happens to be too large + */ +uacpi_status uacpi_object_create_integer_safe( + uacpi_u64, uacpi_overflow_behavior, uacpi_object **out_obj +); + +uacpi_status uacpi_object_assign_integer(uacpi_object*, uacpi_u64 value); +uacpi_status uacpi_object_get_integer(uacpi_object*, uacpi_u64 *out); + +/* + * Create a string/buffer object. Takes in a constant view of the data. + * + * NOTE: The data is copied to a separately allocated buffer and is not taken + * ownership of. + */ +uacpi_object *uacpi_object_create_string(uacpi_data_view); +uacpi_object *uacpi_object_create_cstring(const uacpi_char*); +uacpi_object *uacpi_object_create_buffer(uacpi_data_view); + +/* + * Returns a writable view of the data stored in the string or buffer type + * object. + */ +uacpi_status uacpi_object_get_string_or_buffer( + uacpi_object*, uacpi_data_view *out +); +uacpi_status uacpi_object_get_string(uacpi_object*, uacpi_data_view *out); +uacpi_status uacpi_object_get_buffer(uacpi_object*, uacpi_data_view *out); + +/* + * Returns UACPI_TRUE if the provided string object is actually an AML namepath. + * + * This can only be the case for package elements. If a package element is + * specified as a path to an object in AML, it's not resolved by the interpreter + * right away as it might not have been defined at that point yet, and is + * instead stored as a special string object to be resolved by client code + * when needed. + * + * Example usage: + * uacpi_namespace_node *target_node = UACPI_NULL; + * + * uacpi_object *obj = UACPI_NULL; + * uacpi_eval(scope, path, UACPI_NULL, &obj); + * + * uacpi_object_array arr; + * uacpi_object_get_package(obj, &arr); + * + * if (uacpi_object_is_aml_namepath(arr.objects[0])) { + * uacpi_object_resolve_as_aml_namepath( + * arr.objects[0], scope, &target_node + * ); + * } + */ +uacpi_bool uacpi_object_is_aml_namepath(uacpi_object*); + +/* + * Resolve an AML namepath contained in a string object. + * + * This is only applicable to objects that are package elements. See an + * explanation of how this works in the comment above the declaration of + * uacpi_object_is_aml_namepath. + * + * This is a shorthand for: + * uacpi_data_view view; + * uacpi_object_get_string(object, &view); + * + * target_node = uacpi_namespace_node_resolve_from_aml_namepath( + * scope, view.text + * ); + */ +uacpi_status uacpi_object_resolve_as_aml_namepath( + uacpi_object*, uacpi_namespace_node *scope, uacpi_namespace_node **out_node +); + +/* + * Make the provided object a string/buffer. + * Takes in a constant view of the data to be stored in the object. + * + * NOTE: The data is copied to a separately allocated buffer and is not taken + * ownership of. + */ +uacpi_status uacpi_object_assign_string(uacpi_object*, uacpi_data_view in); +uacpi_status uacpi_object_assign_buffer(uacpi_object*, uacpi_data_view in); + +typedef struct uacpi_object_array { + uacpi_object **objects; + uacpi_size count; +} uacpi_object_array; + +/* + * Create a package object and store all of the objects in the array inside. + * The array is allowed to be empty. + * + * NOTE: the reference count of each object is incremented before being stored + * in the object. Client code must remove all of the locally created + * references at its own discretion. + */ +uacpi_object *uacpi_object_create_package(uacpi_object_array in); + +/* + * Returns the list of objects stored in a package object. + * + * NOTE: the reference count of the objects stored inside is not incremented, + * which means destorying/overwriting the object also potentially destroys + * all of the objects stored inside unless the reference count is + * incremented by the client via uacpi_object_ref. + */ +uacpi_status uacpi_object_get_package(uacpi_object*, uacpi_object_array *out); + +/* + * Make the provided object a package and store all of the objects in the array + * inside. The array is allowed to be empty. + * + * NOTE: the reference count of each object is incremented before being stored + * in the object. Client code must remove all of the locally created + * references at its own discretion. + */ +uacpi_status uacpi_object_assign_package(uacpi_object*, uacpi_object_array in); + +/* + * Create a reference object and make it point to 'child'. + * + * NOTE: child's reference count is incremented by one. Client code must remove + * all of the locally created references at its own discretion. + */ +uacpi_object *uacpi_object_create_reference(uacpi_object *child); + +/* + * Make the provided object a reference and make it point to 'child'. + * + * NOTE: child's reference count is incremented by one. Client code must remove + * all of the locally created references at its own discretion. + */ +uacpi_status uacpi_object_assign_reference(uacpi_object*, uacpi_object *child); + +/* + * Retrieve the object pointed to by a reference object. + * + * NOTE: the reference count of the returned object is incremented by one and + * must be uacpi_object_unref'ed by the client when no longer needed. + */ +uacpi_status uacpi_object_get_dereferenced(uacpi_object*, uacpi_object **out); + +typedef struct uacpi_processor_info { + uacpi_u8 id; + uacpi_u32 block_address; + uacpi_u8 block_length; +} uacpi_processor_info; + +/* + * Returns the information about the provided processor object. + */ +uacpi_status uacpi_object_get_processor_info( + uacpi_object*, uacpi_processor_info *out +); + +typedef struct uacpi_power_resource_info { + uacpi_u8 system_level; + uacpi_u16 resource_order; +} uacpi_power_resource_info; + +/* + * Returns the information about the provided power resource object. + */ +uacpi_status uacpi_object_get_power_resource_info( + uacpi_object*, uacpi_power_resource_info *out +); + +typedef enum uacpi_region_op { + UACPI_REGION_OP_ATTACH = 1, + UACPI_REGION_OP_READ = 2, + UACPI_REGION_OP_WRITE = 3, + UACPI_REGION_OP_DETACH = 4, +} uacpi_region_op; + +typedef struct uacpi_region_attach_data { + void *handler_context; + uacpi_namespace_node *region_node; + void *out_region_context; +} uacpi_region_attach_data; + +typedef struct uacpi_region_rw_data { + void *handler_context; + void *region_context; + union { + uacpi_phys_addr address; + uacpi_u64 offset; + }; + uacpi_u64 value; + uacpi_u8 byte_width; +} uacpi_region_rw_data; + +typedef struct uacpi_region_detach_data { + void *handler_context; + void *region_context; + uacpi_namespace_node *region_node; +} uacpi_region_detach_data; + +typedef uacpi_status (*uacpi_region_handler) + (uacpi_region_op op, uacpi_handle op_data); + +typedef uacpi_status (*uacpi_notify_handler) + (uacpi_handle context, uacpi_namespace_node *node, uacpi_u64 value); + +typedef enum uacpi_address_space { + UACPI_ADDRESS_SPACE_SYSTEM_MEMORY = 0, + UACPI_ADDRESS_SPACE_SYSTEM_IO = 1, + UACPI_ADDRESS_SPACE_PCI_CONFIG = 2, + UACPI_ADDRESS_SPACE_EMBEDDED_CONTROLLER = 3, + UACPI_ADDRESS_SPACE_SMBUS = 4, + UACPI_ADDRESS_SPACE_SYSTEM_CMOS = 5, + UACPI_ADDRESS_SPACE_PCI_BAR_TARGET = 6, + UACPI_ADDRESS_SPACE_IPMI = 7, + UACPI_ADDRESS_SPACE_GENERAL_PURPOSE_IO = 8, + UACPI_ADDRESS_SPACE_GENERIC_SERIAL_BUS = 9, + UACPI_ADDRESS_SPACE_PCC = 0x0A, + UACPI_ADDRESS_SPACE_PRM = 0x0B, + UACPI_ADDRESS_SPACE_FFIXEDHW = 0x7F, + + // Internal type + UACPI_ADDRESS_SPACE_TABLE_DATA = 0xDA1A, +} uacpi_address_space; +const uacpi_char *uacpi_address_space_to_string(uacpi_address_space space); + +typedef union uacpi_object_name { + uacpi_char text[4]; + uacpi_u32 id; +} uacpi_object_name; + +typedef enum uacpi_firmware_request_type { + UACPI_FIRMWARE_REQUEST_TYPE_BREAKPOINT, + UACPI_FIRMWARE_REQUEST_TYPE_FATAL, +} uacpi_firmware_request_type; + +typedef struct uacpi_firmware_request { + uacpi_u8 type; + + union { + // UACPI_FIRMWARE_REQUEST_BREAKPOINT + struct { + // The context of the method currently being executed + uacpi_handle ctx; + } breakpoint; + + // UACPI_FIRMWARE_REQUEST_FATAL + struct { + uacpi_u8 type; + uacpi_u32 code; + uacpi_u64 arg; + } fatal; + }; +} uacpi_firmware_request; + +#define UACPI_INTERRUPT_NOT_HANDLED 0 +#define UACPI_INTERRUPT_HANDLED 1 +typedef uacpi_u32 uacpi_interrupt_ret; + +typedef uacpi_interrupt_ret (*uacpi_interrupt_handler)(uacpi_handle); + +typedef enum uacpi_iteration_decision { + UACPI_ITERATION_DECISION_CONTINUE = 0, + UACPI_ITERATION_DECISION_BREAK, + + // Only applicable for uacpi_namespace_for_each_child + UACPI_ITERATION_DECISION_NEXT_PEER, +} uacpi_iteration_decision; + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/uacpi.h b/src/include/uacpi/uacpi.h new file mode 100644 index 0000000..9c5bebc --- /dev/null +++ b/src/include/uacpi/uacpi.h @@ -0,0 +1,258 @@ +#pragma once + +#include +#include +#include +#include + +#ifdef UACPI_REDUCED_HARDWARE +#define UACPI_MAKE_STUB_FOR_REDUCED_HARDWARE(fn, ret) \ + UACPI_NO_UNUSED_PARAMETER_WARNINGS_BEGIN \ + static inline fn { return ret; } \ + UACPI_NO_UNUSED_PARAMETER_WARNINGS_END + +#define UACPI_STUB_IF_REDUCED_HARDWARE(fn) \ + UACPI_MAKE_STUB_FOR_REDUCED_HARDWARE(fn,) +#define UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE(fn) \ + UACPI_MAKE_STUB_FOR_REDUCED_HARDWARE(fn, UACPI_STATUS_COMPILED_OUT) +#define UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE(fn) \ + UACPI_MAKE_STUB_FOR_REDUCED_HARDWARE(fn, UACPI_STATUS_OK) +#else + +#define UACPI_STUB_IF_REDUCED_HARDWARE(fn) fn; +#define UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE(fn) fn; +#define UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE(fn) fn; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Set up early access to the table subsystem. What this means is: + * - uacpi_table_find() and similar API becomes usable before the call to + * uacpi_initialize(). + * - No kernel API besides logging and map/unmap will be invoked at this stage, + * allowing for heap and scheduling to still be fully offline. + * - The provided 'temporary_buffer' will be used as a temporary storage for the + * internal metadata about the tables (list, reference count, addresses, + * sizes, etc). + * - The 'temporary_buffer' is replaced with a normal heap buffer allocated via + * uacpi_kernel_alloc() after the call to uacpi_initialize() and can therefore + * be reclaimed by the kernel. + * + * The approximate overhead per table is 56 bytes, so a buffer of 4096 bytes + * yields about 73 tables in terms of capacity. uACPI also has an internal + * static buffer for tables, "UACPI_STATIC_TABLE_ARRAY_LEN", which is configured + * as 16 descriptors in length by default. + */ +uacpi_status uacpi_setup_early_table_access( + void *temporary_buffer, uacpi_size buffer_size +); + +/* + * Bad table checksum should be considered a fatal error + * (table load is fully aborted in this case) + */ +#define UACPI_FLAG_BAD_CSUM_FATAL (1ull << 0) + +/* + * Unexpected table signature should be considered a fatal error + * (table load is fully aborted in this case) + */ +#define UACPI_FLAG_BAD_TBL_SIGNATURE_FATAL (1ull << 1) + +/* + * Force uACPI to use RSDT even for later revisions + */ +#define UACPI_FLAG_BAD_XSDT (1ull << 2) + +/* + * If this is set, ACPI mode is not entered during the call to + * uacpi_initialize. The caller is expected to enter it later at their own + * discretion by using uacpi_enter_acpi_mode(). + */ +#define UACPI_FLAG_NO_ACPI_MODE (1ull << 3) + +/* + * Don't create the \_OSI method when building the namespace. + * Only enable this if you're certain that having this method breaks your AML + * blob, a more atomic/granular interface management is available via osi.h + */ +#define UACPI_FLAG_NO_OSI (1ull << 4) + +/* + * Validate table checksums at installation time instead of first use. + * Note that this makes uACPI map the entire table at once, which not all + * hosts are able to handle at early init. + */ +#define UACPI_FLAG_PROACTIVE_TBL_CSUM (1ull << 5) + +/* + * Initializes the uACPI subsystem, iterates & records all relevant RSDT/XSDT + * tables. Enters ACPI mode. + * + * 'flags' is any combination of UACPI_FLAG_* above + */ +uacpi_status uacpi_initialize(uacpi_u64 flags); + +/* + * Parses & executes all of the DSDT/SSDT tables. + * Initializes the event subsystem. + */ +uacpi_status uacpi_namespace_load(void); + +/* + * Initializes all the necessary objects in the namespaces by calling + * _STA/_INI etc. + */ +uacpi_status uacpi_namespace_initialize(void); + +// Returns the current subsystem initialization level +uacpi_init_level uacpi_get_current_init_level(void); + +/* + * Evaluate an object within the namespace and get back its value. + * Either root or path must be valid. + * A value of NULL for 'parent' implies uacpi_namespace_root() relative + * lookups, unless 'path' is already absolute. + */ +uacpi_status uacpi_eval( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +); +uacpi_status uacpi_eval_simple( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +); + +/* + * Same as uacpi_eval() but without a return value. + */ +uacpi_status uacpi_execute( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args +); +uacpi_status uacpi_execute_simple( + uacpi_namespace_node *parent, const uacpi_char *path +); + +/* + * Same as uacpi_eval, but the return value type is validated against + * the 'ret_mask'. UACPI_STATUS_TYPE_MISMATCH is returned on error. + */ +uacpi_status uacpi_eval_typed( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object_type_bits ret_mask, + uacpi_object **ret +); +uacpi_status uacpi_eval_simple_typed( + uacpi_namespace_node *parent, const uacpi_char *path, + uacpi_object_type_bits ret_mask, uacpi_object **ret +); + +/* + * A shorthand for uacpi_eval_typed with UACPI_OBJECT_INTEGER_BIT. + */ +uacpi_status uacpi_eval_integer( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_u64 *out_value +); +uacpi_status uacpi_eval_simple_integer( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_u64 *out_value +); + +/* + * A shorthand for uacpi_eval_typed with + * UACPI_OBJECT_BUFFER_BIT | UACPI_OBJECT_STRING_BIT + * + * Use uacpi_object_get_string_or_buffer to retrieve the resulting buffer data. + */ +uacpi_status uacpi_eval_buffer_or_string( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +); +uacpi_status uacpi_eval_simple_buffer_or_string( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +); + +/* + * A shorthand for uacpi_eval_typed with UACPI_OBJECT_STRING_BIT. + * + * Use uacpi_object_get_string to retrieve the resulting buffer data. + */ +uacpi_status uacpi_eval_string( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +); +uacpi_status uacpi_eval_simple_string( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +); + +/* + * A shorthand for uacpi_eval_typed with UACPI_OBJECT_BUFFER_BIT. + * + * Use uacpi_object_get_buffer to retrieve the resulting buffer data. + */ +uacpi_status uacpi_eval_buffer( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +); +uacpi_status uacpi_eval_simple_buffer( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +); + +/* + * A shorthand for uacpi_eval_typed with UACPI_OBJECT_PACKAGE_BIT. + * + * Use uacpi_object_get_package to retrieve the resulting object array. + */ +uacpi_status uacpi_eval_package( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +); +uacpi_status uacpi_eval_simple_package( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +); + +/* + * Get the bitness of the currently loaded AML code according to the DSDT. + * + * Returns either 32 or 64. + */ +uacpi_status uacpi_get_aml_bitness(uacpi_u8 *out_bitness); + +/* + * Helpers for entering & leaving ACPI mode. Note that ACPI mode is entered + * automatically during the call to uacpi_initialize(). + */ +UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_enter_acpi_mode(void) +) +UACPI_ALWAYS_ERROR_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_leave_acpi_mode(void) +) + +/* + * Attempt to acquire the global lock for 'timeout' milliseconds. + * 0xFFFF implies infinite wait. + * + * On success, 'out_seq' is set to a unique sequence number for the current + * acquire transaction. This number is used for validation during release. + */ +uacpi_status uacpi_acquire_global_lock(uacpi_u16 timeout, uacpi_u32 *out_seq); +uacpi_status uacpi_release_global_lock(uacpi_u32 seq); + +/* + * Reset the global uACPI state by freeing all internally allocated data + * structures & resetting any global variables. After this call, uACPI must be + * re-initialized from scratch to be used again. + * + * This is called by uACPI automatically if a fatal error occurs during a call + * to uacpi_initialize/uacpi_namespace_load etc. in order to prevent accidental + * use of partially uninitialized subsystems. + */ +void uacpi_state_reset(void); + +#ifdef __cplusplus +} +#endif diff --git a/src/include/uacpi/utilities.h b/src/include/uacpi/utilities.h new file mode 100644 index 0000000..49f4c25 --- /dev/null +++ b/src/include/uacpi/utilities.h @@ -0,0 +1,184 @@ +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Checks whether the device at 'node' matches any of the PNP ids provided in + * 'list' (terminated by a UACPI_NULL). This is done by first attempting to + * match the value returned from _HID and then the value(s) from _CID. + * + * Note that the presence of the device (_STA) is not verified here. + */ +uacpi_bool uacpi_device_matches_pnp_id( + uacpi_namespace_node *node, + const uacpi_char *const *list +); + +/* + * Find all the devices in the namespace starting at 'parent' matching the + * specified 'hids' (terminated by a UACPI_NULL) against any value from _HID or + * _CID. Only devices reported as present via _STA are checked. Any matching + * devices are then passed to the 'cb'. + */ +uacpi_status uacpi_find_devices_at( + uacpi_namespace_node *parent, + const uacpi_char *const *hids, + uacpi_iteration_callback cb, + void *user +); + +/* + * Same as uacpi_find_devices_at, except this starts at the root and only + * matches one hid. + */ +uacpi_status uacpi_find_devices( + const uacpi_char *hid, + uacpi_iteration_callback cb, + void *user +); + +typedef enum uacpi_interrupt_model { + UACPI_INTERRUPT_MODEL_PIC = 0, + UACPI_INTERRUPT_MODEL_IOAPIC = 1, + UACPI_INTERRUPT_MODEL_IOSAPIC = 2, +} uacpi_interrupt_model; + +uacpi_status uacpi_set_interrupt_model(uacpi_interrupt_model); + +typedef struct uacpi_pci_routing_table_entry { + uacpi_u32 address; + uacpi_u32 index; + uacpi_namespace_node *source; + uacpi_u8 pin; +} uacpi_pci_routing_table_entry; + +typedef struct uacpi_pci_routing_table { + uacpi_size num_entries; + uacpi_pci_routing_table_entry entries[]; +} uacpi_pci_routing_table; +void uacpi_free_pci_routing_table(uacpi_pci_routing_table*); + +uacpi_status uacpi_get_pci_routing_table( + uacpi_namespace_node *parent, uacpi_pci_routing_table **out_table +); + +typedef struct uacpi_id_string { + // size of the string including the null byte + uacpi_u32 size; + uacpi_char *value; +} uacpi_id_string; +void uacpi_free_id_string(uacpi_id_string *id); + +/* + * Evaluate a device's _HID method and get its value. + * The returned struture must be freed using uacpi_free_id_string. + */ +uacpi_status uacpi_eval_hid(uacpi_namespace_node*, uacpi_id_string **out_id); + +typedef struct uacpi_pnp_id_list { + // number of 'ids' in the list + uacpi_u32 num_ids; + + // size of the 'ids' list including the string lengths + uacpi_u32 size; + + // list of PNP ids + uacpi_id_string ids[]; +} uacpi_pnp_id_list; +void uacpi_free_pnp_id_list(uacpi_pnp_id_list *list); + +/* + * Evaluate a device's _CID method and get its value. + * The returned structure must be freed using uacpi_free_pnp_id_list. + */ +uacpi_status uacpi_eval_cid(uacpi_namespace_node*, uacpi_pnp_id_list **out_list); + +/* + * Evaluate a device's _STA method and get its value. + * If this method is not found, the value of 'flags' is set to all ones. + */ +uacpi_status uacpi_eval_sta(uacpi_namespace_node*, uacpi_u32 *flags); + +/* + * Evaluate a device's _ADR method and get its value. + */ +uacpi_status uacpi_eval_adr(uacpi_namespace_node*, uacpi_u64 *out); + +/* + * Evaluate a device's _CLS method and get its value. + * The format of returned string is BBSSPP where: + * BB => Base Class (e.g. 01 => Mass Storage) + * SS => Sub-Class (e.g. 06 => SATA) + * PP => Programming Interface (e.g. 01 => AHCI) + * The returned struture must be freed using uacpi_free_id_string. + */ +uacpi_status uacpi_eval_cls(uacpi_namespace_node*, uacpi_id_string **out_id); + +/* + * Evaluate a device's _UID method and get its value. + * The returned struture must be freed using uacpi_free_id_string. + */ +uacpi_status uacpi_eval_uid(uacpi_namespace_node*, uacpi_id_string **out_uid); + + +// uacpi_namespace_node_info->flags +#define UACPI_NS_NODE_INFO_HAS_ADR (1 << 0) +#define UACPI_NS_NODE_INFO_HAS_HID (1 << 1) +#define UACPI_NS_NODE_INFO_HAS_UID (1 << 2) +#define UACPI_NS_NODE_INFO_HAS_CID (1 << 3) +#define UACPI_NS_NODE_INFO_HAS_CLS (1 << 4) +#define UACPI_NS_NODE_INFO_HAS_SXD (1 << 5) +#define UACPI_NS_NODE_INFO_HAS_SXW (1 << 6) + +typedef struct uacpi_namespace_node_info { + // Size of the entire structure + uacpi_u32 size; + + // Object information + uacpi_object_name name; + uacpi_object_type type; + uacpi_u8 num_params; + + // UACPI_NS_NODE_INFO_HAS_* + uacpi_u8 flags; + + /* + * A mapping of [S1..S4] to the shallowest D state supported by the device + * in that S state. + */ + uacpi_u8 sxd[4]; + + /* + * A mapping of [S0..S4] to the deepest D state supported by the device + * in that S state to be able to wake itself. + */ + uacpi_u8 sxw[5]; + + uacpi_u64 adr; + uacpi_id_string hid; + uacpi_id_string uid; + uacpi_id_string cls; + uacpi_pnp_id_list cid; +} uacpi_namespace_node_info; +void uacpi_free_namespace_node_info(uacpi_namespace_node_info*); + +/* + * Retrieve information about a namespace node. This includes the attached + * object's type, name, number of parameters (if it's a method), the result of + * evaluating _ADR, _UID, _CLS, _HID, _CID, as well as _SxD and _SxW. + * + * The returned structure must be freed with uacpi_free_namespace_node_info. + */ +uacpi_status uacpi_get_namespace_node_info( + uacpi_namespace_node *node, uacpi_namespace_node_info **out_info +); + +#ifdef __cplusplus +} +#endif diff --git a/src/lib/io.c b/src/lib/io.c new file mode 100644 index 0000000..769b62f --- /dev/null +++ b/src/lib/io.c @@ -0,0 +1,63 @@ +#include + +uint64_t rdmsr(uint64_t msr){ + uint32_t low, high; + asm volatile ( + "rdmsr" + : "=a"(low), "=d"(high) + : "c"(msr) + ); + return ((uint64_t)high << 32) | low; +} + +void wrmsr(uint64_t msr, uint64_t value){ + uint32_t low = value & 0xFFFFFFFF; + uint32_t high = value >> 32; + asm volatile ( + "wrmsr" + : + : "c"(msr), "a"(low), "d"(high) + ); +} + +void outb(uint16_t port, uint8_t val){ + asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) : "memory"); +} + +void outw(uint16_t port, uint16_t val){ + asm volatile ( "outw %0, %1" : : "a"(val), "Nd"(port) : "memory"); +} + +void outl(uint16_t port, uint32_t val){ + asm volatile ( "outl %0, %1" : : "a"(val), "Nd"(port) : "memory"); +} + + +uint8_t inb(uint16_t port){ + uint8_t ret; + asm volatile ( "inb %1, %0" + : "=a"(ret) + : "Nd"(port) + : "memory"); + return ret; +} + +uint16_t inw(uint16_t port){ + uint16_t ret; + asm volatile ( "inw %1, %0" + : "=a"(ret) + : "Nd"(port) + : "memory"); + return ret; +} + + +uint32_t inl(uint16_t port){ + uint32_t ret; + asm volatile ( "inl %1, %0" + : "=a"(ret) + : "Nd"(port) + : "memory"); + return ret; +} + diff --git a/src/lib/spinlock.c b/src/lib/spinlock.c new file mode 100644 index 0000000..b5919b1 --- /dev/null +++ b/src/lib/spinlock.c @@ -0,0 +1,15 @@ +#include +#include +#include + +void acquire_lock(atomic_flag *lock){ + while(atomic_flag_test_and_set_explicit(lock, memory_order_acquire)){ + asm volatile("nop"); + } + + atomic_thread_fence(memory_order_acquire); +} + +void free_lock(atomic_flag *lock){ + atomic_flag_clear_explicit(lock, memory_order_release); +} \ No newline at end of file diff --git a/src/lib/stdio.c b/src/lib/stdio.c new file mode 100644 index 0000000..ae0c62a --- /dev/null +++ b/src/lib/stdio.c @@ -0,0 +1,468 @@ +#include +#include +#include +#include +#include +#include "../include/stdio.h" +#include "../drivers/serial.h" +#include "../hal/tsc.h" + + +#define FORMAT_LENGTH 1 +#define NORMAL 0 +#define STATE_SHORT 2 +#define STATE_LONG 3 +#define FORMAT_SPECIFIER 1 + +#define LENGTH_DEFAULT 0 +#define LENGTH_SHORT_SHORT 1 +#define LENGTH_SHORT 2 +#define LENGTH_LONG 3 +#define LENGTH_LONG_LONG 4 + +extern bool serial_enabled; + +void klog(int level, const char *func, const char *msg){ + switch (level) { + case LOG_INFO: + kprintf("[{d}] info: {s}: {sn}", tsc_get_timestamp(), func, msg); + if(serial_enabled){ + serial_kprintf("{k}KLOG_INFO{k}: {s}: {sn}", ANSI_COLOR_MAGENTA, ANSI_COLOR_RESET, func, msg); + } + return; + case LOG_WARN: + kprintf("[{d}] {k}warning{k}: {s}: {sn}", tsc_get_timestamp(), ANSI_COLOR_YELLOW, ANSI_COLOR_RESET, func, msg); + if(serial_enabled){ + serial_kprintf("{k}KLOG_WARN{k}: {s}: {sn}", ANSI_COLOR_YELLOW, ANSI_COLOR_RESET, func, msg); + } + return; + case LOG_ERROR: + kprintf("[{d}] {k}error{k}: {s}: {sn}", tsc_get_timestamp(), ANSI_COLOR_RED, ANSI_COLOR_RESET, func, msg); + if(serial_enabled){ + serial_kprintf("{k}KLOG_ERROR{k}: {s}: {sn}", ANSI_COLOR_RED, ANSI_COLOR_RESET, func, msg); + } + return; + case LOG_SUCCESS: + kprintf("[{d}] {k}success{k}: {s}: {sn}", tsc_get_timestamp(), ANSI_COLOR_GREEN, ANSI_COLOR_RESET, func, msg); + if(serial_enabled){ + serial_kprintf("{k}KLOG_SUCCESS{k}: {s}: {sn}", ANSI_COLOR_GREEN, ANSI_COLOR_RESET, func, msg); + } + return; + } + + return; + +} + + +atomic_flag printf_lock = ATOMIC_FLAG_INIT; + +/* + printf() + params: + string + arguments + + available format specifiers: + {i}, {d} - integer + {s} - string + {c} - char + {k} - color + {n} - newline (doesnt take in a argument) + {x} - base16 + {b} - binary + */ + +int kprintf(const char *format_string, ...){ + extern struct flanterm_context *ft_ctx; + acquire_lock(&printf_lock); + int state = NORMAL; + va_list a_list; + va_start(a_list, format_string); + for(uint64_t i = 0; i < strlen(format_string); i++){ + char current = format_string[i]; // current char in string + switch (state){ + case NORMAL: + switch (current) { + case '{': + state = FORMAT_SPECIFIER; + break; + default: + print_char(ft_ctx, current); + break; + } + break; + case FORMAT_SPECIFIER: + switch (current) { + case 'n': + print_str(ft_ctx, "\n"); + break; + case 'k': + print_str(ft_ctx, va_arg(a_list, char*)); + break; + case 'd': + case 'i': + print_int(ft_ctx, va_arg(a_list, long long)); + break; + case 's': + print_str(ft_ctx, va_arg(a_list, char*)); + break; + case 'c': + ; + int ch = va_arg(a_list, int); + print_char(ft_ctx, ch); + break; + case 'x': + print_hex(ft_ctx, va_arg(a_list, uint64_t)); + break; + case 'b': + print_bin(ft_ctx, va_arg(a_list, uint64_t)); + break; + case 'l': + current++; + switch (current) { + case 'd': + print_int(ft_ctx, va_arg(a_list, long long int)); + break; + + } + break; + case '}': + state = NORMAL; + break; + + } + break; + } + + } + + va_end(a_list); + free_lock(&printf_lock); + return 0; +} + +int serial_kprintf(const char *format_string, ...){ + int state = NORMAL; + va_list a_list; + va_start(a_list, format_string); + for(uint64_t i = 0; i < strlen(format_string); i++){ + char current = format_string[i]; // current char in string + switch (state){ + case NORMAL: + switch (current) { + case '{': + state = FORMAT_SPECIFIER; + break; + default: + serial_print_char(current); + break; + } + break; + case FORMAT_SPECIFIER: + switch (current) { + case 'n': + serial_print("\n"); + break; + case 'k': + serial_print(va_arg(a_list, char*)); + break; + case 'd': + case 'i': + serial_print_int(va_arg(a_list, long long)); + break; + case 's': + serial_print(va_arg(a_list, char*)); + break; + case 'c': + ; + int ch = va_arg(a_list, int); + serial_print_char(ch); + + break; + case 'x': + serial_print_hex(va_arg(a_list, uint64_t)); + break; + case 'b': + serial_print_bin(va_arg(a_list, uint64_t)); + break; + case 'l': + current++; + switch (current) { + case 'd': + case 'i': + serial_print_int(va_arg(a_list, long long int)); + break; + + } + break; + case '}': + state = NORMAL; + break; + + } + break; + } + + } + + va_end(a_list); + + return 0; +} + +#define MAX_INTERGER_SIZE 128 + +void print_char(struct flanterm_context *ft_ctx, char c){ + kernel_framebuffer_print(&c, 1); +} + +void serial_print_char(char c){ + serial_write(c); +} + +void print_str(struct flanterm_context *ft_ctx, char *str){ + kernel_framebuffer_print(str, strlen(str)); +} + +void print_int(struct flanterm_context *ft_ctx, uint64_t num){ + char buffer[MAX_INTERGER_SIZE] = {0}; + + if(num == 0){ + buffer[0] = '0'; + } + + int arr[MAX_INTERGER_SIZE] = {0}; + int j = 0; + + while(num != 0){ + int mod = num % 10; + arr[j] = dtoc(mod); + num /= 10; + j++; + + if(j == MAX_INTERGER_SIZE){ + return; + } + } + + /* Reverse buffer */ + for(int i = 0; i < j; i++){ + buffer[i] = arr[j - i - 1]; + } + + kernel_framebuffer_print(buffer, strlen(buffer)); +} + +void print_hex(struct flanterm_context *ft_ctx, uint64_t num){ + char buffer[MAX_INTERGER_SIZE] = {0}; + + if(num == 0){ + buffer[0] = '0'; + } + + int arr[MAX_INTERGER_SIZE] = {0}; + int j = 0; + + while(num != 0){ + int mod = num % 16; + arr[j] = dtoc(mod); + num /= 16; + j++; + + if(j == MAX_INTERGER_SIZE){ + return; + } + } + + /* Reverse buffer */ + for(int i = 0; i < j; i++){ + buffer[i] = arr[j - i - 1]; + } + + kernel_framebuffer_print(buffer, strlen(buffer)); +} + +void print_bin(struct flanterm_context *ft_ctx, uint64_t num){ + char buffer[MAX_INTERGER_SIZE] = {0}; + + int arr[MAX_INTERGER_SIZE] = {0}; + int j = 0; + + while(num != 0){ + int mod = num % 2; + arr[j] = dtoc(mod); + num /= 2; + j++; + + if(j == MAX_INTERGER_SIZE){ + return; + } + } + + /* Reverse buffer */ + for(int i = 0; i < j; i++){ + buffer[i] = arr[j - i - 1]; + } + + kernel_framebuffer_print(buffer, strlen(buffer)); +} + +void serial_print_int(uint64_t num){ + char buffer[MAX_INTERGER_SIZE] = {0}; + + int arr[MAX_INTERGER_SIZE] = {0}; + int j = 0; + + while(num != 0){ + int mod = num % 10; + arr[j] = dtoc(mod); + num /= 10; + j++; + + if(j == MAX_INTERGER_SIZE){ + return; + } + } + + /* Reverse buffer */ + for(int i = 0; i < j; i++){ + buffer[i] = arr[j - i - 1]; + } + + kernel_serial_print(buffer, strlen(buffer)); +} + +void serial_print_hex(uint64_t num){ + char buffer[MAX_INTERGER_SIZE] = {0}; + + int arr[MAX_INTERGER_SIZE] = {0}; + int j = 0; + + while(num != 0){ + int mod = num % 16; + arr[j] = dtoc(mod); + num /= 16; + j++; + + if(j == MAX_INTERGER_SIZE){ + return; + } + } + + /* Reverse buffer */ + for(int i = 0; i < j; i++){ + buffer[i] = arr[j - i - 1]; + } + + kernel_serial_print(buffer, strlen(buffer)); +} + +void serial_print_bin(uint64_t num){ + char buffer[MAX_INTERGER_SIZE] = {0}; + + int arr[MAX_INTERGER_SIZE] = {0}; + int j = 0; + + while(num != 0){ + int mod = num % 2; + arr[j] = dtoc(mod); + num /= 2; + j++; + + if(j == MAX_INTERGER_SIZE){ + return; + } + } + + /* Reverse buffer */ + for(int i = 0; i < j; i++){ + buffer[i] = arr[j - i - 1]; + } + + kernel_serial_print(buffer, strlen(buffer)); +} + + +char toupper(char c){ + switch(c){ + case 'a': + return 'A'; + case 'b': + return 'B'; + case 'c': + return 'C'; + case 'd': + return 'D'; + case 'e': + return 'E'; + case 'f': + return 'F'; + case 'g': + return 'G'; + case 'h': + return 'H'; + case 'i': + return 'I'; + case 'j': + return 'J'; + case 'k': + return 'K'; + case 'l': + return 'L'; + case 'm': + return 'M'; + case 'n': + return 'N'; + case 'o': + return 'O'; + case 'p': + return 'P'; + case 't': + return 'T'; + case 'r': + return 'R'; + case 's': + return 'S'; + case 'u': + return 'U'; + case 'v': + return 'V'; + case 'w': + return 'W'; + case 'x': + return 'X'; + case 'y': + return 'Y'; + case 'z': + return 'Z'; + default: + return c; + + } +} + + +atomic_flag fb_spinlock = ATOMIC_FLAG_INIT; + +/* Eventually fix printf so that these print_* functions dont + write to the framebuffer but instead return to printf */ + +/* Prints a char array to the framebuffer, thread safe*/ +void kernel_framebuffer_print(char *buffer, size_t n){ + extern struct flanterm_context *ft_ctx; + //acquire_lock(&fb_spinlock); + flanterm_write(ft_ctx, buffer, n); + //free_lock(&fb_spinlock); +} + +atomic_flag serial_spinlock = ATOMIC_FLAG_INIT; + +/* Prints a char array to serial, thread safe*/ +void kernel_serial_print(char *buffer, size_t n){ + //acquire_lock(&serial_spinlock); + for(size_t i = 0; i < n; i++){ + serial_print_char(buffer[i]); + } + //free_lock(&serial_spinlock); +} \ No newline at end of file diff --git a/src/lib/string.c b/src/lib/string.c new file mode 100644 index 0000000..e217d53 --- /dev/null +++ b/src/lib/string.c @@ -0,0 +1,82 @@ +#include +#include +#include + +void *memset(void *dest, int c, uint64_t n){ + uint8_t *p = (uint8_t *)dest; + + for(uint64_t i = 0; i < n; i++){ + p[i] = (uint8_t)c; + } + + return dest; +} + +void *memcpy(void *dest, const void *src, uint64_t n){ + uint8_t *pdest = (uint8_t *)dest; + const uint8_t *psrc = (const uint8_t *)src; + + for(uint64_t i = 0; i < n; i++){ + pdest[i] = psrc[i]; + } + + return dest; +} + +/* stolen from limine c template */ +void *memmove(void *dest, const void *src, uint64_t n) { + uint8_t *pdest = (uint8_t *)dest; + const uint8_t *psrc = (const uint8_t *)src; + + if(src > dest){ + for (uint64_t i = 0; i < n; i++) { + pdest[i] = psrc[i]; + } + }else if(src < dest){ + for (uint64_t i = n; i > 0; i--) { + pdest[i-1] = psrc[i-1]; + } + } + + return dest; +} + +int memcmp(const void *s1, const void *s2, uint64_t n){ + const uint8_t *p1 = (const uint8_t *)s1; + const uint8_t *p2 = (const uint8_t *)s2; + + for(uint64_t i = 0; i < n; i++){ + if(p1[i] != p2[i]){ + return p1[i] < p2[i] ? -1 : 1; + } + } + + return 0; + +} + +uint64_t strlen(const char* str){ + uint64_t i = 0; + + while (str[i] != '\0'){ + i++; + } + + return i; +} + +/* Converts a digit to a character */ +char dtoc(int digit){ + if(digit > 15){ + return 0; + }else if(digit == 0){ + return '0'; + } + + if(digit < 10){ + return '0' + digit; + }else{ + return 'A' + digit - 10; + } +} + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4042c34 --- /dev/null +++ b/src/main.c @@ -0,0 +1,136 @@ +#include +#include +#include +#include "limine.h" +#include "include/stdio.h" +#include "../flanterm/src/flanterm.h" +#include "flanterm/src/flanterm_backends/fb.h" +#include "hal/gdt.h" +#include "hal/idt.h" +#include "hal/apic.h" +#include "hal/timer.h" +#include "hal/smp.h" +#include "hal/tsc.h" +#include "mm/pmm.h" +#include "mm/vmm.h" +#include "mm/kmalloc.h" +#include "sys/acpi.h" +#include "sys/pci.h" +#include "drivers/serial.h" +#include "drivers/pmt.h" +#include "drivers/ahci.h" +#include "scheduler/sched.h" + +static volatile struct limine_framebuffer_request framebuffer_request = { + .id = LIMINE_FRAMEBUFFER_REQUEST, + .revision = 0, +}; + +static volatile struct limine_hhdm_request hhdm_request = { + .id = LIMINE_HHDM_REQUEST, + .revision = 0, +}; + +struct flanterm_context *ft_ctx; + +uint64_t hhdmoffset = 0; + +void _start(void){ + + if(hhdm_request.response == NULL){ + goto death; + } + + + hhdmoffset = hhdm_request.response->offset; + + /* initalize framebuffer */ + struct limine_framebuffer_response *fb_response = framebuffer_request.response; + + if(fb_response == NULL){ + goto death; + } + + struct limine_framebuffer *fb = fb_response->framebuffers[0]; + + if(fb == NULL){ + goto death; + } + + ft_ctx = flanterm_fb_init( + NULL, + NULL, + fb->address, fb->width, fb->height, fb->pitch, + fb->red_mask_size, fb->red_mask_shift, + fb->green_mask_size, fb->green_mask_shift, + fb->blue_mask_size, fb->blue_mask_shift, + NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, 0, 0, 1, + 0, 0, + 0 + ); + + kprintf("Welcome to SFB/25{n}"); + + extern link_symbol_ptr text_start_addr, text_end_addr; + + klog(LOG_INFO, "serial", "Initalizing serial controller"); + serial_init(); + klog(LOG_SUCCESS, "serial", "Done!"); + + klog(LOG_INFO, "gdt", "Setting up the GDT"); + set_gdt(); + klog(LOG_SUCCESS, "gdt", "Done!"); + + klog(LOG_INFO, "idt", "Setting up the IDT"); + set_idt(); + klog(LOG_SUCCESS, "idt", "Done!");; + + klog(LOG_INFO, "acpi", "Reading ACPI tables"); + acpi_init(); + klog(LOG_SUCCESS, "acpi", "Done!"); + + klog(LOG_INFO, "apic", "Initalizing APIC"); + apic_init(); + klog(LOG_SUCCESS, "apic", "Done!"); + + tsc_init(); + + + klog(LOG_INFO, "pmm", "Setting up the PMM"); + pmm_init(); + klog(LOG_SUCCESS, "pmm", "Done!"); + + klog(LOG_INFO, "vmm", "Setting up the page tables"); + vmm_init(); + klog(LOG_SUCCESS, "vmm", "Done!"); + + kernel_heap_init(); + + klog(LOG_INFO, "smp", "Starting APs"); + smp_init(); + klog(LOG_SUCCESS, "smp", "Done!"); + + klog(LOG_INFO, "pci", "Getting le pci"); + pci_init(); + klog(LOG_SUCCESS, "pci", "Done!"); + + scheduler_init(); + +/* klog(LOG_INFO, "ahci", "Initializing AHCI controller"); + ahci_init(); + klog(LOG_SUCCESS, "ahci", "Done!"); */ + + death: + for(;;); +} + +bool kernel_killed = false; +void kkill(void){ + kernel_killed = true; + asm volatile("cli; hlt"); + for(;;); +} diff --git a/src/mm/kmalloc.c b/src/mm/kmalloc.c new file mode 100644 index 0000000..f56e965 --- /dev/null +++ b/src/mm/kmalloc.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include +#include +#include "pmm.h" +#include "vmm.h" +#include "kmalloc.h" + +#define KERNEL_MAX_BLOCK 512 + +typedef struct block_t { + uint64_t addr; + uint32_t size; + bool free; +} block_t; + +block_t *base = NULL; +uint64_t *heap_addr = NULL; + +void kernel_heap_init(){ + extern struct limine_memmap_response *memmap_response; + extern uint64_t pmm_page_count; + + /* Allocate memory for the blocks*/ + base = kernel_allocate_memory(KERNEL_MAX_BLOCK * sizeof(block_t), PTE_BIT_RW | PTE_BIT_NX); + + if(!base){ + klog(LOG_ERROR, __func__, "Failed to allocate memory for kernel heap blocks"); + kkill(); + } + + memset(base, 0, KERNEL_MAX_BLOCK * sizeof(block_t)); + + /* Allocate memory for the heap */ + heap_addr = kernel_allocate_memory(KERNEL_HEAP_SIZE, PTE_BIT_RW | PTE_BIT_NX); + + if(!heap_addr){ + klog(LOG_ERROR, __func__, "Failed to allocate memory for the kernel heap"); + kkill(); + } + + base->free = true; +} + +void *kmalloc(uint64_t size){ + + /* First check if there is a free block which fits the size requirement */ + for(int i = 0; i < KERNEL_MAX_BLOCK; i++){ + if(base[i].addr && base[i].free && base[i].size >= size){ + base[i].free = false; + return (void*)base[i].addr; + } + } + + int i = 0; + /* Parse the list until you find the next free block */ + while (!base[i].free && !base[i].addr){ + if(i > KERNEL_MAX_BLOCK){ + /* Over max block limit */ + return NULL; + } + + i++; + } + + /* Fill this block in */ + uint64_t addr = (uint64_t)heap_addr; + + /* Calculate address offset */ + for(int j = 0; j < i; j++){ + if(addr > ((uint64_t)heap_addr + KERNEL_HEAP_SIZE)){ + /* Out of heap memory */ + return NULL; + } + + addr += base[j].size; + } + + memset((uint64_t*)addr, 0, size); + + base[i].addr = addr; + base[i].free = false; + base[i].size = size; + + return (void*)addr; +} + +void kfree(void *addr){ + for(int i = 0; i < KERNEL_MAX_BLOCK; i++){ + if(base[i].addr == (uint64_t)addr){ + base[i].free = true; + memset((void*)base[i].addr, 0, base[i].size); + return; + } + } + + kprintf("kfree: attempted to free non-heap address!\n"); + kkill(); +} + +void *krealloc(void *addr, uint64_t size) { + if (addr == NULL) { + return kmalloc(size); + } + + if (size == 0) { + kfree(addr); + return NULL; + } + + // Find the block corresponding to the pointer + int i; + for (i = 0; i < KERNEL_MAX_BLOCK; i++) { + if (base[i].addr == (uint64_t)addr) { + break; + } + } + + if (i == KERNEL_MAX_BLOCK) { + kprintf("krealloc: attempted to realloc non-heap address!\n"); + kkill(); + } + + block_t *block = &base[i]; + uint64_t old_size = block->size; + + // If the current size is already sufficient, return the same pointer + if (old_size >= size) { + return addr; + } + + // Check if this block is the last allocated block in the array + bool is_last = true; + for (int j = i + 1; j < KERNEL_MAX_BLOCK; j++) { + if (base[j].addr != 0) { + is_last = false; + break; + } + } + + // If it's the last block, check if there's enough space to expand + if (is_last) { + uint64_t current_end = block->addr + block->size; + uint64_t heap_end = (uint64_t)heap_addr + KERNEL_HEAP_SIZE; + uint64_t available = heap_end - current_end; + + if (available >= (size - old_size)) { + // Expand the block in place + block->size = size; + return addr; + } + } + + // Allocate a new block, copy data, and free the old block + void *new_ptr = kmalloc(size); + if (!new_ptr) { + return NULL; // Allocation failed + } + + memcpy(new_ptr, addr, old_size); + kfree(addr); + + return new_ptr; +} \ No newline at end of file diff --git a/src/mm/kmalloc.h b/src/mm/kmalloc.h new file mode 100644 index 0000000..50b3d4a --- /dev/null +++ b/src/mm/kmalloc.h @@ -0,0 +1,10 @@ +#include +void kernel_heap_init(); + +void heap_free(uint64_t *addr); +uint64_t *heap_alloc(); + +void *kmalloc(uint64_t size); +void kfree(void *addr); + +#define KERNEL_HEAP_SIZE 0x10000000 \ No newline at end of file diff --git a/src/mm/pmm.c b/src/mm/pmm.c new file mode 100644 index 0000000..216898b --- /dev/null +++ b/src/mm/pmm.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include "pmm.h" +#include "kmalloc.h" + +static volatile struct limine_memmap_request memmap_request = { + .id = LIMINE_MEMMAP_REQUEST, + .revision = 0, +}; + +extern uint64_t hhdmoffset; + +uint64_t pmm_free_page_count = 0; +uint64_t pmm_page_count = 0; +uint64_t mem_size = 0; + +struct limine_memmap_response *memmap_response; + +/* Freelist implementation */ +uint64_t *free_list = NULL; + +atomic_flag pmm_lock = ATOMIC_FLAG_INIT; + +void pmm_free(uint64_t *addr){ + acquire_lock(&pmm_lock); + uint64_t *virt_addr = (uint64_t*)((uint64_t)addr+hhdmoffset); + /* Make the given page point to the previous free page */ + + *virt_addr = (uint64_t)free_list; + + /* Make the free_list point to the newly freed page */ + free_list = virt_addr; + + pmm_free_page_count++; + free_lock(&pmm_lock); + return; +} + +uint64_t *pmm_alloc(){ + acquire_lock(&pmm_lock); + if(pmm_free_page_count <= 0){ + return NULL; + } + + /* Fetch the address of the free page in free_list and make it point to the next free page */ + uint64_t *addr = (uint64_t*)((uint64_t)free_list - hhdmoffset); + free_list = (uint64_t*)(*free_list); + pmm_free_page_count--; + free_lock(&pmm_lock); + return addr; +} + +void pmm_init(){ + + if(memmap_request.response == NULL){ + klog(LOG_ERROR, __func__, "Memmap response is null"); + kkill(); + } + + memmap_response = memmap_request.response; + + + struct limine_memmap_entry **entries = memmap_response->entries; + + for(uint64_t i = 0; i < memmap_response->entry_count; i++){ + switch (entries[i]->type) { + case LIMINE_MEMMAP_USABLE: + //kprintf("usable: base: 0x{x}, length: 0x{xn}", entries[i]->base, entries[i]->length); + mem_size += entries[i]->length; + break; + default: + ; + //kprintf("base: 0x{x}, length: 0x{xn}", entries[i]->base, entries[i]->length); + + } + } + + kprintf("pmm: got a total of {d}MB of memory\n", mem_size / 1048576); + + + bool first_entry = true; + + + uint64_t j; + uint64_t i; + + /* Dogshit fix this */ + for(i = 0; i < memmap_response->entry_count; i++){ + switch (entries[i]->type) { + case LIMINE_MEMMAP_USABLE: + /* First set the first entry if it isn't set already */ + if(first_entry == true){ + first_entry = false; + free_list = (uint64_t*)(entries[i]->base + hhdmoffset); + j = 1; + }else{ + j = 0; + } + + for(; j < (entries[i]->length / BLOCK_SIZE); j++){ + pmm_free((uint64_t*)(entries[i]->base + j*BLOCK_SIZE)); + pmm_page_count++; + } + } + } + +} \ No newline at end of file diff --git a/src/mm/pmm.h b/src/mm/pmm.h new file mode 100644 index 0000000..06c870a --- /dev/null +++ b/src/mm/pmm.h @@ -0,0 +1,14 @@ +#include +#include + +#define BLOCK_SIZE 4096 + +typedef struct free_page_t { + struct free_page_t *next; + uint8_t _padding[4088]; +} __attribute((packed)) free_page_t; + +void pmm_init(void); +uint64_t *pmm_alloc(); +void pmm_free(uint64_t *addr); + diff --git a/src/mm/vmm.c b/src/mm/vmm.c new file mode 100644 index 0000000..da6d683 --- /dev/null +++ b/src/mm/vmm.c @@ -0,0 +1,306 @@ +#include +#include +#include +#include +#include +#include +#include "pmm.h" +#include "vmm.h" +#include "../sys/acpi.h" +#include "../hal/apic.h" + +struct limine_kernel_address_request kernel_addr_request = { + .id = LIMINE_KERNEL_ADDRESS_REQUEST, + .revision = 0 +}; + +struct limine_kernel_address_response *kernel_address; + +extern uint64_t hhdmoffset; + +uint64_t *kernel_page_map = 0; +uint64_t kernel_virt = 0; + +uint64_t text_start, text_end, rodata_start, rodata_end, data_start, data_end; + +uint64_t kernel_start, kernel_end; + +void vmm_set_ctx(uint64_t *page_map){ + __asm__ volatile ( + "movq %0, %%cr3\n" + : : "r" ((uint64_t)((uint64_t)(page_map) - hhdmoffset)) : "memory" + ); +} + + +void vmm_init(){ + + struct limine_kernel_address_response *kernel_address = kernel_addr_request.response; + + if(!kernel_address){ + klog(LOG_ERROR, __func__, "Kernel address not recieved"); + } + + kernel_page_map = (uint64_t*)((uint64_t)pmm_alloc() + hhdmoffset); + + if(!kernel_page_map){ + klog(LOG_ERROR, __func__, "Allocating block for page map failed"); + } + + memset(kernel_page_map, 0, PAGE_SIZE); + + // map kernel, stolen + extern link_symbol_ptr text_start_addr, text_end_addr, + rodata_start_addr, rodata_end_addr, + data_start_addr, data_end_addr; + + text_start = ALIGN_DOWN((uint64_t)text_start_addr, PAGE_SIZE), + rodata_start = ALIGN_DOWN((uint64_t)rodata_start_addr, PAGE_SIZE), + data_start = ALIGN_DOWN((uint64_t)data_start_addr, PAGE_SIZE), + text_end = ALIGN_UP((uint64_t)text_end_addr, PAGE_SIZE), + rodata_end = ALIGN_UP((uint64_t)rodata_end_addr, PAGE_SIZE), + data_end = ALIGN_UP((uint64_t)data_end_addr, PAGE_SIZE); + + + // map usable entries, framebuffer and bootloader reclaimable shit + extern struct limine_memmap_response *memmap_response; + for(uint64_t i = 0; i < memmap_response->entry_count; i++){ + if(memmap_response->entries[i]->type == LIMINE_MEMMAP_USABLE){ + for(uint64_t j = 0; j < memmap_response->entries[i]->length; j+=PAGE_SIZE){ + vmm_map_page(kernel_page_map, memmap_response->entries[i]->base+j+hhdmoffset, memmap_response->entries[i]->base+j, PTE_BIT_PRESENT | PTE_BIT_RW); + } + } + if(memmap_response->entries[i]->type == LIMINE_MEMMAP_FRAMEBUFFER){ + for(uint64_t j = 0; j < memmap_response->entries[i]->length; j+=PAGE_SIZE){ + vmm_map_page(kernel_page_map, memmap_response->entries[i]->base+j+hhdmoffset, memmap_response->entries[i]->base+j, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + } + } + if(memmap_response->entries[i]->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE){ + for(uint64_t j = 0; j < memmap_response->entries[i]->length; j+=PAGE_SIZE){ + vmm_map_page(kernel_page_map, memmap_response->entries[i]->base+j+hhdmoffset, memmap_response->entries[i]->base+j, PTE_BIT_PRESENT | PTE_BIT_RW); + } + } + if(memmap_response->entries[i]->type == LIMINE_MEMMAP_ACPI_RECLAIMABLE){ + for(uint64_t j = 0; j < memmap_response->entries[i]->length; j+=PAGE_SIZE){ + vmm_map_page(kernel_page_map, memmap_response->entries[i]->base+j+hhdmoffset, memmap_response->entries[i]->base+j, PTE_BIT_PRESENT | PTE_BIT_RW); + } + } + + } + + for (uintptr_t text_addr = text_start; text_addr < text_end; text_addr += PAGE_SIZE) { + uintptr_t phys = text_addr - kernel_address->virtual_base + kernel_address->physical_base; + vmm_map_page(kernel_page_map, text_addr, phys, PTE_BIT_PRESENT); + } + /* Kernel starts with the text section */ + kernel_start = text_start; + + kprintf("vmm: text_start: 0x{xn}vmm: text_end: 0x{xn}", text_start, text_end); + + for (uintptr_t rodata_addr = rodata_start; rodata_addr < rodata_end; rodata_addr += PAGE_SIZE) { + uintptr_t phys = rodata_addr - kernel_address->virtual_base + kernel_address->physical_base; + vmm_map_page(kernel_page_map, rodata_addr, phys, PTE_BIT_PRESENT | PTE_BIT_NX); + } + + kprintf("vmm: rodata_start: 0x{xn}vmm: rodata_end: 0x{xn}", rodata_start, rodata_end); + + for (uintptr_t data_addr = data_start; data_addr < data_end; data_addr += PAGE_SIZE) { + uintptr_t phys = data_addr - kernel_address->virtual_base + kernel_address->physical_base; + vmm_map_page(kernel_page_map, data_addr, phys, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + } + + kprintf("vmm: data_start: 0x{xn}vmm: data_end: 0x{xn}", data_start, data_end); + + /* Kernel ends with the data section */ + kernel_end = data_end; + + extern uint64_t lapic_address; + + /* Map the APIC */ + vmm_map_page(kernel_page_map, lapic_address, lapic_address - hhdmoffset, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + + /* Map the ACPI tables */ + extern xsdt_t *xsdt; + extern rsdt_t *rsdt; + + if(!xsdt && rsdt){ + /* Map the amount of pages occupied by the RSDT + 1, because even if it doesn't + fill an entire page it still requires a page */ + for(uint64_t i = 0; i < rsdt->header.length / PAGE_SIZE + 1; i++){ + kprintf("mapping 0x{xn}", (uint64_t)rsdt + i * PAGE_SIZE); + vmm_map_page(kernel_page_map, (uint64_t)rsdt + i * PAGE_SIZE, ((uint64_t)rsdt - hhdmoffset) + i * PAGE_SIZE, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + } + }else{ + for(uint64_t i = 0; i < xsdt->header.length / PAGE_SIZE + 1; i++){ + kprintf("mapping 0x{xn}", (uint64_t)xsdt + i * PAGE_SIZE); + vmm_map_page(kernel_page_map, (uint64_t)xsdt + i * PAGE_SIZE, ((uint64_t)xsdt - hhdmoffset) + i * PAGE_SIZE, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + } + } + + vmm_set_ctx(kernel_page_map); + + asm volatile( + "movq %%cr3, %%rax\n\ + movq %%rax, %%cr3\n" + : : : "rax" + ); + +} + +uint64_t *get_lower_table(uint64_t *page_map, uint64_t offset){ + + if((page_map[offset] & PTE_BIT_PRESENT) != 0){ + return (uint64_t*)( ((uint64_t)page_map[offset] & 0x000ffffffffff000) + hhdmoffset); + } + + + uint64_t *ret = pmm_alloc(); + + if(!ret){ + klog(LOG_ERROR, __func__, "Failed to allocate page table"); + kprintf("page_map: 0x{xn}", (uint64_t)page_map); + kprintf("offset: 0x{xn}", offset); + + return NULL; + } + + memset((uint64_t*)((uint64_t)ret + hhdmoffset), 0, PAGE_SIZE); + + page_map[offset] = (uint64_t)ret | PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_US; + + return (uint64_t*)((uint64_t)ret + hhdmoffset); + + +} + +atomic_flag page_table_lock = ATOMIC_FLAG_INIT; + +void vmm_map_page(uint64_t *page_map, uint64_t virt_addr, uint64_t phys_addr, uint64_t flags){ + /* Probably slow, fix in future */ + acquire_lock(&page_table_lock); + + uint64_t pml4_offset = (virt_addr >> 39) & 0x1ff; + uint64_t pdp_offset = (virt_addr >> 30) & 0x1ff; + uint64_t pd_offset = (virt_addr >> 21) & 0x1ff; + uint64_t pt_offset = (virt_addr >> 12) & 0x1ff; + + uint64_t *pdp = get_lower_table(page_map, pml4_offset); + + if(!pdp){ + klog(LOG_ERROR, __func__, "Failed to allocate PDP"); + kkill(); + } + + uint64_t *pd = get_lower_table(pdp, pdp_offset); + + if(!pd){ + klog(LOG_ERROR, __func__, "Failed to allocate PD"); + kkill(); + } + + uint64_t *pt = get_lower_table(pd, pd_offset); + + if(!pt){ + klog(LOG_ERROR, __func__, "Failed to allocate PT"); + kkill(); + } + + pt[pt_offset] = phys_addr | flags; + + asm volatile( + "movq %%cr3, %%rax\n\ + movq %%rax, %%cr3\n" + : : : "rax" + ); + + free_lock(&page_table_lock); + +} + +void vmm_free_page(uint64_t *page_map, uint64_t virt_addr){ + uint64_t pml4_offset = (virt_addr >> 39) & 0x1ff; + uint64_t pdp_offset = (virt_addr >> 30) & 0x1ff; + uint64_t pd_offset = (virt_addr >> 21) & 0x1ff; + uint64_t pt_offset = (virt_addr >> 12) & 0x1ff; + + uint64_t *pdp = get_lower_table(page_map, pml4_offset); + + + if(!pdp){ + klog(LOG_ERROR, __func__, "Failed to allocate PDP"); + kkill(); + } + + uint64_t *pd = get_lower_table(pdp, pdp_offset); + + if(!pd){ + klog(LOG_ERROR, __func__, "Failed to allocate PD"); + kkill(); + } + + + uint64_t *pt = get_lower_table(pd, pd_offset); + + if(!pt){ + klog(LOG_ERROR, __func__, "Failed to allocate PT"); + kkill(); + } + + /* Free the page at the physical address pointed by the pt entry */ + pmm_free((uint64_t*)(pt[pt_offset] & 0x000ffffffffff000)); + + /* Set it to zero (mark as not present) */ + pt[pt_offset] = 0; + + asm volatile( + "movq %%cr3, %%rax\n\ + movq %%rax, %%cr3\n" + : : : "rax" + ); +} + +/* Maps `size` number of free pages at the specified virtual address */ +int vmm_map_continous_pages(uint64_t *page_map, uint64_t virt_addr, uint64_t phys_addr, uint64_t size, uint64_t flags){ + for(uint64_t i = 0; i < size; i++){ + vmm_map_page(page_map, virt_addr + i * PAGE_SIZE, (uint64_t)phys_addr, flags); + } + + return 0; +} + + + +/* Allocates and maps memory into the kernel address space */ +void *kernel_allocate_memory(uint64_t size, uint64_t flags){ + + if(size == 0){ + return NULL; + } + + void *ret = NULL; + for(uint64_t i = 0; i < size; i += PAGE_SIZE){ + ret = pmm_alloc(); + + + if(!ret){ + return NULL; + } + + vmm_map_page(kernel_page_map, (uint64_t)ret + hhdmoffset, (uint64_t)ret, PTE_BIT_PRESENT | flags); + } + + return (void*)((uint64_t)ret + hhdmoffset); +} + +/* Maps pages from phys_addr to phys_addr+size into the kernels address space */ +void kernel_map_pages(void *phys_addr, uint64_t size, uint64_t flags){ + for(uint64_t i = 0; i < size; i++){ + vmm_map_page(kernel_page_map, (uint64_t)phys_addr + hhdmoffset + (i * PAGE_SIZE), (uint64_t)phys_addr + (i * PAGE_SIZE), PTE_BIT_PRESENT | flags); + } +} + +void kernel_unmap_pages(void *addr, uint64_t size){ + for(uint64_t i = 0; i < size; i++){ + vmm_free_page(kernel_page_map, (uint64_t)addr + i*PAGE_SIZE); + } +} diff --git a/src/mm/vmm.h b/src/mm/vmm.h new file mode 100644 index 0000000..2c2df7d --- /dev/null +++ b/src/mm/vmm.h @@ -0,0 +1,23 @@ + +#include + +#define PTE_BIT_PRESENT 0x1 // Present bit +#define PTE_BIT_RW 0x2 // Read/write bit +#define PTE_BIT_US 0x4 // User and Supervisor bit +#define PTE_BIT_NX 0x4000000000000000 // Non-executable bit +#define PTE_BIT_UNCACHABLE (1 << 4) + +#define PAGE_SIZE 4096 + +void tlb_flush(void); +void vmm_map_page(uint64_t *page_map, uint64_t virt_address, uint64_t phys_address, uint64_t flags); +int vmm_map_continous_pages(uint64_t *page_map, uint64_t virt_addr, uint64_t phys_addr, uint64_t size, uint64_t flags); +void vmm_free_page(uint64_t *page_map, uint64_t virt_addr); +void vmm_init(); +void vmm_set_ctx(uint64_t *page_map); +void *kernel_allocate_memory(uint64_t size, uint64_t flags); +void kernel_map_pages(void *phys_addr, uint64_t size, uint64_t flags); +void kernel_unmap_pages(void *addr, uint64_t size); + +typedef char link_symbol_ptr[]; + diff --git a/src/scheduler/sched.asm b/src/scheduler/sched.asm new file mode 100644 index 0000000..26045d7 --- /dev/null +++ b/src/scheduler/sched.asm @@ -0,0 +1,47 @@ +default rel + +global switch_context + +%macro save_context 0 + push rbx + push rsp + push rbp + push r12 + push r13 + push r14 + push r15 +%endmacro + +%macro load_context 0 + pop r15 + pop r14 + pop r13 + pop r12 + pop rbp + pop rsp + pop rbx +%endmacro + +; Switch context from old to new +; void switch_context(context *old, context* new); +; rdi rsi +switch_context: + + save_context + + mov rdi, rsp + mov rsp, rsi + + load_context + + retfq + + + + + + + + + + diff --git a/src/scheduler/sched.c b/src/scheduler/sched.c new file mode 100644 index 0000000..70002ce --- /dev/null +++ b/src/scheduler/sched.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include "../hal/smp.h" +#include +#include "../mm/kmalloc.h" +#include "sched.h" + +extern context *save_context(); + +extern void switch_context(context *old, context *new); + +#define QUANTUM_US 10000 + +int next_pid = 1; + +void idle_task(){ + kprintf("Hello world from bruhd task!\n"); + for(;;); +} + +void test_task(){ + kprintf("Hello world from scheduled task!\n"); + return; +} + +/* Setup a process structure */ +proc *alloc_process(void){ + asm("cli"); + + cpu_state *state = get_cpu_struct(); + kprintf("hi10\n"); + proc *proc_list = state->process_list; + + uint8_t *sp; + + for(uint64_t i = 0; i < PROC_MAX; i++){ + if(proc_list[i].state == UNUSED){ + /* Set the process ready to be executed */ + kprintf("hi6\n"); + proc_list[i].state = READY; + proc_list[i].kstack = kmalloc(INITIAL_STACK_SIZE); + + if(proc_list[i].kstack == NULL){ + klog(LOG_ERROR, __func__, "Failed to alloc stack"); + } + kprintf("hi7\n"); + + proc_list[i].pid = next_pid++; + + sp = (uint8_t*)((uint64_t)proc_list[i].kstack + INITIAL_STACK_SIZE); + + proc_list[i].context.rip = 0; + proc_list[i].context.rsp = (uint64_t)sp; + + kprintf("hi8\n"); + + asm("sti"); + + return &proc_list[i]; + } + } + asm("sti"); + + return NULL; +} + +kstatus add_task(uint64_t *entry){ + proc *proc = alloc_process(); + + if (proc == NULL) { + klog(LOG_ERROR, __func__, "proc == null!"); + kkill(); + } + + proc->context.rip = (uint64_t)entry; + + return KERNEL_STATUS_SUCCESS; +} + +void scheduler_init(){ + cpu_state *state = get_cpu_struct(); + + if(state->current_process != NULL){ + kprintf("sched: scheduler on CPU {d} already initialized!\n", state->lapic_id); + kkill(); + } + kprintf("hi1\n"); + proc *proc_list = state->process_list; + + /* Put the idle task */ + proc idle = {0, 0, 0, READY, {0, 0, 0, 0, 0, 0, 0, 0, 0}}; + + /* Make the idle structure the firstr process */ + proc_list[0] = idle; + + kprintf("hi2\n"); + add_task((uint64_t*)test_task); + kprintf("hi5\n"); + + for(;;){ + for(int i = 0; i < PROC_MAX; i++){ + if(proc_list[i].state == READY){ + + context old_state = state->current_process->context; + + state->current_process = &proc_list[i]; + state->current_process->state = RUNNING; + + switch_context(&old_state, &state->current_process->context); + + } + } + } +} + +void scheduler_tick(){ + cpu_state *state = get_cpu_struct(); + proc *proc_list = state->process_list; + + + +} \ No newline at end of file diff --git a/src/scheduler/sched.h b/src/scheduler/sched.h new file mode 100644 index 0000000..4f70553 --- /dev/null +++ b/src/scheduler/sched.h @@ -0,0 +1,30 @@ +#include + +#pragma once + +typedef enum proc_state { + RUNNING, + READY, + SLEEPING, + UNUSED = 0 +}proc_state; + +typedef struct context { + uint64_t rbx, rsp, rbp, r12, r13, r14, r15; + uint64_t rip, rflags; +} __attribute((packed))context; + +typedef struct proc { + uint64_t *mem; + uint64_t *kstack; + proc_state state; + uint16_t pid; + context context; +}proc; + +void scheduler_init(); + +#define PROC_MAX 512 // Max number of processes + +#define INITIAL_STACK_SIZE 0x10000 + diff --git a/src/sys/acpi.c b/src/sys/acpi.c new file mode 100644 index 0000000..8e30d70 --- /dev/null +++ b/src/sys/acpi.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include "acpi.h" + +static volatile struct limine_rsdp_request rsdp_request = { + .id = LIMINE_RSDP_REQUEST, + .revision = 0, +}; + +extern uint64_t hhdmoffset; + +xsdt_t *xsdt; +rsdt_t *rsdt; +madt_t *madt; + +/* Returns pointer to the table with specified signature, if it doesnt find it then it returns NULL */ +uint64_t *find_acpi_table(char *signature){ + + uint64_t entries = 0; // stores the total number of entries in the table + + if(xsdt){ + /* The total number of entries is the length of the entire table minus the standard header from the xsdt and divided by 8, as the array is of 8 byte wide headers*/ + entries = (xsdt->header.length - sizeof(desc_header_t)) / 8; + }else{ + entries = (rsdt->header.length - sizeof(desc_header_t)) / 4; + } + + desc_header_t *header; + + for(uint64_t i = 0; i < entries; i++){ + if(xsdt){ + header = (desc_header_t*)(xsdt->entries_base[i]); + }else{ + header = (desc_header_t*)(rsdt->entries_base[i]); + } + + /* Get the virtual address of the header so we can access its signature */ + desc_header_t *virt = (desc_header_t*)((uint64_t)header + hhdmoffset); + + if(memcmp(virt->signature, signature, 4) == 0){ + return (uint64_t*)header; + } + } + + return NULL; + +} + +void acpi_init(void){ + + if(rsdp_request.response == NULL){ + klog(LOG_ERROR, "acpi", "RSDP request is NULL"); + kkill(); + } + + rsdp_t *rsdp = (rsdp_t*)(rsdp_request.response->address); + + kprintf("RSDP address: 0x{xn}", (uint64_t)(rsdp)); + + /* If the systems ACPI revision is higher/equal than 2, then use XSDT */ + if(rsdp->revision >= 2){ + rsdt = NULL; + xsdt = (xsdt_t*)(rsdp->xsdt_address + hhdmoffset); + klog(LOG_INFO, "acpi", "Using XSDT header"); + kprintf("XSDT address: 0x{xn}", (uint64_t)xsdt); + kprintf("OEMID: {ccccccn}", xsdt->header.oemid[0], xsdt->header.oemid[1], xsdt->header.oemid[2], xsdt->header.oemid[3], xsdt->header.oemid[4], xsdt->header.oemid[5]); + }else{ + xsdt = NULL; + rsdt = (rsdt_t*)(rsdp->rsdt_address + hhdmoffset); + klog(LOG_INFO, "acpi", "Using RSDT header"); + kprintf("RSDT address: 0x{xn}", (uint64_t)rsdt); + kprintf("OEMID: {ccccccn}", rsdt->header.oemid[0], rsdt->header.oemid[1], rsdt->header.oemid[2], rsdt->header.oemid[3], rsdt->header.oemid[4], rsdt->header.oemid[5]); + } + + madt = (madt_t*)find_acpi_table("APIC"); + + if(!madt){ + klog(LOG_ERROR, __func__, "MADT table not found"); + kkill(); + } +} + +uint64_t *find_ics(uint64_t type){ + uint64_t length = (madt->header.length - sizeof(desc_header_t) - 8); + uint64_t *base_addr = (uint64_t*)madt->ics; + + uint64_t i = 0; + while (i < length) { + ics_t *header = (ics_t*)((uint64_t)base_addr + i); + if(header->type == type){ + return (uint64_t*)header; + } + i += header->length; + } + + return NULL; +} + +uint32_t find_iso(uint8_t legacy){ + uint64_t length = (madt->header.length - sizeof(desc_header_t) - 8); + uint64_t *base_addr = (uint64_t*)madt->ics; + + uint64_t i = 0; + while (i < length) { + ics_t *header = (ics_t*)((uint64_t)base_addr + i); + if(header->type == 0x2){ + iso_t *iso = (iso_t*)header; + if(legacy == iso->source){ + return iso->gsi; + } + } + i += header->length; + } + + /* GSI is equal to legacy pin */ + return legacy; +} \ No newline at end of file diff --git a/src/sys/acpi.h b/src/sys/acpi.h new file mode 100644 index 0000000..02af6de --- /dev/null +++ b/src/sys/acpi.h @@ -0,0 +1,196 @@ +#include +#include +typedef struct rsdp_t { + uint64_t signature; + uint8_t checksum; + uint8_t oemid[6]; + uint8_t revision; + uint32_t rsdt_address; + + uint32_t length; + uint64_t xsdt_address; + uint8_t ext_checksum; + uint8_t reserved[3]; +} __attribute((packed)) rsdp_t; + +typedef struct desc_header_t { + uint8_t signature[4]; + uint32_t length; + uint8_t revision; + uint8_t checksum; + uint8_t oemid[6]; + uint8_t oem_tableid[8]; + uint32_t oem_revision; + uint32_t creator_id; + uint32_t creator_revision; +} __attribute((packed)) desc_header_t; + +typedef struct rsdt_t { + desc_header_t header; + uint32_t entries_base[]; +} __attribute((packed)) rsdt_t; + +typedef struct xsdt_t { + desc_header_t header; + uint64_t entries_base[]; +} __attribute((packed)) xsdt_t; + +typedef struct ics_t { + uint8_t type; + uint8_t length; +}__attribute((packed)) ics_t; + +typedef struct madt_t { + desc_header_t header; + uint32_t lic_address; + uint32_t flags; + ics_t ics[]; +} __attribute((packed)) madt_t; + +typedef struct lapic_ao_t { + ics_t ics; + uint16_t reserved; + uint64_t lapic_address; +}__attribute((packed)) lapic_ao_t; + +typedef struct gas_t { + uint8_t address_space_id; + uint8_t reg_bit_width; + uint8_t reg_bit_offset; + uint8_t access_size; + uint64_t address; +}__attribute((packed)) gas_t; + +typedef struct hpet_t { + desc_header_t header; + uint32_t event_timer_blkid; + gas_t base_address; + uint8_t hpet_number; + uint16_t minimum_clk_tick; + uint8_t oem_attribute; +}__attribute((packed)) hpet_t; + +typedef struct ioapic_t{ + ics_t ics; + uint8_t ioapic_id; + uint8_t reserved; + uint32_t ioapic_address; + uint32_t gsi_base; +}__attribute((packed)) ioapic_t; + +typedef struct iso_t{ + ics_t ics; + uint8_t bus; + uint8_t source; + uint32_t gsi; + uint16_t flags; +}__attribute((packed)) iso_t; + +/* Copied from OSDEV wiki */ +typedef struct fadt_t{ + desc_header_t header; + uint32_t FirmwareCtrl; + uint32_t Dsdt; + + // field used in ACPI 1.0; no longer in use, for compatibility only + uint8_t Reserved; + + uint8_t PreferredPowerManagementProfile; + uint16_t SCI_Interrupt; + uint32_t SMI_CommandPort; + uint8_t AcpiEnable; + uint8_t AcpiDisable; + uint8_t S4BIOS_REQ; + uint8_t PSTATE_Control; + uint32_t PM1aEventBlock; + uint32_t PM1bEventBlock; + uint32_t PM1aControlBlock; + uint32_t PM1bControlBlock; + uint32_t PM2ControlBlock; + uint32_t PMTimerBlock; + uint32_t GPE0Block; + uint32_t GPE1Block; + uint8_t PM1EventLength; + uint8_t PM1ControlLength; + uint8_t PM2ControlLength; + uint8_t PMTimerLength; + uint8_t GPE0Length; + uint8_t GPE1Length; + uint8_t GPE1Base; + uint8_t CStateControl; + uint16_t WorstC2Latency; + uint16_t WorstC3Latency; + uint16_t FlushSize; + uint16_t FlushStride; + uint8_t DutyOffset; + uint8_t DutyWidth; + uint8_t DayAlarm; + uint8_t MonthAlarm; + uint8_t Century; + + // reserved in ACPI 1.0; used since ACPI 2.0+ + uint16_t BootArchitectureFlags; + + uint8_t Reserved2; + uint32_t Flags; + + // 12 byte structure; see below for details + gas_t ResetReg; + + uint8_t ResetValue; + uint8_t Reserved3[3]; + + // 64bit pointers - Available on ACPI 2.0+ + uint64_t X_FirmwareControl; + uint64_t X_Dsdt; + + gas_t X_PM1aEventBlock; + gas_t X_PM1bEventBlock; + gas_t X_PM1aControlBlock; + gas_t X_PM1bControlBlock; + gas_t X_PM2ControlBlock; + gas_t X_PMTimerBlock; + gas_t X_GPE0Block; + gas_t X_GPE1Block; + + gas_t sleep_ctrl_reg; + gas_t sleep_status_reg; + + uint64_t hypervisor_vendor_id; + + uint8_t wbinvd; + uint8_t wbinvd_flush; + + uint8_t proc_c1; + uint8_t p_lvl2_up; + uint8_t pwr_button; + uint8_t slp_button; + uint8_t fix_rtc; + uint8_t rtc_s4; + uint8_t tmr_val_ext; + uint8_t dck_cap; + + +}__attribute((packed)) fadt_t; + +typedef struct conf_space_t { + uint64_t base_ecm; + uint16_t pci_seg_group; + uint8_t start_pci_num; + uint8_t end_pci_num; + uint32_t reserved; +}__attribute((packed)) conf_space_t; + +typedef struct mcfg_t { + desc_header_t header; + uint64_t reserved; + conf_space_t conf_spaces[]; +}__attribute((packed)) mcfg_t; + +void acpi_init(void); +uint64_t *find_acpi_table(char *signature); +uint64_t *find_ics(uint64_t type); +uint32_t find_iso(uint8_t legacy); + + + diff --git a/src/sys/pci.c b/src/sys/pci.c new file mode 100644 index 0000000..7b0486e --- /dev/null +++ b/src/sys/pci.c @@ -0,0 +1,251 @@ +#include +#include +#include +#include +#include "acpi.h" +#include "../mm/vmm.h" +#include "error.h" +#include "pci.h" + +#define PCIE_CONF_SPACE_WIDTH 4096 +#define PCI_CONF_SPACE_WIDTH 256 +#define PCI_BUS_MAX_DEVICES 32 + +#define PCI_HEADER_TYPE_GENERAL 0x0 // General device +#define PCI_HEADER_TYPE_PCI2PCI 0x1 // PCI to PCI bridge +#define PCI_HEADER_TYPE_PCI2CB 0x2 // PCI to CardBus bridge +#define PCI_HEADER_TYPE_MULTI (1 << 7) // Multifunction device + +#define PCI_FUNCTION_MAX 8 // There are always a max of 8 functions per device + +#define PCI_DEVICE_BUS 8192 // Max number of devices in a + +mcfg_t *mcfg; + +uint64_t config_space_base_addr = 0; +uint8_t start_pci_num = 0; +uint8_t end_pci_num = 0; + +pci_structure *pci_array; + +extern uint64_t hhdmoffset; +extern uint64_t *kernel_page_map; + +atomic_flag pci_array_lock = ATOMIC_FLAG_INIT; + +void pci_add_device(pci_structure structure){ + acquire_lock(&pci_array_lock); + int i = 0; + + /* Find first unused space */ + while(pci_array[i].func_addr[0] != 0){ + if(i >= PCI_DEVICE_BUS){ + klog(LOG_ERROR, __func__, "No more space in the PCI array!"); + kkill(); + } + i++; + } + + pci_array[i].segment = structure.segment; + pci_array[i].bus = structure.bus; + pci_array[i].device = structure.device; + + for(int j = 0; j < PCI_FUNCTION_MAX; j++){ + if(structure.func_addr[j] != 0){ + pci_array[i].func_addr[j] = structure.func_addr[j]; + } + } + + free_lock(&pci_array_lock); + return; +} + +pci_structure *pci_get_device(pci_structure structure){ + acquire_lock(&pci_array_lock); + pci_structure ret = {0}; + + for(int i = 0; i < PCI_DEVICE_BUS; i++){ + if( structure.segment == pci_array[i].segment && + structure.bus == pci_array[i].bus && + structure.device == pci_array[i].device){ + return &pci_array[i]; + free_lock(&pci_array_lock); + } + } + + free_lock(&pci_array_lock); + return NULL; +} + +void parse_conf_space(){ + uint64_t num = (mcfg->header.length - sizeof(desc_header_t)) / sizeof(conf_space_t); + + kprintf("addr: 0x{xn}", (uint64_t)mcfg); + kprintf("header length: {dn}", mcfg->header.length); + kprintf("num: {dn}", num); + + for(uint64_t i = 0; i < num; i++){ + conf_space_t header = mcfg->conf_spaces[i]; + + kprintf("header {dn}", i); + kprintf(" base addr: 0x{xn}", header.base_ecm); + kprintf(" PCI segment group: {dn}", header.pci_seg_group); + kprintf(" start pci bus number: 0x{xn}", header.start_pci_num); + kprintf(" end pci bus number: 0x{xn}", header.end_pci_num); + + config_space_base_addr = header.base_ecm + hhdmoffset; + start_pci_num = header.start_pci_num; + end_pci_num = header.end_pci_num; + } +} + +void enumerate_conf_space(){ + l84_pci_function_return ret; + + for(uint64_t i = 0; i < end_pci_num; i++){ + for(uint64_t j = 0; j < 32; j++){ + ret = check_device(i, j); + + if(ret.func_addr[0] != 0){ + pci_structure structure = {.segment = 1, .bus = i, .device = j}; + memcpy(structure.func_addr, ret.func_addr, PCI_FUNCTION_MAX * sizeof(uint64_t)); + pci_add_device(structure); + } + + } + } +} + +/* pci_header_t *pci_find_device(uint64_t class, int subclass) + + - Parameters: + class - class code + subclass - subclass. if set to -1 then subclass is ignored + + - Return: + Returns pointer to the requested PCI header. Returns NULL if not found + + - TODO: + Handle returning multiple functions + + */ + +pci_header_t *pci_find_device(uint64_t class, int subclass){ + l84_pci_function_return pci_function_return; + + for(uint64_t i = 0; i < end_pci_num; i++){ + for(uint64_t j = 0; j < 32; j++){ + + pci_function_return = check_device(i, j); + + if(pci_function_return.func_addr[0] == 0){ + continue; + } + + /* Get the address of the first function */ + pci_header_t *header = (pci_header_t*)pci_function_return.func_addr[0]; + + + if(header->class_code == class){ + + /* If no subclass is wanted, then just return the header */ + if(subclass == -1){ + return header; + } + + if(subclass == header->subclass){ + return header; + } + + } + + } + } + + /* Found nothing, return null */ + return NULL; +} + +void pci_init(){ + mcfg = (mcfg_t*)find_acpi_table("MCFG"); + + if(!mcfg){ + klog(LOG_ERROR, __func__, "Failed to find MCFG table"); + kprintf("pci: device: PCIe not supported! Halting"); + kkill(); + } + + mcfg = (mcfg_t*)((uint64_t)mcfg + hhdmoffset); + + parse_conf_space(); + + /* Map the config space */ + kernel_map_pages((uint64_t*)(config_space_base_addr - hhdmoffset), (PCIE_CONF_SPACE_WIDTH * end_pci_num) / PAGE_SIZE, PTE_BIT_RW | PTE_BIT_NX); + + /* Stores enough for an entire configuration space */ + pci_array = kmalloc((256 * 32) * sizeof(pci_structure)); + + if(!pci_array){ + klog(LOG_ERROR, __func__, "Failed to allocate memory for PCI structures!"); + kkill(); + } + + enumerate_conf_space(); + +} + +l84_pci_function_return check_device(uint64_t bus, uint64_t device){ + + l84_pci_function_return ret = {0}; + + pci_header_t *header = (pci_header_t*)get_header(bus, device, 0); + + vmm_map_page(kernel_page_map, (uint64_t)header, (uint64_t)header - hhdmoffset, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + + /* If vendor id is 0xffff, that means that this device is unimplemented */ + if(header->vendor_id == 0xffff){ + return (l84_pci_function_return){ 0, {0} }; + } + + if((header->header_type & PCI_HEADER_TYPE_MULTI) != 0){ + /* Mask the 7th bit (multi-function bit) so we get the header type */ + uint64_t multi_header_type = header->header_type & 0x3f; + + ret.multi = true; + + for(uint64_t function = 0; function < PCI_FUNCTION_MAX; function++){ + uint64_t *addr = (uint64_t*)(config_space_base_addr + ((bus) << 20 | device << 15 | function << 12)); + pci_header_t *func = (pci_header_t*)((uint64_t)addr); + + vmm_map_page(kernel_page_map, (uint64_t)func, (uint64_t)func - hhdmoffset, PTE_BIT_PRESENT | PTE_BIT_RW | PTE_BIT_NX); + + if(func->vendor_id != 0xffff){ + //kprintf("pci multi: bus: 0x{x} device: 0x{x} type: 0x{x} class: 0x{x} subclass: 0x{x} vendorid: 0x{xn}", bus, func->device_id, multi_header_type, func->class_code, func->subclass, func->vendor_id); + //serial_kprintf("pci multi: bus: 0x{x} device: 0x{x} type: 0x{x} class: 0x{x} subclass: 0x{x} vendorid: 0x{xn}", bus, func->device_id, multi_header_type, func->class_code, func->subclass, func->vendor_id); + ret.func_addr[function] = (uint64_t)func; + } + } + } + + ret.multi = false; + ret.func_addr[0] = (uint64_t)header; + + //kprintf("pci: bus: 0x{x} device: 0x{x} type: 0x{x} class: 0x{x} subclass: 0x{x} vendorid: 0x{xn}", bus, header->device_id, header->header_type, header->class_code, header->subclass, header->vendor_id); + //serial_kprintf("pci: bus: 0x{x} device: 0x{x} type: 0x{x} class: 0x{x} subclass: 0x{x} vendorid: 0x{xn}", bus, header->header_type, header->class_code, header->subclass, header->vendor_id); + + return ret; +} + +void check_bus(uint64_t bus){ + + for(uint64_t i = 0; i < PCI_BUS_MAX_DEVICES; i++){ + check_device(bus, i); + + } + +} + +uint64_t get_header(uint64_t bus, uint64_t device, uint64_t function){ + return config_space_base_addr + ((bus * 256) + (device * 8) + function) * PCIE_CONF_SPACE_WIDTH; +} + diff --git a/src/sys/pci.h b/src/sys/pci.h new file mode 100644 index 0000000..ca56b32 --- /dev/null +++ b/src/sys/pci.h @@ -0,0 +1,102 @@ +#include +#include +void pci_init(); + +typedef struct pci_header_t { + uint16_t vendor_id; + uint16_t device_id; + uint16_t command; + uint16_t status; + uint8_t revision_id; + uint8_t prog_if; + uint8_t subclass; + uint8_t class_code; + uint8_t cache_line_size; + uint8_t latency_timer; + uint8_t header_type; + uint8_t bist; +}__attribute((packed)) pci_header_t; + +typedef struct pci_header_0_t { + pci_header_t header; + uint32_t bar0; + uint32_t bar1; + uint32_t bar2; + uint32_t bar3; + uint32_t bar4; + uint32_t bar5; + uint32_t cardbus_cis_ptr; + uint16_t subsytem_vendor_id; + uint16_t subsystem_id; + uint32_t expansion_rom_base; + uint8_t capabilities_ptr; + uint8_t reserved1; + uint16_t reserved2; + uint32_t reserved3; + uint8_t interrupt_line; + uint8_t interrupt_pin; + uint8_t min_grant; + uint8_t max_latency; +}__attribute((packed)) pci_header_0_t; + +typedef struct pci_header_1_t { + pci_header_t header; + uint32_t bar0; + uint32_t bar1; + uint8_t primary_bus_number; + uint8_t secondary_bus_number; + uint8_t subordinate_bus_number; + uint8_t secondary_latency_timer; + uint8_t io_base; + uint8_t io_limit; + uint16_t secondary_status; + uint16_t memory_base; + uint16_t memory_limit; + uint16_t prefetch_base_; + uint16_t prefetch_limit; + uint32_t prefetch_base_upper; + uint32_t prefetch_limit_upper; + uint16_t io_base_upper; + uint16_t io_limit_upper; + uint8_t capability_ptr; + uint8_t reserved1; + uint16_t reserved2; + uint32_t expansion_rom_base; + uint8_t interrupt_line; + uint8_t interrupt_pin; + uint16_t bridge_control; +}__attribute((packed)) pci_header_1_t; + +typedef struct pci_header_ahci_t { + pci_header_t header; + uint32_t bar[4]; + uint32_t ahci_bar; + uint16_t subsystem_id; + uint16_t subsytem_vendor_id; + uint32_t expansion_rom_base; + uint8_t capabilities_ptr; + uint16_t interrupt_info; + uint8_t min_grant; + uint8_t max_latency; + +}__attribute((packed)) pci_header_ahci_t; + +/* For internal use */ +typedef struct l84_pci_function_return { + bool multi; // If device has multiple functions this is set to 1, else set to 0. If set to 0, functions index 1-7 are ignored + uint64_t func_addr[8]; +} l84_pci_function_return; + +typedef struct pci_structure { + uint16_t segment; + uint8_t bus; + uint8_t device; + uint64_t func_addr[8]; +} pci_structure; + +l84_pci_function_return check_device(uint64_t bus, uint64_t device); + +uint64_t get_header(uint64_t bus, uint64_t device, uint64_t function); + +pci_header_t *pci_find_device(uint64_t class, int subclass); + diff --git a/src/sys/time.c b/src/sys/time.c new file mode 100644 index 0000000..92ba11e --- /dev/null +++ b/src/sys/time.c @@ -0,0 +1,16 @@ +/* Time keeping */ + +#include +#include "../hal/tsc.h" +uint64_t kernel_get_timestamp(){ + uint64_t ret = 0; + uint64_t tsc = tsc_get_timestamp(); + + if(tsc == 0){ + /* Get APIC timestamp */ + + } + + return tsc; + +} \ No newline at end of file diff --git a/src/uacpi/default_handlers.c b/src/uacpi/default_handlers.c new file mode 100644 index 0000000..5bdb77a --- /dev/null +++ b/src/uacpi/default_handlers.c @@ -0,0 +1,353 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define PCI_ROOT_PNP_ID "PNP0A03" +#define PCI_EXPRESS_ROOT_PNP_ID "PNP0A08" + +static uacpi_namespace_node *find_pci_root(uacpi_namespace_node *node) +{ + static const uacpi_char *pci_root_ids[] = { + PCI_ROOT_PNP_ID, + PCI_EXPRESS_ROOT_PNP_ID, + UACPI_NULL + }; + uacpi_namespace_node *parent = node->parent; + + while (parent != uacpi_namespace_root()) { + if (uacpi_device_matches_pnp_id(parent, pci_root_ids)) { + uacpi_trace( + "found a PCI root node %.4s controlling region %.4s\n", + parent->name.text, node->name.text + ); + return parent; + } + + parent = parent->parent; + } + + uacpi_trace_region_error( + node, "unable to find PCI root controlling", + UACPI_STATUS_NOT_FOUND + ); + return node; +} + +static uacpi_status pci_region_attach(uacpi_region_attach_data *data) +{ + uacpi_namespace_node *node, *pci_root, *device; + uacpi_pci_address address = { 0 }; + uacpi_u64 value; + uacpi_status ret; + + node = data->region_node; + pci_root = find_pci_root(node); + + /* + * Find the actual device object that is supposed to be controlling + * this operation region. + */ + device = node; + while (device) { + uacpi_object_type type; + + ret = uacpi_namespace_node_type(device, &type); + if (uacpi_unlikely_error(ret)) + return ret; + + if (type == UACPI_OBJECT_DEVICE) + break; + + device = device->parent; + } + + if (uacpi_unlikely(device == UACPI_NULL)) { + ret = UACPI_STATUS_NOT_FOUND; + uacpi_trace_region_error( + node, "unable to find device responsible for", ret + ); + return ret; + } + + ret = uacpi_eval_simple_integer(device, "_ADR", &value); + if (ret == UACPI_STATUS_OK) { + address.function = (value >> 0) & 0xFF; + address.device = (value >> 16) & 0xFF; + } + + ret = uacpi_eval_simple_integer(pci_root, "_SEG", &value); + if (ret == UACPI_STATUS_OK) + address.segment = value; + + ret = uacpi_eval_simple_integer(pci_root, "_BBN", &value); + if (ret == UACPI_STATUS_OK) + address.bus = value; + + uacpi_trace( + "detected PCI device %.4s@%04X:%02X:%02X:%01X\n", + device->name.text, address.segment, address.bus, + address.device, address.function + ); + + return uacpi_kernel_pci_device_open(address, &data->out_region_context); +} + +static uacpi_status pci_region_detach(uacpi_region_detach_data *data) +{ + uacpi_kernel_pci_device_close(data->region_context); + return UACPI_STATUS_OK; +} + +static uacpi_status pci_region_do_rw( + uacpi_region_op op, uacpi_region_rw_data *data +) +{ + uacpi_handle dev = data->region_context; + uacpi_u8 width; + uacpi_size offset; + + offset = data->offset; + width = data->byte_width; + + return op == UACPI_REGION_OP_READ ? + uacpi_kernel_pci_read(dev, offset, width, &data->value) : + uacpi_kernel_pci_write(dev, offset, width, data->value); +} + +static uacpi_status handle_pci_region(uacpi_region_op op, uacpi_handle op_data) +{ + switch (op) { + case UACPI_REGION_OP_ATTACH: + return pci_region_attach(op_data); + case UACPI_REGION_OP_DETACH: + return pci_region_detach(op_data); + case UACPI_REGION_OP_READ: + case UACPI_REGION_OP_WRITE: + return pci_region_do_rw(op, op_data); + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } +} + +struct memory_region_ctx { + uacpi_phys_addr phys; + uacpi_u8 *virt; + uacpi_size size; +}; + +static uacpi_status memory_region_attach(uacpi_region_attach_data *data) +{ + struct memory_region_ctx *ctx; + uacpi_object *region_obj; + uacpi_operation_region *op_region; + uacpi_status ret; + + ctx = uacpi_kernel_alloc(sizeof(*ctx)); + if (ctx == UACPI_NULL) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = uacpi_namespace_node_acquire_object_typed( + data->region_node, UACPI_OBJECT_OPERATION_REGION_BIT, ®ion_obj + ); + if (uacpi_unlikely_error(ret)) + return ret; + + op_region = region_obj->op_region; + ctx->size = op_region->length; + + // FIXME: this really shouldn't try to map everything at once + ctx->phys = op_region->offset; + ctx->virt = uacpi_kernel_map(ctx->phys, ctx->size); + + if (uacpi_unlikely(ctx->virt == UACPI_NULL)) { + ret = UACPI_STATUS_MAPPING_FAILED; + uacpi_trace_region_error(data->region_node, "unable to map", ret); + uacpi_free(ctx, sizeof(*ctx)); + goto out; + } + + data->out_region_context = ctx; +out: + uacpi_namespace_node_release_object(region_obj); + return ret; +} + +static uacpi_status memory_region_detach(uacpi_region_detach_data *data) +{ + struct memory_region_ctx *ctx = data->region_context; + + uacpi_kernel_unmap(ctx->virt, ctx->size); + uacpi_free(ctx, sizeof(*ctx)); + return UACPI_STATUS_OK; +} + +struct io_region_ctx { + uacpi_io_addr base; + uacpi_handle handle; +}; + +static uacpi_status io_region_attach(uacpi_region_attach_data *data) +{ + struct io_region_ctx *ctx; + uacpi_object *region_obj; + uacpi_operation_region *op_region; + uacpi_status ret; + + ctx = uacpi_kernel_alloc(sizeof(*ctx)); + if (ctx == UACPI_NULL) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = uacpi_namespace_node_acquire_object_typed( + data->region_node, UACPI_OBJECT_OPERATION_REGION_BIT, ®ion_obj + ); + if (uacpi_unlikely_error(ret)) + return ret; + + op_region = region_obj->op_region; + ctx->base = op_region->offset; + + ret = uacpi_kernel_io_map(ctx->base, op_region->length, &ctx->handle); + if (uacpi_unlikely_error(ret)) { + uacpi_trace_region_error( + data->region_node, "unable to map an IO", ret + ); + uacpi_free(ctx, sizeof(*ctx)); + goto out; + } + + data->out_region_context = ctx; + +out: + uacpi_object_unref(region_obj); + return ret; +} + +static uacpi_status io_region_detach(uacpi_region_detach_data *data) +{ + struct io_region_ctx *ctx = data->region_context; + + uacpi_kernel_io_unmap(ctx->handle); + uacpi_free(ctx, sizeof(*ctx)); + return UACPI_STATUS_OK; +} + +static uacpi_status memory_region_do_rw( + uacpi_region_op op, uacpi_region_rw_data *data +) +{ + struct memory_region_ctx *ctx = data->region_context; + uacpi_u8 *ptr; + + ptr = ctx->virt + (data->address - ctx->phys); + + return op == UACPI_REGION_OP_READ ? + uacpi_system_memory_read(ptr, data->byte_width, &data->value) : + uacpi_system_memory_write(ptr, data->byte_width, data->value); +} + +static uacpi_status handle_memory_region(uacpi_region_op op, uacpi_handle op_data) +{ + switch (op) { + case UACPI_REGION_OP_ATTACH: + return memory_region_attach(op_data); + case UACPI_REGION_OP_DETACH: + return memory_region_detach(op_data); + case UACPI_REGION_OP_READ: + case UACPI_REGION_OP_WRITE: + return memory_region_do_rw(op, op_data); + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } +} + +static uacpi_status table_data_region_do_rw( + uacpi_region_op op, uacpi_region_rw_data *data +) +{ + void *addr = UACPI_VIRT_ADDR_TO_PTR((uacpi_virt_addr)data->offset); + + return op == UACPI_REGION_OP_READ ? + uacpi_system_memory_read(addr, data->byte_width, &data->value) : + uacpi_system_memory_write(addr, data->byte_width, data->value); +} + +static uacpi_status handle_table_data_region(uacpi_region_op op, uacpi_handle op_data) +{ + switch (op) { + case UACPI_REGION_OP_ATTACH: + case UACPI_REGION_OP_DETACH: + return UACPI_STATUS_OK; + case UACPI_REGION_OP_READ: + case UACPI_REGION_OP_WRITE: + return table_data_region_do_rw(op, op_data); + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } +} + +static uacpi_status io_region_do_rw( + uacpi_region_op op, uacpi_region_rw_data *data +) +{ + struct io_region_ctx *ctx = data->region_context; + uacpi_u8 width; + uacpi_size offset; + + offset = data->offset - ctx->base; + width = data->byte_width; + + return op == UACPI_REGION_OP_READ ? + uacpi_kernel_io_read(ctx->handle, offset, width, &data->value) : + uacpi_kernel_io_write(ctx->handle, offset, width, data->value); +} + +static uacpi_status handle_io_region(uacpi_region_op op, uacpi_handle op_data) +{ + switch (op) { + case UACPI_REGION_OP_ATTACH: + return io_region_attach(op_data); + case UACPI_REGION_OP_DETACH: + return io_region_detach(op_data); + case UACPI_REGION_OP_READ: + case UACPI_REGION_OP_WRITE: + return io_region_do_rw(op, op_data); + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } +} + +void uacpi_install_default_address_space_handlers(void) +{ + uacpi_namespace_node *root; + + root = uacpi_namespace_root(); + + uacpi_install_address_space_handler_with_flags( + root, UACPI_ADDRESS_SPACE_SYSTEM_MEMORY, + handle_memory_region, UACPI_NULL, + UACPI_ADDRESS_SPACE_HANDLER_DEFAULT + ); + + uacpi_install_address_space_handler_with_flags( + root, UACPI_ADDRESS_SPACE_SYSTEM_IO, + handle_io_region, UACPI_NULL, + UACPI_ADDRESS_SPACE_HANDLER_DEFAULT + ); + + uacpi_install_address_space_handler_with_flags( + root, UACPI_ADDRESS_SPACE_PCI_CONFIG, + handle_pci_region, UACPI_NULL, + UACPI_ADDRESS_SPACE_HANDLER_DEFAULT + ); + + uacpi_install_address_space_handler_with_flags( + root, UACPI_ADDRESS_SPACE_TABLE_DATA, + handle_table_data_region, UACPI_NULL, + UACPI_ADDRESS_SPACE_HANDLER_DEFAULT + ); +} diff --git a/src/uacpi/event.c b/src/uacpi/event.c new file mode 100644 index 0000000..7e655b0 --- /dev/null +++ b/src/uacpi/event.c @@ -0,0 +1,2424 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define UACPI_EVENT_DISABLED 0 +#define UACPI_EVENT_ENABLED 1 + +#ifndef UACPI_REDUCED_HARDWARE + +static uacpi_handle g_gpe_state_slock; +static struct uacpi_recursive_lock g_event_lock; +static uacpi_bool g_gpes_finalized; + +struct fixed_event { + uacpi_u8 enable_field; + uacpi_u8 status_field; + uacpi_u16 enable_mask; + uacpi_u16 status_mask; +}; + +struct fixed_event_handler { + uacpi_interrupt_handler handler; + uacpi_handle ctx; +}; + +static const struct fixed_event fixed_events[UACPI_FIXED_EVENT_MAX + 1] = { + [UACPI_FIXED_EVENT_GLOBAL_LOCK] = { + .status_field = UACPI_REGISTER_FIELD_GBL_STS, + .enable_field = UACPI_REGISTER_FIELD_GBL_EN, + .enable_mask = ACPI_PM1_EN_GBL_EN_MASK, + .status_mask = ACPI_PM1_STS_GBL_STS_MASK, + }, + [UACPI_FIXED_EVENT_TIMER_STATUS] = { + .status_field = UACPI_REGISTER_FIELD_TMR_STS, + .enable_field = UACPI_REGISTER_FIELD_TMR_EN, + .enable_mask = ACPI_PM1_EN_TMR_EN_MASK, + .status_mask = ACPI_PM1_STS_TMR_STS_MASK, + }, + [UACPI_FIXED_EVENT_POWER_BUTTON] = { + .status_field = UACPI_REGISTER_FIELD_PWRBTN_STS, + .enable_field = UACPI_REGISTER_FIELD_PWRBTN_EN, + .enable_mask = ACPI_PM1_EN_PWRBTN_EN_MASK, + .status_mask = ACPI_PM1_STS_PWRBTN_STS_MASK, + }, + [UACPI_FIXED_EVENT_SLEEP_BUTTON] = { + .status_field = UACPI_REGISTER_FIELD_SLPBTN_STS, + .enable_field = UACPI_REGISTER_FIELD_SLPBTN_EN, + .enable_mask = ACPI_PM1_EN_SLPBTN_EN_MASK, + .status_mask = ACPI_PM1_STS_SLPBTN_STS_MASK, + }, + [UACPI_FIXED_EVENT_RTC] = { + .status_field = UACPI_REGISTER_FIELD_RTC_STS, + .enable_field = UACPI_REGISTER_FIELD_RTC_EN, + .enable_mask = ACPI_PM1_EN_RTC_EN_MASK, + .status_mask = ACPI_PM1_STS_RTC_STS_MASK, + }, +}; + +static struct fixed_event_handler +fixed_event_handlers[UACPI_FIXED_EVENT_MAX + 1]; + +static uacpi_status initialize_fixed_events(void) +{ + uacpi_size i; + + for (i = 0; i < UACPI_FIXED_EVENT_MAX; ++i) { + uacpi_write_register_field( + fixed_events[i].enable_field, UACPI_EVENT_DISABLED + ); + } + + return UACPI_STATUS_OK; +} + +static uacpi_status set_event(uacpi_u8 event, uacpi_u8 value) +{ + uacpi_status ret; + uacpi_u64 raw_value; + const struct fixed_event *ev = &fixed_events[event]; + + ret = uacpi_write_register_field(ev->enable_field, value); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_read_register_field(ev->enable_field, &raw_value); + if (uacpi_unlikely_error(ret)) + return ret; + + if (raw_value != value) { + uacpi_error("failed to %sable fixed event %d\n", + value ? "en" : "dis", event); + return UACPI_STATUS_HARDWARE_TIMEOUT; + } + + uacpi_trace("fixed event %d %sabled successfully\n", + event, value ? "en" : "dis"); + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_enable_fixed_event(uacpi_fixed_event event) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(event < 0 || event > UACPI_FIXED_EVENT_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + /* + * Attempting to enable an event that doesn't have a handler is most likely + * an error, don't allow it. + */ + if (uacpi_unlikely(fixed_event_handlers[event].handler == UACPI_NULL)) { + ret = UACPI_STATUS_NO_HANDLER; + goto out; + } + + ret = set_event(event, UACPI_EVENT_ENABLED); + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_disable_fixed_event(uacpi_fixed_event event) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(event < 0 || event > UACPI_FIXED_EVENT_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = set_event(event, UACPI_EVENT_DISABLED); + + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_clear_fixed_event(uacpi_fixed_event event) +{ + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(event < 0 || event > UACPI_FIXED_EVENT_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + return uacpi_write_register_field( + fixed_events[event].status_field, ACPI_PM1_STS_CLEAR + ); +} + +static uacpi_interrupt_ret dispatch_fixed_event( + const struct fixed_event *ev, uacpi_fixed_event event +) +{ + uacpi_status ret; + struct fixed_event_handler *evh = &fixed_event_handlers[event]; + + ret = uacpi_write_register_field(ev->status_field, ACPI_PM1_STS_CLEAR); + if (uacpi_unlikely_error(ret)) + return UACPI_INTERRUPT_NOT_HANDLED; + + if (uacpi_unlikely_error(evh->handler == UACPI_NULL)) { + uacpi_warn( + "fixed event %d fired but no handler installed, disabling...\n", + event + ); + uacpi_write_register_field(ev->enable_field, UACPI_EVENT_DISABLED); + return UACPI_INTERRUPT_NOT_HANDLED; + } + + return evh->handler(evh->ctx); +} + +static uacpi_interrupt_ret handle_fixed_events(void) +{ + uacpi_interrupt_ret int_ret = UACPI_INTERRUPT_NOT_HANDLED; + uacpi_status ret; + uacpi_u64 enable_mask, status_mask; + uacpi_size i; + + ret = uacpi_read_register(UACPI_REGISTER_PM1_STS, &status_mask); + if (uacpi_unlikely_error(ret)) + return int_ret; + + ret = uacpi_read_register(UACPI_REGISTER_PM1_EN, &enable_mask); + if (uacpi_unlikely_error(ret)) + return int_ret; + + for (i = 0; i < UACPI_FIXED_EVENT_MAX; ++i) + { + const struct fixed_event *ev = &fixed_events[i]; + + if (!(status_mask & ev->status_mask) || + !(enable_mask & ev->enable_mask)) + continue; + + int_ret |= dispatch_fixed_event(ev, i); + } + + return int_ret; +} + +struct gpe_native_handler { + uacpi_gpe_handler cb; + uacpi_handle ctx; + + /* + * Preserved values to be used for state restoration if this handler is + * removed at any point. + */ + uacpi_handle previous_handler; + uacpi_u8 previous_triggering : 1; + uacpi_u8 previous_handler_type : 3; + uacpi_u8 previously_enabled : 1; +}; + +struct gpe_implicit_notify_handler { + struct gpe_implicit_notify_handler *next; + uacpi_namespace_node *device; +}; + +#define EVENTS_PER_GPE_REGISTER 8 + +/* + * NOTE: + * This API and handler types are inspired by ACPICA, let's not reinvent the + * wheel and follow a similar path that people ended up finding useful after + * years of dealing with ACPI. Obviously credit goes to them for inventing + * "implicit notify" and other neat API. + */ +enum gpe_handler_type { + GPE_HANDLER_TYPE_NONE = 0, + GPE_HANDLER_TYPE_AML_HANDLER = 1, + GPE_HANDLER_TYPE_NATIVE_HANDLER = 2, + GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW = 3, + GPE_HANDLER_TYPE_IMPLICIT_NOTIFY = 4, +}; + +struct gp_event { + union { + struct gpe_native_handler *native_handler; + struct gpe_implicit_notify_handler *implicit_handler; + uacpi_namespace_node *aml_handler; + uacpi_handle *any_handler; + }; + + struct gpe_register *reg; + uacpi_u16 idx; + + // "reference count" of the number of times this event has been enabled + uacpi_u8 num_users; + + uacpi_u8 handler_type : 3; + uacpi_u8 triggering : 1; + uacpi_u8 wake : 1; + uacpi_u8 block_interrupts : 1; +}; + +struct gpe_register { + struct acpi_gas status; + struct acpi_gas enable; + + uacpi_u8 runtime_mask; + uacpi_u8 wake_mask; + uacpi_u8 masked_mask; + uacpi_u8 current_mask; + + uacpi_u16 base_idx; +}; + +struct gpe_block { + struct gpe_block *prev, *next; + + /* + * Technically this can only refer to \_GPE, but there's also apparently a + * "GPE Block Device" with id "ACPI0006", which is not used by anyone. We + * still keep it as a possibility that someone might eventually use it, so + * it is supported here. + */ + uacpi_namespace_node *device_node; + + struct gpe_register *registers; + struct gp_event *events; + struct gpe_interrupt_ctx *irq_ctx; + + uacpi_u16 num_registers; + uacpi_u16 num_events; + uacpi_u16 base_idx; +}; + +struct gpe_interrupt_ctx { + struct gpe_interrupt_ctx *prev, *next; + + struct gpe_block *gpe_head; + uacpi_handle irq_handle; + uacpi_u32 irq; +}; +static struct gpe_interrupt_ctx *g_gpe_interrupt_head; + +static uacpi_u8 gpe_get_mask(struct gp_event *event) +{ + return 1 << (event->idx - event->reg->base_idx); +} + +enum gpe_state { + GPE_STATE_ENABLED, + GPE_STATE_ENABLED_CONDITIONALLY, + GPE_STATE_DISABLED, +}; + +static uacpi_status set_gpe_state(struct gp_event *event, enum gpe_state state) +{ + uacpi_status ret; + struct gpe_register *reg = event->reg; + uacpi_u64 enable_mask; + uacpi_u8 event_bit; + uacpi_cpu_flags flags; + + event_bit = gpe_get_mask(event); + if (state != GPE_STATE_DISABLED && (reg->masked_mask & event_bit)) + return UACPI_STATUS_OK; + + if (state == GPE_STATE_ENABLED_CONDITIONALLY) { + if (!(reg->current_mask & event_bit)) + return UACPI_STATUS_OK; + + state = GPE_STATE_ENABLED; + } + + flags = uacpi_kernel_lock_spinlock(g_gpe_state_slock); + + ret = uacpi_gas_read(®->enable, &enable_mask); + if (uacpi_unlikely_error(ret)) + goto out; + + switch (state) { + case GPE_STATE_ENABLED: + enable_mask |= event_bit; + break; + case GPE_STATE_DISABLED: + enable_mask &= ~event_bit; + break; + default: + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + ret = uacpi_gas_write(®->enable, enable_mask); +out: + uacpi_kernel_unlock_spinlock(g_gpe_state_slock, flags); + return ret; +} + +static uacpi_status clear_gpe(struct gp_event *event) +{ + struct gpe_register *reg = event->reg; + + return uacpi_gas_write(®->status, gpe_get_mask(event)); +} + +static uacpi_status restore_gpe(struct gp_event *event) +{ + uacpi_status ret; + + if (event->triggering == UACPI_GPE_TRIGGERING_LEVEL) { + ret = clear_gpe(event); + if (uacpi_unlikely_error(ret)) + return ret; + } + + ret = set_gpe_state(event, GPE_STATE_ENABLED_CONDITIONALLY); + event->block_interrupts = UACPI_FALSE; + + return ret; +} + +static void async_restore_gpe(uacpi_handle opaque) +{ + uacpi_status ret; + struct gp_event *event = opaque; + + ret = restore_gpe(event); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to restore GPE(%02X): %s\n", + event->idx, uacpi_status_to_string(ret)); + } +} + +static void async_run_gpe_handler(uacpi_handle opaque) +{ + uacpi_status ret; + struct gp_event *event = opaque; + + ret = uacpi_namespace_write_lock(); + if (uacpi_unlikely_error(ret)) + goto out_no_unlock; + + switch (event->handler_type) { + case GPE_HANDLER_TYPE_AML_HANDLER: { + uacpi_object *method_obj; + + method_obj = uacpi_namespace_node_get_object_typed( + event->aml_handler, UACPI_OBJECT_METHOD_BIT + ); + if (uacpi_unlikely(method_obj == UACPI_NULL)) { + uacpi_error("GPE(%02X) AML handler gone\n", event->idx); + break; + } + + uacpi_trace( + "executing GPE(%02X) handler %.4s\n", + event->idx, uacpi_namespace_node_name(event->aml_handler).text + ); + + ret = uacpi_execute_control_method( + event->aml_handler, method_obj->method, UACPI_NULL, UACPI_NULL + ); + if (uacpi_unlikely_error(ret)) { + uacpi_error( + "error while executing GPE(%02X) handler %.4s: %s\n", + event->idx, event->aml_handler->name.text, + uacpi_status_to_string(ret) + ); + } + break; + } + + case GPE_HANDLER_TYPE_IMPLICIT_NOTIFY: { + struct gpe_implicit_notify_handler *handler; + + handler = event->implicit_handler; + while (handler) { + /* + * 2 - Device Wake. Used to notify OSPM that the device has signaled + * its wake event, and that OSPM needs to notify OSPM native device + * driver for the device. + */ + uacpi_notify_all(handler->device, 2); + handler = handler->next; + } + break; + } + + default: + break; + } + + uacpi_namespace_write_unlock(); + +out_no_unlock: + /* + * We schedule the work as NOTIFICATION to make sure all other notifications + * finish before this GPE is re-enabled. + */ + ret = uacpi_kernel_schedule_work( + UACPI_WORK_NOTIFICATION, async_restore_gpe, event + ); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to schedule GPE(%02X) restore: %s\n", + event->idx, uacpi_status_to_string(ret)); + async_restore_gpe(event); + } +} + +static uacpi_interrupt_ret dispatch_gpe( + uacpi_namespace_node *device_node, struct gp_event *event +) +{ + uacpi_status ret; + uacpi_interrupt_ret int_ret = UACPI_INTERRUPT_NOT_HANDLED; + + /* + * For raw handlers we don't do any management whatsoever, we just let the + * handler know a GPE has triggered and let it handle disable/enable as + * well as clearing. + */ + if (event->handler_type == GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW) { + return event->native_handler->cb( + event->native_handler->ctx, device_node, event->idx + ); + } + + ret = set_gpe_state(event, GPE_STATE_DISABLED); + if (uacpi_unlikely_error(ret)) { + uacpi_error("failed to disable GPE(%02X): %s\n", + event->idx, uacpi_status_to_string(ret)); + return int_ret; + } + + event->block_interrupts = UACPI_TRUE; + + if (event->triggering == UACPI_GPE_TRIGGERING_EDGE) { + ret = clear_gpe(event); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to clear GPE(%02X): %s\n", + event->idx, uacpi_status_to_string(ret)); + set_gpe_state(event, GPE_STATE_ENABLED_CONDITIONALLY); + return int_ret; + } + } + + switch (event->handler_type) { + case GPE_HANDLER_TYPE_NATIVE_HANDLER: + int_ret = event->native_handler->cb( + event->native_handler->ctx, device_node, event->idx + ); + if (!(int_ret & UACPI_GPE_REENABLE)) + break; + + ret = restore_gpe(event); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to restore GPE(%02X): %s\n", + event->idx, uacpi_status_to_string(ret)); + } + break; + + case GPE_HANDLER_TYPE_AML_HANDLER: + case GPE_HANDLER_TYPE_IMPLICIT_NOTIFY: + ret = uacpi_kernel_schedule_work( + UACPI_WORK_GPE_EXECUTION, async_run_gpe_handler, event + ); + if (uacpi_unlikely_error(ret)) { + uacpi_warn( + "unable to schedule GPE(%02X) for execution: %s\n", + event->idx, uacpi_status_to_string(ret) + ); + } + break; + + default: + uacpi_warn("GPE(%02X) fired but no handler, keeping disabled\n", + event->idx); + break; + } + + return UACPI_INTERRUPT_HANDLED; +} + +static uacpi_interrupt_ret detect_gpes(struct gpe_block *block) +{ + uacpi_status ret; + uacpi_interrupt_ret int_ret = UACPI_INTERRUPT_NOT_HANDLED; + struct gpe_register *reg; + struct gp_event *event; + uacpi_u64 status, enable; + uacpi_size i, j; + + while (block) { + for (i = 0; i < block->num_registers; ++i) { + reg = &block->registers[i]; + + if (!reg->runtime_mask && !reg->wake_mask) + continue; + + ret = uacpi_gas_read(®->status, &status); + if (uacpi_unlikely_error(ret)) + return int_ret; + + ret = uacpi_gas_read(®->enable, &enable); + if (uacpi_unlikely_error(ret)) + return int_ret; + + if (status == 0) + continue; + + for (j = 0; j < EVENTS_PER_GPE_REGISTER; ++j) { + if (!((status & enable) & (1ull << j))) + continue; + + event = &block->events[j + i * EVENTS_PER_GPE_REGISTER]; + int_ret |= dispatch_gpe(block->device_node, event); + } + } + + block = block->next; + } + + return int_ret; +} + +static uacpi_status maybe_dispatch_gpe( + uacpi_namespace_node *gpe_device, struct gp_event *event +) +{ + uacpi_status ret; + struct gpe_register *reg = event->reg; + uacpi_u64 status; + + ret = uacpi_gas_read(®->status, &status); + if (uacpi_unlikely_error(ret)) + return ret; + + if (!(status & gpe_get_mask(event))) + return ret; + + dispatch_gpe(gpe_device, event); + return ret; +} + +static uacpi_interrupt_ret handle_gpes(uacpi_handle opaque) +{ + struct gpe_interrupt_ctx *ctx = opaque; + + if (uacpi_unlikely(ctx == UACPI_NULL)) + return UACPI_INTERRUPT_NOT_HANDLED; + + return detect_gpes(ctx->gpe_head); +} + +static uacpi_status find_or_create_gpe_interrupt_ctx( + uacpi_u32 irq, struct gpe_interrupt_ctx **out_ctx +) +{ + uacpi_status ret; + struct gpe_interrupt_ctx *entry = g_gpe_interrupt_head; + + while (entry) { + if (entry->irq == irq) { + *out_ctx = entry; + return UACPI_STATUS_OK; + } + + entry = entry->next; + } + + entry = uacpi_kernel_alloc_zeroed(sizeof(*entry)); + if (uacpi_unlikely(entry == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + /* + * SCI interrupt is installed by other code and is responsible for more + * things than just the GPE handling. Don't install it here. + */ + if (irq != g_uacpi_rt_ctx.fadt.sci_int) { + ret = uacpi_kernel_install_interrupt_handler( + irq, handle_gpes, entry, &entry->irq_handle + ); + if (uacpi_unlikely_error(ret)) { + uacpi_free(entry, sizeof(*entry)); + return ret; + } + } + + entry->irq = irq; + entry->next = g_gpe_interrupt_head; + g_gpe_interrupt_head = entry; + + *out_ctx = entry; + return UACPI_STATUS_OK; +} + +static void gpe_release_implicit_notify_handlers(struct gp_event *event) +{ + struct gpe_implicit_notify_handler *handler, *next_handler; + + handler = event->implicit_handler; + while (handler) { + next_handler = handler->next; + uacpi_free(handler, sizeof(*handler)); + handler = next_handler; + } + + event->implicit_handler = UACPI_NULL; +} + +enum gpe_block_action +{ + GPE_BLOCK_ACTION_DISABLE_ALL, + GPE_BLOCK_ACTION_ENABLE_ALL_FOR_RUNTIME, + GPE_BLOCK_ACTION_ENABLE_ALL_FOR_WAKE, + GPE_BLOCK_ACTION_CLEAR_ALL, +}; + +static uacpi_status gpe_block_apply_action( + struct gpe_block *block, enum gpe_block_action action +) +{ + uacpi_status ret; + uacpi_size i; + uacpi_u8 value; + struct gpe_register *reg; + + for (i = 0; i < block->num_registers; ++i) { + reg = &block->registers[i]; + + switch (action) { + case GPE_BLOCK_ACTION_DISABLE_ALL: + value = 0; + break; + case GPE_BLOCK_ACTION_ENABLE_ALL_FOR_RUNTIME: + value = reg->runtime_mask & ~reg->masked_mask; + break; + case GPE_BLOCK_ACTION_ENABLE_ALL_FOR_WAKE: + value = reg->wake_mask; + break; + case GPE_BLOCK_ACTION_CLEAR_ALL: + ret = uacpi_gas_write(®->status, 0xFF); + if (uacpi_unlikely_error(ret)) + return ret; + continue; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + reg->current_mask = value; + ret = uacpi_gas_write(®->enable, value); + if (uacpi_unlikely_error(ret)) + return ret; + } + + return UACPI_STATUS_OK; +} + +static void gpe_block_mask_safe(struct gpe_block *block) +{ + uacpi_size i; + struct gpe_register *reg; + + for (i = 0; i < block->num_registers; ++i) { + reg = &block->registers[i]; + + // No need to flush or do anything if it's not currently enabled + if (!reg->current_mask) + continue; + + // 1. Mask the GPEs, this makes sure their state is no longer modifyable + reg->masked_mask = 0xFF; + + /* + * 2. Wait for in-flight work & IRQs to finish, these might already + * be past the respective "if (masked)" check and therefore may + * try to re-enable a masked GPE. + */ + uacpi_kernel_wait_for_work_completion(); + + /* + * 3. Now that this GPE's state is unmodifyable and we know that + * currently in-flight IRQs will see the masked state, we can + * safely disable all events knowing they won't be re-enabled by + * a racing IRQ. + */ + uacpi_gas_write(®->enable, 0x00); + + /* + * 4. Wait for the last possible IRQ to finish, now that this event is + * disabled. + */ + uacpi_kernel_wait_for_work_completion(); + } +} + +static void uninstall_gpe_block(struct gpe_block *block) +{ + if (block->registers != UACPI_NULL) + gpe_block_mask_safe(block); + + if (block->prev) + block->prev->next = block->next; + + if (block->irq_ctx) { + struct gpe_interrupt_ctx *ctx = block->irq_ctx; + + // Are we the first GPE block? + if (block == ctx->gpe_head) { + ctx->gpe_head = ctx->gpe_head->next; + } else { + struct gpe_block *prev_block = ctx->gpe_head; + + // We're not, do a search + while (prev_block) { + if (prev_block->next == block) { + prev_block->next = block->next; + break; + } + + prev_block = prev_block->next; + } + } + + // This GPE block was the last user of this interrupt context, remove it + if (ctx->gpe_head == UACPI_NULL) { + if (ctx->prev) + ctx->prev->next = ctx->next; + + if (ctx->irq != g_uacpi_rt_ctx.fadt.sci_int) { + uacpi_kernel_uninstall_interrupt_handler( + handle_gpes, ctx->irq_handle + ); + } + + uacpi_free(block->irq_ctx, sizeof(*block->irq_ctx)); + } + } + + if (block->events != UACPI_NULL) { + uacpi_size i; + struct gp_event *event; + + for (i = 0; i < block->num_events; ++i) { + event = &block->events[i]; + + switch (event->handler_type) { + case GPE_HANDLER_TYPE_NONE: + case GPE_HANDLER_TYPE_AML_HANDLER: + break; + + case GPE_HANDLER_TYPE_NATIVE_HANDLER: + case GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW: + uacpi_free(event->native_handler, + sizeof(*event->native_handler)); + break; + + case GPE_HANDLER_TYPE_IMPLICIT_NOTIFY: { + gpe_release_implicit_notify_handlers(event); + break; + } + + default: + break; + } + } + + } + + uacpi_free(block->registers, + sizeof(*block->registers) * block->num_registers); + uacpi_free(block->events, + sizeof(*block->events) * block->num_events); + uacpi_free(block, sizeof(*block)); +} + +static struct gp_event *gpe_from_block(struct gpe_block *block, uacpi_u16 idx) +{ + uacpi_u16 offset; + + if (idx < block->base_idx) + return UACPI_NULL; + + offset = idx - block->base_idx; + if (offset > block->num_events) + return UACPI_NULL; + + return &block->events[offset]; +} + +struct gpe_match_ctx { + struct gpe_block *block; + uacpi_u32 matched_count; + uacpi_bool post_dynamic_table_load; +}; + +static uacpi_iteration_decision do_match_gpe_methods( + uacpi_handle opaque, uacpi_namespace_node *node, uacpi_u32 depth +) +{ + uacpi_status ret; + struct gpe_match_ctx *ctx = opaque; + struct gp_event *event; + uacpi_u8 triggering; + uacpi_u64 idx; + + UACPI_UNUSED(depth); + + if (node->name.text[0] != '_') + return UACPI_ITERATION_DECISION_CONTINUE; + + switch (node->name.text[1]) { + case 'L': + triggering = UACPI_GPE_TRIGGERING_LEVEL; + break; + case 'E': + triggering = UACPI_GPE_TRIGGERING_EDGE; + break; + default: + return UACPI_ITERATION_DECISION_CONTINUE; + } + + ret = uacpi_string_to_integer(&node->name.text[2], 2, UACPI_BASE_HEX, &idx); + if (uacpi_unlikely_error(ret)) { + uacpi_trace("invalid GPE method name %.4s, ignored\n", node->name.text); + return UACPI_ITERATION_DECISION_CONTINUE; + } + + event = gpe_from_block(ctx->block, idx); + if (event == UACPI_NULL) + return UACPI_ITERATION_DECISION_CONTINUE; + + switch (event->handler_type) { + /* + * This had implicit notify configured but this is no longer needed as we + * now have an actual AML handler. Free the implicit notify list and switch + * this handler to AML mode. + */ + case GPE_HANDLER_TYPE_IMPLICIT_NOTIFY: + gpe_release_implicit_notify_handlers(event); + UACPI_FALLTHROUGH; + case GPE_HANDLER_TYPE_NONE: + event->aml_handler = node; + event->handler_type = GPE_HANDLER_TYPE_AML_HANDLER; + break; + + case GPE_HANDLER_TYPE_AML_HANDLER: + // This is okay, since we're re-running the detection code + if (!ctx->post_dynamic_table_load) { + uacpi_warn( + "GPE(%02X) already matched %.4s, skipping %.4s\n", + (uacpi_u32)idx, event->aml_handler->name.text, node->name.text + ); + } + return UACPI_ITERATION_DECISION_CONTINUE; + + case GPE_HANDLER_TYPE_NATIVE_HANDLER: + case GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW: + uacpi_trace( + "not assigning GPE(%02X) to %.4s, override " + "installed by user\n", (uacpi_u32)idx, node->name.text + ); + UACPI_FALLTHROUGH; + default: + return UACPI_ITERATION_DECISION_CONTINUE; + } + + uacpi_trace("assigned GPE(%02X) -> %.4s\n", + (uacpi_u32)idx, node->name.text); + event->triggering = triggering; + ctx->matched_count++; + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +void uacpi_events_match_post_dynamic_table_load(void) +{ + struct gpe_match_ctx match_ctx = { + .post_dynamic_table_load = UACPI_TRUE, + }; + + uacpi_namespace_write_unlock(); + + if (uacpi_unlikely_error(uacpi_recursive_lock_acquire(&g_event_lock))) + goto out; + + struct gpe_interrupt_ctx *irq_ctx = g_gpe_interrupt_head; + + while (irq_ctx) { + match_ctx.block = irq_ctx->gpe_head; + + while (match_ctx.block) { + uacpi_namespace_do_for_each_child( + match_ctx.block->device_node, do_match_gpe_methods, UACPI_NULL, + UACPI_OBJECT_METHOD_BIT, UACPI_MAX_DEPTH_ANY, + UACPI_SHOULD_LOCK_YES, UACPI_PERMANENT_ONLY_YES, &match_ctx + ); + match_ctx.block = match_ctx.block->next; + } + + irq_ctx = irq_ctx->next; + } + + if (match_ctx.matched_count) { + uacpi_info("matched %u additional GPEs post dynamic table load\n", + match_ctx.matched_count); + } + +out: + uacpi_recursive_lock_release(&g_event_lock); + uacpi_namespace_write_lock(); +} + +static uacpi_status create_gpe_block( + uacpi_namespace_node *device_node, uacpi_u32 irq, uacpi_u16 base_idx, + uacpi_u64 address, uacpi_u8 address_space_id, uacpi_u16 num_registers +) +{ + uacpi_status ret = UACPI_STATUS_OUT_OF_MEMORY; + struct gpe_match_ctx match_ctx = { 0 }; + struct gpe_block *block; + struct gpe_register *reg; + struct gp_event *event; + uacpi_size i, j; + + block = uacpi_kernel_alloc_zeroed(sizeof(*block)); + if (uacpi_unlikely(block == UACPI_NULL)) + return ret; + + block->device_node = device_node; + block->base_idx = base_idx; + + block->num_registers = num_registers; + block->registers = uacpi_kernel_alloc_zeroed( + num_registers * sizeof(*block->registers) + ); + if (uacpi_unlikely(block->registers == UACPI_NULL)) + goto error_out; + + block->num_events = num_registers * EVENTS_PER_GPE_REGISTER; + block->events = uacpi_kernel_alloc_zeroed( + block->num_events * sizeof(*block->events) + ); + if (uacpi_unlikely(block->events == UACPI_NULL)) + goto error_out; + + for (reg = block->registers, event = block->events, i = 0; + i < num_registers; ++i, ++reg) { + + /* + * Initialize this register pair as well as all the events within it. + * + * Each register has two sub registers: status & enable, 8 bits each. + * Each bit corresponds to one event that we initialize below. + */ + reg->base_idx = base_idx + (i * EVENTS_PER_GPE_REGISTER); + + reg->status.address = address + i; + reg->status.address_space_id = address_space_id; + reg->status.register_bit_width = 8; + + reg->enable.address = address + num_registers + i; + reg->enable.address_space_id = address_space_id; + reg->enable.register_bit_width = 8; + + for (j = 0; j < EVENTS_PER_GPE_REGISTER; ++j, ++event) { + event->idx = reg->base_idx + j; + event->reg = reg; + } + + /* + * Disable all GPEs in this register & clear anything that might be + * pending from earlier. + */ + ret = uacpi_gas_write(®->enable, 0x00); + if (uacpi_unlikely_error(ret)) + goto error_out; + + ret = uacpi_gas_write(®->status, 0xFF); + if (uacpi_unlikely_error(ret)) + goto error_out; + } + + ret = find_or_create_gpe_interrupt_ctx(irq, &block->irq_ctx); + if (uacpi_unlikely_error(ret)) + goto error_out; + + block->next = block->irq_ctx->gpe_head; + block->irq_ctx->gpe_head = block; + match_ctx.block = block; + + uacpi_namespace_do_for_each_child( + device_node, do_match_gpe_methods, UACPI_NULL, + UACPI_OBJECT_METHOD_BIT, UACPI_MAX_DEPTH_ANY, + UACPI_SHOULD_LOCK_YES, UACPI_PERMANENT_ONLY_YES, &match_ctx + ); + + uacpi_trace("initialized GPE block %.4s[%d->%d], %d AML handlers (IRQ %d)\n", + device_node->name.text, base_idx, base_idx + block->num_events, + match_ctx.matched_count, irq); + return UACPI_STATUS_OK; + +error_out: + uninstall_gpe_block(block); + return ret; +} + +typedef uacpi_iteration_decision (*gpe_block_iteration_callback) + (struct gpe_block*, uacpi_handle); + +static void for_each_gpe_block( + gpe_block_iteration_callback cb, uacpi_handle handle +) +{ + uacpi_iteration_decision decision; + struct gpe_interrupt_ctx *irq_ctx = g_gpe_interrupt_head; + struct gpe_block *block; + + while (irq_ctx) { + block = irq_ctx->gpe_head; + + while (block) { + decision = cb(block, handle); + if (decision == UACPI_ITERATION_DECISION_BREAK) + return; + + block = block->next; + } + + irq_ctx = irq_ctx->next; + } +} + +struct gpe_search_ctx { + uacpi_namespace_node *gpe_device; + uacpi_u16 idx; + struct gpe_block *out_block; + struct gp_event *out_event; +}; + +static uacpi_iteration_decision do_find_gpe( + struct gpe_block *block, uacpi_handle opaque +) +{ + struct gpe_search_ctx *ctx = opaque; + + if (block->device_node != ctx->gpe_device) + return UACPI_ITERATION_DECISION_CONTINUE; + + ctx->out_block = block; + ctx->out_event = gpe_from_block(block, ctx->idx); + if (ctx->out_event == UACPI_NULL) + return UACPI_ITERATION_DECISION_CONTINUE; + + return UACPI_ITERATION_DECISION_BREAK; +} + +static struct gp_event *get_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + struct gpe_search_ctx ctx = { + .gpe_device = gpe_device, + .idx = idx, + }; + + for_each_gpe_block(do_find_gpe, &ctx); + return ctx.out_event; +} + +static void gp_event_toggle_masks(struct gp_event *event, uacpi_bool set_on) +{ + uacpi_u8 this_mask; + struct gpe_register *reg = event->reg; + + this_mask = gpe_get_mask(event); + + if (set_on) { + reg->runtime_mask |= this_mask; + reg->current_mask = reg->runtime_mask; + return; + } + + reg->runtime_mask &= ~this_mask; + reg->current_mask = reg->runtime_mask; +} + +static uacpi_status gpe_remove_user(struct gp_event *event) +{ + uacpi_status ret = UACPI_STATUS_OK; + + if (uacpi_unlikely(event->num_users == 0)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (--event->num_users == 0) { + gp_event_toggle_masks(event, UACPI_FALSE); + + ret = set_gpe_state(event, GPE_STATE_DISABLED); + if (uacpi_unlikely_error(ret)) { + gp_event_toggle_masks(event, UACPI_TRUE); + event->num_users++; + } + } + + return ret; +} + +enum event_clear_if_first { + EVENT_CLEAR_IF_FIRST_YES, + EVENT_CLEAR_IF_FIRST_NO, +}; + +static uacpi_status gpe_add_user( + struct gp_event *event, enum event_clear_if_first clear_if_first +) +{ + uacpi_status ret = UACPI_STATUS_OK; + + if (uacpi_unlikely(event->num_users == 0xFF)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (++event->num_users == 1) { + if (clear_if_first == EVENT_CLEAR_IF_FIRST_YES) + clear_gpe(event); + + gp_event_toggle_masks(event, UACPI_TRUE); + + ret = set_gpe_state(event, GPE_STATE_ENABLED); + if (uacpi_unlikely_error(ret)) { + gp_event_toggle_masks(event, UACPI_FALSE); + event->num_users--; + } + } + + return ret; +} + +const uacpi_char *uacpi_gpe_triggering_to_string( + uacpi_gpe_triggering triggering +) +{ + switch (triggering) { + case UACPI_GPE_TRIGGERING_EDGE: + return "edge"; + case UACPI_GPE_TRIGGERING_LEVEL: + return "level"; + default: + return "invalid"; + } +} + +static uacpi_bool gpe_needs_polling(struct gp_event *event) +{ + return event->num_users && event->triggering == UACPI_GPE_TRIGGERING_EDGE; +} + +static uacpi_status gpe_mask_unmask( + struct gp_event *event, uacpi_bool should_mask +) +{ + struct gpe_register *reg; + uacpi_u8 mask; + + reg = event->reg; + mask = gpe_get_mask(event); + + if (should_mask) { + if (reg->masked_mask & mask) + return UACPI_STATUS_INVALID_ARGUMENT; + + // 1. Mask the GPE, this makes sure its state is no longer modifyable + reg->masked_mask |= mask; + + /* + * 2. Wait for in-flight work & IRQs to finish, these might already + * be past the respective "if (masked)" check and therefore may + * try to re-enable a masked GPE. + */ + uacpi_kernel_wait_for_work_completion(); + + /* + * 3. Now that this GPE's state is unmodifyable and we know that currently + * in-flight IRQs will see the masked state, we can safely disable this + * event knowing it won't be re-enabled by a racing IRQ. + */ + set_gpe_state(event, GPE_STATE_DISABLED); + + /* + * 4. Wait for the last possible IRQ to finish, now that this event is + * disabled. + */ + uacpi_kernel_wait_for_work_completion(); + + return UACPI_STATUS_OK; + } + + if (!(reg->masked_mask & mask)) + return UACPI_STATUS_INVALID_ARGUMENT; + + reg->masked_mask &= ~mask; + if (!event->block_interrupts && event->num_users) + set_gpe_state(event, GPE_STATE_ENABLED_CONDITIONALLY); + + return UACPI_STATUS_OK; +} + +/* + * Safely mask the event before we modify its handlers. + * + * This makes sure we can't get an IRQ in the middle of modifying this + * event's structures. + */ +static uacpi_bool gpe_mask_safe(struct gp_event *event) +{ + // No need to flush or do anything if it's not currently enabled + if (!(event->reg->current_mask & gpe_get_mask(event))) + return UACPI_FALSE; + + gpe_mask_unmask(event, UACPI_TRUE); + return UACPI_TRUE; +} + +static uacpi_iteration_decision do_initialize_gpe_block( + struct gpe_block *block, uacpi_handle opaque +) +{ + uacpi_status ret; + uacpi_bool *poll_blocks = opaque; + uacpi_size i, j, count_enabled = 0; + struct gp_event *event; + + for (i = 0; i < block->num_registers; ++i) { + for (j = 0; j < EVENTS_PER_GPE_REGISTER; ++j) { + event = &block->events[j + i * EVENTS_PER_GPE_REGISTER]; + + if (event->wake || + event->handler_type != GPE_HANDLER_TYPE_AML_HANDLER) + continue; + + ret = gpe_add_user(event, EVENT_CLEAR_IF_FIRST_NO); + if (uacpi_unlikely_error(ret)) { + uacpi_warn("failed to enable GPE(%02X): %s\n", + event->idx, uacpi_status_to_string(ret)); + continue; + } + + *poll_blocks |= gpe_needs_polling(event); + count_enabled++; + } + } + + if (count_enabled) { + uacpi_info( + "enabled %zu GPEs in block %.4s@[%d->%d]\n", + count_enabled, block->device_node->name.text, + block->base_idx, block->base_idx + block->num_events + ); + } + return UACPI_ITERATION_DECISION_CONTINUE; +} + +uacpi_status uacpi_finalize_gpe_initialization(void) +{ + uacpi_status ret; + uacpi_bool poll_blocks = UACPI_FALSE; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + if (g_gpes_finalized) + goto out; + + g_gpes_finalized = UACPI_TRUE; + + for_each_gpe_block(do_initialize_gpe_block, &poll_blocks); + if (poll_blocks) + detect_gpes(g_gpe_interrupt_head->gpe_head); + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +static uacpi_status sanitize_device_and_find_gpe( + uacpi_namespace_node **gpe_device, uacpi_u16 idx, + struct gp_event **out_event +) +{ + if (*gpe_device == UACPI_NULL) { + *gpe_device = uacpi_namespace_get_predefined( + UACPI_PREDEFINED_NAMESPACE_GPE + ); + } + + *out_event = get_gpe(*gpe_device, idx); + if (*out_event == UACPI_NULL) + return UACPI_STATUS_NOT_FOUND; + + return UACPI_STATUS_OK; +} + +static uacpi_status do_install_gpe_handler( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_gpe_triggering triggering, enum gpe_handler_type type, + uacpi_gpe_handler handler, uacpi_handle ctx +) +{ + uacpi_status ret; + struct gp_event *event; + struct gpe_native_handler *native_handler; + uacpi_bool did_mask; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + if (uacpi_unlikely(triggering > UACPI_GPE_TRIGGERING_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + if (event->handler_type == GPE_HANDLER_TYPE_NATIVE_HANDLER || + event->handler_type == GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + native_handler = uacpi_kernel_alloc(sizeof(*native_handler)); + if (uacpi_unlikely(native_handler == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + + native_handler->cb = handler; + native_handler->ctx = ctx; + native_handler->previous_handler = event->any_handler; + native_handler->previous_handler_type = event->handler_type; + native_handler->previous_triggering = event->triggering; + native_handler->previously_enabled = UACPI_FALSE; + + did_mask = gpe_mask_safe(event); + + if ((event->handler_type == GPE_HANDLER_TYPE_AML_HANDLER || + event->handler_type == GPE_HANDLER_TYPE_IMPLICIT_NOTIFY) && + event->num_users != 0) { + native_handler->previously_enabled = UACPI_TRUE; + gpe_remove_user(event); + + if (uacpi_unlikely(event->triggering != triggering)) { + uacpi_warn( + "GPE(%02X) user handler claims %s triggering, originally " + "configured as %s\n", idx, + uacpi_gpe_triggering_to_string(triggering), + uacpi_gpe_triggering_to_string(event->triggering) + ); + } + } + + event->native_handler = native_handler; + event->handler_type = type; + event->triggering = triggering; + + if (did_mask) + gpe_mask_unmask(event, UACPI_FALSE); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_install_gpe_handler( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_gpe_triggering triggering, uacpi_gpe_handler handler, + uacpi_handle ctx +) +{ + return do_install_gpe_handler( + gpe_device, idx, triggering, GPE_HANDLER_TYPE_NATIVE_HANDLER, + handler, ctx + ); +} + +uacpi_status uacpi_install_gpe_handler_raw( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_gpe_triggering triggering, uacpi_gpe_handler handler, + uacpi_handle ctx +) +{ + return do_install_gpe_handler( + gpe_device, idx, triggering, GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW, + handler, ctx + ); +} + +uacpi_status uacpi_uninstall_gpe_handler( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_gpe_handler handler +) +{ + uacpi_status ret; + struct gp_event *event; + struct gpe_native_handler *native_handler; + uacpi_bool did_mask; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + if (event->handler_type != GPE_HANDLER_TYPE_NATIVE_HANDLER && + event->handler_type != GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW) { + ret = UACPI_STATUS_NOT_FOUND; + goto out; + } + + native_handler = event->native_handler; + if (uacpi_unlikely(native_handler->cb != handler)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + did_mask = gpe_mask_safe(event); + + event->aml_handler = native_handler->previous_handler; + event->triggering = native_handler->previous_triggering; + event->handler_type = native_handler->previous_handler_type; + + if ((event->handler_type == GPE_HANDLER_TYPE_AML_HANDLER || + event->handler_type == GPE_HANDLER_TYPE_IMPLICIT_NOTIFY) && + native_handler->previously_enabled) { + gpe_add_user(event, EVENT_CLEAR_IF_FIRST_NO); + } + + uacpi_free(native_handler, sizeof(*native_handler)); + + if (did_mask) + gpe_mask_unmask(event, UACPI_FALSE); + + if (gpe_needs_polling(event)) + maybe_dispatch_gpe(gpe_device, event); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_enable_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + uacpi_status ret; + struct gp_event *event; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + if (uacpi_unlikely(event->handler_type == GPE_HANDLER_TYPE_NONE)) { + ret = UACPI_STATUS_NO_HANDLER; + goto out; + } + + ret = gpe_add_user(event, EVENT_CLEAR_IF_FIRST_YES); + if (uacpi_unlikely_error(ret)) + goto out; + + if (gpe_needs_polling(event)) + maybe_dispatch_gpe(gpe_device, event); + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_disable_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + uacpi_status ret; + struct gp_event *event; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + ret = gpe_remove_user(event); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_clear_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + uacpi_status ret; + struct gp_event *event; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + ret = clear_gpe(event); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +static uacpi_status gpe_suspend_resume( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, enum gpe_state state +) +{ + uacpi_status ret; + struct gp_event *event; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + event->block_interrupts = state == GPE_STATE_DISABLED; + ret = set_gpe_state(event, state); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_suspend_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + return gpe_suspend_resume(gpe_device, idx, GPE_STATE_DISABLED); +} + +uacpi_status uacpi_resume_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + return gpe_suspend_resume(gpe_device, idx, GPE_STATE_ENABLED); +} + +uacpi_status uacpi_finish_handling_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + uacpi_status ret; + struct gp_event *event; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + event = get_gpe(gpe_device, idx); + if (uacpi_unlikely(event == UACPI_NULL)) { + ret = UACPI_STATUS_NOT_FOUND; + goto out; + } + + ret = restore_gpe(event); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; + +} + +static uacpi_status gpe_get_mask_unmask( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, uacpi_bool should_mask +) +{ + uacpi_status ret; + struct gp_event *event; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + ret = gpe_mask_unmask(event, should_mask); + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_mask_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + return gpe_get_mask_unmask(gpe_device, idx, UACPI_TRUE); +} + +uacpi_status uacpi_unmask_gpe( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + return gpe_get_mask_unmask(gpe_device, idx, UACPI_FALSE); +} + +uacpi_status uacpi_setup_gpe_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, + uacpi_namespace_node *wake_device +) +{ + uacpi_status ret; + struct gp_event *event; + uacpi_bool did_mask; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + if (wake_device != UACPI_NULL) { + uacpi_bool is_dev = wake_device == uacpi_namespace_root(); + + if (!is_dev) { + ret = uacpi_namespace_node_is(wake_device, UACPI_OBJECT_DEVICE, &is_dev); + if (uacpi_unlikely_error(ret)) + return ret; + } + + if (!is_dev) + return UACPI_STATUS_INVALID_ARGUMENT; + } + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + did_mask = gpe_mask_safe(event); + + if (wake_device != UACPI_NULL) { + switch (event->handler_type) { + case GPE_HANDLER_TYPE_NONE: + event->handler_type = GPE_HANDLER_TYPE_IMPLICIT_NOTIFY; + event->triggering = UACPI_GPE_TRIGGERING_LEVEL; + break; + + case GPE_HANDLER_TYPE_AML_HANDLER: + /* + * An AML handler already exists, we expect it to call Notify() as + * it sees fit. For now just make sure this event is disabled if it + * had been enabled automatically previously during initialization. + */ + gpe_remove_user(event); + break; + + case GPE_HANDLER_TYPE_NATIVE_HANDLER_RAW: + case GPE_HANDLER_TYPE_NATIVE_HANDLER: + uacpi_warn( + "not configuring implicit notify for GPE(%02X) -> %.4s: " + " a user handler already installed\n", event->idx, + wake_device->name.text + ); + break; + + // We will re-check this below + case GPE_HANDLER_TYPE_IMPLICIT_NOTIFY: + break; + + default: + uacpi_warn("invalid GPE(%02X) handler type: %d\n", + event->idx, event->handler_type); + ret = UACPI_STATUS_INTERNAL_ERROR; + goto out_unmask; + } + + /* + * This GPE has no known AML handler, so we configure it to receive + * implicit notifications for wake devices when we get a corresponding + * GPE triggered. Usually it's the job of a matching AML handler, but + * we didn't find any. + */ + if (event->handler_type == GPE_HANDLER_TYPE_IMPLICIT_NOTIFY) { + struct gpe_implicit_notify_handler *implicit_handler; + + implicit_handler = event->implicit_handler; + while (implicit_handler) { + if (implicit_handler->device == wake_device) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out_unmask; + } + + implicit_handler = implicit_handler->next; + } + + implicit_handler = uacpi_kernel_alloc(sizeof(*implicit_handler)); + if (uacpi_likely(implicit_handler != UACPI_NULL)) { + implicit_handler->device = wake_device; + implicit_handler->next = event->implicit_handler; + event->implicit_handler = implicit_handler; + } else { + uacpi_warn( + "unable to configure implicit wake for GPE(%02X) -> %.4s: " + "out of memory\n", event->idx, wake_device->name.text + ); + } + } + } + + event->wake = UACPI_TRUE; + +out_unmask: + if (did_mask) + gpe_mask_unmask(event, UACPI_FALSE); +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +static uacpi_status gpe_enable_disable_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, uacpi_bool enabled +) +{ + uacpi_status ret; + struct gp_event *event; + struct gpe_register *reg; + uacpi_u8 mask; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + if (!event->wake) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + reg = event->reg; + mask = gpe_get_mask(event); + + if (enabled) + reg->wake_mask |= mask; + else + reg->wake_mask &= mask; + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_enable_gpe_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + return gpe_enable_disable_for_wake(gpe_device, idx, UACPI_TRUE); +} + +uacpi_status uacpi_disable_gpe_for_wake( + uacpi_namespace_node *gpe_device, uacpi_u16 idx +) +{ + return gpe_enable_disable_for_wake(gpe_device, idx, UACPI_FALSE); +} + +struct do_for_all_gpes_ctx { + enum gpe_block_action action; + uacpi_status ret; +}; + +static uacpi_iteration_decision do_for_all_gpes( + struct gpe_block *block, uacpi_handle opaque +) +{ + struct do_for_all_gpes_ctx *ctx = opaque; + + ctx->ret = gpe_block_apply_action(block, ctx->action); + if (uacpi_unlikely_error(ctx->ret)) + return UACPI_ITERATION_DECISION_BREAK; + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +static uacpi_status for_all_gpes_locked(struct do_for_all_gpes_ctx *ctx) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + for_each_gpe_block(do_for_all_gpes, ctx); + + uacpi_recursive_lock_release(&g_event_lock); + return ctx->ret; +} + +uacpi_status uacpi_disable_all_gpes(void) +{ + struct do_for_all_gpes_ctx ctx = { + .action = GPE_BLOCK_ACTION_DISABLE_ALL, + }; + return for_all_gpes_locked(&ctx); +} + +uacpi_status uacpi_enable_all_runtime_gpes(void) +{ + struct do_for_all_gpes_ctx ctx = { + .action = GPE_BLOCK_ACTION_ENABLE_ALL_FOR_RUNTIME, + }; + return for_all_gpes_locked(&ctx); +} + +uacpi_status uacpi_enable_all_wake_gpes(void) +{ + struct do_for_all_gpes_ctx ctx = { + .action = GPE_BLOCK_ACTION_ENABLE_ALL_FOR_WAKE, + }; + return for_all_gpes_locked(&ctx); +} + +static uacpi_status initialize_gpes(void) +{ + uacpi_status ret; + uacpi_namespace_node *gpe_node; + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + uacpi_u8 gpe0_regs = 0, gpe1_regs = 0; + + gpe_node = uacpi_namespace_get_predefined(UACPI_PREDEFINED_NAMESPACE_GPE); + + if (fadt->x_gpe0_blk.address && fadt->gpe0_blk_len) { + gpe0_regs = fadt->gpe0_blk_len / 2; + + ret = create_gpe_block( + gpe_node, fadt->sci_int, 0, fadt->x_gpe0_blk.address, + fadt->x_gpe0_blk.address_space_id, gpe0_regs + ); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to create FADT GPE block 0: %s\n", + uacpi_status_to_string(ret)); + } + } + + if (fadt->x_gpe1_blk.address && fadt->gpe1_blk_len) { + gpe1_regs = fadt->gpe1_blk_len / 2; + + if (uacpi_unlikely((gpe0_regs * EVENTS_PER_GPE_REGISTER) > + fadt->gpe1_base)) { + uacpi_error( + "FADT GPE block 1 [%d->%d] collides with GPE block 0 " + "[%d->%d], ignoring\n", + 0, gpe0_regs * EVENTS_PER_GPE_REGISTER, fadt->gpe1_base, + gpe1_regs * EVENTS_PER_GPE_REGISTER + ); + gpe1_regs = 0; + goto out; + } + + ret = create_gpe_block( + gpe_node, fadt->sci_int, fadt->gpe1_base, fadt->x_gpe1_blk.address, + fadt->x_gpe1_blk.address_space_id, gpe1_regs + ); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to create FADT GPE block 1: %s\n", + uacpi_status_to_string(ret)); + } + } + + if (gpe0_regs == 0 && gpe1_regs == 0) + uacpi_trace("platform has no FADT GPE events\n"); + +out: + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_install_gpe_block( + uacpi_namespace_node *gpe_device, uacpi_u64 address, + uacpi_address_space address_space, uacpi_u16 num_registers, uacpi_u32 irq +) +{ + uacpi_status ret; + uacpi_bool is_dev; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_namespace_node_is(gpe_device, UACPI_OBJECT_DEVICE, &is_dev); + if (uacpi_unlikely_error(ret)) + return ret; + if (!is_dev) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + if (uacpi_unlikely(get_gpe(gpe_device, 0) != UACPI_NULL)) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + ret = create_gpe_block( + gpe_device, irq, 0, address, address_space, num_registers + ); + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_uninstall_gpe_block( + uacpi_namespace_node *gpe_device +) +{ + uacpi_status ret; + uacpi_bool is_dev; + struct gpe_search_ctx search_ctx = { + .idx = 0, + .gpe_device = gpe_device, + }; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_namespace_node_is(gpe_device, UACPI_OBJECT_DEVICE, &is_dev); + if (uacpi_unlikely_error(ret)) + return ret; + if (!is_dev) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + for_each_gpe_block(do_find_gpe, &search_ctx); + if (search_ctx.out_block == UACPI_NULL) { + ret = UACPI_STATUS_NOT_FOUND; + goto out; + } + + uninstall_gpe_block(search_ctx.out_block); + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +static uacpi_interrupt_ret handle_global_lock(uacpi_handle ctx) +{ + uacpi_cpu_flags flags; + UACPI_UNUSED(ctx); + + if (uacpi_unlikely(!g_uacpi_rt_ctx.has_global_lock)) { + uacpi_warn("platform has no global lock but a release event " + "was fired anyway?\n"); + return UACPI_INTERRUPT_HANDLED; + } + + flags = uacpi_kernel_lock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock); + if (!g_uacpi_rt_ctx.global_lock_pending) { + uacpi_trace("spurious firmware global lock release notification\n"); + goto out; + } + + uacpi_trace("received a firmware global lock release notification\n"); + + uacpi_kernel_signal_event(g_uacpi_rt_ctx.global_lock_event); + g_uacpi_rt_ctx.global_lock_pending = UACPI_FALSE; + +out: + uacpi_kernel_unlock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock, flags); + return UACPI_INTERRUPT_HANDLED; +} + +static uacpi_interrupt_ret handle_sci(uacpi_handle ctx) +{ + uacpi_interrupt_ret int_ret = UACPI_INTERRUPT_NOT_HANDLED; + + int_ret |= handle_fixed_events(); + int_ret |= handle_gpes(ctx); + + return int_ret; +} + +uacpi_status uacpi_initialize_events_early(void) +{ + uacpi_status ret; + + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + g_gpe_state_slock = uacpi_kernel_create_spinlock(); + if (uacpi_unlikely(g_gpe_state_slock == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = uacpi_recursive_lock_init(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = initialize_fixed_events(); + if (uacpi_unlikely_error(ret)) + return ret; + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_initialize_events(void) +{ + uacpi_status ret; + + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + ret = initialize_gpes(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_kernel_install_interrupt_handler( + g_uacpi_rt_ctx.fadt.sci_int, handle_sci, g_gpe_interrupt_head, + &g_uacpi_rt_ctx.sci_handle + ); + if (uacpi_unlikely_error(ret)) { + uacpi_error( + "unable to install SCI interrupt handler: %s\n", + uacpi_status_to_string(ret) + ); + return ret; + } + g_uacpi_rt_ctx.sci_handle_valid = UACPI_TRUE; + + g_uacpi_rt_ctx.global_lock_event = uacpi_kernel_create_event(); + if (uacpi_unlikely(g_uacpi_rt_ctx.global_lock_event == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + g_uacpi_rt_ctx.global_lock_spinlock = uacpi_kernel_create_spinlock(); + if (uacpi_unlikely(g_uacpi_rt_ctx.global_lock_spinlock == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = uacpi_install_fixed_event_handler( + UACPI_FIXED_EVENT_GLOBAL_LOCK, handle_global_lock, UACPI_NULL + ); + if (uacpi_likely_success(ret)) { + if (uacpi_unlikely(g_uacpi_rt_ctx.facs == UACPI_NULL)) { + uacpi_uninstall_fixed_event_handler(UACPI_FIXED_EVENT_GLOBAL_LOCK); + uacpi_warn("platform has global lock but no FACS was provided\n"); + return ret; + } + g_uacpi_rt_ctx.has_global_lock = UACPI_TRUE; + } else if (ret == UACPI_STATUS_HARDWARE_TIMEOUT) { + // has_global_lock remains set to false + uacpi_trace("platform has no global lock\n"); + ret = UACPI_STATUS_OK; + } + + return ret; +} + +void uacpi_deinitialize_events(void) +{ + struct gpe_interrupt_ctx *ctx, *next_ctx = g_gpe_interrupt_head; + uacpi_size i; + + g_gpes_finalized = UACPI_FALSE; + + if (g_uacpi_rt_ctx.sci_handle_valid) { + uacpi_kernel_uninstall_interrupt_handler( + handle_sci, g_uacpi_rt_ctx.sci_handle + ); + g_uacpi_rt_ctx.sci_handle_valid = UACPI_FALSE; + } + + while (next_ctx) { + ctx = next_ctx; + next_ctx = ctx->next; + + struct gpe_block *block, *next_block = ctx->gpe_head; + while (next_block) { + block = next_block; + next_block = block->next; + uninstall_gpe_block(block); + } + } + + for (i = 0; i < UACPI_FIXED_EVENT_MAX; ++i) { + if (fixed_event_handlers[i].handler) + uacpi_uninstall_fixed_event_handler(i); + } + + if (g_gpe_state_slock != UACPI_NULL) { + uacpi_kernel_free_spinlock(g_gpe_state_slock); + g_gpe_state_slock = UACPI_NULL; + } + + uacpi_recursive_lock_deinit(&g_event_lock); + + g_gpe_interrupt_head = UACPI_NULL; +} + +uacpi_status uacpi_install_fixed_event_handler( + uacpi_fixed_event event, uacpi_interrupt_handler handler, + uacpi_handle user +) +{ + uacpi_status ret; + struct fixed_event_handler *ev; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(event < 0 || event > UACPI_FIXED_EVENT_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ev = &fixed_event_handlers[event]; + + if (ev->handler != UACPI_NULL) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + ev->handler = handler; + ev->ctx = user; + + ret = set_event(event, UACPI_EVENT_ENABLED); + if (uacpi_unlikely_error(ret)) { + ev->handler = UACPI_NULL; + ev->ctx = UACPI_NULL; + } + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_uninstall_fixed_event_handler( + uacpi_fixed_event event +) +{ + uacpi_status ret; + struct fixed_event_handler *ev; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(event < 0 || event > UACPI_FIXED_EVENT_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ev = &fixed_event_handlers[event]; + + ret = set_event(event, UACPI_EVENT_DISABLED); + if (uacpi_unlikely_error(ret)) + goto out; + + uacpi_kernel_wait_for_work_completion(); + + ev->handler = UACPI_NULL; + ev->ctx = UACPI_NULL; + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_fixed_event_info( + uacpi_fixed_event event, uacpi_event_info *out_info +) +{ + uacpi_status ret; + const struct fixed_event *ev; + uacpi_u64 raw_value; + uacpi_event_info info = 0; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(event < 0 || event > UACPI_FIXED_EVENT_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_NOT_FOUND; + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + if (fixed_event_handlers[event].handler != UACPI_NULL) + info |= UACPI_EVENT_INFO_HAS_HANDLER; + + ev = &fixed_events[event]; + + ret = uacpi_read_register_field(ev->enable_field, &raw_value); + if (uacpi_unlikely_error(ret)) + goto out; + if (raw_value) + info |= UACPI_EVENT_INFO_ENABLED | UACPI_EVENT_INFO_HW_ENABLED; + + ret = uacpi_read_register_field(ev->status_field, &raw_value); + if (uacpi_unlikely_error(ret)) + goto out; + if (raw_value) + info |= UACPI_EVENT_INFO_HW_STATUS; + + *out_info = info; +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +uacpi_status uacpi_gpe_info( + uacpi_namespace_node *gpe_device, uacpi_u16 idx, uacpi_event_info *out_info +) +{ + uacpi_status ret; + struct gp_event *event; + struct gpe_register *reg; + uacpi_u8 mask; + uacpi_u64 raw_value; + uacpi_event_info info = 0; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = sanitize_device_and_find_gpe(&gpe_device, idx, &event); + if (uacpi_unlikely_error(ret)) + goto out; + + if (event->handler_type != GPE_HANDLER_TYPE_NONE) + info |= UACPI_EVENT_INFO_HAS_HANDLER; + + mask = gpe_get_mask(event); + reg = event->reg; + + if (reg->runtime_mask & mask) + info |= UACPI_EVENT_INFO_ENABLED; + if (reg->masked_mask & mask) + info |= UACPI_EVENT_INFO_MASKED; + if (reg->wake_mask & mask) + info |= UACPI_EVENT_INFO_ENABLED_FOR_WAKE; + + ret = uacpi_gas_read(®->enable, &raw_value); + if (uacpi_unlikely_error(ret)) + goto out; + if (raw_value & mask) + info |= UACPI_EVENT_INFO_HW_ENABLED; + + ret = uacpi_gas_read(®->status, &raw_value); + if (uacpi_unlikely_error(ret)) + goto out; + if (raw_value & mask) + info |= UACPI_EVENT_INFO_HW_STATUS; + + *out_info = info; +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +#define PM1_STATUS_BITS ( \ + ACPI_PM1_STS_TMR_STS_MASK | \ + ACPI_PM1_STS_BM_STS_MASK | \ + ACPI_PM1_STS_GBL_STS_MASK | \ + ACPI_PM1_STS_PWRBTN_STS_MASK | \ + ACPI_PM1_STS_SLPBTN_STS_MASK | \ + ACPI_PM1_STS_RTC_STS_MASK | \ + ACPI_PM1_STS_PCIEXP_WAKE_STS_MASK | \ + ACPI_PM1_STS_WAKE_STS_MASK \ +) + +uacpi_status uacpi_clear_all_events(void) +{ + uacpi_status ret; + struct do_for_all_gpes_ctx ctx = { + .action = GPE_BLOCK_ACTION_CLEAR_ALL, + }; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + ret = uacpi_recursive_lock_acquire(&g_event_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_write_register(UACPI_REGISTER_PM1_STS, PM1_STATUS_BITS); + if (uacpi_unlikely_error(ret)) + goto out; + + for_each_gpe_block(do_for_all_gpes, &ctx); + ret = ctx.ret; + +out: + uacpi_recursive_lock_release(&g_event_lock); + return ret; +} + +#endif diff --git a/src/uacpi/files.cmake b/src/uacpi/files.cmake new file mode 100644 index 0000000..32e166a --- /dev/null +++ b/src/uacpi/files.cmake @@ -0,0 +1,21 @@ +uacpi_add_sources( + tables.c + types.c + uacpi.c + utilities.c + interpreter.c + opcodes.c + namespace.c + stdlib.c + shareable.c + opregion.c + default_handlers.c + io.c + notify.c + sleep.c + registers.c + resources.c + event.c + mutex.c + osi.c +) diff --git a/src/uacpi/interpreter.c b/src/uacpi/interpreter.c new file mode 100644 index 0000000..61c6921 --- /dev/null +++ b/src/uacpi/interpreter.c @@ -0,0 +1,5822 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +enum item_type { + ITEM_NONE = 0, + ITEM_NAMESPACE_NODE, + ITEM_OBJECT, + ITEM_EMPTY_OBJECT, + ITEM_PACKAGE_LENGTH, + ITEM_IMMEDIATE, +}; + +struct package_length { + uacpi_u32 begin; + uacpi_u32 end; +}; + +struct item { + uacpi_u8 type; + union { + uacpi_handle handle; + uacpi_object *obj; + struct uacpi_namespace_node *node; + struct package_length pkg; + uacpi_u64 immediate; + uacpi_u8 immediate_bytes[8]; + }; +}; + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(item_array, struct item, 8) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL(item_array, struct item, static) + +struct op_context { + uacpi_u8 pc; + uacpi_bool preempted; + + /* + * == 0 -> none + * >= 1 -> item[idx - 1] + */ + uacpi_u8 tracked_pkg_idx; + + const struct uacpi_op_spec *op; + struct item_array items; +}; + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(op_context_array, struct op_context, 8) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + op_context_array, struct op_context, static +) + +static struct op_context *op_context_array_one_before_last( + struct op_context_array *arr +) +{ + uacpi_size size; + + size = op_context_array_size(arr); + + if (size < 2) + return UACPI_NULL; + + return op_context_array_at(arr, size - 2); +} + +enum code_block_type { + CODE_BLOCK_IF = 1, + CODE_BLOCK_ELSE = 2, + CODE_BLOCK_WHILE = 3, + CODE_BLOCK_SCOPE = 4, +}; + +struct code_block { + enum code_block_type type; + uacpi_u32 begin, end; + union { + struct uacpi_namespace_node *node; + uacpi_u64 expiration_point; + }; +}; + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(code_block_array, struct code_block, 8) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + code_block_array, struct code_block, static +) + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(held_mutexes_array, uacpi_mutex*, 8) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + held_mutexes_array, uacpi_mutex*, static +) + +static uacpi_status held_mutexes_array_push( + struct held_mutexes_array *arr, uacpi_mutex *mutex +) +{ + uacpi_mutex **slot; + + slot = held_mutexes_array_alloc(arr); + if (uacpi_unlikely(slot == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + *slot = mutex; + uacpi_shareable_ref(mutex); + return UACPI_STATUS_OK; +} + +static void held_mutexes_array_remove_idx( + struct held_mutexes_array *arr, uacpi_size i +) +{ + uacpi_size size; + + size = held_mutexes_array_inline_capacity(arr); + + // Only the dynamic array part is affected + if (i >= size) { + i -= size; + size = arr->size_including_inline - size; + size -= i + 1; + + uacpi_memmove( + &arr->dynamic_storage[i], &arr->dynamic_storage[i + 1], + size * sizeof(arr->inline_storage[0]) + ); + + held_mutexes_array_pop(arr); + return; + } + + size = UACPI_MIN(held_mutexes_array_inline_capacity(arr), + arr->size_including_inline); + size -= i + 1; + uacpi_memmove( + &arr->inline_storage[i], &arr->inline_storage[i + 1], + size * sizeof(arr->inline_storage[0]) + ); + + size = held_mutexes_array_size(arr); + i = held_mutexes_array_inline_capacity(arr); + + /* + * This array has dynamic storage as well, now we have to take the first + * dynamic item, move it to the top of inline storage, and then shift all + * dynamic items backward by 1 as well. + */ + if (size > i) { + arr->inline_storage[i - 1] = arr->dynamic_storage[0]; + size -= i + 1; + + uacpi_memmove( + &arr->dynamic_storage[0], &arr->dynamic_storage[1], + size * sizeof(arr->inline_storage[0]) + ); + } + + held_mutexes_array_pop(arr); +} + +enum force_release { + FORCE_RELEASE_NO, + FORCE_RELEASE_YES, +}; + +static uacpi_status held_mutexes_array_remove_and_release( + struct held_mutexes_array *arr, uacpi_mutex *mutex, + enum force_release force +) +{ + uacpi_mutex *item; + uacpi_size i; + + if (uacpi_unlikely(held_mutexes_array_size(arr) == 0)) + return UACPI_STATUS_INVALID_ARGUMENT; + + item = *held_mutexes_array_last(arr); + + if (uacpi_unlikely(item->sync_level != mutex->sync_level && + force != FORCE_RELEASE_YES)) { + uacpi_warn( + "ignoring mutex @%p release due to sync level mismatch: %d vs %d\n", + mutex, mutex->sync_level, item->sync_level + ); + + // We still return OK because we don't want to abort because of this + return UACPI_STATUS_OK; + } + + if (mutex->depth > 1 && force == FORCE_RELEASE_NO) { + uacpi_release_aml_mutex(mutex); + return UACPI_STATUS_OK; + } + + // Fast path for well-behaved AML that releases mutexes in descending order + if (uacpi_likely(item == mutex)) { + held_mutexes_array_pop(arr); + goto do_release; + } + + /* + * The mutex being released is not the last one acquired, although we did + * verify that at least it has the same sync level. Anyway, now we have + * to search for it and then remove it from the array while shifting + * everything backwards. + */ + i = held_mutexes_array_size(arr); + for (;;) { + item = *held_mutexes_array_at(arr, --i); + if (item == mutex) + break; + + if (uacpi_unlikely(i == 0)) + return UACPI_STATUS_INVALID_ARGUMENT; + } + + held_mutexes_array_remove_idx(arr, i); + +do_release: + // This is either a force release, or depth was already 1 to begin with + mutex->depth = 1; + uacpi_release_aml_mutex(mutex); + + uacpi_mutex_unref(mutex); + return UACPI_STATUS_OK; +} + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE( + temp_namespace_node_array, uacpi_namespace_node*, 8) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + temp_namespace_node_array, uacpi_namespace_node*, static +) + +static uacpi_status temp_namespace_node_array_push( + struct temp_namespace_node_array *arr, uacpi_namespace_node *node +) +{ + uacpi_namespace_node **slot; + + slot = temp_namespace_node_array_alloc(arr); + if (uacpi_unlikely(slot == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + *slot = node; + return UACPI_STATUS_OK; +} + +struct call_frame { + struct uacpi_control_method *method; + + uacpi_object *args[7]; + uacpi_object *locals[8]; + + struct op_context_array pending_ops; + struct code_block_array code_blocks; + struct temp_namespace_node_array temp_nodes; + struct code_block *last_while; + uacpi_u64 prev_while_expiration; + uacpi_u32 prev_while_code_offset; + + uacpi_u32 code_offset; + + struct uacpi_namespace_node *cur_scope; + + // Only used if the method is serialized + uacpi_u8 prev_sync_level; +}; + +static void *call_frame_cursor(struct call_frame *frame) +{ + return frame->method->code + frame->code_offset; +} + +static uacpi_size call_frame_code_bytes_left(struct call_frame *frame) +{ + return frame->method->size - frame->code_offset; +} + +static uacpi_bool call_frame_has_code(struct call_frame *frame) +{ + return call_frame_code_bytes_left(frame) > 0; +} + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(call_frame_array, struct call_frame, 4) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + call_frame_array, struct call_frame, static +) + +static struct call_frame *call_frame_array_one_before_last( + struct call_frame_array *arr +) +{ + uacpi_size size; + + size = call_frame_array_size(arr); + + if (size < 2) + return UACPI_NULL; + + return call_frame_array_at(arr, size - 2); +} + +// NOTE: Try to keep size under 2 pages +struct execution_context { + uacpi_object *ret; + struct call_frame_array call_stack; + struct held_mutexes_array held_mutexes; + + struct call_frame *cur_frame; + struct code_block *cur_block; + const struct uacpi_op_spec *cur_op; + struct op_context *prev_op_ctx; + struct op_context *cur_op_ctx; + + uacpi_bool skip_else; + uacpi_u8 sync_level; +}; + +#define AML_READ(ptr, offset) (*(((uacpi_u8*)(ptr)) + offset)) + +static uacpi_status parse_nameseg(uacpi_u8 *cursor, + uacpi_object_name *out_name) +{ + if (uacpi_unlikely(!uacpi_is_valid_nameseg(cursor))) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + uacpi_memcpy(&out_name->id, cursor, 4); + return UACPI_STATUS_OK; +} + +/* + * ------------------------------------------------------------- + * RootChar := ‘\’ + * ParentPrefixChar := ‘^’ + * ‘\’ := 0x5C + * ‘^’ := 0x5E + * ------------------------------------------------------------ + * NameSeg := + * NameString := | + * PrefixPath := Nothing | <’^’ prefixpath> + * NamePath := NameSeg | DualNamePath | MultiNamePath | NullName + * DualNamePath := DualNamePrefix NameSeg NameSeg + * MultiNamePath := MultiNamePrefix SegCount NameSeg(SegCount) + */ + +static uacpi_status name_string_to_path( + struct call_frame *frame, uacpi_size offset, + uacpi_char **out_string, uacpi_size *out_size +) +{ + uacpi_size bytes_left, prefix_bytes, nameseg_bytes = 0, namesegs; + uacpi_char *base_cursor, *cursor; + uacpi_char prev_char; + + bytes_left = frame->method->size - offset; + cursor = (uacpi_char*)frame->method->code + offset; + base_cursor = cursor; + namesegs = 0; + + prefix_bytes = 0; + for (;;) { + if (uacpi_unlikely(bytes_left == 0)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + prev_char = *cursor; + + switch (prev_char) { + case '^': + case '\\': + prefix_bytes++; + cursor++; + bytes_left--; + break; + default: + break; + } + + if (prev_char != '^') + break; + } + + // At least a NullName byte is expected here + if (uacpi_unlikely(bytes_left == 0)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + namesegs = 0; + bytes_left--; + switch (*cursor++) + { + case UACPI_DUAL_NAME_PREFIX: + namesegs = 2; + break; + case UACPI_MULTI_NAME_PREFIX: + if (uacpi_unlikely(bytes_left == 0)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + namesegs = *(uacpi_u8*)cursor; + if (uacpi_unlikely(namesegs == 0)) { + uacpi_error("MultiNamePrefix but SegCount is 0\n"); + return UACPI_STATUS_AML_INVALID_NAMESTRING; + } + + cursor++; + bytes_left--; + break; + case UACPI_NULL_NAME: + break; + default: + /* + * Might be an invalid byte, but assume single nameseg for now, + * the code below will validate it for us. + */ + cursor--; + bytes_left++; + namesegs = 1; + break; + } + + if (uacpi_unlikely((namesegs * 4) > bytes_left)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + if (namesegs) { + // 4 chars per nameseg + nameseg_bytes = namesegs * 4; + + // dot separator for every nameseg + nameseg_bytes += namesegs - 1; + } + + *out_size = nameseg_bytes + prefix_bytes + 1; + + *out_string = uacpi_kernel_alloc(*out_size); + if (*out_string == UACPI_NULL) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(*out_string, base_cursor, prefix_bytes); + + base_cursor = *out_string; + base_cursor += prefix_bytes; + + while (namesegs-- > 0) { + uacpi_memcpy(base_cursor, cursor, 4); + cursor += 4; + base_cursor += 4; + + if (namesegs) + *base_cursor++ = '.'; + } + + *base_cursor = '\0'; + return UACPI_STATUS_OK; +} + +enum resolve_behavior { + RESOLVE_CREATE_LAST_NAMESEG_FAIL_IF_EXISTS, + RESOLVE_FAIL_IF_DOESNT_EXIST, +}; + +static uacpi_status resolve_name_string( + struct call_frame *frame, + enum resolve_behavior behavior, + struct uacpi_namespace_node **out_node +) +{ + uacpi_status ret = UACPI_STATUS_OK; + uacpi_u8 *cursor; + uacpi_size bytes_left, namesegs = 0; + struct uacpi_namespace_node *parent, *cur_node = frame->cur_scope; + uacpi_char prev_char = 0; + uacpi_bool just_one_nameseg = UACPI_TRUE; + + bytes_left = call_frame_code_bytes_left(frame); + cursor = call_frame_cursor(frame); + + for (;;) { + if (uacpi_unlikely(bytes_left == 0)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + switch (*cursor) { + case '\\': + if (prev_char == '^') + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + cur_node = uacpi_namespace_root(); + break; + case '^': + // Tried to go behind root + if (uacpi_unlikely(cur_node == uacpi_namespace_root())) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + cur_node = cur_node->parent; + break; + default: + break; + } + + prev_char = *cursor; + + switch (prev_char) { + case '^': + case '\\': + just_one_nameseg = UACPI_FALSE; + cursor++; + bytes_left--; + break; + default: + break; + } + + if (prev_char != '^') + break; + } + + // At least a NullName byte is expected here + if (uacpi_unlikely(bytes_left == 0)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + bytes_left--; + switch (*cursor++) + { + case UACPI_DUAL_NAME_PREFIX: + namesegs = 2; + just_one_nameseg = UACPI_FALSE; + break; + case UACPI_MULTI_NAME_PREFIX: + if (uacpi_unlikely(bytes_left == 0)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + namesegs = *cursor; + if (uacpi_unlikely(namesegs == 0)) { + uacpi_error("MultiNamePrefix but SegCount is 0\n"); + return UACPI_STATUS_AML_INVALID_NAMESTRING; + } + + cursor++; + bytes_left--; + just_one_nameseg = UACPI_FALSE; + break; + case UACPI_NULL_NAME: + if (behavior == RESOLVE_CREATE_LAST_NAMESEG_FAIL_IF_EXISTS || + just_one_nameseg) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + goto out; + default: + /* + * Might be an invalid byte, but assume single nameseg for now, + * the code below will validate it for us. + */ + cursor--; + bytes_left++; + namesegs = 1; + break; + } + + if (uacpi_unlikely((namesegs * 4) > bytes_left)) + return UACPI_STATUS_AML_INVALID_NAMESTRING; + + for (; namesegs; cursor += 4, namesegs--) { + uacpi_object_name name; + + ret = parse_nameseg(cursor, &name); + if (uacpi_unlikely_error(ret)) + return ret; + + parent = cur_node; + cur_node = uacpi_namespace_node_find_sub_node(parent, name); + + switch (behavior) { + case RESOLVE_CREATE_LAST_NAMESEG_FAIL_IF_EXISTS: + if (namesegs == 1) { + if (cur_node) { + cur_node = UACPI_NULL; + ret = UACPI_STATUS_AML_OBJECT_ALREADY_EXISTS; + goto out; + } + + // Create the node and link to parent but don't install YET + cur_node = uacpi_namespace_node_alloc(name); + cur_node->parent = parent; + } + break; + case RESOLVE_FAIL_IF_DOESNT_EXIST: + if (just_one_nameseg) { + while (!cur_node && parent != uacpi_namespace_root()) { + cur_node = parent; + parent = cur_node->parent; + + cur_node = uacpi_namespace_node_find_sub_node(parent, name); + } + } + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (cur_node == UACPI_NULL) { + ret = UACPI_STATUS_NOT_FOUND; + break; + } + } + +out: + cursor += namesegs * 4; + frame->code_offset = cursor - frame->method->code; + + if (uacpi_likely_success(ret) && behavior == RESOLVE_FAIL_IF_DOESNT_EXIST) + uacpi_shareable_ref(cur_node); + + *out_node = cur_node; + return ret; +} + +static uacpi_status do_install_node_item(struct call_frame *frame, + struct item *item) +{ + uacpi_status ret; + + ret = uacpi_namespace_node_install(item->node->parent, item->node); + if (uacpi_unlikely_error(ret)) + return ret; + + if (!frame->method->named_objects_persist) + ret = temp_namespace_node_array_push(&frame->temp_nodes, item->node); + + if (uacpi_likely_success(ret)) + item->node = UACPI_NULL; + + return ret; +} + +static uacpi_status get_op(struct execution_context *ctx) +{ + uacpi_aml_op op; + struct call_frame *frame = ctx->cur_frame; + void *code = frame->method->code; + uacpi_size size = frame->method->size; + + if (uacpi_unlikely(frame->code_offset >= size)) + return UACPI_STATUS_AML_BAD_ENCODING; + + op = AML_READ(code, frame->code_offset++); + if (op == UACPI_EXT_PREFIX) { + if (uacpi_unlikely(frame->code_offset >= size)) + return UACPI_STATUS_AML_BAD_ENCODING; + + op <<= 8; + op |= AML_READ(code, frame->code_offset++); + } + + g_uacpi_rt_ctx.opcodes_executed++; + + ctx->cur_op = uacpi_get_op_spec(op); + if (uacpi_unlikely(ctx->cur_op->properties & UACPI_OP_PROPERTY_RESERVED)) { + uacpi_error( + "invalid opcode '%s' encountered in bytestream\n", + ctx->cur_op->name + ); + return UACPI_STATUS_AML_INVALID_OPCODE; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_buffer(struct execution_context *ctx) +{ + struct package_length *pkg; + uacpi_u8 *src; + uacpi_object *dst, *declared_size; + uacpi_u32 buffer_size, init_size, aml_offset; + struct op_context *op_ctx = ctx->cur_op_ctx; + + aml_offset = item_array_at(&op_ctx->items, 2)->immediate; + src = ctx->cur_frame->method->code; + src += aml_offset; + + pkg = &item_array_at(&op_ctx->items, 0)->pkg; + init_size = pkg->end - aml_offset; + + // TODO: do package bounds checking at parse time + if (uacpi_unlikely(pkg->end > ctx->cur_frame->method->size)) + return UACPI_STATUS_AML_BAD_ENCODING; + + declared_size = item_array_at(&op_ctx->items, 1)->obj; + + if (uacpi_unlikely(declared_size->integer > 0xE0000000)) { + uacpi_error( + "buffer is too large (%"UACPI_PRIu64"), assuming corrupted " + "bytestream\n", UACPI_FMT64(declared_size->integer) + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + if (uacpi_unlikely(declared_size->integer == 0)) { + uacpi_error("attempted to create an empty buffer\n"); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + buffer_size = declared_size->integer; + if (uacpi_unlikely(init_size > buffer_size)) { + uacpi_error( + "too many buffer initializers: %u (size is %u)\n", + init_size, buffer_size + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + dst = item_array_at(&op_ctx->items, 3)->obj; + dst->buffer->data = uacpi_kernel_alloc(buffer_size); + if (uacpi_unlikely(dst->buffer->data == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + dst->buffer->size = buffer_size; + + uacpi_memcpy_zerout(dst->buffer->data, src, buffer_size, init_size); + return UACPI_STATUS_OK; +} + +static uacpi_status handle_string(struct execution_context *ctx) +{ + struct call_frame *frame = ctx->cur_frame; + uacpi_object *obj; + + uacpi_char *string; + uacpi_size length, max_bytes; + + obj = item_array_last(&ctx->cur_op_ctx->items)->obj; + string = call_frame_cursor(frame); + + // TODO: sanitize string for valid UTF-8 + max_bytes = call_frame_code_bytes_left(frame); + length = uacpi_strnlen(string, max_bytes); + + if (uacpi_unlikely((length == max_bytes) || (string[length++] != 0x00))) + return UACPI_STATUS_AML_BAD_ENCODING; + + obj->buffer->text = uacpi_kernel_alloc(length); + if (uacpi_unlikely(obj->buffer->text == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(obj->buffer->text, string, length); + obj->buffer->size = length; + frame->code_offset += length; + return UACPI_STATUS_OK; +} + +static uacpi_status handle_package(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_package *package; + uacpi_u32 num_elements, num_defined_elements, i; + + /* + * Layout of items here: + * [0] -> Package length, not interesting + * [1] -> Immediate or integer object, depending on PackageOp/VarPackageOp + * [2..N-2] -> AML pc+Package element pairs + * [N-1] -> The resulting package object that we're constructing + */ + package = item_array_last(&op_ctx->items)->obj->package; + + // 1. Detect how many elements we have, do sanity checking + if (op_ctx->op->code == UACPI_AML_OP_VarPackageOp) { + uacpi_object *var_num_elements; + + var_num_elements = item_array_at(&op_ctx->items, 1)->obj; + if (uacpi_unlikely(var_num_elements->integer > 0xE0000000)) { + uacpi_error( + "package is too large (%"UACPI_PRIu64"), assuming " + "corrupted bytestream\n", UACPI_FMT64(var_num_elements->integer) + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + num_elements = var_num_elements->integer; + } else { + num_elements = item_array_at(&op_ctx->items, 1)->immediate; + } + + num_defined_elements = (item_array_size(&op_ctx->items) - 3) / 2; + if (uacpi_unlikely(num_defined_elements > num_elements)) { + uacpi_warn( + "too many package initializers: %u, truncating to %u\n", + num_defined_elements, num_elements + ); + + num_defined_elements = num_elements; + } + + // 2. Create every object in the package, start as uninitialized + if (uacpi_unlikely(!uacpi_package_fill(package, num_elements, + UACPI_PREALLOC_OBJECTS_YES))) + return UACPI_STATUS_OUT_OF_MEMORY; + + // 3. Go through every defined object and copy it into the package + for (i = 0; i < num_defined_elements; ++i) { + uacpi_size base_pkg_index; + uacpi_status ret; + struct item *item; + uacpi_object *obj; + + base_pkg_index = (i * 2) + 2; + item = item_array_at(&op_ctx->items, base_pkg_index + 1); + obj = item->obj; + + if (obj != UACPI_NULL && obj->type == UACPI_OBJECT_REFERENCE) { + /* + * For named objects we don't actually need the object itself, but + * simply the path to it. Often times objects referenced by the + * package are not defined until later so it's not possible to + * resolve them. For uniformity and to follow the behavior of NT, + * simply convert the name string to a path string object to be + * resolved later when actually needed. + */ + if (obj->flags == UACPI_REFERENCE_KIND_NAMED) { + uacpi_object_unref(obj); + item->obj = UACPI_NULL; + obj = UACPI_NULL; + } else { + obj = uacpi_unwrap_internal_reference(obj); + } + } + + if (obj == UACPI_NULL) { + uacpi_size length; + uacpi_char *path; + + obj = uacpi_create_object(UACPI_OBJECT_STRING); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = name_string_to_path( + ctx->cur_frame, + item_array_at(&op_ctx->items, base_pkg_index)->immediate, + &path, &length + ); + if (uacpi_unlikely_error(ret)) + return ret; + + obj->flags = UACPI_STRING_KIND_PATH; + obj->buffer->text = path; + obj->buffer->size = length; + + item->obj = obj; + item->type = ITEM_OBJECT; + } + + ret = uacpi_object_assign(package->objects[i], obj, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY); + if (uacpi_unlikely_error(ret)) + return ret; + } + + return UACPI_STATUS_OK; +} + +static uacpi_size field_byte_size(uacpi_object *obj) +{ + uacpi_size bit_length; + + if (obj->type == UACPI_OBJECT_BUFFER_FIELD) + bit_length = obj->buffer_field.bit_length; + else + bit_length = obj->field_unit->bit_length; + + return uacpi_round_up_bits_to_bytes(bit_length); +} + +static uacpi_size sizeof_int(void) +{ + return g_uacpi_rt_ctx.is_rev1 ? 4 : 8; +} + +struct object_storage_as_buffer { + void *ptr; + uacpi_size len; +}; + +static uacpi_status get_object_storage(uacpi_object *obj, + struct object_storage_as_buffer *out_buf, + uacpi_bool include_null) +{ + switch (obj->type) { + case UACPI_OBJECT_INTEGER: + out_buf->len = sizeof_int(); + out_buf->ptr = &obj->integer; + break; + case UACPI_OBJECT_STRING: + out_buf->len = obj->buffer->size; + if (out_buf->len && !include_null) + out_buf->len--; + + out_buf->ptr = obj->buffer->text; + break; + case UACPI_OBJECT_BUFFER: + if (obj->buffer->size == 0) { + out_buf->ptr = UACPI_NULL; + out_buf->len = 0; + break; + } + + out_buf->len = obj->buffer->size; + out_buf->ptr = obj->buffer->data; + break; + case UACPI_OBJECT_REFERENCE: + return UACPI_STATUS_INVALID_ARGUMENT; + default: + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return UACPI_STATUS_OK; +} + +static uacpi_u8 *buffer_index_cursor(uacpi_buffer_index *buf_idx) +{ + uacpi_u8 *out_cursor; + + out_cursor = buf_idx->buffer->data; + out_cursor += buf_idx->idx; + + return out_cursor; +} + +static void write_buffer_index(uacpi_buffer_index *buf_idx, + struct object_storage_as_buffer *src_buf) +{ + uacpi_memcpy_zerout(buffer_index_cursor(buf_idx), src_buf->ptr, + 1, src_buf->len); +} + +/* + * The word "implicit cast" here is only because it's called that in + * the specification. In reality, we just copy one buffer to another + * because that's what NT does. + */ +static uacpi_status object_assign_with_implicit_cast(uacpi_object *dst, + uacpi_object *src) +{ + uacpi_status ret; + struct object_storage_as_buffer src_buf; + + ret = get_object_storage(src, &src_buf, UACPI_FALSE); + if (uacpi_unlikely_error(ret)) + goto out_bad_cast; + + switch (dst->type) { + case UACPI_OBJECT_INTEGER: + case UACPI_OBJECT_STRING: + case UACPI_OBJECT_BUFFER: { + struct object_storage_as_buffer dst_buf; + + ret = get_object_storage(dst, &dst_buf, UACPI_FALSE); + if (uacpi_unlikely_error(ret)) + goto out_bad_cast; + + uacpi_memcpy_zerout(dst_buf.ptr, src_buf.ptr, dst_buf.len, src_buf.len); + break; + } + + case UACPI_OBJECT_BUFFER_FIELD: + uacpi_write_buffer_field(&dst->buffer_field, src_buf.ptr, src_buf.len); + break; + + case UACPI_OBJECT_FIELD_UNIT: + return uacpi_write_field_unit( + dst->field_unit, src_buf.ptr, src_buf.len + ); + + case UACPI_OBJECT_BUFFER_INDEX: + write_buffer_index(&dst->buffer_index, &src_buf); + break; + + default: + ret = UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + goto out_bad_cast; + } + + return ret; + +out_bad_cast: + uacpi_error( + "attempted to perform an invalid implicit cast (%s -> %s)\n", + uacpi_object_type_to_string(src->type), + uacpi_object_type_to_string(dst->type) + ); + return ret; +} + +enum argx_or_localx { + ARGX, + LOCALX, +}; + +static uacpi_status handle_arg_or_local( + struct execution_context *ctx, + uacpi_size idx, enum argx_or_localx type +) +{ + uacpi_object **src; + struct item *dst; + enum uacpi_reference_kind kind; + + if (type == ARGX) { + src = &ctx->cur_frame->args[idx]; + kind = UACPI_REFERENCE_KIND_ARG; + } else { + src = &ctx->cur_frame->locals[idx]; + kind = UACPI_REFERENCE_KIND_LOCAL; + } + + if (*src == UACPI_NULL) { + uacpi_object *default_value; + + default_value = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(default_value == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + *src = uacpi_create_internal_reference(kind, default_value); + if (uacpi_unlikely(*src == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_object_unref(default_value); + } + + dst = item_array_last(&ctx->cur_op_ctx->items); + dst->obj = *src; + dst->type = ITEM_OBJECT; + uacpi_object_ref(dst->obj); + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_local(struct execution_context *ctx) +{ + uacpi_size idx; + struct op_context *op_ctx = ctx->cur_op_ctx; + + idx = op_ctx->op->code - UACPI_AML_OP_Local0Op; + return handle_arg_or_local(ctx, idx, LOCALX); +} + +static uacpi_status handle_arg(struct execution_context *ctx) +{ + uacpi_size idx; + struct op_context *op_ctx = ctx->cur_op_ctx; + + idx = op_ctx->op->code - UACPI_AML_OP_Arg0Op; + return handle_arg_or_local(ctx, idx, ARGX); +} + +static uacpi_status handle_named_object(struct execution_context *ctx) +{ + struct uacpi_namespace_node *src; + struct item *dst; + + src = item_array_at(&ctx->cur_op_ctx->items, 0)->node; + dst = item_array_at(&ctx->cur_op_ctx->items, 1); + + dst->obj = src->object; + dst->type = ITEM_OBJECT; + uacpi_object_ref(dst->obj); + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_create_alias(struct execution_context *ctx) +{ + uacpi_namespace_node *src, *dst; + + src = item_array_at(&ctx->cur_op_ctx->items, 0)->node; + dst = item_array_at(&ctx->cur_op_ctx->items, 1)->node; + + dst->object = src->object; + dst->flags = UACPI_NAMESPACE_NODE_FLAG_ALIAS; + uacpi_object_ref(dst->object); + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_create_op_region(struct execution_context *ctx) +{ + uacpi_namespace_node *node; + uacpi_object *obj; + uacpi_operation_region *op_region; + uacpi_u64 region_end; + + node = item_array_at(&ctx->cur_op_ctx->items, 0)->node; + obj = item_array_at(&ctx->cur_op_ctx->items, 4)->obj; + op_region = obj->op_region; + + op_region->space = item_array_at(&ctx->cur_op_ctx->items, 1)->immediate; + op_region->offset = item_array_at(&ctx->cur_op_ctx->items, 2)->obj->integer; + op_region->length = item_array_at(&ctx->cur_op_ctx->items, 3)->obj->integer; + region_end = op_region->offset + op_region->length; + + if (uacpi_unlikely(op_region->length == 0)) { + // Don't abort here, as long as it's never accessed we don't care + uacpi_warn("unusable/empty operation region %.4s\n", node->name.text); + } else if (uacpi_unlikely(op_region->offset > region_end)) { + uacpi_error( + "invalid operation region %.4s bounds: offset=0x%"UACPI_PRIX64 + " length=0x%"UACPI_PRIX64"\n", node->name.text, + UACPI_FMT64(op_region->offset), UACPI_FMT64(op_region->length) + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + node->object = uacpi_create_internal_reference( + UACPI_REFERENCE_KIND_NAMED, obj + ); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_initialize_opregion_node(node); + return UACPI_STATUS_OK; +} + +static uacpi_status table_id_error( + const uacpi_char *opcode, const uacpi_char *arg, + uacpi_buffer *str +) +{ + uacpi_error("%s: invalid %s '%s'\n", opcode, arg, str->text); + return UACPI_STATUS_AML_BAD_ENCODING; +} + +static void report_table_id_find_error( + const uacpi_char *opcode, struct uacpi_table_identifiers *id, + uacpi_status ret +) +{ + uacpi_error( + "%s: unable to find table '%.4s' (OEM ID '%.6s', " + "OEM Table ID '%.8s'): %s\n", + opcode, id->signature.text, id->oemid, id->oem_table_id, + uacpi_status_to_string(ret) + ); +} + +static uacpi_status build_table_id( + const uacpi_char *opcode, + struct uacpi_table_identifiers *out_id, + uacpi_buffer *signature, uacpi_buffer *oem_id, + uacpi_buffer *oem_table_id +) +{ + if (uacpi_unlikely(signature->size != (sizeof(uacpi_object_name) + 1))) + return table_id_error(opcode, "SignatureString", signature); + + uacpi_memcpy(out_id->signature.text, signature->text, + sizeof(uacpi_object_name)); + + if (uacpi_unlikely(oem_id->size > (sizeof(out_id->oemid) + 1))) + return table_id_error(opcode, "OemIDString", oem_id); + + uacpi_memcpy_zerout( + out_id->oemid, oem_id->text, + sizeof(out_id->oemid), oem_id->size ? oem_id->size - 1 : 0 + ); + + if (uacpi_unlikely(oem_table_id->size > (sizeof(out_id->oem_table_id) + 1))) + return table_id_error(opcode, "OemTableIDString", oem_table_id); + + uacpi_memcpy_zerout( + out_id->oem_table_id, oem_table_id->text, + sizeof(out_id->oem_table_id), + oem_table_id->size ? oem_table_id->size - 1 : 0 + ); + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_create_data_region(struct execution_context *ctx) +{ + uacpi_status ret; + struct item_array *items = &ctx->cur_op_ctx->items; + struct uacpi_table_identifiers table_id; + uacpi_table table; + uacpi_namespace_node *node; + uacpi_object *obj; + uacpi_operation_region *op_region; + + node = item_array_at(items, 0)->node; + + ret = build_table_id( + "DataTableRegion", &table_id, + item_array_at(items, 1)->obj->buffer, + item_array_at(items, 2)->obj->buffer, + item_array_at(items, 3)->obj->buffer + ); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_table_find(&table_id, &table); + if (uacpi_unlikely_error(ret)) { + report_table_id_find_error("DataTableRegion", &table_id, ret); + return ret; + } + + obj = item_array_at(items, 4)->obj; + op_region = obj->op_region; + op_region->space = UACPI_ADDRESS_SPACE_TABLE_DATA; + op_region->offset = table.virt_addr; + op_region->length = table.hdr->length; + op_region->table_idx = table.index; + + node->object = uacpi_create_internal_reference( + UACPI_REFERENCE_KIND_NAMED, obj + ); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_initialize_opregion_node(node); + return UACPI_STATUS_OK; +} + +static uacpi_bool is_dynamic_table_load(enum uacpi_table_load_cause cause) +{ + return cause != UACPI_TABLE_LOAD_CAUSE_INIT; +} + +static void prepare_table_load( + void *ptr, enum uacpi_table_load_cause cause, uacpi_control_method *in_method +) +{ + struct acpi_dsdt *dsdt = ptr; + enum uacpi_log_level log_level = UACPI_LOG_TRACE; + const uacpi_char *log_prefix = "load of"; + + if (is_dynamic_table_load(cause)) { + log_prefix = cause == UACPI_TABLE_LOAD_CAUSE_HOST ? + "host-invoked load of" : "dynamic load of"; + log_level = UACPI_LOG_INFO; + } + + uacpi_log_lvl( + log_level, "%s "UACPI_PRI_TBL_HDR"\n", + log_prefix, UACPI_FMT_TBL_HDR(&dsdt->hdr) + ); + + in_method->code = dsdt->definition_block; + in_method->size = dsdt->hdr.length - sizeof(dsdt->hdr); + in_method->named_objects_persist = UACPI_TRUE; +} + +static uacpi_status do_load_table( + uacpi_namespace_node *parent, struct acpi_sdt_hdr *tbl, + enum uacpi_table_load_cause cause +) +{ + struct uacpi_control_method method = { 0 }; + uacpi_status ret; + + prepare_table_load(tbl, cause, &method); + + ret = uacpi_execute_control_method(parent, &method, UACPI_NULL, UACPI_NULL); + if (uacpi_unlikely_error(ret)) + return ret; + + if (is_dynamic_table_load(cause)) + uacpi_events_match_post_dynamic_table_load(); + + return ret; +} + +static uacpi_status handle_load_table(struct execution_context *ctx) +{ + uacpi_status ret; + struct item_array *items = &ctx->cur_op_ctx->items; + struct item *root_node_item; + struct uacpi_table_identifiers table_id; + uacpi_table table; + uacpi_buffer *root_path, *param_path; + uacpi_control_method *method; + uacpi_namespace_node *root_node, *param_node = UACPI_NULL; + + /* + * If we already have the last true/false object loaded, this is a second + * invocation of this handler. For the second invocation we want to detect + * new AML GPE handlers that might've been loaded, as well as potentially + * remove the target. + */ + if (item_array_size(items) == 12) { + uacpi_size idx; + + idx = item_array_at(items, 2)->immediate; + uacpi_table_unref(&(struct uacpi_table) { .index = idx }); + + /* + * If this load failed, remove the target that was provided via + * ParameterPathString so that it doesn't get stored to. + */ + if (uacpi_unlikely(item_array_at(items, 11)->obj->integer == 0)) { + uacpi_object *target; + + target = item_array_at(items, 3)->obj; + if (target != UACPI_NULL) { + uacpi_object_unref(target); + item_array_at(items, 3)->obj = UACPI_NULL; + } + + return UACPI_STATUS_OK; + } + + uacpi_events_match_post_dynamic_table_load(); + return UACPI_STATUS_OK; + } + + ret = build_table_id( + "LoadTable", &table_id, + item_array_at(items, 5)->obj->buffer, + item_array_at(items, 6)->obj->buffer, + item_array_at(items, 7)->obj->buffer + ); + if (uacpi_unlikely_error(ret)) + return ret; + + root_path = item_array_at(items, 8)->obj->buffer; + param_path = item_array_at(items, 9)->obj->buffer; + root_node_item = item_array_at(items, 0); + + if (root_path->size > 1) { + ret = uacpi_namespace_node_resolve( + ctx->cur_frame->cur_scope, root_path->text, UACPI_SHOULD_LOCK_NO, + UACPI_MAY_SEARCH_ABOVE_PARENT_YES, UACPI_PERMANENT_ONLY_NO, + &root_node + ); + if (uacpi_unlikely_error(ret)) { + table_id_error("LoadTable", "RootPathString", root_path); + if (ret == UACPI_STATUS_NOT_FOUND) + ret = UACPI_STATUS_AML_UNDEFINED_REFERENCE; + return ret; + } + } else { + root_node = uacpi_namespace_root(); + } + + root_node_item->node = root_node; + root_node_item->type = ITEM_NAMESPACE_NODE; + uacpi_shareable_ref(root_node); + + if (param_path->size > 1) { + struct item *param_item; + + ret = uacpi_namespace_node_resolve( + root_node, param_path->text, UACPI_SHOULD_LOCK_NO, + UACPI_MAY_SEARCH_ABOVE_PARENT_YES, UACPI_PERMANENT_ONLY_NO, + ¶m_node + ); + if (uacpi_unlikely_error(ret)) { + table_id_error("LoadTable", "ParameterPathString", root_path); + if (ret == UACPI_STATUS_NOT_FOUND) + ret = UACPI_STATUS_AML_UNDEFINED_REFERENCE; + return ret; + } + + param_item = item_array_at(items, 3); + param_item->obj = param_node->object; + uacpi_object_ref(param_item->obj); + param_item->type = ITEM_OBJECT; + } + + ret = uacpi_table_find(&table_id, &table); + if (uacpi_unlikely_error(ret)) { + report_table_id_find_error("LoadTable", &table_id, ret); + return ret; + } + uacpi_table_mark_as_loaded(table.index); + + item_array_at(items, 2)->immediate = table.index; + method = item_array_at(items, 1)->obj->method; + prepare_table_load(table.hdr, UACPI_TABLE_LOAD_CAUSE_LOAD_TABLE_OP, method); + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_load(struct execution_context *ctx) +{ + uacpi_status ret; + struct item_array *items = &ctx->cur_op_ctx->items; + uacpi_table table; + uacpi_control_method *method; + uacpi_object *src; + struct acpi_sdt_hdr *src_table; + void *table_buffer; + uacpi_size declared_size; + uacpi_bool unmap_src = UACPI_FALSE; + + /* + * If we already have the last true/false object loaded, this is a second + * invocation of this handler. For the second invocation we simply want to + * detect new AML GPE handlers that might've been loaded. + * We do this only if table load was successful though. + */ + if (item_array_size(items) == 5) { + if (item_array_at(items, 4)->obj->integer != 0) + uacpi_events_match_post_dynamic_table_load(); + return UACPI_STATUS_OK; + } + + src = item_array_at(items, 2)->obj; + + switch (src->type) { + case UACPI_OBJECT_OPERATION_REGION: { + uacpi_operation_region *op_region; + + op_region = src->op_region; + if (uacpi_unlikely( + op_region->space != UACPI_ADDRESS_SPACE_SYSTEM_MEMORY + )) { + uacpi_error("Load: operation region is not SystemMemory\n"); + goto error_out; + } + + if (uacpi_unlikely(op_region->length < sizeof(struct acpi_sdt_hdr))) { + uacpi_error( + "Load: operation region is too small: %"UACPI_PRIu64"\n", + UACPI_FMT64(op_region->length) + ); + goto error_out; + } + + src_table = uacpi_kernel_map(op_region->offset, op_region->length); + if (uacpi_unlikely(src_table == UACPI_NULL)) { + uacpi_error( + "Load: failed to map operation region " + "0x%016"UACPI_PRIX64" -> 0x%016"UACPI_PRIX64"\n", + UACPI_FMT64(op_region->offset), + UACPI_FMT64(op_region->offset + op_region->length) + ); + goto error_out; + } + + unmap_src = UACPI_TRUE; + declared_size = op_region->length; + break; + } + + case UACPI_OBJECT_BUFFER: { + uacpi_buffer *buffer; + + buffer = src->buffer; + if (buffer->size < sizeof(struct acpi_sdt_hdr)) { + uacpi_error( + "Load: buffer is too small: %zu\n", + buffer->size + ); + goto error_out; + } + + src_table = buffer->data; + declared_size = buffer->size; + break; + } + + default: + uacpi_error( + "Load: invalid argument '%s', expected " + "Buffer/Field/OperationRegion\n", + uacpi_object_type_to_string(src->type) + ); + goto error_out; + } + + if (uacpi_unlikely(src_table->length > declared_size)) { + uacpi_error( + "Load: table size %u is larger than the declared size %zu\n", + src_table->length, declared_size + ); + goto error_out; + } + + if (uacpi_unlikely(src_table->length < sizeof(struct acpi_sdt_hdr))) { + uacpi_error("Load: table size %u is too small\n", src_table->length); + goto error_out; + } + + table_buffer = uacpi_kernel_alloc(src_table->length); + if (uacpi_unlikely(table_buffer == UACPI_NULL)) + goto error_out; + + uacpi_memcpy(table_buffer, src_table, src_table->length); + + if (unmap_src) { + uacpi_kernel_unmap(src_table, declared_size); + unmap_src = UACPI_FALSE; + } + + ret = uacpi_table_install_with_origin( + table_buffer, UACPI_TABLE_ORIGIN_FIRMWARE_VIRTUAL, &table + ); + if (uacpi_unlikely_error(ret)) { + uacpi_free(table_buffer, src_table->length); + + if (ret != UACPI_STATUS_OVERRIDDEN) + goto error_out; + } + uacpi_table_mark_as_loaded(table.index); + + item_array_at(items, 0)->node = uacpi_namespace_root(); + + method = item_array_at(items, 1)->obj->method; + prepare_table_load(table.ptr, UACPI_TABLE_LOAD_CAUSE_LOAD_OP, method); + + return UACPI_STATUS_OK; + +error_out: + if (unmap_src && src_table) + uacpi_kernel_unmap(src_table, declared_size); + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_execute_table(void *tbl, enum uacpi_table_load_cause cause) +{ + uacpi_status ret; + + ret = uacpi_namespace_write_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = do_load_table(uacpi_namespace_root(), tbl, cause); + + uacpi_namespace_write_unlock(); + return ret; +} + +static uacpi_u32 get_field_length(struct item *item) +{ + struct package_length *pkg = &item->pkg; + return pkg->end - pkg->begin; +} + +struct field_specific_data { + uacpi_namespace_node *region; + struct uacpi_field_unit *field0; + struct uacpi_field_unit *field1; + uacpi_u64 value; +}; + +static uacpi_status ensure_is_a_field_unit(uacpi_namespace_node *node, + uacpi_field_unit **out_field) +{ + uacpi_object *obj; + + obj = uacpi_namespace_node_get_object(node); + if (obj->type != UACPI_OBJECT_FIELD_UNIT) { + uacpi_error( + "invalid argument: '%.4s' is not a field unit (%s)\n", + node->name.text, uacpi_object_type_to_string(obj->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + *out_field = obj->field_unit; + return UACPI_STATUS_OK; +} + +static uacpi_status ensure_is_an_op_region(uacpi_namespace_node *node, + uacpi_namespace_node **out_node) +{ + uacpi_object *obj; + + obj = uacpi_namespace_node_get_object(node); + if (obj->type != UACPI_OBJECT_OPERATION_REGION) { + uacpi_error( + "invalid argument: '%.4s' is not an operation region (%s)\n", + node->name.text, uacpi_object_type_to_string(obj->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + *out_node = node; + return UACPI_STATUS_OK; +} + +static uacpi_status handle_create_field(struct execution_context *ctx) +{ + uacpi_status ret; + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_namespace_node *node; + uacpi_object *obj, *connection_obj = UACPI_NULL; + struct field_specific_data field_data = { 0 }; + uacpi_size i = 1, bit_offset = 0; + + uacpi_u8 raw_value, access_type, lock_rule, update_rule; + uacpi_u8 access_attrib = 0, access_length = 0; + + switch (op_ctx->op->code) { + case UACPI_AML_OP_FieldOp: + node = item_array_at(&op_ctx->items, i++)->node; + ret = ensure_is_an_op_region(node, &field_data.region); + if (uacpi_unlikely_error(ret)) + return ret; + break; + + case UACPI_AML_OP_BankFieldOp: + node = item_array_at(&op_ctx->items, i++)->node; + ret = ensure_is_an_op_region(node, &field_data.region); + if (uacpi_unlikely_error(ret)) + return ret; + + node = item_array_at(&op_ctx->items, i++)->node; + ret = ensure_is_a_field_unit(node, &field_data.field0); + if (uacpi_unlikely_error(ret)) + return ret; + + field_data.value = item_array_at(&op_ctx->items, i++)->obj->integer; + break; + + case UACPI_AML_OP_IndexFieldOp: + node = item_array_at(&op_ctx->items, i++)->node; + ret = ensure_is_a_field_unit(node, &field_data.field0); + if (uacpi_unlikely_error(ret)) + return ret; + + node = item_array_at(&op_ctx->items, i++)->node; + ret = ensure_is_a_field_unit(node, &field_data.field1); + if (uacpi_unlikely_error(ret)) + return ret; + break; + + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + /* + * ByteData + * bit 0-3: AccessType + * 0 AnyAcc + * 1 ByteAcc + * 2 WordAcc + * 3 DWordAcc + * 4 QWordAcc + * 5 BufferAcc + * 6 Reserved + * 7-15 Reserved + * bit 4: LockRule + * 0 NoLock + * 1 Lock + * bit 5-6: UpdateRule + * 0 Preserve + * 1 WriteAsOnes + * 2 WriteAsZeros + * bit 7: Reserved (must be 0) + */ + raw_value = item_array_at(&op_ctx->items, i++)->immediate; + access_type = (raw_value >> 0) & 0b1111; + lock_rule = (raw_value >> 4) & 0b1; + update_rule = (raw_value >> 5) & 0b11; + + while (i < item_array_size(&op_ctx->items)) { + struct item *item; + item = item_array_at(&op_ctx->items, i++); + + // An actual field object + if (item->type == ITEM_NAMESPACE_NODE) { + uacpi_u32 length; + uacpi_field_unit *field; + + length = get_field_length(item_array_at(&op_ctx->items, i++)); + node = item->node; + + obj = item_array_at(&op_ctx->items, i++)->obj; + field = obj->field_unit; + + field->update_rule = update_rule; + field->lock_rule = lock_rule; + field->attributes = access_attrib; + field->access_length = access_length; + + /* + * 0 AnyAcc + * 1 ByteAcc + * 2 WordAcc + * 3 DWordAcc + * 4 QWordAcc + * 5 BufferAcc + * 6 Reserved + * 7-15 Reserved + */ + switch (access_type) { + case 0: + // TODO: optimize to calculate best access strategy + UACPI_FALLTHROUGH; + case 1: + case 5: + field->access_width_bytes = 1; + break; + case 2: + field->access_width_bytes = 2; + break; + case 3: + field->access_width_bytes = 4; + break; + case 4: + field->access_width_bytes = 8; + break; + default: + uacpi_error("invalid field '%.4s' access type %d\n", + node->name.text, access_type); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + field->bit_length = length; + + // FIXME: overflow, OOB, etc checks + field->byte_offset = UACPI_ALIGN_DOWN( + bit_offset / 8, + field->access_width_bytes, + uacpi_u32 + ); + + field->bit_offset_within_first_byte = bit_offset; + field->bit_offset_within_first_byte = + bit_offset & ((field->access_width_bytes * 8) - 1); + + switch (op_ctx->op->code) { + case UACPI_AML_OP_FieldOp: + field->region = field_data.region; + uacpi_shareable_ref(field->region); + + field->kind = UACPI_FIELD_UNIT_KIND_NORMAL; + break; + + case UACPI_AML_OP_BankFieldOp: + field->bank_region = field_data.region; + uacpi_shareable_ref(field->bank_region); + + field->bank_selection = field_data.field0; + uacpi_shareable_ref(field->bank_selection); + + field->bank_value = field_data.value; + field->kind = UACPI_FIELD_UNIT_KIND_BANK; + break; + + case UACPI_AML_OP_IndexFieldOp: + field->index = field_data.field0; + uacpi_shareable_ref(field->index); + + field->data = field_data.field1; + uacpi_shareable_ref(field->data); + + field->kind = UACPI_FIELD_UNIT_KIND_INDEX; + break; + + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + field->connection = connection_obj; + if (field->connection) + uacpi_object_ref(field->connection); + + node->object = uacpi_create_internal_reference( + UACPI_REFERENCE_KIND_NAMED, obj + ); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = do_install_node_item(ctx->cur_frame, item); + if (uacpi_unlikely_error(ret)) + return ret; + + bit_offset += length; + continue; + } + + // All other stuff + switch (item->immediate) { + // ReservedField := 0x00 PkgLength + case 0x00: + bit_offset += get_field_length(item_array_at(&op_ctx->items, i++)); + break; + + // AccessField := 0x01 AccessType AccessAttrib + // ExtendedAccessField := 0x03 AccessType ExtendedAccessAttrib AccessLength + case 0x01: + case 0x03: + raw_value = item_array_at(&op_ctx->items, i++)->immediate; + + access_type = raw_value & 0b1111; + access_attrib = (raw_value >> 6) & 0b11; + + raw_value = item_array_at(&op_ctx->items, i++)->immediate; + + /* + * Bits 7:6 + * 0 = AccessAttrib = Normal Access Attributes + * 1 = AccessAttrib = AttribBytes (x) + * 2 = AccessAttrib = AttribRawBytes (x) + * 3 = AccessAttrib = AttribRawProcessBytes (x) + * x is encoded as bits 0:7 of the AccessAttrib byte. + */ + if (access_attrib) { + switch (access_attrib) { + case 1: + access_attrib = UACPI_ACCESS_ATTRIBUTE_BYTES; + break; + case 2: + access_attrib = UACPI_ACCESS_ATTRIBUTE_RAW_BYTES; + break; + case 3: + access_attrib = UACPI_ACCESS_ATTRIBUTE_RAW_PROCESS_BYTES; + break; + } + + access_length = raw_value; + } else { // Normal access attributes + access_attrib = raw_value; + } + + if (item->immediate == 3) + access_length = item_array_at(&op_ctx->items, i++)->immediate; + break; + + // ConnectField := <0x02 NameString> | <0x02 BufferData> + case 0x02: + connection_obj = item_array_at(&op_ctx->items, i++)->obj; + break; + + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + } + + return UACPI_STATUS_OK; +} + +static void truncate_number_if_needed(uacpi_object *obj) +{ + if (!g_uacpi_rt_ctx.is_rev1) + return; + + obj->integer &= 0xFFFFFFFF; +} + +static uacpi_u64 ones(void) +{ + return g_uacpi_rt_ctx.is_rev1 ? 0xFFFFFFFF : 0xFFFFFFFFFFFFFFFF; +} + +static uacpi_status method_get_ret_target(struct execution_context *ctx, + uacpi_object **out_operand) +{ + uacpi_size depth; + + // Check if we're targeting the previous call frame + depth = call_frame_array_size(&ctx->call_stack); + if (depth > 1) { + struct op_context *op_ctx; + struct call_frame *frame; + + frame = call_frame_array_at(&ctx->call_stack, depth - 2); + depth = op_context_array_size(&frame->pending_ops); + + // Ok, no one wants the return value at call site. Discard it. + if (!depth) { + *out_operand = UACPI_NULL; + return UACPI_STATUS_OK; + } + + op_ctx = op_context_array_at(&frame->pending_ops, depth - 1); + + /* + * Prevent the table being dynamically loaded from attempting to return + * a value to the caller. This is unlikely to be ever encountered in the + * wild, but we should still guard against the possibility. + */ + if (uacpi_unlikely(op_ctx->op->code == UACPI_AML_OP_LoadOp || + op_ctx->op->code == UACPI_AML_OP_LoadTableOp)) { + *out_operand = UACPI_NULL; + return UACPI_STATUS_OK; + } + + *out_operand = item_array_last(&op_ctx->items)->obj; + return UACPI_STATUS_OK; + } + + return UACPI_STATUS_NOT_FOUND; +} + +static uacpi_status method_get_ret_object(struct execution_context *ctx, + uacpi_object **out_obj) +{ + uacpi_status ret; + + ret = method_get_ret_target(ctx, out_obj); + if (ret == UACPI_STATUS_NOT_FOUND) { + *out_obj = ctx->ret; + return UACPI_STATUS_OK; + } + if (ret != UACPI_STATUS_OK || *out_obj == UACPI_NULL) + return ret; + + *out_obj = uacpi_unwrap_internal_reference(*out_obj); + return UACPI_STATUS_OK; +} + +static struct code_block *find_last_block(struct code_block_array *blocks, + enum code_block_type type) +{ + uacpi_size i; + + i = code_block_array_size(blocks); + while (i-- > 0) { + struct code_block *block; + + block = code_block_array_at(blocks, i); + if (block->type == type) + return block; + } + + return UACPI_NULL; +} + +static void update_scope(struct call_frame *frame) +{ + struct code_block *block; + + block = find_last_block(&frame->code_blocks, CODE_BLOCK_SCOPE); + if (block == UACPI_NULL) { + frame->cur_scope = uacpi_namespace_root(); + return; + } + + frame->cur_scope = block->node; +} + +static uacpi_status begin_block_execution(struct execution_context *ctx) +{ + struct call_frame *cur_frame = ctx->cur_frame; + struct op_context *op_ctx = ctx->cur_op_ctx; + struct package_length *pkg; + struct code_block *block; + + block = code_block_array_alloc(&cur_frame->code_blocks); + if (uacpi_unlikely(block == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + pkg = &item_array_at(&op_ctx->items, 0)->pkg; + + switch (op_ctx->op->code) { + case UACPI_AML_OP_IfOp: + block->type = CODE_BLOCK_IF; + break; + case UACPI_AML_OP_ElseOp: + block->type = CODE_BLOCK_ELSE; + break; + case UACPI_AML_OP_WhileOp: + block->type = CODE_BLOCK_WHILE; + + if (pkg->begin == cur_frame->prev_while_code_offset) { + uacpi_u64 cur_ticks; + + cur_ticks = uacpi_kernel_get_nanoseconds_since_boot(); + + if (uacpi_unlikely(cur_ticks > block->expiration_point)) { + uacpi_error("loop time out after running for %u seconds\n", + g_uacpi_rt_ctx.loop_timeout_seconds); + code_block_array_pop(&cur_frame->code_blocks); + return UACPI_STATUS_AML_LOOP_TIMEOUT; + } + + block->expiration_point = cur_frame->prev_while_expiration; + } else { + /* + * Calculate the expiration point for this loop. + * If a loop is executed past this point, it will get aborted. + */ + block->expiration_point = uacpi_kernel_get_nanoseconds_since_boot(); + block->expiration_point += + g_uacpi_rt_ctx.loop_timeout_seconds * UACPI_NANOSECONDS_PER_SEC; + } + break; + case UACPI_AML_OP_ScopeOp: + case UACPI_AML_OP_DeviceOp: + case UACPI_AML_OP_ProcessorOp: + case UACPI_AML_OP_PowerResOp: + case UACPI_AML_OP_ThermalZoneOp: + // Disarm the tracked package so that we don't skip the Scope + op_ctx->tracked_pkg_idx = 0; + + block->type = CODE_BLOCK_SCOPE; + block->node = item_array_at(&op_ctx->items, 1)->node; + break; + default: + code_block_array_pop(&cur_frame->code_blocks); + return UACPI_STATUS_INVALID_ARGUMENT; + } + + // -1 because we want to re-evaluate at the start of the op next time + block->begin = pkg->begin - 1; + block->end = pkg->end; + ctx->cur_block = block; + + cur_frame->last_while = find_last_block(&cur_frame->code_blocks, + CODE_BLOCK_WHILE); + update_scope(cur_frame); + return UACPI_STATUS_OK; +} + +static void frame_reset_post_end_block(struct execution_context *ctx, + enum code_block_type type) +{ + struct call_frame *frame = ctx->cur_frame; + + if (type == CODE_BLOCK_WHILE) { + struct code_block *block = ctx->cur_block; + + // + 1 here to skip the WhileOp and get to the PkgLength + frame->prev_while_code_offset = block->begin + 1; + frame->prev_while_expiration = block->expiration_point; + } + + code_block_array_pop(&frame->code_blocks); + ctx->cur_block = code_block_array_last(&frame->code_blocks); + + if (type == CODE_BLOCK_WHILE) { + frame->last_while = find_last_block(&frame->code_blocks, type); + } else if (type == CODE_BLOCK_SCOPE) { + update_scope(frame); + } +} + +static void debug_store_no_recurse(const uacpi_char *prefix, uacpi_object *src) +{ + switch (src->type) { + case UACPI_OBJECT_UNINITIALIZED: + uacpi_trace("%s Uninitialized\n", prefix); + break; + case UACPI_OBJECT_STRING: + uacpi_trace("%s String => \"%s\"\n", prefix, src->buffer->text); + break; + case UACPI_OBJECT_INTEGER: + if (g_uacpi_rt_ctx.is_rev1) { + uacpi_trace( + "%s Integer => 0x%08X\n", prefix, (uacpi_u32)src->integer + ); + } else { + uacpi_trace( + "%s Integer => 0x%016"UACPI_PRIX64"\n", prefix, + UACPI_FMT64(src->integer) + ); + } + break; + case UACPI_OBJECT_REFERENCE: + uacpi_trace("%s Reference @%p => %p\n", prefix, src, src->inner_object); + break; + case UACPI_OBJECT_PACKAGE: + uacpi_trace( + "%s Package @%p (%p) (%zu elements)\n", + prefix, src, src->package, src->package->count + ); + break; + case UACPI_OBJECT_BUFFER: + uacpi_trace( + "%s Buffer @%p (%p) (%zu bytes)\n", + prefix, src, src->buffer, src->buffer->size + ); + break; + case UACPI_OBJECT_OPERATION_REGION: + uacpi_trace( + "%s OperationRegion (ASID %d) 0x%016"UACPI_PRIX64 + " -> 0x%016"UACPI_PRIX64"\n", prefix, + src->op_region->space, UACPI_FMT64(src->op_region->offset), + UACPI_FMT64(src->op_region->offset + src->op_region->length) + ); + break; + case UACPI_OBJECT_POWER_RESOURCE: + uacpi_trace( + "%s Power Resource %d %d\n", + prefix, src->power_resource.system_level, + src->power_resource.resource_order + ); + break; + case UACPI_OBJECT_PROCESSOR: + uacpi_trace( + "%s Processor[%d] 0x%08X (%d)\n", + prefix, src->processor->id, src->processor->block_address, + src->processor->block_length + ); + break; + case UACPI_OBJECT_BUFFER_INDEX: + uacpi_trace( + "%s Buffer Index %p[%zu] => 0x%02X\n", + prefix, src->buffer_index.buffer->data, src->buffer_index.idx, + *buffer_index_cursor(&src->buffer_index) + ); + break; + case UACPI_OBJECT_MUTEX: + uacpi_trace( + "%s Mutex @%p (%p => %p) sync level %d\n", + prefix, src, src->mutex, src->mutex->handle, + src->mutex->sync_level + ); + break; + case UACPI_OBJECT_METHOD: + uacpi_trace("%s Method @%p (%p)\n", prefix, src, src->method); + break; + default: + uacpi_trace( + "%s %s @%p\n", + prefix, uacpi_object_type_to_string(src->type), src + ); + } +} + +static uacpi_status debug_store(uacpi_object *src) +{ + /* + * Don't bother running the body if current log level is not set to trace. + * All DebugOp logging is done as TRACE exclusively. + */ + if (!uacpi_should_log(UACPI_LOG_TRACE)) + return UACPI_STATUS_OK; + + src = uacpi_unwrap_internal_reference(src); + + debug_store_no_recurse("[AML DEBUG]", src); + + if (src->type == UACPI_OBJECT_PACKAGE) { + uacpi_package *pkg = src->package; + uacpi_size i; + + for (i = 0; i < pkg->count; ++i) { + uacpi_object *obj = pkg->objects[i]; + if (obj->type == UACPI_OBJECT_REFERENCE && + obj->flags == UACPI_REFERENCE_KIND_PKG_INDEX) + obj = obj->inner_object; + + debug_store_no_recurse("Element:", obj); + } + } + + return UACPI_STATUS_OK; +} + +/* + * NOTE: this function returns the parent object + */ +static uacpi_object *reference_unwind(uacpi_object *obj) +{ + uacpi_object *parent = obj; + + while (obj) { + if (obj->type != UACPI_OBJECT_REFERENCE) + return parent; + + parent = obj; + obj = parent->inner_object; + } + + // This should be unreachable + return UACPI_NULL; +} + +static uacpi_iteration_decision opregion_try_detach_from_parent( + void *user, uacpi_namespace_node *node, uacpi_u32 node_depth +) +{ + uacpi_object *target_object = user; + UACPI_UNUSED(node_depth); + + if (node->object == target_object) { + uacpi_opregion_uninstall_handler(node); + return UACPI_ITERATION_DECISION_BREAK; + } + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +static void object_replace_child(uacpi_object *parent, uacpi_object *new_child) +{ + if (parent->flags == UACPI_REFERENCE_KIND_NAMED && + uacpi_object_is(parent->inner_object, UACPI_OBJECT_OPERATION_REGION)) { + + /* + * We're doing a CopyObject or similar to a namespace node that is an + * operation region. Try to find the parent node and manually detach + * the handler. + */ + opregion_try_detach_from_parent(parent, uacpi_namespace_root(), 0); + uacpi_namespace_do_for_each_child( + uacpi_namespace_root(), opregion_try_detach_from_parent, UACPI_NULL, + UACPI_OBJECT_OPERATION_REGION_BIT, UACPI_MAX_DEPTH_ANY, + UACPI_SHOULD_LOCK_NO, UACPI_PERMANENT_ONLY_NO, parent + ); + } + + uacpi_object_detach_child(parent); + uacpi_object_attach_child(parent, new_child); +} + +/* + * Breakdown of what happens here: + * + * CopyObject(..., Obj) where Obj is: + * 1. LocalX -> Overwrite LocalX. + * 2. NAME -> Overwrite NAME. + * 3. ArgX -> Overwrite ArgX unless ArgX is a reference, in that case + * overwrite the referenced object. + * 4. RefOf -> Not allowed here. + * 5. Index -> Overwrite Object stored at the index. + */ + static uacpi_status copy_object_to_reference(uacpi_object *dst, + uacpi_object *src) +{ + uacpi_status ret; + uacpi_object *src_obj, *new_obj; + + switch (dst->flags) { + case UACPI_REFERENCE_KIND_ARG: { + uacpi_object *referenced_obj; + + referenced_obj = uacpi_unwrap_internal_reference(dst); + if (referenced_obj->type == UACPI_OBJECT_REFERENCE) { + dst = reference_unwind(referenced_obj); + break; + } + + UACPI_FALLTHROUGH; + } + case UACPI_REFERENCE_KIND_LOCAL: + case UACPI_REFERENCE_KIND_PKG_INDEX: + case UACPI_REFERENCE_KIND_NAMED: + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + src_obj = uacpi_unwrap_internal_reference(src); + + new_obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(new_obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = uacpi_object_assign(new_obj, src_obj, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY); + if (uacpi_unlikely_error(ret)) + return ret; + + object_replace_child(dst, new_obj); + uacpi_object_unref(new_obj); + + return UACPI_STATUS_OK; +} + +/* + * if Store(..., Obj) where Obj is: + * 1. LocalX/Index -> OVERWRITE unless the object is a reference, in that + * case store to the referenced object _with_ implicit + * cast. + * 2. ArgX -> OVERWRITE unless the object is a reference, in that + * case OVERWRITE the referenced object. + * 3. NAME -> Store with implicit cast. + * 4. RefOf -> Not allowed here. + */ +static uacpi_status store_to_reference(uacpi_object *dst, + uacpi_object *src) +{ + uacpi_object *src_obj; + uacpi_bool overwrite = UACPI_FALSE; + + switch (dst->flags) { + case UACPI_REFERENCE_KIND_LOCAL: + case UACPI_REFERENCE_KIND_ARG: + case UACPI_REFERENCE_KIND_PKG_INDEX: { + uacpi_object *referenced_obj; + + if (dst->flags == UACPI_REFERENCE_KIND_PKG_INDEX) + referenced_obj = dst->inner_object; + else + referenced_obj = uacpi_unwrap_internal_reference(dst); + + if (referenced_obj->type == UACPI_OBJECT_REFERENCE) { + overwrite = dst->flags == UACPI_REFERENCE_KIND_ARG; + dst = reference_unwind(referenced_obj); + break; + } + + overwrite = UACPI_TRUE; + break; + } + case UACPI_REFERENCE_KIND_NAMED: + dst = reference_unwind(dst); + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + src_obj = uacpi_unwrap_internal_reference(src); + overwrite |= dst->inner_object->type == UACPI_OBJECT_UNINITIALIZED; + + if (overwrite) { + uacpi_status ret; + uacpi_object *new_obj; + + new_obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(new_obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + ret = uacpi_object_assign(new_obj, src_obj, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY); + if (uacpi_unlikely_error(ret)) { + uacpi_object_unref(new_obj); + return ret; + } + + object_replace_child(dst, new_obj); + uacpi_object_unref(new_obj); + return UACPI_STATUS_OK; + } + + return object_assign_with_implicit_cast(dst->inner_object, src_obj); +} + +static uacpi_status handle_ref_or_deref_of(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *dst, *src; + + src = item_array_at(&op_ctx->items, 0)->obj; + + if (op_ctx->op->code == UACPI_AML_OP_CondRefOfOp) + dst = item_array_at(&op_ctx->items, 2)->obj; + else + dst = item_array_at(&op_ctx->items, 1)->obj; + + if (op_ctx->op->code == UACPI_AML_OP_DerefOfOp) { + uacpi_bool was_a_reference = UACPI_FALSE; + + if (src->type == UACPI_OBJECT_REFERENCE) { + was_a_reference = UACPI_TRUE; + + /* + * Explicit dereferencing [DerefOf] behavior: + * Simply grabs the bottom-most object that is not a reference. + * This mimics the behavior of NT Acpi.sys: any DerfOf fetches + * the bottom-most reference. Note that this is different from + * ACPICA where DerefOf dereferences one level. + */ + src = reference_unwind(src)->inner_object; + } + + if (src->type == UACPI_OBJECT_BUFFER_INDEX) { + uacpi_buffer_index *buf_idx = &src->buffer_index; + + dst->type = UACPI_OBJECT_INTEGER; + uacpi_memcpy_zerout( + &dst->integer, buffer_index_cursor(buf_idx), + sizeof(dst->integer), 1 + ); + return UACPI_STATUS_OK; + } + + if (!was_a_reference) { + uacpi_error( + "invalid DerefOf argument: %s, expected a reference\n", + uacpi_object_type_to_string(src->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return uacpi_object_assign(dst, src, + UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY); + } + + dst->type = UACPI_OBJECT_REFERENCE; + dst->inner_object = src; + uacpi_object_ref(src); + return UACPI_STATUS_OK; +} + +static uacpi_status do_binary_math( + uacpi_object *arg0, uacpi_object *arg1, + uacpi_object *tgt0, uacpi_object *tgt1, + uacpi_aml_op op +) +{ + uacpi_u64 lhs, rhs, res; + uacpi_bool should_negate = UACPI_FALSE; + + lhs = arg0->integer; + rhs = arg1->integer; + + switch (op) + { + case UACPI_AML_OP_AddOp: + res = lhs + rhs; + break; + case UACPI_AML_OP_SubtractOp: + res = lhs - rhs; + break; + case UACPI_AML_OP_MultiplyOp: + res = lhs * rhs; + break; + case UACPI_AML_OP_ShiftLeftOp: + case UACPI_AML_OP_ShiftRightOp: + if (rhs <= (g_uacpi_rt_ctx.is_rev1 ? 31 : 63)) { + if (op == UACPI_AML_OP_ShiftLeftOp) + res = lhs << rhs; + else + res = lhs >> rhs; + } else { + res = 0; + } + break; + case UACPI_AML_OP_NandOp: + should_negate = UACPI_TRUE; + UACPI_FALLTHROUGH; + case UACPI_AML_OP_AndOp: + res = rhs & lhs; + break; + case UACPI_AML_OP_NorOp: + should_negate = UACPI_TRUE; + UACPI_FALLTHROUGH; + case UACPI_AML_OP_OrOp: + res = rhs | lhs; + break; + case UACPI_AML_OP_XorOp: + res = rhs ^ lhs; + break; + case UACPI_AML_OP_DivideOp: + if (uacpi_unlikely(rhs == 0)) { + uacpi_error("attempted to divide by zero\n"); + return UACPI_STATUS_AML_BAD_ENCODING; + } + tgt1->integer = lhs / rhs; + res = lhs % rhs; + break; + case UACPI_AML_OP_ModOp: + if (uacpi_unlikely(rhs == 0)) { + uacpi_error("attempted to calculate modulo of zero\n"); + return UACPI_STATUS_AML_BAD_ENCODING; + } + res = lhs % rhs; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (should_negate) + res = ~res; + + tgt0->integer = res; + return UACPI_STATUS_OK; +} + +static uacpi_status handle_binary_math(struct execution_context *ctx) +{ + uacpi_object *arg0, *arg1, *tgt0, *tgt1; + struct item_array *items = &ctx->cur_op_ctx->items; + uacpi_aml_op op = ctx->cur_op_ctx->op->code; + + arg0 = item_array_at(items, 0)->obj; + arg1 = item_array_at(items, 1)->obj; + + if (op == UACPI_AML_OP_DivideOp) { + tgt0 = item_array_at(items, 4)->obj; + tgt1 = item_array_at(items, 5)->obj; + } else { + tgt0 = item_array_at(items, 3)->obj; + tgt1 = UACPI_NULL; + } + + return do_binary_math(arg0, arg1, tgt0, tgt1, op); +} + +static uacpi_status handle_unary_math(struct execution_context *ctx) +{ + uacpi_object *arg, *tgt; + struct item_array *items = &ctx->cur_op_ctx->items; + uacpi_aml_op op = ctx->cur_op_ctx->op->code; + + arg = item_array_at(items, 0)->obj; + tgt = item_array_at(items, 2)->obj; + + switch (op) { + case UACPI_AML_OP_NotOp: + tgt->integer = ~arg->integer; + truncate_number_if_needed(tgt); + break; + case UACPI_AML_OP_FindSetRightBitOp: + tgt->integer = uacpi_bit_scan_forward(arg->integer); + break; + case UACPI_AML_OP_FindSetLeftBitOp: + tgt->integer = uacpi_bit_scan_backward(arg->integer); + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status ensure_valid_idx(uacpi_object *obj, uacpi_size idx, + uacpi_size src_size) +{ + if (uacpi_likely(idx < src_size)) + return UACPI_STATUS_OK; + + uacpi_error( + "invalid index %zu, %s@%p has %zu elements\n", + idx, uacpi_object_type_to_string(obj->type), obj, src_size + ); + return UACPI_STATUS_AML_OUT_OF_BOUNDS_INDEX; +} + +static uacpi_status handle_index(struct execution_context *ctx) +{ + uacpi_status ret; + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *src; + struct item *dst; + uacpi_size idx; + + src = item_array_at(&op_ctx->items, 0)->obj; + idx = item_array_at(&op_ctx->items, 1)->obj->integer; + dst = item_array_at(&op_ctx->items, 3); + + switch (src->type) { + case UACPI_OBJECT_BUFFER: + case UACPI_OBJECT_STRING: { + uacpi_buffer_index *buf_idx; + struct object_storage_as_buffer buf; + get_object_storage(src, &buf, UACPI_FALSE); + + ret = ensure_valid_idx(src, idx, buf.len); + if (uacpi_unlikely_error(ret)) + return ret; + + dst->type = ITEM_OBJECT; + dst->obj = uacpi_create_object(UACPI_OBJECT_BUFFER_INDEX); + if (uacpi_unlikely(dst->obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + buf_idx = &dst->obj->buffer_index; + buf_idx->idx = idx; + buf_idx->buffer = src->buffer; + uacpi_shareable_ref(buf_idx->buffer); + + break; + } + case UACPI_OBJECT_PACKAGE: { + uacpi_package *pkg = src->package; + uacpi_object *obj; + + ret = ensure_valid_idx(src, idx, pkg->count); + if (uacpi_unlikely_error(ret)) + return ret; + + /* + * Lazily transform the package element into an internal reference + * to itself of type PKG_INDEX. This is needed to support stuff like + * CopyObject(..., Index(pkg, X)) where the new object must be + * propagated to anyone else with a currently alive index object. + * + * Sidenote: Yes, IndexOp is not a SimpleName, so technically it is + * illegal to CopyObject to it. However, yet again we fall + * victim to the NT ACPI driver implementation, which allows + * it just fine. + */ + obj = pkg->objects[idx]; + if (obj->type != UACPI_OBJECT_REFERENCE || + obj->flags != UACPI_REFERENCE_KIND_PKG_INDEX) { + + obj = uacpi_create_internal_reference( + UACPI_REFERENCE_KIND_PKG_INDEX, obj + ); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + pkg->objects[idx] = obj; + uacpi_object_unref(obj->inner_object); + } + + dst->obj = obj; + dst->type = ITEM_OBJECT; + uacpi_object_ref(dst->obj); + break; + } + default: + uacpi_error( + "invalid argument for Index: %s, " + "expected String/Buffer/Package\n", + uacpi_object_type_to_string(src->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return UACPI_STATUS_OK; +} + +static uacpi_u64 object_to_integer(const uacpi_object *obj, + uacpi_size max_buffer_bytes) +{ + uacpi_u64 dst; + + switch (obj->type) { + case UACPI_OBJECT_INTEGER: + dst = obj->integer; + break; + case UACPI_OBJECT_BUFFER: { + uacpi_size bytes; + bytes = UACPI_MIN(max_buffer_bytes, obj->buffer->size); + uacpi_memcpy_zerout(&dst, obj->buffer->data, sizeof(dst), bytes); + break; + } + case UACPI_OBJECT_STRING: + uacpi_string_to_integer( + obj->buffer->text, obj->buffer->size, UACPI_BASE_AUTO, &dst + ); + break; + default: + dst = 0; + break; + } + + return dst; +} + +static uacpi_status integer_to_string( + uacpi_u64 integer, uacpi_buffer *str, uacpi_bool is_hex +) +{ + int repr_len; + uacpi_char int_buf[21]; + uacpi_size final_size; + + repr_len = uacpi_snprintf( + int_buf, sizeof(int_buf), + is_hex ? "%"UACPI_PRIX64 : "%"UACPI_PRIu64, + UACPI_FMT64(integer) + ); + if (uacpi_unlikely(repr_len < 0)) + return UACPI_STATUS_INVALID_ARGUMENT; + + // 0x prefix + repr + \0 + final_size = (is_hex ? 2 : 0) + repr_len + 1; + + str->data = uacpi_kernel_alloc(final_size); + if (uacpi_unlikely(str->data == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + if (is_hex) { + str->text[0] = '0'; + str->text[1] = 'x'; + } + uacpi_memcpy(str->text + (is_hex ? 2 : 0), int_buf, repr_len + 1); + str->size = final_size; + + return UACPI_STATUS_OK; +} + +static uacpi_status buffer_to_string( + uacpi_buffer *buf, uacpi_buffer *str, uacpi_bool is_hex +) +{ + int repr_len; + uacpi_char int_buf[5]; + uacpi_size i, final_size; + uacpi_char *cursor; + + if (is_hex) { + final_size = 4 * buf->size; + } else { + final_size = 0; + + for (i = 0; i < buf->size; ++i) { + uacpi_u8 value = ((uacpi_u8*)buf->data)[i]; + + if (value < 10) + final_size += 1; + else if (value < 100) + final_size += 2; + else + final_size += 3; + } + } + + // Comma for every value but one + final_size += buf->size - 1; + + // Null terminator + final_size += 1; + + str->data = uacpi_kernel_alloc(final_size); + if (uacpi_unlikely(str->data == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + cursor = str->data; + + for (i = 0; i < buf->size; ++i) { + repr_len = uacpi_snprintf( + int_buf, sizeof(int_buf), + is_hex ? "0x%02X" : "%d", + ((uacpi_u8*)buf->data)[i] + ); + if (uacpi_unlikely(repr_len < 0)) { + uacpi_free(str->data, final_size); + str->data = UACPI_NULL; + return UACPI_STATUS_INVALID_ARGUMENT; + } + + uacpi_memcpy(cursor, int_buf, repr_len + 1); + cursor += repr_len; + + if (i != buf->size - 1) + *cursor++ = ','; + } + + str->size = final_size; + return UACPI_STATUS_OK; +} + +static uacpi_status do_make_empty_object(uacpi_buffer *buf, + uacpi_bool is_string) +{ + buf->text = uacpi_kernel_alloc_zeroed(sizeof(uacpi_char)); + if (uacpi_unlikely(buf->text == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + if (is_string) + buf->size = sizeof(uacpi_char); + + return UACPI_STATUS_OK; +} + +static uacpi_status make_null_string(uacpi_buffer *buf) +{ + return do_make_empty_object(buf, UACPI_TRUE); +} + +static uacpi_status make_null_buffer(uacpi_buffer *buf) +{ + /* + * Allocate at least 1 byte just to be safe, + * even for empty buffers. We still set the + * size to 0 though. + */ + return do_make_empty_object(buf, UACPI_FALSE); +} + +static uacpi_status handle_to(struct execution_context *ctx) +{ + uacpi_status ret = UACPI_STATUS_OK; + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *src, *dst; + + src = item_array_at(&op_ctx->items, 0)->obj; + dst = item_array_at(&op_ctx->items, 2)->obj; + + switch (op_ctx->op->code) { + case UACPI_AML_OP_ToIntegerOp: + // NT always takes the first 8 bytes, even for revision 1 + dst->integer = object_to_integer(src, 8); + break; + + case UACPI_AML_OP_ToHexStringOp: + case UACPI_AML_OP_ToDecimalStringOp: { + uacpi_bool is_hex = op_ctx->op->code == UACPI_AML_OP_ToHexStringOp; + + if (src->type == UACPI_OBJECT_INTEGER) { + ret = integer_to_string(src->integer, dst->buffer, is_hex); + break; + } else if (src->type == UACPI_OBJECT_BUFFER) { + if (uacpi_unlikely(src->buffer->size == 0)) + return make_null_string(dst->buffer); + + ret = buffer_to_string(src->buffer, dst->buffer, is_hex); + break; + } + UACPI_FALLTHROUGH; + } + case UACPI_AML_OP_ToBufferOp: { + struct object_storage_as_buffer buf; + uacpi_u8 *dst_buf; + + ret = get_object_storage(src, &buf, UACPI_TRUE); + if (uacpi_unlikely_error(ret)) + return ret; + + if (uacpi_unlikely(buf.len == 0)) + return make_null_buffer(dst->buffer); + + dst_buf = uacpi_kernel_alloc(buf.len); + if (uacpi_unlikely(dst_buf == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(dst_buf, buf.ptr, buf.len); + dst->buffer->data = dst_buf; + dst->buffer->size = buf.len; + break; + } + + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return ret; +} + +static uacpi_status handle_to_string(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_buffer *src_buf, *dst_buf; + uacpi_size req_len, len; + + src_buf = item_array_at(&op_ctx->items, 0)->obj->buffer; + req_len = item_array_at(&op_ctx->items, 1)->obj->integer; + dst_buf = item_array_at(&op_ctx->items, 3)->obj->buffer; + + len = UACPI_MIN(req_len, src_buf->size); + if (uacpi_unlikely(len == 0)) + return make_null_string(dst_buf); + + len = uacpi_strnlen(src_buf->text, len); + + dst_buf->text = uacpi_kernel_alloc(len + 1); + if (uacpi_unlikely(dst_buf->text == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(dst_buf->text, src_buf->data, len); + dst_buf->text[len] = '\0'; + dst_buf->size = len + 1; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_mid(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *src, *dst; + struct object_storage_as_buffer src_buf; + uacpi_buffer *dst_buf; + uacpi_size idx, len; + uacpi_bool is_string; + + src = item_array_at(&op_ctx->items, 0)->obj; + if (uacpi_unlikely(src->type != UACPI_OBJECT_STRING && + src->type != UACPI_OBJECT_BUFFER)) { + uacpi_error( + "invalid argument for Mid: %s, expected String/Buffer\n", + uacpi_object_type_to_string(src->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + idx = item_array_at(&op_ctx->items, 1)->obj->integer; + len = item_array_at(&op_ctx->items, 2)->obj->integer; + dst = item_array_at(&op_ctx->items, 4)->obj; + dst_buf = dst->buffer; + + is_string = src->type == UACPI_OBJECT_STRING; + get_object_storage(src, &src_buf, UACPI_FALSE); + + if (uacpi_unlikely(src_buf.len == 0 || idx >= src_buf.len || len == 0)) { + if (src->type == UACPI_OBJECT_STRING) { + dst->type = UACPI_OBJECT_STRING; + return make_null_string(dst_buf); + } + + return make_null_buffer(dst_buf); + } + + // Guaranteed to be at least 1 here + len = UACPI_MIN(len, src_buf.len - idx); + + dst_buf->data = uacpi_kernel_alloc(len + is_string); + if (uacpi_unlikely(dst_buf->data == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(dst_buf->data, (uacpi_u8*)src_buf.ptr + idx, len); + dst_buf->size = len; + + if (is_string) { + dst_buf->text[dst_buf->size++] = '\0'; + dst->type = UACPI_OBJECT_STRING; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_concatenate(struct execution_context *ctx) +{ + uacpi_status ret = UACPI_STATUS_OK; + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *arg0, *arg1, *dst; + uacpi_u8 *dst_buf; + uacpi_size buf_size = 0; + + arg0 = item_array_at(&op_ctx->items, 0)->obj; + arg1 = item_array_at(&op_ctx->items, 1)->obj; + dst = item_array_at(&op_ctx->items, 3)->obj; + + switch (arg0->type) { + case UACPI_OBJECT_INTEGER: { + uacpi_u64 arg1_as_int; + uacpi_size int_size; + + int_size = sizeof_int(); + buf_size = int_size * 2; + + dst_buf = uacpi_kernel_alloc(buf_size); + if (uacpi_unlikely(dst_buf == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + arg1_as_int = object_to_integer(arg1, 8); + + uacpi_memcpy(dst_buf, &arg0->integer, int_size); + uacpi_memcpy(dst_buf+ int_size, &arg1_as_int, int_size); + break; + } + case UACPI_OBJECT_BUFFER: { + uacpi_buffer *arg0_buf = arg0->buffer; + struct object_storage_as_buffer arg1_buf; + + get_object_storage(arg1, &arg1_buf, UACPI_TRUE); + buf_size = arg0_buf->size + arg1_buf.len; + + dst_buf = uacpi_kernel_alloc(buf_size); + if (uacpi_unlikely(dst_buf == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(dst_buf, arg0_buf->data, arg0_buf->size); + uacpi_memcpy(dst_buf + arg0_buf->size, arg1_buf.ptr, arg1_buf.len); + break; + } + case UACPI_OBJECT_STRING: { + uacpi_char int_buf[17]; + void *arg1_ptr; + uacpi_size arg0_size, arg1_size; + uacpi_buffer *arg0_buf = arg0->buffer; + + switch (arg1->type) { + case UACPI_OBJECT_INTEGER: { + int size; + size = uacpi_snprintf(int_buf, sizeof(int_buf), "%"UACPI_PRIx64, + UACPI_FMT64(arg1->integer)); + if (size < 0) + return UACPI_STATUS_INVALID_ARGUMENT; + + arg1_ptr = int_buf; + arg1_size = size + 1; + break; + } + case UACPI_OBJECT_STRING: + arg1_ptr = arg1->buffer->data; + arg1_size = arg1->buffer->size; + break; + case UACPI_OBJECT_BUFFER: { + uacpi_buffer tmp_buf; + + ret = buffer_to_string(arg1->buffer, &tmp_buf, UACPI_TRUE); + if (uacpi_unlikely_error(ret)) + return ret; + + arg1_ptr = tmp_buf.data; + arg1_size = tmp_buf.size; + break; + } + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + arg0_size = arg0_buf->size ? arg0_buf->size - 1 : arg0_buf->size; + buf_size = arg0_size + arg1_size; + + dst_buf = uacpi_kernel_alloc(buf_size); + if (uacpi_unlikely(dst_buf == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto cleanup; + } + + uacpi_memcpy(dst_buf, arg0_buf->data, arg0_size); + uacpi_memcpy(dst_buf + arg0_size, arg1_ptr, arg1_size); + dst->type = UACPI_OBJECT_STRING; + + cleanup: + if (arg1->type == UACPI_OBJECT_BUFFER) + uacpi_free(arg1_ptr, arg1_size); + break; + } + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (uacpi_likely_success(ret)) { + dst->buffer->data = dst_buf; + dst->buffer->size = buf_size; + } + return ret; +} + +static uacpi_status handle_concatenate_res(struct execution_context *ctx) +{ + uacpi_status ret; + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *arg0, *arg1, *dst; + uacpi_u8 *dst_buf; + uacpi_size dst_size, arg0_size, arg1_size; + + arg0 = item_array_at(&op_ctx->items, 0)->obj; + arg1 = item_array_at(&op_ctx->items, 1)->obj; + dst = item_array_at(&op_ctx->items, 3)->obj; + + ret = uacpi_find_aml_resource_end_tag(arg0->buffer, &arg0_size); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_find_aml_resource_end_tag(arg1->buffer, &arg1_size); + if (uacpi_unlikely_error(ret)) + return ret; + + dst_size = arg0_size + arg1_size + sizeof(struct acpi_resource_end_tag); + + dst_buf = uacpi_kernel_alloc(dst_size); + if (uacpi_unlikely(dst_buf == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + dst->buffer->data = dst_buf; + dst->buffer->size = dst_size; + + uacpi_memcpy(dst_buf, arg0->buffer->data, arg0_size); + uacpi_memcpy(dst_buf + arg0_size, arg1->buffer->data, arg1_size); + + /* + * Small item (0), End Tag (0x0F), length 1 + * Leave the checksum as 0 + */ + dst_buf[dst_size - 2] = + (ACPI_RESOURCE_END_TAG << ACPI_SMALL_ITEM_NAME_IDX) | + (sizeof(struct acpi_resource_end_tag) - 1); + dst_buf[dst_size - 1] = 0; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_sizeof(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *src, *dst; + + src = item_array_at(&op_ctx->items, 0)->obj; + dst = item_array_at(&op_ctx->items, 1)->obj; + + if (uacpi_likely(src->type == UACPI_OBJECT_REFERENCE)) + src = reference_unwind(src)->inner_object; + + switch (src->type) { + case UACPI_OBJECT_STRING: + case UACPI_OBJECT_BUFFER: { + struct object_storage_as_buffer buf; + get_object_storage(src, &buf, UACPI_FALSE); + + dst->integer = buf.len; + break; + } + + case UACPI_OBJECT_PACKAGE: + dst->integer = src->package->count; + break; + + default: + uacpi_error( + "invalid argument for Sizeof: %s, " + "expected String/Buffer/Package\n", + uacpi_object_type_to_string(src->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_object_type(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *src, *dst; + + src = item_array_at(&op_ctx->items, 0)->obj; + dst = item_array_at(&op_ctx->items, 1)->obj; + + if (uacpi_likely(src->type == UACPI_OBJECT_REFERENCE)) + src = reference_unwind(src)->inner_object; + + dst->integer = src->type; + if (dst->integer == UACPI_OBJECT_BUFFER_INDEX) + dst->integer = UACPI_OBJECT_BUFFER_FIELD; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_timer(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *dst; + + dst = item_array_at(&op_ctx->items, 0)->obj; + dst->integer = uacpi_kernel_get_nanoseconds_since_boot() / 100; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_stall_or_sleep(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_u64 time; + + time = item_array_at(&op_ctx->items, 0)->obj->integer; + + if (op_ctx->op->code == UACPI_AML_OP_SleepOp) { + /* + * ACPICA doesn't allow sleeps longer than 2 seconds, + * so we shouldn't either. + */ + if (time > 2000) + time = 2000; + + uacpi_namespace_write_unlock(); + uacpi_kernel_sleep(time); + uacpi_namespace_write_lock(); + } else { + // Spec says this must evaluate to a ByteData + if (time > 0xFF) + time = 0xFF; + uacpi_kernel_stall(time); + } + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_bcd(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_u64 src, dst = 0; + uacpi_size i; + uacpi_object *dst_obj; + + src = item_array_at(&op_ctx->items, 0)->obj->integer; + dst_obj = item_array_at(&op_ctx->items, 2)->obj; + i = 64; + + /* + * NOTE: ACPICA just errors out for invalid BCD, but NT allows it just fine. + * FromBCD matches NT behavior 1:1 even for invalid BCD, but ToBCD + * produces different results when the input is too large. + */ + if (op_ctx->op->code == UACPI_AML_OP_FromBCDOp) { + do { + i -= 4; + dst *= 10; + dst += (src >> i) & 0b1111; + } while (i); + } else { + while (src != 0) { + dst >>= 4; + i -= 4; + dst |= (src % 10) << 60; + src /= 10; + } + + dst >>= (i % 64); + } + + dst_obj->integer = dst; + return UACPI_STATUS_OK; +} + +static uacpi_status handle_logical_not(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *src, *dst; + + src = item_array_at(&op_ctx->items, 0)->obj; + dst = item_array_at(&op_ctx->items, 1)->obj; + + dst->type = UACPI_OBJECT_INTEGER; + dst->integer = src->integer ? 0 : ones(); + + return UACPI_STATUS_OK; +} + +static uacpi_bool handle_logical_equality(uacpi_object *lhs, uacpi_object *rhs) +{ + uacpi_bool res = UACPI_FALSE; + + if (lhs->type == UACPI_OBJECT_STRING || lhs->type == UACPI_OBJECT_BUFFER) { + res = lhs->buffer->size == rhs->buffer->size; + + if (res && lhs->buffer->size) { + res = uacpi_memcmp( + lhs->buffer->data, + rhs->buffer->data, + lhs->buffer->size + ) == 0; + } + } else if (lhs->type == UACPI_OBJECT_INTEGER) { + res = lhs->integer == rhs->integer; + } + + return res; +} + +static uacpi_bool handle_logical_less_or_greater( + uacpi_aml_op op, uacpi_object *lhs, uacpi_object *rhs +) +{ + if (lhs->type == UACPI_OBJECT_STRING || lhs->type == UACPI_OBJECT_BUFFER) { + int res; + uacpi_buffer *lhs_buf, *rhs_buf; + + lhs_buf = lhs->buffer; + rhs_buf = rhs->buffer; + + res = uacpi_memcmp(lhs_buf->data, rhs_buf->data, + UACPI_MIN(lhs_buf->size, rhs_buf->size)); + if (res == 0) { + if (lhs_buf->size < rhs_buf->size) + res = -1; + else if (lhs_buf->size > rhs_buf->size) + res = 1; + } + + if (op == UACPI_AML_OP_LLessOp) + return res < 0; + + return res > 0; + } + + if (op == UACPI_AML_OP_LLessOp) + return lhs->integer < rhs->integer; + + return lhs->integer > rhs->integer; +} + +static uacpi_status handle_binary_logic(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_aml_op op = op_ctx->op->code; + uacpi_object *lhs, *rhs, *dst; + uacpi_bool res; + + lhs = item_array_at(&op_ctx->items, 0)->obj; + rhs = item_array_at(&op_ctx->items, 1)->obj; + dst = item_array_at(&op_ctx->items, 2)->obj; + + switch (op) { + case UACPI_AML_OP_LEqualOp: + case UACPI_AML_OP_LLessOp: + case UACPI_AML_OP_LGreaterOp: + // TODO: typecheck at parse time + if (lhs->type != rhs->type) { + uacpi_error( + "don't know how to do a logical comparison of '%s' and '%s'\n", + uacpi_object_type_to_string(lhs->type), + uacpi_object_type_to_string(rhs->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + if (op == UACPI_AML_OP_LEqualOp) + res = handle_logical_equality(lhs, rhs); + else + res = handle_logical_less_or_greater(op, lhs, rhs); + break; + default: { + uacpi_u64 lhs_int, rhs_int; + + // NT only looks at the first 4 bytes of a buffer + lhs_int = object_to_integer(lhs, 4); + rhs_int = object_to_integer(rhs, 4); + + if (op == UACPI_AML_OP_LandOp) + res = lhs_int && rhs_int; + else + res = lhs_int || rhs_int; + break; + } + } + + dst->integer = res ? ones() : 0; + return UACPI_STATUS_OK; +} + +enum match_op { + MTR = 0, + MEQ = 1, + MLE = 2, + MLT = 3, + MGE = 4, + MGT = 5, +}; + +static uacpi_bool match_one(enum match_op op, uacpi_u64 lhs, uacpi_u64 rhs) +{ + switch (op) { + case MTR: + return UACPI_TRUE; + case MEQ: + return lhs == rhs; + case MLE: + return lhs <= rhs; + case MLT: + return lhs < rhs; + case MGE: + return lhs >= rhs; + case MGT: + return lhs > rhs; + default: + return UACPI_FALSE; + } +} + +static uacpi_status handle_match(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_package *pkg; + uacpi_u64 operand0, operand1, start_idx, i; + enum match_op mop0, mop1; + uacpi_object *dst; + + pkg = item_array_at(&op_ctx->items, 0)->obj->package; + mop0 = item_array_at(&op_ctx->items, 1)->immediate; + operand0 = item_array_at(&op_ctx->items, 2)->obj->integer; + mop1 = item_array_at(&op_ctx->items, 3)->immediate; + operand1 = item_array_at(&op_ctx->items, 4)->obj->integer; + start_idx = item_array_at(&op_ctx->items, 5)->obj->integer; + dst = item_array_at(&op_ctx->items, 6)->obj; + + for (i = start_idx; i < pkg->count; ++i) { + uacpi_object *obj = pkg->objects[i]; + + if (obj->type != UACPI_OBJECT_INTEGER) + continue; + + if (match_one(mop0, obj->integer, operand0) && + match_one(mop1, obj->integer, operand1)) + break; + } + + if (i < pkg->count) + dst->integer = i; + else + dst->integer = ones(); + + return UACPI_STATUS_OK; +} + +/* + * PkgLength := + * PkgLeadByte | + * | + * | + * PkgLeadByte := + * + * + * + */ +static uacpi_status parse_package_length(struct call_frame *frame, + struct package_length *out_pkg) +{ + uacpi_u32 left, size; + uacpi_u8 *data, marker_length; + + out_pkg->begin = frame->code_offset; + marker_length = 1; + + left = call_frame_code_bytes_left(frame); + if (uacpi_unlikely(left < 1)) + return UACPI_STATUS_AML_BAD_ENCODING; + + data = call_frame_cursor(frame); + marker_length += *data >> 6; + + if (uacpi_unlikely(left < marker_length)) + return UACPI_STATUS_AML_BAD_ENCODING; + + switch (marker_length) { + case 1: + size = *data & 0b111111; + break; + case 2: + case 3: + case 4: { + uacpi_u32 temp_byte = 0; + + size = *data & 0b1111; + uacpi_memcpy(&temp_byte, data + 1, marker_length - 1); + + // marker_length - 1 is at most 3, so this shift is safe + size |= temp_byte << 4; + break; + } + } + + frame->code_offset += marker_length; + + out_pkg->end = out_pkg->begin + size; + if (uacpi_unlikely(out_pkg->end < out_pkg->begin)) { + uacpi_error( + "PkgLength overflow: start=%u, size=%u\n", out_pkg->begin, size + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + return UACPI_STATUS_OK; +} + +/* + * ByteData + * // bit 0-2: ArgCount (0-7) + * // bit 3: SerializeFlag + * // 0 NotSerialized + * // 1 Serialized + * // bit 4-7: SyncLevel (0x00-0x0f) + */ +static void init_method_flags(uacpi_control_method *method, uacpi_u8 flags_byte) +{ + method->args = flags_byte & 0b111; + method->is_serialized = (flags_byte >> 3) & 1; + method->sync_level = flags_byte >> 4; +} + +static uacpi_status handle_create_method(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + struct uacpi_control_method *this_method, *method; + struct package_length *pkg; + struct uacpi_namespace_node *node; + struct uacpi_object *dst; + uacpi_u32 method_begin_offset, method_size; + + this_method = ctx->cur_frame->method; + pkg = &item_array_at(&op_ctx->items, 0)->pkg; + node = item_array_at(&op_ctx->items, 1)->node; + method_begin_offset = item_array_at(&op_ctx->items, 3)->immediate; + + if (uacpi_unlikely(pkg->end < pkg->begin || + pkg->end < method_begin_offset || + pkg->end > this_method->size)) { + uacpi_error( + "invalid method %.4s bounds [%u..%u] (parent size is %u)\n", + node->name.text, method_begin_offset, pkg->end, this_method->size + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + dst = item_array_at(&op_ctx->items, 4)->obj; + + method = dst->method; + method_size = pkg->end - method_begin_offset; + + if (method_size) { + method->code = uacpi_kernel_alloc(method_size); + if (uacpi_unlikely(method->code == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy( + method->code, + ctx->cur_frame->method->code + method_begin_offset, + method_size + ); + method->size = method_size; + method->owns_code = 1; + } + + init_method_flags(method, item_array_at(&op_ctx->items, 2)->immediate); + + node->object = uacpi_create_internal_reference(UACPI_REFERENCE_KIND_NAMED, + dst); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_create_mutex_or_event(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_namespace_node *node; + uacpi_object *dst; + + node = item_array_at(&op_ctx->items, 0)->node; + + if (op_ctx->op->code == UACPI_AML_OP_MutexOp) { + dst = item_array_at(&op_ctx->items, 2)->obj; + + // bits 0-3: SyncLevel (0x00-0x0f), bits 4-7: Reserved (must be 0) + dst->mutex->sync_level = item_array_at(&op_ctx->items, 1)->immediate; + dst->mutex->sync_level &= 0b1111; + } else { + dst = item_array_at(&op_ctx->items, 1)->obj; + } + + node->object = uacpi_create_internal_reference( + UACPI_REFERENCE_KIND_NAMED, + dst + ); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_event_ctl(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *obj; + + obj = uacpi_unwrap_internal_reference( + item_array_at(&op_ctx->items, 0)->obj + ); + if (uacpi_unlikely(obj->type != UACPI_OBJECT_EVENT)) { + uacpi_error( + "%s: invalid argument '%s', expected an Event object\n", + op_ctx->op->name, uacpi_object_type_to_string(obj->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + switch (op_ctx->op->code) + { + case UACPI_AML_OP_SignalOp: + uacpi_kernel_signal_event(obj->event->handle); + break; + case UACPI_AML_OP_ResetOp: + uacpi_kernel_reset_event(obj->event->handle); + break; + case UACPI_AML_OP_WaitOp: { + uacpi_u64 timeout; + uacpi_bool ret; + + timeout = item_array_at(&op_ctx->items, 1)->obj->integer; + if (timeout > 0xFFFF) + timeout = 0xFFFF; + + uacpi_namespace_write_unlock(); + ret = uacpi_kernel_wait_for_event(obj->event->handle, timeout); + uacpi_namespace_write_lock(); + + /* + * The return value here is inverted, we return 0 for success and Ones + * for timeout and everything else. + */ + if (ret) + item_array_at(&op_ctx->items, 2)->obj->integer = 0; + break; + } + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_mutex_ctl(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_object *obj; + + obj = uacpi_unwrap_internal_reference( + item_array_at(&op_ctx->items, 0)->obj + ); + if (uacpi_unlikely(obj->type != UACPI_OBJECT_MUTEX)) { + uacpi_error( + "%s: invalid argument '%s', expected a Mutex object\n", + op_ctx->op->name, uacpi_object_type_to_string(obj->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + switch (op_ctx->op->code) + { + case UACPI_AML_OP_AcquireOp: { + uacpi_u64 timeout; + uacpi_u64 *return_value; + uacpi_status ret; + + return_value = &item_array_at(&op_ctx->items, 2)->obj->integer; + + if (uacpi_unlikely(ctx->sync_level > obj->mutex->sync_level)) { + uacpi_warn( + "ignoring attempt to acquire mutex @%p with a lower sync level " + "(%d < %d)\n", obj->mutex, obj->mutex->sync_level, + ctx->sync_level + ); + break; + } + + timeout = item_array_at(&op_ctx->items, 1)->immediate; + if (timeout > 0xFFFF) + timeout = 0xFFFF; + + if (uacpi_this_thread_owns_aml_mutex(obj->mutex)) { + ret = uacpi_acquire_aml_mutex(obj->mutex, timeout); + if (uacpi_likely_success(ret)) + *return_value = 0; + break; + } + + ret = uacpi_acquire_aml_mutex(obj->mutex, timeout); + if (uacpi_unlikely_error(ret)) + break; + + ret = held_mutexes_array_push(&ctx->held_mutexes, obj->mutex); + if (uacpi_unlikely_error(ret)) { + uacpi_release_aml_mutex(obj->mutex); + return ret; + } + + ctx->sync_level = obj->mutex->sync_level; + *return_value = 0; + break; + } + + case UACPI_AML_OP_ReleaseOp: { + uacpi_status ret; + + if (!uacpi_this_thread_owns_aml_mutex(obj->mutex)) { + uacpi_warn( + "attempted to release not-previously-acquired mutex object " + "@%p (%p)\n", obj->mutex, obj->mutex->handle + ); + break; + } + + ret = held_mutexes_array_remove_and_release( + &ctx->held_mutexes, obj->mutex, + FORCE_RELEASE_NO + ); + if (uacpi_likely_success(ret)) { + uacpi_mutex **last_mutex; + + last_mutex = held_mutexes_array_last(&ctx->held_mutexes); + if (last_mutex == UACPI_NULL) { + ctx->sync_level = 0; + break; + } + + ctx->sync_level = (*last_mutex)->sync_level; + } + break; + } + + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_notify(struct execution_context *ctx) +{ + uacpi_status ret; + struct op_context *op_ctx = ctx->cur_op_ctx; + struct uacpi_namespace_node *node; + uacpi_u64 value; + + node = item_array_at(&op_ctx->items, 0)->node; + value = item_array_at(&op_ctx->items, 1)->obj->integer; + + ret = uacpi_notify_all(node, value); + if (uacpi_likely_success(ret)) + return ret; + + if (ret == UACPI_STATUS_NO_HANDLER) { + const uacpi_char *path; + + path = uacpi_namespace_node_generate_absolute_path(node); + uacpi_warn( + "ignoring firmware Notify(%s, 0x%"UACPI_PRIX64") request, " + "no listeners\n", path, UACPI_FMT64(value) + ); + uacpi_free_dynamic_string(path); + + return UACPI_STATUS_OK; + } + + if (ret == UACPI_STATUS_INVALID_ARGUMENT) { + uacpi_error("Notify() called on an invalid object %.4s\n", + node->name.text); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return ret; +} + +static uacpi_status handle_firmware_request(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_firmware_request req = { 0 }; + + switch (op_ctx->op->code) { + case UACPI_AML_OP_BreakPointOp: + req.type = UACPI_FIRMWARE_REQUEST_TYPE_BREAKPOINT; + req.breakpoint.ctx = ctx; + break; + case UACPI_AML_OP_FatalOp: + req.type = UACPI_FIRMWARE_REQUEST_TYPE_FATAL; + req.fatal.type = item_array_at(&op_ctx->items, 0)->immediate; + req.fatal.code = item_array_at(&op_ctx->items, 1)->immediate; + req.fatal.arg = item_array_at(&op_ctx->items, 2)->obj->integer; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + uacpi_namespace_write_unlock(); + uacpi_kernel_handle_firmware_request(&req); + uacpi_namespace_write_lock(); + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_create_named(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + struct uacpi_namespace_node *node; + uacpi_object *src; + + node = item_array_at(&op_ctx->items, 0)->node; + src = item_array_at(&op_ctx->items, 1)->obj; + + node->object = uacpi_create_internal_reference(UACPI_REFERENCE_KIND_NAMED, + src); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +static uacpi_object_type buffer_field_get_read_type( + struct uacpi_buffer_field *field +) +{ + if (field->bit_length > (g_uacpi_rt_ctx.is_rev1 ? 32u : 64u) || + field->force_buffer) + return UACPI_OBJECT_BUFFER; + + return UACPI_OBJECT_INTEGER; +} + +static uacpi_object_type field_unit_get_read_type( + struct uacpi_field_unit *field +) +{ + if (field->bit_length > (g_uacpi_rt_ctx.is_rev1 ? 32u : 64u)) + return UACPI_OBJECT_BUFFER; + + return UACPI_OBJECT_INTEGER; +} + +static uacpi_object_type field_get_read_type(uacpi_object *obj) +{ + if (obj->type == UACPI_OBJECT_BUFFER_FIELD) + return buffer_field_get_read_type(&obj->buffer_field); + + return field_unit_get_read_type(obj->field_unit); +} + +static uacpi_status handle_field_read(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + struct uacpi_namespace_node *node; + uacpi_object *src_obj, *dst_obj; + uacpi_size dst_size; + void *dst; + + node = item_array_at(&op_ctx->items, 0)->node; + src_obj = uacpi_namespace_node_get_object(node); + dst_obj = item_array_at(&op_ctx->items, 1)->obj; + + if (field_get_read_type(src_obj) == UACPI_OBJECT_BUFFER) { + uacpi_buffer *buf; + + buf = dst_obj->buffer; + dst_size = field_byte_size(src_obj); + + dst = uacpi_kernel_alloc_zeroed(dst_size); + if (dst == UACPI_NULL) + return UACPI_STATUS_OUT_OF_MEMORY; + + buf->data = dst; + buf->size = dst_size; + } else { + dst = &dst_obj->integer; + dst_size = sizeof(uacpi_u64); + } + + if (src_obj->type == UACPI_OBJECT_BUFFER_FIELD) { + uacpi_read_buffer_field(&src_obj->buffer_field, dst); + return UACPI_STATUS_OK; + } + + return uacpi_read_field_unit(src_obj->field_unit, dst, dst_size); +} + +static uacpi_status handle_create_buffer_field(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + struct uacpi_namespace_node *node; + uacpi_buffer *src_buf; + uacpi_object *field_obj; + uacpi_buffer_field *field; + + /* + * Layout of items here: + * [0] -> Type checked source buffer object + * [1] -> Byte/bit index integer object + * [2] ( if CreateField) -> bit length integer object + * [3] (2 if not CreateField) -> the new namespace node + * [4] (3 if not CreateField) -> the buffer field object we're creating here + */ + src_buf = item_array_at(&op_ctx->items, 0)->obj->buffer; + + if (op_ctx->op->code == UACPI_AML_OP_CreateFieldOp) { + uacpi_object *idx_obj, *len_obj; + + idx_obj = item_array_at(&op_ctx->items, 1)->obj; + len_obj = item_array_at(&op_ctx->items, 2)->obj; + node = item_array_at(&op_ctx->items, 3)->node; + field_obj = item_array_at(&op_ctx->items, 4)->obj; + field = &field_obj->buffer_field; + + field->bit_index = idx_obj->integer; + + if (uacpi_unlikely(!len_obj->integer || + len_obj->integer > 0xFFFFFFFF)) { + uacpi_error("invalid bit field length (%u)\n", field->bit_length); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + field->bit_length = len_obj->integer; + field->force_buffer = UACPI_TRUE; + } else { + uacpi_object *idx_obj; + + idx_obj = item_array_at(&op_ctx->items, 1)->obj; + node = item_array_at(&op_ctx->items, 2)->node; + field_obj = item_array_at(&op_ctx->items, 3)->obj; + field = &field_obj->buffer_field; + + field->bit_index = idx_obj->integer; + switch (op_ctx->op->code) { + case UACPI_AML_OP_CreateBitFieldOp: + field->bit_length = 1; + break; + case UACPI_AML_OP_CreateByteFieldOp: + field->bit_length = 8; + break; + case UACPI_AML_OP_CreateWordFieldOp: + field->bit_length = 16; + break; + case UACPI_AML_OP_CreateDWordFieldOp: + field->bit_length = 32; + break; + case UACPI_AML_OP_CreateQWordFieldOp: + field->bit_length = 64; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (op_ctx->op->code != UACPI_AML_OP_CreateBitFieldOp) + field->bit_index *= 8; + } + + if (uacpi_unlikely((field->bit_index + field->bit_length) > + src_buf->size * 8)) { + uacpi_error( + "invalid buffer field: bits [%zu..%zu], buffer size is %zu bytes\n", + field->bit_index, field->bit_index + field->bit_length, + src_buf->size + ); + return UACPI_STATUS_AML_OUT_OF_BOUNDS_INDEX; + } + + field->backing = src_buf; + uacpi_shareable_ref(field->backing); + node->object = uacpi_create_internal_reference(UACPI_REFERENCE_KIND_NAMED, + field_obj); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_control_flow(struct execution_context *ctx) +{ + struct call_frame *frame = ctx->cur_frame; + struct op_context *op_ctx = ctx->cur_op_ctx; + + if (uacpi_unlikely(frame->last_while == UACPI_NULL)) { + uacpi_error( + "attempting to %s outside of a While block\n", + op_ctx->op->code == UACPI_AML_OP_BreakOp ? "Break" : "Continue" + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + for (;;) { + if (ctx->cur_block != frame->last_while) { + frame_reset_post_end_block(ctx, ctx->cur_block->type); + continue; + } + + if (op_ctx->op->code == UACPI_AML_OP_BreakOp) + frame->code_offset = ctx->cur_block->end; + else + frame->code_offset = ctx->cur_block->begin; + frame_reset_post_end_block(ctx, ctx->cur_block->type); + break; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status create_named_scope(struct op_context *op_ctx) +{ + uacpi_namespace_node *node; + uacpi_object *obj; + + node = item_array_at(&op_ctx->items, 1)->node; + obj = item_array_last(&op_ctx->items)->obj; + + switch (op_ctx->op->code) { + case UACPI_AML_OP_ProcessorOp: { + uacpi_processor *proc = obj->processor; + proc->id = item_array_at(&op_ctx->items, 2)->immediate; + proc->block_address = item_array_at(&op_ctx->items, 3)->immediate; + proc->block_length = item_array_at(&op_ctx->items, 4)->immediate; + break; + } + + case UACPI_AML_OP_PowerResOp: { + uacpi_power_resource *power_res = &obj->power_resource; + power_res->system_level = item_array_at(&op_ctx->items, 2)->immediate; + power_res->resource_order = item_array_at(&op_ctx->items, 3)->immediate; + break; + } + + default: + break; + } + + node->object = uacpi_create_internal_reference(UACPI_REFERENCE_KIND_NAMED, + obj); + if (uacpi_unlikely(node->object == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +static uacpi_status handle_code_block(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + struct package_length *pkg; + uacpi_bool skip_block; + + pkg = &item_array_at(&op_ctx->items, 0)->pkg; + + switch (op_ctx->op->code) { + case UACPI_AML_OP_ElseOp: + skip_block = ctx->skip_else; + break; + case UACPI_AML_OP_ProcessorOp: + case UACPI_AML_OP_PowerResOp: + case UACPI_AML_OP_ThermalZoneOp: + case UACPI_AML_OP_DeviceOp: { + uacpi_status ret; + + ret = create_named_scope(op_ctx); + if (uacpi_unlikely_error(ret)) + return ret; + + UACPI_FALLTHROUGH; + } + case UACPI_AML_OP_ScopeOp: + skip_block = UACPI_FALSE; + break; + case UACPI_AML_OP_IfOp: + case UACPI_AML_OP_WhileOp: { + uacpi_object *operand; + + operand = item_array_at(&op_ctx->items, 1)->obj; + skip_block = operand->integer == 0; + break; + } + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (skip_block) { + ctx->cur_frame->code_offset = pkg->end; + return UACPI_STATUS_OK; + } + + return begin_block_execution(ctx); +} + +static uacpi_status handle_return(struct execution_context *ctx) +{ + uacpi_status ret; + uacpi_object *dst = UACPI_NULL; + + ctx->cur_frame->code_offset = ctx->cur_frame->method->size; + ret = method_get_ret_object(ctx, &dst); + + if (uacpi_unlikely_error(ret)) + return ret; + if (dst == UACPI_NULL) + return UACPI_STATUS_OK; + + /* + * Should be possible to move here if method returns a literal + * like Return(Buffer { ... }), otherwise we have to copy just to + * be safe. + */ + return uacpi_object_assign( + dst, + item_array_at(&ctx->cur_op_ctx->items, 0)->obj, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY + ); +} + +static void refresh_ctx_pointers(struct execution_context *ctx) +{ + struct call_frame *frame = ctx->cur_frame; + + if (frame == UACPI_NULL) { + ctx->cur_op_ctx = UACPI_NULL; + ctx->prev_op_ctx = UACPI_NULL; + ctx->cur_block = UACPI_NULL; + return; + } + + ctx->cur_op_ctx = op_context_array_last(&frame->pending_ops); + ctx->prev_op_ctx = op_context_array_one_before_last(&frame->pending_ops); + ctx->cur_block = code_block_array_last(&frame->code_blocks); +} + +static uacpi_bool ctx_has_non_preempted_op(struct execution_context *ctx) +{ + return ctx->cur_op_ctx && !ctx->cur_op_ctx->preempted; +} + +enum op_trace_action_type { + OP_TRACE_ACTION_BEGIN, + OP_TRACE_ACTION_RESUME, + OP_TRACE_ACTION_END, +}; + +static const uacpi_char *const op_trace_action_types[3] = { + [OP_TRACE_ACTION_BEGIN] = "BEGIN", + [OP_TRACE_ACTION_RESUME] = "RESUME", + [OP_TRACE_ACTION_END] = "END", +}; + +static inline void trace_op( + const struct uacpi_op_spec *op, enum op_trace_action_type action +) +{ + uacpi_debug( + "%s OP '%s' (0x%04X)\n", + op_trace_action_types[action], op->name, op->code + ); +} + +static inline void trace_pop(uacpi_u8 pop) +{ + uacpi_debug(" pOP: %s (0x%02X)\n", uacpi_parse_op_to_string(pop), pop); +} + +static uacpi_status frame_push_args(struct call_frame *frame, + struct op_context *op_ctx) +{ + uacpi_size i; + + /* + * MethodCall items: + * items[0] -> method namespace node + * items[1] -> immediate that was used for parsing the arguments + * items[2...nargs-1] -> method arguments + * items[-1] -> return value object + * + * Here we only care about the arguments though. + */ + for (i = 2; i < item_array_size(&op_ctx->items) - 1; i++) { + uacpi_object *src, *dst; + + src = item_array_at(&op_ctx->items, i)->obj; + + dst = uacpi_create_internal_reference(UACPI_REFERENCE_KIND_ARG, src); + if (uacpi_unlikely(dst == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + frame->args[i - 2] = dst; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status frame_setup_base_scope(struct call_frame *frame, + uacpi_namespace_node *scope, + uacpi_control_method *method) +{ + struct code_block *block; + + block = code_block_array_alloc(&frame->code_blocks); + if (uacpi_unlikely(block == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + block->type = CODE_BLOCK_SCOPE; + block->node = scope; + block->begin = 0; + block->end = method->size; + frame->method = method; + frame->cur_scope = scope; + return UACPI_STATUS_OK; +} + +static uacpi_status push_new_frame(struct execution_context *ctx, + struct call_frame **out_frame) +{ + struct call_frame_array *call_stack = &ctx->call_stack; + struct call_frame *prev_frame; + + *out_frame = call_frame_array_calloc(call_stack); + if (uacpi_unlikely(*out_frame == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + /* + * Allocating a new frame might have reallocated the dynamic buffer so our + * execution_context members might now be pointing to freed memory. + * Refresh them here. + */ + prev_frame = call_frame_array_one_before_last(call_stack); + ctx->cur_frame = prev_frame; + refresh_ctx_pointers(ctx); + + return UACPI_STATUS_OK; +} + +static uacpi_bool maybe_end_block(struct execution_context *ctx) +{ + struct code_block *block = ctx->cur_block; + struct call_frame *cur_frame = ctx->cur_frame; + + if (!block) + return UACPI_FALSE; + if (cur_frame->code_offset != block->end) + return UACPI_FALSE; + + ctx->skip_else = UACPI_FALSE; + + if (block->type == CODE_BLOCK_WHILE) { + cur_frame->code_offset = block->begin; + } else if (block->type == CODE_BLOCK_IF) { + ctx->skip_else = UACPI_TRUE; + } + + frame_reset_post_end_block(ctx, block->type); + return UACPI_TRUE; +} + +static uacpi_status store_to_target(uacpi_object *dst, uacpi_object *src) +{ + uacpi_status ret; + + switch (dst->type) { + case UACPI_OBJECT_DEBUG: + ret = debug_store(src); + break; + case UACPI_OBJECT_REFERENCE: + ret = store_to_reference(dst, src); + break; + + case UACPI_OBJECT_BUFFER_INDEX: + src = uacpi_unwrap_internal_reference(src); + ret = object_assign_with_implicit_cast(dst, src); + break; + + case UACPI_OBJECT_INTEGER: + // NULL target + if (dst->integer == 0) { + ret = UACPI_STATUS_OK; + break; + } + UACPI_FALLTHROUGH; + default: + uacpi_error("attempted to store to an invalid target: %s\n", + uacpi_object_type_to_string(dst->type)); + ret = UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return ret; +} + +static uacpi_status handle_copy_object_or_store(struct execution_context *ctx) +{ + uacpi_object *src, *dst; + struct op_context *op_ctx = ctx->cur_op_ctx; + + src = item_array_at(&op_ctx->items, 0)->obj; + dst = item_array_at(&op_ctx->items, 1)->obj; + + if (op_ctx->op->code == UACPI_AML_OP_StoreOp) + return store_to_target(dst, src); + + if (dst->type != UACPI_OBJECT_REFERENCE) + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + + return copy_object_to_reference(dst, src); +} + +static uacpi_status handle_inc_dec(struct execution_context *ctx) +{ + uacpi_object *src, *dst; + struct op_context *op_ctx = ctx->cur_op_ctx; + uacpi_bool field_allowed = UACPI_FALSE; + uacpi_object_type true_src_type; + uacpi_status ret; + + src = item_array_at(&op_ctx->items, 0)->obj; + dst = item_array_at(&op_ctx->items, 1)->obj; + + if (src->type == UACPI_OBJECT_REFERENCE) { + /* + * Increment/Decrement are the only two operators that modify the value + * in-place, thus we need very specific dereference rules here. + * + * Reading buffer fields & field units is only allowed if we were passed + * a namestring directly as opposed to some nested reference chain + * containing a field at the bottom. + */ + if (src->flags == UACPI_REFERENCE_KIND_NAMED) + field_allowed = src->inner_object->type != UACPI_OBJECT_REFERENCE; + + src = reference_unwind(src)->inner_object; + } // else buffer index + + true_src_type = src->type; + + switch (true_src_type) { + case UACPI_OBJECT_INTEGER: + dst->integer = src->integer; + break; + case UACPI_OBJECT_FIELD_UNIT: + case UACPI_OBJECT_BUFFER_FIELD: + if (uacpi_unlikely(!field_allowed)) + goto out_bad_type; + + true_src_type = field_get_read_type(src); + if (true_src_type != UACPI_OBJECT_INTEGER) + goto out_bad_type; + + if (src->type == UACPI_OBJECT_FIELD_UNIT) { + ret = uacpi_read_field_unit( + src->field_unit, &dst->integer, sizeof_int() + ); + if (uacpi_unlikely_error(ret)) + return ret; + } else { + uacpi_read_buffer_field(&src->buffer_field, &dst->integer); + } + break; + case UACPI_OBJECT_BUFFER_INDEX: + dst->integer = *buffer_index_cursor(&src->buffer_index); + break; + default: + goto out_bad_type; + } + + if (op_ctx->op->code == UACPI_AML_OP_IncrementOp) + dst->integer++; + else + dst->integer--; + + return UACPI_STATUS_OK; + +out_bad_type: + uacpi_error("Increment/Decrement: invalid object type '%s'\n", + uacpi_object_type_to_string(true_src_type)); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; +} + +static uacpi_status enter_method( + struct execution_context *ctx, struct call_frame *new_frame, + uacpi_control_method *method +) +{ + uacpi_status ret = UACPI_STATUS_OK; + + uacpi_shareable_ref(method); + + if (!method->is_serialized) + return ret; + + if (uacpi_unlikely(ctx->sync_level > method->sync_level)) { + uacpi_error( + "cannot invoke method @%p, sync level %d is too low " + "(current is %d)\n", + method, method->sync_level, ctx->sync_level + ); + return UACPI_STATUS_AML_SYNC_LEVEL_TOO_HIGH; + } + + if (method->mutex == UACPI_NULL) { + method->mutex = uacpi_create_mutex(); + if (uacpi_unlikely(method->mutex == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + method->mutex->sync_level = method->sync_level; + } + + if (!uacpi_this_thread_owns_aml_mutex(method->mutex)) { + ret = uacpi_acquire_aml_mutex(method->mutex, 0xFFFF); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = held_mutexes_array_push(&ctx->held_mutexes, method->mutex); + if (uacpi_unlikely_error(ret)) { + uacpi_release_aml_mutex(method->mutex); + return ret; + } + } + + new_frame->prev_sync_level = ctx->sync_level; + ctx->sync_level = method->sync_level; + return UACPI_STATUS_OK; +} + +static uacpi_status push_op(struct execution_context *ctx) +{ + struct call_frame *frame = ctx->cur_frame; + struct op_context *op_ctx; + + op_ctx = op_context_array_calloc(&frame->pending_ops); + if (op_ctx == UACPI_NULL) + return UACPI_STATUS_OUT_OF_MEMORY; + + op_ctx->op = ctx->cur_op; + refresh_ctx_pointers(ctx); + return UACPI_STATUS_OK; +} + +static uacpi_bool pop_item(struct op_context *op_ctx) +{ + struct item *item; + + if (item_array_size(&op_ctx->items) == 0) + return UACPI_FALSE; + + item = item_array_last(&op_ctx->items); + + if (item->type == ITEM_OBJECT) + uacpi_object_unref(item->obj); + + if (item->type == ITEM_NAMESPACE_NODE) + uacpi_namespace_node_unref(item->node); + + item_array_pop(&op_ctx->items); + return UACPI_TRUE; +} + +static void pop_op(struct execution_context *ctx) +{ + struct call_frame *frame = ctx->cur_frame; + struct op_context *cur_op_ctx = ctx->cur_op_ctx; + + while (pop_item(cur_op_ctx)); + + item_array_clear(&cur_op_ctx->items); + op_context_array_pop(&frame->pending_ops); + refresh_ctx_pointers(ctx); +} + +static void call_frame_clear(struct call_frame *frame) +{ + uacpi_size i; + op_context_array_clear(&frame->pending_ops); + code_block_array_clear(&frame->code_blocks); + + while (temp_namespace_node_array_size(&frame->temp_nodes) != 0) { + uacpi_namespace_node *node; + + node = *temp_namespace_node_array_last(&frame->temp_nodes); + uacpi_namespace_node_uninstall(node); + temp_namespace_node_array_pop(&frame->temp_nodes); + } + temp_namespace_node_array_clear(&frame->temp_nodes); + + for (i = 0; i < 7; ++i) + uacpi_object_unref(frame->args[i]); + for (i = 0; i < 8; ++i) + uacpi_object_unref(frame->locals[i]); + + uacpi_method_unref(frame->method); +} + +static uacpi_u8 parse_op_generates_item[0x100] = { + [UACPI_PARSE_OP_SIMPLE_NAME] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_SUPERNAME] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_TERM_ARG] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_OPERAND] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_STRING] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_COMPUTATIONAL_DATA] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_TARGET] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_PKGLEN] = ITEM_PACKAGE_LENGTH, + [UACPI_PARSE_OP_TRACKED_PKGLEN] = ITEM_PACKAGE_LENGTH, + [UACPI_PARSE_OP_CREATE_NAMESTRING] = ITEM_NAMESPACE_NODE, + [UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD] = ITEM_NAMESPACE_NODE, + [UACPI_PARSE_OP_EXISTING_NAMESTRING] = ITEM_NAMESPACE_NODE, + [UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL] = ITEM_NAMESPACE_NODE, + [UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD] = ITEM_NAMESPACE_NODE, + [UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT] = ITEM_OBJECT, + [UACPI_PARSE_OP_LOAD_INLINE_IMM] = ITEM_IMMEDIATE, + [UACPI_PARSE_OP_LOAD_ZERO_IMM] = ITEM_IMMEDIATE, + [UACPI_PARSE_OP_LOAD_IMM] = ITEM_IMMEDIATE, + [UACPI_PARSE_OP_LOAD_IMM_AS_OBJECT] = ITEM_OBJECT, + [UACPI_PARSE_OP_LOAD_FALSE_OBJECT] = ITEM_OBJECT, + [UACPI_PARSE_OP_LOAD_TRUE_OBJECT] = ITEM_OBJECT, + [UACPI_PARSE_OP_OBJECT_ALLOC] = ITEM_OBJECT, + [UACPI_PARSE_OP_OBJECT_ALLOC_TYPED] = ITEM_OBJECT, + [UACPI_PARSE_OP_EMPTY_OBJECT_ALLOC] = ITEM_EMPTY_OBJECT, + [UACPI_PARSE_OP_OBJECT_CONVERT_TO_SHALLOW_COPY] = ITEM_OBJECT, + [UACPI_PARSE_OP_OBJECT_CONVERT_TO_DEEP_COPY] = ITEM_OBJECT, + [UACPI_PARSE_OP_RECORD_AML_PC] = ITEM_IMMEDIATE, +}; + +static const uacpi_u8 *op_decode_cursor(const struct op_context *ctx) +{ + const struct uacpi_op_spec *spec = ctx->op; + + if (spec->properties & UACPI_OP_PROPERTY_OUT_OF_LINE) + return &spec->indirect_decode_ops[ctx->pc]; + + return &spec->decode_ops[ctx->pc]; +} + +static uacpi_u8 op_decode_byte(struct op_context *ctx) +{ + uacpi_u8 byte; + + byte = *op_decode_cursor(ctx); + ctx->pc++; + + return byte; +} + +// MSVC doesn't support __VA_OPT__ so we do this weirdness +#define EXEC_OP_DO_LVL(lvl, reason, ...) \ + uacpi_##lvl("Op 0x%04X ('%s'): "reason"\n", \ + op_ctx->op->code, op_ctx->op->name __VA_ARGS__) + +#define EXEC_OP_DO_ERR(reason, ...) EXEC_OP_DO_LVL(error, reason, __VA_ARGS__) +#define EXEC_OP_DO_WARN(reason, ...) EXEC_OP_DO_LVL(warn, reason, __VA_ARGS__) + +#define EXEC_OP_ERR_2(reason, arg0, arg1) EXEC_OP_DO_ERR(reason, ,arg0, arg1) +#define EXEC_OP_ERR_1(reason, arg0) EXEC_OP_DO_ERR(reason, ,arg0) +#define EXEC_OP_ERR(reason) EXEC_OP_DO_ERR(reason) + +#define EXEC_OP_WARN(reason) EXEC_OP_DO_WARN(reason) + +#define SPEC_SIMPLE_NAME "SimpleName := NameString | ArgObj | LocalObj" +#define SPEC_SUPER_NAME \ + "SuperName := SimpleName | DebugObj | ReferenceTypeOpcode" +#define SPEC_TERM_ARG \ + "TermArg := ExpressionOpcode | DataObject | ArgObj | LocalObj" +#define SPEC_OPERAND "Operand := TermArg => Integer" +#define SPEC_STRING "String := TermArg => String" +#define SPEC_TARGET "Target := SuperName | NullName" + +#define SPEC_COMPUTATIONAL_DATA \ + "ComputationalData := ByteConst | WordConst | DWordConst | QWordConst " \ + "| String | ConstObj | RevisionOp | DefBuffer" + +static uacpi_bool op_wants_supername(enum uacpi_parse_op op) +{ + switch (op) { + case UACPI_PARSE_OP_SIMPLE_NAME: + case UACPI_PARSE_OP_SUPERNAME: + case UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED: + case UACPI_PARSE_OP_TARGET: + return UACPI_TRUE; + default: + return UACPI_FALSE; + } +} + +static uacpi_bool op_wants_term_arg_or_operand(enum uacpi_parse_op op) +{ + switch (op) { + case UACPI_PARSE_OP_TERM_ARG: + case UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL: + case UACPI_PARSE_OP_OPERAND: + case UACPI_PARSE_OP_STRING: + case UACPI_PARSE_OP_COMPUTATIONAL_DATA: + return UACPI_TRUE; + default: + return UACPI_FALSE; + } +} + +static uacpi_bool op_allows_unresolved(enum uacpi_parse_op op) +{ + switch (op) { + case UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED: + case UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL: + return UACPI_TRUE; + default: + return UACPI_FALSE; + } +} + +static uacpi_bool op_allows_unresolved_if_load(enum uacpi_parse_op op) +{ + switch (op) { + case UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD: + case UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD: + return UACPI_TRUE; + default: + return UACPI_FALSE; + } +} + +static uacpi_status op_typecheck(const struct op_context *op_ctx, + const struct op_context *cur_op_ctx) +{ + const uacpi_char *expected_type_str; + uacpi_u8 ok_mask = 0; + uacpi_u8 props = cur_op_ctx->op->properties; + + switch (*op_decode_cursor(op_ctx)) { + // SimpleName := NameString | ArgObj | LocalObj + case UACPI_PARSE_OP_SIMPLE_NAME: + expected_type_str = SPEC_SIMPLE_NAME; + ok_mask |= UACPI_OP_PROPERTY_SIMPLE_NAME; + break; + + // Target := SuperName | NullName + case UACPI_PARSE_OP_TARGET: + expected_type_str = SPEC_TARGET; + ok_mask |= UACPI_OP_PROPERTY_TARGET | UACPI_OP_PROPERTY_SUPERNAME; + break; + + // SuperName := SimpleName | DebugObj | ReferenceTypeOpcode + case UACPI_PARSE_OP_SUPERNAME: + case UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED: + expected_type_str = SPEC_SUPER_NAME; + ok_mask |= UACPI_OP_PROPERTY_SUPERNAME; + break; + + // TermArg := ExpressionOpcode | DataObject | ArgObj | LocalObj + case UACPI_PARSE_OP_TERM_ARG: + case UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED: + case UACPI_PARSE_OP_OPERAND: + case UACPI_PARSE_OP_STRING: + case UACPI_PARSE_OP_COMPUTATIONAL_DATA: + expected_type_str = SPEC_TERM_ARG; + ok_mask |= UACPI_OP_PROPERTY_TERM_ARG; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (!(props & ok_mask)) { + EXEC_OP_ERR_2("invalid argument: '%s', expected a %s", + cur_op_ctx->op->name, expected_type_str); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status typecheck_obj( + const struct op_context *op_ctx, + const uacpi_object *obj, + enum uacpi_object_type expected_type, + const uacpi_char *spec_desc +) +{ + if (uacpi_likely(obj->type == expected_type)) + return UACPI_STATUS_OK; + + EXEC_OP_ERR_2("invalid argument type: %s, expected a %s", + uacpi_object_type_to_string(obj->type), spec_desc); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; +} + +static uacpi_status typecheck_operand( + const struct op_context *op_ctx, + const uacpi_object *obj +) +{ + return typecheck_obj(op_ctx, obj, UACPI_OBJECT_INTEGER, SPEC_OPERAND); +} + +static uacpi_status typecheck_string( + const struct op_context *op_ctx, + const uacpi_object *obj +) +{ + return typecheck_obj(op_ctx, obj, UACPI_OBJECT_STRING, SPEC_STRING); +} + +static uacpi_status typecheck_computational_data( + const struct op_context *op_ctx, + const uacpi_object *obj +) +{ + switch (obj->type) { + case UACPI_OBJECT_STRING: + case UACPI_OBJECT_BUFFER: + case UACPI_OBJECT_INTEGER: + return UACPI_STATUS_OK; + default: + EXEC_OP_ERR_2( + "invalid argument type: %s, expected a %s", + uacpi_object_type_to_string(obj->type), + SPEC_COMPUTATIONAL_DATA + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } +} + +static void trace_named_object_lookup_or_creation_failure( + struct call_frame *frame, uacpi_size offset, enum uacpi_parse_op op, + uacpi_status ret, enum uacpi_log_level level +) +{ + static const uacpi_char *oom_prefix = "<...>"; + static const uacpi_char *empty_string = ""; + static const uacpi_char *unknown_path = ""; + static const uacpi_char *invalid_path = ""; + + uacpi_status conv_ret; + const uacpi_char *action; + const uacpi_char *requested_path_to_print; + const uacpi_char *middle_part = UACPI_NULL; + const uacpi_char *prefix_path = UACPI_NULL; + uacpi_char *requested_path = UACPI_NULL; + uacpi_size length; + uacpi_bool is_create; + + is_create = op == UACPI_PARSE_OP_CREATE_NAMESTRING || + op == UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD; + + if (is_create) + action = "create"; + else + action = "lookup"; + + conv_ret = name_string_to_path( + frame, offset, &requested_path, &length + ); + if (uacpi_unlikely_error(conv_ret)) { + if (conv_ret == UACPI_STATUS_OUT_OF_MEMORY) + requested_path_to_print = unknown_path; + else + requested_path_to_print = invalid_path; + } else { + requested_path_to_print = requested_path; + } + + if (requested_path && requested_path[0] != '\\') { + prefix_path = uacpi_namespace_node_generate_absolute_path( + frame->cur_scope + ); + if (uacpi_unlikely(prefix_path == UACPI_NULL)) + prefix_path = oom_prefix; + + if (prefix_path[1] != '\0') + middle_part = "."; + } else { + prefix_path = empty_string; + } + + if (middle_part == UACPI_NULL) + middle_part = empty_string; + + if (length == 5 && !is_create) { + uacpi_log_lvl( + level, + "unable to %s named object '%s' within (or above) " + "scope '%s': %s\n", action, requested_path_to_print, + prefix_path, uacpi_status_to_string(ret) + ); + } else { + uacpi_log_lvl( + level, + "unable to %s named object '%s%s%s': %s\n", + action, prefix_path, middle_part, + requested_path_to_print, uacpi_status_to_string(ret) + ); + } + + uacpi_free(requested_path, length); + if (prefix_path != oom_prefix && prefix_path != empty_string) + uacpi_free_dynamic_string(prefix_path); +} + +static uacpi_status uninstalled_op_handler(struct execution_context *ctx) +{ + struct op_context *op_ctx = ctx->cur_op_ctx; + + EXEC_OP_ERR("no dedicated handler installed"); + return UACPI_STATUS_UNIMPLEMENTED; +} + +enum op_handler { + OP_HANDLER_UNINSTALLED = 0, + OP_HANDLER_LOCAL, + OP_HANDLER_ARG, + OP_HANDLER_STRING, + OP_HANDLER_BINARY_MATH, + OP_HANDLER_CONTROL_FLOW, + OP_HANDLER_CODE_BLOCK, + OP_HANDLER_RETURN, + OP_HANDLER_CREATE_METHOD, + OP_HANDLER_COPY_OBJECT_OR_STORE, + OP_HANDLER_INC_DEC, + OP_HANDLER_REF_OR_DEREF_OF, + OP_HANDLER_LOGICAL_NOT, + OP_HANDLER_BINARY_LOGIC, + OP_HANDLER_NAMED_OBJECT, + OP_HANDLER_BUFFER, + OP_HANDLER_PACKAGE, + OP_HANDLER_CREATE_NAMED, + OP_HANDLER_CREATE_BUFFER_FIELD, + OP_HANDLER_READ_FIELD, + OP_HANDLER_ALIAS, + OP_HANDLER_CONCATENATE, + OP_HANDLER_CONCATENATE_RES, + OP_HANDLER_SIZEOF, + OP_HANDLER_UNARY_MATH, + OP_HANDLER_INDEX, + OP_HANDLER_OBJECT_TYPE, + OP_HANDLER_CREATE_OP_REGION, + OP_HANDLER_CREATE_DATA_REGION, + OP_HANDLER_CREATE_FIELD, + OP_HANDLER_TO, + OP_HANDLER_TO_STRING, + OP_HANDLER_TIMER, + OP_HANDLER_MID, + OP_HANDLER_MATCH, + OP_HANDLER_CREATE_MUTEX_OR_EVENT, + OP_HANDLER_BCD, + OP_HANDLER_LOAD_TABLE, + OP_HANDLER_LOAD, + OP_HANDLER_STALL_OR_SLEEP, + OP_HANDLER_EVENT_CTL, + OP_HANDLER_MUTEX_CTL, + OP_HANDLER_NOTIFY, + OP_HANDLER_FIRMWARE_REQUEST, +}; + +static uacpi_status (*op_handlers[])(struct execution_context *ctx) = { + /* + * All OPs that don't have a handler dispatch to here if + * UACPI_PARSE_OP_INVOKE_HANDLER is reached. + */ + [OP_HANDLER_UNINSTALLED] = uninstalled_op_handler, + [OP_HANDLER_LOCAL] = handle_local, + [OP_HANDLER_ARG] = handle_arg, + [OP_HANDLER_NAMED_OBJECT] = handle_named_object, + [OP_HANDLER_STRING] = handle_string, + [OP_HANDLER_BINARY_MATH] = handle_binary_math, + [OP_HANDLER_CONTROL_FLOW] = handle_control_flow, + [OP_HANDLER_CODE_BLOCK] = handle_code_block, + [OP_HANDLER_RETURN] = handle_return, + [OP_HANDLER_CREATE_METHOD] = handle_create_method, + [OP_HANDLER_CREATE_MUTEX_OR_EVENT] = handle_create_mutex_or_event, + [OP_HANDLER_COPY_OBJECT_OR_STORE] = handle_copy_object_or_store, + [OP_HANDLER_INC_DEC] = handle_inc_dec, + [OP_HANDLER_REF_OR_DEREF_OF] = handle_ref_or_deref_of, + [OP_HANDLER_LOGICAL_NOT] = handle_logical_not, + [OP_HANDLER_BINARY_LOGIC] = handle_binary_logic, + [OP_HANDLER_BUFFER] = handle_buffer, + [OP_HANDLER_PACKAGE] = handle_package, + [OP_HANDLER_CREATE_NAMED] = handle_create_named, + [OP_HANDLER_CREATE_BUFFER_FIELD] = handle_create_buffer_field, + [OP_HANDLER_READ_FIELD] = handle_field_read, + [OP_HANDLER_TO] = handle_to, + [OP_HANDLER_ALIAS] = handle_create_alias, + [OP_HANDLER_CONCATENATE] = handle_concatenate, + [OP_HANDLER_CONCATENATE_RES] = handle_concatenate_res, + [OP_HANDLER_SIZEOF] = handle_sizeof, + [OP_HANDLER_UNARY_MATH] = handle_unary_math, + [OP_HANDLER_INDEX] = handle_index, + [OP_HANDLER_OBJECT_TYPE] = handle_object_type, + [OP_HANDLER_CREATE_OP_REGION] = handle_create_op_region, + [OP_HANDLER_CREATE_DATA_REGION] = handle_create_data_region, + [OP_HANDLER_CREATE_FIELD] = handle_create_field, + [OP_HANDLER_TIMER] = handle_timer, + [OP_HANDLER_TO_STRING] = handle_to_string, + [OP_HANDLER_MID] = handle_mid, + [OP_HANDLER_MATCH] = handle_match, + [OP_HANDLER_BCD] = handle_bcd, + [OP_HANDLER_LOAD_TABLE] = handle_load_table, + [OP_HANDLER_LOAD] = handle_load, + [OP_HANDLER_STALL_OR_SLEEP] = handle_stall_or_sleep, + [OP_HANDLER_EVENT_CTL] = handle_event_ctl, + [OP_HANDLER_MUTEX_CTL] = handle_mutex_ctl, + [OP_HANDLER_NOTIFY] = handle_notify, + [OP_HANDLER_FIRMWARE_REQUEST] = handle_firmware_request, +}; + +static uacpi_u8 handler_idx_of_op[0x100] = { + [UACPI_AML_OP_Local0Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local1Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local2Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local3Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local4Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local5Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local6Op] = OP_HANDLER_LOCAL, + [UACPI_AML_OP_Local7Op] = OP_HANDLER_LOCAL, + + [UACPI_AML_OP_Arg0Op] = OP_HANDLER_ARG, + [UACPI_AML_OP_Arg1Op] = OP_HANDLER_ARG, + [UACPI_AML_OP_Arg2Op] = OP_HANDLER_ARG, + [UACPI_AML_OP_Arg3Op] = OP_HANDLER_ARG, + [UACPI_AML_OP_Arg4Op] = OP_HANDLER_ARG, + [UACPI_AML_OP_Arg5Op] = OP_HANDLER_ARG, + [UACPI_AML_OP_Arg6Op] = OP_HANDLER_ARG, + + [UACPI_AML_OP_StringPrefix] = OP_HANDLER_STRING, + + [UACPI_AML_OP_AddOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_SubtractOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_MultiplyOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_DivideOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_ShiftLeftOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_ShiftRightOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_AndOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_NandOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_OrOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_NorOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_XorOp] = OP_HANDLER_BINARY_MATH, + [UACPI_AML_OP_ModOp] = OP_HANDLER_BINARY_MATH, + + [UACPI_AML_OP_IfOp] = OP_HANDLER_CODE_BLOCK, + [UACPI_AML_OP_ElseOp] = OP_HANDLER_CODE_BLOCK, + [UACPI_AML_OP_WhileOp] = OP_HANDLER_CODE_BLOCK, + [UACPI_AML_OP_ScopeOp] = OP_HANDLER_CODE_BLOCK, + + [UACPI_AML_OP_ContinueOp] = OP_HANDLER_CONTROL_FLOW, + [UACPI_AML_OP_BreakOp] = OP_HANDLER_CONTROL_FLOW, + + [UACPI_AML_OP_ReturnOp] = OP_HANDLER_RETURN, + + [UACPI_AML_OP_MethodOp] = OP_HANDLER_CREATE_METHOD, + + [UACPI_AML_OP_StoreOp] = OP_HANDLER_COPY_OBJECT_OR_STORE, + [UACPI_AML_OP_CopyObjectOp] = OP_HANDLER_COPY_OBJECT_OR_STORE, + + [UACPI_AML_OP_IncrementOp] = OP_HANDLER_INC_DEC, + [UACPI_AML_OP_DecrementOp] = OP_HANDLER_INC_DEC, + + [UACPI_AML_OP_RefOfOp] = OP_HANDLER_REF_OR_DEREF_OF, + [UACPI_AML_OP_DerefOfOp] = OP_HANDLER_REF_OR_DEREF_OF, + + [UACPI_AML_OP_LnotOp] = OP_HANDLER_LOGICAL_NOT, + + [UACPI_AML_OP_LEqualOp] = OP_HANDLER_BINARY_LOGIC, + [UACPI_AML_OP_LandOp] = OP_HANDLER_BINARY_LOGIC, + [UACPI_AML_OP_LorOp] = OP_HANDLER_BINARY_LOGIC, + [UACPI_AML_OP_LGreaterOp] = OP_HANDLER_BINARY_LOGIC, + [UACPI_AML_OP_LLessOp] = OP_HANDLER_BINARY_LOGIC, + + [UACPI_AML_OP_InternalOpNamedObject] = OP_HANDLER_NAMED_OBJECT, + + [UACPI_AML_OP_BufferOp] = OP_HANDLER_BUFFER, + + [UACPI_AML_OP_PackageOp] = OP_HANDLER_PACKAGE, + [UACPI_AML_OP_VarPackageOp] = OP_HANDLER_PACKAGE, + + [UACPI_AML_OP_NameOp] = OP_HANDLER_CREATE_NAMED, + + [UACPI_AML_OP_CreateBitFieldOp] = OP_HANDLER_CREATE_BUFFER_FIELD, + [UACPI_AML_OP_CreateByteFieldOp] = OP_HANDLER_CREATE_BUFFER_FIELD, + [UACPI_AML_OP_CreateWordFieldOp] = OP_HANDLER_CREATE_BUFFER_FIELD, + [UACPI_AML_OP_CreateDWordFieldOp] = OP_HANDLER_CREATE_BUFFER_FIELD, + [UACPI_AML_OP_CreateQWordFieldOp] = OP_HANDLER_CREATE_BUFFER_FIELD, + + [UACPI_AML_OP_InternalOpReadFieldAsBuffer] = OP_HANDLER_READ_FIELD, + [UACPI_AML_OP_InternalOpReadFieldAsInteger] = OP_HANDLER_READ_FIELD, + + [UACPI_AML_OP_ToIntegerOp] = OP_HANDLER_TO, + [UACPI_AML_OP_ToBufferOp] = OP_HANDLER_TO, + [UACPI_AML_OP_ToDecimalStringOp] = OP_HANDLER_TO, + [UACPI_AML_OP_ToHexStringOp] = OP_HANDLER_TO, + [UACPI_AML_OP_ToStringOp] = OP_HANDLER_TO_STRING, + + [UACPI_AML_OP_AliasOp] = OP_HANDLER_ALIAS, + + [UACPI_AML_OP_ConcatOp] = OP_HANDLER_CONCATENATE, + [UACPI_AML_OP_ConcatResOp] = OP_HANDLER_CONCATENATE_RES, + + [UACPI_AML_OP_SizeOfOp] = OP_HANDLER_SIZEOF, + + [UACPI_AML_OP_NotOp] = OP_HANDLER_UNARY_MATH, + [UACPI_AML_OP_FindSetLeftBitOp] = OP_HANDLER_UNARY_MATH, + [UACPI_AML_OP_FindSetRightBitOp] = OP_HANDLER_UNARY_MATH, + + [UACPI_AML_OP_IndexOp] = OP_HANDLER_INDEX, + + [UACPI_AML_OP_ObjectTypeOp] = OP_HANDLER_OBJECT_TYPE, + + [UACPI_AML_OP_MidOp] = OP_HANDLER_MID, + + [UACPI_AML_OP_MatchOp] = OP_HANDLER_MATCH, + + [UACPI_AML_OP_NotifyOp] = OP_HANDLER_NOTIFY, + + [UACPI_AML_OP_BreakPointOp] = OP_HANDLER_FIRMWARE_REQUEST, +}; + +#define EXT_OP_IDX(op) (op & 0xFF) + +static uacpi_u8 handler_idx_of_ext_op[0x100] = { + [EXT_OP_IDX(UACPI_AML_OP_CreateFieldOp)] = OP_HANDLER_CREATE_BUFFER_FIELD, + [EXT_OP_IDX(UACPI_AML_OP_CondRefOfOp)] = OP_HANDLER_REF_OR_DEREF_OF, + [EXT_OP_IDX(UACPI_AML_OP_OpRegionOp)] = OP_HANDLER_CREATE_OP_REGION, + [EXT_OP_IDX(UACPI_AML_OP_DeviceOp)] = OP_HANDLER_CODE_BLOCK, + [EXT_OP_IDX(UACPI_AML_OP_ProcessorOp)] = OP_HANDLER_CODE_BLOCK, + [EXT_OP_IDX(UACPI_AML_OP_PowerResOp)] = OP_HANDLER_CODE_BLOCK, + [EXT_OP_IDX(UACPI_AML_OP_ThermalZoneOp)] = OP_HANDLER_CODE_BLOCK, + [EXT_OP_IDX(UACPI_AML_OP_TimerOp)] = OP_HANDLER_TIMER, + [EXT_OP_IDX(UACPI_AML_OP_MutexOp)] = OP_HANDLER_CREATE_MUTEX_OR_EVENT, + [EXT_OP_IDX(UACPI_AML_OP_EventOp)] = OP_HANDLER_CREATE_MUTEX_OR_EVENT, + + [EXT_OP_IDX(UACPI_AML_OP_FieldOp)] = OP_HANDLER_CREATE_FIELD, + [EXT_OP_IDX(UACPI_AML_OP_IndexFieldOp)] = OP_HANDLER_CREATE_FIELD, + [EXT_OP_IDX(UACPI_AML_OP_BankFieldOp)] = OP_HANDLER_CREATE_FIELD, + + [EXT_OP_IDX(UACPI_AML_OP_FromBCDOp)] = OP_HANDLER_BCD, + [EXT_OP_IDX(UACPI_AML_OP_ToBCDOp)] = OP_HANDLER_BCD, + + [EXT_OP_IDX(UACPI_AML_OP_DataRegionOp)] = OP_HANDLER_CREATE_DATA_REGION, + + [EXT_OP_IDX(UACPI_AML_OP_LoadTableOp)] = OP_HANDLER_LOAD_TABLE, + [EXT_OP_IDX(UACPI_AML_OP_LoadOp)] = OP_HANDLER_LOAD, + + [EXT_OP_IDX(UACPI_AML_OP_StallOp)] = OP_HANDLER_STALL_OR_SLEEP, + [EXT_OP_IDX(UACPI_AML_OP_SleepOp)] = OP_HANDLER_STALL_OR_SLEEP, + + [EXT_OP_IDX(UACPI_AML_OP_SignalOp)] = OP_HANDLER_EVENT_CTL, + [EXT_OP_IDX(UACPI_AML_OP_ResetOp)] = OP_HANDLER_EVENT_CTL, + [EXT_OP_IDX(UACPI_AML_OP_WaitOp)] = OP_HANDLER_EVENT_CTL, + + [EXT_OP_IDX(UACPI_AML_OP_AcquireOp)] = OP_HANDLER_MUTEX_CTL, + [EXT_OP_IDX(UACPI_AML_OP_ReleaseOp)] = OP_HANDLER_MUTEX_CTL, + + [EXT_OP_IDX(UACPI_AML_OP_FatalOp)] = OP_HANDLER_FIRMWARE_REQUEST, +}; + +enum method_call_type { + METHOD_CALL_NATIVE, + METHOD_CALL_AML, + METHOD_CALL_TABLE_LOAD, +}; + +static uacpi_status prepare_method_call( + struct execution_context *ctx, uacpi_namespace_node *node, + uacpi_control_method *method, enum method_call_type type, + const uacpi_object_array *args +) +{ + uacpi_status ret; + struct call_frame *frame; + + if (uacpi_unlikely(call_frame_array_size(&ctx->call_stack) >= + g_uacpi_rt_ctx.max_call_stack_depth)) + return UACPI_STATUS_AML_CALL_STACK_DEPTH_LIMIT; + + ret = push_new_frame(ctx, &frame); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = enter_method(ctx, frame, method); + if (uacpi_unlikely_error(ret)) + goto method_dispatch_error; + + if (type == METHOD_CALL_NATIVE) { + uacpi_u8 arg_count; + + arg_count = args ? args->count : 0; + if (uacpi_unlikely(arg_count != method->args)) { + uacpi_error( + "invalid number of arguments %zu to call %.4s, expected %d\n", + args ? args->count : 0, node->name.text, method->args + ); + + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto method_dispatch_error; + } + + if (args != UACPI_NULL) { + uacpi_u8 i; + + for (i = 0; i < method->args; ++i) { + frame->args[i] = args->objects[i]; + uacpi_object_ref(args->objects[i]); + } + } + } else if (type == METHOD_CALL_AML) { + ret = frame_push_args(frame, ctx->cur_op_ctx); + if (uacpi_unlikely_error(ret)) + goto method_dispatch_error; + } + + ret = frame_setup_base_scope(frame, node, method); + if (uacpi_unlikely_error(ret)) + goto method_dispatch_error; + + ctx->cur_frame = frame; + ctx->cur_op_ctx = UACPI_NULL; + ctx->prev_op_ctx = UACPI_NULL; + ctx->cur_block = code_block_array_last(&ctx->cur_frame->code_blocks); + + if (method->native_call) { + uacpi_object *retval; + + ret = method_get_ret_object(ctx, &retval); + if (uacpi_unlikely_error(ret)) + goto method_dispatch_error; + + return method->handler(ctx, retval); + } + + return UACPI_STATUS_OK; + +method_dispatch_error: + call_frame_clear(frame); + call_frame_array_pop(&ctx->call_stack); + return ret; +} + +static uacpi_status exec_op(struct execution_context *ctx) +{ + uacpi_status ret = UACPI_STATUS_OK; + struct call_frame *frame = ctx->cur_frame; + struct op_context *op_ctx; + struct item *item = UACPI_NULL; + enum uacpi_parse_op prev_op = 0, op; + + /* + * Allocate a new op context if previous is preempted (looking for a + * dynamic argument), or doesn't exist at all. + */ + if (!ctx_has_non_preempted_op(ctx)) { + ret = push_op(ctx); + if (uacpi_unlikely_error(ret)) + return ret; + } else { + trace_op(ctx->cur_op_ctx->op, OP_TRACE_ACTION_RESUME); + } + + if (ctx->prev_op_ctx) + prev_op = *op_decode_cursor(ctx->prev_op_ctx); + + for (;;) { + if (uacpi_unlikely_error(ret)) + return ret; + + op_ctx = ctx->cur_op_ctx; + frame = ctx->cur_frame; + + if (op_ctx->pc == 0 && ctx->prev_op_ctx) { + /* + * Type check the current arg type against what is expected by the + * preempted op. This check is able to catch most type violations + * with the only exception being Operand as we only know whether + * that evaluates to an integer after the fact. + */ + ret = op_typecheck(ctx->prev_op_ctx, ctx->cur_op_ctx); + if (uacpi_unlikely_error(ret)) + return ret; + } + + op = op_decode_byte(op_ctx); + trace_pop(op); + + if (parse_op_generates_item[op] != ITEM_NONE) { + item = item_array_alloc(&op_ctx->items); + if (uacpi_unlikely(item == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + item->type = parse_op_generates_item[op]; + if (item->type == ITEM_OBJECT) { + enum uacpi_object_type type = UACPI_OBJECT_UNINITIALIZED; + + if (op == UACPI_PARSE_OP_OBJECT_ALLOC_TYPED) + type = op_decode_byte(op_ctx); + + item->obj = uacpi_create_object(type); + if (uacpi_unlikely(item->obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + } else { + uacpi_memzero(&item->immediate, sizeof(item->immediate)); + } + } else if (item == UACPI_NULL) { + item = item_array_last(&op_ctx->items); + } + + switch (op) { + case UACPI_PARSE_OP_END: + case UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL: { + trace_op(ctx->cur_op_ctx->op, OP_TRACE_ACTION_END); + + if (op == UACPI_PARSE_OP_SKIP_WITH_WARN_IF_NULL) { + uacpi_u8 idx; + + idx = op_decode_byte(op_ctx); + if (item_array_at(&op_ctx->items, idx)->handle != UACPI_NULL) + break; + + EXEC_OP_WARN("skipping due to previous errors"); + } + + if (op_ctx->tracked_pkg_idx) { + item = item_array_at(&op_ctx->items, op_ctx->tracked_pkg_idx - 1); + frame->code_offset = item->pkg.end; + } + + pop_op(ctx); + if (ctx->cur_op_ctx) { + ctx->cur_op_ctx->preempted = UACPI_FALSE; + ctx->cur_op_ctx->pc++; + } + + return UACPI_STATUS_OK; + } + + case UACPI_PARSE_OP_SIMPLE_NAME: + case UACPI_PARSE_OP_SUPERNAME: + case UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED: + case UACPI_PARSE_OP_TERM_ARG: + case UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED: + case UACPI_PARSE_OP_OPERAND: + case UACPI_PARSE_OP_STRING: + case UACPI_PARSE_OP_COMPUTATIONAL_DATA: + case UACPI_PARSE_OP_TARGET: + /* + * Preempt this op parsing for now as we wait for the dynamic arg + * to be parsed. + */ + op_ctx->preempted = UACPI_TRUE; + op_ctx->pc--; + return UACPI_STATUS_OK; + + case UACPI_PARSE_OP_TRACKED_PKGLEN: + op_ctx->tracked_pkg_idx = item_array_size(&op_ctx->items); + UACPI_FALLTHROUGH; + case UACPI_PARSE_OP_PKGLEN: + ret = parse_package_length(frame, &item->pkg); + break; + + case UACPI_PARSE_OP_LOAD_INLINE_IMM: + case UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT: { + void *dst; + uacpi_u8 src_width; + + if (op == UACPI_PARSE_OP_LOAD_INLINE_IMM_AS_OBJECT) { + item->obj->type = UACPI_OBJECT_INTEGER; + dst = &item->obj->integer; + src_width = 8; + } else { + dst = &item->immediate; + src_width = op_decode_byte(op_ctx); + } + + uacpi_memcpy_zerout( + dst, op_decode_cursor(op_ctx), + sizeof(uacpi_u64), src_width + ); + op_ctx->pc += src_width; + break; + } + + case UACPI_PARSE_OP_LOAD_ZERO_IMM: + break; + + case UACPI_PARSE_OP_LOAD_IMM: + case UACPI_PARSE_OP_LOAD_IMM_AS_OBJECT: { + uacpi_u8 width; + void *dst; + + width = op_decode_byte(op_ctx); + if (uacpi_unlikely(call_frame_code_bytes_left(frame) < width)) + return UACPI_STATUS_AML_BAD_ENCODING; + + if (op == UACPI_PARSE_OP_LOAD_IMM_AS_OBJECT) { + item->obj->type = UACPI_OBJECT_INTEGER; + item->obj->integer = 0; + dst = &item->obj->integer; + } else { + dst = item->immediate_bytes; + } + + uacpi_memcpy(dst, call_frame_cursor(frame), width); + frame->code_offset += width; + break; + } + + case UACPI_PARSE_OP_LOAD_FALSE_OBJECT: + case UACPI_PARSE_OP_LOAD_TRUE_OBJECT: { + uacpi_object *obj = item->obj; + obj->type = UACPI_OBJECT_INTEGER; + obj->integer = op == UACPI_PARSE_OP_LOAD_FALSE_OBJECT ? 0 : ones(); + break; + } + + case UACPI_PARSE_OP_RECORD_AML_PC: + item->immediate = frame->code_offset; + break; + + case UACPI_PARSE_OP_TRUNCATE_NUMBER: + truncate_number_if_needed(item->obj); + break; + + case UACPI_PARSE_OP_TYPECHECK: { + enum uacpi_object_type expected_type; + + expected_type = op_decode_byte(op_ctx); + + if (uacpi_unlikely(item->obj->type != expected_type)) { + EXEC_OP_ERR_2("bad object type: expected %s, got %s!", + uacpi_object_type_to_string(expected_type), + uacpi_object_type_to_string(item->obj->type)); + ret = UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + break; + } + + case UACPI_PARSE_OP_BAD_OPCODE: + case UACPI_PARSE_OP_UNREACHABLE: + EXEC_OP_ERR("invalid/unexpected opcode"); + ret = UACPI_STATUS_AML_INVALID_OPCODE; + break; + + case UACPI_PARSE_OP_AML_PC_DECREMENT: + frame->code_offset--; + break; + + case UACPI_PARSE_OP_IMM_DECREMENT: + item_array_at(&op_ctx->items, op_decode_byte(op_ctx))->immediate--; + break; + + case UACPI_PARSE_OP_ITEM_POP: + pop_item(op_ctx); + break; + + case UACPI_PARSE_OP_IF_HAS_DATA: { + uacpi_size pkg_idx = op_ctx->tracked_pkg_idx - 1; + struct package_length *pkg; + uacpi_u8 bytes_skip; + + bytes_skip = op_decode_byte(op_ctx); + pkg = &item_array_at(&op_ctx->items, pkg_idx)->pkg; + + if (frame->code_offset >= pkg->end) + op_ctx->pc += bytes_skip; + + break; + } + + case UACPI_PARSE_OP_IF_NOT_NULL: + case UACPI_PARSE_OP_IF_NULL: { + uacpi_u8 idx, bytes_skip; + uacpi_bool is_null, skip_if_null; + + idx = op_decode_byte(op_ctx); + bytes_skip = op_decode_byte(op_ctx); + + is_null = item_array_at(&op_ctx->items, idx)->handle == UACPI_NULL; + skip_if_null = op == UACPI_PARSE_OP_IF_NOT_NULL; + + if (is_null == skip_if_null) + op_ctx->pc += bytes_skip; + + break; + } + + case UACPI_PARSE_OP_IF_EQUALS: { + uacpi_u8 value, bytes_skip; + + value = op_decode_byte(op_ctx); + bytes_skip = op_decode_byte(op_ctx); + + if (item->immediate != value) + op_ctx->pc += bytes_skip; + + break; + } + + case UACPI_PARSE_OP_JMP: { + op_ctx->pc = op_decode_byte(op_ctx); + break; + } + + case UACPI_PARSE_OP_CREATE_NAMESTRING: + case UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD: + case UACPI_PARSE_OP_EXISTING_NAMESTRING: + case UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL: + case UACPI_PARSE_OP_EXISTING_NAMESTRING_OR_NULL_IF_LOAD: { + uacpi_size offset = frame->code_offset; + enum resolve_behavior behavior; + + if (op == UACPI_PARSE_OP_CREATE_NAMESTRING || + op == UACPI_PARSE_OP_CREATE_NAMESTRING_OR_NULL_IF_LOAD) + behavior = RESOLVE_CREATE_LAST_NAMESEG_FAIL_IF_EXISTS; + else + behavior = RESOLVE_FAIL_IF_DOESNT_EXIST; + + ret = resolve_name_string(frame, behavior, &item->node); + + if (ret == UACPI_STATUS_NOT_FOUND) { + uacpi_bool is_ok; + + if (prev_op) { + is_ok = op_allows_unresolved(prev_op); + is_ok &= op_allows_unresolved(op); + } else { + // This is the only standalone op where we allow unresolved + is_ok = op_ctx->op->code == UACPI_AML_OP_ExternalOp; + } + + if (is_ok) + ret = UACPI_STATUS_OK; + } + + if (uacpi_unlikely_error(ret)) { + enum uacpi_log_level lvl = UACPI_LOG_ERROR; + uacpi_status trace_ret = ret; + + if (frame->method->named_objects_persist) { + uacpi_bool is_ok; + + is_ok = op_allows_unresolved_if_load(op); + is_ok &= ret == UACPI_STATUS_AML_OBJECT_ALREADY_EXISTS || + ret == UACPI_STATUS_NOT_FOUND; + + if (is_ok) { + lvl = UACPI_LOG_WARN; + ret = UACPI_STATUS_OK; + } + } + + trace_named_object_lookup_or_creation_failure( + frame, offset, op, trace_ret, lvl + ); + + if (ret == UACPI_STATUS_NOT_FOUND) + ret = UACPI_STATUS_AML_UNDEFINED_REFERENCE; + } + + if (behavior == RESOLVE_CREATE_LAST_NAMESEG_FAIL_IF_EXISTS && + !frame->method->named_objects_persist) + item->node->flags |= UACPI_NAMESPACE_NODE_FLAG_TEMPORARY; + + break; + } + + case UACPI_PARSE_OP_INVOKE_HANDLER: { + uacpi_aml_op code = op_ctx->op->code; + uacpi_u8 idx; + + if (code <= 0xFF) + idx = handler_idx_of_op[code]; + else + idx = handler_idx_of_ext_op[EXT_OP_IDX(code)]; + + ret = op_handlers[idx](ctx); + break; + } + + case UACPI_PARSE_OP_INSTALL_NAMESPACE_NODE: + item = item_array_at(&op_ctx->items, op_decode_byte(op_ctx)); + ret = do_install_node_item(frame, item); + break; + + case UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV: + case UACPI_PARSE_OP_OBJECT_COPY_TO_PREV: { + uacpi_object *src; + struct item *dst; + + if (!ctx->prev_op_ctx) + break; + + switch (prev_op) { + case UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL: + case UACPI_PARSE_OP_COMPUTATIONAL_DATA: + case UACPI_PARSE_OP_OPERAND: + case UACPI_PARSE_OP_STRING: + src = uacpi_unwrap_internal_reference(item->obj); + + if (prev_op == UACPI_PARSE_OP_OPERAND) + ret = typecheck_operand(ctx->prev_op_ctx, src); + else if (prev_op == UACPI_PARSE_OP_STRING) + ret = typecheck_string(ctx->prev_op_ctx, src); + else if (prev_op == UACPI_PARSE_OP_COMPUTATIONAL_DATA) + ret = typecheck_computational_data(ctx->prev_op_ctx, src); + + break; + case UACPI_PARSE_OP_SUPERNAME: + case UACPI_PARSE_OP_SUPERNAME_OR_UNRESOLVED: + src = item->obj; + break; + + case UACPI_PARSE_OP_SIMPLE_NAME: + case UACPI_PARSE_OP_TERM_ARG: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED: + case UACPI_PARSE_OP_TARGET: + src = item->obj; + break; + + default: + EXEC_OP_ERR_1("don't know how to copy/transfer object to %d", + prev_op); + ret = UACPI_STATUS_INVALID_ARGUMENT; + break; + } + + if (uacpi_likely_success(ret)) { + dst = item_array_last(&ctx->prev_op_ctx->items); + dst->type = ITEM_OBJECT; + + if (op == UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV) { + dst->obj = src; + uacpi_object_ref(dst->obj); + } else { + dst->obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(dst->obj == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + break; + } + + ret = uacpi_object_assign(dst->obj, src, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY); + } + } + break; + } + + case UACPI_PARSE_OP_STORE_TO_TARGET: + case UACPI_PARSE_OP_STORE_TO_TARGET_INDIRECT: { + uacpi_object *dst, *src; + + dst = item_array_at(&op_ctx->items, op_decode_byte(op_ctx))->obj; + + if (op == UACPI_PARSE_OP_STORE_TO_TARGET_INDIRECT) { + src = item_array_at(&op_ctx->items, + op_decode_byte(op_ctx))->obj; + } else { + src = item->obj; + } + + ret = store_to_target(dst, src); + break; + } + + // Nothing to do here, object is allocated automatically + case UACPI_PARSE_OP_OBJECT_ALLOC: + case UACPI_PARSE_OP_OBJECT_ALLOC_TYPED: + case UACPI_PARSE_OP_EMPTY_OBJECT_ALLOC: + break; + + case UACPI_PARSE_OP_OBJECT_CONVERT_TO_SHALLOW_COPY: + case UACPI_PARSE_OP_OBJECT_CONVERT_TO_DEEP_COPY: { + uacpi_object *temp = item->obj; + enum uacpi_assign_behavior behavior; + + item_array_pop(&op_ctx->items); + item = item_array_last(&op_ctx->items); + + if (op == UACPI_PARSE_OP_OBJECT_CONVERT_TO_SHALLOW_COPY) + behavior = UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY; + else + behavior = UACPI_ASSIGN_BEHAVIOR_DEEP_COPY; + + ret = uacpi_object_assign(temp, item->obj, behavior); + if (uacpi_unlikely_error(ret)) + break; + + uacpi_object_unref(item->obj); + item->obj = temp; + break; + } + + case UACPI_PARSE_OP_DISPATCH_METHOD_CALL: { + struct uacpi_namespace_node *node; + struct uacpi_control_method *method; + + node = item_array_at(&op_ctx->items, 0)->node; + method = uacpi_namespace_node_get_object(node)->method; + + ret = prepare_method_call( + ctx, node, method, METHOD_CALL_AML, UACPI_NULL + ); + return ret; + } + + case UACPI_PARSE_OP_DISPATCH_TABLE_LOAD: { + struct uacpi_namespace_node *node; + struct uacpi_control_method *method; + + node = item_array_at(&op_ctx->items, 0)->node; + method = item_array_at(&op_ctx->items, 1)->obj->method; + + ret = prepare_method_call( + ctx, node, method, METHOD_CALL_TABLE_LOAD, UACPI_NULL + ); + return ret; + } + + case UACPI_PARSE_OP_CONVERT_NAMESTRING: { + uacpi_aml_op new_op = UACPI_AML_OP_InternalOpNamedObject; + uacpi_object *obj; + + if (item->node == UACPI_NULL) { + if (!op_allows_unresolved(prev_op)) + ret = UACPI_STATUS_NOT_FOUND; + break; + } + + obj = uacpi_namespace_node_get_object(item->node); + + switch (obj->type) { + case UACPI_OBJECT_METHOD: { + uacpi_bool should_invoke; + + switch (prev_op) { + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT: + case UACPI_PARSE_OP_TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED: + should_invoke = UACPI_FALSE; + break; + default: + should_invoke = !op_wants_supername(prev_op); + } + + if (!should_invoke) + break; + + new_op = UACPI_AML_OP_InternalOpMethodCall0Args; + new_op += obj->method->args; + break; + } + + case UACPI_OBJECT_BUFFER_FIELD: + case UACPI_OBJECT_FIELD_UNIT: + if (!op_wants_term_arg_or_operand(prev_op)) + break; + + switch (field_get_read_type(obj)) { + case UACPI_OBJECT_BUFFER: + new_op = UACPI_AML_OP_InternalOpReadFieldAsBuffer; + break; + case UACPI_OBJECT_INTEGER: + new_op = UACPI_AML_OP_InternalOpReadFieldAsInteger; + break; + default: + ret = UACPI_STATUS_INVALID_ARGUMENT; + continue; + } + break; + default: + break; + } + + op_ctx->pc = 0; + op_ctx->op = uacpi_get_op_spec(new_op); + break; + } + + default: + EXEC_OP_ERR_1("unhandled parser op '%d'", op); + ret = UACPI_STATUS_UNIMPLEMENTED; + break; + } + } +} + +static void ctx_reload_post_ret(struct execution_context *ctx) +{ + uacpi_control_method *method = ctx->cur_frame->method; + + if (method->is_serialized) { + held_mutexes_array_remove_and_release( + &ctx->held_mutexes, method->mutex, FORCE_RELEASE_YES + ); + ctx->sync_level = ctx->cur_frame->prev_sync_level; + } + + call_frame_clear(ctx->cur_frame); + call_frame_array_pop(&ctx->call_stack); + + ctx->cur_frame = call_frame_array_last(&ctx->call_stack); + refresh_ctx_pointers(ctx); +} + +static void trace_method_abort(struct code_block *block, uacpi_size depth) +{ + static const uacpi_char *unknown_path = ""; + uacpi_char oom_absolute_path[9] = "."; + + const uacpi_char *absolute_path; + + if (block != UACPI_NULL && block->type == CODE_BLOCK_SCOPE) { + absolute_path = uacpi_namespace_node_generate_absolute_path(block->node); + if (uacpi_unlikely(absolute_path == UACPI_NULL)) + uacpi_memcpy(oom_absolute_path + 4, block->node->name.text, 4); + } else { + absolute_path = unknown_path; + } + + uacpi_error(" #%zu in %s()\n", depth, absolute_path); + + if (absolute_path != oom_absolute_path && absolute_path != unknown_path) + uacpi_free_dynamic_string(absolute_path); +} + +static void stack_unwind(struct execution_context *ctx) +{ + uacpi_size depth; + uacpi_bool should_stop; + + /* + * Non-empty call stack here means the execution was aborted at some point, + * probably due to a bytecode error. + */ + depth = call_frame_array_size(&ctx->call_stack); + + if (depth != 0) { + uacpi_size idx = 0; + uacpi_bool table_level_code; + + do { + table_level_code = ctx->cur_frame->method->named_objects_persist; + + if (table_level_code && idx != 0) + /* + * This isn't the first frame that we are aborting. + * If this is table-level code, we have just unwound a call + * chain that had triggered an abort. Stop here, no need to + * abort table load because of it. + */ + break; + + while (op_context_array_size(&ctx->cur_frame->pending_ops) != 0) + pop_op(ctx); + + trace_method_abort( + code_block_array_at(&ctx->cur_frame->code_blocks, 0), idx + ); + + should_stop = idx++ == 0 && table_level_code; + ctx_reload_post_ret(ctx); + } while (--depth && !should_stop); + } +} + +static void execution_context_release(struct execution_context *ctx) +{ + if (ctx->ret) + uacpi_object_unref(ctx->ret); + + while (held_mutexes_array_size(&ctx->held_mutexes) != 0) { + held_mutexes_array_remove_and_release( + &ctx->held_mutexes, + *held_mutexes_array_last(&ctx->held_mutexes), + FORCE_RELEASE_YES + ); + } + + call_frame_array_clear(&ctx->call_stack); + held_mutexes_array_clear(&ctx->held_mutexes); + uacpi_free(ctx, sizeof(*ctx)); +} + +uacpi_status uacpi_execute_control_method( + uacpi_namespace_node *scope, uacpi_control_method *method, + const uacpi_object_array *args, uacpi_object **out_obj +) +{ + uacpi_status ret = UACPI_STATUS_OK; + struct execution_context *ctx; + + ctx = uacpi_kernel_alloc_zeroed(sizeof(*ctx)); + if (uacpi_unlikely(ctx == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + if (out_obj != UACPI_NULL) { + ctx->ret = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(ctx->ret == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + } + + ret = prepare_method_call(ctx, scope, method, METHOD_CALL_NATIVE, args); + if (uacpi_unlikely_error(ret)) + goto out; + + for (;;) { + if (!ctx_has_non_preempted_op(ctx)) { + if (ctx->cur_frame == UACPI_NULL) + break; + + if (maybe_end_block(ctx)) + continue; + + if (!call_frame_has_code(ctx->cur_frame)) { + ctx_reload_post_ret(ctx); + continue; + } + + ret = get_op(ctx); + if (uacpi_unlikely_error(ret)) + goto handle_method_abort; + + trace_op(ctx->cur_op, OP_TRACE_ACTION_BEGIN); + } + + ret = exec_op(ctx); + if (uacpi_unlikely_error(ret)) + goto handle_method_abort; + + ctx->skip_else = UACPI_FALSE; + continue; + + handle_method_abort: + uacpi_error("aborting %s due to previous error: %s\n", + ctx->cur_frame->method->named_objects_persist ? + "table load" : "method invocation", + uacpi_status_to_string(ret)); + stack_unwind(ctx); + + /* + * Having a frame here implies that we just aborted a dynamic table + * load. Signal to the caller that it failed by setting the return + * value to false. + */ + if (ctx->cur_frame) { + struct item *it; + + it = item_array_last(&ctx->cur_op_ctx->items); + if (it != UACPI_NULL && it->obj != UACPI_NULL) + it->obj->integer = 0; + } + } + +out: + if (ctx->ret != UACPI_NULL) { + uacpi_object *ret_obj = UACPI_NULL; + + if (ctx->ret->type != UACPI_OBJECT_UNINITIALIZED) { + ret_obj = ctx->ret; + uacpi_object_ref(ret_obj); + } + + *out_obj = ret_obj; + } + + execution_context_release(ctx); + return ret; +} + +uacpi_status uacpi_osi(uacpi_handle handle, uacpi_object *retval) +{ + struct execution_context *ctx = handle; + uacpi_bool is_supported; + uacpi_status ret; + uacpi_object *arg; + + arg = uacpi_unwrap_internal_reference(ctx->cur_frame->args[0]); + if (arg->type != UACPI_OBJECT_STRING) { + uacpi_error("_OSI: invalid argument type %s, expected a String\n", + uacpi_object_type_to_string(arg->type)); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + if (retval == UACPI_NULL) + return UACPI_STATUS_OK; + + retval->type = UACPI_OBJECT_INTEGER; + + ret = uacpi_handle_osi(arg->buffer->text, &is_supported); + if (uacpi_unlikely_error(ret)) + return ret; + + retval->integer = is_supported ? ones() : 0; + + uacpi_trace("_OSI(%s) => reporting as %ssupported\n", + arg->buffer->text, is_supported ? "" : "un"); + return UACPI_STATUS_OK; +} diff --git a/src/uacpi/io.c b/src/uacpi/io.c new file mode 100644 index 0000000..7757cf1 --- /dev/null +++ b/src/uacpi/io.c @@ -0,0 +1,672 @@ +#include +#include +#include +#include +#include +#include +#include + +uacpi_size uacpi_round_up_bits_to_bytes(uacpi_size bit_length) +{ + return UACPI_ALIGN_UP(bit_length, 8, uacpi_size) / 8; +} + +static void cut_misaligned_tail( + uacpi_u8 *data, uacpi_size offset, uacpi_u32 bit_length +) +{ + uacpi_u8 remainder = bit_length & 7; + + if (remainder == 0) + return; + + data[offset] &= ((1ull << remainder) - 1); +} + +struct bit_span +{ + union { + uacpi_u8 *data; + const uacpi_u8 *const_data; + }; + uacpi_u64 index; + uacpi_u64 length; +}; + +static uacpi_size bit_span_offset(struct bit_span *span, uacpi_size bits) +{ + uacpi_size delta = UACPI_MIN(span->length, bits); + + span->index += delta; + span->length -= delta; + + return delta; +} + +static void bit_copy(struct bit_span *dst, struct bit_span *src) +{ + uacpi_u8 src_shift, dst_shift, bits = 0; + uacpi_u16 dst_mask; + uacpi_u8 *dst_ptr, *src_ptr; + uacpi_u64 dst_count, src_count; + + dst_ptr = dst->data + (dst->index / 8); + src_ptr = src->data + (src->index / 8); + + dst_count = dst->length; + dst_shift = dst->index & 7; + + src_count = src->length; + src_shift = src->index & 7; + + while (dst_count) + { + bits = 0; + + if (src_count) { + bits = *src_ptr >> src_shift; + + if (src_shift && src_count > (uacpi_u32)(8 - src_shift)) + bits |= *(src_ptr + 1) << (8 - src_shift); + + if (src_count < 8) { + bits &= (1 << src_count) - 1; + src_count = 0; + } else { + src_count -= 8; + src_ptr++; + } + } + + dst_mask = (dst_count < 8 ? (1 << dst_count) - 1 : 0xFF) << dst_shift; + *dst_ptr = (*dst_ptr & ~dst_mask) | ((bits << dst_shift) & dst_mask); + + if (dst_shift && dst_count > (uacpi_u32)(8 - dst_shift)) { + dst_mask >>= 8; + *(dst_ptr + 1) &= ~dst_mask; + *(dst_ptr + 1) |= (bits >> (8 - dst_shift)) & dst_mask; + } + + dst_count = dst_count > 8 ? dst_count - 8 : 0; + ++dst_ptr; + } +} + +static void do_misaligned_buffer_read( + const uacpi_buffer_field *field, uacpi_u8 *dst +) +{ + struct bit_span src_span = { + .index = field->bit_index, + .length = field->bit_length, + .const_data = field->backing->data, + }; + struct bit_span dst_span = { + .data = dst, + }; + + dst_span.length = uacpi_round_up_bits_to_bytes(field->bit_length) * 8; + bit_copy(&dst_span, &src_span); +} + +void uacpi_read_buffer_field( + const uacpi_buffer_field *field, void *dst +) +{ + if (!(field->bit_index & 7)) { + uacpi_u8 *src = field->backing->data; + uacpi_size count; + + count = uacpi_round_up_bits_to_bytes(field->bit_length); + uacpi_memcpy(dst, src + (field->bit_index / 8), count); + cut_misaligned_tail(dst, count - 1, field->bit_length); + return; + } + + do_misaligned_buffer_read(field, dst); +} + +static void do_write_misaligned_buffer_field( + uacpi_buffer_field *field, + const void *src, uacpi_size size +) +{ + struct bit_span src_span = { + .length = size * 8, + .const_data = src, + }; + struct bit_span dst_span = { + .index = field->bit_index, + .length = field->bit_length, + .data = field->backing->data, + }; + + bit_copy(&dst_span, &src_span); +} + +void uacpi_write_buffer_field( + uacpi_buffer_field *field, + const void *src, uacpi_size size +) +{ + if (!(field->bit_index & 7)) { + uacpi_u8 *dst, last_byte, tail_shift; + uacpi_size count; + + dst = field->backing->data; + dst += field->bit_index / 8; + count = uacpi_round_up_bits_to_bytes(field->bit_length); + + last_byte = dst[count - 1]; + tail_shift = field->bit_length & 7; + + uacpi_memcpy_zerout(dst, src, count, size); + if (tail_shift) { + uacpi_u8 last_shift = 8 - tail_shift; + dst[count - 1] = dst[count - 1] << last_shift; + dst[count - 1] >>= last_shift; + dst[count - 1] |= (last_byte >> tail_shift) << tail_shift; + } + + return; + } + + do_write_misaligned_buffer_field(field, src, size); +} + +static uacpi_status access_field_unit( + uacpi_field_unit *field, uacpi_u32 offset, uacpi_region_op op, + uacpi_u64 *in_out +) +{ + uacpi_status ret = UACPI_STATUS_OK; + uacpi_namespace_node *region_node; + + if (field->lock_rule) { + ret = uacpi_acquire_aml_mutex( + g_uacpi_rt_ctx.global_lock_mutex, 0xFFFF + ); + if (uacpi_unlikely_error(ret)) + return ret; + } + + switch (field->kind) { + case UACPI_FIELD_UNIT_KIND_BANK: + ret = uacpi_write_field_unit( + field->bank_selection, &field->bank_value, sizeof(field->bank_value) + ); + region_node = field->bank_region; + break; + case UACPI_FIELD_UNIT_KIND_NORMAL: + region_node = field->region; + break; + case UACPI_FIELD_UNIT_KIND_INDEX: + ret = uacpi_write_field_unit( + field->index, &offset, sizeof(offset) + ); + if (uacpi_unlikely_error(ret)) + goto out; + + switch (op) { + case UACPI_REGION_OP_READ: + ret = uacpi_read_field_unit( + field->data, in_out, field->access_width_bytes + ); + break; + case UACPI_REGION_OP_WRITE: + ret = uacpi_write_field_unit( + field->data, in_out, field->access_width_bytes + ); + break; + default: + ret = UACPI_STATUS_INVALID_ARGUMENT; + break; + } + + goto out; + + default: + uacpi_error("invalid field unit kind %d\n", field->kind); + ret = UACPI_STATUS_INVALID_ARGUMENT; + } + + if (uacpi_unlikely_error(ret)) + goto out; + + ret = uacpi_dispatch_opregion_io( + region_node, offset, field->access_width_bytes, op, in_out + ); + +out: + if (field->lock_rule) + uacpi_release_aml_mutex(g_uacpi_rt_ctx.global_lock_mutex); + return ret; +} + +static uacpi_status do_read_misaligned_field_unit( + uacpi_field_unit *field, uacpi_u8 *dst, uacpi_size size +) +{ + uacpi_status ret; + uacpi_size reads_to_do; + uacpi_u64 out; + uacpi_u32 byte_offset = field->byte_offset; + uacpi_u32 bits_left = field->bit_length; + uacpi_u8 width_access_bits = field->access_width_bytes * 8; + + struct bit_span src_span = { + .data = (uacpi_u8*)&out, + .index = field->bit_offset_within_first_byte, + }; + struct bit_span dst_span = { + .data = dst, + .index = 0, + .length = size * 8 + }; + + reads_to_do = UACPI_ALIGN_UP( + field->bit_offset_within_first_byte + field->bit_length, + width_access_bits, + uacpi_u32 + ); + reads_to_do /= width_access_bits; + + while (reads_to_do-- > 0) { + src_span.length = UACPI_MIN( + bits_left, width_access_bits - src_span.index + ); + + ret = access_field_unit( + field, byte_offset, UACPI_REGION_OP_READ, + &out + ); + if (uacpi_unlikely_error(ret)) + return ret; + + bit_copy(&dst_span, &src_span); + bits_left -= src_span.length; + src_span.index = 0; + + bit_span_offset(&dst_span, src_span.length); + byte_offset += field->access_width_bytes; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_read_field_unit( + uacpi_field_unit *field, void *dst, uacpi_size size +) +{ + uacpi_status ret; + uacpi_u32 field_byte_length; + + field_byte_length = uacpi_round_up_bits_to_bytes(field->bit_length); + + /* + * Very simple fast case: + * - Bit offset within first byte is 0 + * AND + * - Field size is <= access width + */ + if (field->bit_offset_within_first_byte == 0 && + field_byte_length <= field->access_width_bytes) + { + uacpi_u64 out; + + ret = access_field_unit( + field, field->byte_offset, UACPI_REGION_OP_READ, &out + ); + if (uacpi_unlikely_error(ret)) + return ret; + + uacpi_memcpy_zerout(dst, &out, size, field_byte_length); + if (size >= field_byte_length) + cut_misaligned_tail(dst, field_byte_length - 1, field->bit_length); + + return UACPI_STATUS_OK; + } + + // Slow case + return do_read_misaligned_field_unit(field, dst, size); +} + +uacpi_status uacpi_write_field_unit( + uacpi_field_unit *field, const void *src, uacpi_size size +) +{ + uacpi_status ret; + uacpi_u32 bits_left, byte_offset = field->byte_offset; + uacpi_u8 width_access_bits = field->access_width_bytes * 8; + uacpi_u64 in; + + struct bit_span src_span = { + .const_data = src, + .index = 0, + .length = size * 8 + }; + struct bit_span dst_span = { + .data = (uacpi_u8*)&in, + .index = field->bit_offset_within_first_byte, + }; + + bits_left = field->bit_length; + + while (bits_left) { + in = 0; + dst_span.length = UACPI_MIN( + width_access_bits - dst_span.index, bits_left + ); + + if (dst_span.index != 0 || dst_span.length < width_access_bits) { + switch (field->update_rule) { + case UACPI_UPDATE_RULE_PRESERVE: + ret = access_field_unit( + field, byte_offset, UACPI_REGION_OP_READ, &in + ); + if (uacpi_unlikely_error(ret)) + return ret; + break; + case UACPI_UPDATE_RULE_WRITE_AS_ONES: + in = ~in; + break; + case UACPI_UPDATE_RULE_WRITE_AS_ZEROES: + break; + default: + uacpi_error("invalid field@%p update rule %d\n", + field, field->update_rule); + return UACPI_STATUS_INVALID_ARGUMENT; + } + } + + bit_copy(&dst_span, &src_span); + bit_span_offset(&src_span, dst_span.length); + + ret = access_field_unit( + field, byte_offset, UACPI_REGION_OP_WRITE, &in + ); + if (uacpi_unlikely_error(ret)) + return ret; + + bits_left -= dst_span.length; + dst_span.index = 0; + byte_offset += field->access_width_bytes; + } + + return UACPI_STATUS_OK; +} + +static uacpi_u8 gas_get_access_bit_width(const struct acpi_gas *gas) +{ + /* + * Same algorithm as ACPICA. + * + * The reason we do this is apparently GAS bit offset being non-zero means + * that it's an APEI register, as opposed to FADT, which needs special + * handling. In the case of a FADT register we want to ignore the specified + * access size. + */ + uacpi_u8 access_bit_width; + + if (gas->register_bit_offset == 0 && + UACPI_IS_POWER_OF_TWO(gas->register_bit_width, uacpi_u8) && + UACPI_IS_ALIGNED(gas->register_bit_width, 8, uacpi_u8)) { + access_bit_width = gas->register_bit_width; + } else if (gas->access_size) { + access_bit_width = gas->access_size * 8; + } else { + uacpi_u8 msb; + + msb = uacpi_bit_scan_backward( + (gas->register_bit_offset + gas->register_bit_width) - 1 + ); + access_bit_width = 1 << msb; + + if (access_bit_width <= 8) { + access_bit_width = 8; + } else { + /* + * Keep backing off to previous power of two until we find one + * that is aligned to the address specified in GAS. + */ + while (!UACPI_IS_ALIGNED( + gas->address, access_bit_width / 8, uacpi_u64 + )) + access_bit_width /= 2; + } + } + + return UACPI_MIN( + access_bit_width, + gas->address_space_id == UACPI_ADDRESS_SPACE_SYSTEM_IO ? 32 : 64 + ); +} + +static uacpi_status gas_validate( + const struct acpi_gas *gas, uacpi_u8 *access_bit_width +) +{ + uacpi_size total_width; + + if (uacpi_unlikely(gas == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (!gas->address) + return UACPI_STATUS_NOT_FOUND; + + if (gas->address_space_id != UACPI_ADDRESS_SPACE_SYSTEM_IO && + gas->address_space_id != UACPI_ADDRESS_SPACE_SYSTEM_MEMORY) { + uacpi_warn("unsupported GAS address space '%s' (%d)\n", + uacpi_address_space_to_string(gas->address_space_id), + gas->address_space_id); + return UACPI_STATUS_UNIMPLEMENTED; + } + + if (gas->access_size > 4) { + uacpi_warn("unsupported GAS access size %d\n", + gas->access_size); + return UACPI_STATUS_UNIMPLEMENTED; + } + + *access_bit_width = gas_get_access_bit_width(gas); + + total_width = UACPI_ALIGN_UP( + gas->register_bit_offset + gas->register_bit_width, + *access_bit_width, uacpi_size + ); + if (total_width > 64) { + uacpi_warn( + "GAS register total width is too large: %zu\n", total_width + ); + return UACPI_STATUS_UNIMPLEMENTED; + } + + return UACPI_STATUS_OK; +} + +/* + * Apparently both reading and writing GAS works differently from operation + * region in that bit offsets are not respected when writing the data. + * + * Let's follow ACPICA's approach here so that we don't accidentally + * break any quirky hardware. + */ + +uacpi_status uacpi_gas_read(const struct acpi_gas *gas, uacpi_u64 *out_value) +{ + uacpi_status ret; + uacpi_u8 access_bit_width, access_byte_width; + uacpi_u8 bit_offset, bits_left, index = 0; + uacpi_u64 data, mask = 0xFFFFFFFFFFFFFFFF; + + ret = gas_validate(gas, &access_bit_width); + if (ret != UACPI_STATUS_OK) + return ret; + + bit_offset = gas->register_bit_offset; + bits_left = bit_offset + gas->register_bit_width; + + access_byte_width = access_bit_width / 8; + + if (access_byte_width < 8) + mask = ~(mask << access_bit_width); + + *out_value = 0; + + while (bits_left) { + if (bit_offset >= access_bit_width) { + data = 0; + bit_offset -= access_bit_width; + } else { + uacpi_u64 address = gas->address + (index * access_byte_width); + + if (gas->address_space_id == UACPI_ADDRESS_SPACE_SYSTEM_IO) { + ret = uacpi_system_io_read(address, access_byte_width, &data); + } else { + void *virt; + + virt = uacpi_kernel_map(address, access_byte_width); + if (uacpi_unlikely(virt == UACPI_NULL)) + return UACPI_STATUS_MAPPING_FAILED; + + ret = uacpi_system_memory_read(virt, access_byte_width, &data); + uacpi_kernel_unmap(virt, access_bit_width); + } + if (uacpi_unlikely_error(ret)) + return ret; + } + + *out_value |= (data & mask) << (index * access_bit_width); + bits_left -= UACPI_MIN(bits_left, access_bit_width); + ++index; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_gas_write(const struct acpi_gas *gas, uacpi_u64 in_value) +{ + uacpi_status ret; + uacpi_u8 access_bit_width, access_byte_width; + uacpi_u8 bit_offset, bits_left, index = 0; + uacpi_u64 data, mask = 0xFFFFFFFFFFFFFFFF; + + ret = gas_validate(gas, &access_bit_width); + if (ret != UACPI_STATUS_OK) + return ret; + + bit_offset = gas->register_bit_offset; + bits_left = bit_offset + gas->register_bit_width; + access_byte_width = access_bit_width / 8; + + if (access_byte_width < 8) + mask = ~(mask << access_bit_width); + + while (bits_left) { + data = (in_value >> (index * access_bit_width)) & mask; + + if (bit_offset >= access_bit_width) { + bit_offset -= access_bit_width; + } else { + uacpi_u64 address = gas->address + (index * access_byte_width); + + if (gas->address_space_id == UACPI_ADDRESS_SPACE_SYSTEM_IO) { + ret = uacpi_system_io_write(address, access_byte_width, data); + } else { + void *virt; + + virt = uacpi_kernel_map(address, access_byte_width); + if (uacpi_unlikely(virt == UACPI_NULL)) + return UACPI_STATUS_MAPPING_FAILED; + + ret = uacpi_system_memory_write(virt, access_byte_width, data); + uacpi_kernel_unmap(virt, access_bit_width); + } + if (uacpi_unlikely_error(ret)) + return ret; + } + + bits_left -= UACPI_MIN(bits_left, access_bit_width); + ++index; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_system_io_read( + uacpi_io_addr address, uacpi_u8 width, uacpi_u64 *out +) +{ + uacpi_status ret; + uacpi_handle handle; + + ret = uacpi_kernel_io_map(address, width, &handle); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_kernel_io_read(handle, 0, width, out); + uacpi_kernel_io_unmap(handle); + + return ret; +} + +uacpi_status uacpi_system_io_write( + uacpi_io_addr address, uacpi_u8 width, uacpi_u64 in +) +{ + uacpi_status ret; + uacpi_handle handle; + + ret = uacpi_kernel_io_map(address, width, &handle); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_kernel_io_write(handle, 0, width, in); + uacpi_kernel_io_unmap(handle); + + return ret; +} + +uacpi_status uacpi_system_memory_read(void *ptr, uacpi_u8 width, uacpi_u64 *out) +{ + switch (width) { + case 1: + *out = *(volatile uacpi_u8*)ptr; + break; + case 2: + *out = *(volatile uacpi_u16*)ptr; + break; + case 4: + *out = *(volatile uacpi_u32*)ptr; + break; + case 8: + *out = *(volatile uacpi_u64*)ptr; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_system_memory_write(void *ptr, uacpi_u8 width, uacpi_u64 in) +{ + switch (width) { + case 1: + *(volatile uacpi_u8*)ptr = in; + break; + case 2: + *(volatile uacpi_u16*)ptr = in; + break; + case 4: + *(volatile uacpi_u32*)ptr = in; + break; + case 8: + *(volatile uacpi_u64*)ptr = in; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return UACPI_STATUS_OK; +} diff --git a/src/uacpi/mutex.c b/src/uacpi/mutex.c new file mode 100644 index 0000000..ee4e6fc --- /dev/null +++ b/src/uacpi/mutex.c @@ -0,0 +1,392 @@ +#include +#include +#include +#include +#include +#include +#include + +#ifndef UACPI_REDUCED_HARDWARE + +#define GLOBAL_LOCK_PENDING (1 << 0) + +#define GLOBAL_LOCK_OWNED_BIT 1 +#define GLOBAL_LOCK_OWNED (1 << GLOBAL_LOCK_OWNED_BIT) + +#define GLOBAL_LOCK_MASK 0b11u + +static uacpi_bool try_acquire_global_lock_from_firmware(uacpi_u32 *lock) +{ + uacpi_u32 value, new_value; + uacpi_bool was_owned; + + value = *(volatile uacpi_u32*)lock; + do { + was_owned = (value & GLOBAL_LOCK_OWNED) >> GLOBAL_LOCK_OWNED_BIT; + + // Clear both owned & pending bits. + new_value = value & ~GLOBAL_LOCK_MASK; + + // Set owned unconditionally + new_value |= GLOBAL_LOCK_OWNED; + + // Set pending iff the lock was owned at the time of reading + if (was_owned) + new_value |= GLOBAL_LOCK_PENDING; + } while (!uacpi_atomic_cmpxchg32(lock, &value, new_value)); + + return !was_owned; +} + +static uacpi_bool do_release_global_lock_to_firmware(uacpi_u32 *lock) +{ + uacpi_u32 value, new_value; + + value = *(volatile uacpi_u32*)lock; + do { + new_value = value & ~GLOBAL_LOCK_MASK; + } while (!uacpi_atomic_cmpxchg32(lock, &value, new_value)); + + return value & GLOBAL_LOCK_PENDING; +} + +static uacpi_status uacpi_acquire_global_lock_from_firmware(void) +{ + uacpi_cpu_flags flags; + uacpi_u16 spins = 0; + uacpi_bool success; + + if (!g_uacpi_rt_ctx.has_global_lock) + return UACPI_STATUS_OK; + + flags = uacpi_kernel_lock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock); + for (;;) { + spins++; + uacpi_trace( + "trying to acquire the global lock from firmware... (attempt %u)\n", + spins + ); + + success = try_acquire_global_lock_from_firmware( + &g_uacpi_rt_ctx.facs->global_lock + ); + if (success) + break; + + if (uacpi_unlikely(spins == 0xFFFF)) + break; + + g_uacpi_rt_ctx.global_lock_pending = UACPI_TRUE; + uacpi_trace( + "global lock is owned by firmware, waiting for a release " + "notification...\n" + ); + uacpi_kernel_unlock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock, flags); + + uacpi_kernel_wait_for_event(g_uacpi_rt_ctx.global_lock_event, 0xFFFF); + flags = uacpi_kernel_lock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock); + } + + g_uacpi_rt_ctx.global_lock_pending = UACPI_FALSE; + uacpi_kernel_unlock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock, flags); + + if (uacpi_unlikely(!success)) { + uacpi_error("unable to acquire global lock after %u attempts\n", spins); + return UACPI_STATUS_HARDWARE_TIMEOUT; + } + + uacpi_trace("global lock successfully acquired after %u attempt%s\n", + spins, spins > 1 ? "s" : ""); + return UACPI_STATUS_OK; +} + +static void uacpi_release_global_lock_to_firmware(void) +{ + if (!g_uacpi_rt_ctx.has_global_lock) + return; + + uacpi_trace("releasing the global lock to firmware...\n"); + if (do_release_global_lock_to_firmware(&g_uacpi_rt_ctx.facs->global_lock)) { + uacpi_trace("notifying firmware of the global lock release since the " + "pending bit was set\n"); + uacpi_write_register_field(UACPI_REGISTER_FIELD_GBL_RLS, 1); + } +} +#endif + +UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE( + uacpi_status uacpi_acquire_global_lock_from_firmware(void) +) +UACPI_STUB_IF_REDUCED_HARDWARE( + void uacpi_release_global_lock_to_firmware(void) +) + +uacpi_status uacpi_acquire_native_mutex_with_timeout( + uacpi_handle mtx, uacpi_u16 timeout +) +{ + uacpi_status ret; + + if (uacpi_unlikely(mtx == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_kernel_acquire_mutex(mtx, timeout); + if (uacpi_likely_success(ret)) + return ret; + + if (uacpi_unlikely(ret != UACPI_STATUS_TIMEOUT || timeout == 0xFFFF)) { + uacpi_error( + "unexpected status %08X (%s) while acquiring %p (timeout=%04X)\n", + ret, uacpi_status_to_string(ret), mtx, timeout + ); + } + + return ret; +} + +uacpi_status uacpi_acquire_global_lock(uacpi_u16 timeout, uacpi_u32 *out_seq) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(out_seq == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_acquire_native_mutex_with_timeout( + g_uacpi_rt_ctx.global_lock_mutex->handle, timeout + ); + if (ret != UACPI_STATUS_OK) + return ret; + + ret = uacpi_acquire_global_lock_from_firmware(); + if (uacpi_unlikely_error(ret)) { + uacpi_release_native_mutex(g_uacpi_rt_ctx.global_lock_mutex->handle); + return ret; + } + + if (uacpi_unlikely(g_uacpi_rt_ctx.global_lock_seq_num == 0xFFFFFFFF)) + g_uacpi_rt_ctx.global_lock_seq_num = 0; + + *out_seq = g_uacpi_rt_ctx.global_lock_seq_num++; + g_uacpi_rt_ctx.global_lock_acquired = UACPI_TRUE; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_release_global_lock(uacpi_u32 seq) +{ + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(!g_uacpi_rt_ctx.global_lock_acquired || + seq != g_uacpi_rt_ctx.global_lock_seq_num)) + return UACPI_STATUS_INVALID_ARGUMENT; + + g_uacpi_rt_ctx.global_lock_acquired = UACPI_FALSE; + uacpi_release_global_lock_to_firmware(); + uacpi_release_native_mutex(g_uacpi_rt_ctx.global_lock_mutex->handle); + + return UACPI_STATUS_OK; +} + +uacpi_bool uacpi_this_thread_owns_aml_mutex(uacpi_mutex *mutex) +{ + uacpi_thread_id id; + + id = UACPI_ATOMIC_LOAD_THREAD_ID(&mutex->owner); + return id == uacpi_kernel_get_thread_id(); +} + +uacpi_status uacpi_acquire_aml_mutex(uacpi_mutex *mutex, uacpi_u16 timeout) +{ + uacpi_thread_id this_id; + uacpi_status ret = UACPI_STATUS_OK; + + this_id = uacpi_kernel_get_thread_id(); + if (UACPI_ATOMIC_LOAD_THREAD_ID(&mutex->owner) == this_id) { + if (uacpi_unlikely(mutex->depth == 0xFFFF)) { + uacpi_warn( + "failing an attempt to acquire mutex @%p, too many recursive " + "acquires\n", mutex + ); + return UACPI_STATUS_DENIED; + } + + mutex->depth++; + return ret; + } + + uacpi_namespace_write_unlock(); + ret = uacpi_acquire_native_mutex_with_timeout(mutex->handle, timeout); + if (ret != UACPI_STATUS_OK) + goto out; + + if (mutex->handle == g_uacpi_rt_ctx.global_lock_mutex->handle) { + ret = uacpi_acquire_global_lock_from_firmware(); + if (uacpi_unlikely_error(ret)) { + uacpi_release_native_mutex(mutex->handle); + goto out; + } + } + + UACPI_ATOMIC_STORE_THREAD_ID(&mutex->owner, this_id); + mutex->depth = 1; + +out: + uacpi_namespace_write_lock(); + return ret; +} + +uacpi_status uacpi_release_aml_mutex(uacpi_mutex *mutex) +{ + if (mutex->depth-- > 1) + return UACPI_STATUS_OK; + + if (mutex->handle == g_uacpi_rt_ctx.global_lock_mutex->handle) + uacpi_release_global_lock_to_firmware(); + + UACPI_ATOMIC_STORE_THREAD_ID(&mutex->owner, UACPI_THREAD_ID_NONE); + uacpi_release_native_mutex(mutex->handle); + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_recursive_lock_init(struct uacpi_recursive_lock *lock) +{ + lock->mutex = uacpi_kernel_create_mutex(); + if (uacpi_unlikely(lock->mutex == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + lock->owner = UACPI_THREAD_ID_NONE; + lock->depth = 0; + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_recursive_lock_deinit(struct uacpi_recursive_lock *lock) +{ + if (uacpi_unlikely(lock->depth)) { + uacpi_warn( + "de-initializing active recursive lock %p with depth=%zu\n", + lock, lock->depth + ); + lock->depth = 0; + } + + lock->owner = UACPI_THREAD_ID_NONE; + + if (lock->mutex != UACPI_NULL) { + uacpi_kernel_free_mutex(lock->mutex); + lock->mutex = UACPI_NULL; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_recursive_lock_acquire(struct uacpi_recursive_lock *lock) +{ + uacpi_thread_id this_id; + uacpi_status ret = UACPI_STATUS_OK; + + this_id = uacpi_kernel_get_thread_id(); + if (UACPI_ATOMIC_LOAD_THREAD_ID(&lock->owner) == this_id) { + lock->depth++; + return ret; + } + + ret = uacpi_acquire_native_mutex(lock->mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + UACPI_ATOMIC_STORE_THREAD_ID(&lock->owner, this_id); + lock->depth = 1; + return ret; +} + +uacpi_status uacpi_recursive_lock_release(struct uacpi_recursive_lock *lock) +{ + if (lock->depth-- > 1) + return UACPI_STATUS_OK; + + UACPI_ATOMIC_STORE_THREAD_ID(&lock->owner, UACPI_THREAD_ID_NONE); + return uacpi_release_native_mutex(lock->mutex); +} + +uacpi_status uacpi_rw_lock_init(struct uacpi_rw_lock *lock) +{ + lock->read_mutex = uacpi_kernel_create_mutex(); + if (uacpi_unlikely(lock->read_mutex == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + lock->write_mutex = uacpi_kernel_create_mutex(); + if (uacpi_unlikely(lock->write_mutex == UACPI_NULL)) { + uacpi_kernel_free_mutex(lock->read_mutex); + lock->read_mutex = UACPI_NULL; + return UACPI_STATUS_OUT_OF_MEMORY; + } + + lock->num_readers = 0; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_rw_lock_deinit(struct uacpi_rw_lock *lock) +{ + if (uacpi_unlikely(lock->num_readers)) { + uacpi_warn("de-initializing rw_lock %p with %zu active readers\n", + lock, lock->num_readers); + lock->num_readers = 0; + } + + if (lock->read_mutex != UACPI_NULL) { + uacpi_kernel_free_mutex(lock->read_mutex); + lock->read_mutex = UACPI_NULL; + } + if (lock->write_mutex != UACPI_NULL) { + uacpi_kernel_free_mutex(lock->write_mutex); + lock->write_mutex = UACPI_NULL; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_rw_lock_read(struct uacpi_rw_lock *lock) +{ + uacpi_status ret; + + ret = uacpi_acquire_native_mutex(lock->read_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + if (lock->num_readers++ == 0) { + ret = uacpi_acquire_native_mutex(lock->write_mutex); + if (uacpi_unlikely_error(ret)) + lock->num_readers = 0; + } + + uacpi_kernel_release_mutex(lock->read_mutex); + return ret; +} + +uacpi_status uacpi_rw_unlock_read(struct uacpi_rw_lock *lock) +{ + uacpi_status ret; + + ret = uacpi_acquire_native_mutex(lock->read_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + if (lock->num_readers-- == 1) + uacpi_release_native_mutex(lock->write_mutex); + + uacpi_kernel_release_mutex(lock->read_mutex); + return ret; +} + +uacpi_status uacpi_rw_lock_write(struct uacpi_rw_lock *lock) +{ + return uacpi_acquire_native_mutex(lock->write_mutex); +} + +uacpi_status uacpi_rw_unlock_write(struct uacpi_rw_lock *lock) +{ + return uacpi_release_native_mutex(lock->write_mutex); +} diff --git a/src/uacpi/namespace.c b/src/uacpi/namespace.c new file mode 100644 index 0000000..62fd9f7 --- /dev/null +++ b/src/uacpi/namespace.c @@ -0,0 +1,1020 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define UACPI_REV_VALUE 2 +#define UACPI_OS_VALUE "Microsoft Windows NT" + +#define MAKE_PREDEFINED(c0, c1, c2, c3) \ + { \ + .name.text = { c0, c1, c2, c3 }, \ + .flags = UACPI_NAMESPACE_NODE_PREDEFINED \ + } + +static uacpi_namespace_node +predefined_namespaces[UACPI_PREDEFINED_NAMESPACE_MAX + 1] = { + [UACPI_PREDEFINED_NAMESPACE_ROOT] = MAKE_PREDEFINED('\\', 0, 0, 0), + [UACPI_PREDEFINED_NAMESPACE_GPE] = MAKE_PREDEFINED('_', 'G', 'P', 'E'), + [UACPI_PREDEFINED_NAMESPACE_PR] = MAKE_PREDEFINED('_', 'P', 'R', '_'), + [UACPI_PREDEFINED_NAMESPACE_SB] = MAKE_PREDEFINED('_', 'S', 'B', '_'), + [UACPI_PREDEFINED_NAMESPACE_SI] = MAKE_PREDEFINED('_', 'S', 'I', '_'), + [UACPI_PREDEFINED_NAMESPACE_TZ] = MAKE_PREDEFINED('_', 'T', 'Z', '_'), + [UACPI_PREDEFINED_NAMESPACE_GL] = MAKE_PREDEFINED('_', 'G', 'L', '_'), + [UACPI_PREDEFINED_NAMESPACE_OS] = MAKE_PREDEFINED('_', 'O', 'S', '_'), + [UACPI_PREDEFINED_NAMESPACE_OSI] = MAKE_PREDEFINED('_', 'O', 'S', 'I'), + [UACPI_PREDEFINED_NAMESPACE_REV] = MAKE_PREDEFINED('_', 'R', 'E', 'V'), +}; + +static struct uacpi_rw_lock namespace_lock; + +uacpi_status uacpi_namespace_read_lock(void) +{ + return uacpi_rw_lock_read(&namespace_lock); +} + +uacpi_status uacpi_namespace_read_unlock(void) +{ + return uacpi_rw_unlock_read(&namespace_lock); +} + +uacpi_status uacpi_namespace_write_lock(void) +{ + return uacpi_rw_lock_write(&namespace_lock); +} + +uacpi_status uacpi_namespace_write_unlock(void) +{ + return uacpi_rw_unlock_write(&namespace_lock); +} + +static uacpi_object *make_object_for_predefined( + enum uacpi_predefined_namespace ns +) +{ + uacpi_object *obj; + + switch (ns) { + case UACPI_PREDEFINED_NAMESPACE_ROOT: + /* + * The real root object is stored in the global context, whereas the \ + * node gets a placeholder uninitialized object instead. This is to + * protect against CopyObject(JUNK, \), so that all of the opregion and + * notify handlers are preserved if AML decides to do that. + */ + g_uacpi_rt_ctx.root_object = uacpi_create_object(UACPI_OBJECT_DEVICE); + if (uacpi_unlikely(g_uacpi_rt_ctx.root_object == UACPI_NULL)) + return UACPI_NULL; + + obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + break; + + case UACPI_PREDEFINED_NAMESPACE_OS: + obj = uacpi_create_object(UACPI_OBJECT_STRING); + if (uacpi_unlikely(obj == UACPI_NULL)) + return obj; + + obj->buffer->text = uacpi_kernel_alloc(sizeof(UACPI_OS_VALUE)); + if (uacpi_unlikely(obj->buffer->text == UACPI_NULL)) { + uacpi_object_unref(obj); + return UACPI_NULL; + } + + obj->buffer->size = sizeof(UACPI_OS_VALUE); + uacpi_memcpy(obj->buffer->text, UACPI_OS_VALUE, obj->buffer->size); + break; + + case UACPI_PREDEFINED_NAMESPACE_REV: + obj = uacpi_create_object(UACPI_OBJECT_INTEGER); + if (uacpi_unlikely(obj == UACPI_NULL)) + return obj; + + obj->integer = UACPI_REV_VALUE; + break; + + case UACPI_PREDEFINED_NAMESPACE_GL: + obj = uacpi_create_object(UACPI_OBJECT_MUTEX); + if (uacpi_likely(obj != UACPI_NULL)) { + uacpi_shareable_ref(obj->mutex); + g_uacpi_rt_ctx.global_lock_mutex = obj->mutex; + } + break; + + case UACPI_PREDEFINED_NAMESPACE_OSI: + obj = uacpi_create_object(UACPI_OBJECT_METHOD); + if (uacpi_unlikely(obj == UACPI_NULL)) + return obj; + + obj->method->native_call = UACPI_TRUE; + obj->method->handler = uacpi_osi; + obj->method->args = 1; + break; + + default: + obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + break; + } + + return obj; +} + +static void namespace_node_detach_object(uacpi_namespace_node *node) +{ + uacpi_object *object; + + object = uacpi_namespace_node_get_object(node); + if (object != UACPI_NULL) { + if (object->type == UACPI_OBJECT_OPERATION_REGION) + uacpi_opregion_uninstall_handler(node); + + uacpi_object_unref(node->object); + node->object = UACPI_NULL; + } +} + +static void free_namespace_node(uacpi_handle handle) +{ + uacpi_namespace_node *node = handle; + + if (uacpi_likely(!uacpi_namespace_node_is_predefined(node))) { + uacpi_free(node, sizeof(*node)); + return; + } + + node->flags = UACPI_NAMESPACE_NODE_PREDEFINED; + node->object = UACPI_NULL; + node->parent = UACPI_NULL; + node->child = UACPI_NULL; + node->next = UACPI_NULL; +} + +uacpi_status uacpi_initialize_namespace(void) +{ + enum uacpi_predefined_namespace ns; + uacpi_object *obj; + uacpi_namespace_node *node; + uacpi_status ret; + + ret = uacpi_rw_lock_init(&namespace_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + for (ns = 0; ns <= UACPI_PREDEFINED_NAMESPACE_MAX; ns++) { + node = &predefined_namespaces[ns]; + uacpi_shareable_init(node); + + obj = make_object_for_predefined(ns); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + node->object = uacpi_create_internal_reference( + UACPI_REFERENCE_KIND_NAMED, obj + ); + if (uacpi_unlikely(node->object == UACPI_NULL)) { + uacpi_object_unref(obj); + return UACPI_STATUS_OUT_OF_MEMORY; + } + + uacpi_object_unref(obj); + } + + for (ns = UACPI_PREDEFINED_NAMESPACE_GPE; + ns <= UACPI_PREDEFINED_NAMESPACE_MAX; ns++) { + + /* + * Skip the installation of \_OSI if it was disabled by user. + * We still create the object, but it's not attached to the namespace. + */ + if (ns == UACPI_PREDEFINED_NAMESPACE_OSI && + uacpi_check_flag(UACPI_FLAG_NO_OSI)) + continue; + + uacpi_namespace_node_install( + uacpi_namespace_root(), &predefined_namespaces[ns] + ); + } + + return UACPI_STATUS_OK; +} + +void uacpi_deinitialize_namespace(void) +{ + uacpi_status ret; + uacpi_namespace_node *current, *next = UACPI_NULL; + uacpi_u32 depth = 1; + + current = uacpi_namespace_root(); + + ret = uacpi_namespace_write_lock(); + + while (depth) { + next = next == UACPI_NULL ? current->child : next->next; + + /* + * The previous value of 'next' was the last child of this subtree, + * we can now remove the entire scope of 'current->child' + */ + if (next == UACPI_NULL) { + depth--; + + // Wipe the subtree + while (current->child != UACPI_NULL) + uacpi_namespace_node_uninstall(current->child); + + // Reset the pointers back as if this iteration never happened + next = current; + current = current->parent; + + continue; + } + + /* + * We have more nodes to process, proceed to the next one, either the + * child of the 'next' node, if one exists, or its peer + */ + if (next->child) { + depth++; + current = next; + next = UACPI_NULL; + } + + // This node has no children, move on to its peer + } + + namespace_node_detach_object(uacpi_namespace_root()); + free_namespace_node(uacpi_namespace_root()); + + if (ret == UACPI_STATUS_OK) + uacpi_namespace_write_unlock(); + + uacpi_object_unref(g_uacpi_rt_ctx.root_object); + g_uacpi_rt_ctx.root_object = UACPI_NULL; + + uacpi_mutex_unref(g_uacpi_rt_ctx.global_lock_mutex); + g_uacpi_rt_ctx.global_lock_mutex = UACPI_NULL; + + uacpi_rw_lock_deinit(&namespace_lock); +} + +uacpi_namespace_node *uacpi_namespace_root(void) +{ + return &predefined_namespaces[UACPI_PREDEFINED_NAMESPACE_ROOT]; +} + +uacpi_namespace_node *uacpi_namespace_get_predefined( + enum uacpi_predefined_namespace ns +) +{ + if (uacpi_unlikely(ns > UACPI_PREDEFINED_NAMESPACE_MAX)) { + uacpi_warn("requested invalid predefined namespace %d\n", ns); + return UACPI_NULL; + } + + return &predefined_namespaces[ns]; +} + +uacpi_namespace_node *uacpi_namespace_node_alloc(uacpi_object_name name) +{ + uacpi_namespace_node *ret; + + ret = uacpi_kernel_alloc_zeroed(sizeof(*ret)); + if (uacpi_unlikely(ret == UACPI_NULL)) + return ret; + + uacpi_shareable_init(ret); + ret->name = name; + return ret; +} + +void uacpi_namespace_node_unref(uacpi_namespace_node *node) +{ + uacpi_shareable_unref_and_delete_if_last(node, free_namespace_node); +} + +uacpi_status uacpi_namespace_node_install( + uacpi_namespace_node *parent, + uacpi_namespace_node *node +) +{ + if (parent == UACPI_NULL) + parent = uacpi_namespace_root(); + + if (uacpi_unlikely(uacpi_namespace_node_is_dangling(node))) { + uacpi_warn("attempting to install a dangling namespace node %.4s\n", + node->name.text); + return UACPI_STATUS_NAMESPACE_NODE_DANGLING; + } + + if (parent->child == UACPI_NULL) { + parent->child = node; + } else { + uacpi_namespace_node *prev = parent->child; + + while (prev->next != UACPI_NULL) + prev = prev->next; + + prev->next = node; + } + + node->parent = parent; + return UACPI_STATUS_OK; +} + +uacpi_bool uacpi_namespace_node_is_alias(uacpi_namespace_node *node) +{ + return node->flags & UACPI_NAMESPACE_NODE_FLAG_ALIAS; +} + +uacpi_bool uacpi_namespace_node_is_dangling(uacpi_namespace_node *node) +{ + return node->flags & UACPI_NAMESPACE_NODE_FLAG_DANGLING; +} + +uacpi_bool uacpi_namespace_node_is_temporary(uacpi_namespace_node *node) +{ + return node->flags & UACPI_NAMESPACE_NODE_FLAG_TEMPORARY; +} + +uacpi_bool uacpi_namespace_node_is_predefined(uacpi_namespace_node *node) +{ + return node->flags & UACPI_NAMESPACE_NODE_PREDEFINED; +} + +uacpi_status uacpi_namespace_node_uninstall(uacpi_namespace_node *node) +{ + uacpi_namespace_node *prev; + + if (uacpi_unlikely(uacpi_namespace_node_is_dangling(node))) { + uacpi_warn("attempting to uninstall a dangling namespace node %.4s\n", + node->name.text); + return UACPI_STATUS_INTERNAL_ERROR; + } + + /* + * The way to trigger this is as follows: + * + * Method (FOO) { + * // Temporary device, will be deleted upon returning from FOO + * Device (\BAR) { + * } + * + * // + * // Load TBL where TBL is: + * // Scope (\BAR) { + * // Name (TEST, 123) + * // } + * // + * Load(TBL) + * } + * + * In the above example, TEST is a permanent node attached by bad AML to a + * temporary node created inside the FOO method at \BAR. The cleanup code + * will attempt to remove the \BAR device upon exit from FOO, but that is + * no longer possible as there's now a permanent child attached to it. + */ + if (uacpi_unlikely(node->child != UACPI_NULL)) { + uacpi_warn( + "refusing to uninstall node %.4s with a child (%.4s)\n", + node->name.text, node->child->name.text + ); + return UACPI_STATUS_DENIED; + } + + /* + * Even though namespace_node is reference-counted it still has an 'invalid' + * state that is entered after it is uninstalled from the global namespace. + * + * Reference counting is only needed to combat dangling pointer issues + * whereas bad AML might try to prolong a local object lifetime by + * returning it from a method, or CopyObject it somewhere. In that case the + * namespace node object itself is still alive, but no longer has a valid + * object associated with it. + * + * Example: + * Method (BAD) { + * OperationRegion(REG, SystemMemory, 0xDEADBEEF, 4) + * Field (REG, AnyAcc, NoLock) { + * FILD, 8, + * } + * + * Return (RefOf(FILD)) + * } + * + * // Local0 is now the sole owner of the 'FILD' object that under the + * // hood is still referencing the 'REG' operation region object from + * // the 'BAD' method. + * Local0 = DerefOf(BAD()) + * + * This is done to prevent potential very deep recursion where an object + * frees a namespace node that frees an attached object that frees a + * namespace node as well as potential infinite cycles between a namespace + * node and an object. + */ + namespace_node_detach_object(node); + + prev = node->parent ? node->parent->child : UACPI_NULL; + + if (prev == node) { + node->parent->child = node->next; + } else { + while (uacpi_likely(prev != UACPI_NULL) && prev->next != node) + prev = prev->next; + + if (uacpi_unlikely(prev == UACPI_NULL)) { + uacpi_warn( + "trying to uninstall a node %.4s (%p) not linked to any peer\n", + node->name.text, node + ); + return UACPI_STATUS_INTERNAL_ERROR; + } + + prev->next = node->next; + } + + node->flags |= UACPI_NAMESPACE_NODE_FLAG_DANGLING; + uacpi_namespace_node_unref(node); + + return UACPI_STATUS_OK; +} + +uacpi_namespace_node *uacpi_namespace_node_find_sub_node( + uacpi_namespace_node *parent, + uacpi_object_name name +) +{ + if (parent == UACPI_NULL) + parent = uacpi_namespace_root(); + + uacpi_namespace_node *node = parent->child; + + while (node) { + if (node->name.id == name.id) + return node; + + node = node->next; + } + + return UACPI_NULL; +} + +static uacpi_object_name segment_to_name( + const uacpi_char **string, uacpi_size *in_out_size +) +{ + uacpi_object_name out_name; + const uacpi_char *cursor = *string; + uacpi_size offset, bytes_left = *in_out_size; + + for (offset = 0; offset < 4; offset++) { + if (bytes_left < 1 || *cursor == '.') { + out_name.text[offset] = '_'; + continue; + } + + out_name.text[offset] = *cursor++; + bytes_left--; + } + + *string = cursor; + *in_out_size = bytes_left; + return out_name; +} + +uacpi_status uacpi_namespace_node_resolve( + uacpi_namespace_node *parent, const uacpi_char *path, + enum uacpi_should_lock should_lock, + enum uacpi_may_search_above_parent may_search_above_parent, + enum uacpi_permanent_only permanent_only, + uacpi_namespace_node **out_node +) +{ + uacpi_namespace_node *cur_node = parent; + uacpi_status ret = UACPI_STATUS_OK; + const uacpi_char *cursor = path; + uacpi_size bytes_left; + uacpi_char prev_char = 0; + uacpi_bool single_nameseg = UACPI_TRUE; + + if (cur_node == UACPI_NULL) + cur_node = uacpi_namespace_root(); + + bytes_left = uacpi_strlen(path); + + if (should_lock == UACPI_SHOULD_LOCK_YES) { + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + } + + for (;;) { + if (bytes_left == 0) + goto out; + + switch (*cursor) { + case '\\': + single_nameseg = UACPI_FALSE; + + if (prev_char == '^') { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + cur_node = uacpi_namespace_root(); + break; + case '^': + single_nameseg = UACPI_FALSE; + + // Tried to go behind root + if (uacpi_unlikely(cur_node == uacpi_namespace_root())) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + cur_node = cur_node->parent; + break; + default: + break; + } + + prev_char = *cursor; + + switch (prev_char) { + case '^': + case '\\': + cursor++; + bytes_left--; + break; + default: + break; + } + + if (prev_char != '^') + break; + } + + while (bytes_left != 0) { + uacpi_object_name nameseg; + + if (*cursor == '.') { + cursor++; + bytes_left--; + } + + nameseg = segment_to_name(&cursor, &bytes_left); + if (bytes_left != 0 && single_nameseg) + single_nameseg = UACPI_FALSE; + + cur_node = uacpi_namespace_node_find_sub_node(cur_node, nameseg); + if (cur_node == UACPI_NULL) { + if (may_search_above_parent == UACPI_MAY_SEARCH_ABOVE_PARENT_NO || + !single_nameseg) + goto out; + + parent = parent->parent; + + while (parent) { + cur_node = uacpi_namespace_node_find_sub_node(parent, nameseg); + if (cur_node != UACPI_NULL) + goto out; + + parent = parent->parent; + } + + goto out; + } + } + +out: + if (uacpi_unlikely(ret == UACPI_STATUS_INVALID_ARGUMENT)) { + uacpi_warn("invalid path '%s'\n", path); + goto out_read_unlock; + } + + if (cur_node == UACPI_NULL) { + ret = UACPI_STATUS_NOT_FOUND; + goto out_read_unlock; + } + + if (uacpi_namespace_node_is_temporary(cur_node) && + permanent_only == UACPI_PERMANENT_ONLY_YES) { + uacpi_warn("denying access to temporary namespace node '%.4s'\n", + cur_node->name.text); + ret = UACPI_STATUS_DENIED; + goto out_read_unlock; + } + + if (out_node != UACPI_NULL) + *out_node = cur_node; + +out_read_unlock: + if (should_lock == UACPI_SHOULD_LOCK_YES) + uacpi_namespace_read_unlock(); + return ret; +} + +uacpi_status uacpi_namespace_node_find( + uacpi_namespace_node *parent, const uacpi_char *path, + uacpi_namespace_node **out_node +) +{ + return uacpi_namespace_node_resolve( + parent, path, UACPI_SHOULD_LOCK_YES, UACPI_MAY_SEARCH_ABOVE_PARENT_NO, + UACPI_PERMANENT_ONLY_YES, out_node + ); +} + +uacpi_status uacpi_namespace_node_resolve_from_aml_namepath( + uacpi_namespace_node *scope, + const uacpi_char *path, + uacpi_namespace_node **out_node +) +{ + return uacpi_namespace_node_resolve( + scope, path, UACPI_SHOULD_LOCK_YES, UACPI_MAY_SEARCH_ABOVE_PARENT_YES, + UACPI_PERMANENT_ONLY_YES, out_node + ); +} + +uacpi_object *uacpi_namespace_node_get_object(const uacpi_namespace_node *node) +{ + if (node == UACPI_NULL || node->object == UACPI_NULL) + return UACPI_NULL; + + return uacpi_unwrap_internal_reference(node->object); +} + +uacpi_object *uacpi_namespace_node_get_object_typed( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask +) +{ + uacpi_object *obj; + + obj = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(obj == UACPI_NULL)) + return obj; + + if (!uacpi_object_is_one_of(obj, type_mask)) + return UACPI_NULL; + + return obj; +} + +uacpi_status uacpi_namespace_node_acquire_object_typed( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask, + uacpi_object **out_obj +) +{ + uacpi_status ret; + uacpi_object *obj; + + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + obj = uacpi_namespace_node_get_object(node); + + if (uacpi_unlikely(obj == UACPI_NULL) || + !uacpi_object_is_one_of(obj, type_mask)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + uacpi_object_ref(obj); + *out_obj = obj; + +out: + uacpi_namespace_read_unlock(); + return ret; +} + +uacpi_status uacpi_namespace_node_acquire_object( + const uacpi_namespace_node *node, uacpi_object **out_obj +) +{ + return uacpi_namespace_node_acquire_object_typed( + node, UACPI_OBJECT_ANY_BIT, out_obj + ); +} + +enum action { + ACTION_REACQUIRE, + ACTION_PUT, +}; + +static uacpi_status object_mutate_refcount( + uacpi_object *obj, void (*cb)(uacpi_object*) +) +{ + uacpi_status ret = UACPI_STATUS_OK; + + if (uacpi_likely(!uacpi_object_is(obj, UACPI_OBJECT_REFERENCE))) { + cb(obj); + return ret; + } + + /* + * Reference objects must be (un)referenced under at least a read lock, as + * this requires walking down the entire reference chain and dropping each + * object ref-count by 1. This might race with the interpreter and + * object_replace_child in case an object in the chain is CopyObject'ed + * into. + */ + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + cb(obj); + + uacpi_namespace_read_unlock(); + return ret; +} + +uacpi_status uacpi_namespace_node_reacquire_object( + uacpi_object *obj +) +{ + return object_mutate_refcount(obj, uacpi_object_ref); +} + +uacpi_status uacpi_namespace_node_release_object(uacpi_object *obj) +{ + return object_mutate_refcount(obj, uacpi_object_unref); +} + +uacpi_object_name uacpi_namespace_node_name(const uacpi_namespace_node *node) +{ + return node->name; +} + +uacpi_status uacpi_namespace_node_type_unlocked( + const uacpi_namespace_node *node, uacpi_object_type *out_type +) +{ + uacpi_object *obj; + + if (uacpi_unlikely(node == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + obj = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_NOT_FOUND; + + *out_type = obj->type; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_namespace_node_type( + const uacpi_namespace_node *node, uacpi_object_type *out_type +) +{ + uacpi_status ret; + + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_namespace_node_type_unlocked(node, out_type); + + uacpi_namespace_read_unlock(); + return ret; +} + +uacpi_status uacpi_namespace_node_is_one_of_unlocked( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask, uacpi_bool *out +) +{ + uacpi_object *obj; + + if (uacpi_unlikely(node == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + obj = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_NOT_FOUND; + + *out = uacpi_object_is_one_of(obj, type_mask); + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_namespace_node_is_one_of( + const uacpi_namespace_node *node, uacpi_object_type_bits type_mask, + uacpi_bool *out +) +{ + uacpi_status ret; + + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_namespace_node_is_one_of_unlocked(node,type_mask, out); + + uacpi_namespace_read_unlock(); + return ret; +} + +uacpi_status uacpi_namespace_node_is( + const uacpi_namespace_node *node, uacpi_object_type type, uacpi_bool *out +) +{ + return uacpi_namespace_node_is_one_of( + node, 1u << type, out + ); +} + +uacpi_status uacpi_namespace_do_for_each_child( + uacpi_namespace_node *node, uacpi_iteration_callback descending_callback, + uacpi_iteration_callback ascending_callback, + uacpi_object_type_bits type_mask, uacpi_u32 max_depth, + enum uacpi_should_lock should_lock, + enum uacpi_permanent_only permanent_only, void *user +) +{ + uacpi_status ret = UACPI_STATUS_OK; + uacpi_iteration_decision decision; + uacpi_iteration_callback cb; + uacpi_bool walking_up = UACPI_FALSE, matches = UACPI_FALSE; + uacpi_u32 depth = 1; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(descending_callback == UACPI_NULL && + ascending_callback == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (uacpi_unlikely(node == UACPI_NULL || max_depth == 0)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (should_lock == UACPI_SHOULD_LOCK_YES) { + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + } + + if (node->child == UACPI_NULL) + goto out; + + node = node->child; + + while (depth) { + uacpi_namespace_node_is_one_of_unlocked(node, type_mask, &matches); + if (!matches) { + decision = UACPI_ITERATION_DECISION_CONTINUE; + goto do_next; + } + + if (permanent_only == UACPI_PERMANENT_ONLY_YES && + uacpi_namespace_node_is_temporary(node)) { + decision = UACPI_ITERATION_DECISION_NEXT_PEER; + goto do_next; + } + + cb = walking_up ? ascending_callback : descending_callback; + if (cb != UACPI_NULL) { + if (should_lock == UACPI_SHOULD_LOCK_YES) { + ret = uacpi_namespace_read_unlock(); + if (uacpi_unlikely_error(ret)) + return ret; + } + + decision = cb(user, node, depth); + if (decision == UACPI_ITERATION_DECISION_BREAK) + goto out; + + if (should_lock == UACPI_SHOULD_LOCK_YES) { + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + } + } else { + decision = UACPI_ITERATION_DECISION_CONTINUE; + } + + do_next: + if (walking_up) { + if (node->next) { + node = node->next; + walking_up = UACPI_FALSE; + continue; + } + + depth--; + node = node->parent; + continue; + } + + switch (decision) { + case UACPI_ITERATION_DECISION_CONTINUE: + if ((depth != max_depth) && (node->child != UACPI_NULL)) { + node = node->child; + depth++; + continue; + } + UACPI_FALLTHROUGH; + case UACPI_ITERATION_DECISION_NEXT_PEER: + walking_up = UACPI_TRUE; + continue; + default: + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + } + +out: + if (should_lock == UACPI_SHOULD_LOCK_YES) + uacpi_namespace_read_unlock(); + return ret; +} + +uacpi_status uacpi_namespace_for_each_child_simple( + uacpi_namespace_node *parent, uacpi_iteration_callback callback, void *user +) +{ + return uacpi_namespace_do_for_each_child( + parent, callback, UACPI_NULL, UACPI_OBJECT_ANY_BIT, UACPI_MAX_DEPTH_ANY, + UACPI_SHOULD_LOCK_YES, UACPI_PERMANENT_ONLY_YES, user + ); +} + +uacpi_status uacpi_namespace_for_each_child( + uacpi_namespace_node *parent, uacpi_iteration_callback descending_callback, + uacpi_iteration_callback ascending_callback, + uacpi_object_type_bits type_mask, uacpi_u32 max_depth, void *user +) +{ + return uacpi_namespace_do_for_each_child( + parent, descending_callback, ascending_callback, type_mask, max_depth, + UACPI_SHOULD_LOCK_YES, UACPI_PERMANENT_ONLY_YES, user + ); +} + +uacpi_size uacpi_namespace_node_depth(const uacpi_namespace_node *node) +{ + uacpi_size depth = 0; + + while (node->parent) { + depth++; + node = node->parent; + } + + return depth; +} + +uacpi_namespace_node *uacpi_namespace_node_parent( + uacpi_namespace_node *node +) +{ + return node->parent; +} + +const uacpi_char *uacpi_namespace_node_generate_absolute_path( + const uacpi_namespace_node *node +) +{ + uacpi_size depth, offset; + uacpi_size bytes_needed; + uacpi_char *path; + + depth = uacpi_namespace_node_depth(node) + 1; + + // \ only needs 1 byte, the rest is 4 bytes + bytes_needed = 1 + (depth - 1) * sizeof(uacpi_object_name); + + // \ and the first NAME don't need a '.', every other segment does + bytes_needed += depth > 2 ? depth - 2 : 0; + + // Null terminator + bytes_needed += 1; + + path = uacpi_kernel_alloc(bytes_needed); + if (uacpi_unlikely(path == UACPI_NULL)) + return path; + + path[0] = '\\'; + + offset = bytes_needed - 1; + path[offset] = '\0'; + + while (node != uacpi_namespace_root()) { + offset -= sizeof(uacpi_object_name); + uacpi_memcpy(&path[offset], node->name.text, sizeof(uacpi_object_name)); + + node = node->parent; + if (node != uacpi_namespace_root()) + path[--offset] = '.'; + } + + return path; +} + +void uacpi_free_absolute_path(const uacpi_char *path) +{ + uacpi_free_dynamic_string(path); +} diff --git a/src/uacpi/notify.c b/src/uacpi/notify.c new file mode 100644 index 0000000..dfde57b --- /dev/null +++ b/src/uacpi/notify.c @@ -0,0 +1,251 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static uacpi_handle notify_mutex; + +uacpi_status uacpi_initialize_notify(void) +{ + notify_mutex = uacpi_kernel_create_mutex(); + if (uacpi_unlikely(notify_mutex == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +void uacpi_deinitialize_notify(void) +{ + if (notify_mutex != UACPI_NULL) + uacpi_kernel_free_mutex(notify_mutex); + + notify_mutex = UACPI_NULL; +} + +struct notification_ctx { + uacpi_namespace_node *node; + uacpi_u64 value; + uacpi_object *node_object; +}; + +static void free_notification_ctx(struct notification_ctx *ctx) +{ + uacpi_namespace_node_release_object(ctx->node_object); + uacpi_namespace_node_unref(ctx->node); + uacpi_free(ctx, sizeof(*ctx)); +} + +static void do_notify(uacpi_handle opaque) +{ + struct notification_ctx *ctx = opaque; + uacpi_device_notify_handler *handler; + uacpi_bool did_notify_root = UACPI_FALSE; + + handler = ctx->node_object->handlers->notify_head; + + for (;;) { + if (handler == UACPI_NULL) { + if (did_notify_root) { + free_notification_ctx(ctx); + return; + } + + handler = g_uacpi_rt_ctx.root_object->handlers->notify_head; + did_notify_root = UACPI_TRUE; + continue; + } + + handler->callback(handler->user_context, ctx->node, ctx->value); + handler = handler->next; + } +} + +uacpi_status uacpi_notify_all(uacpi_namespace_node *node, uacpi_u64 value) +{ + uacpi_status ret; + struct notification_ctx *ctx; + uacpi_object *node_object; + + node_object = uacpi_namespace_node_get_object_typed( + node, UACPI_OBJECT_DEVICE_BIT | UACPI_OBJECT_THERMAL_ZONE_BIT | + UACPI_OBJECT_PROCESSOR_BIT + ); + if (uacpi_unlikely(node_object == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_acquire_native_mutex(notify_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + if (node_object->handlers->notify_head == UACPI_NULL && + g_uacpi_rt_ctx.root_object->handlers->notify_head == UACPI_NULL) { + ret = UACPI_STATUS_NO_HANDLER; + goto out; + } + + ctx = uacpi_kernel_alloc(sizeof(*ctx)); + if (uacpi_unlikely(ctx == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + + ctx->node = node; + // In case this node goes out of scope + uacpi_shareable_ref(node); + + ctx->value = value; + ctx->node_object = uacpi_namespace_node_get_object(node); + uacpi_object_ref(ctx->node_object); + + ret = uacpi_kernel_schedule_work(UACPI_WORK_NOTIFICATION, do_notify, ctx); + if (uacpi_unlikely_error(ret)) { + uacpi_warn("unable to schedule notification work: %s\n", + uacpi_status_to_string(ret)); + free_notification_ctx(ctx); + } + +out: + uacpi_release_native_mutex(notify_mutex); + return ret; +} + +static uacpi_device_notify_handler *handler_container( + uacpi_handlers *handlers, uacpi_notify_handler target_handler +) +{ + uacpi_device_notify_handler *handler = handlers->notify_head; + + while (handler) { + if (handler->callback == target_handler) + return handler; + + handler = handler->next; + } + + return UACPI_NULL; +} + +uacpi_status uacpi_install_notify_handler( + uacpi_namespace_node *node, uacpi_notify_handler handler, + uacpi_handle handler_context +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_handlers *handlers; + uacpi_device_notify_handler *new_handler; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (node == uacpi_namespace_root()) { + obj = g_uacpi_rt_ctx.root_object; + } else { + ret = uacpi_namespace_node_acquire_object_typed( + node, UACPI_OBJECT_DEVICE_BIT | UACPI_OBJECT_THERMAL_ZONE_BIT | + UACPI_OBJECT_PROCESSOR_BIT, &obj + ); + if (uacpi_unlikely_error(ret)) + return ret; + } + + ret = uacpi_acquire_native_mutex(notify_mutex); + if (uacpi_unlikely_error(ret)) + goto out_no_mutex; + + uacpi_kernel_wait_for_work_completion(); + + handlers = obj->handlers; + + if (handler_container(handlers, handler) != UACPI_NULL) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + new_handler = uacpi_kernel_alloc_zeroed(sizeof(*new_handler)); + if (uacpi_unlikely(new_handler == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + new_handler->callback = handler; + new_handler->user_context = handler_context; + new_handler->next = handlers->notify_head; + + handlers->notify_head = new_handler; + +out: + uacpi_release_native_mutex(notify_mutex); +out_no_mutex: + if (node != uacpi_namespace_root()) + uacpi_object_unref(obj); + + return ret; +} + +uacpi_status uacpi_uninstall_notify_handler( + uacpi_namespace_node *node, uacpi_notify_handler handler +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_handlers *handlers; + uacpi_device_notify_handler *containing, *prev_handler; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (node == uacpi_namespace_root()) { + obj = g_uacpi_rt_ctx.root_object; + } else { + ret = uacpi_namespace_node_acquire_object_typed( + node, UACPI_OBJECT_DEVICE_BIT | UACPI_OBJECT_THERMAL_ZONE_BIT | + UACPI_OBJECT_PROCESSOR_BIT, &obj + ); + if (uacpi_unlikely_error(ret)) + return ret; + } + + ret = uacpi_acquire_native_mutex(notify_mutex); + if (uacpi_unlikely_error(ret)) + goto out_no_mutex; + + uacpi_kernel_wait_for_work_completion(); + + handlers = obj->handlers; + + containing = handler_container(handlers, handler); + if (containing == UACPI_NULL) { + ret = UACPI_STATUS_NOT_FOUND; + goto out; + } + + prev_handler = handlers->notify_head; + + // Are we the last linked handler? + if (prev_handler == containing) { + handlers->notify_head = containing->next; + goto out; + } + + // Nope, we're somewhere in the middle. Do a search. + while (prev_handler) { + if (prev_handler->next == containing) { + prev_handler->next = containing->next; + goto out; + } + + prev_handler = prev_handler->next; + } + +out: + uacpi_release_native_mutex(notify_mutex); +out_no_mutex: + if (node != uacpi_namespace_root()) + uacpi_object_unref(obj); + + if (uacpi_likely_success(ret)) + uacpi_free(containing, sizeof(*containing)); + + return ret; +} diff --git a/src/uacpi/opcodes.c b/src/uacpi/opcodes.c new file mode 100644 index 0000000..89d50e5 --- /dev/null +++ b/src/uacpi/opcodes.c @@ -0,0 +1,260 @@ +#include + +#define UACPI_OP(opname, opcode, ...) \ + { #opname, .decode_ops = __VA_ARGS__, .code = opcode }, + +#define UACPI_OUT_OF_LINE_OP(opname, opcode, out_of_line_buf, props) \ + { \ + .name = #opname, \ + .indirect_decode_ops = out_of_line_buf, \ + .properties = props, \ + .code = opcode, \ + }, + +static const struct uacpi_op_spec opcode_table[0x100] = { + UACPI_ENUMERATE_OPCODES +}; + +static const struct uacpi_op_spec ext_opcode_table[] = { + UACPI_ENUMERATE_EXT_OPCODES +}; + +#define _(op) (op & 0x00FF) + +static const uacpi_u8 ext_op_to_idx[0x100] = { + [_(UACPI_AML_OP_MutexOp)] = 1, [_(UACPI_AML_OP_EventOp)] = 2, + [_(UACPI_AML_OP_CondRefOfOp)] = 3, [_(UACPI_AML_OP_CreateFieldOp)] = 4, + [_(UACPI_AML_OP_LoadTableOp)] = 5, [_(UACPI_AML_OP_LoadOp)] = 6, + [_(UACPI_AML_OP_StallOp)] = 7, [_(UACPI_AML_OP_SleepOp)] = 8, + [_(UACPI_AML_OP_AcquireOp)] = 9, [_(UACPI_AML_OP_SignalOp)] = 10, + [_(UACPI_AML_OP_WaitOp)] = 11, [_(UACPI_AML_OP_ResetOp)] = 12, + [_(UACPI_AML_OP_ReleaseOp)] = 13, [_(UACPI_AML_OP_FromBCDOp)] = 14, + [_(UACPI_AML_OP_ToBCDOp)] = 15, [_(UACPI_AML_OP_RevisionOp)] = 16, + [_(UACPI_AML_OP_DebugOp)] = 17, [_(UACPI_AML_OP_FatalOp)] = 18, + [_(UACPI_AML_OP_TimerOp)] = 19, [_(UACPI_AML_OP_OpRegionOp)] = 20, + [_(UACPI_AML_OP_FieldOp)] = 21, [_(UACPI_AML_OP_DeviceOp)] = 22, + [_(UACPI_AML_OP_ProcessorOp)] = 23, [_(UACPI_AML_OP_PowerResOp)] = 24, + [_(UACPI_AML_OP_ThermalZoneOp)] = 25, [_(UACPI_AML_OP_IndexFieldOp)] = 26, + [_(UACPI_AML_OP_BankFieldOp)] = 27, [_(UACPI_AML_OP_DataRegionOp)] = 28, +}; + +const struct uacpi_op_spec *uacpi_get_op_spec(uacpi_aml_op op) +{ + if (op > 0xFF) + return &ext_opcode_table[ext_op_to_idx[_(op)]]; + + return &opcode_table[op]; +} + +#define PARSE_FIELD_ELEMENTS(parse_loop_pc) \ + /* Parse every field element found inside */ \ + UACPI_PARSE_OP_IF_HAS_DATA, 44, \ + /* Look at the first byte */ \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + \ + /* ReservedField := 0x00 PkgLength */ \ + UACPI_PARSE_OP_IF_EQUALS, 0x00, 3, \ + UACPI_PARSE_OP_PKGLEN, \ + UACPI_PARSE_OP_JMP, parse_loop_pc, \ + \ + /* AccessField := 0x01 AccessType AccessAttrib */ \ + UACPI_PARSE_OP_IF_EQUALS, 0x01, 6, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_JMP, parse_loop_pc, \ + \ + /* ConnectField := <0x02 NameString> | <0x02 BufferData> */ \ + UACPI_PARSE_OP_IF_EQUALS, 0x02, 5, \ + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, \ + UACPI_PARSE_OP_TYPECHECK, UACPI_OBJECT_BUFFER, \ + UACPI_PARSE_OP_JMP, parse_loop_pc, \ + \ + /* ExtendedAccessField := 0x03 AccessType ExtendedAccessAttrib \ + * AccessLength */ \ + UACPI_PARSE_OP_IF_EQUALS, 0x03, 8, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_LOAD_IMM, 1, \ + UACPI_PARSE_OP_JMP, parse_loop_pc, \ + \ + /* NamedField := NameSeg PkgLength */ \ + \ + /* \ + * Discard the immediate, as it's the first byte of the \ + * nameseg. We don't need it. \ + */ \ + UACPI_PARSE_OP_ITEM_POP, \ + UACPI_PARSE_OP_AML_PC_DECREMENT, \ + UACPI_PARSE_OP_CREATE_NAMESTRING, \ + UACPI_PARSE_OP_PKGLEN, \ + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_FIELD_UNIT, \ + UACPI_PARSE_OP_JMP, parse_loop_pc, \ + \ + UACPI_PARSE_OP_INVOKE_HANDLER, \ + UACPI_PARSE_OP_END + +uacpi_u8 uacpi_field_op_decode_ops[] = { + UACPI_PARSE_OP_TRACKED_PKGLEN, + UACPI_PARSE_OP_EXISTING_NAMESTRING, + UACPI_PARSE_OP_LOAD_IMM, 1, + PARSE_FIELD_ELEMENTS(4), +}; + +uacpi_u8 uacpi_bank_field_op_decode_ops[] = { + UACPI_PARSE_OP_TRACKED_PKGLEN, + UACPI_PARSE_OP_EXISTING_NAMESTRING, + UACPI_PARSE_OP_EXISTING_NAMESTRING, + UACPI_PARSE_OP_OPERAND, + UACPI_PARSE_OP_LOAD_IMM, 1, + PARSE_FIELD_ELEMENTS(6), +}; + +uacpi_u8 uacpi_index_field_op_decode_ops[] = { + UACPI_PARSE_OP_TRACKED_PKGLEN, + UACPI_PARSE_OP_EXISTING_NAMESTRING, + UACPI_PARSE_OP_EXISTING_NAMESTRING, + UACPI_PARSE_OP_LOAD_IMM, 1, + PARSE_FIELD_ELEMENTS(5), +}; + +uacpi_u8 uacpi_load_op_decode_ops[] = { + // Storage for the scope pointer, this is left as 0 in case of errors + UACPI_PARSE_OP_LOAD_ZERO_IMM, + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_METHOD, + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, + UACPI_PARSE_OP_TARGET, + + /* + * Invoke the handler here to initialize the table. If this fails, it's + * expected to keep the item 0 as NULL, which is checked below to return + * false to the caller of Load. + */ + UACPI_PARSE_OP_INVOKE_HANDLER, + UACPI_PARSE_OP_IF_NULL, 0, 3, + UACPI_PARSE_OP_LOAD_FALSE_OBJECT, + UACPI_PARSE_OP_JMP, 15, + + UACPI_PARSE_OP_LOAD_TRUE_OBJECT, + UACPI_PARSE_OP_DISPATCH_TABLE_LOAD, + + /* + * Invoke the handler a second time to initialize any AML GPE handlers that + * might've been loaded from this table. + */ + UACPI_PARSE_OP_INVOKE_HANDLER, + UACPI_PARSE_OP_STORE_TO_TARGET, 3, + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, + UACPI_PARSE_OP_END, +}; + +uacpi_u8 uacpi_load_table_op_decode_ops[] = { + // Storage for the scope pointer, this is left as 0 in case of errors + UACPI_PARSE_OP_LOAD_ZERO_IMM, + UACPI_PARSE_OP_OBJECT_ALLOC_TYPED, UACPI_OBJECT_METHOD, + // Index of the table we are going to be loaded to unref it later + UACPI_PARSE_OP_LOAD_ZERO_IMM, + // Storage for the target pointer, this is left as 0 if none was requested + UACPI_PARSE_OP_LOAD_ZERO_IMM, + + UACPI_PARSE_OP_LOAD_INLINE_IMM, 1, 5, + UACPI_PARSE_OP_IF_NOT_NULL, 4, 5, + UACPI_PARSE_OP_STRING, + UACPI_PARSE_OP_IMM_DECREMENT, 4, + UACPI_PARSE_OP_JMP, 8, + UACPI_PARSE_OP_TERM_ARG_UNWRAP_INTERNAL, + + /* + * Invoke the handler here to initialize the table. If this fails, it's + * expected to keep the item 0 as NULL, which is checked below to return + * false to the caller of Load. + */ + UACPI_PARSE_OP_INVOKE_HANDLER, + UACPI_PARSE_OP_IF_NULL, 0, 3, + UACPI_PARSE_OP_LOAD_FALSE_OBJECT, + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, + UACPI_PARSE_OP_END, + + UACPI_PARSE_OP_LOAD_TRUE_OBJECT, + UACPI_PARSE_OP_DISPATCH_TABLE_LOAD, + + /* + * Invoke the handler a second time to block the store to target in case + * the load above failed, as well as do any AML GPE handler initialization. + */ + UACPI_PARSE_OP_INVOKE_HANDLER, + + // If we were given a target to store to, do the store + UACPI_PARSE_OP_IF_NOT_NULL, 3, 3, + UACPI_PARSE_OP_STORE_TO_TARGET_INDIRECT, 3, 10, + + UACPI_PARSE_OP_OBJECT_TRANSFER_TO_PREV, + UACPI_PARSE_OP_END, +}; + +#define POP(x) UACPI_PARSE_OP_##x + +static +const uacpi_char *const pop_names[UACPI_PARSE_OP_MAX + 1] = { + [POP(END)] = "", + [POP(SKIP_WITH_WARN_IF_NULL)] = "SKIP_WITH_WARN_IF_NULL", + [POP(SIMPLE_NAME)] = "SIMPLE_NAME", + [POP(SUPERNAME)] = "SUPERNAME", + [POP(SUPERNAME_OR_UNRESOLVED)] = "SUPERNAME_OR_UNRESOLVED", + [POP(TERM_ARG)] = "TERM_ARG", + [POP(TERM_ARG_UNWRAP_INTERNAL)] = "TERM_ARG_UNWRAP_INTERNAL", + [POP(TERM_ARG_OR_NAMED_OBJECT)] = "TERM_ARG_OR_NAMED_OBJECT", + [POP(TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED)] = "TERM_ARG_OR_NAMED_OBJECT_OR_UNRESOLVED", + [POP(OPERAND)] = "OPERAND", + [POP(STRING)] = "STRING", + [POP(COMPUTATIONAL_DATA)] = "COMPUTATIONAL_DATA", + [POP(TARGET)] = "TARGET", + [POP(PKGLEN)] = "PKGLEN", + [POP(TRACKED_PKGLEN)] = "TRACKED_PKGLEN", + [POP(CREATE_NAMESTRING)] = "CREATE_NAMESTRING", + [POP(CREATE_NAMESTRING_OR_NULL_IF_LOAD)] = "CREATE_NAMESTRING_OR_NULL_IF_LOAD", + [POP(EXISTING_NAMESTRING)] = "EXISTING_NAMESTRING", + [POP(EXISTING_NAMESTRING_OR_NULL)] = "EXISTING_NAMESTRING_OR_NULL", + [POP(EXISTING_NAMESTRING_OR_NULL_IF_LOAD)] = "EXISTING_NAMESTRING_OR_NULL_IF_LOAD", + [POP(INVOKE_HANDLER)] = "INVOKE_HANDLER", + [POP(OBJECT_ALLOC)] = "OBJECT_ALLOC", + [POP(EMPTY_OBJECT_ALLOC)] = "EMPTY_OBJECT_ALLOC", + [POP(OBJECT_CONVERT_TO_SHALLOW_COPY)] = "OBJECT_CONVERT_TO_SHALLOW_COPY", + [POP(OBJECT_CONVERT_TO_DEEP_COPY)] = "OBJECT_CONVERT_TO_DEEP_COPY", + [POP(OBJECT_ALLOC_TYPED)] = "OBJECT_ALLOC_TYPED", + [POP(RECORD_AML_PC)] = "RECORD_AML_PC", + [POP(LOAD_INLINE_IMM_AS_OBJECT)] = "LOAD_INLINE_IMM_AS_OBJECT", + [POP(LOAD_INLINE_IMM)] = "LOAD_INLINE_IMM", + [POP(LOAD_ZERO_IMM)] = "LOAD_ZERO_IMM", + [POP(LOAD_IMM)] = "LOAD_IMM", + [POP(LOAD_IMM_AS_OBJECT)] = "LOAD_IMM_AS_OBJECT", + [POP(LOAD_FALSE_OBJECT)] = "LOAD_FALSE_OBJECT", + [POP(LOAD_TRUE_OBJECT)] = "LOAD_TRUE_OBJECT", + [POP(TRUNCATE_NUMBER)] = "TRUNCATE_NUMBER", + [POP(TYPECHECK)] = "TYPECHECK", + [POP(INSTALL_NAMESPACE_NODE)] = "INSTALL_NAMESPACE_NODE", + [POP(OBJECT_TRANSFER_TO_PREV)] = "OBJECT_TRANSFER_TO_PREV", + [POP(OBJECT_COPY_TO_PREV)] = "OBJECT_COPY_TO_PREV", + [POP(STORE_TO_TARGET)] = "STORE_TO_TARGET", + [POP(STORE_TO_TARGET_INDIRECT)] = "STORE_TO_TARGET_INDIRECT", + [POP(UNREACHABLE)] = "UNREACHABLE", + [POP(BAD_OPCODE)] = "BAD_OPCODE", + [POP(AML_PC_DECREMENT)] = "AML_PC_DECREMENT", + [POP(IMM_DECREMENT)] = "IMM_DECREMENT", + [POP(ITEM_POP)] = "ITEM_POP", + [POP(DISPATCH_METHOD_CALL)] = "DISPATCH_METHOD_CALL", + [POP(DISPATCH_TABLE_LOAD)] = "DISPATCH_TABLE_LOAD", + [POP(CONVERT_NAMESTRING)] = "CONVERT_NAMESTRING", + [POP(IF_HAS_DATA)] = "IF_HAS_DATA", + [POP(IF_NULL)] = "IF_NULL", + [POP(IF_NOT_NULL)] = "IF_NOT_NULL", + [POP(IF_EQUALS)] = "IF_EQUALS", + [POP(JMP)] = "JMP", +}; + +const uacpi_char *uacpi_parse_op_to_string(enum uacpi_parse_op op) +{ + if (uacpi_unlikely(op > UACPI_PARSE_OP_MAX)) + return ""; + + return pop_names[op]; +} diff --git a/src/uacpi/opregion.c b/src/uacpi/opregion.c new file mode 100644 index 0000000..dd85204 --- /dev/null +++ b/src/uacpi/opregion.c @@ -0,0 +1,881 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +struct uacpi_recursive_lock g_opregion_lock; + +uacpi_status uacpi_initialize_opregion(void) +{ + return uacpi_recursive_lock_init(&g_opregion_lock); +} + +void uacpi_deinitialize_opregion(void) +{ + uacpi_recursive_lock_deinit(&g_opregion_lock); +} + +void uacpi_trace_region_error( + uacpi_namespace_node *node, uacpi_char *message, uacpi_status ret +) +{ + const uacpi_char *path, *space_string = ""; + uacpi_object *obj; + + path = uacpi_namespace_node_generate_absolute_path(node); + + obj = uacpi_namespace_node_get_object_typed( + node, UACPI_OBJECT_OPERATION_REGION_BIT + ); + if (uacpi_likely(obj != UACPI_NULL)) + space_string = uacpi_address_space_to_string(obj->op_region->space); + + uacpi_error( + "%s (%s) operation region %s: %s\n", + message, space_string, path, uacpi_status_to_string(ret) + ); + uacpi_free_dynamic_string(path); +} + +#define UACPI_TRACE_REGION_IO + +void uacpi_trace_region_io( + uacpi_namespace_node *node, uacpi_address_space space, uacpi_region_op op, + uacpi_u64 offset, uacpi_u8 byte_size, uacpi_u64 ret +) +{ +#ifdef UACPI_TRACE_REGION_IO + const uacpi_char *path; + const uacpi_char *type_str; + + if (!uacpi_should_log(UACPI_LOG_TRACE)) + return; + + switch (op) { + case UACPI_REGION_OP_READ: + type_str = "read from"; + break; + case UACPI_REGION_OP_WRITE: + type_str = "write to"; + break; + default: + type_str = ""; + } + + path = uacpi_namespace_node_generate_absolute_path(node); + + uacpi_trace( + "%s [%s] (%d bytes) %s[0x%016"UACPI_PRIX64"] = 0x%"UACPI_PRIX64"\n", + type_str, path, byte_size, + uacpi_address_space_to_string(space), + UACPI_FMT64(offset), UACPI_FMT64(ret) + ); + + uacpi_free_dynamic_string(path); +#else + UACPI_UNUSED(op); + UACPI_UNUSED(node); + UACPI_UNUSED(offset); + UACPI_UNUSED(byte_size); + UACPI_UNUSED(ret); +#endif +} + +static uacpi_bool space_needs_reg(enum uacpi_address_space space) +{ + if (space == UACPI_ADDRESS_SPACE_SYSTEM_MEMORY || + space == UACPI_ADDRESS_SPACE_SYSTEM_IO || + space == UACPI_ADDRESS_SPACE_TABLE_DATA) + return UACPI_FALSE; + + return UACPI_TRUE; +} + +static uacpi_status region_run_reg( + uacpi_namespace_node *node, uacpi_u8 connection_code +) +{ + uacpi_status ret; + uacpi_namespace_node *reg_node; + uacpi_object_array method_args; + uacpi_object *reg_obj, *args[2]; + + ret = uacpi_namespace_node_resolve( + node->parent, "_REG", UACPI_SHOULD_LOCK_NO, + UACPI_MAY_SEARCH_ABOVE_PARENT_NO, UACPI_PERMANENT_ONLY_NO, ®_node + ); + if (uacpi_unlikely_error(ret)) + return ret; + + reg_obj = uacpi_namespace_node_get_object_typed( + reg_node, UACPI_OBJECT_METHOD_BIT + ); + if (uacpi_unlikely(reg_obj == UACPI_NULL)) + return UACPI_STATUS_OK; + + args[0] = uacpi_create_object(UACPI_OBJECT_INTEGER); + if (uacpi_unlikely(args[0] == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + args[1] = uacpi_create_object(UACPI_OBJECT_INTEGER); + if (uacpi_unlikely(args[1] == UACPI_NULL)) { + uacpi_object_unref(args[0]); + return UACPI_STATUS_OUT_OF_MEMORY; + } + + args[0]->integer = uacpi_namespace_node_get_object(node)->op_region->space; + args[1]->integer = connection_code; + method_args.objects = args; + method_args.count = 2; + + ret = uacpi_execute_control_method( + reg_node, reg_obj->method, &method_args, UACPI_NULL + ); + if (uacpi_unlikely_error(ret)) + uacpi_trace_region_error(node, "error during _REG execution for", ret); + + uacpi_object_unref(args[0]); + uacpi_object_unref(args[1]); + return ret; +} + +uacpi_address_space_handlers *uacpi_node_get_address_space_handlers( + uacpi_namespace_node *node +) +{ + uacpi_object *object; + + if (node == uacpi_namespace_root()) + return g_uacpi_rt_ctx.root_object->address_space_handlers; + + object = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(object == UACPI_NULL)) + return UACPI_NULL; + + switch (object->type) { + case UACPI_OBJECT_DEVICE: + case UACPI_OBJECT_PROCESSOR: + case UACPI_OBJECT_THERMAL_ZONE: + return object->address_space_handlers; + default: + return UACPI_NULL; + } +} + +static uacpi_address_space_handler *find_handler( + uacpi_address_space_handlers *handlers, + enum uacpi_address_space space +) +{ + uacpi_address_space_handler *handler = handlers->head; + + while (handler) { + if (handler->space == space) + return handler; + + handler = handler->next; + } + + return UACPI_NULL; +} + +static uacpi_operation_region *find_previous_region_link( + uacpi_operation_region *region +) +{ + uacpi_address_space_handler *handler = region->handler; + uacpi_operation_region *parent = handler->regions; + + if (parent == region) + // This is the last attached region, it has no previous link + return region; + + while (parent->next != region) { + parent = parent->next; + + if (uacpi_unlikely(parent == UACPI_NULL)) + return UACPI_NULL; + } + + return parent; +} + +uacpi_status uacpi_opregion_attach(uacpi_namespace_node *node) +{ + uacpi_object *obj; + uacpi_operation_region *region; + uacpi_address_space_handler *handler; + uacpi_status ret; + uacpi_region_attach_data attach_data = { 0 }; + + if (uacpi_namespace_node_is_dangling(node)) + return UACPI_STATUS_NAMESPACE_NODE_DANGLING; + + obj = uacpi_namespace_node_get_object_typed( + node, UACPI_OBJECT_OPERATION_REGION_BIT + ); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + region = obj->op_region; + + if (region->handler == UACPI_NULL) + return UACPI_STATUS_NO_HANDLER; + if (region->state_flags & UACPI_OP_REGION_STATE_ATTACHED) + return UACPI_STATUS_OK; + + handler = region->handler; + attach_data.region_node = node; + attach_data.handler_context = handler->user_context; + + uacpi_object_ref(obj); + uacpi_namespace_write_unlock(); + ret = handler->callback(UACPI_REGION_OP_ATTACH, &attach_data); + uacpi_namespace_write_lock(); + + if (uacpi_unlikely_error(ret)) { + uacpi_trace_region_error(node, "failed to attach a handler to", ret); + uacpi_object_unref(obj); + return ret; + } + + region->state_flags |= UACPI_OP_REGION_STATE_ATTACHED; + region->user_context = attach_data.out_region_context; + uacpi_object_unref(obj); + return ret; +} + +static void region_install_handler( + uacpi_namespace_node *node, uacpi_address_space_handler *handler +) +{ + uacpi_operation_region *region; + + region = uacpi_namespace_node_get_object(node)->op_region; + region->handler = handler; + uacpi_shareable_ref(handler); + + region->next = handler->regions; + handler->regions = region; +} + +enum unreg { + UNREG_NO = 0, + UNREG_YES, +}; + +static void region_uninstall_handler( + uacpi_namespace_node *node, enum unreg unreg +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_address_space_handler *handler; + uacpi_operation_region *region, *link; + + obj = uacpi_namespace_node_get_object_typed( + node, UACPI_OBJECT_OPERATION_REGION_BIT + ); + if (uacpi_unlikely(obj == UACPI_NULL)) + return; + + region = obj->op_region; + + handler = region->handler; + if (handler == UACPI_NULL) + return; + + link = find_previous_region_link(region); + if (uacpi_unlikely(link == UACPI_NULL)) { + uacpi_error("operation region @%p not in the handler@%p list(?)\n", + region, handler); + goto out; + } else if (link == region) { + link = link->next; + handler->regions = link; + } else { + link->next = region->next; + } + +out: + if (region->state_flags & UACPI_OP_REGION_STATE_ATTACHED) { + uacpi_region_detach_data detach_data = { + .region_node = node, + .region_context = region->user_context, + .handler_context = handler->user_context, + }; + + uacpi_shareable_ref(node); + uacpi_namespace_write_unlock(); + + ret = handler->callback(UACPI_REGION_OP_DETACH, &detach_data); + + uacpi_namespace_write_lock(); + uacpi_namespace_node_unref(node); + + if (uacpi_unlikely_error(ret)) { + uacpi_trace_region_error( + node, "error during handler detach for", ret + ); + } + } + + if ((region->state_flags & UACPI_OP_REGION_STATE_REG_EXECUTED) && + unreg == UNREG_YES) { + region_run_reg(node, ACPI_REG_DISCONNECT); + region->state_flags &= ~UACPI_OP_REGION_STATE_REG_EXECUTED; + } + + uacpi_address_space_handler_unref(region->handler); + region->handler = UACPI_NULL; + region->state_flags &= ~UACPI_OP_REGION_STATE_ATTACHED; +} + +static uacpi_status upgrade_to_opregion_lock(void) +{ + uacpi_status ret; + + /* + * Drop the namespace lock, and reacquire it after the opregion lock + * so we keep the ordering with user API. + */ + uacpi_namespace_write_unlock(); + + ret = uacpi_recursive_lock_acquire(&g_opregion_lock); + uacpi_namespace_write_lock(); + return ret; +} + +void uacpi_opregion_uninstall_handler(uacpi_namespace_node *node) +{ + if (uacpi_unlikely_error(upgrade_to_opregion_lock())) + return; + + region_uninstall_handler(node, UNREG_YES); + + uacpi_recursive_lock_release(&g_opregion_lock); +} + +uacpi_bool uacpi_address_space_handler_is_default( + uacpi_address_space_handler *handler +) +{ + return handler->flags & UACPI_ADDRESS_SPACE_HANDLER_DEFAULT; +} + +enum opregion_iter_action { + OPREGION_ITER_ACTION_UNINSTALL, + OPREGION_ITER_ACTION_INSTALL, +}; + +struct opregion_iter_ctx { + enum opregion_iter_action action; + uacpi_address_space_handler *handler; +}; + +static uacpi_iteration_decision do_install_or_uninstall_handler( + uacpi_handle opaque, uacpi_namespace_node *node, uacpi_u32 depth +) +{ + struct opregion_iter_ctx *ctx = opaque; + uacpi_address_space_handlers *handlers; + uacpi_object *object; + + UACPI_UNUSED(depth); + + object = uacpi_namespace_node_get_object(node); + if (object->type == UACPI_OBJECT_OPERATION_REGION) { + uacpi_operation_region *region = object->op_region; + + if (region->space != ctx->handler->space) + return UACPI_ITERATION_DECISION_CONTINUE; + + if (ctx->action == OPREGION_ITER_ACTION_INSTALL) { + if (region->handler) + region_uninstall_handler(node, UNREG_NO); + + region_install_handler(node, ctx->handler); + } else { + if (uacpi_unlikely(region->handler != ctx->handler)) { + uacpi_trace_region_error( + node, "handler mismatch for", + UACPI_STATUS_INTERNAL_ERROR + ); + return UACPI_ITERATION_DECISION_CONTINUE; + } + + region_uninstall_handler(node, UNREG_NO); + } + + return UACPI_ITERATION_DECISION_CONTINUE; + } + + handlers = uacpi_node_get_address_space_handlers(node); + if (handlers == UACPI_NULL) + return UACPI_ITERATION_DECISION_CONTINUE; + + // Device already has a handler for this space installed + if (find_handler(handlers, ctx->handler->space) != UACPI_NULL) + return UACPI_ITERATION_DECISION_NEXT_PEER; + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +struct reg_run_ctx { + uacpi_u8 space; + uacpi_u8 connection_code; + uacpi_size reg_executed; + uacpi_size reg_errors; +}; + +static uacpi_iteration_decision do_run_reg( + void *opaque, uacpi_namespace_node *node, uacpi_u32 depth +) +{ + struct reg_run_ctx *ctx = opaque; + uacpi_operation_region *region; + uacpi_status ret; + uacpi_bool was_regged; + + UACPI_UNUSED(depth); + + region = uacpi_namespace_node_get_object(node)->op_region; + + if (region->space != ctx->space) + return UACPI_ITERATION_DECISION_CONTINUE; + + was_regged = region->state_flags & UACPI_OP_REGION_STATE_REG_EXECUTED; + if (was_regged == (ctx->connection_code == ACPI_REG_CONNECT)) + return UACPI_ITERATION_DECISION_CONTINUE; + + ret = region_run_reg(node, ctx->connection_code); + if (ctx->connection_code == ACPI_REG_DISCONNECT) + region->state_flags &= ~UACPI_OP_REGION_STATE_REG_EXECUTED; + + if (ret == UACPI_STATUS_NOT_FOUND) + return UACPI_ITERATION_DECISION_CONTINUE; + + if (ctx->connection_code == ACPI_REG_CONNECT) + region->state_flags |= UACPI_OP_REGION_STATE_REG_EXECUTED; + + ctx->reg_executed++; + + if (uacpi_unlikely_error(ret)) { + ctx->reg_errors++; + return UACPI_ITERATION_DECISION_CONTINUE; + } + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +static uacpi_status reg_or_unreg_all_opregions( + uacpi_namespace_node *device_node, enum uacpi_address_space space, + uacpi_u8 connection_code +) +{ + uacpi_address_space_handlers *handlers; + uacpi_bool is_connect; + enum uacpi_permanent_only perm_only; + struct reg_run_ctx ctx = { + .space = space, + .connection_code = connection_code, + }; + + handlers = uacpi_node_get_address_space_handlers(device_node); + if (uacpi_unlikely(handlers == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + is_connect = connection_code == ACPI_REG_CONNECT; + if (uacpi_unlikely(is_connect && + find_handler(handlers, space) == UACPI_NULL)) + return UACPI_STATUS_NO_HANDLER; + + /* + * We want to unreg non-permanent opregions as well, however, + * registering them is handled separately and should not be + * done by us. + */ + perm_only = is_connect ? UACPI_PERMANENT_ONLY_YES : UACPI_PERMANENT_ONLY_NO; + + uacpi_namespace_do_for_each_child( + device_node, do_run_reg, UACPI_NULL, + UACPI_OBJECT_OPERATION_REGION_BIT, UACPI_MAX_DEPTH_ANY, + UACPI_SHOULD_LOCK_NO, perm_only, &ctx + ); + + uacpi_trace( + "%sactivated all '%s' opregions controlled by '%.4s', " + "%zu _REG() calls (%zu errors)\n", + connection_code == ACPI_REG_CONNECT ? "" : "de", + uacpi_address_space_to_string(space), + device_node->name.text, ctx.reg_executed, ctx.reg_errors + ); + return UACPI_STATUS_OK; +} + +static uacpi_address_space_handlers *extract_handlers( + uacpi_namespace_node *node +) +{ + uacpi_object *handlers_obj; + + if (node == uacpi_namespace_root()) + return g_uacpi_rt_ctx.root_object->address_space_handlers; + + handlers_obj = uacpi_namespace_node_get_object_typed( + node, + UACPI_OBJECT_DEVICE_BIT | UACPI_OBJECT_THERMAL_ZONE_BIT | + UACPI_OBJECT_PROCESSOR_BIT + ); + if (uacpi_unlikely(handlers_obj == UACPI_NULL)) + return UACPI_NULL; + + return handlers_obj->address_space_handlers; +} + +uacpi_status uacpi_reg_all_opregions( + uacpi_namespace_node *device_node, + enum uacpi_address_space space +) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + if (!space_needs_reg(space)) + return UACPI_STATUS_OK; + + ret = uacpi_recursive_lock_acquire(&g_opregion_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_namespace_write_lock(); + if (uacpi_unlikely_error(ret)) { + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; + } + + if (uacpi_unlikely(extract_handlers(device_node) == UACPI_NULL)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + ret = reg_or_unreg_all_opregions(device_node, space, ACPI_REG_CONNECT); + +out: + uacpi_namespace_write_unlock(); + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; +} + +uacpi_status uacpi_install_address_space_handler_with_flags( + uacpi_namespace_node *device_node, enum uacpi_address_space space, + uacpi_region_handler handler, uacpi_handle handler_context, + uacpi_u16 flags +) +{ + uacpi_status ret; + uacpi_address_space_handlers *handlers; + uacpi_address_space_handler *this_handler, *new_handler; + struct opregion_iter_ctx iter_ctx; + + ret = uacpi_recursive_lock_acquire(&g_opregion_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_namespace_write_lock(); + if (uacpi_unlikely_error(ret)) { + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; + } + + handlers = extract_handlers(device_node); + if (uacpi_unlikely(handlers == UACPI_NULL)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + this_handler = find_handler(handlers, space); + if (this_handler != UACPI_NULL) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + new_handler = uacpi_kernel_alloc(sizeof(*new_handler)); + if (new_handler == UACPI_NULL) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + uacpi_shareable_init(new_handler); + + new_handler->next = handlers->head; + new_handler->space = space; + new_handler->user_context = handler_context; + new_handler->callback = handler; + new_handler->regions = UACPI_NULL; + new_handler->flags = flags; + handlers->head = new_handler; + + iter_ctx.handler = new_handler; + iter_ctx.action = OPREGION_ITER_ACTION_INSTALL; + + uacpi_namespace_do_for_each_child( + device_node, do_install_or_uninstall_handler, UACPI_NULL, + UACPI_OBJECT_ANY_BIT, UACPI_MAX_DEPTH_ANY, UACPI_SHOULD_LOCK_NO, + UACPI_PERMANENT_ONLY_YES, &iter_ctx + ); + + if (!space_needs_reg(space)) + goto out; + + /* + * Installing an early address space handler, obviously not possible to + * execute any _REG methods here. Just return and hope that it is either + * a global address space handler, or a handler installed by a user who + * will run uacpi_reg_all_opregions manually after loading/initializing + * the namespace. + */ + if (g_uacpi_rt_ctx.init_level < UACPI_INIT_LEVEL_NAMESPACE_LOADED) + goto out; + + // Init level is NAMESPACE_INITIALIZED, so we can safely run _REG now + ret = reg_or_unreg_all_opregions( + device_node, space, ACPI_REG_CONNECT + ); + +out: + uacpi_namespace_write_unlock(); + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; +} + +uacpi_status uacpi_install_address_space_handler( + uacpi_namespace_node *device_node, enum uacpi_address_space space, + uacpi_region_handler handler, uacpi_handle handler_context +) +{ + return uacpi_install_address_space_handler_with_flags( + device_node, space, handler, handler_context, 0 + ); +} + +uacpi_status uacpi_uninstall_address_space_handler( + uacpi_namespace_node *device_node, + enum uacpi_address_space space +) +{ + uacpi_status ret; + uacpi_address_space_handlers *handlers; + uacpi_address_space_handler *handler = UACPI_NULL, *prev_handler; + struct opregion_iter_ctx iter_ctx; + + ret = uacpi_recursive_lock_acquire(&g_opregion_lock); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_namespace_write_lock(); + if (uacpi_unlikely_error(ret)) { + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; + } + + handlers = extract_handlers(device_node); + if (uacpi_unlikely(handlers == UACPI_NULL)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + handler = find_handler(handlers, space); + if (uacpi_unlikely(handler == UACPI_NULL)) { + ret = UACPI_STATUS_NO_HANDLER; + goto out; + } + + iter_ctx.handler = handler; + iter_ctx.action = OPREGION_ITER_ACTION_UNINSTALL; + + uacpi_namespace_do_for_each_child( + device_node, do_install_or_uninstall_handler, UACPI_NULL, + UACPI_OBJECT_ANY_BIT, UACPI_MAX_DEPTH_ANY, UACPI_SHOULD_LOCK_NO, + UACPI_PERMANENT_ONLY_NO, &iter_ctx + ); + + prev_handler = handlers->head; + + // Are we the last linked handler? + if (prev_handler == handler) { + handlers->head = handler->next; + goto out_unreg; + } + + // Nope, we're somewhere in the middle. Do a search. + while (prev_handler) { + if (prev_handler->next == handler) { + prev_handler->next = handler->next; + goto out; + } + + prev_handler = prev_handler->next; + } + +out_unreg: + if (space_needs_reg(space)) + reg_or_unreg_all_opregions(device_node, space, ACPI_REG_DISCONNECT); + +out: + if (handler != UACPI_NULL) + uacpi_address_space_handler_unref(handler); + + uacpi_namespace_write_unlock(); + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; +} + +uacpi_status uacpi_initialize_opregion_node(uacpi_namespace_node *node) +{ + uacpi_status ret; + uacpi_namespace_node *parent = node->parent; + uacpi_operation_region *region; + uacpi_address_space_handlers *handlers; + uacpi_address_space_handler *handler; + + ret = upgrade_to_opregion_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + region = uacpi_namespace_node_get_object(node)->op_region; + ret = UACPI_STATUS_NOT_FOUND; + + while (parent) { + handlers = uacpi_node_get_address_space_handlers(parent); + if (handlers != UACPI_NULL) { + handler = find_handler(handlers, region->space); + + if (handler != UACPI_NULL) { + region_install_handler(node, handler); + ret = UACPI_STATUS_OK; + break; + } + } + + parent = parent->parent; + } + + if (ret != UACPI_STATUS_OK) + goto out; + if (!space_needs_reg(region->space)) + goto out; + if (uacpi_get_current_init_level() < UACPI_INIT_LEVEL_NAMESPACE_LOADED) + goto out; + + if (region_run_reg(node, ACPI_REG_CONNECT) != UACPI_STATUS_NOT_FOUND) + region->state_flags |= UACPI_OP_REGION_STATE_REG_EXECUTED; + +out: + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; +} + +uacpi_status uacpi_dispatch_opregion_io( + uacpi_namespace_node *region_node, uacpi_u32 offset, uacpi_u8 byte_width, + uacpi_region_op op, uacpi_u64 *in_out +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_operation_region *region; + uacpi_address_space_handler *handler; + uacpi_address_space space; + uacpi_u64 offset_end; + + uacpi_region_rw_data data = { + .byte_width = byte_width, + .offset = offset, + }; + + ret = upgrade_to_opregion_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_opregion_attach(region_node); + if (uacpi_unlikely_error(ret)) { + uacpi_trace_region_error( + region_node, "unable to attach", ret + ); + goto out; + } + + obj = uacpi_namespace_node_get_object_typed( + region_node, UACPI_OBJECT_OPERATION_REGION_BIT + ); + if (uacpi_unlikely(obj == UACPI_NULL)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + region = obj->op_region; + space = region->space; + handler = region->handler; + + offset_end = offset; + offset_end += byte_width; + data.offset += region->offset; + + if (uacpi_unlikely(region->length < offset_end || + data.offset < offset)) { + const uacpi_char *path; + + path = uacpi_namespace_node_generate_absolute_path(region_node); + uacpi_error( + "out-of-bounds access to opregion %s[0x%"UACPI_PRIX64"->" + "0x%"UACPI_PRIX64"] at 0x%"UACPI_PRIX64" (idx=%u, width=%d)\n", + path, UACPI_FMT64(region->offset), + UACPI_FMT64(region->offset + region->length), + UACPI_FMT64(data.offset), offset, byte_width + ); + uacpi_free_dynamic_string(path); + ret = UACPI_STATUS_AML_OUT_OF_BOUNDS_INDEX; + goto out; + } + + data.handler_context = handler->user_context; + data.region_context = region->user_context; + + if (op == UACPI_REGION_OP_WRITE) { + data.value = *in_out; + uacpi_trace_region_io( + region_node, space, op, data.offset, + byte_width, data.value + ); + } + + uacpi_object_ref(obj); + uacpi_namespace_write_unlock(); + + ret = handler->callback(op, &data); + + uacpi_namespace_write_lock(); + uacpi_object_unref(obj); + + if (uacpi_unlikely_error(ret)) { + uacpi_trace_region_error(region_node, "unable to perform IO", ret); + goto out; + } + + if (op == UACPI_REGION_OP_READ) { + *in_out = data.value; + uacpi_trace_region_io( + region_node, space, op, data.offset, + byte_width, data.value + ); + } + +out: + uacpi_recursive_lock_release(&g_opregion_lock); + return ret; +} diff --git a/src/uacpi/osi.c b/src/uacpi/osi.c new file mode 100644 index 0000000..c9f379e --- /dev/null +++ b/src/uacpi/osi.c @@ -0,0 +1,384 @@ +#include +#include +#include +#include +#include +#include +#include + +struct registered_interface { + const uacpi_char *name; + uacpi_u8 weight; + uacpi_u8 kind; + + // Only applicable for predefined host interfaces + uacpi_u8 host_type; + + // Only applicable for predefined interfaces + uacpi_u8 disabled : 1; + uacpi_u8 dynamic : 1; + + struct registered_interface *next; +}; + +static uacpi_handle interface_mutex; +static struct registered_interface *registered_interfaces; +static uacpi_interface_handler interface_handler; +static uacpi_u32 latest_queried_interface; + +#define WINDOWS(string, interface) \ + { \ + .name = "Windows "string, \ + .weight = UACPI_VENDOR_INTERFACE_WINDOWS_##interface, \ + .kind = UACPI_INTERFACE_KIND_VENDOR, \ + .host_type = 0, \ + .disabled = 0, \ + .dynamic = 0, \ + .next = UACPI_NULL \ + } + +#define HOST_FEATURE(string, type) \ + { \ + .name = string, \ + .weight = 0, \ + .kind = UACPI_INTERFACE_KIND_FEATURE, \ + .host_type = UACPI_HOST_INTERFACE_##type, \ + .disabled = 1, \ + .dynamic = 0, \ + .next = UACPI_NULL, \ + } + +static struct registered_interface predefined_interfaces[] = { + // Vendor strings + WINDOWS("2000", 2000), + WINDOWS("2001", XP), + WINDOWS("2001 SP1", XP_SP1), + WINDOWS("2001.1", SERVER_2003), + WINDOWS("2001 SP2", XP_SP2), + WINDOWS("2001.1 SP1", SERVER_2003_SP1), + WINDOWS("2006", VISTA), + WINDOWS("2006.1", SERVER_2008), + WINDOWS("2006 SP1", VISTA_SP1), + WINDOWS("2006 SP2", VISTA_SP2), + WINDOWS("2009", 7), + WINDOWS("2012", 8), + WINDOWS("2013", 8_1), + WINDOWS("2015", 10), + WINDOWS("2016", 10_RS1), + WINDOWS("2017", 10_RS2), + WINDOWS("2017.2", 10_RS3), + WINDOWS("2018", 10_RS4), + WINDOWS("2018.2", 10_RS5), + WINDOWS("2019", 10_19H1), + WINDOWS("2020", 10_20H1), + WINDOWS("2021", 11), + WINDOWS("2022", 11_22H2), + + // Feature strings + HOST_FEATURE("Module Device", MODULE_DEVICE), + HOST_FEATURE("Processor Device", PROCESSOR_DEVICE), + HOST_FEATURE("3.0 Thermal Model", 3_0_THERMAL_MODEL), + HOST_FEATURE("3.0 _SCP Extensions", 3_0_SCP_EXTENSIONS), + HOST_FEATURE("Processor Aggregator Device", PROCESSOR_AGGREGATOR_DEVICE), + + // Interpreter features + { .name = "Extended Address Space Descriptor" }, +}; + +uacpi_status uacpi_initialize_interfaces(void) +{ + uacpi_size i; + + registered_interfaces = &predefined_interfaces[0]; + + interface_mutex = uacpi_kernel_create_mutex(); + if (uacpi_unlikely(interface_mutex == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + for (i = 0; i < (UACPI_ARRAY_SIZE(predefined_interfaces) - 1); ++i) + predefined_interfaces[i].next = &predefined_interfaces[i + 1]; + + return UACPI_STATUS_OK; +} + +void uacpi_deinitialize_interfaces(void) +{ + struct registered_interface *iface, *next_iface = registered_interfaces; + + while (next_iface) { + iface = next_iface; + next_iface = iface->next; + + iface->next = UACPI_NULL; + + if (iface->dynamic) { + uacpi_free_dynamic_string(iface->name); + uacpi_free(iface, sizeof(*iface)); + continue; + } + + // Only features are disabled by default + iface->disabled = iface->kind == UACPI_INTERFACE_KIND_FEATURE ? + UACPI_TRUE : UACPI_FALSE; + } + + if (interface_mutex) + uacpi_kernel_free_mutex(interface_mutex); + + interface_mutex = UACPI_NULL; + interface_handler = UACPI_NULL; + latest_queried_interface = 0; + registered_interfaces = UACPI_NULL; +} + +uacpi_vendor_interface uacpi_latest_queried_vendor_interface(void) +{ + return uacpi_atomic_load32(&latest_queried_interface); +} + +static struct registered_interface *find_interface_unlocked( + const uacpi_char *name +) +{ + struct registered_interface *interface = registered_interfaces; + + while (interface) { + if (uacpi_strcmp(interface->name, name) == 0) + return interface; + + interface = interface->next; + } + + return UACPI_NULL; +} + +static struct registered_interface *find_host_interface_unlocked( + uacpi_host_interface type +) +{ + struct registered_interface *interface = registered_interfaces; + + while (interface) { + if (interface->host_type == type) + return interface; + + interface = interface->next; + } + + return UACPI_NULL; +} + +uacpi_status uacpi_install_interface( + const uacpi_char *name, uacpi_interface_kind kind +) +{ + struct registered_interface *interface; + uacpi_status ret; + uacpi_char *name_copy; + uacpi_size name_size; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex(interface_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + interface = find_interface_unlocked(name); + if (interface != UACPI_NULL) { + if (interface->disabled) + interface->disabled = UACPI_FALSE; + + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + interface = uacpi_kernel_alloc(sizeof(*interface)); + if (uacpi_unlikely(interface == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + + name_size = uacpi_strlen(name) + 1; + name_copy = uacpi_kernel_alloc(name_size); + if (uacpi_unlikely(name_copy == UACPI_NULL)) { + uacpi_free(interface, sizeof(*interface)); + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + + uacpi_memcpy(name_copy, name, name_size); + interface->name = name_copy; + interface->weight = 0; + interface->kind = kind; + interface->host_type = 0; + interface->disabled = 0; + interface->dynamic = 1; + interface->next = registered_interfaces; + registered_interfaces = interface; + +out: + uacpi_release_native_mutex(interface_mutex); + return ret; +} + +uacpi_status uacpi_uninstall_interface(const uacpi_char *name) +{ + struct registered_interface *cur, *prev; + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex(interface_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + cur = registered_interfaces; + prev = cur; + + ret = UACPI_STATUS_NOT_FOUND; + while (cur) { + if (uacpi_strcmp(cur->name, name) != 0) { + prev = cur; + cur = cur->next; + continue; + } + + if (cur->dynamic) { + if (prev == cur) { + registered_interfaces = cur->next; + } else { + prev->next = cur->next; + } + + uacpi_release_native_mutex(interface_mutex); + uacpi_free_dynamic_string(cur->name); + uacpi_free(cur, sizeof(*cur)); + return UACPI_STATUS_OK; + } + + /* + * If this interface was already disabled, pretend we didn't actually + * find it and keep ret as UACPI_STATUS_NOT_FOUND. The fact that it's + * still in the registered list is an implementation detail of + * predefined interfaces. + */ + if (!cur->disabled) { + cur->disabled = UACPI_TRUE; + ret = UACPI_STATUS_OK; + } + + break; + } + + uacpi_release_native_mutex(interface_mutex); + return ret; +} + +static uacpi_status configure_host_interface( + uacpi_host_interface type, uacpi_bool enabled +) +{ + struct registered_interface *interface; + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex(interface_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + interface = find_host_interface_unlocked(type); + if (interface == UACPI_NULL) { + ret = UACPI_STATUS_NOT_FOUND; + goto out; + } + + interface->disabled = !enabled; +out: + uacpi_release_native_mutex(interface_mutex); + return ret; +} + +uacpi_status uacpi_enable_host_interface(uacpi_host_interface type) +{ + return configure_host_interface(type, UACPI_TRUE); +} + +uacpi_status uacpi_disable_host_interface(uacpi_host_interface type) +{ + return configure_host_interface(type, UACPI_FALSE); +} + +uacpi_status uacpi_set_interface_query_handler( + uacpi_interface_handler handler +) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex(interface_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + if (interface_handler != UACPI_NULL && handler != UACPI_NULL) { + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + + interface_handler = handler; +out: + uacpi_release_native_mutex(interface_mutex); + return ret; +} + +uacpi_status uacpi_bulk_configure_interfaces( + uacpi_interface_action action, uacpi_interface_kind kind +) +{ + uacpi_status ret; + struct registered_interface *interface; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex(interface_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + interface = registered_interfaces; + while (interface) { + if (kind & interface->kind) + interface->disabled = (action == UACPI_INTERFACE_ACTION_DISABLE); + + interface = interface->next; + } + + uacpi_release_native_mutex(interface_mutex); + return ret; +} + +uacpi_status uacpi_handle_osi(const uacpi_char *string, uacpi_bool *out_value) +{ + uacpi_status ret; + struct registered_interface *interface; + uacpi_bool is_supported = UACPI_FALSE; + + ret = uacpi_acquire_native_mutex(interface_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + interface = find_interface_unlocked(string); + if (interface == UACPI_NULL) + goto out; + + if (interface->weight > latest_queried_interface) + uacpi_atomic_store32(&latest_queried_interface, interface->weight); + + is_supported = !interface->disabled; + if (interface_handler) + is_supported = interface_handler(string, is_supported); +out: + uacpi_release_native_mutex(interface_mutex); + *out_value = is_supported; + return UACPI_STATUS_OK; +} diff --git a/src/uacpi/registers.c b/src/uacpi/registers.c new file mode 100644 index 0000000..1efe280 --- /dev/null +++ b/src/uacpi/registers.c @@ -0,0 +1,439 @@ +#include +#include +#include +#include +#include + +enum register_kind { + REGISTER_KIND_GAS, + REGISTER_KIND_IO, +}; + +enum register_access_kind { + REGISTER_ACCESS_KIND_PRESERVE, + REGISTER_ACCESS_KIND_WRITE_TO_CLEAR, + REGISTER_ACCESS_KIND_NORMAL, +}; + +struct register_spec { + uacpi_u8 kind; + uacpi_u8 access_kind; + uacpi_u8 access_width; // only REGISTER_KIND_IO + void *accessor0, *accessor1; + uacpi_u64 write_only_mask; + uacpi_u64 preserve_mask; +}; + +static const struct register_spec registers[UACPI_REGISTER_MAX + 1] = { + [UACPI_REGISTER_PM1_STS] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_WRITE_TO_CLEAR, + .accessor0 = &g_uacpi_rt_ctx.pm1a_status_blk, + .accessor1 = &g_uacpi_rt_ctx.pm1b_status_blk, + .preserve_mask = ACPI_PM1_STS_IGN0_MASK, + }, + [UACPI_REGISTER_PM1_EN] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_PRESERVE, + .accessor0 = &g_uacpi_rt_ctx.pm1a_enable_blk, + .accessor1 = &g_uacpi_rt_ctx.pm1b_enable_blk, + }, + [UACPI_REGISTER_PM1_CNT] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_PRESERVE, + .accessor0 = &g_uacpi_rt_ctx.fadt.x_pm1a_cnt_blk, + .accessor1 = &g_uacpi_rt_ctx.fadt.x_pm1b_cnt_blk, + .write_only_mask = ACPI_PM1_CNT_SLP_EN_MASK | + ACPI_PM1_CNT_GBL_RLS_MASK, + .preserve_mask = ACPI_PM1_CNT_PRESERVE_MASK, + }, + [UACPI_REGISTER_PM_TMR] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_PRESERVE, + .accessor0 = &g_uacpi_rt_ctx.fadt.x_pm_tmr_blk, + }, + [UACPI_REGISTER_PM2_CNT] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_PRESERVE, + .accessor0 = &g_uacpi_rt_ctx.fadt.x_pm2_cnt_blk, + .preserve_mask = ACPI_PM2_CNT_PRESERVE_MASK, + }, + [UACPI_REGISTER_SLP_CNT] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_PRESERVE, + .accessor0 = &g_uacpi_rt_ctx.fadt.sleep_control_reg, + .write_only_mask = ACPI_SLP_CNT_SLP_EN_MASK, + .preserve_mask = ACPI_SLP_CNT_PRESERVE_MASK, + }, + [UACPI_REGISTER_SLP_STS] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_WRITE_TO_CLEAR, + .accessor0 = &g_uacpi_rt_ctx.fadt.sleep_status_reg, + .preserve_mask = ACPI_SLP_STS_PRESERVE_MASK, + }, + [UACPI_REGISTER_RESET] = { + .kind = REGISTER_KIND_GAS, + .access_kind = REGISTER_ACCESS_KIND_NORMAL, + .accessor0 = &g_uacpi_rt_ctx.fadt.reset_reg, + }, + [UACPI_REGISTER_SMI_CMD] = { + .kind = REGISTER_KIND_IO, + .access_kind = REGISTER_ACCESS_KIND_NORMAL, + .access_width = 1, + .accessor0 = &g_uacpi_rt_ctx.fadt.smi_cmd, + }, +}; + +static const struct register_spec *get_reg(uacpi_u8 idx) +{ + if (idx > UACPI_REGISTER_MAX) + return UACPI_NULL; + + return ®isters[idx]; +} + +static uacpi_status read_one( + enum register_kind kind, void *reg, uacpi_u8 byte_width, + uacpi_u64 *out_value +) +{ + if (kind == REGISTER_KIND_GAS) { + struct acpi_gas *gas = reg; + + if (!gas->address) + return UACPI_STATUS_OK; + + return uacpi_gas_read(reg, out_value); + } + + return uacpi_system_io_read(*(uacpi_u32*)reg, byte_width, out_value); +} + +static uacpi_status write_one( + enum register_kind kind, void *reg, uacpi_u8 byte_width, + uacpi_u64 in_value +) +{ + if (kind == REGISTER_KIND_GAS) { + struct acpi_gas *gas = reg; + + if (!gas->address) + return UACPI_STATUS_OK; + + return uacpi_gas_write(reg, in_value); + } + + return uacpi_system_io_write(*(uacpi_u32*)reg, byte_width, in_value); +} + +static uacpi_status do_read_register( + const struct register_spec *reg, uacpi_u64 *out_value +) +{ + uacpi_status ret; + uacpi_u64 value0, value1 = 0; + + ret = read_one(reg->kind, reg->accessor0, reg->access_width, &value0); + if (uacpi_unlikely_error(ret)) + return ret; + + if (reg->accessor1) { + ret = read_one(reg->kind, reg->accessor1, reg->access_width, &value1); + if (uacpi_unlikely_error(ret)) + return ret; + } + + *out_value = value0 | value1; + if (reg->write_only_mask) + *out_value &= ~reg->write_only_mask; + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_read_register( + enum uacpi_register reg_enum, uacpi_u64 *out_value +) +{ + const struct register_spec *reg; + + reg = get_reg(reg_enum); + if (uacpi_unlikely(reg == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + return do_read_register(reg, out_value); +} + +static uacpi_status do_write_register( + const struct register_spec *reg, uacpi_u64 in_value +) +{ + uacpi_status ret; + + if (reg->preserve_mask) { + in_value &= ~reg->preserve_mask; + + if (reg->access_kind == REGISTER_ACCESS_KIND_PRESERVE) { + uacpi_u64 data; + + ret = do_read_register(reg, &data); + if (uacpi_unlikely_error(ret)) + return ret; + + in_value |= data & reg->preserve_mask; + } + } + + ret = write_one(reg->kind, reg->accessor0, reg->access_width, in_value); + if (uacpi_unlikely_error(ret)) + return ret; + + if (reg->accessor1) + ret = write_one(reg->kind, reg->accessor1, reg->access_width, in_value); + + return ret; +} + +uacpi_status uacpi_write_register( + enum uacpi_register reg_enum, uacpi_u64 in_value +) +{ + const struct register_spec *reg; + + reg = get_reg(reg_enum); + if (uacpi_unlikely(reg == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + return do_write_register(reg, in_value); +} + +uacpi_status uacpi_write_registers( + enum uacpi_register reg_enum, uacpi_u64 in_value0, uacpi_u64 in_value1 +) +{ + uacpi_status ret; + const struct register_spec *reg; + + reg = get_reg(reg_enum); + if (uacpi_unlikely(reg == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = write_one(reg->kind, reg->accessor0, reg->access_width, in_value0); + if (uacpi_unlikely_error(ret)) + return ret; + + if (reg->accessor1) + ret = write_one(reg->kind, reg->accessor1, reg->access_width, in_value1); + + return ret; +} + +struct register_field { + uacpi_u8 reg; + uacpi_u8 offset; + uacpi_u16 mask; +}; + +static const struct register_field fields[UACPI_REGISTER_FIELD_MAX + 1] = { + [UACPI_REGISTER_FIELD_TMR_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_TMR_STS_IDX, + .mask = ACPI_PM1_STS_TMR_STS_MASK, + }, + [UACPI_REGISTER_FIELD_BM_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_BM_STS_IDX, + .mask = ACPI_PM1_STS_BM_STS_MASK, + }, + [UACPI_REGISTER_FIELD_GBL_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_GBL_STS_IDX, + .mask = ACPI_PM1_STS_GBL_STS_MASK, + }, + [UACPI_REGISTER_FIELD_PWRBTN_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_PWRBTN_STS_IDX, + .mask = ACPI_PM1_STS_PWRBTN_STS_MASK, + }, + [UACPI_REGISTER_FIELD_SLPBTN_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_SLPBTN_STS_IDX, + .mask = ACPI_PM1_STS_SLPBTN_STS_MASK, + }, + [UACPI_REGISTER_FIELD_RTC_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_RTC_STS_IDX, + .mask = ACPI_PM1_STS_RTC_STS_MASK, + }, + [UACPI_REGISTER_FIELD_HWR_WAK_STS] = { + .reg = UACPI_REGISTER_SLP_STS, + .offset = ACPI_SLP_STS_WAK_STS_IDX, + .mask = ACPI_SLP_STS_WAK_STS_MASK, + }, + [UACPI_REGISTER_FIELD_WAK_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_WAKE_STS_IDX, + .mask = ACPI_PM1_STS_WAKE_STS_MASK, + }, + [UACPI_REGISTER_FIELD_PCIEX_WAKE_STS] = { + .reg = UACPI_REGISTER_PM1_STS, + .offset = ACPI_PM1_STS_PCIEXP_WAKE_STS_IDX, + .mask = ACPI_PM1_STS_PCIEXP_WAKE_STS_MASK, + }, + [UACPI_REGISTER_FIELD_TMR_EN] = { + .reg = UACPI_REGISTER_PM1_EN, + .offset = ACPI_PM1_EN_TMR_EN_IDX, + .mask = ACPI_PM1_EN_TMR_EN_MASK, + }, + [UACPI_REGISTER_FIELD_GBL_EN] = { + .reg = UACPI_REGISTER_PM1_EN, + .offset = ACPI_PM1_EN_GBL_EN_IDX, + .mask = ACPI_PM1_EN_GBL_EN_MASK, + }, + [UACPI_REGISTER_FIELD_PWRBTN_EN] = { + .reg = UACPI_REGISTER_PM1_EN, + .offset = ACPI_PM1_EN_PWRBTN_EN_IDX, + .mask = ACPI_PM1_EN_PWRBTN_EN_MASK, + }, + [UACPI_REGISTER_FIELD_SLPBTN_EN] = { + .reg = UACPI_REGISTER_PM1_EN, + .offset = ACPI_PM1_EN_SLPBTN_EN_IDX, + .mask = ACPI_PM1_EN_SLPBTN_EN_MASK, + }, + [UACPI_REGISTER_FIELD_RTC_EN] = { + .reg = UACPI_REGISTER_PM1_EN, + .offset = ACPI_PM1_EN_RTC_EN_IDX, + .mask = ACPI_PM1_EN_RTC_EN_MASK, + }, + [UACPI_REGISTER_FIELD_PCIEXP_WAKE_DIS] = { + .reg = UACPI_REGISTER_PM1_EN, + .offset = ACPI_PM1_EN_PCIEXP_WAKE_DIS_IDX, + .mask = ACPI_PM1_EN_PCIEXP_WAKE_DIS_MASK, + }, + [UACPI_REGISTER_FIELD_SCI_EN] = { + .reg = UACPI_REGISTER_PM1_CNT, + .offset = ACPI_PM1_CNT_SCI_EN_IDX, + .mask = ACPI_PM1_CNT_SCI_EN_MASK, + }, + [UACPI_REGISTER_FIELD_BM_RLD] = { + .reg = UACPI_REGISTER_PM1_CNT, + .offset = ACPI_PM1_CNT_BM_RLD_IDX, + .mask = ACPI_PM1_CNT_BM_RLD_MASK, + }, + [UACPI_REGISTER_FIELD_GBL_RLS] = { + .reg = UACPI_REGISTER_PM1_CNT, + .offset = ACPI_PM1_CNT_GBL_RLS_IDX, + .mask = ACPI_PM1_CNT_GBL_RLS_MASK, + }, + [UACPI_REGISTER_FIELD_SLP_TYP] = { + .reg = UACPI_REGISTER_PM1_CNT, + .offset = ACPI_PM1_CNT_SLP_TYP_IDX, + .mask = ACPI_PM1_CNT_SLP_TYP_MASK, + }, + [UACPI_REGISTER_FIELD_SLP_EN] = { + .reg = UACPI_REGISTER_PM1_CNT, + .offset = ACPI_PM1_CNT_SLP_EN_IDX, + .mask = ACPI_PM1_CNT_SLP_EN_MASK, + }, + [UACPI_REGISTER_FIELD_HWR_SLP_TYP] = { + .reg = UACPI_REGISTER_SLP_CNT, + .offset = ACPI_SLP_CNT_SLP_TYP_IDX, + .mask = ACPI_SLP_CNT_SLP_TYP_MASK, + }, + [UACPI_REGISTER_FIELD_HWR_SLP_EN] = { + .reg = UACPI_REGISTER_SLP_CNT, + .offset = ACPI_SLP_CNT_SLP_EN_IDX, + .mask = ACPI_SLP_CNT_SLP_EN_MASK, + }, + [UACPI_REGISTER_FIELD_ARB_DIS] = { + .reg = UACPI_REGISTER_PM2_CNT, + .offset = ACPI_PM2_CNT_ARB_DIS_IDX, + .mask = ACPI_PM2_CNT_ARB_DIS_MASK, + }, +}; + +static uacpi_handle g_reg_lock; + +uacpi_status uacpi_ininitialize_registers(void) +{ + g_reg_lock = uacpi_kernel_create_spinlock(); + if (uacpi_unlikely(g_reg_lock == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +void uacpi_deininitialize_registers(void) +{ + if (g_reg_lock != UACPI_NULL) { + uacpi_kernel_free_spinlock(g_reg_lock); + g_reg_lock = UACPI_NULL; + } +} + +uacpi_status uacpi_read_register_field( + enum uacpi_register_field field_enum, uacpi_u64 *out_value +) +{ + uacpi_status ret; + uacpi_u8 field_idx = field_enum; + const struct register_field *field; + const struct register_spec *reg; + + if (uacpi_unlikely(field_idx > UACPI_REGISTER_FIELD_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + + field = &fields[field_idx]; + reg = ®isters[field->reg]; + + ret = do_read_register(reg, out_value); + if (uacpi_unlikely_error(ret)) + return ret; + + *out_value = (*out_value & field->mask) >> field->offset; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_write_register_field( + enum uacpi_register_field field_enum, uacpi_u64 in_value +) +{ + uacpi_status ret; + uacpi_u8 field_idx = field_enum; + const struct register_field *field; + const struct register_spec *reg; + uacpi_u64 data; + uacpi_cpu_flags flags; + + if (uacpi_unlikely(field_idx > UACPI_REGISTER_FIELD_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + + field = &fields[field_idx]; + reg = ®isters[field->reg]; + + in_value = (in_value << field->offset) & field->mask; + + flags = uacpi_kernel_lock_spinlock(g_reg_lock); + + if (reg->kind == REGISTER_ACCESS_KIND_WRITE_TO_CLEAR) { + if (in_value == 0) { + ret = UACPI_STATUS_OK; + goto out; + } + + ret = do_write_register(reg, in_value); + goto out; + } + + ret = do_read_register(reg, &data); + if (uacpi_unlikely_error(ret)) + goto out; + + data &= ~field->mask; + data |= in_value; + + ret = do_write_register(reg, data); + +out: + uacpi_kernel_unlock_spinlock(g_reg_lock, flags); + return ret; +} diff --git a/src/uacpi/resources.c b/src/uacpi/resources.c new file mode 100644 index 0000000..a8b683c --- /dev/null +++ b/src/uacpi/resources.c @@ -0,0 +1,2500 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define LARGE_RESOURCE_BASE (ACPI_RESOURCE_END_TAG + 1) +#define L(x) (x + LARGE_RESOURCE_BASE) + +/* + * Map raw AML resource types to the internal enum, this also takes care of type + * sanitization by returning UACPI_AML_RESOURCE_INVALID for any unknown type. + */ +static const uacpi_u8 aml_resource_to_type[256] = { + // Small items + [ACPI_RESOURCE_IRQ] = UACPI_AML_RESOURCE_IRQ, + [ACPI_RESOURCE_DMA] = UACPI_AML_RESOURCE_DMA, + [ACPI_RESOURCE_START_DEPENDENT] = UACPI_AML_RESOURCE_START_DEPENDENT, + [ACPI_RESOURCE_END_DEPENDENT] = UACPI_AML_RESOURCE_END_DEPENDENT, + [ACPI_RESOURCE_IO] = UACPI_AML_RESOURCE_IO, + [ACPI_RESOURCE_FIXED_IO] = UACPI_AML_RESOURCE_FIXED_IO, + [ACPI_RESOURCE_FIXED_DMA] = UACPI_AML_RESOURCE_FIXED_DMA, + [ACPI_RESOURCE_VENDOR_TYPE0] = UACPI_AML_RESOURCE_VENDOR_TYPE0, + [ACPI_RESOURCE_END_TAG] = UACPI_AML_RESOURCE_END_TAG, + + // Large items + [L(ACPI_RESOURCE_MEMORY24)] = UACPI_AML_RESOURCE_MEMORY24, + [L(ACPI_RESOURCE_GENERIC_REGISTER)] = UACPI_AML_RESOURCE_GENERIC_REGISTER, + [L(ACPI_RESOURCE_VENDOR_TYPE1)] = UACPI_AML_RESOURCE_VENDOR_TYPE1, + [L(ACPI_RESOURCE_MEMORY32)] = UACPI_AML_RESOURCE_MEMORY32, + [L(ACPI_RESOURCE_FIXED_MEMORY32)] = UACPI_AML_RESOURCE_FIXED_MEMORY32, + [L(ACPI_RESOURCE_ADDRESS32)] = UACPI_AML_RESOURCE_ADDRESS32, + [L(ACPI_RESOURCE_ADDRESS16)] = UACPI_AML_RESOURCE_ADDRESS16, + [L(ACPI_RESOURCE_EXTENDED_IRQ)] = UACPI_AML_RESOURCE_EXTENDED_IRQ, + [L(ACPI_RESOURCE_ADDRESS64_EXTENDED)] = UACPI_AML_RESOURCE_ADDRESS64_EXTENDED, + [L(ACPI_RESOURCE_ADDRESS64)] = UACPI_AML_RESOURCE_ADDRESS64, + [L(ACPI_RESOURCE_GPIO_CONNECTION)] = UACPI_AML_RESOURCE_GPIO_CONNECTION, + [L(ACPI_RESOURCE_PIN_FUNCTION)] = UACPI_AML_RESOURCE_PIN_FUNCTION, + [L(ACPI_RESOURCE_SERIAL_CONNECTION)] = UACPI_AML_RESOURCE_SERIAL_CONNECTION, + [L(ACPI_RESOURCE_PIN_CONFIGURATION)] = UACPI_AML_RESOURCE_PIN_CONFIGURATION, + [L(ACPI_RESOURCE_PIN_GROUP)] = UACPI_AML_RESOURCE_PIN_GROUP, + [L(ACPI_RESOURCE_PIN_GROUP_FUNCTION)] = UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION, + [L(ACPI_RESOURCE_PIN_GROUP_CONFIGURATION)] = UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION, + [L(ACPI_RESOURCE_CLOCK_INPUT)] = UACPI_AML_RESOURCE_CLOCK_INPUT, +}; + +static const uacpi_u8 type_to_aml_resource[] = { + [UACPI_AML_RESOURCE_IRQ] = ACPI_RESOURCE_IRQ, + [UACPI_AML_RESOURCE_DMA] = ACPI_RESOURCE_DMA, + [UACPI_AML_RESOURCE_START_DEPENDENT] = ACPI_RESOURCE_START_DEPENDENT, + [UACPI_AML_RESOURCE_END_DEPENDENT] = ACPI_RESOURCE_END_DEPENDENT, + [UACPI_AML_RESOURCE_IO] = ACPI_RESOURCE_IO, + [UACPI_AML_RESOURCE_FIXED_IO] = ACPI_RESOURCE_FIXED_IO, + [UACPI_AML_RESOURCE_FIXED_DMA] = ACPI_RESOURCE_FIXED_DMA, + [UACPI_AML_RESOURCE_VENDOR_TYPE0] = ACPI_RESOURCE_VENDOR_TYPE0, + [UACPI_AML_RESOURCE_END_TAG] = ACPI_RESOURCE_END_TAG, + + // Large items + [UACPI_AML_RESOURCE_MEMORY24] = ACPI_RESOURCE_MEMORY24, + [UACPI_AML_RESOURCE_GENERIC_REGISTER] = ACPI_RESOURCE_GENERIC_REGISTER, + [UACPI_AML_RESOURCE_VENDOR_TYPE1] = ACPI_RESOURCE_VENDOR_TYPE1, + [UACPI_AML_RESOURCE_MEMORY32] = ACPI_RESOURCE_MEMORY32, + [UACPI_AML_RESOURCE_FIXED_MEMORY32] = ACPI_RESOURCE_FIXED_MEMORY32, + [UACPI_AML_RESOURCE_ADDRESS32] = ACPI_RESOURCE_ADDRESS32, + [UACPI_AML_RESOURCE_ADDRESS16] = ACPI_RESOURCE_ADDRESS16, + [UACPI_AML_RESOURCE_EXTENDED_IRQ] = ACPI_RESOURCE_EXTENDED_IRQ, + [UACPI_AML_RESOURCE_ADDRESS64_EXTENDED] = ACPI_RESOURCE_ADDRESS64_EXTENDED, + [UACPI_AML_RESOURCE_ADDRESS64] = ACPI_RESOURCE_ADDRESS64, + [UACPI_AML_RESOURCE_GPIO_CONNECTION] = ACPI_RESOURCE_GPIO_CONNECTION, + [UACPI_AML_RESOURCE_PIN_FUNCTION] = ACPI_RESOURCE_PIN_FUNCTION, + [UACPI_AML_RESOURCE_SERIAL_CONNECTION] = ACPI_RESOURCE_SERIAL_CONNECTION, + [UACPI_AML_RESOURCE_PIN_CONFIGURATION] = ACPI_RESOURCE_PIN_CONFIGURATION, + [UACPI_AML_RESOURCE_PIN_GROUP] = ACPI_RESOURCE_PIN_GROUP, + [UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION] = ACPI_RESOURCE_PIN_GROUP_FUNCTION, + [UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION] = ACPI_RESOURCE_PIN_GROUP_CONFIGURATION, + [UACPI_AML_RESOURCE_CLOCK_INPUT] = ACPI_RESOURCE_CLOCK_INPUT, +}; + +static const uacpi_u8 native_resource_to_type[UACPI_RESOURCE_TYPE_MAX + 1] = { + [UACPI_RESOURCE_TYPE_IRQ] = UACPI_AML_RESOURCE_IRQ, + [UACPI_RESOURCE_TYPE_EXTENDED_IRQ] = UACPI_AML_RESOURCE_EXTENDED_IRQ, + [UACPI_RESOURCE_TYPE_DMA] = UACPI_AML_RESOURCE_DMA, + [UACPI_RESOURCE_TYPE_FIXED_DMA] = UACPI_AML_RESOURCE_FIXED_DMA, + [UACPI_RESOURCE_TYPE_IO] = UACPI_AML_RESOURCE_IO, + [UACPI_RESOURCE_TYPE_FIXED_IO] = UACPI_AML_RESOURCE_FIXED_IO, + [UACPI_RESOURCE_TYPE_ADDRESS16] = UACPI_AML_RESOURCE_ADDRESS16, + [UACPI_RESOURCE_TYPE_ADDRESS32] = UACPI_AML_RESOURCE_ADDRESS32, + [UACPI_RESOURCE_TYPE_ADDRESS64] = UACPI_AML_RESOURCE_ADDRESS64, + [UACPI_RESOURCE_TYPE_ADDRESS64_EXTENDED] = UACPI_AML_RESOURCE_ADDRESS64_EXTENDED, + [UACPI_RESOURCE_TYPE_MEMORY24] = UACPI_AML_RESOURCE_MEMORY24, + [UACPI_RESOURCE_TYPE_MEMORY32] = UACPI_AML_RESOURCE_MEMORY32, + [UACPI_RESOURCE_TYPE_FIXED_MEMORY32] = UACPI_AML_RESOURCE_FIXED_MEMORY32, + [UACPI_RESOURCE_TYPE_START_DEPENDENT] = UACPI_AML_RESOURCE_START_DEPENDENT, + [UACPI_RESOURCE_TYPE_END_DEPENDENT] = UACPI_AML_RESOURCE_END_DEPENDENT, + [UACPI_RESOURCE_TYPE_VENDOR_SMALL] = UACPI_AML_RESOURCE_VENDOR_TYPE0, + [UACPI_RESOURCE_TYPE_VENDOR_LARGE] = UACPI_AML_RESOURCE_VENDOR_TYPE1, + [UACPI_RESOURCE_TYPE_GENERIC_REGISTER] = UACPI_AML_RESOURCE_GENERIC_REGISTER, + [UACPI_RESOURCE_TYPE_GPIO_CONNECTION] = UACPI_AML_RESOURCE_GPIO_CONNECTION, + [UACPI_RESOURCE_TYPE_SERIAL_I2C_CONNECTION] = UACPI_AML_RESOURCE_SERIAL_CONNECTION, + [UACPI_RESOURCE_TYPE_SERIAL_SPI_CONNECTION] = UACPI_AML_RESOURCE_SERIAL_CONNECTION, + [UACPI_RESOURCE_TYPE_SERIAL_UART_CONNECTION] = UACPI_AML_RESOURCE_SERIAL_CONNECTION, + [UACPI_RESOURCE_TYPE_SERIAL_CSI2_CONNECTION] = UACPI_AML_RESOURCE_SERIAL_CONNECTION, + [UACPI_RESOURCE_TYPE_PIN_FUNCTION] = UACPI_AML_RESOURCE_PIN_FUNCTION, + [UACPI_RESOURCE_TYPE_PIN_CONFIGURATION] = UACPI_AML_RESOURCE_PIN_CONFIGURATION, + [UACPI_RESOURCE_TYPE_PIN_GROUP] = UACPI_AML_RESOURCE_PIN_GROUP, + [UACPI_RESOURCE_TYPE_PIN_GROUP_FUNCTION] = UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION, + [UACPI_RESOURCE_TYPE_PIN_GROUP_CONFIGURATION] = UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION, + [UACPI_RESOURCE_TYPE_CLOCK_INPUT] = UACPI_AML_RESOURCE_CLOCK_INPUT, + [UACPI_RESOURCE_TYPE_END_TAG] = UACPI_AML_RESOURCE_END_TAG, +}; + +#define SMALL_ITEM_HEADER_SIZE sizeof(struct acpi_small_item) +#define LARGE_ITEM_HEADER_SIZE sizeof(struct acpi_large_item) + +static const uacpi_u8 aml_resource_kind_to_header_size[2] = { + [UACPI_AML_RESOURCE_KIND_SMALL] = SMALL_ITEM_HEADER_SIZE, + [UACPI_AML_RESOURCE_KIND_LARGE] = LARGE_ITEM_HEADER_SIZE, +}; + +static uacpi_size aml_size_with_header(const struct uacpi_resource_spec *spec) +{ + return spec->aml_size + + aml_resource_kind_to_header_size[spec->resource_kind]; +} + +static uacpi_size extra_size_for_native_irq_or_dma( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + UACPI_UNUSED(size); + uacpi_u16 mask; + + if (spec->type == UACPI_AML_RESOURCE_IRQ) { + struct acpi_resource_irq *irq = data; + mask = irq->irq_mask; + } else { + struct acpi_resource_dma *dma = data; + mask = dma->channel_mask; + } + + return uacpi_popcount(mask); +} + +static uacpi_size size_for_aml_irq( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_resource_irq *irq = &resource->irq; + uacpi_size size; + + size = aml_size_with_header(spec); + + switch (irq->length_kind) { + case UACPI_RESOURCE_LENGTH_KIND_FULL: + goto out_full; + case UACPI_RESOURCE_LENGTH_KIND_ONE_LESS: + case UACPI_RESOURCE_LENGTH_KIND_DONT_CARE: + if (irq->triggering != UACPI_TRIGGERING_EDGE) + goto out_full; + if (irq->polarity != UACPI_POLARITY_ACTIVE_HIGH) + goto out_full; + if (irq->sharing != UACPI_EXCLUSIVE) + goto out_full; + + return size - 1; + } + +out_full: + if (uacpi_unlikely(irq->length_kind == + UACPI_RESOURCE_LENGTH_KIND_ONE_LESS)) { + uacpi_warn("requested IRQ resource length is " + "not compatible with specified flags, corrected\n"); + } + + return size; +} + +static uacpi_size size_for_aml_start_dependent( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_resource_start_dependent *start_dep = &resource->start_dependent; + uacpi_size size; + + size = aml_size_with_header(spec); + switch (start_dep->length_kind) { + case UACPI_RESOURCE_LENGTH_KIND_FULL: + goto out_full; + case UACPI_RESOURCE_LENGTH_KIND_ONE_LESS: + case UACPI_RESOURCE_LENGTH_KIND_DONT_CARE: + if (start_dep->compatibility != UACPI_ACCEPTABLE) + goto out_full; + if (start_dep->performance != UACPI_ACCEPTABLE) + goto out_full; + + return size - 1; + } + +out_full: + if (uacpi_unlikely(start_dep->length_kind == + UACPI_RESOURCE_LENGTH_KIND_ONE_LESS)) { + uacpi_warn("requested StartDependentFn resource length is " + "not compatible with specified flags, corrected\n"); + } + + return size; +} + +static uacpi_size extra_size_for_native_vendor( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + UACPI_UNUSED(spec); + UACPI_UNUSED(data); + return size; +} + +static uacpi_size size_for_aml_vendor( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + UACPI_UNUSED(spec); + uacpi_size size = resource->vendor.length; + + if (size > 7 || resource->type == UACPI_RESOURCE_TYPE_VENDOR_LARGE) { + size += aml_resource_kind_to_header_size[ + UACPI_AML_RESOURCE_KIND_LARGE + ]; + + if (uacpi_unlikely(resource->type != UACPI_RESOURCE_TYPE_VENDOR_LARGE)) { + uacpi_warn("vendor data too large for small descriptor (%zu), " + "correcting to large\n", size); + resource->type = UACPI_RESOURCE_TYPE_VENDOR_LARGE; + } + } else { + size += aml_resource_kind_to_header_size[ + UACPI_AML_RESOURCE_KIND_SMALL + ]; + } + + return size; +} + +static uacpi_size extra_size_for_resource_source( + uacpi_size base_size, uacpi_size reported_size +) +{ + uacpi_size string_length; + + if (reported_size <= base_size) + return 0; + + /* + * The remainder of the descriptor minus the resource index field + */ + string_length = (reported_size - base_size) - 1; + return UACPI_ALIGN_UP(string_length, sizeof(void*), uacpi_size); +} + +static uacpi_size size_for_aml_resource_source( + uacpi_resource_source *source, uacpi_bool with_index +) +{ + uacpi_size length = source->length; + + if (uacpi_unlikely(length && !source->index_present)) { + uacpi_warn("resource declares no source index with non-empty " + "string (%zu bytes), corrected\n", length); + source->index_present = UACPI_TRUE; + } + + // If index is included in the dynamic resource source, add it to the length + if (with_index) + length += source->index_present; + + return length; +} + +static uacpi_size extra_size_for_native_address_or_clock_input( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + UACPI_UNUSED(data); + return extra_size_for_resource_source(spec->aml_size, size); +} + +static uacpi_size size_for_aml_address_or_clock_input( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_resource_source *source; + bool has_index = UACPI_TRUE; + + switch (resource->type) { + case UACPI_RESOURCE_TYPE_ADDRESS16: + source = &resource->address16.source; + break; + case UACPI_RESOURCE_TYPE_ADDRESS32: + source = &resource->address32.source; + break; + case UACPI_RESOURCE_TYPE_ADDRESS64: + source = &resource->address64.source; + break; + case UACPI_RESOURCE_TYPE_CLOCK_INPUT: + source = &resource->clock_input.source; + has_index = UACPI_FALSE; + break; + default: + return 0; + } + + return aml_size_with_header(spec) + + size_for_aml_resource_source(source, has_index); +} + +static uacpi_size extra_size_for_extended_irq( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + struct acpi_resource_extended_irq *irq = data; + uacpi_size extra_size = 0; + + extra_size += irq->num_irqs * sizeof(uacpi_u32); + extra_size += extra_size_for_resource_source( + spec->aml_size, size - extra_size + ); + + return extra_size; +} + +static uacpi_size size_for_aml_extended_irq( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_resource_extended_irq *irq = &resource->extended_irq; + uacpi_size size; + + size = aml_size_with_header(spec); + size += irq->num_irqs * 4; + size += size_for_aml_resource_source(&irq->source, UACPI_TRUE); + + return size; +} + +static uacpi_size extra_size_for_native_gpio_or_pins( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + uacpi_size pin_table_offset; + + /* + * These resources pretend to have variable layout by declaring "offset" + * fields, but the layout is hardcoded and mandated by the spec to be + * very specific. We can use the offset numbers here to calculate the final + * length. + * + * For example, the layout of GPIO connection _always_ looks as follows: + * [0...22] -> fixed data + * [23...] -> pin table + * [...] -> source name + * [...] -> vendor data + */ + switch (spec->type) { + case UACPI_AML_RESOURCE_GPIO_CONNECTION: { + struct acpi_resource_gpio_connection *gpio = data; + pin_table_offset = gpio->pin_table_offset; + break; + } + + case UACPI_AML_RESOURCE_PIN_FUNCTION: { + struct acpi_resource_pin_function *pin = data; + pin_table_offset = pin->pin_table_offset; + break; + } + + case UACPI_AML_RESOURCE_PIN_CONFIGURATION: { + struct acpi_resource_pin_configuration *config = data; + pin_table_offset = config->pin_table_offset; + break; + } + + case UACPI_AML_RESOURCE_PIN_GROUP: { + struct acpi_resource_pin_group *group = data; + pin_table_offset = group->pin_table_offset; + break; + } + + default: + return 0; + } + + /* + * The size we get passed here does not include the header size because + * that's how resources are encoded. Subtract it here so that we get the + * correct final length. + */ + return size - (pin_table_offset - LARGE_ITEM_HEADER_SIZE); +} + +static uacpi_size size_for_aml_gpio_or_pins( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_size source_length, vendor_length, pin_table_length, size; + + size = aml_size_with_header(spec); + switch (spec->type) { + case UACPI_AML_RESOURCE_GPIO_CONNECTION: { + uacpi_resource_gpio_connection *res = &resource->gpio_connection; + source_length = res->source.length; + pin_table_length = res->pin_table_length; + vendor_length = res->vendor_data_length; + break; + } + + case UACPI_AML_RESOURCE_PIN_FUNCTION: { + uacpi_resource_pin_function *res = &resource->pin_function; + source_length = res->source.length; + pin_table_length = res->pin_table_length; + vendor_length = res->vendor_data_length; + break; + } + + case UACPI_AML_RESOURCE_PIN_CONFIGURATION: { + uacpi_resource_pin_configuration *res = &resource->pin_configuration; + source_length = res->source.length; + pin_table_length = res->pin_table_length; + vendor_length = res->vendor_data_length; + break; + } + + case UACPI_AML_RESOURCE_PIN_GROUP: { + uacpi_resource_pin_group *res = &resource->pin_group; + source_length = res->label.length; + pin_table_length = res->pin_table_length; + vendor_length = res->vendor_data_length; + break; + } + + default: + return 0; + } + + size += source_length; + size += pin_table_length * 2; + size += vendor_length; + + return size; +} + +static uacpi_size extra_size_for_native_pin_group( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + uacpi_size source_offset; + + switch (spec->type) { + case UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION: { + struct acpi_resource_pin_group_function *func = data; + source_offset = func->source_offset; + break; + } + + case UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION: { + struct acpi_resource_pin_group_configuration *config = data; + source_offset = config->source_offset; + break; + } + + default: + return 0; + } + + // Same logic as extra_size_for_native_gpio_or_pins + return size - (source_offset - LARGE_ITEM_HEADER_SIZE); +} + +static uacpi_size size_for_aml_pin_group( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_size source_length, label_length, vendor_length, size; + + size = aml_size_with_header(spec); + switch (spec->type) { + case UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION: { + uacpi_resource_pin_group_function *res = &resource->pin_group_function; + source_length = res->source.length; + label_length = res->label.length; + vendor_length = res->vendor_data_length; + break; + } + + case UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION: { + uacpi_resource_pin_group_configuration *res; + res = &resource->pin_group_configuration; + source_length = res->source.length; + label_length = res->label.length; + vendor_length = res->vendor_data_length; + break; + } + + default: + return 0; + } + + size += source_length; + size += label_length; + size += vendor_length; + + return size; +} + +#define AML_SERIAL_RESOURCE_EXTRA_SIZE(type) \ + (sizeof(struct acpi_resource_serial_##type) \ + - sizeof(struct acpi_resource_serial)) + +#define NATIVE_SERIAL_RESOURCE_EXTRA_SIZE(type) \ + (sizeof(uacpi_resource_##type##_connection) \ + - sizeof(uacpi_resource_serial_bus_common)) + +static const uacpi_u8 aml_serial_resource_to_extra_aml_size +[ACPI_SERIAL_TYPE_MAX + 1] = { + [ACPI_SERIAL_TYPE_I2C] = AML_SERIAL_RESOURCE_EXTRA_SIZE(i2c), + [ACPI_SERIAL_TYPE_SPI] = AML_SERIAL_RESOURCE_EXTRA_SIZE(spi), + [ACPI_SERIAL_TYPE_UART] = AML_SERIAL_RESOURCE_EXTRA_SIZE(uart), + [ACPI_SERIAL_TYPE_CSI2] = AML_SERIAL_RESOURCE_EXTRA_SIZE(csi2), +}; + +static const uacpi_u8 aml_serial_resource_to_extra_native_size +[ACPI_SERIAL_TYPE_MAX + 1] = { + [ACPI_SERIAL_TYPE_I2C] = NATIVE_SERIAL_RESOURCE_EXTRA_SIZE(i2c), + [ACPI_SERIAL_TYPE_SPI] = NATIVE_SERIAL_RESOURCE_EXTRA_SIZE(spi), + [ACPI_SERIAL_TYPE_UART] = NATIVE_SERIAL_RESOURCE_EXTRA_SIZE(uart), + [ACPI_SERIAL_TYPE_CSI2] = NATIVE_SERIAL_RESOURCE_EXTRA_SIZE(csi2), +}; + +static uacpi_size extra_size_for_serial_connection( + const struct uacpi_resource_spec *spec, void *data, uacpi_size size +) +{ + struct acpi_resource_serial *serial = data; + uacpi_size extra_bytes = size; + + extra_bytes -= spec->aml_size; + extra_bytes -= aml_serial_resource_to_extra_aml_size[serial->type]; + extra_bytes += aml_serial_resource_to_extra_native_size[serial->type]; + + return extra_bytes; +} + +static uacpi_size aml_size_for_serial_connection( + const struct uacpi_resource_spec *spec, uacpi_resource *resource +) +{ + uacpi_size size; + uacpi_resource_serial_bus_common *serial_bus = &resource->serial_bus_common; + + size = aml_size_with_header(spec); + size += aml_serial_resource_to_extra_aml_size[serial_bus->type]; + size += serial_bus->vendor_data_length; + size += serial_bus->source.length; + + return size; +} + +#define OP(short_code, ...) \ +{ \ + .code = UACPI_RESOURCE_CONVERT_OPCODE_##short_code, \ + __VA_ARGS__ \ +} + +#define END() OP(END) + +#define AML_O(short_aml_name, field) \ + uacpi_offsetof(struct acpi_resource_##short_aml_name, field) + +#define AML_F(short_aml_name, field) \ + .aml_offset = AML_O(short_aml_name, field) + +#define NATIVE_O(short_name, field) \ + uacpi_offsetof(uacpi_resource_##short_name, field) + +#define NATIVE_F(short_native_name, field) \ + .native_offset = NATIVE_O(short_native_name, field) + +#define IMM(value) .imm = value +#define ARG0(value) .arg0 = (value) +#define ARG1(value) .arg1 = (value) +#define ARG2(value) .arg2 = (value) + + +static const struct uacpi_resource_convert_instruction convert_irq_to_native[] = { + OP(PACKED_ARRAY_16, AML_F(irq, irq_mask), NATIVE_F(irq, irqs), + ARG2(NATIVE_O(irq, num_irqs))), + OP(SKIP_IF_AML_SIZE_LESS_THAN, ARG0(3), IMM(6)), + OP(SET_TO_IMM, NATIVE_F(irq, length_kind), + IMM(UACPI_RESOURCE_LENGTH_KIND_FULL)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, triggering), IMM(0)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, polarity), IMM(3)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, sharing), IMM(4)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, wake_capability), IMM(5)), + END(), + OP(SET_TO_IMM, NATIVE_F(irq, length_kind), + IMM(UACPI_RESOURCE_LENGTH_KIND_ONE_LESS)), + OP(SET_TO_IMM, NATIVE_F(irq, triggering), IMM(UACPI_TRIGGERING_EDGE)), + END(), +}; + +const struct uacpi_resource_convert_instruction convert_irq_to_aml[] = { + OP(PACKED_ARRAY_16, AML_F(irq, irq_mask), NATIVE_F(irq, irqs), + ARG2(NATIVE_O(irq, num_irqs))), + OP(SKIP_IF_AML_SIZE_LESS_THAN, ARG0(3), IMM(4)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, triggering), IMM(0)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, polarity), IMM(3)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, sharing), IMM(4)), + OP(BIT_FIELD_1, AML_F(irq, flags), NATIVE_F(irq, wake_capability), IMM(5)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_dma[] = { + OP(PACKED_ARRAY_8, AML_F(dma, channel_mask), NATIVE_F(dma, channels), + ARG2(NATIVE_O(dma, num_channels))), + OP(BIT_FIELD_2, AML_F(dma, flags), NATIVE_F(dma, transfer_type), IMM(0)), + OP(BIT_FIELD_1, AML_F(dma, flags), NATIVE_F(dma, bus_master_status), IMM(2)), + OP(BIT_FIELD_2, AML_F(dma, flags), NATIVE_F(dma, channel_speed), IMM(5)), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_start_dependent_to_native[] = { + OP(SKIP_IF_AML_SIZE_LESS_THAN, ARG0(1), IMM(4)), + OP(SET_TO_IMM, NATIVE_F(start_dependent, length_kind), + IMM(UACPI_RESOURCE_LENGTH_KIND_FULL)), + OP(BIT_FIELD_2, AML_F(start_dependent, flags), + NATIVE_F(start_dependent, compatibility), IMM(0)), + OP(BIT_FIELD_2, AML_F(start_dependent, flags), + NATIVE_F(start_dependent, performance), IMM(2)), + END(), + OP(SET_TO_IMM, NATIVE_F(start_dependent, length_kind), + IMM(UACPI_RESOURCE_LENGTH_KIND_ONE_LESS)), + OP(SET_TO_IMM, NATIVE_F(start_dependent, compatibility), + IMM(UACPI_ACCEPTABLE)), + OP(SET_TO_IMM, NATIVE_F(start_dependent, performance), + IMM(UACPI_ACCEPTABLE)), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_start_dependent_to_aml[] = { + OP(SKIP_IF_AML_SIZE_LESS_THAN, ARG0(1), IMM(1)), + OP(BIT_FIELD_2, AML_F(start_dependent, flags), + NATIVE_F(start_dependent, compatibility), IMM(0)), + OP(BIT_FIELD_2, AML_F(start_dependent, flags), + NATIVE_F(start_dependent, performance), IMM(2)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_io[] = { + OP(BIT_FIELD_1, AML_F(io, information), NATIVE_F(io, decode_type)), + OP(FIELD_16, AML_F(io, minimum), NATIVE_F(io, minimum)), + OP(FIELD_16, AML_F(io, maximum), NATIVE_F(io, maximum)), + OP(FIELD_8, AML_F(io, alignment), NATIVE_F(io, alignment)), + OP(FIELD_8, AML_F(io, length), NATIVE_F(io, length)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_fixed_io[] = { + OP(FIELD_16, AML_F(fixed_io, address), NATIVE_F(fixed_io, address)), + OP(FIELD_8, AML_F(fixed_io, length), NATIVE_F(fixed_io, length)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_fixed_dma[] = { + OP(FIELD_16, AML_F(fixed_dma, request_line), + NATIVE_F(fixed_dma, request_line)), + OP(FIELD_16, AML_F(fixed_dma, channel), NATIVE_F(fixed_dma, channel)), + OP(FIELD_8, AML_F(fixed_dma, transfer_width), + NATIVE_F(fixed_dma, transfer_width)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_vendor_type0[] = { + OP(LOAD_AML_SIZE_32, NATIVE_F(vendor, length)), + OP(FIELD_8, AML_F(vendor_defined_type0, byte_data), NATIVE_F(vendor, data)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_vendor_type1[] = { + OP(LOAD_AML_SIZE_32, NATIVE_F(vendor, length)), + OP(FIELD_8, AML_F(vendor_defined_type1, byte_data), NATIVE_F(vendor, data)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_memory24[] = { + OP(BIT_FIELD_1, AML_F(memory24, information), + NATIVE_F(memory24, write_status), IMM(0)), + OP(FIELD_16, AML_F(memory24, minimum), NATIVE_F(memory24, minimum), IMM(4)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_memory32[] = { + OP(BIT_FIELD_1, AML_F(memory32, information), + NATIVE_F(memory32, write_status), IMM(0)), + OP(FIELD_32, AML_F(memory32, minimum), NATIVE_F(memory32, minimum), IMM(4)), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_fixed_memory32[] = { + OP(BIT_FIELD_1, AML_F(fixed_memory32, information), + NATIVE_F(fixed_memory32, write_status), IMM(0)), + OP(FIELD_32, AML_F(fixed_memory32, address), + NATIVE_F(fixed_memory32, address)), + OP(FIELD_32, AML_F(fixed_memory32, length), + NATIVE_F(fixed_memory32, length)), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_generic_register[] = { + OP(FIELD_8, AML_F(generic_register, address_space_id), + NATIVE_F(generic_register, address_space_id), IMM(4)), + OP(FIELD_64, AML_F(generic_register, address), + NATIVE_F(generic_register, address)), + END(), +}; + +#define CONVERT_TYPE_SPECIFIC_FLAGS(addr_type) \ + OP(LOAD_8_STORE, AML_F(addr_type, common.type), \ + NATIVE_F(addr_type, common.type)), \ + OP(SKIP_IF_NOT_EQUALS, ARG0(UACPI_RANGE_MEMORY), IMM(5)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.memory.write_status), IMM(0)), \ + OP(BIT_FIELD_2, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.memory.caching), IMM(1)), \ + OP(BIT_FIELD_2, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.memory.range_type), IMM(3)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.memory.translation), IMM(5)), \ + END(), \ + OP(SKIP_IF_NOT_EQUALS, ARG0(UACPI_RANGE_IO), IMM(4)), \ + OP(BIT_FIELD_2, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.io.range_type), IMM(0)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.io.translation_type), IMM(4)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.io.translation), IMM(5)), \ + END(), \ + /* Memory type that we don't know, just copy the byte */ \ + OP(FIELD_8, AML_F(addr_type, common.type_flags), \ + NATIVE_F(addr_type, common.attribute.type_specific), IMM(0xFF)), \ + END() + +#define CONVERT_GENERAL_ADDRESS_FLAGS(addr_type) \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.flags), \ + NATIVE_F(addr_type, common.direction), IMM(0)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.flags), \ + NATIVE_F(addr_type, common.decode_type), IMM(1)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.flags), \ + NATIVE_F(addr_type, common.fixed_min_address), IMM(2)), \ + OP(BIT_FIELD_1, \ + AML_F(addr_type, common.flags), \ + NATIVE_F(addr_type, common.fixed_max_address), IMM(3)) \ + +#define DEFINE_ADDRESS_CONVERSION(width) \ + static const struct uacpi_resource_convert_instruction \ + convert_address##width[] = { \ + CONVERT_GENERAL_ADDRESS_FLAGS(address##width), \ + OP(FIELD_##width, AML_F(address##width, granularity), \ + NATIVE_F(address##width, granularity), IMM(5)), \ + OP(RESOURCE_SOURCE, NATIVE_F(address##width, source)), \ + CONVERT_TYPE_SPECIFIC_FLAGS(address##width), \ + }; + +DEFINE_ADDRESS_CONVERSION(16) +DEFINE_ADDRESS_CONVERSION(32) +DEFINE_ADDRESS_CONVERSION(64) + +static const struct uacpi_resource_convert_instruction +convert_address64_extended[] = { + CONVERT_GENERAL_ADDRESS_FLAGS(address64_extended), + OP(FIELD_8, AML_F(address64_extended, revision_id), + NATIVE_F(address64_extended, revision_id)), + OP(FIELD_64, AML_F(address64_extended, granularity), + NATIVE_F(address64_extended, granularity), IMM(6)), + CONVERT_TYPE_SPECIFIC_FLAGS(address64_extended), +}; + +static const struct uacpi_resource_convert_instruction +convert_extended_irq[] = { + OP(BIT_FIELD_1, AML_F(extended_irq, flags), + NATIVE_F(extended_irq, direction), IMM(0)), + OP(BIT_FIELD_1, AML_F(extended_irq, flags), + NATIVE_F(extended_irq, triggering), IMM(1)), + OP(BIT_FIELD_1, AML_F(extended_irq, flags), + NATIVE_F(extended_irq, polarity), IMM(2)), + OP(BIT_FIELD_1, AML_F(extended_irq, flags), + NATIVE_F(extended_irq, sharing), IMM(3)), + OP(BIT_FIELD_1, AML_F(extended_irq, flags), + NATIVE_F(extended_irq, wake_capability), IMM(4)), + OP(LOAD_8_STORE, AML_F(extended_irq, num_irqs), + NATIVE_F(extended_irq, num_irqs), IMM(4)), + OP(RESOURCE_SOURCE, NATIVE_F(extended_irq, source)), + + // Use FIELD_8 here since the accumulator has been multiplied by 4 + OP(FIELD_8, AML_F(extended_irq, irqs), NATIVE_F(extended_irq, irqs)), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_clock_input[] = { + OP(FIELD_8, AML_F(clock_input, revision_id), + NATIVE_F(clock_input, revision_id)), + OP(BIT_FIELD_1, AML_F(clock_input, flags), NATIVE_F(clock_input, frequency), + IMM(0)), + OP(BIT_FIELD_2, AML_F(clock_input, flags), NATIVE_F(clock_input, scale), + IMM(1)), + OP(FIELD_16, AML_F(clock_input, divisor), NATIVE_F(clock_input, divisor)), + OP(FIELD_32, AML_F(clock_input, numerator), NATIVE_F(clock_input, numerator)), + OP(FIELD_8, AML_F(clock_input, source_index), NATIVE_F(clock_input, source.index)), + OP(RESOURCE_SOURCE_NO_INDEX, NATIVE_F(clock_input, source)), + END(), +}; + +#define DECODE_SOURCE_INDEX(short_aml_name) \ + OP(FIELD_8, AML_F(short_aml_name, source_index), \ + NATIVE_F(short_aml_name, source.index)) \ + +#define DECODE_RES_PIN_TBL_AND_VENDOR_DATA( \ + short_aml_name, res_opcode, offset_field, res_field \ +) \ + OP(LOAD_PIN_TABLE_LENGTH, AML_F(short_aml_name, offset_field), \ + NATIVE_F(short_aml_name, pin_table_length)), \ + OP(RESOURCE_##res_opcode, NATIVE_F(short_aml_name, res_field), \ + AML_F(short_aml_name, offset_field), \ + ARG2(AML_O(short_aml_name, vendor_data_offset))), \ + OP(PIN_TABLE, AML_F(short_aml_name, pin_table_offset), \ + NATIVE_F(short_aml_name, pin_table_length), \ + ARG2(NATIVE_O(short_aml_name, pin_table))), \ + OP(VENDOR_DATA, AML_F(short_aml_name, vendor_data_offset), \ + NATIVE_F(short_aml_name, vendor_data_length), \ + ARG2(NATIVE_O(short_aml_name, vendor_data))) + +static const struct uacpi_resource_convert_instruction +convert_gpio_connection[] = { + OP(FIELD_8, AML_F(gpio_connection, revision_id), + NATIVE_F(gpio_connection, revision_id)), + OP(BIT_FIELD_1, AML_F(gpio_connection, general_flags), + NATIVE_F(gpio_connection, direction)), + OP(FIELD_8, AML_F(gpio_connection, pull_configuration), + NATIVE_F(gpio_connection, pull_configuration)), + OP(FIELD_16, AML_F(gpio_connection, drive_strength), + NATIVE_F(gpio_connection, drive_strength), IMM(2)), + DECODE_SOURCE_INDEX(gpio_connection), + DECODE_RES_PIN_TBL_AND_VENDOR_DATA( + gpio_connection, SOURCE_NO_INDEX, source_offset, source + ), + OP(LOAD_8_STORE, AML_F(gpio_connection, type), NATIVE_F(gpio_connection, type)), + OP(SKIP_IF_NOT_EQUALS, ARG0(UACPI_GPIO_CONNECTION_INTERRUPT), IMM(5)), + OP(BIT_FIELD_1, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, interrupt.triggering), IMM(0)), + OP(BIT_FIELD_2, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, interrupt.polarity), IMM(1)), + OP(BIT_FIELD_1, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, interrupt.sharing), IMM(3)), + OP(BIT_FIELD_1, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, interrupt.wake_capability), IMM(4)), + END(), + OP(SKIP_IF_NOT_EQUALS, ARG0(UACPI_GPIO_CONNECTION_IO), IMM(3)), + OP(BIT_FIELD_2, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, io.restriction), IMM(0)), + OP(BIT_FIELD_1, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, io.sharing), IMM(3)), + END(), + OP(FIELD_16, AML_F(gpio_connection, connection_flags), + NATIVE_F(gpio_connection, type_specific), IMM(0xFF)), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_pin_function[] = { + OP(FIELD_8, AML_F(pin_function, revision_id), + NATIVE_F(pin_function, revision_id)), + OP(BIT_FIELD_1, AML_F(pin_function, flags), + NATIVE_F(pin_function, sharing), IMM(0)), + OP(FIELD_8, AML_F(pin_function, pull_configuration), + NATIVE_F(pin_function, pull_configuration)), + OP(FIELD_16, AML_F(pin_function, function_number), + NATIVE_F(pin_function, function_number)), + DECODE_SOURCE_INDEX(pin_function), + DECODE_RES_PIN_TBL_AND_VENDOR_DATA( + pin_function, SOURCE_NO_INDEX, source_offset, source + ), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_pin_configuration[] = { + OP(FIELD_8, AML_F(pin_configuration, revision_id), + NATIVE_F(pin_configuration, revision_id)), + OP(BIT_FIELD_1, AML_F(pin_configuration, flags), + NATIVE_F(pin_configuration, sharing), IMM(0)), + OP(BIT_FIELD_1, AML_F(pin_configuration, flags), + NATIVE_F(pin_configuration, direction), IMM(1)), + OP(FIELD_8, AML_F(pin_configuration, type), + NATIVE_F(pin_configuration, type)), + OP(FIELD_32, AML_F(pin_configuration, value), + NATIVE_F(pin_configuration, value)), + DECODE_SOURCE_INDEX(pin_configuration), + DECODE_RES_PIN_TBL_AND_VENDOR_DATA( + pin_configuration, SOURCE_NO_INDEX, source_offset, source + ), + END(), +}; + +static const struct uacpi_resource_convert_instruction convert_pin_group[] = { + OP(FIELD_8, AML_F(pin_group, revision_id), + NATIVE_F(pin_group, revision_id)), + OP(BIT_FIELD_1, AML_F(pin_group, flags), + NATIVE_F(pin_group, direction), IMM(0)), + DECODE_RES_PIN_TBL_AND_VENDOR_DATA( + pin_group, LABEL, source_lable_offset, label + ), + END(), +}; + +#define DECODE_PIN_GROUP_RES_SOURCES(postfix) \ + DECODE_SOURCE_INDEX(pin_group_##postfix), \ + OP(RESOURCE_SOURCE_NO_INDEX, NATIVE_F(pin_group_##postfix, source), \ + AML_F(pin_group_##postfix, source_offset), \ + ARG2(AML_O(pin_group_##postfix, source_lable_offset))), \ + OP(LOAD_16_NATIVE, NATIVE_F(pin_group_##postfix, source.length)), \ + OP(RESOURCE_LABEL, NATIVE_F(pin_group_##postfix, label), \ + AML_F(pin_group_##postfix, source_lable_offset), \ + ARG2(AML_O(pin_group_##postfix, vendor_data_offset))), \ + OP(VENDOR_DATA, AML_F(pin_group_##postfix, vendor_data_offset), \ + NATIVE_F(pin_group_##postfix, vendor_data_length), \ + ARG2(NATIVE_O(pin_group_##postfix, vendor_data))) + +static const struct uacpi_resource_convert_instruction +convert_pin_group_function[] = { + OP(FIELD_8, AML_F(pin_group_function, revision_id), + NATIVE_F(pin_group_function, revision_id)), + OP(BIT_FIELD_1, AML_F(pin_group_function, flags), + NATIVE_F(pin_group_function, sharing), IMM(0)), + OP(BIT_FIELD_1, AML_F(pin_group_function, flags), + NATIVE_F(pin_group_function, direction), IMM(1)), + OP(FIELD_16, AML_F(pin_group_function, function), + NATIVE_F(pin_group_function, function)), + DECODE_PIN_GROUP_RES_SOURCES(function), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_pin_group_configuration[] = { + OP(FIELD_8, AML_F(pin_group_configuration, revision_id), + NATIVE_F(pin_group_configuration, revision_id)), + OP(BIT_FIELD_1, AML_F(pin_group_configuration, flags), + NATIVE_F(pin_group_configuration, sharing), IMM(0)), + OP(BIT_FIELD_1, AML_F(pin_group_configuration, flags), + NATIVE_F(pin_group_configuration, direction), IMM(1)), + OP(FIELD_8, AML_F(pin_group_configuration, type), + NATIVE_F(pin_group_configuration, type)), + OP(FIELD_32, AML_F(pin_group_configuration, value), + NATIVE_F(pin_group_configuration, value)), + DECODE_PIN_GROUP_RES_SOURCES(configuration), + END(), +}; + +static const struct uacpi_resource_convert_instruction +convert_generic_serial_bus[] = { + OP(FIELD_8, AML_F(serial, revision_id), + NATIVE_F(serial_bus_common, revision_id)), + OP(FIELD_8, AML_F(serial, type_specific_revision_id), + NATIVE_F(serial_bus_common, type_revision_id)), + OP(FIELD_8, AML_F(serial, source_index), + NATIVE_F(serial_bus_common, source.index)), + OP(FIELD_16, AML_F(serial, type_data_length), + NATIVE_F(serial_bus_common, type_data_length)), + OP(BIT_FIELD_1, AML_F(serial, flags), + NATIVE_F(serial_bus_common, mode), IMM(0)), + OP(BIT_FIELD_1, AML_F(serial, flags), + NATIVE_F(serial_bus_common, direction), IMM(1)), + OP(BIT_FIELD_1, AML_F(serial, flags), + NATIVE_F(serial_bus_common, sharing), IMM(2)), + OP(SERIAL_TYPE_SPECIFIC, AML_F(serial, type), + NATIVE_F(serial_bus_common, type)), + OP(RESOURCE_SOURCE_NO_INDEX, NATIVE_F(serial_bus_common, source)), + OP(LOAD_8_NATIVE, NATIVE_F(serial_bus_common, type)), + OP(SKIP_IF_NOT_EQUALS, ARG0(ACPI_SERIAL_TYPE_I2C), IMM(4)), + OP(BIT_FIELD_1, AML_F(serial, type_specific_flags), + NATIVE_F(i2c_connection, addressing_mode), IMM(0)), + OP(FIELD_32, AML_F(serial_i2c, connection_speed), + NATIVE_F(i2c_connection, connection_speed), IMM(0xFF)), + OP(FIELD_16, AML_F(serial_i2c, slave_address), + NATIVE_F(i2c_connection, slave_address)), + END(), + OP(SKIP_IF_NOT_EQUALS, ARG0(ACPI_SERIAL_TYPE_SPI), IMM(5)), + OP(BIT_FIELD_1, AML_F(serial, type_specific_flags), + NATIVE_F(spi_connection, wire_mode), IMM(0)), + OP(BIT_FIELD_1, AML_F(serial, type_specific_flags), + NATIVE_F(spi_connection, device_polarity), IMM(1)), + OP(FIELD_32, AML_F(serial_spi, connection_speed), + NATIVE_F(spi_connection, connection_speed), IMM(0xFF)), + OP(FIELD_8, AML_F(serial_spi, data_bit_length), + NATIVE_F(spi_connection, data_bit_length), IMM(5)), + END(), + OP(SKIP_IF_NOT_EQUALS, ARG0(ACPI_SERIAL_TYPE_UART), IMM(8)), + OP(BIT_FIELD_2, AML_F(serial, type_specific_flags), + NATIVE_F(uart_connection, flow_control), IMM(0)), + OP(BIT_FIELD_2, AML_F(serial, type_specific_flags), + NATIVE_F(uart_connection, stop_bits), IMM(2)), + OP(BIT_FIELD_3, AML_F(serial, type_specific_flags), + NATIVE_F(uart_connection, data_bits), IMM(4)), + OP(BIT_FIELD_1, AML_F(serial, type_specific_flags), + NATIVE_F(uart_connection, endianness), IMM(7)), + OP(FIELD_32, AML_F(serial_uart, baud_rate), + NATIVE_F(uart_connection, baud_rate), IMM(0xFF)), + OP(FIELD_16, AML_F(serial_uart, rx_fifo), + NATIVE_F(uart_connection, rx_fifo), IMM(2)), + OP(FIELD_8, AML_F(serial_uart, parity), + NATIVE_F(uart_connection, parity), IMM(2)), + END(), + OP(SKIP_IF_NOT_EQUALS, ARG0(ACPI_SERIAL_TYPE_CSI2), IMM(3)), + OP(BIT_FIELD_2, AML_F(serial, type_specific_flags), + NATIVE_F(csi2_connection, phy_type), IMM(0)), + OP(BIT_FIELD_6, AML_F(serial, type_specific_flags), + NATIVE_F(csi2_connection, local_port), IMM(2)), + END(), + + /* + * Insert a trap to catch unimplemented types, this should be unreachable + * because of validation earlier. + */ + OP(UNREACHABLE), +}; + +#define NATIVE_RESOURCE_HEADER_SIZE 8 + +#define DEFINE_SMALL_AML_RESOURCE(aml_type_enum, native_type_enum, \ + aml_struct, native_struct, ...) \ + [aml_type_enum] = { \ + .type = aml_type_enum, \ + .native_type = native_type_enum, \ + .resource_kind = UACPI_AML_RESOURCE_KIND_SMALL, \ + .aml_size = sizeof(aml_struct) - SMALL_ITEM_HEADER_SIZE, \ + .native_size = sizeof(native_struct) + NATIVE_RESOURCE_HEADER_SIZE, \ + __VA_ARGS__ \ + } + +#define DEFINE_SMALL_AML_RESOURCE_NO_NATIVE_REPR( \ + aml_type_enum, native_type_enum, aml_struct, ... \ +) \ + [aml_type_enum] = { \ + .type = aml_type_enum, \ + .native_type = native_type_enum, \ + .resource_kind = UACPI_AML_RESOURCE_KIND_SMALL, \ + .aml_size = sizeof(aml_struct) - SMALL_ITEM_HEADER_SIZE, \ + .native_size = NATIVE_RESOURCE_HEADER_SIZE, \ + __VA_ARGS__ \ + } + +#define DEFINE_LARGE_AML_RESOURCE(aml_type_enum, native_type_enum, \ + aml_struct, native_struct, ...) \ + [aml_type_enum] = { \ + .type = aml_type_enum, \ + .native_type = native_type_enum, \ + .resource_kind = UACPI_AML_RESOURCE_KIND_LARGE, \ + .aml_size = sizeof(aml_struct) - LARGE_ITEM_HEADER_SIZE, \ + .native_size = sizeof(native_struct) + NATIVE_RESOURCE_HEADER_SIZE, \ + __VA_ARGS__ \ + } + +const struct uacpi_resource_spec aml_resources[UACPI_AML_RESOURCE_MAX + 1] = { + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_IRQ, + UACPI_RESOURCE_TYPE_IRQ, + struct acpi_resource_irq, + uacpi_resource_irq, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED_OR_ONE_LESS, + .extra_size_for_native = extra_size_for_native_irq_or_dma, + .size_for_aml = size_for_aml_irq, + .to_native = convert_irq_to_native, + .to_aml = convert_irq_to_aml, + ), + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_DMA, + UACPI_RESOURCE_TYPE_DMA, + struct acpi_resource_dma, + uacpi_resource_dma, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .extra_size_for_native = extra_size_for_native_irq_or_dma, + .to_native = convert_dma, + .to_aml = convert_dma, + ), + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_START_DEPENDENT, + UACPI_RESOURCE_TYPE_START_DEPENDENT, + struct acpi_resource_start_dependent, + uacpi_resource_start_dependent, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED_OR_ONE_LESS, + .size_for_aml = size_for_aml_start_dependent, + .to_native = convert_start_dependent_to_native, + .to_aml = convert_start_dependent_to_aml, + ), + DEFINE_SMALL_AML_RESOURCE_NO_NATIVE_REPR( + UACPI_AML_RESOURCE_END_DEPENDENT, + UACPI_RESOURCE_TYPE_END_DEPENDENT, + struct acpi_resource_end_dependent, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + ), + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_IO, + UACPI_RESOURCE_TYPE_IO, + struct acpi_resource_io, + uacpi_resource_io, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_io, + .to_aml = convert_io, + ), + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_FIXED_IO, + UACPI_RESOURCE_TYPE_FIXED_IO, + struct acpi_resource_fixed_io, + uacpi_resource_fixed_io, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_fixed_io, + .to_aml = convert_fixed_io, + ), + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_FIXED_DMA, + UACPI_RESOURCE_TYPE_FIXED_DMA, + struct acpi_resource_fixed_dma, + uacpi_resource_fixed_dma, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_fixed_dma, + .to_aml = convert_fixed_dma, + ), + DEFINE_SMALL_AML_RESOURCE( + UACPI_AML_RESOURCE_VENDOR_TYPE0, + UACPI_RESOURCE_TYPE_VENDOR_SMALL, + struct acpi_resource_vendor_defined_type0, + uacpi_resource_vendor, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .size_for_aml = size_for_aml_vendor, + .extra_size_for_native = extra_size_for_native_vendor, + .to_native = convert_vendor_type0, + .to_aml = convert_vendor_type0, + ), + DEFINE_SMALL_AML_RESOURCE_NO_NATIVE_REPR( + UACPI_AML_RESOURCE_END_TAG, + UACPI_RESOURCE_TYPE_END_TAG, + struct acpi_resource_end_tag, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_MEMORY24, + UACPI_RESOURCE_TYPE_MEMORY24, + struct acpi_resource_memory24, + uacpi_resource_memory24, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_memory24, + .to_aml = convert_memory24, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_GENERIC_REGISTER, + UACPI_RESOURCE_TYPE_GENERIC_REGISTER, + struct acpi_resource_generic_register, + uacpi_resource_generic_register, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_generic_register, + .to_aml = convert_generic_register, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_VENDOR_TYPE1, + UACPI_RESOURCE_TYPE_VENDOR_LARGE, + struct acpi_resource_vendor_defined_type1, + uacpi_resource_vendor, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_vendor, + .size_for_aml = size_for_aml_vendor, + .to_native = convert_vendor_type1, + .to_aml = convert_vendor_type1, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_MEMORY32, + UACPI_RESOURCE_TYPE_MEMORY32, + struct acpi_resource_memory32, + uacpi_resource_memory32, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_memory32, + .to_aml = convert_memory32, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_FIXED_MEMORY32, + UACPI_RESOURCE_TYPE_FIXED_MEMORY32, + struct acpi_resource_fixed_memory32, + uacpi_resource_fixed_memory32, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_fixed_memory32, + .to_aml = convert_fixed_memory32, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_ADDRESS32, + UACPI_RESOURCE_TYPE_ADDRESS32, + struct acpi_resource_address32, + uacpi_resource_address32, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_address_or_clock_input, + .size_for_aml = size_for_aml_address_or_clock_input, + .to_native = convert_address32, + .to_aml = convert_address32, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_ADDRESS16, + UACPI_RESOURCE_TYPE_ADDRESS16, + struct acpi_resource_address16, + uacpi_resource_address16, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_address_or_clock_input, + .size_for_aml = size_for_aml_address_or_clock_input, + .to_native = convert_address16, + .to_aml = convert_address16, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_EXTENDED_IRQ, + UACPI_RESOURCE_TYPE_EXTENDED_IRQ, + struct acpi_resource_extended_irq, + uacpi_resource_extended_irq, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_extended_irq, + .size_for_aml = size_for_aml_extended_irq, + .to_native = convert_extended_irq, + .to_aml = convert_extended_irq, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_ADDRESS64, + UACPI_RESOURCE_TYPE_ADDRESS64, + struct acpi_resource_address64, + uacpi_resource_address64, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_address_or_clock_input, + .size_for_aml = size_for_aml_address_or_clock_input, + .to_native = convert_address64, + .to_aml = convert_address64, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_ADDRESS64_EXTENDED, + UACPI_RESOURCE_TYPE_ADDRESS64_EXTENDED, + struct acpi_resource_address64_extended, + uacpi_resource_address64_extended, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_FIXED, + .to_native = convert_address64_extended, + .to_aml = convert_address64_extended, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_GPIO_CONNECTION, + UACPI_RESOURCE_TYPE_GPIO_CONNECTION, + struct acpi_resource_gpio_connection, + uacpi_resource_gpio_connection, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_gpio_or_pins, + .size_for_aml = size_for_aml_gpio_or_pins, + .to_aml = convert_gpio_connection, + .to_native = convert_gpio_connection, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_PIN_FUNCTION, + UACPI_RESOURCE_TYPE_PIN_FUNCTION, + struct acpi_resource_pin_function, + uacpi_resource_pin_function, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_gpio_or_pins, + .size_for_aml = size_for_aml_gpio_or_pins, + .to_aml = convert_pin_function, + .to_native = convert_pin_function, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_SERIAL_CONNECTION, + 0, // the native type here is determined dynamically + struct acpi_resource_serial, + uacpi_resource_serial_bus_common, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_serial_connection, + .size_for_aml = aml_size_for_serial_connection, + .to_native = convert_generic_serial_bus, + .to_aml = convert_generic_serial_bus, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_PIN_CONFIGURATION, + UACPI_RESOURCE_TYPE_PIN_CONFIGURATION, + struct acpi_resource_pin_configuration, + uacpi_resource_pin_configuration, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_gpio_or_pins, + .size_for_aml = size_for_aml_gpio_or_pins, + .to_native = convert_pin_configuration, + .to_aml = convert_pin_configuration, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_PIN_GROUP, + UACPI_RESOURCE_TYPE_PIN_GROUP, + struct acpi_resource_pin_group, + uacpi_resource_pin_group, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_gpio_or_pins, + .size_for_aml = size_for_aml_gpio_or_pins, + .to_native = convert_pin_group, + .to_aml = convert_pin_group, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_PIN_GROUP_FUNCTION, + UACPI_RESOURCE_TYPE_PIN_GROUP_FUNCTION, + struct acpi_resource_pin_group_function, + uacpi_resource_pin_group_function, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_pin_group, + .size_for_aml = size_for_aml_pin_group, + .to_native = convert_pin_group_function, + .to_aml = convert_pin_group_function, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_PIN_GROUP_CONFIGURATION, + UACPI_RESOURCE_TYPE_PIN_GROUP_CONFIGURATION, + struct acpi_resource_pin_group_configuration, + uacpi_resource_pin_group_configuration, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_pin_group, + .size_for_aml = size_for_aml_pin_group, + .to_native = convert_pin_group_configuration, + .to_aml = convert_pin_group_configuration, + ), + DEFINE_LARGE_AML_RESOURCE( + UACPI_AML_RESOURCE_CLOCK_INPUT, + UACPI_RESOURCE_TYPE_CLOCK_INPUT, + struct acpi_resource_clock_input, + uacpi_resource_clock_input, + .size_kind = UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE, + .extra_size_for_native = extra_size_for_native_address_or_clock_input, + .size_for_aml = size_for_aml_address_or_clock_input, + .to_native = convert_clock_input, + .to_aml = convert_clock_input, + ), +}; + +static enum uacpi_aml_resource get_aml_resource_type(uacpi_u8 raw_byte) +{ + if (raw_byte & ACPI_LARGE_ITEM) { + return aml_resource_to_type[ + LARGE_RESOURCE_BASE + (raw_byte & ACPI_LARGE_ITEM_NAME_MASK) + ]; + } + + return aml_resource_to_type[ + (raw_byte >> ACPI_SMALL_ITEM_NAME_IDX) & ACPI_SMALL_ITEM_NAME_MASK + ]; +} + +static uacpi_status get_aml_resource_size( + uacpi_u8 *data, uacpi_size bytes_left, uacpi_u16 *out_size +) +{ + uacpi_u16 size; + + /* + * Resource header is not included in size for both, so we subtract + * the header size from bytes_left to validate it. + */ + if (*data & ACPI_LARGE_ITEM) { + if (uacpi_unlikely(bytes_left < 3)) + return UACPI_STATUS_AML_INVALID_RESOURCE; + + uacpi_memcpy(&size, data + 1, sizeof(size)); + bytes_left -= aml_resource_kind_to_header_size[ + UACPI_AML_RESOURCE_KIND_LARGE + ]; + } else { + size = *data & ACPI_SMALL_ITEM_LENGTH_MASK; + bytes_left -= aml_resource_kind_to_header_size[ + UACPI_AML_RESOURCE_KIND_SMALL + ]; + } + + if (uacpi_unlikely(size > bytes_left)) + return UACPI_STATUS_AML_INVALID_RESOURCE; + + *out_size = size; + return UACPI_STATUS_OK; +} + +static uacpi_status validate_aml_serial_type(uacpi_u8 type) +{ + if (uacpi_unlikely(type < ACPI_SERIAL_TYPE_I2C || + type > ACPI_SERIAL_TYPE_CSI2)) { + uacpi_error("invalid/unsupported serial connection type %d\n", type); + return UACPI_STATUS_AML_INVALID_RESOURCE; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_for_each_aml_resource( + uacpi_buffer *buffer, uacpi_aml_resource_iteration_callback cb, void *user +) +{ + uacpi_status ret; + uacpi_iteration_decision decision; + uacpi_u8 *data; + uacpi_size bytes_left; + uacpi_u16 resource_size; + enum uacpi_aml_resource type; + const struct uacpi_resource_spec *spec; + + bytes_left = buffer->size; + data = buffer->data; + + while (bytes_left) { + type = get_aml_resource_type(*data); + if (uacpi_unlikely(type == UACPI_AML_RESOURCE_TYPE_INVALID)) + return UACPI_STATUS_AML_INVALID_RESOURCE; + + ret = get_aml_resource_size(data, bytes_left, &resource_size); + if (uacpi_unlikely_error(ret)) + return ret; + + spec = &aml_resources[type]; + switch (spec->size_kind) { + case UACPI_AML_RESOURCE_SIZE_KIND_FIXED: + if (resource_size != spec->aml_size) + return UACPI_STATUS_AML_INVALID_RESOURCE; + break; + case UACPI_AML_RESOURCE_SIZE_KIND_VARIABLE: + if (resource_size < spec->aml_size) + return UACPI_STATUS_AML_INVALID_RESOURCE; + break; + case UACPI_AML_RESOURCE_SIZE_KIND_FIXED_OR_ONE_LESS: + if (resource_size != spec->aml_size && + resource_size != (spec->aml_size - 1)) + return UACPI_STATUS_AML_INVALID_RESOURCE; + break; + default: + return UACPI_STATUS_INTERNAL_ERROR; + } + + if (spec->type == UACPI_AML_RESOURCE_SERIAL_CONNECTION) { + struct acpi_resource_serial *serial; + + serial = (struct acpi_resource_serial*)data; + + ret = validate_aml_serial_type(serial->type); + if (uacpi_unlikely_error(ret)) + return ret; + } + + decision = cb(user, data, resource_size, spec); + switch (decision) { + case UACPI_ITERATION_DECISION_BREAK: + return UACPI_STATUS_OK; + case UACPI_ITERATION_DECISION_CONTINUE: { + uacpi_size total_size = resource_size; + + total_size += aml_resource_kind_to_header_size[spec->resource_kind]; + data += total_size; + bytes_left -= total_size; + break; + } + default: + return UACPI_STATUS_INTERNAL_ERROR; + } + + if (type == UACPI_AML_RESOURCE_END_TAG) + return UACPI_STATUS_OK; + } + + return UACPI_STATUS_NO_RESOURCE_END_TAG; +} + +static uacpi_iteration_decision find_end( + void *opaque, uacpi_u8 *data, uacpi_u16 resource_size, + const struct uacpi_resource_spec *spec +) +{ + uacpi_u8 **out_ptr = opaque; + UACPI_UNUSED(resource_size); + + if (spec->type != UACPI_AML_RESOURCE_END_TAG) + return UACPI_ITERATION_DECISION_CONTINUE; + + *out_ptr = data; + return UACPI_ITERATION_DECISION_BREAK; +} + +static uacpi_size native_size_for_aml_resource( + uacpi_u8 *data, uacpi_u16 size, const struct uacpi_resource_spec *spec +) +{ + uacpi_size final_size = spec->native_size; + + if (spec->extra_size_for_native) + final_size += spec->extra_size_for_native(spec, data, size); + + return UACPI_ALIGN_UP(final_size, sizeof(void*), uacpi_size); +} + +uacpi_status uacpi_find_aml_resource_end_tag( + uacpi_buffer *buffer, uacpi_size *out_offset +) +{ + uacpi_u8 *end_tag_ptr; + uacpi_status ret; + + if (!buffer || buffer->size == 0) { + *out_offset = 0; + return UACPI_STATUS_OK; + } + + /* + * This returning UACPI_STATUS_OK guarantees that end_tag_ptr is set to + * a valid value because a missing end tag would produce a + * UACPI_STATUS_NO_RESOURCE_END_TAG error. + */ + ret = uacpi_for_each_aml_resource(buffer, find_end, &end_tag_ptr); + if (uacpi_unlikely_error(ret)) + return ret; + + *out_offset = end_tag_ptr - buffer->byte_data; + return UACPI_STATUS_OK; +} + +struct resource_conversion_ctx { + union { + void *buf; + uacpi_u8 *byte_buf; + uacpi_size size; + }; + uacpi_status st; +}; + +// Opcodes that are the same for both AML->native and native->AML +#define CONVERSION_OPCODES_COMMON(native_buf) \ + case UACPI_RESOURCE_CONVERT_OPCODE_END: \ + return UACPI_ITERATION_DECISION_CONTINUE; \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_FIELD_8: \ + case UACPI_RESOURCE_CONVERT_OPCODE_FIELD_16: \ + case UACPI_RESOURCE_CONVERT_OPCODE_FIELD_32: \ + case UACPI_RESOURCE_CONVERT_OPCODE_FIELD_64: { \ + uacpi_u8 bytes; \ + \ + bytes = 1 << (insn->code - UACPI_RESOURCE_CONVERT_OPCODE_FIELD_8); \ + accumulator = insn->imm == 0xFF ? 0 : accumulator + insn->imm; \ + \ + uacpi_memcpy(dst, src, bytes * UACPI_MAX(1, accumulator)); \ + accumulator = 0; \ + break; \ + } \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_SKIP_IF_AML_SIZE_LESS_THAN: \ + if (aml_size < insn->arg0) \ + pc += insn->imm; \ + break; \ + case UACPI_RESOURCE_CONVERT_OPCODE_SKIP_IF_NOT_EQUALS: \ + if (insn->arg0 != accumulator) \ + pc += insn->imm; \ + break; \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_SET_TO_IMM: \ + uacpi_memcpy(dst, &insn->imm, sizeof(insn->imm)); \ + break; \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_IMM: \ + accumulator = insn->imm; \ + break; \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_8_STORE: \ + uacpi_memcpy_zerout(&accumulator, src, sizeof(accumulator), 1); \ + uacpi_memcpy(dst, &accumulator, 1); \ + \ + if (insn->imm) \ + accumulator *= insn->imm; \ + break; \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_8_NATIVE: \ + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_16_NATIVE: { \ + uacpi_u8 bytes; \ + \ + bytes = \ + 1 << (insn->code - UACPI_RESOURCE_CONVERT_OPCODE_LOAD_8_NATIVE); \ + uacpi_memcpy_zerout( \ + &accumulator, native_buf, sizeof(accumulator), bytes \ + ); \ + break; \ + } \ + \ + case UACPI_RESOURCE_CONVERT_OPCODE_UNREACHABLE: \ + default: \ + if (insn->code != UACPI_RESOURCE_CONVERT_OPCODE_UNREACHABLE) { \ + uacpi_error("unhandled resource conversion opcode %d\n", \ + insn->code); \ + } else { \ + uacpi_error("tried to execute unreachable conversion opcode\n"); \ + } \ + ctx->st = UACPI_STATUS_INTERNAL_ERROR; \ + return UACPI_ITERATION_DECISION_BREAK; + +#define PTR_AT(ptr, offset) (void*)((uacpi_u8*)(ptr) + (offset)) + +#define NATIVE_OFFSET(res, offset) \ + PTR_AT(res, (offset) + (sizeof(uacpi_u32) * 2)) + +#define NATIVE_FIELD(res, name, field) \ + NATIVE_OFFSET(res, NATIVE_O(name, field)) + +#define CHECK_AML_OOB(offset, prefix, what) \ + if (uacpi_unlikely(offset > ((uacpi_u32)aml_size + header_size))) { \ + uacpi_error(prefix what " is OOB: %zu > %u\n", \ + (uacpi_size)offset, (uacpi_u32)aml_size + header_size); \ + ctx->st = UACPI_STATUS_AML_INVALID_RESOURCE; \ + return UACPI_ITERATION_DECISION_BREAK; \ + } + +#define CHECK_AML_OFFSET_BASE(offset, what) \ + if (uacpi_unlikely(offset < base_aml_size_with_header)) { \ + uacpi_error( \ + "invalid " what " offset: %zu, expected at least %u\n", \ + (uacpi_size)offset, base_aml_size_with_header); \ + ctx->st = UACPI_STATUS_AML_INVALID_RESOURCE; \ + return UACPI_ITERATION_DECISION_BREAK; \ + } + +#define CHECK_AML_OFFSET(offset, what) \ + CHECK_AML_OOB(offset, "end of ", what) \ + CHECK_AML_OFFSET_BASE(offset, what) + +static uacpi_resource_type aml_serial_to_native_type( + uacpi_u8 type +) +{ + return (type - ACPI_SERIAL_TYPE_I2C) + + UACPI_RESOURCE_TYPE_SERIAL_I2C_CONNECTION; +} + +static uacpi_iteration_decision do_aml_resource_to_native( + void *opaque, uacpi_u8 *data, uacpi_u16 aml_size, + const struct uacpi_resource_spec *spec +) +{ + struct resource_conversion_ctx *ctx = opaque; + uacpi_resource *resource = ctx->buf; + const struct uacpi_resource_convert_instruction *insns, *insn; + uacpi_u8 header_size, pc = 0; + uacpi_u8 *src, *dst; + void *resource_end; + uacpi_u16 base_aml_size; + uacpi_u32 base_aml_size_with_header, accumulator = 0; + + insns = spec->to_native; + + header_size = aml_resource_kind_to_header_size[spec->resource_kind]; + resource->type = spec->native_type; + resource->length = native_size_for_aml_resource(data, aml_size, spec); + resource_end = ctx->byte_buf + spec->native_size; + ctx->byte_buf += resource->length; + + base_aml_size = base_aml_size_with_header = spec->aml_size; + base_aml_size_with_header += header_size; + + if (insns == UACPI_NULL) + return UACPI_ITERATION_DECISION_CONTINUE; + + for (;;) { + insn = &insns[pc++]; + + src = data + insn->aml_offset; + dst = NATIVE_OFFSET(resource, insn->native_offset); + + switch (insn->code) { + case UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_8: + case UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_16: { + uacpi_size i, j, max_bit; + uacpi_u16 value; + + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_16) { + max_bit = 16; + uacpi_memcpy(&value, src, sizeof(uacpi_u16)); + } else { + max_bit = 8; + uacpi_memcpy_zerout( + &value, src, sizeof(value), sizeof(uacpi_u8) + ); + } + + for (i = 0, j = 0; i < max_bit; ++i) { + if (!(value & (1 << i))) + continue; + + dst[j++] = i; + } + + uacpi_memcpy(NATIVE_OFFSET(resource, insn->arg2), &j, 1); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_1: + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_2: + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_3: + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_6:{ + uacpi_u8 mask, value; + + mask = (insn->code - UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_1) + 1; + mask = (1 << mask) - 1; + + value = (*src >> insn->imm) & mask; + uacpi_memcpy(dst, &value, sizeof(value)); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_AML_SIZE_32: + accumulator = aml_size; + uacpi_memcpy(dst, &accumulator, 4); + break; + + case UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE: + case UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE_NO_INDEX: + case UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_LABEL: { + uacpi_size offset = 0, max_offset, length = 0; + uacpi_char *src_string, *dst_string; + union { + void *ptr; + uacpi_resource_source *source; + uacpi_resource_label *label; + } dst_name = { .ptr = dst }; + + /* + * Check if the string is bounded by anything at the top. If not, we + * just assume it ends at the end of the resource. + */ + if (insn->arg2) { + uacpi_memcpy_zerout(&max_offset, data + insn->arg2, + sizeof(max_offset), sizeof(uacpi_u16)); + CHECK_AML_OFFSET(max_offset, "resource source"); + } else { + max_offset = aml_size + header_size; + } + + offset += base_aml_size_with_header; + offset += accumulator; + + if (insn->code != UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_LABEL) + dst_name.source->index_present = UACPI_TRUE; + + if (offset >= max_offset) { + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE) + dst_name.source->index_present = UACPI_FALSE; + break; + } + + src_string = PTR_AT(data, offset); + + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE) { + uacpi_memcpy(&dst_name.source->index, src_string++, 1); + offset++; + } + + if (offset == max_offset) + break; + + while (offset++ < max_offset) { + if (src_string[length++] == '\0') + break; + } + + if (src_string[length - 1] != '\0') { + uacpi_error("non-null-terminated resource source string\n"); + ctx->st = UACPI_STATUS_AML_INVALID_RESOURCE; + return UACPI_ITERATION_DECISION_BREAK; + } + + dst_string = PTR_AT(resource_end, accumulator); + uacpi_memcpy(dst_string, src_string, length); + + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_LABEL) { + dst_name.label->length = length; + dst_name.label->string = dst_string; + } else { + dst_name.source->length = length; + dst_name.source->string = dst_string; + } + + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_PIN_TABLE_LENGTH: + uacpi_memcpy_zerout(&accumulator, src, + sizeof(accumulator), sizeof(uacpi_u16)); + CHECK_AML_OFFSET(accumulator, "pin table"); + + accumulator -= base_aml_size_with_header; + break; + + case UACPI_RESOURCE_CONVERT_OPCODE_PIN_TABLE: { + uacpi_u16 entry_count = accumulator / 2; + + /* + * Pin table is stored right at the end of the resource buffer, + * copy the data there. + */ + uacpi_memcpy( + resource_end, + data + base_aml_size_with_header, + accumulator + ); + + // Set pin_table_length + uacpi_memcpy(dst, &entry_count, sizeof(entry_count)); + + // Set pin_table pointer + uacpi_memcpy(NATIVE_OFFSET(resource, insn->arg2), + &resource_end, sizeof(void*)); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_VENDOR_DATA: { + uacpi_size length; + uacpi_u16 data_offset, offset_from_end; + void *native_dst, *vendor_data; + + uacpi_memcpy(&data_offset, src, sizeof(data_offset)); + CHECK_AML_OFFSET(data_offset, "vendor data"); + + vendor_data = data + data_offset; + + /* + * Rebase the offset to cut off the header as it's not included + * in the size fields. + */ + data_offset -= header_size; + + length = aml_size - data_offset; + if (length == 0) + break; + + uacpi_memcpy(dst, &length, sizeof(uacpi_u16)); + + offset_from_end = data_offset - base_aml_size; + native_dst = PTR_AT(resource_end, offset_from_end); + + uacpi_memcpy(native_dst, vendor_data, length); + uacpi_memcpy(NATIVE_OFFSET(resource, insn->arg2), + &native_dst, sizeof(void*)); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_SERIAL_TYPE_SPECIFIC: { + uacpi_resource_serial_bus_common *serial_bus_common; + uacpi_u8 serial_type, extra_size, type_length; + + serial_bus_common = &resource->serial_bus_common; + serial_type = *src; + serial_bus_common->type = serial_type; + resource->type = aml_serial_to_native_type(serial_type); + + /* + * Now that we know the serial type rebase the end pointers and + * sizes. + */ + resource_end = PTR_AT( + resource_end, + aml_serial_resource_to_extra_native_size[serial_type] + ); + extra_size = aml_serial_resource_to_extra_aml_size[serial_type]; + base_aml_size += extra_size; + base_aml_size_with_header += extra_size; + + type_length = serial_bus_common->type_data_length; + if (uacpi_unlikely(type_length < extra_size)) { + uacpi_error( + "invalid type-specific data length: %d, " + "expected at least %d\n", type_length, extra_size + ); + ctx->st = UACPI_STATUS_AML_INVALID_RESOURCE; + return UACPI_ITERATION_DECISION_BREAK; + } + + /* + * Calculate the length of the vendor data. All the extra data + * beyond the end of type-specific size is considered vendor data. + */ + accumulator = type_length - extra_size; + if (accumulator == 0) + break; + + serial_bus_common->vendor_data_length = accumulator; + serial_bus_common->vendor_data = resource_end; + uacpi_memcpy( + resource_end, + data + base_aml_size_with_header, + accumulator + ); + break; + } + + CONVERSION_OPCODES_COMMON(dst) + } + } +} + +static uacpi_status aml_resources_to_native( + uacpi_buffer *aml_buffer, void *native_buffer +) +{ + uacpi_status ret; + struct resource_conversion_ctx ctx = { + .buf = native_buffer, + }; + + ret = uacpi_for_each_aml_resource( + aml_buffer, do_aml_resource_to_native, &ctx + ); + if (uacpi_unlikely_error(ret)) + return ret; + + return ctx.st; +} + +static uacpi_iteration_decision accumulate_native_buffer_size( + void *opaque, uacpi_u8 *data, uacpi_u16 resource_size, + const struct uacpi_resource_spec *spec +) +{ + struct resource_conversion_ctx *ctx = opaque; + uacpi_size size_for_this; + + size_for_this = native_size_for_aml_resource(data, resource_size, spec); + if (size_for_this == 0 || (ctx->size + size_for_this) < ctx->size) { + uacpi_error("invalid native size for aml resource: %zu\n", + size_for_this); + ctx->st = UACPI_STATUS_AML_INVALID_RESOURCE; + return UACPI_ITERATION_DECISION_BREAK; + } + + ctx->size += size_for_this; + return UACPI_ITERATION_DECISION_CONTINUE; +} + +static uacpi_status eval_resource_helper( + uacpi_namespace_node *node, const uacpi_char *method, + uacpi_object **out_obj +) +{ + uacpi_object *obj; + + obj = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(obj == UACPI_NULL || obj->type != UACPI_OBJECT_DEVICE)) + return UACPI_STATUS_INVALID_ARGUMENT; + + return uacpi_eval_typed( + node, method, UACPI_NULL, UACPI_OBJECT_BUFFER_BIT, out_obj + ); +} + +uacpi_status uacpi_native_resources_from_aml( + uacpi_buffer *aml_buffer, uacpi_resources **out_resources +) +{ + uacpi_status ret; + struct resource_conversion_ctx ctx = { 0 }; + uacpi_resources *resources; + + ret = uacpi_for_each_aml_resource( + aml_buffer, accumulate_native_buffer_size, &ctx + ); + if (uacpi_unlikely_error(ret)) + return ret; + + if (uacpi_unlikely_error(ctx.st)) + return ctx.st; + + // Realistically any resource buffer bigger than this is probably a bug + if (uacpi_unlikely(ctx.size > (5 * 1024u * 1024u))) { + uacpi_error("bug: bogus native resource buffer size %zu\n", ctx.size); + return UACPI_STATUS_INTERNAL_ERROR; + } + + resources = uacpi_kernel_alloc_zeroed(ctx.size + sizeof(uacpi_resources)); + if (uacpi_unlikely(resources == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + resources->length = ctx.size; + resources->entries = UACPI_PTR_ADD(resources, sizeof(uacpi_resources)); + + ret = aml_resources_to_native(aml_buffer, resources->entries); + if (uacpi_unlikely_error(ret)) { + uacpi_free_resources(resources); + return ret; + } + + *out_resources = resources; + return ret; +} + +void uacpi_free_resources(uacpi_resources *resources) +{ + if (resources == UACPI_NULL) + return; + + uacpi_free(resources, sizeof(uacpi_resources) + resources->length); +} + +static uacpi_status extract_native_resources_from_method( + uacpi_namespace_node *device, const uacpi_char *method, + uacpi_resources **out_resources +) +{ + uacpi_status ret; + uacpi_object *obj; + + ret = eval_resource_helper(device, method, &obj); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_native_resources_from_aml(obj->buffer, out_resources); + uacpi_object_unref(obj); + + return ret; +} + +uacpi_status uacpi_get_current_resources( + uacpi_namespace_node *device, uacpi_resources **out_resources +) +{ + return extract_native_resources_from_method(device, "_CRS", out_resources); +} + +uacpi_status uacpi_get_possible_resources( + uacpi_namespace_node *device, uacpi_resources **out_resources +) +{ + return extract_native_resources_from_method(device, "_PRS", out_resources); +} + +uacpi_status uacpi_for_each_resource( + uacpi_resources *resources, uacpi_resource_iteration_callback cb, void *user +) +{ + uacpi_size bytes_left = resources->length; + uacpi_resource *current = resources->entries; + uacpi_iteration_decision decision; + + while (bytes_left) { + // At least the head bytes + if (uacpi_unlikely(bytes_left < 4)) { + uacpi_error("corrupted resource buffer %p length %zu\n", + resources, resources->length); + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (uacpi_unlikely(current->type > UACPI_RESOURCE_TYPE_MAX)) { + uacpi_error("invalid resource type %d\n", current->type); + return UACPI_STATUS_INVALID_ARGUMENT; + } + + if (uacpi_unlikely(current->length > bytes_left)) { + uacpi_error("corrupted resource@%p length %u (%zu bytes left)\n", + current, current->length, bytes_left); + return UACPI_STATUS_INVALID_ARGUMENT; + } + + decision = cb(user, current); + + if (decision == UACPI_ITERATION_DECISION_BREAK || + current->type == UACPI_RESOURCE_TYPE_END_TAG) + return UACPI_STATUS_OK; + + bytes_left -= current->length; + current = (uacpi_resource*)((uacpi_u8*)current + current->length); + } + + return UACPI_STATUS_NO_RESOURCE_END_TAG; +} + +uacpi_status uacpi_for_each_device_resource( + uacpi_namespace_node *device, const uacpi_char *method, + uacpi_resource_iteration_callback cb, void *user +) +{ + uacpi_status ret; + uacpi_resources *resources; + + ret = extract_native_resources_from_method(device, method, &resources); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_for_each_resource(resources, cb, user); + uacpi_free_resources(resources); + + return ret; +} + +static const struct uacpi_resource_spec *resource_spec_from_native( + uacpi_resource *resource +) +{ + return &aml_resources[native_resource_to_type[resource->type]]; +} + +static uacpi_size aml_size_for_native_resource( + uacpi_resource *resource, const struct uacpi_resource_spec *spec +) +{ + return spec->size_for_aml ? + spec->size_for_aml(spec, resource) : + aml_size_with_header(spec); +} + +static uacpi_iteration_decision do_native_resource_to_aml( + void *opaque, uacpi_resource *resource +) +{ + struct resource_conversion_ctx *ctx = opaque; + const struct uacpi_resource_spec *spec; + const struct uacpi_resource_convert_instruction *insns, *insn; + uacpi_u8 pc = 0; + uacpi_u8 *dst_base, *src, *dst; + uacpi_u32 aml_size, base_aml_size_with_header, accumulator = 0; + void *resource_end; + + spec = resource_spec_from_native(resource); + aml_size = aml_size_for_native_resource(resource, spec); + insns = spec->to_aml; + + dst_base = ctx->byte_buf; + ctx->byte_buf += aml_size; + aml_size -= aml_resource_kind_to_header_size[spec->resource_kind]; + + base_aml_size_with_header = spec->aml_size; + base_aml_size_with_header += aml_resource_kind_to_header_size[ + spec->resource_kind + ]; + resource_end = PTR_AT(resource, spec->native_size); + + if (spec->resource_kind == UACPI_AML_RESOURCE_KIND_LARGE) { + *dst_base = ACPI_LARGE_ITEM | type_to_aml_resource[spec->type]; + uacpi_memcpy(dst_base + 1, &aml_size, sizeof(uacpi_u16)); + } else { + *dst_base = type_to_aml_resource[spec->type] << ACPI_SMALL_ITEM_NAME_IDX; + *dst_base |= aml_size; + } + + if (insns == UACPI_NULL) + return UACPI_ITERATION_DECISION_CONTINUE; + + for (;;) { + insn = &insns[pc++]; + + src = NATIVE_OFFSET(resource, insn->native_offset); + dst = dst_base + insn->aml_offset; + + switch (insn->code) { + case UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_8: + case UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_16: { + uacpi_u8 i, *array_size, bytes = 1; + uacpi_u16 mask = 0; + + array_size = NATIVE_OFFSET(resource, insn->arg2); + for (i = 0; i < *array_size; ++i) + mask |= 1 << src[i]; + + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_PACKED_ARRAY_16) + bytes = 2; + + uacpi_memcpy(dst, &mask, bytes); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_1: + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_2: + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_3: + case UACPI_RESOURCE_CONVERT_OPCODE_BIT_FIELD_6: + *dst |= *src << insn->imm; + break; + + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_AML_SIZE_32: + accumulator = aml_size; + break; + + case UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE: + case UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE_NO_INDEX: + case UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_LABEL: { + uacpi_size source_offset, length; + uacpi_u8 *dst_string; + const uacpi_char *src_string; + union { + void *ptr; + uacpi_resource_source *source; + uacpi_resource_label *label; + } src_name = { .ptr = src }; + + source_offset = base_aml_size_with_header + accumulator; + dst_string = dst_base + source_offset; + + if (insn->aml_offset) + uacpi_memcpy(dst, &source_offset, sizeof(uacpi_u16)); + + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_SOURCE && + src_name.source->index_present) + uacpi_memcpy(dst_string++, &src_name.source->index, 1); + + if (insn->code == UACPI_RESOURCE_CONVERT_OPCODE_RESOURCE_LABEL) { + length = src_name.label->length; + src_string = src_name.label->string; + } else { + length = src_name.source->length; + src_string = src_name.source->string; + } + + if (length == 0) + break; + + if (uacpi_unlikely(src_string == UACPI_NULL)) { + uacpi_error( + "source string length is %zu but the pointer is NULL\n", + length + ); + ctx->st = UACPI_STATUS_INVALID_ARGUMENT; + return UACPI_ITERATION_DECISION_BREAK; + } + + uacpi_memcpy(dst_string, src_string, length); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_LOAD_PIN_TABLE_LENGTH: + uacpi_memcpy_zerout(&accumulator, src, + sizeof(accumulator), sizeof(uacpi_u16)); + accumulator *= sizeof(uacpi_u16); + break; + + case UACPI_RESOURCE_CONVERT_OPCODE_PIN_TABLE: + /* + * The pin table resides right at the end of the base resource, + * set the offset to it in the AML we're encoding. + */ + uacpi_memcpy(dst, &base_aml_size_with_header, sizeof(uacpi_u16)); + + /* + * Copy the actual data. It also resides right at the end of the + * native base resource. + */ + uacpi_memcpy( + dst_base + base_aml_size_with_header, + resource_end, + accumulator + ); + break; + + case UACPI_RESOURCE_CONVERT_OPCODE_VENDOR_DATA: { + uacpi_u16 vendor_data_length, data_offset, vendor_data_offset; + uacpi_u8 *vendor_data; + + // Read the vendor_data pointer + uacpi_memcpy(&vendor_data, NATIVE_OFFSET(resource, insn->arg2), + sizeof(void*)); + uacpi_memcpy(&vendor_data_length, src, sizeof(uacpi_u16)); + + if (vendor_data == UACPI_NULL) { + uacpi_size full_aml_size; + + if (uacpi_unlikely(vendor_data_length != 0)) { + uacpi_error( + "vendor_data_length is %d, but pointer is NULL\n", + vendor_data_length + ); + ctx->st = UACPI_STATUS_INVALID_ARGUMENT; + return UACPI_ITERATION_DECISION_BREAK; + } + + /* + * There's no vendor data. The specification still mandates + * that we fill the vendor data offset field correctly, meaning + * we set it to the total length of the resource. + */ + full_aml_size = aml_size; + full_aml_size += aml_resource_kind_to_header_size[ + spec->resource_kind + ]; + + uacpi_memcpy(dst, &full_aml_size, sizeof(uacpi_u16)); + break; + } + + /* + * Calculate the offset of vendor data from the end of the native + * resource and use it since it matches the offset from the end of + * the AML resource. + * + * Non-zero value means there's a source string in between. + */ + data_offset = vendor_data - (uacpi_u8*)resource_end; + vendor_data_offset = data_offset + base_aml_size_with_header; + + // Write vendor_data_offset + uacpi_memcpy(dst, &vendor_data_offset, sizeof(uacpi_u16)); + + /* + * Write vendor_data_length, this field is right after + * vendor_data_offset, and is completely redundant, but it exists + * nonetheless. + */ + uacpi_memcpy( + dst + sizeof(uacpi_u16), + &vendor_data_length, + sizeof(vendor_data_length) + ); + + // Finally write the data itself + uacpi_memcpy( + dst_base + vendor_data_offset, + vendor_data, + vendor_data_length + ); + break; + } + + case UACPI_RESOURCE_CONVERT_OPCODE_SERIAL_TYPE_SPECIFIC: { + uacpi_u8 serial_type = *src; + *dst = serial_type; + + ctx->st = validate_aml_serial_type(serial_type); + if (uacpi_unlikely_error(ctx->st)) + return UACPI_ITERATION_DECISION_BREAK; + + if (uacpi_unlikely(resource->type != + aml_serial_to_native_type(serial_type))) { + uacpi_error( + "native serial resource type %d doesn't match expected %d\n", + resource->type, aml_serial_to_native_type(serial_type) + ); + ctx->st = UACPI_STATUS_INVALID_ARGUMENT; + return UACPI_ITERATION_DECISION_BREAK; + } + + // Rebase the end pointer & size now that we know the serial type + resource_end = PTR_AT( + resource_end, + aml_serial_resource_to_extra_native_size[serial_type] + ); + base_aml_size_with_header += aml_serial_resource_to_extra_aml_size[ + serial_type + ]; + + accumulator = resource->serial_bus_common.vendor_data_length; + if (accumulator == 0) + break; + + // Copy vendor data + uacpi_memcpy( + dst_base + base_aml_size_with_header, + resource_end, + accumulator + ); + break; + } + + CONVERSION_OPCODES_COMMON(src) + } + } +} + +#define INLINE_END_TAG &(uacpi_resource) { .type = UACPI_RESOURCE_TYPE_END_TAG } + +static uacpi_status native_resources_to_aml( + uacpi_resources *native_resources, void *aml_buffer +) +{ + uacpi_status ret; + struct resource_conversion_ctx ctx = { + .buf = aml_buffer, + }; + + ret = uacpi_for_each_resource( + native_resources, do_native_resource_to_aml, &ctx + ); + if (ret == UACPI_STATUS_NO_RESOURCE_END_TAG) { + // An end tag is always included + do_native_resource_to_aml(&ctx, INLINE_END_TAG); + ret = UACPI_STATUS_OK; + } + if (uacpi_unlikely_error(ret)) + return ret; + + return ctx.st; +} + +static uacpi_iteration_decision accumulate_aml_buffer_size( + void *opaque, uacpi_resource *resource +) +{ + struct resource_conversion_ctx *ctx = opaque; + const struct uacpi_resource_spec *spec; + uacpi_size size_for_this; + + // resource->type is sanitized to be valid here by the iteration function + spec = resource_spec_from_native(resource); + + size_for_this = aml_size_for_native_resource(resource, spec); + if (size_for_this == 0 || (ctx->size + size_for_this) < ctx->size) { + uacpi_error("invalid aml size for native resource: %zu\n", + size_for_this); + ctx->st = UACPI_STATUS_INVALID_ARGUMENT; + return UACPI_ITERATION_DECISION_BREAK; + } + + ctx->size += size_for_this; + return UACPI_ITERATION_DECISION_CONTINUE; +} + +uacpi_status uacpi_native_resources_to_aml( + uacpi_resources *resources, uacpi_object **out_template +) +{ + uacpi_status ret; + uacpi_object *obj; + void *buffer; + struct resource_conversion_ctx ctx = { 0 }; + + ret = uacpi_for_each_resource( + resources, accumulate_aml_buffer_size, &ctx + ); + if (ret == UACPI_STATUS_NO_RESOURCE_END_TAG) { + // An end tag is always included + accumulate_aml_buffer_size(&ctx, INLINE_END_TAG); + ret = UACPI_STATUS_OK; + } + if (uacpi_unlikely_error(ret)) + return ret; + if (uacpi_unlikely_error(ctx.st)) + return ctx.st; + + // Same reasoning as native_resource_from_aml + if (uacpi_unlikely(ctx.size > (5 * 1024u * 1024u))) { + uacpi_error("bug: bogus target aml resource buffer size %zu\n", + ctx.size); + return UACPI_STATUS_INTERNAL_ERROR; + } + + buffer = uacpi_kernel_alloc_zeroed(ctx.size); + if (uacpi_unlikely(buffer == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + obj = uacpi_create_object(UACPI_OBJECT_BUFFER); + if (uacpi_unlikely(obj == UACPI_NULL)) { + uacpi_free(buffer, ctx.size); + return UACPI_STATUS_OUT_OF_MEMORY; + } + + obj->buffer->data = buffer; + obj->buffer->size = ctx.size; + + ret = native_resources_to_aml(resources, buffer); + if (uacpi_unlikely_error(ret)) + uacpi_object_unref(obj); + + if (ret == UACPI_STATUS_OK) + *out_template = obj; + + return ret; +} + +uacpi_status uacpi_set_resources( + uacpi_namespace_node *device, uacpi_resources *resources +) +{ + uacpi_status ret; + uacpi_object *res_template; + uacpi_object_array args; + + ret = uacpi_native_resources_to_aml(resources, &res_template); + if (uacpi_unlikely_error(ret)) + return ret; + + args.objects = &res_template; + args.count = 1; + ret = uacpi_eval(device, "_SRS", &args, UACPI_NULL); + + uacpi_object_unref(res_template); + return ret; +} diff --git a/src/uacpi/shareable.c b/src/uacpi/shareable.c new file mode 100644 index 0000000..9587990 --- /dev/null +++ b/src/uacpi/shareable.c @@ -0,0 +1,66 @@ +#include +#include + +#define BUGGED_REFCOUNT 0xFFFFFFFF + +void uacpi_shareable_init(uacpi_handle handle) +{ + struct uacpi_shareable *shareable = handle; + shareable->reference_count = 1; +} + +uacpi_bool uacpi_bugged_shareable(uacpi_handle handle) +{ + struct uacpi_shareable *shareable = handle; + + if (uacpi_unlikely(shareable->reference_count == 0)) + uacpi_make_shareable_bugged(shareable); + + return uacpi_atomic_load32(&shareable->reference_count) == BUGGED_REFCOUNT; +} + +void uacpi_make_shareable_bugged(uacpi_handle handle) +{ + struct uacpi_shareable *shareable = handle; + uacpi_atomic_store32(&shareable->reference_count, BUGGED_REFCOUNT); +} + +uacpi_u32 uacpi_shareable_ref(uacpi_handle handle) +{ + struct uacpi_shareable *shareable = handle; + + if (uacpi_unlikely(uacpi_bugged_shareable(shareable))) + return BUGGED_REFCOUNT; + + return uacpi_atomic_inc32(&shareable->reference_count) - 1; +} + +uacpi_u32 uacpi_shareable_unref(uacpi_handle handle) +{ + struct uacpi_shareable *shareable = handle; + + if (uacpi_unlikely(uacpi_bugged_shareable(shareable))) + return BUGGED_REFCOUNT; + + return uacpi_atomic_dec32(&shareable->reference_count) + 1; +} + +void uacpi_shareable_unref_and_delete_if_last( + uacpi_handle handle, void (*do_free)(uacpi_handle) +) +{ + if (handle == UACPI_NULL) + return; + + if (uacpi_unlikely(uacpi_bugged_shareable(handle))) + return; + + if (uacpi_shareable_unref(handle) == 1) + do_free(handle); +} + +uacpi_u32 uacpi_shareable_refcount(uacpi_handle handle) +{ + struct uacpi_shareable *shareable = handle; + return uacpi_atomic_load32(&shareable->reference_count); +} diff --git a/src/uacpi/sleep.c b/src/uacpi/sleep.c new file mode 100644 index 0000000..2c5f0ad --- /dev/null +++ b/src/uacpi/sleep.c @@ -0,0 +1,606 @@ +#include +#include +#include +#include +#include +#include +#include + +#ifndef UACPI_REDUCED_HARDWARE +#define CALL_SLEEP_FN(name, state) \ + (uacpi_is_hardware_reduced() ? \ + name##_hw_reduced(state) : name##_hw_full(state)) +#else +#define CALL_SLEEP_FN(name, state) name##_hw_reduced(state); +#endif + +static uacpi_status eval_wak(uacpi_u8 state); +static uacpi_status eval_sst(uacpi_u8 value); + +#ifndef UACPI_REDUCED_HARDWARE +uacpi_status uacpi_set_waking_vector( + uacpi_phys_addr addr32, uacpi_phys_addr addr64 +) +{ + struct acpi_facs *facs = g_uacpi_rt_ctx.facs; + + if (facs == UACPI_NULL) + return UACPI_STATUS_OK; + + facs->firmware_waking_vector = addr32; + + // The 64-bit wake vector doesn't exist, we're done + if (facs->length < 32) + return UACPI_STATUS_OK; + + // Only allow 64-bit wake vector on 1.0 and above FACS + if (facs->version >= 1) + facs->x_firmware_waking_vector = addr64; + else + facs->x_firmware_waking_vector = 0; + + return UACPI_STATUS_OK; +} + +static uacpi_status enter_sleep_state_hw_full(uacpi_u8 state) +{ + uacpi_status ret; + uacpi_u64 wake_status, pm1a, pm1b; + + ret = uacpi_write_register_field( + UACPI_REGISTER_FIELD_WAK_STS, ACPI_PM1_STS_CLEAR + ); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_disable_all_gpes(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_clear_all_events(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_enable_all_wake_gpes(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_read_register(UACPI_REGISTER_PM1_CNT, &pm1a); + if (uacpi_unlikely_error(ret)) + return ret; + + pm1a &= ~((uacpi_u64)(ACPI_PM1_CNT_SLP_TYP_MASK | ACPI_PM1_CNT_SLP_EN_MASK)); + pm1b = pm1a; + + pm1a |= g_uacpi_rt_ctx.last_sleep_typ_a << ACPI_PM1_CNT_SLP_TYP_IDX; + pm1b |= g_uacpi_rt_ctx.last_sleep_typ_b << ACPI_PM1_CNT_SLP_TYP_IDX; + + /* + * Just like ACPICA, split writing SLP_TYP and SLP_EN to work around + * buggy firmware that can't handle both written at the same time. + */ + ret = uacpi_write_registers(UACPI_REGISTER_PM1_CNT, pm1a, pm1b); + if (uacpi_unlikely_error(ret)) + return ret; + + pm1a |= ACPI_PM1_CNT_SLP_EN_MASK; + pm1b |= ACPI_PM1_CNT_SLP_EN_MASK; + + if (state < UACPI_SLEEP_STATE_S4) + UACPI_ARCH_FLUSH_CPU_CACHE(); + + ret = uacpi_write_registers(UACPI_REGISTER_PM1_CNT, pm1a, pm1b); + if (uacpi_unlikely_error(ret)) + return ret; + + if (state > UACPI_SLEEP_STATE_S3) { + /* + * We're still here, this is a bug or very slow firmware. + * Just try spinning for a bit. + */ + uacpi_u64 stalled_time = 0; + + // 10 seconds max + while (stalled_time < (10 * 1000 * 1000)) { + uacpi_kernel_stall(100); + stalled_time += 100; + } + + // Try one more time + ret = uacpi_write_registers(UACPI_REGISTER_PM1_CNT, pm1a, pm1b); + if (uacpi_unlikely_error(ret)) + return ret; + + // Nothing we can do here, give up + return UACPI_STATUS_HARDWARE_TIMEOUT; + } + + do { + ret = uacpi_read_register_field( + UACPI_REGISTER_FIELD_WAK_STS, &wake_status + ); + if (uacpi_unlikely_error(ret)) + return ret; + } while (wake_status != 1); + + return UACPI_STATUS_OK; +} + +static uacpi_status prepare_for_wake_from_sleep_state_hw_full(uacpi_u8 state) +{ + uacpi_status ret; + uacpi_u64 pm1a, pm1b; + UACPI_UNUSED(state); + + /* + * Some hardware apparently relies on S0 values being written to the PM1 + * control register on wake, so do this here. + */ + + if (g_uacpi_rt_ctx.s0_sleep_typ_a == UACPI_SLEEP_TYP_INVALID) + goto out; + + ret = uacpi_read_register(UACPI_REGISTER_PM1_CNT, &pm1a); + if (uacpi_unlikely_error(ret)) + goto out; + + pm1a &= ~((uacpi_u64)(ACPI_PM1_CNT_SLP_TYP_MASK | ACPI_PM1_CNT_SLP_EN_MASK)); + pm1b = pm1a; + + pm1a |= g_uacpi_rt_ctx.s0_sleep_typ_a << ACPI_PM1_CNT_SLP_TYP_IDX; + pm1b |= g_uacpi_rt_ctx.s0_sleep_typ_b << ACPI_PM1_CNT_SLP_TYP_IDX; + + uacpi_write_registers(UACPI_REGISTER_PM1_CNT, pm1a, pm1b); +out: + // Errors ignored intentionally, we don't want to abort because of this + return UACPI_STATUS_OK; +} + +static uacpi_status wake_from_sleep_state_hw_full(uacpi_u8 state) +{ + uacpi_status ret; + g_uacpi_rt_ctx.last_sleep_typ_a = UACPI_SLEEP_TYP_INVALID; + g_uacpi_rt_ctx.last_sleep_typ_b = UACPI_SLEEP_TYP_INVALID; + + // Set the status to 2 (waking) while we execute the wake method. + eval_sst(2); + + ret = uacpi_disable_all_gpes(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_enable_all_runtime_gpes(); + if (uacpi_unlikely_error(ret)) + return ret; + + eval_wak(state); + + // Apparently some BIOSes expect us to clear this, so do it + uacpi_write_register_field( + UACPI_REGISTER_FIELD_WAK_STS, ACPI_PM1_STS_CLEAR + ); + + // Now that we're awake set the status to 1 (running) + eval_sst(1); + + return UACPI_STATUS_OK; +} +#endif + +static uacpi_status get_slp_type_for_state( + uacpi_u8 state, uacpi_u8 *a, uacpi_u8 *b +) +{ + uacpi_char path[] = "_S0"; + uacpi_status ret; + uacpi_object *obj0, *obj1, *ret_obj = UACPI_NULL; + + path[2] += state; + + ret = uacpi_eval_typed( + uacpi_namespace_root(), path, UACPI_NULL, + UACPI_OBJECT_PACKAGE_BIT, &ret_obj + ); + if (ret != UACPI_STATUS_OK) { + if (uacpi_unlikely(ret != UACPI_STATUS_NOT_FOUND)) { + uacpi_warn("error while evaluating %s: %s\n", path, + uacpi_status_to_string(ret)); + } else { + uacpi_trace("sleep state %d is not supported as %s was not found\n", + state, path); + } + goto out; + } + + switch (ret_obj->package->count) { + case 0: + uacpi_error("empty package while evaluating %s!\n", path); + ret = UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + goto out; + + case 1: + obj0 = ret_obj->package->objects[0]; + if (uacpi_unlikely(obj0->type != UACPI_OBJECT_INTEGER)) { + uacpi_error( + "invalid object type at pkg[0] => %s when evaluating %s\n", + uacpi_object_type_to_string(obj0->type), path + ); + goto out; + } + + *a = obj0->integer; + *b = obj0->integer >> 8; + break; + + default: + obj0 = ret_obj->package->objects[0]; + obj1 = ret_obj->package->objects[1]; + + if (uacpi_unlikely(obj0->type != UACPI_OBJECT_INTEGER || + obj1->type != UACPI_OBJECT_INTEGER)) { + uacpi_error( + "invalid object type when evaluating %s: " + "pkg[0] => %s, pkg[1] => %s\n", path, + uacpi_object_type_to_string(obj0->type), + uacpi_object_type_to_string(obj1->type) + ); + ret = UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + goto out; + } + + *a = obj0->integer; + *b = obj1->integer; + break; + } + +out: + if (ret != UACPI_STATUS_OK) { + *a = UACPI_SLEEP_TYP_INVALID; + *b = UACPI_SLEEP_TYP_INVALID; + } + + uacpi_object_unref(ret_obj); + return ret; +} + +static uacpi_status eval_sleep_helper( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_u8 value +) +{ + uacpi_object *arg; + uacpi_object_array args; + uacpi_status ret; + + arg = uacpi_create_object(UACPI_OBJECT_INTEGER); + if (uacpi_unlikely(arg == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + arg->integer = value; + args.objects = &arg; + args.count = 1; + + ret = uacpi_eval(parent, path, &args, UACPI_NULL); + switch (ret) { + case UACPI_STATUS_OK: + break; + case UACPI_STATUS_NOT_FOUND: + ret = UACPI_STATUS_OK; + break; + default: + uacpi_error("error while evaluating %s: %s\n", + path, uacpi_status_to_string(ret)); + break; + } + + uacpi_object_unref(arg); + return ret; +} + +static uacpi_status eval_pts(uacpi_u8 state) +{ + return eval_sleep_helper(uacpi_namespace_root(), "_PTS", state); +} + +static uacpi_status eval_wak(uacpi_u8 state) +{ + return eval_sleep_helper(uacpi_namespace_root(), "_WAK", state); +} + +static uacpi_status eval_sst(uacpi_u8 value) +{ + return eval_sleep_helper( + uacpi_namespace_get_predefined(UACPI_PREDEFINED_NAMESPACE_SI), + "_SST", value + ); +} + +static uacpi_status eval_sst_for_state(enum uacpi_sleep_state state) +{ + uacpi_u8 arg; + + /* + * This optional object is a control method that OSPM invokes to set the + * system status indicator as desired. + * Arguments:(1) + * Arg0 - An Integer containing the system status indicator identifier: + * 0 - No system state indication. Indicator off + * 1 - Working + * 2 - Waking + * 3 - Sleeping. Used to indicate system state S1, S2, or S3 + * 4 - Sleeping with context saved to non-volatile storage + */ + switch (state) { + case UACPI_SLEEP_STATE_S0: + arg = 1; + break; + case UACPI_SLEEP_STATE_S1: + case UACPI_SLEEP_STATE_S2: + case UACPI_SLEEP_STATE_S3: + arg = 3; + break; + case UACPI_SLEEP_STATE_S4: + arg = 4; + break; + case UACPI_SLEEP_STATE_S5: + arg = 0; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return eval_sst(arg); +} + +uacpi_status uacpi_prepare_for_sleep_state(enum uacpi_sleep_state state_enum) +{ + uacpi_u8 state = state_enum; + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED); + + if (uacpi_unlikely(state > UACPI_SLEEP_STATE_S5)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = get_slp_type_for_state( + state, + &g_uacpi_rt_ctx.last_sleep_typ_a, + &g_uacpi_rt_ctx.last_sleep_typ_b + ); + if (ret != UACPI_STATUS_OK) + return ret; + + ret = get_slp_type_for_state( + 0, + &g_uacpi_rt_ctx.s0_sleep_typ_a, + &g_uacpi_rt_ctx.s0_sleep_typ_b + ); + + ret = eval_pts(state); + if (uacpi_unlikely_error(ret)) + return ret; + + eval_sst_for_state(state); + return UACPI_STATUS_OK; +} + +static uacpi_u8 make_hw_reduced_sleep_control(uacpi_u8 slp_typ) +{ + uacpi_u8 value; + + value = (slp_typ << ACPI_SLP_CNT_SLP_TYP_IDX); + value &= ACPI_SLP_CNT_SLP_TYP_MASK; + value |= ACPI_SLP_CNT_SLP_EN_MASK; + + return value; +} + +static uacpi_status enter_sleep_state_hw_reduced(uacpi_u8 state) +{ + uacpi_status ret; + uacpi_u8 sleep_control; + uacpi_u64 wake_status; + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + + if (!fadt->sleep_control_reg.address || !fadt->sleep_status_reg.address) + return UACPI_STATUS_NOT_FOUND; + + ret = uacpi_write_register_field( + UACPI_REGISTER_FIELD_HWR_WAK_STS, + ACPI_SLP_STS_CLEAR + ); + if (uacpi_unlikely_error(ret)) + return ret; + + sleep_control = make_hw_reduced_sleep_control( + g_uacpi_rt_ctx.last_sleep_typ_a + ); + + if (state < UACPI_SLEEP_STATE_S4) + UACPI_ARCH_FLUSH_CPU_CACHE(); + + /* + * To put the system into a sleep state, software will write the HW-reduced + * Sleep Type value (obtained from the \_Sx object in the DSDT) and the + * SLP_EN bit to the sleep control register. + */ + ret = uacpi_write_register(UACPI_REGISTER_SLP_CNT, sleep_control); + if (uacpi_unlikely_error(ret)) + return ret; + + /* + * The OSPM then polls the WAK_STS bit of the SLEEP_STATUS_REG waiting for + * it to be one (1), indicating that the system has been transitioned + * back to the Working state. + */ + do { + ret = uacpi_read_register_field( + UACPI_REGISTER_FIELD_HWR_WAK_STS, &wake_status + ); + if (uacpi_unlikely_error(ret)) + return ret; + } while (wake_status != 1); + + return UACPI_STATUS_OK; +} + +static uacpi_status prepare_for_wake_from_sleep_state_hw_reduced(uacpi_u8 state) +{ + uacpi_u8 sleep_control; + UACPI_UNUSED(state); + + if (g_uacpi_rt_ctx.s0_sleep_typ_a == UACPI_SLEEP_TYP_INVALID) + goto out; + + sleep_control = make_hw_reduced_sleep_control( + g_uacpi_rt_ctx.s0_sleep_typ_a + ); + uacpi_write_register(UACPI_REGISTER_SLP_CNT, sleep_control); + +out: + return UACPI_STATUS_OK; +} + +static uacpi_status wake_from_sleep_state_hw_reduced(uacpi_u8 state) +{ + g_uacpi_rt_ctx.last_sleep_typ_a = UACPI_SLEEP_TYP_INVALID; + g_uacpi_rt_ctx.last_sleep_typ_b = UACPI_SLEEP_TYP_INVALID; + + // Set the status to 2 (waking) while we execute the wake method. + eval_sst(2); + + eval_wak(state); + + // Apparently some BIOSes expect us to clear this, so do it + uacpi_write_register_field( + UACPI_REGISTER_FIELD_HWR_WAK_STS, ACPI_SLP_STS_CLEAR + ); + + // Now that we're awake set the status to 1 (running) + eval_sst(1); + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_enter_sleep_state(enum uacpi_sleep_state state_enum) +{ + uacpi_u8 state = state_enum; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED); + + if (uacpi_unlikely(state > UACPI_SLEEP_STATE_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (uacpi_unlikely(g_uacpi_rt_ctx.last_sleep_typ_a > ACPI_SLP_TYP_MAX || + g_uacpi_rt_ctx.last_sleep_typ_b > ACPI_SLP_TYP_MAX)) { + uacpi_error("invalid SLP_TYP values: 0x%02X:0x%02X\n", + g_uacpi_rt_ctx.last_sleep_typ_a, + g_uacpi_rt_ctx.last_sleep_typ_b); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + return CALL_SLEEP_FN(enter_sleep_state, state); +} + +uacpi_status uacpi_prepare_for_wake_from_sleep_state( + uacpi_sleep_state state_enum +) +{ + uacpi_u8 state = state_enum; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED); + + if (uacpi_unlikely(state > UACPI_SLEEP_STATE_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + + return CALL_SLEEP_FN(prepare_for_wake_from_sleep_state, state); +} + +uacpi_status uacpi_wake_from_sleep_state( + uacpi_sleep_state state_enum +) +{ + uacpi_u8 state = state_enum; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED); + + if (uacpi_unlikely(state > UACPI_SLEEP_STATE_MAX)) + return UACPI_STATUS_INVALID_ARGUMENT; + + return CALL_SLEEP_FN(wake_from_sleep_state, state); +} + +uacpi_status uacpi_reboot(void) +{ + uacpi_status ret; + uacpi_handle pci_dev = UACPI_NULL; + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + struct acpi_gas *reset_reg = &fadt->reset_reg; + + /* + * Allow restarting earlier than namespace load so that the kernel can + * use this in case of some initialization error. + */ + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (!(fadt->flags & ACPI_RESET_REG_SUP) || !reset_reg->address) + return UACPI_STATUS_NOT_FOUND; + + switch (reset_reg->address_space_id) { + case UACPI_ADDRESS_SPACE_SYSTEM_IO: + /* + * For SystemIO we don't do any checking, and we ignore bit width + * because that's what NT does. + */ + ret = uacpi_system_io_write(reset_reg->address, 1, fadt->reset_value); + break; + case UACPI_ADDRESS_SPACE_SYSTEM_MEMORY: + ret = uacpi_write_register(UACPI_REGISTER_RESET, fadt->reset_value); + break; + case UACPI_ADDRESS_SPACE_PCI_CONFIG: { + // Bus is assumed to be 0 here + uacpi_pci_address address = { + .segment = 0, + .bus = 0, + .device = (reset_reg->address >> 32) & 0xFF, + .function = (reset_reg->address >> 16) & 0xFF, + }; + + ret = uacpi_kernel_pci_device_open(address, &pci_dev); + if (uacpi_unlikely_error(ret)) + break; + + ret = uacpi_kernel_pci_write( + pci_dev, reset_reg->address & 0xFFFF, 1, fadt->reset_value + ); + break; + } + default: + uacpi_warn( + "unable to perform a reset: unsupported address space '%s' (%d)\n", + uacpi_address_space_to_string(reset_reg->address_space_id), + reset_reg->address_space_id + ); + ret = UACPI_STATUS_UNIMPLEMENTED; + } + + if (ret == UACPI_STATUS_OK) { + /* + * This should've worked but we're still here. + * Spin for a bit then give up. + */ + uacpi_u64 stalled_time = 0; + + while (stalled_time < (1000 * 1000)) { + uacpi_kernel_stall(100); + stalled_time += 100; + } + + uacpi_error("reset timeout\n"); + ret = UACPI_STATUS_HARDWARE_TIMEOUT; + } + + if (pci_dev != UACPI_NULL) + uacpi_kernel_pci_device_close(pci_dev); + + return ret; +} diff --git a/src/uacpi/stdlib.c b/src/uacpi/stdlib.c new file mode 100644 index 0000000..c607e1b --- /dev/null +++ b/src/uacpi/stdlib.c @@ -0,0 +1,711 @@ +#include +#include +#include + +#ifndef uacpi_memcpy +void *uacpi_memcpy(void *dest, const void *src, size_t count) +{ + uacpi_char *cd = dest; + const uacpi_char *cs = src; + + while (count--) + *cd++ = *cs++; + + return dest; +} +#endif + +#ifndef uacpi_memmove +void *uacpi_memmove(void *dest, const void *src, uacpi_size count) +{ + uacpi_char *cd = dest; + const uacpi_char *cs = src; + + if (src < dest) { + cs += count; + cd += count; + + while (count--) + *--cd = *--cs; + } else { + while (count--) + *cd++ = *cs++; + } + + return dest; +} +#endif + +#ifndef uacpi_memset +void *uacpi_memset(void *dest, uacpi_i32 ch, uacpi_size count) +{ + uacpi_u8 fill = ch; + uacpi_u8 *cdest = dest; + + while (count--) + *cdest++ = fill; + + return dest; +} +#endif + +#ifndef uacpi_memcmp +uacpi_i32 uacpi_memcmp(const void *lhs, const void *rhs, uacpi_size count) +{ + const uacpi_u8 *byte_lhs = lhs; + const uacpi_u8 *byte_rhs = rhs; + uacpi_size i; + + for (i = 0; i < count; ++i) { + if (byte_lhs[i] != byte_rhs[i]) + return byte_lhs[i] - byte_rhs[i]; + } + + return 0; +} +#endif + +#ifndef uacpi_strlen +uacpi_size uacpi_strlen(const uacpi_char *str) +{ + const uacpi_char *str1; + + for (str1 = str; *str1; str1++); + + return str1 - str; +} +#endif + +#ifndef uacpi_strnlen +uacpi_size uacpi_strnlen(const uacpi_char *str, uacpi_size max) +{ + const uacpi_char *str1; + + for (str1 = str; max-- && *str1; str1++); + + return str1 - str; +} +#endif + +#ifndef uacpi_strcmp +uacpi_i32 uacpi_strcmp(const uacpi_char *lhs, const uacpi_char *rhs) +{ + uacpi_size i = 0; + typedef const uacpi_u8 *cucp; + + while (lhs[i] && rhs[i]) { + if (lhs[i] != rhs[i]) + return *(cucp)&lhs[i] - *(cucp)&rhs[i]; + + i++; + } + + return *(cucp)&lhs[i] - *(cucp)&rhs[i]; +} +#endif + +#ifndef uacpi_vsnprintf +struct fmt_buf_state { + uacpi_char *buffer; + uacpi_size capacity; + uacpi_size bytes_written; +}; + +struct fmt_spec { + uacpi_u8 is_signed : 1; + uacpi_u8 prepend : 1; + uacpi_u8 uppercase : 1; + uacpi_u8 left_justify : 1; + uacpi_u8 alternate_form : 1; + uacpi_u8 has_precision : 1; + uacpi_char pad_char; + uacpi_char prepend_char; + uacpi_u64 min_width; + uacpi_u64 precision; + uacpi_u32 base; +}; + +static void write_one(struct fmt_buf_state *fb_state, uacpi_char c) +{ + if (fb_state->bytes_written < fb_state->capacity) + fb_state->buffer[fb_state->bytes_written] = c; + + fb_state->bytes_written++; +} + +static void write_many( + struct fmt_buf_state *fb_state, const uacpi_char *string, uacpi_size count +) +{ + if (fb_state->bytes_written < fb_state->capacity) { + uacpi_size count_to_write; + + count_to_write = UACPI_MIN( + count, fb_state->capacity - fb_state->bytes_written + ); + uacpi_memcpy( + &fb_state->buffer[fb_state->bytes_written], string, count_to_write + ); + } + + fb_state->bytes_written += count; +} + +static uacpi_char hex_char(uacpi_bool upper, uacpi_u64 value) +{ + static const uacpi_char upper_hex[] = "0123456789ABCDEF"; + static const uacpi_char lower_hex[] = "0123456789abcdef"; + + return (upper ? upper_hex : lower_hex)[value]; +} + +static void write_padding( + struct fmt_buf_state *fb_state, struct fmt_spec *fm, uacpi_size repr_size +) +{ + uacpi_u64 mw = fm->min_width; + + if (mw <= repr_size) + return; + + mw -= repr_size; + + while (mw--) + write_one(fb_state, fm->left_justify ? ' ' : fm->pad_char); +} + +#define REPR_BUFFER_SIZE 32 + +static void write_integer( + struct fmt_buf_state *fb_state, struct fmt_spec *fm, uacpi_u64 value +) +{ + uacpi_char repr_buffer[REPR_BUFFER_SIZE]; + uacpi_size index = REPR_BUFFER_SIZE; + uacpi_u64 remainder; + uacpi_char repr; + uacpi_bool negative = UACPI_FALSE; + uacpi_size repr_size; + + if (fm->is_signed) { + uacpi_i64 as_ll = value; + + if (as_ll < 0) { + value = -as_ll; + negative = UACPI_TRUE; + } + } + + if (fm->prepend || negative) + write_one(fb_state, negative ? '-' : fm->prepend_char); + + while (value) { + remainder = value % fm->base; + value /= fm->base; + + if (fm->base == 16) { + repr = hex_char(fm->uppercase, remainder); + } else if (fm->base == 8 || fm->base == 10) { + repr = remainder + '0'; + } else { + repr = '?'; + } + + repr_buffer[--index] = repr; + } + repr_size = REPR_BUFFER_SIZE - index; + + if (repr_size == 0) { + repr_buffer[--index] = '0'; + repr_size = 1; + } + + if (fm->alternate_form) { + if (fm->base == 16) { + repr_buffer[--index] = fm->uppercase ? 'X' : 'x'; + repr_buffer[--index] = '0'; + repr_size += 2; + } else if (fm->base == 8) { + repr_buffer[--index] = '0'; + repr_size += 1; + } + } + + if (fm->left_justify) { + write_many(fb_state, &repr_buffer[index], repr_size); + write_padding(fb_state, fm, repr_size); + } else { + write_padding(fb_state, fm, repr_size); + write_many(fb_state, &repr_buffer[index], repr_size); + } +} + +static uacpi_bool string_has_at_least( + const uacpi_char *string, uacpi_size characters +) +{ + while (*string) { + if (--characters == 0) + return UACPI_TRUE; + + string++; + } + + return UACPI_FALSE; +} + +static uacpi_bool consume_digits( + const uacpi_char **string, uacpi_size *out_size +) +{ + uacpi_size size = 0; + + for (;;) { + char c = **string; + if (c < '0' || c > '9') + break; + + size++; + *string += 1; + } + + if (size == 0) + return UACPI_FALSE; + + *out_size = size; + return UACPI_TRUE; +} + +enum parse_number_mode { + PARSE_NUMBER_MODE_MAYBE, + PARSE_NUMBER_MODE_MUST, +}; + +static uacpi_bool parse_number( + const uacpi_char **fmt, enum parse_number_mode mode, uacpi_u64 *out_value +) +{ + uacpi_status ret; + uacpi_size num_digits; + const uacpi_char *digits = *fmt; + + if (!consume_digits(fmt, &num_digits)) + return mode != PARSE_NUMBER_MODE_MUST; + + ret = uacpi_string_to_integer(digits, num_digits, UACPI_BASE_DEC, out_value); + return ret == UACPI_STATUS_OK; +} + +static uacpi_bool consume(const uacpi_char **string, const uacpi_char *token) +{ + uacpi_size token_size; + + token_size = uacpi_strlen(token); + + if (!string_has_at_least(*string, token_size)) + return UACPI_FALSE; + + if (!uacpi_memcmp(*string, token, token_size)) { + *string += token_size; + return UACPI_TRUE; + } + + return UACPI_FALSE; +} + +static uacpi_bool is_one_of(uacpi_char c, const uacpi_char *list) +{ + for (; *list; list++) { + if (c == *list) + return UACPI_TRUE; + } + + return UACPI_FALSE; +} + +static uacpi_bool consume_one_of( + const uacpi_char **string, const uacpi_char *list, uacpi_char *consumed_char +) +{ + uacpi_char c = **string; + if (!c) + return UACPI_FALSE; + + if (is_one_of(c, list)) { + *consumed_char = c; + *string += 1; + return UACPI_TRUE; + } + + return UACPI_FALSE; +} + +static uacpi_u32 base_from_specifier(uacpi_char specifier) +{ + switch (specifier) + { + case 'x': + case 'X': + return 16; + case 'o': + return 8; + default: + return 10; + } +} + +static uacpi_bool is_uppercase_specifier(uacpi_char specifier) +{ + return specifier == 'X'; +} + +static const uacpi_char *find_next_conversion( + const uacpi_char *fmt, uacpi_size *offset +) +{ + *offset = 0; + + while (*fmt) { + if (*fmt == '%') + return fmt; + + fmt++; + *offset += 1; + } + + return UACPI_NULL; +} + +uacpi_i32 uacpi_vsnprintf( + uacpi_char *buffer, uacpi_size capacity, const uacpi_char *fmt, + uacpi_va_list vlist +) +{ + struct fmt_buf_state fb_state = { + .buffer = buffer, + .capacity = capacity, + .bytes_written = 0 + }; + + uacpi_u64 value; + const uacpi_char *next_conversion; + uacpi_size next_offset; + uacpi_char flag; + + while (*fmt) { + struct fmt_spec fm = { + .pad_char = ' ', + .base = 10, + }; + next_conversion = find_next_conversion(fmt, &next_offset); + + if (next_offset) + write_many(&fb_state, fmt, next_offset); + + if (!next_conversion) + break; + + fmt = next_conversion; + if (consume(&fmt, "%%")) { + write_one(&fb_state, '%'); + continue; + } + + // consume % + fmt++; + + while (consume_one_of(&fmt, "+- 0#", &flag)) { + switch (flag) { + case '+': + case ' ': + fm.prepend = UACPI_TRUE; + fm.prepend_char = flag; + continue; + case '-': + fm.left_justify = UACPI_TRUE; + continue; + case '0': + fm.pad_char = '0'; + continue; + case '#': + fm.alternate_form = UACPI_TRUE; + continue; + default: + return -1; + } + } + + if (consume(&fmt, "*")) { + fm.min_width = uacpi_va_arg(vlist, int); + } else if (!parse_number(&fmt, PARSE_NUMBER_MODE_MAYBE, &fm.min_width)) { + return -1; + } + + if (consume(&fmt, ".")) { + fm.has_precision = UACPI_TRUE; + + if (consume(&fmt, "*")) { + fm.precision = uacpi_va_arg(vlist, int); + } else { + if (!parse_number(&fmt, PARSE_NUMBER_MODE_MUST, &fm.precision)) + return -1; + } + } + + flag = 0; + + if (consume(&fmt, "c")) { + uacpi_char c = uacpi_va_arg(vlist, int); + write_one(&fb_state, c); + continue; + } + + if (consume(&fmt, "s")) { + const uacpi_char *string = uacpi_va_arg(vlist, uacpi_char*); + uacpi_size i; + + for (i = 0; (!fm.has_precision || i < fm.precision) && string[i]; ++i) + write_one(&fb_state, string[i]); + while (fm.has_precision && (i++ < fm.precision)) + write_one(&fb_state, fm.pad_char); + continue; + } + + if (consume(&fmt, "p")) { + value = (uacpi_uintptr)uacpi_va_arg(vlist, void*); + fm.base = 16; + fm.min_width = UACPI_POINTER_SIZE * 2; + fm.pad_char = '0'; + goto write_int; + } + + if (consume(&fmt, "hh")) { + if (consume(&fmt, "d") || consume(&fmt, "i")) { + value = (signed char)uacpi_va_arg(vlist, int); + fm.is_signed = UACPI_TRUE; + } else if (consume_one_of(&fmt, "oxXu", &flag)) { + value = (unsigned char)uacpi_va_arg(vlist, int); + } else { + return -1; + } + goto write_int; + } + + if (consume(&fmt, "h")) { + if (consume(&fmt, "d") || consume(&fmt, "i")) { + value = (signed short)uacpi_va_arg(vlist, int); + fm.is_signed = UACPI_TRUE; + } else if (consume_one_of(&fmt, "oxXu", &flag)) { + value = (unsigned short)uacpi_va_arg(vlist, int); + } else { + return -1; + } + goto write_int; + } + + if (consume(&fmt, "ll") || + (sizeof(uacpi_size) == sizeof(long long) && consume(&fmt, "z"))) { + if (consume(&fmt, "d") || consume(&fmt, "i")) { + value = uacpi_va_arg(vlist, long long); + fm.is_signed = UACPI_TRUE; + } else if (consume_one_of(&fmt, "oxXu", &flag)) { + value = uacpi_va_arg(vlist, unsigned long long); + } else { + return -1; + } + goto write_int; + } + + if (consume(&fmt, "l") || + (sizeof(uacpi_size) == sizeof(long) && consume(&fmt, "z"))) { + if (consume(&fmt, "d") || consume(&fmt, "i")) { + value = uacpi_va_arg(vlist, long); + fm.is_signed = UACPI_TRUE; + } else if (consume_one_of(&fmt, "oxXu", &flag)) { + value = uacpi_va_arg(vlist, unsigned long); + } else { + return -1; + } + goto write_int; + } + + if (consume(&fmt, "d") || consume(&fmt, "i")) { + value = uacpi_va_arg(vlist, uacpi_i32); + fm.is_signed = UACPI_TRUE; + } else if (consume_one_of(&fmt, "oxXu", &flag)) { + value = uacpi_va_arg(vlist, uacpi_u32); + } else { + return -1; + } + + write_int: + if (flag != 0) { + fm.base = base_from_specifier(flag); + fm.uppercase = is_uppercase_specifier(flag); + } + + write_integer(&fb_state, &fm, value); + } + + if (fb_state.capacity) { + uacpi_size last_char; + + last_char = UACPI_MIN(fb_state.bytes_written, fb_state.capacity - 1); + fb_state.buffer[last_char] = '\0'; + } + + return fb_state.bytes_written; +} +#endif + +#ifndef uacpi_snprintf +uacpi_i32 uacpi_snprintf( + uacpi_char *buffer, uacpi_size capacity, const uacpi_char *fmt, ... +) +{ + uacpi_va_list vlist; + uacpi_i32 ret; + + uacpi_va_start(vlist, fmt); + ret = uacpi_vsnprintf(buffer, capacity, fmt, vlist); + uacpi_va_end(vlist); + + return ret; +} +#endif + +void uacpi_memcpy_zerout(void *dst, const void *src, + uacpi_size dst_size, uacpi_size src_size) +{ + uacpi_size bytes_to_copy = UACPI_MIN(src_size, dst_size); + + if (bytes_to_copy) + uacpi_memcpy(dst, src, bytes_to_copy); + + if (dst_size > bytes_to_copy) + uacpi_memzero((uacpi_u8*)dst + bytes_to_copy, dst_size - bytes_to_copy); +} + +uacpi_u8 uacpi_bit_scan_forward(uacpi_u64 value) +{ +#ifdef _MSC_VER + unsigned char ret; + unsigned long index; + +#ifdef _WIN64 + ret = _BitScanForward64(&index, value); + if (ret == 0) + return 0; + + return (uacpi_u8)index + 1; +#else + ret = _BitScanForward(&index, value); + if (ret == 0) { + ret = _BitScanForward(&index, value >> 32); + if (ret == 0) + return 0; + + return (uacpi_u8)index + 33; + } + + return (uacpi_u8)index + 1; +#endif + +#else + return __builtin_ffsll(value); +#endif +} + +uacpi_u8 uacpi_bit_scan_backward(uacpi_u64 value) +{ +#ifdef _MSC_VER + unsigned char ret; + unsigned long index; + +#ifdef _WIN64 + ret = _BitScanReverse64(&index, value); + if (ret == 0) + return 0; + + return (uacpi_u8)index + 1; +#else + ret = _BitScanReverse(&index, value >> 32); + if (ret == 0) { + ret = _BitScanReverse(&index, value); + if (ret == 0) + return 0; + + return (uacpi_u8)index + 1; + } + + return (uacpi_u8)index + 33; +#endif + +#else + if (value == 0) + return 0; + + return 64 - __builtin_clzll(value); +#endif +} + +uacpi_u8 uacpi_popcount(uacpi_u64 value) +{ +#ifdef _MSC_VER + +#ifdef _WIN64 + return __popcnt64(value); +#else + return __popcnt(value) + __popcnt(value >> 32); +#endif + +#else + return __builtin_popcountll(value); +#endif +} + +#ifndef UACPI_FORMATTED_LOGGING + +void uacpi_log(uacpi_log_level lvl, const uacpi_char *str, ...) +{ + uacpi_char buf[UACPI_PLAIN_LOG_BUFFER_SIZE]; + int ret; + + uacpi_va_list vlist; + uacpi_va_start(vlist, str); + + ret = uacpi_vsnprintf(buf, sizeof(buf), str, vlist); + if (uacpi_unlikely(ret < 0)) + return; + + /* + * If this log message is too large for the configured buffer size, cut off + * the end and transform into "...\n" to indicate that it didn't fit and + * prevent the newline from being truncated. + */ + if (uacpi_unlikely(ret >= UACPI_PLAIN_LOG_BUFFER_SIZE)) { + buf[UACPI_PLAIN_LOG_BUFFER_SIZE - 5] = '.'; + buf[UACPI_PLAIN_LOG_BUFFER_SIZE - 4] = '.'; + buf[UACPI_PLAIN_LOG_BUFFER_SIZE - 3] = '.'; + buf[UACPI_PLAIN_LOG_BUFFER_SIZE - 2] = '\n'; + } + + uacpi_kernel_log(lvl, buf); + + uacpi_va_end(vlist); +} +#endif + +#ifndef UACPI_NATIVE_ALLOC_ZEROED +void *uacpi_builtin_alloc_zeroed(uacpi_size size) +{ + void *ptr; + + ptr = uacpi_kernel_alloc(size); + if (uacpi_unlikely(ptr == UACPI_NULL)) + return ptr; + + uacpi_memzero(ptr, size); + return ptr; +} +#endif diff --git a/src/uacpi/tables.c b/src/uacpi/tables.c new file mode 100644 index 0000000..f5a178f --- /dev/null +++ b/src/uacpi/tables.c @@ -0,0 +1,1350 @@ +#include +#include +#include +#include +#include +#include + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE( + table_array, struct uacpi_installed_table, UACPI_STATIC_TABLE_ARRAY_LEN +) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + table_array, struct uacpi_installed_table, static +) + +static struct table_array tables; +static uacpi_bool early_table_access; +static uacpi_table_installation_handler installation_handler; +static uacpi_handle table_mutex; + +static uacpi_status table_install_physical_with_origin_unlocked( + uacpi_phys_addr phys, enum uacpi_table_origin origin, + const uacpi_char *expected_signature, uacpi_table *out_table +); +static uacpi_status table_install_with_origin_unlocked( + void *virt, enum uacpi_table_origin origin, uacpi_table *out_table +); + +UACPI_PACKED(struct uacpi_rxsdt { + struct acpi_sdt_hdr hdr; + uacpi_u8 ptr_bytes[]; +}) + +static void dump_table_header( + uacpi_phys_addr phys_addr, void *hdr +) +{ + struct acpi_sdt_hdr *sdt = hdr; + + if (uacpi_signatures_match(hdr, ACPI_FACS_SIGNATURE)) { + uacpi_info( + "FACS 0x%016"UACPI_PRIX64" %08X\n", UACPI_FMT64(phys_addr), + sdt->length + ); + return; + } + + if (!uacpi_memcmp(hdr, ACPI_RSDP_SIGNATURE, sizeof(ACPI_RSDP_SIGNATURE) - 1)) { + struct acpi_rsdp *rsdp = hdr; + + uacpi_info( + "RSDP 0x%016"UACPI_PRIX64" %08X v%02X (%.6s)\n", + UACPI_FMT64(phys_addr), rsdp->length, rsdp->revision, + rsdp->oemid + ); + return; + } + + uacpi_info( + "%.4s 0x%016"UACPI_PRIX64" %08X v%02X (%.6s %.8s)\n", + sdt->signature, UACPI_FMT64(phys_addr), sdt->length, sdt->revision, + sdt->oemid, sdt->oem_table_id + ); +} + +static uacpi_status initialize_from_rxsdt(uacpi_phys_addr rxsdt_addr, + uacpi_size entry_size) +{ + struct uacpi_rxsdt *rxsdt; + uacpi_size i, entry_bytes, map_len = sizeof(*rxsdt); + uacpi_phys_addr entry_addr; + uacpi_status ret; + + rxsdt = uacpi_kernel_map(rxsdt_addr, map_len); + if (rxsdt == UACPI_NULL) + return UACPI_STATUS_MAPPING_FAILED; + + dump_table_header(rxsdt_addr, rxsdt); + + ret = uacpi_check_table_signature(rxsdt, + entry_size == 8 ? ACPI_XSDT_SIGNATURE : ACPI_RSDT_SIGNATURE); + if (uacpi_unlikely_error(ret)) + goto error_out; + + map_len = rxsdt->hdr.length; + uacpi_kernel_unmap(rxsdt, sizeof(*rxsdt)); + + if (uacpi_unlikely(map_len < (sizeof(*rxsdt) + entry_size))) + return UACPI_STATUS_INVALID_TABLE_LENGTH; + + // Make sure length is aligned to entry size so we don't OOB + entry_bytes = map_len - sizeof(*rxsdt); + entry_bytes &= ~(entry_size - 1); + + rxsdt = uacpi_kernel_map(rxsdt_addr, map_len); + if (uacpi_unlikely(rxsdt == UACPI_NULL)) + return UACPI_STATUS_MAPPING_FAILED; + + ret = uacpi_verify_table_checksum(rxsdt, map_len); + if (uacpi_unlikely_error(ret)) + goto error_out; + + for (i = 0; i < entry_bytes; i += entry_size) { + uacpi_u64 entry_phys_addr_large = 0; + uacpi_memcpy(&entry_phys_addr_large, &rxsdt->ptr_bytes[i], entry_size); + + if (!entry_phys_addr_large) + continue; + + entry_addr = uacpi_truncate_phys_addr_with_warn(entry_phys_addr_large); + ret = uacpi_table_install_physical_with_origin( + entry_addr, UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL, UACPI_NULL + ); + if (uacpi_unlikely(ret != UACPI_STATUS_OK && + ret != UACPI_STATUS_OVERRIDDEN)) + goto error_out; + } + + ret = UACPI_STATUS_OK; + +error_out: + uacpi_kernel_unmap(rxsdt, map_len); + return ret; +} + +static uacpi_status initialize_from_rsdp(void) +{ + uacpi_status ret; + uacpi_phys_addr rsdp_phys; + struct acpi_rsdp *rsdp; + uacpi_phys_addr rxsdt; + uacpi_size rxsdt_entry_size; + + g_uacpi_rt_ctx.is_rev1 = UACPI_TRUE; + + ret = uacpi_kernel_get_rsdp(&rsdp_phys); + if (uacpi_unlikely_error(ret)) + return ret; + + rsdp = uacpi_kernel_map(rsdp_phys, sizeof(struct acpi_rsdp)); + if (rsdp == UACPI_NULL) + return UACPI_STATUS_MAPPING_FAILED; + + dump_table_header(rsdp_phys, rsdp); + + if (rsdp->revision > 1 && rsdp->xsdt_addr && + !uacpi_check_flag(UACPI_FLAG_BAD_XSDT)) + { + rxsdt = uacpi_truncate_phys_addr_with_warn(rsdp->xsdt_addr); + rxsdt_entry_size = 8; + } else { + rxsdt = (uacpi_phys_addr)rsdp->rsdt_addr; + rxsdt_entry_size = 4; + } + + uacpi_kernel_unmap(rsdp, sizeof(struct acpi_rsdp)); + + if (!rxsdt) { + uacpi_error("both RSDT & XSDT tables are NULL!\n"); + return UACPI_STATUS_INVALID_ARGUMENT; + } + + return initialize_from_rxsdt(rxsdt, rxsdt_entry_size); +} + +uacpi_status uacpi_setup_early_table_access( + void *temporary_buffer, uacpi_size buffer_size +) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_IS(UACPI_INIT_LEVEL_EARLY); + if (uacpi_unlikely(early_table_access)) + return UACPI_STATUS_INIT_LEVEL_MISMATCH; + + if (uacpi_unlikely(buffer_size < sizeof(struct uacpi_installed_table))) + return UACPI_STATUS_INVALID_ARGUMENT; + + uacpi_logger_initialize(); + + tables.dynamic_storage = temporary_buffer; + tables.dynamic_capacity = buffer_size / sizeof(struct uacpi_installed_table); + early_table_access = UACPI_TRUE; + + ret = initialize_from_rsdp(); + if (uacpi_unlikely_error(ret)) + uacpi_deinitialize_tables(); + + return ret; +} + +static uacpi_iteration_decision warn_if_early_referenced( + void *user, struct uacpi_installed_table *tbl, uacpi_size idx +) +{ + UACPI_UNUSED(user); + + if (uacpi_unlikely(tbl->reference_count != 0)) { + uacpi_warn( + "table "UACPI_PRI_TBL_HDR" (%zu) still has %d early reference(s)!\n", + UACPI_FMT_TBL_HDR(&tbl->hdr), idx, tbl->reference_count + ); + } + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +uacpi_status uacpi_initialize_tables(void) +{ + if (early_table_access) { + uacpi_size num_tables; + + uacpi_for_each_table(0, warn_if_early_referenced, UACPI_NULL); + + // Reallocate the user buffer into a normal heap array + num_tables = table_array_size(&tables); + if (num_tables > table_array_inline_capacity(&tables)) { + void *new_buf; + + /* + * Allocate a new buffer with size equal to exactly the number of + * dynamic tables (that live in the user provided temporary buffer). + */ + num_tables -= table_array_inline_capacity(&tables); + new_buf = uacpi_kernel_alloc( + sizeof(struct uacpi_installed_table) * num_tables + ); + if (uacpi_unlikely(new_buf == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy(new_buf, tables.dynamic_storage, + sizeof(struct uacpi_installed_table) * num_tables); + tables.dynamic_storage = new_buf; + tables.dynamic_capacity = num_tables; + } else { + /* + * User-provided temporary buffer was not used at all, just remove + * any references to it. + */ + tables.dynamic_storage = UACPI_NULL; + tables.dynamic_capacity = 0; + } + + early_table_access = UACPI_FALSE; + } else { + uacpi_status ret; + + ret = initialize_from_rsdp(); + if (uacpi_unlikely_error(ret)) + return ret; + } + + if (!uacpi_is_hardware_reduced()) { + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + uacpi_table tbl; + + if (fadt->x_firmware_ctrl) { + uacpi_status ret; + + ret = table_install_physical_with_origin_unlocked( + fadt->x_firmware_ctrl, UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL, + ACPI_FACS_SIGNATURE, &tbl + ); + if (uacpi_unlikely(ret != UACPI_STATUS_OK && + ret != UACPI_STATUS_OVERRIDDEN)) + return ret; + + g_uacpi_rt_ctx.facs = tbl.ptr; + } + } + + table_mutex = uacpi_kernel_create_mutex(); + if (uacpi_unlikely(table_mutex == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + return UACPI_STATUS_OK; +} + +void uacpi_deinitialize_tables(void) +{ + uacpi_size i; + + for (i = 0; i < table_array_size(&tables); ++i) { + struct uacpi_installed_table *tbl = table_array_at(&tables, i); + + switch (tbl->origin) { + case UACPI_TABLE_ORIGIN_FIRMWARE_VIRTUAL: + uacpi_free(tbl->ptr, tbl->hdr.length); + break; + case UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL: + case UACPI_TABLE_ORIGIN_HOST_PHYSICAL: + if (tbl->reference_count != 0) + uacpi_kernel_unmap(tbl->ptr, tbl->hdr.length); + break; + default: + break; + } + } + + if (early_table_access) { + uacpi_memzero(&tables, sizeof(tables)); + early_table_access = UACPI_FALSE; + } else { + table_array_clear(&tables); + } + + if (table_mutex) + uacpi_kernel_free_mutex(table_mutex); + + installation_handler = UACPI_NULL; + table_mutex = UACPI_NULL; +} + +uacpi_status uacpi_set_table_installation_handler( + uacpi_table_installation_handler handler +) +{ + uacpi_status ret; + + ret = uacpi_acquire_native_mutex_may_be_null(table_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + if (installation_handler != UACPI_NULL && handler != UACPI_NULL) + goto out; + + installation_handler = handler; + +out: + uacpi_release_native_mutex_may_be_null(table_mutex); + return ret; +} + +static uacpi_status initialize_fadt(const void*); + +static uacpi_u8 table_checksum(void *table, uacpi_size size) +{ + uacpi_u8 *bytes = table; + uacpi_u8 csum = 0; + uacpi_size i; + + for (i = 0; i < size; ++i) + csum += bytes[i]; + + return csum; +} + +uacpi_status uacpi_verify_table_checksum(void *table, uacpi_size size) +{ + uacpi_status ret = UACPI_STATUS_OK; + uacpi_u8 csum; + + csum = table_checksum(table, size); + + if (uacpi_unlikely(csum != 0)) { + enum uacpi_log_level lvl = UACPI_LOG_WARN; + struct acpi_sdt_hdr *hdr = table; + + if (uacpi_check_flag(UACPI_FLAG_BAD_CSUM_FATAL)) { + ret = UACPI_STATUS_BAD_CHECKSUM; + lvl = UACPI_LOG_ERROR; + } + + uacpi_log_lvl( + lvl, "invalid table "UACPI_PRI_TBL_HDR" checksum %d!\n", + UACPI_FMT_TBL_HDR(hdr), csum + ); + } + + return ret; +} + +uacpi_bool uacpi_signatures_match(const void *const lhs, const void *const rhs) +{ + return uacpi_memcmp(lhs, rhs, sizeof(uacpi_object_name)) == 0; +} + +uacpi_status uacpi_check_table_signature(void *table, const uacpi_char *expect) +{ + uacpi_status ret = UACPI_STATUS_OK; + + if (!uacpi_signatures_match(table, expect)) { + enum uacpi_log_level lvl = UACPI_LOG_WARN; + struct acpi_sdt_hdr *hdr = table; + + if (uacpi_check_flag(UACPI_FLAG_BAD_TBL_SIGNATURE_FATAL)) { + ret = UACPI_STATUS_INVALID_SIGNATURE; + lvl = UACPI_LOG_ERROR; + } + + uacpi_log_lvl( + lvl, + "invalid table "UACPI_PRI_TBL_HDR" signature (expected '%.4s')\n", + UACPI_FMT_TBL_HDR(hdr), expect + ); + } + + return ret; +} + +static uacpi_status table_alloc( + struct uacpi_installed_table **out_tbl, uacpi_size *out_idx +) +{ + struct uacpi_installed_table *tbl; + + if (early_table_access && + table_array_size(&tables) == table_array_capacity(&tables)) { + uacpi_warn("early table access buffer capacity exhausted!\n"); + return UACPI_STATUS_OUT_OF_MEMORY; + } + + tbl = table_array_alloc(&tables); + if (uacpi_unlikely(tbl == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + *out_tbl = tbl; + *out_idx = table_array_size(&tables) - 1; + return UACPI_STATUS_OK; +} + +static uacpi_status get_external_table_header( + uacpi_phys_addr phys_addr, struct acpi_sdt_hdr *out_hdr +) +{ + void *virt; + + virt = uacpi_kernel_map(phys_addr, sizeof(*out_hdr)); + if (uacpi_unlikely(virt == UACPI_NULL)) + return UACPI_STATUS_MAPPING_FAILED; + + uacpi_memcpy(out_hdr, virt, sizeof(*out_hdr)); + + uacpi_kernel_unmap(virt, sizeof(*out_hdr)); + return UACPI_STATUS_OK; +} + +static uacpi_status table_ref_unlocked(struct uacpi_installed_table *tbl) +{ + switch (tbl->reference_count) { + case 0: { + uacpi_status ret; + + if (tbl->flags & UACPI_TABLE_INVALID) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (tbl->origin != UACPI_TABLE_ORIGIN_HOST_PHYSICAL && + tbl->origin != UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL) + break; + + tbl->ptr = uacpi_kernel_map(tbl->phys_addr, tbl->hdr.length); + if (uacpi_unlikely(tbl->ptr == UACPI_NULL)) + return UACPI_STATUS_MAPPING_FAILED; + + if (!(tbl->flags & UACPI_TABLE_CSUM_VERIFIED)) { + ret = uacpi_verify_table_checksum(tbl->ptr, tbl->hdr.length); + if (uacpi_unlikely_error(ret)) { + uacpi_kernel_unmap(tbl->ptr, tbl->hdr.length); + tbl->flags |= UACPI_TABLE_INVALID; + tbl->ptr = UACPI_NULL; + return ret; + } + + tbl->flags |= UACPI_TABLE_CSUM_VERIFIED; + } + break; + } + case 0xFFFF - 1: + uacpi_warn( + "too many references for "UACPI_PRI_TBL_HDR + ", mapping permanently\n", UACPI_FMT_TBL_HDR(&tbl->hdr) + ); + break; + default: + break; + } + + if (uacpi_likely(tbl->reference_count != 0xFFFF)) + tbl->reference_count++; + return UACPI_STATUS_OK; +} + +static uacpi_status table_unref_unlocked(struct uacpi_installed_table *tbl) +{ + switch (tbl->reference_count) { + case 0: + uacpi_warn( + "tried to unref table "UACPI_PRI_TBL_HDR" with no references\n", + UACPI_FMT_TBL_HDR(&tbl->hdr) + ); + return UACPI_STATUS_INVALID_ARGUMENT; + case 1: + if (tbl->origin != UACPI_TABLE_ORIGIN_HOST_PHYSICAL && + tbl->origin != UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL) + break; + + uacpi_kernel_unmap(tbl->ptr, tbl->hdr.length); + tbl->ptr = UACPI_NULL; + break; + case 0xFFFF: + /* + * Consider the reference count (overflow) of 0xFFFF to be a permanently + * mapped table as we don't know the actual number of references. + */ + return UACPI_STATUS_OK; + default: + break; + } + + tbl->reference_count--; + return UACPI_STATUS_OK; +} + +static uacpi_status verify_and_install_table( + struct acpi_sdt_hdr *hdr, uacpi_phys_addr phys_addr, void *virt_addr, + enum uacpi_table_origin origin, uacpi_table *out_table +) +{ + uacpi_status ret; + struct uacpi_installed_table *table; + uacpi_bool is_fadt; + uacpi_size idx; + uacpi_u8 flags = 0; + + is_fadt = uacpi_signatures_match(hdr->signature, ACPI_FADT_SIGNATURE); + + /* + * FACS is the only(?) table without a checksum because it has OSPM + * writable fields. Don't try to validate it here. + */ + if (uacpi_signatures_match(hdr->signature, ACPI_FACS_SIGNATURE)) { + flags |= UACPI_TABLE_CSUM_VERIFIED; + } else if (uacpi_check_flag(UACPI_FLAG_PROACTIVE_TBL_CSUM) || is_fadt || + out_table != UACPI_NULL) { + void *mapping = virt_addr; + + // We may already have a valid mapping, reuse it if we do + if (mapping == UACPI_NULL) + mapping = uacpi_kernel_map(phys_addr, hdr->length); + if (uacpi_unlikely(mapping == UACPI_NULL)) + return UACPI_STATUS_MAPPING_FAILED; + + ret = uacpi_verify_table_checksum(mapping, hdr->length); + if (uacpi_likely_success(ret)) { + if (is_fadt) + ret = initialize_fadt(mapping); + flags |= UACPI_TABLE_CSUM_VERIFIED; + } + + if (virt_addr == UACPI_NULL) + uacpi_kernel_unmap(mapping, hdr->length); + + if (uacpi_unlikely_error(ret)) + return ret; + } + + if (uacpi_signatures_match(hdr->signature, ACPI_DSDT_SIGNATURE)) + g_uacpi_rt_ctx.is_rev1 = hdr->revision < 2; + + ret = table_alloc(&table, &idx); + if (uacpi_unlikely_error(ret)) + return ret; + + dump_table_header(phys_addr, hdr); + + uacpi_memcpy(&table->hdr, hdr, sizeof(*hdr)); + table->reference_count = 0; + table->phys_addr = phys_addr; + table->ptr = virt_addr; + table->flags = flags; + table->origin = origin; + + if (out_table == UACPI_NULL) + return UACPI_STATUS_OK; + + table->reference_count++; + out_table->ptr = virt_addr; + out_table->index = idx; + return UACPI_STATUS_OK; +} + +static uacpi_status handle_table_override( + uacpi_table_installation_disposition disposition, uacpi_u64 address, + uacpi_table *out_table +) +{ + uacpi_status ret; + + switch (disposition) { + case UACPI_TABLE_INSTALLATION_DISPOSITON_VIRTUAL_OVERRIDE: + ret = table_install_with_origin_unlocked( + UACPI_VIRT_ADDR_TO_PTR((uacpi_virt_addr)address), + UACPI_TABLE_ORIGIN_HOST_VIRTUAL, + out_table + ); + return ret; + case UACPI_TABLE_INSTALLATION_DISPOSITON_PHYSICAL_OVERRIDE: + return table_install_physical_with_origin_unlocked( + (uacpi_phys_addr)address, + UACPI_TABLE_ORIGIN_HOST_PHYSICAL, + UACPI_NULL, + out_table + ); + default: + uacpi_error("invalid table installation disposition %d\n", disposition); + return UACPI_STATUS_INTERNAL_ERROR; + } +} + +static uacpi_status table_install_physical_with_origin_unlocked( + uacpi_phys_addr phys, enum uacpi_table_origin origin, + const uacpi_char *expected_signature, uacpi_table *out_table +) +{ + struct acpi_sdt_hdr hdr; + void *virt = UACPI_NULL; + uacpi_status ret; + + ret = get_external_table_header(phys, &hdr); + if (uacpi_unlikely_error(ret)) + return ret; + + if (uacpi_unlikely(hdr.length < sizeof(struct acpi_sdt_hdr))) { + uacpi_error("invalid table '%.4s' (0x016%"UACPI_PRIX64") size: %u\n", + hdr.signature, UACPI_FMT64(phys), hdr.length); + return UACPI_STATUS_INVALID_TABLE_LENGTH; + } + + if (expected_signature != UACPI_NULL) { + ret = uacpi_check_table_signature(&hdr, expected_signature); + if (uacpi_unlikely_error(ret)) + return ret; + } + + if (installation_handler != UACPI_NULL || out_table != UACPI_NULL) { + virt = uacpi_kernel_map(phys, hdr.length); + if (uacpi_unlikely(!virt)) + return UACPI_STATUS_MAPPING_FAILED; + } + + if (origin == UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL && + installation_handler != UACPI_NULL) { + uacpi_u64 override; + uacpi_table_installation_disposition disposition; + + disposition = installation_handler(virt, &override); + + switch (disposition) { + case UACPI_TABLE_INSTALLATION_DISPOSITON_ALLOW: + break; + case UACPI_TABLE_INSTALLATION_DISPOSITON_DENY: + uacpi_info( + "table '%.4s' (0x016%"UACPI_PRIX64") installation denied " + "by host\n", hdr.signature, UACPI_FMT64(phys) + ); + ret = UACPI_STATUS_DENIED; + goto out; + + default: + uacpi_info( + "table '%.4s' (0x016%"UACPI_PRIX64") installation " + "overridden by host\n", hdr.signature, UACPI_FMT64(phys) + ); + + ret = handle_table_override(disposition, override, out_table); + if (uacpi_likely_success(ret)) + ret = UACPI_STATUS_OVERRIDDEN; + + goto out; + } + } + + ret = verify_and_install_table(&hdr, phys, virt, origin, out_table); +out: + // We don't unmap only in this case + if (ret == UACPI_STATUS_OK && out_table != UACPI_NULL) + return ret; + + if (virt != UACPI_NULL) + uacpi_kernel_unmap(virt, hdr.length); + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_table_install_physical_with_origin( + uacpi_phys_addr phys, enum uacpi_table_origin origin, uacpi_table *out_table +) +{ + uacpi_status ret; + + ret = uacpi_acquire_native_mutex_may_be_null(table_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = table_install_physical_with_origin_unlocked( + phys, origin, UACPI_NULL, out_table + ); + uacpi_release_native_mutex_may_be_null(table_mutex); + + return ret; +} + +static uacpi_status table_install_with_origin_unlocked( + void *virt, enum uacpi_table_origin origin, uacpi_table *out_table +) +{ + struct acpi_sdt_hdr *hdr = virt; + + if (uacpi_unlikely(hdr->length < sizeof(struct acpi_sdt_hdr))) { + uacpi_error("invalid table '%.4s' (%p) size: %u\n", + hdr->signature, virt, hdr->length); + return UACPI_STATUS_INVALID_TABLE_LENGTH; + } + + if (origin == UACPI_TABLE_ORIGIN_FIRMWARE_VIRTUAL && + installation_handler != UACPI_NULL) { + uacpi_u64 override; + uacpi_table_installation_disposition disposition; + + disposition = installation_handler(virt, &override); + + switch (disposition) { + case UACPI_TABLE_INSTALLATION_DISPOSITON_ALLOW: + break; + case UACPI_TABLE_INSTALLATION_DISPOSITON_DENY: + uacpi_info( + "table "UACPI_PRI_TBL_HDR" installation denied by host\n", + UACPI_FMT_TBL_HDR(hdr) + ); + return UACPI_STATUS_DENIED; + + default: { + uacpi_status ret; + uacpi_info( + "table "UACPI_PRI_TBL_HDR" installation overridden by host\n", + UACPI_FMT_TBL_HDR(hdr) + ); + + ret = handle_table_override(disposition, override, out_table); + if (uacpi_likely_success(ret)) + ret = UACPI_STATUS_OVERRIDDEN; + + return ret; + } + } + } + + return verify_and_install_table( + hdr, 0, virt, origin, out_table + ); +} + +uacpi_status uacpi_table_install_with_origin( + void *virt, enum uacpi_table_origin origin, uacpi_table *out_table +) +{ + uacpi_status ret; + + ret = uacpi_acquire_native_mutex_may_be_null(table_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = table_install_with_origin_unlocked(virt, origin, out_table); + + uacpi_release_native_mutex_may_be_null(table_mutex); + return ret; +} + +uacpi_status uacpi_table_install(void *virt, uacpi_table *out_table) +{ + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + return uacpi_table_install_with_origin( + virt, UACPI_TABLE_ORIGIN_HOST_VIRTUAL, out_table + ); +} + +uacpi_status uacpi_table_install_physical( + uacpi_phys_addr addr, uacpi_table *out_table +) +{ + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + return uacpi_table_install_physical_with_origin( + addr, UACPI_TABLE_ORIGIN_HOST_PHYSICAL, out_table + ); +} + +uacpi_status uacpi_for_each_table( + uacpi_size base_idx, uacpi_table_iteration_callback cb, void *user +) +{ + uacpi_status ret; + uacpi_size idx; + struct uacpi_installed_table *tbl; + uacpi_iteration_decision dec; + + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex_may_be_null(table_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + for (idx = base_idx; idx < table_array_size(&tables); ++idx) { + tbl = table_array_at(&tables, idx); + + if (tbl->flags & UACPI_TABLE_INVALID) + continue; + + dec = cb(user, tbl, idx); + if (dec == UACPI_ITERATION_DECISION_BREAK) + break; + } + + uacpi_release_native_mutex_may_be_null(table_mutex); + return ret; +} + +enum search_type { + SEARCH_TYPE_BY_ID, + SEARCH_TYPE_MATCH, +}; + +struct table_search_ctx { + union { + const uacpi_table_identifiers *id; + uacpi_table_match_callback match_cb; + }; + + uacpi_table *out_table; + uacpi_u8 search_type; + uacpi_status status; +}; + +static uacpi_iteration_decision do_search_tables( + void *user, struct uacpi_installed_table *tbl, uacpi_size idx +) +{ + struct table_search_ctx *ctx = user; + uacpi_table *out_table; + uacpi_status ret; + + switch (ctx->search_type) { + case SEARCH_TYPE_BY_ID: { + const uacpi_table_identifiers *id = ctx->id; + + if (!uacpi_signatures_match(&id->signature, tbl->hdr.signature)) + return UACPI_ITERATION_DECISION_CONTINUE; + if (id->oemid[0] != '\0' && + uacpi_memcmp(id->oemid, tbl->hdr.oemid, sizeof(id->oemid)) != 0) + return UACPI_ITERATION_DECISION_CONTINUE; + + if (id->oem_table_id[0] != '\0' && + uacpi_memcmp(id->oem_table_id, tbl->hdr.oem_table_id, + sizeof(id->oem_table_id)) != 0) + return UACPI_ITERATION_DECISION_CONTINUE; + + break; + } + + case SEARCH_TYPE_MATCH: + if (!ctx->match_cb(tbl)) + return UACPI_ITERATION_DECISION_CONTINUE; + break; + + default: + ctx->status = UACPI_STATUS_INVALID_ARGUMENT; + return UACPI_ITERATION_DECISION_BREAK; + } + + ret = table_ref_unlocked(tbl); + if (uacpi_likely_success(ret)) { + out_table = ctx->out_table; + out_table->ptr = tbl->ptr; + out_table->index = idx; + ctx->status = ret; + return UACPI_ITERATION_DECISION_BREAK; + } + + /* + * Don't abort nor propagate bad checksums, just pretend this table never + * existed and go on with the search. + */ + if (ret == UACPI_STATUS_BAD_CHECKSUM) + return UACPI_ITERATION_DECISION_CONTINUE; + + ctx->status = ret; + return UACPI_ITERATION_DECISION_BREAK; +} + +uacpi_status uacpi_table_match( + uacpi_size base_idx, uacpi_table_match_callback cb, uacpi_table *out_table +) +{ + uacpi_status ret; + struct table_search_ctx ctx = { + .match_cb = cb, + .search_type = SEARCH_TYPE_MATCH, + .out_table = out_table, + .status = UACPI_STATUS_NOT_FOUND, + }; + + ret = uacpi_for_each_table(base_idx, do_search_tables, &ctx); + if (uacpi_unlikely_error(ret)) + return ret; + + return ctx.status; +} + +static uacpi_status find_table( + uacpi_size base_idx, const uacpi_table_identifiers *id, + uacpi_table *out_table +) +{ + uacpi_status ret; + struct table_search_ctx ctx = { + .id = id, + .out_table = out_table, + .search_type = SEARCH_TYPE_BY_ID, + .status = UACPI_STATUS_NOT_FOUND, + }; + + ret = uacpi_for_each_table(base_idx, do_search_tables, &ctx); + if (uacpi_unlikely_error(ret)) + return ret; + + return ctx.status; +} + +uacpi_status uacpi_table_find_by_signature( + const uacpi_char *signature_string, struct uacpi_table *out_table +) +{ + struct uacpi_table_identifiers id = { + .signature = { + .text = { + signature_string[0], + signature_string[1], + signature_string[2], + signature_string[3] + } + } + }; + + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + return find_table(0, &id, out_table); +} + +uacpi_status uacpi_table_find_next_with_same_signature( + uacpi_table *in_out_table +) +{ + struct uacpi_table_identifiers id = { 0 }; + + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_unlikely(in_out_table->ptr == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + uacpi_memcpy(&id.signature, in_out_table->hdr->signature, + sizeof(id.signature)); + uacpi_table_unref(in_out_table); + + return find_table(in_out_table->index + 1, &id, in_out_table); +} + +uacpi_status uacpi_table_find( + const uacpi_table_identifiers *id, uacpi_table *out_table +) +{ + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + return find_table(0, id, out_table); +} + +#define TABLE_CTL_SET_FLAGS (1 << 0) +#define TABLE_CTL_CLEAR_FLAGS (1 << 1) +#define TABLE_CTL_VALIDATE_SET_FLAGS (1 << 2) +#define TABLE_CTL_VALIDATE_CLEAR_FLAGS (1 << 3) +#define TABLE_CTL_GET (1 << 4) +#define TABLE_CTL_PUT (1 << 5) + +struct table_ctl_request { + uacpi_u8 type; + + uacpi_u8 expect_set; + uacpi_u8 expect_clear; + uacpi_u8 set; + uacpi_u8 clear; + + void *out_tbl; +}; + +static uacpi_status table_ctl(uacpi_size idx, struct table_ctl_request *req) +{ + uacpi_status ret; + struct uacpi_installed_table *tbl; + + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + ret = uacpi_acquire_native_mutex_may_be_null(table_mutex); + if (uacpi_unlikely_error(ret)) + return ret; + + if (uacpi_unlikely(table_array_size(&tables) <= idx)) { + uacpi_error( + "requested invalid table index %zu (%zu tables installed)\n", + idx, table_array_size(&tables) + ); + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + + tbl = table_array_at(&tables, idx); + if (uacpi_unlikely(tbl->flags & UACPI_TABLE_INVALID)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (req->type & TABLE_CTL_VALIDATE_SET_FLAGS) { + uacpi_u8 mask = req->expect_set; + + if (uacpi_unlikely((tbl->flags & mask) != mask)) { + uacpi_error( + "unexpected table '%.4s' flags %02X, expected %02X to be set\n", + tbl->hdr.signature, tbl->flags, mask + ); + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out; + } + } + + if (req->type & TABLE_CTL_VALIDATE_CLEAR_FLAGS) { + uacpi_u8 mask = req->expect_clear; + + if (uacpi_unlikely((tbl->flags & mask) != 0)) { + uacpi_error( + "unexpected table '%.4s' flags %02X, expected %02X " + "to be clear\n", tbl->hdr.signature, tbl->flags, mask + ); + ret = UACPI_STATUS_ALREADY_EXISTS; + goto out; + } + } + + if (req->type & TABLE_CTL_GET) { + ret = table_ref_unlocked(tbl); + if (uacpi_unlikely_error(ret)) + goto out; + + req->out_tbl = tbl->ptr; + } + + if (req->type & TABLE_CTL_PUT) { + ret = table_unref_unlocked(tbl); + if (uacpi_unlikely_error(ret)) + goto out; + } + + if (req->type & TABLE_CTL_SET_FLAGS) + tbl->flags |= req->set; + if (req->type & TABLE_CTL_CLEAR_FLAGS) + tbl->flags &= ~req->clear; + +out: + uacpi_release_native_mutex_may_be_null(table_mutex); + return ret; +} + +uacpi_status uacpi_table_load_with_cause( + uacpi_size idx, enum uacpi_table_load_cause cause +) +{ + uacpi_status ret; + struct table_ctl_request req = { + .type = TABLE_CTL_SET_FLAGS | TABLE_CTL_VALIDATE_CLEAR_FLAGS | + TABLE_CTL_GET, + .set = UACPI_TABLE_LOADED, + .expect_clear = UACPI_TABLE_LOADED, + }; + + ret = table_ctl(idx, &req); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_execute_table(req.out_tbl, cause); + + req.type = TABLE_CTL_PUT; + table_ctl(idx, &req); + return ret; +} + +uacpi_status uacpi_table_load(uacpi_size idx) +{ + return uacpi_table_load_with_cause(idx, UACPI_TABLE_LOAD_CAUSE_HOST); +} + +void uacpi_table_mark_as_loaded(uacpi_size idx) +{ + table_ctl(idx, &(struct table_ctl_request) { + .type = TABLE_CTL_SET_FLAGS, .set = UACPI_TABLE_LOADED + }); +} + +uacpi_status uacpi_table_ref(uacpi_table *tbl) +{ + return table_ctl(tbl->index, &(struct table_ctl_request) { + .type = TABLE_CTL_GET + }); +} + +uacpi_status uacpi_table_unref(uacpi_table *tbl) +{ + return table_ctl(tbl->index, &(struct table_ctl_request) { + .type = TABLE_CTL_PUT + }); +} + +uacpi_u16 fadt_version_sizes[] = { + 116, 132, 244, 244, 268, 276 +}; + +static void fadt_ensure_correct_revision(struct acpi_fadt *fadt) +{ + uacpi_size current_rev, rev; + + current_rev = fadt->hdr.revision; + + for (rev = 0; rev < UACPI_ARRAY_SIZE(fadt_version_sizes); ++rev) { + if (fadt->hdr.length <= fadt_version_sizes[rev]) + break; + } + + if (rev == UACPI_ARRAY_SIZE(fadt_version_sizes)) { + uacpi_trace( + "FADT revision (%zu) is likely greater than the last " + "supported, reducing to %zu\n", current_rev, rev + ); + fadt->hdr.revision = rev; + return; + } + + rev++; + + if (current_rev != rev && !(rev == 3 && current_rev == 4)) { + uacpi_warn( + "FADT length %u doesn't match expected for revision %zu, " + "assuming version %zu\n", fadt->hdr.length, current_rev, + rev + ); + fadt->hdr.revision = rev; + } +} + +static void gas_init_system_io( + struct acpi_gas *gas, uacpi_u64 address, uacpi_u8 byte_size +) +{ + gas->address = address; + gas->address_space_id = UACPI_ADDRESS_SPACE_SYSTEM_IO; + gas->register_bit_width = UACPI_MIN(255, byte_size * 8); + gas->register_bit_offset = 0; + gas->access_size = 0; +} + + +struct register_description { + uacpi_size offset, xoffset; + uacpi_size length_offset; +}; + +#define fadt_offset(field) uacpi_offsetof(struct acpi_fadt, field) + +/* + * We convert all the legacy registers into GAS format and write them into + * the x_* fields for convenience and faster access at runtime. + */ +static struct register_description fadt_registers[] = { + { + .offset = fadt_offset(pm1a_evt_blk), + .xoffset = fadt_offset(x_pm1a_evt_blk), + .length_offset = fadt_offset(pm1_evt_len), + }, + { + .offset = fadt_offset(pm1b_evt_blk), + .xoffset = fadt_offset(x_pm1b_evt_blk), + .length_offset = fadt_offset(pm1_evt_len), + }, + { + .offset = fadt_offset(pm1a_cnt_blk), + .xoffset = fadt_offset(x_pm1a_cnt_blk), + .length_offset = fadt_offset(pm1_cnt_len), + }, + { + .offset = fadt_offset(pm1b_cnt_blk), + .xoffset = fadt_offset(x_pm1b_cnt_blk), + .length_offset = fadt_offset(pm1_cnt_len), + }, + { + .offset = fadt_offset(pm2_cnt_blk), + .xoffset = fadt_offset(x_pm2_cnt_blk), + .length_offset = fadt_offset(pm2_cnt_len), + }, + { + .offset = fadt_offset(pm_tmr_blk), + .xoffset = fadt_offset(x_pm_tmr_blk), + .length_offset = fadt_offset(pm_tmr_len), + }, + { + .offset = fadt_offset(gpe0_blk), + .xoffset = fadt_offset(x_gpe0_blk), + .length_offset = fadt_offset(gpe0_blk_len), + }, + { + .offset = fadt_offset(gpe1_blk), + .xoffset = fadt_offset(x_gpe1_blk), + .length_offset = fadt_offset(gpe1_blk_len), + }, +}; + +static void *fadt_relative(uacpi_size offset) +{ + return ((uacpi_u8*)&g_uacpi_rt_ctx.fadt) + offset; +} + +static void convert_registers_to_gas(void) +{ + uacpi_size i; + struct register_description *desc; + struct acpi_gas *gas; + uacpi_u32 legacy_addr; + uacpi_u8 length; + + for (i = 0; i < UACPI_ARRAY_SIZE(fadt_registers); ++i) { + desc = &fadt_registers[i]; + + legacy_addr = *(uacpi_u32*)fadt_relative(desc->offset); + length = *(uacpi_u8*)fadt_relative(desc->length_offset); + gas = fadt_relative(desc->xoffset); + + if (gas->address) + continue; + + gas_init_system_io(gas, legacy_addr, length); + } +} + +static void split_one_block( + struct acpi_gas *src, struct acpi_gas *dst0, struct acpi_gas *dst1 +) +{ + uacpi_size byte_length; + + if (src->address == 0) + return; + + byte_length = src->register_bit_width / 8; + byte_length /= 2; + + gas_init_system_io(dst0, src->address, byte_length); + gas_init_system_io(dst1, src->address + byte_length, byte_length); +} + +static void split_event_blocks(void) +{ + split_one_block( + &g_uacpi_rt_ctx.fadt.x_pm1a_evt_blk, + &g_uacpi_rt_ctx.pm1a_status_blk, + &g_uacpi_rt_ctx.pm1a_enable_blk + ); + split_one_block( + &g_uacpi_rt_ctx.fadt.x_pm1b_evt_blk, + &g_uacpi_rt_ctx.pm1b_status_blk, + &g_uacpi_rt_ctx.pm1b_enable_blk + ); +} + +static uacpi_status initialize_fadt(const void *virt) +{ + uacpi_status ret; + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + const struct acpi_sdt_hdr *hdr = virt; + + /* + * Here we (roughly) follow ACPICA initialization sequence to make sure we + * handle potential BIOS quirks with garbage inside FADT correctly. + */ + + uacpi_memcpy(fadt, hdr, UACPI_MIN(sizeof(*fadt), hdr->length)); + +#ifndef UACPI_REDUCED_HARDWARE + g_uacpi_rt_ctx.is_hardware_reduced = fadt->flags & ACPI_HW_REDUCED_ACPI; +#endif + + fadt_ensure_correct_revision(fadt); + + /* + * These are reserved prior to version 3, so zero them out to work around + * BIOS implementations that might dirty these. + */ + if (fadt->hdr.revision <= 2) { + fadt->preferred_pm_profile = 0; + fadt->pstate_cnt = 0; + fadt->cst_cnt = 0; + fadt->iapc_boot_arch = 0; + } + + if (!fadt->x_dsdt) + fadt->x_dsdt = fadt->dsdt; + + if (fadt->x_dsdt) { + ret = table_install_physical_with_origin_unlocked( + fadt->x_dsdt, UACPI_TABLE_ORIGIN_FIRMWARE_PHYSICAL, + ACPI_DSDT_SIGNATURE, UACPI_NULL + ); + if (uacpi_unlikely(ret != UACPI_STATUS_OK && + ret != UACPI_STATUS_OVERRIDDEN)) + return ret; + } + + /* + * Unconditionally use 32 bit FACS if it exists, as 64 bit FACS is known + * to cause issues on some firmware: + * https://bugzilla.kernel.org/show_bug.cgi?id=74021 + * + * Note that we don't install it here as FACS needs permanent mapping, which + * we might not be able to obtain at this point in case of early table + * access. + */ + if (fadt->firmware_ctrl) + fadt->x_firmware_ctrl = fadt->firmware_ctrl; + + if (!uacpi_is_hardware_reduced()) { + convert_registers_to_gas(); + split_event_blocks(); + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_table_fadt(struct acpi_fadt **out_fadt) +{ + if (!early_table_access) + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + *out_fadt = &g_uacpi_rt_ctx.fadt; + return UACPI_STATUS_OK; +} diff --git a/src/uacpi/types.c b/src/uacpi/types.c new file mode 100644 index 0000000..3d271ca --- /dev/null +++ b/src/uacpi/types.c @@ -0,0 +1,1460 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const uacpi_char *uacpi_object_type_to_string(uacpi_object_type type) +{ + switch (type) { + case UACPI_OBJECT_UNINITIALIZED: + return "Uninitialized"; + case UACPI_OBJECT_INTEGER: + return "Integer"; + case UACPI_OBJECT_STRING: + return "String"; + case UACPI_OBJECT_BUFFER: + return "Buffer"; + case UACPI_OBJECT_PACKAGE: + return "Package"; + case UACPI_OBJECT_FIELD_UNIT: + return "Field Unit"; + case UACPI_OBJECT_DEVICE: + return "Device"; + case UACPI_OBJECT_EVENT: + return "Event"; + case UACPI_OBJECT_REFERENCE: + return "Reference"; + case UACPI_OBJECT_BUFFER_INDEX: + return "Buffer Index"; + case UACPI_OBJECT_METHOD: + return "Method"; + case UACPI_OBJECT_MUTEX: + return "Mutex"; + case UACPI_OBJECT_OPERATION_REGION: + return "Operation Region"; + case UACPI_OBJECT_POWER_RESOURCE: + return "Power Resource"; + case UACPI_OBJECT_PROCESSOR: + return "Processor"; + case UACPI_OBJECT_THERMAL_ZONE: + return "Thermal Zone"; + case UACPI_OBJECT_BUFFER_FIELD: + return "Buffer Field"; + case UACPI_OBJECT_DEBUG: + return "Debug"; + default: + return ""; + } +} + +const uacpi_char *uacpi_address_space_to_string( + enum uacpi_address_space space +) +{ + switch (space) { + case UACPI_ADDRESS_SPACE_SYSTEM_MEMORY: + return "SystemMemory"; + case UACPI_ADDRESS_SPACE_SYSTEM_IO: + return "SystemIO"; + case UACPI_ADDRESS_SPACE_PCI_CONFIG: + return "PCI_Config"; + case UACPI_ADDRESS_SPACE_EMBEDDED_CONTROLLER: + return "EmbeddedControl"; + case UACPI_ADDRESS_SPACE_SMBUS: + return "SMBus"; + case UACPI_ADDRESS_SPACE_SYSTEM_CMOS: + return "SystemCMOS"; + case UACPI_ADDRESS_SPACE_PCI_BAR_TARGET: + return "PciBarTarget"; + case UACPI_ADDRESS_SPACE_IPMI: + return "IPMI"; + case UACPI_ADDRESS_SPACE_GENERAL_PURPOSE_IO: + return "GeneralPurposeIO"; + case UACPI_ADDRESS_SPACE_GENERIC_SERIAL_BUS: + return "GenericSerialBus"; + case UACPI_ADDRESS_SPACE_PCC: + return "PCC"; + case UACPI_ADDRESS_SPACE_PRM: + return "PRM"; + case UACPI_ADDRESS_SPACE_FFIXEDHW: + return "FFixedHW"; + case UACPI_ADDRESS_SPACE_TABLE_DATA: + return "TableData"; + default: + return ""; + } +} + +static uacpi_bool buffer_alloc(uacpi_object *obj, uacpi_size initial_size) +{ + uacpi_buffer *buf; + + buf = uacpi_kernel_alloc_zeroed(sizeof(uacpi_buffer)); + if (uacpi_unlikely(buf == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(buf); + + if (initial_size) { + buf->data = uacpi_kernel_alloc(initial_size); + if (uacpi_unlikely(buf->data == UACPI_NULL)) { + uacpi_free(buf, sizeof(*buf)); + return UACPI_FALSE; + } + + buf->size = initial_size; + } + + obj->buffer = buf; + return UACPI_TRUE; +} + +static uacpi_bool empty_buffer_or_string_alloc(uacpi_object *object) +{ + return buffer_alloc(object, 0); +} + +uacpi_bool uacpi_package_fill( + uacpi_package *pkg, uacpi_size num_elements, + enum uacpi_prealloc_objects prealloc_objects +) +{ + uacpi_size i; + + if (uacpi_unlikely(num_elements == 0)) + return UACPI_TRUE; + + pkg->objects = uacpi_kernel_alloc_zeroed( + num_elements * sizeof(uacpi_handle) + ); + if (uacpi_unlikely(pkg->objects == UACPI_NULL)) + return UACPI_FALSE; + + pkg->count = num_elements; + + if (prealloc_objects == UACPI_PREALLOC_OBJECTS_YES) { + for (i = 0; i < num_elements; ++i) { + pkg->objects[i] = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + + if (uacpi_unlikely(pkg->objects[i] == UACPI_NULL)) + return UACPI_FALSE; + } + } + + return UACPI_TRUE; +} + +static uacpi_bool package_alloc( + uacpi_object *obj, uacpi_size initial_size, + enum uacpi_prealloc_objects prealloc +) +{ + uacpi_package *pkg; + + pkg = uacpi_kernel_alloc_zeroed(sizeof(uacpi_package)); + if (uacpi_unlikely(pkg == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(pkg); + + if (uacpi_unlikely(!uacpi_package_fill(pkg, initial_size, prealloc))) { + uacpi_free(pkg, sizeof(*pkg)); + return UACPI_FALSE; + } + + obj->package = pkg; + return UACPI_TRUE; +} + +static uacpi_bool empty_package_alloc(uacpi_object *object) +{ + return package_alloc(object, 0, UACPI_PREALLOC_OBJECTS_NO); +} + +uacpi_mutex *uacpi_create_mutex(void) +{ + uacpi_mutex *mutex; + + mutex = uacpi_kernel_alloc_zeroed(sizeof(uacpi_mutex)); + if (uacpi_unlikely(mutex == UACPI_NULL)) + return UACPI_NULL; + + mutex->owner = UACPI_THREAD_ID_NONE; + + mutex->handle = uacpi_kernel_create_mutex(); + if (mutex->handle == UACPI_NULL) { + uacpi_free(mutex, sizeof(*mutex)); + return UACPI_NULL; + } + + uacpi_shareable_init(mutex); + return mutex; +} + +static uacpi_bool mutex_alloc(uacpi_object *obj) +{ + obj->mutex = uacpi_create_mutex(); + return obj->mutex != UACPI_NULL; +} + +static uacpi_bool event_alloc(uacpi_object *obj) +{ + uacpi_event *event; + + event = uacpi_kernel_alloc_zeroed(sizeof(uacpi_event)); + if (uacpi_unlikely(event == UACPI_NULL)) + return UACPI_FALSE; + + event->handle = uacpi_kernel_create_event(); + if (event->handle == UACPI_NULL) { + uacpi_free(event, sizeof(*event)); + return UACPI_FALSE; + } + + uacpi_shareable_init(event); + obj->event = event; + + return UACPI_TRUE; +} + +static uacpi_bool method_alloc(uacpi_object *obj) +{ + uacpi_control_method *method; + + method = uacpi_kernel_alloc_zeroed(sizeof(*method)); + if (uacpi_unlikely(method == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(method); + obj->method = method; + + return UACPI_TRUE; +} + +static uacpi_bool op_region_alloc(uacpi_object *obj) +{ + uacpi_operation_region *op_region; + + op_region = uacpi_kernel_alloc_zeroed(sizeof(*op_region)); + if (uacpi_unlikely(op_region == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(op_region); + obj->op_region = op_region; + + return UACPI_TRUE; +} + +static uacpi_bool field_unit_alloc(uacpi_object *obj) +{ + uacpi_field_unit *field_unit; + + field_unit = uacpi_kernel_alloc_zeroed(sizeof(*field_unit)); + if (uacpi_unlikely(field_unit == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(field_unit); + obj->field_unit = field_unit; + + return UACPI_TRUE; +} + +static uacpi_bool processor_alloc(uacpi_object *obj) +{ + uacpi_processor *processor; + + processor = uacpi_kernel_alloc_zeroed(sizeof(*processor)); + if (uacpi_unlikely(processor == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(processor); + obj->processor = processor; + + return UACPI_TRUE; +} + +static uacpi_bool device_alloc(uacpi_object *obj) +{ + uacpi_device *device; + + device = uacpi_kernel_alloc_zeroed(sizeof(*device)); + if (uacpi_unlikely(device == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(device); + obj->device = device; + + return UACPI_TRUE; +} + +static uacpi_bool thermal_zone_alloc(uacpi_object *obj) +{ + uacpi_thermal_zone *thermal_zone; + + thermal_zone = uacpi_kernel_alloc_zeroed(sizeof(*thermal_zone)); + if (uacpi_unlikely(thermal_zone == UACPI_NULL)) + return UACPI_FALSE; + + uacpi_shareable_init(thermal_zone); + obj->thermal_zone = thermal_zone; + + return UACPI_TRUE; +} + +typedef uacpi_bool (*object_ctor)(uacpi_object *obj); + +static object_ctor object_constructor_table[UACPI_OBJECT_MAX_TYPE_VALUE + 1] = { + [UACPI_OBJECT_STRING] = empty_buffer_or_string_alloc, + [UACPI_OBJECT_BUFFER] = empty_buffer_or_string_alloc, + [UACPI_OBJECT_PACKAGE] = empty_package_alloc, + [UACPI_OBJECT_FIELD_UNIT] = field_unit_alloc, + [UACPI_OBJECT_MUTEX] = mutex_alloc, + [UACPI_OBJECT_EVENT] = event_alloc, + [UACPI_OBJECT_OPERATION_REGION] = op_region_alloc, + [UACPI_OBJECT_METHOD] = method_alloc, + [UACPI_OBJECT_PROCESSOR] = processor_alloc, + [UACPI_OBJECT_DEVICE] = device_alloc, + [UACPI_OBJECT_THERMAL_ZONE] = thermal_zone_alloc, +}; + +uacpi_object *uacpi_create_object(uacpi_object_type type) +{ + uacpi_object *ret; + object_ctor ctor; + + ret = uacpi_kernel_alloc_zeroed(sizeof(*ret)); + if (uacpi_unlikely(ret == UACPI_NULL)) + return ret; + + uacpi_shareable_init(ret); + ret->type = type; + + ctor = object_constructor_table[type]; + if (ctor == UACPI_NULL) + return ret; + + if (uacpi_unlikely(!ctor(ret))) { + uacpi_free(ret, sizeof(*ret)); + return UACPI_NULL; + } + + return ret; +} + +static void free_buffer(uacpi_handle handle) +{ + uacpi_buffer *buf = handle; + + if (buf->data != UACPI_NULL) + /* + * If buffer has a size of 0 but a valid data pointer it's probably an + * "empty" buffer allocated by the interpreter in make_null_buffer + * and its real size is actually 1. + */ + uacpi_free(buf->data, UACPI_MAX(buf->size, 1)); + + uacpi_free(buf, sizeof(*buf)); +} + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(free_queue, uacpi_package*, 4) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL(free_queue, uacpi_package*, static) + +static uacpi_bool free_queue_push(struct free_queue *queue, uacpi_package *pkg) +{ + uacpi_package **slot; + + slot = free_queue_alloc(queue); + if (uacpi_unlikely(slot == UACPI_NULL)) + return UACPI_FALSE; + + *slot = pkg; + return UACPI_TRUE; +} + +static void free_object(uacpi_object *obj); + +// No references allowed here, only plain objects +static void free_plain_no_recurse(uacpi_object *obj, struct free_queue *queue) +{ + switch (obj->type) { + case UACPI_OBJECT_PACKAGE: + if (uacpi_shareable_unref(obj->package) > 1) + break; + + if (uacpi_unlikely(!free_queue_push(queue, + obj->package))) { + uacpi_warn( + "unable to free nested package @%p: not enough memory\n", + obj->package + ); + } + + // Don't call free_object here as that will recurse + uacpi_free(obj, sizeof(*obj)); + break; + default: + /* + * This call is guaranteed to not recurse further as we handle + * recursive cases elsewhere explicitly. + */ + free_object(obj); + } +} + +static void unref_plain_no_recurse(uacpi_object *obj, struct free_queue *queue) +{ + if (uacpi_shareable_unref(obj) > 1) + return; + + free_plain_no_recurse(obj, queue); +} + +static void unref_chain_no_recurse(uacpi_object *obj, struct free_queue *queue) +{ + uacpi_object *next_obj = UACPI_NULL; + + while (obj) { + if (obj->type == UACPI_OBJECT_REFERENCE) + next_obj = obj->inner_object; + + if (uacpi_shareable_unref(obj) > 1) + goto do_next; + + if (obj->type == UACPI_OBJECT_REFERENCE) { + uacpi_free(obj, sizeof(*obj)); + } else { + free_plain_no_recurse(obj, queue); + } + + do_next: + obj = next_obj; + next_obj = UACPI_NULL; + } +} + +static void unref_object_no_recurse(uacpi_object *obj, struct free_queue *queue) +{ + if (obj->type == UACPI_OBJECT_REFERENCE) { + unref_chain_no_recurse(obj, queue); + return; + } + + unref_plain_no_recurse(obj, queue); +} + +static void free_package(uacpi_handle handle) +{ + struct free_queue queue = { 0 }; + uacpi_package *pkg = handle; + uacpi_object *obj; + uacpi_size i; + + free_queue_push(&queue, pkg); + + while (free_queue_size(&queue) != 0) { + pkg = *free_queue_last(&queue); + free_queue_pop(&queue); + + /* + * 1. Unref/free every object in the package. Note that this might add + * even more packages into the free queue. + */ + for (i = 0; i < pkg->count; ++i) { + obj = pkg->objects[i]; + unref_object_no_recurse(obj, &queue); + } + + // 2. Release the object array + uacpi_free(pkg->objects, sizeof(*pkg->objects) * pkg->count); + + // 3. Release the package itself + uacpi_free(pkg, sizeof(*pkg)); + } + + free_queue_clear(&queue); +} + +static void free_mutex(uacpi_handle handle) +{ + uacpi_mutex *mutex = handle; + + uacpi_kernel_free_mutex(mutex->handle); + uacpi_free(mutex, sizeof(*mutex)); +} + +void uacpi_mutex_unref(uacpi_mutex *mutex) +{ + if (mutex == UACPI_NULL) + return; + + uacpi_shareable_unref_and_delete_if_last(mutex, free_mutex); +} + +static void free_event(uacpi_handle handle) +{ + uacpi_event *event = handle; + + uacpi_kernel_free_event(event->handle); + uacpi_free(event, sizeof(*event)); +} + +static void free_address_space_handler(uacpi_handle handle) +{ + uacpi_address_space_handler *handler = handle; + uacpi_free(handler, sizeof(*handler)); +} + +static void free_address_space_handlers( + uacpi_address_space_handler *handler +) +{ + uacpi_address_space_handler *next_handler; + + while (handler) { + next_handler = handler->next; + uacpi_shareable_unref_and_delete_if_last( + handler, free_address_space_handler + ); + handler = next_handler; + } +} + +static void free_device_notify_handlers(uacpi_device_notify_handler *handler) +{ + uacpi_device_notify_handler *next_handler; + + while (handler) { + next_handler = handler->next; + uacpi_free(handler, sizeof(*handler)); + handler = next_handler; + } +} + +static void free_handlers(uacpi_handle handle) +{ + uacpi_handlers *handlers = handle; + + free_address_space_handlers(handlers->address_space_head); + free_device_notify_handlers(handlers->notify_head); +} + +void uacpi_address_space_handler_unref(uacpi_address_space_handler *handler) +{ + uacpi_shareable_unref_and_delete_if_last( + handler, free_address_space_handler + ); +} + +static void free_op_region(uacpi_handle handle) +{ + uacpi_operation_region *op_region = handle; + + if (uacpi_unlikely(op_region->handler != UACPI_NULL)) { + uacpi_warn( + "BUG: attempting to free an opregion@%p with a handler attached\n", + op_region + ); + } + + if (op_region->space == UACPI_ADDRESS_SPACE_TABLE_DATA) + uacpi_table_unref(&(struct uacpi_table) { .index = op_region->table_idx }); + uacpi_free(op_region, sizeof(*op_region)); +} + +static void free_device(uacpi_handle handle) +{ + uacpi_device *device = handle; + free_handlers(device); + uacpi_free(device, sizeof(*device)); +} + +static void free_processor(uacpi_handle handle) +{ + uacpi_processor *processor = handle; + free_handlers(processor); + uacpi_free(processor, sizeof(*processor)); +} + +static void free_thermal_zone(uacpi_handle handle) +{ + uacpi_thermal_zone *thermal_zone = handle; + free_handlers(thermal_zone); + uacpi_free(thermal_zone, sizeof(*thermal_zone)); +} + +static void free_field_unit(uacpi_handle handle) +{ + uacpi_field_unit *field_unit = handle; + + if (field_unit->connection) + uacpi_object_unref(field_unit->connection); + + switch (field_unit->kind) { + case UACPI_FIELD_UNIT_KIND_NORMAL: + uacpi_namespace_node_unref(field_unit->region); + break; + case UACPI_FIELD_UNIT_KIND_BANK: + uacpi_namespace_node_unref(field_unit->bank_region); + uacpi_shareable_unref_and_delete_if_last( + field_unit->bank_selection, free_field_unit + ); + break; + case UACPI_FIELD_UNIT_KIND_INDEX: + uacpi_shareable_unref_and_delete_if_last( + field_unit->index, free_field_unit + ); + uacpi_shareable_unref_and_delete_if_last( + field_unit->data, free_field_unit + ); + break; + default: + break; + } + + uacpi_free(field_unit, sizeof(*field_unit)); +} + +static void free_method(uacpi_handle handle) +{ + uacpi_control_method *method = handle; + + uacpi_shareable_unref_and_delete_if_last( + method->mutex, free_mutex + ); + + if (!method->native_call && method->owns_code) + uacpi_free(method->code, method->size); + uacpi_free(method, sizeof(*method)); +} + +void uacpi_method_unref(uacpi_control_method *method) +{ + uacpi_shareable_unref_and_delete_if_last(method, free_method); +} + +static void free_object_storage(uacpi_object *obj) +{ + switch (obj->type) { + case UACPI_OBJECT_STRING: + case UACPI_OBJECT_BUFFER: + uacpi_shareable_unref_and_delete_if_last(obj->buffer, free_buffer); + break; + case UACPI_OBJECT_BUFFER_FIELD: + uacpi_shareable_unref_and_delete_if_last(obj->buffer_field.backing, + free_buffer); + break; + case UACPI_OBJECT_BUFFER_INDEX: + uacpi_shareable_unref_and_delete_if_last(obj->buffer_index.buffer, + free_buffer); + break; + case UACPI_OBJECT_METHOD: + uacpi_method_unref(obj->method); + break; + case UACPI_OBJECT_PACKAGE: + uacpi_shareable_unref_and_delete_if_last(obj->package, + free_package); + break; + case UACPI_OBJECT_FIELD_UNIT: + uacpi_shareable_unref_and_delete_if_last(obj->field_unit, + free_field_unit); + break; + case UACPI_OBJECT_MUTEX: + uacpi_mutex_unref(obj->mutex); + break; + case UACPI_OBJECT_EVENT: + uacpi_shareable_unref_and_delete_if_last(obj->event, + free_event); + break; + case UACPI_OBJECT_OPERATION_REGION: + uacpi_shareable_unref_and_delete_if_last(obj->op_region, + free_op_region); + break; + case UACPI_OBJECT_PROCESSOR: + uacpi_shareable_unref_and_delete_if_last(obj->processor, + free_processor); + break; + case UACPI_OBJECT_DEVICE: + uacpi_shareable_unref_and_delete_if_last(obj->device, + free_device); + break; + case UACPI_OBJECT_THERMAL_ZONE: + uacpi_shareable_unref_and_delete_if_last(obj->thermal_zone, + free_thermal_zone); + break; + default: + break; + } +} + +static void free_object(uacpi_object *obj) +{ + free_object_storage(obj); + uacpi_free(obj, sizeof(*obj)); +} + +static void make_chain_bugged(uacpi_object *obj) +{ + uacpi_warn("object refcount bug, marking chain @%p as bugged\n", obj); + + while (obj) { + uacpi_make_shareable_bugged(obj); + + if (obj->type == UACPI_OBJECT_REFERENCE) + obj = obj->inner_object; + else + obj = UACPI_NULL; + } +} + +void uacpi_object_ref(uacpi_object *obj) +{ + while (obj) { + uacpi_shareable_ref(obj); + + if (obj->type == UACPI_OBJECT_REFERENCE) + obj = obj->inner_object; + else + obj = UACPI_NULL; + } +} + +static void free_chain(uacpi_object *obj) +{ + uacpi_object *next_obj = UACPI_NULL; + + while (obj) { + if (obj->type == UACPI_OBJECT_REFERENCE) + next_obj = obj->inner_object; + + if (uacpi_shareable_refcount(obj) == 0) + free_object(obj); + + obj = next_obj; + next_obj = UACPI_NULL; + } +} + +void uacpi_object_unref(uacpi_object *obj) +{ + uacpi_object *this_obj = obj; + + if (!obj) + return; + + while (obj) { + if (uacpi_unlikely(uacpi_bugged_shareable(obj))) + return; + + uacpi_shareable_unref(obj); + + if (obj->type == UACPI_OBJECT_REFERENCE) { + obj = obj->inner_object; + } else { + obj = UACPI_NULL; + } + } + + if (uacpi_shareable_refcount(this_obj) == 0) + free_chain(this_obj); +} + +static uacpi_status buffer_alloc_and_store( + uacpi_object *obj, uacpi_size buf_size, + const void *src, uacpi_size src_size +) +{ + if (uacpi_unlikely(!buffer_alloc(obj, buf_size))) + return UACPI_STATUS_OUT_OF_MEMORY; + + uacpi_memcpy_zerout(obj->buffer->data, src, buf_size, src_size); + return UACPI_STATUS_OK; +} + +static uacpi_status assign_buffer(uacpi_object *dst, uacpi_object *src, + enum uacpi_assign_behavior behavior) +{ + if (behavior == UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY) { + dst->buffer = src->buffer; + uacpi_shareable_ref(dst->buffer); + return UACPI_STATUS_OK; + } + + return buffer_alloc_and_store(dst, src->buffer->size, + src->buffer->data, src->buffer->size); +} + +struct pkg_copy_req { + uacpi_object *dst; + uacpi_package *src; +}; + +DYNAMIC_ARRAY_WITH_INLINE_STORAGE(pkg_copy_reqs, struct pkg_copy_req, 2) +DYNAMIC_ARRAY_WITH_INLINE_STORAGE_IMPL( + pkg_copy_reqs, struct pkg_copy_req, static +) + +static uacpi_bool pkg_copy_reqs_push( + struct pkg_copy_reqs *reqs, + uacpi_object *dst, uacpi_package *pkg +) +{ + struct pkg_copy_req *req; + + req = pkg_copy_reqs_alloc(reqs); + if (uacpi_unlikely(req == UACPI_NULL)) + return UACPI_FALSE; + + req->dst = dst; + req->src = pkg; + + return UACPI_TRUE; +} + +static uacpi_status deep_copy_package_no_recurse( + uacpi_object *dst, uacpi_package *src, + struct pkg_copy_reqs *reqs +) +{ + uacpi_size i; + uacpi_package *dst_package; + + if (uacpi_unlikely(!package_alloc(dst, src->count, + UACPI_PREALLOC_OBJECTS_YES))) + return UACPI_STATUS_OUT_OF_MEMORY; + + dst->type = UACPI_OBJECT_PACKAGE; + dst_package = dst->package; + + for (i = 0; i < src->count; ++i) { + uacpi_status st; + uacpi_object *src_obj = src->objects[i]; + uacpi_object *dst_obj = dst_package->objects[i]; + + // Don't copy the internal package index reference + if (src_obj->type == UACPI_OBJECT_REFERENCE && + src_obj->flags == UACPI_REFERENCE_KIND_PKG_INDEX) + src_obj = src_obj->inner_object; + + if (src_obj->type == UACPI_OBJECT_PACKAGE) { + uacpi_bool ret; + + ret = pkg_copy_reqs_push(reqs, dst_obj, src_obj->package); + if (uacpi_unlikely(!ret)) + return UACPI_STATUS_OUT_OF_MEMORY; + + continue; + } + + st = uacpi_object_assign(dst_obj, src_obj, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY); + if (uacpi_unlikely_error(st)) + return st; + } + + return UACPI_STATUS_OK; +} + +static uacpi_status deep_copy_package(uacpi_object *dst, uacpi_object *src) +{ + uacpi_status ret = UACPI_STATUS_OK; + struct pkg_copy_reqs reqs = { 0 }; + + pkg_copy_reqs_push(&reqs, dst, src->package); + + while (pkg_copy_reqs_size(&reqs) != 0) { + struct pkg_copy_req req; + + req = *pkg_copy_reqs_last(&reqs); + pkg_copy_reqs_pop(&reqs); + + ret = deep_copy_package_no_recurse(req.dst, req.src, &reqs); + if (uacpi_unlikely_error(ret)) + break; + } + + pkg_copy_reqs_clear(&reqs); + return ret; +} + +static uacpi_status assign_mutex(uacpi_object *dst, uacpi_object *src, + enum uacpi_assign_behavior behavior) +{ + if (behavior == UACPI_ASSIGN_BEHAVIOR_DEEP_COPY) { + if (uacpi_likely(mutex_alloc(dst))) { + dst->mutex->sync_level = src->mutex->sync_level; + return UACPI_STATUS_OK; + } + + return UACPI_STATUS_OUT_OF_MEMORY; + } + + dst->mutex = src->mutex; + uacpi_shareable_ref(dst->mutex); + + return UACPI_STATUS_OK; +} + +static uacpi_status assign_event(uacpi_object *dst, uacpi_object *src, + enum uacpi_assign_behavior behavior) +{ + if (behavior == UACPI_ASSIGN_BEHAVIOR_DEEP_COPY) { + if (uacpi_likely(event_alloc(dst))) + return UACPI_STATUS_OK; + + return UACPI_STATUS_OUT_OF_MEMORY; + } + + dst->event = src->event; + uacpi_shareable_ref(dst->event); + + return UACPI_STATUS_OK; +} + +static uacpi_status assign_package(uacpi_object *dst, uacpi_object *src, + enum uacpi_assign_behavior behavior) +{ + if (behavior == UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY) { + dst->package = src->package; + uacpi_shareable_ref(dst->package); + return UACPI_STATUS_OK; + } + + return deep_copy_package(dst, src); +} + +void uacpi_object_attach_child(uacpi_object *parent, uacpi_object *child) +{ + uacpi_u32 refs_to_add; + + parent->inner_object = child; + + if (uacpi_unlikely(uacpi_bugged_shareable(parent))) { + make_chain_bugged(child); + return; + } + + refs_to_add = uacpi_shareable_refcount(parent); + while (refs_to_add--) + uacpi_object_ref(child); +} + +void uacpi_object_detach_child(uacpi_object *parent) +{ + uacpi_u32 refs_to_remove; + uacpi_object *child; + + child = parent->inner_object; + parent->inner_object = UACPI_NULL; + + if (uacpi_unlikely(uacpi_bugged_shareable(parent))) + return; + + refs_to_remove = uacpi_shareable_refcount(parent); + while (refs_to_remove--) + uacpi_object_unref(child); +} + +uacpi_object_type uacpi_object_get_type(uacpi_object *obj) +{ + return obj->type; +} + +uacpi_object_type_bits uacpi_object_get_type_bit(uacpi_object *obj) +{ + return (1u << obj->type); +} + +uacpi_bool uacpi_object_is(uacpi_object *obj, uacpi_object_type type) +{ + return obj->type == type; +} + +uacpi_bool uacpi_object_is_one_of( + uacpi_object *obj, uacpi_object_type_bits type_mask +) +{ + return (uacpi_object_get_type_bit(obj) & type_mask) != 0; +} + +#define TYPE_CHECK_USER_OBJ_RET(obj, type_bits, ret) \ + do { \ + if (uacpi_unlikely(obj == UACPI_NULL || \ + !uacpi_object_is_one_of(obj, type_bits))) \ + return ret; \ + } while (0) + +#define TYPE_CHECK_USER_OBJ(obj, type_bits) \ + TYPE_CHECK_USER_OBJ_RET(obj, type_bits, UACPI_STATUS_INVALID_ARGUMENT) + +#define ENSURE_VALID_USER_OBJ_RET(obj, ret) \ + do { \ + if (uacpi_unlikely(obj == UACPI_NULL)) \ + return ret; \ + } while (0) + +#define ENSURE_VALID_USER_OBJ(obj) \ + ENSURE_VALID_USER_OBJ_RET(obj, UACPI_STATUS_INVALID_ARGUMENT) + +uacpi_status uacpi_object_get_integer(uacpi_object *obj, uacpi_u64 *out) +{ + TYPE_CHECK_USER_OBJ(obj, UACPI_OBJECT_INTEGER_BIT); + + *out = obj->integer; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_object_assign_integer(uacpi_object *obj, uacpi_u64 value) +{ + ENSURE_VALID_USER_OBJ(obj); + + return uacpi_object_assign(obj, &(uacpi_object) { + .type = UACPI_OBJECT_INTEGER, + .integer = value, + }, UACPI_ASSIGN_BEHAVIOR_DEEP_COPY); +} + +static uacpi_status uacpi_object_do_get_string_or_buffer( + uacpi_object *obj, uacpi_data_view *out, uacpi_u32 mask +) +{ + TYPE_CHECK_USER_OBJ(obj, mask); + + out->bytes = obj->buffer->data; + out->length = obj->buffer->size; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_object_get_string_or_buffer( + uacpi_object *obj, uacpi_data_view *out +) +{ + return uacpi_object_do_get_string_or_buffer( + obj, out, UACPI_OBJECT_STRING_BIT | UACPI_OBJECT_BUFFER_BIT + ); +} + +uacpi_status uacpi_object_get_string(uacpi_object *obj, uacpi_data_view *out) +{ + return uacpi_object_do_get_string_or_buffer( + obj, out, UACPI_OBJECT_STRING_BIT + ); +} + +uacpi_status uacpi_object_get_buffer(uacpi_object *obj, uacpi_data_view *out) +{ + return uacpi_object_do_get_string_or_buffer( + obj, out, UACPI_OBJECT_BUFFER_BIT + ); +} + +uacpi_bool uacpi_object_is_aml_namepath(uacpi_object *obj) +{ + TYPE_CHECK_USER_OBJ_RET(obj, UACPI_OBJECT_STRING_BIT, UACPI_FALSE); + return obj->flags == UACPI_STRING_KIND_PATH; +} + +uacpi_status uacpi_object_resolve_as_aml_namepath( + uacpi_object *obj, uacpi_namespace_node *scope, + uacpi_namespace_node **out_node +) +{ + uacpi_status ret; + uacpi_namespace_node *node; + + if (!uacpi_object_is_aml_namepath(obj)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_namespace_node_resolve_from_aml_namepath( + scope, obj->buffer->text, &node + ); + if (uacpi_likely_success(ret)) + *out_node = node; + return ret; +} + +static uacpi_status uacpi_object_do_assign_buffer( + uacpi_object *obj, uacpi_data_view in, uacpi_object_type type +) +{ + uacpi_status ret; + uacpi_object tmp_obj = { + .type = type, + }; + uacpi_size dst_buf_size = in.length; + + ENSURE_VALID_USER_OBJ(obj); + + if (type == UACPI_OBJECT_STRING && (in.length == 0 || + in.const_bytes[in.length - 1] != 0x00)) + dst_buf_size++; + + ret = buffer_alloc_and_store( + &tmp_obj, dst_buf_size, in.const_bytes, in.length + ); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_object_assign( + obj, &tmp_obj, UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY + ); + uacpi_shareable_unref_and_delete_if_last(tmp_obj.buffer, free_buffer); + + return ret; +} + +uacpi_status uacpi_object_assign_string(uacpi_object *obj, uacpi_data_view in) +{ + return uacpi_object_do_assign_buffer(obj, in, UACPI_OBJECT_STRING); +} + +uacpi_status uacpi_object_assign_buffer(uacpi_object *obj, uacpi_data_view in) +{ + return uacpi_object_do_assign_buffer(obj, in, UACPI_OBJECT_BUFFER); +} + +uacpi_object *uacpi_object_create_uninitialized(void) +{ + return uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); +} + +uacpi_status uacpi_object_create_integer_safe( + uacpi_u64 value, uacpi_overflow_behavior behavior, uacpi_object **out_obj +) +{ + uacpi_status ret; + uacpi_u8 bitness; + uacpi_object *obj; + + ret = uacpi_get_aml_bitness(&bitness); + if (uacpi_unlikely_error(ret)) + return ret; + + switch (behavior) { + case UACPI_OVERFLOW_TRUNCATE: + case UACPI_OVERFLOW_DISALLOW: + if (bitness == 32 && value > 0xFFFFFFFF) { + if (behavior == UACPI_OVERFLOW_DISALLOW) + return UACPI_STATUS_INVALID_ARGUMENT; + + value &= 0xFFFFFFFF; + } + UACPI_FALLTHROUGH; + case UACPI_OVERFLOW_ALLOW: + obj = uacpi_object_create_integer(value); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + *out_obj = obj; + return ret; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } +} + +uacpi_object *uacpi_object_create_integer(uacpi_u64 value) +{ + uacpi_object *obj; + + obj = uacpi_create_object(UACPI_OBJECT_INTEGER); + if (uacpi_unlikely(obj == UACPI_NULL)) + return obj; + + obj->integer = value; + return obj; +} + +static uacpi_object *uacpi_object_do_create_string_or_buffer( + uacpi_data_view view, uacpi_object_type type +) +{ + uacpi_status ret; + uacpi_object *obj; + + obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_NULL; + + ret = uacpi_object_do_assign_buffer(obj, view, type); + if (uacpi_unlikely_error(ret)) { + uacpi_object_unref(obj); + return UACPI_NULL; + } + + return obj; +} + +uacpi_object *uacpi_object_create_string(uacpi_data_view view) +{ + return uacpi_object_do_create_string_or_buffer(view, UACPI_OBJECT_STRING); +} + +uacpi_object *uacpi_object_create_buffer(uacpi_data_view view) +{ + return uacpi_object_do_create_string_or_buffer(view, UACPI_OBJECT_BUFFER); +} + +uacpi_object *uacpi_object_create_cstring(const uacpi_char *str) +{ + return uacpi_object_create_string((uacpi_data_view) { + .const_text = str, + .length = uacpi_strlen(str) + 1, + }); +} + +uacpi_status uacpi_object_get_package( + uacpi_object *obj, uacpi_object_array *out +) +{ + TYPE_CHECK_USER_OBJ(obj, UACPI_OBJECT_PACKAGE_BIT); + + out->objects = obj->package->objects; + out->count = obj->package->count; + return UACPI_STATUS_OK; +} + +uacpi_object *uacpi_object_create_reference(uacpi_object *child) +{ + uacpi_object *obj; + + ENSURE_VALID_USER_OBJ_RET(child, UACPI_NULL); + + obj = uacpi_create_object(UACPI_OBJECT_REFERENCE); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_NULL; + + uacpi_object_attach_child(obj, child); + obj->flags = UACPI_REFERENCE_KIND_ARG; + + return obj; +} + +uacpi_status uacpi_object_assign_reference( + uacpi_object *obj, uacpi_object *child +) +{ + uacpi_status ret; + + ENSURE_VALID_USER_OBJ(obj); + ENSURE_VALID_USER_OBJ(child); + + // First clear out the object + ret = uacpi_object_assign( + obj, &(uacpi_object) { .type = UACPI_OBJECT_UNINITIALIZED }, + UACPI_ASSIGN_BEHAVIOR_DEEP_COPY + ); + if (uacpi_unlikely_error(ret)) + return ret; + + obj->type = UACPI_OBJECT_REFERENCE; + uacpi_object_attach_child(obj, child); + obj->flags = UACPI_REFERENCE_KIND_ARG; + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_object_get_dereferenced( + uacpi_object *obj, uacpi_object **out +) +{ + TYPE_CHECK_USER_OBJ(obj, UACPI_OBJECT_REFERENCE_BIT); + + *out = obj->inner_object; + uacpi_shareable_ref(*out); + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_object_get_processor_info( + uacpi_object *obj, uacpi_processor_info *out +) +{ + TYPE_CHECK_USER_OBJ(obj, UACPI_OBJECT_PROCESSOR_BIT); + + out->id = obj->processor->id; + out->block_address = obj->processor->block_address; + out->block_length = obj->processor->block_length; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_object_get_power_resource_info( + uacpi_object *obj, uacpi_power_resource_info *out +) +{ + TYPE_CHECK_USER_OBJ(obj, UACPI_OBJECT_POWER_RESOURCE_BIT); + + out->system_level = obj->power_resource.system_level; + out->resource_order = obj->power_resource.resource_order; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_object_assign_package( + uacpi_object *obj, uacpi_object_array in +) +{ + uacpi_status ret; + uacpi_size i; + uacpi_object tmp_obj = { + .type = UACPI_OBJECT_PACKAGE, + }; + + ENSURE_VALID_USER_OBJ(obj); + + if (uacpi_unlikely(!package_alloc(&tmp_obj, in.count, + UACPI_PREALLOC_OBJECTS_NO))) + return UACPI_STATUS_OUT_OF_MEMORY; + + obj->type = UACPI_OBJECT_PACKAGE; + + for (i = 0; i < in.count; ++i) { + tmp_obj.package->objects[i] = in.objects[i]; + uacpi_object_ref(tmp_obj.package->objects[i]); + } + + ret = uacpi_object_assign(obj, &tmp_obj, UACPI_ASSIGN_BEHAVIOR_SHALLOW_COPY); + uacpi_shareable_unref_and_delete_if_last(tmp_obj.package, free_package); + + return ret; +} + +uacpi_object *uacpi_object_create_package(uacpi_object_array in) +{ + uacpi_status ret; + uacpi_object *obj; + + obj = uacpi_object_create_uninitialized(); + if (uacpi_unlikely(obj == UACPI_NULL)) + return obj; + + ret = uacpi_object_assign_package(obj, in); + if (uacpi_unlikely_error(ret)) { + uacpi_object_unref(obj); + return UACPI_NULL; + } + + return obj; +} + +uacpi_status uacpi_object_assign(uacpi_object *dst, uacpi_object *src, + enum uacpi_assign_behavior behavior) +{ + uacpi_status ret = UACPI_STATUS_OK; + + if (src == dst) + return ret; + + switch (dst->type) { + case UACPI_OBJECT_REFERENCE: + uacpi_object_detach_child(dst); + break; + case UACPI_OBJECT_STRING: + case UACPI_OBJECT_BUFFER: + case UACPI_OBJECT_METHOD: + case UACPI_OBJECT_PACKAGE: + case UACPI_OBJECT_MUTEX: + case UACPI_OBJECT_EVENT: + case UACPI_OBJECT_PROCESSOR: + case UACPI_OBJECT_DEVICE: + case UACPI_OBJECT_THERMAL_ZONE: + free_object_storage(dst); + break; + default: + break; + } + + switch (src->type) { + case UACPI_OBJECT_UNINITIALIZED: + case UACPI_OBJECT_DEBUG: + break; + case UACPI_OBJECT_BUFFER: + case UACPI_OBJECT_STRING: + dst->flags = src->flags; + ret = assign_buffer(dst, src, behavior); + break; + case UACPI_OBJECT_BUFFER_FIELD: + dst->buffer_field = src->buffer_field; + uacpi_shareable_ref(dst->buffer_field.backing); + break; + case UACPI_OBJECT_BUFFER_INDEX: + dst->buffer_index = src->buffer_index; + uacpi_shareable_ref(dst->buffer_index.buffer); + break; + case UACPI_OBJECT_INTEGER: + dst->integer = src->integer; + break; + case UACPI_OBJECT_METHOD: + dst->method = src->method; + uacpi_shareable_ref(dst->method); + break; + case UACPI_OBJECT_MUTEX: + ret = assign_mutex(dst, src, behavior); + break; + case UACPI_OBJECT_EVENT: + ret = assign_event(dst, src, behavior); + break; + case UACPI_OBJECT_OPERATION_REGION: + dst->op_region = src->op_region; + uacpi_shareable_ref(dst->op_region); + break; + case UACPI_OBJECT_PACKAGE: + ret = assign_package(dst, src, behavior); + break; + case UACPI_OBJECT_FIELD_UNIT: + dst->field_unit = src->field_unit; + uacpi_shareable_ref(dst->field_unit); + break; + case UACPI_OBJECT_REFERENCE: + uacpi_object_attach_child(dst, src->inner_object); + break; + case UACPI_OBJECT_PROCESSOR: + dst->processor = src->processor; + uacpi_shareable_ref(dst->processor); + break; + case UACPI_OBJECT_DEVICE: + dst->device = src->device; + uacpi_shareable_ref(dst->device); + break; + case UACPI_OBJECT_THERMAL_ZONE: + dst->thermal_zone = src->thermal_zone; + uacpi_shareable_ref(dst->thermal_zone); + break; + default: + ret = UACPI_STATUS_UNIMPLEMENTED; + } + + if (ret == UACPI_STATUS_OK) + dst->type = src->type; + + return ret; +} + +struct uacpi_object *uacpi_create_internal_reference( + enum uacpi_reference_kind kind, uacpi_object *child +) +{ + uacpi_object *ret; + + ret = uacpi_create_object(UACPI_OBJECT_REFERENCE); + if (uacpi_unlikely(ret == UACPI_NULL)) + return ret; + + ret->flags = kind; + uacpi_object_attach_child(ret, child); + return ret; +} + +uacpi_object *uacpi_unwrap_internal_reference(uacpi_object *object) +{ + for (;;) { + if (object->type != UACPI_OBJECT_REFERENCE || + (object->flags == UACPI_REFERENCE_KIND_REFOF || + object->flags == UACPI_REFERENCE_KIND_PKG_INDEX)) + return object; + + object = object->inner_object; + } +} diff --git a/src/uacpi/uacpi.c b/src/uacpi/uacpi.c new file mode 100644 index 0000000..d08ae31 --- /dev/null +++ b/src/uacpi/uacpi.c @@ -0,0 +1,944 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct uacpi_runtime_context g_uacpi_rt_ctx = { 0 }; + +void uacpi_context_set_log_level(uacpi_log_level lvl) +{ + if (lvl == 0) + lvl = UACPI_DEFAULT_LOG_LEVEL; + + g_uacpi_rt_ctx.log_level = lvl; +} + +void uacpi_logger_initialize(void) +{ + if (g_uacpi_rt_ctx.log_level != 0) + return; + + uacpi_context_set_log_level(UACPI_DEFAULT_LOG_LEVEL); +} + +void uacpi_context_set_loop_timeout(uacpi_u32 seconds) +{ + if (seconds == 0) + seconds = UACPI_DEFAULT_LOOP_TIMEOUT_SECONDS; + + g_uacpi_rt_ctx.loop_timeout_seconds = seconds; +} + +void uacpi_context_set_max_call_stack_depth(uacpi_u32 depth) +{ + if (depth == 0) + depth = UACPI_DEFAULT_MAX_CALL_STACK_DEPTH; + + g_uacpi_rt_ctx.max_call_stack_depth = depth; +} + +uacpi_u32 uacpi_context_get_loop_timeout(void) +{ + return g_uacpi_rt_ctx.loop_timeout_seconds; +} + +void uacpi_context_set_proactive_table_checksum(uacpi_bool setting) +{ + if (setting) + g_uacpi_rt_ctx.flags |= UACPI_FLAG_PROACTIVE_TBL_CSUM; + else + g_uacpi_rt_ctx.flags &= ~UACPI_FLAG_PROACTIVE_TBL_CSUM; +} + +const uacpi_char *uacpi_status_to_string(uacpi_status st) +{ + switch (st) { + case UACPI_STATUS_OK: + return "no error"; + case UACPI_STATUS_MAPPING_FAILED: + return "failed to map memory"; + case UACPI_STATUS_OUT_OF_MEMORY: + return "out of memory"; + case UACPI_STATUS_BAD_CHECKSUM: + return "bad table checksum"; + case UACPI_STATUS_INVALID_SIGNATURE: + return "invalid table signature"; + case UACPI_STATUS_INVALID_TABLE_LENGTH: + return "invalid table length"; + case UACPI_STATUS_NOT_FOUND: + return "not found"; + case UACPI_STATUS_INVALID_ARGUMENT: + return "invalid argument"; + case UACPI_STATUS_UNIMPLEMENTED: + return "unimplemented"; + case UACPI_STATUS_ALREADY_EXISTS: + return "already exists"; + case UACPI_STATUS_INTERNAL_ERROR: + return "internal error"; + case UACPI_STATUS_TYPE_MISMATCH: + return "object type mismatch"; + case UACPI_STATUS_INIT_LEVEL_MISMATCH: + return "init level too low/high for this action"; + case UACPI_STATUS_NAMESPACE_NODE_DANGLING: + return "attempting to use a dangling namespace node"; + case UACPI_STATUS_NO_HANDLER: + return "no handler found"; + case UACPI_STATUS_NO_RESOURCE_END_TAG: + return "resource template without an end tag"; + case UACPI_STATUS_COMPILED_OUT: + return "this functionality has been compiled out of this build"; + case UACPI_STATUS_HARDWARE_TIMEOUT: + return "timed out waiting for hardware response"; + case UACPI_STATUS_TIMEOUT: + return "wait timed out"; + case UACPI_STATUS_OVERRIDDEN: + return "the requested action has been overridden"; + case UACPI_STATUS_DENIED: + return "the requested action has been denied"; + + case UACPI_STATUS_AML_UNDEFINED_REFERENCE: + return "AML referenced an undefined object"; + case UACPI_STATUS_AML_INVALID_NAMESTRING: + return "invalid AML name string"; + case UACPI_STATUS_AML_OBJECT_ALREADY_EXISTS: + return "object already exists"; + case UACPI_STATUS_AML_INVALID_OPCODE: + return "invalid AML opcode"; + case UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE: + return "incompatible AML object type"; + case UACPI_STATUS_AML_BAD_ENCODING: + return "bad AML instruction encoding"; + case UACPI_STATUS_AML_OUT_OF_BOUNDS_INDEX: + return "out of bounds AML index"; + case UACPI_STATUS_AML_SYNC_LEVEL_TOO_HIGH: + return "AML attempted to acquire a mutex with a lower sync level"; + case UACPI_STATUS_AML_INVALID_RESOURCE: + return "invalid resource template encoding or type"; + case UACPI_STATUS_AML_LOOP_TIMEOUT: + return "hanging AML while loop"; + case UACPI_STATUS_AML_CALL_STACK_DEPTH_LIMIT: + return "reached maximum AML call stack depth"; + default: + return ""; + } +} + +#ifndef UACPI_REDUCED_HARDWARE +enum hw_mode { + HW_MODE_ACPI = 0, + HW_MODE_LEGACY = 1, +}; + +static enum hw_mode read_mode(void) +{ + uacpi_status ret; + uacpi_u64 raw_value; + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + + if (!fadt->smi_cmd) + return HW_MODE_ACPI; + + ret = uacpi_read_register_field(UACPI_REGISTER_FIELD_SCI_EN, &raw_value); + if (uacpi_unlikely_error(ret)) + return HW_MODE_LEGACY; + + return raw_value ? HW_MODE_ACPI : HW_MODE_LEGACY; +} + +static uacpi_status set_mode(enum hw_mode mode) +{ + uacpi_status ret; + uacpi_u64 raw_value, stalled_time = 0; + struct acpi_fadt *fadt = &g_uacpi_rt_ctx.fadt; + + if (uacpi_unlikely(!fadt->smi_cmd)) { + uacpi_error("SMI_CMD is not implemented by the firmware\n"); + return UACPI_STATUS_NOT_FOUND; + } + + if (uacpi_unlikely(!fadt->acpi_enable && !fadt->acpi_disable)) { + uacpi_error("mode transition is not implemented by the hardware\n"); + return UACPI_STATUS_NOT_FOUND; + } + + switch (mode) { + case HW_MODE_ACPI: + raw_value = fadt->acpi_enable; + break; + case HW_MODE_LEGACY: + raw_value = fadt->acpi_disable; + break; + default: + return UACPI_STATUS_INVALID_ARGUMENT; + } + + ret = uacpi_write_register(UACPI_REGISTER_SMI_CMD, raw_value); + if (uacpi_unlikely_error(ret)) + return ret; + + // Allow up to 5 seconds for the hardware to enter the desired mode + while (stalled_time < (5 * 1000 * 1000)) { + if (read_mode() == mode) + return UACPI_STATUS_OK; + + uacpi_kernel_stall(100); + stalled_time += 100; + } + + uacpi_error("hardware time out while changing modes\n"); + return UACPI_STATUS_HARDWARE_TIMEOUT; +} + +static uacpi_status enter_mode(enum hw_mode mode, uacpi_bool *did_change) +{ + uacpi_status ret; + const uacpi_char *mode_str; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + if (uacpi_is_hardware_reduced()) + return UACPI_STATUS_OK; + + mode_str = mode == HW_MODE_LEGACY ? "legacy" : "acpi"; + + if (read_mode() == mode) { + uacpi_trace("%s mode already enabled\n", mode_str); + return UACPI_STATUS_OK; + } + + ret = set_mode(mode); + if (uacpi_unlikely_error(ret)) { + uacpi_warn( + "unable to enter %s mode: %s\n", + mode_str, uacpi_status_to_string(ret) + ); + return ret; + } + + uacpi_trace("entered %s mode\n", mode_str); + if (did_change != UACPI_NULL) + *did_change = UACPI_TRUE; + + return ret; +} + +uacpi_status uacpi_enter_acpi_mode(void) +{ + return enter_mode(HW_MODE_ACPI, UACPI_NULL); +} + +uacpi_status uacpi_leave_acpi_mode(void) +{ + return enter_mode(HW_MODE_LEGACY, UACPI_NULL); +} + +static void enter_acpi_mode_initial(void) +{ + enter_mode(HW_MODE_ACPI, &g_uacpi_rt_ctx.was_in_legacy_mode); +} +#else +static void enter_acpi_mode_initial(void) { } +#endif + +uacpi_init_level uacpi_get_current_init_level(void) +{ + return g_uacpi_rt_ctx.init_level; +} + +void uacpi_state_reset(void) +{ + uacpi_deinitialize_namespace(); + uacpi_deinitialize_interfaces(); + uacpi_deinitialize_events(); + uacpi_deinitialize_notify(); + uacpi_deinitialize_opregion(); + uacpi_deininitialize_registers(); + uacpi_deinitialize_tables(); + +#ifndef UACPI_REDUCED_HARDWARE + if (g_uacpi_rt_ctx.was_in_legacy_mode) + uacpi_leave_acpi_mode(); + if (g_uacpi_rt_ctx.global_lock_event) + uacpi_kernel_free_event(g_uacpi_rt_ctx.global_lock_event); + if (g_uacpi_rt_ctx.global_lock_spinlock) + uacpi_kernel_free_spinlock(g_uacpi_rt_ctx.global_lock_spinlock); +#endif + + uacpi_memzero(&g_uacpi_rt_ctx, sizeof(g_uacpi_rt_ctx)); + +#ifdef UACPI_KERNEL_INITIALIZATION + uacpi_kernel_deinitialize(); +#endif +} + +uacpi_status uacpi_initialize(uacpi_u64 flags) +{ + uacpi_status ret; + + UACPI_ENSURE_INIT_LEVEL_IS(UACPI_INIT_LEVEL_EARLY); + +#ifdef UACPI_KERNEL_INITIALIZATION + ret = uacpi_kernel_initialize(UACPI_INIT_LEVEL_EARLY); + if (uacpi_unlikely_error(ret)) + return ret; +#endif + + g_uacpi_rt_ctx.init_level = UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED; + g_uacpi_rt_ctx.last_sleep_typ_a = UACPI_SLEEP_TYP_INVALID; + g_uacpi_rt_ctx.last_sleep_typ_b = UACPI_SLEEP_TYP_INVALID; + g_uacpi_rt_ctx.s0_sleep_typ_a = UACPI_SLEEP_TYP_INVALID; + g_uacpi_rt_ctx.s0_sleep_typ_b = UACPI_SLEEP_TYP_INVALID; + g_uacpi_rt_ctx.flags = flags; + + uacpi_logger_initialize(); + + if (g_uacpi_rt_ctx.loop_timeout_seconds == 0) + uacpi_context_set_loop_timeout(UACPI_DEFAULT_LOOP_TIMEOUT_SECONDS); + if (g_uacpi_rt_ctx.max_call_stack_depth == 0) + uacpi_context_set_max_call_stack_depth(UACPI_DEFAULT_MAX_CALL_STACK_DEPTH); + + ret = uacpi_initialize_tables(); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; + + ret = uacpi_ininitialize_registers(); + if (uacpi_unlikely_error(ret)) + return ret; + + ret = uacpi_initialize_events_early(); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; + + ret = uacpi_initialize_opregion(); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; + + ret = uacpi_initialize_interfaces(); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; + + ret = uacpi_initialize_namespace(); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; + + ret = uacpi_initialize_notify(); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; + + uacpi_install_default_address_space_handlers(); + + if (!uacpi_check_flag(UACPI_FLAG_NO_ACPI_MODE)) + enter_acpi_mode_initial(); + + return UACPI_STATUS_OK; + +out_fatal_error: + uacpi_state_reset(); + return ret; +} + +struct table_load_stats { + uacpi_u32 load_counter; + uacpi_u32 failure_counter; +}; + +static void trace_table_load_failure( + struct acpi_sdt_hdr *tbl, uacpi_log_level lvl, uacpi_status ret +) +{ + uacpi_log_lvl( + lvl, + "failed to load "UACPI_PRI_TBL_HDR": %s\n", + UACPI_FMT_TBL_HDR(tbl), uacpi_status_to_string(ret) + ); +} + +static uacpi_bool match_ssdt_or_psdt(struct uacpi_installed_table *tbl) +{ + if (tbl->flags & UACPI_TABLE_LOADED) + return UACPI_FALSE; + + return uacpi_signatures_match(tbl->hdr.signature, ACPI_SSDT_SIGNATURE) || + uacpi_signatures_match(tbl->hdr.signature, ACPI_PSDT_SIGNATURE); +} + +static uacpi_u64 elapsed_ms(uacpi_u64 begin_ns, uacpi_u64 end_ns) +{ + return (end_ns - begin_ns) / (1000ull * 1000ull); +} + +uacpi_status uacpi_namespace_load(void) +{ + struct uacpi_table tbl; + uacpi_status ret; + uacpi_u64 begin_ts, end_ts; + struct table_load_stats st = { 0 }; + uacpi_size cur_index; + + UACPI_ENSURE_INIT_LEVEL_IS(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + +#ifdef UACPI_KERNEL_INITIALIZATION + ret = uacpi_kernel_initialize(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + if (uacpi_unlikely_error(ret)) + goto out_fatal_error; +#endif + + begin_ts = uacpi_kernel_get_nanoseconds_since_boot(); + + ret = uacpi_table_find_by_signature(ACPI_DSDT_SIGNATURE, &tbl); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to find DSDT: %s\n", uacpi_status_to_string(ret)); + goto out_fatal_error; + } + + ret = uacpi_table_load_with_cause(tbl.index, UACPI_TABLE_LOAD_CAUSE_INIT); + if (uacpi_unlikely_error(ret)) { + trace_table_load_failure(tbl.hdr, UACPI_LOG_ERROR, ret); + st.failure_counter++; + } + st.load_counter++; + uacpi_table_unref(&tbl); + + for (cur_index = 0;; cur_index = tbl.index + 1) { + ret = uacpi_table_match(cur_index, match_ssdt_or_psdt, &tbl); + if (ret != UACPI_STATUS_OK) { + if (uacpi_unlikely(ret != UACPI_STATUS_NOT_FOUND)) + goto out_fatal_error; + + break; + } + + ret = uacpi_table_load_with_cause(tbl.index, UACPI_TABLE_LOAD_CAUSE_INIT); + if (uacpi_unlikely_error(ret)) { + trace_table_load_failure(tbl.hdr, UACPI_LOG_WARN, ret); + st.failure_counter++; + } + st.load_counter++; + uacpi_table_unref(&tbl); + } + + end_ts = uacpi_kernel_get_nanoseconds_since_boot(); + + if (uacpi_unlikely(st.failure_counter != 0)) { + uacpi_info( + "loaded %u AML blob%s in %"UACPI_PRIu64"ms (%u error%s)\n", + st.load_counter, st.load_counter > 1 ? "s" : "", + UACPI_FMT64(elapsed_ms(begin_ts, end_ts)), st.failure_counter, + st.failure_counter > 1 ? "s" : "" + ); + } else { + uacpi_u64 ops = g_uacpi_rt_ctx.opcodes_executed; + uacpi_u64 ops_per_sec = ops * UACPI_NANOSECONDS_PER_SEC; + + if (uacpi_likely(end_ts > begin_ts)) + ops_per_sec /= end_ts - begin_ts; + + uacpi_info( + "successfully loaded %u AML blob%s, %"UACPI_PRIu64" ops in " + "%"UACPI_PRIu64"ms (avg %"UACPI_PRIu64"/s)\n", + st.load_counter, st.load_counter > 1 ? "s" : "", + UACPI_FMT64(ops), UACPI_FMT64(elapsed_ms(begin_ts, end_ts)), + UACPI_FMT64(ops_per_sec) + ); + } + + ret = uacpi_initialize_events(); + if (uacpi_unlikely_error(ret)) { + uacpi_error("event initialization failed: %s\n", + uacpi_status_to_string(ret)); + goto out_fatal_error; + } + + g_uacpi_rt_ctx.init_level = UACPI_INIT_LEVEL_NAMESPACE_LOADED; + return UACPI_STATUS_OK; + +out_fatal_error: + uacpi_state_reset(); + return ret; +} + +struct ns_init_context { + uacpi_size ini_executed; + uacpi_size ini_errors; + uacpi_size sta_executed; + uacpi_size sta_errors; + uacpi_size devices; + uacpi_size thermal_zones; +}; + +static void ini_eval(struct ns_init_context *ctx, uacpi_namespace_node *node) +{ + uacpi_status ret; + + ret = uacpi_eval(node, "_INI", UACPI_NULL, UACPI_NULL); + if (ret == UACPI_STATUS_NOT_FOUND) + return; + + ctx->ini_executed++; + if (uacpi_unlikely_error(ret)) + ctx->ini_errors++; +} + +static uacpi_status sta_eval( + struct ns_init_context *ctx, uacpi_namespace_node *node, + uacpi_u32 *value +) +{ + uacpi_status ret; + + ret = uacpi_eval_sta(node, value); + if (*value == 0xFFFFFFFF) + return ret; + + ctx->sta_executed++; + if (uacpi_unlikely_error(ret)) + ctx->sta_errors++; + + return ret; +} + +static uacpi_iteration_decision do_sta_ini( + void *opaque, uacpi_namespace_node *node, uacpi_u32 depth +) +{ + struct ns_init_context *ctx = opaque; + uacpi_status ret; + uacpi_object_type type = UACPI_OBJECT_UNINITIALIZED; + uacpi_u32 sta_ret; + + UACPI_UNUSED(depth); + + // We don't care about aliases + if (uacpi_namespace_node_is_alias(node)) + return UACPI_ITERATION_DECISION_NEXT_PEER; + + ret = uacpi_namespace_node_type(node, &type); + switch (type) { + case UACPI_OBJECT_DEVICE: + case UACPI_OBJECT_PROCESSOR: + ctx->devices++; + break; + case UACPI_OBJECT_THERMAL_ZONE: + ctx->thermal_zones++; + break; + default: + if (node != uacpi_namespace_get_predefined(UACPI_PREDEFINED_NAMESPACE_TZ)) + return UACPI_ITERATION_DECISION_CONTINUE; + } + + ret = sta_eval(ctx, node, &sta_ret); + if (uacpi_unlikely_error(ret)) + return UACPI_ITERATION_DECISION_CONTINUE; + + if (!(sta_ret & ACPI_STA_RESULT_DEVICE_PRESENT)) { + if (!(sta_ret & ACPI_STA_RESULT_DEVICE_FUNCTIONING)) + return UACPI_ITERATION_DECISION_NEXT_PEER; + + /* + * ACPI 6.5 specification: + * _STA may return bit 0 clear (not present) with bit [3] set (device + * is functional). This case is used to indicate a valid device for + * which no device driver should be loaded (for example, a bridge + * device.) Children of this device may be present and valid. OSPM + * should continue enumeration below a device whose _STA returns this + * bit combination. + */ + return UACPI_ITERATION_DECISION_CONTINUE; + } + + ini_eval(ctx, node); + + return UACPI_ITERATION_DECISION_CONTINUE; +} + +uacpi_status uacpi_namespace_initialize(void) +{ + struct ns_init_context ctx = { 0 }; + uacpi_namespace_node *root; + uacpi_u64 begin_ts, end_ts; + uacpi_address_space_handlers *handlers; + uacpi_address_space_handler *handler; + uacpi_status ret = UACPI_STATUS_OK; + + UACPI_ENSURE_INIT_LEVEL_IS(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + +#ifdef UACPI_KERNEL_INITIALIZATION + ret = uacpi_kernel_initialize(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + if (uacpi_unlikely_error(ret)) + goto out; +#endif + + /* + * Initialization order here is identical to ACPICA because ACPI + * specification doesn't really have any detailed steps that explain + * how to do it. + */ + + root = uacpi_namespace_root(); + + begin_ts = uacpi_kernel_get_nanoseconds_since_boot(); + + // Step 1 - Execute \_INI + ini_eval(&ctx, root); + + // Step 2 - Execute \_SB._INI + ini_eval( + &ctx, uacpi_namespace_get_predefined(UACPI_PREDEFINED_NAMESPACE_SB) + ); + + /* + * Step 3 - Run _REG methods for all globally installed + * address space handlers. + */ + handlers = uacpi_node_get_address_space_handlers(root); + if (handlers) { + handler = handlers->head; + + while (handler) { + if (uacpi_address_space_handler_is_default(handler)) + uacpi_reg_all_opregions(root, handler->space); + + handler = handler->next; + } + } + + // Step 4 - Run all other _STA and _INI methods + uacpi_namespace_for_each_child( + root, do_sta_ini, UACPI_NULL, + UACPI_OBJECT_ANY_BIT, UACPI_MAX_DEPTH_ANY, &ctx + ); + + end_ts = uacpi_kernel_get_nanoseconds_since_boot(); + + uacpi_info( + "namespace initialization done in %"UACPI_PRIu64"ms: " + "%zu devices, %zu thermal zones\n", + UACPI_FMT64(elapsed_ms(begin_ts, end_ts)), + ctx.devices, ctx.thermal_zones + ); + + uacpi_trace( + "_STA calls: %zu (%zu errors), _INI calls: %zu (%zu errors)\n", + ctx.sta_executed, ctx.sta_errors, ctx.ini_executed, + ctx.ini_errors + ); + + g_uacpi_rt_ctx.init_level = UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED; +#ifdef UACPI_KERNEL_INITIALIZATION + ret = uacpi_kernel_initialize(UACPI_INIT_LEVEL_NAMESPACE_INITIALIZED); +out: + if (uacpi_unlikely_error(ret)) + uacpi_state_reset(); +#endif + return ret; +} + +uacpi_status uacpi_eval( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **out_obj +) +{ + struct uacpi_namespace_node *node; + uacpi_control_method *method; + uacpi_object *obj; + uacpi_status ret = UACPI_STATUS_INVALID_ARGUMENT; + + if (uacpi_unlikely(parent == UACPI_NULL && path == UACPI_NULL)) + return ret; + + ret = uacpi_namespace_read_lock(); + if (uacpi_unlikely_error(ret)) + return ret; + + if (path != UACPI_NULL) { + ret = uacpi_namespace_node_resolve( + parent, path, UACPI_SHOULD_LOCK_NO, + UACPI_MAY_SEARCH_ABOVE_PARENT_NO, UACPI_PERMANENT_ONLY_YES, + &node + ); + if (uacpi_unlikely_error(ret)) + goto out_read_unlock; + } else { + node = parent; + } + + obj = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(obj == UACPI_NULL)) { + ret = UACPI_STATUS_INVALID_ARGUMENT; + goto out_read_unlock; + } + + if (obj->type != UACPI_OBJECT_METHOD) { + uacpi_object *new_obj; + + if (uacpi_unlikely(out_obj == UACPI_NULL)) + goto out_read_unlock; + + new_obj = uacpi_create_object(UACPI_OBJECT_UNINITIALIZED); + if (uacpi_unlikely(new_obj == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out_read_unlock; + } + + ret = uacpi_object_assign( + new_obj, obj, UACPI_ASSIGN_BEHAVIOR_DEEP_COPY + ); + if (uacpi_unlikely_error(ret)) { + uacpi_object_unref(new_obj); + goto out_read_unlock; + } + *out_obj = new_obj; + + out_read_unlock: + uacpi_namespace_read_unlock(); + return ret; + } + + method = obj->method; + uacpi_shareable_ref(method); + uacpi_namespace_read_unlock(); + + // Upgrade to a write-lock since we're about to run a method + ret = uacpi_namespace_write_lock(); + if (uacpi_unlikely_error(ret)) + goto out_no_write_lock; + + ret = uacpi_execute_control_method(node, method, args, out_obj); + uacpi_namespace_write_unlock(); + +out_no_write_lock: + uacpi_method_unref(method); + return ret; +} + +uacpi_status uacpi_eval_simple( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +) +{ + return uacpi_eval(parent, path, UACPI_NULL, ret); +} + +uacpi_status uacpi_execute( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args +) +{ + return uacpi_eval(parent, path, args, UACPI_NULL); +} + +uacpi_status uacpi_execute_simple( + uacpi_namespace_node *parent, const uacpi_char *path +) +{ + return uacpi_eval(parent, path, UACPI_NULL, UACPI_NULL); +} + +#define TRACE_BAD_RET(path_fmt, type, ...) \ + uacpi_warn( \ + "unexpected '%s' object returned by method "path_fmt \ + ", expected type mask: %08X\n", uacpi_object_type_to_string(type), \ + __VA_ARGS__ \ + ) + +#define TRACE_NO_RET(path_fmt, ...) \ + uacpi_warn( \ + "no value returned from method "path_fmt", expected type mask: " \ + "%08X\n", __VA_ARGS__ \ + ) + +static void trace_invalid_return_type( + uacpi_namespace_node *parent, const uacpi_char *path, + uacpi_object_type_bits expected_mask, uacpi_object_type actual_type +) +{ + const uacpi_char *abs_path; + uacpi_bool dynamic_abs_path = UACPI_FALSE; + + if (parent == UACPI_NULL || (path != UACPI_NULL && path[0] == '\\')) { + abs_path = path; + } else { + abs_path = uacpi_namespace_node_generate_absolute_path(parent); + dynamic_abs_path = UACPI_TRUE; + } + + if (dynamic_abs_path && path != UACPI_NULL) { + if (actual_type == UACPI_OBJECT_UNINITIALIZED) + TRACE_NO_RET("%s.%s", abs_path, path, expected_mask); + else + TRACE_BAD_RET("%s.%s", actual_type, abs_path, path, expected_mask); + } else { + if (actual_type == UACPI_OBJECT_UNINITIALIZED) { + TRACE_NO_RET("%s", abs_path, expected_mask); + } else { + TRACE_BAD_RET("%s", actual_type, abs_path, expected_mask); + } + } + + if (dynamic_abs_path) + uacpi_free_dynamic_string(abs_path); +} + +uacpi_status uacpi_eval_typed( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object_type_bits ret_mask, + uacpi_object **out_obj +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_object_type returned_type = UACPI_OBJECT_UNINITIALIZED; + + if (uacpi_unlikely(out_obj == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_eval(parent, path, args, &obj); + if (uacpi_unlikely_error(ret)) + return ret; + + if (obj != UACPI_NULL) + returned_type = obj->type; + + if (ret_mask && (ret_mask & (1 << returned_type)) == 0) { + trace_invalid_return_type(parent, path, ret_mask, returned_type); + uacpi_object_unref(obj); + return UACPI_STATUS_TYPE_MISMATCH; + } + + *out_obj = obj; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_eval_simple_typed( + uacpi_namespace_node *parent, const uacpi_char *path, + uacpi_object_type_bits ret_mask, uacpi_object **ret +) +{ + return uacpi_eval_typed(parent, path, UACPI_NULL, ret_mask, ret); +} + +uacpi_status uacpi_eval_integer( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_u64 *out_value +) +{ + uacpi_object *int_obj; + uacpi_status ret; + + ret = uacpi_eval_typed( + parent, path, args, UACPI_OBJECT_INTEGER_BIT, &int_obj + ); + if (uacpi_unlikely_error(ret)) + return ret; + + *out_value = int_obj->integer; + uacpi_object_unref(int_obj); + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_eval_simple_integer( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_u64 *out_value +) +{ + return uacpi_eval_integer(parent, path, UACPI_NULL, out_value); +} + +uacpi_status uacpi_eval_buffer_or_string( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, args, + UACPI_OBJECT_BUFFER_BIT | UACPI_OBJECT_STRING_BIT, + ret + ); +} + +uacpi_status uacpi_eval_simple_buffer_or_string( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, UACPI_NULL, + UACPI_OBJECT_BUFFER_BIT | UACPI_OBJECT_STRING_BIT, + ret + ); +} + +uacpi_status uacpi_eval_string( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, args, UACPI_OBJECT_STRING_BIT, ret + ); +} + +uacpi_status uacpi_eval_simple_string( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, UACPI_NULL, UACPI_OBJECT_STRING_BIT, ret + ); +} + +uacpi_status uacpi_eval_buffer( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, args, UACPI_OBJECT_BUFFER_BIT, ret + ); +} + +uacpi_status uacpi_eval_simple_buffer( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, UACPI_NULL, UACPI_OBJECT_BUFFER_BIT, ret + ); +} + +uacpi_status uacpi_eval_package( + uacpi_namespace_node *parent, const uacpi_char *path, + const uacpi_object_array *args, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, args, UACPI_OBJECT_PACKAGE_BIT, ret + ); +} + +uacpi_status uacpi_eval_simple_package( + uacpi_namespace_node *parent, const uacpi_char *path, uacpi_object **ret +) +{ + return uacpi_eval_typed( + parent, path, UACPI_NULL, UACPI_OBJECT_PACKAGE_BIT, ret + ); +} + +uacpi_status uacpi_get_aml_bitness(uacpi_u8 *out_bitness) +{ + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED); + + *out_bitness = g_uacpi_rt_ctx.is_rev1 ? 32 : 64; + return UACPI_STATUS_OK; +} diff --git a/src/uacpi/utilities.c b/src/uacpi/utilities.c new file mode 100644 index 0000000..33da1c1 --- /dev/null +++ b/src/uacpi/utilities.c @@ -0,0 +1,1139 @@ +#include +#include +#include + +#include +#include +#include +#include + +void uacpi_eisa_id_to_string(uacpi_u32 id, uacpi_char *out_string) +{ + static uacpi_char hex_to_ascii[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F' + }; + + /* + * For whatever reason bits are encoded upper to lower here, swap + * them around so that we don't have to do ridiculous bit shifts + * everywhere. + */ + union { + uacpi_u8 bytes[4]; + uacpi_u32 dword; + } orig, swapped; + + orig.dword = id; + swapped.bytes[0] = orig.bytes[3]; + swapped.bytes[1] = orig.bytes[2]; + swapped.bytes[2] = orig.bytes[1]; + swapped.bytes[3] = orig.bytes[0]; + + /* + * Bit 16 - 20: 3rd character (- 0x40) of mfg code + * Bit 21 - 25: 2nd character (- 0x40) of mfg code + * Bit 26 - 30: 1st character (- 0x40) of mfg code + */ + out_string[0] = (uacpi_char)(0x40 + ((swapped.dword >> 26) & 0x1F)); + out_string[1] = (uacpi_char)(0x40 + ((swapped.dword >> 21) & 0x1F)); + out_string[2] = (uacpi_char)(0x40 + ((swapped.dword >> 16) & 0x1F)); + + /* + * Bit 0 - 3 : 4th hex digit of product number + * Bit 4 - 7 : 3rd hex digit of product number + * Bit 8 - 11: 2nd hex digit of product number + * Bit 12 - 15: 1st hex digit of product number + */ + out_string[3] = hex_to_ascii[(swapped.dword >> 12) & 0x0F]; + out_string[4] = hex_to_ascii[(swapped.dword >> 8 ) & 0x0F]; + out_string[5] = hex_to_ascii[(swapped.dword >> 4 ) & 0x0F]; + out_string[6] = hex_to_ascii[(swapped.dword >> 0 ) & 0x0F]; + + out_string[7] = '\0'; +} + +enum char_type { + CHAR_TYPE_CONTROL = 1 << 0, + CHAR_TYPE_SPACE = 1 << 1, + CHAR_TYPE_BLANK = 1 << 2, + CHAR_TYPE_PUNCTUATION = 1 << 3, + CHAR_TYPE_LOWER = 1 << 4, + CHAR_TYPE_UPPER = 1 << 5, + CHAR_TYPE_DIGIT = 1 << 6, + CHAR_TYPE_HEX_DIGIT = 1 << 7, + CHAR_TYPE_ALPHA = CHAR_TYPE_LOWER | CHAR_TYPE_UPPER, + CHAR_TYPE_ALHEX = CHAR_TYPE_ALPHA | CHAR_TYPE_HEX_DIGIT, + CHAR_TYPE_ALNUM = CHAR_TYPE_ALPHA | CHAR_TYPE_DIGIT, +}; + +static const uacpi_u8 ascii_map[256] = { + CHAR_TYPE_CONTROL, // 0 + CHAR_TYPE_CONTROL, // 1 + CHAR_TYPE_CONTROL, // 2 + CHAR_TYPE_CONTROL, // 3 + CHAR_TYPE_CONTROL, // 4 + CHAR_TYPE_CONTROL, // 5 + CHAR_TYPE_CONTROL, // 6 + CHAR_TYPE_CONTROL, // 7 + CHAR_TYPE_CONTROL, // -> 8 control codes + + CHAR_TYPE_CONTROL | CHAR_TYPE_SPACE | CHAR_TYPE_BLANK, // 9 tab + + CHAR_TYPE_CONTROL | CHAR_TYPE_SPACE, // 10 + CHAR_TYPE_CONTROL | CHAR_TYPE_SPACE, // 11 + CHAR_TYPE_CONTROL | CHAR_TYPE_SPACE, // 12 + CHAR_TYPE_CONTROL | CHAR_TYPE_SPACE, // -> 13 whitespaces + + CHAR_TYPE_CONTROL, // 14 + CHAR_TYPE_CONTROL, // 15 + CHAR_TYPE_CONTROL, // 16 + CHAR_TYPE_CONTROL, // 17 + CHAR_TYPE_CONTROL, // 18 + CHAR_TYPE_CONTROL, // 19 + CHAR_TYPE_CONTROL, // 20 + CHAR_TYPE_CONTROL, // 21 + CHAR_TYPE_CONTROL, // 22 + CHAR_TYPE_CONTROL, // 23 + CHAR_TYPE_CONTROL, // 24 + CHAR_TYPE_CONTROL, // 25 + CHAR_TYPE_CONTROL, // 26 + CHAR_TYPE_CONTROL, // 27 + CHAR_TYPE_CONTROL, // 28 + CHAR_TYPE_CONTROL, // 29 + CHAR_TYPE_CONTROL, // 30 + CHAR_TYPE_CONTROL, // -> 31 control codes + + CHAR_TYPE_SPACE | CHAR_TYPE_BLANK, // 32 space + + CHAR_TYPE_PUNCTUATION, // 33 + CHAR_TYPE_PUNCTUATION, // 34 + CHAR_TYPE_PUNCTUATION, // 35 + CHAR_TYPE_PUNCTUATION, // 36 + CHAR_TYPE_PUNCTUATION, // 37 + CHAR_TYPE_PUNCTUATION, // 38 + CHAR_TYPE_PUNCTUATION, // 39 + CHAR_TYPE_PUNCTUATION, // 40 + CHAR_TYPE_PUNCTUATION, // 41 + CHAR_TYPE_PUNCTUATION, // 42 + CHAR_TYPE_PUNCTUATION, // 43 + CHAR_TYPE_PUNCTUATION, // 44 + CHAR_TYPE_PUNCTUATION, // 45 + CHAR_TYPE_PUNCTUATION, // 46 + CHAR_TYPE_PUNCTUATION, // -> 47 punctuation + + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 48 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 49 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 50 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 51 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 52 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 53 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 54 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 55 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // 56 + CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT, // -> 57 digits + + CHAR_TYPE_PUNCTUATION, // 58 + CHAR_TYPE_PUNCTUATION, // 59 + CHAR_TYPE_PUNCTUATION, // 60 + CHAR_TYPE_PUNCTUATION, // 61 + CHAR_TYPE_PUNCTUATION, // 62 + CHAR_TYPE_PUNCTUATION, // 63 + CHAR_TYPE_PUNCTUATION, // -> 64 punctuation + + CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT, // 65 + CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT, // 66 + CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT, // 67 + CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT, // 68 + CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT, // 69 + CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT, // -> 70 ABCDEF + + CHAR_TYPE_UPPER, // 71 + CHAR_TYPE_UPPER, // 72 + CHAR_TYPE_UPPER, // 73 + CHAR_TYPE_UPPER, // 74 + CHAR_TYPE_UPPER, // 75 + CHAR_TYPE_UPPER, // 76 + CHAR_TYPE_UPPER, // 77 + CHAR_TYPE_UPPER, // 78 + CHAR_TYPE_UPPER, // 79 + CHAR_TYPE_UPPER, // 80 + CHAR_TYPE_UPPER, // 81 + CHAR_TYPE_UPPER, // 82 + CHAR_TYPE_UPPER, // 83 + CHAR_TYPE_UPPER, // 84 + CHAR_TYPE_UPPER, // 85 + CHAR_TYPE_UPPER, // 86 + CHAR_TYPE_UPPER, // 87 + CHAR_TYPE_UPPER, // 88 + CHAR_TYPE_UPPER, // 89 + CHAR_TYPE_UPPER, // -> 90 the rest of UPPERCASE alphabet + + CHAR_TYPE_PUNCTUATION, // 91 + CHAR_TYPE_PUNCTUATION, // 92 + CHAR_TYPE_PUNCTUATION, // 93 + CHAR_TYPE_PUNCTUATION, // 94 + CHAR_TYPE_PUNCTUATION, // 95 + CHAR_TYPE_PUNCTUATION, // -> 96 punctuation + + CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT, // 97 + CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT, // 98 + CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT, // 99 + CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT, // 100 + CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT, // 101 + CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT, // -> 102 abcdef + + CHAR_TYPE_LOWER, // 103 + CHAR_TYPE_LOWER, // 104 + CHAR_TYPE_LOWER, // 105 + CHAR_TYPE_LOWER, // 106 + CHAR_TYPE_LOWER, // 107 + CHAR_TYPE_LOWER, // 108 + CHAR_TYPE_LOWER, // 109 + CHAR_TYPE_LOWER, // 110 + CHAR_TYPE_LOWER, // 111 + CHAR_TYPE_LOWER, // 112 + CHAR_TYPE_LOWER, // 113 + CHAR_TYPE_LOWER, // 114 + CHAR_TYPE_LOWER, // 115 + CHAR_TYPE_LOWER, // 116 + CHAR_TYPE_LOWER, // 117 + CHAR_TYPE_LOWER, // 118 + CHAR_TYPE_LOWER, // 119 + CHAR_TYPE_LOWER, // 120 + CHAR_TYPE_LOWER, // 121 + CHAR_TYPE_LOWER, // -> 122 the rest of UPPERCASE alphabet + + CHAR_TYPE_PUNCTUATION, // 123 + CHAR_TYPE_PUNCTUATION, // 124 + CHAR_TYPE_PUNCTUATION, // 125 + CHAR_TYPE_PUNCTUATION, // -> 126 punctuation + + CHAR_TYPE_CONTROL // 127 backspace +}; + +static inline uacpi_bool is_valid_name_byte(uacpi_u8 c) +{ + // ‘_’ := 0x5F + if (c == 0x5F) + return UACPI_TRUE; + + /* + * LeadNameChar := ‘A’-‘Z’ | ‘_’ + * DigitChar := ‘0’ - ‘9’ + * NameChar := DigitChar | LeadNameChar + * ‘A’-‘Z’ := 0x41 - 0x5A + * ‘0’-‘9’ := 0x30 - 0x39 + */ + return (ascii_map[c] & (CHAR_TYPE_DIGIT | CHAR_TYPE_UPPER)) != 0; +} + +uacpi_bool uacpi_is_valid_nameseg(uacpi_u8 *nameseg) +{ + return is_valid_name_byte(nameseg[0]) && + is_valid_name_byte(nameseg[1]) && + is_valid_name_byte(nameseg[2]) && + is_valid_name_byte(nameseg[3]); +} + +static uacpi_bool is_char(uacpi_char c, enum char_type type) +{ + return (ascii_map[(uacpi_u8)c] & type) == type; +} + +static uacpi_char to_lower(uacpi_char c) +{ + if (is_char(c, CHAR_TYPE_UPPER)) + return c + ('a' - 'A'); + + return c; +} + +static uacpi_bool peek_one( + const uacpi_char **str, const uacpi_size *size, uacpi_char *out_char +) +{ + if (*size == 0) + return UACPI_FALSE; + + *out_char = **str; + return UACPI_TRUE; +} + +static uacpi_bool consume_one( + const uacpi_char **str, uacpi_size *size, uacpi_char *out_char +) +{ + if (!peek_one(str, size, out_char)) + return UACPI_FALSE; + + *str += 1; + *size -= 1; + return UACPI_TRUE; +} + +static uacpi_bool consume_if( + const uacpi_char **str, uacpi_size *size, enum char_type type +) +{ + uacpi_char c; + + if (!peek_one(str, size, &c) || !is_char(c, type)) + return UACPI_FALSE; + + *str += 1; + *size -= 1; + return UACPI_TRUE; +} + +static uacpi_bool consume_if_equals( + const uacpi_char **str, uacpi_size *size, uacpi_char c +) +{ + uacpi_char c1; + + if (!peek_one(str, size, &c1) || to_lower(c1) != c) + return UACPI_FALSE; + + *str += 1; + *size -= 1; + return UACPI_TRUE; +} + +uacpi_status uacpi_string_to_integer( + const uacpi_char *str, uacpi_size max_chars, enum uacpi_base base, + uacpi_u64 *out_value +) +{ + uacpi_status ret = UACPI_STATUS_INVALID_ARGUMENT; + uacpi_bool negative = UACPI_FALSE; + uacpi_u64 next, value = 0; + uacpi_char c = '\0'; + + while (consume_if(&str, &max_chars, CHAR_TYPE_SPACE)); + + if (consume_if_equals(&str, &max_chars, '-')) + negative = UACPI_TRUE; + else + consume_if_equals(&str, &max_chars, '+'); + + if (base == UACPI_BASE_AUTO) { + base = UACPI_BASE_DEC; + + if (consume_if_equals(&str, &max_chars, '0')) { + base = UACPI_BASE_OCT; + if (consume_if_equals(&str, &max_chars, 'x')) + base = UACPI_BASE_HEX; + } + } + + while (consume_one(&str, &max_chars, &c)) { + switch (ascii_map[(uacpi_u8)c] & (CHAR_TYPE_DIGIT | CHAR_TYPE_ALHEX)) { + case CHAR_TYPE_DIGIT | CHAR_TYPE_HEX_DIGIT: + next = c - '0'; + if (base == UACPI_BASE_OCT && next > 7) + goto out; + break; + case CHAR_TYPE_LOWER | CHAR_TYPE_HEX_DIGIT: + case CHAR_TYPE_UPPER | CHAR_TYPE_HEX_DIGIT: + if (base != UACPI_BASE_HEX) + goto out; + next = 10 + (to_lower(c) - 'a'); + break; + default: + goto out; + } + + next = (value * base) + next; + if ((next / base) != value) { + value = 0xFFFFFFFFFFFFFFFF; + goto out; + } + + value = next; + } + +out: + if (negative) + value = -((uacpi_i64)value); + + *out_value = value; + if (max_chars == 0 || c == '\0') + ret = UACPI_STATUS_OK; + + return ret; +} + +#define PNP_ID_LENGTH 8 + +uacpi_status uacpi_eval_hid(uacpi_namespace_node *node, uacpi_id_string **out_id) +{ + uacpi_status ret; + uacpi_object *hid_ret; + uacpi_id_string *id = UACPI_NULL; + uacpi_u32 size; + + ret = uacpi_eval_typed( + node, "_HID", UACPI_NULL, + UACPI_OBJECT_INTEGER_BIT | UACPI_OBJECT_STRING_BIT, + &hid_ret + ); + if (ret != UACPI_STATUS_OK) + return ret; + + size = sizeof(uacpi_id_string); + + switch (hid_ret->type) { + case UACPI_OBJECT_STRING: { + uacpi_buffer *buf = hid_ret->buffer; + + size += buf->size; + if (uacpi_unlikely(buf->size == 0 || size < buf->size)) { + uacpi_error( + "%.4s._HID: empty/invalid EISA ID string (%zu bytes)\n", + uacpi_namespace_node_name(node).text, buf->size + ); + ret = UACPI_STATUS_AML_BAD_ENCODING; + break; + } + + id = uacpi_kernel_alloc(size); + if (uacpi_unlikely(id == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + break; + } + id->size = buf->size; + id->value = UACPI_PTR_ADD(id, sizeof(uacpi_id_string)); + + uacpi_memcpy(id->value, buf->text, buf->size); + id->value[buf->size - 1] = '\0'; + break; + } + + case UACPI_OBJECT_INTEGER: + size += PNP_ID_LENGTH; + + id = uacpi_kernel_alloc(size); + if (uacpi_unlikely(id == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + break; + } + id->size = PNP_ID_LENGTH; + id->value = UACPI_PTR_ADD(id, sizeof(uacpi_id_string)); + + uacpi_eisa_id_to_string(hid_ret->integer, id->value); + break; + } + + uacpi_object_unref(hid_ret); + if (uacpi_likely_success(ret)) + *out_id = id; + return ret; +} + +void uacpi_free_id_string(uacpi_id_string *id) +{ + if (id == UACPI_NULL) + return; + + uacpi_free(id, sizeof(uacpi_id_string) + id->size); +} + +uacpi_status uacpi_eval_cid( + uacpi_namespace_node *node, uacpi_pnp_id_list **out_list +) +{ + uacpi_status ret; + uacpi_object *object, *cid_ret; + uacpi_object **objects; + uacpi_size num_ids, i; + uacpi_u32 size; + uacpi_id_string *id; + uacpi_char *id_buffer; + uacpi_pnp_id_list *list; + + ret = uacpi_eval_typed( + node, "_CID", UACPI_NULL, + UACPI_OBJECT_INTEGER_BIT | UACPI_OBJECT_STRING_BIT | + UACPI_OBJECT_PACKAGE_BIT, + &cid_ret + ); + if (ret != UACPI_STATUS_OK) + return ret; + + switch (cid_ret->type) { + case UACPI_OBJECT_PACKAGE: + objects = cid_ret->package->objects; + num_ids = cid_ret->package->count; + break; + default: + objects = &cid_ret; + num_ids = 1; + break; + } + + size = sizeof(uacpi_pnp_id_list); + size += num_ids * sizeof(uacpi_id_string); + + for (i = 0; i < num_ids; ++i) { + object = objects[i]; + + switch (object->type) { + case UACPI_OBJECT_STRING: { + uacpi_size buf_size = object->buffer->size; + + if (uacpi_unlikely(buf_size == 0)) { + uacpi_error( + "%.4s._CID: empty EISA ID string (sub-object %zu)\n", + uacpi_namespace_node_name(node).text, i + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + + size += buf_size; + if (uacpi_unlikely(size < buf_size)) { + uacpi_error( + "%.4s._CID: buffer size overflow (+ %zu)\n", + uacpi_namespace_node_name(node).text, buf_size + ); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + break; + } + + case UACPI_OBJECT_INTEGER: + size += PNP_ID_LENGTH; + break; + default: + uacpi_error( + "%.4s._CID: invalid package sub-object %zu type: %s\n", + uacpi_namespace_node_name(node).text, i, + uacpi_object_type_to_string(object->type) + ); + return UACPI_STATUS_AML_INCOMPATIBLE_OBJECT_TYPE; + } + } + + list = uacpi_kernel_alloc(size); + if (uacpi_unlikely(list == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + list->num_ids = num_ids; + list->size = size - sizeof(uacpi_pnp_id_list); + + id_buffer = UACPI_PTR_ADD(list, sizeof(uacpi_pnp_id_list)); + id_buffer += num_ids * sizeof(uacpi_id_string); + + for (i = 0; i < num_ids; ++i) { + object = objects[i]; + id = &list->ids[i]; + + switch (object->type) { + case UACPI_OBJECT_STRING: { + uacpi_buffer *buf = object->buffer; + + id->size = buf->size; + id->value = id_buffer; + + uacpi_memcpy(id->value, buf->text, id->size); + id->value[id->size - 1] = '\0'; + break; + } + + case UACPI_OBJECT_INTEGER: + id->size = PNP_ID_LENGTH; + id->value = id_buffer; + uacpi_eisa_id_to_string(object->integer, id_buffer); + break; + } + + id_buffer += id->size; + } + + uacpi_object_unref(cid_ret); + *out_list = list; + return ret; +} + +void uacpi_free_pnp_id_list(uacpi_pnp_id_list *list) +{ + if (list == UACPI_NULL) + return; + + uacpi_free(list, sizeof(uacpi_pnp_id_list) + list->size); +} + +uacpi_status uacpi_eval_sta(uacpi_namespace_node *node, uacpi_u32 *flags) +{ + uacpi_status ret; + uacpi_u64 value = 0; + + ret = uacpi_eval_integer(node, "_STA", UACPI_NULL, &value); + + /* + * ACPI 6.5 specification: + * If a device object (including the processor object) does not have + * an _STA object, then OSPM assumes that all of the above bits are + * set (i.e., the device is present, enabled, shown in the UI, + * and functioning). + */ + if (ret == UACPI_STATUS_NOT_FOUND) { + value = 0xFFFFFFFF; + ret = UACPI_STATUS_OK; + } + + *flags = value; + return ret; +} + +uacpi_status uacpi_eval_adr(uacpi_namespace_node *node, uacpi_u64 *out) +{ + return uacpi_eval_integer(node, "_ADR", UACPI_NULL, out); +} + +#define CLS_REPR_SIZE 7 + +static uacpi_u8 extract_package_byte_or_zero(uacpi_package *pkg, uacpi_size i) +{ + uacpi_object *obj; + + if (uacpi_unlikely(pkg->count <= i)) + return 0; + + obj = pkg->objects[i]; + if (uacpi_unlikely(obj->type != UACPI_OBJECT_INTEGER)) + return 0; + + return obj->integer; +} + +uacpi_status uacpi_eval_cls( + uacpi_namespace_node *node, uacpi_id_string **out_id +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_package *pkg; + uacpi_u8 class_codes[3]; + uacpi_id_string *id_string; + + ret = uacpi_eval_typed( + node, "_CLS", UACPI_NULL, UACPI_OBJECT_PACKAGE_BIT, &obj + ); + if (ret != UACPI_STATUS_OK) + return ret; + + pkg = obj->package; + class_codes[0] = extract_package_byte_or_zero(pkg, 0); + class_codes[1] = extract_package_byte_or_zero(pkg, 1); + class_codes[2] = extract_package_byte_or_zero(pkg, 2); + + id_string = uacpi_kernel_alloc(sizeof(uacpi_id_string) + CLS_REPR_SIZE); + if (uacpi_unlikely(id_string == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + + id_string->size = CLS_REPR_SIZE; + id_string->value = UACPI_PTR_ADD(id_string, sizeof(uacpi_id_string)); + + uacpi_snprintf( + id_string->value, CLS_REPR_SIZE, "%02X%02X%02X", + class_codes[0], class_codes[1], class_codes[2] + ); + +out: + if (uacpi_likely_success(ret)) + *out_id = id_string; + + uacpi_object_unref(obj); + return ret; +} + +uacpi_status uacpi_eval_uid( + uacpi_namespace_node *node, uacpi_id_string **out_uid +) +{ + uacpi_status ret; + uacpi_object *obj; + uacpi_id_string *id_string; + uacpi_u32 size; + + ret = uacpi_eval_typed( + node, "_UID", UACPI_NULL, + UACPI_OBJECT_INTEGER_BIT | UACPI_OBJECT_STRING_BIT, + &obj + ); + if (ret != UACPI_STATUS_OK) + return ret; + + if (obj->type == UACPI_OBJECT_STRING) { + size = obj->buffer->size; + if (uacpi_unlikely(size == 0 || size > 0xE0000000)) { + uacpi_error( + "invalid %.4s._UID string size: %u\n", + uacpi_namespace_node_name(node).text, size + ); + ret = UACPI_STATUS_AML_BAD_ENCODING; + goto out; + } + } else { + size = uacpi_snprintf( + UACPI_NULL, 0, "%"UACPI_PRIu64, UACPI_FMT64(obj->integer) + ) + 1; + } + + id_string = uacpi_kernel_alloc(sizeof(uacpi_id_string) + size); + if (uacpi_unlikely(id_string == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + + id_string->value = UACPI_PTR_ADD(id_string, sizeof(uacpi_id_string)); + id_string->size = size; + + if (obj->type == UACPI_OBJECT_STRING) { + uacpi_memcpy(id_string->value, obj->buffer->text, size); + id_string->value[size - 1] = '\0'; + } else { + uacpi_snprintf( + id_string->value, id_string->size, "%"UACPI_PRIu64, + UACPI_FMT64(obj->integer) + ); + } + +out: + if (uacpi_likely_success(ret)) + *out_uid = id_string; + + uacpi_object_unref(obj); + return ret; +} + +static uacpi_bool matches_any( + uacpi_id_string *id, const uacpi_char *const *ids +) +{ + uacpi_size i; + + for (i = 0; ids[i]; ++i) { + if (uacpi_strcmp(id->value, ids[i]) == 0) + return UACPI_TRUE; + } + + return UACPI_FALSE; +} + +static uacpi_status uacpi_eval_dstate_method_template( + uacpi_namespace_node *parent, uacpi_char *template, uacpi_u8 num_methods, + uacpi_u8 *out_values +) +{ + uacpi_u8 i; + uacpi_status ret = UACPI_STATUS_NOT_FOUND, eval_ret; + uacpi_object *obj; + + // We expect either _SxD or _SxW, so increment template[2] + for (i = 0; i < num_methods; ++i, template[2]++) { + eval_ret = uacpi_eval_typed( + parent, template, UACPI_NULL, UACPI_OBJECT_INTEGER_BIT, &obj + ); + if (eval_ret == UACPI_STATUS_OK) { + ret = UACPI_STATUS_OK; + out_values[i] = obj->integer; + uacpi_object_unref(obj); + continue; + } + + out_values[i] = 0xFF; + if (uacpi_unlikely(eval_ret != UACPI_STATUS_NOT_FOUND)) { + const char *path; + + path = uacpi_namespace_node_generate_absolute_path(parent); + uacpi_warn( + "failed to evaluate %s.%s: %s\n", + path, template, uacpi_status_to_string(eval_ret) + ); + uacpi_free_dynamic_string(path); + } + } + + return ret; +} + +#define NODE_INFO_EVAL_ADD_ID(name) \ + if (uacpi_eval_##name(node, &name) == UACPI_STATUS_OK) { \ + size += name->size; \ + if (uacpi_unlikely(size < name->size)) { \ + ret = UACPI_STATUS_AML_BAD_ENCODING; \ + goto out; \ + } \ + } + +#define NODE_INFO_COPY_ID(name, flag) \ + if (name != UACPI_NULL) { \ + flags |= UACPI_NS_NODE_INFO_HAS_##flag; \ + info->name.value = cursor; \ + info->name.size = name->size; \ + uacpi_memcpy(cursor, name->value, name->size); \ + cursor += name->size; \ + } else { \ + uacpi_memzero(&info->name, sizeof(*name)); \ + } \ + +uacpi_status uacpi_get_namespace_node_info( + uacpi_namespace_node *node, uacpi_namespace_node_info **out_info +) +{ + uacpi_status ret = UACPI_STATUS_OK; + uacpi_u32 size = sizeof(uacpi_namespace_node_info); + uacpi_object *obj; + uacpi_namespace_node_info *info; + uacpi_id_string *hid = UACPI_NULL, *uid = UACPI_NULL, *cls = UACPI_NULL; + uacpi_pnp_id_list *cid = UACPI_NULL; + uacpi_char *cursor; + uacpi_u64 adr = 0; + uacpi_u8 flags = 0; + uacpi_u8 sxd[4], sxw[5]; + + obj = uacpi_namespace_node_get_object(node); + if (uacpi_unlikely(obj == UACPI_NULL)) + return UACPI_STATUS_INVALID_ARGUMENT; + + if (obj->type == UACPI_OBJECT_DEVICE || + obj->type == UACPI_OBJECT_PROCESSOR) { + char dstate_method_template[5] = { '_', 'S', '1', 'D', '\0' }; + + NODE_INFO_EVAL_ADD_ID(hid) + NODE_INFO_EVAL_ADD_ID(uid) + NODE_INFO_EVAL_ADD_ID(cls) + NODE_INFO_EVAL_ADD_ID(cid) + + if (uacpi_eval_adr(node, &adr) == UACPI_STATUS_OK) + flags |= UACPI_NS_NODE_INFO_HAS_ADR; + + if (uacpi_eval_dstate_method_template( + node, dstate_method_template, sizeof(sxd), sxd + ) == UACPI_STATUS_OK) + flags |= UACPI_NS_NODE_INFO_HAS_SXD; + + dstate_method_template[2] = '0'; + dstate_method_template[3] = 'W'; + + if (uacpi_eval_dstate_method_template( + node, dstate_method_template, sizeof(sxw), sxw + ) == UACPI_STATUS_OK) + flags |= UACPI_NS_NODE_INFO_HAS_SXW; + } + + info = uacpi_kernel_alloc(size); + if (uacpi_unlikely(info == UACPI_NULL)) { + ret = UACPI_STATUS_OUT_OF_MEMORY; + goto out; + } + info->size = size; + cursor = UACPI_PTR_ADD(info, sizeof(uacpi_namespace_node_info)); + info->name = uacpi_namespace_node_name(node); + info->type = obj->type; + info->num_params = info->type == UACPI_OBJECT_METHOD ? obj->method->args : 0; + + info->adr = adr; + if (flags & UACPI_NS_NODE_INFO_HAS_SXD) + uacpi_memcpy(info->sxd, sxd, sizeof(sxd)); + else + uacpi_memzero(info->sxd, sizeof(info->sxd)); + + if (flags & UACPI_NS_NODE_INFO_HAS_SXW) + uacpi_memcpy(info->sxw, sxw, sizeof(sxw)); + else + uacpi_memzero(info->sxw, sizeof(info->sxw)); + + if (cid != UACPI_NULL) { + uacpi_u32 i; + + uacpi_memcpy(&info->cid, cid, cid->size + sizeof(*cid)); + cursor += cid->num_ids * sizeof(uacpi_id_string); + + for (i = 0; i < cid->num_ids; ++i) { + info->cid.ids[i].value = cursor; + cursor += info->cid.ids[i].size; + } + + flags |= UACPI_NS_NODE_INFO_HAS_CID; + } else { + uacpi_memzero(&info->cid, sizeof(*cid)); + } + + NODE_INFO_COPY_ID(hid, HID) + NODE_INFO_COPY_ID(uid, UID) + NODE_INFO_COPY_ID(cls, CLS) + +out: + if (uacpi_likely_success(ret)) { + info->flags = flags; + *out_info = info; + } + + uacpi_free_id_string(hid); + uacpi_free_id_string(uid); + uacpi_free_id_string(cls); + uacpi_free_pnp_id_list(cid); + return ret; +} + +void uacpi_free_namespace_node_info(uacpi_namespace_node_info *info) +{ + if (info == UACPI_NULL) + return; + + uacpi_free(info, info->size); +} + +uacpi_bool uacpi_device_matches_pnp_id( + uacpi_namespace_node *node, const uacpi_char *const *ids +) +{ + uacpi_status st; + uacpi_bool ret = UACPI_FALSE; + uacpi_id_string *id = UACPI_NULL; + uacpi_pnp_id_list *id_list = UACPI_NULL; + + st = uacpi_eval_hid(node, &id); + if (st == UACPI_STATUS_OK && matches_any(id, ids)) { + ret = UACPI_TRUE; + goto out; + } + + st = uacpi_eval_cid(node, &id_list); + if (st == UACPI_STATUS_OK) { + uacpi_size i; + + for (i = 0; i < id_list->num_ids; ++i) { + if (matches_any(&id_list->ids[i], ids)) { + ret = UACPI_TRUE; + goto out; + } + } + } + +out: + uacpi_free_id_string(id); + uacpi_free_pnp_id_list(id_list); + return ret; +} + +struct device_find_ctx { + const uacpi_char *const *target_hids; + void *user; + uacpi_iteration_callback cb; +}; + +static uacpi_iteration_decision find_one_device( + void *opaque, uacpi_namespace_node *node, uacpi_u32 depth +) +{ + struct device_find_ctx *ctx = opaque; + uacpi_status ret; + uacpi_u32 flags; + + if (!uacpi_device_matches_pnp_id(node, ctx->target_hids)) + return UACPI_ITERATION_DECISION_CONTINUE; + + ret = uacpi_eval_sta(node, &flags); + if (uacpi_unlikely_error(ret)) + return UACPI_ITERATION_DECISION_NEXT_PEER; + + if (!(flags & ACPI_STA_RESULT_DEVICE_PRESENT) && + !(flags & ACPI_STA_RESULT_DEVICE_FUNCTIONING)) + return UACPI_ITERATION_DECISION_NEXT_PEER; + + return ctx->cb(ctx->user, node, depth); +} + + +uacpi_status uacpi_find_devices_at( + uacpi_namespace_node *parent, const uacpi_char *const *hids, + uacpi_iteration_callback cb, void *user +) +{ + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + struct device_find_ctx ctx = { + .target_hids = hids, + .user = user, + .cb = cb, + }; + + return uacpi_namespace_for_each_child( + parent, find_one_device, UACPI_NULL, UACPI_OBJECT_DEVICE_BIT, + UACPI_MAX_DEPTH_ANY, &ctx + ); +} + +uacpi_status uacpi_find_devices( + const uacpi_char *hid, uacpi_iteration_callback cb, void *user +) +{ + const uacpi_char *hids[] = { + hid, UACPI_NULL + }; + + return uacpi_find_devices_at(uacpi_namespace_root(), hids, cb, user); +} + +uacpi_status uacpi_set_interrupt_model(uacpi_interrupt_model model) +{ + uacpi_status ret; + uacpi_object *arg; + uacpi_object_array args; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + arg = uacpi_create_object(UACPI_OBJECT_INTEGER); + if (uacpi_unlikely(arg == UACPI_NULL)) + return UACPI_STATUS_OUT_OF_MEMORY; + + arg->integer = model; + args.objects = &arg; + args.count = 1; + + ret = uacpi_eval(uacpi_namespace_root(), "_PIC", &args, UACPI_NULL); + uacpi_object_unref(arg); + + if (ret == UACPI_STATUS_NOT_FOUND) + ret = UACPI_STATUS_OK; + + return ret; +} + +uacpi_status uacpi_get_pci_routing_table( + uacpi_namespace_node *parent, uacpi_pci_routing_table **out_table +) +{ + uacpi_status ret; + uacpi_object *obj, *entry_obj, *elem_obj; + uacpi_package *table_pkg, *entry_pkg; + uacpi_pci_routing_table_entry *entry; + uacpi_pci_routing_table *table; + uacpi_size size, i; + + UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_NAMESPACE_LOADED); + + obj = uacpi_namespace_node_get_object(parent); + if (uacpi_unlikely(obj == UACPI_NULL || obj->type != UACPI_OBJECT_DEVICE)) + return UACPI_STATUS_INVALID_ARGUMENT; + + ret = uacpi_eval_typed( + parent, "_PRT", UACPI_NULL, UACPI_OBJECT_PACKAGE_BIT, &obj + ); + if (uacpi_unlikely_error(ret)) + return ret; + + table_pkg = obj->package; + if (uacpi_unlikely(table_pkg->count == 0 || table_pkg->count > 1024)) { + uacpi_error("invalid number of _PRT entries: %zu\n", table_pkg->count); + uacpi_object_unref(obj); + return UACPI_STATUS_AML_BAD_ENCODING; + } + + size = table_pkg->count * sizeof(uacpi_pci_routing_table_entry); + table = uacpi_kernel_alloc(sizeof(uacpi_pci_routing_table) + size); + if (uacpi_unlikely(table == UACPI_NULL)) { + uacpi_object_unref(obj); + return UACPI_STATUS_OUT_OF_MEMORY; + } + table->num_entries = table_pkg->count; + + for (i = 0; i < table_pkg->count; ++i) { + entry_obj = table_pkg->objects[i]; + + if (uacpi_unlikely(entry_obj->type != UACPI_OBJECT_PACKAGE)) { + uacpi_error("_PRT sub-object %zu is not a package: %s\n", + i, uacpi_object_type_to_string(entry_obj->type)); + goto out_bad_encoding; + } + + entry_pkg = entry_obj->package; + if (uacpi_unlikely(entry_pkg->count != 4)) { + uacpi_error("invalid _PRT sub-package entry count %zu\n", + entry_pkg->count); + goto out_bad_encoding; + } + + entry = &table->entries[i]; + + elem_obj = entry_pkg->objects[0]; + if (uacpi_unlikely(elem_obj->type != UACPI_OBJECT_INTEGER)) { + uacpi_error("invalid _PRT sub-package %zu address type: %s\n", + i, uacpi_object_type_to_string(elem_obj->type)); + goto out_bad_encoding; + } + entry->address = elem_obj->integer; + + elem_obj = entry_pkg->objects[1]; + if (uacpi_unlikely(elem_obj->type != UACPI_OBJECT_INTEGER)) { + uacpi_error("invalid _PRT sub-package %zu pin type: %s\n", + i, uacpi_object_type_to_string(elem_obj->type)); + goto out_bad_encoding; + } + entry->pin = elem_obj->integer; + + elem_obj = entry_pkg->objects[2]; + switch (elem_obj->type) { + case UACPI_OBJECT_STRING: + ret = uacpi_object_resolve_as_aml_namepath( + elem_obj, parent, &entry->source + ); + if (uacpi_unlikely_error(ret)) { + uacpi_error("unable to lookup _PRT source %s: %s\n", + elem_obj->buffer->text, uacpi_status_to_string(ret)); + goto out_bad_encoding; + } + break; + case UACPI_OBJECT_INTEGER: + entry->source = UACPI_NULL; + break; + default: + uacpi_error("invalid _PRT sub-package %zu source type: %s\n", + i, uacpi_object_type_to_string(elem_obj->type)); + goto out_bad_encoding; + } + + elem_obj = entry_pkg->objects[3]; + if (uacpi_unlikely(elem_obj->type != UACPI_OBJECT_INTEGER)) { + uacpi_error("invalid _PRT sub-package %zu source index type: %s\n", + i, uacpi_object_type_to_string(elem_obj->type)); + goto out_bad_encoding; + } + entry->index = elem_obj->integer; + } + + uacpi_object_unref(obj); + *out_table = table; + return UACPI_STATUS_OK; + +out_bad_encoding: + uacpi_object_unref(obj); + uacpi_free_pci_routing_table(table); + return UACPI_STATUS_AML_BAD_ENCODING; +} + +void uacpi_free_pci_routing_table(uacpi_pci_routing_table *table) +{ + if (table == UACPI_NULL) + return; + + uacpi_free( + table, + sizeof(uacpi_pci_routing_table) + + table->num_entries * sizeof(uacpi_pci_routing_table_entry) + ); +} + +void uacpi_free_dynamic_string(const uacpi_char *str) +{ + if (str == UACPI_NULL) + return; + + uacpi_free((void*)str, uacpi_strlen(str) + 1); +} diff --git a/src/uacpi_kernel_api.c b/src/uacpi_kernel_api.c new file mode 100644 index 0000000..7af0834 --- /dev/null +++ b/src/uacpi_kernel_api.c @@ -0,0 +1,141 @@ +#include +#include "sys/pci.h" +#include "mm/vmm.h" +#include "mm/kmalloc.h" +#include +#include +#include +#include + +uacpi_status uacpi_kernel_get_rsdp(uacpi_phys_addr *out_rsdp_address){ + extern struct limine_rsdp_request rsdp_request; + out_rsdp_address = (uacpi_phys_addr*)rsdp_request.response->address; + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_kernel_pci_device_open(uacpi_pci_address address, uacpi_handle *out_handle){ + + if(address.segment != 0){ + klog(LOG_ERROR, __func__, "Multiple segments not implemented!"); + return UACPI_STATUS_UNIMPLEMENTED; + } + + l84_pci_function_return ret = check_device(address.bus, address.device); + + return ret.func_addr[address.function]; +} + +void uacpi_kernel_pci_device_close(uacpi_handle handle){ + return; +} + +uacpi_status uacpi_kernel_pci_read(uacpi_handle device, uacpi_size offset, uacpi_u8 byte_width, uacpi_u64 *value){ + if(byte_width == 1){ + uint8_t read = *(uint8_t*)((uint64_t)device + offset); + *value = read; + }else if(byte_width == 2){ + uint16_t read = *(uint16_t*)((uint64_t)device + offset); + *value = read; + }else if(byte_width == 4){ + uint32_t read = *(uint32_t*)((uint64_t)device + offset); + *value = read; + }else{ + return UACPI_STATUS_INTERNAL_ERROR; + } + + return UACPI_STATUS_OK; +}; + +uacpi_status uacpi_kernel_pci_write(uacpi_handle device, uacpi_size offset, uacpi_u8 byte_width, uacpi_u64 value){ + if(byte_width == 1){ + *(uint8_t*)((uint64_t)device + offset) = value; + }else if(byte_width == 2){ + *(uint16_t*)((uint64_t)device + offset) = value; + }else if(byte_width == 4){ + *(uint32_t*)((uint64_t)device + offset) = value; + }else{ + return UACPI_STATUS_INTERNAL_ERROR; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_kernel_io_map(uacpi_io_addr base, uacpi_size len, uacpi_handle *out_handle){ + return UACPI_STATUS_UNIMPLEMENTED; +} + +void uacpi_kernel_io_unmap(uacpi_handle handle){ + asm("nop"); +} + +uacpi_status uacpi_kernel_io_read(uacpi_handle handle, uacpi_size offset, uacpi_u8 byte_width, uacpi_u64 *value){ + if(byte_width == 1){ + *value = inb((uint16_t)offset); + }else if(byte_width == 2){ + *value = inw((uint16_t)offset); + }else if(byte_width == 4){ + *value = inl((uint16_t)offset); + }else{ + return UACPI_STATUS_INTERNAL_ERROR; + } + + return UACPI_STATUS_OK; +} + +uacpi_status uacpi_kernel_io_write(uacpi_handle handle, uacpi_size offset, uacpi_u8 byte_width, uacpi_u64 value){ + if(byte_width == 1){ + outb((uint16_t)offset, value); + }else if(byte_width == 2){ + outw((uint16_t)offset, value); + }else if(byte_width == 4){ + outl((uint16_t)offset, value); + }else{ + return UACPI_STATUS_INTERNAL_ERROR; + } + + return UACPI_STATUS_OK; +} + +void *uacpi_kernel_map(uacpi_phys_addr addr, uacpi_size len){ + kernel_map_pages((void*)addr, len, PTE_BIT_RW); + return (void*)addr; +} + +void uacpi_kernel_unmap(void *addr, uacpi_size len){ + kernel_unmap_pages(addr, len); +} + +void *uacpi_kernel_alloc(uacpi_size size){ + void *ret = kmalloc(size); + + if(ret == NULL){ + klog(LOG_ERROR, __func__, "OOM"); + kkill(); + } + + return ret; + +} + +void uacpi_kernel_free(void *mem){ + + if(mem == NULL){ + return; + } + + kfree(mem); +} + +void uacpi_kernel_log(uacpi_log_level level, const uacpi_char *str){ + switch(level){ + case UACPI_LOG_ERROR: + kprintf("uacpi: error: %s\n", str); + case UACPI_LOG_WARN: + kprintf("uacpi: warn: %s\n", str); + case UACPI_LOG_INFO: + kprintf("uacpi: info: %s\n", str); + default: + asm("nop"); + + } +} \ No newline at end of file