4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
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.
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.
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
23 #include "wine/port.h"
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
41 #include "wine/winbase16.h"
43 #include "kernel_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(dosmem
);
48 WINE_DECLARE_DEBUG_CHANNEL(selector
);
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 */
54 /* use 2 low bits of 'size' for the housekeeping */
56 #define DM_BLOCK_DEBUG 0xABE00000
57 #define DM_BLOCK_TERMINAL 0x00000001
58 #define DM_BLOCK_FREE 0x00000002
59 #define DM_BLOCK_MASK 0x001FFFFC
62 #define __DOSMEM_DEBUG__
74 #define NEXT_BLOCK(block) \
75 (dosmem_entry*)(((char*)(block)) + \
76 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
78 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
79 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
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
;
89 /* Start of DOS conventional memory */
90 static char *DOSMEM_membase
;
92 static void DOSMEM_InitMemory(void);
94 /***********************************************************************
97 * Gets the DOS memory top.
99 static char *DOSMEM_MemoryTop(void)
101 return DOSMEM_dosmem
+0x9FFFC; /* 640K */
104 /***********************************************************************
107 * Gets the DOS memory info block.
109 static dosmem_info
*DOSMEM_InfoBlock(void)
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
122 if (DOSMEM_dosmem
!= DOSMEM_sysmem
)
123 reserve
= 0x10000; /* 64k */
125 reserve
= 0x600; /* 1.5k */
128 * Round to paragraph boundary in order to make
129 * sure the alignment is correct.
131 reserve
= ((reserve
+ 15) >> 4) << 4;
134 * Set DOS memory base and initialize conventional memory.
136 DOSMEM_membase
= DOSMEM_dosmem
+ reserve
;
140 return (dosmem_info
*)DOSMEM_membase
;
143 /***********************************************************************
146 * Gets the DOS memory root block.
148 static dosmem_entry
*DOSMEM_RootBlock(void)
150 /* first block has to be paragraph-aligned */
151 return (dosmem_entry
*)(((char*)DOSMEM_InfoBlock()) +
152 ((((sizeof(dosmem_info
) + 0xf) & ~0xf) - sizeof(dosmem_entry
))));
155 /***********************************************************************
156 * DOSMEM_FillIsrTable
158 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
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.
167 static void DOSMEM_FillIsrTable(void)
169 SEGPTR
*isr
= (SEGPTR
*)DOSMEM_sysmem
;
172 for (x
=0; x
<256; x
++) isr
[x
]=MAKESEGPTR(VM_STUB_SEGMENT
,x
*4);
175 static void DOSMEM_MakeIsrStubs(void)
177 DWORD
*stub
= (DWORD
*)(DOSMEM_dosmem
+ (VM_STUB_SEGMENT
<< 4));
180 for (x
=0; x
<256; x
++) stub
[x
]=VM_STUB(x
);
183 static BIOSDATA
* DOSMEM_BiosData(void)
185 return (BIOSDATA
*)(DOSMEM_sysmem
+ 0x400);
188 /**********************************************************************
189 * DOSMEM_GetTicksSinceMidnight
191 * Return number of clock ticks since midnight.
193 static DWORD
DOSMEM_GetTicksSinceMidnight(void)
199 /* This should give us the (approximately) correct
200 * 18.206 clock ticks per second since midnight.
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);
210 /***********************************************************************
211 * DOSMEM_FillBiosSegments
213 * Fill the BIOS data segment with dummy values.
215 static void DOSMEM_FillBiosSegments(void)
217 BYTE
*pBiosSys
= DOSMEM_dosmem
+ 0xf0000;
218 BYTE
*pBiosROMTable
= pBiosSys
+0xe6f5;
219 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
221 /* Clear all unused values */
222 memset( pBiosData
, 0, sizeof(*pBiosData
) );
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;
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 */
262 /* BIOS date string */
263 strcpy((char *)pBiosSys
+0xfff5, "13/01/99");
266 *(pBiosSys
+0xfffe) = 0xfc;
268 /* Reboot vector (f000:fff0 or ffff:0000) */
269 *(DWORD
*)(pBiosSys
+ 0xfff0) = VM_STUB(0x19);
272 /***********************************************************************
275 * Initialises the DOS memory structures.
277 static void DOSMEM_InitMemory(void)
279 dosmem_info
* info_block
= DOSMEM_InfoBlock();
280 dosmem_entry
* root_block
= DOSMEM_RootBlock();
283 root_block
->size
= DOSMEM_MemoryTop() - (((char*)root_block
) + sizeof(dosmem_entry
));
285 info_block
->blocks
= 0;
286 info_block
->free
= root_block
->size
;
288 dm
= NEXT_BLOCK(root_block
);
289 dm
->size
= DM_BLOCK_TERMINAL
;
290 root_block
->size
|= DM_BLOCK_FREE
291 #ifdef __DOSMEM_DEBUG__
296 TRACE( "DOS conventional memory initialized, %d bytes free.\n",
297 DOSMEM_Available() );
301 /**********************************************************************
304 * Setup the first megabyte for DOS memory access
306 static void setup_dos_mem( int dos_init
)
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 */
314 /* now map from address 0 */
315 addr
= wine_anon_mmap( NULL
, 0x110000, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_FIXED
);
318 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
322 /* inform the memory manager that there is a mapping here */
323 VirtualAlloc( addr
, 0x110000, MEM_RESERVE
| MEM_SYSTEM
, PAGE_EXECUTE_READWRITE
);
325 /* protect the first 64K to catch NULL pointers */
328 VirtualProtect( addr
, 0x10000, PAGE_NOACCESS
, NULL
);
329 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
330 sys_offset
+= 0xf0000;
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
);
341 ERR( "Cannot allocate DOS memory\n" );
345 DOSMEM_dosmem
= addr
;
346 DOSMEM_sysmem
= (char*)addr
+ sys_offset
;
350 /***********************************************************************
353 * Create the dos memory segments, and store them into the KERNEL
356 BOOL
DOSMEM_Init(BOOL dos_init
)
358 static int already_done
, already_mapped
;
362 setup_dos_mem( dos_init
);
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();
374 else if (dos_init
&& !already_mapped
)
378 ERR( "Needs access to the first megabyte for DOS mode\n" );
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();
398 /***********************************************************************
401 * Increment the BIOS tick counter. Called by timer signal handler.
403 void DOSMEM_Tick( WORD timer
)
405 BIOSDATA
*pBiosData
= DOSMEM_BiosData();
406 if (pBiosData
) pBiosData
->Ticks
++;
409 /***********************************************************************
412 * Carve a chunk of the DOS memory block (without selector).
414 LPVOID
DOSMEM_GetBlock(UINT size
, UINT16
* pseg
)
418 dosmem_info
*info_block
= DOSMEM_InfoBlock();
420 #ifdef __DOSMEM_DEBUG_
421 dosmem_entry
*prev
= NULL
;
424 if( size
> info_block
->free
) return NULL
;
425 dm
= DOSMEM_RootBlock();
427 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
429 #ifdef __DOSMEM_DEBUG__
430 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
432 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
437 if( dm
->size
& DM_BLOCK_FREE
)
439 dosmem_entry
*next
= NEXT_BLOCK(dm
);
441 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
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
);
448 blocksize
= dm
->size
& DM_BLOCK_MASK
;
449 if( blocksize
>= size
)
451 block
= ((char*)dm
) + sizeof(dosmem_entry
);
452 if( blocksize
- size
> 0x20 )
454 /* split dm so that the next one stays
455 * paragraph-aligned (and dm loses free bit) */
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__
467 } else dm
->size
&= DM_BLOCK_MASK
;
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
;
479 else dm
= NEXT_BLOCK(dm
);
481 return (LPVOID
)block
;
484 /***********************************************************************
487 BOOL
DOSMEM_FreeBlock(void* ptr
)
489 dosmem_info
*info_block
= DOSMEM_InfoBlock();
491 if( ptr
>= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry
)) &&
492 ptr
< (void*)DOSMEM_MemoryTop() && !((((char*)ptr
)
493 - DOSMEM_dosmem
) & 0xf) )
495 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
497 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
498 #ifdef __DOSMEM_DEBUG__
499 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
503 info_block
->blocks
--;
504 info_block
->free
+= dm
->size
;
506 dm
->size
|= DM_BLOCK_FREE
;
513 /***********************************************************************
516 * Resize DOS memory block in place. Returns block size or -1 on error.
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.
522 UINT
DOSMEM_ResizeBlock(void *ptr
, UINT size
, BOOL exact
)
525 dosmem_info
*info_block
= DOSMEM_InfoBlock();
531 if( (ptr
< (void*)(sizeof(dosmem_entry
) + (char*)DOSMEM_RootBlock())) ||
532 (ptr
>= (void*)DOSMEM_MemoryTop()) ||
533 (((((char*)ptr
) - DOSMEM_dosmem
) & 0xf) != 0) )
536 dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
537 if( dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
) )
540 next
= NEXT_BLOCK(dm
);
541 orgsize
= dm
->size
& DM_BLOCK_MASK
;
543 /* collapse free blocks */
544 while( next
->size
& DM_BLOCK_FREE
)
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
);
551 blocksize
= dm
->size
& DM_BLOCK_MASK
;
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).
558 if (blocksize
< size
)
559 size
= exact
? orgsize
: blocksize
;
561 block
= ((char*)dm
) + sizeof(dosmem_entry
);
562 if( blocksize
- size
> 0x20 )
565 * split dm so that the next one stays
566 * paragraph-aligned (and next gains free bit)
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
;
578 dm
->size
&= DM_BLOCK_MASK
;
582 * Adjust available memory if block size changes.
584 info_block
->free
+= orgsize
- dm
->size
;
589 /***********************************************************************
592 UINT
DOSMEM_Available(void)
594 UINT blocksize
, available
= 0;
597 dm
= DOSMEM_RootBlock();
599 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
601 #ifdef __DOSMEM_DEBUG__
602 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
604 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT
)prev
);
609 if( dm
->size
& DM_BLOCK_FREE
)
611 dosmem_entry
*next
= NEXT_BLOCK(dm
);
613 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
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
);
620 blocksize
= dm
->size
& DM_BLOCK_MASK
;
621 if ( blocksize
> available
) available
= blocksize
;
624 else dm
= NEXT_BLOCK(dm
);
630 /***********************************************************************
631 * DOSMEM_MapLinearToDos
633 * Linear address to the DOS address space.
635 UINT
DOSMEM_MapLinearToDos(LPVOID ptr
)
637 if (((char*)ptr
>= DOSMEM_dosmem
) &&
638 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
639 return (UINT
)ptr
- (UINT
)DOSMEM_dosmem
;
644 /***********************************************************************
645 * DOSMEM_MapDosToLinear
647 * DOS linear address to the linear address space.
649 LPVOID
DOSMEM_MapDosToLinear(UINT ptr
)
651 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT
)DOSMEM_dosmem
);
656 /***********************************************************************
657 * DOSMEM_MapRealToLinear
659 * Real mode DOS address into a linear pointer
661 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
665 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
666 TRACE_(selector
)("(0x%08lx) returns %p.\n", x
, lin
);
670 /***********************************************************************
671 * DOSMEM_AllocSelector
673 * Allocates a protected mode selector for a realmode segment.
675 WORD
DOSMEM_AllocSelector(WORD realsel
)
677 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
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
);