From 19d4f90c44c3a5bd244c188997fa5a5c57c6fcc0 Mon Sep 17 00:00:00 2001 From: Jiaxun Yang Date: Sun, 31 Jul 2022 18:17:07 +0100 Subject: [PATCH] Use auvx to detect CPUCFG on mips/loongson It's safer and easier than SIGILL. Signed-off-by: Jiaxun Yang --- driver/others/dynamic_mips64.c | 80 +++++++++------------------------- 1 file changed, 21 insertions(+), 59 deletions(-) diff --git a/driver/others/dynamic_mips64.c b/driver/others/dynamic_mips64.c index 9fd19d739..d50b2dc27 100644 --- a/driver/others/dynamic_mips64.c +++ b/driver/others/dynamic_mips64.c @@ -38,6 +38,15 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include "common.h" +#if (defined OS_LINUX || defined OS_ANDROID) +#include +#include + +#ifndef HWCAP_LOONGSON_CPUCFG +#define HWCAP_LOONGSON_CPUCFG (1 << 14) +#endif +#endif + extern gotoblas_t gotoblas_LOONGSON3R3; extern gotoblas_t gotoblas_LOONGSON3R4; @@ -81,59 +90,10 @@ static gotoblas_t *force_coretype(char *coretype) { return NULL; } +#if (defined OS_LINUX || defined OS_ANDROID) #define MMI_MASK 0x00000010 #define MSA_MASK 0x00000020 -int fd[2]; -int support_cpucfg; - -static void handler(int signum) -{ - close(fd[1]); - exit(1); -} - -/* Brief : Function to check if cpucfg supported on loongson - * Return: 1 supported - * 0 not supported - */ -static int cpucfg_test(void) { - pid_t pid; - int status = 0; - - support_cpucfg = 0; - pipe(fd); - pid = fork(); - if (pid == 0) { /* Subprocess */ - struct sigaction act; - close(fd[0]); - /* Set signal action for SIGILL. */ - act.sa_handler = handler; - sigaction(SIGILL,&act,NULL); - - /* Execute cpucfg in subprocess. */ - __asm__ volatile( - ".insn \n\t" - ".word (0xc8080118) \n\t" - ::: - ); - support_cpucfg = 1; - write(fd[1],&support_cpucfg,sizeof(support_cpucfg)); - close(fd[1]); - exit(0); - } else if (pid > 0){ /* Parent process*/ - close(fd[1]); - if ((waitpid(pid,&status,0) <= 0) || - (read(fd[0],&support_cpucfg,sizeof(support_cpucfg)) <= 0)) - support_cpucfg = 0; - close(fd[0]); - } else { - support_cpucfg = 0; - } - - return support_cpucfg; -} - static gotoblas_t *get_coretype_from_cpucfg(void) { int flag = 0; __asm__ volatile( @@ -153,7 +113,7 @@ static gotoblas_t *get_coretype_from_cpucfg(void) { } static gotoblas_t *get_coretype_from_cpuinfo(void) { -#ifdef linux +#ifdef __linux FILE *infile; char buffer[512], *p; @@ -176,17 +136,19 @@ static gotoblas_t *get_coretype_from_cpuinfo(void) { return NULL; } #endif - return NULL; + return NULL; } +#endif static gotoblas_t *get_coretype(void) { - int ret = 0; - - ret = cpucfg_test(); - if (ret == 1) - return get_coretype_from_cpucfg(); - else - return get_coretype_from_cpuinfo(); +#if (!defined OS_LINUX && !defined OS_ANDROID) + return NULL; +#else + if (!(getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG)) + return get_coretype_from_cpucfg(); + else + return get_coretype_from_cpuinfo(); +#endif } void gotoblas_dynamic_init(void) {