Added a first-cut version of MapVirtualKeyExW() that has the same
[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 "ldt.h"
13 #include "miscemu.h"
14 #include "selectors.h"
15 #include "stackframe.h"
16 #include "process.h"
17 #include "server.h"
18 #include "debugtools.h"
19 #include "toolhelp.h"
20
21 DEFAULT_DEBUG_CHANNEL(selector);
22
23 #define LDT_SIZE 8192
24
25 /* get the number of selectors needed to cover up to the selector limit */
26 inline 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  */
36 static 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
55
56 /***********************************************************************
57  *           AllocSelectorArray   (KERNEL.206)
58  */
59 WORD WINAPI AllocSelectorArray16( WORD count )
60 {
61     WORD i, sel = SELECTOR_AllocArray( count );
62
63     if (sel)
64     {
65         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 );
70     }
71     return sel;
72 }
73
74
75 /***********************************************************************
76  *           AllocSelector   (KERNEL.175)
77  */
78 WORD WINAPI AllocSelector16( WORD sel )
79 {
80     WORD newsel, count, i;
81
82     count = sel ? get_sel_count(sel) : 1;
83     newsel = SELECTOR_AllocArray( count );
84     TRACE("(%04x): returning %04x\n", sel, newsel );
85     if (!newsel) return 0;
86     if (!sel) return newsel;  /* nothing to copy */
87     for (i = 0; i < count; i++)
88     {
89         LDT_ENTRY entry;
90         wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
91         wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
92     }
93     return newsel;
94 }
95
96
97 /***********************************************************************
98  *           FreeSelector   (KERNEL.176)
99  */
100 WORD WINAPI FreeSelector16( WORD sel )
101 {
102     LDT_ENTRY entry;
103
104     if (IS_SELECTOR_FREE(sel)) return sel;  /* error */
105
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;
119     return 0;
120 }
121
122
123 /***********************************************************************
124  *           SELECTOR_SetEntries
125  *
126  * Set the LDT entries for an array of selectors.
127  */
128 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
129 {
130     LDT_ENTRY entry;
131     WORD i, count;
132
133     wine_ldt_set_base( &entry, base );
134     wine_ldt_set_limit( &entry, size - 1 );
135     wine_ldt_set_flags( &entry, flags );
136     /* Make sure base and limit are not 0 together if the size is not 0 */
137     if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
138     count = (size + 0xffff) / 0x10000;
139     for (i = 0; i < count; i++)
140     {
141         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 );
144     }
145 }
146
147
148 /***********************************************************************
149  *           SELECTOR_AllocBlock
150  *
151  * Allocate selectors for a block of linear memory.
152  */
153 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
154 {
155     WORD sel, count;
156
157     if (!size) return 0;
158     count = (size + 0xffff) / 0x10000;
159     sel = SELECTOR_AllocArray( count );
160     if (sel) SELECTOR_SetEntries( sel, base, size, flags );
161     return sel;
162 }
163
164
165 /***********************************************************************
166  *           SELECTOR_FreeBlock
167  *
168  * Free a block of selectors.
169  */
170 void SELECTOR_FreeBlock( WORD sel )
171 {
172     WORD i, count = get_sel_count( sel );
173
174     TRACE("(%04x,%d)\n", sel, count );
175     for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
176 }
177
178
179 /***********************************************************************
180  *           SELECTOR_ReallocBlock
181  *
182  * Change the size of a block of selectors.
183  */
184 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
185 {
186     LDT_ENTRY entry;
187     WORD i, oldcount, newcount;
188
189     if (!size) size = 1;
190     oldcount = get_sel_count( sel );
191     newcount = (size + 0xffff) >> 16;
192     wine_ldt_get_entry( sel, &entry );
193
194     if (oldcount < newcount)  /* We need to add selectors */
195     {
196         WORD index = sel >> __AHSHIFT;
197           /* Check if the next selectors are free */
198         if (index + newcount > LDT_SIZE) i = oldcount;
199         else
200             for (i = oldcount; i < newcount; i++)
201                 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
202
203         if (i < newcount)  /* they are not free */
204         {
205             SELECTOR_FreeBlock( sel );
206             sel = SELECTOR_AllocArray( newcount );
207         }
208         else  /* mark the selectors as allocated */
209         {
210             for (i = oldcount; i < newcount; i++)
211                 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
212         }
213     }
214     else if (oldcount > newcount) /* We need to remove selectors */
215     {
216         SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
217     }
218     if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
219     return sel;
220 }
221
222
223 /***********************************************************************
224  *           PrestoChangoSelector   (KERNEL.177)
225  */
226 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
227 {
228     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 );
233     return selDst;
234 }
235
236
237 /***********************************************************************
238  *           AllocCStoDSAlias   (KERNEL.170)
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, W32S_APPLICATION() ? W32S_OFFSET : 0 );
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,
324         W32S_APP2WINE( base, W32S_APPLICATION() ? W32S_OFFSET : 0 ) );
325     return sel;
326 }
327 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
328 {
329     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 );
333     return sel;
334 }
335
336
337 /***********************************************************************
338  *           GetSelectorLimit   (KERNEL.188)
339  */
340 DWORD WINAPI GetSelectorLimit16( WORD sel )
341 {
342     return wine_ldt_copy.limit[sel >> __AHSHIFT];
343 }
344
345
346 /***********************************************************************
347  *           SetSelectorLimit   (KERNEL.189)
348  */
349 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
350 {
351     LDT_ENTRY entry;
352     wine_ldt_get_entry( sel, &entry );
353     wine_ldt_set_limit( &entry, limit );
354     wine_ldt_set_entry( sel, &entry );
355     return sel;
356 }
357
358
359 /***********************************************************************
360  *           SelectorAccessRights   (KERNEL.196)
361  */
362 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
363 {
364     LDT_ENTRY entry;
365     wine_ldt_get_entry( sel, &entry );
366
367     if (op == 0)  /* get */
368     {
369         return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
370     }
371     else  /* set */
372     {
373         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 );
376         return 0;
377     }
378 }
379
380
381 /***********************************************************************
382  *           IsBadCodePtr16   (KERNEL.336)
383  */
384 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
385 {
386     WORD sel;
387     LDT_ENTRY entry;
388
389     sel = SELECTOROF(lpfn);
390     if (!sel) return TRUE;
391     if (IS_SELECTOR_FREE(sel)) return TRUE;
392     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;
396     return FALSE;
397 }
398
399
400 /***********************************************************************
401  *           IsBadStringPtr16   (KERNEL.337)
402  */
403 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
404 {
405     WORD sel;
406     LDT_ENTRY entry;
407
408     sel = SELECTOROF(ptr);
409     if (!sel) return TRUE;
410     if (IS_SELECTOR_FREE(sel)) return TRUE;
411     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 */
415     if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr)) + 1;
416     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
417     return FALSE;
418 }
419
420
421 /***********************************************************************
422  *           IsBadHugeReadPtr16   (KERNEL.346)
423  */
424 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
425 {
426     WORD sel;
427     LDT_ENTRY entry;
428
429     sel = SELECTOROF(ptr);
430     if (!sel) return TRUE;
431     if (IS_SELECTOR_FREE(sel)) return TRUE;
432     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;
437     return FALSE;
438 }
439
440
441 /***********************************************************************
442  *           IsBadHugeWritePtr16   (KERNEL.347)
443  */
444 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
445 {
446     WORD sel;
447     LDT_ENTRY entry;
448
449     sel = SELECTOROF(ptr);
450     if (!sel) return TRUE;
451     if (IS_SELECTOR_FREE(sel)) return TRUE;
452     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;
456     return FALSE;
457 }
458
459 /***********************************************************************
460  *           IsBadReadPtr16   (KERNEL.334)
461  */
462 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
463 {
464     return IsBadHugeReadPtr16( ptr, size );
465 }
466
467
468 /***********************************************************************
469  *           IsBadWritePtr16   (KERNEL.335)
470  */
471 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
472 {
473     return IsBadHugeWritePtr16( ptr, size );
474 }
475
476
477 /***********************************************************************
478  *           IsBadFlatReadWritePtr16   (KERNEL.627)
479  */
480 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
481 {
482     return bWrite? IsBadHugeWritePtr16( ptr, size )
483                  : IsBadHugeReadPtr16( ptr, size );
484 }
485
486
487 /***********************************************************************
488  *           MemoryRead   (TOOLHELP.78)
489  */
490 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
491 {
492     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 );
499     return count;
500 }
501
502
503 /***********************************************************************
504  *           MemoryWrite   (TOOLHELP.79)
505  */
506 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
507 {
508     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 );
515     return count;
516 }
517
518 /************************************* Win95 pointer mapping functions *
519  *
520  */
521
522 /***********************************************************************
523  *           MapSL   (KERNEL32.523)
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.524)
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 (LPVOID)PTR_SEG_TO_LIN(sptr);
543 }
544
545 /***********************************************************************
546  *           UnMapSLFixArray   (KERNEL32.701)
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
594     {
595         struct get_selector_entry_request *req = server_alloc_req( sizeof(*req), 0 );
596
597         req->handle = hthread;
598         req->entry = sel >> __AHSHIFT;
599         if ((ret = !server_call( REQ_GET_SELECTOR_ENTRY )))
600         {
601             if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
602             {
603                 SetLastError( ERROR_MR_MID_NOT_FOUND );  /* sic */
604                 ret = FALSE;
605             }
606             else
607             {
608                 wine_ldt_set_base( ldtent, (void *)req->base );
609                 wine_ldt_set_limit( ldtent, req->limit );
610                 wine_ldt_set_flags( ldtent, req->flags );
611             }
612         }
613     }
614     SERVER_END_REQ;
615     return ret;
616 #else
617     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
618     return FALSE;
619 #endif
620 }
621
622
623 /**********************************************************************
624  *              SMapLS*         (KERNEL32)
625  * These functions map linear pointers at [EBP+xxx] to segmented pointers
626  * and return them.
627  * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
628  * unravel them at SUnMapLS. We just store the segmented pointer there.
629  */
630 static void
631 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
632     DWORD       val,ptr; 
633
634     val =*(DWORD*)(context->Ebp + argoff);
635     if (val<0x10000) {
636         ptr=val;
637         *(DWORD*)(context->Ebp + argoff) = 0;
638     } else {
639         ptr = MapLS((LPVOID)val);
640         *(DWORD*)(context->Ebp + argoff) = ptr;
641     }
642     context->Eax = ptr;
643 }
644
645 /***********************************************************************
646  *              SMapLS_IP_EBP_8 (KERNEL32.601)
647  */
648 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
649
650 /***********************************************************************
651  *              SMapLS_IP_EBP_12 (KERNEL32.593)
652  */
653 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
654
655 /***********************************************************************
656  *              SMapLS_IP_EBP_16 (KERNEL32.594)
657  */
658 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
659
660 /***********************************************************************
661  *              SMapLS_IP_EBP_20 (KERNEL32.595)
662  */
663 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
664
665 /***********************************************************************
666  *              SMapLS_IP_EBP_24 (KERNEL32.596)
667  */
668 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
669
670 /***********************************************************************
671  *              SMapLS_IP_EBP_28 (KERNEL32.597)
672  */
673 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
674
675 /***********************************************************************
676  *              SMapLS_IP_EBP_32 (KERNEL32.598)
677  */
678 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
679
680 /***********************************************************************
681  *              SMapLS_IP_EBP_36 (KERNEL32.599)
682  */
683 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
684
685 /***********************************************************************
686  *              SMapLS_IP_EBP_40 (KERNEL32.600)
687  */
688 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
689
690 /***********************************************************************
691  *              SMapLS (KERNEL32.592)
692  */
693 void WINAPI SMapLS( CONTEXT86 *context )
694 {
695     if (HIWORD(context->Eax))
696     {
697         context->Eax = MapLS( (LPVOID)context->Eax );
698         context->Edx = context->Eax;
699     } else {
700         context->Edx = 0;
701     }
702 }
703
704 /***********************************************************************
705  *              SUnMapLS (KERNEL32.602)
706  */
707
708 void WINAPI SUnMapLS( CONTEXT86 *context )
709 {
710     if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
711 }
712
713 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
714 {
715     SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
716     if (*ptr)
717     {
718         UnMapLS( *ptr );
719         *ptr = 0;
720     }
721 }
722
723 /***********************************************************************
724  *              SUnMapLS_IP_EBP_8 (KERNEL32.611)
725  */
726 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
727
728 /***********************************************************************
729  *              SUnMapLS_IP_EBP_12 (KERNEL32.603)
730  */
731 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
732
733 /***********************************************************************
734  *              SUnMapLS_IP_EBP_16 (KERNEL32.604)
735  */
736 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
737
738 /***********************************************************************
739  *              SUnMapLS_IP_EBP_20 (KERNEL32.605)
740  */
741 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
742
743 /***********************************************************************
744  *              SUnMapLS_IP_EBP_24 (KERNEL32.606)
745  */
746 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
747
748 /***********************************************************************
749  *              SUnMapLS_IP_EBP_28 (KERNEL32.607)
750  */
751 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
752
753 /***********************************************************************
754  *              SUnMapLS_IP_EBP_32 (KERNEL32.608)
755  */
756 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
757
758 /***********************************************************************
759  *              SUnMapLS_IP_EBP_36 (KERNEL32.609)
760  */
761 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
762
763 /***********************************************************************
764  *              SUnMapLS_IP_EBP_40 (KERNEL32.610)
765  */
766 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
767
768 /**********************************************************************
769  *              AllocMappedBuffer       (KERNEL32.38)
770  *
771  * This is a undocumented KERNEL32 function that 
772  * SMapLS's a GlobalAlloc'ed buffer.
773  *
774  * Input:   EDI register: size of buffer to allocate
775  * Output:  EDI register: pointer to buffer
776  *
777  * Note: The buffer is preceded by 8 bytes:
778  *        ...
779  *       edi+0   buffer
780  *       edi-4   SEGPTR to buffer
781  *       edi-8   some magic Win95 needs for SUnMapLS
782  *               (we use it for the memory handle)
783  *
784  *       The SEGPTR is used by the caller!
785  */
786
787 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
788 {
789     HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
790     DWORD *buffer = (DWORD *)GlobalLock(handle);
791     SEGPTR ptr = 0;
792
793     if (buffer)
794         if (!(ptr = MapLS(buffer + 2)))
795         {
796             GlobalUnlock(handle);
797             GlobalFree(handle);
798         }
799
800     if (!ptr)
801         context->Eax = context->Edi = 0;
802     else
803     {
804         buffer[0] = handle;
805         buffer[1] = ptr;
806
807         context->Eax = (DWORD) ptr;
808         context->Edi = (DWORD)(buffer + 2);
809     }
810 }
811
812 /**********************************************************************
813  *              FreeMappedBuffer        (KERNEL32.39)
814  *
815  * Free a buffer allocated by AllocMappedBuffer
816  *
817  * Input: EDI register: pointer to buffer
818  */
819
820 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
821 {
822     if (context->Edi)
823     {
824         DWORD *buffer = (DWORD *)context->Edi - 2;
825
826         UnMapLS(buffer[1]);
827
828         GlobalUnlock(buffer[0]);
829         GlobalFree(buffer[0]);
830     }
831 }
832
833 #ifdef __i386__
834 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
835 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
836 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
837 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
838 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
839 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
840 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
841 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
842 #endif