Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 17 | #include "registration.hpp" |
| 18 | |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 19 | #include <libpdbg.h> |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 20 | #include <sys/wait.h> |
| 21 | #include <unistd.h> |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 22 | |
| 23 | #include <phosphor-logging/log.hpp> |
Brad Bishop | 5e5d445 | 2020-10-27 19:46:13 -0400 | [diff] [blame] | 24 | |
Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 25 | #include <system_error> |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | namespace openpower |
| 29 | { |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 30 | namespace misc |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 31 | { |
| 32 | |
| 33 | /** |
| 34 | * @brief Calls sbe_enter_mpipl on the SBE in the provided target. |
| 35 | * @return void |
| 36 | */ |
| 37 | void sbeEnterMpReboot(struct pdbg_target* tgt) |
| 38 | { |
| 39 | using namespace phosphor::logging; |
| 40 | int error = 0; |
| 41 | if ((error = sbe_mpipl_enter(tgt)) < 0) |
| 42 | { |
| 43 | log<level::ERR>("Failed to initiate memory preserving reboot"); |
| 44 | // TODO Create a PEL in the future for this failure case. |
| 45 | throw std::system_error(error, std::generic_category(), |
| 46 | "Failed to initiate memory preserving reboot"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @brief initiate memory preserving reboot on each SBE. |
| 52 | * @return void |
| 53 | */ |
| 54 | void enterMpReboot() |
| 55 | { |
| 56 | using namespace phosphor::logging; |
| 57 | struct pdbg_target* target; |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 58 | std::vector<pid_t> pidList; |
| 59 | bool failed = false; |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 60 | pdbg_targets_init(NULL); |
| 61 | |
| 62 | log<level::INFO>("Starting memory preserving reboot"); |
| 63 | pdbg_for_each_class_target("pib", target) |
| 64 | { |
| 65 | if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED) |
| 66 | { |
| 67 | continue; |
| 68 | } |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 69 | |
| 70 | pid_t pid = fork(); |
| 71 | |
| 72 | if (pid < 0) |
| 73 | { |
| 74 | log<level::ERR>("Fork failed while starting mp reboot"); |
| 75 | failed = true; |
| 76 | } |
| 77 | else if (pid == 0) |
| 78 | { |
| 79 | sbeEnterMpReboot(target); |
| 80 | std::exit(EXIT_SUCCESS); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | pidList.push_back(std::move(pid)); |
| 85 | } |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 88 | for (auto& p : pidList) |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 89 | { |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 90 | int status = 0; |
| 91 | waitpid(p, &status, 0); |
| 92 | if (WEXITSTATUS(status)) |
| 93 | { |
| 94 | log<level::ERR>("Memory preserving reboot failed"); |
| 95 | failed = true; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (failed) |
| 100 | { |
| 101 | std::exit(EXIT_FAILURE); |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 105 | REGISTER_PROCEDURE("enterMpReboot", enterMpReboot) |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 106 | |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 107 | } // namespace misc |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 108 | } // namespace openpower |