Update the address of the Free Software Foundation.
[wine] / dlls / kernel / selector.c
1 /*
2  * Selector manipulation functions
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <string.h>
25
26 #include "winerror.h"
27 #include "wine/winbase16.h"
28 #include "wine/server.h"
29 #include "wine/debug.h"
30 #include "kernel_private.h"
31 #include "toolhelp.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(selector);
34
35 #define LDT_SIZE 8192
36
37 /* get the number of selectors needed to cover up to the selector limit */
38 inline static WORD get_sel_count( WORD sel )
39 {
40     return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
41 }
42
43
44 /***********************************************************************
45  *           AllocSelectorArray   (KERNEL.206)
46  */
47 WORD WINAPI AllocSelectorArray16( WORD count )
48 {
49     WORD i, sel = wine_ldt_alloc_entries( count );
50
51     if (sel)
52     {
53         LDT_ENTRY entry;
54         wine_ldt_set_base( &entry, 0 );
55         wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
56         wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
57         for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
58     }
59     return sel;
60 }
61
62
63 /***********************************************************************
64  *           AllocSelector   (KERNEL.175)
65  */
66 WORD WINAPI AllocSelector16( WORD sel )
67 {
68     WORD newsel, count, i;
69
70     count = sel ? get_sel_count(sel) : 1;
71     newsel = wine_ldt_alloc_entries( count );
72     TRACE("(%04x): returning %04x\n", sel, newsel );
73     if (!newsel) return 0;
74     if (!sel) return newsel;  /* nothing to copy */
75     for (i = 0; i < count; i++)
76     {
77         LDT_ENTRY entry;
78         wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
79         wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
80     }
81     return newsel;
82 }
83
84
85 /***********************************************************************
86  *           FreeSelector   (KERNEL.176)
87  */
88 WORD WINAPI FreeSelector16( WORD sel )
89 {
90     LDT_ENTRY entry;
91
92     wine_ldt_get_entry( sel, &entry );
93     if (wine_ldt_is_empty( &entry )) return sel;  /* error */
94 #ifdef __i386__
95     /* Check if we are freeing current %fs selector */
96     if (!((wine_get_fs() ^ sel) & ~3))
97         WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
98 #endif  /* __i386__ */
99     wine_ldt_free_entries( sel, 1 );
100     return 0;
101 }
102
103
104 /***********************************************************************
105  *           SELECTOR_SetEntries
106  *
107  * Set the LDT entries for an array of selectors.
108  */
109 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
110 {
111     LDT_ENTRY entry;
112     WORD i, count;
113
114     wine_ldt_set_base( &entry, base );
115     wine_ldt_set_limit( &entry, size - 1 );
116     wine_ldt_set_flags( &entry, flags );
117     count = (size + 0xffff) / 0x10000;
118     for (i = 0; i < count; i++)
119     {
120         wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
121         wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
122         /* yep, Windows sets limit like that, not 64K sel units */
123         wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
124     }
125 }
126
127
128 /***********************************************************************
129  *           SELECTOR_AllocBlock
130  *
131  * Allocate selectors for a block of linear memory.
132  */
133 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
134 {
135     WORD sel, count;
136
137     if (!size) return 0;
138     count = (size + 0xffff) / 0x10000;
139     sel = wine_ldt_alloc_entries( count );
140     if (sel) SELECTOR_SetEntries( sel, base, size, flags );
141     return sel;
142 }
143
144
145 /***********************************************************************
146  *           SELECTOR_FreeBlock
147  *
148  * Free a block of selectors.
149  */
150 void SELECTOR_FreeBlock( WORD sel )
151 {
152     WORD i, count = get_sel_count( sel );
153
154     TRACE("(%04x,%d)\n", sel, count );
155     for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
156 }
157
158
159 /***********************************************************************
160  *           SELECTOR_ReallocBlock
161  *
162  * Change the size of a block of selectors.
163  */
164 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
165 {
166     LDT_ENTRY entry;
167     int oldcount, newcount;
168
169     if (!size) size = 1;
170     wine_ldt_get_entry( sel, &entry );
171     oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
172     newcount = (size + 0xffff) >> 16;
173
174     sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
175     if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
176     return sel;
177 }
178
179
180 /***********************************************************************
181  *           PrestoChangoSelector   (KERNEL.177)
182  */
183 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
184 {
185     LDT_ENTRY entry;
186     wine_ldt_get_entry( selSrc, &entry );
187     /* toggle the executable bit */
188     entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
189     wine_ldt_set_entry( selDst, &entry );
190     return selDst;
191 }
192
193
194 /***********************************************************************
195  *           AllocCStoDSAlias   (KERNEL.170)
196  *           AllocAlias         (KERNEL.172)
197  */
198 WORD WINAPI AllocCStoDSAlias16( WORD sel )
199 {
200     WORD newsel;
201     LDT_ENTRY entry;
202
203     newsel = wine_ldt_alloc_entries( 1 );
204     TRACE("(%04x): returning %04x\n",
205                       sel, newsel );
206     if (!newsel) return 0;
207     wine_ldt_get_entry( sel, &entry );
208     entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
209     wine_ldt_set_entry( newsel, &entry );
210     return newsel;
211 }
212
213
214 /***********************************************************************
215  *           AllocDStoCSAlias   (KERNEL.171)
216  */
217 WORD WINAPI AllocDStoCSAlias16( WORD sel )
218 {
219     WORD newsel;
220     LDT_ENTRY entry;
221
222     newsel = wine_ldt_alloc_entries( 1 );
223     TRACE("(%04x): returning %04x\n",
224                       sel, newsel );
225     if (!newsel) return 0;
226     wine_ldt_get_entry( sel, &entry );
227     entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
228     wine_ldt_set_entry( newsel, &entry );
229     return newsel;
230 }
231
232
233 /***********************************************************************
234  *           LongPtrAdd   (KERNEL.180)
235  */
236 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
237 {
238     LDT_ENTRY entry;
239     wine_ldt_get_entry( SELECTOROF(ptr), &entry );
240     wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
241     wine_ldt_set_entry( SELECTOROF(ptr), &entry );
242 }
243
244
245 /***********************************************************************
246  *             GetSelectorBase   (KERNEL.186)
247  */
248 DWORD WINAPI GetSelectorBase( WORD sel )
249 {
250     void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
251
252     /* if base points into DOSMEM, assume we have to
253      * return pointer into physical lower 1MB */
254
255     return DOSMEM_MapLinearToDos( base );
256 }
257
258
259 /***********************************************************************
260  *             SetSelectorBase   (KERNEL.187)
261  */
262 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
263 {
264     LDT_ENTRY entry;
265     wine_ldt_get_entry( sel, &entry );
266     wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
267     wine_ldt_set_entry( sel, &entry );
268     return sel;
269 }
270
271
272 /***********************************************************************
273  *           GetSelectorLimit   (KERNEL.188)
274  */
275 DWORD WINAPI GetSelectorLimit16( WORD sel )
276 {
277     return wine_ldt_copy.limit[sel >> __AHSHIFT];
278 }
279
280
281 /***********************************************************************
282  *           SetSelectorLimit   (KERNEL.189)
283  */
284 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
285 {
286     LDT_ENTRY entry;
287     wine_ldt_get_entry( sel, &entry );
288     wine_ldt_set_limit( &entry, limit );
289     wine_ldt_set_entry( sel, &entry );
290     return sel;
291 }
292
293
294 /***********************************************************************
295  *           SelectorAccessRights   (KERNEL.196)
296  */
297 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
298 {
299     LDT_ENTRY entry;
300     wine_ldt_get_entry( sel, &entry );
301
302     if (op == 0)  /* get */
303     {
304         return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
305     }
306     else  /* set */
307     {
308         entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
309         entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
310         wine_ldt_set_entry( sel, &entry );
311         return 0;
312     }
313 }
314
315
316 /***********************************************************************
317  *           IsBadCodePtr   (KERNEL.336)
318  */
319 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
320 {
321     WORD sel;
322     LDT_ENTRY entry;
323
324     sel = SELECTOROF(lpfn);
325     if (!sel) return TRUE;
326     wine_ldt_get_entry( sel, &entry );
327     if (wine_ldt_is_empty( &entry )) return TRUE;
328     /* check for code segment, ignoring conforming, read-only and accessed bits */
329     if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
330     if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
331     return FALSE;
332 }
333
334
335 /***********************************************************************
336  *           IsBadStringPtr   (KERNEL.337)
337  */
338 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
339 {
340     WORD sel;
341     LDT_ENTRY entry;
342
343     sel = SELECTOROF(ptr);
344     if (!sel) return TRUE;
345     wine_ldt_get_entry( sel, &entry );
346     if (wine_ldt_is_empty( &entry )) return TRUE;
347     /* check for data or readable code segment */
348     if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
349     if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
350     if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
351     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
352     return FALSE;
353 }
354
355
356 /***********************************************************************
357  *           IsBadHugeReadPtr   (KERNEL.346)
358  */
359 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
360 {
361     WORD sel;
362     LDT_ENTRY entry;
363
364     sel = SELECTOROF(ptr);
365     if (!sel) return TRUE;
366     wine_ldt_get_entry( sel, &entry );
367     if (wine_ldt_is_empty( &entry )) return TRUE;
368     /* check for data or readable code segment */
369     if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
370     if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
371     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
372     return FALSE;
373 }
374
375
376 /***********************************************************************
377  *           IsBadHugeWritePtr   (KERNEL.347)
378  */
379 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
380 {
381     WORD sel;
382     LDT_ENTRY entry;
383
384     sel = SELECTOROF(ptr);
385     if (!sel) return TRUE;
386     wine_ldt_get_entry( sel, &entry );
387     if (wine_ldt_is_empty( &entry )) return TRUE;
388     /* check for writeable data segment, ignoring expand-down and accessed flags */
389     if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
390     if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
391     return FALSE;
392 }
393
394 /***********************************************************************
395  *           IsBadReadPtr   (KERNEL.334)
396  */
397 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
398 {
399     return IsBadHugeReadPtr16( ptr, size );
400 }
401
402
403 /***********************************************************************
404  *           IsBadWritePtr   (KERNEL.335)
405  */
406 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
407 {
408     return IsBadHugeWritePtr16( ptr, size );
409 }
410
411
412 /***********************************************************************
413  *           IsBadFlatReadWritePtr   (KERNEL.627)
414  */
415 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
416 {
417     return bWrite? IsBadHugeWritePtr16( ptr, size )
418                  : IsBadHugeReadPtr16( ptr, size );
419 }
420
421
422 /***********************************************************************
423  *           MemoryRead   (TOOLHELP.78)
424  */
425 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
426 {
427     LDT_ENTRY entry;
428     DWORD limit;
429
430     wine_ldt_get_entry( sel, &entry );
431     if (wine_ldt_is_empty( &entry )) return 0;
432     limit = wine_ldt_get_limit( &entry );
433     if (offset > limit) return 0;
434     if (offset + count > limit + 1) count = limit + 1 - offset;
435     memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
436     return count;
437 }
438
439
440 /***********************************************************************
441  *           MemoryWrite   (TOOLHELP.79)
442  */
443 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
444 {
445     LDT_ENTRY entry;
446     DWORD limit;
447
448     wine_ldt_get_entry( sel, &entry );
449     if (wine_ldt_is_empty( &entry )) return 0;
450     limit = wine_ldt_get_limit( &entry );
451     if (offset > limit) return 0;
452     if (offset + count > limit) count = limit + 1 - offset;
453     memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
454     return count;
455 }
456
457 /************************************* Win95 pointer mapping functions *
458  *
459  */
460
461 struct mapls_entry
462 {
463     struct mapls_entry *next;
464     void               *addr;   /* linear address */
465     int                 count;  /* ref count */
466     WORD                sel;    /* selector */
467 };
468
469 static struct mapls_entry *first_entry;
470
471
472 /***********************************************************************
473  *           MapLS   (KERNEL32.@)
474  *           MapLS   (KERNEL.358)
475  *
476  * Maps linear pointer to segmented.
477  */
478 SEGPTR WINAPI MapLS( LPCVOID ptr )
479 {
480     struct mapls_entry *entry, *free = NULL;
481     const void *base;
482     SEGPTR ret = 0;
483
484     if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr);
485
486     base = (const char *)ptr - ((unsigned int)ptr & 0x7fff);
487     HeapLock( GetProcessHeap() );
488     for (entry = first_entry; entry; entry = entry->next)
489     {
490         if (entry->addr == base) break;
491         if (!entry->count) free = entry;
492     }
493
494     if (!entry)
495     {
496         if (!free)  /* no free entry found, create a new one */
497         {
498             if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
499             if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
500             {
501                 HeapFree( GetProcessHeap(), 0, free );
502                 goto done;
503             }
504             free->count = 0;
505             free->next = first_entry;
506             first_entry = free;
507         }
508         SetSelectorBase( free->sel, (DWORD)base );
509         free->addr = (void*)base;
510         entry = free;
511     }
512     entry->count++;
513     ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
514  done:
515     HeapUnlock( GetProcessHeap() );
516     return ret;
517 }
518
519 /***********************************************************************
520  *           UnMapLS   (KERNEL32.@)
521  *           UnMapLS   (KERNEL.359)
522  *
523  * Free mapped selector.
524  */
525 void WINAPI UnMapLS( SEGPTR sptr )
526 {
527     struct mapls_entry *entry;
528     WORD sel = SELECTOROF(sptr);
529
530     if (sel)
531     {
532         HeapLock( GetProcessHeap() );
533         for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
534         if (entry && entry->count > 0) entry->count--;
535         HeapUnlock( GetProcessHeap() );
536     }
537 }
538
539 /***********************************************************************
540  *           MapSL   (KERNEL32.@)
541  *           MapSL   (KERNEL.357)
542  *
543  * Maps fixed segmented pointer to linear.
544  */
545 LPVOID WINAPI MapSL( SEGPTR sptr )
546 {
547     return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
548 }
549
550 /***********************************************************************
551  *           MapSLFix   (KERNEL32.@)
552  *
553  * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
554  * unexpected linear address change when GlobalCompact() shuffles
555  * moveable blocks.
556  */
557
558 LPVOID WINAPI MapSLFix( SEGPTR sptr )
559 {
560     return MapSL(sptr);
561 }
562
563 /***********************************************************************
564  *           UnMapSLFixArray   (KERNEL32.@)
565  *
566  * Must not change EAX, hence defined as asm function.
567  */
568 #ifdef __i386__
569 __ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" );
570 #endif
571
572
573 /***********************************************************************
574  *           GetThreadSelectorEntry   (KERNEL32.@)
575  */
576 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
577 {
578 #ifdef __i386__
579     BOOL ret;
580
581     if (!(sel & 4))  /* GDT selector */
582     {
583         sel &= ~3;  /* ignore RPL */
584         if (!sel)  /* null selector */
585         {
586             memset( ldtent, 0, sizeof(*ldtent) );
587             return TRUE;
588         }
589         ldtent->BaseLow                   = 0;
590         ldtent->HighWord.Bits.BaseMid     = 0;
591         ldtent->HighWord.Bits.BaseHi      = 0;
592         ldtent->LimitLow                  = 0xffff;
593         ldtent->HighWord.Bits.LimitHi     = 0xf;
594         ldtent->HighWord.Bits.Dpl         = 3;
595         ldtent->HighWord.Bits.Sys         = 0;
596         ldtent->HighWord.Bits.Pres        = 1;
597         ldtent->HighWord.Bits.Granularity = 1;
598         ldtent->HighWord.Bits.Default_Big = 1;
599         ldtent->HighWord.Bits.Type        = 0x12;
600         /* it has to be one of the system GDT selectors */
601         if (sel == (wine_get_ds() & ~3)) return TRUE;
602         if (sel == (wine_get_ss() & ~3)) return TRUE;
603         if (sel == (wine_get_cs() & ~3))
604         {
605             ldtent->HighWord.Bits.Type |= 8;  /* code segment */
606             return TRUE;
607         }
608         SetLastError( ERROR_NOACCESS );
609         return FALSE;
610     }
611
612     SERVER_START_REQ( get_selector_entry )
613     {
614         req->handle = hthread;
615         req->entry = sel >> __AHSHIFT;
616         if ((ret = !wine_server_call_err( req )))
617         {
618             if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
619             {
620                 SetLastError( ERROR_MR_MID_NOT_FOUND );  /* sic */
621                 ret = FALSE;
622             }
623             else
624             {
625                 wine_ldt_set_base( ldtent, (void *)reply->base );
626                 wine_ldt_set_limit( ldtent, reply->limit );
627                 wine_ldt_set_flags( ldtent, reply->flags );
628             }
629         }
630     }
631     SERVER_END_REQ;
632     return ret;
633 #else
634     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
635     return FALSE;
636 #endif
637 }
638
639
640 #ifdef __i386__
641
642 /***********************************************************************
643  *              SMapLS (KERNEL32.@)
644  */
645 __ASM_GLOBAL_FUNC( SMapLS,
646                    "xor %edx,%edx\n\t"
647                    "testl $0xffff0000,%eax\n\t"
648                    "jz 1f\n\t"
649                    "pushl %eax\n\t"
650                    "call " __ASM_NAME("MapLS") "\n\t"
651                    "movl %eax,%edx\n"
652                    "1:\tret" );
653
654 /***********************************************************************
655  *              SUnMapLS (KERNEL32.@)
656  */
657 __ASM_GLOBAL_FUNC( SUnMapLS,
658                    "pushl %eax\n\t"  /* preserve eax */
659                    "pushl %eax\n\t"
660                    "call " __ASM_NAME("UnMapLS") "\n\t"
661                    "popl %eax\n\t"
662                    "ret" );
663
664 /***********************************************************************
665  *              SMapLS_IP_EBP_8 (KERNEL32.@)
666  *              SMapLS_IP_EBP_12 (KERNEL32.@)
667  *              SMapLS_IP_EBP_16 (KERNEL32.@)
668  *              SMapLS_IP_EBP_20 (KERNEL32.@)
669  *              SMapLS_IP_EBP_24 (KERNEL32.@)
670  *              SMapLS_IP_EBP_28 (KERNEL32.@)
671  *              SMapLS_IP_EBP_32 (KERNEL32.@)
672  *              SMapLS_IP_EBP_36 (KERNEL32.@)
673  *              SMapLS_IP_EBP_40 (KERNEL32.@)
674  *
675  * These functions map linear pointers at [EBP+xxx] to segmented pointers
676  * and return them.
677  * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
678  * unravel them at SUnMapLS. We just store the segmented pointer there.
679  */
680 #define DEFINE_SMapLS(n) \
681   __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \
682                      "movl " #n "(%ebp),%eax\n\t" \
683                      "call " __ASM_NAME("SMapLS") "\n\t" \
684                      "movl %edx," #n "(%ebp)\n\t" \
685                      "ret" );
686
687 DEFINE_SMapLS(8);
688 DEFINE_SMapLS(12);
689 DEFINE_SMapLS(16);
690 DEFINE_SMapLS(20);
691 DEFINE_SMapLS(24);
692 DEFINE_SMapLS(28);
693 DEFINE_SMapLS(32);
694 DEFINE_SMapLS(36);
695 DEFINE_SMapLS(40);
696
697
698 /***********************************************************************
699  *              SUnMapLS_IP_EBP_8 (KERNEL32.@)
700  *              SUnMapLS_IP_EBP_12 (KERNEL32.@)
701  *              SUnMapLS_IP_EBP_16 (KERNEL32.@)
702  *              SUnMapLS_IP_EBP_20 (KERNEL32.@)
703  *              SUnMapLS_IP_EBP_24 (KERNEL32.@)
704  *              SUnMapLS_IP_EBP_28 (KERNEL32.@)
705  *              SUnMapLS_IP_EBP_32 (KERNEL32.@)
706  *              SUnMapLS_IP_EBP_36 (KERNEL32.@)
707  *              SUnMapLS_IP_EBP_40 (KERNEL32.@)
708  */
709
710 #define DEFINE_SUnMapLS(n) \
711   __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \
712                      "pushl %eax\n\t"  /* preserve eax */ \
713                      "pushl " #n "(%ebp)\n\t" \
714                      "call " __ASM_NAME("UnMapLS") "\n\t" \
715                      "movl $0," #n "(%ebp)\n\t" \
716                      "popl %eax\n\t" \
717                      "ret" )
718
719 DEFINE_SUnMapLS(8);
720 DEFINE_SUnMapLS(12);
721 DEFINE_SUnMapLS(16);
722 DEFINE_SUnMapLS(20);
723 DEFINE_SUnMapLS(24);
724 DEFINE_SUnMapLS(28);
725 DEFINE_SUnMapLS(32);
726 DEFINE_SUnMapLS(36);
727 DEFINE_SUnMapLS(40);
728
729 #endif  /* __i386__ */