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