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 "kernel16_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 /************************************* Win95 pointer mapping functions *
426 struct mapls_entry
*next
;
427 void *addr
; /* linear address */
428 int count
; /* ref count */
429 WORD sel
; /* selector */
432 static struct mapls_entry
*first_entry
;
435 /***********************************************************************
439 * Maps linear pointer to segmented.
441 SEGPTR WINAPI
MapLS( LPCVOID ptr
)
443 struct mapls_entry
*entry
, *free
= NULL
;
447 if (!HIWORD(ptr
)) return (SEGPTR
)LOWORD(ptr
);
449 base
= (const char *)ptr
- ((ULONG_PTR
)ptr
& 0x7fff);
450 HeapLock( GetProcessHeap() );
451 for (entry
= first_entry
; entry
; entry
= entry
->next
)
453 if (entry
->addr
== base
) break;
454 if (!entry
->count
) free
= entry
;
459 if (!free
) /* no free entry found, create a new one */
461 if (!(free
= HeapAlloc( GetProcessHeap(), 0, sizeof(*free
) ))) goto done
;
462 if (!(free
->sel
= SELECTOR_AllocBlock( base
, 0x10000, WINE_LDT_FLAGS_DATA
)))
464 HeapFree( GetProcessHeap(), 0, free
);
468 free
->next
= first_entry
;
471 SetSelectorBase( free
->sel
, (DWORD
)base
);
472 free
->addr
= (void*)base
;
476 ret
= MAKESEGPTR( entry
->sel
, (const char *)ptr
- (char *)entry
->addr
);
478 HeapUnlock( GetProcessHeap() );
482 /***********************************************************************
483 * UnMapLS (KERNEL32.@)
484 * UnMapLS (KERNEL.359)
486 * Free mapped selector.
488 void WINAPI
UnMapLS( SEGPTR sptr
)
490 struct mapls_entry
*entry
;
491 WORD sel
= SELECTOROF(sptr
);
495 HeapLock( GetProcessHeap() );
496 for (entry
= first_entry
; entry
; entry
= entry
->next
) if (entry
->sel
== sel
) break;
497 if (entry
&& entry
->count
> 0) entry
->count
--;
498 HeapUnlock( GetProcessHeap() );
502 /***********************************************************************
506 * Maps fixed segmented pointer to linear.
508 LPVOID WINAPI
MapSL( SEGPTR sptr
)
510 return (char *)wine_ldt_copy
.base
[SELECTOROF(sptr
) >> __AHSHIFT
] + OFFSETOF(sptr
);
513 /***********************************************************************
514 * MapSLFix (KERNEL32.@)
516 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
517 * unexpected linear address change when GlobalCompact() shuffles
521 LPVOID WINAPI
MapSLFix( SEGPTR sptr
)
528 /***********************************************************************
529 * UnMapSLFixArray (KERNEL32.@)
531 * Must not change EAX, hence defined as asm function.
533 __ASM_STDCALL_FUNC( UnMapSLFixArray
, 8, "ret $8" )
535 /***********************************************************************
536 * SMapLS (KERNEL32.@)
538 __ASM_STDCALL_FUNC( SMapLS
, 0,
540 "testl $0xffff0000,%eax\n\t"
543 "call " __ASM_NAME("MapLS") __ASM_STDCALL(4) "\n\t"
547 /***********************************************************************
548 * SUnMapLS (KERNEL32.@)
550 __ASM_STDCALL_FUNC( SUnMapLS
, 0,
551 "pushl %eax\n\t" /* preserve eax */
553 "call " __ASM_NAME("UnMapLS") __ASM_STDCALL(4) "\n\t"
557 /***********************************************************************
558 * SMapLS_IP_EBP_8 (KERNEL32.@)
559 * SMapLS_IP_EBP_12 (KERNEL32.@)
560 * SMapLS_IP_EBP_16 (KERNEL32.@)
561 * SMapLS_IP_EBP_20 (KERNEL32.@)
562 * SMapLS_IP_EBP_24 (KERNEL32.@)
563 * SMapLS_IP_EBP_28 (KERNEL32.@)
564 * SMapLS_IP_EBP_32 (KERNEL32.@)
565 * SMapLS_IP_EBP_36 (KERNEL32.@)
566 * SMapLS_IP_EBP_40 (KERNEL32.@)
568 * These functions map linear pointers at [EBP+xxx] to segmented pointers
570 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
571 * unravel them at SUnMapLS. We just store the segmented pointer there.
573 #define DEFINE_SMapLS(n) \
574 __ASM_STDCALL_FUNC( SMapLS_IP_EBP_ ## n, 0, \
575 "movl " #n "(%ebp),%eax\n\t" \
576 "call " __ASM_NAME("SMapLS") __ASM_STDCALL(4) "\n\t" \
577 "movl %edx," #n "(%ebp)\n\t" \
591 /***********************************************************************
592 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
593 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
594 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
595 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
596 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
597 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
598 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
599 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
600 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
603 #define DEFINE_SUnMapLS(n) \
604 __ASM_STDCALL_FUNC( SUnMapLS_IP_EBP_ ## n, 0, \
605 "pushl %eax\n\t" /* preserve eax */ \
606 "pushl " #n "(%ebp)\n\t" \
607 "call " __ASM_NAME("UnMapLS") __ASM_STDCALL(4) "\n\t" \
608 "movl $0," #n "(%ebp)\n\t" \
622 #endif /* __i386__ */