linuxkm_wc_port.h 23 KB

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