OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 2:40 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Virtual 8086 issues when OS is installed to Hard Drive
PostPosted: Thu May 21, 2020 6:22 am 
Offline
Member
Member

Joined: Thu Apr 19, 2018 5:31 am
Posts: 27
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:
// 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:
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/tree/master/kernel/src/system/virtual8086
Code that is causing problems: https://github.com/Remco123/CactusOS/blob/master/kernel/src/system/components/edid.cpp#L58 and https://github.com/Remco123/CactusOS/blob/master/kernel/src/system/components/vesa.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

_________________
Currently working on https://github.com/Remco123/CactusOS


Top
 Profile  
 
 Post subject: Re: Virtual 8086 issues when OS is installed to Hard Drive
PostPosted: Fri May 22, 2020 8:53 pm 
Offline
User avatar

Joined: Sat Jul 27, 2019 5:47 pm
Posts: 10
Location: Granada, Spain
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.


Top
 Profile  
 
 Post subject: Re: Virtual 8086 issues when OS is installed to Hard Drive
PostPosted: Sat May 23, 2020 6:05 am 
Offline
Member
Member

Joined: Thu Apr 19, 2018 5:31 am
Posts: 27
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:
File comment: You can see the ES register being set to 0xb825
bochsDBG.png
bochsDBG.png [ 71.07 KiB | Viewed 790 times ]

_________________
Currently working on https://github.com/Remco123/CactusOS
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: awik, Bing [Bot], Google [Bot] and 211 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group