Fixed bug in system heap creation when it already exists.
[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 struct mapls_entry
522 {
523     struct mapls_entry *next;
524     void               *addr;   /* linear address */
525     int                 count;  /* ref count */
526     WORD                sel;    /* selector */
527 };
528
529 static struct mapls_entry *first_entry;
530
531
532 /***********************************************************************
533  *           MapLS   (KERNEL32.@)
534  *           MapLS   (KERNEL.358)
535  *
536  * Maps linear pointer to segmented.
537  */
538 SEGPTR WINAPI MapLS( LPCVOID ptr )
539 {
540     struct mapls_entry *entry, *free = NULL;
541     void *base;
542     SEGPTR ret = 0;
543
544     if (!HIWORD(ptr)) return (SEGPTR)ptr;
545
546     base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
547     HeapLock( GetProcessHeap() );
548     for (entry = first_entry; entry; entry = entry->next)
549     {
550         if (entry->addr == base) break;
551         if (!entry->count) free = entry;
552     }
553
554     if (!entry)
555     {
556         if (!free)  /* no free entry found, create a new one */
557         {
558             if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
559             if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
560             {
561                 HeapFree( GetProcessHeap(), 0, free );
562                 goto done;
563             }
564             free->count = 0;
565             free->next = first_entry;
566             first_entry = free;
567         }
568         SetSelectorBase( free->sel, (DWORD)base );
569         free->addr = base;
570         entry = free;
571     }
572     entry->count++;
573     ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
574  done:
575     HeapUnlock( GetProcessHeap() );
576     return ret;
577 }
578
579 /***********************************************************************
580  *           UnMapLS   (KERNEL32.@)
581  *           UnMapLS   (KERNEL.359)
582  *
583  * Free mapped selector.
584  */
585 void WINAPI UnMapLS( SEGPTR sptr )
586 {
587     struct mapls_entry *entry;
588     WORD sel = SELECTOROF(sptr);
589
590     if (sel)
591     {
592         HeapLock( GetProcessHeap() );
593         for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
594         if (entry && entry->count > 0) entry->count--;
595         HeapUnlock( GetProcessHeap() );
596     }
597 }
598
599 /***********************************************************************
600  *           MapSL   (KERNEL32.@)
601  *           MapSL   (KERNEL.357)
602  *
603  * Maps fixed segmented pointer to linear.
604  */
605 LPVOID WINAPI MapSL( SEGPTR sptr )
606 {
607     return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
608 }
609
610 /***********************************************************************
611  *           MapSLFix   (KERNEL32.@)
612  *
613  * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
614  * unexpected linear address change when GlobalCompact() shuffles
615  * moveable blocks.
616  */
617
618 LPVOID WINAPI MapSLFix( SEGPTR sptr )
619 {
620     return MapSL(sptr);
621 }
622
623 /***********************************************************************
624  *           UnMapSLFixArray   (KERNEL32.@)
625  */
626
627 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
628 {
629     /* Must not change EAX, hence defined as 'register' function */
630 }
631
632 /***********************************************************************
633  *           GetThreadSelectorEntry   (KERNEL32.@)
634  */
635 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
636 {
637 #ifdef __i386__
638     BOOL ret;
639
640     if (!(sel & 4))  /* GDT selector */
641     {
642         sel &= ~3;  /* ignore RPL */
643         if (!sel)  /* null selector */
644         {
645             memset( ldtent, 0, sizeof(*ldtent) );
646             return TRUE;
647         }
648         ldtent->BaseLow                   = 0;
649         ldtent->HighWord.Bits.BaseMid     = 0;
650         ldtent->HighWord.Bits.BaseHi      = 0;
651         ldtent->LimitLow                  = 0xffff;
652         ldtent->HighWord.Bits.LimitHi     = 0xf;
653         ldtent->HighWord.Bits.Dpl         = 3;
654         ldtent->HighWord.Bits.Sys         = 0;
655         ldtent->HighWord.Bits.Pres        = 1;
656         ldtent->HighWord.Bits.Granularity = 1;
657         ldtent->HighWord.Bits.Default_Big = 1;
658         ldtent->HighWord.Bits.Type        = 0x12;
659         /* it has to be one of the system GDT selectors */
660         if (sel == (__get_ds() & ~3)) return TRUE;
661         if (sel == (__get_ss() & ~3)) return TRUE;
662         if (sel == (__get_cs() & ~3))
663         {
664             ldtent->HighWord.Bits.Type |= 8;  /* code segment */
665             return TRUE;
666         }
667         SetLastError( ERROR_NOACCESS );
668         return FALSE;
669     }
670
671     SERVER_START_REQ( get_selector_entry )
672     {
673         req->handle = hthread;
674         req->entry = sel >> __AHSHIFT;
675         if ((ret = !wine_server_call_err( req )))
676         {
677             if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
678             {
679                 SetLastError( ERROR_MR_MID_NOT_FOUND );  /* sic */
680                 ret = FALSE;
681             }
682             else
683             {
684                 wine_ldt_set_base( ldtent, (void *)reply->base );
685                 wine_ldt_set_limit( ldtent, reply->limit );
686                 wine_ldt_set_flags( ldtent, reply->flags );
687             }
688         }
689     }
690     SERVER_END_REQ;
691     return ret;
692 #else
693     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
694     return FALSE;
695 #endif
696 }
697
698
699 /**********************************************************************
700  *              SMapLS*         (KERNEL32)
701  * These functions map linear pointers at [EBP+xxx] to segmented pointers
702  * and return them.
703  * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
704  * unravel them at SUnMapLS. We just store the segmented pointer there.
705  */
706 static void
707 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
708     DWORD       val,ptr; 
709
710     val =*(DWORD*)(context->Ebp + argoff);
711     if (val<0x10000) {
712         ptr=val;
713         *(DWORD*)(context->Ebp + argoff) = 0;
714     } else {
715         ptr = MapLS((LPVOID)val);
716         *(DWORD*)(context->Ebp + argoff) = ptr;
717     }
718     context->Eax = ptr;
719 }
720
721 /***********************************************************************
722  *              SMapLS_IP_EBP_8 (KERNEL32.@)
723  */
724 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
725
726 /***********************************************************************
727  *              SMapLS_IP_EBP_12 (KERNEL32.@)
728  */
729 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
730
731 /***********************************************************************
732  *              SMapLS_IP_EBP_16 (KERNEL32.@)
733  */
734 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
735
736 /***********************************************************************
737  *              SMapLS_IP_EBP_20 (KERNEL32.@)
738  */
739 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
740
741 /***********************************************************************
742  *              SMapLS_IP_EBP_24 (KERNEL32.@)
743  */
744 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
745
746 /***********************************************************************
747  *              SMapLS_IP_EBP_28 (KERNEL32.@)
748  */
749 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
750
751 /***********************************************************************
752  *              SMapLS_IP_EBP_32 (KERNEL32.@)
753  */
754 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
755
756 /***********************************************************************
757  *              SMapLS_IP_EBP_36 (KERNEL32.@)
758  */
759 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
760
761 /***********************************************************************
762  *              SMapLS_IP_EBP_40 (KERNEL32.@)
763  */
764 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
765
766 /***********************************************************************
767  *              SMapLS (KERNEL32.@)
768  */
769 void WINAPI SMapLS( CONTEXT86 *context )
770 {
771     if (HIWORD(context->Eax))
772     {
773         context->Eax = MapLS( (LPVOID)context->Eax );
774         context->Edx = context->Eax;
775     } else {
776         context->Edx = 0;
777     }
778 }
779
780 /***********************************************************************
781  *              SUnMapLS (KERNEL32.@)
782  */
783
784 void WINAPI SUnMapLS( CONTEXT86 *context )
785 {
786     if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
787 }
788
789 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
790 {
791     SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
792     if (*ptr)
793     {
794         UnMapLS( *ptr );
795         *ptr = 0;
796     }
797 }
798
799 /***********************************************************************
800  *              SUnMapLS_IP_EBP_8 (KERNEL32.@)
801  */
802 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
803
804 /***********************************************************************
805  *              SUnMapLS_IP_EBP_12 (KERNEL32.@)
806  */
807 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
808
809 /***********************************************************************
810  *              SUnMapLS_IP_EBP_16 (KERNEL32.@)
811  */
812 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
813
814 /***********************************************************************
815  *              SUnMapLS_IP_EBP_20 (KERNEL32.@)
816  */
817 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
818
819 /***********************************************************************
820  *              SUnMapLS_IP_EBP_24 (KERNEL32.@)
821  */
822 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
823
824 /***********************************************************************
825  *              SUnMapLS_IP_EBP_28 (KERNEL32.@)
826  */
827 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
828
829 /***********************************************************************
830  *              SUnMapLS_IP_EBP_32 (KERNEL32.@)
831  */
832 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
833
834 /***********************************************************************
835  *              SUnMapLS_IP_EBP_36 (KERNEL32.@)
836  */
837 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
838
839 /***********************************************************************
840  *              SUnMapLS_IP_EBP_40 (KERNEL32.@)
841  */
842 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
843
844 /**********************************************************************
845  *              AllocMappedBuffer       (KERNEL32.38)
846  *
847  * This is a undocumented KERNEL32 function that 
848  * SMapLS's a GlobalAlloc'ed buffer.
849  *
850  * Input:   EDI register: size of buffer to allocate
851  * Output:  EDI register: pointer to buffer
852  *
853  * Note: The buffer is preceded by 8 bytes:
854  *        ...
855  *       edi+0   buffer
856  *       edi-4   SEGPTR to buffer
857  *       edi-8   some magic Win95 needs for SUnMapLS
858  *               (we use it for the memory handle)
859  *
860  *       The SEGPTR is used by the caller!
861  */
862
863 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
864 {
865     HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
866     DWORD *buffer = (DWORD *)GlobalLock(handle);
867     SEGPTR ptr = 0;
868
869     if (buffer)
870         if (!(ptr = MapLS(buffer + 2)))
871         {
872             GlobalUnlock(handle);
873             GlobalFree(handle);
874         }
875
876     if (!ptr)
877         context->Eax = context->Edi = 0;
878     else
879     {
880         buffer[0] = handle;
881         buffer[1] = ptr;
882
883         context->Eax = (DWORD) ptr;
884         context->Edi = (DWORD)(buffer + 2);
885     }
886 }
887
888 /**********************************************************************
889  *              FreeMappedBuffer        (KERNEL32.39)
890  *
891  * Free a buffer allocated by AllocMappedBuffer
892  *
893  * Input: EDI register: pointer to buffer
894  */
895
896 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
897 {
898     if (context->Edi)
899     {
900         DWORD *buffer = (DWORD *)context->Edi - 2;
901
902         UnMapLS(buffer[1]);
903
904         GlobalUnlock(buffer[0]);
905         GlobalFree(buffer[0]);
906     }
907 }
908
909 #ifdef __i386__
910 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
911 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
912 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
913 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
914 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
915 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
916 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
917 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
918 #endif