- allow to save/restore some properties into the registry (like font,
[wine.git] / msdos / dosmem.c
blob56604d6fe701630091e9b3d05768ccbd05d35752
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
6 */
8 #include "config.h"
9 #include "wine/port.h"
11 #include <signal.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #ifdef HAVE_SYS_MMAN_H
16 # include <sys/mman.h>
17 #endif
19 #include "winbase.h"
20 #include "wine/winbase16.h"
22 #include "global.h"
23 #include "selectors.h"
24 #include "miscemu.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(dosmem);
28 DECLARE_DEBUG_CHANNEL(selector);
30 WORD DOSMEM_0000H; /* segment at 0:0 */
31 WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
32 WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
34 DWORD DOSMEM_CollateTable;
36 /* use 2 low bits of 'size' for the housekeeping */
38 #define DM_BLOCK_DEBUG 0xABE00000
39 #define DM_BLOCK_TERMINAL 0x00000001
40 #define DM_BLOCK_FREE 0x00000002
41 #define DM_BLOCK_MASK 0x001FFFFC
44 #define __DOSMEM_DEBUG__
47 typedef struct {
48 unsigned size;
49 } dosmem_entry;
51 typedef struct {
52 unsigned blocks;
53 unsigned free;
54 } dosmem_info;
56 #define NEXT_BLOCK(block) \
57 (dosmem_entry*)(((char*)(block)) + \
58 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
60 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
61 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
63 /* DOS memory base */
64 static char *DOSMEM_dosmem;
65 /* DOS system base (for interrupt vector table and BIOS data area)
66 * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
67 * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
68 * and only relocated to NULL when absolutely necessary */
69 static char *DOSMEM_sysmem;
71 /* various real-mode code stubs */
72 WORD DOSMEM_wrap_seg;
73 WORD DOSMEM_xms_seg;
74 WORD DOSMEM_dpmi_seg;
75 WORD DOSMEM_dpmi_sel;
77 DWORD DOS_LOLSeg;
79 /***********************************************************************
80 * DOSMEM_SystemBase
82 * Gets the virtual DOS memory base (interrupt table).
84 char *DOSMEM_SystemBase(void)
86 return DOSMEM_sysmem;
89 /***********************************************************************
90 * DOSMEM_MemoryBase
92 * Gets the DOS memory base.
94 char *DOSMEM_MemoryBase(void)
96 return DOSMEM_dosmem;
99 /***********************************************************************
100 * DOSMEM_MemoryTop
102 * Gets the DOS memory top.
104 static char *DOSMEM_MemoryTop(void)
106 return DOSMEM_dosmem+0x9FFFC; /* 640K */
109 /***********************************************************************
110 * DOSMEM_InfoBlock
112 * Gets the DOS memory info block.
114 static dosmem_info *DOSMEM_InfoBlock(void)
116 return (dosmem_info*)(DOSMEM_dosmem+0x10000); /* 64K */
119 /***********************************************************************
120 * DOSMEM_RootBlock
122 * Gets the DOS memory root block.
124 static dosmem_entry *DOSMEM_RootBlock(void)
126 /* first block has to be paragraph-aligned */
127 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
128 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
131 /***********************************************************************
132 * DOSMEM_FillIsrTable
134 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
136 * NOTES:
137 * Linux normally only traps INTs performed from or destined to BIOSSEG
138 * for us to handle, if the int_revectored table is empty. Filling the
139 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
140 * to hook interrupts, as well as use their familiar retf tricks to call
141 * them, AND let Wine handle any unhooked interrupts transparently.
143 static void DOSMEM_FillIsrTable(void)
145 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
146 int x;
148 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
151 static void DOSMEM_MakeIsrStubs(void)
153 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
154 int x;
156 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
159 /***********************************************************************
160 * DOSMEM_InitDPMI
162 * Allocate the global DPMI RMCB wrapper.
164 static void DOSMEM_InitDPMI(void)
166 LPSTR ptr;
168 static const char wrap_code[]={
169 0xCD,0x31, /* int $0x31 */
170 0xCB /* lret */
173 static const char enter_xms[]=
175 /* XMS hookable entry point */
176 0xEB,0x03, /* jmp entry */
177 0x90,0x90,0x90, /* nop;nop;nop */
178 /* entry: */
179 /* real entry point */
180 /* for simplicity, we'll just use the same hook as DPMI below */
181 0xCD,0x31, /* int $0x31 */
182 0xCB /* lret */
185 static const char enter_pm[]=
187 0x50, /* pushw %ax */
188 0x52, /* pushw %dx */
189 0x55, /* pushw %bp */
190 0x89,0xE5, /* movw %sp,%bp */
191 /* get return CS */
192 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
193 /* just call int 31 here to get into protected mode... */
194 /* it'll check whether it was called from dpmi_seg... */
195 0xCD,0x31, /* int $0x31 */
196 /* we are now in the context of a 16-bit relay call */
197 /* need to fixup our stack;
198 * 16-bit relay return address will be lost, but we won't worry quite yet */
199 0x8E,0xD0, /* movw %ax,%ss */
200 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
201 /* set return CS */
202 0x89,0x56,0x08, /* movw %dx,8(%bp) */
203 0x5D, /* popw %bp */
204 0x5A, /* popw %dx */
205 0x58, /* popw %ax */
206 0xCB /* lret */
209 ptr = DOSMEM_GetBlock( sizeof(wrap_code), &DOSMEM_wrap_seg );
210 memcpy( ptr, wrap_code, sizeof(wrap_code) );
211 ptr = DOSMEM_GetBlock( sizeof(enter_xms), &DOSMEM_xms_seg );
212 memcpy( ptr, enter_xms, sizeof(enter_xms) );
213 ptr = DOSMEM_GetBlock( sizeof(enter_pm), &DOSMEM_dpmi_seg );
214 memcpy( ptr, enter_pm, sizeof(enter_pm) );
215 DOSMEM_dpmi_sel = SELECTOR_AllocBlock( ptr, sizeof(enter_pm), WINE_LDT_FLAGS_CODE );
218 BIOSDATA * DOSMEM_BiosData()
220 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
223 BYTE * DOSMEM_BiosSys()
225 return DOSMEM_dosmem+0xf0000;
228 /***********************************************************************
229 * DOSMEM_FillBiosSegments
231 * Fill the BIOS data segment with dummy values.
233 static void DOSMEM_FillBiosSegments(void)
235 BYTE *pBiosSys = DOSMEM_BiosSys();
236 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
237 BIOSDATA *pBiosData = DOSMEM_BiosData();
239 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
240 VIDEOFUNCTIONALITY *pVidFunc = (VIDEOFUNCTIONALITY *)(pBiosSys+0xe000);
241 VIDEOSTATE *pVidState = (VIDEOSTATE *)(pBiosSys+0xe010);
242 int i;
244 /* Clear all unused values */
245 memset( pBiosData, 0, sizeof(*pBiosData) );
246 memset( pVidFunc, 0, sizeof(*pVidFunc ) );
247 memset( pVidState, 0, sizeof(*pVidState) );
249 /* FIXME: should check the number of configured drives and ports */
251 pBiosData->Com1Addr = 0x3f8;
252 pBiosData->Com2Addr = 0x2f8;
253 pBiosData->Lpt1Addr = 0x378;
254 pBiosData->Lpt2Addr = 0x278;
255 pBiosData->InstalledHardware = 0x5463;
256 pBiosData->MemSize = 640;
257 pBiosData->NextKbdCharPtr = 0x1e;
258 pBiosData->FirstKbdCharPtr = 0x1e;
259 pBiosData->VideoMode = 3;
260 pBiosData->VideoColumns = 80;
261 pBiosData->VideoPageSize = 80 * 25 * 2;
262 pBiosData->VideoPageStartAddr = 0xb800;
263 pBiosData->VideoCtrlAddr = 0x3d4;
264 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
265 pBiosData->NbHardDisks = 2;
266 pBiosData->KbdBufferStart = 0x1e;
267 pBiosData->KbdBufferEnd = 0x3e;
268 pBiosData->RowsOnScreenMinus1 = 23;
269 pBiosData->BytesPerChar = 0x10;
270 pBiosData->ModeOptions = 0x64;
271 pBiosData->FeatureBitsSwitches = 0xf9;
272 pBiosData->VGASettings = 0x51;
273 pBiosData->DisplayCombination = 0x08;
274 pBiosData->DiskDataRate = 0;
276 /* fill ROM configuration table (values from Award) */
277 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
278 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
279 *(pBiosROMTable+0x2) = 0xfc; /* model */
280 *(pBiosROMTable+0x3) = 0x01; /* submodel */
281 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
282 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
283 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
284 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
285 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
286 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
289 for (i = 0; i < 7; i++)
290 pVidFunc->ModeSupport[i] = 0xff;
292 pVidFunc->ScanlineSupport = 7;
293 pVidFunc->NumberCharBlocks = 0;
294 pVidFunc->ActiveCharBlocks = 0;
295 pVidFunc->MiscFlags = 0x8ff;
296 pVidFunc->SavePointerFlags = 0x3f;
298 pVidState->StaticFuncTable = 0xf000e000; /* FIXME: always real mode ? */
299 pVidState->VideoMode = pBiosData->VideoMode; /* needs updates! */
300 pVidState->NumberColumns = pBiosData->VideoColumns; /* needs updates! */
301 pVidState->RegenBufLen = 0;
302 pVidState->RegenBufAddr = 0;
304 for (i = 0; i < 8; i++)
305 pVidState->CursorPos[i] = 0;
307 pVidState->CursorType = 0x0a0b; /* start/end line */
308 pVidState->ActivePage = 0;
309 pVidState->CRTCPort = 0x3da;
310 pVidState->Port3x8 = 0;
311 pVidState->Port3x9 = 0;
312 pVidState->NumberRows = 23; /* number of rows - 1 */
313 pVidState->BytesPerChar = 0x10;
314 pVidState->DCCActive = pBiosData->DisplayCombination;
315 pVidState->DCCAlternate = 0;
316 pVidState->NumberColors = 16;
317 pVidState->NumberPages = 1;
318 pVidState->NumberScanlines = 3; /* (0,1,2,3) = (200,350,400,480) */
319 pVidState->CharBlockPrimary = 0;
320 pVidState->CharBlockSecondary = 0;
321 pVidState->MiscFlags =
322 (pBiosData->VGASettings & 0x0f)
323 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
324 pVidState->NonVGASupport = 0;
325 pVidState->VideoMem = (pBiosData->ModeOptions & 0x60 >> 5);
326 pVidState->SavePointerState = 0;
327 pVidState->DisplayStatus = 4;
329 /* BIOS date string */
330 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
332 /* BIOS ID */
333 *(pBiosSys+0xfffe) = 0xfc;
336 /***********************************************************************
337 * DOSMEM_InitCollateTable
339 * Initialises the collate table (character sorting, language dependent)
341 static void DOSMEM_InitCollateTable()
343 DWORD x;
344 unsigned char *tbl;
345 int i;
347 x = GlobalDOSAlloc16(258);
348 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
349 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
350 *(WORD*)tbl = 0x100;
351 tbl += 2;
352 for ( i = 0; i < 0x100; i++) *tbl++ = i;
355 /***********************************************************************
356 * DOSMEM_InitErrorTable
358 * Initialises the error tables (DOS 5+)
360 static void DOSMEM_InitErrorTable()
362 #if 0 /* no longer used */
363 DWORD x;
364 char *call;
366 /* We will use a snippet of real mode code that calls */
367 /* a WINE-only interrupt to handle moving the requested */
368 /* message into the buffer... */
370 /* FIXME - There is still something wrong... */
372 /* FIXME - Find hex values for opcodes...
374 (On call, AX contains message number
375 DI contains 'offset' (??)
376 Resturn, ES:DI points to counted string )
378 PUSH BX
379 MOV BX, AX
380 MOV AX, (arbitrary subfunction number)
381 INT (WINE-only interrupt)
382 POP BX
387 const int code = 4;
388 const int buffer = 80;
389 const int SIZE_TO_ALLOCATE = code + buffer;
391 /* FIXME - Complete rewrite of the table system to save */
392 /* precious DOS space. Now, we return the 0001:???? as */
393 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
394 /* as a special case and programs will use the alternate */
395 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
396 /* 0x08) which lets us have a smaller memory footprint anyway. */
398 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
400 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
401 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
403 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
405 memset(call, 0, SIZE_TO_ALLOCATE);
406 #endif
407 /* Fixme - Copy assembly into buffer here */
410 /***********************************************************************
411 * DOSMEM_InitMemory
413 * Initialises the DOS memory structures.
415 static void DOSMEM_InitMemory(void)
417 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
418 * 1000:0000 and ends at 9FFF:FFEF. */
420 dosmem_info* info_block = DOSMEM_InfoBlock();
421 dosmem_entry* root_block = DOSMEM_RootBlock();
422 dosmem_entry* dm;
424 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
426 info_block->blocks = 0;
427 info_block->free = root_block->size;
429 dm = NEXT_BLOCK(root_block);
430 dm->size = DM_BLOCK_TERMINAL;
431 root_block->size |= DM_BLOCK_FREE
432 #ifdef __DOSMEM_DEBUG__
433 | DM_BLOCK_DEBUG
434 #endif
438 /**********************************************************************
439 * setup_dos_mem
441 * Setup the first megabyte for DOS memory access
443 static void setup_dos_mem( int dos_init )
445 int sys_offset = 0;
446 int page_size = getpagesize();
447 void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
448 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
449 if (addr == (void *)page_size) /* we got what we wanted */
451 /* now map from address 0 */
452 addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
453 if (addr)
455 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
456 ExitProcess(1);
459 /* inform the memory manager that there is a mapping here */
460 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
462 /* protect the first 64K to catch NULL pointers */
463 if (!dos_init)
465 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
466 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
467 sys_offset += 0xf0000;
470 else
472 ERR("Cannot use first megabyte for DOS address space, please report\n" );
473 if (dos_init) ExitProcess(1);
474 /* allocate the DOS area somewhere else */
475 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
476 if (!addr)
478 ERR( "Cannot allocate DOS memory\n" );
479 ExitProcess(1);
482 DOSMEM_dosmem = addr;
483 DOSMEM_sysmem = (char*)addr + sys_offset;
487 /***********************************************************************
488 * DOSMEM_Init
490 * Create the dos memory segments, and store them into the KERNEL
491 * exported values.
493 BOOL DOSMEM_Init(BOOL dos_init)
495 static int already_done, already_mapped;
497 if (!already_done)
499 setup_dos_mem( dos_init );
501 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
502 0x10000, 0, WINE_LDT_FLAGS_DATA );
503 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
504 0x100, 0, WINE_LDT_FLAGS_DATA );
505 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
506 0x10000, 0, WINE_LDT_FLAGS_DATA );
507 DOSMEM_FillBiosSegments();
508 DOSMEM_FillIsrTable();
509 DOSMEM_InitMemory();
510 DOSMEM_InitCollateTable();
511 DOSMEM_InitErrorTable();
512 DOSMEM_InitDPMI();
513 already_done = 1;
515 else if (dos_init && !already_mapped)
517 if (DOSMEM_dosmem)
519 ERR( "Needs access to the first megabyte for DOS mode\n" );
520 ExitProcess(1);
522 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
523 " NULL pointer accesses will no longer be caught.\n" );
524 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
525 /* copy the BIOS and ISR area down */
526 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
527 DOSMEM_sysmem = DOSMEM_dosmem;
528 SetSelectorBase( DOSMEM_0000H, 0 );
529 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
530 /* we may now need the actual interrupt stubs, and since we've just moved the
531 * interrupt vector table away, we can fill the area with stubs instead... */
532 DOSMEM_MakeIsrStubs();
533 already_mapped = 1;
535 return TRUE;
539 /***********************************************************************
540 * DOSMEM_Tick
542 * Increment the BIOS tick counter. Called by timer signal handler.
544 void DOSMEM_Tick( WORD timer )
546 BIOSDATA *pBiosData = DOSMEM_BiosData();
547 if (pBiosData) pBiosData->Ticks++;
550 /***********************************************************************
551 * DOSMEM_GetBlock
553 * Carve a chunk of the DOS memory block (without selector).
555 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
557 UINT blocksize;
558 char *block = NULL;
559 dosmem_info *info_block = DOSMEM_InfoBlock();
560 dosmem_entry *dm;
561 #ifdef __DOSMEM_DEBUG_
562 dosmem_entry *prev = NULL;
563 #endif
565 if( size > info_block->free ) return NULL;
566 dm = DOSMEM_RootBlock();
568 while (dm && dm->size != DM_BLOCK_TERMINAL)
570 #ifdef __DOSMEM_DEBUG__
571 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
573 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
574 return NULL;
576 prev = dm;
577 #endif
578 if( dm->size & DM_BLOCK_FREE )
580 dosmem_entry *next = NEXT_BLOCK(dm);
582 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
584 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
585 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
586 next = NEXT_BLOCK(dm);
589 blocksize = dm->size & DM_BLOCK_MASK;
590 if( blocksize >= size )
592 block = ((char*)dm) + sizeof(dosmem_entry);
593 if( blocksize - size > 0x20 )
595 /* split dm so that the next one stays
596 * paragraph-aligned (and dm loses free bit) */
598 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
599 sizeof(dosmem_entry));
600 next = (dosmem_entry*)(((char*)dm) +
601 sizeof(dosmem_entry) + dm->size);
602 next->size = (blocksize - (dm->size +
603 sizeof(dosmem_entry))) | DM_BLOCK_FREE
604 #ifdef __DOSMEM_DEBUG__
605 | DM_BLOCK_DEBUG
606 #endif
608 } else dm->size &= DM_BLOCK_MASK;
610 info_block->blocks++;
611 info_block->free -= dm->size;
612 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
613 #ifdef __DOSMEM_DEBUG__
614 dm->size |= DM_BLOCK_DEBUG;
615 #endif
616 break;
618 dm = next;
620 else dm = NEXT_BLOCK(dm);
622 return (LPVOID)block;
625 /***********************************************************************
626 * DOSMEM_FreeBlock
628 BOOL DOSMEM_FreeBlock(void* ptr)
630 dosmem_info *info_block = DOSMEM_InfoBlock();
632 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
633 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
634 - DOSMEM_dosmem) & 0xf) )
636 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
638 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
639 #ifdef __DOSMEM_DEBUG__
640 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
641 #endif
644 info_block->blocks--;
645 info_block->free += dm->size;
647 dm->size |= DM_BLOCK_FREE;
648 return TRUE;
651 return FALSE;
654 /***********************************************************************
655 * DOSMEM_ResizeBlock
657 LPVOID DOSMEM_ResizeBlock(void* ptr, UINT size, UINT16* pseg)
659 char *block = NULL;
660 dosmem_info *info_block = DOSMEM_InfoBlock();
662 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
663 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
664 - DOSMEM_dosmem) & 0xf) )
666 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
668 if( pseg ) *pseg = ((char*)ptr - DOSMEM_dosmem) >> 4;
670 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
673 dosmem_entry *next = NEXT_BLOCK(dm);
674 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
676 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
678 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
679 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
680 next = NEXT_BLOCK(dm);
683 blocksize = dm->size & DM_BLOCK_MASK;
684 if (blocksize >= size)
686 block = ((char*)dm) + sizeof(dosmem_entry);
687 if( blocksize - size > 0x20 )
689 /* split dm so that the next one stays
690 * paragraph-aligned (and next gains free bit) */
692 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
693 sizeof(dosmem_entry));
694 next = (dosmem_entry*)(((char*)dm) +
695 sizeof(dosmem_entry) + dm->size);
696 next->size = (blocksize - (dm->size +
697 sizeof(dosmem_entry))) | DM_BLOCK_FREE
699 } else dm->size &= DM_BLOCK_MASK;
701 info_block->free += orgsize - dm->size;
702 } else {
703 /* the collapse didn't help, try getting a new block */
704 block = DOSMEM_GetBlock(size, pseg);
705 if (block) {
706 /* we got one, copy the old data there (we do need to, right?) */
707 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
708 (size<orgsize) ? size : orgsize);
709 /* free old block */
710 info_block->blocks--;
711 info_block->free += dm->size;
713 dm->size |= DM_BLOCK_FREE;
714 } else {
715 /* and Bill Gates said 640K should be enough for everyone... */
717 /* need to split original and collapsed blocks apart again,
718 * and free the collapsed blocks again, before exiting */
719 if( blocksize - orgsize > 0x20 )
721 /* split dm so that the next one stays
722 * paragraph-aligned (and next gains free bit) */
724 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
725 sizeof(dosmem_entry));
726 next = (dosmem_entry*)(((char*)dm) +
727 sizeof(dosmem_entry) + dm->size);
728 next->size = (blocksize - (dm->size +
729 sizeof(dosmem_entry))) | DM_BLOCK_FREE
731 } else dm->size &= DM_BLOCK_MASK;
736 return (LPVOID)block;
740 /***********************************************************************
741 * DOSMEM_Available
743 UINT DOSMEM_Available(void)
745 UINT blocksize, available = 0;
746 dosmem_entry *dm;
748 dm = DOSMEM_RootBlock();
750 while (dm && dm->size != DM_BLOCK_TERMINAL)
752 #ifdef __DOSMEM_DEBUG__
753 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
755 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
756 return NULL;
758 prev = dm;
759 #endif
760 if( dm->size & DM_BLOCK_FREE )
762 dosmem_entry *next = NEXT_BLOCK(dm);
764 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
766 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
767 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
768 next = NEXT_BLOCK(dm);
771 blocksize = dm->size & DM_BLOCK_MASK;
772 if ( blocksize > available ) available = blocksize;
773 dm = next;
775 else dm = NEXT_BLOCK(dm);
777 return available;
781 /***********************************************************************
782 * DOSMEM_MapLinearToDos
784 * Linear address to the DOS address space.
786 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
788 if (((char*)ptr >= DOSMEM_dosmem) &&
789 ((char*)ptr < DOSMEM_dosmem + 0x100000))
790 return (UINT)ptr - (UINT)DOSMEM_dosmem;
791 return (UINT)ptr;
795 /***********************************************************************
796 * DOSMEM_MapDosToLinear
798 * DOS linear address to the linear address space.
800 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
802 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
803 return (LPVOID)ptr;
807 /***********************************************************************
808 * DOSMEM_MapRealToLinear
810 * Real mode DOS address into a linear pointer
812 LPVOID DOSMEM_MapRealToLinear(DWORD x)
814 LPVOID lin;
816 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
817 TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
818 return lin;
821 /***********************************************************************
822 * DOSMEM_AllocSelector
824 * Allocates a protected mode selector for a realmode segment.
826 WORD DOSMEM_AllocSelector(WORD realsel)
828 HMODULE16 hModule = GetModuleHandle16("KERNEL");
829 WORD sel;
831 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
832 hModule, WINE_LDT_FLAGS_DATA );
833 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
834 return sel;