linuxkm_wc_port.h 24 KB

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