~ubuntu-branches/debian/wheezy/linux-2.6/wheezy

« back to all changes in this revision

Viewing changes to arch/x86/kernel/xsave.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno, Martin Michlmayr
  • Date: 2011-04-06 13:53:30 UTC
  • mfrom: (43.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110406135330-wjufxhd0tvn3zx4z
Tags: 2.6.38-3
[ Ben Hutchings ]
* [ppc64] Add to linux-tools package architectures (Closes: #620124)
* [amd64] Save cr4 to mmu_cr4_features at boot time (Closes: #620284)
* appletalk: Fix bugs introduced when removing use of BKL
* ALSA: Fix yet another race in disconnection
* cciss: Fix lost command issue
* ath9k: Fix kernel panic in AR2427
* ses: Avoid kernel panic when lun 0 is not mapped
* PCI/ACPI: Report ASPM support to BIOS if not disabled from command line

[ Aurelien Jarno ]
* rtlwifi: fix build when PCI is not enabled.

[ Martin Michlmayr ]
* rtlwifi: Eliminate udelay calls with too large values (Closes: #620204)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 */
17
17
u64 pcntxt_mask;
18
18
 
 
19
/*
 
20
 * Represents init state for the supported extended state.
 
21
 */
 
22
static struct xsave_struct *init_xstate_buf;
 
23
 
19
24
struct _fpx_sw_bytes fx_sw_reserved;
20
25
#ifdef CONFIG_IA32_EMULATION
21
26
struct _fpx_sw_bytes fx_sw_reserved_ia32;
22
27
#endif
23
28
 
 
29
static unsigned int *xstate_offsets, *xstate_sizes, xstate_features;
 
30
 
 
31
/*
 
32
 * If a processor implementation discern that a processor state component is
 
33
 * in its initialized state it may modify the corresponding bit in the
 
34
 * xsave_hdr.xstate_bv as '0', with out modifying the corresponding memory
 
35
 * layout in the case of xsaveopt. While presenting the xstate information to
 
36
 * the user, we always ensure that the memory layout of a feature will be in
 
37
 * the init state if the corresponding header bit is zero. This is to ensure
 
38
 * that the user doesn't see some stale state in the memory layout during
 
39
 * signal handling, debugging etc.
 
40
 */
 
41
void __sanitize_i387_state(struct task_struct *tsk)
 
42
{
 
43
        u64 xstate_bv;
 
44
        int feature_bit = 0x2;
 
45
        struct i387_fxsave_struct *fx = &tsk->thread.fpu.state->fxsave;
 
46
 
 
47
        if (!fx)
 
48
                return;
 
49
 
 
50
        BUG_ON(task_thread_info(tsk)->status & TS_USEDFPU);
 
51
 
 
52
        xstate_bv = tsk->thread.fpu.state->xsave.xsave_hdr.xstate_bv;
 
53
 
 
54
        /*
 
55
         * None of the feature bits are in init state. So nothing else
 
56
         * to do for us, as the memory layout is upto date.
 
57
         */
 
58
        if ((xstate_bv & pcntxt_mask) == pcntxt_mask)
 
59
                return;
 
60
 
 
61
        /*
 
62
         * FP is in init state
 
63
         */
 
64
        if (!(xstate_bv & XSTATE_FP)) {
 
65
                fx->cwd = 0x37f;
 
66
                fx->swd = 0;
 
67
                fx->twd = 0;
 
68
                fx->fop = 0;
 
69
                fx->rip = 0;
 
70
                fx->rdp = 0;
 
71
                memset(&fx->st_space[0], 0, 128);
 
72
        }
 
73
 
 
74
        /*
 
75
         * SSE is in init state
 
76
         */
 
77
        if (!(xstate_bv & XSTATE_SSE))
 
78
                memset(&fx->xmm_space[0], 0, 256);
 
79
 
 
80
        xstate_bv = (pcntxt_mask & ~xstate_bv) >> 2;
 
81
 
 
82
        /*
 
83
         * Update all the other memory layouts for which the corresponding
 
84
         * header bit is in the init state.
 
85
         */
 
86
        while (xstate_bv) {
 
87
                if (xstate_bv & 0x1) {
 
88
                        int offset = xstate_offsets[feature_bit];
 
89
                        int size = xstate_sizes[feature_bit];
 
90
 
 
91
                        memcpy(((void *) fx) + offset,
 
92
                               ((void *) init_xstate_buf) + offset,
 
93
                               size);
 
94
                }
 
95
 
 
96
                xstate_bv >>= 1;
 
97
                feature_bit++;
 
98
        }
 
99
}
 
100
 
24
101
/*
25
102
 * Check for the presence of extended state information in the
26
103
 * user fpstate pointer in the sigcontext.
36
113
 
37
114
        err = __copy_from_user(fx_sw_user, &buf->sw_reserved[0],
38
115
                               sizeof(struct _fpx_sw_bytes));
39
 
 
40
116
        if (err)
41
 
                return err;
 
117
                return -EFAULT;
42
118
 
43
119
        /*
44
120
         * First Magic check failed.
45
121
         */
46
122
        if (fx_sw_user->magic1 != FP_XSTATE_MAGIC1)
47
 
                return -1;
 
123
                return -EINVAL;
48
124
 
49
125
        /*
50
126
         * Check for error scenarios.
52
128
        if (fx_sw_user->xstate_size < min_xstate_size ||
53
129
            fx_sw_user->xstate_size > xstate_size ||
54
130
            fx_sw_user->xstate_size > fx_sw_user->extended_size)
55
 
                return -1;
 
131
                return -EINVAL;
56
132
 
57
133
        err = __get_user(magic2, (__u32 *) (((void *)fpstate) +
58
134
                                            fx_sw_user->extended_size -
59
135
                                            FP_XSTATE_MAGIC2_SIZE));
 
136
        if (err)
 
137
                return err;
60
138
        /*
61
139
         * Check for the presence of second magic word at the end of memory
62
140
         * layout. This detects the case where the user just copied the legacy
63
141
         * fpstate layout with out copying the extended state information
64
142
         * in the memory layout.
65
143
         */
66
 
        if (err || magic2 != FP_XSTATE_MAGIC2)
67
 
                return -1;
 
144
        if (magic2 != FP_XSTATE_MAGIC2)
 
145
                return -EFAULT;
68
146
 
69
147
        return 0;
70
148
}
91
169
                return 0;
92
170
 
93
171
        if (task_thread_info(tsk)->status & TS_USEDFPU) {
94
 
                /*
95
 
                 * Start with clearing the user buffer. This will present a
96
 
                 * clean context for the bytes not touched by the fxsave/xsave.
97
 
                 */
98
 
                err = __clear_user(buf, sig_xstate_size);
99
 
                if (err)
100
 
                        return err;
101
 
 
102
 
                if (task_thread_info(tsk)->status & TS_XSAVE)
 
172
                if (use_xsave())
103
173
                        err = xsave_user(buf);
104
174
                else
105
175
                        err = fxsave_user(buf);
109
179
                task_thread_info(tsk)->status &= ~TS_USEDFPU;
110
180
                stts();
111
181
        } else {
112
 
                if (__copy_to_user(buf, &tsk->thread.xstate->fxsave,
 
182
                sanitize_i387_state(tsk);
 
183
                if (__copy_to_user(buf, &tsk->thread.fpu.state->fxsave,
113
184
                                   xstate_size))
114
185
                        return -1;
115
186
        }
116
187
 
117
188
        clear_used_math(); /* trigger finit */
118
189
 
119
 
        if (task_thread_info(tsk)->status & TS_XSAVE) {
 
190
        if (use_xsave()) {
120
191
                struct _fpstate __user *fx = buf;
121
192
                struct _xstate __user *x = buf;
122
193
                u64 xstate_bv;
184
255
         * init the state skipped by the user.
185
256
         */
186
257
        mask = pcntxt_mask & ~mask;
187
 
 
188
 
        xrstor_state(init_xstate_buf, mask);
 
258
        if (unlikely(mask))
 
259
                xrstor_state(init_xstate_buf, mask);
189
260
 
190
261
        return 0;
191
262
 
225
296
                clts();
226
297
                task_thread_info(current)->status |= TS_USEDFPU;
227
298
        }
228
 
        if (task_thread_info(tsk)->status & TS_XSAVE)
 
299
        if (use_xsave())
229
300
                err = restore_user_xstate(buf);
230
301
        else
231
302
                err = fxrstor_checking((__force struct i387_fxsave_struct *)
274
345
#endif
275
346
}
276
347
 
277
 
/*
278
 
 * Represents init state for the supported extended state.
279
 
 */
280
 
struct xsave_struct *init_xstate_buf;
281
 
 
282
348
#ifdef CONFIG_X86_64
283
349
unsigned int sig_xstate_size = sizeof(struct _fpstate);
284
350
#endif
286
352
/*
287
353
 * Enable the extended processor state save/restore feature
288
354
 */
289
 
void __cpuinit xsave_init(void)
 
355
static inline void xstate_enable(void)
290
356
{
291
 
        if (!cpu_has_xsave)
292
 
                return;
293
 
 
294
357
        set_in_cr4(X86_CR4_OSXSAVE);
295
 
 
296
 
        /*
297
 
         * Enable all the features that the HW is capable of
298
 
         * and the Linux kernel is aware of.
299
 
         */
300
358
        xsetbv(XCR_XFEATURE_ENABLED_MASK, pcntxt_mask);
301
359
}
302
360
 
303
361
/*
 
362
 * Record the offsets and sizes of different state managed by the xsave
 
363
 * memory layout.
 
364
 */
 
365
static void __init setup_xstate_features(void)
 
366
{
 
367
        int eax, ebx, ecx, edx, leaf = 0x2;
 
368
 
 
369
        xstate_features = fls64(pcntxt_mask);
 
370
        xstate_offsets = alloc_bootmem(xstate_features * sizeof(int));
 
371
        xstate_sizes = alloc_bootmem(xstate_features * sizeof(int));
 
372
 
 
373
        do {
 
374
                cpuid_count(XSTATE_CPUID, leaf, &eax, &ebx, &ecx, &edx);
 
375
 
 
376
                if (eax == 0)
 
377
                        break;
 
378
 
 
379
                xstate_offsets[leaf] = ebx;
 
380
                xstate_sizes[leaf] = eax;
 
381
 
 
382
                leaf++;
 
383
        } while (1);
 
384
}
 
385
 
 
386
/*
304
387
 * setup the xstate image representing the init state
305
388
 */
306
389
static void __init setup_xstate_init(void)
307
390
{
308
 
        init_xstate_buf = alloc_bootmem(xstate_size);
 
391
        setup_xstate_features();
 
392
 
 
393
        /*
 
394
         * Setup init_xstate_buf to represent the init state of
 
395
         * all the features managed by the xsave
 
396
         */
 
397
        init_xstate_buf = alloc_bootmem_align(xstate_size,
 
398
                                              __alignof__(struct xsave_struct));
309
399
        init_xstate_buf->i387.mxcsr = MXCSR_DEFAULT;
 
400
 
 
401
        clts();
 
402
        /*
 
403
         * Init all the features state with header_bv being 0x0
 
404
         */
 
405
        xrstor_state(init_xstate_buf, -1);
 
406
        /*
 
407
         * Dump the init state again. This is to identify the init state
 
408
         * of any feature which is not represented by all zero's.
 
409
         */
 
410
        xsave_state(init_xstate_buf, -1);
 
411
        stts();
310
412
}
311
413
 
312
414
/*
313
415
 * Enable and initialize the xsave feature.
314
416
 */
315
 
void __ref xsave_cntxt_init(void)
 
417
static void __init xstate_enable_boot_cpu(void)
316
418
{
317
419
        unsigned int eax, ebx, ecx, edx;
318
420
 
319
 
        cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
 
421
        if (boot_cpu_data.cpuid_level < XSTATE_CPUID) {
 
422
                WARN(1, KERN_ERR "XSTATE_CPUID missing\n");
 
423
                return;
 
424
        }
 
425
 
 
426
        cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
320
427
        pcntxt_mask = eax + ((u64)edx << 32);
321
428
 
322
429
        if ((pcntxt_mask & XSTATE_FPSSE) != XSTATE_FPSSE) {
329
436
         * Support only the state known to OS.
330
437
         */
331
438
        pcntxt_mask = pcntxt_mask & XCNTXT_MASK;
332
 
        xsave_init();
 
439
 
 
440
        xstate_enable();
333
441
 
334
442
        /*
335
443
         * Recompute the context size for enabled features
336
444
         */
337
 
        cpuid_count(0xd, 0, &eax, &ebx, &ecx, &edx);
 
445
        cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
338
446
        xstate_size = ebx;
339
447
 
 
448
        update_regset_xstate_info(xstate_size, pcntxt_mask);
340
449
        prepare_fx_sw_frame();
341
450
 
342
451
        setup_xstate_init();
345
454
               "cntxt size 0x%x\n",
346
455
               pcntxt_mask, xstate_size);
347
456
}
 
457
 
 
458
/*
 
459
 * For the very first instance, this calls xstate_enable_boot_cpu();
 
460
 * for all subsequent instances, this calls xstate_enable().
 
461
 *
 
462
 * This is somewhat obfuscated due to the lack of powerful enough
 
463
 * overrides for the section checks.
 
464
 */
 
465
void __cpuinit xsave_init(void)
 
466
{
 
467
        static __refdata void (*next_func)(void) = xstate_enable_boot_cpu;
 
468
        void (*this_func)(void);
 
469
 
 
470
        if (!cpu_has_xsave)
 
471
                return;
 
472
 
 
473
        this_func = next_func;
 
474
        next_func = xstate_enable;
 
475
        this_func();
 
476
}