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