Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / winedos / dosmem.c
blob5d7f25f33ede69a12377d239109e74dc836180e2
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 <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 # include <sys/mman.h>
32 #endif
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "excpt.h"
38 #include "winternl.h"
39 #include "wine/winbase16.h"
41 #include "dosexe.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;
54 /* 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 static inline dosmem_entry* next_block(dosmem_entry* block)
75 return (dosmem_entry*)((char*)block +
76 sizeof(dosmem_entry) + (block->size & DM_BLOCK_MASK));
79 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
80 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
82 /* FIXME: this should be moved to the LOL, and the whole allocation strategy
83 * should use real MCB
85 static dosmem_info* DOSMEM_info_block;
87 /***********************************************************************
88 * DOSMEM_MemoryTop
90 * Gets the DOS memory top.
92 static char *DOSMEM_MemoryTop(void)
94 return DOSMEM_dosmem+0x9FFFC; /* 640K */
97 /***********************************************************************
98 * DOSMEM_RootBlock
100 * Gets the DOS memory root block.
102 static dosmem_entry *DOSMEM_RootBlock(void)
104 /* first block has to be paragraph-aligned */
105 return (dosmem_entry*)(((char*)DOSMEM_info_block) +
106 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
109 /***********************************************************************
110 * DOSMEM_FillIsrTable
112 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
114 * NOTES:
115 * Linux normally only traps INTs performed from or destined to BIOSSEG
116 * for us to handle, if the int_revectored table is empty. Filling the
117 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
118 * to hook interrupts, as well as use their familiar retf tricks to call
119 * them, AND let Wine handle any unhooked interrupts transparently.
121 static void DOSMEM_FillIsrTable(void)
123 SEGPTR *isr = (SEGPTR*)DOSMEM_sysmem;
124 int x;
126 for (x=0; x<256; x++) isr[x]=MAKESEGPTR(VM_STUB_SEGMENT,x*4);
129 static void DOSMEM_MakeIsrStubs(void)
131 DWORD *stub = (DWORD*)(DOSMEM_dosmem + (VM_STUB_SEGMENT << 4));
132 int x;
134 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
137 BIOSDATA* DOSVM_BiosData(void)
139 return (BIOSDATA *)(DOSMEM_sysmem + 0x400);
142 /**********************************************************************
143 * DOSMEM_GetTicksSinceMidnight
145 * Return number of clock ticks since midnight.
147 static DWORD DOSMEM_GetTicksSinceMidnight(void)
149 SYSTEMTIME time;
151 /* This should give us the (approximately) correct
152 * 18.206 clock ticks per second since midnight.
155 GetLocalTime( &time );
157 return (((time.wHour * 3600 + time.wMinute * 60 +
158 time.wSecond) * 18206) / 1000) +
159 (time.wMilliseconds * 1000 / 54927);
162 /***********************************************************************
163 * DOSMEM_FillBiosSegments
165 * Fill the BIOS data segment with dummy values.
167 static void DOSMEM_FillBiosSegments(void)
169 char *pBiosSys = DOSMEM_dosmem + 0xf0000;
170 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
171 BIOSDATA *pBiosData = DOSVM_BiosData();
173 /* Clear all unused values */
174 memset( pBiosData, 0, sizeof(*pBiosData) );
176 /* FIXME: should check the number of configured drives and ports */
177 pBiosData->Com1Addr = 0x3f8;
178 pBiosData->Com2Addr = 0x2f8;
179 pBiosData->Lpt1Addr = 0x378;
180 pBiosData->Lpt2Addr = 0x278;
181 pBiosData->InstalledHardware = 0x5463;
182 pBiosData->MemSize = 640;
183 pBiosData->NextKbdCharPtr = 0x1e;
184 pBiosData->FirstKbdCharPtr = 0x1e;
185 pBiosData->VideoMode = 3;
186 pBiosData->VideoColumns = 80;
187 pBiosData->VideoPageSize = 80 * 25 * 2;
188 pBiosData->VideoPageStartAddr = 0xb800;
189 pBiosData->VideoCtrlAddr = 0x3d4;
190 pBiosData->Ticks = DOSMEM_GetTicksSinceMidnight();
191 pBiosData->NbHardDisks = 2;
192 pBiosData->KbdBufferStart = 0x1e;
193 pBiosData->KbdBufferEnd = 0x3e;
194 pBiosData->RowsOnScreenMinus1 = 24;
195 pBiosData->BytesPerChar = 0x10;
196 pBiosData->ModeOptions = 0x64;
197 pBiosData->FeatureBitsSwitches = 0xf9;
198 pBiosData->VGASettings = 0x51;
199 pBiosData->DisplayCombination = 0x08;
200 pBiosData->DiskDataRate = 0;
202 /* fill ROM configuration table (values from Award) */
203 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
204 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
205 *(pBiosROMTable+0x2) = 0xfc; /* model */
206 *(pBiosROMTable+0x3) = 0x01; /* submodel */
207 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
208 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
209 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
210 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
211 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
212 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
214 /* BIOS date string */
215 strcpy(pBiosSys+0xfff5, "13/01/99");
217 /* BIOS ID */
218 *(pBiosSys+0xfffe) = 0xfc;
220 /* Reboot vector (f000:fff0 or ffff:0000) */
221 *(DWORD*)(pBiosSys + 0xfff0) = VM_STUB(0x19);
224 /***********************************************************************
225 * BiosTick
227 * Increment the BIOS tick counter. Called by timer signal handler.
229 void BiosTick( WORD timer )
231 BIOSDATA *pBiosData = DOSVM_BiosData();
232 if (pBiosData) pBiosData->Ticks++;
235 /***********************************************************************
236 * DOSMEM_AllocBlock
238 * Carve a chunk of the DOS memory block (without selector).
240 LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg)
242 UINT blocksize;
243 char *block = NULL;
244 dosmem_info *info_block = DOSMEM_info_block;
245 dosmem_entry *dm;
246 #ifdef __DOSMEM_DEBUG_
247 dosmem_entry *prev = NULL;
248 #endif
250 if( size > info_block->free ) return NULL;
251 dm = DOSMEM_RootBlock();
253 while (dm && dm->size != DM_BLOCK_TERMINAL)
255 #ifdef __DOSMEM_DEBUG__
256 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
258 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
259 return NULL;
261 prev = dm;
262 #endif
263 if( dm->size & DM_BLOCK_FREE )
265 dosmem_entry *next = next_block(dm);
267 while ( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
269 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
270 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
271 next = next_block(dm);
274 blocksize = dm->size & DM_BLOCK_MASK;
275 if( blocksize >= size )
277 block = ((char*)dm) + sizeof(dosmem_entry);
278 if( blocksize - size > 0x20 )
280 /* split dm so that the next one stays
281 * paragraph-aligned (and dm loses free bit) */
283 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
284 sizeof(dosmem_entry));
285 next = (dosmem_entry*)(((char*)dm) +
286 sizeof(dosmem_entry) + dm->size);
287 next->size = (blocksize - (dm->size +
288 sizeof(dosmem_entry))) | DM_BLOCK_FREE
289 #ifdef __DOSMEM_DEBUG__
290 | DM_BLOCK_DEBUG
291 #endif
293 } else dm->size &= DM_BLOCK_MASK;
295 info_block->blocks++;
296 info_block->free -= dm->size;
297 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
298 #ifdef __DOSMEM_DEBUG__
299 dm->size |= DM_BLOCK_DEBUG;
300 #endif
301 break;
303 dm = next;
305 else dm = next_block(dm);
307 return (LPVOID)block;
310 /***********************************************************************
311 * DOSMEM_FreeBlock
313 BOOL DOSMEM_FreeBlock(void* ptr)
315 dosmem_info *info_block = DOSMEM_info_block;
317 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
318 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
319 - DOSMEM_dosmem) & 0xf) )
321 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
323 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
324 #ifdef __DOSMEM_DEBUG__
325 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
326 #endif
329 info_block->blocks--;
330 info_block->free += dm->size;
332 dm->size |= DM_BLOCK_FREE;
333 return TRUE;
336 return FALSE;
339 /***********************************************************************
340 * DOSMEM_ResizeBlock
342 * Resize DOS memory block in place. Returns block size or -1 on error.
344 * If exact is TRUE, returned value is either old or requested block
345 * size. If exact is FALSE, block is expanded even if there is not
346 * enough space for full requested block size.
348 UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
350 char *block = NULL;
351 dosmem_info *info_block = DOSMEM_info_block;
352 dosmem_entry *dm;
353 dosmem_entry *next;
354 UINT blocksize;
355 UINT orgsize;
357 if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) ||
358 (ptr >= (void*)DOSMEM_MemoryTop()) ||
359 (((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) )
360 return (UINT)-1;
362 dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
363 if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) )
364 return (UINT)-1;
366 next = next_block(dm);
367 orgsize = dm->size & DM_BLOCK_MASK;
369 /* collapse free blocks */
370 while ( next->size & DM_BLOCK_FREE )
372 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
373 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
374 next = next_block(dm);
377 blocksize = dm->size & DM_BLOCK_MASK;
380 * If collapse didn't help we either expand block to maximum
381 * available size (exact == FALSE) or give collapsed blocks
382 * back to free storage (exact == TRUE).
384 if (blocksize < size)
385 size = exact ? orgsize : blocksize;
387 block = ((char*)dm) + sizeof(dosmem_entry);
388 if( blocksize - size > 0x20 )
391 * split dm so that the next one stays
392 * paragraph-aligned (and next gains free bit)
395 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
396 sizeof(dosmem_entry));
397 next = (dosmem_entry*)(((char*)dm) +
398 sizeof(dosmem_entry) + dm->size);
399 next->size = (blocksize - (dm->size +
400 sizeof(dosmem_entry))) | DM_BLOCK_FREE;
402 else
404 dm->size &= DM_BLOCK_MASK;
408 * Adjust available memory if block size changes.
410 info_block->free += orgsize - dm->size;
412 return size;
415 /***********************************************************************
416 * DOSMEM_Available
418 UINT DOSMEM_Available(void)
420 UINT blocksize, available = 0;
421 dosmem_entry *dm;
423 dm = DOSMEM_RootBlock();
425 while (dm && dm->size != DM_BLOCK_TERMINAL)
427 #ifdef __DOSMEM_DEBUG__
428 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
430 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
431 return NULL;
433 prev = dm;
434 #endif
435 if( dm->size & DM_BLOCK_FREE )
437 dosmem_entry *next = next_block(dm);
439 while ( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
441 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
442 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
443 next = next_block(dm);
446 blocksize = dm->size & DM_BLOCK_MASK;
447 if ( blocksize > available ) available = blocksize;
448 dm = next;
450 else dm = next_block(dm);
452 return available;
455 /***********************************************************************
456 * DOSMEM_InitMemory
458 * Initialises the DOS memory structures.
460 static void DOSMEM_InitMemory(char* addr)
462 dosmem_entry* root_block;
463 dosmem_entry* dm;
465 DOSMEM_FillBiosSegments();
466 DOSMEM_FillIsrTable();
468 DOSMEM_info_block = (dosmem_info*)addr;
469 root_block = DOSMEM_RootBlock();
470 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
472 DOSMEM_info_block->blocks = 0;
473 DOSMEM_info_block->free = root_block->size;
475 dm = next_block(root_block);
476 dm->size = DM_BLOCK_TERMINAL;
477 root_block->size |= DM_BLOCK_FREE
478 #ifdef __DOSMEM_DEBUG__
479 | DM_BLOCK_DEBUG
480 #endif
483 TRACE("DOS conventional memory initialized, %d bytes free.\n",
484 DOSMEM_Available());
487 /******************************************************************
488 * DOSMEM_InitDosMemory
490 * When WineDOS is loaded, initializes the current DOS memory layout.
492 BOOL DOSMEM_InitDosMemory(void)
494 HMODULE16 hModule;
495 unsigned short sel;
496 LDT_ENTRY entry;
497 DWORD reserve;
499 if (!(hModule = GetModuleHandle16("KERNEL"))) return FALSE;
500 /* KERNEL.194: __F000H */
501 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(ULONG_PTR)194));
502 wine_ldt_get_entry(sel, &entry);
503 DOSMEM_dosmem = (char*)wine_ldt_get_base(&entry) - 0xF0000;
504 /* KERNEL.183: __0000H */
505 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
506 wine_ldt_get_entry(sel, &entry);
507 DOSMEM_sysmem = wine_ldt_get_base(&entry);
510 * Reserve either:
511 * - lowest 64k for NULL pointer catching (Win16)
512 * - lowest 1k for interrupt handlers and
513 * another 0.5k for BIOS, DOS and intra-application
514 * areas (DOS)
516 if (DOSMEM_dosmem != DOSMEM_sysmem)
517 reserve = 0x10000; /* 64k */
518 else
519 reserve = 0x600; /* 1.5k */
522 * Round to paragraph boundary in order to make
523 * sure the alignment is correct.
525 reserve = ((reserve + 15) >> 4) << 4;
528 * Set DOS memory base and initialize conventional memory.
530 DOSMEM_InitMemory(DOSMEM_dosmem + reserve);
531 return TRUE;
534 /******************************************************************
535 * DOSMEM_MapDosLayout
537 * Initialize the first MB of memory to look like a real DOS setup
539 BOOL DOSMEM_MapDosLayout(void)
541 static int already_mapped;
543 if (!already_mapped)
545 HMODULE16 hModule;
546 unsigned short sel;
547 LDT_ENTRY entry;
549 if (DOSMEM_dosmem)
551 ERR( "Needs access to the first megabyte for DOS mode\n" );
552 ExitProcess(1);
554 MESSAGE( "Warning: unprotecting memory to allow real-mode calls.\n"
555 " NULL pointer accesses will no longer be caught.\n" );
556 VirtualProtect( NULL, DOSMEM_SIZE, PAGE_EXECUTE_READWRITE, NULL );
557 /* copy the BIOS and ISR area down */
558 memcpy( DOSMEM_dosmem, DOSMEM_sysmem, 0x400 + 0x100 );
559 DOSMEM_sysmem = DOSMEM_dosmem;
560 hModule = GetModuleHandle16("KERNEL");
561 /* selector to 0000H */
562 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)183));
563 wine_ldt_get_entry(sel, &entry);
564 wine_ldt_set_base(&entry, NULL);
565 wine_ldt_set_entry(sel, &entry);
566 /* selector to BiosData */
567 sel = LOWORD(GetProcAddress16(hModule, (LPCSTR)(DWORD_PTR)193));
568 wine_ldt_get_entry(sel, &entry);
569 wine_ldt_set_base(&entry, (const void*)0x400);
570 wine_ldt_set_entry(sel, &entry);
571 /* we may now need the actual interrupt stubs, and since we've just moved the
572 * interrupt vector table away, we can fill the area with stubs instead... */
573 DOSMEM_MakeIsrStubs();
574 already_mapped = 1;
576 return TRUE;