<sect>The Virtual Flags
<p>
Info written by Hans 
<htmlurl url="mailto:lermen@fgan.de" name="&lt;lermen@fgan.de&gt;">
to describe the virtual flags used by DOSEmu

<itemize>
<item> DOS sees only IF, but IF will never <em/really/ set in the CPU flagregister,
   because this would block Linux. So Linus maintains a virtual IF (VIF),
   which he sets and resets accordingly to CLI, STI, POPFx, IRETx.
   Because the DOS programm cannot look directly into the flagregister
   (exception the low 8 bits, but the IF is in bit 9), it does not
   realize, that the IF isn't set. To see it, it has to perform
   PUSHF and look at the stack.

   Well, but PUSHF or INTxx is intercepted by vm86 and then Linus looks
   at his virtual IF to set the IF <em/on/ the stack.
   Also, if IRETx or POPFx happen, Linus is getting the IF <em/from/ the
   stack, sets VIF accordingly, but leaves the <em/real/ IF untouched.

<item> Now, how is this realized? This is a bit more complicated.
   We have 3 places were the eflag register is stored in memory:

<descrip>
   <tag/vm86s.regs.eflags/                     in user space, seen by dosemu
   <tag/current->tss.v86flags/                virtual flags, macro VEFLAGS
   <tag/current->tss.vm86_info->regs.eflags/  the <em/real/ flags, CPU reg. VM86
</descrip>

   The last one is a kernel space copy of vm86_struct.regs.eflags.

   Also there are some masks to define, which bits of the flag
   should be passed to DOS and which should be taken from DOS:

<descrip>
   <tag/current->tss.v86mask/         CPU model dependent bits
   <tag/SAFE_MASK       (0xDD5)/      used the way <em/to/ DOS
   <tag/RETURN_MASK     (0xDFF)/      used the way <em/from/ DOS
</descrip>

   When sys_vm86 is entered, it first makes a copy of the whole
   vm86_struct vm86s (containing regs.eflags) and saves a pointer to it
   to current->tss.vm86_info. It merges the flags from 32-bit user
   space (NOTE: IF is always set) over SAFE_MASK and current->tss.v86mask
   into current->tss.v86mask. From this point on, all changes to
   VIF, VIP (virtual interrupt pending) are only done in VEFLAGS.
   To handle the flag setting there are macros within vm86.c,
   which do the following:

<descrip>
   <tag/set_IF, clear_IF/  only modifies VEFLAGS;
   <tag/clear_TF/          sets a bit in the <em/real/ flags;
   <tag/set_vflags(x)/     set flags x over SAFE_MASK  to <em/real/ flags
                     (IF is not touched)
   <tag/x=get_vflags/      returns <em/real/ flags over RETURN_MASK and translates 
                     VIF of VEFLAGS to IF in x;
</descrip> 

   When it's time to return from vm86() to user space, the <em/real/ flags
   are merged together with VIF and VIP from VEFLAGS and put into
   the userspace vm86s.regs.eflags. This is done by save_v86_state()
   and this does <em/not/ translate the VIF to IF, it should be as it
   was on entry of sys_vm86: set to 1.

<item> Now what are we doing with eflags in dosemu ?
   Well, this I don't really know. I saw IF used (told it Larry), I saw
   VIF tested an set, I saw TF cleared, and NT flag e.t.c.

   But I know what Linus thinks that we should do:
   Always interpret and set VIF, and let IF untouched, it will nevertheless
   set to 1 at entry of sys_vm86.

   How I think we should proceed? Well this I did describe in my last mail.


,,,, and this from a follow-up mail:


<em/NOTE/ VIF and VIP in DOS-CPU-flagsregister are inherited from 32-bit,
  so actually they are both ZERO.

      On return to 32-bit, <em/only/ VIF will appear in vm86s.regs.eflags !
      <em/VIP will be ZERO/, again: <em/VIP/ will be used <em/only once/ !!!!
<p>
[ ,,, ]
<p>
I have to add, that VIP will be cleared, because it is not in any
of the masks of vm86.

</itemize>
