:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / ke / i386 / fpu.c
1 /*
2  *  ReactOS kernel
3  *  Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*
20  * PROJECT:         ReactOS kernel
21  * FILE:            ntoskrnl/ke/i386/fpu.c
22  * PURPOSE:         Handles the FPU
23  * PROGRAMMER:      David Welch (welch@mcmail.com)
24  * UPDATE HISTORY:
25  *                  Created 22/05/98
26  */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <ddk/ntddk.h>
31 #include <internal/ke.h>
32 #include <internal/mm.h>
33 #include <internal/ps.h>
34
35 #define NDEBUG
36 #include <internal/debug.h>
37
38 /* GLOBALS *******************************************************************/
39
40 ULONG HardwareMathSupport;
41
42 /* FUNCTIONS *****************************************************************/
43
44 VOID 
45 KiCheckFPU(VOID)
46 {
47    unsigned short int status;
48    int cr0;
49    
50    HardwareMathSupport = 0;
51    
52    __asm__("movl %%cr0, %0\n\t" : "=a" (cr0));
53    /* Set NE and MP. */
54    cr0 = cr0 | 0x22;
55    /* Clear EM */
56    cr0 = cr0 & (~0x4);
57    __asm__("movl %0, %%cr0\n\t" : : "a" (cr0));
58
59    __asm__("clts\n\t");
60    __asm__("fninit\n\t");
61    __asm__("fstsw %0\n\t" : "=a" (status));
62    if (status != 0)
63      {
64         __asm__("movl %%cr0, %0\n\t" : "=a" (cr0));
65         /* Set the EM flag in CR0 so any FPU instructions cause a trap. */
66         cr0 = cr0 | 0x4;
67         __asm__("movl %0, %%cr0\n\t" :
68                 : "a" (cr0));
69         return;
70      }
71    /* fsetpm for i287, ignored by i387 */
72    __asm__(".byte 0xDB, 0xE4\n\t");
73    HardwareMathSupport = 1;
74 }