selector.c 19.6 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Selector manipulation functions
 *
 * Copyright 1995 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include "config.h"
22
#include "wine/port.h"
23

Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <string.h>
25

26
#include "wine/winbase16.h"
27
#include "wine/server.h"
28
#include "wine/debug.h"
29
#include "kernel_private.h"
Patrik Stridvall's avatar
Patrik Stridvall committed
30
#include "toolhelp.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
31

32
WINE_DEFAULT_DEBUG_CHANNEL(selector);
33

34 35 36
#define LDT_SIZE 8192

/* get the number of selectors needed to cover up to the selector limit */
37
static inline WORD get_sel_count( WORD sel )
38 39 40
{
    return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
41

42 43 44 45 46 47

/***********************************************************************
 *           AllocSelectorArray   (KERNEL.206)
 */
WORD WINAPI AllocSelectorArray16( WORD count )
{
48
    WORD i, sel = wine_ldt_alloc_entries( count );
49 50

    if (sel)
Alexandre Julliard's avatar
Alexandre Julliard committed
51
    {
52 53 54 55 56
        LDT_ENTRY entry;
        wine_ldt_set_base( &entry, 0 );
        wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */
        wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA );
        for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
57
    }
58
    return sel;
Alexandre Julliard's avatar
Alexandre Julliard committed
59 60 61 62 63 64
}


/***********************************************************************
 *           AllocSelector   (KERNEL.175)
 */
65
WORD WINAPI AllocSelector16( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
66 67 68
{
    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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
72 73 74 75
    if (!newsel) return 0;
    if (!sel) return newsel;  /* nothing to copy */
    for (i = 0; i < count; i++)
    {
76 77 78
        LDT_ENTRY entry;
        wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry );
        wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
79 80 81 82 83 84 85 86
    }
    return newsel;
}


/***********************************************************************
 *           FreeSelector   (KERNEL.176)
 */
87
WORD WINAPI FreeSelector16( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
88
{
89
    LDT_ENTRY entry;
90

91 92
    wine_ldt_get_entry( sel, &entry );
    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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101 102 103 104 105 106 107
    return 0;
}


/***********************************************************************
 *           SELECTOR_SetEntries
 *
 * Set the LDT entries for an array of selectors.
 */
108
static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
109
{
110
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112
    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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
116 117 118
    count = (size + 0xffff) / 0x10000;
    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 );
Alexandre Julliard's avatar
Alexandre Julliard committed
123 124 125 126 127 128 129 130 131
    }
}


/***********************************************************************
 *           SELECTOR_AllocBlock
 *
 * Allocate selectors for a block of linear memory.
 */
132
WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
133 134 135 136 137
{
    WORD sel, count;

    if (!size) return 0;
    count = (size + 0xffff) / 0x10000;
138
    sel = wine_ldt_alloc_entries( count );
139
    if (sel) SELECTOR_SetEntries( sel, base, size, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
140 141 142 143
    return sel;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
144 145 146 147 148
/***********************************************************************
 *           SELECTOR_FreeBlock
 *
 * Free a block of selectors.
 */
149
void SELECTOR_FreeBlock( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
150
{
151
    WORD i, count = get_sel_count( sel );
Alexandre Julliard's avatar
Alexandre Julliard committed
152

153
    TRACE("(%04x,%d)\n", sel, count );
154
    for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) );
Alexandre Julliard's avatar
Alexandre Julliard committed
155 156 157
}


Alexandre Julliard's avatar
Alexandre Julliard committed
158 159 160 161 162
/***********************************************************************
 *           SELECTOR_ReallocBlock
 *
 * Change the size of a block of selectors.
 */
163
WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size )
Alexandre Julliard's avatar
Alexandre Julliard committed
164
{
165
    LDT_ENTRY entry;
166
    int oldcount, newcount;
Alexandre Julliard's avatar
Alexandre Julliard committed
167

Alexandre Julliard's avatar
Alexandre Julliard committed
168
    if (!size) size = 1;
169
    wine_ldt_get_entry( sel, &entry );
170 171
    oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1;
    newcount = (size + 0xffff) >> 16;
Alexandre Julliard's avatar
Alexandre Julliard committed
172

173
    sel = wine_ldt_realloc_entries( sel, oldcount, newcount );
174
    if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) );
Alexandre Julliard's avatar
Alexandre Julliard committed
175 176 177 178 179 180 181
    return sel;
}


/***********************************************************************
 *           PrestoChangoSelector   (KERNEL.177)
 */
182
WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst )
Alexandre Julliard's avatar
Alexandre Julliard committed
183
{
184 185 186 187 188
    LDT_ENTRY entry;
    wine_ldt_get_entry( selSrc, &entry );
    /* toggle the executable bit */
    entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA);
    wine_ldt_set_entry( selDst, &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
189 190 191 192 193
    return selDst;
}


/***********************************************************************
194
 *           AllocCStoDSAlias   (KERNEL.170)
Patrik Stridvall's avatar
Patrik Stridvall committed
195
 *           AllocAlias         (KERNEL.172)
Alexandre Julliard's avatar
Alexandre Julliard committed
196
 */
197
WORD WINAPI AllocCStoDSAlias16( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
198 199
{
    WORD newsel;
200
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
201

202
    newsel = wine_ldt_alloc_entries( 1 );
203
    TRACE("(%04x): returning %04x\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
204 205
                      sel, newsel );
    if (!newsel) return 0;
206 207 208
    wine_ldt_get_entry( sel, &entry );
    entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA;
    wine_ldt_set_entry( newsel, &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
209 210 211 212 213 214 215
    return newsel;
}


/***********************************************************************
 *           AllocDStoCSAlias   (KERNEL.171)
 */
216
WORD WINAPI AllocDStoCSAlias16( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
217 218
{
    WORD newsel;
219
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
220

221
    newsel = wine_ldt_alloc_entries( 1 );
222
    TRACE("(%04x): returning %04x\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
223 224
                      sel, newsel );
    if (!newsel) return 0;
225 226 227
    wine_ldt_get_entry( sel, &entry );
    entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE;
    wine_ldt_set_entry( newsel, &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
228 229 230 231 232 233 234
    return newsel;
}


/***********************************************************************
 *           LongPtrAdd   (KERNEL.180)
 */
235
void WINAPI LongPtrAdd16( DWORD ptr, DWORD add )
Alexandre Julliard's avatar
Alexandre Julliard committed
236
{
237 238 239 240
    LDT_ENTRY entry;
    wine_ldt_get_entry( SELECTOROF(ptr), &entry );
    wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add );
    wine_ldt_set_entry( SELECTOROF(ptr), &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
241 242 243 244
}


/***********************************************************************
245
 *             GetSelectorBase   (KERNEL.186)
246
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
247
DWORD WINAPI GetSelectorBase( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
248
{
249
    void *base = wine_ldt_copy.base[sel >> __AHSHIFT];
Alexandre Julliard's avatar
Alexandre Julliard committed
250 251

    /* if base points into DOSMEM, assume we have to
Alexandre Julliard's avatar
Alexandre Julliard committed
252 253
     * return pointer into physical lower 1MB */

254
    return DOSMEM_MapLinearToDos( base );
Alexandre Julliard's avatar
Alexandre Julliard committed
255 256 257 258
}


/***********************************************************************
259
 *             SetSelectorBase   (KERNEL.187)
260
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
261
WORD WINAPI SetSelectorBase( WORD sel, DWORD base )
Alexandre Julliard's avatar
Alexandre Julliard committed
262
{
263 264 265 266
    LDT_ENTRY entry;
    wine_ldt_get_entry( sel, &entry );
    wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) );
    wine_ldt_set_entry( sel, &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
267 268 269 270 271 272 273
    return sel;
}


/***********************************************************************
 *           GetSelectorLimit   (KERNEL.188)
 */
274
DWORD WINAPI GetSelectorLimit16( WORD sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
275
{
276
    return wine_ldt_copy.limit[sel >> __AHSHIFT];
Alexandre Julliard's avatar
Alexandre Julliard committed
277 278 279 280 281 282
}


/***********************************************************************
 *           SetSelectorLimit   (KERNEL.189)
 */
283
WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit )
Alexandre Julliard's avatar
Alexandre Julliard committed
284
{
285 286 287 288
    LDT_ENTRY entry;
    wine_ldt_get_entry( sel, &entry );
    wine_ldt_set_limit( &entry, limit );
    wine_ldt_set_entry( sel, &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
289 290 291 292 293 294 295
    return sel;
}


/***********************************************************************
 *           SelectorAccessRights   (KERNEL.196)
 */
296
WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val )
Alexandre Julliard's avatar
Alexandre Julliard committed
297
{
298 299 300
    LDT_ENTRY entry;
    wine_ldt_get_entry( sel, &entry );

Alexandre Julliard's avatar
Alexandre Julliard committed
301 302
    if (op == 0)  /* get */
    {
303
        return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0);
Alexandre Julliard's avatar
Alexandre Julliard committed
304 305 306
    }
    else  /* set */
    {
307 308 309
        entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0;
        entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0);
        wine_ldt_set_entry( sel, &entry );
Alexandre Julliard's avatar
Alexandre Julliard committed
310 311 312 313 314 315
        return 0;
    }
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
316
 *           IsBadCodePtr   (KERNEL.336)
Alexandre Julliard's avatar
Alexandre Julliard committed
317
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
318
BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn )
Alexandre Julliard's avatar
Alexandre Julliard committed
319 320
{
    WORD sel;
321
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
322 323

    sel = SELECTOROF(lpfn);
Alexandre Julliard's avatar
Alexandre Julliard committed
324
    if (!sel) return TRUE;
325
    wine_ldt_get_entry( sel, &entry );
326
    if (wine_ldt_is_empty( &entry )) return TRUE;
327 328 329
    /* check for code segment, ignoring conforming, read-only and accessed bits */
    if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE;
    if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
330
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
331 332 333 334
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
335
 *           IsBadStringPtr   (KERNEL.337)
Alexandre Julliard's avatar
Alexandre Julliard committed
336
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
337
BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size )
Alexandre Julliard's avatar
Alexandre Julliard committed
338 339
{
    WORD sel;
340
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
341 342

    sel = SELECTOROF(ptr);
Alexandre Julliard's avatar
Alexandre Julliard committed
343
    if (!sel) return TRUE;
344
    wine_ldt_get_entry( sel, &entry );
345
    if (wine_ldt_is_empty( &entry )) return TRUE;
346 347 348
    /* check for data or readable code segment */
    if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
    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;
Alexandre Julliard's avatar
Alexandre Julliard committed
351
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
352 353 354 355
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
356
 *           IsBadHugeReadPtr   (KERNEL.346)
Alexandre Julliard's avatar
Alexandre Julliard committed
357
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
358
BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size )
Alexandre Julliard's avatar
Alexandre Julliard committed
359 360
{
    WORD sel;
361
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
362 363

    sel = SELECTOROF(ptr);
Alexandre Julliard's avatar
Alexandre Julliard committed
364
    if (!sel) return TRUE;
365
    wine_ldt_get_entry( sel, &entry );
366
    if (wine_ldt_is_empty( &entry )) return TRUE;
367 368 369 370
    /* check for data or readable code segment */
    if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE;  /* system descriptor */
    if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE;  /* non-readable code segment */
    if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
371
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
372 373 374 375
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
376
 *           IsBadHugeWritePtr   (KERNEL.347)
Alexandre Julliard's avatar
Alexandre Julliard committed
377
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
378
BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size )
Alexandre Julliard's avatar
Alexandre Julliard committed
379 380
{
    WORD sel;
381
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
382 383

    sel = SELECTOROF(ptr);
Alexandre Julliard's avatar
Alexandre Julliard committed
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 389
    if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE;
    if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
390
    return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
391 392 393
}

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
394
 *           IsBadReadPtr   (KERNEL.334)
Alexandre Julliard's avatar
Alexandre Julliard committed
395
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
396
BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size )
Alexandre Julliard's avatar
Alexandre Julliard committed
397
{
Alexandre Julliard's avatar
Alexandre Julliard committed
398
    return IsBadHugeReadPtr16( ptr, size );
Alexandre Julliard's avatar
Alexandre Julliard committed
399 400 401 402
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
403
 *           IsBadWritePtr   (KERNEL.335)
Alexandre Julliard's avatar
Alexandre Julliard committed
404
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
405
BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size )
Alexandre Julliard's avatar
Alexandre Julliard committed
406
{
Alexandre Julliard's avatar
Alexandre Julliard committed
407
    return IsBadHugeWritePtr16( ptr, size );
Alexandre Julliard's avatar
Alexandre Julliard committed
408 409 410
}


411
/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
412
 *           IsBadFlatReadWritePtr   (KERNEL.627)
413 414 415 416 417 418 419 420
 */
BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite )
{
    return bWrite? IsBadHugeWritePtr16( ptr, size )
                 : IsBadHugeReadPtr16( ptr, size );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
421 422 423
/***********************************************************************
 *           MemoryRead   (TOOLHELP.78)
 */
424
DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count )
Alexandre Julliard's avatar
Alexandre Julliard committed
425
{
426 427
    LDT_ENTRY entry;
    DWORD limit;
428

429 430 431 432 433 434
    wine_ldt_get_entry( sel, &entry );
    if (wine_ldt_is_empty( &entry )) return 0;
    limit = wine_ldt_get_limit( &entry );
    if (offset > limit) return 0;
    if (offset + count > limit + 1) count = limit + 1 - offset;
    memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count );
Alexandre Julliard's avatar
Alexandre Julliard committed
435 436 437 438 439 440 441
    return count;
}


/***********************************************************************
 *           MemoryWrite   (TOOLHELP.79)
 */
442
DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count )
Alexandre Julliard's avatar
Alexandre Julliard committed
443
{
444 445
    LDT_ENTRY entry;
    DWORD limit;
446

447 448 449 450 451 452
    wine_ldt_get_entry( sel, &entry );
    if (wine_ldt_is_empty( &entry )) return 0;
    limit = wine_ldt_get_limit( &entry );
    if (offset > limit) return 0;
    if (offset + count > limit) count = limit + 1 - offset;
    memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count );
Alexandre Julliard's avatar
Alexandre Julliard committed
453 454
    return count;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
455 456 457 458 459

/************************************* Win95 pointer mapping functions *
 *
 */

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
struct mapls_entry
{
    struct mapls_entry *next;
    void               *addr;   /* linear address */
    int                 count;  /* ref count */
    WORD                sel;    /* selector */
};

static struct mapls_entry *first_entry;


/***********************************************************************
 *           MapLS   (KERNEL32.@)
 *           MapLS   (KERNEL.358)
 *
 * Maps linear pointer to segmented.
 */
SEGPTR WINAPI MapLS( LPCVOID ptr )
{
    struct mapls_entry *entry, *free = NULL;
480
    const void *base;
481 482
    SEGPTR ret = 0;

483
    if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr);
484

485
    base = (const char *)ptr - ((ULONG_PTR)ptr & 0x7fff);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    HeapLock( GetProcessHeap() );
    for (entry = first_entry; entry; entry = entry->next)
    {
        if (entry->addr == base) break;
        if (!entry->count) free = entry;
    }

    if (!entry)
    {
        if (!free)  /* no free entry found, create a new one */
        {
            if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done;
            if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA )))
            {
                HeapFree( GetProcessHeap(), 0, free );
                goto done;
            }
            free->count = 0;
            free->next = first_entry;
            first_entry = free;
        }
        SetSelectorBase( free->sel, (DWORD)base );
508
        free->addr = (void*)base;
509 510 511
        entry = free;
    }
    entry->count++;
512
    ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr );
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
 done:
    HeapUnlock( GetProcessHeap() );
    return ret;
}

/***********************************************************************
 *           UnMapLS   (KERNEL32.@)
 *           UnMapLS   (KERNEL.359)
 *
 * Free mapped selector.
 */
void WINAPI UnMapLS( SEGPTR sptr )
{
    struct mapls_entry *entry;
    WORD sel = SELECTOROF(sptr);

    if (sel)
    {
        HeapLock( GetProcessHeap() );
        for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break;
        if (entry && entry->count > 0) entry->count--;
        HeapUnlock( GetProcessHeap() );
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
538
/***********************************************************************
539
 *           MapSL   (KERNEL32.@)
Patrik Stridvall's avatar
Patrik Stridvall committed
540
 *           MapSL   (KERNEL.357)
Alexandre Julliard's avatar
Alexandre Julliard committed
541 542 543
 *
 * Maps fixed segmented pointer to linear.
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
544
LPVOID WINAPI MapSL( SEGPTR sptr )
Alexandre Julliard's avatar
Alexandre Julliard committed
545
{
546
    return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr);
Alexandre Julliard's avatar
Alexandre Julliard committed
547 548
}

Alexandre Julliard's avatar
Alexandre Julliard committed
549
/***********************************************************************
550
 *           MapSLFix   (KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
551 552 553 554 555 556 557 558
 *
 * FIXME: MapSLFix and UnMapSLFixArray should probably prevent
 * unexpected linear address change when GlobalCompact() shuffles
 * moveable blocks.
 */

LPVOID WINAPI MapSLFix( SEGPTR sptr )
{
559
    return MapSL(sptr);
Alexandre Julliard's avatar
Alexandre Julliard committed
560 561 562
}

/***********************************************************************
563
 *           UnMapSLFixArray   (KERNEL32.@)
564 565
 *
 * Must not change EAX, hence defined as asm function.
Alexandre Julliard's avatar
Alexandre Julliard committed
566
 */
567
#ifdef __i386__
568
__ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" )
569
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
570

571

Alexandre Julliard's avatar
Alexandre Julliard committed
572
/***********************************************************************
573
 *           GetThreadSelectorEntry   (KERNEL32.@)
574
 */
575
BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
576
{
577 578
    THREAD_DESCRIPTOR_INFORMATION       tdi;
    NTSTATUS                            status;
579

580 581 582 583
    tdi.Selector = sel;
    status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry,
                                       &tdi, sizeof(tdi), NULL);
    if (status)
584
    {
585
        SetLastError( RtlNtStatusToDosError(status) );
586 587
        return FALSE;
    }
588 589
    *ldtent = tdi.Entry;
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
590 591 592
}


593
#ifdef __i386__
Alexandre Julliard's avatar
Alexandre Julliard committed
594

595
/***********************************************************************
596
 *		SMapLS (KERNEL32.@)
597
 */
598 599 600 601 602 603 604
__ASM_GLOBAL_FUNC( SMapLS,
                   "xor %edx,%edx\n\t"
                   "testl $0xffff0000,%eax\n\t"
                   "jz 1f\n\t"
                   "pushl %eax\n\t"
                   "call " __ASM_NAME("MapLS") "\n\t"
                   "movl %eax,%edx\n"
605
                   "1:\tret" )
Alexandre Julliard's avatar
Alexandre Julliard committed
606

607
/***********************************************************************
608
 *		SUnMapLS (KERNEL32.@)
609
 */
610 611 612 613 614
__ASM_GLOBAL_FUNC( SUnMapLS,
                   "pushl %eax\n\t"  /* preserve eax */
                   "pushl %eax\n\t"
                   "call " __ASM_NAME("UnMapLS") "\n\t"
                   "popl %eax\n\t"
615
                   "ret" )
616 617

/***********************************************************************
618 619 620 621 622 623 624 625 626 627 628 629 630 631
 *		SMapLS_IP_EBP_8 (KERNEL32.@)
 *		SMapLS_IP_EBP_12 (KERNEL32.@)
 *		SMapLS_IP_EBP_16 (KERNEL32.@)
 *		SMapLS_IP_EBP_20 (KERNEL32.@)
 *		SMapLS_IP_EBP_24 (KERNEL32.@)
 *		SMapLS_IP_EBP_28 (KERNEL32.@)
 *		SMapLS_IP_EBP_32 (KERNEL32.@)
 *		SMapLS_IP_EBP_36 (KERNEL32.@)
 *		SMapLS_IP_EBP_40 (KERNEL32.@)
 *
 * These functions map linear pointers at [EBP+xxx] to segmented pointers
 * and return them.
 * Win95 uses some kind of alias structs, which it stores in [EBP+x] to
 * unravel them at SUnMapLS. We just store the segmented pointer there.
632
 */
633
#define DEFINE_SMapLS(n) \
634 635 636 637
  __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \
                     "movl " #n "(%ebp),%eax\n\t" \
                     "call " __ASM_NAME("SMapLS") "\n\t" \
                     "movl %edx," #n "(%ebp)\n\t" \
638
                     "ret" )
639

640 641 642 643 644 645 646 647 648
DEFINE_SMapLS(8)
DEFINE_SMapLS(12)
DEFINE_SMapLS(16)
DEFINE_SMapLS(20)
DEFINE_SMapLS(24)
DEFINE_SMapLS(28)
DEFINE_SMapLS(32)
DEFINE_SMapLS(36)
DEFINE_SMapLS(40)
649 650 651


/***********************************************************************
652 653
 *		SUnMapLS_IP_EBP_8 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_12 (KERNEL32.@)
654 655 656 657 658 659 660
 *		SUnMapLS_IP_EBP_16 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_20 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_24 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_28 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_32 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_36 (KERNEL32.@)
 *		SUnMapLS_IP_EBP_40 (KERNEL32.@)
661
 */
662 663

#define DEFINE_SUnMapLS(n) \
664 665 666 667 668 669 670
  __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \
                     "pushl %eax\n\t"  /* preserve eax */ \
                     "pushl " #n "(%ebp)\n\t" \
                     "call " __ASM_NAME("UnMapLS") "\n\t" \
                     "movl $0," #n "(%ebp)\n\t" \
                     "popl %eax\n\t" \
                     "ret" )
671

672 673 674 675 676 677 678 679 680
DEFINE_SUnMapLS(8)
DEFINE_SUnMapLS(12)
DEFINE_SUnMapLS(16)
DEFINE_SUnMapLS(20)
DEFINE_SUnMapLS(24)
DEFINE_SUnMapLS(28)
DEFINE_SUnMapLS(32)
DEFINE_SUnMapLS(36)
DEFINE_SUnMapLS(40)
681

682
#endif  /* __i386__ */