2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
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.
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.
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
22 #include "wine/port.h"
26 #include "wine/winbase16.h"
27 #include "wine/server.h"
28 #include "wine/debug.h"
29 #include "kernel_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(selector);
36 /* get the number of selectors needed to cover up to the selector limit */
37 static inline WORD get_sel_count( WORD sel )
39 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
43 /***********************************************************************
44 * AllocSelectorArray (KERNEL.206)
46 WORD WINAPI AllocSelectorArray16( WORD count )
48 WORD i, sel = wine_ldt_alloc_entries( count );
53 wine_ldt_set_base( &entry, 0 );
54 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
55 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
56 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
62 /***********************************************************************
63 * AllocSelector (KERNEL.175)
65 WORD WINAPI AllocSelector16( WORD sel )
67 WORD newsel, count, i;
69 count = sel ? get_sel_count(sel) : 1;
70 newsel = wine_ldt_alloc_entries( count );
71 TRACE("(%04x): returning %04x\n", sel, newsel );
72 if (!newsel) return 0;
73 if (!sel) return newsel; /* nothing to copy */
74 for (i = 0; i < count; i++)
77 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
78 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
84 /***********************************************************************
85 * FreeSelector (KERNEL.176)
87 WORD WINAPI FreeSelector16( WORD sel )
91 wine_ldt_get_entry( sel, &entry );
92 if (wine_ldt_is_empty( &entry )) return sel; /* error */
94 /* Check if we are freeing current %fs selector */
95 if (!((wine_get_fs() ^ sel) & ~3))
96 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
98 wine_ldt_free_entries( sel, 1 );
103 /***********************************************************************
104 * SELECTOR_SetEntries
106 * Set the LDT entries for an array of selectors.
108 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
113 wine_ldt_set_base( &entry, base );
114 wine_ldt_set_limit( &entry, size - 1 );
115 wine_ldt_set_flags( &entry, flags );
116 count = (size + 0xffff) / 0x10000;
117 for (i = 0; i < count; i++)
119 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
120 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
121 /* yep, Windows sets limit like that, not 64K sel units */
122 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
127 /***********************************************************************
128 * SELECTOR_AllocBlock
130 * Allocate selectors for a block of linear memory.
132 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
137 count = (size + 0xffff) / 0x10000;
138 sel = wine_ldt_alloc_entries( count );
139 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
144 /***********************************************************************
147 * Free a block of selectors.
149 void SELECTOR_FreeBlock( WORD sel )
151 WORD i, count = get_sel_count( sel );
153 TRACE("(%04x,%d)\n", sel, count );
154 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
158 /***********************************************************************
159 * SELECTOR_ReallocBlock
161 * Change the size of a block of selectors.
163 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
166 int oldcount, newcount;
169 wine_ldt_get_entry( sel, &entry );
170 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
171 newcount = (size + 0xffff) >> 16;
173 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
174 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
179 /***********************************************************************
180 * PrestoChangoSelector (KERNEL.177)
182 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
185 wine_ldt_get_entry( selSrc, &entry );
186 /* toggle the executable bit */
187 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
188 wine_ldt_set_entry( selDst, &entry );
193 /***********************************************************************
194 * AllocCStoDSAlias (KERNEL.170)
195 * AllocAlias (KERNEL.172)
197 WORD WINAPI AllocCStoDSAlias16( WORD sel )
202 newsel = wine_ldt_alloc_entries( 1 );
203 TRACE("(%04x): returning %04x\n",
205 if (!newsel) return 0;
206 wine_ldt_get_entry( sel, &entry );
207 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
208 wine_ldt_set_entry( newsel, &entry );
213 /***********************************************************************
214 * AllocDStoCSAlias (KERNEL.171)
216 WORD WINAPI AllocDStoCSAlias16( WORD sel )
221 newsel = wine_ldt_alloc_entries( 1 );
222 TRACE("(%04x): returning %04x\n",
224 if (!newsel) return 0;
225 wine_ldt_get_entry( sel, &entry );
226 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
227 wine_ldt_set_entry( newsel, &entry );
232 /***********************************************************************
233 * LongPtrAdd (KERNEL.180)
235 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
238 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
239 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
240 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
244 /***********************************************************************
245 * GetSelectorBase (KERNEL.186)
247 DWORD WINAPI GetSelectorBase( WORD sel )
249 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
251 /* if base points into DOSMEM, assume we have to
252 * return pointer into physical lower 1MB */
254 return DOSMEM_MapLinearToDos( base );
258 /***********************************************************************
259 * SetSelectorBase (KERNEL.187)
261 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
264 wine_ldt_get_entry( sel, &entry );
265 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
266 wine_ldt_set_entry( sel, &entry );
271 /***********************************************************************
272 * GetSelectorLimit (KERNEL.188)
274 DWORD WINAPI GetSelectorLimit16( WORD sel )
276 return wine_ldt_copy.limit[sel >> __AHSHIFT];
280 /***********************************************************************
281 * SetSelectorLimit (KERNEL.189)
283 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
286 wine_ldt_get_entry( sel, &entry );
287 wine_ldt_set_limit( &entry, limit );
288 wine_ldt_set_entry( sel, &entry );
293 /***********************************************************************
294 * SelectorAccessRights (KERNEL.196)
296 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
299 wine_ldt_get_entry( sel, &entry );
301 if (op == 0) /* get */
303 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
307 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
308 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
309 wine_ldt_set_entry( sel, &entry );
315 /***********************************************************************
316 * IsBadCodePtr (KERNEL.336)
318 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
323 sel = SELECTOROF(lpfn);
324 if (!sel) return TRUE;
325 wine_ldt_get_entry( sel, &entry );
326 if (wine_ldt_is_empty( &entry )) return TRUE;
327 /* check for code segment, ignoring conforming, read-only and accessed bits */
328 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
329 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
334 /***********************************************************************
335 * IsBadStringPtr (KERNEL.337)
337 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
342 sel = SELECTOROF(ptr);
343 if (!sel) return TRUE;
344 wine_ldt_get_entry( sel, &entry );
345 if (wine_ldt_is_empty( &entry )) return TRUE;
346 /* check for data or readable code segment */
347 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
348 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
349 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
350 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
355 /***********************************************************************
356 * IsBadHugeReadPtr (KERNEL.346)
358 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
363 sel = SELECTOROF(ptr);
364 if (!sel) return TRUE;
365 wine_ldt_get_entry( sel, &entry );
366 if (wine_ldt_is_empty( &entry )) return TRUE;
367 /* check for data or readable code segment */
368 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
369 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
370 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
375 /***********************************************************************
376 * IsBadHugeWritePtr (KERNEL.347)
378 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
383 sel = SELECTOROF(ptr);
384 if (!sel) return TRUE;
385 wine_ldt_get_entry( sel, &entry );
386 if (wine_ldt_is_empty( &entry )) return TRUE;
387 /* check for writable data segment, ignoring expand-down and accessed flags */
388 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
389 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
393 /***********************************************************************
394 * IsBadReadPtr (KERNEL.334)
396 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
398 return IsBadHugeReadPtr16( ptr, size );
402 /***********************************************************************
403 * IsBadWritePtr (KERNEL.335)
405 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
407 return IsBadHugeWritePtr16( ptr, size );
411 /***********************************************************************
412 * IsBadFlatReadWritePtr (KERNEL.627)
414 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
416 return bWrite? IsBadHugeWritePtr16( ptr, size )
417 : IsBadHugeReadPtr16( ptr, size );
421 /***********************************************************************
422 * MemoryRead (TOOLHELP.78)
424 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
429 wine_ldt_get_entry( sel, &entry );
430 if (wine_ldt_is_empty( &entry )) return 0;
431 limit = wine_ldt_get_limit( &entry );
432 if (offset > limit) return 0;
433 if (offset + count > limit + 1) count = limit + 1 - offset;
434 memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
439 /***********************************************************************
440 * MemoryWrite (TOOLHELP.79)
442 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
447 wine_ldt_get_entry( sel, &entry );
448 if (wine_ldt_is_empty( &entry )) return 0;
449 limit = wine_ldt_get_limit( &entry );
450 if (offset > limit) return 0;
451 if (offset + count > limit) count = limit + 1 - offset;
452 memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
456 /************************************* Win95 pointer mapping functions *
462 struct mapls_entry *next;
463 void *addr; /* linear address */
464 int count; /* ref count */
465 WORD sel; /* selector */
468 static struct mapls_entry *first_entry;
471 /***********************************************************************
475 * Maps linear pointer to segmented.
477 SEGPTR WINAPI MapLS( LPCVOID ptr )
479 struct mapls_entry *entry, *free = NULL;
483 if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr);
485 base = (const char *)ptr - ((ULONG_PTR)ptr & 0x7fff);
486 HeapLock( GetProcessHeap() );
487 for (entry = first_entry; entry; entry = entry->next)
489 if (entry->addr == base) break;
490 if (!entry->count) free = entry;
495 if (!free) /* no free entry found, create a new one */
497 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
498 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
500 HeapFree( GetProcessHeap(), 0, free );
504 free->next = first_entry;
507 SetSelectorBase( free->sel, (DWORD)base );
508 free->addr = (void*)base;
512 ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
514 HeapUnlock( GetProcessHeap() );
518 /***********************************************************************
519 * UnMapLS (KERNEL32.@)
520 * UnMapLS (KERNEL.359)
522 * Free mapped selector.
524 void WINAPI UnMapLS( SEGPTR sptr )
526 struct mapls_entry *entry;
527 WORD sel = SELECTOROF(sptr);
531 HeapLock( GetProcessHeap() );
532 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
533 if (entry && entry->count > 0) entry->count--;
534 HeapUnlock( GetProcessHeap() );
538 /***********************************************************************
542 * Maps fixed segmented pointer to linear.
544 LPVOID WINAPI MapSL( SEGPTR sptr )
546 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
549 /***********************************************************************
550 * MapSLFix (KERNEL32.@)
552 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
553 * unexpected linear address change when GlobalCompact() shuffles
557 LPVOID WINAPI MapSLFix( SEGPTR sptr )
562 /***********************************************************************
563 * UnMapSLFixArray (KERNEL32.@)
565 * Must not change EAX, hence defined as asm function.
568 __ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" )
572 /***********************************************************************
573 * GetThreadSelectorEntry (KERNEL32.@)
575 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
577 THREAD_DESCRIPTOR_INFORMATION tdi;
581 status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry,
582 &tdi, sizeof(tdi), NULL);
585 SetLastError( RtlNtStatusToDosError(status) );
595 /***********************************************************************
596 * SMapLS (KERNEL32.@)
598 __ASM_GLOBAL_FUNC( SMapLS,
600 "testl $0xffff0000,%eax\n\t"
603 "call " __ASM_NAME("MapLS") "\n\t"
607 /***********************************************************************
608 * SUnMapLS (KERNEL32.@)
610 __ASM_GLOBAL_FUNC( SUnMapLS,
611 "pushl %eax\n\t" /* preserve eax */
613 "call " __ASM_NAME("UnMapLS") "\n\t"
617 /***********************************************************************
618 * SMapLS_IP_EBP_8 (KERNEL32.@)
619 * SMapLS_IP_EBP_12 (KERNEL32.@)
620 * SMapLS_IP_EBP_16 (KERNEL32.@)
621 * SMapLS_IP_EBP_20 (KERNEL32.@)
622 * SMapLS_IP_EBP_24 (KERNEL32.@)
623 * SMapLS_IP_EBP_28 (KERNEL32.@)
624 * SMapLS_IP_EBP_32 (KERNEL32.@)
625 * SMapLS_IP_EBP_36 (KERNEL32.@)
626 * SMapLS_IP_EBP_40 (KERNEL32.@)
628 * These functions map linear pointers at [EBP+xxx] to segmented pointers
630 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
631 * unravel them at SUnMapLS. We just store the segmented pointer there.
633 #define DEFINE_SMapLS(n) \
634 __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \
635 "movl " #n "(%ebp),%eax\n\t" \
636 "call " __ASM_NAME("SMapLS") "\n\t" \
637 "movl %edx," #n "(%ebp)\n\t" \
651 /***********************************************************************
652 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
653 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
654 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
655 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
656 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
657 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
658 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
659 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
660 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
663 #define DEFINE_SUnMapLS(n) \
664 __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \
665 "pushl %eax\n\t" /* preserve eax */ \
666 "pushl " #n "(%ebp)\n\t" \
667 "call " __ASM_NAME("UnMapLS") "\n\t" \
668 "movl $0," #n "(%ebp)\n\t" \
682 #endif /* __i386__ */