Page 1 of 1

Handling power button press

Posted: Tue Feb 06, 2018 1:25 am
by anatolik
Hi,

I am working on my hobby OS and now I am trying to add a shutdown handler. I need to install a handler for power button. I tryed to google the answer and it looks like I need to use SCI interruption. I parsed ACPI FADI and found that IRQ number for SCI at my machine is 9. I installed an IRQ handler then use QEMU monitor command 'system_powerdown' but I do not see that my power button press handler is called.

What is the semantic of power button handler setup? Is there additional ACPI configuration ineeded for this use-case?

Re: Handling power button press

Posted: Tue Feb 06, 2018 4:13 am
by Brendan
Hi,
anatolik wrote:I am working on my hobby OS and now I am trying to add a shutdown handler. I need to install a handler for power button. I tryed to google the answer and it looks like I need to use SCI interruption. I parsed ACPI FADI and found that IRQ number for SCI at my machine is 9. I installed an IRQ handler then use QEMU monitor command 'system_powerdown' but I do not see that my power button press handler is called.

What is the semantic of power button handler setup? Is there additional ACPI configuration ineeded for this use-case?
You can think of firmware as having 2 modes.

For the default/legacy mode, the user presses the power button (for 5+ seconds), the firmware gets a SMI/"System Management Interrupt", and the firmware shuts the computer down (the OS is not aware of any of this happening).

For "ACPI mode", the user presses the power button and the hardware (ACPI's embedded controller) sends an SCI/"System Control Interrupt" (instead of the SMI), the operating system's "SCI handler" starts and responds by interpreting ACPI's AML, the AML figures out what caused the SCI and generate a "button event", and the OS handles the button event however it likes (typically by using other AML functions to turn the computer off or to hibernate/suspend). Note that in "ACPI mode" you may also need to handle things like "over-temperature events" correctly (to prevent the computer from melting).

To switch from default/legacy mode to "ACPI mode" you need to follow a sequence that begins with installing an interrupt handler for the SCI and fiddling with an "enable flag" (described by fields in ACPI's FADT), and then call a few initialisation functions in the firmware's AML (with your AML interpreter) to do things like tell the AML which version of Windows your OS is, whether it intends to use PIC or IO APIC, etc. This typically means porting ACPICA to your OS to avoid the ~10 years of pain required to use AML properly/reliably (and emulate the quirks of whichever version of Windows your OS said it is). Note that if your OS tells AML the truth (instead of pretending its a version of Windows), then AML won't recognise your OS and will only provide bare minimum features (almost everything disabled).

For a hobby OS, my advice is to use the default/legacy mode. It just means that the user will need to hold the power button for 5+ seconds to ge the computer to turn itself off (and that the OS won't have a chance to do anything before power off).


Cheers,

Brendan

Re: Handling power button press

Posted: Tue Feb 13, 2018 8:39 am
by anatolik
Hello Brendan.

Thank you for the information. It looks like using ACPICA is the only reasonable way to achieve my goal. Though integrating ACPICA into my code will take some time.