:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / msvcrt / setjmp / setjmp.c
1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
2 /* modified by Boudewijn Dekker */
3 /* ms uses a smaller jmp_buf structure */
4 /* might do a realloc in setjmp */
5
6 typedef struct {
7   unsigned int __eax, __ebx, __ecx, __edx, __esi;
8   unsigned int __edi, __ebp, __esp, __eip, __eflags;
9   unsigned short __cs, __ds, __es, __fs, __gs, __ss;
10   unsigned long __sigmask; /* for POSIX signals only */
11   unsigned long __signum; /* for expansion */
12   unsigned long __exception_ptr; /* pointer to previous exception */
13   unsigned char __fpu_state[108]; /* for future use */
14 } jmp_buf[1];
15
16
17 /* jumps back to position specified in jmp_buf */
18
19 int longjmp( jmp_buf env, int value )
20 {
21   //push ebp             generated by the compiler
22   //mov ebp, esp
23
24   __asm__ __volatile__ (
25         "movl   8(%ebp),%edi\n\t"       /* get jmp_buf */
26         "movl   12(%ebp),%eax\n\t"      /* store retval in j->eax */
27         "movl   %eax,0(%edi)\n\t"
28
29         "movw   46(%edi),%fs\n\t"
30         "movw   48(%edi),%gs\n\t"
31         "movl   4(%edi),%ebx\n\t"
32         "movl   8(%edi),%ecx\n\t"
33         "movl   12(%edi),%edx\n\t"
34         "movl   24(%edi),%ebp\n\t"
35
36         /* Now for some uglyness.  The jmp_buf structure may be ABOVE the
37            point on the new SS:ESP we are moving to.  We don't allow overlap,
38            but do force that it always be valid.  We will use ES:ESI for
39            our new stack before swapping to it.  */
40
41         "movw   50(%edi),%es\n\t"
42         "movl   28(%edi),%esi\n\t"
43         "subl   $28,%esi\n\t"   /* We need 7 working longwords on stack */
44
45         "movl   60(%edi),%eax\n\t"
46         "es\n\t"
47         "movl   %eax,(%esi)\n\t"        /* Exception pointer */
48
49         "movzwl 42(%edi),%eax\n\t"
50         "es\n\t"
51         "movl   %eax,4(%esi)\n\t"       /* DS */
52
53         "movl   20(%edi),%eax\n\t"
54         "es\n\t"
55         "movl   %eax,8(%esi)\n\t"       /* EDI */
56
57         "movl   16(%edi),%eax\n\t"
58         "es\n\t"
59         "movl   %eax,12(%esi)\n\t"      /* ESI */
60
61         "movl   32(%edi),%eax\n\t"
62         "es\n\t"
63         "movl   %eax,16(%esi)\n\t"      /* EIP - start of IRET frame */
64
65         "movl   40(%edi),%eax\n\t"
66         "es\n\t"
67         "movl   %eax,20(%esi)\n\t"      /* CS */
68
69         "movl   36(%edi),%eax\n\t"
70         "es\n\t"
71         "movl   %eax,24(%esi)\n\t"      /* EFLAGS */
72
73         "movl   0(%edi),%eax\n\t"
74         "movw   44(%edi),%es\n\t"
75
76         "movw   50(%edi),%ss\n\t"
77         "movl   %esi,%esp\n\t"
78
79         //"popl ___djgpp_exception_state_ptr\n\t"
80         "popl   %edi\n\t"       // dummy popl instead of djgpp_exception_state_ptr
81         "popl   %ds\n\t"
82         "popl   %edi\n\t"
83         "popl   %esi\n\t"
84
85         "iret\n\t"              /* actually jump to new cs:eip loading flags */
86         );
87
88   return value; // dummy return never reached
89 }
90
91
92 int _setjmp( jmp_buf env )
93 {
94   //push ebp             generated by the compiler
95   //mov ebp, esp
96
97   __asm__ __volatile__ (
98         "pushl  %edi\n\t"
99         "movl   8(%ebp),%edi\n\t"
100
101         "movl   %eax, (%edi)\n\t"
102         "movl   %ebx,4(%edi)\n\t"
103         "movl   %ecx,8(%edi)\n\t"
104         "movl   %edx,12(%edi)\n\t"
105         "movl   %esi,16(%edi)\n\t"
106
107         "movl   -4(%ebp),%eax\n\t"
108         "movl   %eax,20(%edi)\n\t"
109
110         "movl   (%ebp),%eax\n\t"
111         "movl   %eax,24(%edi)\n\t"
112
113         "movl   %esp,%eax\n\t"
114         "addl   $12,%eax\n\t"
115         "movl   %eax,28(%edi)\n\t"
116
117         "movl   4(%ebp),%eax\n\t"
118         "movl   %eax,32(%edi)\n\t"
119
120         "pushfl\n\t"
121         "popl   36(%edi)\n\t"
122
123         "movw   %cs, 40(%edi)\n\t"
124         "movw   %ds, 42(%edi)\n\t"
125         "movw   %es, 44(%edi)\n\t"
126         "movw   %fs, 46(%edi)\n\t"
127         "movw   %gs, 48(%edi)\n\t"
128         "movw   %ss, 50(%edi)\n\t"
129
130         //movl  ___djgpp_exception_state_ptr, %eax
131         //movl  %eax, 60(%edi)
132
133         "popl   %edi\n\t"
134         );
135
136   return 0;
137 }