2
0

linuxkm_wc_port.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* linuxkm_wc_port.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* included by wolfssl/wolfcrypt/wc_port.h */
  22. #ifndef LINUXKM_WC_PORT_H
  23. #define LINUXKM_WC_PORT_H
  24. #include <linux/version.h>
  25. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)
  26. #error Unsupported kernel.
  27. #endif
  28. #ifdef HAVE_CONFIG_H
  29. #ifndef PACKAGE_NAME
  30. #error wc_port.h included before config.h
  31. #endif
  32. /* config.h is autogenerated without gating, and is subject to repeat
  33. * inclusions, so gate it out here to keep autodetection masking
  34. * intact:
  35. */
  36. #undef HAVE_CONFIG_H
  37. #endif
  38. /* suppress inclusion of stdint-gcc.h to avoid conflicts with Linux native
  39. * include/linux/types.h:
  40. */
  41. #define _GCC_STDINT_H
  42. #define WC_PTR_TYPE uintptr_t
  43. /* needed to suppress inclusion of stdio.h in wolfssl/wolfcrypt/types.h */
  44. #define XSNPRINTF snprintf
  45. /* the rigmarole around kstrtoll() here is to accommodate its
  46. * warn-unused-result attribute.
  47. *
  48. * also needed to suppress inclusion of stdlib.h in
  49. * wolfssl/wolfcrypt/types.h.
  50. */
  51. #define XATOI(s) ({ \
  52. long long _xatoi_res = 0; \
  53. int _xatoi_ret = kstrtoll(s, 10, &_xatoi_res); \
  54. if (_xatoi_ret != 0) { \
  55. _xatoi_res = 0; \
  56. } \
  57. (int)_xatoi_res; \
  58. })
  59. /* Kbuild+gcc on x86 doesn't consistently honor the default ALIGN16 on stack objects,
  60. * but gives adequate alignment with "32".
  61. */
  62. #if defined(CONFIG_X86) && !defined(ALIGN16)
  63. #define ALIGN16 __attribute__ ( (aligned (32)))
  64. #endif
  65. /* kvmalloc()/kvfree() and friends added in linux commit a7c3e901 */
  66. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  67. #define HAVE_KVMALLOC
  68. #endif
  69. /* kernel printf doesn't implement fp. */
  70. #ifndef WOLFSSL_NO_FLOAT_FMT
  71. #define WOLFSSL_NO_FLOAT_FMT
  72. #endif
  73. #ifdef BUILDING_WOLFSSL
  74. #if defined(CONFIG_MIPS) && defined(HAVE_LINUXKM_PIE_SUPPORT)
  75. /* __ZBOOT__ disables some unhelpful macros around the mem*() funcs in
  76. * legacy arch/mips/include/asm/string.h
  77. */
  78. #define __ZBOOT__
  79. #define memcmp __builtin_memcmp
  80. #define __ARCH_MEMCMP_NO_REDIRECT
  81. #define __ARCH_MEMCPY_NO_REDIRECT
  82. #define __builtin_memcpy memcpy
  83. extern void *memcpy(void *dest, const void *src, unsigned int n);
  84. #define __ARCH_MEMCPY_NO_REDIRECT
  85. #define __builtin_memset memset
  86. extern void *memset(void *dest, int c, unsigned int n);
  87. #endif
  88. _Pragma("GCC diagnostic push");
  89. /* we include all the needed kernel headers with these masked out. else
  90. * there are profuse warnings.
  91. */
  92. _Pragma("GCC diagnostic ignored \"-Wunused-parameter\"");
  93. _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"");
  94. _Pragma("GCC diagnostic ignored \"-Wshadow\"");
  95. _Pragma("GCC diagnostic ignored \"-Wnested-externs\"");
  96. _Pragma("GCC diagnostic ignored \"-Wredundant-decls\"");
  97. _Pragma("GCC diagnostic ignored \"-Wsign-compare\"");
  98. _Pragma("GCC diagnostic ignored \"-Wpointer-sign\"");
  99. _Pragma("GCC diagnostic ignored \"-Wbad-function-cast\"");
  100. _Pragma("GCC diagnostic ignored \"-Wdiscarded-qualifiers\"");
  101. _Pragma("GCC diagnostic ignored \"-Wtype-limits\"");
  102. _Pragma("GCC diagnostic ignored \"-Wswitch-enum\"");
  103. #include <linux/kconfig.h>
  104. #include <linux/kernel.h>
  105. #include <linux/ctype.h>
  106. #include <linux/init.h>
  107. #include <linux/module.h>
  108. #ifdef __PIE__
  109. /* without this, mm.h brings in static, but not inline, pmd_to_page(),
  110. * with direct references to global vmem variables.
  111. */
  112. #undef USE_SPLIT_PMD_PTLOCKS
  113. #define USE_SPLIT_PMD_PTLOCKS 0
  114. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  115. /* without this, static show_free_areas() mm.h brings in direct
  116. * reference to unexported __show_free_areas().
  117. */
  118. #define __show_free_areas my__show_free_areas
  119. void my__show_free_areas(
  120. unsigned int flags,
  121. nodemask_t *nodemask,
  122. int max_zone_idx);
  123. #endif
  124. #endif
  125. #include <linux/mm.h>
  126. #ifndef SINGLE_THREADED
  127. #include <linux/kthread.h>
  128. #endif
  129. #include <linux/net.h>
  130. #include <linux/slab.h>
  131. #if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || defined(WOLFSSL_SP_X86_64_ASM)
  132. #ifndef CONFIG_X86
  133. #error X86 SIMD extensions requested, but CONFIG_X86 is not set.
  134. #endif
  135. #define WOLFSSL_LINUXKM_SIMD
  136. #define WOLFSSL_LINUXKM_SIMD_X86
  137. #ifndef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  138. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  139. #endif
  140. #elif defined(WOLFSSL_ARMASM) || defined(WOLFSSL_SP_ARM32_ASM) || \
  141. defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM_THUMB_ASM) ||\
  142. defined(WOLFSSL_SP_ARM_CORTEX_M_ASM)
  143. #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  144. #error ARM SIMD extensions requested, but CONFIG_ARM* is not set.
  145. #endif
  146. #define WOLFSSL_LINUXKM_SIMD
  147. #define WOLFSSL_LINUXKM_SIMD_ARM
  148. #ifndef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  149. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  150. #endif
  151. #else
  152. #ifndef WOLFSSL_NO_ASM
  153. #define WOLFSSL_NO_ASM
  154. #endif
  155. #endif
  156. /* benchmarks.c uses floating point math, so needs a working SAVE_VECTOR_REGISTERS(). */
  157. #if defined(WOLFSSL_LINUXKM_BENCHMARKS) && !defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  158. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  159. #endif
  160. #if defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && defined(CONFIG_X86)
  161. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  162. #include <asm/i387.h>
  163. #else
  164. #include <asm/simd.h>
  165. #endif
  166. #ifndef SAVE_VECTOR_REGISTERS
  167. #define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_x86(); if (_svr_ret != 0) { fail_clause } }
  168. #define SAVE_VECTOR_REGISTERS2() save_vector_registers_x86()
  169. #endif
  170. #ifndef RESTORE_VECTOR_REGISTERS
  171. #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_x86()
  172. #endif
  173. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
  174. #include <asm/fpsimd.h>
  175. #ifndef SAVE_VECTOR_REGISTERS
  176. #define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_arm(); if (_svr_ret != 0) { fail_clause } }
  177. #define SAVE_VECTOR_REGISTERS2() save_vector_registers_arm()
  178. #endif
  179. #ifndef RESTORE_VECTOR_REGISTERS
  180. #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_arm()
  181. #endif
  182. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  183. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  184. #endif
  185. _Pragma("GCC diagnostic pop");
  186. /* avoid -Wpointer-arith, encountered when -DCONFIG_FORTIFY_SOURCE */
  187. #undef __is_constexpr
  188. #define __is_constexpr(x) __builtin_constant_p(x)
  189. /* the kernel uses -std=c89, but not -pedantic, and makes full use of anon
  190. * structs/unions, so we should too.
  191. */
  192. #define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
  193. #define NO_THREAD_LS
  194. #define NO_ATTRIBUTE_CONSTRUCTOR
  195. #ifdef HAVE_FIPS
  196. extern int wolfCrypt_FIPS_first(void);
  197. extern int wolfCrypt_FIPS_last(void);
  198. #endif
  199. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  200. /* work around backward dependency of asn.c on ssl.c. */
  201. struct Signer;
  202. struct Signer *GetCA(void *signers, unsigned char *hash);
  203. #ifndef NO_SKID
  204. struct Signer *GetCAByName(void* signers, unsigned char *hash);
  205. #endif
  206. #endif
  207. #if defined(__PIE__) && !defined(USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE)
  208. #error "compiling -fPIE requires PIE redirect table."
  209. #endif
  210. #if defined(HAVE_FIPS) && !defined(HAVE_LINUXKM_PIE_SUPPORT)
  211. #error "FIPS build requires PIE support."
  212. #endif
  213. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  214. #ifdef CONFIG_MIPS
  215. #undef __ARCH_MEMCMP_NO_REDIRECT
  216. #undef memcmp
  217. extern int memcmp(const void *s1, const void *s2, size_t n);
  218. #endif
  219. struct wolfssl_linuxkm_pie_redirect_table {
  220. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  221. typeof(memcmp) *memcmp;
  222. #endif
  223. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  224. typeof(memcpy) *memcpy;
  225. #endif
  226. #ifndef __ARCH_MEMSET_NO_REDIRECT
  227. typeof(memset) *memset;
  228. #endif
  229. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  230. typeof(memmove) *memmove;
  231. #endif
  232. #ifndef __ARCH_STRCMP_NO_REDIRECT
  233. typeof(strcmp) *strcmp;
  234. #endif
  235. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  236. typeof(strncmp) *strncmp;
  237. #endif
  238. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  239. typeof(strcasecmp) *strcasecmp;
  240. #endif
  241. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  242. typeof(strncasecmp) *strncasecmp;
  243. #endif
  244. #ifndef __ARCH_STRLEN_NO_REDIRECT
  245. typeof(strlen) *strlen;
  246. #endif
  247. #ifndef __ARCH_STRSTR_NO_REDIRECT
  248. typeof(strstr) *strstr;
  249. #endif
  250. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  251. typeof(strncpy) *strncpy;
  252. #endif
  253. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  254. typeof(strncat) *strncat;
  255. #endif
  256. typeof(kstrtoll) *kstrtoll;
  257. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  258. typeof(_printk) *_printk;
  259. #else
  260. typeof(printk) *printk;
  261. #endif
  262. typeof(snprintf) *snprintf;
  263. const unsigned char *_ctype;
  264. typeof(kmalloc) *kmalloc;
  265. typeof(kfree) *kfree;
  266. typeof(ksize) *ksize;
  267. typeof(krealloc) *krealloc;
  268. #ifdef HAVE_KVMALLOC
  269. typeof(kvmalloc_node) *kvmalloc_node;
  270. typeof(kvfree) *kvfree;
  271. #endif
  272. typeof(is_vmalloc_addr) *is_vmalloc_addr;
  273. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  274. typeof(kmalloc_trace) *kmalloc_trace;
  275. #else
  276. typeof(kmem_cache_alloc_trace) *kmem_cache_alloc_trace;
  277. typeof(kmalloc_order_trace) *kmalloc_order_trace;
  278. #endif
  279. typeof(get_random_bytes) *get_random_bytes;
  280. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  281. typeof(getnstimeofday) *getnstimeofday;
  282. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  283. typeof(current_kernel_time64) *current_kernel_time64;
  284. #else
  285. typeof(ktime_get_coarse_real_ts64) *ktime_get_coarse_real_ts64;
  286. #endif
  287. struct task_struct *(*get_current)(void);
  288. int (*preempt_count)(void);
  289. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  290. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
  291. typeof(cpu_number) *cpu_number;
  292. #else
  293. typeof(pcpu_hot) *pcpu_hot;
  294. #endif
  295. typeof(nr_cpu_ids) *nr_cpu_ids;
  296. #if defined(CONFIG_SMP) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0))
  297. /* note the current and needed version of these were added in af449901b8 (2020-Sep-17) */
  298. typeof(migrate_disable) *migrate_disable;
  299. typeof(migrate_enable) *migrate_enable;
  300. #endif
  301. #ifdef CONFIG_X86
  302. typeof(irq_fpu_usable) *irq_fpu_usable;
  303. /* kernel_fpu_begin() replaced by kernel_fpu_begin_mask() in commit e4512289,
  304. * released in kernel 5.11, backported to 5.4.93
  305. */
  306. #ifdef kernel_fpu_begin
  307. typeof(kernel_fpu_begin_mask) *kernel_fpu_begin_mask;
  308. #else
  309. typeof(kernel_fpu_begin) *kernel_fpu_begin;
  310. #endif
  311. typeof(kernel_fpu_end) *kernel_fpu_end;
  312. #else /* !CONFIG_X86 */
  313. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  314. #endif /* arch */
  315. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  316. typeof(__mutex_init) *__mutex_init;
  317. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  318. typeof(mutex_lock_nested) *mutex_lock_nested;
  319. #else
  320. typeof(mutex_lock) *mutex_lock;
  321. #endif
  322. typeof(mutex_unlock) *mutex_unlock;
  323. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  324. typeof(mutex_destroy) *mutex_destroy;
  325. #endif
  326. #ifdef HAVE_FIPS
  327. typeof(wolfCrypt_FIPS_first) *wolfCrypt_FIPS_first;
  328. typeof(wolfCrypt_FIPS_last) *wolfCrypt_FIPS_last;
  329. #endif
  330. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  331. typeof(GetCA) *GetCA;
  332. #ifndef NO_SKID
  333. typeof(GetCAByName) *GetCAByName;
  334. #endif
  335. #endif
  336. const void *_last_slot;
  337. };
  338. extern const struct wolfssl_linuxkm_pie_redirect_table *wolfssl_linuxkm_get_pie_redirect_table(void);
  339. #ifdef __PIE__
  340. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  341. #define memcmp (wolfssl_linuxkm_get_pie_redirect_table()->memcmp)
  342. #endif
  343. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  344. #define memcpy (wolfssl_linuxkm_get_pie_redirect_table()->memcpy)
  345. #endif
  346. #ifndef __ARCH_MEMSET_NO_REDIRECT
  347. #define memset (wolfssl_linuxkm_get_pie_redirect_table()->memset)
  348. #endif
  349. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  350. #define memmove (wolfssl_linuxkm_get_pie_redirect_table()->memmove)
  351. #endif
  352. #ifndef __ARCH_STRCMP_NO_REDIRECT
  353. #define strcmp (wolfssl_linuxkm_get_pie_redirect_table()->strcmp)
  354. #endif
  355. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  356. #define strncmp (wolfssl_linuxkm_get_pie_redirect_table()->strncmp)
  357. #endif
  358. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  359. #define strcasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strcasecmp)
  360. #endif
  361. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  362. #define strncasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strncasecmp)
  363. #endif
  364. #ifndef __ARCH_STRLEN_NO_REDIRECT
  365. #define strlen (wolfssl_linuxkm_get_pie_redirect_table()->strlen)
  366. #endif
  367. #ifndef __ARCH_STRSTR_NO_REDIRECT
  368. #define strstr (wolfssl_linuxkm_get_pie_redirect_table()->strstr)
  369. #endif
  370. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  371. #define strncpy (wolfssl_linuxkm_get_pie_redirect_table()->strncpy)
  372. #endif
  373. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  374. #define strncat (wolfssl_linuxkm_get_pie_redirect_table()->strncat)
  375. #endif
  376. #define kstrtoll (wolfssl_linuxkm_get_pie_redirect_table()->kstrtoll)
  377. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  378. #define _printk (wolfssl_linuxkm_get_pie_redirect_table()->_printk)
  379. #else
  380. #define printk (wolfssl_linuxkm_get_pie_redirect_table()->printk)
  381. #endif
  382. #define snprintf (wolfssl_linuxkm_get_pie_redirect_table()->snprintf)
  383. #define _ctype (wolfssl_linuxkm_get_pie_redirect_table()->_ctype)
  384. #define kmalloc (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc)
  385. #define kfree (wolfssl_linuxkm_get_pie_redirect_table()->kfree)
  386. #define ksize (wolfssl_linuxkm_get_pie_redirect_table()->ksize)
  387. #define krealloc (wolfssl_linuxkm_get_pie_redirect_table()->krealloc)
  388. #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO)
  389. #ifdef HAVE_KVMALLOC
  390. #define kvmalloc_node (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node)
  391. #define kvfree (wolfssl_linuxkm_get_pie_redirect_table()->kvfree)
  392. #endif
  393. #define is_vmalloc_addr (wolfssl_linuxkm_get_pie_redirect_table()->is_vmalloc_addr)
  394. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  395. #define kmalloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_trace)
  396. #else
  397. #define kmem_cache_alloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmem_cache_alloc_trace)
  398. #define kmalloc_order_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_order_trace)
  399. #endif
  400. #define get_random_bytes (wolfssl_linuxkm_get_pie_redirect_table()->get_random_bytes)
  401. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  402. #define getnstimeofday (wolfssl_linuxkm_get_pie_redirect_table()->getnstimeofday)
  403. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  404. #define current_kernel_time64 (wolfssl_linuxkm_get_pie_redirect_table()->current_kernel_time64)
  405. #else
  406. #define ktime_get_coarse_real_ts64 (wolfssl_linuxkm_get_pie_redirect_table()->ktime_get_coarse_real_ts64)
  407. #endif
  408. #undef get_current
  409. #define get_current (wolfssl_linuxkm_get_pie_redirect_table()->get_current)
  410. #undef preempt_count
  411. #define preempt_count (wolfssl_linuxkm_get_pie_redirect_table()->preempt_count)
  412. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  413. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
  414. #define cpu_number (*(wolfssl_linuxkm_get_pie_redirect_table()->cpu_number))
  415. #else
  416. #define pcpu_hot (*(wolfssl_linuxkm_get_pie_redirect_table()->pcpu_hot))
  417. #endif
  418. #define nr_cpu_ids (*(wolfssl_linuxkm_get_pie_redirect_table()->nr_cpu_ids))
  419. #if defined(CONFIG_SMP) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0))
  420. #define migrate_disable (*(wolfssl_linuxkm_get_pie_redirect_table()->migrate_disable))
  421. #define migrate_enable (*(wolfssl_linuxkm_get_pie_redirect_table()->migrate_enable))
  422. #endif
  423. #ifdef CONFIG_X86
  424. #define irq_fpu_usable (wolfssl_linuxkm_get_pie_redirect_table()->irq_fpu_usable)
  425. #ifdef kernel_fpu_begin
  426. #define kernel_fpu_begin_mask (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_begin_mask)
  427. #else
  428. #define kernel_fpu_begin (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_begin)
  429. #endif
  430. #define kernel_fpu_end (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_end)
  431. #else /* !CONFIG_X86 */
  432. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  433. #endif /* archs */
  434. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  435. #define __mutex_init (wolfssl_linuxkm_get_pie_redirect_table()->__mutex_init)
  436. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  437. #define mutex_lock_nested (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock_nested)
  438. #else
  439. #define mutex_lock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock)
  440. #endif
  441. #define mutex_unlock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_unlock)
  442. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  443. #define mutex_destroy (wolfssl_linuxkm_get_pie_redirect_table()->mutex_destroy)
  444. #endif
  445. /* per linux/ctype.h, tolower() and toupper() are macros bound to static inlines
  446. * that use macros that bring in the _ctype global. for __PIE__, this needs to
  447. * be masked out.
  448. */
  449. #undef tolower
  450. #undef toupper
  451. #define tolower(c) (islower(c) ? (c) : ((c) + ('a'-'A')))
  452. #define toupper(c) (isupper(c) ? (c) : ((c) - ('a'-'A')))
  453. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  454. #define GetCA (wolfssl_linuxkm_get_pie_redirect_table()->GetCA)
  455. #ifndef NO_SKID
  456. #define GetCAByName (wolfssl_linuxkm_get_pie_redirect_table()->GetCAByName)
  457. #endif
  458. #endif
  459. #endif /* __PIE__ */
  460. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
  461. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  462. #ifdef CONFIG_X86
  463. extern __must_check int allocate_wolfcrypt_linuxkm_fpu_states(void);
  464. extern void free_wolfcrypt_linuxkm_fpu_states(void);
  465. extern __must_check int save_vector_registers_x86(void);
  466. extern void restore_vector_registers_x86(void);
  467. #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  468. #error kernel module ARM SIMD is not yet tested or usable.
  469. static WARN_UNUSED_RESULT inline int save_vector_registers_arm(void)
  470. {
  471. preempt_disable();
  472. if (! may_use_simd()) {
  473. preempt_enable();
  474. return BAD_STATE_E;
  475. } else {
  476. fpsimd_preserve_current_state();
  477. return 0;
  478. }
  479. }
  480. static inline void restore_vector_registers_arm(void)
  481. {
  482. fpsimd_restore_current_state();
  483. preempt_enable();
  484. }
  485. #endif
  486. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  487. /* remove this multifariously conflicting macro, picked up from
  488. * Linux arch/<arch>/include/asm/current.h.
  489. */
  490. #ifndef WOLFSSL_NEED_LINUX_CURRENT
  491. #undef current
  492. #endif
  493. /* min() and max() in linux/kernel.h over-aggressively type-check, producing
  494. * myriad spurious -Werrors throughout the codebase.
  495. */
  496. #undef min
  497. #undef max
  498. /* work around namespace conflict between wolfssl/internal.h (enum HandShakeType)
  499. * and linux/key.h (extern int()).
  500. */
  501. #define key_update wc_key_update
  502. #define lkm_printf(format, args...) printk(KERN_INFO "wolfssl: %s(): " format, __func__, ## args)
  503. #define printf(...) lkm_printf(__VA_ARGS__)
  504. #ifdef HAVE_FIPS
  505. extern void fipsEntry(void);
  506. #endif
  507. /* suppress false-positive "writing 1 byte into a region of size 0" warnings
  508. * building old kernels with new gcc:
  509. */
  510. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  511. _Pragma("GCC diagnostic ignored \"-Wstringop-overflow\"");
  512. #endif
  513. /* includes are all above, with incompatible warnings masked out. */
  514. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
  515. typedef __kernel_time_t time_t;
  516. #else
  517. typedef __kernel_time64_t time_t;
  518. #endif
  519. extern time_t time(time_t * timer);
  520. #define XTIME time
  521. #define WOLFSSL_GMTIME
  522. #define XGMTIME(c, t) gmtime(c)
  523. #define NO_TIMEVAL 1
  524. #endif /* BUILDING_WOLFSSL */
  525. /* if BUILDING_WOLFSSL, mutex.h will have already been included recursively
  526. * above, with the bevy of warnings suppressed, and the below include will
  527. * be a redundant no-op.
  528. */
  529. #include <linux/mutex.h>
  530. typedef struct mutex wolfSSL_Mutex;
  531. /* prevent gcc's mm_malloc.h from being included, since it unconditionally
  532. * includes stdlib.h, which is kernel-incompatible.
  533. */
  534. #define _MM_MALLOC_H_INCLUDED
  535. /* fun fact: since linux commit 59bb47985c, kmalloc with power-of-2 size is
  536. * aligned to the size.
  537. */
  538. #define WC_LINUXKM_ROUND_UP_P_OF_2(x) ( \
  539. { \
  540. size_t _alloc_sz = (x); \
  541. _alloc_sz = 1UL << ((sizeof(_alloc_sz) * 8UL) - __builtin_clzl(_alloc_sz)); \
  542. _alloc_sz; \
  543. })
  544. #ifdef HAVE_KVMALLOC
  545. #define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), GFP_KERNEL, NUMA_NO_NODE)
  546. #define free(ptr) kvfree(ptr)
  547. void *lkm_realloc(void *ptr, size_t newsize);
  548. #define realloc(ptr, newsize) lkm_realloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize))
  549. #else
  550. #define malloc(size) kmalloc(WC_LINUXKM_ROUND_UP_P_OF_2(size), GFP_KERNEL)
  551. #define free(ptr) kfree(ptr)
  552. #define realloc(ptr, newsize) krealloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize), GFP_KERNEL)
  553. #endif
  554. #ifdef WOLFSSL_TRACK_MEMORY
  555. #include <wolfssl/wolfcrypt/memory.h>
  556. #define XMALLOC(s, h, t) ({(void)(h); (void)(t); wolfSSL_Malloc(s);})
  557. #define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) wolfSSL_Free(_xp);})
  558. #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); wolfSSL_Realloc(p, n);})
  559. #else
  560. #define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);})
  561. #define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) free(_xp);})
  562. #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);})
  563. #endif
  564. #include <linux/limits.h>
  565. /* Linux headers define these using C expressions, but we need
  566. * them to be evaluable by the preprocessor, for use in sp_int.h.
  567. */
  568. #if BITS_PER_LONG == 64
  569. _Static_assert(sizeof(ULONG_MAX) == 8,
  570. "BITS_PER_LONG is 64, but ULONG_MAX is not.");
  571. #undef UCHAR_MAX
  572. #define UCHAR_MAX 255
  573. #undef USHRT_MAX
  574. #define USHRT_MAX 65535
  575. #undef UINT_MAX
  576. #define UINT_MAX 4294967295U
  577. #undef ULONG_MAX
  578. #define ULONG_MAX 18446744073709551615UL
  579. #undef ULLONG_MAX
  580. #define ULLONG_MAX ULONG_MAX
  581. #undef INT_MAX
  582. #define INT_MAX 2147483647
  583. #undef LONG_MAX
  584. #define LONG_MAX 9223372036854775807L
  585. #undef LLONG_MAX
  586. #define LLONG_MAX LONG_MAX
  587. #elif BITS_PER_LONG == 32
  588. _Static_assert(sizeof(ULONG_MAX) == 4,
  589. "BITS_PER_LONG is 32, but ULONG_MAX is not.");
  590. #undef UCHAR_MAX
  591. #define UCHAR_MAX 255
  592. #undef USHRT_MAX
  593. #define USHRT_MAX 65535
  594. #undef UINT_MAX
  595. #define UINT_MAX 4294967295U
  596. #undef ULONG_MAX
  597. #define ULONG_MAX 4294967295UL
  598. #undef INT_MAX
  599. #define INT_MAX 2147483647
  600. #undef LONG_MAX
  601. #define LONG_MAX 2147483647L
  602. #undef ULLONG_MAX
  603. #undef LLONG_MAX
  604. #if BITS_PER_LONG_LONG == 64
  605. #define ULLONG_MAX 18446744073709551615UL
  606. #define LLONG_MAX 9223372036854775807L
  607. #else
  608. #undef NO_64BIT
  609. #define NO_64BIT
  610. #define ULLONG_MAX ULONG_MAX
  611. #define LLONG_MAX LONG_MAX
  612. #endif
  613. #else
  614. #error unexpected BITS_PER_LONG value.
  615. #endif
  616. #endif /* LINUXKM_WC_PORT_H */