# Design and Implementation of Virtual Security Function Based on Multiple Enclaves
## 3 Background  
### Sgx  
SGX (Intel(r) Software Guard Extensions, [5; 20]) is a new CPU-based trusted execution environment technology with the goal of implementing advanced protection for confidentiality and integrity. The overall architecture of SGX is shown in Figure 1. It allocates the hardware-protected memory for an application code and data in which an isolated enclave runs [21]. The data in the enclave memory space can only be accessed by code that is also located in the enclave memory space. The privileged software such as the virtual machine monitor (VMM), BIOS, and even the operating system outside the enclave cannot directly access sensitive data and code inside the enclave. SGX has certain hardware limitations on the size of the protected memory, usually up to 128 MB (the available memory for user data only is \(\approx\)93 M) [22]. Therefore, the number of enclaves running in protected memory is also limited. The use case shows that there are typically 5-20 enclaves that reside in memory, at the same time depending on the memory usage of each enclave. Enclave can be called with special instructions. The SGX architecture includes 17 new instructions, new processor architecture, and new execution mode. SGX loads the enclave into the protected memory, accesses resources through page table mapping, and executes the enclave application. The system software still maintains control of the enclave's access to resources. The entire application can be packaged into an enclave, or it can be decomposed into multiple small components, and only the critical security components are placed in the enclave. Due to the limitation of SGX memory size, the latter is our commonly used enclave application development method. EPC. In order to achieve memory protection, SGX requires new hardware and structure. The enclave pages and SGX structures are stored in a protected memory called the enclave page cache (EPC). EPC is protected by both hardware and software and is divided into many EPC pages, each of which is a 4 KB block. Inside the EPC, there are many different enclaves running when an enclave accesses memory in the EPC; whether to allow access is determined by the CPU processor. The access control information for each page in the EPC is maintained in a hardware structure called the enclave page cache map (EPCM) in the processor. Enclave created. The enclave binary is loaded into the EPC and an identity for the enclave binary is established in the creation process. The creation of the enclave includes the following steps: initialize the enclave control structure, allocate EPC pages and load the contents of the enclave into the pages, measure enclave content, and eventually establish the enclave identity. Enclave exit and entry. The most important thing to maintain the confidentiality and integrity of the enclave is to control the data into and out of the enclave. The entry process needs to clear all caches transfers associated with the enclave address area. All memory accesses in enclaves need to be properly checked. The entry process must confirm that the processor is inside the enclave, and the processor should transfer control and enable the enclave execution mode. Before exiting the security zone, all cache translations in the protected area must be cleared, so that other software cannot use the cache translation to access the protected memory area of the enclave. Interrupts, faults, or anomalies may occur when operating in enclave mode. The processor can pass the specific fault handling program through the system software traditionally. The fault handler saves the register states, and once the event is processed, the system software will restore the register state and return control to the point of interruption. SGX places the system software within the enclave's trust boundary to allow the system software to read and modify the enclave's registration status. Therefore, a new routine is introduced in SGX to protect the integrity and confidentiality of the enclave.