- delayed initialisation of BIOS and SYSTEM information until someone

actually needs it
- now using some manifest constants to make code clearer
This commit is contained in:
Eric Pouech 2005-04-27 11:08:47 +00:00 committed by Alexandre Julliard
parent 8a7960c4f9
commit 1575c940ef
6 changed files with 118 additions and 50 deletions

View File

@ -33,6 +33,9 @@
#include "windef.h" #include "windef.h"
#include "winbase.h" #include "winbase.h"
#include "winreg.h"
#include "excpt.h"
#include "winternl.h"
#include "wine/winbase16.h" #include "wine/winbase16.h"
#include "kernel_private.h" #include "kernel_private.h"
@ -47,6 +50,10 @@ WORD DOSMEM_0000H; /* segment at 0:0 */
WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */ WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */ WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
/* DOS memory highest address (including HMA) */
#define DOSMEM_SIZE 0x110000
#define DOSMEM_64KB 0x10000
/* use 2 low bits of 'size' for the housekeeping */ /* use 2 low bits of 'size' for the housekeeping */
#define DM_BLOCK_DEBUG 0xABE00000 #define DM_BLOCK_DEBUG 0xABE00000
@ -74,16 +81,32 @@ typedef struct {
#define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */ #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
#define VM_STUB_SEGMENT 0xf000 /* BIOS segment */ #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
/* DOS memory base */ /* when looking at DOS and real mode memory, we activate in three different
* modes, depending the situation.
* 1/ By default (protected mode), the first MB of memory (actually 0x110000,
* when you also look at the HMA part) is always reserved, whatever you do.
* We allocated some PM selectors to this memory, even if this area is not
* committed at startup
* 2/ if a program tries to use the memory through the selectors, we actually
* commit this memory, made of: BIOS segment, but also some system
* information, usually low in memory that we map for the circumstance also
* in the BIOS segment, so that we keep the low memory protected (for NULL
* pointer deref catching for example). In this case, we'res still in PM
* mode, accessing part of the "physicale" real mode memory.
* 3/ if the process enters the real mode, then we commit the full first MB of
* memory (and also initialize the DOS structures in it).
*/
/* DOS memory base (linear in process address space) */
static char *DOSMEM_dosmem; static char *DOSMEM_dosmem;
/* DOS system base (for interrupt vector table and BIOS data area) /* DOS system base (for interrupt vector table and BIOS data area)
* ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL), * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
* but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers, * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
* and only relocated to NULL when absolutely necessary */ * and only relocated to NULL when absolutely necessary */
static char *DOSMEM_sysmem; static char *DOSMEM_sysmem;
/* number of bytes protected from _dosmem. 0 when DOS memory is initialized,
/* Start of DOS conventional memory */ * 64k otherwise to trap NULL pointers deref */
static char *DOSMEM_membase; static DWORD DOSMEM_protect;
static void DOSMEM_InitMemory(void); static void DOSMEM_InitMemory(void);
@ -104,6 +127,9 @@ static char *DOSMEM_MemoryTop(void)
*/ */
static dosmem_info *DOSMEM_InfoBlock(void) static dosmem_info *DOSMEM_InfoBlock(void)
{ {
/* Start of DOS conventional memory */
static char *DOSMEM_membase;
if (!DOSMEM_membase) if (!DOSMEM_membase)
{ {
DWORD reserve; DWORD reserve;
@ -116,7 +142,7 @@ static dosmem_info *DOSMEM_InfoBlock(void)
* areas (DOS) * areas (DOS)
*/ */
if (DOSMEM_dosmem != DOSMEM_sysmem) if (DOSMEM_dosmem != DOSMEM_sysmem)
reserve = 0x10000; /* 64k */ reserve = DOSMEM_64KB;
else else
reserve = 0x600; /* 1.5k */ reserve = 0x600; /* 1.5k */
@ -291,48 +317,81 @@ static void DOSMEM_InitMemory(void)
DOSMEM_Available() ); DOSMEM_Available() );
} }
static void dosmem_bios_init(void)
{
static int bios_created;
if (!bios_created)
{
DOSMEM_FillBiosSegments();
DOSMEM_FillIsrTable();
bios_created = TRUE;
}
}
/******************************************************************
* dosmem_handler
*
* Handler to catch access to our 1MB address space reserved for real memory
*/
static LONG WINAPI dosmem_handler(EXCEPTION_POINTERS* except)
{
if (except->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
{
DWORD addr = except->ExceptionRecord->ExceptionInformation[1];
if (addr >= (ULONG_PTR)DOSMEM_sysmem &&
addr < (ULONG_PTR)DOSMEM_sysmem + DOSMEM_64KB)
{
VirtualProtect( DOSMEM_sysmem, DOSMEM_64KB, PAGE_EXECUTE_READWRITE, NULL );
dosmem_bios_init();
return EXCEPTION_CONTINUE_EXECUTION;
}
if (addr >= (ULONG_PTR)DOSMEM_dosmem + DOSMEM_protect &&
addr < (ULONG_PTR)DOSMEM_dosmem + DOSMEM_SIZE)
{
VirtualProtect( DOSMEM_dosmem + DOSMEM_protect, DOSMEM_SIZE - DOSMEM_protect,
PAGE_EXECUTE_READWRITE, NULL );
dosmem_bios_init();
return EXCEPTION_CONTINUE_EXECUTION;
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
/********************************************************************** /**********************************************************************
* setup_dos_mem * setup_dos_mem
* *
* Setup the first megabyte for DOS memory access * Setup the first megabyte for DOS memory access
*/ */
static void setup_dos_mem( int dos_init ) static void setup_dos_mem(void)
{ {
int sys_offset = 0; int sys_offset = 0;
int page_size = getpagesize(); int page_size = getpagesize();
void *addr = NULL; void *addr = NULL;
if (wine_mmap_is_in_reserved_area( NULL, 0x110000 ) != 1) if (wine_mmap_is_in_reserved_area( NULL, DOSMEM_SIZE ) != 1)
{ {
addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size, addr = wine_anon_mmap( (void *)page_size, DOSMEM_SIZE-page_size,
PROT_READ | PROT_WRITE | PROT_EXEC, 0 ); PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
if (addr == (void *)page_size) addr = NULL; /* we got what we wanted */ if (addr == (void *)page_size) addr = NULL; /* we got what we wanted */
else munmap( addr, 0x110000 - page_size ); else munmap( addr, DOSMEM_SIZE - page_size );
} }
if (!addr) if (!addr)
{ {
/* now map from address 0 */ /* now reserve from address 0 */
wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED ); wine_anon_mmap( NULL, DOSMEM_SIZE, 0, MAP_FIXED );
/* inform the memory manager that there is a mapping here */ /* inform the memory manager that there is a mapping here, but don't commit yet */
VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE ); VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE | MEM_SYSTEM, PAGE_NOACCESS );
sys_offset = 0xf0000;
/* protect the first 64K to catch NULL pointers */ DOSMEM_protect = DOSMEM_64KB;
if (!dos_init)
{
VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
/* move the BIOS and ISR area from 0x00000 to 0xf0000 */
sys_offset += 0xf0000;
}
} }
else else
{ {
ERR("Cannot use first megabyte for DOS address space, please report\n" ); ERR("Cannot use first megabyte for DOS address space, please report\n" );
if (dos_init) ExitProcess(1);
/* allocate the DOS area somewhere else */ /* allocate the DOS area somewhere else */
addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE ); addr = VirtualAlloc( NULL, DOSMEM_SIZE, MEM_RESERVE, PAGE_NOACCESS );
if (!addr) if (!addr)
{ {
ERR( "Cannot allocate DOS memory\n" ); ERR( "Cannot allocate DOS memory\n" );
@ -341,6 +400,7 @@ static void setup_dos_mem( int dos_init )
} }
DOSMEM_dosmem = addr; DOSMEM_dosmem = addr;
DOSMEM_sysmem = (char*)addr + sys_offset; DOSMEM_sysmem = (char*)addr + sys_offset;
RtlAddVectoredExceptionHandler(FALSE, dosmem_handler);
} }
@ -350,34 +410,41 @@ static void setup_dos_mem( int dos_init )
* Create the dos memory segments, and store them into the KERNEL * Create the dos memory segments, and store them into the KERNEL
* exported values. * exported values.
*/ */
BOOL DOSMEM_Init(BOOL dos_init) BOOL DOSMEM_Init(void)
{ {
static int already_done, already_mapped; setup_dos_mem();
if (!already_done)
{
setup_dos_mem( dos_init );
DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem, DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
0x10000, 0, WINE_LDT_FLAGS_DATA ); DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400, DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
0x100, 0, WINE_LDT_FLAGS_DATA ); 0x100, 0, WINE_LDT_FLAGS_DATA );
DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem + 0xf0000, DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem + 0xf0000,
0x10000, 0, WINE_LDT_FLAGS_DATA ); DOSMEM_64KB, 0, WINE_LDT_FLAGS_DATA );
DOSMEM_FillBiosSegments();
DOSMEM_FillIsrTable(); return TRUE;
already_done = 1;
} }
else if (dos_init && !already_mapped)
/******************************************************************
* DOSMEM_InitDosMem
*
* Initialize the first MB of memory to look like a real DOS setup
*/
BOOL DOSMEM_InitDosMem(void)
{
static int already_mapped;
if (!already_mapped)
{ {
if (DOSMEM_dosmem) if (DOSMEM_dosmem)
{ {
ERR( "Needs access to the first megabyte for DOS mode\n" ); ERR( "Needs access to the first megabyte for DOS mode\n" );
ExitProcess(1); ExitProcess(1);
} }
MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n" MESSAGE( "Warning: unprotecting memory to allow real-mode calls.\n"
" NULL pointer accesses will no longer be caught.\n" ); " NULL pointer accesses will no longer be caught.\n" );
VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL ); VirtualProtect( NULL, DOSMEM_SIZE, PAGE_EXECUTE_READWRITE, NULL );
DOSMEM_protect = 0;
dosmem_bios_init();
/* copy the BIOS and ISR area down */ /* copy the BIOS and ISR area down */
memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 ); memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
DOSMEM_sysmem = DOSMEM_dosmem; DOSMEM_sysmem = DOSMEM_dosmem;
@ -632,7 +699,7 @@ UINT DOSMEM_Available(void)
UINT DOSMEM_MapLinearToDos(LPVOID ptr) UINT DOSMEM_MapLinearToDos(LPVOID ptr)
{ {
if (((char*)ptr >= DOSMEM_dosmem) && if (((char*)ptr >= DOSMEM_dosmem) &&
((char*)ptr < DOSMEM_dosmem + 0x100000)) ((char*)ptr < DOSMEM_dosmem + DOSMEM_SIZE))
return (UINT)ptr - (UINT)DOSMEM_dosmem; return (UINT)ptr - (UINT)DOSMEM_dosmem;
return (UINT)ptr; return (UINT)ptr;
} }
@ -645,7 +712,7 @@ UINT DOSMEM_MapLinearToDos(LPVOID ptr)
*/ */
LPVOID DOSMEM_MapDosToLinear(UINT ptr) LPVOID DOSMEM_MapDosToLinear(UINT ptr)
{ {
if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem); if (ptr < DOSMEM_SIZE) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
return (LPVOID)ptr; return (LPVOID)ptr;
} }
@ -659,7 +726,7 @@ LPVOID DOSMEM_MapRealToLinear(DWORD x)
{ {
LPVOID lin; LPVOID lin;
lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16); lin = DOSMEM_dosmem + HIWORD(x) * 16 + LOWORD(x);
TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin ); TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
return lin; return lin;
} }
@ -674,7 +741,7 @@ WORD DOSMEM_AllocSelector(WORD realsel)
HMODULE16 hModule = GetModuleHandle16("KERNEL"); HMODULE16 hModule = GetModuleHandle16("KERNEL");
WORD sel; WORD sel;
sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000, sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, DOSMEM_64KB,
hModule, WINE_LDT_FLAGS_DATA ); hModule, WINE_LDT_FLAGS_DATA );
TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel); TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
return sel; return sel;

View File

@ -1170,7 +1170,7 @@
@ cdecl DOSMEM_Available() @ cdecl DOSMEM_Available()
@ cdecl DOSMEM_FreeBlock(ptr) @ cdecl DOSMEM_FreeBlock(ptr)
@ cdecl DOSMEM_GetBlock(long ptr) @ cdecl DOSMEM_GetBlock(long ptr)
@ cdecl DOSMEM_Init(long) @ cdecl DOSMEM_InitDosMem(long)
@ cdecl DOSMEM_ResizeBlock(ptr long long) @ cdecl DOSMEM_ResizeBlock(ptr long long)
@ cdecl LOCAL_Alloc(long long long) @ cdecl LOCAL_Alloc(long long long)
@ cdecl LOCAL_Compact(long long long) @ cdecl LOCAL_Compact(long long long)

View File

@ -121,7 +121,7 @@ static BOOL process_attach(void)
if (!WOWTHUNK_Init()) return FALSE; if (!WOWTHUNK_Init()) return FALSE;
/* Initialize DOS memory */ /* Initialize DOS memory */
if (!DOSMEM_Init(0)) return FALSE; if (!DOSMEM_Init()) return FALSE;
/* Setup computer name */ /* Setup computer name */
COMPUTERNAME_Init(); COMPUTERNAME_Init();

View File

@ -968,7 +968,7 @@ void WINAPI DOSVM_Int31Handler( CONTEXT86 *context )
/* check if Win16 app wants to access lower 64K of DOS memory */ /* check if Win16 app wants to access lower 64K of DOS memory */
if (base < 0x10000 && DOSVM_IsWin16()) if (base < 0x10000 && DOSVM_IsWin16())
DOSMEM_Init(TRUE); DOSMEM_InitDosMem();
SetSelectorBase( sel, base ); SetSelectorBase( sel, base );
} }

View File

@ -193,7 +193,7 @@ static BOOL MZ_InitMemory(void)
{ {
/* initialize the memory */ /* initialize the memory */
TRACE("Initializing DOS memory structures\n"); TRACE("Initializing DOS memory structures\n");
DOSMEM_Init(TRUE); DOSMEM_InitDosMem();
DOSDEV_InstallDOSDevices(); DOSDEV_InstallDOSDevices();
return TRUE; return TRUE;

View File

@ -93,7 +93,8 @@ extern WORD DOSMEM_BiosDataSeg;
extern WORD DOSMEM_BiosSysSeg; extern WORD DOSMEM_BiosSysSeg;
/* msdos/dosmem.c */ /* msdos/dosmem.c */
extern BOOL DOSMEM_Init(BOOL); extern BOOL DOSMEM_Init(void);
extern BOOL DOSMEM_InitDosMem(void);
extern void DOSMEM_Tick(WORD timer); extern void DOSMEM_Tick(WORD timer);
extern WORD DOSMEM_AllocSelector(WORD); extern WORD DOSMEM_AllocSelector(WORD);
extern LPVOID DOSMEM_GetBlock(UINT size, WORD* p); extern LPVOID DOSMEM_GetBlock(UINT size, WORD* p);