blob: 9d31b2b8786fb61f8de58c8ee6bbfe77f41f9d36 [file] [log] [blame]
Alexandre Julliarde2abbb11995-03-19 17:39:39 +00001/*
2 * Selector manipulation functions
3 *
4 * Copyright 1995 Alexandre Julliard
5 */
6
Alexandre Julliardfa68b751995-04-03 16:55:37 +00007#include <string.h>
Dimitrie O. Paun0b7a7bb2000-11-25 01:31:17 +00008
9#include "config.h"
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +000010#include "winerror.h"
Marcus Meissner04c3e1d1999-02-19 10:37:02 +000011#include "wine/winbase16.h"
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000012#include "ldt.h"
Alexandre Julliard21979011997-03-05 08:22:35 +000013#include "miscemu.h"
Alexandre Julliardfa68b751995-04-03 16:55:37 +000014#include "selectors.h"
Alexandre Julliarde2991ea1995-07-29 13:09:43 +000015#include "stackframe.h"
Andreas Mohra00b49f1998-12-07 10:48:09 +000016#include "process.h"
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +000017#include "server.h"
Alexandre Julliard15657091999-05-23 10:25:25 +000018#include "debugtools.h"
Patrik Stridvall1ed4ecf1999-06-26 14:58:24 +000019#include "toolhelp.h"
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000020
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +000021DEFAULT_DEBUG_CHANNEL(selector);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000022
Alexandre Julliard914406f2000-11-14 01:54:49 +000023#define LDT_SIZE 8192
24
25/* get the number of selectors needed to cover up to the selector limit */
26inline static WORD get_sel_count( WORD sel )
27{
28 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
29}
30
31/***********************************************************************
32 * SELECTOR_AllocArray
33 *
34 * Allocate a selector array without setting the LDT entries
35 */
36static WORD SELECTOR_AllocArray( WORD count )
37{
38 WORD i, sel, size = 0;
39
40 if (!count) return 0;
41 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
42 {
43 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
44 else if (++size >= count) break;
45 }
46 if (i == LDT_SIZE) return 0;
47 sel = i - size + 1;
48
49 /* mark selectors as allocated */
50 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
51
52 return (sel << __AHSHIFT) | 7;
53}
54
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000055
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000056/***********************************************************************
57 * AllocSelectorArray (KERNEL.206)
58 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000059WORD WINAPI AllocSelectorArray16( WORD count )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000060{
Alexandre Julliard914406f2000-11-14 01:54:49 +000061 WORD i, sel = SELECTOR_AllocArray( count );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000062
Alexandre Julliard914406f2000-11-14 01:54:49 +000063 if (sel)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000064 {
Alexandre Julliard914406f2000-11-14 01:54:49 +000065 LDT_ENTRY entry;
66 wine_ldt_set_base( &entry, 0 );
67 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
68 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
69 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000070 }
Alexandre Julliard914406f2000-11-14 01:54:49 +000071 return sel;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000072}
73
74
75/***********************************************************************
76 * AllocSelector (KERNEL.175)
77 */
Alexandre Julliarda3960291999-02-26 11:11:13 +000078WORD WINAPI AllocSelector16( WORD sel )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000079{
80 WORD newsel, count, i;
81
Alexandre Julliard914406f2000-11-14 01:54:49 +000082 count = sel ? get_sel_count(sel) : 1;
83 newsel = SELECTOR_AllocArray( count );
84 TRACE("(%04x): returning %04x\n", sel, newsel );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000085 if (!newsel) return 0;
86 if (!sel) return newsel; /* nothing to copy */
87 for (i = 0; i < count; i++)
88 {
Alexandre Julliard914406f2000-11-14 01:54:49 +000089 LDT_ENTRY entry;
90 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
91 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +000092 }
93 return newsel;
94}
95
96
97/***********************************************************************
98 * FreeSelector (KERNEL.176)
99 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000100WORD WINAPI FreeSelector16( WORD sel )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000101{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000102 LDT_ENTRY entry;
103
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000104 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
Alexandre Julliard914406f2000-11-14 01:54:49 +0000105
106#ifdef __i386__
107 /* Check if we are freeing current %fs or %gs selector */
108 if (!((__get_fs() ^ sel) & ~7))
109 {
110 WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
111 __set_fs( 0 );
112 }
113 if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
114#endif /* __i386__ */
115
116 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
117 wine_ldt_set_entry( sel, &entry );
118 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000119 return 0;
120}
121
122
123/***********************************************************************
124 * SELECTOR_SetEntries
125 *
126 * Set the LDT entries for an array of selectors.
127 */
Alexandre Julliard914406f2000-11-14 01:54:49 +0000128static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000129{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000130 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000131 WORD i, count;
132
Alexandre Julliard914406f2000-11-14 01:54:49 +0000133 wine_ldt_set_base( &entry, base );
134 wine_ldt_set_limit( &entry, size - 1 );
135 wine_ldt_set_flags( &entry, flags );
Alexandre Julliard7d654eb1996-02-25 11:36:22 +0000136 /* Make sure base and limit are not 0 together if the size is not 0 */
Alexandre Julliard914406f2000-11-14 01:54:49 +0000137 if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000138 count = (size + 0xffff) / 0x10000;
139 for (i = 0; i < count; i++)
140 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000141 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
142 wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
143 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000144 }
145}
146
147
148/***********************************************************************
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000149 * SELECTOR_AllocBlock
150 *
151 * Allocate selectors for a block of linear memory.
152 */
Alexandre Julliard914406f2000-11-14 01:54:49 +0000153WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000154{
155 WORD sel, count;
156
157 if (!size) return 0;
158 count = (size + 0xffff) / 0x10000;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000159 sel = SELECTOR_AllocArray( count );
160 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000161 return sel;
162}
163
164
165/***********************************************************************
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000166 * SELECTOR_FreeBlock
167 *
168 * Free a block of selectors.
169 */
Alexandre Julliard914406f2000-11-14 01:54:49 +0000170void SELECTOR_FreeBlock( WORD sel )
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000171{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000172 WORD i, count = get_sel_count( sel );
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000173
Alexandre Julliard15657091999-05-23 10:25:25 +0000174 TRACE("(%04x,%d)\n", sel, count );
Alexandre Julliard914406f2000-11-14 01:54:49 +0000175 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
Alexandre Julliard8cc3a5e1996-08-11 15:49:51 +0000176}
177
178
179/***********************************************************************
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000180 * SELECTOR_ReallocBlock
181 *
182 * Change the size of a block of selectors.
183 */
Alexandre Julliard284c9b91999-04-11 15:07:13 +0000184WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000185{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000186 LDT_ENTRY entry;
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000187 WORD i, oldcount, newcount;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000188
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000189 if (!size) size = 1;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000190 oldcount = get_sel_count( sel );
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000191 newcount = (size + 0xffff) >> 16;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000192 wine_ldt_get_entry( sel, &entry );
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000193
194 if (oldcount < newcount) /* We need to add selectors */
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000195 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000196 WORD index = sel >> __AHSHIFT;
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000197 /* Check if the next selectors are free */
Alexandre Julliard914406f2000-11-14 01:54:49 +0000198 if (index + newcount > LDT_SIZE) i = oldcount;
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000199 else
200 for (i = oldcount; i < newcount; i++)
Alexandre Julliard914406f2000-11-14 01:54:49 +0000201 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000202
203 if (i < newcount) /* they are not free */
204 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000205 SELECTOR_FreeBlock( sel );
206 sel = SELECTOR_AllocArray( newcount );
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000207 }
208 else /* mark the selectors as allocated */
209 {
210 for (i = oldcount; i < newcount; i++)
Alexandre Julliard914406f2000-11-14 01:54:49 +0000211 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000212 }
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000213 }
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000214 else if (oldcount > newcount) /* We need to remove selectors */
215 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000216 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
Alexandre Julliarde2991ea1995-07-29 13:09:43 +0000217 }
Alexandre Julliard914406f2000-11-14 01:54:49 +0000218 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000219 return sel;
220}
221
222
223/***********************************************************************
224 * PrestoChangoSelector (KERNEL.177)
225 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000226WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000227{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000228 LDT_ENTRY entry;
229 wine_ldt_get_entry( selSrc, &entry );
230 /* toggle the executable bit */
231 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
232 wine_ldt_set_entry( selDst, &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000233 return selDst;
234}
235
236
237/***********************************************************************
238 * AllocCStoDSAlias (KERNEL.170)
239 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000240WORD WINAPI AllocCStoDSAlias16( WORD sel )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000241{
242 WORD newsel;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000243 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000244
Alexandre Julliard914406f2000-11-14 01:54:49 +0000245 newsel = SELECTOR_AllocArray( 1 );
Alexandre Julliard15657091999-05-23 10:25:25 +0000246 TRACE("(%04x): returning %04x\n",
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000247 sel, newsel );
248 if (!newsel) return 0;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000249 wine_ldt_get_entry( sel, &entry );
250 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
251 wine_ldt_set_entry( newsel, &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000252 return newsel;
253}
254
255
256/***********************************************************************
257 * AllocDStoCSAlias (KERNEL.171)
258 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000259WORD WINAPI AllocDStoCSAlias16( WORD sel )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000260{
261 WORD newsel;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000262 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000263
Alexandre Julliard914406f2000-11-14 01:54:49 +0000264 newsel = SELECTOR_AllocArray( 1 );
Alexandre Julliard15657091999-05-23 10:25:25 +0000265 TRACE("(%04x): returning %04x\n",
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000266 sel, newsel );
267 if (!newsel) return 0;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000268 wine_ldt_get_entry( sel, &entry );
269 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
270 wine_ldt_set_entry( newsel, &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000271 return newsel;
272}
273
274
275/***********************************************************************
276 * LongPtrAdd (KERNEL.180)
277 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000278void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000279{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000280 LDT_ENTRY entry;
281 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
282 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
283 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000284}
285
286
287/***********************************************************************
288 * GetSelectorBase (KERNEL.186)
289 */
Andreas Mohra00b49f1998-12-07 10:48:09 +0000290DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
291{
292 /*
293 * Note: For Win32s processes, the whole linear address space is
294 * shifted by 0x10000 relative to the OS linear address space.
295 * See the comment in msdos/vxd.c.
296 */
297
298 DWORD base = GetSelectorBase( sel );
299 return W32S_WINE2APP( base, W32S_APPLICATION() ? W32S_OFFSET : 0 );
300}
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000301DWORD WINAPI GetSelectorBase( WORD sel )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000302{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000303 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000304
Alexandre Julliardac9c9b01996-07-28 18:50:11 +0000305 /* if base points into DOSMEM, assume we have to
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +0000306 * return pointer into physical lower 1MB */
307
Alexandre Julliard914406f2000-11-14 01:54:49 +0000308 return DOSMEM_MapLinearToDos( base );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000309}
310
311
312/***********************************************************************
313 * SetSelectorBase (KERNEL.187)
314 */
Andreas Mohra00b49f1998-12-07 10:48:09 +0000315DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
316{
317 /*
318 * Note: For Win32s processes, the whole linear address space is
319 * shifted by 0x10000 relative to the OS linear address space.
320 * See the comment in msdos/vxd.c.
321 */
322
323 SetSelectorBase( sel,
324 W32S_APP2WINE( base, W32S_APPLICATION() ? W32S_OFFSET : 0 ) );
325 return sel;
326}
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000327WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000328{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000329 LDT_ENTRY entry;
330 wine_ldt_get_entry( sel, &entry );
331 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
332 wine_ldt_set_entry( sel, &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000333 return sel;
334}
335
336
337/***********************************************************************
338 * GetSelectorLimit (KERNEL.188)
339 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000340DWORD WINAPI GetSelectorLimit16( WORD sel )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000341{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000342 return wine_ldt_copy.limit[sel >> __AHSHIFT];
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000343}
344
345
346/***********************************************************************
347 * SetSelectorLimit (KERNEL.189)
348 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000349WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000350{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000351 LDT_ENTRY entry;
352 wine_ldt_get_entry( sel, &entry );
353 wine_ldt_set_limit( &entry, limit );
354 wine_ldt_set_entry( sel, &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000355 return sel;
356}
357
358
359/***********************************************************************
360 * SelectorAccessRights (KERNEL.196)
361 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000362WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000363{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000364 LDT_ENTRY entry;
365 wine_ldt_get_entry( sel, &entry );
366
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000367 if (op == 0) /* get */
368 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000369 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000370 }
371 else /* set */
372 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000373 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
374 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
375 wine_ldt_set_entry( sel, &entry );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000376 return 0;
377 }
378}
379
380
381/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000382 * IsBadCodePtr16 (KERNEL.336)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000383 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000384BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000385{
386 WORD sel;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000387 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000388
389 sel = SELECTOROF(lpfn);
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000390 if (!sel) return TRUE;
391 if (IS_SELECTOR_FREE(sel)) return TRUE;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000392 wine_ldt_get_entry( sel, &entry );
393 /* check for code segment, ignoring conforming, read-only and accessed bits */
394 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
395 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000396 return FALSE;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000397}
398
399
400/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000401 * IsBadStringPtr16 (KERNEL.337)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000402 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000403BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000404{
405 WORD sel;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000406 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000407
408 sel = SELECTOROF(ptr);
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000409 if (!sel) return TRUE;
410 if (IS_SELECTOR_FREE(sel)) return TRUE;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000411 wine_ldt_get_entry( sel, &entry );
412 /* check for data or readable code segment */
413 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
414 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
Uwe Bonnes0a1645d1999-08-15 18:44:06 +0000415 if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr)) + 1;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000416 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000417 return FALSE;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000418}
419
420
421/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000422 * IsBadHugeReadPtr16 (KERNEL.346)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000423 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000424BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000425{
426 WORD sel;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000427 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000428
429 sel = SELECTOROF(ptr);
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000430 if (!sel) return TRUE;
431 if (IS_SELECTOR_FREE(sel)) return TRUE;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000432 wine_ldt_get_entry( sel, &entry );
433 /* check for data or readable code segment */
434 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
435 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
436 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000437 return FALSE;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000438}
439
440
441/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000442 * IsBadHugeWritePtr16 (KERNEL.347)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000443 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000444BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000445{
446 WORD sel;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000447 LDT_ENTRY entry;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000448
449 sel = SELECTOROF(ptr);
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000450 if (!sel) return TRUE;
451 if (IS_SELECTOR_FREE(sel)) return TRUE;
Alexandre Julliard914406f2000-11-14 01:54:49 +0000452 wine_ldt_get_entry( sel, &entry );
453 /* check for writeable data segment, ignoring expand-down and accessed flags */
454 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
455 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
Alexandre Julliardbd34d4f1995-06-20 19:08:12 +0000456 return FALSE;
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000457}
458
459/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000460 * IsBadReadPtr16 (KERNEL.334)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000461 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000462BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000463{
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000464 return IsBadHugeReadPtr16( ptr, size );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000465}
466
467
468/***********************************************************************
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000469 * IsBadWritePtr16 (KERNEL.335)
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000470 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000471BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000472{
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000473 return IsBadHugeWritePtr16( ptr, size );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000474}
475
476
477/***********************************************************************
Ulrich Weigand98b5f771999-07-27 17:09:14 +0000478 * IsBadFlatReadWritePtr16 (KERNEL.627)
479 */
480BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
481{
482 return bWrite? IsBadHugeWritePtr16( ptr, size )
483 : IsBadHugeReadPtr16( ptr, size );
484}
485
486
487/***********************************************************************
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000488 * MemoryRead (TOOLHELP.78)
489 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000490DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000491{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000492 WORD index = sel >> __AHSHIFT;
493
494 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
495 if (offset > wine_ldt_copy.limit[index]) return 0;
496 if (offset + count > wine_ldt_copy.limit[index] + 1)
497 count = wine_ldt_copy.limit[index] + 1 - offset;
498 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000499 return count;
500}
501
502
503/***********************************************************************
504 * MemoryWrite (TOOLHELP.79)
505 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000506DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000507{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000508 WORD index = sel >> __AHSHIFT;
509
510 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
511 if (offset > wine_ldt_copy.limit[index]) return 0;
512 if (offset + count > wine_ldt_copy.limit[index] + 1)
513 count = wine_ldt_copy.limit[index] + 1 - offset;
514 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
Alexandre Julliarde2abbb11995-03-19 17:39:39 +0000515 return count;
516}
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000517
518/************************************* Win95 pointer mapping functions *
519 *
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000520 */
521
522/***********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000523 * MapSL (KERNEL32.523)
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000524 *
525 * Maps fixed segmented pointer to linear.
526 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000527LPVOID WINAPI MapSL( SEGPTR sptr )
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000528{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000529 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000530}
531
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000532/***********************************************************************
533 * MapSLFix (KERNEL32.524)
534 *
535 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
536 * unexpected linear address change when GlobalCompact() shuffles
537 * moveable blocks.
538 */
539
540LPVOID WINAPI MapSLFix( SEGPTR sptr )
541{
542 return (LPVOID)PTR_SEG_TO_LIN(sptr);
543}
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000544
545/***********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000546 * UnMapSLFixArray (KERNEL32.701)
547 */
548
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000549void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000550{
Ulrich Weigand90c245c1999-02-02 10:34:46 +0000551 /* Must not change EAX, hence defined as 'register' function */
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000552}
553
554/***********************************************************************
555 * MapLS (KERNEL32.522)
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000556 *
557 * Maps linear pointer to segmented.
558 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000559SEGPTR WINAPI MapLS( LPVOID ptr )
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000560{
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000561 if (!HIWORD(ptr))
562 return (SEGPTR)ptr;
563 else
564 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000565 WORD sel = SELECTOR_AllocBlock( ptr, 0x10000, WINE_LDT_FLAGS_DATA );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000566 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
567 }
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000568}
569
570
571/***********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000572 * UnMapLS (KERNEL32.700)
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000573 *
574 * Free mapped selector.
575 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000576void WINAPI UnMapLS( SEGPTR sptr )
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000577{
Alexandre Julliard914406f2000-11-14 01:54:49 +0000578 if (SELECTOROF(sptr)) FreeSelector16( SELECTOROF(sptr) );
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000579}
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000580
581/***********************************************************************
582 * GetThreadSelectorEntry (KERNEL32)
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000583 */
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000584BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000585{
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000586#ifdef __i386__
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000587 BOOL ret;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000588
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000589 if (!(sel & 4)) /* GDT selector */
590 {
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000591 sel &= ~3; /* ignore RPL */
592 if (!sel) /* null selector */
593 {
594 memset( ldtent, 0, sizeof(*ldtent) );
595 return TRUE;
596 }
597 ldtent->BaseLow = 0;
598 ldtent->HighWord.Bits.BaseMid = 0;
599 ldtent->HighWord.Bits.BaseHi = 0;
600 ldtent->LimitLow = 0xffff;
601 ldtent->HighWord.Bits.LimitHi = 0xf;
602 ldtent->HighWord.Bits.Dpl = 3;
603 ldtent->HighWord.Bits.Sys = 0;
604 ldtent->HighWord.Bits.Pres = 1;
605 ldtent->HighWord.Bits.Granularity = 1;
606 ldtent->HighWord.Bits.Default_Big = 1;
607 ldtent->HighWord.Bits.Type = 0x12;
608 /* it has to be one of the system GDT selectors */
Alexandre Julliard916f9752000-02-26 16:51:13 +0000609 if (sel == (__get_ds() & ~3)) return TRUE;
610 if (sel == (__get_ss() & ~3)) return TRUE;
611 if (sel == (__get_cs() & ~3))
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000612 {
613 ldtent->HighWord.Bits.Type |= 8; /* code segment */
614 return TRUE;
615 }
616 SetLastError( ERROR_NOACCESS );
617 return FALSE;
618 }
619
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000620 SERVER_START_REQ
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000621 {
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000622 struct get_selector_entry_request *req = server_alloc_req( sizeof(*req), 0 );
623
624 req->handle = hthread;
625 req->entry = sel >> __AHSHIFT;
626 if ((ret = !server_call( REQ_GET_SELECTOR_ENTRY )))
627 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000628 if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000629 {
630 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
631 ret = FALSE;
632 }
633 else
634 {
Alexandre Julliard914406f2000-11-14 01:54:49 +0000635 wine_ldt_set_base( ldtent, (void *)req->base );
636 wine_ldt_set_limit( ldtent, req->limit );
637 wine_ldt_set_flags( ldtent, req->flags );
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000638 }
639 }
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000640 }
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000641 SERVER_END_REQ;
642 return ret;
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000643#else
Ulrich Weigandafd6a4b2000-06-04 01:48:05 +0000644 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
Alexandre Julliard0a7c1f62000-01-27 02:54:17 +0000645 return FALSE;
646#endif
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000647}
648
649
650/**********************************************************************
651 * SMapLS* (KERNEL32)
652 * These functions map linear pointers at [EBP+xxx] to segmented pointers
653 * and return them.
654 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
655 * unravel them at SUnMapLS. We just store the segmented pointer there.
656 */
657static void
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000658x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000659 DWORD val,ptr;
660
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000661 val =*(DWORD*)(context->Ebp + argoff);
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000662 if (val<0x10000) {
663 ptr=val;
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000664 *(DWORD*)(context->Ebp + argoff) = 0;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000665 } else {
666 ptr = MapLS((LPVOID)val);
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000667 *(DWORD*)(context->Ebp + argoff) = ptr;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000668 }
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000669 context->Eax = ptr;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000670}
671
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000672/***********************************************************************
673 * SMapLS_IP_EBP_8 (KERNEL32.601)
674 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000675void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000676
677/***********************************************************************
678 * SMapLS_IP_EBP_12 (KERNEL32.593)
679 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000680void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000681
682/***********************************************************************
683 * SMapLS_IP_EBP_16 (KERNEL32.594)
684 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000685void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000686
687/***********************************************************************
688 * SMapLS_IP_EBP_20 (KERNEL32.595)
689 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000690void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000691
692/***********************************************************************
693 * SMapLS_IP_EBP_24 (KERNEL32.596)
694 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000695void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000696
697/***********************************************************************
698 * SMapLS_IP_EBP_28 (KERNEL32.597)
699 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000700void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000701
702/***********************************************************************
703 * SMapLS_IP_EBP_32 (KERNEL32.598)
704 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000705void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000706
707/***********************************************************************
708 * SMapLS_IP_EBP_36 (KERNEL32.599)
709 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000710void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000711
712/***********************************************************************
713 * SMapLS_IP_EBP_40 (KERNEL32.600)
714 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000715void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000716
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000717/***********************************************************************
718 * SMapLS (KERNEL32.592)
719 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000720void WINAPI SMapLS( CONTEXT86 *context )
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000721{
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000722 if (HIWORD(context->Eax))
723 {
724 context->Eax = MapLS( (LPVOID)context->Eax );
725 context->Edx = context->Eax;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000726 } else {
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000727 context->Edx = 0;
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000728 }
729}
730
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000731/***********************************************************************
732 * SUnMapLS (KERNEL32.602)
733 */
734
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000735void WINAPI SUnMapLS( CONTEXT86 *context )
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000736{
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000737 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000738}
739
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000740inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
741{
742 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
743 if (*ptr)
744 {
745 UnMapLS( *ptr );
746 *ptr = 0;
747 }
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000748}
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000749
750/***********************************************************************
751 * SUnMapLS_IP_EBP_8 (KERNEL32.611)
752 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000753void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000754
755/***********************************************************************
756 * SUnMapLS_IP_EBP_12 (KERNEL32.603)
757 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000758void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000759
760/***********************************************************************
761 * SUnMapLS_IP_EBP_16 (KERNEL32.604)
762 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000763void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000764
765/***********************************************************************
766 * SUnMapLS_IP_EBP_20 (KERNEL32.605)
767 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000768void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000769
770/***********************************************************************
771 * SUnMapLS_IP_EBP_24 (KERNEL32.606)
772 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000773void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000774
775/***********************************************************************
776 * SUnMapLS_IP_EBP_28 (KERNEL32.607)
777 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000778void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000779
780/***********************************************************************
781 * SUnMapLS_IP_EBP_32 (KERNEL32.608)
782 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000783void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000784
785/***********************************************************************
786 * SUnMapLS_IP_EBP_36 (KERNEL32.609)
787 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000788void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
Patrik Stridvall54fe8382000-04-06 20:21:16 +0000789
790/***********************************************************************
791 * SUnMapLS_IP_EBP_40 (KERNEL32.610)
792 */
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000793void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000794
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000795/**********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000796 * AllocMappedBuffer (KERNEL32.38)
797 *
798 * This is a undocumented KERNEL32 function that
799 * SMapLS's a GlobalAlloc'ed buffer.
800 *
801 * Input: EDI register: size of buffer to allocate
802 * Output: EDI register: pointer to buffer
803 *
804 * Note: The buffer is preceeded by 8 bytes:
805 * ...
806 * edi+0 buffer
807 * edi-4 SEGPTR to buffer
808 * edi-8 some magic Win95 needs for SUnMapLS
809 * (we use it for the memory handle)
810 *
811 * The SEGPTR is used by the caller!
812 */
813
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000814void WINAPI AllocMappedBuffer( CONTEXT86 *context )
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000815{
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000816 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
Alexandre Julliarda3960291999-02-26 11:11:13 +0000817 DWORD *buffer = (DWORD *)GlobalLock(handle);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000818 SEGPTR ptr = 0;
819
820 if (buffer)
821 if (!(ptr = MapLS(buffer + 2)))
822 {
Alexandre Julliarda3960291999-02-26 11:11:13 +0000823 GlobalUnlock(handle);
824 GlobalFree(handle);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000825 }
826
827 if (!ptr)
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000828 context->Eax = context->Edi = 0;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000829 else
830 {
831 buffer[0] = handle;
832 buffer[1] = ptr;
833
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000834 context->Eax = (DWORD) ptr;
835 context->Edi = (DWORD)(buffer + 2);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000836 }
837}
838
839/**********************************************************************
840 * FreeMappedBuffer (KERNEL32.39)
841 *
842 * Free a buffer allocated by AllocMappedBuffer
843 *
844 * Input: EDI register: pointer to buffer
845 */
846
Ulrich Weigandeb94c7d1999-11-13 23:54:04 +0000847void WINAPI FreeMappedBuffer( CONTEXT86 *context )
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000848{
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000849 if (context->Edi)
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000850 {
Alexandre Julliardd8fab2e2000-09-25 23:53:07 +0000851 DWORD *buffer = (DWORD *)context->Edi - 2;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000852
853 UnMapLS(buffer[1]);
854
Alexandre Julliarda3960291999-02-26 11:11:13 +0000855 GlobalUnlock(buffer[0]);
856 GlobalFree(buffer[0]);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000857 }
858}
859
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000860
861/***********************************************************************
862 * UTSelectorOffsetToLinear (WIN32S16.48)
863 *
864 * rough guesswork, but seems to work (I had no "reasonable" docu)
865 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000866LPVOID WINAPI UTSelectorOffsetToLinear16(SEGPTR sptr)
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000867{
868 return PTR_SEG_TO_LIN(sptr);
869}
870
871/***********************************************************************
872 * UTLinearToSelectorOffset (WIN32S16.49)
873 *
874 * FIXME: I don't know if that's the right way to do linear -> segmented
875 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000876SEGPTR WINAPI UTLinearToSelectorOffset16(LPVOID lptr)
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000877{
Alexandre Julliard44ed71f1997-12-21 19:17:50 +0000878 return (SEGPTR)lptr;
879}
Alexandre Julliard916f9752000-02-26 16:51:13 +0000880
881#ifdef __i386__
Patrik Stridvallea4f8312000-02-27 16:39:48 +0000882__ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
883__ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
884__ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
885__ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
886__ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
887__ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
888__ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
889__ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
Alexandre Julliard916f9752000-02-26 16:51:13 +0000890#endif