How to generally enter virtual 8086 mode?

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.
uruseiyatsura
Posts: 16
Joined: Mon Apr 04, 2022 9:20 am

How to generally enter virtual 8086 mode?

Post by uruseiyatsura »

Hello guys, i'm searching for a working code to switch into v8086. I found this on OSDev Wiki:

Code: Select all

; you should declare this function as :
; extern void enter_v86(uint32_t ss, uint32_t esp, uint32_t cs, uint32_t eip);
enter_v86:
   mov ebp, esp               ; save stack pointer

   push dword  [ebp+4]        ; ss
   push dword  [ebp+8]        ; esp
   pushfd                     ; eflags
   or dword [esp], (1 << 17)  ; set VM flags
   push dword [ebp+12]        ; cs
   push dword  [ebp+16]       ; eip
   iret
But I'm so stupid :mrgreen: and so I can not understand how to use this function and how to exit from a v8086 task.
I need a help especially for activating VGA 256 colours mode, and floppy disk reading. I know you don't reccomend 8086 disk reading, but I don't care. :lol:
I'm a beginner in fact of OSDev programming.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How to generally enter virtual 8086 mode?

Post by Octocontrabass »

Entering virtual 8086 mode is the same as entering user mode. The only real difference between a virtual 8086 mode task and any other task is the state of EFLAGS.VM.

Exiting virtual 8086 mode is also the same as exiting user mode: the task will perform a system call, or the task will cause an exception, or external hardware will interrupt the CPU.

Reading the floppy disk requires IRQs, which means your kernel will need to catch the floppy disk IRQs (and possibly others, like timer IRQs) and redirect them into the virtual 8086 mode task. This is not exactly a beginner-level task...
rdos
Member
Member
Posts: 3296
Joined: Wed Oct 01, 2008 1:55 pm

Re: How to generally enter virtual 8086 mode?

Post by rdos »

I think calling real BIOS functions when developing support for V86 mode is not a good idea. You should start by calling V86 code that just does an iret. Note that you will need to handle exceptions from V86 mode, in particular those that affect flags and those that return back to your kernel.

You also need to create a stack for V86 mode, and then you need to push a reference to something that takes you back to your kernel. For instance, if you use iret to return (which a BIOS function will do), then you need to push 16-bit flags, cs and ip that reference some location in V86 mode that contains code that trigger you to return to your kernel. As an example, you could place an invalid instruction there and handle this exception in your kernel, and detect the source. Of course, you will need to unwind your exception handler and save the return position in your kernel so the exception handler can go there.

There actually is a big difference between calling V86 code and user mode. Generally, user mode code will use syscalls to access your kernel, which works fine with the normal flow. However, with V86 code, you want to "call" certain software in the BIOS, which is in the reverse direction and not part of the normal execution flow.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: How to generally enter virtual 8086 mode?

Post by MichaelPetch »

I have a very basic sample here that enters V8086 mode. V8086 mode is simply exited with a UD2 instruction (not useful way of doing it but was meant as an example). It has very basic support for an INT N wrapper etc. https://github.com/marleyd386/OSDev/tre ... -intn-iret
uruseiyatsura
Posts: 16
Joined: Mon Apr 04, 2022 9:20 am

Re: How to generally enter virtual 8086 mode?

Post by uruseiyatsura »

thank you, but i still not understand. [-X i will study, then i will return on this thread.
goodbye guys
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to generally enter virtual 8086 mode?

Post by iansjack »

Are you, unintentionally, making life difficult for yourself?

Set the VGA mode before you go into protected mode.

Write a protected mode floppy disk driver; it's probably easier than grappling with virtual 8086 mode (and getting real mode interrupts to work). When your OS grows up and wants to use 64-bit mode the virtual 8086 mode isn't available.
I know you don't reccomend 8086 disk reading, but I don't care.
Fair enough, but if you don't care why should anyone else care enough to help you?
uruseiyatsura
Posts: 16
Joined: Mon Apr 04, 2022 9:20 am

Re: How to generally enter virtual 8086 mode?

Post by uruseiyatsura »

Are you, unintentionally, making life difficult for yourself?

Set the VGA mode before you go into protected mode.

Write a protected mode floppy disk driver; it's probably easier than grappling with virtual 8086 mode (and getting real mode interrupts to work). When your OS grows up and wants to use 64-bit mode the virtual 8086 mode isn't available.
How can I set a protected mode floppy driver so? :?: :cry:
How can I set VGA mode when I use grub that automatically sets protected mode? [-X [-o<
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to generally enter virtual 8086 mode?

Post by iansjack »

You don't get it, you write it. That's what OS development is all about. Information here: https://wiki.osdev.org/Floppy_Disk_Controller

Grub lets you specify the graphics mode: https://www.gnu.org/software/grub/manua ... ation.html
uruseiyatsura
Posts: 16
Joined: Mon Apr 04, 2022 9:20 am

Re: How to generally enter virtual 8086 mode?

Post by uruseiyatsura »

You don't get it, you write it. That's what OS development is all about. Information here: https://wiki.osdev.org/Floppy_Disk_Controller
Thank you very much! But don't close this thread please.
uruseiyatsura
Posts: 16
Joined: Mon Apr 04, 2022 9:20 am

Re: How to generally enter virtual 8086 mode?

Post by uruseiyatsura »

iansjack wrote:You don't get it, you write it. That's what OS development is all about. Information here: https://wiki.osdev.org/Floppy_Disk_Controller

Grub lets you specify the graphics mode: https://www.gnu.org/software/grub/manua ... ation.html
Cannot find the GRUB option you said to exist. I'm using a multiboot kernel, this can help you if you want to help me, but actually I found nothing on that site.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: How to generally enter virtual 8086 mode?

Post by kzinti »

To select the video mode in Grub, you use the gfxmode option. It took me all of 5 seconds to find it at the link you provided:

https://www.gnu.org/software/grub/manua ... ml#gfxmode

As to how you retrieve that info from multiboot, check the spec you are using (multiboot 1 or 2), it has the necessary information.
uruseiyatsura
Posts: 16
Joined: Mon Apr 04, 2022 9:20 am

Re: How to generally enter virtual 8086 mode?

Post by uruseiyatsura »

kzinti wrote:To select the video mode in Grub, you use the gfxmode option. It took me all of 5 seconds to find it at the link you provided:

https://www.gnu.org/software/grub/manua ... ml#gfxmode
This is my grub.cfg:

Code: Select all

GRUB_TERMINAL_OUTPUT=gfxmode
menuentry "KabiCOM Control Panel for Silly People" {
	set gfxmode=320x200x8
	multiboot (cd)/boot/CpSp.bin
}
it doesn't work
iProgramInCpp
Member
Member
Posts: 81
Joined: Sun Apr 21, 2019 7:39 am

Re: How to generally enter virtual 8086 mode?

Post by iProgramInCpp »

uruseiyatsura wrote: I need a help especially for activating VGA 256 colours mode
Do you really need BIOS interrupts? Because you can switch the VGA mode without them;
check https://files.osdev.org/mirrors/geezer/ ... cs/modes.c for some code, and https://wiki.osdev.org/VGA_Resources.

Every piece of hardware has an interface you can write a driver for, you should not need to depend on black-box code provided by the BIOS.
Hey! I'm developing two operating systems:

NanoShell --- A 32-bit operating system whose GUI takes inspiration from Windows 9x and early UNIX desktop managers.
Boron --- A portable SMP operating system taking inspiration from the design of the Windows NT kernel.
iProgramInCpp
Member
Member
Posts: 81
Joined: Sun Apr 21, 2019 7:39 am

Re: How to generally enter virtual 8086 mode?

Post by iProgramInCpp »

uruseiyatsura wrote:
kzinti wrote:To select the video mode in Grub, you use the gfxmode option. It took me all of 5 seconds to find it at the link you provided:

https://www.gnu.org/software/grub/manua ... ml#gfxmode
This is my grub.cfg:

Code: Select all

GRUB_TERMINAL_OUTPUT=gfxmode
menuentry "KabiCOM Control Panel for Silly People" {
	set gfxmode=320x200x8
	multiboot (cd)/boot/CpSp.bin
}
it doesn't work
Did you turn on https://www.gnu.org/software/grub/manua ... ot_002eh-1 MULTIBOOT_VIDEO_MODE in your Multiboot header? This will allow you to use setgfxpayload, which will set the graphics payload. The menuentry is broken right now because you set gfxmode, not gfxpayload, and you don't call "boot", and the gfxpayload set is done before loading the kernel, when it should be after. Here's a fixed configuration:

Code: Select all

menuentry "KabiCOM Control Panel for Silly People" {
	multiboot (cd)/boot/CpSp.bin
	set gfxpayload=800x600x32
	boot
}
One more thing, this will set a VBE video mode, if you really want the 320x200 256 color screen mode I recommend checking the resources I posted earlier, or keeping on with V8086
Hey! I'm developing two operating systems:

NanoShell --- A 32-bit operating system whose GUI takes inspiration from Windows 9x and early UNIX desktop managers.
Boron --- A portable SMP operating system taking inspiration from the design of the Windows NT kernel.
rdos
Member
Member
Posts: 3296
Joined: Wed Oct 01, 2008 1:55 pm

Re: How to generally enter virtual 8086 mode?

Post by rdos »

Actually, if he just wants to set a video mode, then it would be much easier to do his own boot-loader, and in the boot process call BIOS to set the desired mode.

No real OS will depend on GRUB anyway. Takes away all the fun. ;-)
Post Reply