2018-03-07 20:57:14 +08:00
|
|
|
/*
|
2022-04-05 18:59:24 +08:00
|
|
|
* Copyright (C) 2018 Intel Corporation.
|
2018-03-07 20:57:14 +08:00
|
|
|
*
|
2018-05-26 01:49:13 +08:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2018-03-07 20:57:14 +08:00
|
|
|
*/
|
|
|
|
|
2021-04-23 15:50:57 +08:00
|
|
|
#include <asm/guest/vm.h>
|
|
|
|
#include <asm/guest/vm_reset.h>
|
|
|
|
#include <asm/guest/vmcs.h>
|
|
|
|
#include <asm/guest/vmexit.h>
|
|
|
|
#include <asm/guest/virq.h>
|
2018-03-07 20:57:14 +08:00
|
|
|
#include <schedule.h>
|
2019-02-26 15:42:56 +08:00
|
|
|
#include <profiling.h>
|
2019-10-16 19:16:37 +08:00
|
|
|
#include <sprintf.h>
|
2019-02-26 15:42:56 +08:00
|
|
|
#include <trace.h>
|
|
|
|
#include <logmsg.h>
|
2018-03-07 20:57:14 +08:00
|
|
|
|
2019-05-23 16:17:28 +08:00
|
|
|
void vcpu_thread(struct thread_object *obj)
|
2018-03-07 20:57:14 +08:00
|
|
|
{
|
2020-03-30 10:57:54 +08:00
|
|
|
struct acrn_vcpu *vcpu = container_of(obj, struct acrn_vcpu, thread_obj);
|
2018-06-28 16:27:12 +08:00
|
|
|
int32_t ret = 0;
|
2018-03-07 20:57:14 +08:00
|
|
|
|
|
|
|
do {
|
2019-06-11 13:22:32 +08:00
|
|
|
if (!is_lapic_pt_enabled(vcpu)) {
|
2021-12-08 15:46:35 +08:00
|
|
|
CPU_IRQ_DISABLE_ON_CONFIG();
|
2018-11-11 14:07:33 +08:00
|
|
|
}
|
2018-03-07 20:57:14 +08:00
|
|
|
|
2019-06-24 15:28:10 +08:00
|
|
|
/* Don't open interrupt window between here and vmentry */
|
2019-10-14 16:38:58 +08:00
|
|
|
if (need_reschedule(pcpuid_from_vcpu(vcpu))) {
|
2019-06-24 15:28:10 +08:00
|
|
|
schedule();
|
|
|
|
}
|
|
|
|
|
2018-05-25 12:29:00 +08:00
|
|
|
/* Check and process pending requests(including interrupt) */
|
2018-05-28 13:00:04 +08:00
|
|
|
ret = acrn_handle_pending_request(vcpu);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_fatal("vcpu handling pending request fail");
|
2020-07-13 18:22:57 +08:00
|
|
|
get_vm_lock(vcpu->vm);
|
2020-05-19 15:59:51 +08:00
|
|
|
zombie_vcpu(vcpu, VCPU_ZOMBIE);
|
2020-07-13 18:22:57 +08:00
|
|
|
put_vm_lock(vcpu->vm);
|
2019-07-09 18:02:57 +08:00
|
|
|
/* Fatal error happened (triple fault). Stop the vcpu running. */
|
2019-10-28 12:03:21 +08:00
|
|
|
continue;
|
2018-05-28 13:00:04 +08:00
|
|
|
}
|
2018-03-07 20:57:14 +08:00
|
|
|
|
2019-12-02 17:03:23 +08:00
|
|
|
reset_event(&vcpu->events[VCPU_EVENT_VIRTUAL_INTERRUPT]);
|
2018-10-23 03:29:21 +08:00
|
|
|
profiling_vmenter_handler(vcpu);
|
|
|
|
|
2019-01-14 11:30:36 +08:00
|
|
|
TRACE_2L(TRACE_VM_ENTER, 0UL, 0UL);
|
2018-09-07 22:36:12 +08:00
|
|
|
ret = run_vcpu(vcpu);
|
2018-05-22 14:33:13 +08:00
|
|
|
if (ret != 0) {
|
|
|
|
pr_fatal("vcpu resume failed");
|
2020-07-13 18:22:57 +08:00
|
|
|
get_vm_lock(vcpu->vm);
|
2020-05-19 15:59:51 +08:00
|
|
|
zombie_vcpu(vcpu, VCPU_ZOMBIE);
|
2020-07-13 18:22:57 +08:00
|
|
|
put_vm_lock(vcpu->vm);
|
2019-07-09 18:02:57 +08:00
|
|
|
/* Fatal error happened (resume vcpu failed). Stop the vcpu running. */
|
2019-10-28 12:03:21 +08:00
|
|
|
continue;
|
2018-05-22 14:33:13 +08:00
|
|
|
}
|
2021-08-20 02:06:06 +08:00
|
|
|
TRACE_2L(TRACE_VM_EXIT, vcpu->arch.exit_reason, vcpu_get_rip(vcpu));
|
2018-03-07 20:57:14 +08:00
|
|
|
|
2018-12-13 07:12:42 +08:00
|
|
|
profiling_pre_vmexit_handler(vcpu);
|
|
|
|
|
2019-06-11 13:22:32 +08:00
|
|
|
if (!is_lapic_pt_enabled(vcpu)) {
|
2021-12-08 15:46:35 +08:00
|
|
|
CPU_IRQ_ENABLE_ON_CONFIG();
|
2018-11-11 14:07:33 +08:00
|
|
|
}
|
2018-03-07 20:57:14 +08:00
|
|
|
/* Dispatch handler */
|
2018-05-22 14:33:13 +08:00
|
|
|
ret = vmexit_handler(vcpu);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_fatal("dispatch VM exit handler failed for reason"
|
2021-08-20 02:06:06 +08:00
|
|
|
" %d, ret = %d!", vcpu->arch.exit_reason, ret);
|
2018-07-04 16:04:40 +08:00
|
|
|
vcpu_inject_gp(vcpu, 0U);
|
2018-05-22 14:33:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-07 20:57:14 +08:00
|
|
|
|
2018-12-13 07:12:42 +08:00
|
|
|
profiling_post_vmexit_handler(vcpu);
|
2018-03-07 20:57:14 +08:00
|
|
|
} while (1);
|
|
|
|
}
|
2018-12-20 15:15:20 +08:00
|
|
|
|
2019-05-23 16:17:28 +08:00
|
|
|
void default_idle(__unused struct thread_object *obj)
|
2018-12-20 15:15:20 +08:00
|
|
|
{
|
2019-04-22 05:52:12 +08:00
|
|
|
uint16_t pcpu_id = get_pcpu_id();
|
2018-12-20 15:15:20 +08:00
|
|
|
|
|
|
|
while (1) {
|
2018-12-25 14:48:17 +08:00
|
|
|
if (need_reschedule(pcpu_id)) {
|
2018-12-20 15:15:20 +08:00
|
|
|
schedule();
|
2019-07-15 17:43:49 +08:00
|
|
|
} else if (need_offline(pcpu_id)) {
|
2018-12-20 15:15:20 +08:00
|
|
|
cpu_dead();
|
2019-02-28 01:54:58 +08:00
|
|
|
} else if (need_shutdown_vm(pcpu_id)) {
|
|
|
|
shutdown_vm_from_idle(pcpu_id);
|
2018-12-20 15:15:20 +08:00
|
|
|
} else {
|
2021-12-08 15:46:35 +08:00
|
|
|
CPU_IRQ_ENABLE_ON_CONFIG();
|
2018-12-20 15:15:20 +08:00
|
|
|
cpu_do_idle();
|
2021-12-08 15:46:35 +08:00
|
|
|
CPU_IRQ_DISABLE_ON_CONFIG();
|
2018-12-20 15:15:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 19:16:37 +08:00
|
|
|
|
|
|
|
void run_idle_thread(void)
|
|
|
|
{
|
|
|
|
uint16_t pcpu_id = get_pcpu_id();
|
|
|
|
struct thread_object *idle = &per_cpu(idle, pcpu_id);
|
|
|
|
char idle_name[16];
|
|
|
|
|
|
|
|
snprintf(idle_name, 16U, "idle%hu", pcpu_id);
|
|
|
|
(void)strncpy_s(idle->name, 16U, idle_name, 16U);
|
|
|
|
idle->pcpu_id = pcpu_id;
|
|
|
|
idle->thread_entry = default_idle;
|
|
|
|
idle->switch_out = NULL;
|
|
|
|
idle->switch_in = NULL;
|
2021-09-09 16:21:30 +08:00
|
|
|
idle->priority = PRIO_IDLE;
|
2019-10-16 19:16:37 +08:00
|
|
|
|
|
|
|
run_thread(idle);
|
|
|
|
|
|
|
|
/* Control should not come here */
|
|
|
|
cpu_dead();
|
|
|
|
}
|