Skip to content

Commit 2adcba7

Browse files
jarkkojssuryasaimadhu
authored andcommitted
selftests/x86: Add a selftest for SGX
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 <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Acked-by: Jethro Beekman <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 8466436 commit 2adcba7

File tree

12 files changed

+1222
-0
lines changed

12 files changed

+1222
-0
lines changed

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ TARGETS += openat2
5050
TARGETS += rseq
5151
TARGETS += rtc
5252
TARGETS += seccomp
53+
TARGETS += sgx
5354
TARGETS += sigaltstack
5455
TARGETS += size
5556
TARGETS += sparc64
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test_sgx
2+
test_encl.elf

tools/testing/selftests/sgx/Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
top_srcdir = ../../../..
2+
3+
include ../lib.mk
4+
5+
.PHONY: all clean
6+
7+
CAN_BUILD_X86_64 := $(shell ../x86/check_cc.sh $(CC) \
8+
../x86/trivial_64bit_program.c)
9+
10+
ifndef OBJCOPY
11+
OBJCOPY := $(CROSS_COMPILE)objcopy
12+
endif
13+
14+
INCLUDES := -I$(top_srcdir)/tools/include
15+
HOST_CFLAGS := -Wall -Werror -g $(INCLUDES) -fPIC -z noexecstack
16+
ENCL_CFLAGS := -Wall -Werror -static -nostdlib -nostartfiles -fPIC \
17+
-fno-stack-protector -mrdrnd $(INCLUDES)
18+
19+
TEST_CUSTOM_PROGS := $(OUTPUT)/test_sgx
20+
21+
ifeq ($(CAN_BUILD_X86_64), 1)
22+
all: $(TEST_CUSTOM_PROGS) $(OUTPUT)/test_encl.elf
23+
endif
24+
25+
$(OUTPUT)/test_sgx: $(OUTPUT)/main.o \
26+
$(OUTPUT)/load.o \
27+
$(OUTPUT)/sigstruct.o \
28+
$(OUTPUT)/call.o
29+
$(CC) $(HOST_CFLAGS) -o $@ $^ -lcrypto
30+
31+
$(OUTPUT)/main.o: main.c
32+
$(CC) $(HOST_CFLAGS) -c $< -o $@
33+
34+
$(OUTPUT)/load.o: load.c
35+
$(CC) $(HOST_CFLAGS) -c $< -o $@
36+
37+
$(OUTPUT)/sigstruct.o: sigstruct.c
38+
$(CC) $(HOST_CFLAGS) -c $< -o $@
39+
40+
$(OUTPUT)/call.o: call.S
41+
$(CC) $(HOST_CFLAGS) -c $< -o $@
42+
43+
$(OUTPUT)/test_encl.elf: test_encl.lds test_encl.c test_encl_bootstrap.S
44+
$(CC) $(ENCL_CFLAGS) -T $^ -o $@
45+
46+
EXTRA_CLEAN := \
47+
$(OUTPUT)/test_encl.elf \
48+
$(OUTPUT)/load.o \
49+
$(OUTPUT)/call.o \
50+
$(OUTPUT)/main.o \
51+
$(OUTPUT)/sigstruct.o \
52+
$(OUTPUT)/test_sgx \
53+
$(OUTPUT)/test_sgx.o \

tools/testing/selftests/sgx/call.S

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/**
3+
* Copyright(c) 2016-20 Intel Corporation.
4+
*/
5+
6+
.text
7+
8+
.global sgx_call_vdso
9+
sgx_call_vdso:
10+
.cfi_startproc
11+
push %r15
12+
.cfi_adjust_cfa_offset 8
13+
.cfi_rel_offset %r15, 0
14+
push %r14
15+
.cfi_adjust_cfa_offset 8
16+
.cfi_rel_offset %r14, 0
17+
push %r13
18+
.cfi_adjust_cfa_offset 8
19+
.cfi_rel_offset %r13, 0
20+
push %r12
21+
.cfi_adjust_cfa_offset 8
22+
.cfi_rel_offset %r12, 0
23+
push %rbx
24+
.cfi_adjust_cfa_offset 8
25+
.cfi_rel_offset %rbx, 0
26+
push $0
27+
.cfi_adjust_cfa_offset 8
28+
push 0x38(%rsp)
29+
.cfi_adjust_cfa_offset 8
30+
call *eenter(%rip)
31+
add $0x10, %rsp
32+
.cfi_adjust_cfa_offset -0x10
33+
pop %rbx
34+
.cfi_adjust_cfa_offset -8
35+
pop %r12
36+
.cfi_adjust_cfa_offset -8
37+
pop %r13
38+
.cfi_adjust_cfa_offset -8
39+
pop %r14
40+
.cfi_adjust_cfa_offset -8
41+
pop %r15
42+
.cfi_adjust_cfa_offset -8
43+
ret
44+
.cfi_endproc

tools/testing/selftests/sgx/defines.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright(c) 2016-20 Intel Corporation.
4+
*/
5+
6+
#ifndef DEFINES_H
7+
#define DEFINES_H
8+
9+
#include <stdint.h>
10+
11+
#define PAGE_SIZE 4096
12+
#define PAGE_MASK (~(PAGE_SIZE - 1))
13+
14+
#define __aligned(x) __attribute__((__aligned__(x)))
15+
#define __packed __attribute__((packed))
16+
17+
#include "../../../../arch/x86/kernel/cpu/sgx/arch.h"
18+
#include "../../../../arch/x86/include/asm/enclu.h"
19+
#include "../../../../arch/x86/include/uapi/asm/sgx.h"
20+
21+
#endif /* DEFINES_H */

0 commit comments

Comments
 (0)