Skip to content

Commit c6bc092

Browse files
peter-mitsiskartben
authored andcommitted
kernel: Move current_fp field out of z_kernel
The current_fp field in the z_kernel structure is only used by 32-bit x86 (which does not support SMP). As such, it should reside in the arch specific of section of _kernel.cpus[0]. This also changes the name of 'current_fp' to 'fpu_owner' to be more consistent with other architectures. Signed-off-by: Peter Mitsis <[email protected]>
1 parent 22c04bd commit c6bc092

File tree

9 files changed

+53
-30
lines changed

9 files changed

+53
-30
lines changed

arch/x86/core/ia32/float.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void z_float_enable(struct k_thread *thread, unsigned int options)
194194
* must be preserved).
195195
*/
196196

197-
fp_owner = _kernel.current_fp;
197+
fp_owner = _kernel.cpus[0].arch.fpu_owner;
198198
if (fp_owner != NULL) {
199199
if ((fp_owner->arch.flags & X86_THREAD_FLAG_ALL) != 0) {
200200
FpCtxSave(fp_owner);
@@ -215,7 +215,7 @@ void z_float_enable(struct k_thread *thread, unsigned int options)
215215
* (The FP context is "live" in hardware, not saved in TCS.)
216216
*/
217217

218-
_kernel.current_fp = thread;
218+
_kernel.cpus[0].arch.fpu_owner = thread;
219219
} else {
220220
/*
221221
* When enabling FP support for someone else, assign ownership
@@ -230,7 +230,7 @@ void z_float_enable(struct k_thread *thread, unsigned int options)
230230
* to its original state.
231231
*/
232232

233-
_kernel.current_fp = thread;
233+
_kernel.cpus[0].arch.fpu_owner = thread;
234234
z_FpAccessDisable();
235235
} else {
236236
/*
@@ -280,10 +280,10 @@ int z_float_disable(struct k_thread *thread)
280280

281281
if (thread == _current) {
282282
z_FpAccessDisable();
283-
_kernel.current_fp = (struct k_thread *)0;
283+
_kernel.cpus[0].arch.fpu_owner = (struct k_thread *)0;
284284
} else {
285-
if (_kernel.current_fp == thread) {
286-
_kernel.current_fp = (struct k_thread *)0;
285+
if (_kernel.cpus[0].arch.fpu_owner == thread) {
286+
_kernel.cpus[0].arch.fpu_owner = (struct k_thread *)0;
287287
}
288288
}
289289

arch/x86/core/ia32/swap.S

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* Floating point registers are handled using a lazy save/restore mechanism
5151
* since it's expected relatively few threads will be created with the
5252
* K_FP_REGS or K_SSE_REGS option bits. The kernel data structure maintains a
53-
* 'current_fp' field to keep track of the thread that "owns" the floating
53+
* 'fpu_owner' field to keep track of the thread that "owns" the floating
5454
* point registers. Floating point registers consist of ST0->ST7 (x87 FPU and
5555
* MMX registers) and XMM0 -> XMM7.
5656
*
@@ -176,7 +176,7 @@ SECTION_FUNC(PINNED_TEXT, arch_swap)
176176
* If so, there there is no need to restore the floating point context.
177177
*/
178178

179-
movl _kernel_offset_to_current_fp(%edi), %ebx
179+
movl _kernel_offset_to_fpu_owner(%edi), %ebx
180180
cmpl %ebx, %eax
181181
je restoreContext_NoFloatSwap
182182

@@ -265,7 +265,7 @@ restoreContext_NoFloatRestore:
265265

266266
/* record that the incoming thread "owns" the floating point registers */
267267

268-
movl %eax, _kernel_offset_to_current_fp(%edi)
268+
movl %eax, _kernel_offset_to_fpu_owner(%edi)
269269

270270

271271
/*

arch/x86/core/offsets/ia32_offsets.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
GEN_OFFSET_SYM(_thread_arch_t, excNestCount);
3434
#endif
3535

36+
#if defined(CONFIG_FPU_SHARING)
37+
GEN_OFFSET_SYM(_kernel_t, cpus);
38+
GEN_OFFSET_SYM(_cpu_t, arch);
39+
GEN_OFFSET_SYM(_cpu_arch_t, fpu_owner);
40+
#endif
41+
3642
#ifdef CONFIG_USERSPACE
3743
GEN_OFFSET_SYM(_thread_arch_t, psp);
3844
#ifndef CONFIG_X86_COMMON_PAGE_TABLE

arch/x86/include/ia32/offsets_short_arch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#define _kernel_offset_to_isf \
1515
(___kernel_t_arch_OFFSET + ___kernel_arch_t_isf_OFFSET)
1616

17+
#define _kernel_offset_to_fpu_owner \
18+
(___kernel_t_cpus_OFFSET + ___cpu_t_arch_OFFSET + ___cpu_arch_t_fpu_owner_OFFSET)
19+
1720
/* end - kernel */
1821

1922
/* threads */

include/zephyr/arch/structs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <zephyr/arch/riscv/structs.h>
3030
#elif defined(CONFIG_ARM)
3131
#include <zephyr/arch/arm/structs.h>
32+
#elif defined(CONFIG_X86) && !defined(CONFIG_X86_64)
33+
#include <zephyr/arch/x86/ia32/structs.h>
3234
#else
3335

3436
/* Default definitions when no architecture specific definitions exist. */
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2025 Intel
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef ZEPHYR_INCLUDE_X86_STRUCTS_H_
7+
#define ZEPHYR_INCLUDE_X86_STRUCTS_H_
8+
9+
#include <stdint.h>
10+
11+
struct k_thread;
12+
13+
/* Per CPU architecture specifics (empty) */
14+
struct _cpu_arch {
15+
16+
#if defined(CONFIG_FPU_SHARING)
17+
/*
18+
* A 'sse_owner' field does not exist in addition to the 'fpu_owner'
19+
* field since it's not possible to divide the IA-32 non-integer
20+
* registers into 2 distinct blocks owned by differing threads. In
21+
* other words, given that the 'fxnsave/fxrstor' instructions
22+
* save/restore both the X87 FPU and XMM registers, it's not possible
23+
* for a thread to only "own" the XMM registers.
24+
*/
25+
26+
struct k_thread *fpu_owner;
27+
#elif defined(__cplusplus)
28+
/* Ensure this struct does not have a size of 0 which is not allowed in C++. */
29+
uint8_t dummy;
30+
#endif
31+
};
32+
33+
#endif /* ZEPHYR_INCLUDE_X86_STRUCTS_H_ */

include/zephyr/kernel_structs.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,6 @@ struct z_kernel {
217217
struct _ready_q ready_q;
218218
#endif
219219

220-
#ifdef CONFIG_FPU_SHARING
221-
/*
222-
* A 'current_sse' field does not exist in addition to the 'current_fp'
223-
* field since it's not possible to divide the IA-32 non-integer
224-
* registers into 2 distinct blocks owned by differing threads. In
225-
* other words, given that the 'fxnsave/fxrstor' instructions
226-
* save/restore both the X87 FPU and XMM registers, it's not possible
227-
* for a thread to only "own" the XMM registers.
228-
*/
229-
230-
/* thread that owns the FP regs */
231-
struct k_thread *current_fp;
232-
#endif
233-
234220
#if defined(CONFIG_THREAD_MONITOR)
235221
struct k_thread *threads; /* singly linked list of ALL threads */
236222
#endif

kernel/include/kernel_offsets.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ GEN_OFFSET_SYM(_kernel_t, ready_q);
4848
GEN_OFFSET_SYM(_ready_q_t, cache);
4949
#endif /* CONFIG_SMP */
5050

51-
#ifdef CONFIG_FPU_SHARING
52-
GEN_OFFSET_SYM(_kernel_t, current_fp);
53-
#endif /* CONFIG_FPU_SHARING */
54-
5551
GEN_OFFSET_SYM(_thread_base_t, user_options);
5652

5753
GEN_OFFSET_SYM(_thread_t, base);

kernel/include/offsets_short.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
#define _kernel_offset_to_idle \
3535
(___kernel_t_idle_OFFSET)
3636

37-
#define _kernel_offset_to_current_fp \
38-
(___kernel_t_current_fp_OFFSET)
39-
4037
#define _kernel_offset_to_ready_q_cache \
4138
(___kernel_t_ready_q_OFFSET + ___ready_q_t_cache_OFFSET)
4239

0 commit comments

Comments
 (0)