Debian Codegraph @

js/src/jit/mips-shared/Architecture-mips-shared.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/mips-shared/Architecture-mips-shared.h"
8
9 #include <fcntl.h>
10 #include <unistd.h>
11
12 #include "jit/FlushICache.h" // js::jit::FlushICache
13 #include "jit/mips64/Simulator-mips64.h"
14 #include "jit/RegisterSets.h"
15
16 #if defined(__linux__) && !defined(JS_SIMULATOR)
17 # include <sys/cachectl.h>
18 #endif
19
20 #define HWCAP_MIPS (1 << 28)
21 #define HWCAP_LOONGSON (1 << 27)
22 #define HWCAP_R2 (1 << 26)
23 #define HWCAP_FPU (1 << 0)
24
25 namespace js {
26 namespace jit {
27
28 static uint32_t get_mips_flags() {
29 uint32_t flags = HWCAP_MIPS;
30
31 #if defined(JS_SIMULATOR_MIPS64)
32 flags |= HWCAP_FPU;
33 flags |= HWCAP_R2;
34 #else
35 # ifdef __linux__
36 FILE* fp = fopen("/proc/cpuinfo", "r");
37 if (fp) {
38 char buf[1024] = {};
39 size_t len = fread(buf, sizeof(char), sizeof(buf) - 1, fp);
40 fclose(fp);
41 buf[len] = 0;
42 if (strstr(buf, "FPU")) {
43 flags |= HWCAP_FPU;
44 }
45 if (strstr(buf, "Loongson")) {
46 flags |= HWCAP_LOONGSON;
47 }
48 if (strstr(buf, "mips32r2") || strstr(buf, "mips64r2")) {
49 flags |= HWCAP_R2;
50 }
51 }
52 # endif
53 #endif // JS_SIMULATOR_MIPS64
54 return flags;
55 }
56
57 static bool check_fpu() { return mips_private::Flags & HWCAP_FPU; }
58
59 static bool check_loongson() { return mips_private::Flags & HWCAP_LOONGSON; }
60
61 static bool check_r2() { return mips_private::Flags & HWCAP_R2; }
62
63 namespace mips_private {
64 // Cache a local copy so we only have to read /proc/cpuinfo once.
65 uint32_t Flags = get_mips_flags();
66 bool hasFPU = check_fpu();
67 ;
68 bool isLoongson = check_loongson();
69 bool hasR2 = check_r2();
70 } // namespace mips_private
71
72 bool CPUFlagsHaveBeenComputed() {
73 // Flags were computed above.
74 return true;
75 }
76
77 Registers::Code Registers::FromName(const char* name) {
78 for (size_t i = 0; i < Total; i++) {
79 if (strcmp(GetName(i), name) == 0) {
80 return Code(i);
81 }
82 }
83
84 return Invalid;
85 }
86
87 void FlushICache(void* code, size_t size) {
88 #if defined(JS_SIMULATOR)
89 js::jit::SimulatorProcess::FlushICache(code, size);
90
91 #elif defined(_MIPS_ARCH_LOONGSON3A)
92 // On Loongson3-CPUs, The cache flushed automatically
93 // by hardware. Just need to execute an instruction hazard.
94 uintptr_t tmp;
95 asm volatile(
96 ".set push \n"
97 ".set noreorder \n"
98 "move %[tmp], $ra \n"
99 "bal 1f \n"
100 "daddiu $ra, 8 \n"
101 "1: \n"
102 "jr.hb $ra \n"
103 "move $ra, %[tmp] \n"
104 ".set pop\n"
105 : [tmp] "=&r"(tmp));
106
107 #elif defined(__GNUC__)
108 intptr_t end = reinterpret_cast<intptr_t>(code) + size;
109 __builtin___clear_cache(reinterpret_cast<char*>(code),
110 reinterpret_cast<char*>(end));
111
112 #else
113 _flush_cache(reinterpret_cast<char*>(code), size, BCACHE);
114
115 #endif
116 }
117
118 } // namespace jit
119 } // namespace js