From patchwork Wed Mar 15 14:40:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Fei" X-Patchwork-Id: 13175974 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6D009C61DA4 for ; Wed, 15 Mar 2023 14:40:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pcSIy-0008QK-U2; Wed, 15 Mar 2023 10:40:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pcSIw-0008Pa-WA for qemu-devel@nongnu.org; Wed, 15 Mar 2023 10:40:39 -0400 Received: from mga07.intel.com ([134.134.136.100]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pcSIu-0000FE-IZ for qemu-devel@nongnu.org; Wed, 15 Mar 2023 10:40:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678891236; x=1710427236; h=message-id:date:to:from:subject: content-transfer-encoding:mime-version; bh=gC0OjEe+yHUfPdnXcnDTQ9F3Z5oF9QAVCNadWMrLOcc=; b=GaFHI0Kf7qoLJowvT00orUoWlynPIyfchhOQv4Pt1avo6+jj/QCutIi+ p4VYKLKOw1tSLOzkYKI1nJeUGutDo97bkQzrQpP5AngjTzRwNkSiRpz3u vWUy11Mfc38PLdcaWO0bUpC2lBa/wE3A3UWB5jmy5SpNK5TA0jpYXg1qr tAJLUyqk6plhA4Z0DcbOqpe5Wl8iVeKq+84bg2isyiJVq2uVTKfQ+WyPg lrAdPsKtw/Uqjab2mOfdZUeGXVz64guY3LTwDcj6Wfebf7aMmUGemfoo9 5e0Eo9m6//OLUBeXmoZstZnkB5z/zrcFT1PMgT26PO/ky4RslMhtUOleO Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10649"; a="402589132" X-IronPort-AV: E=Sophos;i="5.98,262,1673942400"; d="scan'208";a="402589132" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Mar 2023 07:40:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10649"; a="709704980" X-IronPort-AV: E=Sophos;i="5.98,262,1673942400"; d="scan'208";a="709704980" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by orsmga008.jf.intel.com with ESMTP; 15 Mar 2023 07:40:32 -0700 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Wed, 15 Mar 2023 07:40:32 -0700 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21 via Frontend Transport; Wed, 15 Mar 2023 07:40:32 -0700 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (104.47.58.168) by edgegateway.intel.com (134.134.137.102) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.21; Wed, 15 Mar 2023 07:40:31 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CYCr3Od9fIQ94xOD0Cwee86pG7clpv/fSdGnbiVBNaS6qScd9fglXPv8s3+y/bhLBaEjF1fsuoE3+a9Of/qf1VTUZiB8myBFo/MDhwYxFvmiedwHzZXWQkJQsJgTd0aDznBi46UdeqQUFSjyEYmPuFqOP4XJppg4FiFm4Qdj9hFL0elJQjPTphidJ1jTj6rQ/eIoRUc+jJ97grT3Epil337OtrCoCIx6DqDUduxiLOksZq0NeXx0wkl32vfzfyrW/k7kAP7xV+X5gwEMSNGgbnuBeg7bpL3VeJJDmuHiNgbCq/rZjzM1ZcXEfc5wFnaFMBgtjrxk8xjgRley6UpKfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ddU/SdTHdp1qjTkVGu54MIzWf04vI7aR4J/Kdh0K4f4=; b=itdJn0hLTCsOD/tZV65l0BCKYB0WFmZMw3VWDKJgsidSi7mlJ0/qQv8gd6K/OMgkfeGck1gW32D1YZzZI4YZ1kKV2yXJgfW0lC+VnTTCThqI25q+cdIVSWSre/g81m++29t3mUwI6/IinWVQxc5C9V4C0C94mlB9DplpHYGEF/OBnWx5C5Pxx/o7gCBC03vzhnOUM0IzVet2QD7O3XG+OZnV1vhWlwufUH69m9JElhk4x42UNAqCCXQp4hERoIUcBRU3YEpDd9GQNaAX5KMynLZvlDLZGAUYfUCoQUFpG8Sg4bJFc3sypV/1nPrwa+KFqjU/lpCFDKBAr481nNsAcQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from BY5PR11MB4500.namprd11.prod.outlook.com (2603:10b6:a03:1c3::24) by SJ2PR11MB7618.namprd11.prod.outlook.com (2603:10b6:a03:4cb::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.24; Wed, 15 Mar 2023 14:40:25 +0000 Received: from BY5PR11MB4500.namprd11.prod.outlook.com ([fe80::68a4:ef95:6726:3fc5]) by BY5PR11MB4500.namprd11.prod.outlook.com ([fe80::68a4:ef95:6726:3fc5%4]) with mapi id 15.20.6178.026; Wed, 15 Mar 2023 14:40:25 +0000 Message-ID: <60517f41-a5db-a907-03d1-545b69615a62@intel.com> Date: Wed, 15 Mar 2023 22:40:17 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Content-Language: en-US To: , From: "Wu, Fei" Subject: Enable cross-page block chaining for user mode tcg X-ClientProxiedBy: SG2PR01CA0130.apcprd01.prod.exchangelabs.com (2603:1096:4:40::34) To BY5PR11MB4500.namprd11.prod.outlook.com (2603:10b6:a03:1c3::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BY5PR11MB4500:EE_|SJ2PR11MB7618:EE_ X-MS-Office365-Filtering-Correlation-Id: 2fd16a42-c0b7-41d4-fdfd-08db2563360f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LBE4lvI0CZ1BhLULc6A2T0a4KC62KUmCydqt7I9f5lrZRFPri45yYXOnKakvKUynyRunXDsk6xcfAL6Je7ThQjF8WyaLICeN38gBrRhnmcastIIxz/pl4rhb6wzTvBFwRZJtodhG9KJISicn7npnW7cYphuqdaJq539sFQwPM37y6lCgNJSTxf9CKo2tzRrunaCcdyJrinsdCI/edrBzddIe1qN1A57mP/OeggwjHiePsc6L0/fXD8rVCyXXWzLudX8LX+JkM8MktYJm8VwgvC1aGM++bfTUvof1K71FasedIY95Q4Rbdeu3LIwTZU3xA2ZfBBXNd+ZQdJJf3ECUIURoaqQVz3zmVM/JoMqJ9FRCR1pW2a/txQSEg/6Q5mjFrcgFpw4UPGQx/IEzstnpEUGYfGozUHcm+EnzpTB9XruFOx1b0e2BkkORV3g0uproS+dIY690q7P70xudV65LmctA/EdR6O4FchUwrCuTi1kGD/RsGWqhehR5WT0uq+tvePsGlLN32HofPXv74oum24A8jPg59MIk2IgL4mi7eIMyq7ABQTbLYBFfwgBOyGZisZkUHvCQeJXJil6io2HT350pTrVPdKORE4MMZpl3w9LX2dBbphImcon8Wgjf/af+o8mPRa4surbdawnwc3u4MNgx2cTwVvm3fn0e3U8zmgAGtmbHsxy8xZjTPDuTvE7+/O1IK3gsgVrwV0XC92JvAp+/SObxjD1oLI1RZuXrWH0= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BY5PR11MB4500.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230025)(39860400002)(396003)(136003)(366004)(346002)(376002)(451199018)(31696002)(86362001)(316002)(36756003)(82960400001)(38100700002)(66946007)(41300700001)(66476007)(66556008)(8936002)(8676002)(478600001)(2906002)(2616005)(5660300002)(83380400001)(6486002)(6666004)(186003)(26005)(6512007)(6506007)(31686004)(45980500001)(43740500002); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?QN+0ex2SbSMBLUYiRo1BECQX6XQB?= =?utf-8?q?QLZ4bTmj1yvi0QbDSBY9r1vnSUHQN4vYcMKqHWUQSTLvRM4NokxYLuSxPMPBTzggZ?= =?utf-8?q?4GTTL3Ei6mp2/U2bMU8C37n7hexuxIM+jnceMuKb6kAhMHUd+dnh0PQvMuLUtSpNF?= =?utf-8?q?saJTRI3ednWcBymzni6uDQRya9t+TmwBY/+ofVKps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HjAEexKJeJsUvo?= =?utf-8?q?XDTAuyTB7ih2A+TCn9/QkgybNfre+Ibzerk+PmPzlLBpEJCOS5RvNRMhnhZBUrYLq?= =?utf-8?q?n95Vhu0th41lwBAkVKFgMMCXl8a9T38y4QnLXmPnxeWMGxp6H268GmrKTOcyBqqBG?= =?utf-8?q?B85JghMG3YwGmFn7K8OhkXivlRGOhMzIbyxJt6JFAVu/V6Dij/OEkkQ6oBnGWGutF?= =?utf-8?q?8HfkPBkphcaLvMOb29k5YhNeNJPi4SRHbzO1mtZcDgrFO9McdPWywNjUlH5ZJe/Lv?= =?utf-8?q?AGPsWfe8ZxZwy5Pj1LCEsJ1jz0vaMMSEjUcYhksRK5mNWL2r+pX6KQvrtcCtXUY+y?= =?utf-8?q?nXY4RYeKgT7ZzhmdxhdgsYWgDFgY9a+EnpKWHwjp/+2VZ/UztETYcR1LOQs7+9MdU?= =?utf-8?q?Ij2S/izBA/6XiExlQjPowTRP/TjHOAhQYsajf3IrnVwS2bz4hgoJeQCcSZG/o535j?= =?utf-8?q?YVg+c7VnseiC?= X-MS-Exchange-CrossTenant-Network-Message-Id: 2fd16a42-c0b7-41d4-fdfd-08db2563360f X-MS-Exchange-CrossTenant-AuthSource: BY5PR11MB4500.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Mar 2023 14:40:24.9876 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: mZ+BNAA/JmmVWsrNuRto32/+AI32I2MEdezSE7ul/06/5xuAoL+bTklACSrA5EQJDpJzyqLWWHGRYIAe7Q6LDA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR11MB7618 X-OriginatorOrg: intel.com Received-SPF: pass client-ip=134.134.136.100; envelope-from=fei2.wu@intel.com; helo=mga07.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Block chaining is one of the key performance factors of tcg. Currently tcg doesn't allow chaining across page boundary, an example can be found in gen_goto_tb() in target/riscv/translate.c. For user-mode tcg, it's possible to enable cross-page chaining with careful attentions, assume there are chains like this: preceding page -> 1st page -> 2nd page Nth page -> 2nd page There are 2 situations to consider: 1. First page should not jump to 2nd page directly anymore, if there is a new breakpoint added to 3rd page, otherwise the breakpoint might not be hit. One method to address this problem is when receiving gdb commands, call tb_flush() to invalidate all the TBs, and make sure each TB can only contain single instruction later, no matter the new JIT-ed TBs use chain or not, the tcg core loop always has the chance to check if there is any breakpoint on each instruction. There could be other methods, but current tcg has already done this. 2. The protection of 2nd page has changed by mprotect/munmap, e.g. from executable (X) to non-executable (NX), it's an error if the 1st page jumps to 2nd page without checking the new protection. The point here is to invalidate TBs in 2nd page and unlink all the TBs which jumps to it, including 1st page and others(Nth in above chart). This is already done in page_set_flags(). A small testcase runs on user-mode guest: void *page = mmap(NULL, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); memcpy(page, func_add, pagesize); f = (FUNC)page; f(1, 1); // good mprotect(f, pagesize, PROT_READ | PROT_EXEC); f(1, 2); // good mprotect(f, pagesize, PROT_READ); f(1, 3); // segfault So it looks like current tcg implementation is ready to enable cross-page chaining for user-mode. Correct? void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, Thanks, Fei. diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c index 7bda43ff61..822644c7a4 100644 --- a/accel/tcg/translator.c +++ b/accel/tcg/translator.c @@ -25,8 +25,12 @@ bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest) return false; } +#ifdef CONFIG_USER_ONLY + return true; +#else /* Check for the dest on the same page as the start of the TB. */ return ((db->pc_first ^ dest) & TARGET_PAGE_MASK) == 0; +#endif }