From 04f7fd70b9980f162aacf20706b4ea67b7d63db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Wed, 15 Nov 2023 22:10:20 +0100 Subject: [PATCH] cpu-features: Improve the AVX2 detection By also checking that the CPU supports XSAVE, and that the OS has enabled it. This means we can get rid of the Linux-specific `noxsave` detection, and the "is actually enabled"-part is OS-agnostic. Co-authored-by: Stefano Moioli --- gum/gum.c | 46 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/gum/gum.c b/gum/gum.c index f6e6243f8..bd4c43199 100644 --- a/gum/gum.c +++ b/gum/gum.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2008-2022 Ole André Vadla Ravnås + * Copyright (C) 2023 Stefano Moioli * * Licence: wxWindows Library Licence, Version 3.1 */ @@ -719,7 +720,6 @@ gum_query_cpu_features (void) #if defined (HAVE_I386) -static gboolean gum_query_noxsave (void); static gboolean gum_get_cpuid (guint level, guint * a, guint * b, guint * c, guint * d); @@ -729,49 +729,19 @@ gum_do_query_cpu_features (void) GumCpuFeatures features = 0; guint a, b, c, d; - if (gum_query_noxsave ()) - return features; - if (gum_get_cpuid (7, &a, &b, &c, &d)) { - if ((b & (1 << 5)) != 0) - features |= GUM_CPU_AVX2; - } - - return features; -} - -static gboolean -gum_query_noxsave (void) -{ - gboolean noxsave = FALSE; - -#ifdef HAVE_LINUX - gchar * cmdline = NULL; - gchar ** params = NULL; - gint num_params, i; + gboolean cpu_has_avx2, cpu_has_xsave, os_enabled_xsave; - if (!g_file_get_contents ("/proc/cmdline", &cmdline, NULL, NULL)) - goto beach; + cpu_has_avx2 = (b & (1 << 5)) != 0; + cpu_has_xsave = (c & (1 << 27)) != 0; + os_enabled_xsave = (c & (1 << 28)) != 0; - if (!g_shell_parse_argv (cmdline, &num_params, ¶ms, NULL)) - goto beach; - - for (i = 0; i != num_params; i++) - { - if (strcmp (params[i], "noxsave") == 0) - { - noxsave = TRUE; - break; - } + if (cpu_has_avx2 && cpu_has_xsave && os_enabled_xsave) + features |= GUM_CPU_AVX2; } -beach: - g_strfreev (params); - g_free (cmdline); -#endif - - return noxsave; + return features; } static gboolean