Repository navigation
7.0 - #166
7.0#166
Conversation
_InterlockedCompareExchange takes the value to store before the value to compare against, and we were passing them the other way around. The only user of this so far is the firmware global lock, which as a result was never written to on MSVC builds: a free lock was reported as acquired without being marked as owned, and a lock owned by the firmware never got its pending bit set, so the release that we then waited for was never signaled. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
b40a5e6 to
54a05de
Compare
Look at the lock in the FACS via a DataTableRegion after acquiring it, and then again after releasing it with the pending bit set. Only our own runner is expected to pass this, since e.g. acpiexec doesn't do the handshake with the firmware at all, so the test is skipped for everything else. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
54a05de to
e60f36f
Compare
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
The time that is left to wait is in nanoseconds, while SleepConditionVariableCS expects milliseconds. We were only dividing it by a thousand, which made every timed wait last a thousand times longer than requested. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
The deadline of a timed wait was calculated by adding the entire timeout to the nanosecond field of the current time, and normalizing it after. That field is a long, which is only 32 bits wide on a 32-bit build, so any timeout longer than about two seconds would overflow it and produce a deadline that is either in the past or nowhere near the requested one. Split the timeout into seconds and nanoseconds before adding it. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
All of the work scheduled via uacpi_kernel_schedule_work is executed right away by the caller, and waiting for its completion is a no-op. This is a perfectly valid way to implement the kernel API, but it makes it impossible to test anything that involves work being in flight while something else is going on, which is what happens under any real kernel. Add a mode where the work is handed off to dedicated threads instead, one per work type, with uacpi_kernel_wait_for_work_completion actually waiting for them. A test can also ask for the work to be held back until someone starts waiting for it, which makes it possible to reliably have uACPI wait for work that hasn't even started yet. Since a bug in this area tends to manifest itself as a deadlock, also add a watchdog that terminates the runner if the work threads are still running 30 seconds after they were started. The runner state that is now reachable by more than one thread is protected by a mutex, which is only ever taken while the work threads are running. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
The interrupt handlers installed by uACPI were dropped on the floor, and the event registers were backed by plain memory, with the GPE0 block sitting right on top of the PM1 event block. This left no way to trigger a fixed or a general purpose event, let alone to have it handled. Fix all of that: - Keep track of the interrupt handlers and add a way to invoke them. - Give the GPE0 block an address range of its own. - Make the status registers behave the way the real ones do: writing a one to a bit clears it, anything else leaves it alone. - Have the event registers start out zeroed, as opposed to containing whatever the allocator has returned. While at it, reject an IO access that crosses the end of the IO space instead of going out of bounds. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
|
When I apply this pull request, my test laptop no longer shuts down on a |
Don't worry, I will have other people test it before merging it. However, since this changed a lot of kernel API, can you make sure it's not an issue with your implementation? I'd also like to see the trace log of when you try to shut down with and without the patches. |
|
Unfortunately, on real hardware, I don't have anyway to save the trace at shutdown. At this point, all of my devices have been "removed" from the system and I have no way to write to a media device or display to the screen. However, when emulated on QEMU, here is my trace at shutdown: This is using the real DSDT(s) from the real hardware. I apologize, I might have been a little misleading when I say that the code shuts down QEMU, but not real hardware. Since the code shuts down the laptop before this PR, but not after, I will work through the additions and see which addition makes the change. As you state, it could be my interface implementation. |
If it doesn't shut down you should have a way to make it so logs stay on screen right? At least with a hack in your code to keep the screen attached? "It doesn't shut down" is not enough information for me to work with unfortunately.
That's not very useful since QEMU does essentially no work on shutdown, as it has no _PTS method, as opposed to real hardware.
I'm not sure that would be a useful test and I never assumed you claimed that, it obviously wouldn't work, since AML is very chipset and device specific. |
|
No worries, I found the error. My semaphore mechanism was implemented a little different than what uACPI was expecting. I have now modified it to work and the machine shuts down as expected. Sorry for the worry, and thanks for the help. I hope I am not being a bother. I expect that you would want to know if something wasn't quite right, and an investigation to make sure it was right or indeed wrong would be desired. Thanks, |
Great! Out of curiosity (maybe documentation needs adjusting and wasn't clear), what were your expectations and what was wrong?
Yeah, absolutely. Let me know if you encounter any more issues. |
No adjusting is needed. I made the mistake of assuming my implementation was sufficient and I was looking elsewhere for the error instead of studying that part of the PR more thoroughly. My apologizes. |
|
Ah ok, no problem. Thanks for reporting it, could've been a regression since there are a lot of changes here. |
e60f36f to
0dc5b5d
Compare
An interrupt handler is not allowed to acquire a mutex or to wait for an event, nor for the deferred work to complete. Nothing was stopping one from doing that here though, as an interrupt is handled by a regular thread, so calling into an API that is off limits to interrupt handlers would go unnoticed unless it happened to deadlock. Keep track of the threads that are in the middle of handling an interrupt, and treat any attempt to wait by one of them as an error. That includes asking who the current thread is, as the only one who needs to know is a mutex: one that is owned by the thread that was interrupted would be acquired without ever getting to the wait. A spinlock is the one thing that an interrupt handler is free to take, so don't implement it in terms of the mutex API anymore. The deferred work is not subject to any of this, even if it's executed right away by the interrupt handler that has scheduled it. It's still not allowed to wait for the work to complete though, which so far was only caught for the work that is executed by the work threads. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
uacpi_install_notify_handler returned right away if it was unable to allocate the new handler, without releasing the notify mutex or dropping the reference to the object it was installing the handler for. Since the mutex stayed locked forever, every subsequent Notify() as well as any attempt to (un)install a notify handler would hang. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
test_runner.c declares every test that it runs by hand, with nothing to check those declarations against the definitions. Move them to tests.h, and include it from both sides. interface_cleanup is not a test, so that one goes to helpers.h alongside the rest of what the kernel interface provides to the runner. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
This needs a way to make an allocation fail on demand, so teach the runner how to do that. This is the third test that calls the API directly instead of having \MAIN evaluated, and there are more to come, so also turn the chain of string comparisons that such a test is selected with into a table. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Both uacpi_install_notify_handler and uacpi_uninstall_notify_handler used to wait for all of the in-flight work to complete while holding the notify mutex. This was done to make sure that nobody is walking the handler list while it's being modified, as do_notify does that without taking the mutex. The problem is that the very work we were waiting for might need the notify mutex in order to make progress, as it's taken by uacpi_notify_all to queue a notification. This is the case for: - A GPE handler that executes a Notify(), which is how pretty much every AC/battery/lid/button event is delivered. - A GPE that is configured for implicit notify. - A notify handler that ends up evaluating AML that executes a Notify(). The work doesn't even have to queue a notification itself: any thread that executes a Notify() blocks on the notify mutex while holding the namespace lock, which is in turn needed by a GPE handler to run. In all of these cases the thread (un)installing a handler ends up waiting for the work, while the work is waiting for that thread to release the mutex, and both hang forever. This is trivially hit by a kernel that installs a notify handler after the GPEs have been enabled. Fix this by never waiting with the mutex held: - Installing a handler no longer waits at all. A new handler is fully initialized before being linked in, so it's safe for an in-flight notification to either see it or not. - Uninstalling a handler unlinks it under the mutex, and only then waits for the in-flight notifications that might still be looking at it, after which it's safe to free. This preserves the guarantee that the handler is never invoked again after the uninstall returns. The handler lists are still walked without taking the mutex, as the work might be executed right from uacpi_kernel_schedule_work, which is called with the mutex held. Make this well defined on weakly ordered architectures by using the atomic helpers to publish & load the handlers. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Make sure that a notify handler can be installed & uninstalled while there's work in flight that is about to produce a notification, which used to deadlock. This is done for every possible source of one: - A GPE handler that executes a Notify() - A GPE that is configured for implicit notify - A notify handler that evaluates a method that executes a Notify() Also check that a handler is never invoked after it's uninstalled, and that a handler that is still running is waited for. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Both uacpi_enable_fixed_event & uacpi_disable_fixed_event take the event lock, which is a mutex, and so does uacpi_fixed_event_info. This makes them impossible to use from an interrupt context, which most notably includes the handler of the event itself: a handler that wants to disable its own event, e.g. an RTC alarm that is only supposed to fire once, has no way of doing that. The only thing that any of them need the lock for is the handler of the event, which is installed & removed with it held. Protect the handler with a spinlock instead, along with the enable bit of the event, and make that the only lock that the API in question takes. Removing a handler is now a matter of disabling the event & dropping its handler in one critical section, so there's no window for someone to enable the event in between. The interrupt handler used to rely on a handler staying around until every in-flight interrupt is done, so make it look at the handler with the spinlock held as well, and only after making sure that the event is still enabled. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
0dc5b5d to
de8b7e3
Compare
The API that reconfigures an event has to wait for all of the in-flight interrupts & work to complete before it's safe to touch the event, and it used to do that while holding the event lock. This applies to: - uacpi_mask_gpe - uacpi_install_gpe_handler(_raw), uacpi_uninstall_gpe_handler and uacpi_setup_gpe_for_wake, in case the GPE is currently enabled - uacpi_uninstall_gpe_block - uacpi_uninstall_fixed_event_handler The work that we're waiting for might need the event lock itself though: - A GPE handler, or AML executed by a notify handler, that loads a table ends up in uacpi_events_match_post_dynamic_table_load, which takes the event lock to match the GPE methods that the table has brought in. - A notify handler might call into the event API, e.g. to enable or to finish handling a GPE, or to query its state. In both cases the work blocks on the event lock, while the thread that is holding it is waiting for that work to complete, and both hang forever. Fix this by dropping the event lock for the duration of the wait. Since this would allow someone else to reconfigure, or even free, the event that we're in the middle of reconfiguring, introduce a separate lock that serializes the API in question. This lock is never taken by the work, so it's safe to wait under. The state of an event that is being reconfigured can now be changed by the work while we wait, so make sure that: - A GPE handler that is being replaced is only looked at after the event has been masked, since a dynamic table load might replace it. - A GPE block that is being uninstalled has all of its registers masked before we wait for any of them, so that none of the events can be enabled behind our back. While at it, document which parts of the event API are not safe to call from the deferred work. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Make sure that an event can be reconfigured while there's work in flight that needs the event lock, which used to deadlock. Both a notify handler that calls into the event API and a GPE handler that loads a table are covered. Also check that a handler that gets replaced by a dynamic table load while the event is being reconfigured is picked up correctly. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
An event that is claimed by the interrupt handler is marked as blocked, which stays that way until the event is restored once it's been handled. If clearing the status of an edge triggered event fails, the event is not handled at all: it's enabled right back, but nothing ever restores it, so it's left blocked for good. A blocked event is not enabled when it's unmasked, which is something that is done every time that its handler is replaced or that it's set up for wake, so the next one of those would leave the event disabled with nothing to bring it back other than it getting its first user. Unblock the event along with enabling it. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Unmasking an event that has users enables it, but whether that has worked out was never looked at. An event that we have failed to enable was reported as unmasked anyway, which left it disabled with no way to have another go at it: it's not masked anymore, so unmasking it again is rejected, and it already has users, so getting another one doesn't enable it either. Leave such an event masked and report the error instead, so that it's both visible and possible to retry. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
A native handler that is installed in place of an AML one takes away the user that the event was given during initialization, which is given back once the native handler is uninstalled. An event only has room for so many users though, and whether there was any left for this one was never checked. If there wasn't the native handler was uninstalled anyway, and the AML one was left with nothing to keep its event enabled once those who took all of the room were gone. There's nothing that can be done about it after the fact, as the handler that knew about the missing user is freed by then. Hand the user back first, and leave everything as is if that doesn't work out. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
An event is identified by a 16-bit index, and so is the number of events that a block has. A block of 8192 registers or more is too much for that: the number of events wraps around, so the array that is allocated for them is way smaller than what is then written to it, one event for every bit of every register. uacpi_install_gpe_block takes whatever number of registers it's given, so reject a block that doesn't fit, as well as one that has no registers at all. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
A GPE block that has an interrupt of its own takes the handler of that interrupt with it when it's uninstalled. The kernel might have to wait for a handler that is still running to return before it's able to do that, which was done with the event lock held. That is a deadlock if the handler ends up needing the lock itself, which is possible for a kernel that executes deferred work right as it's scheduled: the work is free to load a table, which takes the event lock in order to match the GPE methods that it has brought in. This is the same problem that waiting for the work to complete used to have, so solve it the same way, by dropping the lock for the duration of the call. Both the block & the interrupt context are unlinked by then, and neither is freed until every handler that might still be looking at them has been waited for. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
An event that has no handler notifies the devices that use it for wake, which are kept in a list. If the entry for a device couldn't be allocated all that was done about it was a warning: the event was still marked as a wake one and the call was reported as a success, even though the device is never going to be notified. An event that had no handler at all was also left with an implicit notify one that has nobody to notify. Allocate the entry before anything about the event is changed, and fail if that doesn't work out. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
An event that has a method is given a user when the GPE initialization is finalized, which keeps it enabled. That is not what a wake event is supposed to be, so setting one up for wake takes the user away. This was done for every call though, and an event may be shared by any number of wake devices: the first call would take away the user that was given automatically, and every one after that would take away a user that somebody else has added, e.g. a driver that wants the event enabled. The same goes for an event that was set up for wake before the initialization was finalized, which never had a user of ours to begin with. Keep track of whether the event still has the user that we gave it, and only take away that one. The call fails if that doesn't work out, which leaves the user there to be taken away the next time around. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Neither one has any effect on the hardware for as long as the event is masked, but both are still accounted for, same as suspending & resuming it: an event that has lost its last user while it was masked is not enabled once it's unmasked, and one that has gained a user is. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Deferred work is free to evaluate AML, which is the case for both the GPE methods and the notify handlers that choose to do so. Any of it that accesses an operation region, or acquires the global lock, is blocked for as long as somebody else is in the middle of doing the same thing. Waiting for such work to complete from an address space handler, or with the global lock held, is thus a deadlock. This has always been the case, it's just not something that was mentioned anywhere. Mention it in the note of every API that waits for work, and tell the kernel which contexts uacpi_kernel_wait_for_work_completion is never called from while at it. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
A command is sent over a PCC subspace as soon as AML writes to the command field of its shared memory region, which is 12 bytes into it for an extended subspace. That's where we were looking for the write. A PCC operation region doesn't cover the signature that the shared memory region starts with though, only what follows it (see ACPI 6.6: 5.5.2.4.8. Declaring PCC Operation Regions), which puts the command at an offset of 8 as far as AML is concerned. A write to it was treated like any other as a result, and the command was never sent, while a write to the first bytes of the payload did send one. The specification does have an example that skips 4 bytes in front of the fields, which is presumably where this came from. It's at odds with everything else that the section has to say though, the other example included, and doesn't even fit in the region that it declares. Document what the buffer that is given to the handler stands for while at it, and adjust the existing test, as it follows the old layout. Note that the generic subspaces (types 0-2), which have a command field of their own, are still not supported. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
A command is sent over a PCC subspace as soon as AML writes to the command field, which is 4 bytes wide. A field is written one byte at a time though, and every one of those that had to do with the command was treated as a reason to send it. That only works out if the field is declared in a way that makes it one access, which is not something that it's allowed to be: a PCC field is supposed to be accessed byte by byte (see ACPI 6.6: 5.5.2.4.8.3. Declaring message fields within a PCC OperationRegion). The command was sent four times for such a field, three of them before it was even written in its entirety. Go by the field that is being written instead. A field that starts where the command does is a write to it, no matter how wide it is, and the command is sent once the last access of that field is done. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Iterating over the children of a node is something that might have to be done with the namespace already locked, which is not something that uacpi_namespace_node_next_typed allows, as it takes the lock itself. Factor out the iteration itself into an internal helper that expects the caller to be holding the lock instead. No functional change. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
_REG belongs to the scope that the regions are declared in, but it's looked up via the parent of a region, which makes it impossible to execute for a scope that has no regions of its own. Take the scope itself instead. An error that _REG fails with now names the scope rather than the region, and goes by the address space that it was executed for, instead of looking at a node that might not even be a region anymore by that point. No functional change otherwise. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
An embedded controller is often accessed via SystemMemory or SystemIO instead of the EmbeddedControl address space, yet still has a _REG method that is meant to tell it that the OS is ready for it. Such _REG methods usually set an "EC available" flag that most of the EC-related AML checks before doing anything, so leaving them out breaks things like the battery, thermal zones and the lid. That flag was never set, as _REG is only executed for the regions that a handler is connected to, and these ECs have none. Out of the 692 machines in the ACPI dumps that the tests make use of, 59 have an EC with a _REG method and no EmbeddedControl regions anywhere at all. Once the _REG methods of the EmbeddedControl regions are executed, also execute the _REG of every EC that has none of those regions directly underneath it, be it the device that the handler is installed to or any of the ones underneath it. The ECs are looked for by their PNP ID, as the handler is not necessarily installed to the EC device itself, e.g. the root is a common choice, as it also covers the regions that are located outside of the EC device. A _REG method that gets rid of the only region of its EC is executed once more as a result, which is reflected in the test that covers it. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Same as with an EC, a GPIO controller might have a _REG method that is meant to tell it that the OS is ready for it, without having any GeneralPurposeIo regions for it to be executed for. These usually set an "available" flag that the rest of the AML checks before using the pins of the controller. Out of the 692 machines in the ACPI dumps that the tests make use of, 10 have such a controller, 4 of which do check the flag elsewhere. Unlike an EC, a GPIO controller has no common PNP ID to be looked for by, so only check the device that the handler is installed to. That's the controller itself for a handler that is installed per controller, which is the usual way of doing it. Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
Signed-off-by: Daniil Tatianin <99danilt@gmail.com>
de8b7e3 to
90db0c4
Compare
No description provided.