Skip to content

Commit b23f59f

Browse files
authored
Merge pull request #9540 from sameehj/linuxkm_tegra_fips_fixes
linuxkm: fix Tegra Yocto FIPS build issues (ARM64, RT, PIE)
2 parents 04a06fe + a5f1fde commit b23f59f

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ CONFIG_PICOLIBC
135135
CONFIG_POSIX_API
136136
CONFIG_POSIX_THREADS
137137
CONFIG_PREEMPT_COUNT
138+
CONFIG_PREEMPT_RT
138139
CONFIG_PTHREAD_IPC
139140
CONFIG_SCHED_INFO
140141
CONFIG_SMP

linuxkm/Kbuild

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ ifeq "$(ENABLED_LINUXKM_PIE)" "yes"
145145
endif
146146
ccflags-y += $(PIE_SUPPORT_FLAGS)
147147
$(WOLFCRYPT_PIE_FILES): ccflags-y += $(PIE_FLAGS)
148-
$(WOLFCRYPT_PIE_FILES): ccflags-remove-y += -pg
148+
$(WOLFCRYPT_PIE_FILES): ccflags-remove-y += -pg \
149+
$(call cc-option,-ftrivial-auto-var-init=zero)
149150
ifdef FORCE_GLOBAL_OBJTOOL_OFF
150151
undefine CONFIG_OBJTOOL
151152
endif

linuxkm/linuxkm_wc_port.h

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@
2424
#ifndef LINUXKM_WC_PORT_H
2525
#define LINUXKM_WC_PORT_H
2626

27+
/*
28+
* CRITICAL: Disable ARM64 LSE atomics for out-of-tree modules.
29+
*
30+
* When CONFIG_ARM64_LSE_ATOMICS is enabled, the kernel uses static keys
31+
* (jump labels) in system_uses_lse_atomics() to choose between LSE and
32+
* LL/SC atomic implementations at runtime. These static keys generate
33+
* asm goto statements that reference .jump_table section symbols which
34+
* cannot be resolved in out-of-tree modules, causing:
35+
* "error: impossible constraint in 'asm'"
36+
*
37+
* By undefining CONFIG_ARM64_LSE_ATOMICS here (before any kernel headers
38+
* that use atomics are included), we force use of the LL/SC fallback path
39+
* which works correctly in out-of-tree modules.
40+
*
41+
* This must appear BEFORE #include <linux/version.h> because that header
42+
* may transitively include headers that use atomics.
43+
*/
44+
#ifdef CONFIG_ARM64_LSE_ATOMICS
45+
#undef CONFIG_ARM64_LSE_ATOMICS
46+
#endif
47+
2748
#include <linux/version.h>
2849

2950
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)
@@ -941,7 +962,15 @@
941962

942963
#endif /* WOLFSSL_USE_SAVE_VECTOR_REGISTERS */
943964

944-
typeof(__mutex_init) *__mutex_init;
965+
#ifndef CONFIG_PREEMPT_RT
966+
typeof(__mutex_init) *__mutex_init;
967+
#else
968+
typeof(__rt_mutex_init) *__rt_mutex_init;
969+
typeof(rt_mutex_base_init) *rt_mutex_base_init;
970+
typeof(rt_spin_lock) *rt_spin_lock;
971+
typeof(rt_spin_unlock) *rt_spin_unlock;
972+
#endif
973+
945974
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
946975
typeof(mutex_lock_nested) *mutex_lock_nested;
947976
#else
@@ -1048,6 +1077,7 @@
10481077
#endif
10491078

10501079
#ifdef CONFIG_ARM64
1080+
#ifndef CONFIG_ARCH_TEGRA
10511081
#ifdef WC_CONTAINERIZE_THIS
10521082
/* alt_cb_patch_nops and queued_spin_lock_slowpath are defined early
10531083
* to allow shimming in system headers, but now we need the native
@@ -1062,6 +1092,7 @@
10621092
typeof(queued_spin_lock_slowpath) *queued_spin_lock_slowpath;
10631093
#endif
10641094
#endif
1095+
#endif
10651096

10661097
typeof(preempt_count) *preempt_count;
10671098
#ifndef _raw_spin_lock_irqsave
@@ -1260,7 +1291,17 @@
12601291
#error WOLFSSL_USE_SAVE_VECTOR_REGISTERS is set for an unimplemented architecture.
12611292
#endif /* WOLFSSL_USE_SAVE_VECTOR_REGISTERS */
12621293

1263-
#define __mutex_init WC_PIE_INDIRECT_SYM(__mutex_init)
1294+
#ifndef CONFIG_PREEMPT_RT
1295+
#define __mutex_init WC_PIE_INDIRECT_SYM(__mutex_init)
1296+
#else
1297+
/* On RT kernels, __mutex_init is a macro pointing to __rt_mutex_init */
1298+
#undef __mutex_init
1299+
#define __rt_mutex_init WC_PIE_INDIRECT_SYM(__rt_mutex_init)
1300+
#define __mutex_init(mutex, name, key) __rt_mutex_init(mutex, name, key)
1301+
#define rt_mutex_base_init WC_PIE_INDIRECT_SYM(rt_mutex_base_init)
1302+
#define rt_spin_lock WC_PIE_INDIRECT_SYM(rt_spin_lock)
1303+
#define rt_spin_unlock WC_PIE_INDIRECT_SYM(rt_spin_unlock)
1304+
#endif
12641305
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
12651306
#define mutex_lock_nested WC_PIE_INDIRECT_SYM(mutex_lock_nested)
12661307
#else
@@ -1327,8 +1368,31 @@
13271368

13281369
/* this is defined in linux/spinlock.h as an inline that calls the unshimmed
13291370
* raw_spin_unlock_irqrestore(). use a macro here to supersede it.
1371+
* Note: On PREEMPT_RT kernels, spinlock_t doesn't have rlock member,
1372+
* so we skip this redefinition and use the kernel's native implementation.
13301373
*/
1331-
#define spin_unlock_irqrestore(lock, flags) raw_spin_unlock_irqrestore(&((lock)->rlock), flags)
1374+
#ifndef CONFIG_PREEMPT_RT
1375+
#define spin_unlock_irqrestore(lock, flags) raw_spin_unlock_irqrestore(&((lock)->rlock), flags)
1376+
#else
1377+
/* Undo internal wolfSSL PIE macro rewriting */
1378+
#ifdef rt_spin_unlock
1379+
#undef rt_spin_unlock
1380+
#endif
1381+
#ifdef rt_spin_lock
1382+
#undef rt_spin_lock
1383+
#endif
1384+
static inline int wolfssl_spin_unlock_irqrestore_rt(spinlock_t *lock,
1385+
unsigned long flags)
1386+
{
1387+
(void)flags; /* rt_spin_unlock ignores flags */
1388+
WC_PIE_INDIRECT_SYM(rt_spin_unlock)(lock);
1389+
return 0;
1390+
}
1391+
1392+
#undef spin_unlock_irqrestore
1393+
#define spin_unlock_irqrestore(lock, flags) \
1394+
wolfssl_spin_unlock_irqrestore_rt((lock), (flags))
1395+
#endif
13321396

13331397
#define wc_linuxkm_sig_ignore_begin WC_PIE_INDIRECT_SYM(wc_linuxkm_sig_ignore_begin);
13341398
#define wc_linuxkm_sig_ignore_end WC_PIE_INDIRECT_SYM(wc_linuxkm_sig_ignore_end);

linuxkm/module_hooks.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,14 @@ static int set_up_wolfssl_linuxkm_pie_redirect_table(void) {
13101310
#error WOLFSSL_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
13111311
#endif /* WOLFSSL_USE_SAVE_VECTOR_REGISTERS */
13121312

1313-
wolfssl_linuxkm_pie_redirect_table.__mutex_init = __mutex_init;
1313+
#ifndef CONFIG_PREEMPT_RT
1314+
wolfssl_linuxkm_pie_redirect_table.__mutex_init = __mutex_init;
1315+
#else
1316+
wolfssl_linuxkm_pie_redirect_table.__rt_mutex_init = __rt_mutex_init;
1317+
wolfssl_linuxkm_pie_redirect_table.rt_mutex_base_init = rt_mutex_base_init;
1318+
wolfssl_linuxkm_pie_redirect_table.rt_spin_lock = rt_spin_lock;
1319+
wolfssl_linuxkm_pie_redirect_table.rt_spin_unlock = rt_spin_unlock;
1320+
#endif
13141321
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
13151322
wolfssl_linuxkm_pie_redirect_table.mutex_lock_nested = mutex_lock_nested;
13161323
#else
@@ -1489,8 +1496,10 @@ static int set_up_wolfssl_linuxkm_pie_redirect_table(void) {
14891496
#endif
14901497

14911498
#ifdef CONFIG_ARM64
1499+
#ifndef CONFIG_ARCH_TEGRA
14921500
wolfssl_linuxkm_pie_redirect_table.alt_cb_patch_nops = alt_cb_patch_nops;
14931501
wolfssl_linuxkm_pie_redirect_table.queued_spin_lock_slowpath = queued_spin_lock_slowpath;
1502+
#endif
14941503
#endif
14951504

14961505
wolfssl_linuxkm_pie_redirect_table.wc_linuxkm_sig_ignore_begin = wc_linuxkm_sig_ignore_begin;

wolfcrypt/src/wc_port.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4977,6 +4977,7 @@ char* wolfSSL_strnstr(const char* s1, const char* s2, unsigned int n)
49774977

49784978
#if defined(WOLFSSL_LINUXKM) && defined(CONFIG_ARM64) && \
49794979
defined(WC_SYM_RELOC_TABLES)
4980+
#ifndef CONFIG_ARCH_TEGRA
49804981
noinstr void my__alt_cb_patch_nops(struct alt_instr *alt, __le32 *origptr,
49814982
__le32 *updptr, int nr_inst)
49824983
{
@@ -4990,3 +4991,4 @@ void my__queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
49904991
(lock, val);
49914992
}
49924993
#endif
4994+
#endif

0 commit comments

Comments
 (0)