Virtual 8086 issues when OS is installed to Hard Drive

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
GhelloWorld
Member
Member
Posts: 27
Joined: Thu Apr 19, 2018 5:31 am

Virtual 8086 issues when OS is installed to Hard Drive

Post by GhelloWorld »

Hi there,

It has been a while since I have posted something on this forum but the time has arrived for me to start asking questions again. Lately I have been working on writing a installer/setup program for my OS. With this installer it is possible to install the operating system onto a hard drive so that it is possible to boot without a live-CD. Lets start with the good news shall we. The installer is working as expected and the operating system is actually able to boot from a hard disk. The problem comes when using bios functions from vm86 mode that include buffer access. This is perhaps a bit vague so here is a example:

Code: Select all

// Enable power management for all devices
MemoryOperations::memset(&args, 0, sizeof(VM86Arguments));
args.AX = (uint16_t)0x5300 | (uint16_t)APM_FUNC_ENABLE_POWER_MANAGEMENT;
args.BX = APM_ALL_DEVICE; //All Devices
args.CX = 0x1; //Enable power control by APM BIOS
System::vm86Manager->CallInterrupt(0x15, &args);
The above code works as intended and does not use a pointer to a buffer

Code: Select all

Log(Info, "Probing For Controller Information");
VM86Arguments regs;
MemoryOperations::memset(&regs, 0, sizeof(VM86Arguments));
regs.AX = 0x4F00;
regs.DI = (uint16_t)vesaInfo; //This buffer does not get modified
virtual8086Manager->CallInterrupt(0x10, &regs);
The weird thing is that things like this one above do not work

The buffer (in this case vesaInfo) remains the same which results in a error. This behavior is not only present in the VBE functions but also when using the bios to receive the EDID information. The weird thing is that these functions work perfect when running the liveCD.

I have run the above code in a debugger and there everything seems to be fine as well, it does not look like memory corruption or something like that. I have also checked that the executable executed by grub is the same as on the liveCD and this is indeed the case. I have faced a lot of issues over the years but those at least seemed to make sense. I have no idea what is causing this behavior and I would really like any help.

My OS can be found at: https://github.com/Remco123/CactusOS
Virtual8068 Source: https://github.com/Remco123/CactusOS/tr ... irtual8086
Code that is causing problems: https://github.com/Remco123/CactusOS/bl ... id.cpp#L58 and https://github.com/Remco123/CactusOS/bl ... sa.cpp#L63
Hard drive image (7 days link): https://send.firefox.com/download/bd7f1 ... kFmDpgvKMg

Thanks for reading, I look forward to your suggestions :D
User avatar
jlxip
Posts: 10
Joined: Sat Jul 27, 2019 5:47 pm
Location: Granada, Spain
Contact:

Re: Virtual 8086 issues when OS is installed to Hard Drive

Post by jlxip »

I can almost 100% guarantee that your bug has nothing to do with your OS being installed on the hard drive or not.

Try using Bochs to debug your V86 assembly (setting breakpoints in the source and such). Just before the interrupt, are the registers set like you expected? Dump the buffer with Bochs before and after the interrupt is executed. If everything seems right, it might be a misuse of the BIOS interrupt (I assume you've checked Ralf Brown's extensively, but read it again and again).

Maybe the buffer address is wrong? Another part of your code might be writing to it "asynchronously" or something (specially if it's a magic number that you use multiple times). If you're using GRUB there might be an issue with that (unlikely, but might be worth checking); as far as I remember, GRUB sometimes loads modules and stuff in parts of the memory. I am not sure about this, I have almost no experience using it.

Take a break and look at the big picture. If you limit where the error is and it makes no sense, the bug is in some completely unrelated part of your code. Happened to me recently, my filesystem implementation was broken since the beginning and the bug manifested itself in a completely non related scenario, just because the kernel binary got bigger.

It might be worth checking V86 documentation as well. Have you disabled paging before the CPU goes into it?

Good luck.
GhelloWorld
Member
Member
Posts: 27
Joined: Thu Apr 19, 2018 5:31 am

Re: Virtual 8086 issues when OS is installed to Hard Drive

Post by GhelloWorld »

I decided to re-check the registers before the interrupt and there is indeed something wrong. For some reason ES gets set to a random value while it should be zero. After removing the ES register from the VM86 Arguments everything works fine. I am happy that it works again, but I do need to investigate this issue more. Perhaps more pieces of code are vulnerable. Thanks for the Help!
Attachments
You can see the ES register being set to 0xb825
You can see the ES register being set to 0xb825
Post Reply