4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
13 #ifdef HAVE_SYS_MMAN_H
14 # include <sys/mman.h>
18 #include "wine/winbase16.h"
22 #include "selectors.h"
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(dosmem
);
29 DECLARE_DEBUG_CHANNEL(selector
);
31 WORD DOSMEM_0000H
; /* segment at 0:0 */
32 WORD DOSMEM_BiosDataSeg
; /* BIOS data segment at 0x40:0 */
33 WORD DOSMEM_BiosSysSeg
; /* BIOS ROM segment at 0xf000:0 */
35 DWORD DOSMEM_CollateTable
;
37 /* use 2 low bits of 'size' for the housekeeping */
39 #define DM_BLOCK_DEBUG 0xABE00000
40 #define DM_BLOCK_TERMINAL 0x00000001
41 #define DM_BLOCK_FREE 0x00000002
42 #define DM_BLOCK_MASK 0x001FFFFC
45 #define __DOSMEM_DEBUG__
57 #define NEXT_BLOCK(block) \
58 (dosmem_entry*)(((char*)(block)) + \
59 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
61 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
62 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
65 static char *DOSMEM_dosmem
;
66 /* bios area; normally at offset 0x400, but can be moved to trap NULL pointers */
67 static void *DOSMEM_biosdata
;
69 /* various real-mode code stubs */
75 /***********************************************************************
78 * Gets the DOS memory base.
80 char *DOSMEM_MemoryBase(void)
85 /***********************************************************************
88 * Gets the DOS memory top.
90 static char *DOSMEM_MemoryTop(void)
92 return DOSMEM_dosmem
+0x9FFFC; /* 640K */
95 /***********************************************************************
98 * Gets the DOS memory info block.
100 static dosmem_info
*DOSMEM_InfoBlock(void)
102 return (dosmem_info
*)(DOSMEM_dosmem
+0x10000); /* 64K */
105 /***********************************************************************
108 * Gets the DOS memory root block.
110 static dosmem_entry
*DOSMEM_RootBlock(void)
112 /* first block has to be paragraph-aligned */
113 return (dosmem_entry
*)(((char*)DOSMEM_InfoBlock()) +
114 ((((sizeof(dosmem_info
) + 0xf) & ~0xf) - sizeof(dosmem_entry
))));
117 /***********************************************************************
118 * DOSMEM_FillIsrTable
120 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
123 * Linux normally only traps INTs performed from or destined to BIOSSEG
124 * for us to handle, if the int_revectored table is empty. Filling the
125 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
126 * to hook interrupts, as well as use their familiar retf tricks to call
127 * them, AND let Wine handle any unhooked interrupts transparently.
129 static void DOSMEM_FillIsrTable(void)
131 SEGPTR
*isr
= (SEGPTR
*)DOSMEM_dosmem
;
132 DWORD
*stub
= (DWORD
*)((char*)isr
+ (VM_STUB_SEGMENT
<< 4));
135 for (x
=0; x
<256; x
++) isr
[x
]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT
,x
*4);
136 for (x
=0; x
<256; x
++) stub
[x
]=VM_STUB(x
);
139 /***********************************************************************
142 * Allocate the global DPMI RMCB wrapper.
144 static void DOSMEM_InitDPMI(void)
148 static const char wrap_code
[]={
149 0xCD,0x31, /* int $0x31 */
153 static const char enter_xms
[]=
155 /* XMS hookable entry point */
156 0xEB,0x03, /* jmp entry */
157 0x90,0x90,0x90, /* nop;nop;nop */
159 /* real entry point */
160 /* for simplicity, we'll just use the same hook as DPMI below */
161 0xCD,0x31, /* int $0x31 */
165 static const char enter_pm
[]=
167 0x50, /* pushw %ax */
168 0x52, /* pushw %dx */
169 0x55, /* pushw %bp */
170 0x89,0xE5, /* movw %sp,%bp */
172 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
173 /* just call int 31 here to get into protected mode... */
174 /* it'll check whether it was called from dpmi_seg... */
175 0xCD,0x31, /* int $0x31 */
176 /* we are now in the context of a 16-bit relay call */
177 /* need to fixup our stack;
178 * 16-bit relay return address will be lost, but we won't worry quite yet */
179 0x8E,0xD0, /* movw %ax,%ss */
180 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
182 0x89,0x56,0x08, /* movw %dx,8(%bp) */
189 ptr
= DOSMEM_GetBlock( sizeof(wrap_code
), &DOSMEM_wrap_seg
);
190 memcpy( ptr
, wrap_code
, sizeof(wrap_code
) );
191 ptr
= DOSMEM_GetBlock( sizeof(enter_xms
), &DOSMEM_xms_seg
);
192 memcpy( ptr
, enter_xms
, sizeof(enter_xms
) );
193 ptr
= DOSMEM_GetBlock( sizeof(enter_pm
), &DOSMEM_dpmi_seg
);
194 memcpy( ptr
, enter_pm
, sizeof(enter_pm
) );
195 DOSMEM_dpmi_sel
= SELECTOR_AllocBlock( ptr
, sizeof(enter_pm
), SEGMENT_CODE
, FALSE
, FALSE
);
198 BIOSDATA
* DOSMEM_BiosData()
200 return (BIOSDATA
*)DOSMEM_biosdata
;
203 BYTE
* DOSMEM_BiosSys()
205 return DOSMEM_dosmem
+0xf0000;
208 struct _DOS_LISTOFLISTS
* DOSMEM_LOL()
210 return (struct _DOS_LISTOFLISTS
*)DOSMEM_MapRealToLinear
211 (PTR_SEG_OFF_TO_SEGPTR(HIWORD(DOS_LOLSeg
),0));
214 /***********************************************************************
215 * DOSMEM_FillBiosSegments
217 * Fill the BIOS data segment with dummy values.
219 static void DOSMEM_FillBiosSegments(void)
221 BYTE
*pBiosSys
= DOSMEM_BiosSys();
222 BYTE
*pBiosROMTable
= pBiosSys
+0xe6f5;
223 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
225 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
226 VIDEOFUNCTIONALITY
*pVidFunc
= (VIDEOFUNCTIONALITY
*)(pBiosSys
+0xe000);
227 VIDEOSTATE
*pVidState
= (VIDEOSTATE
*)(pBiosSys
+0xe010);
230 /* Clear all unused values */
231 memset( pBiosData
, 0, sizeof(*pBiosData
) );
232 memset( pVidFunc
, 0, sizeof(*pVidFunc
) );
233 memset( pVidState
, 0, sizeof(*pVidState
) );
235 /* FIXME: should check the number of configured drives and ports */
237 pBiosData
->Com1Addr
= 0x3f8;
238 pBiosData
->Com2Addr
= 0x2f8;
239 pBiosData
->Lpt1Addr
= 0x378;
240 pBiosData
->Lpt2Addr
= 0x278;
241 pBiosData
->InstalledHardware
= 0x5463;
242 pBiosData
->MemSize
= 640;
243 pBiosData
->NextKbdCharPtr
= 0x1e;
244 pBiosData
->FirstKbdCharPtr
= 0x1e;
245 pBiosData
->VideoMode
= 3;
246 pBiosData
->VideoColumns
= 80;
247 pBiosData
->VideoPageSize
= 80 * 25 * 2;
248 pBiosData
->VideoPageStartAddr
= 0xb800;
249 pBiosData
->VideoCtrlAddr
= 0x3d4;
250 pBiosData
->Ticks
= INT1A_GetTicksSinceMidnight();
251 pBiosData
->NbHardDisks
= 2;
252 pBiosData
->KbdBufferStart
= 0x1e;
253 pBiosData
->KbdBufferEnd
= 0x3e;
254 pBiosData
->RowsOnScreenMinus1
= 23;
255 pBiosData
->BytesPerChar
= 0x10;
256 pBiosData
->ModeOptions
= 0x64;
257 pBiosData
->FeatureBitsSwitches
= 0xf9;
258 pBiosData
->VGASettings
= 0x51;
259 pBiosData
->DisplayCombination
= 0x08;
260 pBiosData
->DiskDataRate
= 0;
262 /* fill ROM configuration table (values from Award) */
263 *(pBiosROMTable
+0x0) = 0x08; /* number of bytes following LO */
264 *(pBiosROMTable
+0x1) = 0x00; /* number of bytes following HI */
265 *(pBiosROMTable
+0x2) = 0xfc; /* model */
266 *(pBiosROMTable
+0x3) = 0x01; /* submodel */
267 *(pBiosROMTable
+0x4) = 0x00; /* BIOS revision */
268 *(pBiosROMTable
+0x5) = 0x74; /* feature byte 1 */
269 *(pBiosROMTable
+0x6) = 0x00; /* feature byte 2 */
270 *(pBiosROMTable
+0x7) = 0x00; /* feature byte 3 */
271 *(pBiosROMTable
+0x8) = 0x00; /* feature byte 4 */
272 *(pBiosROMTable
+0x9) = 0x00; /* feature byte 5 */
275 for (i
= 0; i
< 7; i
++)
276 pVidFunc
->ModeSupport
[i
] = 0xff;
278 pVidFunc
->ScanlineSupport
= 7;
279 pVidFunc
->NumberCharBlocks
= 0;
280 pVidFunc
->ActiveCharBlocks
= 0;
281 pVidFunc
->MiscFlags
= 0x8ff;
282 pVidFunc
->SavePointerFlags
= 0x3f;
284 pVidState
->StaticFuncTable
= 0xf000e000; /* FIXME: always real mode ? */
285 pVidState
->VideoMode
= pBiosData
->VideoMode
; /* needs updates! */
286 pVidState
->NumberColumns
= pBiosData
->VideoColumns
; /* needs updates! */
287 pVidState
->RegenBufLen
= 0;
288 pVidState
->RegenBufAddr
= 0;
290 for (i
= 0; i
< 8; i
++)
291 pVidState
->CursorPos
[i
] = 0;
293 pVidState
->CursorType
= 0x0a0b; /* start/end line */
294 pVidState
->ActivePage
= 0;
295 pVidState
->CRTCPort
= 0x3da;
296 pVidState
->Port3x8
= 0;
297 pVidState
->Port3x9
= 0;
298 pVidState
->NumberRows
= 23; /* number of rows - 1 */
299 pVidState
->BytesPerChar
= 0x10;
300 pVidState
->DCCActive
= pBiosData
->DisplayCombination
;
301 pVidState
->DCCAlternate
= 0;
302 pVidState
->NumberColors
= 16;
303 pVidState
->NumberPages
= 1;
304 pVidState
->NumberScanlines
= 3; /* (0,1,2,3) = (200,350,400,480) */
305 pVidState
->CharBlockPrimary
= 0;
306 pVidState
->CharBlockSecondary
= 0;
307 pVidState
->MiscFlags
=
308 (pBiosData
->VGASettings
& 0x0f)
309 | ((pBiosData
->ModeOptions
& 1) << 4); /* cursor emulation */
310 pVidState
->NonVGASupport
= 0;
311 pVidState
->VideoMem
= (pBiosData
->ModeOptions
& 0x60 >> 5);
312 pVidState
->SavePointerState
= 0;
313 pVidState
->DisplayStatus
= 4;
315 /* BIOS date string */
316 strcpy((char *)pBiosSys
+0xfff5, "13/01/99");
319 *(pBiosSys
+0xfffe) = 0xfc;
322 /***********************************************************************
323 * DOSMEM_InitCollateTable
325 * Initialises the collate table (character sorting, language dependent)
327 static void DOSMEM_InitCollateTable()
333 x
= GlobalDOSAlloc16(258);
334 DOSMEM_CollateTable
= MAKELONG(0,(x
>>16));
335 tbl
= DOSMEM_MapRealToLinear(DOSMEM_CollateTable
);
338 for ( i
= 0; i
< 0x100; i
++) *tbl
++ = i
;
341 /***********************************************************************
342 * DOSMEM_InitErrorTable
344 * Initialises the error tables (DOS 5+)
346 static void DOSMEM_InitErrorTable()
348 #if 0 /* no longer used */
352 /* We will use a snippet of real mode code that calls */
353 /* a WINE-only interrupt to handle moving the requested */
354 /* message into the buffer... */
356 /* FIXME - There is still something wrong... */
358 /* FIXME - Find hex values for opcodes...
360 (On call, AX contains message number
361 DI contains 'offset' (??)
362 Resturn, ES:DI points to counted string )
366 MOV AX, (arbitrary subfunction number)
367 INT (WINE-only interrupt)
374 const int buffer
= 80;
375 const int SIZE_TO_ALLOCATE
= code
+ buffer
;
377 /* FIXME - Complete rewrite of the table system to save */
378 /* precious DOS space. Now, we return the 0001:???? as */
379 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
380 /* as a special case and programs will use the alternate */
381 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
382 /* 0x08) which lets us have a smaller memory footprint anyway. */
384 x
= GlobalDOSAlloc16(SIZE_TO_ALLOCATE
);
386 DOSMEM_ErrorCall
= MAKELONG(0,(x
>>16));
387 DOSMEM_ErrorBuffer
= DOSMEM_ErrorCall
+ code
;
389 call
= DOSMEM_MapRealToLinear(DOSMEM_ErrorCall
);
391 memset(call
, 0, SIZE_TO_ALLOCATE
);
393 /* Fixme - Copy assembly into buffer here */
396 /***********************************************************************
399 * Initialises the DOS memory structures.
401 static void DOSMEM_InitMemory(void)
403 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
404 * 1000:0000 and ends at 9FFF:FFEF. */
406 dosmem_info
* info_block
= DOSMEM_InfoBlock();
407 dosmem_entry
* root_block
= DOSMEM_RootBlock();
410 root_block
->size
= DOSMEM_MemoryTop() - (((char*)root_block
) + sizeof(dosmem_entry
));
412 info_block
->blocks
= 0;
413 info_block
->free
= root_block
->size
;
415 dm
= NEXT_BLOCK(root_block
);
416 dm
->size
= DM_BLOCK_TERMINAL
;
417 root_block
->size
|= DM_BLOCK_FREE
418 #ifdef __DOSMEM_DEBUG__
424 /**********************************************************************
427 * Setup the first megabyte for DOS memory access
429 static void setup_dos_mem( int dos_init
)
431 int bios_offset
= 0x400;
432 int page_size
= VIRTUAL_GetPageSize();
433 void *addr
= VIRTUAL_mmap( -1, (void *)page_size
, 0x110000-page_size
, 0,
434 PROT_READ
| PROT_WRITE
| PROT_EXEC
, 0 );
435 if (addr
== (void *)page_size
) /* we got what we wanted */
437 /* now map from address 0 */
438 addr
= VIRTUAL_mmap( -1, NULL
, 0x110000, 0,
439 PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_FIXED
);
442 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
446 /* inform the memory manager that there is a mapping here */
447 VirtualAlloc( addr
, 0x110000, MEM_RESERVE
| MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
449 /* protect the first 64K to catch NULL pointers */
452 VirtualProtect( addr
, 0x10000, PAGE_NOACCESS
, NULL
);
453 /* move the BIOS data from 0x400 to 0xf0400 */
454 bios_offset
+= 0xf0000;
459 ERR("Cannot use first megabyte for DOS address space, please report\n" );
460 if (dos_init
) ExitProcess(1);
461 /* allocate the DOS area somewhere else */
462 addr
= VirtualAlloc( NULL
, 0x110000, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
465 ERR( "Cannot allocate DOS memory\n" );
469 DOSMEM_dosmem
= addr
;
470 DOSMEM_biosdata
= (char *)addr
+ bios_offset
;
474 /***********************************************************************
477 * Create the dos memory segments, and store them into the KERNEL
480 BOOL
DOSMEM_Init(BOOL dos_init
)
482 static int already_done
;
486 setup_dos_mem( dos_init
);
488 DOSMEM_0000H
= GLOBAL_CreateBlock( GMEM_FIXED
, DOSMEM_biosdata
- 0x400,
489 0x10000, 0, FALSE
, FALSE
, FALSE
);
490 DOSMEM_BiosDataSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_biosdata
,
491 0x100, 0, FALSE
, FALSE
, FALSE
);
492 DOSMEM_BiosSysSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_dosmem
+0xf0000,
493 0x10000, 0, FALSE
, FALSE
, FALSE
);
494 DOSMEM_FillBiosSegments();
496 DOSMEM_InitCollateTable();
497 DOSMEM_InitErrorTable();
499 DOSDEV_InstallDOSDevices();
506 ERR( "Needs access to the first megabyte for DOS mode\n" );
509 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
510 " NULL pointer accesses will no longer be caught.\n" );
511 VirtualProtect( NULL
, 0x10000, PAGE_EXECUTE_READWRITE
, NULL
);
512 /* copy the BIOS area down to 0x400 */
513 memcpy( DOSMEM_dosmem
+ 0x400, DOSMEM_biosdata
, 0x100 );
514 DOSMEM_biosdata
= DOSMEM_dosmem
+ 0x400;
515 SetSelectorBase( DOSMEM_0000H
, 0 );
516 SetSelectorBase( DOSMEM_BiosDataSeg
, 0x400 );
518 /* interrupt table is at addr 0, so only do this when setting up DOS mode */
519 if (dos_init
) DOSMEM_FillIsrTable();
524 /***********************************************************************
527 * Increment the BIOS tick counter. Called by timer signal handler.
529 void DOSMEM_Tick( WORD timer
)
531 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
532 if (pBiosData
) pBiosData
->Ticks
++;
535 /***********************************************************************
538 * Carve a chunk of the DOS memory block (without selector).
540 LPVOID
DOSMEM_GetBlock(UINT size
, UINT16
* pseg
)
544 dosmem_info
*info_block
= DOSMEM_InfoBlock();
546 #ifdef __DOSMEM_DEBUG_
547 dosmem_entry
*prev
= NULL
;
550 if( size
> info_block
->free
) return NULL
;
551 dm
= DOSMEM_RootBlock();
553 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
555 #ifdef __DOSMEM_DEBUG__
556 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
558 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
563 if( dm
->size
& DM_BLOCK_FREE
)
565 dosmem_entry
*next
= NEXT_BLOCK(dm
);
567 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
569 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
570 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
571 next
= NEXT_BLOCK(dm
);
574 blocksize
= dm
->size
& DM_BLOCK_MASK
;
575 if( blocksize
>= size
)
577 block
= ((char*)dm
) + sizeof(dosmem_entry
);
578 if( blocksize
- size
> 0x20 )
580 /* split dm so that the next one stays
581 * paragraph-aligned (and dm loses free bit) */
583 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
584 sizeof(dosmem_entry
));
585 next
= (dosmem_entry
*)(((char*)dm
) +
586 sizeof(dosmem_entry
) + dm
->size
);
587 next
->size
= (blocksize
- (dm
->size
+
588 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
589 #ifdef __DOSMEM_DEBUG__
593 } else dm
->size
&= DM_BLOCK_MASK
;
595 info_block
->blocks
++;
596 info_block
->free
-= dm
->size
;
597 if( pseg
) *pseg
= (block
- DOSMEM_dosmem
) >> 4;
598 #ifdef __DOSMEM_DEBUG__
599 dm
->size
|= DM_BLOCK_DEBUG
;
605 else dm
= NEXT_BLOCK(dm
);
607 return (LPVOID
)block
;
610 /***********************************************************************
613 BOOL
DOSMEM_FreeBlock(void* ptr
)
615 dosmem_info
*info_block
= DOSMEM_InfoBlock();
617 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
618 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
619 - DOSMEM_dosmem
) & 0xf) )
621 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
623 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
624 #ifdef __DOSMEM_DEBUG__
625 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
629 info_block
->blocks
--;
630 info_block
->free
+= dm
->size
;
632 dm
->size
|= DM_BLOCK_FREE
;
639 /***********************************************************************
642 LPVOID
DOSMEM_ResizeBlock(void* ptr
, UINT size
, UINT16
* pseg
)
645 dosmem_info
*info_block
= DOSMEM_InfoBlock();
647 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
648 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
649 - DOSMEM_dosmem
) & 0xf) )
651 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
653 if( pseg
) *pseg
= ((char*)ptr
- DOSMEM_dosmem
) >> 4;
655 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
658 dosmem_entry
*next
= NEXT_BLOCK(dm
);
659 UINT blocksize
, orgsize
= dm
->size
& DM_BLOCK_MASK
;
661 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
663 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
664 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
665 next
= NEXT_BLOCK(dm
);
668 blocksize
= dm
->size
& DM_BLOCK_MASK
;
669 if (blocksize
>= size
)
671 block
= ((char*)dm
) + sizeof(dosmem_entry
);
672 if( blocksize
- size
> 0x20 )
674 /* split dm so that the next one stays
675 * paragraph-aligned (and next gains free bit) */
677 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
678 sizeof(dosmem_entry
));
679 next
= (dosmem_entry
*)(((char*)dm
) +
680 sizeof(dosmem_entry
) + dm
->size
);
681 next
->size
= (blocksize
- (dm
->size
+
682 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
684 } else dm
->size
&= DM_BLOCK_MASK
;
686 info_block
->free
+= orgsize
- dm
->size
;
688 /* the collapse didn't help, try getting a new block */
689 block
= DOSMEM_GetBlock(size
, pseg
);
691 /* we got one, copy the old data there (we do need to, right?) */
692 memcpy(block
, ((char*)dm
) + sizeof(dosmem_entry
),
693 (size
<orgsize
) ? size
: orgsize
);
695 info_block
->blocks
--;
696 info_block
->free
+= dm
->size
;
698 dm
->size
|= DM_BLOCK_FREE
;
700 /* and Bill Gates said 640K should be enough for everyone... */
702 /* need to split original and collapsed blocks apart again,
703 * and free the collapsed blocks again, before exiting */
704 if( blocksize
- orgsize
> 0x20 )
706 /* split dm so that the next one stays
707 * paragraph-aligned (and next gains free bit) */
709 dm
->size
= (((orgsize
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
710 sizeof(dosmem_entry
));
711 next
= (dosmem_entry
*)(((char*)dm
) +
712 sizeof(dosmem_entry
) + dm
->size
);
713 next
->size
= (blocksize
- (dm
->size
+
714 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
716 } else dm
->size
&= DM_BLOCK_MASK
;
721 return (LPVOID
)block
;
725 /***********************************************************************
728 UINT
DOSMEM_Available(void)
730 UINT blocksize
, available
= 0;
733 dm
= DOSMEM_RootBlock();
735 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
737 #ifdef __DOSMEM_DEBUG__
738 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
740 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
745 if( dm
->size
& DM_BLOCK_FREE
)
747 dosmem_entry
*next
= NEXT_BLOCK(dm
);
749 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
751 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
752 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
753 next
= NEXT_BLOCK(dm
);
756 blocksize
= dm
->size
& DM_BLOCK_MASK
;
757 if ( blocksize
> available
) available
= blocksize
;
760 else dm
= NEXT_BLOCK(dm
);
766 /***********************************************************************
767 * DOSMEM_MapLinearToDos
769 * Linear address to the DOS address space.
771 UINT
DOSMEM_MapLinearToDos(LPVOID ptr
)
773 if (((char*)ptr
>= DOSMEM_dosmem
) &&
774 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
775 return (UINT
)ptr
- (UINT
)DOSMEM_dosmem
;
780 /***********************************************************************
781 * DOSMEM_MapDosToLinear
783 * DOS linear address to the linear address space.
785 LPVOID
DOSMEM_MapDosToLinear(UINT ptr
)
787 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT
)DOSMEM_dosmem
);
792 /***********************************************************************
793 * DOSMEM_MapRealToLinear
795 * Real mode DOS address into a linear pointer
797 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
801 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
802 TRACE_(selector
)("(0x%08lx) returns 0x%p.\n", x
, lin
);
806 /***********************************************************************
807 * DOSMEM_AllocSelector
809 * Allocates a protected mode selector for a realmode segment.
811 WORD
DOSMEM_AllocSelector(WORD realsel
)
813 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
816 sel
=GLOBAL_CreateBlock(
817 GMEM_FIXED
,DOSMEM_dosmem
+realsel
*16,0x10000,
818 hModule
,FALSE
,FALSE
,FALSE
);
819 TRACE_(selector
)("(0x%04x) returns 0x%04x.\n", realsel
,sel
);