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"
29 #include "selectors.h"
30 #include "wine/server.h"
31 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(selector);
38 /* get the number of selectors needed to cover up to the selector limit */
39 inline static WORD get_sel_count( WORD sel )
41 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
45 /***********************************************************************
46 * AllocSelectorArray (KERNEL.206)
48 WORD WINAPI AllocSelectorArray16( WORD count )
50 WORD i, sel = wine_ldt_alloc_entries( count );
55 wine_ldt_set_base( &entry, 0 );
56 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
57 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
58 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
64 /***********************************************************************
65 * AllocSelector (KERNEL.175)
67 WORD WINAPI AllocSelector16( WORD sel )
69 WORD newsel, count, i;
71 count = sel ? get_sel_count(sel) : 1;
72 newsel = wine_ldt_alloc_entries( count );
73 TRACE("(%04x): returning %04x\n", sel, newsel );
74 if (!newsel) return 0;
75 if (!sel) return newsel; /* nothing to copy */
76 for (i = 0; i < count; i++)
79 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
80 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
86 /***********************************************************************
87 * FreeSelector (KERNEL.176)
89 WORD WINAPI FreeSelector16( WORD sel )
93 wine_ldt_get_entry( sel, &entry );
94 if (wine_ldt_is_empty( &entry )) return sel; /* error */
96 /* Check if we are freeing current %fs selector */
97 if (!((wine_get_fs() ^ sel) & ~3))
98 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
100 wine_ldt_free_entries( sel, 1 );
105 /***********************************************************************
106 * SELECTOR_SetEntries
108 * Set the LDT entries for an array of selectors.
110 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
115 wine_ldt_set_base( &entry, base );
116 wine_ldt_set_limit( &entry, size - 1 );
117 wine_ldt_set_flags( &entry, flags );
118 count = (size + 0xffff) / 0x10000;
119 for (i = 0; i < count; i++)
121 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
122 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
123 /* yep, Windows sets limit like that, not 64K sel units */
124 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
129 /***********************************************************************
130 * SELECTOR_AllocBlock
132 * Allocate selectors for a block of linear memory.
134 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
139 count = (size + 0xffff) / 0x10000;
140 sel = wine_ldt_alloc_entries( count );
141 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
146 /***********************************************************************
149 * Free a block of selectors.
151 void SELECTOR_FreeBlock( WORD sel )
153 WORD i, count = get_sel_count( sel );
155 TRACE("(%04x,%d)\n", sel, count );
156 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
160 /***********************************************************************
161 * SELECTOR_ReallocBlock
163 * Change the size of a block of selectors.
165 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
168 int oldcount, newcount;
171 wine_ldt_get_entry( sel, &entry );
172 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
173 newcount = (size + 0xffff) >> 16;
175 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
176 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
181 /***********************************************************************
182 * PrestoChangoSelector (KERNEL.177)
184 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
187 wine_ldt_get_entry( selSrc, &entry );
188 /* toggle the executable bit */
189 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
190 wine_ldt_set_entry( selDst, &entry );
195 /***********************************************************************
196 * AllocCStoDSAlias (KERNEL.170)
197 * AllocAlias (KERNEL.172)
199 WORD WINAPI AllocCStoDSAlias16( WORD sel )
204 newsel = wine_ldt_alloc_entries( 1 );
205 TRACE("(%04x): returning %04x\n",
207 if (!newsel) return 0;
208 wine_ldt_get_entry( sel, &entry );
209 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
210 wine_ldt_set_entry( newsel, &entry );
215 /***********************************************************************
216 * AllocDStoCSAlias (KERNEL.171)
218 WORD WINAPI AllocDStoCSAlias16( WORD sel )
223 newsel = wine_ldt_alloc_entries( 1 );
224 TRACE("(%04x): returning %04x\n",
226 if (!newsel) return 0;
227 wine_ldt_get_entry( sel, &entry );
228 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
229 wine_ldt_set_entry( newsel, &entry );
234 /***********************************************************************
235 * LongPtrAdd (KERNEL.180)
237 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
240 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
241 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
242 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
246 /***********************************************************************
247 * GetSelectorBase (KERNEL.186)
249 DWORD WINAPI GetSelectorBase( WORD sel )
251 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
253 /* if base points into DOSMEM, assume we have to
254 * return pointer into physical lower 1MB */
256 return DOSMEM_MapLinearToDos( base );
260 /***********************************************************************
261 * SetSelectorBase (KERNEL.187)
263 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
266 wine_ldt_get_entry( sel, &entry );
267 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
268 wine_ldt_set_entry( sel, &entry );
273 /***********************************************************************
274 * GetSelectorLimit (KERNEL.188)
276 DWORD WINAPI GetSelectorLimit16( WORD sel )
278 return wine_ldt_copy.limit[sel >> __AHSHIFT];
282 /***********************************************************************
283 * SetSelectorLimit (KERNEL.189)
285 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
288 wine_ldt_get_entry( sel, &entry );
289 wine_ldt_set_limit( &entry, limit );
290 wine_ldt_set_entry( sel, &entry );
295 /***********************************************************************
296 * SelectorAccessRights (KERNEL.196)
298 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
301 wine_ldt_get_entry( sel, &entry );
303 if (op == 0) /* get */
305 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
309 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
310 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
311 wine_ldt_set_entry( sel, &entry );
317 /***********************************************************************
318 * IsBadCodePtr (KERNEL.336)
320 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
325 sel = SELECTOROF(lpfn);
326 if (!sel) return TRUE;
327 wine_ldt_get_entry( sel, &entry );
328 if (wine_ldt_is_empty( &entry )) return TRUE;
329 /* check for code segment, ignoring conforming, read-only and accessed bits */
330 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
331 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
336 /***********************************************************************
337 * IsBadStringPtr (KERNEL.337)
339 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
344 sel = SELECTOROF(ptr);
345 if (!sel) return TRUE;
346 wine_ldt_get_entry( sel, &entry );
347 if (wine_ldt_is_empty( &entry )) return TRUE;
348 /* check for data or readable code segment */
349 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
350 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
351 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
352 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
357 /***********************************************************************
358 * IsBadHugeReadPtr (KERNEL.346)
360 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
365 sel = SELECTOROF(ptr);
366 if (!sel) return TRUE;
367 wine_ldt_get_entry( sel, &entry );
368 if (wine_ldt_is_empty( &entry )) return TRUE;
369 /* check for data or readable code segment */
370 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
371 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
372 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
377 /***********************************************************************
378 * IsBadHugeWritePtr (KERNEL.347)
380 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
385 sel = SELECTOROF(ptr);
386 if (!sel) return TRUE;
387 wine_ldt_get_entry( sel, &entry );
388 if (wine_ldt_is_empty( &entry )) return TRUE;
389 /* check for writeable data segment, ignoring expand-down and accessed flags */
390 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
391 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
395 /***********************************************************************
396 * IsBadReadPtr (KERNEL.334)
398 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
400 return IsBadHugeReadPtr16( ptr, size );
404 /***********************************************************************
405 * IsBadWritePtr (KERNEL.335)
407 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
409 return IsBadHugeWritePtr16( ptr, size );
413 /***********************************************************************
414 * IsBadFlatReadWritePtr (KERNEL.627)
416 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
418 return bWrite? IsBadHugeWritePtr16( ptr, size )
419 : IsBadHugeReadPtr16( ptr, size );
423 /***********************************************************************
424 * MemoryRead (TOOLHELP.78)
426 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
431 wine_ldt_get_entry( sel, &entry );
432 if (wine_ldt_is_empty( &entry )) return 0;
433 limit = wine_ldt_get_limit( &entry );
434 if (offset > limit) return 0;
435 if (offset + count > limit + 1) count = limit + 1 - offset;
436 memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
441 /***********************************************************************
442 * MemoryWrite (TOOLHELP.79)
444 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
449 wine_ldt_get_entry( sel, &entry );
450 if (wine_ldt_is_empty( &entry )) return 0;
451 limit = wine_ldt_get_limit( &entry );
452 if (offset > limit) return 0;
453 if (offset + count > limit) count = limit + 1 - offset;
454 memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
458 /************************************* Win95 pointer mapping functions *
464 struct mapls_entry *next;
465 void *addr; /* linear address */
466 int count; /* ref count */
467 WORD sel; /* selector */
470 static struct mapls_entry *first_entry;
473 /***********************************************************************
477 * Maps linear pointer to segmented.
479 SEGPTR WINAPI MapLS( LPCVOID ptr )
481 struct mapls_entry *entry, *free = NULL;
485 if (!HIWORD(ptr)) return (SEGPTR)ptr;
487 base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
488 HeapLock( GetProcessHeap() );
489 for (entry = first_entry; entry; entry = entry->next)
491 if (entry->addr == base) break;
492 if (!entry->count) free = entry;
497 if (!free) /* no free entry found, create a new one */
499 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
500 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
502 HeapFree( GetProcessHeap(), 0, free );
506 free->next = first_entry;
509 SetSelectorBase( free->sel, (DWORD)base );
514 ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
516 HeapUnlock( GetProcessHeap() );
520 /***********************************************************************
521 * UnMapLS (KERNEL32.@)
522 * UnMapLS (KERNEL.359)
524 * Free mapped selector.
526 void WINAPI UnMapLS( SEGPTR sptr )
528 struct mapls_entry *entry;
529 WORD sel = SELECTOROF(sptr);
533 HeapLock( GetProcessHeap() );
534 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
535 if (entry && entry->count > 0) entry->count--;
536 HeapUnlock( GetProcessHeap() );
540 /***********************************************************************
544 * Maps fixed segmented pointer to linear.
546 LPVOID WINAPI MapSL( SEGPTR sptr )
548 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
551 /***********************************************************************
552 * MapSLFix (KERNEL32.@)
554 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
555 * unexpected linear address change when GlobalCompact() shuffles
559 LPVOID WINAPI MapSLFix( SEGPTR sptr )
564 /***********************************************************************
565 * UnMapSLFixArray (KERNEL32.@)
568 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
570 /* Must not change EAX, hence defined as 'register' function */
573 /***********************************************************************
574 * GetThreadSelectorEntry (KERNEL32.@)
576 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
581 if (!(sel & 4)) /* GDT selector */
583 sel &= ~3; /* ignore RPL */
584 if (!sel) /* null selector */
586 memset( ldtent, 0, sizeof(*ldtent) );
590 ldtent->HighWord.Bits.BaseMid = 0;
591 ldtent->HighWord.Bits.BaseHi = 0;
592 ldtent->LimitLow = 0xffff;
593 ldtent->HighWord.Bits.LimitHi = 0xf;
594 ldtent->HighWord.Bits.Dpl = 3;
595 ldtent->HighWord.Bits.Sys = 0;
596 ldtent->HighWord.Bits.Pres = 1;
597 ldtent->HighWord.Bits.Granularity = 1;
598 ldtent->HighWord.Bits.Default_Big = 1;
599 ldtent->HighWord.Bits.Type = 0x12;
600 /* it has to be one of the system GDT selectors */
601 if (sel == (wine_get_ds() & ~3)) return TRUE;
602 if (sel == (wine_get_ss() & ~3)) return TRUE;
603 if (sel == (wine_get_cs() & ~3))
605 ldtent->HighWord.Bits.Type |= 8; /* code segment */
608 SetLastError( ERROR_NOACCESS );
612 SERVER_START_REQ( get_selector_entry )
614 req->handle = hthread;
615 req->entry = sel >> __AHSHIFT;
616 if ((ret = !wine_server_call_err( req )))
618 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
620 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
625 wine_ldt_set_base( ldtent, (void *)reply->base );
626 wine_ldt_set_limit( ldtent, reply->limit );
627 wine_ldt_set_flags( ldtent, reply->flags );
634 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
640 /**********************************************************************
642 * These functions map linear pointers at [EBP+xxx] to segmented pointers
644 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
645 * unravel them at SUnMapLS. We just store the segmented pointer there.
648 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
651 val =*(DWORD*)(context->Ebp + argoff);
654 *(DWORD*)(context->Ebp + argoff) = 0;
656 ptr = MapLS((LPVOID)val);
657 *(DWORD*)(context->Ebp + argoff) = ptr;
662 /***********************************************************************
663 * SMapLS_IP_EBP_8 (KERNEL32.@)
665 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
667 /***********************************************************************
668 * SMapLS_IP_EBP_12 (KERNEL32.@)
670 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
672 /***********************************************************************
673 * SMapLS_IP_EBP_16 (KERNEL32.@)
675 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
677 /***********************************************************************
678 * SMapLS_IP_EBP_20 (KERNEL32.@)
680 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
682 /***********************************************************************
683 * SMapLS_IP_EBP_24 (KERNEL32.@)
685 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
687 /***********************************************************************
688 * SMapLS_IP_EBP_28 (KERNEL32.@)
690 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
692 /***********************************************************************
693 * SMapLS_IP_EBP_32 (KERNEL32.@)
695 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
697 /***********************************************************************
698 * SMapLS_IP_EBP_36 (KERNEL32.@)
700 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
702 /***********************************************************************
703 * SMapLS_IP_EBP_40 (KERNEL32.@)
705 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
707 /***********************************************************************
708 * SMapLS (KERNEL32.@)
710 void WINAPI SMapLS( CONTEXT86 *context )
712 if (HIWORD(context->Eax))
714 context->Eax = MapLS( (LPVOID)context->Eax );
715 context->Edx = context->Eax;
721 /***********************************************************************
722 * SUnMapLS (KERNEL32.@)
725 void WINAPI SUnMapLS( CONTEXT86 *context )
727 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
730 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
732 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
740 /***********************************************************************
741 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
743 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
745 /***********************************************************************
746 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
748 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
750 /***********************************************************************
751 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
753 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
755 /***********************************************************************
756 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
758 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
760 /***********************************************************************
761 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
763 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
765 /***********************************************************************
766 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
768 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
770 /***********************************************************************
771 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
773 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
775 /***********************************************************************
776 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
778 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
780 /***********************************************************************
781 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
783 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }