1 // SPDX-License-Identifier: GPL-2.0
2 /*  Copyright(c) 2016-20 Intel Corporation. */
3 
4 #include <stddef.h>
5 #include "defines.h"
6 
7 /*
8  * Data buffer spanning two pages that will be placed first in .data
9  * segment. Even if not used internally the second page is needed by
10  * external test manipulating page permissions.
11  */
12 static uint8_t encl_buffer[8192] = { 1 };
13 
14 static void *memcpy(void *dest, const void *src, size_t n)
15 {
16 	size_t i;
17 
18 	for (i = 0; i < n; i++)
19 		((char *)dest)[i] = ((char *)src)[i];
20 
21 	return dest;
22 }
23 
24 static void do_encl_op_put_to_buf(void *op)
25 {
26 	struct encl_op_put_to_buf *op2 = op;
27 
28 	memcpy(&encl_buffer[0], &op2->value, 8);
29 }
30 
31 static void do_encl_op_get_from_buf(void *op)
32 {
33 	struct encl_op_get_from_buf *op2 = op;
34 
35 	memcpy(&op2->value, &encl_buffer[0], 8);
36 }
37 
38 static void do_encl_op_put_to_addr(void *_op)
39 {
40 	struct encl_op_put_to_addr *op = _op;
41 
42 	memcpy((void *)op->addr, &op->value, 8);
43 }
44 
45 static void do_encl_op_get_from_addr(void *_op)
46 {
47 	struct encl_op_get_from_addr *op = _op;
48 
49 	memcpy(&op->value, (void *)op->addr, 8);
50 }
51 
52 void encl_body(void *rdi,  void *rsi)
53 {
54 	const void (*encl_op_array[ENCL_OP_MAX])(void *) = {
55 		do_encl_op_put_to_buf,
56 		do_encl_op_get_from_buf,
57 		do_encl_op_put_to_addr,
58 		do_encl_op_get_from_addr,
59 	};
60 
61 	struct encl_op_header *op = (struct encl_op_header *)rdi;
62 
63 	if (op->type < ENCL_OP_MAX)
64 		(*encl_op_array[op->type])(op);
65 }
66