From patchwork Sun Jun 11 11:54:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?John_H=C3=B6gberg?= X-Patchwork-Id: 1793546 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=ericsson.com header.i=@ericsson.com header.a=rsa-sha256 header.s=selector1 header.b=tCV3zcTT; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QfCsZ4Nfyz20Ww for ; Sun, 11 Jun 2023 21:54:26 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q8JeC-0007ZV-KM; Sun, 11 Jun 2023 07:54:16 -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 1q8JeA-0007ZK-ON for qemu-devel@nongnu.org; Sun, 11 Jun 2023 07:54:14 -0400 Received: from mail-db3eur04on061b.outbound.protection.outlook.com ([2a01:111:f400:fe0c::61b] helo=EUR04-DB3-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q8Je8-00088x-Rl for qemu-devel@nongnu.org; Sun, 11 Jun 2023 07:54:14 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B2hID1LlxGoWnYq9pAS3q91UGV7M8Gplhkv8KpxpBLYE3SXFDAdCpmI9g3aE2XuQ++2rGoy4Kw1iEsVI4uWDiGYjg97nisJi/2MNqW51mpY1a+OxtMepSQhsYnF3HxIYK9SQg5tq/cp4psiEeDP6x/7yJVsyi//54k37YCX78blD6EN1ClET5gDoX28phLkWOK+s/Lqx4K7rL+SUn5rnoFzkZnXf2WierZ1iWO06qyzX+MNsL5/rZpdJb6htfzwSGHU3Ds+DT14yUTiuM4NZKWuMCUU+KCRuLc6ai4YBx9dln5X1qXl6IR9cQDK2N9G5mZMuKy2LoHv5hLh1SVXFgQ== 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=gFW77lzn1ZK3N73vC+NUxRjMYltfw4iy7PXItfj0o5s=; b=Gyyr+n1yrPDyp7ANGab4oOK1L+rMOWkAWqbQ5Zn2ORcvLpJifCs+ZBewiFyLDv+fYMN3+MjLmRlfrylq0mKFWbaNupbaVeiyCFlnpmcaupxWOCO7ERV66LfZFCLoN89qxTve7LyUtbsf0uzrF3a84GBOWBUH/e3nm9KkrL0Uct38U36ax7r0qGy2/aIskvrYa7Z2M3fCchTWDtVyLH0VaLn6Amt6OSyc9fVfY7anNpZ23f/O5mCWxt8hb4yfqNyRI9OAMAomrnH2fOTb5KNdV8RDVG86VpLPMpbfAHtvAt0L89o0O3Avr6EDHsbUEXKOmWow9HEEU9rBn841krNhUQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=ericsson.com; dmarc=pass action=none header.from=ericsson.com; dkim=pass header.d=ericsson.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gFW77lzn1ZK3N73vC+NUxRjMYltfw4iy7PXItfj0o5s=; b=tCV3zcTTXJYP+bUeZRcnMIZQ1Rp6ow/v/YfWsEdvNN9XgcK643Kt6xC6Y+gYK/5E3SLGDEOQoZ9cLWyhu4DVOKCD/YOZTEchIP1Tr+J8mZqjdPHsUO8HN0wMcMT1pihLcO306eaT0LdwShspJ7DzQIl7Dpa96oPJ8QPj+73vnWU= Received: from DU0PR07MB8833.eurprd07.prod.outlook.com (2603:10a6:10:310::5) by VI1PR07MB6606.eurprd07.prod.outlook.com (2603:10a6:800:185::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.38; Sun, 11 Jun 2023 11:54:09 +0000 Received: from DU0PR07MB8833.eurprd07.prod.outlook.com ([fe80::568:2863:92fb:8e85]) by DU0PR07MB8833.eurprd07.prod.outlook.com ([fe80::568:2863:92fb:8e85%7]) with mapi id 15.20.6455.030; Sun, 11 Jun 2023 11:54:09 +0000 From: =?utf-8?q?John_H=C3=B6gberg?= To: "qemu-devel@nongnu.org" CC: "peter.maydell@linaro.org" Subject: [PATCH 2/2] tests/tcg/aarch64: Add testcases for IC IVAU and dual-mapped code Thread-Topic: [PATCH 2/2] tests/tcg/aarch64: Add testcases for IC IVAU and dual-mapped code Thread-Index: AQHZnFtu/DkQ/Y7eOEG5yn3LJGJQbg== Date: Sun, 11 Jun 2023 11:54:09 +0000 Message-ID: References: <833e7382b3fb90a2bbb007932cb5dbb8cfff5a33.camel@ericsson.com> In-Reply-To: <833e7382b3fb90a2bbb007932cb5dbb8cfff5a33.camel@ericsson.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=ericsson.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: DU0PR07MB8833:EE_|VI1PR07MB6606:EE_ x-ms-office365-filtering-correlation-id: e1f82403-5a16-49c0-8bff-08db6a729138 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: LK1RMAz1sh4gEqT6fZn2qMYey+ULOny7d9zO5ZjkTBVMl899vc7ZXCUofqmWAzb4n/bt0nCTMOEODHnhs9FnlXCnHJycKwrwGeqQ8X187liIjRcbTw0Ju15iJA1xaP89vBFVOsAJsjaBdg2OhLTDbgVwh9uu4s+TxP1Un0CfdIRMArXtHLLSoDsIYDMEsh/bOUQwdCEwVuAfleIz96gYfr3foMZoz39o4iK3DOImhgCc0Yinx99IQvV1g4aaAeTw7xr8AFqPK36ItMG6i6ONrJ77zItmWB5XRwDfBy9hNzfB9SnXFXsINnXkb9MDnkR9mU9/Px8wRi82W561Tdg8ZRAaDuDKJV+v13oTJ30u7RlmU8AGtIHNgIthQNAPy/HympamVgpDjvJke3XJfGM7OuEsLBuzNMa3kXVbpOsRNsDVMsTfxSI1npOZhle3wRm4HJRo4jRlTMRzJZJG8Vk9MYMXpvcyRmFUH6Vb9Vc9noROubwilTAt7AvBEPlD28QbuRd7nlJJH0z1I3KUPv2htPXitqEBCD+2vMn4E3xuGpswiwnDR4jg3LpbWTVXU4vK6txhtGMD/6Sv83jElNoE9VELZj3Y67ZNJS8X9yJA5rI= x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU0PR07MB8833.eurprd07.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(396003)(346002)(366004)(136003)(39860400002)(376002)(451199021)(966005)(6486002)(316002)(41300700001)(2616005)(83380400001)(186003)(38070700005)(26005)(6506007)(86362001)(66574015)(6512007)(2906002)(82960400001)(122000001)(38100700002)(5660300002)(85182001)(36756003)(8936002)(85202003)(8676002)(66946007)(66556008)(66476007)(76116006)(91956017)(478600001)(71200400001)(66446008)(4326008)(6916009)(64756008); DIR:OUT; SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?PsM+vi5ZgXBTicGvMyK/jgLQwn8i?= =?utf-8?q?/CdnYfdtvUoWP5TSxe4WuSFOdya3TZkbXcc1bUFLqGlhuGsJd3ylV+F/o49xT/nxM?= =?utf-8?q?9kaMxYsQ7oTpjK2jSaw19fslljEXh5D7cAe5JXwGXGvjRZZHqjbPbtR6KBQhFgazj?= =?utf-8?q?8MlJdMKfME1mEH1HdpjnoOXjk0lUpvgbnoy+8MgNzruqDXjhOZYlERadLmBKlQZYg?= =?utf-8?q?BgPBoHQpoWTIfG64nEC8H3jvhwSCdDxYL26Z/E8UeOnhpcmB2NtM3eeTmHC3dUead?= =?utf-8?q?i00/jP9kfsnzCYeX+vy8Hjlk12G+ktNgkPHdPLYZ1QVlhZ5SxVD/KLK/5LDUDOlsc?= =?utf-8?q?7Cso6F8kWckKD86Rqv/XkXOpQJDRQ6JnriJp++ZZZiiQ5KyjMY7T5QiZS2NbZe8SF?= =?utf-8?q?AsPCkToLseiJGhRK+6SYFIbrSpbzSgmlGYwOWjVPfemgjQbLgysyw5umeN4ND0LJr?= =?utf-8?q?1wqbfLERGRPMeruxCTzngFtLGsr/u74m8lE0mpn2UDSrEk0h7B5GLbKCcbt/FK1Gr?= =?utf-8?q?M4nasUqdK0h7z4GQqqFytwwesKZAK9OyL+ovM5BIcdJnYwFNNQFDyrVTKCRtvlJOj?= =?utf-8?q?izm+v+ANFHXm0gq2OhvDQN35Isp8I1jbEtJru9tjRTJ4ICMoKXyPgyPxG3aSzdd0w?= =?utf-8?q?Ey4xH32uTZaw1GNAYnV/M7itabmyJeDURjfsBGj6YwsDp/jz+artIsQHCb8LA91fi?= =?utf-8?q?HcQOmmsqlOLcs/ao1DBmCN6us2woCNiKXdiHOeq+gjsm2dJA5dQCmhAIUX1JGiGSC?= =?utf-8?q?i4f+n13lRGpNDJrgTuizu8vXK4fHSb8lIF10IfVI7119d6PGTNP49NRjopuOz7//E?= =?utf-8?q?1SpGeNcIgvzzKXQwriMvqB9h68Gp0uENfaYQPggQ9vV8CwOktb4IlxybO4YEM95Wz?= =?utf-8?q?F4K8rAUvO63gh7C9qVxsme8oPKjh1tD/ZxHbGQKYBN8H84F47jn5JrdY9L6ZScTVB?= =?utf-8?q?FNDi97MZWbZVyebbuG5grDGIZ61Z2wIfpp6TyHz8dRhtSV+CEtYyzkw2j6qalhm2H?= =?utf-8?q?MhX0Li/X7bi/QQTIz1hKrCfDsPNphYKKEhlj2GewKyJXCCjWXu6js8wgzaY9mlsLq?= =?utf-8?q?esqMauXnXSufyzqXr38kzOrSlGQ+WWdLxA9VHJOrchmbhg2KWTS3mIpJqSbGGD8jm?= =?utf-8?q?QRiNtqN98eONDXkRmSg1IiT60YxNzxZvJfVtrJ+jQ9Ox4c9Sbt4a9PSLHBDcOTU/y?= =?utf-8?q?OxzSHOhELp06W+/pUAL+rlbPQqol10O7IM5ngtTfAkqWTW+5U0jFGrsH/2yk5saX0?= =?utf-8?q?8ADanNz5p3lRHgKi8GGOFl3v+bDp12xQu6LURVoQXM7t1NbYDCKPSIl1TB/FtpHzv?= =?utf-8?q?OCzJ5TABbURBVDXQ0WYpvH8yK9hbdyZ86DUj9oCZGyFwo4Y70L5ASsdKdSJSMZm9B?= =?utf-8?q?1C+D4bbKTsg2/ANzR+l1UdBw7YSzQ3xiMZf9GJQBQVBd+crmAT9p0yRuH5z7A3ofU?= =?utf-8?q?ZjPpjIfxWEAZGDuLb0tYcV3ad+98P1arhSIjc82Qmbm3Ajsmb9jmIfatvxC+jrcJ3?= =?utf-8?q?hiUc5rb8ZBQ0ZRIINTM6sK9pkUdJPX1f5Q=3D=3D?= Content-ID: <90E17B1CA15491438DBE015578355C8E@eurprd07.prod.outlook.com> MIME-Version: 1.0 X-OriginatorOrg: ericsson.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DU0PR07MB8833.eurprd07.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e1f82403-5a16-49c0-8bff-08db6a729138 X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Jun 2023 11:54:09.8107 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 92e84ceb-fbfd-47ab-be52-080c6b87953f X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: ivql4StnzjCB89Ojuy6Nnah0cAX4LP/bmadas/5Vhgu2UkXRVnay9tj6JRkdvCYCURoO1aqIK2rVcloOCsX8KxjTrH8LmruGumnZHcpvimU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR07MB6606 Received-SPF: pass client-ip=2a01:111:f400:fe0c::61b; envelope-from=john.hogberg@ericsson.com; helo=EUR04-DB3-obe.outbound.protection.outlook.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org https://gitlab.com/qemu-project/qemu/-/issues/1034 Signed-off-by: John Högberg --- tests/tcg/aarch64/Makefile.target | 3 +- tests/tcg/aarch64/icivau.c | 204 ++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/aarch64/icivau.c compare_tests[i]); + } + + if (success) { + return EXIT_SUCCESS; + } + } + } + + return EXIT_FAILURE; +} diff --git a/tests/tcg/aarch64/Makefile.target b/tests/tcg/aarch64/Makefile.target index 3430fd3cd8..de6566d0d4 100644 --- a/tests/tcg/aarch64/Makefile.target +++ b/tests/tcg/aarch64/Makefile.target @@ -9,9 +9,10 @@ AARCH64_SRC=$(SRC_PATH)/tests/tcg/aarch64 VPATH += $(AARCH64_SRC) # Base architecture tests -AARCH64_TESTS=fcvt pcalign-a64 +AARCH64_TESTS=fcvt pcalign-a64 icivau fcvt: LDFLAGS+=-lm +icivau: LDFLAGS+=-lrt run-fcvt: fcvt $(call run-test,$<,$(QEMU) $<, "$< on $(TARGET_NAME)") diff --git a/tests/tcg/aarch64/icivau.c b/tests/tcg/aarch64/icivau.c new file mode 100644 index 0000000000..ff80d3d868 --- /dev/null +++ b/tests/tcg/aarch64/icivau.c @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +#include + +#define PAYLOAD_SIZE (256) + +typedef int (*SelfModTestPtr)(char *, const char*, int); +typedef int (*CompareTestPtr)(int, int); + +void flush_icache(const char *exec_data, size_t length) +{ + size_t dcache_stride, icache_stride, i; + unsigned long ctr_el0; + + /* + * Step according to minimum cache sizes, as the cache maintenance + * instructions operate on the cache line of the given address. + * + * We assume that exec_data is properly aligned. + */ + __asm__("mrs %0, ctr_el0\n" : "=r"(ctr_el0)); + dcache_stride = (4 << ((ctr_el0 >> 16) & 0xF)); + icache_stride = (4 << (ctr_el0 & 0xF)); + + for (i = 0; i < length; i += dcache_stride) { + const char *dc_addr = &exec_data[i]; + __asm__ ("dc cvau, %x[dc_addr]\n" + : /* no outputs */ + : [dc_addr] "r"(dc_addr) + : "memory"); + } + + __asm__ ("dmb ish\n"); + + for (i = 0; i < length; i += icache_stride) { + const char *ic_addr = &exec_data[i]; + __asm__ ("ic ivau, %x[ic_addr]\n" + : /* no outputs */ + : [ic_addr] "r"(ic_addr) + : "memory"); + } + + __asm__ ("dmb ish\n" + "isb sy\n"); +} + +/* + * The unmodified assembly of this function returns 0, it self- modifies to + * return the value indicated by new_move. + */ +int self_modification_payload(char *rw_data, const char *exec_data, + int new_move) +{ + register int result __asm__ ("w0") = new_move; + + __asm__ (/* Get the writable address of __modify_me. */ + "sub %x[rw_data], %x[rw_data], %x[exec_data]\n" + "adr %x[exec_data], __modify_me\n" + "add %x[rw_data], %x[rw_data], %x[exec_data]\n" + /* Overwrite the `MOV W0, #0` with the new move. */ + "str %w[result], [%x[rw_data]]\n" + /* + * Mark the code as modified. + * + * Note that we align to the nearest 64 bytes in an attempt to put + * the flush sequence in the same cache line as the modified move. + */ + ".align 6\n" + "dc cvau, %x[exec_data]\n" + ".align 2\n" + "dmb ish\n" + "ic ivau, %x[exec_data]\n" + "dmb ish\n" + "isb sy\n" + "__modify_me: mov w0, #0x0\n" + : [result] "+r"(result), + [rw_data] "+r"(rw_data), + [exec_data] "+r"(exec_data) + : /* No untouched inputs */ + : "memory"); + + return result; +} + +int self_modification_test(char *rw_data, const char *exec_data) +{ + SelfModTestPtr copied_ptr = (SelfModTestPtr)exec_data; + int i; + + /* + * Bluntly assumes that the payload is position-independent and not larger + * than PAYLOAD_SIZE. + */ + memcpy(rw_data, self_modification_payload, PAYLOAD_SIZE); + + /* + * Notify all PEs that the code at exec_data has been altered. + * + * For completeness we could assert that we should fail when this is + * omitted, which works in user mode and on actual hardware as the + * modification won't "take," but doesn't work in system mode as the + * softmmu handles everything for us. + */ + flush_icache(exec_data, PAYLOAD_SIZE); + + for (i = 1; i < 10; i++) { + const int mov_w0_template = 0x52800000; + + /* MOV W0, i */ + if (copied_ptr(rw_data, exec_data, mov_w0_template | (i << 5)) != i) { + return 0; + } + } + + return 1; +} + +int compare_copied(char *rw_data, const char *exec_data, + int (*reference_ptr)(int, int)) +{ + CompareTestPtr copied_ptr = (CompareTestPtr)exec_data; + int a, b; + + memcpy(rw_data, reference_ptr, PAYLOAD_SIZE); + flush_icache(exec_data, PAYLOAD_SIZE); + + for (a = 1; a < 10; a++) { + for (b = 1; b < 10; b++) { + if (copied_ptr(a, b) != reference_ptr(a, b)) { + return 0; + } + } + } + + return 1; +} + +int compare_alpha(int a, int b) +{ + return a + b; +} + +int compare_beta(int a, int b) +{ + return a - b; +} + +int compare_gamma(int a, int b) +{ + return a * b; +} + +int compare_delta(int a, int b) +{ + return a / b; +} + +int main(int argc, char **argv) +{ + const char *shm_name = "qemu-test-tcg-aarch64-icivau"; + int fd; + + fd = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); + + if (fd < 0) { + return EXIT_FAILURE; + } + + /* Unlink early to avoid leaving garbage in case the test crashes. */ + shm_unlink(shm_name); + + if (ftruncate(fd, PAYLOAD_SIZE) == 0) { + const char *exec_data; + char *rw_data; + + rw_data = mmap(0, PAYLOAD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, + fd, 0); + exec_data = mmap(0, PAYLOAD_SIZE, PROT_READ | PROT_EXEC, MAP_SHARED, + fd, 0); + + if (rw_data && exec_data) { + CompareTestPtr compare_tests[4] = {compare_alpha, + compare_beta, + compare_gamma, + compare_delta}; + int success, i; + + success = self_modification_test(rw_data, exec_data); + + for (i = 0; i < 4; i++) { + success &= compare_copied(rw_data, exec_data,