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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 #include "wine/winbase16.h"
28 #include "wine/server.h"
29 #include "wine/debug.h"
30 #include "kernel_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(selector);
37 /* get the number of selectors needed to cover up to the selector limit */
38 inline static WORD get_sel_count( WORD sel )
40 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
44 /***********************************************************************
45 * AllocSelectorArray (KERNEL.206)
47 WORD WINAPI AllocSelectorArray16( WORD count )
49 WORD i, sel = wine_ldt_alloc_entries( count );
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 );
63 /***********************************************************************
64 * AllocSelector (KERNEL.175)
66 WORD WINAPI AllocSelector16( WORD sel )
68 WORD newsel, count, i;
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++)
78 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
79 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
85 /***********************************************************************
86 * FreeSelector (KERNEL.176)
88 WORD WINAPI FreeSelector16( WORD sel )
92 wine_ldt_get_entry( sel, &entry );
93 if (wine_ldt_is_empty( &entry )) return sel; /* error */
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() );
99 wine_ldt_free_entries( sel, 1 );
104 /***********************************************************************
105 * SELECTOR_SetEntries
107 * Set the LDT entries for an array of selectors.
109 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
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++)
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 );
128 /***********************************************************************
129 * SELECTOR_AllocBlock
131 * Allocate selectors for a block of linear memory.
133 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
138 count = (size + 0xffff) / 0x10000;
139 sel = wine_ldt_alloc_entries( count );
140 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
145 /***********************************************************************
148 * Free a block of selectors.
150 void SELECTOR_FreeBlock( WORD sel )
152 WORD i, count = get_sel_count( sel );
154 TRACE("(%04x,%d)\n", sel, count );
155 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
159 /***********************************************************************
160 * SELECTOR_ReallocBlock
162 * Change the size of a block of selectors.
164 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
167 int oldcount, newcount;
170 wine_ldt_get_entry( sel, &entry );
171 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
172 newcount = (size + 0xffff) >> 16;
174 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
175 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
180 /***********************************************************************
181 * PrestoChangoSelector (KERNEL.177)
183 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
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 );
194 /***********************************************************************
195 * AllocCStoDSAlias (KERNEL.170)
196 * AllocAlias (KERNEL.172)
198 WORD WINAPI AllocCStoDSAlias16( WORD sel )
203 newsel = wine_ldt_alloc_entries( 1 );
204 TRACE("(%04x): returning %04x\n",
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 );
214 /***********************************************************************
215 * AllocDStoCSAlias (KERNEL.171)
217 WORD WINAPI AllocDStoCSAlias16( WORD sel )
222 newsel = wine_ldt_alloc_entries( 1 );
223 TRACE("(%04x): returning %04x\n",
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 );
233 /***********************************************************************
234 * LongPtrAdd (KERNEL.180)
236 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
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 );
245 /***********************************************************************
246 * GetSelectorBase (KERNEL.186)
248 DWORD WINAPI GetSelectorBase( WORD sel )
250 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
252 /* if base points into DOSMEM, assume we have to
253 * return pointer into physical lower 1MB */
255 return DOSMEM_MapLinearToDos( base );
259 /***********************************************************************
260 * SetSelectorBase (KERNEL.187)
262 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
265 wine_ldt_get_entry( sel, &entry );
266 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
267 wine_ldt_set_entry( sel, &entry );
272 /***********************************************************************
273 * GetSelectorLimit (KERNEL.188)
275 DWORD WINAPI GetSelectorLimit16( WORD sel )
277 return wine_ldt_copy.limit[sel >> __AHSHIFT];
281 /***********************************************************************
282 * SetSelectorLimit (KERNEL.189)
284 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
287 wine_ldt_get_entry( sel, &entry );
288 wine_ldt_set_limit( &entry, limit );
289 wine_ldt_set_entry( sel, &entry );
294 /***********************************************************************
295 * SelectorAccessRights (KERNEL.196)
297 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
300 wine_ldt_get_entry( sel, &entry );
302 if (op == 0) /* get */
304 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
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 );
316 /***********************************************************************
317 * IsBadCodePtr (KERNEL.336)
319 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
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;
335 /***********************************************************************
336 * IsBadStringPtr (KERNEL.337)
338 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
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;
356 /***********************************************************************
357 * IsBadHugeReadPtr (KERNEL.346)
359 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
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;
376 /***********************************************************************
377 * IsBadHugeWritePtr (KERNEL.347)
379 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
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;
394 /***********************************************************************
395 * IsBadReadPtr (KERNEL.334)
397 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
399 return IsBadHugeReadPtr16( ptr, size );
403 /***********************************************************************
404 * IsBadWritePtr (KERNEL.335)
406 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
408 return IsBadHugeWritePtr16( ptr, size );
412 /***********************************************************************
413 * IsBadFlatReadWritePtr (KERNEL.627)
415 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
417 return bWrite? IsBadHugeWritePtr16( ptr, size )
418 : IsBadHugeReadPtr16( ptr, size );
422 /***********************************************************************
423 * MemoryRead (TOOLHELP.78)
425 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
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 );
440 /***********************************************************************
441 * MemoryWrite (TOOLHELP.79)
443 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
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 );
457 /************************************* Win95 pointer mapping functions *
463 struct mapls_entry *next;
464 void *addr; /* linear address */
465 int count; /* ref count */
466 WORD sel; /* selector */
469 static struct mapls_entry *first_entry;
472 /***********************************************************************
476 * Maps linear pointer to segmented.
478 SEGPTR WINAPI MapLS( LPCVOID ptr )
480 struct mapls_entry *entry, *free = NULL;
484 if (!HIWORD(ptr)) return (SEGPTR)ptr;
486 base = (const char *)ptr - ((unsigned int)ptr & 0x7fff);
487 HeapLock( GetProcessHeap() );
488 for (entry = first_entry; entry; entry = entry->next)
490 if (entry->addr == base) break;
491 if (!entry->count) free = entry;
496 if (!free) /* no free entry found, create a new one */
498 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
499 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
501 HeapFree( GetProcessHeap(), 0, free );
505 free->next = first_entry;
508 SetSelectorBase( free->sel, (DWORD)base );
509 free->addr = (void*)base;
513 ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
515 HeapUnlock( GetProcessHeap() );
519 /***********************************************************************
520 * UnMapLS (KERNEL32.@)
521 * UnMapLS (KERNEL.359)
523 * Free mapped selector.
525 void WINAPI UnMapLS( SEGPTR sptr )
527 struct mapls_entry *entry;
528 WORD sel = SELECTOROF(sptr);
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() );
539 /***********************************************************************
543 * Maps fixed segmented pointer to linear.
545 LPVOID WINAPI MapSL( SEGPTR sptr )
547 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
550 /***********************************************************************
551 * MapSLFix (KERNEL32.@)
553 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
554 * unexpected linear address change when GlobalCompact() shuffles
558 LPVOID WINAPI MapSLFix( SEGPTR sptr )
563 /***********************************************************************
564 * UnMapSLFixArray (KERNEL32.@)
566 void WINAPI __regs_UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
568 /* Must not change EAX, hence defined as 'register' function */
570 #ifdef DEFINE_REGS_ENTRYPOINT
571 DEFINE_REGS_ENTRYPOINT( UnMapSLFixArray, 8, 8 );
574 /***********************************************************************
575 * GetThreadSelectorEntry (KERNEL32.@)
577 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
582 if (!(sel & 4)) /* GDT selector */
584 sel &= ~3; /* ignore RPL */
585 if (!sel) /* null selector */
587 memset( ldtent, 0, sizeof(*ldtent) );
591 ldtent->HighWord.Bits.BaseMid = 0;
592 ldtent->HighWord.Bits.BaseHi = 0;
593 ldtent->LimitLow = 0xffff;
594 ldtent->HighWord.Bits.LimitHi = 0xf;
595 ldtent->HighWord.Bits.Dpl = 3;
596 ldtent->HighWord.Bits.Sys = 0;
597 ldtent->HighWord.Bits.Pres = 1;
598 ldtent->HighWord.Bits.Granularity = 1;
599 ldtent->HighWord.Bits.Default_Big = 1;
600 ldtent->HighWord.Bits.Type = 0x12;
601 /* it has to be one of the system GDT selectors */
602 if (sel == (wine_get_ds() & ~3)) return TRUE;
603 if (sel == (wine_get_ss() & ~3)) return TRUE;
604 if (sel == (wine_get_cs() & ~3))
606 ldtent->HighWord.Bits.Type |= 8; /* code segment */
609 SetLastError( ERROR_NOACCESS );
613 SERVER_START_REQ( get_selector_entry )
615 req->handle = hthread;
616 req->entry = sel >> __AHSHIFT;
617 if ((ret = !wine_server_call_err( req )))
619 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
621 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
626 wine_ldt_set_base( ldtent, (void *)reply->base );
627 wine_ldt_set_limit( ldtent, reply->limit );
628 wine_ldt_set_flags( ldtent, reply->flags );
635 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
641 #ifdef DEFINE_REGS_ENTRYPOINT
643 /***********************************************************************
644 * SMapLS (KERNEL32.@)
646 void WINAPI __regs_SMapLS( CONTEXT86 *context )
648 if (HIWORD(context->Eax))
650 context->Eax = MapLS( (LPVOID)context->Eax );
651 context->Edx = context->Eax;
656 DEFINE_REGS_ENTRYPOINT( SMapLS, 0, 0 );
658 /***********************************************************************
659 * SUnMapLS (KERNEL32.@)
661 void WINAPI __regs_SUnMapLS( CONTEXT86 *context )
663 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
665 DEFINE_REGS_ENTRYPOINT( SUnMapLS, 0, 0 );
668 /***********************************************************************
669 * SMapLS_IP_EBP_8 (KERNEL32.@)
670 * SMapLS_IP_EBP_12 (KERNEL32.@)
671 * SMapLS_IP_EBP_16 (KERNEL32.@)
672 * SMapLS_IP_EBP_20 (KERNEL32.@)
673 * SMapLS_IP_EBP_24 (KERNEL32.@)
674 * SMapLS_IP_EBP_28 (KERNEL32.@)
675 * SMapLS_IP_EBP_32 (KERNEL32.@)
676 * SMapLS_IP_EBP_36 (KERNEL32.@)
677 * SMapLS_IP_EBP_40 (KERNEL32.@)
679 * These functions map linear pointers at [EBP+xxx] to segmented pointers
681 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
682 * unravel them at SUnMapLS. We just store the segmented pointer there.
684 #define DEFINE_SMapLS(n) \
685 void WINAPI __regs_SMapLS_IP_EBP_ ## n (CONTEXT86 *context) \
687 SEGPTR *ptr = (SEGPTR *)(context->Ebp + n); \
690 context->Eax = *ptr; \
693 else *ptr = context->Eax = MapLS((LPVOID)*ptr); \
695 DEFINE_REGS_ENTRYPOINT( SMapLS_IP_EBP_ ## n, 0, 0 )
708 /***********************************************************************
709 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
710 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
711 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
712 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
713 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
714 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
715 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
716 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
717 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
720 #define DEFINE_SUnMapLS(n) \
721 void WINAPI __regs_SUnMapLS_IP_EBP_ ## n (CONTEXT86 *context) \
723 SEGPTR *ptr = (SEGPTR *)(context->Ebp + n); \
727 DEFINE_REGS_ENTRYPOINT( SUnMapLS_IP_EBP_ ## n, 0, 0 )
739 #endif /* DEFINE_REGS_ENTRYPOINT */