biosemu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /****************************************************************************
  2. *
  3. * BIOS emulator and interface
  4. * to Realmode X86 Emulator Library
  5. *
  6. * Copyright (C) 1996-1999 SciTech Software, Inc.
  7. *
  8. * ========================================================================
  9. *
  10. * Permission to use, copy, modify, distribute, and sell this software and
  11. * its documentation for any purpose is hereby granted without fee,
  12. * provided that the above copyright notice appear in all copies and that
  13. * both that copyright notice and this permission notice appear in
  14. * supporting documentation, and that the name of the authors not be used
  15. * in advertising or publicity pertaining to distribution of the software
  16. * without specific, written prior permission. The authors makes no
  17. * representations about the suitability of this software for any purpose.
  18. * It is provided "as is" without express or implied warranty.
  19. *
  20. * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  21. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  22. * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  23. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  24. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  25. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  26. * PERFORMANCE OF THIS SOFTWARE.
  27. *
  28. * ========================================================================
  29. *
  30. * Language: ANSI C
  31. * Environment: Any
  32. * Developer: Kendall Bennett
  33. *
  34. * Description: Module implementing the system specific functions. This
  35. * module is always compiled and linked in the OS depedent
  36. * libraries, and never in a binary portable driver.
  37. *
  38. ****************************************************************************/
  39. #include "biosemui.h"
  40. #include <string.h>
  41. #include <stdlib.h>
  42. /*------------------------- Global Variables ------------------------------*/
  43. BE_sysEnv _BE_env;
  44. #ifdef __DRIVER__
  45. PM_imports _VARAPI _PM_imports;
  46. #endif
  47. static X86EMU_memFuncs _BE_mem = {
  48. BE_rdb,
  49. BE_rdw,
  50. BE_rdl,
  51. BE_wrb,
  52. BE_wrw,
  53. BE_wrl,
  54. };
  55. #ifdef DEBUG
  56. static X86EMU_pioFuncs _BE_pio = {
  57. BE_inb,
  58. BE_inw,
  59. BE_inl,
  60. BE_outb,
  61. BE_outw,
  62. BE_outl,
  63. };
  64. #else
  65. static X86EMU_pioFuncs _BE_pio = {
  66. (void*)PM_inpb,
  67. (void*)PM_inpw,
  68. (void*)PM_inpd,
  69. (void*)PM_outpb,
  70. (void*)PM_outpw,
  71. (void*)PM_outpd,
  72. };
  73. #endif
  74. /*-------------------------- Implementation -------------------------------*/
  75. #define OFF(addr) (u16)(((addr) >> 0) & 0xffff)
  76. #define SEG(addr) (u16)(((addr) >> 4) & 0xf000)
  77. /****************************************************************************
  78. PARAMETERS:
  79. debugFlags - Flags to enable debugging options (debug builds only)
  80. memSize - Amount of memory to allocate for real mode machine
  81. info - Pointer to default VGA device information
  82. REMARKS:
  83. This functions initialises the BElib, and uses the passed in
  84. BIOS image as the BIOS that is used and emulated at 0xC0000.
  85. ****************************************************************************/
  86. ibool PMAPI BE_init(
  87. u32 debugFlags,
  88. int memSize,
  89. BE_VGAInfo *info)
  90. {
  91. #ifndef __DRIVER__
  92. PM_init();
  93. #endif
  94. memset(&M,0,sizeof(M));
  95. if (memSize < 20480)
  96. PM_fatalError("Emulator requires at least 20Kb of memory!\n");
  97. if ((M.mem_base = (unsigned long)malloc(memSize)) == NULL)
  98. PM_fatalError("Out of memory!");
  99. M.mem_size = memSize;
  100. _BE_env.busmem_base = (ulong)PM_mapPhysicalAddr(0xA0000,0x5FFFF,true);
  101. M.x86.debug = debugFlags;
  102. _BE_bios_init((u32*)info->LowMem);
  103. X86EMU_setupMemFuncs(&_BE_mem);
  104. X86EMU_setupPioFuncs(&_BE_pio);
  105. BE_setVGA(info);
  106. return true;
  107. }
  108. /****************************************************************************
  109. PARAMETERS:
  110. debugFlags - Flags to enable debugging options (debug builds only)
  111. REMARKS:
  112. This function allows the application to enable logging and debug flags
  113. on a function call basis, so we can specifically enable logging only
  114. for specific functions that are causing problems in debug mode.
  115. ****************************************************************************/
  116. void PMAPI BE_setDebugFlags(
  117. u32 debugFlags)
  118. {
  119. M.x86.debug = debugFlags;
  120. }
  121. /****************************************************************************
  122. PARAMETERS:
  123. info - Pointer to VGA device information to make current
  124. REMARKS:
  125. This function sets the VGA BIOS functions in the emulator to point to the
  126. specific VGA BIOS in use. This includes swapping the BIOS interrupt
  127. vectors, BIOS image and BIOS data area to the new BIOS. This allows the
  128. real mode BIOS to be swapped without resetting the entire emulator.
  129. ****************************************************************************/
  130. void PMAPI BE_setVGA(
  131. BE_VGAInfo *info)
  132. {
  133. _BE_env.vgaInfo.pciInfo = info->pciInfo;
  134. _BE_env.vgaInfo.BIOSImage = info->BIOSImage;
  135. if (info->BIOSImage) {
  136. _BE_env.biosmem_base = (ulong)info->BIOSImage;
  137. _BE_env.biosmem_limit = 0xC0000 + info->BIOSImageLen-1;
  138. }
  139. else {
  140. _BE_env.biosmem_base = _BE_env.busmem_base + 0x20000;
  141. _BE_env.biosmem_limit = 0xC7FFF;
  142. }
  143. if (*((u32*)info->LowMem) == 0)
  144. _BE_bios_init((u32*)info->LowMem);
  145. memcpy((u8*)M.mem_base,info->LowMem,sizeof(info->LowMem));
  146. }
  147. /****************************************************************************
  148. PARAMETERS:
  149. info - Pointer to VGA device information to retrieve current
  150. REMARKS:
  151. This function returns the VGA BIOS functions currently active in the
  152. emulator, so they can be restored at a later date.
  153. ****************************************************************************/
  154. void PMAPI BE_getVGA(
  155. BE_VGAInfo *info)
  156. {
  157. info->pciInfo = _BE_env.vgaInfo.pciInfo;
  158. info->BIOSImage = _BE_env.vgaInfo.BIOSImage;
  159. memcpy(info->LowMem,(u8*)M.mem_base,sizeof(info->LowMem));
  160. }
  161. /****************************************************************************
  162. PARAMETERS:
  163. r_seg - Segment for pointer to convert
  164. r_off - Offset for pointer to convert
  165. REMARKS:
  166. This function maps a real mode pointer in the emulator memory to a protected
  167. mode pointer that can be used to directly access the memory.
  168. NOTE: The memory is *always* in little endian format, son on non-x86
  169. systems you will need to do endian translations to access this
  170. memory.
  171. ****************************************************************************/
  172. void * PMAPI BE_mapRealPointer(
  173. uint r_seg,
  174. uint r_off)
  175. {
  176. u32 addr = ((u32)r_seg << 4) + r_off;
  177. if (addr >= 0xC0000 && addr <= _BE_env.biosmem_limit) {
  178. return (void*)(_BE_env.biosmem_base + addr - 0xC0000);
  179. }
  180. else if (addr >= 0xA0000 && addr <= 0xFFFFF) {
  181. return (void*)(_BE_env.busmem_base + addr - 0xA0000);
  182. }
  183. return (void*)(M.mem_base + addr);
  184. }
  185. /****************************************************************************
  186. PARAMETERS:
  187. len - Return the length of the VESA buffer
  188. rseg - Place to store VESA buffer segment
  189. roff - Place to store VESA buffer offset
  190. REMARKS:
  191. This function returns the address of the VESA transfer buffer in real
  192. mode emulator memory. The VESA transfer buffer is always 1024 bytes long,
  193. and located at 15Kb into the start of the real mode memory (16Kb is where
  194. we put the real mode code we execute for issuing interrupts).
  195. NOTE: The memory is *always* in little endian format, son on non-x86
  196. systems you will need to do endian translations to access this
  197. memory.
  198. ****************************************************************************/
  199. void * PMAPI BE_getVESABuf(
  200. uint *len,
  201. uint *rseg,
  202. uint *roff)
  203. {
  204. *len = 1024;
  205. *rseg = SEG(0x03C00);
  206. *roff = OFF(0x03C00);
  207. return (void*)(M.mem_base + ((u32)*rseg << 4) + *roff);
  208. }
  209. /****************************************************************************
  210. REMARKS:
  211. Cleans up and exits the emulator.
  212. ****************************************************************************/
  213. void PMAPI BE_exit(void)
  214. {
  215. free((void*)M.mem_base);
  216. PM_freePhysicalAddr((void*)_BE_env.busmem_base,0x5FFFF);
  217. }
  218. /****************************************************************************
  219. PARAMETERS:
  220. seg - Segment of code to call
  221. off - Offset of code to call
  222. regs - Real mode registers to load
  223. sregs - Real mode segment registers to load
  224. REMARKS:
  225. This functions calls a real mode far function at the specified address,
  226. and loads all the x86 registers from the passed in registers structure.
  227. On exit the registers returned from the call are returned in the same
  228. structures.
  229. ****************************************************************************/
  230. void PMAPI BE_callRealMode(
  231. uint seg,
  232. uint off,
  233. RMREGS *regs,
  234. RMSREGS *sregs)
  235. {
  236. M.x86.R_EAX = regs->e.eax;
  237. M.x86.R_EBX = regs->e.ebx;
  238. M.x86.R_ECX = regs->e.ecx;
  239. M.x86.R_EDX = regs->e.edx;
  240. M.x86.R_ESI = regs->e.esi;
  241. M.x86.R_EDI = regs->e.edi;
  242. M.x86.R_DS = sregs->ds;
  243. M.x86.R_ES = sregs->es;
  244. M.x86.R_FS = sregs->fs;
  245. M.x86.R_GS = sregs->gs;
  246. M.x86.R_CS = (u16)seg;
  247. M.x86.R_IP = (u16)off;
  248. M.x86.R_SS = SEG(M.mem_size - 1);
  249. M.x86.R_SP = OFF(M.mem_size - 1);
  250. X86EMU_exec();
  251. regs->e.cflag = M.x86.R_EFLG & F_CF;
  252. regs->e.eax = M.x86.R_EAX;
  253. regs->e.ebx = M.x86.R_EBX;
  254. regs->e.ecx = M.x86.R_ECX;
  255. regs->e.edx = M.x86.R_EDX;
  256. regs->e.esi = M.x86.R_ESI;
  257. regs->e.edi = M.x86.R_EDI;
  258. sregs->ds = M.x86.R_DS;
  259. sregs->es = M.x86.R_ES;
  260. sregs->fs = M.x86.R_FS;
  261. sregs->gs = M.x86.R_GS;
  262. }
  263. /****************************************************************************
  264. PARAMETERS:
  265. intno - Interrupt number to execute
  266. in - Real mode registers to load
  267. out - Place to store resulting real mode registers
  268. REMARKS:
  269. This functions calls a real mode interrupt function at the specified address,
  270. and loads all the x86 registers from the passed in registers structure.
  271. On exit the registers returned from the call are returned in out stucture.
  272. ****************************************************************************/
  273. int PMAPI BE_int86(
  274. int intno,
  275. RMREGS *in,
  276. RMREGS *out)
  277. {
  278. M.x86.R_EAX = in->e.eax;
  279. M.x86.R_EBX = in->e.ebx;
  280. M.x86.R_ECX = in->e.ecx;
  281. M.x86.R_EDX = in->e.edx;
  282. M.x86.R_ESI = in->e.esi;
  283. M.x86.R_EDI = in->e.edi;
  284. ((u8*)M.mem_base)[0x4000] = 0xCD;
  285. ((u8*)M.mem_base)[0x4001] = (u8)intno;
  286. ((u8*)M.mem_base)[0x4002] = 0xC3;
  287. M.x86.R_CS = SEG(0x04000);
  288. M.x86.R_IP = OFF(0x04000);
  289. M.x86.R_SS = SEG(M.mem_size - 1);
  290. M.x86.R_SP = OFF(M.mem_size - 1);
  291. X86EMU_exec();
  292. out->e.cflag = M.x86.R_EFLG & F_CF;
  293. out->e.eax = M.x86.R_EAX;
  294. out->e.ebx = M.x86.R_EBX;
  295. out->e.ecx = M.x86.R_ECX;
  296. out->e.edx = M.x86.R_EDX;
  297. out->e.esi = M.x86.R_ESI;
  298. out->e.edi = M.x86.R_EDI;
  299. return out->x.ax;
  300. }
  301. /****************************************************************************
  302. PARAMETERS:
  303. intno - Interrupt number to execute
  304. in - Real mode registers to load
  305. out - Place to store resulting real mode registers
  306. sregs - Real mode segment registers to load
  307. REMARKS:
  308. This functions calls a real mode interrupt function at the specified address,
  309. and loads all the x86 registers from the passed in registers structure.
  310. On exit the registers returned from the call are returned in out stucture.
  311. ****************************************************************************/
  312. int PMAPI BE_int86x(
  313. int intno,
  314. RMREGS *in,
  315. RMREGS *out,
  316. RMSREGS *sregs)
  317. {
  318. M.x86.R_EAX = in->e.eax;
  319. M.x86.R_EBX = in->e.ebx;
  320. M.x86.R_ECX = in->e.ecx;
  321. M.x86.R_EDX = in->e.edx;
  322. M.x86.R_ESI = in->e.esi;
  323. M.x86.R_EDI = in->e.edi;
  324. M.x86.R_DS = sregs->ds;
  325. M.x86.R_ES = sregs->es;
  326. M.x86.R_FS = sregs->fs;
  327. M.x86.R_GS = sregs->gs;
  328. ((u8*)M.mem_base)[0x4000] = 0xCD;
  329. ((u8*)M.mem_base)[0x4001] = (u8)intno;
  330. ((u8*)M.mem_base)[0x4002] = 0xC3;
  331. M.x86.R_CS = SEG(0x04000);
  332. M.x86.R_IP = OFF(0x04000);
  333. M.x86.R_SS = SEG(M.mem_size - 1);
  334. M.x86.R_SP = OFF(M.mem_size - 1);
  335. X86EMU_exec();
  336. out->e.cflag = M.x86.R_EFLG & F_CF;
  337. out->e.eax = M.x86.R_EAX;
  338. out->e.ebx = M.x86.R_EBX;
  339. out->e.ecx = M.x86.R_ECX;
  340. out->e.edx = M.x86.R_EDX;
  341. out->e.esi = M.x86.R_ESI;
  342. out->e.edi = M.x86.R_EDI;
  343. sregs->ds = M.x86.R_DS;
  344. sregs->es = M.x86.R_ES;
  345. sregs->fs = M.x86.R_FS;
  346. sregs->gs = M.x86.R_GS;
  347. return out->x.ax;
  348. }
  349. #ifdef __DRIVER__
  350. /****************************************************************************
  351. REMARKS:
  352. Empty log function for binary portable DLL. The BPD is compiled without
  353. debug information, so very little is logged anyway so it is simpler this
  354. way.
  355. ****************************************************************************/
  356. void printk(const char *msg, ...)
  357. {
  358. }
  359. /****************************************************************************
  360. REMARKS:
  361. Fatal error handler called when a non-imported function is called by the
  362. driver. We leave this to a runtime error so that older applications and
  363. shell drivers will work with newer bpd drivers provided no newer functions
  364. are required by the driver itself. If they are, the application or shell
  365. driver needs to be recompiled.
  366. ****************************************************************************/
  367. static void _PM_fatalErrorHandler(void)
  368. {
  369. PM_fatalError("Unsupported PM_imports import function called! Please re-compile!\n");
  370. }
  371. /****************************************************************************
  372. PARAMETERS:
  373. beImp - BE library imports
  374. beImp - Generic emulator imports
  375. RETURNS:
  376. Pointer to exported function list
  377. REMARKS:
  378. This function initialises the BIOS emulator library and returns the list of
  379. loader library exported functions.
  380. {secret}
  381. ****************************************************************************/
  382. BE_exports * _CEXPORT BE_initLibrary(
  383. PM_imports *pmImp)
  384. {
  385. static BE_exports _BE_exports = {
  386. sizeof(BE_exports),
  387. BE_init,
  388. BE_setVGA,
  389. BE_getVGA,
  390. BE_mapRealPointer,
  391. BE_getVESABuf,
  392. BE_callRealMode,
  393. BE_int86,
  394. BE_int86x,
  395. NULL,
  396. BE_exit,
  397. };
  398. int i,max;
  399. ulong *p;
  400. /* Initialize all default imports to point to fatal error handler */
  401. /* for upwards compatibility. */
  402. max = sizeof(_PM_imports)/sizeof(BE_initLibrary_t);
  403. for (i = 0,p = (ulong*)&_PM_imports; i < max; i++)
  404. *p++ = (ulong)_PM_fatalErrorHandler;
  405. /* Now copy all our imported functions */
  406. memcpy(&_PM_imports,pmImp,MIN(sizeof(_PM_imports),pmImp->dwSize));
  407. return &_BE_exports;
  408. }
  409. #endif /* __DRIVER__ */