Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pkTy7-0000zL-87 for mharc-qemu-riscv@gnu.org; Thu, 06 Apr 2023 14:04:19 -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 1pkTy6-0000z5-5e for qemu-riscv@nongnu.org; Thu, 06 Apr 2023 14:04:18 -0400 Received: from mail-oa1-x36.google.com ([2001:4860:4864:20::36]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pkTy2-0000bu-Kj for qemu-riscv@nongnu.org; Thu, 06 Apr 2023 14:04:17 -0400 Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-17ac5ee3f9cso43197753fac.12 for ; Thu, 06 Apr 2023 11:04:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1680804251; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gwHVgVMzQCDJB5vK9WQl9LSOsUrvo+0sTqi3vha+yuc=; b=DTXmvRgXSEvpFJQOqLZYVBX/ts9TEra3l2MGUbsghhb3eSf2bdKFVYKhLzrco3yNUn OCv15cmPfHgc0FTjkfP+t/xoBTKdFQrflIlpb1dKl+IdIlWLx0gmSIf+mamnlCZiNAXl g02IJ7atjh+Svasy/U/TMoQXXWxbCBZ1pzqwaBDTFx7qoUmIqj0DECfvnz3Wlo9V1a5p 44KiZXwDyFZr1VL6JmYaJfjYR6t2E6k7BBlkDWfwH2DwU+Lij2vDYb//U4TzuX5XyC4m bq8Q9jvl/ZWtdrdltFvu979W9zfBP3n4td2EWw/qhJGzZlFknWSGU2F/ohQ9XGv1Zn+o fwNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680804251; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gwHVgVMzQCDJB5vK9WQl9LSOsUrvo+0sTqi3vha+yuc=; b=KlqJXFmVjQ7etSr8yIU8i/TAxjrnDdpfGgnBblpjyqiPYA7nZyPZ91QE9Uh10dAhOn t1uiJm8Q8Wr/9kmXQ/zkrCCh26YB5afGiCHnWLhwgz32uB31buyriWa9jHD9MPm8gGN/ gurtwkYH64OTci+mqPB8rGrH2VbC2xs/88/plMQvQP33SeO2+7aCS3GDQ/GwIEHl7P+r Gh+b1t8ood136Pnrr4xQE6iVUQPohG6Wm+v+yRvJ8/alzF66Yf5i/r5rIFxtkna80icC HZz5ameCBswALEvcKVs2DZokC5rldZBWAZN5J+lEusyHwgUw65HDqXjQ8accspP5w995 xmCA== X-Gm-Message-State: AAQBX9d+7cOUYbefMEgxVq8irWnj5ziHMeleK5InlqDGcO1221kxa9Q+ HmPM5PnGJUNTTrajY+U7QJHM9A== X-Google-Smtp-Source: AKy350bhtC6sNMMXK21aUubXjurcLlK9TseVKcRt93Vaxct6/fJnFvC5CGkY1DUhz7tZyRWRccbvjQ== X-Received: by 2002:a05:6870:2196:b0:177:9040:d236 with SMTP id l22-20020a056870219600b001779040d236mr106267oae.28.1680804251714; Thu, 06 Apr 2023 11:04:11 -0700 (PDT) Received: from grind.. ([191.255.108.232]) by smtp.gmail.com with ESMTPSA id g4-20020a056870d20400b0018045663fc5sm906309oac.48.2023.04.06.11.04.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 11:04:11 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Cc: qemu-riscv@nongnu.org, alistair.francis@wdc.com, bmeng@tinylab.org, liweiwei@iscas.ac.cn, zhiwei_liu@linux.alibaba.com, palmer@rivosinc.com, Daniel Henrique Barboza Subject: [PATCH v4 04/20] target/riscv: introduce riscv_cpu_add_misa_properties() Date: Thu, 6 Apr 2023 15:03:35 -0300 Message-Id: <20230406180351.570807-5-dbarboza@ventanamicro.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230406180351.570807-1-dbarboza@ventanamicro.com> References: <20230406180351.570807-1-dbarboza@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2001:4860:4864:20::36; envelope-from=dbarboza@ventanamicro.com; helo=mail-oa1-x36.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-riscv@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Apr 2023 18:04:18 -0000 Ever since RISCVCPUConfig got introduced users are able to set CPU extensions in the command line. User settings are reflected in the cpu->cfg object for later use. These properties are used in the target/riscv/cpu.c code, most notably in riscv_cpu_validate_set_extensions(), where most of our realize time validations are made. And then there's env->misa_ext, the field where the MISA extensions are set, that is read everywhere else. We need to keep env->misa_ext updated with cpu->cfg settings, since our validations rely on it, forcing us to make register_cpu_props() write cpu->cfg.ext_N flags to cover for named CPUs that aren't used named properties but also needs to go through the same validation steps. Failing to so will make those name CPUs fail validation (see c66ffcd5358b for more info). Not only that, but we also need to sync env->misa_ext with cpu->cfg again during realize() time to catch any change the user might have done, since the rest of the code relies on that. Making cpu->cfg.ext_N and env->misa_ext reflect each other is not needed. What we want is a way for users to enable/disable MISA extensions, and there's nothing stopping us from letting the user write env->misa_ext directly. Here are the artifacts that will enable us to do that: - RISCVCPUMisaExtConfig will declare each MISA property; - cpu_set_misa_ext_cfg() is the setter for each property. We'll write env->misa_ext and env->misa_ext_mask with the appropriate misa_bit; cutting off cpu->cfg.ext_N from the logic; - cpu_get_misa_ext_cfg() is a getter that will retrieve the current val of the property based on env->misa_ext; - riscv_cpu_add_misa_properties() will be called in register_cpu_props() to init all MISA properties from the misa_ext_cfgs[] array. With this infrastructure we'll start to get rid of each cpu->cfg.ext_N attribute in the next patches. Signed-off-by: Daniel Henrique Barboza Reviewed-by: Weiwei Li Reviewed-by: Alistair Francis --- target/riscv/cpu.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index fbf612292a..3b234a03d0 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1453,6 +1453,69 @@ static void riscv_cpu_init(Object *obj) #endif /* CONFIG_USER_ONLY */ } +typedef struct RISCVCPUMisaExtConfig { + const char *name; + const char *description; + target_ulong misa_bit; + bool enabled; +} RISCVCPUMisaExtConfig; + +static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque; + target_ulong misa_bit = misa_ext_cfg->misa_bit; + RISCVCPU *cpu = RISCV_CPU(obj); + CPURISCVState *env = &cpu->env; + bool value; + + if (!visit_type_bool(v, name, &value, errp)) { + return; + } + + if (value) { + env->misa_ext |= misa_bit; + env->misa_ext_mask |= misa_bit; + } else { + env->misa_ext &= ~misa_bit; + env->misa_ext_mask &= ~misa_bit; + } +} + +static void cpu_get_misa_ext_cfg(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + const RISCVCPUMisaExtConfig *misa_ext_cfg = opaque; + target_ulong misa_bit = misa_ext_cfg->misa_bit; + RISCVCPU *cpu = RISCV_CPU(obj); + CPURISCVState *env = &cpu->env; + bool value; + + value = env->misa_ext & misa_bit; + + visit_type_bool(v, name, &value, errp); +} + +static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = {}; + +static void riscv_cpu_add_misa_properties(Object *cpu_obj) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(misa_ext_cfgs); i++) { + const RISCVCPUMisaExtConfig *misa_cfg = &misa_ext_cfgs[i]; + + object_property_add(cpu_obj, misa_cfg->name, "bool", + cpu_get_misa_ext_cfg, + cpu_set_misa_ext_cfg, + NULL, (void *)misa_cfg); + object_property_set_description(cpu_obj, misa_cfg->name, + misa_cfg->description); + object_property_set_bool(cpu_obj, misa_cfg->name, + misa_cfg->enabled, NULL); + } +} + static Property riscv_cpu_extensions[] = { /* Defaults for standard extensions */ DEFINE_PROP_BOOL("i", RISCVCPU, cfg.ext_i, true), @@ -1599,6 +1662,8 @@ static void register_cpu_props(Object *obj) return; } + riscv_cpu_add_misa_properties(obj); + for (prop = riscv_cpu_extensions; prop && prop->name; prop++) { qdev_property_add_static(dev, prop); } -- 2.39.2