-
Notifications
You must be signed in to change notification settings - Fork 392
/
omrcpu.c
183 lines (159 loc) · 4.98 KB
/
omrcpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*******************************************************************************
* Copyright (c) 1991, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
/**
* @file
* @ingroup Port
* @brief CPU Control.
*
* Functions setting CPU attributes.
*/
#include <stdlib.h>
#if defined(RS6000) || defined (LINUXPPC) || defined (PPC)
#include <string.h>
#endif
#include "omrport.h"
#if defined(RS6000) || defined (LINUXPPC) || defined (PPC)
#include "omrportpriv.h"
#include "omrportpg.h"
#endif
#if defined(OSX)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
/**
* PortLibrary startup.
*
* This function is called during startup of the portLibrary. Any resources that are required for
* the exit operations may be created here. All resources created here should be destroyed
* in @ref omrcpu_shutdown.
*
* @param[in] portLibrary The port library
*
* @return 0 on success, negative error code on failure. Error code values returned are
* \arg OMRPORT_ERROR_STARTUP_CPU
*
* @note Most implementations will simply return success.
*/
int32_t
omrcpu_startup(struct OMRPortLibrary *portLibrary)
{
/* initialize the ppc level 1 cache line size */
#if defined(RS6000) || defined (LINUXPPC) || defined (PPC)
int32_t ppcCacheLineSize;
int i;
char buf[1024];
memset(buf, 255, 1024);
__asm__(
"dcbz 0, %0"
: /* no outputs */
:"r"((void *) &buf[512]));
for (i = 0, ppcCacheLineSize = 0; i < 1024; i++) {
if (buf[i] == 0) {
ppcCacheLineSize++;
}
}
PPG_mem_ppcCacheLineSize = ppcCacheLineSize;
#endif
return 0;
}
/**
* PortLibrary shutdown.
*
* This function is called during shutdown of the portLibrary. Any resources that were created by @ref omrcpu_startup
* should be destroyed here.
*
* @param[in] portLibrary The port library
*
* @note Most implementations will be empty.
*/
void
omrcpu_shutdown(struct OMRPortLibrary *portLibrary)
{
}
/**
* @brief CPU Control operations.
*
* Flush the instruction cache to memory.
*
* @param[in] portLibrary The port library
* @param[in] memoryPointer The base address of memory to flush.
* @param[in] byteAmount Number of bytes to flush.
*/
void
omrcpu_flush_icache(struct OMRPortLibrary *portLibrary, void *memoryPointer, uintptr_t byteAmount)
{
#if defined(RS6000) || defined (LINUXPPC) || defined (PPC)
int32_t cacheLineSize = PPG_mem_ppcCacheLineSize;
unsigned char *addr;
unsigned char *limit;
limit = (unsigned char *)(((unsigned long)memoryPointer + (unsigned int)byteAmount + (cacheLineSize - 1))
/ cacheLineSize * cacheLineSize);
/* for each cache line, do a data cache block flush */
for (addr = (unsigned char *)memoryPointer ; addr < limit; addr += cacheLineSize) {
__asm__(
"dcbst 0,%0"
: /* no outputs */
: "r"(addr));
}
__asm__("sync");
/* for each cache line do an icache block invalidate */
for (addr = (unsigned char *)memoryPointer; addr < limit; addr += cacheLineSize) {
__asm__(
"icbi 0,%0"
: /* no outputs */
: "r"(addr));
}
__asm__("sync");
__asm__("isync");
#elif defined(ARM) || defined(AARCH64) /* defined(RS6000) || defined(LINUXPPC) || defined(PPC) */
#if defined(__GNUC__)
// GCC built-in function
__builtin___clear_cache(memoryPointer, (void *)((char *)memoryPointer+byteAmount));
#else
#error Not supported
#endif /* defined(__GNUC__) */
#endif /* defined(ARM) || defined(AARCH64) */
}
int32_t
omrcpu_get_cache_line_size(struct OMRPortLibrary *portLibrary, int32_t *lineSize)
{
int32_t rc = OMRPORT_ERROR_NOT_SUPPORTED_ON_THIS_PLATFORM;
if (NULL == lineSize) {
rc = OMRPORT_ERROR_INVALID_ARGUMENTS;
} else {
#if defined(RS6000) || defined (LINUXPPC) || defined (PPC)
*lineSize = PPG_mem_ppcCacheLineSize;
rc = 0;
#elif defined (OSX)
int queryPath[] = {CTL_HW, HW_CACHELINE};
int64_t result = 0;
size_t resultSize = sizeof(result);
if (0 != sysctl(queryPath, 2, &result, &resultSize, NULL, 0)) {
rc = OMRPORT_ERROR_SYSINFO_OPFAILED;
} else {
*lineSize = (int32_t) result;
rc = 0;
}
#endif /* defined(OSX) */
}
return rc;
}