Work around problems on Solaris if config.h is not included.
[wine] / memory / selector.c
1 /*
2  * Selector manipulation functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  */
6
7 #include <string.h>
8
9 #include "config.h"
10 #include "winerror.h"
11 #include "wine/winbase16.h"
12 #include "miscemu.h"
13 #include "selectors.h"
14 #include "stackframe.h"
15 #include "server.h"
16 #include "debugtools.h"
17 #include "toolhelp.h"
18
19 DEFAULT_DEBUG_CHANNEL(selector);
20
21 #define LDT_SIZE 8192
22
23 /* get the number of selectors needed to cover up to the selector limit */
24 inline static WORD get_sel_count( WORD sel )
25 {
26     return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
27 }
28
29 /***********************************************************************
30  *           SELECTOR_AllocArray
31  *
32  * Allocate a selector array without setting the LDT entries
33  */
34 static WORD SELECTOR_AllocArray( WORD count )
35 {
36     WORD i, sel, size = 0;
37
38     if (!count) return 0;
39     for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
40     {
41         if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
42         else if (++size >= count) break;
43     }
44     if (i == LDT_SIZE) return 0;
45     sel = i - size + 1;
46
47     /* mark selectors as allocated */
48     for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
49
50     return (sel << __AHSHIFT) | 7;
51 }
52
53
54 /***********************************************************************
55  *           AllocSelectorArray   (KERNEL.206)
56  */
57 WORD WINAPI AllocSelectorArray16( WORD count )
58 {
59     WORD i, sel = SELECTOR_AllocArray( count );
60
61     if (sel)
62     {
63         LDT_ENTRY entry;
64         wine_ldt_set_base( &entry, 0 );
65         wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
66         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
67         for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
68     }
69     return sel;
70 }
71
72
73 /***********************************************************************
74  *           AllocSelector   (KERNEL.175)
75  */
76 WORD WINAPI AllocSelector16( WORD sel )
77 {
78     WORD newsel, count, i;
79
80     count = sel ? get_sel_count(sel) : 1;
81     newsel = SELECTOR_AllocArray( count );
82     TRACE("(%04x): returning %04x\n", sel, newsel );
83     if (!newsel) return 0;
84     if (!sel) return newsel;  /* nothing to copy */
85     for (i = 0; i < count; i++)
86     {
87         LDT_ENTRY entry;
88         wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
89         wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
90     }
91     return newsel;
92 }
93
94
95 /***********************************************************************
96  *           FreeSelector   (KERNEL.176)
97  */
98 WORD WINAPI FreeSelector16( WORD sel )
99 {
100     LDT_ENTRY entry;
101
102     if (IS_SELECTOR_FREE(sel)) return sel;  /* error */
103
104 #ifdef __i386__
105     /* Check if we are freeing current %fs or %gs selector */
106     if (!((__get_fs() ^ sel) & ~7))
107     {
108         WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
109         __set_fs( 0 );
110     }
111     if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
112 #endif  /* __i386__ */
113
114     memset( &entry, 0, sizeof(entry) );  /* clear the LDT entries */
115     wine_ldt_set_entry( sel, &entry );
116     wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
117     return 0;
118 }
119
120
121 /***********************************************************************
122  *           SELECTOR_SetEntries
123  *
124  * Set the LDT entries for an array of selectors.
125  */
126 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
127 {
128     LDT_ENTRY entry;
129     WORD i, count;
130
131     wine_ldt_set_base( &entry, base );
132     wine_ldt_set_limit( &entry, size - 1 );
133     wine_ldt_set_flags( &entry, flags );
134     /* Make sure base and limit are not 0 together if the size is not 0 */
135     if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
136     count = (size + 0xffff) / 0x10000;
137     for (i = 0; i < count; i++)
138     {
139         wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
140         wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
141         wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
142     }
143 }
144
145
146 /***********************************************************************
147  *           SELECTOR_AllocBlock
148  *
149  * Allocate selectors for a block of linear memory.
150  */
151 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
152 {
153     WORD sel, count;
154
155     if (!size) return 0;
156     count = (size + 0xffff) / 0x10000;
157     sel = SELECTOR_AllocArray( count );
158     if (sel) SELECTOR_SetEntries( sel, base, size, flags );
159     return sel;
160 }
161
162
163 /***********************************************************************
164  *           SELECTOR_FreeBlock
165  *
166  * Free a block of selectors.
167  */
168 void SELECTOR_FreeBlock( WORD sel )
169 {
170     WORD i, count = get_sel_count( sel );
171
172     TRACE("(%04x,%d)\n", sel, count );
173     for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
174 }
175
176
177 /***********************************************************************
178  *           SELECTOR_ReallocBlock
179  *
180  * Change the size of a block of selectors.
181  */
182 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
183 {
184     LDT_ENTRY entry;
185     WORD i, oldcount, newcount;
186
187     if (!size) size = 1;
188     oldcount = get_sel_count( sel );
189     newcount = (size + 0xffff) >> 16;
190     wine_ldt_get_entry( sel, &entry );
191
192     if (oldcount < newcount)  /* We need to add selectors */
193     {
194         WORD index = sel >> __AHSHIFT;
195           /* Check if the next selectors are free */
196         if (index + newcount > LDT_SIZE) i = oldcount;
197         else
198             for (i = oldcount; i < newcount; i++)
199                 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
200
201         if (i < newcount)  /* they are not free */
202         {
203             SELECTOR_FreeBlock( sel );
204             sel = SELECTOR_AllocArray( newcount );
205         }
206         else  /* mark the selectors as allocated */
207         {
208             for (i = oldcount; i < newcount; i++)
209                 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
210         }
211     }
212     else if (oldcount > newcount) /* We need to remove selectors */
213     {
214         SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
215     }
216     if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
217     return sel;
218 }
219
220
221 /***********************************************************************
222  *           PrestoChangoSelector   (KERNEL.177)
223  */
224 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
225 {
226     LDT_ENTRY entry;
227     wine_ldt_get_entry( selSrc, &entry );
228     /* toggle the executable bit */
229     entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
230     wine_ldt_set_entry( selDst, &entry );
231     return selDst;
232 }
233
234
235 /***********************************************************************
236  *           AllocCStoDSAlias   (KERNEL.170)
237  */
238 WORD WINAPI AllocCStoDSAlias16( WORD sel )
239 {
240     WORD newsel;
241     LDT_ENTRY entry;
242
243     newsel = SELECTOR_AllocArray( 1 );
244     TRACE("(%04x): returning %04x\n",
245                       sel, newsel );
246     if (!newsel) return 0;
247     wine_ldt_get_entry( sel, &entry );
248     entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
249     wine_ldt_set_entry( newsel, &entry );
250     return newsel;
251 }
252
253
254 /***********************************************************************
255  *           AllocDStoCSAlias   (KERNEL.171)
256  */
257 WORD WINAPI AllocDStoCSAlias16( WORD sel )
258 {
259     WORD newsel;
260     LDT_ENTRY entry;
261
262     newsel = SELECTOR_AllocArray( 1 );
263     TRACE("(%04x): returning %04x\n",
264                       sel, newsel );
265     if (!newsel) return 0;
266     wine_ldt_get_entry( sel, &entry );
267     entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
268     wine_ldt_set_entry( newsel, &entry );
269     return newsel;
270 }
271
272
273 /***********************************************************************
274  *           LongPtrAdd   (KERNEL.180)
275  */
276 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
277 {
278     LDT_ENTRY entry;
279     wine_ldt_get_entry( SELECTOROF(ptr), &entry );
280     wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
281     wine_ldt_set_entry( SELECTOROF(ptr), &entry );
282 }
283
284
285 /***********************************************************************
286  *           GetSelectorBase   (KERNEL.186)
287  */
288 DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
289 {
290     /*
291      * Note: For Win32s processes, the whole linear address space is
292      *       shifted by 0x10000 relative to the OS linear address space.
293      *       See the comment in msdos/vxd.c.
294      */
295
296     DWORD base = GetSelectorBase( sel );
297     return W32S_WINE2APP( base );
298 }
299 DWORD WINAPI GetSelectorBase( WORD sel )
300 {
301     void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
302
303     /* if base points into DOSMEM, assume we have to
304      * return pointer into physical lower 1MB */
305
306     return DOSMEM_MapLinearToDos( base );
307 }
308
309
310 /***********************************************************************
311  *           SetSelectorBase   (KERNEL.187)
312  */
313 DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
314 {
315     /*
316      * Note: For Win32s processes, the whole linear address space is
317      *       shifted by 0x10000 relative to the OS linear address space.
318      *       See the comment in msdos/vxd.c.
319      */
320
321     SetSelectorBase( sel, W32S_APP2WINE( base ) );
322     return sel;
323 }
324 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
325 {
326     LDT_ENTRY entry;
327     wine_ldt_get_entry( sel, &entry );
328     wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
329     wine_ldt_set_entry( sel, &entry );
330     return sel;
331 }
332
333
334 /***********************************************************************
335  *           GetSelectorLimit   (KERNEL.188)
336  */
337 DWORD WINAPI GetSelectorLimit16( WORD sel )
338 {
339     return wine_ldt_copy.limit[sel >> __AHSHIFT];
340 }
341
342
343 /***********************************************************************
344  *           SetSelectorLimit   (KERNEL.189)
345  */
346 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
347 {
348     LDT_ENTRY entry;
349     wine_ldt_get_entry( sel, &entry );
350     wine_ldt_set_limit( &entry, limit );
351     wine_ldt_set_entry( sel, &entry );
352     return sel;
353 }
354
355
356 /***********************************************************************
357  *           SelectorAccessRights   (KERNEL.196)
358  */
359 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
360 {
361     LDT_ENTRY entry;
362     wine_ldt_get_entry( sel, &entry );
363
364     if (op == 0)  /* get */
365     {
366         return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
367     }
368     else  /* set */
369     {
370         entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
371         entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
372         wine_ldt_set_entry( sel, &entry );
373         return 0;
374     }
375 }
376
377
378 /***********************************************************************
379  *           IsBadCodePtr16   (KERNEL.336)
380  */
381 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
382 {
383     WORD sel;
384     LDT_ENTRY entry;
385
386     sel = SELECTOROF(lpfn);
387     if (!sel) return TRUE;
388     if (IS_SELECTOR_FREE(sel)) return TRUE;
389     wine_ldt_get_entry( sel, &entry );
390     /* check for code segment, ignoring conforming, read-only and accessed bits */
391     if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
392     if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
393     return FALSE;
394 }
395
396
397 /***********************************************************************
398  *           IsBadStringPtr16   (KERNEL.337)
399  */
400 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
401 {
402     WORD sel;
403     LDT_ENTRY entry;
404
405     sel = SELECTOROF(ptr);
406     if (!sel) return TRUE;
407     if (IS_SELECTOR_FREE(sel)) return TRUE;
408     wine_ldt_get_entry( sel, &entry );
409     /* check for data or readable code segment */
410     if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
411     if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
412     if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
413     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
414     return FALSE;
415 }
416
417
418 /***********************************************************************
419  *           IsBadHugeReadPtr16   (KERNEL.346)
420  */
421 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
422 {
423     WORD sel;
424     LDT_ENTRY entry;
425
426     sel = SELECTOROF(ptr);
427     if (!sel) return TRUE;
428     if (IS_SELECTOR_FREE(sel)) return TRUE;
429     wine_ldt_get_entry( sel, &entry );
430     /* check for data or readable code segment */
431     if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
432     if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
433     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
434     return FALSE;
435 }
436
437
438 /***********************************************************************
439  *           IsBadHugeWritePtr16   (KERNEL.347)
440  */
441 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
442 {
443     WORD sel;
444     LDT_ENTRY entry;
445
446     sel = SELECTOROF(ptr);
447     if (!sel) return TRUE;
448     if (IS_SELECTOR_FREE(sel)) return TRUE;
449     wine_ldt_get_entry( sel, &entry );
450     /* check for writeable data segment, ignoring expand-down and accessed flags */
451     if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
452     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
453     return FALSE;
454 }
455
456 /***********************************************************************
457  *           IsBadReadPtr16   (KERNEL.334)
458  */
459 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
460 {
461     return IsBadHugeReadPtr16( ptr, size );
462 }
463
464
465 /***********************************************************************
466  *           IsBadWritePtr16   (KERNEL.335)
467  */
468 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
469 {
470     return IsBadHugeWritePtr16( ptr, size );
471 }
472
473
474 /***********************************************************************
475  *           IsBadFlatReadWritePtr16   (KERNEL.627)
476  */
477 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
478 {
479     return bWrite? IsBadHugeWritePtr16( ptr, size )
480                  : IsBadHugeReadPtr16( ptr, size );
481 }
482
483
484 /***********************************************************************
485  *           MemoryRead   (TOOLHELP.78)
486  */
487 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
488 {
489     WORD index = sel >> __AHSHIFT;
490
491     if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
492     if (offset > wine_ldt_copy.limit[index]) return 0;
493     if (offset + count > wine_ldt_copy.limit[index] + 1)
494         count = wine_ldt_copy.limit[index] + 1 - offset;
495     memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
496     return count;
497 }
498
499
500 /***********************************************************************
501  *           MemoryWrite   (TOOLHELP.79)
502  */
503 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
504 {
505     WORD index = sel >> __AHSHIFT;
506
507     if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
508     if (offset > wine_ldt_copy.limit[index]) return 0;
509     if (offset + count > wine_ldt_copy.limit[index] + 1)
510         count = wine_ldt_copy.limit[index] + 1 - offset;
511     memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
512     return count;
513 }
514
515 /************************************* Win95 pointer mapping functions *
516  *
517  */
518
519 /***********************************************************************
520  *           MapSL   (KERNEL32.523)
521  *
522  * Maps fixed segmented pointer to linear.
523  */
524 LPVOID WINAPI MapSL( SEGPTR sptr )
525 {
526     return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
527 }
528
529 /***********************************************************************
530  *           MapSLFix   (KERNEL32.524)
531  *
532  * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
533  * unexpected linear address change when GlobalCompact() shuffles
534  * moveable blocks.
535  */
536
537 LPVOID WINAPI MapSLFix( SEGPTR sptr )
538 {
539     return MapSL(sptr);
540 }
541
542 /***********************************************************************
543  *           UnMapSLFixArray   (KERNEL32.701)
544  */
545
546 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
547 {
548     /* Must not change EAX, hence defined as 'register' function */
549 }
550
551 /***********************************************************************
552  *           GetThreadSelectorEntry   (KERNEL32)
553  */
554 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
555 {
556 #ifdef __i386__
557     BOOL ret;
558
559     if (!(sel & 4))  /* GDT selector */
560     {
561         sel &= ~3;  /* ignore RPL */
562         if (!sel)  /* null selector */
563         {
564             memset( ldtent, 0, sizeof(*ldtent) );
565             return TRUE;
566         }
567         ldtent->BaseLow                   = 0;
568         ldtent->HighWord.Bits.BaseMid     = 0;
569         ldtent->HighWord.Bits.BaseHi      = 0;
570         ldtent->LimitLow                  = 0xffff;
571         ldtent->HighWord.Bits.LimitHi     = 0xf;
572         ldtent->HighWord.Bits.Dpl         = 3;
573         ldtent->HighWord.Bits.Sys         = 0;
574         ldtent->HighWord.Bits.Pres        = 1;
575         ldtent->HighWord.Bits.Granularity = 1;
576         ldtent->HighWord.Bits.Default_Big = 1;
577         ldtent->HighWord.Bits.Type        = 0x12;
578         /* it has to be one of the system GDT selectors */
579         if (sel == (__get_ds() & ~3)) return TRUE;
580         if (sel == (__get_ss() & ~3)) return TRUE;
581         if (sel == (__get_cs() & ~3))
582         {
583             ldtent->HighWord.Bits.Type |= 8;  /* code segment */
584             return TRUE;
585         }
586         SetLastError( ERROR_NOACCESS );
587         return FALSE;
588     }
589
590     SERVER_START_REQ
591     {
592         struct get_selector_entry_request *req = server_alloc_req( sizeof(*req), 0 );
593
594         req->handle = hthread;
595         req->entry = sel >> __AHSHIFT;
596         if ((ret = !server_call( REQ_GET_SELECTOR_ENTRY )))
597         {
598             if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
599             {
600                 SetLastError( ERROR_MR_MID_NOT_FOUND );  /* sic */
601                 ret = FALSE;
602             }
603             else
604             {
605                 wine_ldt_set_base( ldtent, (void *)req->base );
606                 wine_ldt_set_limit( ldtent, req->limit );
607                 wine_ldt_set_flags( ldtent, req->flags );
608             }
609         }
610     }
611     SERVER_END_REQ;
612     return ret;
613 #else
614     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
615     return FALSE;
616 #endif
617 }
618
619
620 /**********************************************************************
621  *              SMapLS*         (KERNEL32)
622  * These functions map linear pointers at [EBP+xxx] to segmented pointers
623  * and return them.
624  * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
625  * unravel them at SUnMapLS. We just store the segmented pointer there.
626  */
627 static void
628 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
629     DWORD       val,ptr; 
630
631     val =*(DWORD*)(context->Ebp + argoff);
632     if (val<0x10000) {
633         ptr=val;
634         *(DWORD*)(context->Ebp + argoff) = 0;
635     } else {
636         ptr = MapLS((LPVOID)val);
637         *(DWORD*)(context->Ebp + argoff) = ptr;
638     }
639     context->Eax = ptr;
640 }
641
642 /***********************************************************************
643  *              SMapLS_IP_EBP_8 (KERNEL32.601)
644  */
645 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
646
647 /***********************************************************************
648  *              SMapLS_IP_EBP_12 (KERNEL32.593)
649  */
650 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
651
652 /***********************************************************************
653  *              SMapLS_IP_EBP_16 (KERNEL32.594)
654  */
655 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
656
657 /***********************************************************************
658  *              SMapLS_IP_EBP_20 (KERNEL32.595)
659  */
660 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
661
662 /***********************************************************************
663  *              SMapLS_IP_EBP_24 (KERNEL32.596)
664  */
665 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
666
667 /***********************************************************************
668  *              SMapLS_IP_EBP_28 (KERNEL32.597)
669  */
670 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
671
672 /***********************************************************************
673  *              SMapLS_IP_EBP_32 (KERNEL32.598)
674  */
675 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
676
677 /***********************************************************************
678  *              SMapLS_IP_EBP_36 (KERNEL32.599)
679  */
680 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
681
682 /***********************************************************************
683  *              SMapLS_IP_EBP_40 (KERNEL32.600)
684  */
685 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
686
687 /***********************************************************************
688  *              SMapLS (KERNEL32.592)
689  */
690 void WINAPI SMapLS( CONTEXT86 *context )
691 {
692     if (HIWORD(context->Eax))
693     {
694         context->Eax = MapLS( (LPVOID)context->Eax );
695         context->Edx = context->Eax;
696     } else {
697         context->Edx = 0;
698     }
699 }
700
701 /***********************************************************************
702  *              SUnMapLS (KERNEL32.602)
703  */
704
705 void WINAPI SUnMapLS( CONTEXT86 *context )
706 {
707     if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
708 }
709
710 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
711 {
712     SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
713     if (*ptr)
714     {
715         UnMapLS( *ptr );
716         *ptr = 0;
717     }
718 }
719
720 /***********************************************************************
721  *              SUnMapLS_IP_EBP_8 (KERNEL32.611)
722  */
723 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
724
725 /***********************************************************************
726  *              SUnMapLS_IP_EBP_12 (KERNEL32.603)
727  */
728 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
729
730 /***********************************************************************
731  *              SUnMapLS_IP_EBP_16 (KERNEL32.604)
732  */
733 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
734
735 /***********************************************************************
736  *              SUnMapLS_IP_EBP_20 (KERNEL32.605)
737  */
738 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
739
740 /***********************************************************************
741  *              SUnMapLS_IP_EBP_24 (KERNEL32.606)
742  */
743 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
744
745 /***********************************************************************
746  *              SUnMapLS_IP_EBP_28 (KERNEL32.607)
747  */
748 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
749
750 /***********************************************************************
751  *              SUnMapLS_IP_EBP_32 (KERNEL32.608)
752  */
753 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
754
755 /***********************************************************************
756  *              SUnMapLS_IP_EBP_36 (KERNEL32.609)
757  */
758 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
759
760 /***********************************************************************
761  *              SUnMapLS_IP_EBP_40 (KERNEL32.610)
762  */
763 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
764
765 /**********************************************************************
766  *              AllocMappedBuffer       (KERNEL32.38)
767  *
768  * This is a undocumented KERNEL32 function that 
769  * SMapLS's a GlobalAlloc'ed buffer.
770  *
771  * Input:   EDI register: size of buffer to allocate
772  * Output:  EDI register: pointer to buffer
773  *
774  * Note: The buffer is preceded by 8 bytes:
775  *        ...
776  *       edi+0   buffer
777  *       edi-4   SEGPTR to buffer
778  *       edi-8   some magic Win95 needs for SUnMapLS
779  *               (we use it for the memory handle)
780  *
781  *       The SEGPTR is used by the caller!
782  */
783
784 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
785 {
786     HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
787     DWORD *buffer = (DWORD *)GlobalLock(handle);
788     SEGPTR ptr = 0;
789
790     if (buffer)
791         if (!(ptr = MapLS(buffer + 2)))
792         {
793             GlobalUnlock(handle);
794             GlobalFree(handle);
795         }
796
797     if (!ptr)
798         context->Eax = context->Edi = 0;
799     else
800     {
801         buffer[0] = handle;
802         buffer[1] = ptr;
803
804         context->Eax = (DWORD) ptr;
805         context->Edi = (DWORD)(buffer + 2);
806     }
807 }
808
809 /**********************************************************************
810  *              FreeMappedBuffer        (KERNEL32.39)
811  *
812  * Free a buffer allocated by AllocMappedBuffer
813  *
814  * Input: EDI register: pointer to buffer
815  */
816
817 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
818 {
819     if (context->Edi)
820     {
821         DWORD *buffer = (DWORD *)context->Edi - 2;
822
823         UnMapLS(buffer[1]);
824
825         GlobalUnlock(buffer[0]);
826         GlobalFree(buffer[0]);
827     }
828 }
829
830 #ifdef __i386__
831 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
832 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
833 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
834 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
835 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
836 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
837 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
838 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
839 #endif