Files
Martin Storsjö 47855fce76 arm, aarch64: Don't use the ccnt / pmccntr_el0 registers for READ_TIME
These registers aren't universally readable - on Linux, they are
inaccessible from user space by default, unless they have been
unlocked (e.g. with a custom kernel module). Attempting to read them
triggers an illegal instruction.

This doesn't make any difference for checkasm, which uses its own
definitions of timers. Checkasm dynamically tests which timers are
accessible and picks the best one that is usable.

This avoids surprises if READ_TIME is used outside of a benchmarking
context. (av_get_random_seed() has got a final fallback through
get_generic_seed(), which uses READ_TIME.)

The recently added test fate-softfloat also contains START_TIMER /
STOP_TIMER, which uses READ_TIME. This would cause the fate-softfloat
test to fail on Linux on arm or aarch64, if built with
--disable-linux-perf.

The default on Linux is to try to detect perf by default. In these
cases, the test doesn't trigger the illegal instruction, even
if the START_TIMER / STOP_TIMER benchmarking fails if perf isn't
accessible to the current user.

On Windows, these registers actually are documented to always be
readable from user space. However, if Windows binaries are run in
Wine on Linux, and the register hasn't been unlocked for user space,
then we hit the illegal instruction.

Thus: This fixes illegal instructions in the fate-softfloat test
in some build configurations, and avoids potential issues if any
other code ends up calling READ_TIME.
2026-09-30 17:16:00 +00:00

47 lines
1.4 KiB
C

/*
* Copyright (c) 2015 Janne Grunau <janne-libav@jannau.net>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVUTIL_AARCH64_TIMER_H
#define AVUTIL_AARCH64_TIMER_H
#include <stdint.h>
#include "config.h"
#if HAVE_INLINE_ASM
#define AV_READ_TIME read_time
static inline uint64_t read_time(void)
{
uint64_t cycle_counter;
__asm__ volatile(
"isb \t\n"
// cntvct_el0 has lower resolution than pmccntr_el0, but is usually
// accessible from user space by default.
"mrs %0, cntvct_el0 "
: "=r"(cycle_counter) :: "memory" );
return cycle_counter;
}
#endif /* HAVE_INLINE_ASM */
#endif /* AVUTIL_AARCH64_TIMER_H */