Skip to content

Commit 19d4f90

Browse files
committed
Use auvx to detect CPUCFG on mips/loongson
It's safer and easier than SIGILL. Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
1 parent ef9c976 commit 19d4f90

File tree

1 file changed

+21
-59
lines changed

1 file changed

+21
-59
lines changed

driver/others/dynamic_mips64.c

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
#include <sys/resource.h>
3939
#include "common.h"
4040

41+
#if (defined OS_LINUX || defined OS_ANDROID)
42+
#include <asm/hwcap.h>
43+
#include <sys/auxv.h>
44+
45+
#ifndef HWCAP_LOONGSON_CPUCFG
46+
#define HWCAP_LOONGSON_CPUCFG (1 << 14)
47+
#endif
48+
#endif
49+
4150
extern gotoblas_t gotoblas_LOONGSON3R3;
4251
extern gotoblas_t gotoblas_LOONGSON3R4;
4352

@@ -81,59 +90,10 @@ static gotoblas_t *force_coretype(char *coretype) {
8190
return NULL;
8291
}
8392

93+
#if (defined OS_LINUX || defined OS_ANDROID)
8494
#define MMI_MASK 0x00000010
8595
#define MSA_MASK 0x00000020
8696

87-
int fd[2];
88-
int support_cpucfg;
89-
90-
static void handler(int signum)
91-
{
92-
close(fd[1]);
93-
exit(1);
94-
}
95-
96-
/* Brief : Function to check if cpucfg supported on loongson
97-
* Return: 1 supported
98-
* 0 not supported
99-
*/
100-
static int cpucfg_test(void) {
101-
pid_t pid;
102-
int status = 0;
103-
104-
support_cpucfg = 0;
105-
pipe(fd);
106-
pid = fork();
107-
if (pid == 0) { /* Subprocess */
108-
struct sigaction act;
109-
close(fd[0]);
110-
/* Set signal action for SIGILL. */
111-
act.sa_handler = handler;
112-
sigaction(SIGILL,&act,NULL);
113-
114-
/* Execute cpucfg in subprocess. */
115-
__asm__ volatile(
116-
".insn \n\t"
117-
".word (0xc8080118) \n\t"
118-
:::
119-
);
120-
support_cpucfg = 1;
121-
write(fd[1],&support_cpucfg,sizeof(support_cpucfg));
122-
close(fd[1]);
123-
exit(0);
124-
} else if (pid > 0){ /* Parent process*/
125-
close(fd[1]);
126-
if ((waitpid(pid,&status,0) <= 0) ||
127-
(read(fd[0],&support_cpucfg,sizeof(support_cpucfg)) <= 0))
128-
support_cpucfg = 0;
129-
close(fd[0]);
130-
} else {
131-
support_cpucfg = 0;
132-
}
133-
134-
return support_cpucfg;
135-
}
136-
13797
static gotoblas_t *get_coretype_from_cpucfg(void) {
13898
int flag = 0;
13999
__asm__ volatile(
@@ -153,7 +113,7 @@ static gotoblas_t *get_coretype_from_cpucfg(void) {
153113
}
154114

155115
static gotoblas_t *get_coretype_from_cpuinfo(void) {
156-
#ifdef linux
116+
#ifdef __linux
157117
FILE *infile;
158118
char buffer[512], *p;
159119

@@ -176,17 +136,19 @@ static gotoblas_t *get_coretype_from_cpuinfo(void) {
176136
return NULL;
177137
}
178138
#endif
179-
return NULL;
139+
return NULL;
180140
}
141+
#endif
181142

182143
static gotoblas_t *get_coretype(void) {
183-
int ret = 0;
184-
185-
ret = cpucfg_test();
186-
if (ret == 1)
187-
return get_coretype_from_cpucfg();
188-
else
189-
return get_coretype_from_cpuinfo();
144+
#if (!defined OS_LINUX && !defined OS_ANDROID)
145+
return NULL;
146+
#else
147+
if (!(getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG))
148+
return get_coretype_from_cpucfg();
149+
else
150+
return get_coretype_from_cpuinfo();
151+
#endif
190152
}
191153

192154
void gotoblas_dynamic_init(void) {

0 commit comments

Comments
 (0)