Release 940405
[wine/gsoc-2012-control.git] / if1632 / relay.c
blob21898550b24457dd5b31540eefd9dbe9f03f7f6e
1 static char RCSId[] = "$Id: relay.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #ifdef linux
12 #include <linux/unistd.h>
13 #include <linux/head.h>
14 #include <linux/ldt.h>
15 #include <linux/segment.h>
16 #endif
17 #include <errno.h>
19 #include "neexe.h"
20 #include "segmem.h"
21 #include "prototypes.h"
22 #include "dlls.h"
23 #include "options.h"
25 #define DEBUG_RELAY /* */
27 #define N_BUILTINS 10
29 struct dll_name_table_entry_s dll_builtin_table[N_BUILTINS] =
31 { "KERNEL", KERNEL_table, 410, 1 },
32 { "USER", USER_table, 540, 2 },
33 { "GDI", GDI_table, 490, 3 },
34 { "UNIXLIB", UNIXLIB_table, 10, 4 },
35 { "WIN87EM", WIN87EM_table, 10, 5 },
36 { "SHELL", SHELL_table, 256, 6 },
37 { "SOUND", SOUND_table, 20, 7 },
38 { "KEYBOARD",KEYBOARD_table,137, 8 },
39 { "WINSOCK", WINSOCK_table, 155, 9 },
40 { "STRESS", STRESS_table, 15,10 },
43 unsigned short *Stack16Frame;
45 extern unsigned long IF1632_Saved16_esp;
46 extern unsigned long IF1632_Saved16_ebp;
47 extern unsigned short IF1632_Saved16_ss;
49 /**********************************************************************
50 * DLLRelay
52 * We get a stack frame pointer to data that looks like this:
54 * Hex Offset Contents
55 * ---------- -------
56 * +00 previous saved_16ss
57 * +02 previous saved_16ebp
58 * +06 previous saved_16esp
59 * +0A 16-bit es
60 * +0C 16-bit ds
61 * +0E 16-bit ebp
62 * +12 length of 16-bit arguments
63 * +14 16-bit ip
64 * +16 16-bit cs
65 * +18 arguments
67 int
68 DLLRelay(unsigned int func_num, unsigned int seg_off)
70 struct dll_table_entry_s *dll_p;
71 unsigned short *saved_Stack16Frame;
72 unsigned int segment;
73 unsigned int offset;
74 unsigned int dll_id;
75 unsigned int ordinal;
76 int arg_table[DLL_MAX_ARGS];
77 void *arg_ptr;
78 int (*func_ptr)();
79 int i;
80 int ret_val;
83 * Determine address of arguments.
85 saved_Stack16Frame = Stack16Frame;
86 Stack16Frame = (unsigned short *) seg_off;
87 arg_ptr = (void *) (seg_off + 0x18);
90 * Extract the DLL number and ordinal number.
92 dll_id = ((func_num >> 16) & 0xffff) - 1;
93 ordinal = func_num & 0xffff;
94 dll_p = &dll_builtin_table[dll_id].dll_table[ordinal];
96 #ifdef DEBUG_RELAY
97 if (Options.relay_debug)
99 unsigned int *ret_addr;
100 unsigned short *stack_p;
102 ret_addr = (unsigned int *) ((char *) seg_off + 0x14);
103 printf("Calling %s (%s.%d), 16-bit stack at %04x:%04x, ",
104 dll_p->export_name,
105 dll_builtin_table[dll_id].dll_name, ordinal,
106 seg_off >> 16, seg_off & 0xffff);
107 printf("return to %08x\n", *ret_addr);
108 printf(" ESP %08x, EBP %08x, SS %04x\n",
109 IF1632_Saved16_esp, IF1632_Saved16_ebp,
110 IF1632_Saved16_ss);
112 #ifdef DEBUG_STACK
113 stack_p = (unsigned short *) seg_off;
114 for (i = 0; i < 24; i++, stack_p++)
116 printf("%04x ", *stack_p);
117 if ((i & 7) == 7)
118 printf("\n");
120 printf("\n");
121 #endif /* DEBUG_STACK */
123 #endif /* DEBUG_RELAY */
126 * Make sure we have a handler defined for this call.
128 if (dll_p->handler == NULL)
130 char buffer[100];
132 sprintf(buffer, "No handler for routine %s.%d",
133 dll_builtin_table[dll_id].dll_name, ordinal);
134 myerror(buffer);
136 func_ptr = dll_p->handler;
139 * OK, special case. If the handler is define as taking no arguments
140 * then pass the address of the arguments on the 16-bit stack to the
141 * handler. It will just ignore the pointer if it really takes no
142 * arguments. This allows us to write slightly faster library routines
143 * if we choose.
145 if (dll_p->n_args == 0)
147 ret_val = (*func_ptr)(arg_ptr);
148 Stack16Frame = saved_Stack16Frame;
149 return ret_val;
153 * Getting this far means we need to convert the 16-bit argument stack.
155 for (i = 0; i < dll_p->n_args; i++)
157 short *sp;
158 int *ip;
160 offset = dll_p->args[i].dst_arg;
162 switch (dll_p->args[i].src_type)
164 case DLL_ARGTYPE_SIGNEDWORD:
165 sp = (short *) ((char *) arg_ptr + offset);
166 arg_table[i] = *sp;
167 break;
169 case DLL_ARGTYPE_WORD:
170 sp = (short *) ((char *) arg_ptr + offset);
171 arg_table[i] = (int) *sp & 0xffff;
172 break;
174 case DLL_ARGTYPE_LONG:
175 ip = (int *) ((char *) arg_ptr + offset);
176 arg_table[i] = *ip;
177 break;
179 case DLL_ARGTYPE_FARPTR:
180 ip = (int *) ((char *) arg_ptr + offset);
181 if (*ip & 0xffff0000)
182 arg_table[i] = FIXPTR(*ip);
183 else
184 arg_table[i] = *ip;
185 break;
190 * Call the handler
192 ret_val = (*func_ptr)(arg_table[0], arg_table[1], arg_table[2],
193 arg_table[3], arg_table[4], arg_table[5],
194 arg_table[6], arg_table[7], arg_table[8],
195 arg_table[9], arg_table[10], arg_table[11],
196 arg_table[12], arg_table[13], arg_table[14],
197 arg_table[15]);
199 #ifdef DEBUG_RELAY
200 if (Options.relay_debug)
202 printf("Returning %08.8x from %s (%s.%d)\n",
203 ret_val,
204 dll_p->export_name,
205 dll_builtin_table[dll_id].dll_name, ordinal);
207 #endif
209 Stack16Frame = saved_Stack16Frame;
210 return ret_val;
213 /**********************************************************************
214 * FindDLLTable
216 struct dll_table_entry_s *
217 FindDLLTable(char *dll_name)
219 int i;
221 for (i = 0; i < N_BUILTINS; i++)
222 if (strcmp(dll_builtin_table[i].dll_name, dll_name) == 0)
223 return dll_builtin_table[i].dll_table;
225 return NULL;
228 /**********************************************************************
229 * FindOrdinalFromName
232 FindOrdinalFromName(struct dll_table_entry_s *dll_table, char *func_name)
234 int i, limit;
236 for (i = 0; i < N_BUILTINS; i++)
237 if (dll_table == dll_builtin_table[i].dll_table)
238 break;
240 if (i == N_BUILTINS)
241 return 0;
243 limit = dll_builtin_table[i].dll_table_length;
244 for (i = 0; i < limit; i++)
245 if (strcasecmp(dll_table[i].export_name, func_name) == 0)
246 return i;
248 return 0;
250 /**********************************************************************
251 * ReturnArg
254 ReturnArg(int arg)
256 return arg;
259 #ifdef WINESTAT
260 void winestat(){
261 int i, j;
262 double perc;
263 int used, implemented;
264 int tused, timplemented;
265 struct dll_table_entry_s *table;
267 tused = 0;
268 timplemented = 0;
269 for (i = 0; i < N_BUILTINS; i++) {
270 table = dll_builtin_table[i].dll_table;
271 used = 0;
272 implemented = 0;
273 for(j=0; j < dll_builtin_table[i].dll_table_length; j++) {
274 if(table[j].used){
275 used++;
276 if (table[j].handler) implemented++;
277 else
278 printf("%s.%d\n",
279 dll_builtin_table[i].dll_name,
283 tused += used;
284 timplemented += implemented;
285 if(used)
286 perc = implemented * 100.00 / used;
287 else
288 perc = 0.0;
289 printf("%s: %d %d %3.1f\n", dll_builtin_table[i].dll_name, implemented, used, perc);
291 perc = timplemented * 100.00 / tused;
292 printf("TOTAL: %d %d %3.1f\n",timplemented, tused, perc);
294 #endif /* WINESTAT */