init.c 7.2 KB
Newer Older
1
/*
2
 * x86 FPU boot time init code:
3
 */
4
#include <asm/fpu/internal.h>
5
#include <asm/tlbflush.h>
6 7
#include <asm/setup.h>
#include <asm/cmdline.h>
8

9
#include <linux/sched.h>
10
#include <linux/init.h>
11

I
Ingo Molnar 已提交
12 13 14 15 16 17 18 19
/*
 * Initialize the registers found in all CPUs, CR0 and CR4:
 */
static void fpu__init_cpu_generic(void)
{
	unsigned long cr0;
	unsigned long cr4_mask = 0;

20
	if (boot_cpu_has(X86_FEATURE_FXSR))
I
Ingo Molnar 已提交
21
		cr4_mask |= X86_CR4_OSFXSR;
22
	if (boot_cpu_has(X86_FEATURE_XMM))
I
Ingo Molnar 已提交
23 24 25 26 27 28
		cr4_mask |= X86_CR4_OSXMMEXCPT;
	if (cr4_mask)
		cr4_set_bits(cr4_mask);

	cr0 = read_cr0();
	cr0 &= ~(X86_CR0_TS|X86_CR0_EM); /* clear TS and EM */
29
	if (!boot_cpu_has(X86_FEATURE_FPU))
I
Ingo Molnar 已提交
30 31
		cr0 |= X86_CR0_EM;
	write_cr0(cr0);
32 33

	/* Flush out any pending x87 state: */
34
#ifdef CONFIG_MATH_EMULATION
35
	if (!boot_cpu_has(X86_FEATURE_FPU))
36 37 38 39
		fpstate_init_soft(&current->thread.fpu.state.soft);
	else
#endif
		asm volatile ("fninit");
I
Ingo Molnar 已提交
40 41 42
}

/*
43
 * Enable all supported FPU features. Called when a CPU is brought online:
I
Ingo Molnar 已提交
44 45 46 47 48 49 50
 */
void fpu__init_cpu(void)
{
	fpu__init_cpu_generic();
	fpu__init_cpu_xstate();
}

51
static bool fpu__probe_without_cpuid(void)
52 53 54 55 56 57 58 59 60 61
{
	unsigned long cr0;
	u16 fsw, fcw;

	fsw = fcw = 0xffff;

	cr0 = read_cr0();
	cr0 &= ~(X86_CR0_TS | X86_CR0_EM);
	write_cr0(cr0);

62 63 64
	asm volatile("fninit ; fnstsw %0 ; fnstcw %1" : "+m" (fsw), "+m" (fcw));

	pr_info("x86/fpu: Probing for FPU: FSW=0x%04hx FCW=0x%04hx\n", fsw, fcw);
65

66 67 68 69 70 71 72 73 74
	return fsw == 0 && (fcw & 0x103f) == 0x003f;
}

static void fpu__init_system_early_generic(struct cpuinfo_x86 *c)
{
	if (!boot_cpu_has(X86_FEATURE_CPUID) &&
	    !test_bit(X86_FEATURE_FPU, (unsigned long *)cpu_caps_cleared)) {
		if (fpu__probe_without_cpuid())
			setup_force_cpu_cap(X86_FEATURE_FPU);
75
		else
76
			setup_clear_cpu_cap(X86_FEATURE_FPU);
77
	}
78 79

#ifndef CONFIG_MATH_EMULATION
80
	if (!test_cpu_cap(&boot_cpu_data, X86_FEATURE_FPU)) {
81
		pr_emerg("x86/fpu: Giving up, no FPU found and no math emulation present\n");
82 83 84 85
		for (;;)
			asm volatile("hlt");
	}
#endif
86 87
}

88 89 90
/*
 * Boot time FPU feature detection code:
 */
91
unsigned int mxcsr_feature_mask __read_mostly = 0xffffffffu;
92

93
static void __init fpu__init_system_mxcsr(void)
94
{
95
	unsigned int mask = 0;
96

97
	if (boot_cpu_has(X86_FEATURE_FXSR)) {
98 99
		/* Static because GCC does not get 16-byte stack alignment right: */
		static struct fxregs_state fxregs __initdata;
100

101
		asm volatile("fxsave %0" : "+m" (fxregs));
102

103
		mask = fxregs.mxcsr_mask;
104 105 106 107 108 109

		/*
		 * If zero then use the default features mask,
		 * which has all features set, except the
		 * denormals-are-zero feature bit:
		 */
110 111 112 113 114 115
		if (mask == 0)
			mask = 0x0000ffbf;
	}
	mxcsr_feature_mask &= mask;
}

116 117 118
/*
 * Once per bootup FPU initialization sequences that will run on most x86 CPUs:
 */
119
static void __init fpu__init_system_generic(void)
120 121 122 123 124
{
	/*
	 * Set up the legacy init FPU context. (xstate init might overwrite this
	 * with a more modern format, if the CPU supports it.)
	 */
125
	fpstate_init(&init_fpstate);
126 127 128 129

	fpu__init_system_mxcsr();
}

130 131 132 133 134 135
/*
 * Size of the FPU context state. All tasks in the system use the
 * same context size, regardless of what portion they use.
 * This is inherent to the XSAVE architecture which puts all state
 * components into a single, continuous memory block:
 */
136 137
unsigned int fpu_kernel_xstate_size;
EXPORT_SYMBOL_GPL(fpu_kernel_xstate_size);
I
Ingo Molnar 已提交
138

139 140 141 142 143 144 145 146 147
/* Get alignment of the TYPE. */
#define TYPE_ALIGN(TYPE) offsetof(struct { char x; TYPE test; }, test)

/*
 * Enforce that 'MEMBER' is the last field of 'TYPE'.
 *
 * Align the computed size with alignment of the TYPE,
 * because that's how C aligns structs.
 */
148
#define CHECK_MEMBER_AT_END_OF(TYPE, MEMBER) \
149 150
	BUILD_BUG_ON(sizeof(TYPE) != ALIGN(offsetofend(TYPE, MEMBER), \
					   TYPE_ALIGN(TYPE)))
151 152

/*
153
 * We append the 'struct fpu' to the task_struct:
154
 */
155
static void __init fpu__init_task_struct_size(void)
156 157 158 159 160 161 162 163 164 165 166 167 168
{
	int task_size = sizeof(struct task_struct);

	/*
	 * Subtract off the static size of the register state.
	 * It potentially has a bunch of padding.
	 */
	task_size -= sizeof(((struct task_struct *)0)->thread.fpu.state);

	/*
	 * Add back the dynamically-calculated register state
	 * size.
	 */
169
	task_size += fpu_kernel_xstate_size;
170 171 172 173 174 175 176 177 178 179 180 181

	/*
	 * We dynamically size 'struct fpu', so we require that
	 * it be at the end of 'thread_struct' and that
	 * 'thread_struct' be at the end of 'task_struct'.  If
	 * you hit a compile error here, check the structure to
	 * see if something got added to the end.
	 */
	CHECK_MEMBER_AT_END_OF(struct fpu, state);
	CHECK_MEMBER_AT_END_OF(struct thread_struct, fpu);
	CHECK_MEMBER_AT_END_OF(struct task_struct, thread);

182
	arch_task_struct_size = task_size;
183 184
}

I
Ingo Molnar 已提交
185
/*
186
 * Set up the user and kernel xstate sizes based on the legacy FPU context size.
I
Ingo Molnar 已提交
187 188 189 190
 *
 * We set this up first, and later it will be overwritten by
 * fpu__init_system_xstate() if the CPU knows about xstates.
 */
191
static void __init fpu__init_system_xstate_size_legacy(void)
192
{
193
	static int on_boot_cpu __initdata = 1;
194 195 196 197

	WARN_ON_FPU(!on_boot_cpu);
	on_boot_cpu = 0;

198
	/*
199
	 * Note that xstate sizes might be overwritten later during
I
Ingo Molnar 已提交
200
	 * fpu__init_system_xstate().
201 202
	 */

203
	if (!boot_cpu_has(X86_FEATURE_FPU)) {
204 205 206 207 208 209
		/*
		 * Disable xsave as we do not support it if i387
		 * emulation is enabled.
		 */
		setup_clear_cpu_cap(X86_FEATURE_XSAVE);
		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);
210
		fpu_kernel_xstate_size = sizeof(struct swregs_state);
211
	} else {
212
		if (boot_cpu_has(X86_FEATURE_FXSR))
213 214
			fpu_kernel_xstate_size =
				sizeof(struct fxregs_state);
215
		else
216 217
			fpu_kernel_xstate_size =
				sizeof(struct fregs_state);
218
	}
219

220
	fpu_user_xstate_size = fpu_kernel_xstate_size;
221 222
}

223 224 225 226 227 228 229
/*
 * Find supported xfeatures based on cpu features and command-line input.
 * This must be called after fpu__init_parse_early_param() is called and
 * xfeatures_mask is enumerated.
 */
u64 __init fpu__get_supported_xfeatures_mask(void)
{
230
	return XCNTXT_MASK;
231 232
}

233
/* Legacy code to initialize eager fpu mode. */
234
static void __init fpu__init_system_ctx_switch(void)
235
{
236
	static bool on_boot_cpu __initdata = 1;
237 238 239 240 241

	WARN_ON_FPU(!on_boot_cpu);
	on_boot_cpu = 0;

	WARN_ON_FPU(current->thread.fpu.fpstate_active);
242 243
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/*
 * We parse fpu parameters early because fpu__init_system() is executed
 * before parse_early_param().
 */
static void __init fpu__init_parse_early_param(void)
{
	if (cmdline_find_option_bool(boot_command_line, "no387"))
		setup_clear_cpu_cap(X86_FEATURE_FPU);

	if (cmdline_find_option_bool(boot_command_line, "nofxsr")) {
		setup_clear_cpu_cap(X86_FEATURE_FXSR);
		setup_clear_cpu_cap(X86_FEATURE_FXSR_OPT);
		setup_clear_cpu_cap(X86_FEATURE_XMM);
	}

	if (cmdline_find_option_bool(boot_command_line, "noxsave"))
		fpu__xstate_clear_all_cpu_caps();

	if (cmdline_find_option_bool(boot_command_line, "noxsaveopt"))
		setup_clear_cpu_cap(X86_FEATURE_XSAVEOPT);

	if (cmdline_find_option_bool(boot_command_line, "noxsaves"))
		setup_clear_cpu_cap(X86_FEATURE_XSAVES);
}

269
/*
270 271
 * Called on the boot CPU once per system bootup, to set up the initial
 * FPU state that is later cloned into all processes:
272
 */
273
void __init fpu__init_system(struct cpuinfo_x86 *c)
274
{
275
	fpu__init_parse_early_param();
276 277
	fpu__init_system_early_generic(c);

278 279 280 281
	/*
	 * The FPU has to be operational for some of the
	 * later FPU init activities:
	 */
282
	fpu__init_cpu();
283

284
	fpu__init_system_generic();
285
	fpu__init_system_xstate_size_legacy();
I
Ingo Molnar 已提交
286
	fpu__init_system_xstate();
287
	fpu__init_task_struct_size();
288

289
	fpu__init_system_ctx_switch();
290
}