|
91 | 91 | #define HAVE_KVREALLOC |
92 | 92 | #endif |
93 | 93 |
|
| 94 | + #ifdef WOLFCRYPT_ONLY |
| 95 | + #ifdef HAVE_KVMALLOC |
| 96 | + #define USE_KVMALLOC |
| 97 | + #endif |
| 98 | + #ifdef HAVE_KVREALLOC |
| 99 | + #define USE_KVREALLOC |
| 100 | + #endif |
| 101 | + #else |
| 102 | + /* functioning realloc() is needed for the TLS stack. */ |
| 103 | + #if defined(HAVE_KVMALLOC) && defined(HAVE_KVREALLOC) |
| 104 | + #define USE_KVMALLOC |
| 105 | + #define USE_KVREALLOC |
| 106 | + #endif |
| 107 | + #endif |
| 108 | + |
94 | 109 | /* kernel printf doesn't implement fp. */ |
95 | 110 | #ifndef WOLFSSL_NO_FLOAT_FMT |
96 | 111 | #define WOLFSSL_NO_FLOAT_FMT |
|
273 | 288 |
|
274 | 289 | #endif /* !CONFIG_FORTIFY_SOURCE */ |
275 | 290 |
|
| 291 | +#if defined(__PIE__) && (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)) && \ |
| 292 | + defined(CONFIG_X86) |
| 293 | + /* linux/slab.h will recursively bring in linux/page-flags.h, polluting the |
| 294 | + * wolfCrypt container objects with static functions const_folio_flags() and |
| 295 | + * folio_flags(), unless we kludge it off thusly. |
| 296 | + */ |
| 297 | + #define PAGE_FLAGS_H |
| 298 | +#endif |
| 299 | + |
| 300 | + #include <linux/init.h> |
| 301 | + #include <linux/module.h> |
| 302 | + #include <linux/delay.h> |
| 303 | + |
| 304 | + #ifdef __PIE__ |
| 305 | + /* without this, mm.h brings in static, but not inline, pmd_to_page(), |
| 306 | + * with direct references to global vmem variables. |
| 307 | + */ |
| 308 | + #undef USE_SPLIT_PMD_PTLOCKS |
| 309 | + #define USE_SPLIT_PMD_PTLOCKS 0 |
| 310 | + |
| 311 | + #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) |
| 312 | + /* without this, static show_free_areas() mm.h brings in direct |
| 313 | + * reference to unexported __show_free_areas(). |
| 314 | + */ |
| 315 | + #define __show_free_areas my__show_free_areas |
| 316 | + void my__show_free_areas( |
| 317 | + unsigned int flags, |
| 318 | + nodemask_t *nodemask, |
| 319 | + int max_zone_idx); |
| 320 | + #endif |
| 321 | + #endif |
| 322 | + |
| 323 | +#if !defined(__PIE__) || (LINUX_VERSION_CODE < KERNEL_VERSION(6, 1, 0)) |
| 324 | + #include <linux/mm.h> |
| 325 | +#endif |
| 326 | + |
| 327 | + #ifndef SINGLE_THREADED |
| 328 | + #include <linux/kthread.h> |
| 329 | + #endif |
| 330 | +#ifndef __PIE__ |
| 331 | + #include <linux/net.h> |
| 332 | +#endif |
| 333 | + #include <linux/slab.h> |
| 334 | + #include <linux/sched.h> |
| 335 | + #include <linux/random.h> |
| 336 | + |
276 | 337 | #ifdef LINUXKM_LKCAPI_REGISTER |
277 | 338 | /* the LKCAPI assumes that expanded encrypt and decrypt keys will stay |
278 | 339 | * loaded simultaneously, and the Linux in-tree implementations have two |
|
290 | 351 | #ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS |
291 | 352 | #define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS |
292 | 353 | #endif |
293 | | - #endif /* LINUXKM_LKCAPI_REGISTER */ |
294 | 354 |
|
295 | | - #include <linux/init.h> |
296 | | -#ifndef __PIE__ |
297 | | - #include <linux/module.h> |
298 | | - #include <linux/delay.h> |
299 | | -#endif |
300 | | - |
301 | | -#ifdef __PIE__ |
302 | | - /* linux/slab.h will recursively bring in linux/page-flags.h, polluting the |
303 | | - * wolfCrypt container objects with static functions const_folio_flags() and |
304 | | - * folio_flags(), unless we kludge it off thusly. |
305 | | - */ |
306 | | - #define PAGE_FLAGS_H |
307 | | -#else |
308 | | - #include <linux/mm.h> |
309 | | -#endif |
310 | | - |
311 | | - #include <linux/slab.h> |
312 | | - #include <linux/sched.h> |
313 | | - |
314 | | -#ifndef __PIE__ |
315 | | - #ifndef SINGLE_THREADED |
316 | | - #include <linux/kthread.h> |
317 | | - #endif |
318 | | - #include <linux/net.h> |
319 | | - |
320 | | - #ifdef LINUXKM_LKCAPI_REGISTER |
321 | | - #include <linux/crypto.h> |
322 | | - #include <linux/scatterlist.h> |
323 | | - #include <crypto/scatterwalk.h> |
324 | | - #include <crypto/internal/aead.h> |
325 | | - #include <crypto/internal/hash.h> |
326 | | - #include <crypto/internal/rng.h> |
327 | | - #include <crypto/internal/skcipher.h> |
328 | | - #include <crypto/internal/akcipher.h> |
329 | | - #include <crypto/internal/kpp.h> |
330 | | - #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0) |
331 | | - #include <crypto/internal/sig.h> |
332 | | - #endif /* linux ver >= 6.13 */ |
333 | | - #ifdef WOLFSSL_LINUXKM_USE_GET_RANDOM_KPROBES |
334 | | - #include <linux/kprobes.h> |
335 | | - #endif |
| 355 | + #ifndef __PIE__ |
| 356 | + #include <linux/crypto.h> |
| 357 | + #include <linux/scatterlist.h> |
| 358 | + #include <crypto/scatterwalk.h> |
| 359 | + #include <crypto/internal/aead.h> |
| 360 | + #include <crypto/internal/hash.h> |
| 361 | + #include <crypto/internal/rng.h> |
| 362 | + #include <crypto/internal/skcipher.h> |
| 363 | + #include <crypto/internal/akcipher.h> |
| 364 | + #include <crypto/internal/kpp.h> |
| 365 | + #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 13, 0) |
| 366 | + #include <crypto/internal/sig.h> |
| 367 | + #endif /* linux ver >= 6.13 */ |
| 368 | + #ifdef WOLFSSL_LINUXKM_USE_GET_RANDOM_KPROBES |
| 369 | + #include <linux/kprobes.h> |
| 370 | + #endif |
336 | 371 |
|
337 | | - #if defined(_LINUX_REFCOUNT_H) || defined(_LINUX_REFCOUNT_TYPES_H) |
338 | | - #define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount.refs))) |
339 | | - #else |
340 | | - #define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount))) |
341 | | - #endif |
342 | | - #endif |
343 | | -#endif /* !__PIE__ */ |
| 372 | + #if defined(_LINUX_REFCOUNT_H) || defined(_LINUX_REFCOUNT_TYPES_H) |
| 373 | + #define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount.refs))) |
| 374 | + #else |
| 375 | + #define WC_LKM_REFCOUNT_TO_INT(refcount) (atomic_read(&(refcount))) |
| 376 | + #endif |
| 377 | + #endif /* !__PIE__ */ |
| 378 | + #endif /* LINUXKM_LKCAPI_REGISTER */ |
344 | 379 |
|
345 | 380 | #if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || \ |
346 | 381 | defined(WOLFSSL_SP_X86_64_ASM) |
|
1171 | 1206 | */ |
1172 | 1207 | #define _MM_MALLOC_H_INCLUDED |
1173 | 1208 |
|
| 1209 | + #ifndef BUILDING_WOLFSSL |
| 1210 | + #include <linux/slab.h> |
| 1211 | + #if defined(USE_KVMALLOC) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 16, 0)) |
| 1212 | + #include <linux/mm.h> |
| 1213 | + #endif |
| 1214 | + #endif |
| 1215 | + |
1174 | 1216 | /* fun fact: since linux commit 59bb47985c, kmalloc with power-of-2 size is |
1175 | 1217 | * aligned to the size. |
1176 | 1218 | */ |
|
1182 | 1224 | ((sizeof(_alloc_sz) * 8UL) - __builtin_clzl(_alloc_sz - 1)); \ |
1183 | 1225 | _alloc_sz; \ |
1184 | 1226 | }) |
1185 | | - #ifdef HAVE_KVMALLOC |
| 1227 | + #ifdef USE_KVMALLOC |
1186 | 1228 | #define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC), NUMA_NO_NODE) |
1187 | 1229 | #define free(ptr) kvfree(ptr) |
1188 | | - #ifdef HAVE_KVREALLOC |
| 1230 | + #ifdef USE_KVREALLOC |
1189 | 1231 | #define realloc(ptr, newsize) kvrealloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize), (preempt_count() == 0 ? GFP_KERNEL : GFP_ATOMIC)) |
1190 | 1232 | #else |
1191 | 1233 | #define realloc(ptr, newsize) ((void)(ptr), (void)(newsize), NULL) |
|
1212 | 1254 | #endif |
1213 | 1255 | #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); wolfSSL_Realloc(p, n);}) |
1214 | 1256 | #else |
1215 | | - #define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);}) |
1216 | | - #ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK |
1217 | | - #define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);}) |
1218 | | - #else |
1219 | | - #define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);}) |
1220 | | - #endif |
1221 | | - #if defined(HAVE_KVREALLOC) || !defined(HAVE_KVMALLOC) |
1222 | | - #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);}) |
1223 | | - #endif |
| 1257 | + #if !defined(XMALLOC_USER) && !defined(XMALLOC_OVERRIDE) |
| 1258 | + #define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);}) |
| 1259 | + #ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK |
| 1260 | + #define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);}) |
| 1261 | + #else |
| 1262 | + #define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);}) |
| 1263 | + #endif |
| 1264 | + #if defined(USE_KVREALLOC) || !defined(USE_KVMALLOC) |
| 1265 | + #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);}) |
| 1266 | + #endif |
| 1267 | + #endif /* !XMALLOC_USER && !XMALLOC_OVERRIDE */ |
1224 | 1268 | #endif |
1225 | 1269 |
|
1226 | 1270 | #include <linux/limits.h> |
|
0 commit comments