[v12,08/31] LoongArch: KVM: Implement vcpu handle exit interface

Message ID 20230530015223.147755-9-zhaotianrui@loongson.cn
State New
Headers
Series Add KVM LoongArch support |

Commit Message

zhaotianrui May 30, 2023, 1:52 a.m. UTC
  Implement vcpu handle exit interface, getting the exit code by ESTAT
register and using kvm exception vector to handle it.

Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 arch/loongarch/kvm/vcpu.c | 46 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
  

Comments

maobibo June 5, 2023, 1:03 p.m. UTC | #1
在 2023/5/30 09:52, Tianrui Zhao 写道:
> Implement vcpu handle exit interface, getting the exit code by ESTAT
> register and using kvm exception vector to handle it.
> 
> Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
> ---
>  arch/loongarch/kvm/vcpu.c | 46 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index eca8b96a3e6e..ddea480fa5b0 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
> @@ -55,6 +55,52 @@ static void kvm_pre_enter_guest(struct kvm_vcpu *vcpu)
>  	vcpu->arch.aux_inuse &= ~KVM_LARCH_CSR;
>  }
>  
> +/*
> + * Return 1 for resume guest and "<= 0" for resume host.
> + */
> +static int _kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
> +{
> +	unsigned long exst = vcpu->arch.host_estat;
> +	u32 intr = exst & 0x1fff; /* ignore NMI */
> +	u32 exccode = (exst & CSR_ESTAT_EXC) >> CSR_ESTAT_EXC_SHIFT;
> +	int ret = RESUME_GUEST;
> +
> +	vcpu->mode = OUTSIDE_GUEST_MODE;
> +
> +	/* Set a default exit reason */
> +	run->exit_reason = KVM_EXIT_UNKNOWN;
> +	run->ready_for_interrupt_injection = 1;
Is ready_for_interrupt_injection used in qemu or kvm for LoongArch?

> +
> +	local_irq_enable();
> +	guest_state_exit_irqoff();
> +
> +	trace_kvm_exit(vcpu, exccode);
> +	if (exccode) {
> +		ret = _kvm_handle_fault(vcpu, exccode);
> +	} else {
> +		WARN(!intr, "suspicious vm exiting");
how about comments like this?
  WARN(!intr, "vm exiting with suspicious irq \n") 

Regards
Bibo, Mao
> +		++vcpu->stat.int_exits;
> +	}
> +
> +	cond_resched();
> +	local_irq_disable();
> +
> +	if (ret == RESUME_HOST)
> +		return ret;
> +
> +	/* Only check for signals if not already exiting to userspace */
> +	if (signal_pending(current)) {
> +		vcpu->run->exit_reason = KVM_EXIT_INTR;
> +		++vcpu->stat.signal_exits;
> +		return -EINTR;
> +	}
> +
> +	kvm_pre_enter_guest(vcpu);
> +	trace_kvm_reenter(vcpu);
> +	guest_state_enter_irqoff();
> +	return RESUME_GUEST;
> +}
> +
>  int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
>  {
>  	unsigned long timer_hz;
  
zhaotianrui June 6, 2023, 2:13 a.m. UTC | #2
在 2023年06月05日 21:03, bibo, mao 写道:
>
> 在 2023/5/30 09:52, Tianrui Zhao 写道:
>> Implement vcpu handle exit interface, getting the exit code by ESTAT
>> register and using kvm exception vector to handle it.
>>
>> Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
>> ---
>>   arch/loongarch/kvm/vcpu.c | 46 +++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 46 insertions(+)
>>
>> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
>> index eca8b96a3e6e..ddea480fa5b0 100644
>> --- a/arch/loongarch/kvm/vcpu.c
>> +++ b/arch/loongarch/kvm/vcpu.c
>> @@ -55,6 +55,52 @@ static void kvm_pre_enter_guest(struct kvm_vcpu *vcpu)
>>   	vcpu->arch.aux_inuse &= ~KVM_LARCH_CSR;
>>   }
>>   
>> +/*
>> + * Return 1 for resume guest and "<= 0" for resume host.
>> + */
>> +static int _kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
>> +{
>> +	unsigned long exst = vcpu->arch.host_estat;
>> +	u32 intr = exst & 0x1fff; /* ignore NMI */
>> +	u32 exccode = (exst & CSR_ESTAT_EXC) >> CSR_ESTAT_EXC_SHIFT;
>> +	int ret = RESUME_GUEST;
>> +
>> +	vcpu->mode = OUTSIDE_GUEST_MODE;
>> +
>> +	/* Set a default exit reason */
>> +	run->exit_reason = KVM_EXIT_UNKNOWN;
>> +	run->ready_for_interrupt_injection = 1;
> Is ready_for_interrupt_injection used in qemu or kvm for LoongArch?
We do not handle ready_for_interrupt_injection when vcpu exit in 
qemu/kvm, and I will remove it.

Thanks
Tianrui Zhao
>
>> +
>> +	local_irq_enable();
>> +	guest_state_exit_irqoff();
>> +
>> +	trace_kvm_exit(vcpu, exccode);
>> +	if (exccode) {
>> +		ret = _kvm_handle_fault(vcpu, exccode);
>> +	} else {
>> +		WARN(!intr, "suspicious vm exiting");
> how about comments like this?
>    WARN(!intr, "vm exiting with suspicious irq \n")
>
> Regards
> Bibo, Mao
Thanks, this comment should contain more information, I will fix it.

Thanks
Tianrui Zhao
>> +		++vcpu->stat.int_exits;
>> +	}
>> +
>> +	cond_resched();
>> +	local_irq_disable();
>> +
>> +	if (ret == RESUME_HOST)
>> +		return ret;
>> +
>> +	/* Only check for signals if not already exiting to userspace */
>> +	if (signal_pending(current)) {
>> +		vcpu->run->exit_reason = KVM_EXIT_INTR;
>> +		++vcpu->stat.signal_exits;
>> +		return -EINTR;
>> +	}
>> +
>> +	kvm_pre_enter_guest(vcpu);
>> +	trace_kvm_reenter(vcpu);
>> +	guest_state_enter_irqoff();
>> +	return RESUME_GUEST;
>> +}
>> +
>>   int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
>>   {
>>   	unsigned long timer_hz;
  

Patch

diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index eca8b96a3e6e..ddea480fa5b0 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -55,6 +55,52 @@  static void kvm_pre_enter_guest(struct kvm_vcpu *vcpu)
 	vcpu->arch.aux_inuse &= ~KVM_LARCH_CSR;
 }
 
+/*
+ * Return 1 for resume guest and "<= 0" for resume host.
+ */
+static int _kvm_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
+{
+	unsigned long exst = vcpu->arch.host_estat;
+	u32 intr = exst & 0x1fff; /* ignore NMI */
+	u32 exccode = (exst & CSR_ESTAT_EXC) >> CSR_ESTAT_EXC_SHIFT;
+	int ret = RESUME_GUEST;
+
+	vcpu->mode = OUTSIDE_GUEST_MODE;
+
+	/* Set a default exit reason */
+	run->exit_reason = KVM_EXIT_UNKNOWN;
+	run->ready_for_interrupt_injection = 1;
+
+	local_irq_enable();
+	guest_state_exit_irqoff();
+
+	trace_kvm_exit(vcpu, exccode);
+	if (exccode) {
+		ret = _kvm_handle_fault(vcpu, exccode);
+	} else {
+		WARN(!intr, "suspicious vm exiting");
+		++vcpu->stat.int_exits;
+	}
+
+	cond_resched();
+	local_irq_disable();
+
+	if (ret == RESUME_HOST)
+		return ret;
+
+	/* Only check for signals if not already exiting to userspace */
+	if (signal_pending(current)) {
+		vcpu->run->exit_reason = KVM_EXIT_INTR;
+		++vcpu->stat.signal_exits;
+		return -EINTR;
+	}
+
+	kvm_pre_enter_guest(vcpu);
+	trace_kvm_reenter(vcpu);
+	guest_state_enter_irqoff();
+	return RESUME_GUEST;
+}
+
 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 {
 	unsigned long timer_hz;