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