x86 realmode: Use x86emu register file + defines

By using the (global) register file as defined by x86emu,
we can use the same register access for YABEL and realmode
interrupt handlers.

- the x86 realmode interrupt handlers changed in signature
- to access registers, use X86_$REGNAME now (eg. X86_EAX)
- x86_exception_handler still uses struct eregs *regs to
  avoid spilling the x86emu register file stuff everywhere

Coccinelle script that handled most of this commit:
  @ inthandler @
  identifier FUNC, regs;
  @@
  int FUNC(
  -struct eregs *regs
  +void
   )
  { ... }

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->eax
  +X86_EAX

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->ebx
  +X86_EBX

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->ecx
  +X86_ECX

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->edx
  +X86_EDX

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->esi
  +X86_ESI

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->edi
  +X86_EDI

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->eflags
  +X86_EFLAGS

  @ depends on inthandler @
  identifier regs;
  @@
  -regs->vector
  +M.x86.intno

Change-Id: I60cc2c36646fe4b7f97457b1e297e3df086daa36
Signed-off-by: Patrick Georgi <patrick.georgi@secunet.com>
Reviewed-on: http://review.coreboot.org/1891
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/src/devices/oprom/realmode/x86.c b/src/devices/oprom/realmode/x86.c
index 2e01617..6a82a69 100644
--- a/src/devices/oprom/realmode/x86.c
+++ b/src/devices/oprom/realmode/x86.c
@@ -31,6 +31,11 @@
 #include "x86.h"
 #include "vbe.h"
 #include <lib/jpeg.h>
+/* we use x86emu's register file representation */
+#include <x86emu/regs.h>
+
+/* to have a common register file for interrupt handlers */
+X86EMU_sysEnv _X86EMU_env;
 
 void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
 		u32 esi, u32 edi) __attribute__((regparm(0))) =
@@ -52,10 +57,28 @@
 	write8(0xffffe, 0xfc);
 }
 
-int (*intXX_handler[256])(struct eregs *regs) = { NULL };
+static int (*intXX_handler[256])(void) = { NULL };
 
-static int intXX_exception_handler(struct eregs *regs)
+static int intXX_exception_handler(void)
 {
+	/* compatibility shim */
+	struct eregs reg_info = {
+		.eax=X86_EAX,
+		.ecx=X86_ECX,
+		.edx=X86_EDX,
+		.ebx=X86_EBX,
+		.esp=X86_ESP,
+		.ebp=X86_EBP,
+		.esi=X86_ESI,
+		.edi=X86_EDI,
+		.vector=M.x86.intno,
+		.error_code=0, // FIXME: fill in
+		.eip=X86_EIP,
+		.cs=X86_CS,
+		.eflags=X86_EFLAGS
+	};
+	struct eregs *regs = &reg_info;
+
 	printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
 			regs->vector);
 	x86_exception(regs);	// Call coreboot exception handler
@@ -63,10 +86,10 @@
 	return 0;		// Never really returns
 }
 
-static int intXX_unknown_handler(struct eregs *regs)
+static int intXX_unknown_handler(void)
 {
 	printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
-			regs->vector, regs->eax);
+			M.x86.intno, X86_EAX);
 
 	return -1;
 }
@@ -403,7 +426,6 @@
 	u32 cs;
 	u32 flags;
 	int ret = 0;
-	struct eregs reg_info;
 
 	ip = cs_ip & 0xffff;
 	cs = cs_ip >> 16;
@@ -419,27 +441,24 @@
 		     ip, cs, flags);
 #endif
 
-	// Fetch arguments from the stack and put them into
-	// a structure that we want to pass on to our sub interrupt
-	// handlers.
-	reg_info = (struct eregs) {
-		.eax=eax,
-		.ecx=ecx,
-		.edx=edx,
-		.ebx=ebx,
-		.esp=esp,
-		.ebp=ebp,
-		.esi=esi,
-		.edi=edi,
-		.vector=intnumber,
-		.error_code=0, // ??
-		.eip=ip,
-		.cs=cs,
-		.eflags=flags // ??
-	};
+	// Fetch arguments from the stack and put them to a place
+	// suitable for the interrupt handlers
+	X86_EAX = eax;
+	X86_ECX = ecx;
+	X86_EDX = edx;
+	X86_EBX = ebx;
+	X86_ESP = esp;
+	X86_EBP = ebp;
+	X86_ESI = esi;
+	X86_EDI = edi;
+	M.x86.intno = intnumber;
+	/* TODO: error_code must be stored somewhere */
+	X86_EIP = ip;
+	X86_CS = cs;
+	X86_EFLAGS = flags;
 
 	// Call the interrupt handler for this int#
-	ret = intXX_handler[intnumber](&reg_info);
+	ret = intXX_handler[intnumber]();
 
 	// Put registers back on the stack. The assembler code
 	// will later pop them.
@@ -447,13 +466,13 @@
 	// the values of the parameters of this function. We do this
 	// because we know that they stay alive on the stack after
 	// we leave this function. Don't say this is bollocks.
-	*(volatile u32 *)&eax = reg_info.eax;
-	*(volatile u32 *)&ecx = reg_info.ecx;
-	*(volatile u32 *)&edx = reg_info.edx;
-	*(volatile u32 *)&ebx = reg_info.ebx;
-	*(volatile u32 *)&esi = reg_info.esi;
-	*(volatile u32 *)&edi = reg_info.edi;
-	flags = reg_info.eflags;
+	*(volatile u32 *)&eax = X86_EAX;
+	*(volatile u32 *)&ecx = X86_ECX;
+	*(volatile u32 *)&edx = X86_EDX;
+	*(volatile u32 *)&ebx = X86_EBX;
+	*(volatile u32 *)&esi = X86_ESI;
+	*(volatile u32 *)&edi = X86_EDI;
+	flags = X86_EFLAGS;
 
 	/* Pass success or error back to our caller via the CARRY flag */
 	if (ret) {
diff --git a/src/devices/oprom/realmode/x86.h b/src/devices/oprom/realmode/x86.h
index 40c5778..7dfa60f 100644
--- a/src/devices/oprom/realmode/x86.h
+++ b/src/devices/oprom/realmode/x86.h
@@ -42,8 +42,8 @@
 #define INITIAL_EBDA_SEGMENT 0xF600
 #define INITIAL_EBDA_SIZE 0x400
 
-int int10_handler(struct eregs *regs);
-int int12_handler(struct eregs *regs);
-int int16_handler(struct eregs *regs);
-int int1a_handler(struct eregs *regs);
+int int10_handler(void);
+int int12_handler(void);
+int int16_handler(void);
+int int1a_handler(void);
 
diff --git a/src/devices/oprom/realmode/x86_interrupts.c b/src/devices/oprom/realmode/x86_interrupts.c
index b4ff135..b3764f9 100644
--- a/src/devices/oprom/realmode/x86_interrupts.c
+++ b/src/devices/oprom/realmode/x86_interrupts.c
@@ -27,6 +27,8 @@
 #include <arch/io.h>
 #include <arch/registers.h>
 #include "x86.h"
+/* we use x86emu's register file representation */
+#include <x86emu/regs.h>
 
 // errors go in AH. Just set these up so that word assigns
 // will work. KISS.
@@ -38,27 +40,27 @@
 	PCIBIOS_BADREG = 0x8700
 };
 
-int int10_handler(struct eregs *regs)
+int int10_handler(void)
 {
 	int res=0;
 	static u8 cursor_row=0, cursor_col=0;
-	switch((regs->eax & 0xff00)>>8) {
+	switch((X86_EAX & 0xff00)>>8) {
 	case 0x01: // Set cursor shape
 		res = 1;
 		break;
 	case 0x02: // Set cursor position
-		if (cursor_row != ((regs->edx >> 8) & 0xff) ||
-		    cursor_col >= (regs->edx & 0xff)) {
+		if (cursor_row != ((X86_EDX >> 8) & 0xff) ||
+		    cursor_col >= (X86_EDX & 0xff)) {
 			printk(BIOS_INFO, "\n");
 		}
-		cursor_row = (regs->edx >> 8) & 0xff;
-		cursor_col = regs->edx & 0xff;
+		cursor_row = (X86_EDX >> 8) & 0xff;
+		cursor_col = X86_EDX & 0xff;
 		res = 1;
 		break;
 	case 0x03: // Get cursor position
-		regs->eax &= 0x00ff;
-		regs->ecx = 0x0607;
-		regs->edx = (cursor_row << 8) | cursor_col;
+		X86_EAX &= 0x00ff;
+		X86_ECX = 0x0607;
+		X86_EDX = (cursor_row << 8) | cursor_col;
 		res = 1;
 		break;
 	case 0x06: // Scroll up
@@ -66,48 +68,48 @@
 		res = 1;
 		break;
 	case 0x08: // Get Character and Mode at Cursor Position
-		regs->eax = 0x0f00 | 'A'; // White on black 'A'
+		X86_EAX = 0x0f00 | 'A'; // White on black 'A'
 		res = 1;
 		break;
 	case 0x09: // Write Character and attribute
 	case 0x0e: // Write Character
-		printk(BIOS_INFO, "%c", regs->eax & 0xff);
+		printk(BIOS_INFO, "%c", X86_EAX & 0xff);
 		res = 1;
 		break;
 	case 0x0f: // Get video mode
-		regs->eax = 0x5002; //80x25
-		regs->ebx &= 0x00ff;
+		X86_EAX = 0x5002; //80x25
+		X86_EBX &= 0x00ff;
 		res = 1;
 		break;
         default:
 		printk(BIOS_WARNING, "Unknown INT10 function %04x!\n",
-				regs->eax & 0xffff);
+				X86_EAX & 0xffff);
 		break;
 	}
 	return res;
 }
 
-int int12_handler(struct eregs *regs)
+int int12_handler(void)
 {
-	regs->eax = 64 * 1024;
+	X86_EAX = 64 * 1024;
 	return 1;
 }
 
-int int16_handler(struct eregs *regs)
+int int16_handler(void)
 {
 	int res=0;
-	switch((regs->eax & 0xff00)>>8) {
+	switch((X86_EAX & 0xff00)>>8) {
 	case 0x00: // Check for Keystroke
-		regs->eax = 0x6120; // Space Bar, Space
+		X86_EAX = 0x6120; // Space Bar, Space
 		res = 1;
 		break;
 	case 0x01: // Check for Keystroke
-		regs->eflags |= 1<<6; // Zero Flag set (no key available)
+		X86_EFLAGS |= 1<<6; // Zero Flag set (no key available)
 		res = 1;
 		break;
         default:
 		printk(BIOS_WARNING, "Unknown INT16 function %04x!\n",
-				regs->eax & 0xffff);
+				X86_EAX & 0xffff);
 		break;
 	}
 	return res;
@@ -116,9 +118,9 @@
 #define PCI_CONFIG_SPACE_TYPE1	(1 << 0)
 #define PCI_SPECIAL_CYCLE_TYPE1	(1 << 4)
 
-int int1a_handler(struct eregs *regs)
+int int1a_handler(void)
 {
-	unsigned short func = (unsigned short)regs->eax;
+	unsigned short func = (unsigned short)X86_EAX;
 	int retval = 1;
 	unsigned short devid, vendorid, devfn;
 	/* Use short to get rid of gabage in upper half of 32-bit register */
@@ -131,19 +133,19 @@
 
 	switch (func) {
 	case 0xb101: /* PCIBIOS Check */
-		regs->edx = 0x20494350;	/* ' ICP' */
-		regs->eax &= 0xffff0000; /* Clear AH / AL */
-		regs->eax |= PCI_CONFIG_SPACE_TYPE1 | PCI_SPECIAL_CYCLE_TYPE1;
+		X86_EDX = 0x20494350;	/* ' ICP' */
+		X86_EAX &= 0xffff0000; /* Clear AH / AL */
+		X86_EAX |= PCI_CONFIG_SPACE_TYPE1 | PCI_SPECIAL_CYCLE_TYPE1;
 		// last bus in the system. Hard code to 255 for now.
 		// dev_enumerate() does not seem to tell us (publically)
-		regs->ecx = 0xff;
-		regs->edi = 0x00000000;	/* protected mode entry */
+		X86_ECX = 0xff;
+		X86_EDI = 0x00000000;	/* protected mode entry */
 		retval = 1;
 		break;
 	case 0xb102: /* Find Device */
-		devid = regs->ecx;
-		vendorid = regs->edx;
-		devindex = regs->esi;
+		devid = X86_ECX;
+		vendorid = X86_EDX;
+		devindex = X86_ESI;
 		dev = 0;
 		while ((dev = dev_find_device(vendorid, devid, dev))) {
 			if (devindex <= 0)
@@ -152,18 +154,18 @@
 		}
 		if (dev) {
 			unsigned short busdevfn;
-			regs->eax &= 0xffff00ff; /* Clear AH */
-			regs->eax |= PCIBIOS_SUCCESSFUL;
+			X86_EAX &= 0xffff00ff; /* Clear AH */
+			X86_EAX |= PCIBIOS_SUCCESSFUL;
 			// busnum is an unsigned char;
 			// devfn is an int, so we mask it off.
 			busdevfn = (dev->bus->secondary << 8)
 			    | (dev->path.pci.devfn & 0xff);
 			printk(BIOS_DEBUG, "0x%x: return 0x%x\n", func, busdevfn);
-			regs->ebx = busdevfn;
+			X86_EBX = busdevfn;
 			retval = 1;
 		} else {
-			regs->eax &= 0xffff00ff; /* Clear AH */
-			regs->eax |= PCIBIOS_NODEV;
+			X86_EAX &= 0xffff00ff; /* Clear AH */
+			X86_EAX |= PCIBIOS_NODEV;
 			retval = 0;
 		}
 		break;
@@ -173,57 +175,57 @@
 	case 0xb10d: /* Write Config Dword */
 	case 0xb10c: /* Write Config Word */
 	case 0xb10b: /* Write Config Byte */
-		devfn = regs->ebx & 0xff;
-		bus = regs->ebx >> 8;
-		reg = regs->edi;
+		devfn = X86_EBX & 0xff;
+		bus = X86_EBX >> 8;
+		reg = X86_EDI;
 		dev = dev_find_slot(bus, devfn);
 		if (!dev) {
 			printk(BIOS_DEBUG, "0x%x: BAD DEVICE bus %d devfn 0x%x\n", func, bus, devfn);
 			// Or are we supposed to return PCIBIOS_NODEV?
-			regs->eax &= 0xffff00ff; /* Clear AH */
-			regs->eax |= PCIBIOS_BADREG;
+			X86_EAX &= 0xffff00ff; /* Clear AH */
+			X86_EAX |= PCIBIOS_BADREG;
 			retval = 0;
 			return retval;
 		}
 		switch (func) {
 		case 0xb108: /* Read Config Byte */
 			byte = pci_read_config8(dev, reg);
-			regs->ecx = byte;
+			X86_ECX = byte;
 			break;
 		case 0xb109: /* Read Config Word */
 			word = pci_read_config16(dev, reg);
-			regs->ecx = word;
+			X86_ECX = word;
 			break;
 		case 0xb10a: /* Read Config Dword */
 			dword = pci_read_config32(dev, reg);
-			regs->ecx = dword;
+			X86_ECX = dword;
 			break;
 		case 0xb10b: /* Write Config Byte */
-			byte = regs->ecx;
+			byte = X86_ECX;
 			pci_write_config8(dev, reg, byte);
 			break;
 		case 0xb10c: /* Write Config Word */
-			word = regs->ecx;
+			word = X86_ECX;
 			pci_write_config16(dev, reg, word);
 			break;
 		case 0xb10d: /* Write Config Dword */
-			dword = regs->ecx;
+			dword = X86_ECX;
 			pci_write_config32(dev, reg, dword);
 			break;
 		}
 
 #if CONFIG_REALMODE_DEBUG
 		printk(BIOS_DEBUG, "0x%x: bus %d devfn 0x%x reg 0x%x val 0x%x\n",
-			     func, bus, devfn, reg, regs->ecx);
+			     func, bus, devfn, reg, X86_ECX);
 #endif
-		regs->eax &= 0xffff00ff; /* Clear AH */
-		regs->eax |= PCIBIOS_SUCCESSFUL;
+		X86_EAX &= 0xffff00ff; /* Clear AH */
+		X86_EAX |= PCIBIOS_SUCCESSFUL;
 		retval = 1;
 		break;
 	default:
 		printk(BIOS_ERR, "UNSUPPORTED PCIBIOS FUNCTION 0x%x\n", func);
-		regs->eax &= 0xffff00ff; /* Clear AH */
-		regs->eax |= PCIBIOS_UNSUPPORTED;
+		X86_EAX &= 0xffff00ff; /* Clear AH */
+		X86_EAX |= PCIBIOS_UNSUPPORTED;
 		retval = 0;
 		break;
 	}