selftests/sgx: Introduce TCS initialization enclave operation
authorReinette Chatre <reinette.chatre@intel.com>
Tue, 10 May 2022 18:09:02 +0000 (11:09 -0700)
committerDave Hansen <dave.hansen@linux.intel.com>
Thu, 7 Jul 2022 17:13:04 +0000 (10:13 -0700)
The Thread Control Structure (TCS) contains meta-data used by the
hardware to save and restore thread specific information when
entering/exiting the enclave. A TCS can be added to an initialized
enclave by first adding a new regular enclave page, initializing the
content of the new page from within the enclave, and then changing that
page's type to a TCS.

Support the initialization of a TCS from within the enclave.
The variable information needed that should be provided from outside
the enclave is the address of the TCS, address of the State Save Area
(SSA), and the entry point that the thread should use to enter the
enclave. With this information provided all needed fields of a TCS
can be initialized.

Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Acked-by: Jarkko Sakkinen <jarkko@kernel.org>
Link: https://lkml.kernel.org/r/bad6052056188bde753a54313da1ac8f1e29088a.1652137848.git.reinette.chatre@intel.com
tools/testing/selftests/sgx/defines.h
tools/testing/selftests/sgx/test_encl.c

index b638eb98c80c87a3ea3b6549b38c2ad03a9ab1ed..d8587c971941a8ac40f9a284fc34d2993fa0d8af 100644 (file)
@@ -26,6 +26,7 @@ enum encl_op_type {
        ENCL_OP_NOP,
        ENCL_OP_EACCEPT,
        ENCL_OP_EMODPE,
+       ENCL_OP_INIT_TCS_PAGE,
        ENCL_OP_MAX,
 };
 
@@ -68,4 +69,11 @@ struct encl_op_emodpe {
        uint64_t flags;
 };
 
+struct encl_op_init_tcs_page {
+       struct encl_op_header header;
+       uint64_t tcs_page;
+       uint64_t ssa;
+       uint64_t entry;
+};
+
 #endif /* DEFINES_H */
index 5b6c653315270538ed0c797b4c9a9d3e087b53b9..c0d6397295e311499484e1332e4f7f7d05775bbb 100644 (file)
@@ -57,6 +57,35 @@ static void *memcpy(void *dest, const void *src, size_t n)
        return dest;
 }
 
+static void *memset(void *dest, int c, size_t n)
+{
+       size_t i;
+
+       for (i = 0; i < n; i++)
+               ((char *)dest)[i] = c;
+
+       return dest;
+}
+
+static void do_encl_init_tcs_page(void *_op)
+{
+       struct encl_op_init_tcs_page *op = _op;
+       void *tcs = (void *)op->tcs_page;
+       uint32_t val_32;
+
+       memset(tcs, 0, 16);                     /* STATE and FLAGS */
+       memcpy(tcs + 16, &op->ssa, 8);          /* OSSA */
+       memset(tcs + 24, 0, 4);                 /* CSSA */
+       val_32 = 1;
+       memcpy(tcs + 28, &val_32, 4);           /* NSSA */
+       memcpy(tcs + 32, &op->entry, 8);        /* OENTRY */
+       memset(tcs + 40, 0, 24);                /* AEP, OFSBASE, OGSBASE */
+       val_32 = 0xFFFFFFFF;
+       memcpy(tcs + 64, &val_32, 4);           /* FSLIMIT */
+       memcpy(tcs + 68, &val_32, 4);           /* GSLIMIT */
+       memset(tcs + 72, 0, 4024);              /* Reserved */
+}
+
 static void do_encl_op_put_to_buf(void *op)
 {
        struct encl_op_put_to_buf *op2 = op;
@@ -100,6 +129,7 @@ void encl_body(void *rdi,  void *rsi)
                do_encl_op_nop,
                do_encl_eaccept,
                do_encl_emodpe,
+               do_encl_init_tcs_page,
        };
 
        struct encl_op_header *op = (struct encl_op_header *)rdi;