linuxkm_wc_port.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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
  60. * objects, 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. #if defined(CONFIG_FORTIFY_SOURCE) || defined(DEBUG_LINUXKM_FORTIFY_OVERLAY)
  107. #ifdef __PIE__
  108. /* the inline definitions in fortify-string.h use non-inline
  109. * fortify_panic().
  110. */
  111. extern void __my_fortify_panic(const char *name) __noreturn __cold;
  112. #define fortify_panic __my_fortify_panic
  113. #endif
  114. /* the _FORTIFY_SOURCE macros and implementations for several string
  115. * functions are incompatible with libwolfssl, so just reimplement with
  116. * inlines and remap with macros.
  117. */
  118. #define __ARCH_STRLEN_NO_REDIRECT
  119. #define __ARCH_MEMCPY_NO_REDIRECT
  120. #define __ARCH_MEMSET_NO_REDIRECT
  121. #define __ARCH_MEMMOVE_NO_REDIRECT
  122. /* the inline definitions in fortify-string.h use non-inline
  123. * strlen().
  124. */
  125. static inline size_t strlen(const char *s) {
  126. const char *s_start = s;
  127. while (*s)
  128. ++s;
  129. return (size_t)((uintptr_t)s - (uintptr_t)s_start);
  130. }
  131. #include <linux/string.h>
  132. #undef strlen
  133. #define strlen(s) \
  134. ((__builtin_constant_p(s) && __builtin_constant_p(*(s))) ? \
  135. (sizeof(s) - 1) : strlen(s))
  136. static inline void *my_memcpy(void *dest, const void *src, size_t n) {
  137. if (! (((uintptr_t)dest | (uintptr_t)src | (uintptr_t)n)
  138. & (uintptr_t)(sizeof(uintptr_t) - 1)))
  139. {
  140. uintptr_t *src_longs = (uintptr_t *)src,
  141. *dest_longs = (uintptr_t *)dest,
  142. *endp = (uintptr_t *)((u8 *)src + n);
  143. while (src_longs < endp)
  144. *dest_longs++ = *src_longs++;
  145. } else {
  146. u8 *src_bytes = (u8 *)src,
  147. *dest_bytes = (u8 *)dest,
  148. *endp = src_bytes + n;
  149. while (src_bytes < endp)
  150. *dest_bytes++ = *src_bytes++;
  151. }
  152. return dest;
  153. }
  154. #undef memcpy
  155. #define memcpy my_memcpy
  156. static inline void *my_memset(void *dest, int c, size_t n) {
  157. if (! (((uintptr_t)dest | (uintptr_t)n)
  158. & (uintptr_t)(sizeof(uintptr_t) - 1)))
  159. {
  160. uintptr_t c_long = __builtin_choose_expr(
  161. sizeof(uintptr_t) == 8,
  162. (uintptr_t)(u8)c * 0x0101010101010101UL,
  163. (uintptr_t)(u8)c * 0x01010101U
  164. );
  165. uintptr_t *dest_longs = (uintptr_t *)dest,
  166. *endp = (uintptr_t *)((u8 *)dest_longs + n);
  167. while (dest_longs < endp)
  168. *dest_longs++ = c_long;
  169. } else {
  170. u8 *dest_bytes = (u8 *)dest, *endp = dest_bytes + n;
  171. while (dest_bytes < endp)
  172. *dest_bytes++ = (u8)c;
  173. }
  174. return dest;
  175. }
  176. #undef memset
  177. #define memset my_memset
  178. static inline void *my_memmove(void *dest, const void *src, size_t n) {
  179. if (! (((uintptr_t)dest | (uintptr_t)src | (uintptr_t)n)
  180. & (uintptr_t)(sizeof(uintptr_t) - 1)))
  181. {
  182. uintptr_t *src_longs = (uintptr_t *)src,
  183. *dest_longs = (uintptr_t *)dest;
  184. n >>= __builtin_choose_expr(
  185. sizeof(uintptr_t) == 8,
  186. 3U,
  187. 2U);
  188. if (src_longs < dest_longs) {
  189. uintptr_t *startp = src_longs;
  190. src_longs += n - 1;
  191. dest_longs += n - 1;
  192. while (src_longs >= startp)
  193. *dest_longs-- = *src_longs--;
  194. } else if (src_longs > dest_longs) {
  195. uintptr_t *endp = src_longs + n;
  196. while (src_longs < endp)
  197. *dest_longs++ = *src_longs++;
  198. }
  199. } else {
  200. u8 *src_bytes = (u8 *)src, *dest_bytes = (u8 *)dest;
  201. if (src_bytes < dest_bytes) {
  202. u8 *startp = src_bytes;
  203. src_bytes += n - 1;
  204. dest_bytes += n - 1;
  205. while (src_bytes >= startp)
  206. *dest_bytes-- = *src_bytes--;
  207. } else if (src_bytes > dest_bytes) {
  208. u8 *endp = src_bytes + n;
  209. while (src_bytes < endp)
  210. *dest_bytes++ = *src_bytes++;
  211. }
  212. }
  213. return dest;
  214. }
  215. #undef memmove
  216. #define memmove my_memmove
  217. #endif /* CONFIG_FORTIFY_SOURCE */
  218. #include <linux/init.h>
  219. #include <linux/module.h>
  220. #include <linux/delay.h>
  221. #ifdef __PIE__
  222. /* without this, mm.h brings in static, but not inline, pmd_to_page(),
  223. * with direct references to global vmem variables.
  224. */
  225. #undef USE_SPLIT_PMD_PTLOCKS
  226. #define USE_SPLIT_PMD_PTLOCKS 0
  227. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  228. /* without this, static show_free_areas() mm.h brings in direct
  229. * reference to unexported __show_free_areas().
  230. */
  231. #define __show_free_areas my__show_free_areas
  232. void my__show_free_areas(
  233. unsigned int flags,
  234. nodemask_t *nodemask,
  235. int max_zone_idx);
  236. #endif
  237. #endif
  238. #include <linux/mm.h>
  239. #ifndef SINGLE_THREADED
  240. #include <linux/kthread.h>
  241. #endif
  242. #include <linux/net.h>
  243. #include <linux/slab.h>
  244. #ifdef LINUXKM_LKCAPI_REGISTER
  245. #include <linux/crypto.h>
  246. #include <linux/scatterlist.h>
  247. #include <crypto/scatterwalk.h>
  248. #include <crypto/internal/aead.h>
  249. #include <crypto/internal/skcipher.h>
  250. /* the LKCAPI assumes that expanded encrypt and decrypt keys will stay
  251. * loaded simultaneously, and the Linux in-tree implementations have two
  252. * AES key structs in each context, one for each direction. in
  253. * linuxkm/lkcapi_glue.c (used for CBC, CFB, and GCM), we do the same
  254. * thing with "struct km_AesCtx". however, wolfCrypt struct AesXts
  255. * already has two AES expanded keys, the main and tweak, and the tweak
  256. * is always used in the encrypt direction regardless of the main
  257. * direction. to avoid allocating and computing a duplicate second
  258. * tweak encrypt key, we set
  259. * WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS, which adds a second
  260. * Aes slot to wolfCrypt's struct AesXts, and activates support for
  261. * AES_ENCRYPTION_AND_DECRYPTION on AES-XTS.
  262. */
  263. #ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  264. #define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  265. #endif
  266. #endif
  267. #if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || \
  268. defined(WOLFSSL_SP_X86_64_ASM)
  269. #ifndef CONFIG_X86
  270. #error X86 SIMD extensions requested, but CONFIG_X86 is not set.
  271. #endif
  272. #define WOLFSSL_LINUXKM_SIMD
  273. #define WOLFSSL_LINUXKM_SIMD_X86
  274. #ifndef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  275. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  276. #endif
  277. #elif defined(WOLFSSL_ARMASM) || defined(WOLFSSL_SP_ARM32_ASM) || \
  278. defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM_THUMB_ASM) ||\
  279. defined(WOLFSSL_SP_ARM_CORTEX_M_ASM)
  280. #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  281. #error ARM SIMD extensions requested, but CONFIG_ARM* is not set.
  282. #endif
  283. #define WOLFSSL_LINUXKM_SIMD
  284. #define WOLFSSL_LINUXKM_SIMD_ARM
  285. #ifndef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  286. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  287. #endif
  288. #else
  289. #ifndef WOLFSSL_NO_ASM
  290. #define WOLFSSL_NO_ASM
  291. #endif
  292. #endif
  293. /* benchmarks.c uses floating point math, so needs a working
  294. * SAVE_VECTOR_REGISTERS().
  295. */
  296. #if defined(WOLFSSL_LINUXKM_BENCHMARKS) && \
  297. !defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  298. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  299. #endif
  300. #if defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && \
  301. defined(CONFIG_X86)
  302. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  303. #include <asm/i387.h>
  304. #else
  305. #include <asm/simd.h>
  306. #endif
  307. #ifndef CAN_SAVE_VECTOR_REGISTERS
  308. #ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
  309. #define CAN_SAVE_VECTOR_REGISTERS() (can_save_vector_registers_x86() && (SAVE_VECTOR_REGISTERS2_fuzzer() == 0))
  310. #else
  311. #define CAN_SAVE_VECTOR_REGISTERS() can_save_vector_registers_x86()
  312. #endif
  313. #endif
  314. #ifndef SAVE_VECTOR_REGISTERS
  315. #define SAVE_VECTOR_REGISTERS(fail_clause) { \
  316. int _svr_ret = save_vector_registers_x86(); \
  317. if (_svr_ret != 0) { \
  318. fail_clause \
  319. } \
  320. }
  321. #endif
  322. #ifndef SAVE_VECTOR_REGISTERS2
  323. #ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
  324. #define SAVE_VECTOR_REGISTERS2() ({ \
  325. int _fuzzer_ret = SAVE_VECTOR_REGISTERS2_fuzzer(); \
  326. (_fuzzer_ret == 0) ? \
  327. save_vector_registers_x86() : \
  328. _fuzzer_ret; \
  329. })
  330. #else
  331. #define SAVE_VECTOR_REGISTERS2() save_vector_registers_x86()
  332. #endif
  333. #endif
  334. #ifndef RESTORE_VECTOR_REGISTERS
  335. #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_x86()
  336. #endif
  337. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
  338. #include <asm/fpsimd.h>
  339. #ifndef SAVE_VECTOR_REGISTERS
  340. #define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_arm(); if (_svr_ret != 0) { fail_clause } }
  341. #endif
  342. #ifndef SAVE_VECTOR_REGISTERS2
  343. #define SAVE_VECTOR_REGISTERS2() save_vector_registers_arm()
  344. #endif
  345. #ifndef CAN_SAVE_VECTOR_REGISTERS
  346. #define CAN_SAVE_VECTOR_REGISTERS() can_save_vector_registers_arm()
  347. #endif
  348. #ifndef RESTORE_VECTOR_REGISTERS
  349. #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_arm()
  350. #endif
  351. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  352. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  353. #endif
  354. _Pragma("GCC diagnostic pop");
  355. /* avoid -Wpointer-arith, encountered when -DCONFIG_FORTIFY_SOURCE */
  356. #undef __is_constexpr
  357. #define __is_constexpr(x) __builtin_constant_p(x)
  358. /* the kernel uses -std=c89, but not -pedantic, and makes full use of anon
  359. * structs/unions, so we should too.
  360. */
  361. #define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
  362. #define NO_THREAD_LS
  363. #define NO_ATTRIBUTE_CONSTRUCTOR
  364. #ifdef HAVE_FIPS
  365. extern int wolfCrypt_FIPS_first(void);
  366. extern int wolfCrypt_FIPS_last(void);
  367. #if FIPS_VERSION3_GE(6,0,0)
  368. extern int wolfCrypt_FIPS_AES_sanity(void);
  369. extern int wolfCrypt_FIPS_CMAC_sanity(void);
  370. extern int wolfCrypt_FIPS_DH_sanity(void);
  371. extern int wolfCrypt_FIPS_ECC_sanity(void);
  372. extern int wolfCrypt_FIPS_ED25519_sanity(void);
  373. extern int wolfCrypt_FIPS_ED448_sanity(void);
  374. extern int wolfCrypt_FIPS_HMAC_sanity(void);
  375. extern int wolfCrypt_FIPS_KDF_sanity(void);
  376. extern int wolfCrypt_FIPS_PBKDF_sanity(void);
  377. extern int wolfCrypt_FIPS_DRBG_sanity(void);
  378. extern int wolfCrypt_FIPS_RSA_sanity(void);
  379. extern int wolfCrypt_FIPS_SHA_sanity(void);
  380. extern int wolfCrypt_FIPS_SHA256_sanity(void);
  381. extern int wolfCrypt_FIPS_SHA512_sanity(void);
  382. extern int wolfCrypt_FIPS_SHA3_sanity(void);
  383. extern int wolfCrypt_FIPS_FT_sanity(void);
  384. extern int wc_RunAllCast_fips(void);
  385. #endif
  386. #endif
  387. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  388. /* work around backward dependency of asn.c on ssl.c. */
  389. struct Signer;
  390. struct Signer *GetCA(void *signers, unsigned char *hash);
  391. #ifndef NO_SKID
  392. struct Signer *GetCAByName(void* signers, unsigned char *hash);
  393. #endif
  394. #endif
  395. #if defined(__PIE__) && !defined(USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE)
  396. #error "compiling -fPIE requires PIE redirect table."
  397. #endif
  398. #if defined(HAVE_FIPS) && !defined(HAVE_LINUXKM_PIE_SUPPORT)
  399. #error "FIPS build requires PIE support."
  400. #endif
  401. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  402. #ifdef CONFIG_MIPS
  403. #undef __ARCH_MEMCMP_NO_REDIRECT
  404. #undef memcmp
  405. extern int memcmp(const void *s1, const void *s2, size_t n);
  406. #endif
  407. struct wolfssl_linuxkm_pie_redirect_table {
  408. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  409. typeof(memcmp) *memcmp;
  410. #endif
  411. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  412. typeof(memcpy) *memcpy;
  413. #endif
  414. #ifndef __ARCH_MEMSET_NO_REDIRECT
  415. typeof(memset) *memset;
  416. #endif
  417. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  418. typeof(memmove) *memmove;
  419. #endif
  420. #ifndef __ARCH_STRCMP_NO_REDIRECT
  421. typeof(strcmp) *strcmp;
  422. #endif
  423. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  424. typeof(strncmp) *strncmp;
  425. #endif
  426. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  427. typeof(strcasecmp) *strcasecmp;
  428. #endif
  429. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  430. typeof(strncasecmp) *strncasecmp;
  431. #endif
  432. #ifndef __ARCH_STRLEN_NO_REDIRECT
  433. typeof(strlen) *strlen;
  434. #endif
  435. #ifndef __ARCH_STRSTR_NO_REDIRECT
  436. typeof(strstr) *strstr;
  437. #endif
  438. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  439. typeof(strncpy) *strncpy;
  440. #endif
  441. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  442. typeof(strncat) *strncat;
  443. #endif
  444. typeof(kstrtoll) *kstrtoll;
  445. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  446. typeof(_printk) *_printk;
  447. #else
  448. typeof(printk) *printk;
  449. #endif
  450. #ifdef CONFIG_FORTIFY_SOURCE
  451. typeof(__warn_printk) *__warn_printk;
  452. #endif
  453. typeof(snprintf) *snprintf;
  454. const unsigned char *_ctype;
  455. typeof(kmalloc) *kmalloc;
  456. typeof(kfree) *kfree;
  457. typeof(ksize) *ksize;
  458. typeof(krealloc) *krealloc;
  459. #ifdef HAVE_KVMALLOC
  460. typeof(kvmalloc_node) *kvmalloc_node;
  461. typeof(kvfree) *kvfree;
  462. #endif
  463. typeof(is_vmalloc_addr) *is_vmalloc_addr;
  464. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  465. typeof(kmalloc_trace) *kmalloc_trace;
  466. #else
  467. typeof(kmem_cache_alloc_trace) *kmem_cache_alloc_trace;
  468. typeof(kmalloc_order_trace) *kmalloc_order_trace;
  469. #endif
  470. typeof(get_random_bytes) *get_random_bytes;
  471. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  472. typeof(getnstimeofday) *getnstimeofday;
  473. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  474. typeof(current_kernel_time64) *current_kernel_time64;
  475. #else
  476. typeof(ktime_get_coarse_real_ts64) *ktime_get_coarse_real_ts64;
  477. #endif
  478. struct task_struct *(*get_current)(void);
  479. int (*preempt_count)(void);
  480. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  481. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
  482. typeof(cpu_number) *cpu_number;
  483. #else
  484. typeof(pcpu_hot) *pcpu_hot;
  485. #endif
  486. typeof(nr_cpu_ids) *nr_cpu_ids;
  487. #if defined(CONFIG_SMP) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)) && !defined(WOLFSSL_COMMERCIAL_LICENSE)
  488. /* note the current and needed version of these were added in af449901b8 (2020-Sep-17) */
  489. typeof(migrate_disable) *migrate_disable;
  490. typeof(migrate_enable) *migrate_enable;
  491. #endif
  492. #ifdef CONFIG_X86
  493. typeof(irq_fpu_usable) *irq_fpu_usable;
  494. #ifdef WOLFSSL_COMMERCIAL_LICENSE
  495. typeof(fpregs_lock) *fpregs_lock;
  496. typeof(fpregs_lock) *fpregs_unlock;
  497. #else /* !WOLFSSL_COMMERCIAL_LICENSE */
  498. /* kernel_fpu_begin() replaced by kernel_fpu_begin_mask() in commit e4512289,
  499. * released in kernel 5.11, backported to 5.4.93
  500. */
  501. #ifdef kernel_fpu_begin
  502. typeof(kernel_fpu_begin_mask) *kernel_fpu_begin_mask;
  503. #else
  504. typeof(kernel_fpu_begin) *kernel_fpu_begin;
  505. #endif
  506. typeof(kernel_fpu_end) *kernel_fpu_end;
  507. #endif /* !defined(WOLFSSL_COMMERCIAL_LICENSE) */
  508. #else /* !CONFIG_X86 */
  509. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  510. #endif /* arch */
  511. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  512. typeof(__mutex_init) *__mutex_init;
  513. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  514. typeof(mutex_lock_nested) *mutex_lock_nested;
  515. #else
  516. typeof(mutex_lock) *mutex_lock;
  517. #endif
  518. typeof(mutex_unlock) *mutex_unlock;
  519. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  520. typeof(mutex_destroy) *mutex_destroy;
  521. #endif
  522. #ifdef HAVE_FIPS
  523. typeof(wolfCrypt_FIPS_first) *wolfCrypt_FIPS_first;
  524. typeof(wolfCrypt_FIPS_last) *wolfCrypt_FIPS_last;
  525. #if FIPS_VERSION3_GE(6,0,0)
  526. typeof(wolfCrypt_FIPS_AES_sanity) *wolfCrypt_FIPS_AES_sanity;
  527. typeof(wolfCrypt_FIPS_CMAC_sanity) *wolfCrypt_FIPS_CMAC_sanity;
  528. typeof(wolfCrypt_FIPS_DH_sanity) *wolfCrypt_FIPS_DH_sanity;
  529. typeof(wolfCrypt_FIPS_ECC_sanity) *wolfCrypt_FIPS_ECC_sanity;
  530. typeof(wolfCrypt_FIPS_ED25519_sanity) *wolfCrypt_FIPS_ED25519_sanity;
  531. typeof(wolfCrypt_FIPS_ED448_sanity) *wolfCrypt_FIPS_ED448_sanity;
  532. typeof(wolfCrypt_FIPS_HMAC_sanity) *wolfCrypt_FIPS_HMAC_sanity;
  533. typeof(wolfCrypt_FIPS_KDF_sanity) *wolfCrypt_FIPS_KDF_sanity;
  534. typeof(wolfCrypt_FIPS_PBKDF_sanity) *wolfCrypt_FIPS_PBKDF_sanity;
  535. typeof(wolfCrypt_FIPS_DRBG_sanity) *wolfCrypt_FIPS_DRBG_sanity;
  536. typeof(wolfCrypt_FIPS_RSA_sanity) *wolfCrypt_FIPS_RSA_sanity;
  537. typeof(wolfCrypt_FIPS_SHA_sanity) *wolfCrypt_FIPS_SHA_sanity;
  538. typeof(wolfCrypt_FIPS_SHA256_sanity) *wolfCrypt_FIPS_SHA256_sanity;
  539. typeof(wolfCrypt_FIPS_SHA512_sanity) *wolfCrypt_FIPS_SHA512_sanity;
  540. typeof(wolfCrypt_FIPS_SHA3_sanity) *wolfCrypt_FIPS_SHA3_sanity;
  541. typeof(wolfCrypt_FIPS_FT_sanity) *wolfCrypt_FIPS_FT_sanity;
  542. typeof(wc_RunAllCast_fips) *wc_RunAllCast_fips;
  543. #endif
  544. #endif
  545. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  546. typeof(GetCA) *GetCA;
  547. #ifndef NO_SKID
  548. typeof(GetCAByName) *GetCAByName;
  549. #endif
  550. #endif
  551. const void *_last_slot;
  552. };
  553. extern const struct wolfssl_linuxkm_pie_redirect_table *wolfssl_linuxkm_get_pie_redirect_table(void);
  554. #ifdef __PIE__
  555. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  556. #define memcmp (wolfssl_linuxkm_get_pie_redirect_table()->memcmp)
  557. #endif
  558. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  559. #define memcpy (wolfssl_linuxkm_get_pie_redirect_table()->memcpy)
  560. #endif
  561. #ifndef __ARCH_MEMSET_NO_REDIRECT
  562. #define memset (wolfssl_linuxkm_get_pie_redirect_table()->memset)
  563. #endif
  564. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  565. #define memmove (wolfssl_linuxkm_get_pie_redirect_table()->memmove)
  566. #endif
  567. #ifndef __ARCH_STRCMP_NO_REDIRECT
  568. #define strcmp (wolfssl_linuxkm_get_pie_redirect_table()->strcmp)
  569. #endif
  570. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  571. #define strncmp (wolfssl_linuxkm_get_pie_redirect_table()->strncmp)
  572. #endif
  573. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  574. #define strcasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strcasecmp)
  575. #endif
  576. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  577. #define strncasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strncasecmp)
  578. #endif
  579. #ifndef __ARCH_STRLEN_NO_REDIRECT
  580. #define strlen (wolfssl_linuxkm_get_pie_redirect_table()->strlen)
  581. #endif
  582. #ifndef __ARCH_STRSTR_NO_REDIRECT
  583. #define strstr (wolfssl_linuxkm_get_pie_redirect_table()->strstr)
  584. #endif
  585. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  586. #define strncpy (wolfssl_linuxkm_get_pie_redirect_table()->strncpy)
  587. #endif
  588. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  589. #define strncat (wolfssl_linuxkm_get_pie_redirect_table()->strncat)
  590. #endif
  591. #define kstrtoll (wolfssl_linuxkm_get_pie_redirect_table()->kstrtoll)
  592. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  593. #define _printk (wolfssl_linuxkm_get_pie_redirect_table()->_printk)
  594. #else
  595. #define printk (wolfssl_linuxkm_get_pie_redirect_table()->printk)
  596. #endif
  597. #ifdef CONFIG_FORTIFY_SOURCE
  598. #define __warn_printk (wolfssl_linuxkm_get_pie_redirect_table()->__warn_printk)
  599. #endif
  600. #define snprintf (wolfssl_linuxkm_get_pie_redirect_table()->snprintf)
  601. #define _ctype (wolfssl_linuxkm_get_pie_redirect_table()->_ctype)
  602. #define kmalloc (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc)
  603. #define kfree (wolfssl_linuxkm_get_pie_redirect_table()->kfree)
  604. #define ksize (wolfssl_linuxkm_get_pie_redirect_table()->ksize)
  605. #define krealloc (wolfssl_linuxkm_get_pie_redirect_table()->krealloc)
  606. #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO)
  607. #ifdef HAVE_KVMALLOC
  608. #define kvmalloc_node (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node)
  609. #define kvfree (wolfssl_linuxkm_get_pie_redirect_table()->kvfree)
  610. #endif
  611. #define is_vmalloc_addr (wolfssl_linuxkm_get_pie_redirect_table()->is_vmalloc_addr)
  612. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  613. #define kmalloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_trace)
  614. #else
  615. #define kmem_cache_alloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmem_cache_alloc_trace)
  616. #define kmalloc_order_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_order_trace)
  617. #endif
  618. #define get_random_bytes (wolfssl_linuxkm_get_pie_redirect_table()->get_random_bytes)
  619. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  620. #define getnstimeofday (wolfssl_linuxkm_get_pie_redirect_table()->getnstimeofday)
  621. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  622. #define current_kernel_time64 (wolfssl_linuxkm_get_pie_redirect_table()->current_kernel_time64)
  623. #else
  624. #define ktime_get_coarse_real_ts64 (wolfssl_linuxkm_get_pie_redirect_table()->ktime_get_coarse_real_ts64)
  625. #endif
  626. #undef get_current
  627. #define get_current (wolfssl_linuxkm_get_pie_redirect_table()->get_current)
  628. #undef preempt_count
  629. #define preempt_count (wolfssl_linuxkm_get_pie_redirect_table()->preempt_count)
  630. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  631. #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
  632. #define cpu_number (*(wolfssl_linuxkm_get_pie_redirect_table()->cpu_number))
  633. #else
  634. #define pcpu_hot (*(wolfssl_linuxkm_get_pie_redirect_table()->pcpu_hot))
  635. #endif
  636. #define nr_cpu_ids (*(wolfssl_linuxkm_get_pie_redirect_table()->nr_cpu_ids))
  637. #if defined(CONFIG_SMP) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)) && !defined(WOLFSSL_COMMERCIAL_LICENSE)
  638. #define migrate_disable (*(wolfssl_linuxkm_get_pie_redirect_table()->migrate_disable))
  639. #define migrate_enable (*(wolfssl_linuxkm_get_pie_redirect_table()->migrate_enable))
  640. #endif
  641. #ifdef CONFIG_X86
  642. #define irq_fpu_usable (wolfssl_linuxkm_get_pie_redirect_table()->irq_fpu_usable)
  643. #ifdef WOLFSSL_COMMERCIAL_LICENSE
  644. #define fpregs_lock() (wolfssl_linuxkm_get_pie_redirect_table()->fpregs_lock())
  645. #define fpregs_unlock() (wolfssl_linuxkm_get_pie_redirect_table()->fpregs_unlock())
  646. #else /* !defined(WOLFSSL_COMMERCIAL_LICENSE) */
  647. #ifdef kernel_fpu_begin
  648. #define kernel_fpu_begin_mask (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_begin_mask)
  649. #else
  650. #define kernel_fpu_begin (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_begin)
  651. #endif
  652. #define kernel_fpu_end (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_end)
  653. #endif /* !defined(WOLFSSL_COMMERCIAL_LICENSE) */
  654. #else /* !CONFIG_X86 */
  655. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  656. #endif /* archs */
  657. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  658. #define __mutex_init (wolfssl_linuxkm_get_pie_redirect_table()->__mutex_init)
  659. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  660. #define mutex_lock_nested (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock_nested)
  661. #else
  662. #define mutex_lock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock)
  663. #endif
  664. #define mutex_unlock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_unlock)
  665. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  666. #define mutex_destroy (wolfssl_linuxkm_get_pie_redirect_table()->mutex_destroy)
  667. #endif
  668. /* per linux/ctype.h, tolower() and toupper() are macros bound to static inlines
  669. * that use macros that bring in the _ctype global. for __PIE__, this needs to
  670. * be masked out.
  671. */
  672. #undef tolower
  673. #undef toupper
  674. #define tolower(c) (islower(c) ? (c) : ((c) + ('a'-'A')))
  675. #define toupper(c) (isupper(c) ? (c) : ((c) - ('a'-'A')))
  676. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  677. #define GetCA (wolfssl_linuxkm_get_pie_redirect_table()->GetCA)
  678. #ifndef NO_SKID
  679. #define GetCAByName (wolfssl_linuxkm_get_pie_redirect_table()->GetCAByName)
  680. #endif
  681. #endif
  682. #endif /* __PIE__ */
  683. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
  684. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  685. #ifdef CONFIG_X86
  686. extern __must_check int allocate_wolfcrypt_linuxkm_fpu_states(void);
  687. extern void free_wolfcrypt_linuxkm_fpu_states(void);
  688. extern __must_check int can_save_vector_registers_x86(void);
  689. extern __must_check int save_vector_registers_x86(void);
  690. extern void restore_vector_registers_x86(void);
  691. #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  692. #error kernel module ARM SIMD is not yet tested or usable.
  693. static WARN_UNUSED_RESULT inline int save_vector_registers_arm(void)
  694. {
  695. preempt_disable();
  696. if (! may_use_simd()) {
  697. preempt_enable();
  698. return BAD_STATE_E;
  699. } else {
  700. fpsimd_preserve_current_state();
  701. return 0;
  702. }
  703. }
  704. static inline void restore_vector_registers_arm(void)
  705. {
  706. fpsimd_restore_current_state();
  707. preempt_enable();
  708. }
  709. #endif
  710. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  711. /* remove this multifariously conflicting macro, picked up from
  712. * Linux arch/<arch>/include/asm/current.h.
  713. */
  714. #ifndef WOLFSSL_NEED_LINUX_CURRENT
  715. #undef current
  716. #endif
  717. /* min() and max() in linux/kernel.h over-aggressively type-check, producing
  718. * myriad spurious -Werrors throughout the codebase.
  719. */
  720. #undef min
  721. #undef max
  722. /* work around namespace conflict between wolfssl/internal.h (enum HandShakeType)
  723. * and linux/key.h (extern int()).
  724. */
  725. #define key_update wc_key_update
  726. #define lkm_printf(format, args...) printk(KERN_INFO "wolfssl: %s(): " format, __func__, ## args)
  727. #define printf(...) lkm_printf(__VA_ARGS__)
  728. #ifdef HAVE_FIPS
  729. extern void fipsEntry(void);
  730. #endif
  731. /* suppress false-positive "writing 1 byte into a region of size 0" warnings
  732. * building old kernels with new gcc:
  733. */
  734. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  735. _Pragma("GCC diagnostic ignored \"-Wstringop-overflow\"");
  736. #endif
  737. /* includes are all above, with incompatible warnings masked out. */
  738. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
  739. typedef __kernel_time_t time_t;
  740. #else
  741. typedef __kernel_time64_t time_t;
  742. #endif
  743. extern time_t time(time_t * timer);
  744. #define XTIME time
  745. #define WOLFSSL_GMTIME
  746. #define XGMTIME(c, t) gmtime(c)
  747. #define NO_TIMEVAL 1
  748. #endif /* BUILDING_WOLFSSL */
  749. /* if BUILDING_WOLFSSL, mutex.h will have already been included recursively
  750. * above, with the bevy of warnings suppressed, and the below include will
  751. * be a redundant no-op.
  752. */
  753. #include <linux/mutex.h>
  754. typedef struct mutex wolfSSL_Mutex;
  755. #define WOLFSSL_MUTEX_INITIALIZER(lockname) __MUTEX_INITIALIZER(lockname)
  756. /* prevent gcc's mm_malloc.h from being included, since it unconditionally
  757. * includes stdlib.h, which is kernel-incompatible.
  758. */
  759. #define _MM_MALLOC_H_INCLUDED
  760. /* fun fact: since linux commit 59bb47985c, kmalloc with power-of-2 size is
  761. * aligned to the size.
  762. */
  763. #define WC_LINUXKM_ROUND_UP_P_OF_2(x) ( \
  764. { \
  765. size_t _alloc_sz = (x); \
  766. if (_alloc_sz < 8192) \
  767. _alloc_sz = 1UL << \
  768. ((sizeof(_alloc_sz) * 8UL) - __builtin_clzl(_alloc_sz - 1)); \
  769. _alloc_sz; \
  770. })
  771. #ifdef HAVE_KVMALLOC
  772. #define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), GFP_KERNEL, NUMA_NO_NODE)
  773. #define free(ptr) kvfree(ptr)
  774. void *lkm_realloc(void *ptr, size_t newsize);
  775. #define realloc(ptr, newsize) lkm_realloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize))
  776. #else
  777. #define malloc(size) kmalloc(WC_LINUXKM_ROUND_UP_P_OF_2(size), GFP_KERNEL)
  778. #define free(ptr) kfree(ptr)
  779. #define realloc(ptr, newsize) krealloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize), GFP_KERNEL)
  780. #endif
  781. #ifndef static_assert
  782. #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
  783. #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
  784. #endif
  785. #include <wolfssl/wolfcrypt/memory.h>
  786. #ifdef WOLFSSL_TRACK_MEMORY
  787. #define XMALLOC(s, h, t) ({(void)(h); (void)(t); wolfSSL_Malloc(s);})
  788. #ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
  789. #define XFREE(p, h, t) ({(void)(h); (void)(t); wolfSSL_Free(p);})
  790. #else
  791. #define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) wolfSSL_Free(_xp);})
  792. #endif
  793. #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); wolfSSL_Realloc(p, n);})
  794. #else
  795. #define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);})
  796. #ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
  797. #define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);})
  798. #else
  799. #define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);})
  800. #endif
  801. #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);})
  802. #endif
  803. #include <linux/limits.h>
  804. /* Linux headers define these using C expressions, but we need
  805. * them to be evaluable by the preprocessor, for use in sp_int.h.
  806. */
  807. #if BITS_PER_LONG == 64
  808. static_assert(sizeof(ULONG_MAX) == 8,
  809. "BITS_PER_LONG is 64, but ULONG_MAX is not.");
  810. #undef UCHAR_MAX
  811. #define UCHAR_MAX 255
  812. #undef USHRT_MAX
  813. #define USHRT_MAX 65535
  814. #undef UINT_MAX
  815. #define UINT_MAX 4294967295U
  816. #undef ULONG_MAX
  817. #define ULONG_MAX 18446744073709551615UL
  818. #undef ULLONG_MAX
  819. #define ULLONG_MAX ULONG_MAX
  820. #undef INT_MAX
  821. #define INT_MAX 2147483647
  822. #undef LONG_MAX
  823. #define LONG_MAX 9223372036854775807L
  824. #undef LLONG_MAX
  825. #define LLONG_MAX LONG_MAX
  826. #elif BITS_PER_LONG == 32
  827. static_assert(sizeof(ULONG_MAX) == 4,
  828. "BITS_PER_LONG is 32, but ULONG_MAX is not.");
  829. #undef UCHAR_MAX
  830. #define UCHAR_MAX 255
  831. #undef USHRT_MAX
  832. #define USHRT_MAX 65535
  833. #undef UINT_MAX
  834. #define UINT_MAX 4294967295U
  835. #undef ULONG_MAX
  836. #define ULONG_MAX 4294967295UL
  837. #undef INT_MAX
  838. #define INT_MAX 2147483647
  839. #undef LONG_MAX
  840. #define LONG_MAX 2147483647L
  841. #undef ULLONG_MAX
  842. #undef LLONG_MAX
  843. #if BITS_PER_LONG_LONG == 64
  844. #define ULLONG_MAX 18446744073709551615UL
  845. #define LLONG_MAX 9223372036854775807L
  846. #else
  847. #undef NO_64BIT
  848. #define NO_64BIT
  849. #define ULLONG_MAX ULONG_MAX
  850. #define LLONG_MAX LONG_MAX
  851. #endif
  852. #else
  853. #error unexpected BITS_PER_LONG value.
  854. #endif
  855. #endif /* LINUXKM_WC_PORT_H */