quartz: Only allocate 1 buffer in transform filter.
[wine/wine64.git] / dlls / kernel32 / selector.c
blobc8841b1ad0b6da637f08875b5a166edb8e77664e
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 "wine/server.h"
28 #include "wine/debug.h"
29 #include "kernel_private.h"
30 #include "toolhelp.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(selector);
34 #define LDT_SIZE 8192
36 /* get the number of selectors needed to cover up to the selector limit */
37 static inline WORD get_sel_count( WORD sel )
39 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
43 /***********************************************************************
44 * AllocSelectorArray (KERNEL.206)
46 WORD WINAPI AllocSelectorArray16( WORD count )
48 WORD i, sel = wine_ldt_alloc_entries( count );
50 if (sel)
52 LDT_ENTRY entry;
53 wine_ldt_set_base( &entry, 0 );
54 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
55 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
56 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
58 return sel;
62 /***********************************************************************
63 * AllocSelector (KERNEL.175)
65 WORD WINAPI AllocSelector16( WORD sel )
67 WORD newsel, count, i;
69 count = sel ? get_sel_count(sel) : 1;
70 newsel = wine_ldt_alloc_entries( count );
71 TRACE("(%04x): returning %04x\n", sel, newsel );
72 if (!newsel) return 0;
73 if (!sel) return newsel; /* nothing to copy */
74 for (i = 0; i < count; i++)
76 LDT_ENTRY entry;
77 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
78 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
80 return newsel;
84 /***********************************************************************
85 * FreeSelector (KERNEL.176)
87 WORD WINAPI FreeSelector16( WORD sel )
89 LDT_ENTRY entry;
91 wine_ldt_get_entry( sel, &entry );
92 if (wine_ldt_is_empty( &entry )) return sel; /* error */
93 #ifdef __i386__
94 /* Check if we are freeing current %fs selector */
95 if (!((wine_get_fs() ^ sel) & ~3))
96 WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() );
97 #endif /* __i386__ */
98 wine_ldt_free_entries( sel, 1 );
99 return 0;
103 /***********************************************************************
104 * SELECTOR_SetEntries
106 * Set the LDT entries for an array of selectors.
108 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
110 LDT_ENTRY entry;
111 WORD i, count;
113 wine_ldt_set_base( &entry, base );
114 wine_ldt_set_limit( &entry, size - 1 );
115 wine_ldt_set_flags( &entry, flags );
116 count = (size + 0xffff) / 0x10000;
117 for (i = 0; i < count; i++)
119 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
120 wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000);
121 /* yep, Windows sets limit like that, not 64K sel units */
122 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
127 /***********************************************************************
128 * SELECTOR_AllocBlock
130 * Allocate selectors for a block of linear memory.
132 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
134 WORD sel, count;
136 if (!size) return 0;
137 count = (size + 0xffff) / 0x10000;
138 sel = wine_ldt_alloc_entries( count );
139 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
140 return sel;
144 /***********************************************************************
145 * SELECTOR_FreeBlock
147 * Free a block of selectors.
149 void SELECTOR_FreeBlock( WORD sel )
151 WORD i, count = get_sel_count( sel );
153 TRACE("(%04x,%d)\n", sel, count );
154 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
158 /***********************************************************************
159 * SELECTOR_ReallocBlock
161 * Change the size of a block of selectors.
163 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
165 LDT_ENTRY entry;
166 int oldcount, newcount;
168 if (!size) size = 1;
169 wine_ldt_get_entry( sel, &entry );
170 oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
171 newcount = (size + 0xffff) >> 16;
173 sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
174 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
175 return sel;
179 /***********************************************************************
180 * PrestoChangoSelector (KERNEL.177)
182 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
184 LDT_ENTRY entry;
185 wine_ldt_get_entry( selSrc, &entry );
186 /* toggle the executable bit */
187 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
188 wine_ldt_set_entry( selDst, &entry );
189 return selDst;
193 /***********************************************************************
194 * AllocCStoDSAlias (KERNEL.170)
195 * AllocAlias (KERNEL.172)
197 WORD WINAPI AllocCStoDSAlias16( WORD sel )
199 WORD newsel;
200 LDT_ENTRY entry;
202 newsel = wine_ldt_alloc_entries( 1 );
203 TRACE("(%04x): returning %04x\n",
204 sel, newsel );
205 if (!newsel) return 0;
206 wine_ldt_get_entry( sel, &entry );
207 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
208 wine_ldt_set_entry( newsel, &entry );
209 return newsel;
213 /***********************************************************************
214 * AllocDStoCSAlias (KERNEL.171)
216 WORD WINAPI AllocDStoCSAlias16( WORD sel )
218 WORD newsel;
219 LDT_ENTRY entry;
221 newsel = wine_ldt_alloc_entries( 1 );
222 TRACE("(%04x): returning %04x\n",
223 sel, newsel );
224 if (!newsel) return 0;
225 wine_ldt_get_entry( sel, &entry );
226 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
227 wine_ldt_set_entry( newsel, &entry );
228 return newsel;
232 /***********************************************************************
233 * LongPtrAdd (KERNEL.180)
235 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
237 LDT_ENTRY entry;
238 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
239 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
240 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
244 /***********************************************************************
245 * GetSelectorBase (KERNEL.186)
247 DWORD WINAPI GetSelectorBase( WORD sel )
249 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
251 /* if base points into DOSMEM, assume we have to
252 * return pointer into physical lower 1MB */
254 return DOSMEM_MapLinearToDos( base );
258 /***********************************************************************
259 * SetSelectorBase (KERNEL.187)
261 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
263 LDT_ENTRY entry;
264 wine_ldt_get_entry( sel, &entry );
265 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
266 wine_ldt_set_entry( sel, &entry );
267 return sel;
271 /***********************************************************************
272 * GetSelectorLimit (KERNEL.188)
274 DWORD WINAPI GetSelectorLimit16( WORD sel )
276 return wine_ldt_copy.limit[sel >> __AHSHIFT];
280 /***********************************************************************
281 * SetSelectorLimit (KERNEL.189)
283 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
285 LDT_ENTRY entry;
286 wine_ldt_get_entry( sel, &entry );
287 wine_ldt_set_limit( &entry, limit );
288 wine_ldt_set_entry( sel, &entry );
289 return sel;
293 /***********************************************************************
294 * SelectorAccessRights (KERNEL.196)
296 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
298 LDT_ENTRY entry;
299 wine_ldt_get_entry( sel, &entry );
301 if (op == 0) /* get */
303 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
305 else /* set */
307 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
308 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
309 wine_ldt_set_entry( sel, &entry );
310 return 0;
315 /***********************************************************************
316 * IsBadCodePtr (KERNEL.336)
318 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
320 WORD sel;
321 LDT_ENTRY entry;
323 sel = SELECTOROF(lpfn);
324 if (!sel) return TRUE;
325 wine_ldt_get_entry( sel, &entry );
326 if (wine_ldt_is_empty( &entry )) return TRUE;
327 /* check for code segment, ignoring conforming, read-only and accessed bits */
328 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
329 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
330 return FALSE;
334 /***********************************************************************
335 * IsBadStringPtr (KERNEL.337)
337 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
339 WORD sel;
340 LDT_ENTRY entry;
342 sel = SELECTOROF(ptr);
343 if (!sel) return TRUE;
344 wine_ldt_get_entry( sel, &entry );
345 if (wine_ldt_is_empty( &entry )) return TRUE;
346 /* check for data or readable code segment */
347 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
348 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
349 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
350 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
351 return FALSE;
355 /***********************************************************************
356 * IsBadHugeReadPtr (KERNEL.346)
358 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
360 WORD sel;
361 LDT_ENTRY entry;
363 sel = SELECTOROF(ptr);
364 if (!sel) return TRUE;
365 wine_ldt_get_entry( sel, &entry );
366 if (wine_ldt_is_empty( &entry )) return TRUE;
367 /* check for data or readable code segment */
368 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
369 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
370 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
371 return FALSE;
375 /***********************************************************************
376 * IsBadHugeWritePtr (KERNEL.347)
378 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
380 WORD sel;
381 LDT_ENTRY entry;
383 sel = SELECTOROF(ptr);
384 if (!sel) return TRUE;
385 wine_ldt_get_entry( sel, &entry );
386 if (wine_ldt_is_empty( &entry )) return TRUE;
387 /* check for writable data segment, ignoring expand-down and accessed flags */
388 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
389 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
390 return FALSE;
393 /***********************************************************************
394 * IsBadReadPtr (KERNEL.334)
396 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
398 return IsBadHugeReadPtr16( ptr, size );
402 /***********************************************************************
403 * IsBadWritePtr (KERNEL.335)
405 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
407 return IsBadHugeWritePtr16( ptr, size );
411 /***********************************************************************
412 * IsBadFlatReadWritePtr (KERNEL.627)
414 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
416 return bWrite? IsBadHugeWritePtr16( ptr, size )
417 : IsBadHugeReadPtr16( ptr, size );
421 /***********************************************************************
422 * MemoryRead (TOOLHELP.78)
424 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
426 LDT_ENTRY entry;
427 DWORD limit;
429 wine_ldt_get_entry( sel, &entry );
430 if (wine_ldt_is_empty( &entry )) return 0;
431 limit = wine_ldt_get_limit( &entry );
432 if (offset > limit) return 0;
433 if (offset + count > limit + 1) count = limit + 1 - offset;
434 memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
435 return count;
439 /***********************************************************************
440 * MemoryWrite (TOOLHELP.79)
442 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
444 LDT_ENTRY entry;
445 DWORD limit;
447 wine_ldt_get_entry( sel, &entry );
448 if (wine_ldt_is_empty( &entry )) return 0;
449 limit = wine_ldt_get_limit( &entry );
450 if (offset > limit) return 0;
451 if (offset + count > limit) count = limit + 1 - offset;
452 memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
453 return count;
456 /************************************* Win95 pointer mapping functions *
460 struct mapls_entry
462 struct mapls_entry *next;
463 void *addr; /* linear address */
464 int count; /* ref count */
465 WORD sel; /* selector */
468 static struct mapls_entry *first_entry;
471 /***********************************************************************
472 * MapLS (KERNEL32.@)
473 * MapLS (KERNEL.358)
475 * Maps linear pointer to segmented.
477 SEGPTR WINAPI MapLS( LPCVOID ptr )
479 struct mapls_entry *entry, *free = NULL;
480 const void *base;
481 SEGPTR ret = 0;
483 if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr);
485 base = (const char *)ptr - ((ULONG_PTR)ptr & 0x7fff);
486 HeapLock( GetProcessHeap() );
487 for (entry = first_entry; entry; entry = entry->next)
489 if (entry->addr == base) break;
490 if (!entry->count) free = entry;
493 if (!entry)
495 if (!free) /* no free entry found, create a new one */
497 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
498 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
500 HeapFree( GetProcessHeap(), 0, free );
501 goto done;
503 free->count = 0;
504 free->next = first_entry;
505 first_entry = free;
507 SetSelectorBase( free->sel, (DWORD)base );
508 free->addr = (void*)base;
509 entry = free;
511 entry->count++;
512 ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
513 done:
514 HeapUnlock( GetProcessHeap() );
515 return ret;
518 /***********************************************************************
519 * UnMapLS (KERNEL32.@)
520 * UnMapLS (KERNEL.359)
522 * Free mapped selector.
524 void WINAPI UnMapLS( SEGPTR sptr )
526 struct mapls_entry *entry;
527 WORD sel = SELECTOROF(sptr);
529 if (sel)
531 HeapLock( GetProcessHeap() );
532 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
533 if (entry && entry->count > 0) entry->count--;
534 HeapUnlock( GetProcessHeap() );
538 /***********************************************************************
539 * MapSL (KERNEL32.@)
540 * MapSL (KERNEL.357)
542 * Maps fixed segmented pointer to linear.
544 LPVOID WINAPI MapSL( SEGPTR sptr )
546 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
549 /***********************************************************************
550 * MapSLFix (KERNEL32.@)
552 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
553 * unexpected linear address change when GlobalCompact() shuffles
554 * moveable blocks.
557 LPVOID WINAPI MapSLFix( SEGPTR sptr )
559 return MapSL(sptr);
562 /***********************************************************************
563 * UnMapSLFixArray (KERNEL32.@)
565 * Must not change EAX, hence defined as asm function.
567 #ifdef __i386__
568 __ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" )
569 #endif
572 /***********************************************************************
573 * GetThreadSelectorEntry (KERNEL32.@)
575 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
577 THREAD_DESCRIPTOR_INFORMATION tdi;
578 NTSTATUS status;
580 tdi.Selector = sel;
581 status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry,
582 &tdi, sizeof(tdi), NULL);
583 if (status)
585 SetLastError( RtlNtStatusToDosError(status) );
586 return FALSE;
588 *ldtent = tdi.Entry;
589 return TRUE;
593 #ifdef __i386__
595 /***********************************************************************
596 * SMapLS (KERNEL32.@)
598 __ASM_GLOBAL_FUNC( SMapLS,
599 "xor %edx,%edx\n\t"
600 "testl $0xffff0000,%eax\n\t"
601 "jz 1f\n\t"
602 "pushl %eax\n\t"
603 "call " __ASM_NAME("MapLS") "\n\t"
604 "movl %eax,%edx\n"
605 "1:\tret" )
607 /***********************************************************************
608 * SUnMapLS (KERNEL32.@)
610 __ASM_GLOBAL_FUNC( SUnMapLS,
611 "pushl %eax\n\t" /* preserve eax */
612 "pushl %eax\n\t"
613 "call " __ASM_NAME("UnMapLS") "\n\t"
614 "popl %eax\n\t"
615 "ret" )
617 /***********************************************************************
618 * SMapLS_IP_EBP_8 (KERNEL32.@)
619 * SMapLS_IP_EBP_12 (KERNEL32.@)
620 * SMapLS_IP_EBP_16 (KERNEL32.@)
621 * SMapLS_IP_EBP_20 (KERNEL32.@)
622 * SMapLS_IP_EBP_24 (KERNEL32.@)
623 * SMapLS_IP_EBP_28 (KERNEL32.@)
624 * SMapLS_IP_EBP_32 (KERNEL32.@)
625 * SMapLS_IP_EBP_36 (KERNEL32.@)
626 * SMapLS_IP_EBP_40 (KERNEL32.@)
628 * These functions map linear pointers at [EBP+xxx] to segmented pointers
629 * and return them.
630 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
631 * unravel them at SUnMapLS. We just store the segmented pointer there.
633 #define DEFINE_SMapLS(n) \
634 __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \
635 "movl " #n "(%ebp),%eax\n\t" \
636 "call " __ASM_NAME("SMapLS") "\n\t" \
637 "movl %edx," #n "(%ebp)\n\t" \
638 "ret" )
640 DEFINE_SMapLS(8)
641 DEFINE_SMapLS(12)
642 DEFINE_SMapLS(16)
643 DEFINE_SMapLS(20)
644 DEFINE_SMapLS(24)
645 DEFINE_SMapLS(28)
646 DEFINE_SMapLS(32)
647 DEFINE_SMapLS(36)
648 DEFINE_SMapLS(40)
651 /***********************************************************************
652 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
653 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
654 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
655 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
656 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
657 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
658 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
659 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
660 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
663 #define DEFINE_SUnMapLS(n) \
664 __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \
665 "pushl %eax\n\t" /* preserve eax */ \
666 "pushl " #n "(%ebp)\n\t" \
667 "call " __ASM_NAME("UnMapLS") "\n\t" \
668 "movl $0," #n "(%ebp)\n\t" \
669 "popl %eax\n\t" \
670 "ret" )
672 DEFINE_SUnMapLS(8)
673 DEFINE_SUnMapLS(12)
674 DEFINE_SUnMapLS(16)
675 DEFINE_SUnMapLS(20)
676 DEFINE_SUnMapLS(24)
677 DEFINE_SUnMapLS(28)
678 DEFINE_SUnMapLS(32)
679 DEFINE_SUnMapLS(36)
680 DEFINE_SUnMapLS(40)
682 #endif /* __i386__ */