Skip to content

Commit 1fd07ba

Browse files
YadongQijren1
authored andcommitted
trusty: Simulate Secure Monitor Call(SMC) by Hypercall
For ARM, The SMC instruction is used to generate a synchronous exception that is handled by Secure Monitor code running in EL3. In the ARM architecture, synchronous control is transferred between the normal Non-secure state and the Secure state through Secure Monitor Call exceptions. SMC exceptions are generated by the SMC instruction, and handled by the Secure Monitor.The operation of the Secure Monitor is determined by the parameters that are passed in through registers. For ACRN, Hypervisor will simulate SMC by hypercall to switch vCPU State between Normal World and Secure World. There are 4 registers(RDI, RSI, RDX, RBX) reserved for paramters passing between Normal World and Secure World. Signed-off-by: Qi Yadong <yadong.qi@intel.com>
1 parent 0d9d628 commit 1fd07ba

File tree

8 files changed

+235
-0
lines changed

8 files changed

+235
-0
lines changed

hypervisor/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ C_SRCS += lib/crypto/hkdf.c
134134
C_SRCS += lib/sprintf.c
135135
C_SRCS += common/hv_main.c
136136
C_SRCS += common/hypercall.c
137+
C_SRCS += common/trusty_hypercall.c
137138
C_SRCS += common/schedule.c
138139
C_SRCS += common/vm_load.c
139140

hypervisor/arch/x86/guest/vmcall.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ int vmcall_handler(struct vcpu *vcpu)
134134
ret = hcall_setup_sbuf(vm, param1);
135135
break;
136136

137+
case HC_WORLD_SWITCH:
138+
ret = hcall_world_switch(vcpu);
139+
break;
140+
137141
default:
138142
pr_err("op %d: Invalid hypercall\n", hypcall_id);
139143
ret = -1;

hypervisor/arch/x86/trusty.c

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131
#include <hv_lib.h>
3232
#include <acrn_common.h>
3333
#include <hv_arch.h>
34+
#include <acrn_hv_defs.h>
35+
#include <hv_debug.h>
36+
37+
_Static_assert(NR_WORLD == 2, "Only 2 Worlds supported!");
38+
39+
/* Trusty EPT rebase gpa: 511G */
40+
#define TRUSTY_EPT_REBASE_GPA (511ULL*1024ULL*1024ULL*1024ULL)
41+
42+
#define save_segment(seg, SEG_NAME) \
43+
{ \
44+
seg.selector = exec_vmread(VMX_GUEST_##SEG_NAME##_SEL); \
45+
seg.base = exec_vmread(VMX_GUEST_##SEG_NAME##_BASE); \
46+
seg.limit = exec_vmread(VMX_GUEST_##SEG_NAME##_LIMIT); \
47+
seg.attr = exec_vmread(VMX_GUEST_##SEG_NAME##_ATTR); \
48+
}
49+
50+
#define load_segment(seg, SEG_NAME) \
51+
{ \
52+
exec_vmwrite(VMX_GUEST_##SEG_NAME##_SEL, seg.selector); \
53+
exec_vmwrite(VMX_GUEST_##SEG_NAME##_BASE, seg.base); \
54+
exec_vmwrite(VMX_GUEST_##SEG_NAME##_LIMIT, seg.limit); \
55+
exec_vmwrite(VMX_GUEST_##SEG_NAME##_ATTR, seg.attr); \
56+
}
3457

3558
void create_secure_world_ept(struct vm *vm, uint64_t gpa,
3659
uint64_t size, uint64_t rebased_gpa)
@@ -97,3 +120,122 @@ void create_secure_world_ept(struct vm *vm, uint64_t gpa,
97120

98121
}
99122

123+
static void save_world_ctx(struct run_context *context)
124+
{
125+
/* VMCS Execution field */
126+
context->tsc_offset = exec_vmread64(VMX_TSC_OFFSET_FULL);
127+
128+
/* VMCS GUEST field */
129+
/* CR3, RIP, RSP, RFLAGS already saved on VMEXIT */
130+
context->cr0 = exec_vmread(VMX_GUEST_CR0);
131+
context->cr4 = exec_vmread(VMX_GUEST_CR4);
132+
context->dr7 = exec_vmread(VMX_GUEST_DR7);
133+
context->ia32_debugctl = exec_vmread64(VMX_GUEST_IA32_DEBUGCTL_FULL);
134+
context->ia32_pat = exec_vmread64(VMX_GUEST_IA32_PAT_FULL);
135+
context->ia32_efer = exec_vmread64(VMX_GUEST_IA32_EFER_FULL);
136+
context->ia32_sysenter_cs = exec_vmread(VMX_GUEST_IA32_SYSENTER_CS);
137+
context->ia32_sysenter_esp = exec_vmread(VMX_GUEST_IA32_SYSENTER_ESP);
138+
context->ia32_sysenter_eip = exec_vmread(VMX_GUEST_IA32_SYSENTER_EIP);
139+
save_segment(context->cs, CS);
140+
save_segment(context->ss, SS);
141+
save_segment(context->ds, DS);
142+
save_segment(context->es, ES);
143+
save_segment(context->fs, FS);
144+
save_segment(context->gs, GS);
145+
save_segment(context->tr, TR);
146+
save_segment(context->ldtr, LDTR);
147+
/* Only base and limit for IDTR and GDTR */
148+
context->idtr.base = exec_vmread(VMX_GUEST_IDTR_BASE);
149+
context->idtr.limit = exec_vmread(VMX_GUEST_IDTR_LIMIT);
150+
context->gdtr.base = exec_vmread(VMX_GUEST_GDTR_BASE);
151+
context->gdtr.limit = exec_vmread(VMX_GUEST_GDTR_LIMIT);
152+
153+
/* MSRs which not in the VMCS */
154+
context->ia32_star = msr_read(MSR_IA32_STAR);
155+
context->ia32_lstar = msr_read(MSR_IA32_LSTAR);
156+
context->ia32_fmask = msr_read(MSR_IA32_FMASK);
157+
context->ia32_kernel_gs_base = msr_read(MSR_IA32_KERNEL_GS_BASE);
158+
159+
/* FX area */
160+
asm volatile("fxsave (%0)"
161+
: : "r" (context->fxstore_guest_area) : "memory");
162+
}
163+
164+
static void load_world_ctx(struct run_context *context)
165+
{
166+
/* VMCS Execution field */
167+
exec_vmwrite64(VMX_TSC_OFFSET_FULL, context->tsc_offset);
168+
169+
/* VMCS GUEST field */
170+
exec_vmwrite(VMX_GUEST_CR0, context->cr0);
171+
exec_vmwrite(VMX_GUEST_CR3, context->cr3);
172+
exec_vmwrite(VMX_GUEST_CR4, context->cr4);
173+
exec_vmwrite(VMX_GUEST_RIP, context->rip);
174+
exec_vmwrite(VMX_GUEST_RSP, context->rsp);
175+
exec_vmwrite(VMX_GUEST_RFLAGS, context->rflags);
176+
exec_vmwrite(VMX_GUEST_DR7, context->dr7);
177+
exec_vmwrite64(VMX_GUEST_IA32_DEBUGCTL_FULL, context->ia32_debugctl);
178+
exec_vmwrite64(VMX_GUEST_IA32_PAT_FULL, context->ia32_pat);
179+
exec_vmwrite64(VMX_GUEST_IA32_EFER_FULL, context->ia32_efer);
180+
exec_vmwrite(VMX_GUEST_IA32_SYSENTER_CS, context->ia32_sysenter_cs);
181+
exec_vmwrite(VMX_GUEST_IA32_SYSENTER_ESP, context->ia32_sysenter_esp);
182+
exec_vmwrite(VMX_GUEST_IA32_SYSENTER_EIP, context->ia32_sysenter_eip);
183+
load_segment(context->cs, CS);
184+
load_segment(context->ss, SS);
185+
load_segment(context->ds, DS);
186+
load_segment(context->es, ES);
187+
load_segment(context->fs, FS);
188+
load_segment(context->gs, GS);
189+
load_segment(context->tr, TR);
190+
load_segment(context->ldtr, LDTR);
191+
/* Only base and limit for IDTR and GDTR */
192+
exec_vmwrite(VMX_GUEST_IDTR_BASE, context->idtr.base);
193+
exec_vmwrite(VMX_GUEST_IDTR_LIMIT, context->idtr.limit);
194+
exec_vmwrite(VMX_GUEST_GDTR_BASE, context->gdtr.base);
195+
exec_vmwrite(VMX_GUEST_GDTR_LIMIT, context->gdtr.limit);
196+
197+
/* MSRs which not in the VMCS */
198+
msr_write(MSR_IA32_STAR, context->ia32_star);
199+
msr_write(MSR_IA32_LSTAR, context->ia32_lstar);
200+
msr_write(MSR_IA32_FMASK, context->ia32_fmask);
201+
msr_write(MSR_IA32_KERNEL_GS_BASE, context->ia32_kernel_gs_base);
202+
203+
/* FX area */
204+
asm volatile("fxrstor (%0)" : : "r" (context->fxstore_guest_area));
205+
}
206+
207+
static void copy_smc_param(struct run_context *prev_ctx,
208+
struct run_context *next_ctx)
209+
{
210+
next_ctx->guest_cpu_regs.regs.rdi = prev_ctx->guest_cpu_regs.regs.rdi;
211+
next_ctx->guest_cpu_regs.regs.rsi = prev_ctx->guest_cpu_regs.regs.rsi;
212+
next_ctx->guest_cpu_regs.regs.rdx = prev_ctx->guest_cpu_regs.regs.rdx;
213+
next_ctx->guest_cpu_regs.regs.rbx = prev_ctx->guest_cpu_regs.regs.rbx;
214+
}
215+
216+
void switch_world(struct vcpu *vcpu, int next_world)
217+
{
218+
struct vcpu_arch *arch_vcpu = &vcpu->arch_vcpu;
219+
220+
/* save previous world context */
221+
save_world_ctx(&arch_vcpu->contexts[!next_world]);
222+
223+
/* load next world context */
224+
load_world_ctx(&arch_vcpu->contexts[next_world]);
225+
226+
/* Copy SMC parameters: RDI, RSI, RDX, RBX */
227+
copy_smc_param(&arch_vcpu->contexts[!next_world],
228+
&arch_vcpu->contexts[next_world]);
229+
230+
/* load EPTP for next world */
231+
if (next_world == NORMAL_WORLD) {
232+
exec_vmwrite64(VMX_EPT_POINTER_FULL,
233+
((uint64_t)vcpu->vm->arch_vm.nworld_eptp) | (3<<3) | 6);
234+
} else {
235+
exec_vmwrite64(VMX_EPT_POINTER_FULL,
236+
((uint64_t)vcpu->vm->arch_vm.sworld_eptp) | (3<<3) | 6);
237+
}
238+
239+
/* Update world index */
240+
arch_vcpu->cur_context = next_world;
241+
}

hypervisor/common/hypercall.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040

4141
#define ACRN_DBG_HYCALL 6
4242

43+
bool is_hypercall_from_ring0(void)
44+
{
45+
uint64_t cs_sel;
46+
47+
cs_sel = exec_vmread(VMX_GUEST_CS_SEL);
48+
/* cs_selector[1:0] is CPL */
49+
if ((cs_sel & 0x3) == 0)
50+
return true;
51+
52+
return false;
53+
}
54+
4355
int64_t hcall_get_api_version(struct vm *vm, uint64_t param)
4456
{
4557
struct hc_api_version version;

hypervisor/common/trusty_hypercall.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Intel Corporation nor the names of its
15+
* contributors may be used to endorse or promote products derived
16+
* from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <hypervisor.h>
32+
#include <hv_lib.h>
33+
#include <acrn_common.h>
34+
#include <hv_arch.h>
35+
#include <hypercall.h>
36+
#include <acrn_hv_defs.h>
37+
#include <hv_debug.h>
38+
39+
int64_t hcall_world_switch(struct vcpu *vcpu)
40+
{
41+
int next_world_id = !(vcpu->arch_vcpu.cur_context);
42+
43+
if (!is_hypercall_from_ring0()) {
44+
pr_err("%s() is only allowed from RING-0!\n", __func__);
45+
return -1;
46+
}
47+
48+
if (!vcpu->vm->sworld_control.sworld_enabled) {
49+
pr_err("Secure World is not enabled!\n");
50+
return -1;
51+
}
52+
53+
if (!vcpu->vm->arch_vm.sworld_eptp) {
54+
pr_err("Trusty is not launched!\n");
55+
return -1;
56+
}
57+
58+
ASSERT(next_world_id < NR_WORLD,
59+
"world_id exceed max number of Worlds");
60+
61+
switch_world(vcpu, next_world_id);
62+
return 0;
63+
}

hypervisor/include/arch/x86/trusty.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,7 @@ struct secure_world_control {
128128
struct secure_world_memory sworld_memory;
129129
};
130130

131+
void switch_world(struct vcpu *vcpu, int next_world);
132+
131133
#endif /* TRUSTY_H_ */
132134

hypervisor/include/common/hypercall.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
struct vhm_request;
4141

42+
bool is_hypercall_from_ring0(void);
4243
int acrn_insert_request_wait(struct vcpu *vcpu, struct vhm_request *req);
4344
int acrn_insert_request_nowait(struct vcpu *vcpu, struct vhm_request *req);
4445
int get_req_info(char *str, int str_max);
@@ -325,6 +326,15 @@ int64_t hcall_reset_ptdev_intr_info(struct vm *vm, uint64_t vmid,
325326
*/
326327
int64_t hcall_setup_sbuf(struct vm *vm, uint64_t param);
327328

329+
/**
330+
* @brief Switch VCPU state between Normal/Secure World.
331+
*
332+
* @param VCPU Pointer to VCPU data structure
333+
*
334+
* @return 0 on success, non-zero on error.
335+
*/
336+
int64_t hcall_world_switch(struct vcpu *vcpu);
337+
328338
/**
329339
* @}
330340
*/

hypervisor/include/public/acrn_hv_defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,5 @@ struct hc_api_version {
229229
/**
230230
* @}
231231
*/
232+
232233
#endif /* ACRN_HV_DEFS_H */

0 commit comments

Comments
 (0)