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"
28 #include "wine/debug.h"
29 #include "kernel_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(selector
);
35 /* get the number of selectors needed to cover up to the selector limit */
36 static inline WORD
get_sel_count( WORD sel
)
38 return (wine_ldt_copy
.limit
[sel
>> __AHSHIFT
] >> 16) + 1;
42 /***********************************************************************
43 * AllocSelectorArray (KERNEL.206)
45 WORD WINAPI
AllocSelectorArray16( WORD count
)
47 WORD i
, sel
= wine_ldt_alloc_entries( count
);
52 wine_ldt_set_base( &entry
, 0 );
53 wine_ldt_set_limit( &entry
, 1 ); /* avoid 0 base and limit */
54 wine_ldt_set_flags( &entry
, WINE_LDT_FLAGS_DATA
);
55 for (i
= 0; i
< count
; i
++) wine_ldt_set_entry( sel
+ (i
<< __AHSHIFT
), &entry
);
61 /***********************************************************************
62 * AllocSelector (KERNEL.175)
64 WORD WINAPI
AllocSelector16( WORD sel
)
66 WORD newsel
, count
, i
;
68 count
= sel
? get_sel_count(sel
) : 1;
69 newsel
= wine_ldt_alloc_entries( count
);
70 TRACE("(%04x): returning %04x\n", sel
, newsel
);
71 if (!newsel
) return 0;
72 if (!sel
) return newsel
; /* nothing to copy */
73 for (i
= 0; i
< count
; i
++)
76 wine_ldt_get_entry( sel
+ (i
<< __AHSHIFT
), &entry
);
77 wine_ldt_set_entry( newsel
+ (i
<< __AHSHIFT
), &entry
);
83 /***********************************************************************
84 * FreeSelector (KERNEL.176)
86 WORD WINAPI
FreeSelector16( WORD sel
)
90 wine_ldt_get_entry( sel
, &entry
);
91 if (wine_ldt_is_empty( &entry
)) return sel
; /* error */
93 /* Check if we are freeing current %fs selector */
94 if (!((wine_get_fs() ^ sel
) & ~3))
95 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
97 wine_ldt_free_entries( sel
, 1 );
102 /***********************************************************************
103 * SELECTOR_SetEntries
105 * Set the LDT entries for an array of selectors.
107 static void SELECTOR_SetEntries( WORD sel
, const void *base
, DWORD size
, unsigned char flags
)
112 wine_ldt_set_base( &entry
, base
);
113 wine_ldt_set_limit( &entry
, size
- 1 );
114 wine_ldt_set_flags( &entry
, flags
);
115 count
= (size
+ 0xffff) / 0x10000;
116 for (i
= 0; i
< count
; i
++)
118 wine_ldt_set_entry( sel
+ (i
<< __AHSHIFT
), &entry
);
119 wine_ldt_set_base( &entry
, (char*)wine_ldt_get_base(&entry
) + 0x10000);
120 /* yep, Windows sets limit like that, not 64K sel units */
121 wine_ldt_set_limit( &entry
, wine_ldt_get_limit(&entry
) - 0x10000 );
126 /***********************************************************************
127 * SELECTOR_AllocBlock
129 * Allocate selectors for a block of linear memory.
131 WORD
SELECTOR_AllocBlock( const void *base
, DWORD size
, unsigned char flags
)
136 count
= (size
+ 0xffff) / 0x10000;
137 sel
= wine_ldt_alloc_entries( count
);
138 if (sel
) SELECTOR_SetEntries( sel
, base
, size
, flags
);
143 /***********************************************************************
146 * Free a block of selectors.
148 void SELECTOR_FreeBlock( WORD sel
)
150 WORD i
, count
= get_sel_count( sel
);
152 TRACE("(%04x,%d)\n", sel
, count
);
153 for (i
= 0; i
< count
; i
++) FreeSelector16( sel
+ (i
<< __AHSHIFT
) );
157 /***********************************************************************
158 * SELECTOR_ReallocBlock
160 * Change the size of a block of selectors.
162 WORD
SELECTOR_ReallocBlock( WORD sel
, const void *base
, DWORD size
)
165 int oldcount
, newcount
;
168 wine_ldt_get_entry( sel
, &entry
);
169 oldcount
= (wine_ldt_get_limit(&entry
) >> 16) + 1;
170 newcount
= (size
+ 0xffff) >> 16;
172 sel
= wine_ldt_realloc_entries( sel
, oldcount
, newcount
);
173 if (sel
) SELECTOR_SetEntries( sel
, base
, size
, wine_ldt_get_flags(&entry
) );
178 /***********************************************************************
179 * PrestoChangoSelector (KERNEL.177)
181 WORD WINAPI
PrestoChangoSelector16( WORD selSrc
, WORD selDst
)
184 wine_ldt_get_entry( selSrc
, &entry
);
185 /* toggle the executable bit */
186 entry
.HighWord
.Bits
.Type
^= (WINE_LDT_FLAGS_CODE
^ WINE_LDT_FLAGS_DATA
);
187 wine_ldt_set_entry( selDst
, &entry
);
192 /***********************************************************************
193 * AllocCStoDSAlias (KERNEL.170)
194 * AllocAlias (KERNEL.172)
196 WORD WINAPI
AllocCStoDSAlias16( WORD sel
)
201 newsel
= wine_ldt_alloc_entries( 1 );
202 TRACE("(%04x): returning %04x\n",
204 if (!newsel
) return 0;
205 wine_ldt_get_entry( sel
, &entry
);
206 entry
.HighWord
.Bits
.Type
= WINE_LDT_FLAGS_DATA
;
207 wine_ldt_set_entry( newsel
, &entry
);
212 /***********************************************************************
213 * AllocDStoCSAlias (KERNEL.171)
215 WORD WINAPI
AllocDStoCSAlias16( WORD sel
)
220 newsel
= wine_ldt_alloc_entries( 1 );
221 TRACE("(%04x): returning %04x\n",
223 if (!newsel
) return 0;
224 wine_ldt_get_entry( sel
, &entry
);
225 entry
.HighWord
.Bits
.Type
= WINE_LDT_FLAGS_CODE
;
226 wine_ldt_set_entry( newsel
, &entry
);
231 /***********************************************************************
232 * LongPtrAdd (KERNEL.180)
234 void WINAPI
LongPtrAdd16( DWORD ptr
, DWORD add
)
237 wine_ldt_get_entry( SELECTOROF(ptr
), &entry
);
238 wine_ldt_set_base( &entry
, (char *)wine_ldt_get_base(&entry
) + add
);
239 wine_ldt_set_entry( SELECTOROF(ptr
), &entry
);
243 /***********************************************************************
244 * GetSelectorBase (KERNEL.186)
246 DWORD WINAPI
GetSelectorBase( WORD sel
)
248 void *base
= wine_ldt_copy
.base
[sel
>> __AHSHIFT
];
250 /* if base points into DOSMEM, assume we have to
251 * return pointer into physical lower 1MB */
253 return DOSMEM_MapLinearToDos( base
);
257 /***********************************************************************
258 * SetSelectorBase (KERNEL.187)
260 WORD WINAPI
SetSelectorBase( WORD sel
, DWORD base
)
263 wine_ldt_get_entry( sel
, &entry
);
264 wine_ldt_set_base( &entry
, DOSMEM_MapDosToLinear(base
) );
265 wine_ldt_set_entry( sel
, &entry
);
270 /***********************************************************************
271 * GetSelectorLimit (KERNEL.188)
273 DWORD WINAPI
GetSelectorLimit16( WORD sel
)
275 return wine_ldt_copy
.limit
[sel
>> __AHSHIFT
];
279 /***********************************************************************
280 * SetSelectorLimit (KERNEL.189)
282 WORD WINAPI
SetSelectorLimit16( WORD sel
, DWORD limit
)
285 wine_ldt_get_entry( sel
, &entry
);
286 wine_ldt_set_limit( &entry
, limit
);
287 wine_ldt_set_entry( sel
, &entry
);
292 /***********************************************************************
293 * SelectorAccessRights (KERNEL.196)
295 WORD WINAPI
SelectorAccessRights16( WORD sel
, WORD op
, WORD val
)
298 wine_ldt_get_entry( sel
, &entry
);
300 if (op
== 0) /* get */
302 return entry
.HighWord
.Bytes
.Flags1
| ((entry
.HighWord
.Bytes
.Flags2
<< 8) & 0xf0);
306 entry
.HighWord
.Bytes
.Flags1
= LOBYTE(val
) | 0xf0;
307 entry
.HighWord
.Bytes
.Flags2
= (entry
.HighWord
.Bytes
.Flags2
& 0x0f) | (HIBYTE(val
) & 0xf0);
308 wine_ldt_set_entry( sel
, &entry
);
314 /***********************************************************************
315 * IsBadCodePtr (KERNEL.336)
317 BOOL16 WINAPI
IsBadCodePtr16( SEGPTR lpfn
)
322 sel
= SELECTOROF(lpfn
);
323 if (!sel
) return TRUE
;
324 wine_ldt_get_entry( sel
, &entry
);
325 if (wine_ldt_is_empty( &entry
)) return TRUE
;
326 /* check for code segment, ignoring conforming, read-only and accessed bits */
327 if ((entry
.HighWord
.Bits
.Type
^ WINE_LDT_FLAGS_CODE
) & 0x18) return TRUE
;
328 if (OFFSETOF(lpfn
) > wine_ldt_get_limit(&entry
)) return TRUE
;
333 /***********************************************************************
334 * IsBadStringPtr (KERNEL.337)
336 BOOL16 WINAPI
IsBadStringPtr16( SEGPTR ptr
, UINT16 size
)
341 sel
= SELECTOROF(ptr
);
342 if (!sel
) return TRUE
;
343 wine_ldt_get_entry( sel
, &entry
);
344 if (wine_ldt_is_empty( &entry
)) return TRUE
;
345 /* check for data or readable code segment */
346 if (!(entry
.HighWord
.Bits
.Type
& 0x10)) return TRUE
; /* system descriptor */
347 if ((entry
.HighWord
.Bits
.Type
& 0x0a) == 0x08) return TRUE
; /* non-readable code segment */
348 if (strlen(MapSL(ptr
)) < size
) size
= strlen(MapSL(ptr
)) + 1;
349 if (size
&& (OFFSETOF(ptr
) + size
- 1 > wine_ldt_get_limit(&entry
))) return TRUE
;
354 /***********************************************************************
355 * IsBadHugeReadPtr (KERNEL.346)
357 BOOL16 WINAPI
IsBadHugeReadPtr16( SEGPTR ptr
, DWORD size
)
362 sel
= SELECTOROF(ptr
);
363 if (!sel
) return TRUE
;
364 wine_ldt_get_entry( sel
, &entry
);
365 if (wine_ldt_is_empty( &entry
)) return TRUE
;
366 /* check for data or readable code segment */
367 if (!(entry
.HighWord
.Bits
.Type
& 0x10)) return TRUE
; /* system descriptor */
368 if ((entry
.HighWord
.Bits
.Type
& 0x0a) == 0x08) return TRUE
; /* non-readable code segment */
369 if (size
&& (OFFSETOF(ptr
) + size
- 1 > wine_ldt_get_limit( &entry
))) return TRUE
;
374 /***********************************************************************
375 * IsBadHugeWritePtr (KERNEL.347)
377 BOOL16 WINAPI
IsBadHugeWritePtr16( SEGPTR ptr
, DWORD size
)
382 sel
= SELECTOROF(ptr
);
383 if (!sel
) return TRUE
;
384 wine_ldt_get_entry( sel
, &entry
);
385 if (wine_ldt_is_empty( &entry
)) return TRUE
;
386 /* check for writable data segment, ignoring expand-down and accessed flags */
387 if ((entry
.HighWord
.Bits
.Type
^ WINE_LDT_FLAGS_DATA
) & ~5) return TRUE
;
388 if (size
&& (OFFSETOF(ptr
) + size
- 1 > wine_ldt_get_limit( &entry
))) return TRUE
;
392 /***********************************************************************
393 * IsBadReadPtr (KERNEL.334)
395 BOOL16 WINAPI
IsBadReadPtr16( SEGPTR ptr
, UINT16 size
)
397 return IsBadHugeReadPtr16( ptr
, size
);
401 /***********************************************************************
402 * IsBadWritePtr (KERNEL.335)
404 BOOL16 WINAPI
IsBadWritePtr16( SEGPTR ptr
, UINT16 size
)
406 return IsBadHugeWritePtr16( ptr
, size
);
410 /***********************************************************************
411 * IsBadFlatReadWritePtr (KERNEL.627)
413 BOOL16 WINAPI
IsBadFlatReadWritePtr16( SEGPTR ptr
, DWORD size
, BOOL16 bWrite
)
415 return bWrite
? IsBadHugeWritePtr16( ptr
, size
)
416 : IsBadHugeReadPtr16( ptr
, size
);
420 /***********************************************************************
421 * MemoryRead (TOOLHELP.78)
423 DWORD WINAPI
MemoryRead16( WORD sel
, DWORD offset
, void *buffer
, DWORD count
)
428 wine_ldt_get_entry( sel
, &entry
);
429 if (wine_ldt_is_empty( &entry
)) return 0;
430 limit
= wine_ldt_get_limit( &entry
);
431 if (offset
> limit
) return 0;
432 if (offset
+ count
> limit
+ 1) count
= limit
+ 1 - offset
;
433 memcpy( buffer
, (char *)wine_ldt_get_base(&entry
) + offset
, count
);
438 /***********************************************************************
439 * MemoryWrite (TOOLHELP.79)
441 DWORD WINAPI
MemoryWrite16( WORD sel
, DWORD offset
, void *buffer
, DWORD count
)
446 wine_ldt_get_entry( sel
, &entry
);
447 if (wine_ldt_is_empty( &entry
)) return 0;
448 limit
= wine_ldt_get_limit( &entry
);
449 if (offset
> limit
) return 0;
450 if (offset
+ count
> limit
) count
= limit
+ 1 - offset
;
451 memcpy( (char *)wine_ldt_get_base(&entry
) + offset
, buffer
, count
);
455 /************************************* Win95 pointer mapping functions *
461 struct mapls_entry
*next
;
462 void *addr
; /* linear address */
463 int count
; /* ref count */
464 WORD sel
; /* selector */
467 static struct mapls_entry
*first_entry
;
470 /***********************************************************************
474 * Maps linear pointer to segmented.
476 SEGPTR WINAPI
MapLS( LPCVOID ptr
)
478 struct mapls_entry
*entry
, *free
= NULL
;
482 if (!HIWORD(ptr
)) return (SEGPTR
)LOWORD(ptr
);
484 base
= (const char *)ptr
- ((ULONG_PTR
)ptr
& 0x7fff);
485 HeapLock( GetProcessHeap() );
486 for (entry
= first_entry
; entry
; entry
= entry
->next
)
488 if (entry
->addr
== base
) break;
489 if (!entry
->count
) free
= entry
;
494 if (!free
) /* no free entry found, create a new one */
496 if (!(free
= HeapAlloc( GetProcessHeap(), 0, sizeof(*free
) ))) goto done
;
497 if (!(free
->sel
= SELECTOR_AllocBlock( base
, 0x10000, WINE_LDT_FLAGS_DATA
)))
499 HeapFree( GetProcessHeap(), 0, free
);
503 free
->next
= first_entry
;
506 SetSelectorBase( free
->sel
, (DWORD
)base
);
507 free
->addr
= (void*)base
;
511 ret
= MAKESEGPTR( entry
->sel
, (const char *)ptr
- (char *)entry
->addr
);
513 HeapUnlock( GetProcessHeap() );
517 /***********************************************************************
518 * UnMapLS (KERNEL32.@)
519 * UnMapLS (KERNEL.359)
521 * Free mapped selector.
523 void WINAPI
UnMapLS( SEGPTR sptr
)
525 struct mapls_entry
*entry
;
526 WORD sel
= SELECTOROF(sptr
);
530 HeapLock( GetProcessHeap() );
531 for (entry
= first_entry
; entry
; entry
= entry
->next
) if (entry
->sel
== sel
) break;
532 if (entry
&& entry
->count
> 0) entry
->count
--;
533 HeapUnlock( GetProcessHeap() );
537 /***********************************************************************
541 * Maps fixed segmented pointer to linear.
543 LPVOID WINAPI
MapSL( SEGPTR sptr
)
545 return (char *)wine_ldt_copy
.base
[SELECTOROF(sptr
) >> __AHSHIFT
] + OFFSETOF(sptr
);
548 /***********************************************************************
549 * MapSLFix (KERNEL32.@)
551 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
552 * unexpected linear address change when GlobalCompact() shuffles
556 LPVOID WINAPI
MapSLFix( SEGPTR sptr
)
561 /***********************************************************************
562 * UnMapSLFixArray (KERNEL32.@)
564 * Must not change EAX, hence defined as asm function.
567 __ASM_GLOBAL_FUNC( UnMapSLFixArray
, "ret $8" )
571 /***********************************************************************
572 * GetThreadSelectorEntry (KERNEL32.@)
574 BOOL WINAPI
GetThreadSelectorEntry( HANDLE hthread
, DWORD sel
, LPLDT_ENTRY ldtent
)
576 THREAD_DESCRIPTOR_INFORMATION tdi
;
580 status
= NtQueryInformationThread( hthread
, ThreadDescriptorTableEntry
,
581 &tdi
, sizeof(tdi
), NULL
);
584 SetLastError( RtlNtStatusToDosError(status
) );
594 /***********************************************************************
595 * SMapLS (KERNEL32.@)
597 __ASM_GLOBAL_FUNC( SMapLS
,
599 "testl $0xffff0000,%eax\n\t"
602 "call " __ASM_NAME("MapLS") "\n\t"
606 /***********************************************************************
607 * SUnMapLS (KERNEL32.@)
609 __ASM_GLOBAL_FUNC( SUnMapLS
,
610 "pushl %eax\n\t" /* preserve eax */
612 "call " __ASM_NAME("UnMapLS") "\n\t"
616 /***********************************************************************
617 * SMapLS_IP_EBP_8 (KERNEL32.@)
618 * SMapLS_IP_EBP_12 (KERNEL32.@)
619 * SMapLS_IP_EBP_16 (KERNEL32.@)
620 * SMapLS_IP_EBP_20 (KERNEL32.@)
621 * SMapLS_IP_EBP_24 (KERNEL32.@)
622 * SMapLS_IP_EBP_28 (KERNEL32.@)
623 * SMapLS_IP_EBP_32 (KERNEL32.@)
624 * SMapLS_IP_EBP_36 (KERNEL32.@)
625 * SMapLS_IP_EBP_40 (KERNEL32.@)
627 * These functions map linear pointers at [EBP+xxx] to segmented pointers
629 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
630 * unravel them at SUnMapLS. We just store the segmented pointer there.
632 #define DEFINE_SMapLS(n) \
633 __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \
634 "movl " #n "(%ebp),%eax\n\t" \
635 "call " __ASM_NAME("SMapLS") "\n\t" \
636 "movl %edx," #n "(%ebp)\n\t" \
650 /***********************************************************************
651 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
652 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
653 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
654 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
655 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
656 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
657 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
658 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
659 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
662 #define DEFINE_SUnMapLS(n) \
663 __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \
664 "pushl %eax\n\t" /* preserve eax */ \
665 "pushl " #n "(%ebp)\n\t" \
666 "call " __ASM_NAME("UnMapLS") "\n\t" \
667 "movl $0," #n "(%ebp)\n\t" \
681 #endif /* __i386__ */