blob: a589a790c22ad57d5399677bfd96103f61b68a5f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2* Copyright (c) 2015 Intel Corporation
3*
4* Permission is hereby granted, free of charge, to any person obtaining
5* a copy of this software and associated documentation files
6* (the "Software"), to deal in the Software without restriction,
7* including without limitation the rights to use, copy, modify, merge,
8* publish, distribute, sublicense, and/or sell copies of the Software,
9* and to permit persons to whom the Software is furnished to do so,
10* subject to the following conditions:
11*
12* The above copyright notice and this permission notice shall be
13* included in all copies or substantial portions of the Software.
14*
15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24#include <arch/x86.h>
25#include <arch/fpu.h>
26#include <kernel/thread.h>
27
28#if X86_WITH_FPU
29
30/* CPUID EAX = 1 return values */
31
32#define ECX_SSE3 (0x00000001 << 0)
33#define ECX_SSSE3 (0x00000001 << 9)
34#define ECX_SSE4_1 (0x00000001 << 19)
35#define ECX_SSE4_2 (0x00000001 << 20)
36#define EDX_FXSR (0x00000001 << 24)
37#define EDX_SSE (0x00000001 << 25)
38#define EDX_SSE2 (0x00000001 << 26)
39#define EDX_FPU (0x00000001 << 0)
40
41#define FPU_CAP(ecx, edx) ((edx & EDX_FPU) != 0)
42
43#define SSE_CAP(ecx, edx) ( \
44 ((ecx & (ECX_SSE3 | ECX_SSSE3 | ECX_SSE4_1 | ECX_SSE4_2)) != 0) || \
45 ((edx & (EDX_SSE | EDX_SSE2)) != 0) \
46 )
47
48#define FXSAVE_CAP(ecx, edx) ((edx & EDX_FXSR) != 0)
49
50static int fp_supported;
51static thread_t *fp_owner;
52
53static void get_cpu_cap(uint32_t *ecx, uint32_t *edx)
54{
55 uint32_t eax = 1;
56
57 __asm__ __volatile__
58 ("cpuid" : "=c" (*ecx), "=d" (*edx) : "a" (eax));
59}
60
61void fpu_init(void)
62{
63 uint32_t ecx = 0, edx = 0;
64 uint16_t fcw;
65 uint32_t mxcsr;
66
67#ifdef ARCH_X86_64
68 uint64_t x;
69#else
70 uint32_t x;
71#endif
72
73 fp_supported = 0;
74 fp_owner = NULL;
75
76 get_cpu_cap(&ecx, &edx);
77
78 if (!FPU_CAP(ecx, edx) || !SSE_CAP(ecx, edx) || !FXSAVE_CAP(ecx, edx))
79 return;
80
81 fp_supported = 1;
82
83 /* No x87 emul, monitor co-processor */
84
85 x = x86_get_cr0();
86 x &= ~X86_CR0_EM;
87 x |= X86_CR0_NE;
88 x |= X86_CR0_MP;
89 x86_set_cr0(x);
90
91 /* Init x87 and unmask all exceptions */
92
93 __asm__ __volatile__ ("finit");
94 __asm__ __volatile__("fstcw %0" : "=m" (fcw));
95 fcw &= 0xffc0;
96 __asm__ __volatile__("fldcw %0" : : "m" (fcw));
97
98 /* Init SSE and unmask all exceptions */
99
100 x = x86_get_cr4();
101 x |= X86_CR4_OSXMMEXPT;
102 x |= X86_CR4_OSFXSR;
103 x &= ~X86_CR4_OSXSAVE;
104 x86_set_cr4(x);
105
106 __asm__ __volatile__("stmxcsr %0" : "=m" (mxcsr));
107 mxcsr &= 0x0000003f;
108 __asm__ __volatile__("ldmxcsr %0" : : "m" (mxcsr));
109
110 x86_set_cr0(x86_get_cr0() | X86_CR0_TS);
111 return;
112}
113
114void fpu_context_switch(thread_t *old_thread, thread_t *new_thread)
115{
116 if (fp_supported == 0)
117 return;
118
119 if (new_thread != fp_owner)
120 x86_set_cr0(x86_get_cr0() | X86_CR0_TS);
121 else
122 x86_set_cr0(x86_get_cr0() & ~X86_CR0_TS);
123
124 return;
125}
126
127void fpu_dev_na_handler(void)
128{
129 thread_t *self;
130
131 x86_set_cr0(x86_get_cr0() & ~X86_CR0_TS);
132
133 if (fp_supported == 0)
134 return;
135
136 self = get_current_thread();
137
138 if ((fp_owner != NULL) && (fp_owner != self)) {
139 __asm__ __volatile__("fxsave %0" : "=m" (*fp_owner->arch.fpu_states));
140 __asm__ __volatile__("fxrstor %0" : : "m" (*self->arch.fpu_states));
141 }
142
143 fp_owner = self;
144 return;
145}
146#endif
147
148/* End of file */