annotate liboffloadmic/runtime/offload_util.cpp @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /*
kono
parents:
diff changeset
2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
5 modification, are permitted provided that the following conditions
kono
parents:
diff changeset
6 are met:
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 * Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
9 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
10 * Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
11 notice, this list of conditions and the following disclaimer in the
kono
parents:
diff changeset
12 documentation and/or other materials provided with the distribution.
kono
parents:
diff changeset
13 * Neither the name of Intel Corporation nor the names of its
kono
parents:
diff changeset
14 contributors may be used to endorse or promote products derived
kono
parents:
diff changeset
15 from this software without specific prior written permission.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kono
parents:
diff changeset
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kono
parents:
diff changeset
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kono
parents:
diff changeset
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kono
parents:
diff changeset
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
kono
parents:
diff changeset
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
kono
parents:
diff changeset
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
kono
parents:
diff changeset
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
kono
parents:
diff changeset
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
kono
parents:
diff changeset
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
kono
parents:
diff changeset
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kono
parents:
diff changeset
28 */
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #include "offload_util.h"
kono
parents:
diff changeset
32 #include <errno.h>
kono
parents:
diff changeset
33 #include "liboffload_error_codes.h"
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #ifdef TARGET_WINNT
kono
parents:
diff changeset
36 void *thread_getspecific(pthread_key_t key)
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 if (key == 0) {
kono
parents:
diff changeset
39 return NULL;
kono
parents:
diff changeset
40 }
kono
parents:
diff changeset
41 else {
kono
parents:
diff changeset
42 return TlsGetValue(key);
kono
parents:
diff changeset
43 }
kono
parents:
diff changeset
44 }
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 int thread_setspecific(pthread_key_t key, const void *value)
kono
parents:
diff changeset
47 {
kono
parents:
diff changeset
48 return (TlsSetValue(key, (LPVOID)value)) ? 0 : GetLastError();
kono
parents:
diff changeset
49 }
kono
parents:
diff changeset
50 #endif // TARGET_WINNT
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 bool __offload_parse_size_string(const char *str, uint64_t &new_size)
kono
parents:
diff changeset
53 {
kono
parents:
diff changeset
54 uint64_t val;
kono
parents:
diff changeset
55 char *suffix;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 errno = 0;
kono
parents:
diff changeset
58 #ifdef TARGET_WINNT
kono
parents:
diff changeset
59 val = strtoul(str, &suffix, 10);
kono
parents:
diff changeset
60 #else // TARGET_WINNT
kono
parents:
diff changeset
61 val = strtoull(str, &suffix, 10);
kono
parents:
diff changeset
62 #endif // TARGET_WINNT
kono
parents:
diff changeset
63 if (errno != 0 || suffix == str) {
kono
parents:
diff changeset
64 return false;
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 if (suffix[0] == '\0') {
kono
parents:
diff changeset
68 // default is Kilobytes
kono
parents:
diff changeset
69 new_size = val * 1024;
kono
parents:
diff changeset
70 return true;
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72 else if (suffix[1] == '\0') {
kono
parents:
diff changeset
73 // Optional suffixes: B (bytes), K (Kilobytes), M (Megabytes),
kono
parents:
diff changeset
74 // G (Gigabytes), or T (Terabytes) specify the units.
kono
parents:
diff changeset
75 switch (suffix[0]) {
kono
parents:
diff changeset
76 case 'b':
kono
parents:
diff changeset
77 case 'B':
kono
parents:
diff changeset
78 new_size = val;
kono
parents:
diff changeset
79 break;
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 case 'k':
kono
parents:
diff changeset
82 case 'K':
kono
parents:
diff changeset
83 new_size = val * 1024;
kono
parents:
diff changeset
84 break;
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 case 'm':
kono
parents:
diff changeset
87 case 'M':
kono
parents:
diff changeset
88 new_size = val * 1024 * 1024;
kono
parents:
diff changeset
89 break;
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 case 'g':
kono
parents:
diff changeset
92 case 'G':
kono
parents:
diff changeset
93 new_size = val * 1024 * 1024 * 1024;
kono
parents:
diff changeset
94 break;
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 case 't':
kono
parents:
diff changeset
97 case 'T':
kono
parents:
diff changeset
98 new_size = val * 1024 * 1024 * 1024 * 1024;
kono
parents:
diff changeset
99 break;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 default:
kono
parents:
diff changeset
102 return false;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104 return true;
kono
parents:
diff changeset
105 }
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 return false;
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 bool __offload_parse_int_string(const char *str, int64_t &value)
kono
parents:
diff changeset
111 {
kono
parents:
diff changeset
112 int64_t val;
kono
parents:
diff changeset
113 char *suffix;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 errno = 0;
kono
parents:
diff changeset
116 #ifdef TARGET_WINNT
kono
parents:
diff changeset
117 val = strtol(str, &suffix, 0);
kono
parents:
diff changeset
118 #else
kono
parents:
diff changeset
119 val = strtoll(str, &suffix, 0);
kono
parents:
diff changeset
120 #endif
kono
parents:
diff changeset
121 if (errno == 0 && suffix != str && *suffix == '\0') {
kono
parents:
diff changeset
122 value = val;
kono
parents:
diff changeset
123 return true;
kono
parents:
diff changeset
124 }
kono
parents:
diff changeset
125 return false;
kono
parents:
diff changeset
126 }
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 #ifdef TARGET_WINNT
kono
parents:
diff changeset
129 extern void* DL_open(const char *path)
kono
parents:
diff changeset
130 {
kono
parents:
diff changeset
131 void *handle;
kono
parents:
diff changeset
132 int error_mode;
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 /*
kono
parents:
diff changeset
135 * do not display message box with error if it the call below fails to
kono
parents:
diff changeset
136 * load dynamic library.
kono
parents:
diff changeset
137 */
kono
parents:
diff changeset
138 error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* load dynamic library */
kono
parents:
diff changeset
141 handle = (void*) LoadLibrary(path);
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 /* restore error mode */
kono
parents:
diff changeset
144 SetErrorMode(error_mode);
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 return handle;
kono
parents:
diff changeset
147 }
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 extern int DL_addr(const void *addr, Dl_info *dl_info)
kono
parents:
diff changeset
150 {
kono
parents:
diff changeset
151 MEMORY_BASIC_INFORMATION mem_info;
kono
parents:
diff changeset
152 char mod_name[MAX_PATH];
kono
parents:
diff changeset
153 HMODULE mod_handle;
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 /* Fill MEMORY_BASIC_INFORMATION struct */
kono
parents:
diff changeset
156 if (!VirtualQuery(addr, &mem_info, sizeof(mem_info))) {
kono
parents:
diff changeset
157 return 0;
kono
parents:
diff changeset
158 }
kono
parents:
diff changeset
159 mod_handle = (HMODULE)mem_info.AllocationBase;
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 /* ANSI file name for module */
kono
parents:
diff changeset
162 if (!GetModuleFileNameA(mod_handle, (char*) mod_name, sizeof(mod_name))) {
kono
parents:
diff changeset
163 return 0;
kono
parents:
diff changeset
164 }
kono
parents:
diff changeset
165 strcpy(dl_info->dli_fname, mod_name);
kono
parents:
diff changeset
166 dl_info->dli_fbase = mem_info.BaseAddress;
kono
parents:
diff changeset
167 dl_info->dli_saddr = addr;
kono
parents:
diff changeset
168 strcpy(dl_info->dli_sname, mod_name);
kono
parents:
diff changeset
169 return 1;
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 // Run once
kono
parents:
diff changeset
173 static BOOL CALLBACK __offload_run_once_wrapper(
kono
parents:
diff changeset
174 PINIT_ONCE initOnce,
kono
parents:
diff changeset
175 PVOID parameter,
kono
parents:
diff changeset
176 PVOID *context
kono
parents:
diff changeset
177 )
kono
parents:
diff changeset
178 {
kono
parents:
diff changeset
179 void (*init_routine)(void) = (void(*)(void)) parameter;
kono
parents:
diff changeset
180 init_routine();
kono
parents:
diff changeset
181 return true;
kono
parents:
diff changeset
182 }
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void))
kono
parents:
diff changeset
185 {
kono
parents:
diff changeset
186 InitOnceExecuteOnce(ctrl, __offload_run_once_wrapper, (void*) func, 0);
kono
parents:
diff changeset
187 }
kono
parents:
diff changeset
188 #endif // TARGET_WINNT
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 /* ARGSUSED */ // version is not used on windows
kono
parents:
diff changeset
191 void* DL_sym(void *handle, const char *name, const char *version)
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 #ifdef TARGET_WINNT
kono
parents:
diff changeset
194 return GetProcAddress((HMODULE) handle, name);
kono
parents:
diff changeset
195 #else // TARGET_WINNT
kono
parents:
diff changeset
196 if (version == 0) {
kono
parents:
diff changeset
197 return dlsym(handle, name);
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199 else {
kono
parents:
diff changeset
200 return dlvsym(handle, name, version);
kono
parents:
diff changeset
201 }
kono
parents:
diff changeset
202 #endif // TARGET_WINNT
kono
parents:
diff changeset
203 }
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 int64_t get_el_value(
kono
parents:
diff changeset
206 char *base,
kono
parents:
diff changeset
207 int64_t offset,
kono
parents:
diff changeset
208 int64_t size)
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 int64_t val = 0;
kono
parents:
diff changeset
211 switch (size) {
kono
parents:
diff changeset
212 case 1:
kono
parents:
diff changeset
213 val = static_cast<int64_t>(*((char *)(base + offset)));
kono
parents:
diff changeset
214 break;
kono
parents:
diff changeset
215 case 2:
kono
parents:
diff changeset
216 val = static_cast<int64_t>(*((short *)(base + offset)));
kono
parents:
diff changeset
217 break;
kono
parents:
diff changeset
218 case 4:
kono
parents:
diff changeset
219 val = static_cast<int64_t>(*((int *)(base + offset)));
kono
parents:
diff changeset
220 break;
kono
parents:
diff changeset
221 default:
kono
parents:
diff changeset
222 val = *((int64_t *)(base + offset));
kono
parents:
diff changeset
223 break;
kono
parents:
diff changeset
224 }
kono
parents:
diff changeset
225 return val;
kono
parents:
diff changeset
226 }