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 | |
Jayanth Othayoth | 785cf6a | 2021-06-28 00:16:35 -0500 | [diff] [blame] | 19 | extern "C" |
| 20 | { |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 21 | #include <libpdbg.h> |
Jayanth Othayoth | 785cf6a | 2021-06-28 00:16:35 -0500 | [diff] [blame] | 22 | #include <libpdbg_sbe.h> |
| 23 | } |
| 24 | |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 25 | #include <sys/wait.h> |
| 26 | #include <unistd.h> |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 27 | |
| 28 | #include <phosphor-logging/log.hpp> |
Brad Bishop | 5e5d445 | 2020-10-27 19:46:13 -0400 | [diff] [blame] | 29 | |
Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 30 | #include <system_error> |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | namespace openpower |
| 34 | { |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 35 | namespace misc |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 36 | { |
| 37 | |
| 38 | /** |
| 39 | * @brief Calls sbe_enter_mpipl on the SBE in the provided target. |
| 40 | * @return void |
| 41 | */ |
| 42 | void sbeEnterMpReboot(struct pdbg_target* tgt) |
| 43 | { |
| 44 | using namespace phosphor::logging; |
| 45 | int error = 0; |
| 46 | if ((error = sbe_mpipl_enter(tgt)) < 0) |
| 47 | { |
| 48 | log<level::ERR>("Failed to initiate memory preserving reboot"); |
| 49 | // TODO Create a PEL in the future for this failure case. |
| 50 | throw std::system_error(error, std::generic_category(), |
| 51 | "Failed to initiate memory preserving reboot"); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @brief initiate memory preserving reboot on each SBE. |
| 57 | * @return void |
| 58 | */ |
| 59 | void enterMpReboot() |
| 60 | { |
| 61 | using namespace phosphor::logging; |
| 62 | struct pdbg_target* target; |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 63 | std::vector<pid_t> pidList; |
| 64 | bool failed = false; |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 65 | pdbg_targets_init(NULL); |
| 66 | |
| 67 | log<level::INFO>("Starting memory preserving reboot"); |
| 68 | pdbg_for_each_class_target("pib", target) |
| 69 | { |
| 70 | if (pdbg_target_probe(target) != PDBG_TARGET_ENABLED) |
| 71 | { |
| 72 | continue; |
| 73 | } |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 74 | |
| 75 | pid_t pid = fork(); |
| 76 | |
| 77 | if (pid < 0) |
| 78 | { |
| 79 | log<level::ERR>("Fork failed while starting mp reboot"); |
| 80 | failed = true; |
| 81 | } |
| 82 | else if (pid == 0) |
| 83 | { |
| 84 | sbeEnterMpReboot(target); |
| 85 | std::exit(EXIT_SUCCESS); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | pidList.push_back(std::move(pid)); |
| 90 | } |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 91 | } |
| 92 | |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 93 | for (auto& p : pidList) |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 94 | { |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 95 | int status = 0; |
| 96 | waitpid(p, &status, 0); |
| 97 | if (WEXITSTATUS(status)) |
| 98 | { |
| 99 | log<level::ERR>("Memory preserving reboot failed"); |
| 100 | failed = true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (failed) |
| 105 | { |
| 106 | std::exit(EXIT_FAILURE); |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Brad Bishop | 63508a7 | 2020-10-27 18:55:01 -0400 | [diff] [blame] | 110 | REGISTER_PROCEDURE("enterMpReboot", enterMpReboot) |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 111 | |
Dhruvaraj Subhashchandran | 285f73e | 2020-05-15 03:35:02 -0500 | [diff] [blame] | 112 | } // namespace misc |
Dhruvaraj Subhashchandran | 08fc264 | 2020-02-17 01:47:05 -0600 | [diff] [blame] | 113 | } // namespace openpower |