kernel32: clean up imports
[wine/kumbayo.git] / dlls / kernel32 / selector.c
blobf34aecc18644c1c5e724ae60a36c5acc4baa12d7
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
26 #include "wine/winbase16.h"
27 #include "winternl.h"
28 #include "wine/debug.h"
29 #include "kernel_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(selector);
33 #define LDT_SIZE 8192
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 );
49 if (sel)
51 LDT_ENTRY entry;
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 );
57 return sel;
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++)
75 LDT_ENTRY entry;
76 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
77 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
79 return newsel;
83 /***********************************************************************
84 * FreeSelector (KERNEL.176)
86 WORD WINAPI FreeSelector16( WORD sel )
88 LDT_ENTRY entry;
90 wine_ldt_get_entry( sel, &entry );
91 if (wine_ldt_is_empty( &entry )) return sel; /* error */
92 #ifdef __i386__
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() );
96 #endif /* __i386__ */
97 wine_ldt_free_entries( sel, 1 );
98 return 0;
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 )
109 LDT_ENTRY entry;
110 WORD i, count;
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 )
133 WORD sel, count;
135 if (!size) return 0;
136 count = (size + 0xffff) / 0x10000;
137 sel = wine_ldt_alloc_entries( count );
138 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
139 return sel;
143 /***********************************************************************
144 * SELECTOR_FreeBlock
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 )
164 LDT_ENTRY entry;
165 int oldcount, newcount;
167 if (!size) size = 1;
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) );
174 return sel;
178 /***********************************************************************
179 * PrestoChangoSelector (KERNEL.177)
181 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
183 LDT_ENTRY entry;
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 );
188 return selDst;
192 /***********************************************************************
193 * AllocCStoDSAlias (KERNEL.170)
194 * AllocAlias (KERNEL.172)
196 WORD WINAPI AllocCStoDSAlias16( WORD sel )
198 WORD newsel;
199 LDT_ENTRY entry;
201 newsel = wine_ldt_alloc_entries( 1 );
202 TRACE("(%04x): returning %04x\n",
203 sel, newsel );
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 );
208 return newsel;
212 /***********************************************************************
213 * AllocDStoCSAlias (KERNEL.171)
215 WORD WINAPI AllocDStoCSAlias16( WORD sel )
217 WORD newsel;
218 LDT_ENTRY entry;
220 newsel = wine_ldt_alloc_entries( 1 );
221 TRACE("(%04x): returning %04x\n",
222 sel, newsel );
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 );
227 return newsel;
231 /***********************************************************************
232 * LongPtrAdd (KERNEL.180)
234 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
236 LDT_ENTRY entry;
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 )
262 LDT_ENTRY entry;
263 wine_ldt_get_entry( sel, &entry );
264 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
265 wine_ldt_set_entry( sel, &entry );
266 return sel;
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 )
284 LDT_ENTRY entry;
285 wine_ldt_get_entry( sel, &entry );
286 wine_ldt_set_limit( &entry, limit );
287 wine_ldt_set_entry( sel, &entry );
288 return sel;
292 /***********************************************************************
293 * SelectorAccessRights (KERNEL.196)
295 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
297 LDT_ENTRY entry;
298 wine_ldt_get_entry( sel, &entry );
300 if (op == 0) /* get */
302 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
304 else /* set */
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 );
309 return 0;
314 /***********************************************************************
315 * IsBadCodePtr (KERNEL.336)
317 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
319 WORD sel;
320 LDT_ENTRY entry;
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;
329 return FALSE;
333 /***********************************************************************
334 * IsBadStringPtr (KERNEL.337)
336 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
338 WORD sel;
339 LDT_ENTRY entry;
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;
350 return FALSE;
354 /***********************************************************************
355 * IsBadHugeReadPtr (KERNEL.346)
357 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
359 WORD sel;
360 LDT_ENTRY entry;
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;
370 return FALSE;
374 /***********************************************************************
375 * IsBadHugeWritePtr (KERNEL.347)
377 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
379 WORD sel;
380 LDT_ENTRY entry;
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;
389 return FALSE;
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 )
425 LDT_ENTRY entry;
426 DWORD limit;
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 );
434 return count;
438 /***********************************************************************
439 * MemoryWrite (TOOLHELP.79)
441 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
443 LDT_ENTRY entry;
444 DWORD limit;
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 );
452 return count;
455 /************************************* Win95 pointer mapping functions *
459 struct mapls_entry
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 /***********************************************************************
471 * MapLS (KERNEL32.@)
472 * MapLS (KERNEL.358)
474 * Maps linear pointer to segmented.
476 SEGPTR WINAPI MapLS( LPCVOID ptr )
478 struct mapls_entry *entry, *free = NULL;
479 const void *base;
480 SEGPTR ret = 0;
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;
492 if (!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 );
500 goto done;
502 free->count = 0;
503 free->next = first_entry;
504 first_entry = free;
506 SetSelectorBase( free->sel, (DWORD)base );
507 free->addr = (void*)base;
508 entry = free;
510 entry->count++;
511 ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
512 done:
513 HeapUnlock( GetProcessHeap() );
514 return ret;
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);
528 if (sel)
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 /***********************************************************************
538 * MapSL (KERNEL32.@)
539 * MapSL (KERNEL.357)
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
553 * moveable blocks.
556 LPVOID WINAPI MapSLFix( SEGPTR sptr )
558 return MapSL(sptr);
561 /***********************************************************************
562 * UnMapSLFixArray (KERNEL32.@)
564 * Must not change EAX, hence defined as asm function.
566 #ifdef __i386__
567 __ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" )
568 #endif
571 /***********************************************************************
572 * GetThreadSelectorEntry (KERNEL32.@)
574 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
576 THREAD_DESCRIPTOR_INFORMATION tdi;
577 NTSTATUS status;
579 tdi.Selector = sel;
580 status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry,
581 &tdi, sizeof(tdi), NULL);
582 if (status)
584 SetLastError( RtlNtStatusToDosError(status) );
585 return FALSE;
587 *ldtent = tdi.Entry;
588 return TRUE;
592 #ifdef __i386__
594 /***********************************************************************
595 * SMapLS (KERNEL32.@)
597 __ASM_GLOBAL_FUNC( SMapLS,
598 "xor %edx,%edx\n\t"
599 "testl $0xffff0000,%eax\n\t"
600 "jz 1f\n\t"
601 "pushl %eax\n\t"
602 "call " __ASM_NAME("MapLS") "\n\t"
603 "movl %eax,%edx\n"
604 "1:\tret" )
606 /***********************************************************************
607 * SUnMapLS (KERNEL32.@)
609 __ASM_GLOBAL_FUNC( SUnMapLS,
610 "pushl %eax\n\t" /* preserve eax */
611 "pushl %eax\n\t"
612 "call " __ASM_NAME("UnMapLS") "\n\t"
613 "popl %eax\n\t"
614 "ret" )
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
628 * and return them.
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" \
637 "ret" )
639 DEFINE_SMapLS(8)
640 DEFINE_SMapLS(12)
641 DEFINE_SMapLS(16)
642 DEFINE_SMapLS(20)
643 DEFINE_SMapLS(24)
644 DEFINE_SMapLS(28)
645 DEFINE_SMapLS(32)
646 DEFINE_SMapLS(36)
647 DEFINE_SMapLS(40)
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" \
668 "popl %eax\n\t" \
669 "ret" )
671 DEFINE_SUnMapLS(8)
672 DEFINE_SUnMapLS(12)
673 DEFINE_SUnMapLS(16)
674 DEFINE_SUnMapLS(20)
675 DEFINE_SUnMapLS(24)
676 DEFINE_SUnMapLS(28)
677 DEFINE_SUnMapLS(32)
678 DEFINE_SUnMapLS(36)
679 DEFINE_SUnMapLS(40)
681 #endif /* __i386__ */