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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
39 #include "wine/winbase16.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
46 /* DOS memory highest address (including HMA) */
47 #define DOSMEM_SIZE 0x110000
48 #define DOSMEM_64KB 0x10000
50 /* see dlls/kernel/dosmem.c for the details */
51 static char *DOSMEM_dosmem;
52 static char *DOSMEM_sysmem;
55 * Memory Control Block (MCB) definition
56 * FIXME: implement Allocation Strategy
59 #define MCB_DUMP(mc) \
60 TRACE ("MCB_DUMP base=%p type=%02xh psp=%04xh size=%04xh\n", mc, mc->type, mc->psp , mc->size )
62 #define MCB_NEXT(mc) \
63 (MCB*) ((mc->type==MCB_TYPE_LAST) ? NULL : (char*)(mc) + ((mc->size + 1) << 4) )
65 /* FIXME: should we check more? */
66 #define MCB_VALID(mc) \
67 ((mc->type==MCB_TYPE_NORMAL) || (mc->type==MCB_TYPE_LAST))
70 #define MCB_TYPE_NORMAL 0x4d
71 #define MCB_TYPE_LAST 0x5a
73 #define MCB_PSP_DOS 0x0060
74 #define MCB_PSP_FREE 0
79 WORD psp; /* segment of owner psp */
80 WORD size; /* in paragraphs */
87 #define __DOSMEM_DEBUG__
90 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
91 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
93 /* FIXME: this should be moved to the LOL */
94 static MCB* DOSMEM_root_block;
96 /***********************************************************************
99 * Gets the DOS memory top.
101 static char *DOSMEM_MemoryTop(void)
103 return DOSMEM_dosmem+0x9FFFC; /* 640K */
106 /***********************************************************************
107 * DOSMEM_FillIsrTable
109 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
112 * Linux normally only traps INTs performed from or destined to BIOSSEG
113 * for us to handle, if the int_revectored table is empty. Filling the
114 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
115 * to hook interrupts, as well as use their familiar retf tricks to call
116 * them, AND let Wine handle any unhooked interrupts transparently.
118 static void DOSMEM_FillIsrTable(void)
120 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
123 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
126 static void DOSMEM_MakeIsrStubs(void)
128 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
131 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
134 BIOSDATA* DOSVM_BiosData(void)
136 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
139 /**********************************************************************
140 * DOSMEM_GetTicksSinceMidnight
142 * Return number of clock ticks since midnight.
144 static DWORD DOSMEM_GetTicksSinceMidnight(void)
148 /* This should give us the (approximately) correct
149 * 18.206 clock ticks per second since midnight.
152 GetLocalTime( &time );
154 return (((time.wHour * 3600 + time.wMinute * 60 +
155 time.wSecond) * 18206) / 1000) +
156 (time.wMilliseconds * 1000 / 54927);
159 /***********************************************************************
160 * DOSMEM_FillBiosSegments
162 * Fill the BIOS data segment with dummy values.
164 static void DOSMEM_FillBiosSegments(void)
166 BYTE *pBiosSys = (BYTE*)DOSMEM_dosmem + 0xf0000;
167 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
168 BIOSDATA *pBiosData = DOSVM_BiosData();
169 static const char bios_date[] = "13/01/99";
171 /* Clear all unused values */
172 memset( pBiosData, 0, sizeof(*pBiosData) );
174 /* FIXME: should check the number of configured drives and ports */
175 pBiosData->Com1Addr = 0x3f8;
176 pBiosData->Com2Addr = 0x2f8;
177 pBiosData->Lpt1Addr = 0x378;
178 pBiosData->Lpt2Addr = 0x278;
179 pBiosData->InstalledHardware = 0x5463;
180 pBiosData->MemSize = 640;
181 pBiosData->NextKbdCharPtr = 0x1e;
182 pBiosData->FirstKbdCharPtr = 0x1e;
183 pBiosData->VideoMode = 3;
184 pBiosData->VideoColumns = 80;
185 pBiosData->VideoPageSize = 80 * 25 * 2;
186 pBiosData->VideoPageStartAddr = 0xb800;
187 pBiosData->VideoCtrlAddr = 0x3d4;
188 pBiosData->Ticks = DOSMEM_GetTicksSinceMidnight();
189 pBiosData->NbHardDisks = 2;
190 pBiosData->KbdBufferStart = 0x1e;
191 pBiosData->KbdBufferEnd = 0x3e;
192 pBiosData->RowsOnScreenMinus1 = 24;
193 pBiosData->BytesPerChar = 0x10;
194 pBiosData->ModeOptions = 0x64;
195 pBiosData->FeatureBitsSwitches = 0xf9;
196 pBiosData->VGASettings = 0x51;
197 pBiosData->DisplayCombination = 0x08;
198 pBiosData->DiskDataRate = 0;
200 /* fill ROM configuration table (values from Award) */
201 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
202 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
203 *(pBiosROMTable+0x2) = 0xfc; /* model */
204 *(pBiosROMTable+0x3) = 0x01; /* submodel */
205 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
206 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
207 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
208 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
209 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
210 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
212 /* BIOS date string */
213 memcpy(pBiosSys+0xfff5, bios_date, sizeof bios_date);
216 *(pBiosSys+0xfffe) = 0xfc;
218 /* Reboot vector (f000:fff0 or ffff:0000) */
219 *(DWORD*)(pBiosSys + 0xfff0) = VM_STUB(0x19);
222 /***********************************************************************
225 * Increment the BIOS tick counter. Called by timer signal handler.
227 static void CALLBACK BiosTick( LPVOID arg, DWORD low, DWORD high )
229 BIOSDATA *pBiosData = arg;
233 /***********************************************************************
236 static DWORD CALLBACK timer_thread( void *arg )
241 if (!(timer = CreateWaitableTimerA( NULL, FALSE, NULL ))) return 0;
243 when.u.LowPart = when.u.HighPart = 0;
244 SetWaitableTimer( timer, &when, 55 /* actually 54.925 */, BiosTick, arg, FALSE );
245 for (;;) SleepEx( INFINITE, TRUE );
248 /***********************************************************************
251 * Helper function for internal use only.
252 * Attach all following free blocks to this one, even if this one is not free.
254 static void DOSMEM_Collapse( MCB* mcb )
256 MCB* next = MCB_NEXT( mcb );
258 while (next && next->psp == MCB_PSP_FREE)
260 mcb->size = mcb->size + next->size + 1;
261 mcb->type = next->type; /* make sure keeping MCB_TYPE_LAST */
262 next = MCB_NEXT( next );
266 /***********************************************************************
269 * Carve a chunk of the DOS memory block (without selector).
271 LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg)
273 MCB *curr = DOSMEM_root_block;
275 WORD psp = DOSVM_psp;
282 TRACE( "(%04xh)\n", size );
284 /* round up to paragraph */
285 size = (size + 15) >> 4;
287 #ifdef __DOSMEM_DEBUG__
288 DOSMEM_Available(); /* checks the whole MCB list */
291 /* loop over all MCB and search the next large enough MCB */
294 if (!MCB_VALID (curr))
296 ERR( "MCB List Corrupt\n" );
300 if (curr->psp == MCB_PSP_FREE)
302 DOSMEM_Collapse( curr );
303 /* is it large enough (one paragraph for the MCB)? */
304 if (curr->size >= size)
306 if (curr->size > size)
309 next = (MCB *) ((char*) curr + ((size+1) << 4));
310 next->psp = MCB_PSP_FREE;
311 next->size = curr->size - (size+1);
312 next->type = curr->type;
313 curr->type = MCB_TYPE_NORMAL;
316 /* curr is the found block */
318 if( pseg ) *pseg = (((char*)curr) + 16 - DOSMEM_dosmem) >> 4;
319 return (LPVOID) ((char*)curr + 16);
322 curr = MCB_NEXT(curr);
327 /***********************************************************************
330 BOOL DOSMEM_FreeBlock(void* ptr)
332 MCB* mcb = (MCB*) ((char*)ptr - 16);
334 TRACE( "(%p)\n", ptr );
336 #ifdef __DOSMEM_DEBUG__
340 if (!MCB_VALID (mcb))
342 ERR( "MCB invalid\n" );
347 mcb->psp = MCB_PSP_FREE;
348 DOSMEM_Collapse( mcb );
352 /***********************************************************************
355 * Resize DOS memory block in place. Returns block size or -1 on error.
357 * If exact is TRUE, returned value is either old or requested block
358 * size. If exact is FALSE, block is expanded even if there is not
359 * enough space for full requested block size.
361 * TODO: return also biggest block size
363 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
365 MCB* mcb = (MCB*) ((char*)ptr - 16);
368 TRACE( "(%p,%04xh,%s)\n", ptr, size, exact ? "TRUE" : "FALSE" );
370 /* round up to paragraph */
371 size = (size + 15) >> 4;
373 #ifdef __DOSMEM_DEBUG__
377 if (!MCB_VALID (mcb))
379 ERR( "MCB invalid\n" );
385 if (mcb->size == size)
388 /* collapse free blocks */
389 DOSMEM_Collapse( mcb );
392 if (mcb->size > size)
394 next = (MCB *) ((char*)mcb + ((size+1) << 4));
395 next->type = mcb->type;
396 next->psp = MCB_PSP_FREE;
397 next->size = mcb->size - (size+1);
398 mcb->type = MCB_TYPE_NORMAL;
405 return mcb->size << 4;
411 /***********************************************************************
414 UINT DOSMEM_Available(void)
418 MCB *curr = DOSMEM_root_block;
419 /* loop over all MCB and search the largest free MCB */
422 #ifdef __DOSMEM_DEBUG__
425 if (!MCB_VALID (curr))
427 ERR( "MCB List Corrupt\n" );
431 if (curr->psp == MCB_PSP_FREE &&
432 curr->size > available )
433 available = curr->size;
435 total += curr->size + 1;
436 curr = MCB_NEXT( curr );
438 TRACE( " %04xh of %04xh paragraphs available\n", available, total );
439 return available << 4;
442 /***********************************************************************
445 * Initialises the DOS memory structures.
447 static void DOSMEM_InitMemory(char* addr)
449 DOSMEM_FillBiosSegments();
450 DOSMEM_FillIsrTable();
452 /* align root block to paragraph */
453 DOSMEM_root_block = (MCB*) (( (DWORD_PTR)(addr+0xf) >> 4) << 4);
454 DOSMEM_root_block->type = MCB_TYPE_LAST;
455 DOSMEM_root_block->psp = MCB_PSP_FREE;
456 DOSMEM_root_block->size = (DOSMEM_MemoryTop() - ((char*)DOSMEM_root_block)) >> 4;
458 TRACE("DOS conventional memory initialized, %d bytes free.\n",
462 /******************************************************************
463 * DOSMEM_InitDosMemory
465 * When WineDOS is loaded, initializes the current DOS memory layout.
467 BOOL DOSMEM_InitDosMemory(void)
474 if (!(hModule = GetModuleHandle16("KERNEL"))) return FALSE;
475 /* KERNEL.194: __F000H */
476 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(ULONG_PTR)194));
477 wine_ldt_get_entry(sel, &entry);
478 DOSMEM_dosmem = (char*)wine_ldt_get_base(&entry) - 0xF0000;
479 /* KERNEL.183: __0000H */
480 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
481 wine_ldt_get_entry(sel, &entry);
482 DOSMEM_sysmem = wine_ldt_get_base(&entry);
486 * - lowest 64k for NULL pointer catching (Win16)
487 * - lowest 1k for interrupt handlers and
488 * another 0.5k for BIOS, DOS and intra-application
491 if (DOSMEM_dosmem != DOSMEM_sysmem)
492 reserve = 0x10000; /* 64k */
494 reserve = 0x600; /* 1.5k */
497 * Round to paragraph boundary in order to make
498 * sure the alignment is correct.
500 reserve = ((reserve + 15) >> 4) << 4;
503 * Set DOS memory base and initialize conventional memory.
505 DOSMEM_InitMemory(DOSMEM_dosmem + reserve);
507 CloseHandle( CreateThread( NULL, 0, timer_thread, DOSVM_BiosData(), 0, NULL ));
511 /******************************************************************
512 * DOSMEM_MapDosLayout
514 * Initialize the first MB of memory to look like a real DOS setup
516 BOOL DOSMEM_MapDosLayout(void)
518 static int already_mapped;
526 if (DOSMEM_dosmem || !VirtualProtect( NULL, DOSMEM_SIZE, PAGE_EXECUTE_READWRITE, NULL ))
528 ERR( "Need full access to the first megabyte for DOS mode\n" );
531 /* copy the BIOS and ISR area down */
532 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
533 DOSMEM_sysmem = DOSMEM_dosmem;
534 hModule = GetModuleHandle16("KERNEL");
535 /* selector to 0000H */
536 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
537 wine_ldt_get_entry(sel, &entry);
538 wine_ldt_set_base(&entry, NULL);
539 wine_ldt_set_entry(sel, &entry);
540 /* selector to BiosData */
541 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)193));
542 wine_ldt_get_entry(sel, &entry);
543 wine_ldt_set_base(&entry, (const void*)0x400);
544 wine_ldt_set_entry(sel, &entry);
545 /* we may now need the actual interrupt stubs, and since we've just moved the
546 * interrupt vector table away, we can fill the area with stubs instead... */
547 DOSMEM_MakeIsrStubs();