xref: /linux/tools/testing/selftests/sgx/test_encl.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
12adcba79SJarkko Sakkinen // SPDX-License-Identifier: GPL-2.0
22adcba79SJarkko Sakkinen /*  Copyright(c) 2016-20 Intel Corporation. */
32adcba79SJarkko Sakkinen 
42adcba79SJarkko Sakkinen #include <stddef.h>
52adcba79SJarkko Sakkinen #include "defines.h"
62adcba79SJarkko Sakkinen 
7abc5cec4SReinette Chatre /*
8a4c39ef4SJo Van Bulck  * Data buffer spanning two pages that will be placed first in the .data
9*02241649SJo Van Bulck  * segment via the linker script. Even if not used internally the second page
10*02241649SJo Van Bulck  * is needed by external test manipulating page permissions, so mark
11*02241649SJo Van Bulck  * encl_buffer as "used" to make sure it is entirely preserved by the compiler.
12abc5cec4SReinette Chatre  */
13*02241649SJo Van Bulck static uint8_t __used __section(".data.encl_buffer") encl_buffer[8192] = { 1 };
1422118ce1SJarkko Sakkinen 
1520404a80SReinette Chatre enum sgx_enclu_function {
1620404a80SReinette Chatre 	EACCEPT = 0x5,
1720404a80SReinette Chatre 	EMODPE = 0x6,
1820404a80SReinette Chatre };
1920404a80SReinette Chatre 
do_encl_emodpe(void * _op)2020404a80SReinette Chatre static void do_encl_emodpe(void *_op)
2120404a80SReinette Chatre {
2220404a80SReinette Chatre 	struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0};
2320404a80SReinette Chatre 	struct encl_op_emodpe *op = _op;
2420404a80SReinette Chatre 
2520404a80SReinette Chatre 	secinfo.flags = op->flags;
2620404a80SReinette Chatre 
2720404a80SReinette Chatre 	asm volatile(".byte 0x0f, 0x01, 0xd7"
28853a57a4SJo Van Bulck 				: /* no outputs */
2920404a80SReinette Chatre 				: "a" (EMODPE),
3020404a80SReinette Chatre 				  "b" (&secinfo),
31853a57a4SJo Van Bulck 				  "c" (op->epc_addr)
32853a57a4SJo Van Bulck 				: "memory" /* read from secinfo pointer */);
3320404a80SReinette Chatre }
3420404a80SReinette Chatre 
do_encl_eaccept(void * _op)3520404a80SReinette Chatre static void do_encl_eaccept(void *_op)
3620404a80SReinette Chatre {
3720404a80SReinette Chatre 	struct sgx_secinfo secinfo __aligned(sizeof(struct sgx_secinfo)) = {0};
3820404a80SReinette Chatre 	struct encl_op_eaccept *op = _op;
3920404a80SReinette Chatre 	int rax;
4020404a80SReinette Chatre 
4120404a80SReinette Chatre 	secinfo.flags = op->flags;
4220404a80SReinette Chatre 
4320404a80SReinette Chatre 	asm volatile(".byte 0x0f, 0x01, 0xd7"
4420404a80SReinette Chatre 				: "=a" (rax)
4520404a80SReinette Chatre 				: "a" (EACCEPT),
4620404a80SReinette Chatre 				  "b" (&secinfo),
47853a57a4SJo Van Bulck 				  "c" (op->epc_addr)
48853a57a4SJo Van Bulck 				: "memory" /* read from secinfo pointer */);
4920404a80SReinette Chatre 
5020404a80SReinette Chatre 	op->ret = rax;
5120404a80SReinette Chatre }
5220404a80SReinette Chatre 
memcpy(void * dest,const void * src,size_t n)532adcba79SJarkko Sakkinen static void *memcpy(void *dest, const void *src, size_t n)
542adcba79SJarkko Sakkinen {
552adcba79SJarkko Sakkinen 	size_t i;
562adcba79SJarkko Sakkinen 
572adcba79SJarkko Sakkinen 	for (i = 0; i < n; i++)
582adcba79SJarkko Sakkinen 		((char *)dest)[i] = ((char *)src)[i];
592adcba79SJarkko Sakkinen 
602adcba79SJarkko Sakkinen 	return dest;
612adcba79SJarkko Sakkinen }
622adcba79SJarkko Sakkinen 
memset(void * dest,int c,size_t n)63b564982fSReinette Chatre static void *memset(void *dest, int c, size_t n)
64b564982fSReinette Chatre {
65b564982fSReinette Chatre 	size_t i;
66b564982fSReinette Chatre 
67b564982fSReinette Chatre 	for (i = 0; i < n; i++)
68b564982fSReinette Chatre 		((char *)dest)[i] = c;
69b564982fSReinette Chatre 
70b564982fSReinette Chatre 	return dest;
71b564982fSReinette Chatre }
72b564982fSReinette Chatre 
do_encl_init_tcs_page(void * _op)73b564982fSReinette Chatre static void do_encl_init_tcs_page(void *_op)
74b564982fSReinette Chatre {
75b564982fSReinette Chatre 	struct encl_op_init_tcs_page *op = _op;
76b564982fSReinette Chatre 	void *tcs = (void *)op->tcs_page;
77b564982fSReinette Chatre 	uint32_t val_32;
78b564982fSReinette Chatre 
79b564982fSReinette Chatre 	memset(tcs, 0, 16);			/* STATE and FLAGS */
80b564982fSReinette Chatre 	memcpy(tcs + 16, &op->ssa, 8);		/* OSSA */
81b564982fSReinette Chatre 	memset(tcs + 24, 0, 4);			/* CSSA */
82b564982fSReinette Chatre 	val_32 = 1;
83b564982fSReinette Chatre 	memcpy(tcs + 28, &val_32, 4);		/* NSSA */
84b564982fSReinette Chatre 	memcpy(tcs + 32, &op->entry, 8);	/* OENTRY */
85b564982fSReinette Chatre 	memset(tcs + 40, 0, 24);		/* AEP, OFSBASE, OGSBASE */
86b564982fSReinette Chatre 	val_32 = 0xFFFFFFFF;
87b564982fSReinette Chatre 	memcpy(tcs + 64, &val_32, 4);		/* FSLIMIT */
88b564982fSReinette Chatre 	memcpy(tcs + 68, &val_32, 4);		/* GSLIMIT */
89b564982fSReinette Chatre 	memset(tcs + 72, 0, 4024);		/* Reserved */
90b564982fSReinette Chatre }
91b564982fSReinette Chatre 
do_encl_op_put_to_buf(void * op)92c085dfc7SReinette Chatre static void do_encl_op_put_to_buf(void *op)
9341493a09SJarkko Sakkinen {
94c085dfc7SReinette Chatre 	struct encl_op_put_to_buf *op2 = op;
9541493a09SJarkko Sakkinen 
9641493a09SJarkko Sakkinen 	memcpy(&encl_buffer[0], &op2->value, 8);
9741493a09SJarkko Sakkinen }
9841493a09SJarkko Sakkinen 
do_encl_op_get_from_buf(void * op)99c085dfc7SReinette Chatre static void do_encl_op_get_from_buf(void *op)
10041493a09SJarkko Sakkinen {
101c085dfc7SReinette Chatre 	struct encl_op_get_from_buf *op2 = op;
10241493a09SJarkko Sakkinen 
10341493a09SJarkko Sakkinen 	memcpy(&op2->value, &encl_buffer[0], 8);
10441493a09SJarkko Sakkinen }
10541493a09SJarkko Sakkinen 
do_encl_op_put_to_addr(void * _op)106abc5cec4SReinette Chatre static void do_encl_op_put_to_addr(void *_op)
107abc5cec4SReinette Chatre {
108abc5cec4SReinette Chatre 	struct encl_op_put_to_addr *op = _op;
109abc5cec4SReinette Chatre 
110abc5cec4SReinette Chatre 	memcpy((void *)op->addr, &op->value, 8);
111abc5cec4SReinette Chatre }
112abc5cec4SReinette Chatre 
do_encl_op_get_from_addr(void * _op)113abc5cec4SReinette Chatre static void do_encl_op_get_from_addr(void *_op)
114abc5cec4SReinette Chatre {
115abc5cec4SReinette Chatre 	struct encl_op_get_from_addr *op = _op;
116abc5cec4SReinette Chatre 
117abc5cec4SReinette Chatre 	memcpy(&op->value, (void *)op->addr, 8);
118abc5cec4SReinette Chatre }
119abc5cec4SReinette Chatre 
do_encl_op_nop(void * _op)120688542e2SReinette Chatre static void do_encl_op_nop(void *_op)
121688542e2SReinette Chatre {
122688542e2SReinette Chatre 
123688542e2SReinette Chatre }
124688542e2SReinette Chatre 
125d06978e8SJo Van Bulck /*
126d06978e8SJo Van Bulck  * Symbol placed at the start of the enclave image by the linker script.
127d06978e8SJo Van Bulck  * Declare this extern symbol with visibility "hidden" to ensure the compiler
128d06978e8SJo Van Bulck  * does not access it through the GOT and generates position-independent
129d06978e8SJo Van Bulck  * addressing as __encl_base(%rip), so we can get the actual enclave base
130d06978e8SJo Van Bulck  * during runtime.
131d06978e8SJo Van Bulck  */
132d06978e8SJo Van Bulck extern const uint8_t __attribute__((visibility("hidden"))) __encl_base;
133d06978e8SJo Van Bulck 
134d06978e8SJo Van Bulck typedef void (*encl_op_t)(void *);
135d06978e8SJo Van Bulck static const encl_op_t encl_op_array[ENCL_OP_MAX] = {
136c085dfc7SReinette Chatre 	do_encl_op_put_to_buf,
137c085dfc7SReinette Chatre 	do_encl_op_get_from_buf,
138abc5cec4SReinette Chatre 	do_encl_op_put_to_addr,
139abc5cec4SReinette Chatre 	do_encl_op_get_from_addr,
140688542e2SReinette Chatre 	do_encl_op_nop,
14120404a80SReinette Chatre 	do_encl_eaccept,
14220404a80SReinette Chatre 	do_encl_emodpe,
143b564982fSReinette Chatre 	do_encl_init_tcs_page,
14441493a09SJarkko Sakkinen };
14522118ce1SJarkko Sakkinen 
encl_body(void * rdi,void * rsi)146d06978e8SJo Van Bulck void encl_body(void *rdi,  void *rsi)
147d06978e8SJo Van Bulck {
148d06978e8SJo Van Bulck 	struct encl_op_header *header = (struct encl_op_header *)rdi;
149d06978e8SJo Van Bulck 	encl_op_t op;
15022118ce1SJarkko Sakkinen 
151d06978e8SJo Van Bulck 	if (header->type >= ENCL_OP_MAX)
152d06978e8SJo Van Bulck 		return;
153d06978e8SJo Van Bulck 
154d06978e8SJo Van Bulck 	/*
155d06978e8SJo Van Bulck 	 * The enclave base address needs to be added, as this call site
156d06978e8SJo Van Bulck 	 * *cannot be* made rip-relative by the compiler, or fixed up by
157d06978e8SJo Van Bulck 	 * any other possible means.
158d06978e8SJo Van Bulck 	 */
159d06978e8SJo Van Bulck 	op = ((uint64_t)&__encl_base) + encl_op_array[header->type];
160d06978e8SJo Van Bulck 
161d06978e8SJo Van Bulck 	(*op)(header);
1622adcba79SJarkko Sakkinen }
163