NMI (soft reset) control code enablement

Changes here include:
   1) A service that triggers Open BMC App (openpower-proc-nmi)
   2) An application that waits for a dbus event (NMI) to occur
   3) A reset logic that triggers a NMI/softreset dbus service which
      will invoke pdbg call to trigger stop followed by sreset on all
      threads
   4) Necessary Makefile.am changes

Tested: Verified following
 1) openpower-proc-nmi app is automatically started once host is started
 2) After killing the app made sure app is restarted successfully
 3) Verified NMI is detected on the Host side and crash dump being
    collected in /var/crash/ and taking a watchdog triggered reboot
 4) System coming back to original state once all #3 is completed
 5) Powered off the system (obmcutil poweroff) service disabled and
    enabled after host is powered on

Signed-off-by: Lakshminarayana R. Kammath <lkammath@in.ibm.com>
Change-Id: I16f3bb2a2ed0c0ffcea2a720a2ae39a2b303ef9e
diff --git a/Makefile.am b/Makefile.am
index 6c540f7..1280311 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,10 +1,13 @@
 AM_DEFAULT_SOURCE_EXT = .cpp
 
 systemdsystemunit_DATA = \
-  pcie-poweroff@.service
+  pcie-poweroff@.service \
+  xyz.openbmc_project.Control.Host.NMI.service \
+  nmi.service
 
 bin_PROGRAMS = \
-	openpower-proc-control
+	openpower-proc-control \
+	openpower-proc-nmi
 
 openpower_proc_control_SOURCES = \
 	proc_control.cpp \
@@ -15,6 +18,10 @@
 	openpower_procedures.cpp \
 	ext_interface.cpp
 
+openpower_proc_nmi_SOURCES = \
+	nmi_main.cpp \
+	nmi_interface.cpp
+
 CLEANFILES = openpower_procedures.cpp
 
 openpower_proc_control_LDFLAGS = $(PHOSPHOR_LOGGING_LIBS) \
@@ -28,6 +35,14 @@
                                   $(OPENPOWER_DBUS_INTERFACES_CFLAGS) \
                                   $(SDBUSPLUS_CFLAGS)
 
+openpower_proc_nmi_LDFLAGS = $(PHOSPHOR_LOGGING_LIBS) \
+                             $(PHOSPHOR_DBUS_INTERFACES_LIBS) \
+                             $(SDBUSPLUS_LIBS)
+
+openpower_proc_nmi_CXXFLAGS = $(PHOSPHOR_LOGGING_CFLAGS) \
+                              $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
+                              $(SDBUSPLUS_CFLAGS)
+
 SUBDIRS = test
 
 -include Makefile.generated
diff --git a/configure.ac b/configure.ac
index 0347604..ece0591 100644
--- a/configure.ac
+++ b/configure.ac
@@ -67,4 +67,6 @@
 
 AC_CONFIG_FILES([Makefile test/Makefile])
 AC_CONFIG_FILES([pcie-poweroff@.service])
+AC_CONFIG_FILES([xyz.openbmc_project.Control.Host.NMI.service])
+AC_CONFIG_FILES([nmi.service])
 AC_OUTPUT
diff --git a/nmi.service.in b/nmi.service.in
new file mode 100644
index 0000000..304faea
--- /dev/null
+++ b/nmi.service.in
@@ -0,0 +1,10 @@
+[Unit]
+Description=Enable Open Power NMI service
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart=@bindir@/pdbg -a stop
+ExecStart=@bindir@/pdbg -a sreset
+SyslogIdentifier=openpower-proc-nmi
+
diff --git a/nmi_interface.cpp b/nmi_interface.cpp
new file mode 100644
index 0000000..aea38ae
--- /dev/null
+++ b/nmi_interface.cpp
@@ -0,0 +1,58 @@
+/**
+ * Copyright (C) 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "nmi_interface.hpp"
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+namespace openpower
+{
+namespace proc
+{
+
+NMI::NMI(sdbusplus::bus::bus& bus, const char* path) :
+    Interface(bus, path), bus(bus), objectPath(path)
+{
+}
+
+void NMI::nMI()
+{
+    using namespace phosphor::logging;
+    using sdbusplus::exception::SdBusError;
+    using InternalFailure =
+        sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+
+    constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
+    constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
+    constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+
+    auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append("nmi.service", "replace");
+    try
+    {
+        bus.call_noreply(method);
+    }
+    catch (const SdBusError& e)
+    {
+        log<level::ALERT>("Error in starting NMI service. ");
+        report<InternalFailure>();
+    }
+}
+} // namespace proc
+} // namespace openpower
diff --git a/nmi_interface.hpp b/nmi_interface.hpp
new file mode 100644
index 0000000..9e3f052
--- /dev/null
+++ b/nmi_interface.hpp
@@ -0,0 +1,47 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
+#include <xyz/openbmc_project/Control/Host/NMI/server.hpp>
+
+namespace openpower
+{
+namespace proc
+{
+
+using Base = sdbusplus::xyz::openbmc_project::Control::Host::server::NMI;
+using Interface = sdbusplus::server::object::object<Base>;
+
+/*  @class NMI
+ *  @brief Implementation of NMI (Soft Reset)
+ */
+class NMI : public Interface
+{
+  public:
+    NMI() = delete;
+    NMI(const NMI&) = delete;
+    NMI& operator=(const NMI&) = delete;
+    NMI(NMI&&) = delete;
+    NMI& operator=(NMI&&) = delete;
+    virtual ~NMI() = default;
+
+    /*  @brief Constructor to put object onto bus at a dbus path.
+     *  @param[in] bus - sdbusplus D-Bus to attach to.
+     *  @param[in] path - Path to attach to.
+     */
+    NMI(sdbusplus::bus::bus& bus, const char* path);
+
+    /*  @brief trigger stop followed by soft reset.
+     */
+    void nMI() override;
+
+  private:
+    /** @brief sdbus handle */
+    sdbusplus::bus::bus& bus;
+
+    /** @brief object path */
+    std::string objectPath;
+};
+
+} // namespace proc
+} // namespace openpower
diff --git a/nmi_main.cpp b/nmi_main.cpp
new file mode 100644
index 0000000..dafc9b1
--- /dev/null
+++ b/nmi_main.cpp
@@ -0,0 +1,40 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "nmi_interface.hpp"
+
+#include <sdbusplus/bus.hpp>
+
+int main(int argc, char* argv[])
+{
+
+    constexpr auto BUSPATH_NMI = "/xyz/openbmc_project/control/host0/nmi";
+    constexpr auto BUSNAME_NMI = "xyz.openbmc_project.Control.Host.NMI";
+    auto bus = sdbusplus::bus::new_default();
+
+    // Add sdbusplus ObjectManager
+    sdbusplus::server::manager::manager objManager(bus, BUSPATH_NMI);
+    openpower::proc::NMI NMI(bus, BUSPATH_NMI);
+    bus.request_name(BUSNAME_NMI);
+
+    while (true)
+    {
+        bus.process_discard();
+        bus.wait();
+    }
+
+    return 0;
+}
diff --git a/proc_control.cpp b/proc_control.cpp
index b295c13..0035b4f 100644
--- a/proc_control.cpp
+++ b/proc_control.cpp
@@ -27,6 +27,7 @@
 #include <xyz/openbmc_project/Common/error.hpp>
 
 using namespace openpower::util;
+
 namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
 namespace device_error = sdbusplus::xyz::openbmc_project::Common::Device::Error;
 namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
diff --git a/xyz.openbmc_project.Control.Host.NMI.service.in b/xyz.openbmc_project.Control.Host.NMI.service.in
new file mode 100644
index 0000000..e13687d
--- /dev/null
+++ b/xyz.openbmc_project.Control.Host.NMI.service.in
@@ -0,0 +1,15 @@
+[Unit]
+Description=Start the Open Power Host Control NMI service
+Wants=obmc-host-started@0.target
+After=obmc-host-started@0.target
+Conflicts=obmc-host-stop@0.target
+
+[Service]
+ExecStart=@bindir@/openpower-proc-nmi
+SyslogIdentifier=openpower-proc-nmi
+Restart=always
+Type=dbus
+BusName=xyz.openbmc_project.Control.Host.NMI
+
+[Install]
+WantedBy=obmc-host-started@0.target