Setup exception frame around 16-bit calls to unwind stack properly.
[wine] / include / stackframe.h
1 /*
2  * 16-bit and 32-bit mode stack frame layout
3  *
4  * Copyright 1995, 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifndef __WINE_STACKFRAME_H
22 #define __WINE_STACKFRAME_H
23
24 #include <string.h>
25
26 #include "thread.h"
27 #include "winnt.h"
28 #include "wine/winbase16.h"
29
30 #include "pshpack1.h"
31
32   /* 32-bit stack layout after CallTo16() */
33 typedef struct _STACK32FRAME
34 {
35     DWORD   restore_addr;   /* 00 return address for restoring code selector */
36     DWORD   codeselector;   /* 04 code selector to restore */
37     EXCEPTION_FRAME frame;  /* 08 Exception frame */
38     SEGPTR  frame16;        /* 10 16-bit frame from last CallFrom16() */
39     DWORD   edi;            /* 14 saved registers */
40     DWORD   esi;            /* 18 */
41     DWORD   edx;            /* 1c */
42     DWORD   ecx;            /* 20 */
43     DWORD   ebx;            /* 24 */
44     DWORD   ebp;            /* 28 saved 32-bit frame pointer */
45     DWORD   retaddr;        /* 2c return address */
46     DWORD   target;         /* 30 target address / CONTEXT86 pointer */
47     DWORD   nb_args;        /* 34 number of 16-bit argument bytes */
48 } STACK32FRAME;
49
50   /* 16-bit stack layout after CallFrom16() */
51 typedef struct _STACK16FRAME
52 {
53     STACK32FRAME *frame32;        /* 00 32-bit frame from last CallTo16() */
54     DWORD         edx;            /* 04 saved registers */
55     DWORD         ecx;            /* 08 */
56     DWORD         ebp;            /* 0c */
57     WORD          ds;             /* 10 */
58     WORD          es;             /* 12 */
59     WORD          fs;             /* 14 */
60     WORD          gs;             /* 16 */
61     DWORD         callfrom_ip;    /* 18 callfrom tail IP */
62     DWORD         module_cs;      /* 1c module code segment */
63     DWORD         relay;          /* 20 relay function address */
64     WORD          entry_ip;       /* 22 entry point IP */
65     DWORD         entry_point;    /* 26 API entry point to call, reused as mutex count */
66     WORD          bp;             /* 2a 16-bit stack frame chain */
67     WORD          ip;             /* 2c return address */
68     WORD          cs;             /* 2e */
69 } STACK16FRAME;
70
71 #include "poppack.h"
72
73 #define THREAD_STACK16(teb)  ((STACK16FRAME*)MapSL((teb)->cur_stack))
74 #define CURRENT_STACK16      (THREAD_STACK16(NtCurrentTeb()))
75 #define CURRENT_DS           (CURRENT_STACK16->ds)
76
77 /* varargs lists on the 16-bit stack */
78
79 typedef void *VA_LIST16;
80
81 #define __VA_ROUNDED16(type) \
82     ((sizeof(type) + sizeof(WORD) - 1) / sizeof(WORD) * sizeof(WORD))
83 #define VA_START16(list) ((list) = (VA_LIST16)(CURRENT_STACK16 + 1))
84 #define VA_ARG16(list,type) \
85     (((list) = (VA_LIST16)((char *)(list) + __VA_ROUNDED16(type))), \
86      *((type *)(void *)((char *)(list) - __VA_ROUNDED16(type))))
87 #define VA_END16(list) ((void)0)
88
89
90 /* Push bytes on the 16-bit stack of a thread;
91  * return a segptr to the first pushed byte
92  */
93 static inline SEGPTR WINE_UNUSED stack16_push( int size )
94 {
95     TEB *teb = NtCurrentTeb();
96     STACK16FRAME *frame = THREAD_STACK16(teb);
97     memmove( (char*)frame - size, frame, sizeof(*frame) );
98     teb->cur_stack -= size;
99     return (SEGPTR)(teb->cur_stack + sizeof(*frame));
100 }
101
102 /* Pop bytes from the 16-bit stack of a thread */
103 static inline void WINE_UNUSED stack16_pop( int size )
104 {
105     TEB *teb = NtCurrentTeb();
106     STACK16FRAME *frame = THREAD_STACK16(teb);
107     memmove( (char*)frame + size, frame, sizeof(*frame) );
108     teb->cur_stack += size;
109 }
110
111 /* Push a DWORD on the 32-bit stack */
112 static inline void WINE_UNUSED stack32_push( CONTEXT86 *context, DWORD val )
113 {
114     context->Esp -= sizeof(DWORD);
115     *(DWORD *)context->Esp = val;
116 }
117
118 /* Pop a DWORD from the 32-bit stack */
119 static inline DWORD WINE_UNUSED stack32_pop( CONTEXT86 *context )
120 {
121     DWORD ret = *(DWORD *)context->Esp;
122     context->Esp += sizeof(DWORD);
123     return ret;
124 }
125
126 #endif /* __WINE_STACKFRAME_H */