Skip to content

Commit

Permalink
Check SPRR by issuing MRS
Browse files Browse the repository at this point in the history
  • Loading branch information
wtdcode committed Dec 8, 2024
1 parent 958ed09 commit 13a8da8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
30 changes: 12 additions & 18 deletions qemu/configure
Original file line number Diff line number Diff line change
Expand Up @@ -2151,32 +2151,26 @@ EOF
cat > $TMPC << EOF
#include "stdint.h"
int main() {
uint64_t commpage_sprr = (*(uint64_t*)0xFFFFFC10C);
// In Apple Hypervisor, this value is not accessbile and
// pthread_jit_write_protect_np essentially is a no-op
/*
if (!commpage_sprr) {
return 1;
} else {
return 0;
}
*/
uint64_t v;
// Now it is accessible but always zero, let's probe it runtime.
__asm__ __volatile__("isb sy\n"
"mrs %0, S3_6_c15_c1_5\n"
: "=r"(v)::"memory");
// In Apple Hypervisor virtualized environment (EL1), this value is not accessbile
// but pthread_jit_write_protect_np essentially is a no-op.
return 0;
}
EOF
if ! compile_prog ""; then
have_sprr='no'
have_sprr_mrs='no'
have_pthread_jit_protect='no'
else
$TMPE
if [ $? -eq 0 ]; then
have_sprr='yes'
have_sprr_mrs='yes'
else
have_sprr='no'
have_sprr_mrs='no'
have_pthread_jit_protect='no'
fi
fi
fi
Expand Down Expand Up @@ -2560,8 +2554,8 @@ if test "$have_pthread_jit_protect" = "yes" ; then
echo "HAVE_PTHREAD_JIT_PROTECT=y" >> $config_host_mak
fi

if test "$have_sprr" = "yes" ; then
echo "HAVE_SPRR=y" >> $config_host_mak
if test "$have_sprr_mrs" = "yes" ; then
echo "HAVE_SPRR_MRS=y" >> $config_host_mak
fi

# Hold two types of flag:
Expand Down
22 changes: 10 additions & 12 deletions qemu/include/tcg/tcg-apple-jit.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
#include "stdlib.h"
#include "stdbool.h"

#if defined(__APPLE__) && defined(HAVE_SPRR) && (defined(__arm__) || defined(__aarch64__))

// Returns the S3_6_c15_c1_5 register's value
// Taken from
// https://stackoverflow.com/questions/70019553/lldb-how-to-read-the-permissions-of-a-memory-region-for-a-thread
// https://blog.svenpeter.dev/posts/m1_sprr_gxf/
// On Github Action (Virtualized environment), this shall always returns 0
#if defined(HAVE_SPRR_MRS)
static inline uint64_t read_sprr_perm(void)
{
uint64_t v;
Expand All @@ -45,6 +44,14 @@ static inline uint64_t read_sprr_perm(void)
: "=r"(v)::"memory");
return v;
}
#else
static inline uint64_t read_sprr_perm(void)
{
return 0;
}
#endif

#if defined(__APPLE__) && defined(HAVE_PTHREAD_JIT_PROTECT) && (defined(__arm__) || defined(__aarch64__))

__attribute__((unused)) static inline uint8_t thread_mask()
{
Expand Down Expand Up @@ -77,15 +84,6 @@ static inline void assert_executable(bool executable) {

#else

// Returns the S3_6_c15_c1_5 register's value
// Taken from
// https://stackoverflow.com/questions/70019553/lldb-how-to-read-the-permissions-of-a-memory-region-for-a-thread
// https://blog.svenpeter.dev/posts/m1_sprr_gxf/
static inline uint64_t read_sprr_perm(void)
{
return 0;
}

__attribute__((unused)) static inline uint8_t thread_mask()
{
return 0;
Expand All @@ -107,7 +105,7 @@ static inline void assert_executable(bool executable) {
#endif


#if defined(__APPLE__) && defined(HAVE_PTHREAD_JIT_PROTECT) && defined(HAVE_SPRR) && (defined(__arm__) || defined(__aarch64__))
#if defined(__APPLE__) && defined(HAVE_PTHREAD_JIT_PROTECT) && (defined(__arm__) || defined(__aarch64__))

/* write protect enable = write disable */
static inline void jit_write_protect(int enabled)
Expand Down

0 comments on commit 13a8da8

Please sign in to comment.