From 149b1f38e8ca67aef965b8f5b1dac5d1e2155c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonny=20Sv=C3=A4rd?= Date: Fri, 19 Apr 2024 14:59:15 +0200 Subject: Verify return value of pmu_event_value() calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure that pmu_event_value returns a valid PMU event value before writing to PMU HW registers. Change-Id: I546493a7870fc73ef2b7269cd4d7be4072c3cebf Signed-off-by: Jonny Svärd --- src/ethosu_pmu.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/ethosu_pmu.c b/src/ethosu_pmu.c index b5925c4..0b58c6f 100644 --- a/src/ethosu_pmu.c +++ b/src/ethosu_pmu.c @@ -1,6 +1,5 @@ /* - * SPDX-FileCopyrightText: Copyright 2019-2023 Arm Limited and/or its affiliates - * + * SPDX-FileCopyrightText: Copyright 2019-2024 Arm Limited and/or its affiliates * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the License); you may @@ -75,10 +74,8 @@ static uint32_t pmu_event_value(enum ethosu_pmu_event_type event) { return eventbyid[event]; } - else - { - return (uint32_t)(-1); - } + + return UINT32_MAX; } /***************************************************************************** @@ -110,6 +107,12 @@ void ETHOSU_PMU_Set_EVTYPER(struct ethosu_driver *drv, uint32_t num, enum ethosu { assert(num < ETHOSU_PMU_NCOUNTERS); uint32_t val = pmu_event_value(type); + if (val == UINT32_MAX) + { + LOG_ERR("Invalid ethosu_pmu_event_type: %d", type); + return; + } + LOG_DEBUG("num=%" PRIu32 ", type=%d, val=%" PRIu32, num, type, val); drv->dev->reg->PMEVTYPER[num].word = val; } @@ -269,8 +272,14 @@ void ETHOSU_PMU_CNTR_Increment(struct ethosu_driver *drv, uint32_t mask) void ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(struct ethosu_driver *drv, enum ethosu_pmu_event_type start_event) { LOG_DEBUG("start_event=%u", start_event); - uint32_t val = pmu_event_value(start_event); struct pmccntr_cfg_r cfg; + uint32_t val = pmu_event_value(start_event); + if (val == UINT32_MAX) + { + LOG_ERR("Invalid ethosu_pmu_event_type: %d", start_event); + return; + } + cfg.word = drv->dev->reg->PMCCNTR_CFG.word; cfg.CYCLE_CNT_CFG_START = val; drv->dev->reg->PMCCNTR_CFG.word = cfg.word; @@ -279,8 +288,14 @@ void ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(struct ethosu_driver *drv, enum etho void ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(struct ethosu_driver *drv, enum ethosu_pmu_event_type stop_event) { LOG_DEBUG("stop_event=%u", stop_event); - uint32_t val = pmu_event_value(stop_event); struct pmccntr_cfg_r cfg; + uint32_t val = pmu_event_value(stop_event); + if (val == UINT32_MAX) + { + LOG_ERR("Invalid ethosu_pmu_event_type: %d", stop_event); + return; + } + cfg.word = drv->dev->reg->PMCCNTR_CFG.word; cfg.CYCLE_CNT_CFG_STOP = val; drv->dev->reg->PMCCNTR_CFG.word = cfg.word; -- cgit v1.2.1