# RISC-V GCC 12 issue when setting the MTIMECMP register

**URL:** <https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850>\
**Category:** Kernel\
**Created:** [September 16, 2022, 8:17am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850 "2022-09-16T08:17:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![marksat](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/marksat/32/2858_2.png) [@marksat](https://forums.freertos.org/u/marksat)\
**Post date:** [September 16, 2022, 8:17am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/1 "2022-09-16T08:17:39Z")

</div>

Hi All,

We’ve been using the GCC v7.2.0 toolchain for compiling our code with FreeRTOS 10.2.0 for RISC-V.

I recently experimented GCC v12.2.0 and found that FreeRTOS was not running any of my tasks, not even the IDLE task was given an opportunity to run.

Digging into it, I determined that the issue is that the following variable in port.c has a 0 in it:  
`const uint32_t ulTimerIncrementsForOneTick = ( uint32_t ) ( configRTC_CLOCK_HZ / configTICK_RATE_HZ ); /* Assumes increment won't go over 32-bits. */`

This means that the machine timer (MTIME) is effectively stuck in an interrupt. As soon as MTIMECMP is updated, it is time to service the interrupt again.

What I found, was that if I removed the `const` keyword from `ulTimerIncrementsForOneTick`, then the MTIME timer works again and the system works. I cannot rationalise why the removal of the “const” keyword means that the correct value gets loaded into the variable during the device’s `init()`.

`configRTC_CLOCK_HZ` and `configTICK_RATE_HZ` are fixed values that the compiler has access to.

The same code works with GCC 7.2.0 and not in GCC 12.2.0. Can someone help me shed some light? I don’t want to just remove the `const`, it doesn’t feel like the right thing to do.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 16, 2022, 9:06pm UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/2 "2022-09-16T21:06:59Z")

</div>

If the constant is only used in assembly code then it’s possible the compiler removes it and you need to use a “keep” directive to prevent that happening. However I can’t find that constant in the code anywhere so don’t know if that is the case. Grateful for a [github link](https://github.com/FreeRTOS/FreeRTOS) to the line of code.

---

<div class="post-metadata">

**Author:** ![marksat](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/marksat/32/2858_2.png) [@marksat](https://forums.freertos.org/u/marksat)\
**Post date:** [September 16, 2022, 10:58pm UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/3 "2022-09-16T22:58:02Z")

</div>

Hi Richard,

It looks like the type of the variable has changed from a `uint32_t` to a `size_t` in a newer version of the kernel.

Here’s a [link](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/b0a8bd8f28d0138b5eb70e8b53da3e9d17ce8d40/portable/GCC/RISC-V/port.c#L92) to the constant.

The variable is used when the scheduler starts in [vPortSetupTimerInterrupt()](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/b0a8bd8f28d0138b5eb70e8b53da3e9d17ce8d40/portable/GCC/RISC-V/port.c#L147). My understanding of this is that it sets up the first MTIME interrupt by loading in the number of timer increments for one RTOS tick into the MTIMECMP register. After the first timer interrupt fires, the [assembly](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/b0a8bd8f28d0138b5eb70e8b53da3e9d17ce8d40/portable/GCC/RISC-V/portASM.S#L113) takes over to constantly update the MTIMECMP register to schedule the next RTOS tick.

portASM.S does look slightly different from the version I am using (v10.2.0), maybe I will try update the kernel to the latest to see if the GCC compiler treats it any differently.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 17, 2022, 5:02pm UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/4 "2022-09-17T17:02:42Z")

</div>

Is it possible for you to share the generated assembly of `vPortSetupTimerInterrupt`?

---

<div class="post-metadata">

**Author:** ![marksat](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/marksat/32/2858_2.png) [@marksat](https://forums.freertos.org/u/marksat)\
**Post date:** [September 20, 2022, 9:05am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/5 "2022-09-20T09:05:15Z")

</div>

Hi Gaurav,

Here are some links to GitHub gists. The only different between the two is whether or not `ulTimerIncrementsForOneTick` in port.c has the const keyword.

- With const keyword  
\*\* [FreeRTOS v10.2.0, GCC 12.2.0 (with const) · GitHub](https://gist.github.com/marksat/b1bfa1e44cd543801b71080e78fd2971)  
\*\* The compiler has chosen to directly load in the value 306 (no reference to memory). This looks OK, but it only run when FreeRTOS starts. After the first MTIME interrupt, the MTIMECMP needs to be updated, this is done in postASM.S, `handle_asynchronous` in my gist. Here, it references a memory location 0x80000124, which has never been initialised and contains the value 0.
- Without const keyword  
\*\* [FreeRTOS v10.2.0, GCC 12.2.0 (without const) · GitHub](https://gist.github.com/marksat/12e6830e7d14247103b324f5c3ef9fa0)  
\*\* Both `vPortSetupTimerInterrupt()` and `handle_asynchronous()` reference a memory location (0x80000124) which has been pre-initialised with the value 306.

Maybe the compiler is getting confused when optimising a const variable which has been externed from port.c into portASM.S?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 20, 2022, 9:48am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/6 "2022-09-20T09:48:47Z")

</div>

> [@marksat](#):
>
> Maybe the compiler is getting confused when optimising a const variable which has been externed from port.c into portASM.S?

Yes, that seems to be the case. Can you try the following:

1. Add the following line to portmacro.h:

```auto
#define portDONT_DISCARD __attribute__ ( ( used ) )

```

1. Change the declaration of the variable to the following:

```auto
portDONT_DISCARD const size_t uxTimerIncrementsForOneTick = ( size_t ) ( ( configCPU_CLOCK_HZ ) / ( configTICK_RATE_HZ ) ); /* Assumes increment won't go over 32-bits. */

```

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 20, 2022, 11:12am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/7 "2022-09-20T11:12:35Z")

</div>

This relates to a KNOWN BUG in GCC that whole program optimization doesn’t take into account non-C/C++ code that references items.

The **attribute** ( (used) ) is the defined fix for this issue.

---

<div class="post-metadata">

**Author:** ![marksat](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/marksat/32/2858_2.png) [@marksat](https://forums.freertos.org/u/marksat)\
**Post date:** [September 20, 2022, 9:10pm UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/8 "2022-09-20T21:10:31Z")

</div>

Hi Gaurav, Richard,

I tried using ` __attribute__ ( ( used ) )` as specified, but unfortunately, this did not work.

I searched for GCC Variable Attributes to understand `used` a bit better and found this on [gnu.org](https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#Common-Variable-Attributes). It states that `used` can only be used on variables with static storage. `uxTimerIncrementsForOneTick` is not static, it is externed into `portASM.S`.

This lead me to a very insightful [Stack Overflow thread](https://stackoverflow.com/questions/61255179/setting-attribute-used-to-c-variable-constant-has-no-effect). The topic is becoming more difficult for me to comprehend, however, the takeaway from the thread (right at the bottom) is to add `--undefined=ulTimerIncrementsForOneTick` to my linker flags. This _compiles and works._

For reference, my full linker flags is as follows: `-Wl,--cref -mabi=ilp32 -g3 -gdwarf-2 -march=rv32imac_zicsr_zifencei -mcmodel=medlow -std=c99 -Werror -nostartfiles -static -nostdlib -L. -Wl,--start-group -Wl,--end-group -Wl,-gc-sections,--print-memory-usage,--undefined=ulTimerIncrementsForOneTick -lc -static-libgcc -lgcc`

I’m not sure I would call this a solution, what do you think? It feels a bit dirty to put this exception in the linker flags, however, I don’t want to go around modifying bits of the FreeRTOS kernel by removing a const keyword. My other alternative is just to stay put and continue to use GCC 7.2.0.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 20, 2022, 11:57pm UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/9 "2022-09-20T23:57:06Z")

</div>

There are some [dirty hacks](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/tasks.c#L2073) whereby we reference variables after the scheduler started, so in code that won’t run, but the compiler doesn’t know that. In your case that would [have to be done in the portable layer](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/b0a8bd8f28d0138b5eb70e8b53da3e9d17ce8d40/portable/GCC/RISC-V/port.c#L157), rather than the core code.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [September 21, 2022, 2:19am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/10 "2022-09-21T02:19:41Z")

</div>

So maybe this is actually an issue with the linker garbage collection of unused sections (and compiling with -fdata-sections, assuming you are doing that). Would be interested to see the linker output with option -Wl,-print-gc-sections to prove it. That option prints to stderr any section dropped due to being “unused”. Then search the stderr output for `ulTimerIncrementsForOneTick`. Maybe this is not be related to whole-program optimization at all.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 21, 2022, 4:55am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/11 "2022-09-21T04:55:34Z")

</div>

By any chance, are you compiling FreeRTOS as a static archive and then linking? Is it possible for you to share a minimal project showing the problem that I can use to repro it?

---

<div class="post-metadata">

**Author:** ![marksat](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/marksat/32/2858_2.png) [@marksat](https://forums.freertos.org/u/marksat)\
**Post date:** [September 21, 2022, 8:40am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/12 "2022-09-21T08:40:19Z")

</div>

Thanks for the help and feedback so far.

I will get the output from `--print-gc-sections` later today.

I am compiling FreeRTOS as a static library and linking it into my executable.

I will start on getting a minimal project together that demonstrates the issue, however, this is for an ASIC we have developed and I am checking with the team that I would be OK to share the header files. It might be that I create a private repo and share access as needed.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 21, 2022, 9:35am UTC](https://forums.freertos.org/t/risc-v-gcc-12-issue-when-setting-the-mtimecmp-register/15850/13 "2022-09-21T09:35:41Z")

</div>

> [@marksat](#):
>
> I am compiling FreeRTOS as a static library and linking it into my executable.

That is likely the cause. The following stack overflow response explains the issue - [c - What does the GNU ld --undefined option do? - Stack Overflow](https://stackoverflow.com/questions/42588983/what-does-the-gnu-ld-undefined-option-do)

Can you try compiling FreeRTOS sources along with the executable instead of building FreeRTOS as a static library?
