From nobody Mon Feb 9 17:59:02 2026 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.zohomail.com; dkim=fail; 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 1499957133023284.98224759242214; Thu, 13 Jul 2017 07:45:33 -0700 (PDT) Received: from localhost ([::1]:60452 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVfMs-0003V2-Ez for importer@patchew.org; Thu, 13 Jul 2017 10:45:26 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51219) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVf3Q-0000zN-1o for qemu-devel@nongnu.org; Thu, 13 Jul 2017 10:25:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVf3N-0002Qw-Oq for qemu-devel@nongnu.org; Thu, 13 Jul 2017 10:25:20 -0400 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]:34411) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dVf3N-0002Ph-FX for qemu-devel@nongnu.org; Thu, 13 Jul 2017 10:25:17 -0400 Received: by mail-wr0-x241.google.com with SMTP id k67so9397970wrc.1 for ; Thu, 13 Jul 2017 07:25:17 -0700 (PDT) Received: from 640k.lan (94-39-191-51.adsl-ull.clienti.tiscali.it. [94.39.191.51]) by smtp.gmail.com with ESMTPSA id k75sm6042448wmh.10.2017.07.13.07.25.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Jul 2017 07:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LaaZUNSMN5aVOgv3pLPWfJAyvKJjnYIz4A+O2zF8qbo=; b=TJIMGgPc74ZLxinHUm25nOZ6+OMUgMBZOED9gWSFXLBXttOml7184De6XdpbAq5jM7 tINa9k1B2DWbkPL/fNChL9poEFKIUtqB7K86ALqiRInaRSZMFxGyFF+6YWHoQsLuP9x1 3S2AujorQod7SlNkOkAU/H5D6/T/iHyGiM/YsZe6tFbNeyE16j8kGzn44SMxBVWDua6A QdbMPK992TeRNJSQCR8/6WGC4ZclsMRQkKgKOV2oHefxueSbo0KcAbmYqkToxNO+LP41 74zn4AJk43wwRnkdIBKF95rui6wAUiWzXw/J4VANcuyPxs4/v5LlhqTZ/8gHKL4lsSAV 88JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=LaaZUNSMN5aVOgv3pLPWfJAyvKJjnYIz4A+O2zF8qbo=; b=rKalUepOkMixQgvubXM8DaysWw0VEQoTsexKR9XsYCBAYChFnfkY82PS+nk4fet9pu f7l8Q6OblHTRbu9kWC1PCuepCR+iGifIsCL6Jra4hCq9bEFhKLn36mnamIkpFOSe4KQB HTEIDQ/FfozQpW3/Q2blKTmpoXzyvbXLJb9VfDlCw+ZOIzdw39SrIjrNjHiKiU0DSxta hNPvkiGJV8i7pq0JpR1aXFMMg+uLc1pTr0Q5+lWO9JNt78c5pqFJd374mwXz+XMF01tl BvtJrFrgvj87vzKu1qoIs/PqTB3nK1zrNMgX1mShdXMbaDCd5tLTvMYY0jz35lgHviEA 9slQ== X-Gm-Message-State: AIVw113JpfDqiJsSHkQr20ZCfWK9EQjN+sAKmX+SR6/9nixaJ/1IWoUs YpBl/oRSwaoehJoF3aY= X-Received: by 10.223.164.218 with SMTP id h26mr1998079wrb.100.1499955916272; Thu, 13 Jul 2017 07:25:16 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 13 Jul 2017 16:24:16 +0200 Message-Id: <1499955874-10954-24-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1499955874-10954-1-git-send-email-pbonzini@redhat.com> References: <1499955874-10954-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::241 Subject: [Qemu-devel] [PULL 23/41] 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 , Pranith Kumar Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 From: Pranith Kumar 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 Reviewed-by: Richard Henderson Reviewed-by: Alex Benn=C3=83=C2=A9e Signed-off-by: Pranith Kumar Message-Id: <20170712215143.19594-2-bobby.prani@gmail.com> [Get rid completely of the TCG-specific code. - Paolo] Signed-off-by: Paolo Bonzini --- hw/i386/kvmvapic.c | 85 +++++++++++++++++++++++++-------------------------= ---- 1 file changed, 39 insertions(+), 46 deletions(-) diff --git a/hw/i386/kvmvapic.c b/hw/i386/kvmvapic.c index 0d9ef77..fc962c5 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,77 +392,71 @@ 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) +typedef struct PatchInfo { + VAPICHandlers *handler; + target_ulong ip; +} PatchInfo; + +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); + PatchInfo *info =3D (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; - - if (smp_cpus =3D=3D 1) { - handlers =3D &s->rom_state.up; - } else { - handlers =3D &s->rom_state.mp; - } - - if (tcg_enabled()) { - cpu_restore_state(cs, cs->mem_io_pc); - cpu_get_tb_cpu_state(env, ¤t_pc, ¤t_cs_base, - ¤t_flags); - /* Account this instruction, because we will exit the tb. - This is the first instruction in the block. Therefore - there is no need in restoring CPU state. */ - if (use_icount) { - --cs->icount_decr.u16.low; - } - } - - pause_all_vcpus(); =20 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; + PatchInfo *info; =20 - if (tcg_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); + if (smp_cpus =3D=3D 1) { + handlers =3D &s->rom_state.up; + } else { + handlers =3D &s->rom_state.mp; } + + info =3D g_new(PatchInfo, 1); + info->handler =3D handlers; + info->ip =3D ip; + + async_safe_run_on_cpu(cs, do_patch_instruction, RUN_ON_CPU_HOST_PTR(in= fo)); } =20 void vapic_report_tpr_access(DeviceState *dev, CPUState *cs, target_ulong = ip, --=20 1.8.3.1