0

Is it possible to read the Cycle Count Register (DWT_CYCCNT) when executing at unprivileged?

#define DWT_CYCCNT  (*(volatile uint32_t*)(0xE0001004)) /**<  Cycle Count Register */
CycleCount = DWT_CYCCNT; /* Unprivileged read of the Cycle Count Register causes a Bus Fault. */

Related: Measuring clock cycle count on cortex m7

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • I assume you want the cycle count register as a hires timestamp. `clock_gettime(CLOCK_MONOTONIC)` will do that on most arm arches. The register _can_ be made accessible to userspace with some kernel help. `clock_gettime` does that. The kernel (using the `VDSO` mechanism) will inject the code directly into the app (i.e. _no_ syscall required). If you write a program that calls `clock_gettime`, you can then disassemble it with `gdb` to see what it's doing. I've had to do all that before and I've found that using `clock_gettime` is _better_ than direct access to the register in most cases. – Craig Estey May 24 '22 at 21:25
  • Thank you Craig; unfortunately I am not using Linux but have a bare metal application utilising unprivileged to lock down application – Paul Wearne May 25 '22 at 06:02
  • No, you cannot. I have run into this problem before. – Realtime Rik May 30 '22 at 14:29

1 Answers1

0

Short answer: no.

Debug registers are located in special reserved address space

The architecture reserves address space 0xE0000000 to 0xFFFFFFFF for system-level use. ARM reserves the first 1MB of this system address space, 0xE0000000 to 0xE00FFFFF, as the Private Peripheral Bus (PPB)

Register in question specifically

Data Watchpoint and Trace (DWT) block 0xE0001000-0xE0001FFF

Section "C1.2.1 General rules applying to debug register access" explicitly state that DWT memory, as a rule of thumb, is privilege access only:

The Private Peripheral Bus (PPB), address range 0xE0000000 to 0xE0100000, supports the following general rules:

  • The region is defined as Strongly-ordered memory
  • Registers are always accessed little-endian
  • Debug registers can only be accessed as a word access
  • A reserved register or bit field has the value UNK/SBZP

Unprivileged access to the PPB causes BusFault errors unless otherwise stated.


Retrieved from "ARMv7-M Architecture Reference Manual" (ARM DDI 0403D)
user3124812
  • 1,861
  • 3
  • 18
  • 39