kernel32: Renamed the kernel directory to kernel32.
[wine/multimedia.git] / dlls / kernel32 / selector.c
blob86d150ca790fca559bb0c2ff13752b7f1226a433
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 "winerror.h"
27 #include "wine/winbase16.h"
28 #include "wine/server.h"
29 #include "wine/debug.h"
30 #include "kernel_private.h"
31 #include "toolhelp.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(selector);
35 #define LDT_SIZE 8192
37 /* get the number of selectors needed to cover up to the selector limit */
38 inline static WORD get_sel_count( WORD sel )
40 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
44 /***********************************************************************
45 * AllocSelectorArray (KERNEL.206)
47 WORD WINAPI AllocSelectorArray16( WORD count )
49 WORD i, sel = wine_ldt_alloc_entries( count );
51 if (sel)
53 LDT_ENTRY entry;
54 wine_ldt_set_base( &entry, 0 );
55 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
56 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
57 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
59 return sel;
63 /***********************************************************************
64 * AllocSelector (KERNEL.175)
66 WORD WINAPI AllocSelector16( WORD sel )
68 WORD newsel, count, i;
70 count = sel ? get_sel_count(sel) : 1;
71 newsel = wine_ldt_alloc_entries( count );
72 TRACE("(%04x): returning %04x\n", sel, newsel );
73 if (!newsel) return 0;
74 if (!sel) return newsel; /* nothing to copy */
75 for (i = 0; i < count; i++)
77 LDT_ENTRY entry;
78 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
79 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
81 return newsel;
85 /***********************************************************************
86 * FreeSelector (KERNEL.176)
88 WORD WINAPI FreeSelector16( WORD sel )
90 LDT_ENTRY entry;
92 wine_ldt_get_entry( sel, &entry );
93 if (wine_ldt_is_empty( &entry )) return sel; /* error */
94 #ifdef __i386__
95 /* Check if we are freeing current %fs selector */
96 if (!((wine_get_fs() ^ sel) & ~3))
97 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
98 #endif /* __i386__ */
99 wine_ldt_free_entries( sel, 1 );
100 return 0;
104 /***********************************************************************
105 * SELECTOR_SetEntries
107 * Set the LDT entries for an array of selectors.
109 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
111 LDT_ENTRY entry;
112 WORD i, count;
114 wine_ldt_set_base( &entry, base );
115 wine_ldt_set_limit( &entry, size - 1 );
116 wine_ldt_set_flags( &entry, flags );
117 count = (size + 0xffff) / 0x10000;
118 for (i = 0; i < count; i++)
120 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
121 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
122 /* yep, Windows sets limit like that, not 64K sel units */
123 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
128 /***********************************************************************
129 * SELECTOR_AllocBlock
131 * Allocate selectors for a block of linear memory.
133 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
135 WORD sel, count;
137 if (!size) return 0;
138 count = (size + 0xffff) / 0x10000;
139 sel = wine_ldt_alloc_entries( count );
140 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
141 return sel;
145 /***********************************************************************
146 * SELECTOR_FreeBlock
148 * Free a block of selectors.
150 void SELECTOR_FreeBlock( WORD sel )
152 WORD i, count = get_sel_count( sel );
154 TRACE("(%04x,%d)\n", sel, count );
155 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
159 /***********************************************************************
160 * SELECTOR_ReallocBlock
162 * Change the size of a block of selectors.
164 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
166 LDT_ENTRY entry;
167 int oldcount, newcount;
169 if (!size) size = 1;
170 wine_ldt_get_entry( sel, &entry );
171 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
172 newcount = (size + 0xffff) >> 16;
174 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
175 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
176 return sel;
180 /***********************************************************************
181 * PrestoChangoSelector (KERNEL.177)
183 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
185 LDT_ENTRY entry;
186 wine_ldt_get_entry( selSrc, &entry );
187 /* toggle the executable bit */
188 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
189 wine_ldt_set_entry( selDst, &entry );
190 return selDst;
194 /***********************************************************************
195 * AllocCStoDSAlias (KERNEL.170)
196 * AllocAlias (KERNEL.172)
198 WORD WINAPI AllocCStoDSAlias16( WORD sel )
200 WORD newsel;
201 LDT_ENTRY entry;
203 newsel = wine_ldt_alloc_entries( 1 );
204 TRACE("(%04x): returning %04x\n",
205 sel, newsel );
206 if (!newsel) return 0;
207 wine_ldt_get_entry( sel, &entry );
208 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
209 wine_ldt_set_entry( newsel, &entry );
210 return newsel;
214 /***********************************************************************
215 * AllocDStoCSAlias (KERNEL.171)
217 WORD WINAPI AllocDStoCSAlias16( WORD sel )
219 WORD newsel;
220 LDT_ENTRY entry;
222 newsel = wine_ldt_alloc_entries( 1 );
223 TRACE("(%04x): returning %04x\n",
224 sel, newsel );
225 if (!newsel) return 0;
226 wine_ldt_get_entry( sel, &entry );
227 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
228 wine_ldt_set_entry( newsel, &entry );
229 return newsel;
233 /***********************************************************************
234 * LongPtrAdd (KERNEL.180)
236 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
238 LDT_ENTRY entry;
239 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
240 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
241 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
245 /***********************************************************************
246 * GetSelectorBase (KERNEL.186)
248 DWORD WINAPI GetSelectorBase( WORD sel )
250 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
252 /* if base points into DOSMEM, assume we have to
253 * return pointer into physical lower 1MB */
255 return DOSMEM_MapLinearToDos( base );
259 /***********************************************************************
260 * SetSelectorBase (KERNEL.187)
262 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
264 LDT_ENTRY entry;
265 wine_ldt_get_entry( sel, &entry );
266 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
267 wine_ldt_set_entry( sel, &entry );
268 return sel;
272 /***********************************************************************
273 * GetSelectorLimit (KERNEL.188)
275 DWORD WINAPI GetSelectorLimit16( WORD sel )
277 return wine_ldt_copy.limit[sel >> __AHSHIFT];
281 /***********************************************************************
282 * SetSelectorLimit (KERNEL.189)
284 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
286 LDT_ENTRY entry;
287 wine_ldt_get_entry( sel, &entry );
288 wine_ldt_set_limit( &entry, limit );
289 wine_ldt_set_entry( sel, &entry );
290 return sel;
294 /***********************************************************************
295 * SelectorAccessRights (KERNEL.196)
297 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
299 LDT_ENTRY entry;
300 wine_ldt_get_entry( sel, &entry );
302 if (op == 0) /* get */
304 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
306 else /* set */
308 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
309 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
310 wine_ldt_set_entry( sel, &entry );
311 return 0;
316 /***********************************************************************
317 * IsBadCodePtr (KERNEL.336)
319 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
321 WORD sel;
322 LDT_ENTRY entry;
324 sel = SELECTOROF(lpfn);
325 if (!sel) return TRUE;
326 wine_ldt_get_entry( sel, &entry );
327 if (wine_ldt_is_empty( &entry )) return TRUE;
328 /* check for code segment, ignoring conforming, read-only and accessed bits */
329 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
330 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
331 return FALSE;
335 /***********************************************************************
336 * IsBadStringPtr (KERNEL.337)
338 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
340 WORD sel;
341 LDT_ENTRY entry;
343 sel = SELECTOROF(ptr);
344 if (!sel) return TRUE;
345 wine_ldt_get_entry( sel, &entry );
346 if (wine_ldt_is_empty( &entry )) return TRUE;
347 /* check for data or readable code segment */
348 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
349 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
350 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
351 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
352 return FALSE;
356 /***********************************************************************
357 * IsBadHugeReadPtr (KERNEL.346)
359 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
361 WORD sel;
362 LDT_ENTRY entry;
364 sel = SELECTOROF(ptr);
365 if (!sel) return TRUE;
366 wine_ldt_get_entry( sel, &entry );
367 if (wine_ldt_is_empty( &entry )) return TRUE;
368 /* check for data or readable code segment */
369 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
370 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
371 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
372 return FALSE;
376 /***********************************************************************
377 * IsBadHugeWritePtr (KERNEL.347)
379 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
381 WORD sel;
382 LDT_ENTRY entry;
384 sel = SELECTOROF(ptr);
385 if (!sel) return TRUE;
386 wine_ldt_get_entry( sel, &entry );
387 if (wine_ldt_is_empty( &entry )) return TRUE;
388 /* check for writeable data segment, ignoring expand-down and accessed flags */
389 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
390 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
391 return FALSE;
394 /***********************************************************************
395 * IsBadReadPtr (KERNEL.334)
397 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
399 return IsBadHugeReadPtr16( ptr, size );
403 /***********************************************************************
404 * IsBadWritePtr (KERNEL.335)
406 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
408 return IsBadHugeWritePtr16( ptr, size );
412 /***********************************************************************
413 * IsBadFlatReadWritePtr (KERNEL.627)
415 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
417 return bWrite? IsBadHugeWritePtr16( ptr, size )
418 : IsBadHugeReadPtr16( ptr, size );
422 /***********************************************************************
423 * MemoryRead (TOOLHELP.78)
425 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
427 LDT_ENTRY entry;
428 DWORD limit;
430 wine_ldt_get_entry( sel, &entry );
431 if (wine_ldt_is_empty( &entry )) return 0;
432 limit = wine_ldt_get_limit( &entry );
433 if (offset > limit) return 0;
434 if (offset + count > limit + 1) count = limit + 1 - offset;
435 memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
436 return count;
440 /***********************************************************************
441 * MemoryWrite (TOOLHELP.79)
443 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
445 LDT_ENTRY entry;
446 DWORD limit;
448 wine_ldt_get_entry( sel, &entry );
449 if (wine_ldt_is_empty( &entry )) return 0;
450 limit = wine_ldt_get_limit( &entry );
451 if (offset > limit) return 0;
452 if (offset + count > limit) count = limit + 1 - offset;
453 memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
454 return count;
457 /************************************* Win95 pointer mapping functions *
461 struct mapls_entry
463 struct mapls_entry *next;
464 void *addr; /* linear address */
465 int count; /* ref count */
466 WORD sel; /* selector */
469 static struct mapls_entry *first_entry;
472 /***********************************************************************
473 * MapLS (KERNEL32.@)
474 * MapLS (KERNEL.358)
476 * Maps linear pointer to segmented.
478 SEGPTR WINAPI MapLS( LPCVOID ptr )
480 struct mapls_entry *entry, *free = NULL;
481 const void *base;
482 SEGPTR ret = 0;
484 if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr);
486 base = (const char *)ptr - ((unsigned int)ptr & 0x7fff);
487 HeapLock( GetProcessHeap() );
488 for (entry = first_entry; entry; entry = entry->next)
490 if (entry->addr == base) break;
491 if (!entry->count) free = entry;
494 if (!entry)
496 if (!free) /* no free entry found, create a new one */
498 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
499 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
501 HeapFree( GetProcessHeap(), 0, free );
502 goto done;
504 free->count = 0;
505 free->next = first_entry;
506 first_entry = free;
508 SetSelectorBase( free->sel, (DWORD)base );
509 free->addr = (void*)base;
510 entry = free;
512 entry->count++;
513 ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
514 done:
515 HeapUnlock( GetProcessHeap() );
516 return ret;
519 /***********************************************************************
520 * UnMapLS (KERNEL32.@)
521 * UnMapLS (KERNEL.359)
523 * Free mapped selector.
525 void WINAPI UnMapLS( SEGPTR sptr )
527 struct mapls_entry *entry;
528 WORD sel = SELECTOROF(sptr);
530 if (sel)
532 HeapLock( GetProcessHeap() );
533 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
534 if (entry && entry->count > 0) entry->count--;
535 HeapUnlock( GetProcessHeap() );
539 /***********************************************************************
540 * MapSL (KERNEL32.@)
541 * MapSL (KERNEL.357)
543 * Maps fixed segmented pointer to linear.
545 LPVOID WINAPI MapSL( SEGPTR sptr )
547 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
550 /***********************************************************************
551 * MapSLFix (KERNEL32.@)
553 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
554 * unexpected linear address change when GlobalCompact() shuffles
555 * moveable blocks.
558 LPVOID WINAPI MapSLFix( SEGPTR sptr )
560 return MapSL(sptr);
563 /***********************************************************************
564 * UnMapSLFixArray (KERNEL32.@)
566 * Must not change EAX, hence defined as asm function.
568 #ifdef __i386__
569 __ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" );
570 #endif
573 /***********************************************************************
574 * GetThreadSelectorEntry (KERNEL32.@)
576 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
578 THREAD_DESCRIPTOR_INFORMATION tdi;
579 NTSTATUS status;
581 tdi.Selector = sel;
582 status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry,
583 &tdi, sizeof(tdi), NULL);
584 if (status)
586 SetLastError( RtlNtStatusToDosError(status) );
587 return FALSE;
589 *ldtent = tdi.Entry;
590 return TRUE;
594 #ifdef __i386__
596 /***********************************************************************
597 * SMapLS (KERNEL32.@)
599 __ASM_GLOBAL_FUNC( SMapLS,
600 "xor %edx,%edx\n\t"
601 "testl $0xffff0000,%eax\n\t"
602 "jz 1f\n\t"
603 "pushl %eax\n\t"
604 "call " __ASM_NAME("MapLS") "\n\t"
605 "movl %eax,%edx\n"
606 "1:\tret" );
608 /***********************************************************************
609 * SUnMapLS (KERNEL32.@)
611 __ASM_GLOBAL_FUNC( SUnMapLS,
612 "pushl %eax\n\t" /* preserve eax */
613 "pushl %eax\n\t"
614 "call " __ASM_NAME("UnMapLS") "\n\t"
615 "popl %eax\n\t"
616 "ret" );
618 /***********************************************************************
619 * SMapLS_IP_EBP_8 (KERNEL32.@)
620 * SMapLS_IP_EBP_12 (KERNEL32.@)
621 * SMapLS_IP_EBP_16 (KERNEL32.@)
622 * SMapLS_IP_EBP_20 (KERNEL32.@)
623 * SMapLS_IP_EBP_24 (KERNEL32.@)
624 * SMapLS_IP_EBP_28 (KERNEL32.@)
625 * SMapLS_IP_EBP_32 (KERNEL32.@)
626 * SMapLS_IP_EBP_36 (KERNEL32.@)
627 * SMapLS_IP_EBP_40 (KERNEL32.@)
629 * These functions map linear pointers at [EBP+xxx] to segmented pointers
630 * and return them.
631 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
632 * unravel them at SUnMapLS. We just store the segmented pointer there.
634 #define DEFINE_SMapLS(n) \
635 __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \
636 "movl " #n "(%ebp),%eax\n\t" \
637 "call " __ASM_NAME("SMapLS") "\n\t" \
638 "movl %edx," #n "(%ebp)\n\t" \
639 "ret" );
641 DEFINE_SMapLS(8);
642 DEFINE_SMapLS(12);
643 DEFINE_SMapLS(16);
644 DEFINE_SMapLS(20);
645 DEFINE_SMapLS(24);
646 DEFINE_SMapLS(28);
647 DEFINE_SMapLS(32);
648 DEFINE_SMapLS(36);
649 DEFINE_SMapLS(40);
652 /***********************************************************************
653 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
654 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
655 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
656 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
657 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
658 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
659 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
660 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
661 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
664 #define DEFINE_SUnMapLS(n) \
665 __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \
666 "pushl %eax\n\t" /* preserve eax */ \
667 "pushl " #n "(%ebp)\n\t" \
668 "call " __ASM_NAME("UnMapLS") "\n\t" \
669 "movl $0," #n "(%ebp)\n\t" \
670 "popl %eax\n\t" \
671 "ret" )
673 DEFINE_SUnMapLS(8);
674 DEFINE_SUnMapLS(12);
675 DEFINE_SUnMapLS(16);
676 DEFINE_SUnMapLS(20);
677 DEFINE_SUnMapLS(24);
678 DEFINE_SUnMapLS(28);
679 DEFINE_SUnMapLS(32);
680 DEFINE_SUnMapLS(36);
681 DEFINE_SUnMapLS(40);
683 #endif /* __i386__ */