Moved UTSelectorOffsetToLinear and UTLinearToSelectorOffset to
[wine/multimedia.git] / memory / selector.c
blob7f48f83c859e81bff24632421f783295bae4a781
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 "ldt.h"
13 #include "miscemu.h"
14 #include "selectors.h"
15 #include "stackframe.h"
16 #include "process.h"
17 #include "server.h"
18 #include "debugtools.h"
19 #include "toolhelp.h"
21 DEFAULT_DEBUG_CHANNEL(selector);
23 #define LDT_SIZE 8192
25 /* get the number of selectors needed to cover up to the selector limit */
26 inline static WORD get_sel_count( WORD sel )
28 return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
31 /***********************************************************************
32 * SELECTOR_AllocArray
34 * Allocate a selector array without setting the LDT entries
36 static WORD SELECTOR_AllocArray( WORD count )
38 WORD i, sel, size = 0;
40 if (!count) return 0;
41 for (i = FIRST_LDT_ENTRY_TO_ALLOC; i < LDT_SIZE; i++)
43 if (wine_ldt_copy.flags[i] & WINE_LDT_FLAGS_ALLOCATED) size = 0;
44 else if (++size >= count) break;
46 if (i == LDT_SIZE) return 0;
47 sel = i - size + 1;
49 /* mark selectors as allocated */
50 for (i = 0; i < count; i++) wine_ldt_copy.flags[sel + i] |= WINE_LDT_FLAGS_ALLOCATED;
52 return (sel << __AHSHIFT) | 7;
56 /***********************************************************************
57 * AllocSelectorArray (KERNEL.206)
59 WORD WINAPI AllocSelectorArray16( WORD count )
61 WORD i, sel = SELECTOR_AllocArray( count );
63 if (sel)
65 LDT_ENTRY entry;
66 wine_ldt_set_base( &entry, 0 );
67 wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
68 wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
69 for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
71 return sel;
75 /***********************************************************************
76 * AllocSelector (KERNEL.175)
78 WORD WINAPI AllocSelector16( WORD sel )
80 WORD newsel, count, i;
82 count = sel ? get_sel_count(sel) : 1;
83 newsel = SELECTOR_AllocArray( count );
84 TRACE("(%04x): returning %04x\n", sel, newsel );
85 if (!newsel) return 0;
86 if (!sel) return newsel; /* nothing to copy */
87 for (i = 0; i < count; i++)
89 LDT_ENTRY entry;
90 wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
91 wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
93 return newsel;
97 /***********************************************************************
98 * FreeSelector (KERNEL.176)
100 WORD WINAPI FreeSelector16( WORD sel )
102 LDT_ENTRY entry;
104 if (IS_SELECTOR_FREE(sel)) return sel; /* error */
106 #ifdef __i386__
107 /* Check if we are freeing current %fs or %gs selector */
108 if (!((__get_fs() ^ sel) & ~7))
110 WARN("Freeing %%fs selector (%04x), not good.\n", __get_fs() );
111 __set_fs( 0 );
113 if (!((__get_gs() ^ sel) & ~7)) __set_gs( 0 );
114 #endif /* __i386__ */
116 memset( &entry, 0, sizeof(entry) ); /* clear the LDT entries */
117 wine_ldt_set_entry( sel, &entry );
118 wine_ldt_copy.flags[sel >> __AHSHIFT] &= ~WINE_LDT_FLAGS_ALLOCATED;
119 return 0;
123 /***********************************************************************
124 * SELECTOR_SetEntries
126 * Set the LDT entries for an array of selectors.
128 static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
130 LDT_ENTRY entry;
131 WORD i, count;
133 wine_ldt_set_base( &entry, base );
134 wine_ldt_set_limit( &entry, size - 1 );
135 wine_ldt_set_flags( &entry, flags );
136 /* Make sure base and limit are not 0 together if the size is not 0 */
137 if (!base && size == 1) wine_ldt_set_limit( &entry, 1 );
138 count = (size + 0xffff) / 0x10000;
139 for (i = 0; i < count; i++)
141 wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
142 wine_ldt_set_base( &entry, wine_ldt_get_base(&entry) + 0x10000 );
143 wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 );
148 /***********************************************************************
149 * SELECTOR_AllocBlock
151 * Allocate selectors for a block of linear memory.
153 WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
155 WORD sel, count;
157 if (!size) return 0;
158 count = (size + 0xffff) / 0x10000;
159 sel = SELECTOR_AllocArray( count );
160 if (sel) SELECTOR_SetEntries( sel, base, size, flags );
161 return sel;
165 /***********************************************************************
166 * SELECTOR_FreeBlock
168 * Free a block of selectors.
170 void SELECTOR_FreeBlock( WORD sel )
172 WORD i, count = get_sel_count( sel );
174 TRACE("(%04x,%d)\n", sel, count );
175 for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
179 /***********************************************************************
180 * SELECTOR_ReallocBlock
182 * Change the size of a block of selectors.
184 WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
186 LDT_ENTRY entry;
187 WORD i, oldcount, newcount;
189 if (!size) size = 1;
190 oldcount = get_sel_count( sel );
191 newcount = (size + 0xffff) >> 16;
192 wine_ldt_get_entry( sel, &entry );
194 if (oldcount < newcount) /* We need to add selectors */
196 WORD index = sel >> __AHSHIFT;
197 /* Check if the next selectors are free */
198 if (index + newcount > LDT_SIZE) i = oldcount;
199 else
200 for (i = oldcount; i < newcount; i++)
201 if (wine_ldt_copy.flags[index+i] & WINE_LDT_FLAGS_ALLOCATED) break;
203 if (i < newcount) /* they are not free */
205 SELECTOR_FreeBlock( sel );
206 sel = SELECTOR_AllocArray( newcount );
208 else /* mark the selectors as allocated */
210 for (i = oldcount; i < newcount; i++)
211 wine_ldt_copy.flags[index+i] |= WINE_LDT_FLAGS_ALLOCATED;
214 else if (oldcount > newcount) /* We need to remove selectors */
216 SELECTOR_FreeBlock( sel + (newcount << __AHSHIFT) );
218 if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
219 return sel;
223 /***********************************************************************
224 * PrestoChangoSelector (KERNEL.177)
226 WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
228 LDT_ENTRY entry;
229 wine_ldt_get_entry( selSrc, &entry );
230 /* toggle the executable bit */
231 entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
232 wine_ldt_set_entry( selDst, &entry );
233 return selDst;
237 /***********************************************************************
238 * AllocCStoDSAlias (KERNEL.170)
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, W32S_APPLICATION() ? W32S_OFFSET : 0 );
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,
324 W32S_APP2WINE( base, W32S_APPLICATION() ? W32S_OFFSET : 0 ) );
325 return sel;
327 WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
329 LDT_ENTRY entry;
330 wine_ldt_get_entry( sel, &entry );
331 wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
332 wine_ldt_set_entry( sel, &entry );
333 return sel;
337 /***********************************************************************
338 * GetSelectorLimit (KERNEL.188)
340 DWORD WINAPI GetSelectorLimit16( WORD sel )
342 return wine_ldt_copy.limit[sel >> __AHSHIFT];
346 /***********************************************************************
347 * SetSelectorLimit (KERNEL.189)
349 WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
351 LDT_ENTRY entry;
352 wine_ldt_get_entry( sel, &entry );
353 wine_ldt_set_limit( &entry, limit );
354 wine_ldt_set_entry( sel, &entry );
355 return sel;
359 /***********************************************************************
360 * SelectorAccessRights (KERNEL.196)
362 WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
364 LDT_ENTRY entry;
365 wine_ldt_get_entry( sel, &entry );
367 if (op == 0) /* get */
369 return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
371 else /* set */
373 entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
374 entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
375 wine_ldt_set_entry( sel, &entry );
376 return 0;
381 /***********************************************************************
382 * IsBadCodePtr16 (KERNEL.336)
384 BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
386 WORD sel;
387 LDT_ENTRY entry;
389 sel = SELECTOROF(lpfn);
390 if (!sel) return TRUE;
391 if (IS_SELECTOR_FREE(sel)) return TRUE;
392 wine_ldt_get_entry( sel, &entry );
393 /* check for code segment, ignoring conforming, read-only and accessed bits */
394 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
395 if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
396 return FALSE;
400 /***********************************************************************
401 * IsBadStringPtr16 (KERNEL.337)
403 BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
405 WORD sel;
406 LDT_ENTRY entry;
408 sel = SELECTOROF(ptr);
409 if (!sel) return TRUE;
410 if (IS_SELECTOR_FREE(sel)) return TRUE;
411 wine_ldt_get_entry( sel, &entry );
412 /* check for data or readable code segment */
413 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
414 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
415 if (strlen(PTR_SEG_TO_LIN(ptr)) < size) size = strlen(PTR_SEG_TO_LIN(ptr)) + 1;
416 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE;
417 return FALSE;
421 /***********************************************************************
422 * IsBadHugeReadPtr16 (KERNEL.346)
424 BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
426 WORD sel;
427 LDT_ENTRY entry;
429 sel = SELECTOROF(ptr);
430 if (!sel) return TRUE;
431 if (IS_SELECTOR_FREE(sel)) return TRUE;
432 wine_ldt_get_entry( sel, &entry );
433 /* check for data or readable code segment */
434 if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */
435 if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */
436 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
437 return FALSE;
441 /***********************************************************************
442 * IsBadHugeWritePtr16 (KERNEL.347)
444 BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
446 WORD sel;
447 LDT_ENTRY entry;
449 sel = SELECTOROF(ptr);
450 if (!sel) return TRUE;
451 if (IS_SELECTOR_FREE(sel)) return TRUE;
452 wine_ldt_get_entry( sel, &entry );
453 /* check for writeable data segment, ignoring expand-down and accessed flags */
454 if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
455 if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
456 return FALSE;
459 /***********************************************************************
460 * IsBadReadPtr16 (KERNEL.334)
462 BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
464 return IsBadHugeReadPtr16( ptr, size );
468 /***********************************************************************
469 * IsBadWritePtr16 (KERNEL.335)
471 BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
473 return IsBadHugeWritePtr16( ptr, size );
477 /***********************************************************************
478 * IsBadFlatReadWritePtr16 (KERNEL.627)
480 BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
482 return bWrite? IsBadHugeWritePtr16( ptr, size )
483 : IsBadHugeReadPtr16( ptr, size );
487 /***********************************************************************
488 * MemoryRead (TOOLHELP.78)
490 DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
492 WORD index = sel >> __AHSHIFT;
494 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
495 if (offset > wine_ldt_copy.limit[index]) return 0;
496 if (offset + count > wine_ldt_copy.limit[index] + 1)
497 count = wine_ldt_copy.limit[index] + 1 - offset;
498 memcpy( buffer, (char *)wine_ldt_copy.base[index] + offset, count );
499 return count;
503 /***********************************************************************
504 * MemoryWrite (TOOLHELP.79)
506 DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
508 WORD index = sel >> __AHSHIFT;
510 if (!(wine_ldt_copy.flags[index] & WINE_LDT_FLAGS_ALLOCATED)) return 0;
511 if (offset > wine_ldt_copy.limit[index]) return 0;
512 if (offset + count > wine_ldt_copy.limit[index] + 1)
513 count = wine_ldt_copy.limit[index] + 1 - offset;
514 memcpy( (char *)wine_ldt_copy.base[index] + offset, buffer, count );
515 return count;
518 /************************************* Win95 pointer mapping functions *
522 /***********************************************************************
523 * MapSL (KERNEL32.523)
525 * Maps fixed segmented pointer to linear.
527 LPVOID WINAPI MapSL( SEGPTR sptr )
529 return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
532 /***********************************************************************
533 * MapSLFix (KERNEL32.524)
535 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
536 * unexpected linear address change when GlobalCompact() shuffles
537 * moveable blocks.
540 LPVOID WINAPI MapSLFix( SEGPTR sptr )
542 return (LPVOID)PTR_SEG_TO_LIN(sptr);
545 /***********************************************************************
546 * UnMapSLFixArray (KERNEL32.701)
549 void WINAPI UnMapSLFixArray( SEGPTR sptr[], INT length, CONTEXT86 *context )
551 /* Must not change EAX, hence defined as 'register' function */
554 /***********************************************************************
555 * MapLS (KERNEL32.522)
557 * Maps linear pointer to segmented.
559 SEGPTR WINAPI MapLS( LPVOID ptr )
561 if (!HIWORD(ptr))
562 return (SEGPTR)ptr;
563 else
565 WORD sel = SELECTOR_AllocBlock( ptr, 0x10000, WINE_LDT_FLAGS_DATA );
566 return PTR_SEG_OFF_TO_SEGPTR( sel, 0 );
571 /***********************************************************************
572 * UnMapLS (KERNEL32.700)
574 * Free mapped selector.
576 void WINAPI UnMapLS( SEGPTR sptr )
578 if (SELECTOROF(sptr)) FreeSelector16( SELECTOROF(sptr) );
581 /***********************************************************************
582 * GetThreadSelectorEntry (KERNEL32)
584 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent)
586 #ifdef __i386__
587 BOOL ret;
589 if (!(sel & 4)) /* GDT selector */
591 sel &= ~3; /* ignore RPL */
592 if (!sel) /* null selector */
594 memset( ldtent, 0, sizeof(*ldtent) );
595 return TRUE;
597 ldtent->BaseLow = 0;
598 ldtent->HighWord.Bits.BaseMid = 0;
599 ldtent->HighWord.Bits.BaseHi = 0;
600 ldtent->LimitLow = 0xffff;
601 ldtent->HighWord.Bits.LimitHi = 0xf;
602 ldtent->HighWord.Bits.Dpl = 3;
603 ldtent->HighWord.Bits.Sys = 0;
604 ldtent->HighWord.Bits.Pres = 1;
605 ldtent->HighWord.Bits.Granularity = 1;
606 ldtent->HighWord.Bits.Default_Big = 1;
607 ldtent->HighWord.Bits.Type = 0x12;
608 /* it has to be one of the system GDT selectors */
609 if (sel == (__get_ds() & ~3)) return TRUE;
610 if (sel == (__get_ss() & ~3)) return TRUE;
611 if (sel == (__get_cs() & ~3))
613 ldtent->HighWord.Bits.Type |= 8; /* code segment */
614 return TRUE;
616 SetLastError( ERROR_NOACCESS );
617 return FALSE;
620 SERVER_START_REQ
622 struct get_selector_entry_request *req = server_alloc_req( sizeof(*req), 0 );
624 req->handle = hthread;
625 req->entry = sel >> __AHSHIFT;
626 if ((ret = !server_call( REQ_GET_SELECTOR_ENTRY )))
628 if (!(req->flags & WINE_LDT_FLAGS_ALLOCATED))
630 SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */
631 ret = FALSE;
633 else
635 wine_ldt_set_base( ldtent, (void *)req->base );
636 wine_ldt_set_limit( ldtent, req->limit );
637 wine_ldt_set_flags( ldtent, req->flags );
641 SERVER_END_REQ;
642 return ret;
643 #else
644 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
645 return FALSE;
646 #endif
650 /**********************************************************************
651 * SMapLS* (KERNEL32)
652 * These functions map linear pointers at [EBP+xxx] to segmented pointers
653 * and return them.
654 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
655 * unravel them at SUnMapLS. We just store the segmented pointer there.
657 static void
658 x_SMapLS_IP_EBP_x(CONTEXT86 *context,int argoff) {
659 DWORD val,ptr;
661 val =*(DWORD*)(context->Ebp + argoff);
662 if (val<0x10000) {
663 ptr=val;
664 *(DWORD*)(context->Ebp + argoff) = 0;
665 } else {
666 ptr = MapLS((LPVOID)val);
667 *(DWORD*)(context->Ebp + argoff) = ptr;
669 context->Eax = ptr;
672 /***********************************************************************
673 * SMapLS_IP_EBP_8 (KERNEL32.601)
675 void WINAPI SMapLS_IP_EBP_8 (CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context, 8);}
677 /***********************************************************************
678 * SMapLS_IP_EBP_12 (KERNEL32.593)
680 void WINAPI SMapLS_IP_EBP_12(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,12);}
682 /***********************************************************************
683 * SMapLS_IP_EBP_16 (KERNEL32.594)
685 void WINAPI SMapLS_IP_EBP_16(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,16);}
687 /***********************************************************************
688 * SMapLS_IP_EBP_20 (KERNEL32.595)
690 void WINAPI SMapLS_IP_EBP_20(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,20);}
692 /***********************************************************************
693 * SMapLS_IP_EBP_24 (KERNEL32.596)
695 void WINAPI SMapLS_IP_EBP_24(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,24);}
697 /***********************************************************************
698 * SMapLS_IP_EBP_28 (KERNEL32.597)
700 void WINAPI SMapLS_IP_EBP_28(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,28);}
702 /***********************************************************************
703 * SMapLS_IP_EBP_32 (KERNEL32.598)
705 void WINAPI SMapLS_IP_EBP_32(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,32);}
707 /***********************************************************************
708 * SMapLS_IP_EBP_36 (KERNEL32.599)
710 void WINAPI SMapLS_IP_EBP_36(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,36);}
712 /***********************************************************************
713 * SMapLS_IP_EBP_40 (KERNEL32.600)
715 void WINAPI SMapLS_IP_EBP_40(CONTEXT86 *context) {x_SMapLS_IP_EBP_x(context,40);}
717 /***********************************************************************
718 * SMapLS (KERNEL32.592)
720 void WINAPI SMapLS( CONTEXT86 *context )
722 if (HIWORD(context->Eax))
724 context->Eax = MapLS( (LPVOID)context->Eax );
725 context->Edx = context->Eax;
726 } else {
727 context->Edx = 0;
731 /***********************************************************************
732 * SUnMapLS (KERNEL32.602)
735 void WINAPI SUnMapLS( CONTEXT86 *context )
737 if (HIWORD(context->Eax)) UnMapLS( (SEGPTR)context->Eax );
740 inline static void x_SUnMapLS_IP_EBP_x(CONTEXT86 *context,int argoff)
742 SEGPTR *ptr = (SEGPTR *)(context->Ebp + argoff);
743 if (*ptr)
745 UnMapLS( *ptr );
746 *ptr = 0;
750 /***********************************************************************
751 * SUnMapLS_IP_EBP_8 (KERNEL32.611)
753 void WINAPI SUnMapLS_IP_EBP_8 (CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context, 8); }
755 /***********************************************************************
756 * SUnMapLS_IP_EBP_12 (KERNEL32.603)
758 void WINAPI SUnMapLS_IP_EBP_12(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,12); }
760 /***********************************************************************
761 * SUnMapLS_IP_EBP_16 (KERNEL32.604)
763 void WINAPI SUnMapLS_IP_EBP_16(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,16); }
765 /***********************************************************************
766 * SUnMapLS_IP_EBP_20 (KERNEL32.605)
768 void WINAPI SUnMapLS_IP_EBP_20(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,20); }
770 /***********************************************************************
771 * SUnMapLS_IP_EBP_24 (KERNEL32.606)
773 void WINAPI SUnMapLS_IP_EBP_24(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,24); }
775 /***********************************************************************
776 * SUnMapLS_IP_EBP_28 (KERNEL32.607)
778 void WINAPI SUnMapLS_IP_EBP_28(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,28); }
780 /***********************************************************************
781 * SUnMapLS_IP_EBP_32 (KERNEL32.608)
783 void WINAPI SUnMapLS_IP_EBP_32(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,32); }
785 /***********************************************************************
786 * SUnMapLS_IP_EBP_36 (KERNEL32.609)
788 void WINAPI SUnMapLS_IP_EBP_36(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,36); }
790 /***********************************************************************
791 * SUnMapLS_IP_EBP_40 (KERNEL32.610)
793 void WINAPI SUnMapLS_IP_EBP_40(CONTEXT86 *context) { x_SUnMapLS_IP_EBP_x(context,40); }
795 /**********************************************************************
796 * AllocMappedBuffer (KERNEL32.38)
798 * This is a undocumented KERNEL32 function that
799 * SMapLS's a GlobalAlloc'ed buffer.
801 * Input: EDI register: size of buffer to allocate
802 * Output: EDI register: pointer to buffer
804 * Note: The buffer is preceeded by 8 bytes:
805 * ...
806 * edi+0 buffer
807 * edi-4 SEGPTR to buffer
808 * edi-8 some magic Win95 needs for SUnMapLS
809 * (we use it for the memory handle)
811 * The SEGPTR is used by the caller!
814 void WINAPI AllocMappedBuffer( CONTEXT86 *context )
816 HGLOBAL handle = GlobalAlloc(0, context->Edi + 8);
817 DWORD *buffer = (DWORD *)GlobalLock(handle);
818 SEGPTR ptr = 0;
820 if (buffer)
821 if (!(ptr = MapLS(buffer + 2)))
823 GlobalUnlock(handle);
824 GlobalFree(handle);
827 if (!ptr)
828 context->Eax = context->Edi = 0;
829 else
831 buffer[0] = handle;
832 buffer[1] = ptr;
834 context->Eax = (DWORD) ptr;
835 context->Edi = (DWORD)(buffer + 2);
839 /**********************************************************************
840 * FreeMappedBuffer (KERNEL32.39)
842 * Free a buffer allocated by AllocMappedBuffer
844 * Input: EDI register: pointer to buffer
847 void WINAPI FreeMappedBuffer( CONTEXT86 *context )
849 if (context->Edi)
851 DWORD *buffer = (DWORD *)context->Edi - 2;
853 UnMapLS(buffer[1]);
855 GlobalUnlock(buffer[0]);
856 GlobalFree(buffer[0]);
860 #ifdef __i386__
861 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" )
862 __ASM_GLOBAL_FUNC( __get_ds, "movw %ds,%ax\n\tret" )
863 __ASM_GLOBAL_FUNC( __get_es, "movw %es,%ax\n\tret" )
864 __ASM_GLOBAL_FUNC( __get_fs, "movw %fs,%ax\n\tret" )
865 __ASM_GLOBAL_FUNC( __get_gs, "movw %gs,%ax\n\tret" )
866 __ASM_GLOBAL_FUNC( __get_ss, "movw %ss,%ax\n\tret" )
867 __ASM_GLOBAL_FUNC( __set_fs, "movl 4(%esp),%eax\n\tmovw %ax,%fs\n\tret" )
868 __ASM_GLOBAL_FUNC( __set_gs, "movl 4(%esp),%eax\n\tmovw %ax,%gs\n\tret" )
869 #endif