Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 74d8ea0

Browse files
committed
Merge pull request #1128 from hjelmn/v2_0_0_patcher
v2.0.0 patcher fixes
2 parents 9956922 + f88a9e9 commit 74d8ea0

File tree

12 files changed

+63
-676
lines changed

12 files changed

+63
-676
lines changed

opal/mca/memory/patcher/configure.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ AC_DEFUN([MCA_opal_memory_patcher_CONFIG],[
8484
AC_DEFINE_UNQUOTED([OPAL_MEMORY_PATCHER_HAVE___SYSCALL], [$memory_patcher_have___syscall],
8585
[Whether the internal __syscall call exists])
8686

87+
AC_CHECK_HEADERS([linux/mman.h])
88+
8789
[$1]
8890

8991
OPAL_VAR_SCOPE_POP

opal/mca/memory/patcher/memory_patcher_component.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include <sys/time.h>
4444
#include <sys/syscall.h>
4545

46+
#if defined(HAVE_LINUX_MMAN_H)
47+
#include <linux/mman.h>
48+
#endif
49+
4650
#include "memory_patcher.h"
4751
#undef opal_memory_changed
4852

@@ -162,11 +166,20 @@ static int intercept_munmap(void *start, size_t length)
162166

163167
#if defined (SYS_mremap)
164168

169+
#if defined(__linux__)
165170
/* on linux this function has an optional extra argument but ... can not be used here because it
166171
* causes issues when intercepting a 4-argument mremap call */
167172
static void *(*original_mremap) (void *, size_t, size_t, int, void *);
173+
#else
174+
/* mremap has a different signature on BSD systems */
175+
static void *(*original_mremap) (void *, size_t, void *, size_t, int);
176+
#endif
168177

178+
#if defined(__linux__)
169179
static void *intercept_mremap (void *start, size_t oldlen, size_t newlen, int flags, void *new_address)
180+
#else
181+
static void *intercept_mremap (void *start, size_t oldlen, void *new_address, size_t newlen, int flags)
182+
#endif
170183
{
171184
OPAL_PATCHER_BEGIN;
172185
void *result = MAP_FAILED;
@@ -175,15 +188,25 @@ static void *intercept_mremap (void *start, size_t oldlen, size_t newlen, int fl
175188
opal_mem_hooks_release_hook (start, oldlen, true);
176189
}
177190

191+
#if defined(MREMAP_FIXED)
178192
if (!(flags & MREMAP_FIXED)) {
179193
new_address = NULL;
180194
}
195+
#endif
181196

197+
#if defined(__linux__)
182198
if (!original_mremap) {
183199
result = (void *)(intptr_t) memory_patcher_syscall (SYS_mremap, start, oldlen, newlen, flags, new_address);
184200
} else {
185201
result = original_mremap (start, oldlen, newlen, flags, new_address);
186202
}
203+
#else
204+
if (!original_mremap) {
205+
result = (void *)(intptr_t) memory_patcher_syscall (SYS_mremap, start, oldlen, new_address, newlen, flags);
206+
} else {
207+
result = original_mremap (start, oldlen, new_address, newlen, flags);
208+
}
209+
#endif
187210

188211
OPAL_PATCHER_END;
189212
return result;

opal/mca/patcher/base/base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ OPAL_DECLSPEC int mca_patcher_base_patch_hook (mca_patcher_base_module_t *module
6868
OPAL_DECLSPEC void mca_base_patcher_patch_apply_binary (mca_patcher_base_patch_t *patch);
6969

7070
static inline uintptr_t mca_patcher_base_addr_text (uintptr_t addr) {
71-
#if (defined(__PPC64__) || defined(__powerpc64__) || defined(__PPC__)) && _CALL_ELF != 2
71+
#if (OPAL_ASSEMBLY_ARCH == OPAL_POWERPC64) && (!defined (_CALL_ELF) || (_CALL_ELF != 2))
7272
struct odp_t {
7373
uintptr_t text;
7474
uintptr_t toc;

opal/mca/patcher/base/patcher_base_patch.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,37 @@ static int PatchLoadImm (uintptr_t addr, unsigned int reg, size_t value)
7575

7676
#endif
7777

78-
#if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
79-
80-
static void flush_and_invalidate_cache (unsigned long a)
78+
static void flush_and_invalidate_cache (unsigned long a)
8179
{
82-
#if defined(__i386__)
83-
/* does not work with AMD processors */
84-
__asm__ volatile("mfence;clflush %0;mfence" : :"m" (*(char*)a));
85-
#elif defined(__x86_64__)
80+
#if OPAL_ASSEMBLY_ARCH == OPAL_IA32
81+
static int have_clflush = -1;
82+
83+
if (OPAL_UNLIKELY(-1 == have_clflush)) {
84+
int32_t cpuid1, cpuid2, tmp;
85+
const int32_t level = 1;
86+
87+
/* cpuid clobbers ebx but it must be restored for -fPIC so save
88+
* then restore ebx */
89+
__asm__ volatile ("xchgl %%ebx, %2\n"
90+
"cpuid\n"
91+
"xchgl %%ebx, %2\n":
92+
"=a" (cpuid1), "=d" (cpuid2), "=r" (tmp) :
93+
"a" (level) :
94+
"ecx");
95+
/* clflush is in edx bit 19 */
96+
have_clflush = !!(cpuid2 & (1 << 19));
97+
}
98+
99+
if (have_clflush) {
100+
/* does not work with AMD processors */
101+
__asm__ volatile("mfence;clflush %0;mfence" : :"m" (*(char*)a));
102+
}
103+
#elif OPAL_ASSEMBLY_ARCH == OPAL_AMD64
86104
__asm__ volatile("mfence;clflush %0;mfence" : :"m" (*(char*)a));
87-
#elif defined(__ia64__)
105+
#elif OPAL_ASSEMBLY_ARCH == OPAL_IA64
88106
__asm__ volatile ("fc %0;; sync.i;; srlz.i;;" : : "r"(a) : "memory");
89107
#endif
90108
}
91-
#endif
92109

93110
// modify protection of memory range
94111
static void ModifyMemoryProtection (uintptr_t addr, size_t length, int prot)
@@ -105,7 +122,7 @@ static void ModifyMemoryProtection (uintptr_t addr, size_t length, int prot)
105122
if (mprotect((void *)base, page_size, prot))
106123
perror("MemHook: mprotect failed");
107124
base += page_size;
108-
} while (base < addr + length);
125+
} while (base < bound);
109126
#else
110127
if (mprotect((void *) base, length, prot)) {
111128
perror("MemHook: mprotect failed");
@@ -117,11 +134,9 @@ static inline void apply_patch (unsigned char *patch_data, uintptr_t address, si
117134
{
118135
ModifyMemoryProtection (address, data_size, PROT_EXEC|PROT_READ|PROT_WRITE);
119136
memcpy ((void *) address, patch_data, data_size);
120-
#if defined(__i386__) || defined(__x86_64__) || defined(__ia64__)
121137
for (size_t i = 0 ; i < data_size ; i += 16) {
122138
flush_and_invalidate_cache (address + i);
123139
}
124-
#endif
125140

126141
ModifyMemoryProtection (address, data_size, PROT_EXEC|PROT_READ);
127142
}
@@ -141,26 +156,26 @@ void mca_base_patcher_patch_apply_binary (mca_patcher_base_patch_t *patch)
141156

142157
int mca_patcher_base_patch_hook (mca_patcher_base_module_t *module, uintptr_t hook_addr)
143158
{
144-
#if defined(__PPC64__) || defined(__powerpc64__) || defined(__PPC__)
159+
#if (OPAL_ASSEMBLY_ARCH == OPAL_POWERPC64)
145160
mca_patcher_base_patch_t *hook_patch;
146161
const unsigned int nop = 0x60000000;
147-
unsigned int *nop_addr;
148162

149163
hook_patch = OBJ_NEW(mca_patcher_base_patch_t);
150164
if (OPAL_UNLIKELY(NULL == hook_patch)) {
151165
return OPAL_ERR_OUT_OF_RESOURCE;
152166
}
153167

154168
// locate reserved code space in hook function
155-
for (nop_addr = (unsigned int *)hook_addr ; ; nop_addr++) {
169+
for (unsigned int *nop_addr = (unsigned int *)hook_addr ; ; nop_addr++) {
156170
if (nop_addr[0] == nop && nop_addr[1] == nop && nop_addr[2] == nop
157171
&& nop_addr[3] == nop && nop_addr[4] == nop) {
172+
hook_patch->patch_orig = (uintptr_t) nop_addr;
158173
break;
159174
}
160175
}
176+
161177
// generate code to restore TOC
162178
register unsigned long toc asm("r2");
163-
hook_patch->patch_orig = (uintptr_t) nop_addr;
164179
hook_patch->patch_data_size = PatchLoadImm((uintptr_t)hook_patch->patch_data, 2, toc);
165180

166181
/* put the hook patch on the patch list so it will be undone on finalize */

opal/mca/patcher/linux/Makefile.am

Lines changed: 0 additions & 47 deletions
This file was deleted.

opal/mca/patcher/linux/configure.m4

Lines changed: 0 additions & 53 deletions
This file was deleted.

opal/mca/patcher/linux/patcher_linux.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

opal/mca/patcher/linux/patcher_linux_component.c

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)