update for HEAD-2003091401
[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 #ifndef CONTEXT_CONTROL
38 #define CONTEXT_CONTROL         (CONTEXT_i386 | 1)      
39 #endif
40 #ifndef CONTEXT_INTEGER
41 #define CONTEXT_INTEGER         (CONTEXT_i386 | 2)      
42 #endif
43 #ifndef CONTEXT_SEGMENTS
44 #define CONTEXT_SEGMENTS        (CONTEXT_i386 | 4)      
45 #endif
46 #ifndef CONTEXT_FLOATING_POINT
47 #define CONTEXT_FLOATING_POINT  (CONTEXT_i386 | 8)      
48 #endif
49 #ifndef CONTEXT_DEBUG_REGISTERS
50 #define CONTEXT_DEBUG_REGISTERS (CONTEXT_i386 | 0x10)
51 #endif
52 #ifndef CONTEXT_FULL
53 #define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS)
54 #endif
55         
56 /* FUNCTIONS *****************************************************************/
57
58 /*
59  *
60  */
61
62 .globl _PsBeginThreadWithContextInternal
63 .globl _PsBeginThread
64
65 _PsBeginThread:
66         /*
67          * This isn't really a function, we are called as the return address
68          * of the context switch function
69          */
70
71         /*
72          * Do the necessary prolog after a context switch
73          */
74         call    _PiBeforeBeginThread
75
76         /*
77          * Call the actual start of the thread
78          */
79         movl    4(%esp), %ebx                /* Start routine */
80         movl    8(%esp), %eax                /* Start context */
81         pushl   %eax
82         call    *%ebx                        /* Call the start routine */
83         addl    $4, %esp
84
85         /*
86          * Terminate the thread
87          */
88         pushl   %eax
89         call    _PsTerminateSystemThread@4
90         addl    $4, %esp
91
92         /*
93          * If that fails then bug check
94          */
95         pushl   $0
96         call    _KeBugCheck@4
97         addl    $4, %esp
98
99         /*
100          * And if that fails then loop
101          */
102 .1:
103         jmp     .1
104
105
106 _PsBeginThreadWithContextInternal:
107         /*
108          * This isn't really a function, we are called as the return
109          * address of a context switch
110          */
111
112         /*
113          * Do the necessary prolog before the context switch
114          */
115         call    _PiBeforeBeginThread
116
117         /*
118          * Load the context flags.
119          */
120         popl    %ebx
121         
122         /*
123          * Load the debugging registers
124          */
125         testl   $(CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386), %ebx
126         jz      .L1
127         popl    %eax            /* Dr0 */
128         movl    %eax, %dr0
129         popl    %eax            /* Dr1 */
130         movl    %eax, %dr1
131         popl    %eax            /* Dr2 */
132         movl    %eax, %dr2
133         popl    %eax            /* Dr3 */
134         movl    %eax, %dr3
135         popl    %eax            /* Dr6 */
136         movl    %eax, %dr6
137         popl    %eax            /* Dr7 */
138         movl    %eax, %dr7
139         jmp     .L3
140 .L1:
141         addl    $24, %esp
142 .L3:    
143         
144         /*
145          * Load the floating point registers
146          */
147         movl    (_HardwareMathSupport), %eax
148         testl   %eax,%eax
149         jz      .L2
150         testl   $(CONTEXT_FLOATING_POINT & ~CONTEXT_i386), %ebx
151         jz      .L2
152         frstor  (%esp)
153 .L2:
154         addl    $112, %esp
155
156         /* Load the rest of the thread's user mode context. */
157         movl    $0, %eax
158         jmp     KeReturnFromSystemCallWithHook
159