From patchwork Fri Jul 14 10:10:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 788297 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3x87zw4fbLz9s78 for ; Fri, 14 Jul 2017 20:22:52 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="steEIj9Z"; dkim-atps=neutral Received: from localhost ([::1]:36760 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVxkI-0002xm-LA for incoming@patchwork.ozlabs.org; Fri, 14 Jul 2017 06:22:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42356) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVxYV-0000CE-I0 for qemu-devel@nongnu.org; Fri, 14 Jul 2017 06:10:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVxYS-0004vo-GE for qemu-devel@nongnu.org; Fri, 14 Jul 2017 06:10:39 -0400 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]:35961) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dVxYS-0004vE-6e for qemu-devel@nongnu.org; Fri, 14 Jul 2017 06:10:36 -0400 Received: by mail-wr0-x241.google.com with SMTP id 77so11332075wrb.3 for ; Fri, 14 Jul 2017 03:10:36 -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=eHCT1uVhFWWCuYK9kxrnOATPnuepPw/vKdUhFiIud5w=; b=steEIj9Zpj3X3I8GSYWsi1bqESVwBRbMX+M4PHQRqA2zs5T0cne4SDRR0E3xQIpGU3 oZ+EABTroXZoWapNO9Gb78otYGSVIvl1tjpZQ7rciUtqlwjCzILCjCpVqt1a288l7Ycd Y9SEabID4B6seSOkQ2O1BbwOlMbKuormjnxhniPegGXtxjaYRQE5kzbsUYOJ5VXisjm4 m1S6rV/AXzN0JN/WuKdRpj1ViPwv+Z0ttu9IBfxj/hYM2gy48zjE1AhlV0OD3PfgeRot lnu+/2OgLpXbjxwtmLMtOeTFoMJ9b21/5Kcj5xOMt05E6xJRP0OG9k8vezcoBjVvbYrg JDlQ== 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=eHCT1uVhFWWCuYK9kxrnOATPnuepPw/vKdUhFiIud5w=; b=TmjuB4AbaQSRmdOj7iH4PiV6oxCIpXLsa2F7kOsCJGIvk+TJMo6zcbOE3rERcchhoj zI256VreTYyBN70Cpx9kfe8dzDhM8IgHp6mr9QCaOHhzmwrtddbDQSarLwMik8PYq9P/ m9ZT+Nz8GF2x7nLyCof3NKlFx6Pj1tHDPogqUzQG+unZAw417UCqUISREBX7fAaJC2Ss 6+mmyyJfQSclicQvn6Q6C4gvYaelv8PaoowQ2sBdsz5l+NoyNefZJ2L4aBYaqrehc8c8 kK6V/z1xgRMAdKiyBDYiWXd011hDTIQgmW1qXMhGxBG8Kxj7niIzMNfE0nyjqiz2ujSM Xr/w== X-Gm-Message-State: AIVw110LfrntlFaI1uYYE5YuUs4WRqxeslLpzavYCgZCH+Zz73ABviX2 P8WQpXVcz8ME8Cf9TfQ= X-Received: by 10.223.183.36 with SMTP id l36mr4144406wre.115.1500027035037; Fri, 14 Jul 2017 03:10:35 -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 143sm2934870wmg.9.2017.07.14.03.10.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 14 Jul 2017 03:10:34 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 14 Jul 2017 12:10:14 +0200 Message-Id: <1500027028-4127-3-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1500027028-4127-1-git-send-email-pbonzini@redhat.com> References: <1500027028-4127-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 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/55] 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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Ă©e 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); } -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; @@ -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); } -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 = CPU(cpu); - CPUX86State *env = &cpu->env; - VAPICHandlers *handlers; + X86CPU *x86_cpu = X86_CPU(cs); + PatchInfo *info = (PatchInfo *) data.host_ptr; + VAPICHandlers *handlers = info->handler; + target_ulong ip = info->ip; uint8_t opcode[2]; uint32_t imm32 = 0; - target_ulong current_pc = 0; - target_ulong current_cs_base = 0; - uint32_t current_flags = 0; - - if (smp_cpus == 1) { - handlers = &s->rom_state.up; - } else { - handlers = &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(); cpu_memory_rw_debug(cs, ip, opcode, sizeof(opcode), 0); 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(); } - resume_all_vcpus(); + g_free(info); +} + +static void patch_instruction(VAPICROMState *s, X86CPU *cpu, target_ulong ip) +{ + CPUState *cs = CPU(cpu); + VAPICHandlers *handlers; + PatchInfo *info; - 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 == 1) { + handlers = &s->rom_state.up; + } else { + handlers = &s->rom_state.mp; } + + info = g_new(PatchInfo, 1); + info->handler = handlers; + info->ip = ip; + + async_safe_run_on_cpu(cs, do_patch_instruction, RUN_ON_CPU_HOST_PTR(info)); } void vapic_report_tpr_access(DeviceState *dev, CPUState *cs, target_ulong ip,