From nobody Sun May 5 13:33:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 14882597527811021.3041926900214; Mon, 27 Feb 2017 21:29:12 -0800 (PST) Received: from localhost ([::1]:58695 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ciaLW-0003jc-HL for importer@patchew.org; Tue, 28 Feb 2017 00:29:10 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58014) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ciaKi-0003Sq-DQ for qemu-devel@nongnu.org; Tue, 28 Feb 2017 00:28:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ciaKf-0004hr-9Z for qemu-devel@nongnu.org; Tue, 28 Feb 2017 00:28:20 -0500 Received: from mail-yw0-x243.google.com ([2607:f8b0:4002:c05::243]:33558) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ciaKf-0004gx-33 for qemu-devel@nongnu.org; Tue, 28 Feb 2017 00:28:17 -0500 Received: by mail-yw0-x243.google.com with SMTP id l138so180619ywc.0 for ; Mon, 27 Feb 2017 21:28:14 -0800 (PST) Received: from localhost.localdomain (c-73-207-178-95.hsd1.ga.comcast.net. [73.207.178.95]) by smtp.gmail.com with ESMTPSA id m124sm247174ywe.38.2017.02.27.21.28.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Feb 2017 21:28:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=FHXDsEsqAAoX1+xhdfWSrne8OfkH/0dDU3EDMUnNHI8=; b=NORyxN3wPNoxo4RBuprCr8lH8OW5GBfIZEzjYgBX7m0RUlgJZcBCZCfTNA3PhRRhK9 xgOQUEjN2qXVB6wC0xvfjqVfvE4aPC+zzLxUYqRRz2quI4TOsBps1du0I9eWZPBvMlTu aIZADeOh61AJ//h07Q1YjKjGoU7BsKA8Uoy9CKGLeuj98zKw98ODNu7I7JcwT+oKMEYJ 4zB9qhbUAl9hPgR7WANCkwMF2jLqUk+pPTPQwUDuB0MtbDsTnpG0jo6A8yNvbIXNVNkh k9HZNweCncU3DN5NCqtWA8+aMCXW+1B/cBui2lVi7YD0ehgBJEeDsp0hZ3t2PM3gDV6v eyWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=FHXDsEsqAAoX1+xhdfWSrne8OfkH/0dDU3EDMUnNHI8=; b=c9RuLcyDkkXcCgsBG06DSqIe/5bINjk+lKKXkzzj8cEE2wb9a+BIrYNAxBmIeWh+zI q3/Z5XY3zVAaG1ytK3DO+6eswuV7XRqJ2FNTyYx+HD9T+EOgiQwQ+vQF3J3nMdHMtecN +4dQ2kmOrcuX8VdjrqNEgR9pFq6wlPUIhHGB8+LzTSDKmXXphldOAA9PiZEdoX6S9vTN klDCOwrsMo8+xfUv6d395W1Z40bisA8Kt/oNrwWPaC6ER9h4MhBOYhgO52P5bqGyZaNW kqwLsHoU7DrKOFnXYuHMnrg3DLWP10kj1DqXNjlFqdEfdOgPvnNv3N9Q7BXyh+/G+StB ApZg== X-Gm-Message-State: AMke39mZvN0AglRT82R+r2gchInwF2bIkBpKdjP25Ger3tKu7n/KH8T9AD5DDqPzC/+0ZQ== X-Received: by 10.13.214.131 with SMTP id y125mr59859ywd.13.1488259693491; Mon, 27 Feb 2017 21:28:13 -0800 (PST) From: Pranith Kumar To: "Michael S. Tsirkin" , Paolo Bonzini , Richard Henderson , Eduardo Habkost , qemu-devel@nongnu.org (open list:All patches CC here) Date: Tue, 28 Feb 2017 00:28:11 -0500 Message-Id: <20170228052811.25264-1-bobby.prani@gmail.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4002:c05::243 Subject: [Qemu-devel] [PATCH] mttcg/i386: Patch instruction using async_safe_* framework X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 In mttcg, calling pause_all_vcpus() during execution from the generated TBs causes a deadlock if some vCPU is waiting for exclusive execution in start_exclusive(). Fix this by using the aync_safe_* framework instead of pausing vcpus for patching instructions. CC: Paolo Bonzini CC: Peter Maydell Signed-off-by: Pranith Kumar Reviewed-by: Richard Henderson Reviewed-by: Alex Benn=C3=A9e --- hw/i386/kvmvapic.c | 82 ++++++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/hw/i386/kvmvapic.c b/hw/i386/kvmvapic.c index 82a49556af..11b0d494eb 100644 --- a/hw/i386/kvmvapic.c +++ b/hw/i386/kvmvapic.c @@ -383,8 +383,7 @@ static void patch_byte(X86CPU *cpu, target_ulong addr, = uint8_t byte) cpu_memory_rw_debug(CPU(cpu), addr, &byte, 1, 1); } =20 -static void patch_call(VAPICROMState *s, X86CPU *cpu, target_ulong ip, - uint32_t target) +static void patch_call(X86CPU *cpu, target_ulong ip, uint32_t target) { uint32_t offset; =20 @@ -393,23 +392,24 @@ static void patch_call(VAPICROMState *s, X86CPU *cpu,= target_ulong ip, cpu_memory_rw_debug(CPU(cpu), ip + 1, (void *)&offset, sizeof(offset),= 1); } =20 -static void patch_instruction(VAPICROMState *s, X86CPU *cpu, target_ulong = ip) +struct PatchInfo { + VAPICHandlers *handler; + target_ulong ip; +}; + +static void do_patch_instruction(CPUState *cs, run_on_cpu_data data) { - CPUState *cs =3D CPU(cpu); - CPUX86State *env =3D &cpu->env; - VAPICHandlers *handlers; + X86CPU *x86_cpu =3D X86_CPU(cs); + CPUX86State *env =3D &x86_cpu->env; + struct PatchInfo *info =3D (struct PatchInfo *) data.host_ptr; + VAPICHandlers *handlers =3D info->handler; + target_ulong ip =3D info->ip; uint8_t opcode[2]; uint32_t imm32 =3D 0; target_ulong current_pc =3D 0; target_ulong current_cs_base =3D 0; uint32_t current_flags =3D 0; =20 - if (smp_cpus =3D=3D 1) { - handlers =3D &s->rom_state.up; - } else { - handlers =3D &s->rom_state.mp; - } - if (!kvm_enabled()) { cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, ¤t_flags); @@ -421,48 +421,70 @@ static void patch_instruction(VAPICROMState *s, X86CP= U *cpu, target_ulong ip) } } =20 - pause_all_vcpus(); - cpu_memory_rw_debug(cs, ip, opcode, sizeof(opcode), 0); =20 switch (opcode[0]) { case 0x89: /* mov r32 to r/m32 */ - patch_byte(cpu, ip, 0x50 + modrm_reg(opcode[1])); /* push reg */ - patch_call(s, cpu, ip + 1, handlers->set_tpr); + patch_byte(x86_cpu, ip, 0x50 + modrm_reg(opcode[1])); /* push reg= */ + patch_call(x86_cpu, ip + 1, handlers->set_tpr); break; case 0x8b: /* mov r/m32 to r32 */ - patch_byte(cpu, ip, 0x90); - patch_call(s, cpu, ip + 1, handlers->get_tpr[modrm_reg(opcode[1])]= ); + patch_byte(x86_cpu, ip, 0x90); + patch_call(x86_cpu, ip + 1, handlers->get_tpr[modrm_reg(opcode[1])= ]); break; case 0xa1: /* mov abs to eax */ - patch_call(s, cpu, ip, handlers->get_tpr[0]); + patch_call(x86_cpu, ip, handlers->get_tpr[0]); break; case 0xa3: /* mov eax to abs */ - patch_call(s, cpu, ip, handlers->set_tpr_eax); + patch_call(x86_cpu, ip, handlers->set_tpr_eax); break; case 0xc7: /* mov imm32, r/m32 (c7/0) */ - patch_byte(cpu, ip, 0x68); /* push imm32 */ + patch_byte(x86_cpu, ip, 0x68); /* push imm32 */ cpu_memory_rw_debug(cs, ip + 6, (void *)&imm32, sizeof(imm32), 0); cpu_memory_rw_debug(cs, ip + 1, (void *)&imm32, sizeof(imm32), 1); - patch_call(s, cpu, ip + 5, handlers->set_tpr); + patch_call(x86_cpu, ip + 5, handlers->set_tpr); break; case 0xff: /* push r/m32 */ - patch_byte(cpu, ip, 0x50); /* push eax */ - patch_call(s, cpu, ip + 1, handlers->get_tpr_stack); + patch_byte(x86_cpu, ip, 0x50); /* push eax */ + patch_call(x86_cpu, ip + 1, handlers->get_tpr_stack); break; default: abort(); } =20 - resume_all_vcpus(); + g_free(info); +} + +static void patch_instruction(VAPICROMState *s, X86CPU *cpu, target_ulong = ip) +{ + CPUState *cs =3D CPU(cpu); + VAPICHandlers *handlers; + struct PatchInfo *info; + + if (smp_cpus =3D=3D 1) { + handlers =3D &s->rom_state.up; + } else { + handlers =3D &s->rom_state.mp; + } + + info =3D g_new(struct PatchInfo, 1); + info->handler =3D handlers; + info->ip =3D ip; =20 if (!kvm_enabled()) { - /* Both tb_lock and iothread_mutex will be reset when - * longjmps back into the cpu_exec loop. */ - tb_lock(); - tb_gen_code(cs, current_pc, current_cs_base, current_flags, 1); - cpu_loop_exit_noexc(cs); + const run_on_cpu_func fn =3D do_patch_instruction; + + async_safe_run_on_cpu(cs, fn, RUN_ON_CPU_HOST_PTR(info)); + cs->exception_index =3D EXCP_INTERRUPT; + cpu_loop_exit(cs); } + + pause_all_vcpus(); + + do_patch_instruction(cs, RUN_ON_CPU_HOST_PTR(info)); + + resume_all_vcpus(); + g_free(info); } =20 void vapic_report_tpr_access(DeviceState *dev, CPUState *cs, target_ulong = ip, --=20 2.11.0