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