4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
13 #include <sys/types.h>
14 #ifdef HAVE_SYS_MMAN_H
15 # include <sys/mman.h>
19 #include "wine/winbase16.h"
23 #include "selectors.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(dosmem
);
30 DECLARE_DEBUG_CHANNEL(selector
);
32 WORD DOSMEM_0000H
; /* segment at 0:0 */
33 WORD DOSMEM_BiosDataSeg
; /* BIOS data segment at 0x40:0 */
34 WORD DOSMEM_BiosSysSeg
; /* BIOS ROM segment at 0xf000:0 */
36 DWORD DOSMEM_CollateTable
;
38 /* use 2 low bits of 'size' for the housekeeping */
40 #define DM_BLOCK_DEBUG 0xABE00000
41 #define DM_BLOCK_TERMINAL 0x00000001
42 #define DM_BLOCK_FREE 0x00000002
43 #define DM_BLOCK_MASK 0x001FFFFC
46 #define __DOSMEM_DEBUG__
58 #define NEXT_BLOCK(block) \
59 (dosmem_entry*)(((char*)(block)) + \
60 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
62 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
63 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
66 static char *DOSMEM_dosmem
;
67 /* DOS system base (for interrupt vector table and BIOS data area)
68 * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
69 * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
70 * and only relocated to NULL when absolutely necessary */
71 static char *DOSMEM_sysmem
;
73 /* various real-mode code stubs */
79 /***********************************************************************
82 * Gets the virtual DOS memory base (interrupt table).
84 char *DOSMEM_SystemBase(void)
89 /***********************************************************************
92 * Gets the DOS memory base.
94 char *DOSMEM_MemoryBase(void)
99 /***********************************************************************
102 * Gets the DOS memory top.
104 static char *DOSMEM_MemoryTop(void)
106 return DOSMEM_dosmem
+0x9FFFC; /* 640K */
109 /***********************************************************************
112 * Gets the DOS memory info block.
114 static dosmem_info
*DOSMEM_InfoBlock(void)
116 return (dosmem_info
*)(DOSMEM_dosmem
+0x10000); /* 64K */
119 /***********************************************************************
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).
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
;
148 for (x
=0; x
<256; x
++) isr
[x
]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT
,x
*4);
151 static void DOSMEM_MakeIsrStubs(void)
153 DWORD
*stub
= (DWORD
*)(DOSMEM_dosmem
+ (VM_STUB_SEGMENT
<< 4));
156 for (x
=0; x
<256; x
++) stub
[x
]=VM_STUB(x
);
159 /***********************************************************************
162 * Allocate the global DPMI RMCB wrapper.
164 static void DOSMEM_InitDPMI(void)
168 static const char wrap_code
[]={
169 0xCD,0x31, /* int $0x31 */
173 static const char enter_xms
[]=
175 /* XMS hookable entry point */
176 0xEB,0x03, /* jmp entry */
177 0x90,0x90,0x90, /* nop;nop;nop */
179 /* real entry point */
180 /* for simplicity, we'll just use the same hook as DPMI below */
181 0xCD,0x31, /* int $0x31 */
185 static const char enter_pm
[]=
187 0x50, /* pushw %ax */
188 0x52, /* pushw %dx */
189 0x55, /* pushw %bp */
190 0x89,0xE5, /* movw %sp,%bp */
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 */
202 0x89,0x56,0x08, /* movw %dx,8(%bp) */
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 struct _DOS_LISTOFLISTS
* DOSMEM_LOL()
230 return (struct _DOS_LISTOFLISTS
*)DOSMEM_MapRealToLinear
231 (PTR_SEG_OFF_TO_SEGPTR(HIWORD(DOS_LOLSeg
),0));
234 /***********************************************************************
235 * DOSMEM_FillBiosSegments
237 * Fill the BIOS data segment with dummy values.
239 static void DOSMEM_FillBiosSegments(void)
241 BYTE
*pBiosSys
= DOSMEM_BiosSys();
242 BYTE
*pBiosROMTable
= pBiosSys
+0xe6f5;
243 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
245 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
246 VIDEOFUNCTIONALITY
*pVidFunc
= (VIDEOFUNCTIONALITY
*)(pBiosSys
+0xe000);
247 VIDEOSTATE
*pVidState
= (VIDEOSTATE
*)(pBiosSys
+0xe010);
250 /* Clear all unused values */
251 memset( pBiosData
, 0, sizeof(*pBiosData
) );
252 memset( pVidFunc
, 0, sizeof(*pVidFunc
) );
253 memset( pVidState
, 0, sizeof(*pVidState
) );
255 /* FIXME: should check the number of configured drives and ports */
257 pBiosData
->Com1Addr
= 0x3f8;
258 pBiosData
->Com2Addr
= 0x2f8;
259 pBiosData
->Lpt1Addr
= 0x378;
260 pBiosData
->Lpt2Addr
= 0x278;
261 pBiosData
->InstalledHardware
= 0x5463;
262 pBiosData
->MemSize
= 640;
263 pBiosData
->NextKbdCharPtr
= 0x1e;
264 pBiosData
->FirstKbdCharPtr
= 0x1e;
265 pBiosData
->VideoMode
= 3;
266 pBiosData
->VideoColumns
= 80;
267 pBiosData
->VideoPageSize
= 80 * 25 * 2;
268 pBiosData
->VideoPageStartAddr
= 0xb800;
269 pBiosData
->VideoCtrlAddr
= 0x3d4;
270 pBiosData
->Ticks
= INT1A_GetTicksSinceMidnight();
271 pBiosData
->NbHardDisks
= 2;
272 pBiosData
->KbdBufferStart
= 0x1e;
273 pBiosData
->KbdBufferEnd
= 0x3e;
274 pBiosData
->RowsOnScreenMinus1
= 23;
275 pBiosData
->BytesPerChar
= 0x10;
276 pBiosData
->ModeOptions
= 0x64;
277 pBiosData
->FeatureBitsSwitches
= 0xf9;
278 pBiosData
->VGASettings
= 0x51;
279 pBiosData
->DisplayCombination
= 0x08;
280 pBiosData
->DiskDataRate
= 0;
282 /* fill ROM configuration table (values from Award) */
283 *(pBiosROMTable
+0x0) = 0x08; /* number of bytes following LO */
284 *(pBiosROMTable
+0x1) = 0x00; /* number of bytes following HI */
285 *(pBiosROMTable
+0x2) = 0xfc; /* model */
286 *(pBiosROMTable
+0x3) = 0x01; /* submodel */
287 *(pBiosROMTable
+0x4) = 0x00; /* BIOS revision */
288 *(pBiosROMTable
+0x5) = 0x74; /* feature byte 1 */
289 *(pBiosROMTable
+0x6) = 0x00; /* feature byte 2 */
290 *(pBiosROMTable
+0x7) = 0x00; /* feature byte 3 */
291 *(pBiosROMTable
+0x8) = 0x00; /* feature byte 4 */
292 *(pBiosROMTable
+0x9) = 0x00; /* feature byte 5 */
295 for (i
= 0; i
< 7; i
++)
296 pVidFunc
->ModeSupport
[i
] = 0xff;
298 pVidFunc
->ScanlineSupport
= 7;
299 pVidFunc
->NumberCharBlocks
= 0;
300 pVidFunc
->ActiveCharBlocks
= 0;
301 pVidFunc
->MiscFlags
= 0x8ff;
302 pVidFunc
->SavePointerFlags
= 0x3f;
304 pVidState
->StaticFuncTable
= 0xf000e000; /* FIXME: always real mode ? */
305 pVidState
->VideoMode
= pBiosData
->VideoMode
; /* needs updates! */
306 pVidState
->NumberColumns
= pBiosData
->VideoColumns
; /* needs updates! */
307 pVidState
->RegenBufLen
= 0;
308 pVidState
->RegenBufAddr
= 0;
310 for (i
= 0; i
< 8; i
++)
311 pVidState
->CursorPos
[i
] = 0;
313 pVidState
->CursorType
= 0x0a0b; /* start/end line */
314 pVidState
->ActivePage
= 0;
315 pVidState
->CRTCPort
= 0x3da;
316 pVidState
->Port3x8
= 0;
317 pVidState
->Port3x9
= 0;
318 pVidState
->NumberRows
= 23; /* number of rows - 1 */
319 pVidState
->BytesPerChar
= 0x10;
320 pVidState
->DCCActive
= pBiosData
->DisplayCombination
;
321 pVidState
->DCCAlternate
= 0;
322 pVidState
->NumberColors
= 16;
323 pVidState
->NumberPages
= 1;
324 pVidState
->NumberScanlines
= 3; /* (0,1,2,3) = (200,350,400,480) */
325 pVidState
->CharBlockPrimary
= 0;
326 pVidState
->CharBlockSecondary
= 0;
327 pVidState
->MiscFlags
=
328 (pBiosData
->VGASettings
& 0x0f)
329 | ((pBiosData
->ModeOptions
& 1) << 4); /* cursor emulation */
330 pVidState
->NonVGASupport
= 0;
331 pVidState
->VideoMem
= (pBiosData
->ModeOptions
& 0x60 >> 5);
332 pVidState
->SavePointerState
= 0;
333 pVidState
->DisplayStatus
= 4;
335 /* BIOS date string */
336 strcpy((char *)pBiosSys
+0xfff5, "13/01/99");
339 *(pBiosSys
+0xfffe) = 0xfc;
342 /***********************************************************************
343 * DOSMEM_InitCollateTable
345 * Initialises the collate table (character sorting, language dependent)
347 static void DOSMEM_InitCollateTable()
353 x
= GlobalDOSAlloc16(258);
354 DOSMEM_CollateTable
= MAKELONG(0,(x
>>16));
355 tbl
= DOSMEM_MapRealToLinear(DOSMEM_CollateTable
);
358 for ( i
= 0; i
< 0x100; i
++) *tbl
++ = i
;
361 /***********************************************************************
362 * DOSMEM_InitErrorTable
364 * Initialises the error tables (DOS 5+)
366 static void DOSMEM_InitErrorTable()
368 #if 0 /* no longer used */
372 /* We will use a snippet of real mode code that calls */
373 /* a WINE-only interrupt to handle moving the requested */
374 /* message into the buffer... */
376 /* FIXME - There is still something wrong... */
378 /* FIXME - Find hex values for opcodes...
380 (On call, AX contains message number
381 DI contains 'offset' (??)
382 Resturn, ES:DI points to counted string )
386 MOV AX, (arbitrary subfunction number)
387 INT (WINE-only interrupt)
394 const int buffer
= 80;
395 const int SIZE_TO_ALLOCATE
= code
+ buffer
;
397 /* FIXME - Complete rewrite of the table system to save */
398 /* precious DOS space. Now, we return the 0001:???? as */
399 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
400 /* as a special case and programs will use the alternate */
401 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
402 /* 0x08) which lets us have a smaller memory footprint anyway. */
404 x
= GlobalDOSAlloc16(SIZE_TO_ALLOCATE
);
406 DOSMEM_ErrorCall
= MAKELONG(0,(x
>>16));
407 DOSMEM_ErrorBuffer
= DOSMEM_ErrorCall
+ code
;
409 call
= DOSMEM_MapRealToLinear(DOSMEM_ErrorCall
);
411 memset(call
, 0, SIZE_TO_ALLOCATE
);
413 /* Fixme - Copy assembly into buffer here */
416 /***********************************************************************
419 * Initialises the DOS memory structures.
421 static void DOSMEM_InitMemory(void)
423 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
424 * 1000:0000 and ends at 9FFF:FFEF. */
426 dosmem_info
* info_block
= DOSMEM_InfoBlock();
427 dosmem_entry
* root_block
= DOSMEM_RootBlock();
430 root_block
->size
= DOSMEM_MemoryTop() - (((char*)root_block
) + sizeof(dosmem_entry
));
432 info_block
->blocks
= 0;
433 info_block
->free
= root_block
->size
;
435 dm
= NEXT_BLOCK(root_block
);
436 dm
->size
= DM_BLOCK_TERMINAL
;
437 root_block
->size
|= DM_BLOCK_FREE
438 #ifdef __DOSMEM_DEBUG__
444 /**********************************************************************
447 * Setup the first megabyte for DOS memory access
449 static void setup_dos_mem( int dos_init
)
452 int page_size
= VIRTUAL_GetPageSize();
453 void *addr
= VIRTUAL_mmap( -1, (void *)page_size
, 0x110000-page_size
, 0,
454 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
455 if (addr
== (void *)page_size
) /* we got what we wanted */
457 /* now map from address 0 */
458 addr
= VIRTUAL_mmap( -1, NULL
, 0x110000, 0,
459 PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_FIXED
);
462 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
466 /* inform the memory manager that there is a mapping here */
467 VirtualAlloc( addr
, 0x110000, MEM_RESERVE
| MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
469 /* protect the first 64K to catch NULL pointers */
472 VirtualProtect( addr
, 0x10000, PAGE_NOACCESS
, NULL
);
473 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
474 sys_offset
+= 0xf0000;
479 ERR("Cannot use first megabyte for DOS address space, please report\n" );
480 if (dos_init
) ExitProcess(1);
481 /* allocate the DOS area somewhere else */
482 addr
= VirtualAlloc( NULL
, 0x110000, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
485 ERR( "Cannot allocate DOS memory\n" );
489 DOSMEM_dosmem
= addr
;
490 DOSMEM_sysmem
= (char*)addr
+ sys_offset
;
494 /***********************************************************************
497 * Create the dos memory segments, and store them into the KERNEL
500 BOOL
DOSMEM_Init(BOOL dos_init
)
502 static int already_done
, already_mapped
;
506 setup_dos_mem( dos_init
);
508 DOSMEM_0000H
= GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_sysmem
,
509 0x10000, 0, WINE_LDT_FLAGS_DATA
);
510 DOSMEM_BiosDataSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_sysmem
+ 0x400,
511 0x100, 0, WINE_LDT_FLAGS_DATA
);
512 DOSMEM_BiosSysSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_dosmem
+0xf0000,
513 0x10000, 0, WINE_LDT_FLAGS_DATA
);
514 DOSMEM_FillBiosSegments();
515 DOSMEM_FillIsrTable();
517 DOSMEM_InitCollateTable();
518 DOSMEM_InitErrorTable();
520 DOSDEV_InstallDOSDevices();
523 else if (dos_init
&& !already_mapped
)
527 ERR( "Needs access to the first megabyte for DOS mode\n" );
530 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
531 " NULL pointer accesses will no longer be caught.\n" );
532 VirtualProtect( NULL
, 0x10000, PAGE_EXECUTE_READWRITE
, NULL
);
533 /* copy the BIOS and ISR area down */
534 memcpy( DOSMEM_dosmem
, DOSMEM_sysmem
, 0x400 + 0x100 );
535 DOSMEM_sysmem
= DOSMEM_dosmem
;
536 SetSelectorBase( DOSMEM_0000H
, 0 );
537 SetSelectorBase( DOSMEM_BiosDataSeg
, 0x400 );
538 /* we may now need the actual interrupt stubs, and since we've just moved the
539 * interrupt vector table away, we can fill the area with stubs instead... */
540 DOSMEM_MakeIsrStubs();
547 /***********************************************************************
550 * Increment the BIOS tick counter. Called by timer signal handler.
552 void DOSMEM_Tick( WORD timer
)
554 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
555 if (pBiosData
) pBiosData
->Ticks
++;
558 /***********************************************************************
561 * Carve a chunk of the DOS memory block (without selector).
563 LPVOID
DOSMEM_GetBlock(UINT size
, UINT16
* pseg
)
567 dosmem_info
*info_block
= DOSMEM_InfoBlock();
569 #ifdef __DOSMEM_DEBUG_
570 dosmem_entry
*prev
= NULL
;
573 if( size
> info_block
->free
) return NULL
;
574 dm
= DOSMEM_RootBlock();
576 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
578 #ifdef __DOSMEM_DEBUG__
579 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
581 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
586 if( dm
->size
& DM_BLOCK_FREE
)
588 dosmem_entry
*next
= NEXT_BLOCK(dm
);
590 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
592 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
593 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
594 next
= NEXT_BLOCK(dm
);
597 blocksize
= dm
->size
& DM_BLOCK_MASK
;
598 if( blocksize
>= size
)
600 block
= ((char*)dm
) + sizeof(dosmem_entry
);
601 if( blocksize
- size
> 0x20 )
603 /* split dm so that the next one stays
604 * paragraph-aligned (and dm loses free bit) */
606 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
607 sizeof(dosmem_entry
));
608 next
= (dosmem_entry
*)(((char*)dm
) +
609 sizeof(dosmem_entry
) + dm
->size
);
610 next
->size
= (blocksize
- (dm
->size
+
611 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
612 #ifdef __DOSMEM_DEBUG__
616 } else dm
->size
&= DM_BLOCK_MASK
;
618 info_block
->blocks
++;
619 info_block
->free
-= dm
->size
;
620 if( pseg
) *pseg
= (block
- DOSMEM_dosmem
) >> 4;
621 #ifdef __DOSMEM_DEBUG__
622 dm
->size
|= DM_BLOCK_DEBUG
;
628 else dm
= NEXT_BLOCK(dm
);
630 return (LPVOID
)block
;
633 /***********************************************************************
636 BOOL
DOSMEM_FreeBlock(void* ptr
)
638 dosmem_info
*info_block
= DOSMEM_InfoBlock();
640 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
641 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
642 - DOSMEM_dosmem
) & 0xf) )
644 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
646 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
647 #ifdef __DOSMEM_DEBUG__
648 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
652 info_block
->blocks
--;
653 info_block
->free
+= dm
->size
;
655 dm
->size
|= DM_BLOCK_FREE
;
662 /***********************************************************************
665 LPVOID
DOSMEM_ResizeBlock(void* ptr
, UINT size
, UINT16
* pseg
)
668 dosmem_info
*info_block
= DOSMEM_InfoBlock();
670 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
671 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
672 - DOSMEM_dosmem
) & 0xf) )
674 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
676 if( pseg
) *pseg
= ((char*)ptr
- DOSMEM_dosmem
) >> 4;
678 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
681 dosmem_entry
*next
= NEXT_BLOCK(dm
);
682 UINT blocksize
, orgsize
= dm
->size
& DM_BLOCK_MASK
;
684 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
686 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
687 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
688 next
= NEXT_BLOCK(dm
);
691 blocksize
= dm
->size
& DM_BLOCK_MASK
;
692 if (blocksize
>= size
)
694 block
= ((char*)dm
) + sizeof(dosmem_entry
);
695 if( blocksize
- size
> 0x20 )
697 /* split dm so that the next one stays
698 * paragraph-aligned (and next gains free bit) */
700 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
701 sizeof(dosmem_entry
));
702 next
= (dosmem_entry
*)(((char*)dm
) +
703 sizeof(dosmem_entry
) + dm
->size
);
704 next
->size
= (blocksize
- (dm
->size
+
705 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
707 } else dm
->size
&= DM_BLOCK_MASK
;
709 info_block
->free
+= orgsize
- dm
->size
;
711 /* the collapse didn't help, try getting a new block */
712 block
= DOSMEM_GetBlock(size
, pseg
);
714 /* we got one, copy the old data there (we do need to, right?) */
715 memcpy(block
, ((char*)dm
) + sizeof(dosmem_entry
),
716 (size
<orgsize
) ? size
: orgsize
);
718 info_block
->blocks
--;
719 info_block
->free
+= dm
->size
;
721 dm
->size
|= DM_BLOCK_FREE
;
723 /* and Bill Gates said 640K should be enough for everyone... */
725 /* need to split original and collapsed blocks apart again,
726 * and free the collapsed blocks again, before exiting */
727 if( blocksize
- orgsize
> 0x20 )
729 /* split dm so that the next one stays
730 * paragraph-aligned (and next gains free bit) */
732 dm
->size
= (((orgsize
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
733 sizeof(dosmem_entry
));
734 next
= (dosmem_entry
*)(((char*)dm
) +
735 sizeof(dosmem_entry
) + dm
->size
);
736 next
->size
= (blocksize
- (dm
->size
+
737 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
739 } else dm
->size
&= DM_BLOCK_MASK
;
744 return (LPVOID
)block
;
748 /***********************************************************************
751 UINT
DOSMEM_Available(void)
753 UINT blocksize
, available
= 0;
756 dm
= DOSMEM_RootBlock();
758 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
760 #ifdef __DOSMEM_DEBUG__
761 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
763 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
768 if( dm
->size
& DM_BLOCK_FREE
)
770 dosmem_entry
*next
= NEXT_BLOCK(dm
);
772 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
774 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
775 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
776 next
= NEXT_BLOCK(dm
);
779 blocksize
= dm
->size
& DM_BLOCK_MASK
;
780 if ( blocksize
> available
) available
= blocksize
;
783 else dm
= NEXT_BLOCK(dm
);
789 /***********************************************************************
790 * DOSMEM_MapLinearToDos
792 * Linear address to the DOS address space.
794 UINT
DOSMEM_MapLinearToDos(LPVOID ptr
)
796 if (((char*)ptr
>= DOSMEM_dosmem
) &&
797 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
798 return (UINT
)ptr
- (UINT
)DOSMEM_dosmem
;
803 /***********************************************************************
804 * DOSMEM_MapDosToLinear
806 * DOS linear address to the linear address space.
808 LPVOID
DOSMEM_MapDosToLinear(UINT ptr
)
810 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT
)DOSMEM_dosmem
);
815 /***********************************************************************
816 * DOSMEM_MapRealToLinear
818 * Real mode DOS address into a linear pointer
820 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
824 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
825 TRACE_(selector
)("(0x%08lx) returns 0x%p.\n", x
, lin
);
829 /***********************************************************************
830 * DOSMEM_AllocSelector
832 * Allocates a protected mode selector for a realmode segment.
834 WORD
DOSMEM_AllocSelector(WORD realsel
)
836 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
839 sel
=GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_dosmem
+realsel
*16, 0x10000,
840 hModule
, WINE_LDT_FLAGS_DATA
);
841 TRACE_(selector
)("(0x%04x) returns 0x%04x.\n", realsel
,sel
);