acrn-hypervisor/hypervisor/common/event.c
Wu Zhou 064be1e3e6 hv: support halt in hv idle
When all vCPU threads on one pCPU are put to sleep (e.g., when all
guests execute HLT), hv would schedule to idle thread. Currently the
idle thread executes PAUSE which does not enter any c-state and consumes
a lot of power. This patch is to support HLT in the idle thread.

When we switch to HLT, we have to make sure events that would wake a
vCPU must also be able to wake the pCPU. Those events are either
generated by local interrupt or issued by other pCPUs followed by an
ipi kick.

Each of them have an interrupt involved, so they are also able to wake
the halted pCPU. Except when the pCPU has just scheduled to idle thread
but not yet halted, interrupts could be missed.

sleep-------schedule to idle------IRQ ON---HLT--(kick missed)
                                         ^
                              wake---kick|

This areas should be protected. This is done by a safe halt
mechanism leveraging STI instruction’s delay effect (same as Linux).

vCPUs with lapic_pt or hv with CONFIG_KEEP_IRQ_DISABLED=y does not allow
interrupts in root mode, so they could never wake from HLT (INIT kick
does not wake HLT in root mode either). They should continue using PAUSE
in idle.

Tracked-On: #8507
Signed-off-by: Wu Zhou <wu.zhou@intel.com>
Reviewed-by: Junjie Mao <junjie.mao@intel.com>
2023-09-15 11:52:40 +08:00

64 lines
1.7 KiB
C

/*
* Copyright (C) 2018-2022 Intel Corporation.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <asm/cpu.h>
#include <schedule.h>
#include <event.h>
#include <logmsg.h>
void init_event(struct sched_event *event)
{
spinlock_init(&event->lock);
event->set = false;
event->waiting_thread = NULL;
}
void reset_event(struct sched_event *event)
{
uint64_t rflag;
spinlock_irqsave_obtain(&event->lock, &rflag);
event->set = false;
event->waiting_thread = NULL;
spinlock_irqrestore_release(&event->lock, rflag);
}
/* support exclusive waiting only
*
* During wait, the pCPU could be scheduled to run the idle thread when run queue
* is empty. Signal_event() can happen when schedule() is in process.
* This signal_event is not going to be lost, for the idle thread will always
* check need_reschedule() after it is switched to at schedule().
*/
void wait_event(struct sched_event *event)
{
uint64_t rflag;
spinlock_irqsave_obtain(&event->lock, &rflag);
ASSERT((event->waiting_thread == NULL), "only support exclusive waiting");
event->waiting_thread = sched_get_current(get_pcpu_id());
while (!event->set && (event->waiting_thread != NULL)) {
sleep_thread(event->waiting_thread);
spinlock_irqrestore_release(&event->lock, rflag);
schedule();
spinlock_irqsave_obtain(&event->lock, &rflag);
}
event->set = false;
event->waiting_thread = NULL;
spinlock_irqrestore_release(&event->lock, rflag);
}
void signal_event(struct sched_event *event)
{
uint64_t rflag;
spinlock_irqsave_obtain(&event->lock, &rflag);
event->set = true;
if (event->waiting_thread != NULL) {
wake_thread(event->waiting_thread);
}
spinlock_irqrestore_release(&event->lock, rflag);
}