-
Notifications
You must be signed in to change notification settings - Fork 5
feat(syscall): implemented the reboot syscall with shutdown and reboot commands #180
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include "syscall/handlers/sys_power.h" | ||
|
|
||
| #include "power/power.h" | ||
| #include "hw/cpu.h" | ||
| #include "common/logging.h" | ||
|
|
||
| // Linux reboot(2) magic values, required so a stray syscall with a | ||
| // garbage number cannot take the machine down by accident. | ||
| constexpr uint32_t REBOOT_MAGIC1 = 0xFEE1DEAD; | ||
| constexpr uint32_t REBOOT_MAGIC2 = 672274793; | ||
| constexpr uint32_t REBOOT_MAGIC2A = 85072278; | ||
| constexpr uint32_t REBOOT_MAGIC2B = 369367448; | ||
| constexpr uint32_t REBOOT_MAGIC2C = 537993216; | ||
|
|
||
| // Linux reboot(2) commands | ||
| constexpr uint32_t REBOOT_CMD_RESTART = 0x01234567; | ||
| constexpr uint32_t REBOOT_CMD_HALT = 0xCDEF0123; | ||
| constexpr uint32_t REBOOT_CMD_POWER_OFF = 0x4321FEDC; | ||
|
|
||
| DEFINE_SYSCALL3(reboot, magic1, magic2, cmd) { | ||
| if (static_cast<uint32_t>(magic1) != REBOOT_MAGIC1) { | ||
| return syscall::EINVAL; | ||
| } | ||
|
|
||
| uint32_t m2 = static_cast<uint32_t>(magic2); | ||
| if (m2 != REBOOT_MAGIC2 && m2 != REBOOT_MAGIC2A && | ||
| m2 != REBOOT_MAGIC2B && m2 != REBOOT_MAGIC2C) { | ||
| return syscall::EINVAL; | ||
| } | ||
|
|
||
| switch (static_cast<uint32_t>(cmd)) { | ||
| case REBOOT_CMD_RESTART: | ||
| log::info("power: restarting system"); | ||
| power::reboot(); | ||
| return syscall::EOPNOTSUPP; | ||
| case REBOOT_CMD_POWER_OFF: | ||
| log::info("power: powering off"); | ||
| power::shutdown(); | ||
| return syscall::EOPNOTSUPP; | ||
| case REBOOT_CMD_HALT: | ||
| log::info("power: system halted"); | ||
| cpu::irq_disable(); | ||
| while (true) { | ||
| cpu::halt(); | ||
| } | ||
| default: | ||
| return syscall::EINVAL; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #ifndef STELLUX_SYSCALL_HANDLERS_SYS_POWER_H | ||
| #define STELLUX_SYSCALL_HANDLERS_SYS_POWER_H | ||
|
|
||
| #include "syscall/syscall_table.h" | ||
|
|
||
| DECLARE_SYSCALL(reboot); | ||
|
|
||
| #endif // STELLUX_SYSCALL_HANDLERS_SYS_POWER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| APP_NAME := reboot | ||
| include ../../mk/app.mk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #include <stdio.h> | ||
| #include <sys/reboot.h> | ||
|
|
||
| int main(void) { | ||
| printf("Restarting system...\n"); | ||
| fflush(stdout); | ||
|
|
||
| reboot(RB_AUTOBOOT); | ||
|
|
||
| // Only reachable when the kernel could not restart the machine | ||
| perror("reboot"); | ||
| return 1; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| APP_NAME := shutdown | ||
| include ../../mk/app.mk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #include <stdio.h> | ||
| #include <sys/reboot.h> | ||
|
|
||
| int main(void) { | ||
| printf("Powering off...\n"); | ||
| fflush(stdout); | ||
|
|
||
| reboot(RB_POWER_OFF); | ||
|
|
||
| // Only reachable when the platform could not power off, so stop | ||
| // the machine in a state that is safe to switch off by hand. | ||
| printf("Power off unsupported, halting instead.\n"); | ||
| fflush(stdout); | ||
| reboot(RB_HALT_SYSTEM); | ||
|
|
||
| perror("shutdown"); | ||
| return 1; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.