Skip to content

Commit

Permalink
selftests/x86: Add a selftest for SGX
Browse files Browse the repository at this point in the history
Add a selftest for SGX. It is a trivial test where a simple enclave
copies one 64-bit word of memory between two memory locations,
but ensures that all SGX hardware and software infrastructure is
functioning.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Acked-by: Jethro Beekman <jethro@fortanix.com>
Cc: linux-kselftest@vger.kernel.org
Link: https://lkml.kernel.org/r/20201112220135.165028-21-jarkko@kernel.org
  • Loading branch information
Jarkko Sakkinen authored and Borislav Petkov committed Nov 18, 2020
1 parent 8466436 commit 2adcba7
Show file tree
Hide file tree
Showing 12 changed files with 1,222 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ TARGETS += openat2
TARGETS += rseq
TARGETS += rtc
TARGETS += seccomp
TARGETS += sgx
TARGETS += sigaltstack
TARGETS += size
TARGETS += sparc64
Expand Down
2 changes: 2 additions & 0 deletions tools/testing/selftests/sgx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test_sgx
test_encl.elf
53 changes: 53 additions & 0 deletions tools/testing/selftests/sgx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
top_srcdir = ../../../..

include ../lib.mk

.PHONY: all clean

CAN_BUILD_X86_64 := $(shell ../x86/check_cc.sh $(CC) \
../x86/trivial_64bit_program.c)

ifndef OBJCOPY
OBJCOPY := $(CROSS_COMPILE)objcopy
endif

INCLUDES := -I$(top_srcdir)/tools/include
HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC -z noexecstack
ENCL_CFLAGS := -Wall -Werror -static -nostdlib -nostartfiles -fPIC \
-fno-stack-protector -mrdrnd $(INCLUDES)

TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx

ifeq ($(CAN_BUILD_X86_64), 1)
all: $(TEST_CUSTOM_PROGS) $(OUTPUT)/test_encl.elf
endif

$(OUTPUT)/test_sgx: $(OUTPUT)/main.o \
$(OUTPUT)/load.o \
$(OUTPUT)/sigstruct.o \
$(OUTPUT)/call.o
$(CC) $(HOST_CFLAGS) -o $@ $^ -lcrypto

$(OUTPUT)/main.o: main.c
$(CC) $(HOST_CFLAGS) -c $< -o $@

$(OUTPUT)/load.o: load.c
$(CC) $(HOST_CFLAGS) -c $< -o $@

$(OUTPUT)/sigstruct.o: sigstruct.c
$(CC) $(HOST_CFLAGS) -c $< -o $@

$(OUTPUT)/call.o: call.S
$(CC) $(HOST_CFLAGS) -c $< -o $@

$(OUTPUT)/test_encl.elf: test_encl.lds test_encl.c test_encl_bootstrap.S
$(CC) $(ENCL_CFLAGS) -T $^ -o $@

EXTRA_CLEAN := \
$(OUTPUT)/test_encl.elf \
$(OUTPUT)/load.o \
$(OUTPUT)/call.o \
$(OUTPUT)/main.o \
$(OUTPUT)/sigstruct.o \
$(OUTPUT)/test_sgx \
$(OUTPUT)/test_sgx.o \
44 changes: 44 additions & 0 deletions tools/testing/selftests/sgx/call.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* SPDX-License-Identifier: GPL-2.0 */
/**
* Copyright(c) 2016-20 Intel Corporation.
*/

.text

.global sgx_call_vdso
sgx_call_vdso:
.cfi_startproc
push %r15
.cfi_adjust_cfa_offset 8
.cfi_rel_offset %r15, 0
push %r14
.cfi_adjust_cfa_offset 8
.cfi_rel_offset %r14, 0
push %r13
.cfi_adjust_cfa_offset 8
.cfi_rel_offset %r13, 0
push %r12
.cfi_adjust_cfa_offset 8
.cfi_rel_offset %r12, 0
push %rbx
.cfi_adjust_cfa_offset 8
.cfi_rel_offset %rbx, 0
push $0
.cfi_adjust_cfa_offset 8
push 0x38(%rsp)
.cfi_adjust_cfa_offset 8
call *eenter(%rip)
add $0x10, %rsp
.cfi_adjust_cfa_offset -0x10
pop %rbx
.cfi_adjust_cfa_offset -8
pop %r12
.cfi_adjust_cfa_offset -8
pop %r13
.cfi_adjust_cfa_offset -8
pop %r14
.cfi_adjust_cfa_offset -8
pop %r15
.cfi_adjust_cfa_offset -8
ret
.cfi_endproc
21 changes: 21 additions & 0 deletions tools/testing/selftests/sgx/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright(c) 2016-20 Intel Corporation.
*/

#ifndef DEFINES_H
#define DEFINES_H

#include <stdint.h>

#define PAGE_SIZE 4096
#define PAGE_MASK (~(PAGE_SIZE - 1))

#define __aligned(x) __attribute__((__aligned__(x)))
#define __packed __attribute__((packed))

#include "../../../../arch/x86/kernel/cpu/sgx/arch.h"
#include "../../../../arch/x86/include/asm/enclu.h"
#include "../../../../arch/x86/include/uapi/asm/sgx.h"

#endif /* DEFINES_H */
Loading

0 comments on commit 2adcba7

Please sign in to comment.