From 26bbcd97d817453a114a43b661bceff5aa0131f2 Mon Sep 17 00:00:00 2001
From: Stephen Cprek <smcprek@us.ibm.com>
Date: Wed, 10 Jan 2018 16:51:09 -0600
Subject: Open Untrusted Communication for SP

Change-Id: I87c7fe74349521eaec6ebf373e99f13eb1bf74f8
CQ:SW412793
Backport:release-fips910
Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/51742
Reviewed-by: Nicholas E. Bofferding <bofferdn@us.ibm.com>
Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP Build CI <op-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP HW <op-hw-jenkins+hostboot@us.ibm.com>
Reviewed-by: Michael Baiocchi <mbaiocch@us.ibm.com>
Reviewed-by: Daniel M. Crowell <dcrowell@us.ibm.com>
---
 src/usr/runtime/hdatservice.C        |  31 ++++++++++-
 src/usr/runtime/hdatstructs.H        |  22 +++++++-
 src/usr/runtime/populate_hbruntime.C | 105 ++++++++++++++++++++++++++++++++++-
 3 files changed, 155 insertions(+), 3 deletions(-)

(limited to 'src/usr/runtime')

diff --git a/src/usr/runtime/hdatservice.C b/src/usr/runtime/hdatservice.C
index 721e9ccc9..3986efce6 100644
--- a/src/usr/runtime/hdatservice.C
+++ b/src/usr/runtime/hdatservice.C
@@ -5,7 +5,7 @@
 /*                                                                        */
 /* OpenPOWER HostBoot Project                                             */
 /*                                                                        */
-/* Contributors Listed Below - COPYRIGHT 2012,2017                        */
+/* Contributors Listed Below - COPYRIGHT 2012,2018                        */
 /* [+] International Business Machines Corp.                              */
 /*                                                                        */
 /*                                                                        */
@@ -927,6 +927,31 @@ errlHndl_t hdatService::getHostDataSection( SectionId i_section,
             o_dataSize = MDT_MAINSTORE_ADDR_SECTION_HYP_HB_COMM_ADDR_SIZE; // 8 bytes
 
         }
+        // SPIRA-H CPU Controls
+        else if ( RUNTIME::CPU_CTRL == i_section )
+        {
+            // Find the right tuple and verify it makes sense
+            errhdl = getAndCheckTuple(i_section, tuple);
+            if( errhdl )
+            {
+                break;
+            }
+            TRACDCOMP( g_trac_runtime, "CPU_CTRL tuple=%p", tuple );
+
+            uint64_t base_addr = 0;
+            errhdl = getSpiraTupleVA(tuple, base_addr);
+            if( errhdl )
+            {
+                break;
+            }
+
+            TRACDCOMP( g_trac_runtime, "cpu_ctrl_data=%p", base_addr );
+
+            // set the base address and size for the section
+            record_size = tuple->hdatActualSize;
+            o_dataSize = record_size;
+            o_dataAddr = base_addr;
+        }
         // Not sure how we could get here...
         else
         {
@@ -1506,6 +1531,10 @@ errlHndl_t hdatService::getAndCheckTuple(const SectionId i_section,
             l_spiraS = SPIRAS_MDT;
             l_spiraL = SPIRAL_MDT;
             break;
+        case RUNTIME::CPU_CTRL:
+            l_spiraH = SPIRAH_CPU_CTRL;
+            l_spiraL = SPIRAL_CPU_CTRL;
+            break;
         default:
             TRACFCOMP(g_trac_runtime, ERR_MRK"getAndCheckTuple> section %d not supported",
                       i_section );
diff --git a/src/usr/runtime/hdatstructs.H b/src/usr/runtime/hdatstructs.H
index 78c057fde..29f23eac8 100644
--- a/src/usr/runtime/hdatstructs.H
+++ b/src/usr/runtime/hdatstructs.H
@@ -5,7 +5,7 @@
 /*                                                                        */
 /* OpenPOWER HostBoot Project                                             */
 /*                                                                        */
-/* Contributors Listed Below - COPYRIGHT 2012,2017                        */
+/* Contributors Listed Below - COPYRIGHT 2012,2018                        */
 /* [+] International Business Machines Corp.                              */
 /*                                                                        */
 /*                                                                        */
@@ -418,4 +418,24 @@ struct hbHypCommArea_t
     }
 } PACKED;
 
+/** @brief The fields in the CPU Controls Legacy structure are a set of
+ *         address / length pairs as shown below
+ */
+struct hdatCpuCtrlPair_t
+{
+    uint64_t address;    // 0x0000 Absolute main store adress relative to HRMOR
+    uint64_t size;       // 0x0008 Data structure size in bytes
+}__attribute__ ((packed));
+
+/** @brief CPU Controls Legacy Structure pointed to by the CPU Controls Header
+ *         Area pointer in the CPU Controls
+ */
+struct hdatCpuCtrlInfo_t
+{
+    hdatCpuCtrlPair_t spAddrTable;       // SP Address Table (SPAT)
+    hdatCpuCtrlPair_t spAttnArea1;       // SP Attention Area 1
+    hdatCpuCtrlPair_t spAttnArea2;       // SP Attention Area 2
+    hdatCpuCtrlPair_t servRoutineData;   // Service Routines Data Area
+}  __attribute__ ((packed));
+
 #endif
diff --git a/src/usr/runtime/populate_hbruntime.C b/src/usr/runtime/populate_hbruntime.C
index f4d69694c..afd0a6b10 100644
--- a/src/usr/runtime/populate_hbruntime.C
+++ b/src/usr/runtime/populate_hbruntime.C
@@ -5,7 +5,7 @@
 /*                                                                        */
 /* OpenPOWER HostBoot Project                                             */
 /*                                                                        */
-/* Contributors Listed Below - COPYRIGHT 2016,2017                        */
+/* Contributors Listed Below - COPYRIGHT 2016,2018                        */
 /* [+] International Business Machines Corp.                              */
 /*                                                                        */
 /*                                                                        */
@@ -75,6 +75,7 @@
 #include <util/utilmclmgr.H>
 #include <pnor/pnor_reasoncodes.H>
 #include <runtime/common/runtime_utils.H>
+#include <limits.h>
 
 namespace RUNTIME
 {
@@ -2327,5 +2328,107 @@ errlHndl_t persistent_rwAttrRuntimeCheck( void )
     return l_err;
 } // end persistent_rwAttrRuntimeCheck
 
+errlHndl_t openUntrustedSpCommArea()
+{
+    TRACFCOMP( g_trac_runtime, ENTER_MRK "openUntrustedSpCommArea()");
+    errlHndl_t l_err = nullptr;
+
+    do {
+    TARGETING::Target * l_sys = nullptr;
+    TARGETING::targetService().getTopLevelTarget(l_sys);
+    assert(l_sys != nullptr, "openUntrustedSpCommArea: top level target nullptr");
+
+    // Get Payload HRMOR
+    uint64_t l_hrmor = l_sys->getAttr<TARGETING::ATTR_PAYLOAD_BASE>() * MEGABYTE;
+
+    // pass 0 since there is only one record
+    const uint64_t l_instance = 0;
+    uint64_t l_cpuCtrlDataAddr = 0;
+    size_t l_cpuCtrlDataSizeMax = 0;
+
+    // Get the address of the Spira-H CPU control section
+    l_err = RUNTIME::get_host_data_section( RUNTIME::CPU_CTRL,
+                                            l_instance,
+                                            l_cpuCtrlDataAddr,
+                                            l_cpuCtrlDataSizeMax);
+    if(l_err != nullptr)
+    {
+        TRACFCOMP( g_trac_runtime, ERR_MRK "openUntrustedSpCommArea(): get_host_data_section() failed for CPU_CTRL HDAT section");
+        break;
+    }
+
+    // Traverse CPU Controls Header Area pointer to find CPU Controls Structure
+    auto const l_pCpuCtrlHdr =
+        reinterpret_cast<hdatHDIF_t*>(l_cpuCtrlDataAddr);
+    auto const l_pCpuDataPointer =
+        reinterpret_cast<hdatHDIFDataHdr_t*>(l_cpuCtrlDataAddr +
+                                             l_pCpuCtrlHdr->hdatDataPtrOffset);
+    auto const l_pCpuCtrlInfo =
+        reinterpret_cast<hdatCpuCtrlInfo_t*>(l_cpuCtrlDataAddr +
+                                             l_pCpuDataPointer->hdatOffset);
+
+    // Get Address of First SP ATTN area and size of both SP ATTN areas
+    // Add HRMOR to address as it's relative to the HRMOR
+    uint64_t l_spAttnStartAddr = l_pCpuCtrlInfo->spAttnArea1.address + l_hrmor;
+    size_t l_spAttnCombinedSize = l_pCpuCtrlInfo->spAttnArea1.size +
+                                  l_pCpuCtrlInfo->spAttnArea2.size;
+
+    TRACFCOMP( g_trac_runtime, "openUntrustedSpCommArea() SP ATTN addr = 0x%016llx combined size 0x%X",
+               l_spAttnStartAddr,
+               l_spAttnCombinedSize);
+
+    // Open unsecure SBE memory regions
+    // Loop through all functional Procs
+    TARGETING::TargetHandleList l_procChips;
+    getAllChips(l_procChips, TARGETING::TYPE_PROC);
+    for (const auto & l_procChip : l_procChips)
+    {
+        // Get Instance ID of proc for trace
+        uint32_t l_id = l_procChip->getAttr<TARGETING::ATTR_HBRT_HYP_ID>();
+
+        // Open SP ATTN region
+        l_err = SBEIO::openUnsecureMemRegion(l_spAttnStartAddr,
+                                             l_spAttnCombinedSize,
+                                             true, //true=Read-Write
+                                             l_procChip);
+        if (l_err)
+        {
+            TRACFCOMP( g_trac_runtime, ERR_MRK "openUntrustedSpCommArea(): openUnsecureMemRegion() failed proc = 0x%X addr = 0x%016llx size = 0x%X",
+                      l_id,
+                      l_spAttnStartAddr,
+                      l_spAttnCombinedSize);
+            break;
+        }
+
+        // Only open additional SBE window in PHYP mode
+        if(TARGETING::is_phyp_load())
+        {
+            l_err = SBEIO::openUnsecureMemRegion(
+                                        RUNTIME::SP_HOST_UNTRUSTED_COMM_AREA_ADDR,
+                                        RUNTIME::SP_HOST_UNTRUSTED_COMM_AREA_SIZE,
+                                        true, //true=Read-Write
+                                        l_procChip);
+            if (l_err)
+            {
+                TRACFCOMP(g_trac_runtime, ERR_MRK "openUntrustedSpCommArea(): openUnsecureMemRegion() failed proc = 0x%X addr = 0x%016llx size = 0x%X",
+                          l_id,
+                          RUNTIME::SP_HOST_UNTRUSTED_COMM_AREA_ADDR,
+                          RUNTIME::SP_HOST_UNTRUSTED_COMM_AREA_SIZE);
+                break;
+            }
+        }
+    }
+    if(l_err)
+    {
+        break;
+    }
+
+    } while(0);
+
+    TRACFCOMP( g_trac_runtime, EXIT_MRK"openUntrustedSpCommArea()");
+
+    return l_err;
+}
+
 } //namespace RUNTIME
 
-- 
cgit v1.2.1