Skip to content

Commit ea49c4a

Browse files
author
Luca Toniolo
committed
Deprecate uses_fp: always enable FPU state save on all threads
GCC on x86-64 may emit SSE instructions (e.g. for struct zeroing) even in functions that don't explicitly use floating point. On RTAI, threads created with uses_fp=0 skip FPU/SSE state save/restore, silently corrupting XMM registers of other Linux processes and causing heap corruption and system crashes. Fix by always enabling FPU state save regardless of the uses_fp parameter: - rtai_rtapi.c: always pass 1 to rt_task_init_cpuid - hal_lib.c: override uses_fp=0 to 1 in hal_create_thread - hal_lib.c: remove addf FP compatibility check (now meaningless) Emit deprecation warnings when uses_fp=0 is requested. Ref: #3895
1 parent 8ef8e16 commit ea49c4a

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/hal/hal_lib.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,12 @@ int hal_create_thread(const char *name, unsigned long period_nsec, int uses_fp)
19171917
long prev_period, curr_period;
19181918
char buf[HAL_NAME_LEN + 1];
19191919

1920+
if (!uses_fp) {
1921+
rtapi_print_msg(RTAPI_MSG_WARN,
1922+
"HAL: WARNING: thread '%s' created with uses_fp=0, "
1923+
"overriding to uses_fp=1 (uses_fp is deprecated).\n", name);
1924+
uses_fp = 1;
1925+
}
19201926
rtapi_print_msg(RTAPI_MSG_DBG,
19211927
"HAL: creating thread %s, %ld nsec\n", name, period_nsec);
19221928
if (hal_data == 0) {
@@ -2202,13 +2208,8 @@ int hal_add_funct_to_thread(const char *funct_name, const char *thread_name, int
22022208
"HAL: ERROR: thread '%s' not found\n", thread_name);
22032209
return -EINVAL;
22042210
}
2205-
/* ok, we have thread and function, are they compatible? */
2206-
if ((funct->uses_fp) && (!thread->uses_fp)) {
2207-
rtapi_mutex_give(&(hal_data->mutex));
2208-
rtapi_print_msg(RTAPI_MSG_ERR,
2209-
"HAL: ERROR: function '%s' needs FP\n", funct_name);
2210-
return -EINVAL;
2211-
}
2211+
/* All threads now save FPU state (uses_fp is deprecated),
2212+
so the FP compatibility check is no longer needed. */
22122213
/* find insertion point */
22132214
list_root = &(thread->funct_list);
22142215
list_entry = list_root;

src/rtapi/rtai_rtapi.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,18 @@ int rtapi_task_new(void (*taskcode) (void *), void *arg,
685685
}
686686
task->taskcode = taskcode;
687687
task->arg = arg;
688-
/* call OS to initialize the task - use predetermined CPU */
688+
if (!uses_fp) {
689+
rtapi_print_msg(RTAPI_MSG_WARN,
690+
"RTAPI: WARNING: task created with uses_fp=0, "
691+
"overriding to uses_fp=1. All threads now save FPU state "
692+
"(uses_fp is deprecated).\n");
693+
}
694+
/* call OS to initialize the task - use predetermined CPU
695+
Always enable FPU state save/restore: modern compilers may emit
696+
SSE instructions even in code that doesn't explicitly use FP,
697+
causing silent XMM register corruption without FPU save. */
689698
retval = rt_task_init_cpuid(ostask_array[task_id], wrapper, task_id,
690-
stacksize, prio, uses_fp, 0 /* signal */, rtapi_data->rt_cpu );
699+
stacksize, prio, 1 /* always save FPU */, 0 /* signal */, rtapi_data->rt_cpu );
691700
if (retval != 0) {
692701
/* couldn't create task, free task data memory */
693702
kfree(ostask_array[task_id]);

0 commit comments

Comments
 (0)