Moved heap functions to ntdll.
[wine/dcerpc.git] / memory / selector.c
blobe68a92ff1ea83704da04f012dadaf07ad7771c45
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <string.h>
11 #include "winerror.h"
12 #include "wine/winbase16.h"
13 #include "miscemu.h"
14 #include "selectors.h"
15 #include "stackframe.h"
16 #include "wine/server.h"
17 #include "debugtools.h"
18 #include "toolhelp.h"
20 DEFAULT_DEBUG_CHANNEL(selector);
22 #define LDT_SIZE 8192
24 /* get the number of selectors needed to cover up to the selector limit */
25 inline static WORD get_sel_count( WORD sel )
27 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
30 /***********************************************************************
31 * SELECTOR_AllocArray
33 * Allocate a selector array without setting the LDT entries
35 static WORD SELECTOR_AllocArray( WORD count )
37 WORD i, sel, size = 0;
39 if (!count) return 0;
40 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
42 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
43 else if (++size >= count) break;
45 if (i == LDT_SIZE) return 0;
46 sel = i - size + 1;
48 /* mark selectors as allocated */
49 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
51 return (sel << __AHSHIFT) | 7;
55 /***********************************************************************
56 * AllocSelectorArray (KERNEL.206)
58 WORD WINAPI AllocSelectorArray16( WORD count )
60 WORD i, sel = SELECTOR_AllocArray( count );
62 if (sel)
64 LDT_ENTRY entry;
65 wine_ldt_set_base( &entry, 0 );
66 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
67 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
68 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
70 return sel;
74 /***********************************************************************
75 * AllocSelector (KERNEL.175)
77 WORD WINAPI AllocSelector16( WORD sel )
79 WORD newsel, count, i;
81 count = sel ? get_sel_count(sel) : 1;
82 newsel = SELECTOR_AllocArray( count );
83 TRACE("(%04x): returning %04x\n", sel, newsel );
84 if (!newsel) return 0;
85 if (!sel) return newsel; /* nothing to copy */
86 for (i = 0; i < count; i++)
88 LDT_ENTRY entry;
89 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
90 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
92 return newsel;
96 /***********************************************************************
97 * FreeSelector (KERNEL.176)
99 WORD WINAPI FreeSelector16( WORD sel )
101 LDT_ENTRY entry;
103 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
105 #ifdef __i386__
106 /* Check if we are freeing current %fs or %gs selector */
107 if (!((__get_fs() ^ sel) & ~7))
109 WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
110 __set_fs( 0 );
112 if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
113 #endif /* __i386__ */
115 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
116 wine_ldt_set_entry( sel, &entry );
117 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
118 return 0;
122 /***********************************************************************
123 * SELECTOR_SetEntries
125 * Set the LDT entries for an array of selectors.
127 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
129 LDT_ENTRY entry;
130 WORD i, count;
132 wine_ldt_set_base( &entry, base );
133 wine_ldt_set_limit( &entry, size - 1 );
134 wine_ldt_set_flags( &entry, flags );
135 /* Make sure base and limit are not 0 together if the size is not 0 */
136 if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
137 count = (size + 0xffff) / 0x10000;
138 for (i = 0; i < count; i++)
140 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
141 wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
142 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
147 /***********************************************************************
148 * SELECTOR_AllocBlock
150 * Allocate selectors for a block of linear memory.
152 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
154 WORD sel, count;
156 if (!size) return 0;
157 count = (size + 0xffff) / 0x10000;
158 sel = SELECTOR_AllocArray( count );
159 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
160 return sel;
164 /***********************************************************************
165 * SELECTOR_FreeBlock
167 * Free a block of selectors.
169 void SELECTOR_FreeBlock( WORD sel )
171 WORD i, count = get_sel_count( sel );
173 TRACE("(%04x,%d)\n", sel, count );
174 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
178 /***********************************************************************
179 * SELECTOR_ReallocBlock
181 * Change the size of a block of selectors.
183 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
185 LDT_ENTRY entry;
186 WORD i, oldcount, newcount;
188 if (!size) size = 1;
189 oldcount = get_sel_count( sel );
190 newcount = (size + 0xffff) >> 16;
191 wine_ldt_get_entry( sel, &entry );
193 if (oldcount < newcount) /* We need to add selectors */
195 WORD index = sel >> __AHSHIFT;
196 /* Check if the next selectors are free */
197 if (index + newcount > LDT_SIZE) i = oldcount;
198 else
199 for (i = oldcount; i < newcount; i++)
200 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
202 if (i < newcount) /* they are not free */
204 SELECTOR_FreeBlock( sel );
205 sel = SELECTOR_AllocArray( newcount );
207 else /* mark the selectors as allocated */
209 for (i = oldcount; i < newcount; i++)
210 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
213 else if (oldcount > newcount) /* We need to remove selectors */
215 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
217 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
218 return sel;
222 /***********************************************************************
223 * PrestoChangoSelector (KERNEL.177)
225 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
227 LDT_ENTRY entry;
228 wine_ldt_get_entry( selSrc, &entry );
229 /* toggle the executable bit */
230 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
231 wine_ldt_set_entry( selDst, &entry );
232 return selDst;
236 /***********************************************************************
237 * AllocCStoDSAlias (KERNEL.170)
238 * AllocAlias (KERNEL.172)
240 WORD WINAPI AllocCStoDSAlias16( WORD sel )
242 WORD newsel;
243 LDT_ENTRY entry;
245 newsel = SELECTOR_AllocArray( 1 );
246 TRACE("(%04x): returning %04x\n",
247 sel, newsel );
248 if (!newsel) return 0;
249 wine_ldt_get_entry( sel, &entry );
250 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
251 wine_ldt_set_entry( newsel, &entry );
252 return newsel;
256 /***********************************************************************
257 * AllocDStoCSAlias (KERNEL.171)
259 WORD WINAPI AllocDStoCSAlias16( WORD sel )
261 WORD newsel;
262 LDT_ENTRY entry;
264 newsel = SELECTOR_AllocArray( 1 );
265 TRACE("(%04x): returning %04x\n",
266 sel, newsel );
267 if (!newsel) return 0;
268 wine_ldt_get_entry( sel, &entry );
269 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
270 wine_ldt_set_entry( newsel, &entry );
271 return newsel;
275 /***********************************************************************
276 * LongPtrAdd (KERNEL.180)
278 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
280 LDT_ENTRY entry;
281 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
282 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
283 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
287 /***********************************************************************
288 * GetSelectorBase (KERNEL.186)
290 DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
293 * Note: For Win32s processes, the whole linear address space is
294 * shifted by 0x10000 relative to the OS linear address space.
295 * See the comment in msdos/vxd.c.
298 DWORD base = GetSelectorBase( sel );
299 return W32S_WINE2APP( base );
301 DWORD WINAPI GetSelectorBase( WORD sel )
303 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
305 /* if base points into DOSMEM, assume we have to
306 * return pointer into physical lower 1MB */
308 return DOSMEM_MapLinearToDos( base );
312 /***********************************************************************
313 * SetSelectorBase (KERNEL.187)
315 DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
318 * Note: For Win32s processes, the whole linear address space is
319 * shifted by 0x10000 relative to the OS linear address space.
320 * See the comment in msdos/vxd.c.
323 SetSelectorBase( sel, W32S_APP2WINE( base ) );
324 return sel;
326 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
328 LDT_ENTRY entry;
329 wine_ldt_get_entry( sel, &entry );
330 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
331 wine_ldt_set_entry( sel, &entry );
332 return sel;
336 /***********************************************************************
337 * GetSelectorLimit (KERNEL.188)
339 DWORD WINAPI GetSelectorLimit16( WORD sel )
341 return wine_ldt_copy.limit[sel >> __AHSHIFT];
345 /***********************************************************************
346 * SetSelectorLimit (KERNEL.189)
348 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
350 LDT_ENTRY entry;
351 wine_ldt_get_entry( sel, &entry );
352 wine_ldt_set_limit( &entry, limit );
353 wine_ldt_set_entry( sel, &entry );
354 return sel;
358 /***********************************************************************
359 * SelectorAccessRights (KERNEL.196)
361 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
363 LDT_ENTRY entry;
364 wine_ldt_get_entry( sel, &entry );
366 if (op == 0) /* get */
368 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
370 else /* set */
372 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
373 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
374 wine_ldt_set_entry( sel, &entry );
375 return 0;
380 /***********************************************************************
381 * IsBadCodePtr (KERNEL.336)
383 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
385 WORD sel;
386 LDT_ENTRY entry;
388 sel = SELECTOROF(lpfn);
389 if (!sel) return TRUE;
390 if (IS_SELECTOR_FREE(sel)) return TRUE;
391 wine_ldt_get_entry( sel, &entry );
392 /* check for code segment, ignoring conforming, read-only and accessed bits */
393 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
394 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
395 return FALSE;
399 /***********************************************************************
400 * IsBadStringPtr (KERNEL.337)
402 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
404 WORD sel;
405 LDT_ENTRY entry;
407 sel = SELECTOROF(ptr);
408 if (!sel) return TRUE;
409 if (IS_SELECTOR_FREE(sel)) return TRUE;
410 wine_ldt_get_entry( sel, &entry );
411 /* check for data or readable code segment */
412 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
413 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
414 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
415 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
416 return FALSE;
420 /***********************************************************************
421 * IsBadHugeReadPtr (KERNEL.346)
423 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
425 WORD sel;
426 LDT_ENTRY entry;
428 sel = SELECTOROF(ptr);
429 if (!sel) return TRUE;
430 if (IS_SELECTOR_FREE(sel)) return TRUE;
431 wine_ldt_get_entry( sel, &entry );
432 /* check for data or readable code segment */
433 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
434 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
435 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
436 return FALSE;
440 /***********************************************************************
441 * IsBadHugeWritePtr (KERNEL.347)
443 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
445 WORD sel;
446 LDT_ENTRY entry;
448 sel = SELECTOROF(ptr);
449 if (!sel) return TRUE;
450 if (IS_SELECTOR_FREE(sel)) return TRUE;
451 wine_ldt_get_entry( sel, &entry );
452 /* check for writeable data segment, ignoring expand-down and accessed flags */
453 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
454 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
455 return FALSE;
458 /***********************************************************************
459 * IsBadReadPtr (KERNEL.334)
461 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
463 return IsBadHugeReadPtr16( ptr, size );
467 /***********************************************************************
468 * IsBadWritePtr (KERNEL.335)
470 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
472 return IsBadHugeWritePtr16( ptr, size );
476 /***********************************************************************
477 * IsBadFlatReadWritePtr (KERNEL.627)
479 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
481 return bWrite? IsBadHugeWritePtr16( ptr, size )
482 : IsBadHugeReadPtr16( ptr, size );
486 /***********************************************************************
487 * MemoryRead (TOOLHELP.78)
489 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
491 WORD index = sel >> __AHSHIFT;
493 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
494 if (offset > wine_ldt_copy.limit[index]) return 0;
495 if (offset + count > wine_ldt_copy.limit[index] + 1)
496 count = wine_ldt_copy.limit[index] + 1 - offset;
497 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
498 return count;
502 /***********************************************************************
503 * MemoryWrite (TOOLHELP.79)
505 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
507 WORD index = sel >> __AHSHIFT;
509 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
510 if (offset > wine_ldt_copy.limit[index]) return 0;
511 if (offset + count > wine_ldt_copy.limit[index] + 1)
512 count = wine_ldt_copy.limit[index] + 1 - offset;
513 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
514 return count;
517 /************************************* Win95 pointer mapping functions *
521 struct mapls_entry
523 struct mapls_entry *next;
524 void *addr; /* linear address */
525 int count; /* ref count */
526 WORD sel; /* selector */
529 static struct mapls_entry *first_entry;
532 /***********************************************************************
533 * MapLS (KERNEL32.@)
534 * MapLS (KERNEL.358)
536 * Maps linear pointer to segmented.
538 SEGPTR WINAPI MapLS( LPCVOID ptr )
540 struct mapls_entry *entry, *free = NULL;
541 void *base;
542 SEGPTR ret = 0;
544 if (!HIWORD(ptr)) return (SEGPTR)ptr;
546 base = (char *)ptr - ((unsigned int)ptr & 0x7fff);
547 HeapLock( GetProcessHeap() );
548 for (entry = first_entry; entry; entry = entry->next)
550 if (entry->addr == base) break;
551 if (!entry->count) free = entry;
554 if (!entry)
556 if (!free) /* no free entry found, create a new one */
558 if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
559 if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
561 HeapFree( GetProcessHeap(), 0, free );
562 goto done;
564 free->count = 0;
565 free->next = first_entry;
566 first_entry = free;
568 SetSelectorBase( free->sel, (DWORD)base );
569 free->addr = base;
570 entry = free;
572 entry->count++;
573 ret = MAKESEGPTR( entry->sel, (char *)ptr - (char *)entry->addr );
574 done:
575 HeapUnlock( GetProcessHeap() );
576 return ret;
579 /***********************************************************************
580 * UnMapLS (KERNEL32.@)
581 * UnMapLS (KERNEL.359)
583 * Free mapped selector.
585 void WINAPI UnMapLS( SEGPTR sptr )
587 struct mapls_entry *entry;
588 WORD sel = SELECTOROF(sptr);
590 if (sel)
592 HeapLock( GetProcessHeap() );
593 for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
594 if (entry && entry->count > 0) entry->count--;
595 HeapUnlock( GetProcessHeap() );
599 /***********************************************************************
600 * MapSL (KERNEL32.@)
601 * MapSL (KERNEL.357)
603 * Maps fixed segmented pointer to linear.
605 LPVOID WINAPI MapSL( SEGPTR sptr )
607 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
610 /***********************************************************************
611 * MapSLFix (KERNEL32.@)
613 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
614 * unexpected linear address change when GlobalCompact() shuffles
615 * moveable blocks.
618 LPVOID WINAPI MapSLFix( SEGPTR sptr )
620 return MapSL(sptr);
623 /***********************************************************************
624 * UnMapSLFixArray (KERNEL32.@)
627 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
629 /* Must not change EAX, hence defined as 'register' function */
632 /***********************************************************************
633 * GetThreadSelectorEntry (KERNEL32.@)
635 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
637 #ifdef __i386__
638 BOOL ret;
640 if (!(sel & 4)) /* GDT selector */
642 sel &= ~3; /* ignore RPL */
643 if (!sel) /* null selector */
645 memset( ldtent, 0, sizeof(*ldtent) );
646 return TRUE;
648 ldtent->BaseLow = 0;
649 ldtent->HighWord.Bits.BaseMid = 0;
650 ldtent->HighWord.Bits.BaseHi = 0;
651 ldtent->LimitLow = 0xffff;
652 ldtent->HighWord.Bits.LimitHi = 0xf;
653 ldtent->HighWord.Bits.Dpl = 3;
654 ldtent->HighWord.Bits.Sys = 0;
655 ldtent->HighWord.Bits.Pres = 1;
656 ldtent->HighWord.Bits.Granularity = 1;
657 ldtent->HighWord.Bits.Default_Big = 1;
658 ldtent->HighWord.Bits.Type = 0x12;
659 /* it has to be one of the system GDT selectors */
660 if (sel == (__get_ds() & ~3)) return TRUE;
661 if (sel == (__get_ss() & ~3)) return TRUE;
662 if (sel == (__get_cs() & ~3))
664 ldtent->HighWord.Bits.Type |= 8; /* code segment */
665 return TRUE;
667 SetLastError( ERROR_NOACCESS );
668 return FALSE;
671 SERVER_START_REQ( get_selector_entry )
673 req->handle = hthread;
674 req->entry = sel >> __AHSHIFT;
675 if ((ret = !wine_server_call_err( req )))
677 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
679 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
680 ret = FALSE;
682 else
684 wine_ldt_set_base( ldtent, (void *)reply->base );
685 wine_ldt_set_limit( ldtent, reply->limit );
686 wine_ldt_set_flags( ldtent, reply->flags );
690 SERVER_END_REQ;
691 return ret;
692 #else
693 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
694 return FALSE;
695 #endif
699 /**********************************************************************
700 * SMapLS* (KERNEL32)
701 * These functions map linear pointers at [EBP+xxx] to segmented pointers
702 * and return them.
703 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
704 * unravel them at SUnMapLS. We just store the segmented pointer there.
706 static void
707 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
708 DWORD val,ptr;
710 val =*(DWORD*)(context->Ebp + argoff);
711 if (val<0x10000) {
712 ptr=val;
713 *(DWORD*)(context->Ebp + argoff) = 0;
714 } else {
715 ptr = MapLS((LPVOID)val);
716 *(DWORD*)(context->Ebp + argoff) = ptr;
718 context->Eax = ptr;
721 /***********************************************************************
722 * SMapLS_IP_EBP_8 (KERNEL32.@)
724 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
726 /***********************************************************************
727 * SMapLS_IP_EBP_12 (KERNEL32.@)
729 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
731 /***********************************************************************
732 * SMapLS_IP_EBP_16 (KERNEL32.@)
734 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
736 /***********************************************************************
737 * SMapLS_IP_EBP_20 (KERNEL32.@)
739 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
741 /***********************************************************************
742 * SMapLS_IP_EBP_24 (KERNEL32.@)
744 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
746 /***********************************************************************
747 * SMapLS_IP_EBP_28 (KERNEL32.@)
749 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
751 /***********************************************************************
752 * SMapLS_IP_EBP_32 (KERNEL32.@)
754 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
756 /***********************************************************************
757 * SMapLS_IP_EBP_36 (KERNEL32.@)
759 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
761 /***********************************************************************
762 * SMapLS_IP_EBP_40 (KERNEL32.@)
764 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
766 /***********************************************************************
767 * SMapLS (KERNEL32.@)
769 void WINAPI SMapLS( CONTEXT86 *context )
771 if (HIWORD(context->Eax))
773 context->Eax = MapLS( (LPVOID)context->Eax );
774 context->Edx = context->Eax;
775 } else {
776 context->Edx = 0;
780 /***********************************************************************
781 * SUnMapLS (KERNEL32.@)
784 void WINAPI SUnMapLS( CONTEXT86 *context )
786 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
789 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
791 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
792 if (*ptr)
794 UnMapLS( *ptr );
795 *ptr = 0;
799 /***********************************************************************
800 * SUnMapLS_IP_EBP_8 (KERNEL32.@)
802 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
804 /***********************************************************************
805 * SUnMapLS_IP_EBP_12 (KERNEL32.@)
807 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
809 /***********************************************************************
810 * SUnMapLS_IP_EBP_16 (KERNEL32.@)
812 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
814 /***********************************************************************
815 * SUnMapLS_IP_EBP_20 (KERNEL32.@)
817 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
819 /***********************************************************************
820 * SUnMapLS_IP_EBP_24 (KERNEL32.@)
822 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
824 /***********************************************************************
825 * SUnMapLS_IP_EBP_28 (KERNEL32.@)
827 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
829 /***********************************************************************
830 * SUnMapLS_IP_EBP_32 (KERNEL32.@)
832 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
834 /***********************************************************************
835 * SUnMapLS_IP_EBP_36 (KERNEL32.@)
837 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
839 /***********************************************************************
840 * SUnMapLS_IP_EBP_40 (KERNEL32.@)
842 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
844 /**********************************************************************
845 * AllocMappedBuffer (KERNEL32.38)
847 * This is a undocumented KERNEL32 function that
848 * SMapLS's a GlobalAlloc'ed buffer.
850 * Input: EDI register: size of buffer to allocate
851 * Output: EDI register: pointer to buffer
853 * Note: The buffer is preceded by 8 bytes:
854 * ...
855 * edi+0 buffer
856 * edi-4 SEGPTR to buffer
857 * edi-8 some magic Win95 needs for SUnMapLS
858 * (we use it for the memory handle)
860 * The SEGPTR is used by the caller!
863 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
865 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
866 DWORD *buffer = (DWORD *)GlobalLock(handle);
867 SEGPTR ptr = 0;
869 if (buffer)
870 if (!(ptr = MapLS(buffer + 2)))
872 GlobalUnlock(handle);
873 GlobalFree(handle);
876 if (!ptr)
877 context->Eax = context->Edi = 0;
878 else
880 buffer[0] = handle;
881 buffer[1] = ptr;
883 context->Eax = (DWORD) ptr;
884 context->Edi = (DWORD)(buffer + 2);
888 /**********************************************************************
889 * FreeMappedBuffer (KERNEL32.39)
891 * Free a buffer allocated by AllocMappedBuffer
893 * Input: EDI register: pointer to buffer
896 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
898 if (context->Edi)
900 DWORD *buffer = (DWORD *)context->Edi - 2;
902 UnMapLS(buffer[1]);
904 GlobalUnlock(buffer[0]);
905 GlobalFree(buffer[0]);
909 #ifdef __i386__
910 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
911 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
912 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
913 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
914 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
915 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
916 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
917 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
918 #endif