msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / winedos / relay.c
blob9f6692c026ba95535108162d494f85fda1141d73
1 /*
2 * Routines for dynamically building calls to Wine from
3 * protected mode applications.
5 * Copyright 2002 Jukka Heinonen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "dosexe.h"
22 #include "wine/winbase16.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(int);
28 * Magic DWORD used to check stack integrity.
30 #define RELAY_MAGIC 0xabcdef00
33 * Memory block for temporary 16-bit stacks used with relay calls.
35 typedef struct {
36 DWORD inuse; /* non-zero if stack block is in use */
37 DWORD eip; /* saved ip */
38 DWORD seg_cs; /* saved cs */
39 DWORD esp; /* saved sp */
40 DWORD seg_ss; /* saved ss */
41 DWORD stack_bottom; /* guard dword */
42 BYTE stack[256-7*4]; /* 16-bit stack */
43 DWORD stack_top; /* guard dword */
44 } RELAY_Stack16;
47 /**********************************************************************
48 * RELAY_GetPointer
50 * Get pointer to stack block when given esp pointing to 16-bit stack
51 * inside relay data segment.
53 static RELAY_Stack16 *RELAY_GetPointer( DWORD offset )
55 offset = offset / sizeof(RELAY_Stack16) * sizeof(RELAY_Stack16);
56 return MapSL(MAKESEGPTR(DOSVM_dpmi_segments->relay_data_sel, offset));
60 /**********************************************************************
61 * RELAY_MakeShortContext
63 * Allocate separate 16-bit stack, make stack pointer point to this
64 * stack and make code pointer point to stub that restores everything.
65 * So, after this routine, SS and CS are guaranteed to be 16-bit.
67 * Note: This might be called from signal handler, so the stack
68 * allocation algorithm must be signal safe.
70 static void RELAY_MakeShortContext( CONTEXT86 *context )
72 DWORD offset = offsetof(RELAY_Stack16, stack_top);
73 RELAY_Stack16 *stack = RELAY_GetPointer( 0 );
75 while (stack->inuse && offset < DOSVM_RELAY_DATA_SIZE) {
76 stack++;
77 offset += sizeof(RELAY_Stack16);
80 if (offset >= DOSVM_RELAY_DATA_SIZE)
81 ERR( "Too many nested interrupts!\n" );
83 stack->inuse = 1;
84 stack->eip = context->Eip;
85 stack->seg_cs = context->SegCs;
86 stack->esp = context->Esp;
87 stack->seg_ss = context->SegSs;
89 stack->stack_bottom = RELAY_MAGIC;
90 stack->stack_top = RELAY_MAGIC;
92 context->SegSs = DOSVM_dpmi_segments->relay_data_sel;
93 context->Esp = offset;
94 context->SegCs = DOSVM_dpmi_segments->relay_code_sel;
95 context->Eip = 3;
99 /**********************************************************************
100 * RELAY_RelayStub
102 * This stub is called by __wine_call_from_16_regs in order to marshall
103 * relay parameters.
105 static void __stdcall RELAY_RelayStub( DOSRELAY proc,
106 unsigned char *args,
107 void *ctx86 )
109 if (proc)
111 CONTEXT86 *context = (CONTEXT86*)ctx86;
112 RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
114 DWORD old_seg_cs = context->SegCs;
115 DWORD old_eip = context->Eip;
116 DWORD old_seg_ss = context->SegSs;
117 DWORD old_esp = context->Esp;
119 context->SegCs = stack->seg_cs;
120 context->Eip = stack->eip;
121 context->SegSs = stack->seg_ss;
122 context->Esp = stack->esp;
124 proc( context, *(LPVOID *)args );
126 stack->seg_cs = context->SegCs;
127 stack->eip = context->Eip;
128 stack->seg_ss = context->SegSs;
129 stack->esp = context->Esp;
131 context->SegCs = old_seg_cs;
132 context->Eip = old_eip;
133 context->SegSs = old_seg_ss;
134 context->Esp = old_esp;
139 /**********************************************************************
140 * DOSVM_RelayHandler
142 * Restore saved code and stack pointers and release stack block.
144 void DOSVM_RelayHandler( CONTEXT86 *context )
146 RELAY_Stack16 *stack = RELAY_GetPointer( context->Esp );
148 context->SegSs = stack->seg_ss;
149 context->Esp = stack->esp;
150 context->SegCs = stack->seg_cs;
151 context->Eip = stack->eip;
153 if (!stack->inuse ||
154 stack->stack_bottom != RELAY_MAGIC ||
155 stack->stack_top != RELAY_MAGIC)
156 ERR( "Stack corrupted!\n" );
158 stack->inuse = 0;
162 /**********************************************************************
163 * DOSVM_BuildCallFrame
165 * Modifies the context so that return to context calls DOSRELAY and
166 * only after return from DOSRELAY the original context will be returned to.
168 void DOSVM_BuildCallFrame( CONTEXT86 *context, DOSRELAY relay, LPVOID data )
170 static void (*__wine_call_from_16_regs_ptr)();
171 WORD code_sel = DOSVM_dpmi_segments->relay_code_sel;
174 * Allocate separate stack for relay call.
176 RELAY_MakeShortContext( context );
179 * Build call frame.
181 PUSH_WORD16( context, HIWORD(data) ); /* argument.hiword */
182 PUSH_WORD16( context, LOWORD(data) ); /* argument.loword */
183 PUSH_WORD16( context, context->SegCs ); /* STACK16FRAME.cs */
184 PUSH_WORD16( context, LOWORD(context->Eip) ); /* STACK16FRAME.ip */
185 PUSH_WORD16( context, LOWORD(context->Ebp) ); /* STACK16FRAME.bp */
186 PUSH_WORD16( context, HIWORD(relay) ); /* STACK16FRAME.entry_point.hiword */
187 PUSH_WORD16( context, LOWORD(relay) ); /* STACK16FRAME.entry_point.loword */
188 PUSH_WORD16( context, 0 ); /* STACK16FRAME.entry_ip */
189 PUSH_WORD16( context, HIWORD(RELAY_RelayStub) ); /* STACK16FRAME.relay.hiword */
190 PUSH_WORD16( context, LOWORD(RELAY_RelayStub) ); /* STACK16FRAME.relay.loword */
191 PUSH_WORD16( context, 0 ); /* STACK16FRAME.module_cs.hiword */
192 PUSH_WORD16( context, code_sel ); /* STACK16FRAME.module_cs.loword */
193 PUSH_WORD16( context, 0 ); /* STACK16FRAME.callfrom_ip.hiword */
194 PUSH_WORD16( context, 0 ); /* STACK16FRAME.callfrom_ip.loword */
197 * Adjust code pointer.
199 if (!__wine_call_from_16_regs_ptr)
200 __wine_call_from_16_regs_ptr = (void *)GetProcAddress(GetModuleHandleA("kernel32.dll"),
201 "__wine_call_from_16_regs" );
202 context->SegCs = wine_get_cs();
203 context->Eip = (DWORD)__wine_call_from_16_regs_ptr;