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 */
92 /* Check if we are freeing current %fs selector */
93 if (!((wine_get_fs() ^ sel) & ~3))
94 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
95 wine_ldt_free_entries( sel, 1 );
100 /***********************************************************************
101 * SELECTOR_SetEntries
103 * Set the LDT entries for an array of selectors.
105 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
110 wine_ldt_set_base( &entry, base );
111 wine_ldt_set_limit( &entry, size - 1 );
112 wine_ldt_set_flags( &entry, flags );
113 count = (size + 0xffff) / 0x10000;
114 for (i = 0; i < count; i++)
116 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
117 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
118 /* yep, Windows sets limit like that, not 64K sel units */
119 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
124 /***********************************************************************
125 * SELECTOR_AllocBlock
127 * Allocate selectors for a block of linear memory.
129 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
134 count = (size + 0xffff) / 0x10000;
135 sel = wine_ldt_alloc_entries( count );
136 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
141 /***********************************************************************
144 * Free a block of selectors.
146 void SELECTOR_FreeBlock( WORD sel )
148 WORD i, count = get_sel_count( sel );
150 TRACE("(%04x,%d)\n", sel, count );
151 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
155 /***********************************************************************
156 * SELECTOR_ReallocBlock
158 * Change the size of a block of selectors.
160 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
163 int oldcount, newcount;
166 wine_ldt_get_entry( sel, &entry );
167 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
168 newcount = (size + 0xffff) >> 16;
170 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
171 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
176 /***********************************************************************
177 * PrestoChangoSelector (KERNEL.177)
179 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
182 wine_ldt_get_entry( selSrc, &entry );
183 /* toggle the executable bit */
184 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
185 wine_ldt_set_entry( selDst, &entry );
190 /***********************************************************************
191 * AllocCStoDSAlias (KERNEL.170)
192 * AllocAlias (KERNEL.172)
194 WORD WINAPI AllocCStoDSAlias16( WORD sel )
199 newsel = wine_ldt_alloc_entries( 1 );
200 TRACE("(%04x): returning %04x\n",
202 if (!newsel) return 0;
203 wine_ldt_get_entry( sel, &entry );
204 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
205 wine_ldt_set_entry( newsel, &entry );
210 /***********************************************************************
211 * AllocDStoCSAlias (KERNEL.171)
213 WORD WINAPI AllocDStoCSAlias16( WORD sel )
218 newsel = wine_ldt_alloc_entries( 1 );
219 TRACE("(%04x): returning %04x\n",
221 if (!newsel) return 0;
222 wine_ldt_get_entry( sel, &entry );
223 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
224 wine_ldt_set_entry( newsel, &entry );
229 /***********************************************************************
230 * LongPtrAdd (KERNEL.180)
232 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
235 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
236 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
237 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
241 /***********************************************************************
242 * GetSelectorBase (KERNEL.186)
244 DWORD WINAPI GetSelectorBase( WORD sel )
246 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
248 /* if base points into DOSMEM, assume we have to
249 * return pointer into physical lower 1MB */
251 return DOSMEM_MapLinearToDos( base );
255 /***********************************************************************
256 * SetSelectorBase (KERNEL.187)
258 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
261 wine_ldt_get_entry( sel, &entry );
262 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
263 wine_ldt_set_entry( sel, &entry );
268 /***********************************************************************
269 * GetSelectorLimit (KERNEL.188)
271 DWORD WINAPI GetSelectorLimit16( WORD sel )
273 return wine_ldt_copy.limit[sel >> __AHSHIFT];
277 /***********************************************************************
278 * SetSelectorLimit (KERNEL.189)
280 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
283 wine_ldt_get_entry( sel, &entry );
284 wine_ldt_set_limit( &entry, limit );
285 wine_ldt_set_entry( sel, &entry );
290 /***********************************************************************
291 * SelectorAccessRights (KERNEL.196)
293 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
296 wine_ldt_get_entry( sel, &entry );
298 if (op == 0) /* get */
300 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
304 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
305 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
306 wine_ldt_set_entry( sel, &entry );
312 /***********************************************************************
313 * IsBadCodePtr (KERNEL.336)
315 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
320 sel = SELECTOROF(lpfn);
321 if (!sel) return TRUE;
322 wine_ldt_get_entry( sel, &entry );
323 if (wine_ldt_is_empty( &entry )) return TRUE;
324 /* check for code segment, ignoring conforming, read-only and accessed bits */
325 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
326 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
331 /***********************************************************************
332 * IsBadStringPtr (KERNEL.337)
334 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
339 sel = SELECTOROF(ptr);
340 if (!sel) return TRUE;
341 wine_ldt_get_entry( sel, &entry );
342 if (wine_ldt_is_empty( &entry )) return TRUE;
343 /* check for data or readable code segment */
344 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
345 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
346 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
347 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
352 /***********************************************************************
353 * IsBadHugeReadPtr (KERNEL.346)
355 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
360 sel = SELECTOROF(ptr);
361 if (!sel) return TRUE;
362 wine_ldt_get_entry( sel, &entry );
363 if (wine_ldt_is_empty( &entry )) return TRUE;
364 /* check for data or readable code segment */
365 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
366 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
367 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
372 /***********************************************************************
373 * IsBadHugeWritePtr (KERNEL.347)
375 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
380 sel = SELECTOROF(ptr);
381 if (!sel) return TRUE;
382 wine_ldt_get_entry( sel, &entry );
383 if (wine_ldt_is_empty( &entry )) return TRUE;
384 /* check for writable data segment, ignoring expand-down and accessed flags */
385 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
386 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
390 /***********************************************************************
391 * IsBadReadPtr (KERNEL.334)
393 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
395 return IsBadHugeReadPtr16( ptr, size );
399 /***********************************************************************
400 * IsBadWritePtr (KERNEL.335)
402 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
404 return IsBadHugeWritePtr16( ptr, size );
408 /***********************************************************************
409 * IsBadFlatReadWritePtr (KERNEL.627)
411 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
413 return bWrite? IsBadHugeWritePtr16( ptr, size )
414 : IsBadHugeReadPtr16( ptr, size );
418 /************************************* Win95 pointer mapping functions *
424 struct mapls_entry *next;
425 void *addr; /* linear address */
426 int count; /* ref count */
427 WORD sel; /* selector */
430 static struct mapls_entry *first_entry;
433 /***********************************************************************
437 * Maps linear pointer to segmented.
439 SEGPTR WINAPI MapLS( LPCVOID ptr )
441 struct mapls_entry *entry, *free = NULL;
445 if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr);
447 base = (const char *)ptr - ((ULONG_PTR)ptr & 0x7fff);
448 HeapLock( GetProcessHeap() );
449 for (entry = first_entry; entry; entry = entry->next)
451 if (entry->addr == base) break;
452 if (!entry->count) free = entry;
457 if (!free) /* no free entry found, create a new one */
459 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
460 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
462 HeapFree( GetProcessHeap(), 0, free );
466 free->next = first_entry;
469 SetSelectorBase( free->sel, (DWORD)base );
470 free->addr = (void*)base;
474 ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
476 HeapUnlock( GetProcessHeap() );
480 /***********************************************************************
481 * UnMapLS (KERNEL32.@)
482 * UnMapLS (KERNEL.359)
484 * Free mapped selector.
486 void WINAPI UnMapLS( SEGPTR sptr )
488 struct mapls_entry *entry;
489 WORD sel = SELECTOROF(sptr);
493 HeapLock( GetProcessHeap() );
494 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
495 if (entry && entry->count > 0) entry->count--;
496 HeapUnlock( GetProcessHeap() );
500 /***********************************************************************
504 * Maps fixed segmented pointer to linear.
506 LPVOID WINAPI MapSL( SEGPTR sptr )
508 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
511 /***********************************************************************
512 * MapSLFix (KERNEL32.@)
514 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
515 * unexpected linear address change when GlobalCompact() shuffles
519 LPVOID WINAPI MapSLFix( SEGPTR sptr )
525 /***********************************************************************
526 * UnMapSLFixArray (KERNEL32.@)
528 * Must not change EAX, hence defined as asm function.
530 __ASM_STDCALL_FUNC( UnMapSLFixArray, 8, "ret $8" )
532 /***********************************************************************
533 * SMapLS (KERNEL32.@)
535 __ASM_STDCALL_FUNC( SMapLS, 0,
537 "testl $0xffff0000,%eax\n\t"
540 "call " __ASM_NAME("MapLS") __ASM_STDCALL(4) "\n\t"
544 /***********************************************************************
545 * SUnMapLS (KERNEL32.@)
547 __ASM_STDCALL_FUNC( SUnMapLS, 0,
548 "pushl %eax\n\t" /* preserve eax */
550 "call " __ASM_NAME("UnMapLS") __ASM_STDCALL(4) "\n\t"
554 /***********************************************************************
555 * SMapLS_IP_EBP_8 (KERNEL32.@)
556 * SMapLS_IP_EBP_12 (KERNEL32.@)
557 * SMapLS_IP_EBP_16 (KERNEL32.@)
558 * SMapLS_IP_EBP_20 (KERNEL32.@)
559 * SMapLS_IP_EBP_24 (KERNEL32.@)
560 * SMapLS_IP_EBP_28 (KERNEL32.@)
561 * SMapLS_IP_EBP_32 (KERNEL32.@)
562 * SMapLS_IP_EBP_36 (KERNEL32.@)
563 * SMapLS_IP_EBP_40 (KERNEL32.@)
565 * These functions map linear pointers at [EBP+xxx] to segmented pointers
567 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
568 * unravel them at SUnMapLS. We just store the segmented pointer there.
570 #define DEFINE_SMapLS(n) \
571 __ASM_STDCALL_FUNC( SMapLS_IP_EBP_ ## n, 0, \
572 "movl " #n "(%ebp),%eax\n\t" \
573 "call " __ASM_NAME("SMapLS") __ASM_STDCALL(4) "\n\t" \
574 "movl %edx," #n "(%ebp)\n\t" \
588 /***********************************************************************
589 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
590 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
591 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
592 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
593 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
594 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
595 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
596 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
597 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
600 #define DEFINE_SUnMapLS(n) \
601 __ASM_STDCALL_FUNC( SUnMapLS_IP_EBP_ ## n, 0, \
602 "pushl %eax\n\t" /* preserve eax */ \
603 "pushl " #n "(%ebp)\n\t" \
604 "call " __ASM_NAME("UnMapLS") __ASM_STDCALL(4) "\n\t" \
605 "movl $0," #n "(%ebp)\n\t" \