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