Skip to content

Conversation

lkundrak
Copy link
Contributor

@lkundrak lkundrak commented Mar 2, 2025

While hunting out a suspected bug (which turned out to be a false alarm) while running OpenFirmware x86 Forth dictionaries, I've noticed some differences in libx86emu compared to my actual Intel processor. I've patched them to rule them out as culprits of my bug -- this branch contains the patches + test cases.

I don't think any of these are actually bugs, but it still might be useful for emulation to mimic actual hardware more closely.

lkundrak added 2 commits March 2, 2025 13:52
…tack

The segment registers are always 16-bit. Pushing them onto 32-bit stack
segment advances the stack pointer by 32 bits, but only writes 16 bits,
keeping the most significant 16 bits unchanged.

On an Intel Core i7 (stepping 12):

  $ cat pushseg.c
  #include <stdio.h>

  int
  main ()
  {
      long wrd;

      asm (
          "push $0x12345678\n\t"
          "pop %%eax\n\t"
          "push %%ss\n\t"
          "pop %0\n\t"
          : "=r" (wrd) :
      );

      printf ("0x%08x\n", wrd);
  }

  $ cc -m32 pushseg.c
  $ ./a.out
  0x1234002b
  $
Left shift is documented to leave AF undefined, but in reality it resets
the flag if the bit count is different from zero.

On an Intel Core i7 (stepping 12):

  $ cat shlaf.c
  #include <stdio.h>

  int
  main ()
  {
      long wrd;

      asm (
          "mov $0xffffffff, %%ecx\n\t"
          "push $0x210\n\t" // IF | AF
          "popf\n\t"
          "shl $1, %%ecx\n\t"
          "pushf\n\t"
          "pop %0\n\t"
          : "=r" (wrd) :
      );

      printf ("AF%c\n", wrd & 0x10 ? '+' : '-');
  }
  $ cc -m32 shlaf.c
  $ ./a.out
  AF-
  $
Copy link
Owner

@wfeldt wfeldt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not on my AMD processor, afaics.

While the AMD doc is not specific enough,
"Intel 64 and IA-32 Architectures Software Developer’s Manual" says:

If the source operand is a segment register (16 bits) and the operand size
is 64-bits, a zero- extended value is pushed on the stack; if the operand
size is 32-bits, either a zero-extended value is pushed on the stack or the
segment selector is written on the stack using a 16-bit move. For the last
case, all recent Core and Atom processors perform a 16-bit move, leaving the
upper portion of the stack location unmodified

So it can be either way.

It might make sense to have it configurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants