Add mainboard x86emu interrupt function support. Add tim5690 VGA BIOS functions:...
[coreboot.git] / util / x86emu / x86.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <device/pci.h>
21 #include <string.h>
22
23 #ifdef CONFIG_COREBOOT_V2
24 #include <arch/io.h>
25 #include <arch/registers.h>
26 #include <console/console.h>
27 #define printk(x...) do_printk(x)
28 #else
29 #include <console.h>
30 #endif
31
32 #include <arch/interrupt.h>
33
34 #define REALMODE_BASE ((void *)0x600)
35
36 struct realmode_idt {
37         u16 offset, cs;
38 };
39
40 void x86_exception(struct eregs *info);
41 void run_bios(struct device *dev, unsigned long addr);
42
43 extern unsigned char __idt_handler, __idt_handler_size;
44 extern unsigned char __realmode_code, __realmode_code_size;
45 extern unsigned char __run_optionrom, __run_interrupt;
46
47 void (*run_optionrom)(u32 devfn) __attribute__((regparm(0))) = (void *)&__run_optionrom;
48 void (*vga_enable_console)(void) __attribute__((regparm(0))) = (void *)&__run_interrupt;
49
50 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
51
52 static int intXX_exception_handler(struct eregs *regs)
53 {
54         printk(BIOS_INFO, "Oops, exception %d while executing option rom\n", 
55                         regs->vector);
56         x86_exception(regs);    // Call coreboot exception handler 
57
58         return 0;               // Never returns?
59 }
60
61 static int intXX_unknown_handler(struct eregs *regs)
62 {
63         printk(BIOS_INFO, "Unsupported software interrupt #0x%x\n", 
64                         regs->vector);
65
66         return -1;
67 }
68
69 /* setup interrupt handlers for mainboard */
70 void mainboard_interrupt_handlers(int intXX, void *intXX_func)
71 {
72         intXX_handler[intXX] = intXX_func;
73 }
74
75 int int12_handler(struct eregs *regs);
76 int int15_handler(struct eregs *regs);
77 int int1a_handler(struct eregs *regs);
78
79 static void setup_interrupt_handlers(void)
80 {
81         int i;
82
83         /* The first 16 intXX functions are not BIOS services, 
84          * but the CPU-generated exceptions ("hardware interrupts")
85          */
86         for (i = 0; i < 0x10; i++)
87                 intXX_handler[i] = &intXX_exception_handler;
88         
89         /* Mark all other intXX calls as unknown first */
90         for (i = 0x10; i < 0x100; i++)
91         {
92                 /* If the mainboard_interrupt_handler isn't called first.
93                  */
94                 if(!intXX_handler[i])
95                 {
96                         /* Now set the default functions that are actually
97                          * needed to initialize the option roms. This is very
98                          * slick, as it allows us to implement mainboard specific
99                          * interrupt handlers, such as the int15
100                          */
101                         switch (i) {
102                         case 0x12:
103                                 intXX_handler[0x12] = &int12_handler;
104                                 break;
105                         case 0x15:
106                                 intXX_handler[0x15] = &int15_handler;
107                                 break;
108                         case 0x1a:
109                                 intXX_handler[0x1a] = &int1a_handler;
110                                 break;
111                         default:
112                                 intXX_handler[i] = &intXX_unknown_handler;
113                                 break;
114                         }
115                 }
116         }
117 }
118
119 static void write_idt_stub(void *target, u8 intnum)
120 {
121         unsigned char *codeptr;
122         codeptr = (unsigned char *) target;
123         memcpy(codeptr, &__idt_handler, (size_t)&__idt_handler_size);
124         codeptr[3] = intnum; /* modify int# in the code stub. */
125 }
126
127 static void setup_realmode_idt(void)
128 {
129         struct realmode_idt *idts = (struct realmode_idt *) 0;
130         int i;
131
132         /* Copy IDT stub code for each interrupt. This might seem wasteful
133          * but it is really simple
134          */
135          for (i = 0; i < 256; i++) {
136                 idts[i].cs = 0;
137                 idts[i].offset = 0x1000 + (i * (u32)&__idt_handler_size);
138                 write_idt_stub((void *)((u32 )idts[i].offset), i);
139         }
140
141         /* Many option ROMs use the hard coded interrupt entry points in the
142          * system bios. So install them at the known locations. 
143          * Only need int10 so far.
144          */
145         
146         /* int42 is the relocated int10 */
147         write_idt_stub((void *)0xff065, 0x42); 
148 }
149
150 void run_bios(struct device *dev, unsigned long addr)
151 {
152         /* clear vga bios data area */
153         memset((void *)0x400, 0, 0x200);
154
155         /* Set up C interrupt handlers */
156         setup_interrupt_handlers();
157
158         /* Setting up realmode IDT */
159         setup_realmode_idt();
160
161         memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
162         printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
163                         (u32)&__realmode_code_size);
164
165         printk(BIOS_DEBUG, "Calling Option ROM...\n");
166         run_optionrom((dev->bus->secondary << 8) | dev->path.pci.devfn);
167         printk(BIOS_DEBUG, "... Option ROM returned.\n");
168 }
169
170 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
171             u32 gsfs, u32 dses,
172             u32 edi, u32 esi,
173             u32 ebp, u32 esp,
174             u32 ebx, u32 edx,
175             u32 ecx, u32 eax,
176             u32 cs_ip, u16 stackflags);
177
178 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
179             u32 gsfs, u32 dses,
180             u32 edi, u32 esi,
181             u32 ebp, u32 esp,
182             u32 ebx, u32 edx,
183             u32 ecx, u32 eax,
184             u32 cs_ip, u16 stackflags)
185 {
186         u32 ip;
187         u32 cs;
188         u32 flags;
189         int ret = -1;
190         struct eregs reg_info;
191
192         ip = cs_ip & 0xffff;
193         cs = cs_ip >> 16;
194         flags = stackflags;
195
196         printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
197         printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
198                       eax, ebx, ecx, edx);
199         printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
200                      ebp, esp, edi, esi);
201         printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
202                      ip, cs, flags);
203
204         // Fetch arguments from the stack and put them into
205         // a structure that we want to pass on to our sub interrupt
206         // handlers.
207         reg_info = (struct eregs) {
208                 .eax=eax,
209                 .ecx=ecx,
210                 .edx=edx,
211                 .ebx=ebx,
212                 .esp=esp,
213                 .ebp=ebp,
214                 .esi=esi,
215                 .edi=edi,
216                 .vector=intnumber,
217                 .error_code=0, // ??
218                 .eip=ip,
219                 .cs=cs,
220                 .eflags=flags // ??
221         };
222
223         // Call the interrupt handler for this int#
224         ret = intXX_handler[intnumber](&reg_info);
225
226         // Put registers back on the stack. The assembler code
227         // will later pop them.
228         // What happens here is that we force (volatile!) changing
229         // the values of the parameters of this function. We do this
230         // because we know that they stay alive on the stack after 
231         // we leave this function. Don't say this is bollocks.
232         *(volatile u32 *)&eax = reg_info.eax;
233         *(volatile u32 *)&ecx = reg_info.ecx;
234         *(volatile u32 *)&edx = reg_info.edx;
235         *(volatile u32 *)&ebx = reg_info.ebx;
236         *(volatile u32 *)&esi = reg_info.esi;
237         *(volatile u32 *)&edi = reg_info.edi;
238         flags = reg_info.eflags;
239
240         /* Pass errors back to our caller via the CARRY flag */
241         if (ret) {
242                 printk(BIOS_DEBUG,"error!\n");
243                 flags |= 1;  // error: set carry
244         }else{
245                 flags &= ~1; // no error: clear carry
246         }
247         *(volatile u16 *)&stackflags = flags;
248
249         return ret;
250 }
251