:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / ke / i386 / bthread.S
1 /*
2  *  ReactOS kernel
3  *  Copyright (C) 2000 David Welch <welch@cwcom.net>
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 /* $Id$
20  *
21  * COPYRIGHT:       See COPYING in the top level directory
22  * PROJECT:         ReactOS kernel
23  * FILE:            ntoskrnl/ke/i386/bthread.S
24  * PURPOSE:         Trap handlers
25  * PROGRAMMER:      David Welch (david.welch@seh.ox.ac.uk)
26  */
27
28 /* INCLUDES ******************************************************************/
29         
30 #include <ddk/status.h>
31 #include <internal/i386/segment.h>
32 #include <internal/ps.h>
33 #include <ddk/defines.h>
34
35 /* Values for contextflags */
36 #define CONTEXT_i386    0x10000
37 #define CONTEXT_CONTROL         (CONTEXT_i386 | 1)      
38 #define CONTEXT_INTEGER         (CONTEXT_i386 | 2)      
39 #define CONTEXT_SEGMENTS        (CONTEXT_i386 | 4)      
40 #define CONTEXT_FLOATING_POINT  (CONTEXT_i386 | 8)      
41 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_i386 | 0x10)
42 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS)
43         
44 /* FUNCTIONS *****************************************************************/
45
46 /*
47  *
48  */
49
50 .globl _PsBeginThreadWithContextInternal
51 .globl _PsBeginThread
52
53 _PsBeginThread:
54         /*
55          * This isn't really a function, we are called as the return address
56          * of the context switch function
57          */
58
59         /*
60          * Do the necessary prolog after a context switch
61          */
62         call    _PiBeforeBeginThread
63
64         /*
65          * Call the actual start of the thread
66          */
67         movl    4(%esp), %ebx                /* Start routine */
68         movl    8(%esp), %eax                /* Start context */
69         pushl   %eax
70         call    *%ebx                        /* Call the start routine */
71         addl    $4, %esp
72
73         /*
74          * Terminate the thread
75          */
76         pushl   %eax
77         call    _PsTerminateSystemThread@4
78         addl    $4, %esp
79
80         /*
81          * If that fails then bug check
82          */
83         pushl   $0
84         call    _KeBugCheck@4
85         addl    $4, %esp
86
87         /*
88          * And if that fails then loop
89          */
90 .1:
91         jmp     .1
92
93
94 _PsBeginThreadWithContextInternal:
95         /*
96          * This isn't really a function, we are called as the return
97          * address of a context switch
98          */
99
100         /*
101          * Do the necessary prolog before the context switch
102          */
103         call    _PiBeforeBeginThread
104
105         /*
106          * Load the context flags.
107          */
108         popl    %ebx
109         
110         /*
111          * Load the debugging registers
112          */
113         testl   $CONTEXT_DEBUG_REGISTERS, %ebx
114         jz      .L1
115         popl    %eax            /* Dr0 */
116         movl    %eax, %dr0
117         popl    %eax            /* Dr1 */
118         movl    %eax, %dr1
119         popl    %eax            /* Dr2 */
120         movl    %eax, %dr2
121         popl    %eax            /* Dr3 */
122         movl    %eax, %dr3
123         popl    %eax            /* Dr6 */
124         movl    %eax, %dr6
125         popl    %eax            /* Dr7 */
126         movl    %eax, %dr7
127         jmp     .L3
128 .L1:
129         addl    $24, %esp
130 .L3:    
131         
132         /*
133          * Load the floating point registers
134          */
135         movl    (_HardwareMathSupport), %eax
136         jz      .L2
137         testl   $CONTEXT_FLOATING_POINT, %ebx
138         jz      .L2
139         frstor  (%esp)
140 .L2:
141         addl    $112, %esp
142
143         /* Load the rest of the thread's user mode context. */
144         movl    $0, %eax
145         jmp     KeReturnFromSystemCallWithHook
146