Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutdown/Reboot #116

Merged
merged 11 commits into from
Sep 28, 2024
Merged

Shutdown/Reboot #116

merged 11 commits into from
Sep 28, 2024

Conversation

Amjad50
Copy link
Owner

@Amjad50 Amjad50 commented Sep 27, 2024

Summary

Implemented shutdown functionality

Related issue

Partial fix for #38, only shutdown for now

Changes

  • Added implementation for ACPI to read the relevant data \_Sx from AML to be prepared for shutdown/sleep.
  • Added implementation to use the \_Sx data to issue a shutdown/sleep with ACPI.
  • Implemented unmounting for filesystems (for now we can't unmount a specific fs, but all at once as part of the shutdown process).
  • Implemented shutdown process which is as follows:
    • power::start_shutdown is called.
    • scheduler stops scheduling and starts exiting processes when they arrive for rescheduling, keeps waiting until all have exited.
    • the filesystem is unmounted and any related data is flushed.
    • Issue a shutdown command to ACPI to finally shutdown the machine.
  • Implement reboot process which is similar to the above shutdown process except the last part:
    • Issue a reset command through the PS2 interface in the keyboard/mouse device.
    • We can implement this in ACPI using the reset_register, but its not available for my testing setup now.
  • Userspace access to shutdown and reboot through /devices/power virtual device
    • shutdown can be started by echo shutdown > /devices/power or echo reboot > ...
  • Added power commands that does the above, choose shutdown or reboot

Checklist

  • The changes are tested and works as expected (mention if not)
  • Tests if applicable (new features, regression tests, etc...)
    • won't do this for now, I want to write test but it would be tricky, since we need a custom test image that does shutdown and reboot and a userspace (linux program/shell) that monitors qemu and asserts shutdown/reboot is good.
  • Documentation
  • Needed README changes

We can now shutdown and throw everything in the garbage since we don't save anything
or handle any hardware power-off

But this is a good first step. More to come later

Signed-off-by: Amjad Alsharafi <[email protected]>
- Also added some comments for the trait

Signed-off-by: Amjad Alsharafi <[email protected]>
- Now, the shutdown process can be started from anywhere with
  `power::start_shutdown`.
- Then, the scheduler will not allow any new process, and will exit
  current processes when they get back to the scheduler.
- The scheduler then will wait until all processes have shutdown.
- Then, the cleanup starts.
- For now only the filesystem is cleaned up (after closing all opened
  files, now we have opened files in the processes (which should all
  have dropped) and the log file, which is closed here).
- Then, we issue ACPI command to shutdown the machine.

Signed-off-by: Amjad Alsharafi <[email protected]>
This is needed because I tried implementing shutdown access from
userspace through the `/devices` filesystem, and issuing shutdown
command by writing to a specific file.

It works well except when calling `scheduler::stop_scheduler`, because
the `sys_write` syscall reads the files from the `Process` struct stored
inside the `SCHEDULER`, and when calling `stop_scheduler` we will still
be inside the `Mutex` of `SCHEDULER` and this causes double lock.

The fix is to have the `SHUTDOWN` flag be global atomic, easy to modify,
the scheduler will check if it needs to shutdown and does that and
continue the shutdown procedure as is.

Signed-off-by: Amjad Alsharafi <[email protected]>
…o them

This will make it possible to implement something like:
`echo shutdown > /devices/power`
When opening a file that already exist with `>`, it will make it
`truncate` to `0`, before this commit, truncating devices was invalid.

Signed-off-by: Amjad Alsharafi <[email protected]>
Now we can do `echo shutdown > /devices/power` and it will shutdown the
machine.
We can implement other types as well of power commands.

Signed-off-by: Amjad Alsharafi <[email protected]>
This just writes `shutdown` into `/devices/power`, but this is better
implemented as part of `emerald_runtime` to be uniform

Signed-off-by: Amjad Alsharafi <[email protected]>
There are several methods to perform reboot/reset, check: https://wiki.osdev.org/Reboot

ACPI reset_register wasn't defined for me on my qemu setup, so the next
best thing is using the keyboard PS2 device.

Signed-off-by: Amjad Alsharafi <[email protected]>
Now we can shutdown and reboot with `power shutdown` and `power reboot`.

It would be nice to have one command for each, but it would be more
executable. A better approach is to support aliases.

Signed-off-by: Amjad Alsharafi <[email protected]>
@Amjad50 Amjad50 changed the title Shutdown Shutdown/Reboot Sep 28, 2024
@Amjad50 Amjad50 merged commit 6bec15c into master Sep 28, 2024
4 checks passed
@Amjad50 Amjad50 deleted the shutdown branch September 28, 2024 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant