Prevent multiple definitions caused by MSVCRT headers.
[wine.git] / memory / selector.c
blob12b449a77cc6a2a5bd7420f33cd4caed272221c4
1 /*
2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <string.h>
9 #include "config.h"
10 #include "winerror.h"
11 #include "wine/winbase16.h"
12 #include "miscemu.h"
13 #include "selectors.h"
14 #include "stackframe.h"
15 #include "server.h"
16 #include "debugtools.h"
17 #include "toolhelp.h"
19 DEFAULT_DEBUG_CHANNEL(selector);
21 #define LDT_SIZE 8192
23 /* get the number of selectors needed to cover up to the selector limit */
24 inline static WORD get_sel_count( WORD sel )
26 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
29 /***********************************************************************
30 * SELECTOR_AllocArray
32 * Allocate a selector array without setting the LDT entries
34 static WORD SELECTOR_AllocArray( WORD count )
36 WORD i, sel, size = 0;
38 if (!count) return 0;
39 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
41 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
42 else if (++size >= count) break;
44 if (i == LDT_SIZE) return 0;
45 sel = i - size + 1;
47 /* mark selectors as allocated */
48 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
50 return (sel << __AHSHIFT) | 7;
54 /***********************************************************************
55 * AllocSelectorArray (KERNEL.206)
57 WORD WINAPI AllocSelectorArray16( WORD count )
59 WORD i, sel = SELECTOR_AllocArray( count );
61 if (sel)
63 LDT_ENTRY entry;
64 wine_ldt_set_base( &entry, 0 );
65 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
66 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
67 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
69 return sel;
73 /***********************************************************************
74 * AllocSelector (KERNEL.175)
76 WORD WINAPI AllocSelector16( WORD sel )
78 WORD newsel, count, i;
80 count = sel ? get_sel_count(sel) : 1;
81 newsel = SELECTOR_AllocArray( count );
82 TRACE("(%04x): returning %04x\n", sel, newsel );
83 if (!newsel) return 0;
84 if (!sel) return newsel; /* nothing to copy */
85 for (i = 0; i < count; i++)
87 LDT_ENTRY entry;
88 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
89 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
91 return newsel;
95 /***********************************************************************
96 * FreeSelector (KERNEL.176)
98 WORD WINAPI FreeSelector16( WORD sel )
100 LDT_ENTRY entry;
102 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
104 #ifdef __i386__
105 /* Check if we are freeing current %fs or %gs selector */
106 if (!((__get_fs() ^ sel) & ~7))
108 WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
109 __set_fs( 0 );
111 if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
112 #endif /* __i386__ */
114 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
115 wine_ldt_set_entry( sel, &entry );
116 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
117 return 0;
121 /***********************************************************************
122 * SELECTOR_SetEntries
124 * Set the LDT entries for an array of selectors.
126 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
128 LDT_ENTRY entry;
129 WORD i, count;
131 wine_ldt_set_base( &entry, base );
132 wine_ldt_set_limit( &entry, size - 1 );
133 wine_ldt_set_flags( &entry, flags );
134 /* Make sure base and limit are not 0 together if the size is not 0 */
135 if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
136 count = (size + 0xffff) / 0x10000;
137 for (i = 0; i < count; i++)
139 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
140 wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
141 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
146 /***********************************************************************
147 * SELECTOR_AllocBlock
149 * Allocate selectors for a block of linear memory.
151 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
153 WORD sel, count;
155 if (!size) return 0;
156 count = (size + 0xffff) / 0x10000;
157 sel = SELECTOR_AllocArray( count );
158 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
159 return sel;
163 /***********************************************************************
164 * SELECTOR_FreeBlock
166 * Free a block of selectors.
168 void SELECTOR_FreeBlock( WORD sel )
170 WORD i, count = get_sel_count( sel );
172 TRACE("(%04x,%d)\n", sel, count );
173 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
177 /***********************************************************************
178 * SELECTOR_ReallocBlock
180 * Change the size of a block of selectors.
182 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
184 LDT_ENTRY entry;
185 WORD i, oldcount, newcount;
187 if (!size) size = 1;
188 oldcount = get_sel_count( sel );
189 newcount = (size + 0xffff) >> 16;
190 wine_ldt_get_entry( sel, &entry );
192 if (oldcount < newcount) /* We need to add selectors */
194 WORD index = sel >> __AHSHIFT;
195 /* Check if the next selectors are free */
196 if (index + newcount > LDT_SIZE) i = oldcount;
197 else
198 for (i = oldcount; i < newcount; i++)
199 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
201 if (i < newcount) /* they are not free */
203 SELECTOR_FreeBlock( sel );
204 sel = SELECTOR_AllocArray( newcount );
206 else /* mark the selectors as allocated */
208 for (i = oldcount; i < newcount; i++)
209 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
212 else if (oldcount > newcount) /* We need to remove selectors */
214 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
216 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
217 return sel;
221 /***********************************************************************
222 * PrestoChangoSelector (KERNEL.177)
224 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
226 LDT_ENTRY entry;
227 wine_ldt_get_entry( selSrc, &entry );
228 /* toggle the executable bit */
229 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
230 wine_ldt_set_entry( selDst, &entry );
231 return selDst;
235 /***********************************************************************
236 * AllocCStoDSAlias (KERNEL.170)
238 WORD WINAPI AllocCStoDSAlias16( WORD sel )
240 WORD newsel;
241 LDT_ENTRY entry;
243 newsel = SELECTOR_AllocArray( 1 );
244 TRACE("(%04x): returning %04x\n",
245 sel, newsel );
246 if (!newsel) return 0;
247 wine_ldt_get_entry( sel, &entry );
248 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
249 wine_ldt_set_entry( newsel, &entry );
250 return newsel;
254 /***********************************************************************
255 * AllocDStoCSAlias (KERNEL.171)
257 WORD WINAPI AllocDStoCSAlias16( WORD sel )
259 WORD newsel;
260 LDT_ENTRY entry;
262 newsel = SELECTOR_AllocArray( 1 );
263 TRACE("(%04x): returning %04x\n",
264 sel, newsel );
265 if (!newsel) return 0;
266 wine_ldt_get_entry( sel, &entry );
267 entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
268 wine_ldt_set_entry( newsel, &entry );
269 return newsel;
273 /***********************************************************************
274 * LongPtrAdd (KERNEL.180)
276 void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
278 LDT_ENTRY entry;
279 wine_ldt_get_entry( SELECTOROF(ptr), &entry );
280 wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
281 wine_ldt_set_entry( SELECTOROF(ptr), &entry );
285 /***********************************************************************
286 * GetSelectorBase (KERNEL.186)
288 DWORD WINAPI WIN16_GetSelectorBase( WORD sel )
291 * Note: For Win32s processes, the whole linear address space is
292 * shifted by 0x10000 relative to the OS linear address space.
293 * See the comment in msdos/vxd.c.
296 DWORD base = GetSelectorBase( sel );
297 return W32S_WINE2APP( base );
299 DWORD WINAPI GetSelectorBase( WORD sel )
301 void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
303 /* if base points into DOSMEM, assume we have to
304 * return pointer into physical lower 1MB */
306 return DOSMEM_MapLinearToDos( base );
310 /***********************************************************************
311 * SetSelectorBase (KERNEL.187)
313 DWORD WINAPI WIN16_SetSelectorBase( WORD sel, DWORD base )
316 * Note: For Win32s processes, the whole linear address space is
317 * shifted by 0x10000 relative to the OS linear address space.
318 * See the comment in msdos/vxd.c.
321 SetSelectorBase( sel, W32S_APP2WINE( base ) );
322 return sel;
324 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
326 LDT_ENTRY entry;
327 wine_ldt_get_entry( sel, &entry );
328 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
329 wine_ldt_set_entry( sel, &entry );
330 return sel;
334 /***********************************************************************
335 * GetSelectorLimit (KERNEL.188)
337 DWORD WINAPI GetSelectorLimit16( WORD sel )
339 return wine_ldt_copy.limit[sel >> __AHSHIFT];
343 /***********************************************************************
344 * SetSelectorLimit (KERNEL.189)
346 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
348 LDT_ENTRY entry;
349 wine_ldt_get_entry( sel, &entry );
350 wine_ldt_set_limit( &entry, limit );
351 wine_ldt_set_entry( sel, &entry );
352 return sel;
356 /***********************************************************************
357 * SelectorAccessRights (KERNEL.196)
359 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
361 LDT_ENTRY entry;
362 wine_ldt_get_entry( sel, &entry );
364 if (op == 0) /* get */
366 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
368 else /* set */
370 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
371 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
372 wine_ldt_set_entry( sel, &entry );
373 return 0;
378 /***********************************************************************
379 * IsBadCodePtr16 (KERNEL.336)
381 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
383 WORD sel;
384 LDT_ENTRY entry;
386 sel = SELECTOROF(lpfn);
387 if (!sel) return TRUE;
388 if (IS_SELECTOR_FREE(sel)) return TRUE;
389 wine_ldt_get_entry( sel, &entry );
390 /* check for code segment, ignoring conforming, read-only and accessed bits */
391 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
392 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
393 return FALSE;
397 /***********************************************************************
398 * IsBadStringPtr16 (KERNEL.337)
400 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
402 WORD sel;
403 LDT_ENTRY entry;
405 sel = SELECTOROF(ptr);
406 if (!sel) return TRUE;
407 if (IS_SELECTOR_FREE(sel)) return TRUE;
408 wine_ldt_get_entry( sel, &entry );
409 /* check for data or readable code segment */
410 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
411 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
412 if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1;
413 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
414 return FALSE;
418 /***********************************************************************
419 * IsBadHugeReadPtr16 (KERNEL.346)
421 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
423 WORD sel;
424 LDT_ENTRY entry;
426 sel = SELECTOROF(ptr);
427 if (!sel) return TRUE;
428 if (IS_SELECTOR_FREE(sel)) return TRUE;
429 wine_ldt_get_entry( sel, &entry );
430 /* check for data or readable code segment */
431 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
432 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
433 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
434 return FALSE;
438 /***********************************************************************
439 * IsBadHugeWritePtr16 (KERNEL.347)
441 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
443 WORD sel;
444 LDT_ENTRY entry;
446 sel = SELECTOROF(ptr);
447 if (!sel) return TRUE;
448 if (IS_SELECTOR_FREE(sel)) return TRUE;
449 wine_ldt_get_entry( sel, &entry );
450 /* check for writeable data segment, ignoring expand-down and accessed flags */
451 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
452 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
453 return FALSE;
456 /***********************************************************************
457 * IsBadReadPtr16 (KERNEL.334)
459 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
461 return IsBadHugeReadPtr16( ptr, size );
465 /***********************************************************************
466 * IsBadWritePtr16 (KERNEL.335)
468 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
470 return IsBadHugeWritePtr16( ptr, size );
474 /***********************************************************************
475 * IsBadFlatReadWritePtr16 (KERNEL.627)
477 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
479 return bWrite? IsBadHugeWritePtr16( ptr, size )
480 : IsBadHugeReadPtr16( ptr, size );
484 /***********************************************************************
485 * MemoryRead (TOOLHELP.78)
487 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
489 WORD index = sel >> __AHSHIFT;
491 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
492 if (offset > wine_ldt_copy.limit[index]) return 0;
493 if (offset + count > wine_ldt_copy.limit[index] + 1)
494 count = wine_ldt_copy.limit[index] + 1 - offset;
495 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
496 return count;
500 /***********************************************************************
501 * MemoryWrite (TOOLHELP.79)
503 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
505 WORD index = sel >> __AHSHIFT;
507 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
508 if (offset > wine_ldt_copy.limit[index]) return 0;
509 if (offset + count > wine_ldt_copy.limit[index] + 1)
510 count = wine_ldt_copy.limit[index] + 1 - offset;
511 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
512 return count;
515 /************************************* Win95 pointer mapping functions *
519 /***********************************************************************
520 * MapSL (KERNEL32.523)
522 * Maps fixed segmented pointer to linear.
524 LPVOID WINAPI MapSL( SEGPTR sptr )
526 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
529 /***********************************************************************
530 * MapSLFix (KERNEL32.524)
532 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
533 * unexpected linear address change when GlobalCompact() shuffles
534 * moveable blocks.
537 LPVOID WINAPI MapSLFix( SEGPTR sptr )
539 return MapSL(sptr);
542 /***********************************************************************
543 * UnMapSLFixArray (KERNEL32.701)
546 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
548 /* Must not change EAX, hence defined as 'register' function */
551 /***********************************************************************
552 * GetThreadSelectorEntry (KERNEL32)
554 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
556 #ifdef __i386__
557 BOOL ret;
559 if (!(sel & 4)) /* GDT selector */
561 sel &= ~3; /* ignore RPL */
562 if (!sel) /* null selector */
564 memset( ldtent, 0, sizeof(*ldtent) );
565 return TRUE;
567 ldtent->BaseLow = 0;
568 ldtent->HighWord.Bits.BaseMid = 0;
569 ldtent->HighWord.Bits.BaseHi = 0;
570 ldtent->LimitLow = 0xffff;
571 ldtent->HighWord.Bits.LimitHi = 0xf;
572 ldtent->HighWord.Bits.Dpl = 3;
573 ldtent->HighWord.Bits.Sys = 0;
574 ldtent->HighWord.Bits.Pres = 1;
575 ldtent->HighWord.Bits.Granularity = 1;
576 ldtent->HighWord.Bits.Default_Big = 1;
577 ldtent->HighWord.Bits.Type = 0x12;
578 /* it has to be one of the system GDT selectors */
579 if (sel == (__get_ds() & ~3)) return TRUE;
580 if (sel == (__get_ss() & ~3)) return TRUE;
581 if (sel == (__get_cs() & ~3))
583 ldtent->HighWord.Bits.Type |= 8; /* code segment */
584 return TRUE;
586 SetLastError( ERROR_NOACCESS );
587 return FALSE;
590 SERVER_START_REQ( get_selector_entry )
592 req->handle = hthread;
593 req->entry = sel >> __AHSHIFT;
594 if ((ret = !SERVER_CALL_ERR()))
596 if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
598 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
599 ret = FALSE;
601 else
603 wine_ldt_set_base( ldtent, (void *)req->base );
604 wine_ldt_set_limit( ldtent, req->limit );
605 wine_ldt_set_flags( ldtent, req->flags );
609 SERVER_END_REQ;
610 return ret;
611 #else
612 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
613 return FALSE;
614 #endif
618 /**********************************************************************
619 * SMapLS* (KERNEL32)
620 * These functions map linear pointers at [EBP+xxx] to segmented pointers
621 * and return them.
622 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
623 * unravel them at SUnMapLS. We just store the segmented pointer there.
625 static void
626 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
627 DWORD val,ptr;
629 val =*(DWORD*)(context->Ebp + argoff);
630 if (val<0x10000) {
631 ptr=val;
632 *(DWORD*)(context->Ebp + argoff) = 0;
633 } else {
634 ptr = MapLS((LPVOID)val);
635 *(DWORD*)(context->Ebp + argoff) = ptr;
637 context->Eax = ptr;
640 /***********************************************************************
641 * SMapLS_IP_EBP_8 (KERNEL32.601)
643 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
645 /***********************************************************************
646 * SMapLS_IP_EBP_12 (KERNEL32.593)
648 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
650 /***********************************************************************
651 * SMapLS_IP_EBP_16 (KERNEL32.594)
653 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
655 /***********************************************************************
656 * SMapLS_IP_EBP_20 (KERNEL32.595)
658 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
660 /***********************************************************************
661 * SMapLS_IP_EBP_24 (KERNEL32.596)
663 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
665 /***********************************************************************
666 * SMapLS_IP_EBP_28 (KERNEL32.597)
668 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
670 /***********************************************************************
671 * SMapLS_IP_EBP_32 (KERNEL32.598)
673 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
675 /***********************************************************************
676 * SMapLS_IP_EBP_36 (KERNEL32.599)
678 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
680 /***********************************************************************
681 * SMapLS_IP_EBP_40 (KERNEL32.600)
683 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
685 /***********************************************************************
686 * SMapLS (KERNEL32.592)
688 void WINAPI SMapLS( CONTEXT86 *context )
690 if (HIWORD(context->Eax))
692 context->Eax = MapLS( (LPVOID)context->Eax );
693 context->Edx = context->Eax;
694 } else {
695 context->Edx = 0;
699 /***********************************************************************
700 * SUnMapLS (KERNEL32.602)
703 void WINAPI SUnMapLS( CONTEXT86 *context )
705 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
708 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
710 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
711 if (*ptr)
713 UnMapLS( *ptr );
714 *ptr = 0;
718 /***********************************************************************
719 * SUnMapLS_IP_EBP_8 (KERNEL32.611)
721 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
723 /***********************************************************************
724 * SUnMapLS_IP_EBP_12 (KERNEL32.603)
726 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
728 /***********************************************************************
729 * SUnMapLS_IP_EBP_16 (KERNEL32.604)
731 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
733 /***********************************************************************
734 * SUnMapLS_IP_EBP_20 (KERNEL32.605)
736 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
738 /***********************************************************************
739 * SUnMapLS_IP_EBP_24 (KERNEL32.606)
741 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
743 /***********************************************************************
744 * SUnMapLS_IP_EBP_28 (KERNEL32.607)
746 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
748 /***********************************************************************
749 * SUnMapLS_IP_EBP_32 (KERNEL32.608)
751 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
753 /***********************************************************************
754 * SUnMapLS_IP_EBP_36 (KERNEL32.609)
756 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
758 /***********************************************************************
759 * SUnMapLS_IP_EBP_40 (KERNEL32.610)
761 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
763 /**********************************************************************
764 * AllocMappedBuffer (KERNEL32.38)
766 * This is a undocumented KERNEL32 function that
767 * SMapLS's a GlobalAlloc'ed buffer.
769 * Input: EDI register: size of buffer to allocate
770 * Output: EDI register: pointer to buffer
772 * Note: The buffer is preceded by 8 bytes:
773 * ...
774 * edi+0 buffer
775 * edi-4 SEGPTR to buffer
776 * edi-8 some magic Win95 needs for SUnMapLS
777 * (we use it for the memory handle)
779 * The SEGPTR is used by the caller!
782 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
784 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
785 DWORD *buffer = (DWORD *)GlobalLock(handle);
786 SEGPTR ptr = 0;
788 if (buffer)
789 if (!(ptr = MapLS(buffer + 2)))
791 GlobalUnlock(handle);
792 GlobalFree(handle);
795 if (!ptr)
796 context->Eax = context->Edi = 0;
797 else
799 buffer[0] = handle;
800 buffer[1] = ptr;
802 context->Eax = (DWORD) ptr;
803 context->Edi = (DWORD)(buffer + 2);
807 /**********************************************************************
808 * FreeMappedBuffer (KERNEL32.39)
810 * Free a buffer allocated by AllocMappedBuffer
812 * Input: EDI register: pointer to buffer
815 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
817 if (context->Edi)
819 DWORD *buffer = (DWORD *)context->Edi - 2;
821 UnMapLS(buffer[1]);
823 GlobalUnlock(buffer[0]);
824 GlobalFree(buffer[0]);
828 #ifdef __i386__
829 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
830 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
831 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
832 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
833 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
834 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
835 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
836 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
837 #endif