Author Topic: How to use Virtual x86 Mode with NASM  (Read 22182 times)

nobody

  • Guest
How to use Virtual x86 Mode with NASM
« on: May 04, 2009, 12:46:40 PM »
Hello,

for some reason I want to use the Virtual x86 Mode with NASM also in order to use the original BIOS Interrupts and Routines in a kind of Real-Mode simulated data aera.
I think this is quiet helpfull and instructive and it shouldn't be nessecary to use a so called DOS-BOX. I also don't want to use dosemu or as far as possible the kernel support with C-Headers or stuff like this.

I'd only like to know how i can manage to switch in that mode and than again back in PM on a - as far as possible - simple way. All I found on that topic until know was a generell description but not really a usefull example for me on how to do it directly. I think it is not very difficult to understand the generall idea. For example you can use an INT n instruction witch call the kernel running in privilege level 0. Before you use the IRET-Instruction to return to the caller you set the VM-Flag 17 in the image of eflag on the stack and have pushed the segment registers in Real Mode adressing too, and after you return to privilege 3 user level you should be in the Virtual Mode.

Theoretical this should be one of a least two ways to do it. But how can I get in an interrupt as an normal user? I mean, how can I put the right code in the Interrupts structure whithout having access to the inside of it? (If it is at all alloud and possible) Normally I call interrupts in linux whith only one  line (int 080h) and that's it, waiting for the kernel to it's work and return by itself, if its ready, without any support of own programm code by myself.

Another problems seems to be how to mapp the original BIOS in the right data area and how to get it (perhaps with break?)

Above all can it be a problem that I've been working with an x86_64 arch?

Here are two links on that topic:
http://lowlevel.brainsware.org/wiki/index.php/Virtual_8086_Mode
http://osdev.berlios.de/v86.html
and here's a excerption of the Intel manual about the Virtual- 8086 Mode:

"15.2.1       Enabling Virtual-8086 Mode
The processor runs in virtual-8086 mode when the VM (virtual machine) flag in the
EFLAGS register is set. This flag can only be set when the processor switches to a
new protected-mode task or resumes virtual-8086 mode via an IRET instruction.
System software cannot change the state of the VM flag directly in the EFLAGS
register (for example, by using the POPFD instruction). Instead it changes the flag in
the image of the EFLAGS register stored in the TSS or on the stack following a call to
an interrupt- or exception-handler procedure. For example, software sets the VM flag
in the EFLAGS image in the TSS when first creating a virtual-8086 task.
The processor tests the VM flag under three general conditions:
•   When loading segment registers, to determine whether to use 8086-style
    address translation.
•   When decoding instructions, to determine which instructions are not supported in
    virtual-8086 mode and which instructions are sensitive to IOPL.
•   When checking privileged instructions, on page accesses, or when performing
    other permission checks. (Virtual-8086 mode always executes at CPL 3.)..."

Does anyone know a kind of realizeable way, a kind of scaffold, or template that shows how it really can be done? Perhaps it is possible in NASM without using C or more advanced concepts like dosemu is?

Thanks a lot for every answer in advane

Michel

Tom Timmermann

  • Guest
Re: How to use Virtual x86 Mode with NASM
« Reply #1 on: May 04, 2009, 05:37:30 PM »
You may find some help here:

http://wiki.osdev.org/Virtual_8086_Mode


Also here by doing a search on v86:

http://forum.osdev.org/

TomT

nobody

  • Guest
Re: How to use Virtual x86 Mode with NASM
« Reply #2 on: May 06, 2009, 06:24:16 PM »
Thanks TomT,

the links helped a bit, but I think, it's just a very difficult and complicated topic. Now I know more, but before I try something, I have still to read a lot and to think about it too, until I have an usefull idea to start this whole thing for a first try.
It seems - unlike other parts and questions - here (for me) is no complete and immediatly understandable explaination. I have to find different pieces step by step, before I can get them together.

Michel

Offline yousafsajjad

  • Jr. Member
  • *
  • Posts: 5
Re: How to use Virtual x86 Mode with NASM
« Reply #3 on: July 08, 2010, 11:25:48 PM »
(I know its an old post but it is still a tough one)

Hey,

I am kinda looking into the same thing .. so have you find anything good regarding it ???

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: How to use Virtual x86 Mode with NASM
« Reply #4 on: July 08, 2010, 11:31:01 PM »
Everything you need to get V8086 Mode going can be found, directly or indirectly, at the OSDev.org link posted above by Tom.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to use Virtual x86 Mode with NASM
« Reply #5 on: July 09, 2010, 09:20:18 AM »
"nobody"/ Michel, back in may of last year, asked:

Quote
Perhaps it is possible in NASM without using C or more advanced concepts like dosemu is?

Yes, it is certainly possible in Nasm!

But he insisted on doing it without any help from the kernel syscalls, and from 64-bit, no less. (I'm not sure that's possible... someone told me recently that MS didn't have any choice about dropping dos support in 64-bit versions, since long mode doesn't support v86 mode). He wanted to do this from Linux, and presumably wanted to be back in Linux afterward. I think I'd have sent him to "kernel hackers" rather than "osdev", although I'm sure the osdev info would be helpful, too. Only thing I know is "sys_mmap" would probably be a better bet than "sys_brk" for getting memory...

How about yourself, yousafsajjad? Do you insist on doing it "without oxygen, without Sherpas, naked, in the winter", or are you willing to utilize some existing tools, at least to get started...

http://sourceforge.net/projects/lrmi/

What parts of the original question are you interested in? Yeah, we can do it in Nasm - those "higher concepts" don't have access to any instructions that we don't! But if the hardware won't do it, or the OS (if you insist on a particular OS) won't permit it, fergeddit! Whether it's "worthwhile" is another question.

Best,
Frank


Offline yousafsajjad

  • Jr. Member
  • *
  • Posts: 5
Re: How to use Virtual x86 Mode with NASM
« Reply #6 on: July 09, 2010, 02:49:27 PM »
hey frank, well I just want to do some basic stuff (i guess) .. to start with calling - WBINVD, I tried doing that it gave me a segmentation fault .. so i guess that is because CPL is not 0. So how can i set it to 0 ??

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to use Virtual x86 Mode with NASM
« Reply #7 on: July 09, 2010, 07:28:48 PM »
Boot from floppy.

If you can do it from "userland" in a protected OS, tell the developers - they'll want to fix the bug. I saw one once for win95(?) that involved replacing the divide-by-zero handler with your own, and then dividing by zero, but I think that's been fixed long ago.

I thought we were talking about using BIOS interrupts from user code - which *can* be done - dosemu does it. The closest I've ever come is getting a value in ax that looks suspiciously like the error code I'd get from a BIOS interrupt - so I think "something" happened - but I never got it to "work". As I mentioned, from "long mode" it may be flat-out impossible (although... "impossible"s a big word...).

That applies to getting v86 mode (which is CPL=3, unless I'm mistaken). To get to CPL=0 and doing wbinvd... maybe a kernel module, I dunno. Seems to me you'd invalidate the whole purpose of the OS, not just caches. Is there some non-malicious purpose for this?

Try the "kernel hackers" groups - it's probably a FAQ.

Best,
Frank