Remove encl_build() and introduce encl_build_segment(), which builds and maps one segment of the enclave with given flags and permissions. This enables to load segments directly from an ELF files. Cc: Sean Christopherson <sean.j.christopherson@xxxxxxxxx> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx> --- tools/testing/selftests/sgx/main.c | 35 ++++++++++++++---------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index 995423565c83..a78e64159313 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -191,30 +191,18 @@ static bool encl_add_pages(int dev_fd, unsigned long offset, void *data, return true; } -#define SGX_REG_PAGE_FLAGS \ - (SGX_SECINFO_REG | SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_X) -static bool encl_build(int encl_fd, struct sgx_secs *secs, void *bin, - unsigned long bin_size) +static bool encl_build_segment(int encl_fd, struct sgx_secs *secs, void *bin, + unsigned long seg_offset, unsigned long seg_size, + uint64_t flags, int prot) { void *addr; - if (!encl_add_pages(encl_fd, 0, bin, PAGE_SIZE, SGX_SECINFO_TCS)) + if (!encl_add_pages(encl_fd, seg_offset, bin + seg_offset, seg_size, + flags)) return false; - if (!encl_add_pages(encl_fd, PAGE_SIZE, bin + PAGE_SIZE, - bin_size - PAGE_SIZE, SGX_REG_PAGE_FLAGS)) - return false; - - addr = mmap((void *)secs->base, PAGE_SIZE, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_FIXED, encl_fd, 0); - if (addr == MAP_FAILED) { - fprintf(stderr, "mmap() failed on TCS, errno=%d.\n", errno); - return false; - } - - addr = mmap((void *)(secs->base + PAGE_SIZE), bin_size - PAGE_SIZE, - PROT_READ | PROT_WRITE | PROT_EXEC, + addr = mmap((void *)secs->base + seg_offset, seg_size, prot, MAP_SHARED | MAP_FIXED, encl_fd, 0); if (addr == MAP_FAILED) { fprintf(stderr, "mmap() failed, errno=%d.\n", errno); @@ -324,7 +312,16 @@ int main(int argc, char *argv[], char *envp[]) if (!encl_create(ctx.encl_fd, ctx.bin_size, &ctx.secs)) goto err; - if (!encl_build(ctx.encl_fd, &ctx.secs, ctx.bin, ctx.bin_size)) + /* TCS */ + if (!encl_build_segment(ctx.encl_fd, &ctx.secs, ctx.bin, 0, PAGE_SIZE, + SGX_SECINFO_TCS, PROT_READ | PROT_WRITE)) + goto err; + + if (!encl_build_segment(ctx.encl_fd, &ctx.secs, ctx.bin, PAGE_SIZE, + ctx.bin_size - PAGE_SIZE, + SGX_SECINFO_REG | SGX_SECINFO_R | + SGX_SECINFO_W | SGX_SECINFO_X, + PROT_READ | PROT_WRITE | PROT_EXEC)) goto err; if (!encl_create_sigstruct(ctx.bin, ctx.bin_size, &sigstruct)) -- 2.25.1