Add Mach-O (Darwin file format) support.
[wine/dcerpc.git] / msdos / dosmem.c
blob82fe3e798a2482083f273f4d73dd0ec3cbf5e3da
1 /*
2 * DOS memory emulation
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
33 #include <time.h>
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
36 #endif
38 #include "winbase.h"
39 #include "wine/winbase16.h"
41 #include "global.h"
42 #include "selectors.h"
43 #include "miscemu.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
47 WINE_DECLARE_DEBUG_CHANNEL(selector);
49 WORD DOSMEM_0000H; /* segment at 0:0 */
50 WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
51 WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
53 /* use 2 low bits of 'size' for the housekeeping */
55 #define DM_BLOCK_DEBUG 0xABE00000
56 #define DM_BLOCK_TERMINAL 0x00000001
57 #define DM_BLOCK_FREE 0x00000002
58 #define DM_BLOCK_MASK 0x001FFFFC
61 #define __DOSMEM_DEBUG__
64 typedef struct {
65 unsigned size;
66 } dosmem_entry;
68 typedef struct {
69 unsigned blocks;
70 unsigned free;
71 } dosmem_info;
73 #define NEXT_BLOCK(block) \
74 (dosmem_entry*)(((char*)(block)) + \
75 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
77 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
78 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
80 /* DOS memory base */
81 static char *DOSMEM_dosmem;
82 /* DOS system base (for interrupt vector table and BIOS data area)
83 * ...should in theory (i.e. Windows) be equal to DOSMEM_dosmem (NULL),
84 * but is normally set to 0xf0000 in Wine to allow trapping of NULL pointers,
85 * and only relocated to NULL when absolutely necessary */
86 static char *DOSMEM_sysmem;
88 /* Start of DOS conventional memory */
89 static char *DOSMEM_membase;
91 static void DOSMEM_InitMemory(void);
93 /***********************************************************************
94 * DOSMEM_MemoryTop
96 * Gets the DOS memory top.
98 static char *DOSMEM_MemoryTop(void)
100 return DOSMEM_dosmem+0x9FFFC; /* 640K */
103 /***********************************************************************
104 * DOSMEM_InfoBlock
106 * Gets the DOS memory info block.
108 static dosmem_info *DOSMEM_InfoBlock(void)
110 if (!DOSMEM_membase)
112 DWORD reserve;
115 * Reserve either:
116 * - lowest 64k for NULL pointer catching (Win16)
117 * - lowest 1k for interrupt handlers and
118 * another 0.5k for BIOS, DOS and intra-application
119 * areas (DOS)
121 if (DOSMEM_dosmem != DOSMEM_sysmem)
122 reserve = 0x10000; /* 64k */
123 else
124 reserve = 0x600; /* 1.5k */
127 * Round to paragraph boundary in order to make
128 * sure the alignment is correct.
130 reserve = ((reserve + 15) >> 4) << 4;
133 * Set DOS memory base and initialize conventional memory.
135 DOSMEM_membase = DOSMEM_dosmem + reserve;
136 DOSMEM_InitMemory();
139 return (dosmem_info*)DOSMEM_membase;
142 /***********************************************************************
143 * DOSMEM_RootBlock
145 * Gets the DOS memory root block.
147 static dosmem_entry *DOSMEM_RootBlock(void)
149 /* first block has to be paragraph-aligned */
150 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
151 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
154 /***********************************************************************
155 * DOSMEM_FillIsrTable
157 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
159 * NOTES:
160 * Linux normally only traps INTs performed from or destined to BIOSSEG
161 * for us to handle, if the int_revectored table is empty. Filling the
162 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
163 * to hook interrupts, as well as use their familiar retf tricks to call
164 * them, AND let Wine handle any unhooked interrupts transparently.
166 static void DOSMEM_FillIsrTable(void)
168 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
169 int x;
171 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
174 static void DOSMEM_MakeIsrStubs(void)
176 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
177 int x;
179 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
182 static BIOSDATA * DOSMEM_BiosData(void)
184 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
187 /**********************************************************************
188 * DOSMEM_GetTicksSinceMidnight
190 * Return number of clock ticks since midnight.
192 static DWORD DOSMEM_GetTicksSinceMidnight(void)
194 struct tm *bdtime;
195 struct timeval tvs;
196 time_t seconds;
198 /* This should give us the (approximately) correct
199 * 18.206 clock ticks per second since midnight.
201 gettimeofday( &tvs, NULL );
202 seconds = tvs.tv_sec;
203 bdtime = localtime( &seconds );
204 return (((bdtime->tm_hour * 3600 + bdtime->tm_min * 60 +
205 bdtime->tm_sec) * 18206) / 1000) +
206 (tvs.tv_usec / 54927);
209 /***********************************************************************
210 * DOSMEM_FillBiosSegments
212 * Fill the BIOS data segment with dummy values.
214 static void DOSMEM_FillBiosSegments(void)
216 BYTE *pBiosSys = DOSMEM_dosmem + 0xf0000;
217 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
218 BIOSDATA *pBiosData = DOSMEM_BiosData();
220 /* Clear all unused values */
221 memset( pBiosData, 0, sizeof(*pBiosData) );
223 /* FIXME: should check the number of configured drives and ports */
224 pBiosData->Com1Addr = 0x3f8;
225 pBiosData->Com2Addr = 0x2f8;
226 pBiosData->Lpt1Addr = 0x378;
227 pBiosData->Lpt2Addr = 0x278;
228 pBiosData->InstalledHardware = 0x5463;
229 pBiosData->MemSize = 640;
230 pBiosData->NextKbdCharPtr = 0x1e;
231 pBiosData->FirstKbdCharPtr = 0x1e;
232 pBiosData->VideoMode = 3;
233 pBiosData->VideoColumns = 80;
234 pBiosData->VideoPageSize = 80 * 25 * 2;
235 pBiosData->VideoPageStartAddr = 0xb800;
236 pBiosData->VideoCtrlAddr = 0x3d4;
237 pBiosData->Ticks = DOSMEM_GetTicksSinceMidnight();
238 pBiosData->NbHardDisks = 2;
239 pBiosData->KbdBufferStart = 0x1e;
240 pBiosData->KbdBufferEnd = 0x3e;
241 pBiosData->RowsOnScreenMinus1 = 24;
242 pBiosData->BytesPerChar = 0x10;
243 pBiosData->ModeOptions = 0x64;
244 pBiosData->FeatureBitsSwitches = 0xf9;
245 pBiosData->VGASettings = 0x51;
246 pBiosData->DisplayCombination = 0x08;
247 pBiosData->DiskDataRate = 0;
249 /* fill ROM configuration table (values from Award) */
250 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
251 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
252 *(pBiosROMTable+0x2) = 0xfc; /* model */
253 *(pBiosROMTable+0x3) = 0x01; /* submodel */
254 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
255 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
256 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
257 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
258 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
259 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
261 /* BIOS date string */
262 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
264 /* BIOS ID */
265 *(pBiosSys+0xfffe) = 0xfc;
268 /***********************************************************************
269 * DOSMEM_InitMemory
271 * Initialises the DOS memory structures.
273 static void DOSMEM_InitMemory(void)
275 dosmem_info* info_block = DOSMEM_InfoBlock();
276 dosmem_entry* root_block = DOSMEM_RootBlock();
277 dosmem_entry* dm;
279 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
281 info_block->blocks = 0;
282 info_block->free = root_block->size;
284 dm = NEXT_BLOCK(root_block);
285 dm->size = DM_BLOCK_TERMINAL;
286 root_block->size |= DM_BLOCK_FREE
287 #ifdef __DOSMEM_DEBUG__
288 | DM_BLOCK_DEBUG
289 #endif
292 TRACE( "DOS conventional memory initialized, %d bytes free.\n",
293 DOSMEM_Available() );
297 /**********************************************************************
298 * setup_dos_mem
300 * Setup the first megabyte for DOS memory access
302 static void setup_dos_mem( int dos_init )
304 int sys_offset = 0;
305 int page_size = getpagesize();
306 void *addr = wine_anon_mmap( (void *)page_size, 0x110000-page_size,
307 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
308 if (addr == (void *)page_size) /* we got what we wanted */
310 /* now map from address 0 */
311 addr = wine_anon_mmap( NULL, 0x110000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
312 if (addr)
314 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
315 ExitProcess(1);
318 /* inform the memory manager that there is a mapping here */
319 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
321 /* protect the first 64K to catch NULL pointers */
322 if (!dos_init)
324 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
325 /* move the BIOS and ISR area from 0x00000 to 0xf0000 */
326 sys_offset += 0xf0000;
329 else
331 ERR("Cannot use first megabyte for DOS address space, please report\n" );
332 if (dos_init) ExitProcess(1);
333 /* allocate the DOS area somewhere else */
334 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
335 if (!addr)
337 ERR( "Cannot allocate DOS memory\n" );
338 ExitProcess(1);
341 DOSMEM_dosmem = addr;
342 DOSMEM_sysmem = (char*)addr + sys_offset;
346 /***********************************************************************
347 * DOSMEM_Init
349 * Create the dos memory segments, and store them into the KERNEL
350 * exported values.
352 BOOL DOSMEM_Init(BOOL dos_init)
354 static int already_done, already_mapped;
356 if (!already_done)
358 setup_dos_mem( dos_init );
360 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_sysmem,
361 0x10000, 0, WINE_LDT_FLAGS_DATA );
362 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_sysmem + 0x400,
363 0x100, 0, WINE_LDT_FLAGS_DATA );
364 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
365 0x10000, 0, WINE_LDT_FLAGS_DATA );
366 DOSMEM_FillBiosSegments();
367 DOSMEM_FillIsrTable();
368 already_done = 1;
370 else if (dos_init && !already_mapped)
372 if (DOSMEM_dosmem)
374 ERR( "Needs access to the first megabyte for DOS mode\n" );
375 ExitProcess(1);
377 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
378 " NULL pointer accesses will no longer be caught.\n" );
379 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
380 /* copy the BIOS and ISR area down */
381 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
382 DOSMEM_sysmem = DOSMEM_dosmem;
383 SetSelectorBase( DOSMEM_0000H, 0 );
384 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
385 /* we may now need the actual interrupt stubs, and since we've just moved the
386 * interrupt vector table away, we can fill the area with stubs instead... */
387 DOSMEM_MakeIsrStubs();
388 already_mapped = 1;
390 return TRUE;
394 /***********************************************************************
395 * DOSMEM_Tick
397 * Increment the BIOS tick counter. Called by timer signal handler.
399 void DOSMEM_Tick( WORD timer )
401 BIOSDATA *pBiosData = DOSMEM_BiosData();
402 if (pBiosData) pBiosData->Ticks++;
405 /***********************************************************************
406 * DOSMEM_GetBlock
408 * Carve a chunk of the DOS memory block (without selector).
410 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
412 UINT blocksize;
413 char *block = NULL;
414 dosmem_info *info_block = DOSMEM_InfoBlock();
415 dosmem_entry *dm;
416 #ifdef __DOSMEM_DEBUG_
417 dosmem_entry *prev = NULL;
418 #endif
420 if( size > info_block->free ) return NULL;
421 dm = DOSMEM_RootBlock();
423 while (dm && dm->size != DM_BLOCK_TERMINAL)
425 #ifdef __DOSMEM_DEBUG__
426 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
428 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
429 return NULL;
431 prev = dm;
432 #endif
433 if( dm->size & DM_BLOCK_FREE )
435 dosmem_entry *next = NEXT_BLOCK(dm);
437 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
439 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
440 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
441 next = NEXT_BLOCK(dm);
444 blocksize = dm->size & DM_BLOCK_MASK;
445 if( blocksize >= size )
447 block = ((char*)dm) + sizeof(dosmem_entry);
448 if( blocksize - size > 0x20 )
450 /* split dm so that the next one stays
451 * paragraph-aligned (and dm loses free bit) */
453 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
454 sizeof(dosmem_entry));
455 next = (dosmem_entry*)(((char*)dm) +
456 sizeof(dosmem_entry) + dm->size);
457 next->size = (blocksize - (dm->size +
458 sizeof(dosmem_entry))) | DM_BLOCK_FREE
459 #ifdef __DOSMEM_DEBUG__
460 | DM_BLOCK_DEBUG
461 #endif
463 } else dm->size &= DM_BLOCK_MASK;
465 info_block->blocks++;
466 info_block->free -= dm->size;
467 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
468 #ifdef __DOSMEM_DEBUG__
469 dm->size |= DM_BLOCK_DEBUG;
470 #endif
471 break;
473 dm = next;
475 else dm = NEXT_BLOCK(dm);
477 return (LPVOID)block;
480 /***********************************************************************
481 * DOSMEM_FreeBlock
483 BOOL DOSMEM_FreeBlock(void* ptr)
485 dosmem_info *info_block = DOSMEM_InfoBlock();
487 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
488 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
489 - DOSMEM_dosmem) & 0xf) )
491 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
493 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
494 #ifdef __DOSMEM_DEBUG__
495 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
496 #endif
499 info_block->blocks--;
500 info_block->free += dm->size;
502 dm->size |= DM_BLOCK_FREE;
503 return TRUE;
506 return FALSE;
509 /***********************************************************************
510 * DOSMEM_ResizeBlock
512 * Resize DOS memory block in place. Returns block size or -1 on error.
514 * If exact is TRUE, returned value is either old or requested block
515 * size. If exact is FALSE, block is expanded even if there is not
516 * enough space for full requested block size.
518 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
520 char *block = NULL;
521 dosmem_info *info_block = DOSMEM_InfoBlock();
522 dosmem_entry *dm;
523 dosmem_entry *next;
524 UINT blocksize;
525 UINT orgsize;
527 if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) ||
528 (ptr >= (void*)DOSMEM_MemoryTop()) ||
529 (((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) )
530 return (UINT)-1;
532 dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
533 if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) )
534 return (UINT)-1;
536 next = NEXT_BLOCK(dm);
537 orgsize = dm->size & DM_BLOCK_MASK;
539 /* collapse free blocks */
540 while( next->size & DM_BLOCK_FREE )
542 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
543 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
544 next = NEXT_BLOCK(dm);
547 blocksize = dm->size & DM_BLOCK_MASK;
550 * If collapse didn't help we either expand block to maximum
551 * available size (exact == FALSE) or give collapsed blocks
552 * back to free storage (exact == TRUE).
554 if (blocksize < size)
555 size = exact ? orgsize : blocksize;
557 block = ((char*)dm) + sizeof(dosmem_entry);
558 if( blocksize - size > 0x20 )
561 * split dm so that the next one stays
562 * paragraph-aligned (and next gains free bit)
565 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
566 sizeof(dosmem_entry));
567 next = (dosmem_entry*)(((char*)dm) +
568 sizeof(dosmem_entry) + dm->size);
569 next->size = (blocksize - (dm->size +
570 sizeof(dosmem_entry))) | DM_BLOCK_FREE;
572 else
574 dm->size &= DM_BLOCK_MASK;
578 * Adjust available memory if block size changes.
580 info_block->free += orgsize - dm->size;
582 return size;
585 /***********************************************************************
586 * DOSMEM_Available
588 UINT DOSMEM_Available(void)
590 UINT blocksize, available = 0;
591 dosmem_entry *dm;
593 dm = DOSMEM_RootBlock();
595 while (dm && dm->size != DM_BLOCK_TERMINAL)
597 #ifdef __DOSMEM_DEBUG__
598 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
600 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
601 return NULL;
603 prev = dm;
604 #endif
605 if( dm->size & DM_BLOCK_FREE )
607 dosmem_entry *next = NEXT_BLOCK(dm);
609 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
611 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
612 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
613 next = NEXT_BLOCK(dm);
616 blocksize = dm->size & DM_BLOCK_MASK;
617 if ( blocksize > available ) available = blocksize;
618 dm = next;
620 else dm = NEXT_BLOCK(dm);
622 return available;
626 /***********************************************************************
627 * DOSMEM_MapLinearToDos
629 * Linear address to the DOS address space.
631 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
633 if (((char*)ptr >= DOSMEM_dosmem) &&
634 ((char*)ptr < DOSMEM_dosmem + 0x100000))
635 return (UINT)ptr - (UINT)DOSMEM_dosmem;
636 return (UINT)ptr;
640 /***********************************************************************
641 * DOSMEM_MapDosToLinear
643 * DOS linear address to the linear address space.
645 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
647 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
648 return (LPVOID)ptr;
652 /***********************************************************************
653 * DOSMEM_MapRealToLinear
655 * Real mode DOS address into a linear pointer
657 LPVOID DOSMEM_MapRealToLinear(DWORD x)
659 LPVOID lin;
661 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
662 TRACE_(selector)("(0x%08lx) returns %p.\n", x, lin );
663 return lin;
666 /***********************************************************************
667 * DOSMEM_AllocSelector
669 * Allocates a protected mode selector for a realmode segment.
671 WORD DOSMEM_AllocSelector(WORD realsel)
673 HMODULE16 hModule = GetModuleHandle16("KERNEL");
674 WORD sel;
676 sel=GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_dosmem+realsel*16, 0x10000,
677 hModule, WINE_LDT_FLAGS_DATA );
678 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
679 return sel;