Removed some unnecessary includes.
[wine] / dlls / kernel / dosmem.c
1 /*
2  * DOS memory emulation
3  *
4  * Copyright 1995 Alexandre Julliard
5  * Copyright 1996 Marcus Meissner
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
32 #endif
33
34 #include <time.h>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 #endif
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wine/winbase16.h"
42
43 #include "global.h"
44 #include "miscemu.h"
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
48 WINE_DECLARE_DEBUG_CHANNEL(selector);
49
50 WORD DOSMEM_0000H;        /* segment at 0:0 */
51 WORD DOSMEM_BiosDataSeg;  /* BIOS data segment at 0x40:0 */
52 WORD DOSMEM_BiosSysSeg;   /* BIOS ROM segment at 0xf000:0 */
53
54 /* use 2 low bits of 'size' for the housekeeping */
55
56 #define DM_BLOCK_DEBUG          0xABE00000
57 #define DM_BLOCK_TERMINAL       0x00000001
58 #define DM_BLOCK_FREE           0x00000002
59 #define DM_BLOCK_MASK           0x001FFFFC
60
61 /*
62 #define __DOSMEM_DEBUG__
63  */
64
65 typedef struct {
66    unsigned     size;
67 } dosmem_entry;
68
69 typedef struct {
70   unsigned      blocks;
71   unsigned      free;
72 } dosmem_info;
73
74 #define NEXT_BLOCK(block) \
75         (dosmem_entry*)(((char*)(block)) + \
76          sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
77
78 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
79 #define VM_STUB_SEGMENT 0xf000         /* BIOS segment */
80
81 /* DOS memory base */
82 static char *DOSMEM_dosmem;
83 /* DOS system base (for interrupt vector table and BIOS data area)
84  * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
85  * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
86  * and only relocated to NULL when absolutely necessary */
87 static char *DOSMEM_sysmem;
88
89 /* Start of DOS conventional memory */
90 static char *DOSMEM_membase;
91
92 static void DOSMEM_InitMemory(void);
93
94 /***********************************************************************
95  *           DOSMEM_MemoryTop
96  *
97  * Gets the DOS memory top.
98  */
99 static char *DOSMEM_MemoryTop(void)
100 {
101     return DOSMEM_dosmem+0x9FFFC; /* 640K */
102 }
103
104 /***********************************************************************
105  *           DOSMEM_InfoBlock
106  *
107  * Gets the DOS memory info block.
108  */
109 static dosmem_info *DOSMEM_InfoBlock(void)
110 {
111     if (!DOSMEM_membase)
112     {
113         DWORD         reserve;
114
115         /*
116          * Reserve either:
117          * - lowest 64k for NULL pointer catching (Win16)
118          * - lowest 1k for interrupt handlers and 
119          *   another 0.5k for BIOS, DOS and intra-application
120          *   areas (DOS)
121          */
122         if (DOSMEM_dosmem != DOSMEM_sysmem)
123             reserve = 0x10000; /* 64k */
124         else
125             reserve = 0x600; /* 1.5k */
126
127         /*
128          * Round to paragraph boundary in order to make 
129          * sure the alignment is correct.
130          */
131         reserve = ((reserve + 15) >> 4) << 4;
132
133         /*
134          * Set DOS memory base and initialize conventional memory.
135          */
136         DOSMEM_membase = DOSMEM_dosmem + reserve;
137         DOSMEM_InitMemory();
138     }
139
140     return (dosmem_info*)DOSMEM_membase;
141 }
142
143 /***********************************************************************
144  *           DOSMEM_RootBlock
145  *
146  * Gets the DOS memory root block.
147  */
148 static dosmem_entry *DOSMEM_RootBlock(void)
149 {
150     /* first block has to be paragraph-aligned */
151     return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
152                            ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
153 }
154
155 /***********************************************************************
156  *           DOSMEM_FillIsrTable
157  *
158  * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
159  *
160  * NOTES:
161  * Linux normally only traps INTs performed from or destined to BIOSSEG
162  * for us to handle, if the int_revectored table is empty. Filling the
163  * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
164  * to hook interrupts, as well as use their familiar retf tricks to call
165  * them, AND let Wine handle any unhooked interrupts transparently.
166  */
167 static void DOSMEM_FillIsrTable(void)
168 {
169     SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
170     int x;
171
172     for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
173 }
174
175 static void DOSMEM_MakeIsrStubs(void)
176 {
177     DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
178     int x;
179
180     for (x=0; x<256; x++) stub[x]=VM_STUB(x);
181 }
182
183 static BIOSDATA * DOSMEM_BiosData(void)
184 {
185     return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
186 }
187
188 /**********************************************************************
189  *          DOSMEM_GetTicksSinceMidnight
190  *
191  * Return number of clock ticks since midnight.
192  */
193 static DWORD DOSMEM_GetTicksSinceMidnight(void)
194 {
195     struct tm *bdtime;
196     struct timeval tvs;
197     time_t seconds;
198
199     /* This should give us the (approximately) correct
200      * 18.206 clock ticks per second since midnight.
201      */
202     gettimeofday( &tvs, NULL );
203     seconds = tvs.tv_sec;
204     bdtime = localtime( &seconds );
205     return (((bdtime->tm_hour * 3600 + bdtime->tm_min * 60 +
206               bdtime->tm_sec) * 18206) / 1000) +
207                   (tvs.tv_usec / 54927);
208 }
209
210 /***********************************************************************
211  *           DOSMEM_FillBiosSegments
212  *
213  * Fill the BIOS data segment with dummy values.
214  */
215 static void DOSMEM_FillBiosSegments(void)
216 {
217     BYTE *pBiosSys = DOSMEM_dosmem + 0xf0000;
218     BYTE *pBiosROMTable = pBiosSys+0xe6f5;
219     BIOSDATA *pBiosData = DOSMEM_BiosData();
220
221       /* Clear all unused values */
222     memset( pBiosData, 0, sizeof(*pBiosData) );
223
224     /* FIXME: should check the number of configured drives and ports */
225     pBiosData->Com1Addr             = 0x3f8;
226     pBiosData->Com2Addr             = 0x2f8;
227     pBiosData->Lpt1Addr             = 0x378;
228     pBiosData->Lpt2Addr             = 0x278;
229     pBiosData->InstalledHardware    = 0x5463;
230     pBiosData->MemSize              = 640;
231     pBiosData->NextKbdCharPtr       = 0x1e;
232     pBiosData->FirstKbdCharPtr      = 0x1e;
233     pBiosData->VideoMode            = 3;
234     pBiosData->VideoColumns         = 80;
235     pBiosData->VideoPageSize        = 80 * 25 * 2;
236     pBiosData->VideoPageStartAddr   = 0xb800;
237     pBiosData->VideoCtrlAddr        = 0x3d4;
238     pBiosData->Ticks                = DOSMEM_GetTicksSinceMidnight();
239     pBiosData->NbHardDisks          = 2;
240     pBiosData->KbdBufferStart       = 0x1e;
241     pBiosData->KbdBufferEnd         = 0x3e;
242     pBiosData->RowsOnScreenMinus1   = 24;
243     pBiosData->BytesPerChar         = 0x10;
244     pBiosData->ModeOptions          = 0x64;
245     pBiosData->FeatureBitsSwitches  = 0xf9;
246     pBiosData->VGASettings          = 0x51;
247     pBiosData->DisplayCombination   = 0x08;
248     pBiosData->DiskDataRate         = 0;
249
250     /* fill ROM configuration table (values from Award) */
251     *(pBiosROMTable+0x0)        = 0x08; /* number of bytes following LO */
252     *(pBiosROMTable+0x1)        = 0x00; /* number of bytes following HI */
253     *(pBiosROMTable+0x2)        = 0xfc; /* model */
254     *(pBiosROMTable+0x3)        = 0x01; /* submodel */
255     *(pBiosROMTable+0x4)        = 0x00; /* BIOS revision */
256     *(pBiosROMTable+0x5)        = 0x74; /* feature byte 1 */
257     *(pBiosROMTable+0x6)        = 0x00; /* feature byte 2 */
258     *(pBiosROMTable+0x7)        = 0x00; /* feature byte 3 */
259     *(pBiosROMTable+0x8)        = 0x00; /* feature byte 4 */
260     *(pBiosROMTable+0x9)        = 0x00; /* feature byte 5 */
261
262     /* BIOS date string */
263     strcpy((char *)pBiosSys+0xfff5, "13/01/99");
264
265     /* BIOS ID */
266     *(pBiosSys+0xfffe) = 0xfc;
267
268     /* Reboot vector (f000:fff0 or ffff:0000) */
269     *(DWORD*)(pBiosSys + 0xfff0) = VM_STUB(0x19);
270 }
271
272 /***********************************************************************
273  *           DOSMEM_InitMemory
274  *
275  * Initialises the DOS memory structures.
276  */
277 static void DOSMEM_InitMemory(void)
278 {
279     dosmem_info*        info_block = DOSMEM_InfoBlock();
280     dosmem_entry*       root_block = DOSMEM_RootBlock();
281     dosmem_entry*       dm;
282
283     root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
284
285     info_block->blocks = 0;
286     info_block->free = root_block->size;
287
288     dm = NEXT_BLOCK(root_block);
289     dm->size = DM_BLOCK_TERMINAL;
290     root_block->size |= DM_BLOCK_FREE
291 #ifdef __DOSMEM_DEBUG__
292                      | DM_BLOCK_DEBUG
293 #endif
294                      ;
295
296     TRACE( "DOS conventional memory initialized, %d bytes free.\n", 
297            DOSMEM_Available() );
298 }
299
300
301 /**********************************************************************
302  *              setup_dos_mem
303  *
304  * Setup the first megabyte for DOS memory access
305  */
306 static void setup_dos_mem( int dos_init )
307 {
308     int sys_offset = 0;
309     int page_size = getpagesize();
310     void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
311                                  PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
312     if (addr == (void *)page_size)  /* we got what we wanted */
313     {
314         /* now map from address 0 */
315         addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
316         if (addr)
317         {
318             ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
319             ExitProcess(1);
320         }
321
322         /* inform the memory manager that there is a mapping here */
323         VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
324
325         /* protect the first 64K to catch NULL pointers */
326         if (!dos_init)
327         {
328             VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
329             /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
330             sys_offset += 0xf0000;
331         }
332     }
333     else
334     {
335         ERR("Cannot use first megabyte for DOS address space, please report\n" );
336         if (dos_init) ExitProcess(1);
337         /* allocate the DOS area somewhere else */
338         addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
339         if (!addr)
340         {
341             ERR( "Cannot allocate DOS memory\n" );
342             ExitProcess(1);
343         }
344     }
345     DOSMEM_dosmem = addr;
346     DOSMEM_sysmem = (char*)addr + sys_offset;
347 }
348
349
350 /***********************************************************************
351  *           DOSMEM_Init
352  *
353  * Create the dos memory segments, and store them into the KERNEL
354  * exported values.
355  */
356 BOOL DOSMEM_Init(BOOL dos_init)
357 {
358     static int already_done, already_mapped;
359
360     if (!already_done)
361     {
362         setup_dos_mem( dos_init );
363
364         DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
365                                            0x10000, 0, WINE_LDT_FLAGS_DATA );
366         DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
367                                                 0x100, 0, WINE_LDT_FLAGS_DATA );
368         DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
369                                                0x10000, 0, WINE_LDT_FLAGS_DATA );
370         DOSMEM_FillBiosSegments();
371         DOSMEM_FillIsrTable();
372         already_done = 1;
373     }
374     else if (dos_init && !already_mapped)
375     {
376         if (DOSMEM_dosmem)
377         {
378             ERR( "Needs access to the first megabyte for DOS mode\n" );
379             ExitProcess(1);
380         }
381         MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
382                  "         NULL pointer accesses will no longer be caught.\n" );
383         VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
384         /* copy the BIOS and ISR area down */
385         memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
386         DOSMEM_sysmem = DOSMEM_dosmem;
387         SetSelectorBase( DOSMEM_0000H, 0 );
388         SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
389         /* we may now need the actual interrupt stubs, and since we've just moved the
390          * interrupt vector table away, we can fill the area with stubs instead... */
391         DOSMEM_MakeIsrStubs();
392         already_mapped = 1;
393     }
394     return TRUE;
395 }
396
397
398 /***********************************************************************
399  *           DOSMEM_Tick
400  *
401  * Increment the BIOS tick counter. Called by timer signal handler.
402  */
403 void DOSMEM_Tick( WORD timer )
404 {
405     BIOSDATA *pBiosData = DOSMEM_BiosData();
406     if (pBiosData) pBiosData->Ticks++;
407 }
408
409 /***********************************************************************
410  *           DOSMEM_GetBlock
411  *
412  * Carve a chunk of the DOS memory block (without selector).
413  */
414 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
415 {
416    UINT          blocksize;
417    char         *block = NULL;
418    dosmem_info  *info_block = DOSMEM_InfoBlock();
419    dosmem_entry *dm;
420 #ifdef __DOSMEM_DEBUG_
421    dosmem_entry *prev = NULL;
422 #endif
423
424    if( size > info_block->free ) return NULL;
425    dm = DOSMEM_RootBlock();
426
427    while (dm && dm->size != DM_BLOCK_TERMINAL)
428    {
429 #ifdef __DOSMEM_DEBUG__
430        if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
431        {
432             WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
433             return NULL;
434        }
435        prev = dm;
436 #endif
437        if( dm->size & DM_BLOCK_FREE )
438        {
439            dosmem_entry  *next = NEXT_BLOCK(dm);
440
441            while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
442            {
443                dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
444                next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
445                next = NEXT_BLOCK(dm);
446            }
447
448            blocksize = dm->size & DM_BLOCK_MASK;
449            if( blocksize >= size )
450            {
451                block = ((char*)dm) + sizeof(dosmem_entry);
452                if( blocksize - size > 0x20 )
453                {
454                    /* split dm so that the next one stays
455                     * paragraph-aligned (and dm loses free bit) */
456
457                    dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
458                                               sizeof(dosmem_entry));
459                    next = (dosmem_entry*)(((char*)dm) +
460                            sizeof(dosmem_entry) + dm->size);
461                    next->size = (blocksize - (dm->size +
462                            sizeof(dosmem_entry))) | DM_BLOCK_FREE
463 #ifdef __DOSMEM_DEBUG__
464                                                   | DM_BLOCK_DEBUG
465 #endif
466                                                   ;
467                } else dm->size &= DM_BLOCK_MASK;
468
469                info_block->blocks++;
470                info_block->free -= dm->size;
471                if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
472 #ifdef __DOSMEM_DEBUG__
473                dm->size |= DM_BLOCK_DEBUG;
474 #endif
475                break;
476            }
477            dm = next;
478        }
479        else dm = NEXT_BLOCK(dm);
480    }
481    return (LPVOID)block;
482 }
483
484 /***********************************************************************
485  *           DOSMEM_FreeBlock
486  */
487 BOOL DOSMEM_FreeBlock(void* ptr)
488 {
489    dosmem_info  *info_block = DOSMEM_InfoBlock();
490
491    if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
492        ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
493                   - DOSMEM_dosmem) & 0xf) )
494    {
495        dosmem_entry  *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
496
497        if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
498 #ifdef __DOSMEM_DEBUG__
499          && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
500 #endif
501          )
502        {
503              info_block->blocks--;
504              info_block->free += dm->size;
505
506              dm->size |= DM_BLOCK_FREE;
507              return TRUE;
508        }
509    }
510    return FALSE;
511 }
512
513 /***********************************************************************
514  *           DOSMEM_ResizeBlock
515  *
516  * Resize DOS memory block in place. Returns block size or -1 on error.
517  *
518  * If exact is TRUE, returned value is either old or requested block
519  * size. If exact is FALSE, block is expanded even if there is not
520  * enough space for full requested block size.
521  */
522 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
523 {
524    char         *block = NULL;
525    dosmem_info  *info_block = DOSMEM_InfoBlock();
526    dosmem_entry *dm;
527    dosmem_entry *next;
528    UINT blocksize;
529    UINT orgsize;
530
531    if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) ||
532        (ptr >= (void*)DOSMEM_MemoryTop()) ||
533        (((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) )
534      return (UINT)-1;
535
536    dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
537    if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) )
538        return (UINT)-1;
539
540    next = NEXT_BLOCK(dm);
541    orgsize = dm->size & DM_BLOCK_MASK;
542
543    /* collapse free blocks */
544    while( next->size & DM_BLOCK_FREE )
545    {
546        dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
547        next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
548        next = NEXT_BLOCK(dm);
549    }
550
551    blocksize = dm->size & DM_BLOCK_MASK;
552
553    /*
554     * If collapse didn't help we either expand block to maximum
555     * available size (exact == FALSE) or give collapsed blocks
556     * back to free storage (exact == TRUE).
557     */
558    if (blocksize < size)
559        size = exact ? orgsize : blocksize;
560
561    block = ((char*)dm) + sizeof(dosmem_entry);
562    if( blocksize - size > 0x20 )
563    {
564        /*
565         * split dm so that the next one stays
566         * paragraph-aligned (and next gains free bit) 
567         */
568
569        dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
570                    sizeof(dosmem_entry));
571        next = (dosmem_entry*)(((char*)dm) +
572                               sizeof(dosmem_entry) + dm->size);
573        next->size = (blocksize - (dm->size +
574                                   sizeof(dosmem_entry))) | DM_BLOCK_FREE;
575    } 
576    else 
577    {
578        dm->size &= DM_BLOCK_MASK;
579    }
580
581    /*
582     * Adjust available memory if block size changes.
583     */
584    info_block->free += orgsize - dm->size;
585
586    return size;
587 }
588
589 /***********************************************************************
590  *           DOSMEM_Available
591  */
592 UINT DOSMEM_Available(void)
593 {
594    UINT          blocksize, available = 0;
595    dosmem_entry *dm;
596
597    dm = DOSMEM_RootBlock();
598
599    while (dm && dm->size != DM_BLOCK_TERMINAL)
600    {
601 #ifdef __DOSMEM_DEBUG__
602        if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
603        {
604             WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
605             return NULL;
606        }
607        prev = dm;
608 #endif
609        if( dm->size & DM_BLOCK_FREE )
610        {
611            dosmem_entry  *next = NEXT_BLOCK(dm);
612
613            while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
614            {
615                dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
616                next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
617                next = NEXT_BLOCK(dm);
618            }
619
620            blocksize = dm->size & DM_BLOCK_MASK;
621            if ( blocksize > available ) available = blocksize;
622            dm = next;
623        }
624        else dm = NEXT_BLOCK(dm);
625    }
626    return available;
627 }
628
629
630 /***********************************************************************
631  *           DOSMEM_MapLinearToDos
632  *
633  * Linear address to the DOS address space.
634  */
635 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
636 {
637     if (((char*)ptr >= DOSMEM_dosmem) &&
638         ((char*)ptr < DOSMEM_dosmem + 0x100000))
639           return (UINT)ptr - (UINT)DOSMEM_dosmem;
640     return (UINT)ptr;
641 }
642
643
644 /***********************************************************************
645  *           DOSMEM_MapDosToLinear
646  *
647  * DOS linear address to the linear address space.
648  */
649 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
650 {
651     if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
652     return (LPVOID)ptr;
653 }
654
655
656 /***********************************************************************
657  *           DOSMEM_MapRealToLinear
658  *
659  * Real mode DOS address into a linear pointer
660  */
661 LPVOID DOSMEM_MapRealToLinear(DWORD x)
662 {
663    LPVOID       lin;
664
665    lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
666    TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
667    return lin;
668 }
669
670 /***********************************************************************
671  *           DOSMEM_AllocSelector
672  *
673  * Allocates a protected mode selector for a realmode segment.
674  */
675 WORD DOSMEM_AllocSelector(WORD realsel)
676 {
677         HMODULE16 hModule = GetModuleHandle16("KERNEL");
678         WORD    sel;
679
680         sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
681                                 hModule, WINE_LDT_FLAGS_DATA );
682         TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
683         return sel;
684 }