9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Nick Owens <mischief@9.offblast.org>
To: 9front@9front.org
Subject: realemu: implementing IDIV
Date: Fri, 31 Jan 2014 01:45:09 -0800	[thread overview]
Message-ID: <20140131094509.GB27169@iota.offblast.org> (raw)

currently aux/realemu does not have the IDIV instruction, which is used
by newer versions of vga bioses included with qemu (1.7 here on debian
8). please review this patch which implements IDIV (signed integer
division). it should account correctly for sign extension and overflow.
see p858-860 of the intel manual.

this patch also includes a fix for writing to 0xEXXXX, which is written
to by the newer vga bios. writes to this range were previously trapped
and considered errors.

also included is a fix for overflow in the DIV instruction. on overflow,
a division by zero trap should occur. 
"Overflow is indicated with the #DE (divide error) exception rather than
with the CF flag."
see p706 of the intel manual.

http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf

diff -r 20c2c6fccfee sys/src/cmd/aux/realemu/main.c
--- a/sys/src/cmd/aux/realemu/main.c  Sat Jan 11 16:07:35 2014 +0100
+++ b/sys/src/cmd/aux/realemu/main.c  Thu Jan 30 01:29:46 2014 -0800
@@ -286,7 +286,7 @@
  /* B */ nil,  rrealmem, wrealmem, /* RAM: VGA framebuffer */
  /* C */ memory, rmem, wmem, /* ROM: VGA BIOS */
  /* D */ nil,  rbad, wbad,
- /* E */ memory, rmem, wbad, /* ROM: BIOS */
+ /* E */ memory, rmem, wmem, /* ROM: BIOS */
  /* F */ memory, rmem, wbad, /* ROM: BIOS */
 };
 
diff -r 20c2c6fccfee sys/src/cmd/aux/realemu/xec.c
--- a/sys/src/cmd/aux/realemu/xec.c Sat Jan 11 16:07:35 2014 +0100
+++ b/sys/src/cmd/aux/realemu/xec.c Thu Jan 30 01:29:46 2014 -0800
@@ -769,12 +769,59 @@
  n = (uvlong)ar(ra)<<s | (uvlong)ar(qa);
  q = n/d;
  if(q > m)
-   trap(cpu, EGPF);
+   trap(cpu, EDIV0);
  r = n%d;
  aw(ra, r);
  aw(qa, q);
 }
- 
+
+static void
+opidiv(Cpu *cpu, Inst *i)
+{
+ Iarg *qa, *ra;
+ vlong n, q, min, max;
+ long m, r, d;
+ int s;
+
+ s = i->a1->len*8;
+ m = mask(s);
+ d = ars(i->a1);
+ if(d == 0)
+   trap(cpu, EDIV0);
+ if(s == 8){
+   qa = areg(cpu, 1, RAX);
+   ra = adup(qa);
+   ra->tag |= TH;
+ } else {
+   qa = areg(cpu, i->olen, RAX);
+   ra = areg(cpu, i->olen, RDX);
+ }
+ n = (vlong)ars(ra)<<s | (uvlong)ars(qa);
+ q = n/d;
+ r = n%d;
+
+ /* check for overflow based on operand size */
+ switch(s) {
+ case 8:
+   min = (char)0x80;
+   max = 0x7F;
+ case 16:
+   min = (short)0x8000;
+   max = 0x7FFF;
+ case 32:
+   min = (long)0x80000000;
+   max = 0x7FFFFFFF;
+ }
+
+ fprint(2, "s: %d m: %#lux d: %ld n: %lld q: %lld r: %ld min: %lld max: %lld\n",
+      s,  m,   d,   n,  q,  r, min,    max);
+
+ if(q > max || q < min)
+   trap(cpu, EDIV0);
+
+ aw(ra, r);
+ aw(qa, q);
+}  
 
 static int
 cctrue(Cpu *cpu, Inst *i)
@@ -1249,6 +1296,7 @@
  [OMUL] = opmul,
  [OIMUL] = opimul,
  [ODIV] = opdiv,
+ [OIDIV] = opidiv,
 
  [OLEA] = oplea,
  [OMOV] = opmov,



             reply	other threads:[~2014-01-31  9:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-31  9:45 Nick Owens [this message]
2014-02-01  9:47 ` [9front] " cinap_lenrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140131094509.GB27169@iota.offblast.org \
    --to=mischief@9.offblast.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).