Commit 2adcba79 authored by Jarkko Sakkinen's avatar Jarkko Sakkinen Committed by Borislav Petkov
Browse files

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: default avatarJarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
Acked-by: default avatarJethro Beekman <jethro@fortanix.com>
Cc: linux-kselftest@vger.kernel.org
Link: https://lkml.kernel.org/r/20201112220135.165028-21-jarkko@kernel.org
parent 84664369
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ TARGETS += openat2
TARGETS += rseq
TARGETS += rtc
TARGETS += seccomp
TARGETS += sgx
TARGETS += sigaltstack
TARGETS += size
TARGETS += sparc64
+2 −0
Original line number Diff line number Diff line
test_sgx
test_encl.elf
+53 −0
Original line number Diff line number Diff line
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 −0
Original line number Diff line number Diff line
/* 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 −0
Original line number Diff line number Diff line
/* 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