2018-03-27 17:26:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Intel Corporation. All rights reserved.
|
|
|
|
*
|
2018-05-26 01:49:13 +08:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2018-03-27 17:26:38 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <hypervisor.h>
|
|
|
|
#include <hypercall.h>
|
|
|
|
|
2018-08-03 09:54:58 +08:00
|
|
|
#define ACRN_DBG_TRUSTY_HYCALL 6U
|
|
|
|
|
2018-06-16 14:46:51 +08:00
|
|
|
/* this hcall is only come from trusty enabled vcpu itself, and cannot be
|
|
|
|
* called from other vcpus
|
|
|
|
*/
|
2018-07-19 23:17:38 +08:00
|
|
|
int32_t hcall_world_switch(struct vcpu *vcpu)
|
2018-03-27 17:26:38 +08:00
|
|
|
{
|
2018-06-28 16:27:12 +08:00
|
|
|
int32_t next_world_id = !(vcpu->arch_vcpu.cur_context);
|
2018-03-27 17:26:38 +08:00
|
|
|
|
2018-05-29 15:47:23 +08:00
|
|
|
if (next_world_id >= NR_WORLD) {
|
2018-08-03 09:54:58 +08:00
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"%s world_id %d exceed max number of Worlds\n",
|
2018-05-29 15:47:23 +08:00
|
|
|
__func__, next_world_id);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-05-25 09:30:37 +08:00
|
|
|
if (!vcpu->vm->sworld_control.flag.supported) {
|
2018-08-03 09:54:58 +08:00
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"Secure World is not supported!\n");
|
2018-05-29 15:47:23 +08:00
|
|
|
return -EPERM;
|
2018-03-27 17:26:38 +08:00
|
|
|
}
|
|
|
|
|
2018-05-25 09:30:37 +08:00
|
|
|
if (!vcpu->vm->sworld_control.flag.active) {
|
2018-08-03 09:54:58 +08:00
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"Trusty is not initialized!\n");
|
2018-05-29 15:47:23 +08:00
|
|
|
return -EPERM;
|
2018-03-27 17:26:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_world(vcpu, next_world_id);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-27 17:27:51 +08:00
|
|
|
|
2018-06-16 14:46:51 +08:00
|
|
|
/* this hcall is only come from trusty enabled vcpu itself, and cannot be
|
|
|
|
* called from other vcpus
|
|
|
|
*/
|
2018-07-19 23:17:38 +08:00
|
|
|
int32_t hcall_initialize_trusty(struct vcpu *vcpu, uint64_t param)
|
2018-03-27 17:27:51 +08:00
|
|
|
{
|
2018-05-25 09:30:37 +08:00
|
|
|
if (!vcpu->vm->sworld_control.flag.supported) {
|
2018-08-03 09:54:58 +08:00
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"Secure World is not supported!\n");
|
2018-05-29 15:47:23 +08:00
|
|
|
return -EPERM;
|
2018-03-27 17:27:51 +08:00
|
|
|
}
|
|
|
|
|
2018-05-25 09:30:37 +08:00
|
|
|
if (vcpu->vm->sworld_control.flag.active) {
|
2018-08-03 09:54:58 +08:00
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"Trusty already initialized!\n");
|
2018-05-29 15:47:23 +08:00
|
|
|
return -EPERM;
|
2018-03-27 17:27:51 +08:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:47:23 +08:00
|
|
|
if (vcpu->arch_vcpu.cur_context != NORMAL_WORLD) {
|
2018-08-03 09:54:58 +08:00
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"%s, must initialize Trusty from Normal World!\n",
|
2018-05-29 15:47:23 +08:00
|
|
|
__func__);
|
|
|
|
return -EPERM;
|
2018-03-27 17:27:51 +08:00
|
|
|
}
|
|
|
|
|
2018-07-13 06:02:55 +08:00
|
|
|
if (!initialize_trusty(vcpu, param)) {
|
2018-05-29 15:47:23 +08:00
|
|
|
return -ENODEV;
|
2018-07-13 06:02:55 +08:00
|
|
|
}
|
2018-05-29 15:47:23 +08:00
|
|
|
|
2018-05-25 09:30:37 +08:00
|
|
|
vcpu->vm->sworld_control.flag.active = 1UL;
|
|
|
|
|
2018-03-27 17:27:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-25 13:08:04 +08:00
|
|
|
|
|
|
|
int64_t hcall_save_restore_sworld_ctx(struct vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct vm *vm = vcpu->vm;
|
|
|
|
|
|
|
|
if (!vm->sworld_control.flag.supported) {
|
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"Secure World is not supported!\n");
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Currently, Secure World is only running on vCPU0 */
|
|
|
|
if (!is_vcpu_bsp(vcpu)) {
|
|
|
|
dev_dbg(ACRN_DBG_TRUSTY_HYCALL,
|
|
|
|
"This hypercall is only allowed from vcpu0!");
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm->sworld_control.flag.active) {
|
|
|
|
save_sworld_context(vcpu);
|
|
|
|
vm->sworld_control.flag.ctx_saved = 1UL;
|
|
|
|
} else {
|
|
|
|
if (vm->sworld_control.flag.ctx_saved) {
|
|
|
|
restore_sworld_context(vcpu);
|
|
|
|
vm->sworld_control.flag.ctx_saved = 0UL;
|
|
|
|
vm->sworld_control.flag.active = 1UL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|