57c5338914b7748ebd214f7935349f8097897642
[coreboot.git] / src / devices / oprom / x86_asm.S
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009-2010 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 #define REALMODE_BASE           0x600
21 #define RELOCATED(x)    (x - __realmode_code + REALMODE_BASE)
22
23 /* CR0 bits */
24 #define PE              (1 << 0)
25
26 /* This is the intXX interrupt handler stub code. It gets copied
27  * to the IDT and to some fixed addresses in the F segment. Before
28  * the code can used, it gets patched up by the C function copying
29  * it: byte 3 (the $0 in movb $0, %al) is overwritten with the int#.
30  */
31
32         .code16
33         .globl __idt_handler
34 __idt_handler:
35         pushal
36         movb    $0, %al /* This instruction gets modified */
37         ljmp    $0, $__interrupt_handler_16bit
38         .globl __idt_handler_size
39 __idt_handler_size = ( . - __idt_handler)
40
41
42 /* In order to be independent of coreboot's position in RAM
43  * we relocate a part of the code to the low megabyte, so the
44  * CPU can use it in real-mode. This code lives at __realmode_code.
45  */
46         .globl __realmode_code
47 __realmode_code:
48
49 /* Realmode IDT pointer structure. */
50         .globl __realmode_idt
51 __realmode_idt = RELOCATED(.)
52         .word 1023      /* 16 bit limit */
53         .long 0         /* 24 bit base */
54         .word 0
55
56 /* Preserve old stack */
57 __stack = RELOCATED(.)
58         .long 0
59
60 /* Register store for realmode_call and realmode_interrupt */
61 __registers = RELOCATED(.)
62         .long 0 /*  0 - EAX */
63         .long 0 /*  4 - EBX */
64         .long 0 /*  8 - ECX */
65         .long 0 /* 12 - EDX */
66         .long 0 /* 16 - ESI */
67         .long 0 /* 20 - EDI */
68
69 /* 256 byte buffer, used by int10 */
70         .globl __buffer
71 __buffer = RELOCATED(.)
72         .skip 256
73
74         .code32
75         .globl __realmode_call
76 __realmode_call = RELOCATED(.)
77         /* save all registers to the stack */
78         pushal
79
80         /* Move the protected mode stack to a safe place */
81         movl    %esp, __stack
82         movl    %esp, %ebp
83
84         /* This function is called with regparm=0 and we have
85          * to skip the 32 byte from pushal. Hence start at 36.
86          */
87
88         /* entry point */
89         movl    36(%ebp), %eax
90         mov     %ax, __lcall_instr + 1
91         andl    $0xffff0000, %eax
92         shrl    $4, %eax
93         mov     %ax, __lcall_instr + 3
94
95         /* initial register values */
96         movl    40(%ebp), %eax
97         movl    %eax, __registers +  0 /* eax */
98         movl    44(%ebp), %eax
99         movl    %eax, __registers +  4 /* ebx */
100         movl    48(%ebp), %eax
101         movl    %eax, __registers +  8 /* ecx */
102         movl    52(%ebp), %eax
103         movl    %eax, __registers + 12 /* edx */
104         movl    56(%ebp), %eax
105         movl    %eax, __registers + 16 /* esi */
106         movl    60(%ebp), %eax
107         movl    %eax, __registers + 20 /* edi */
108
109         /* Activate the right segment descriptor real mode. */
110         ljmp    $0x28, $RELOCATED(1f)
111 1:
112 .code16
113         /* 16 bit code from here on... */
114
115         /* Load the segment registers w/ properly configured
116          * segment descriptors. They will retain these
117          * configurations (limits, writability, etc.) once
118          * protected mode is turned off.
119          */
120         mov     $0x30, %ax
121         mov     %ax, %ds
122         mov     %ax, %es
123         mov     %ax, %fs
124         mov     %ax, %gs
125         mov     %ax, %ss
126
127         /* Turn off protection */
128         movl    %cr0, %eax
129         andl    $~PE, %eax
130         movl    %eax, %cr0
131
132         /* Now really going into real mode */
133         ljmp    $0, $RELOCATED(1f)
134 1:
135         /* Setup a stack: Put the stack at the end of page zero.
136          * That way we can easily share it between real and
137          * protected, since the 16 bit ESP at segment 0 will
138          * work for any case. */
139         mov     $0x0, %ax
140         mov     %ax, %ss
141         movl    $0x1000, %eax
142         movl    %eax, %esp
143
144         /* Load 16 bit IDT */
145         xor     %ax, %ax
146         mov     %ax, %ds
147         lidt    __realmode_idt
148
149         /* initialize registers for option rom lcall */
150         movl    __registers +  0, %eax
151         movl    __registers +  4, %ebx
152         movl    __registers +  8, %ecx
153         movl    __registers + 12, %edx
154         movl    __registers + 16, %esi
155         movl    __registers + 20, %edi
156
157         /* Set all segments to 0x0000, ds to 0x0040 */
158         push    %ax
159         xor     %ax, %ax
160         mov     %ax, %es
161         mov     %ax, %fs
162         mov     %ax, %gs
163         mov     $0x40, %ax
164         mov     %ax, %ds
165         pop     %ax
166
167         /* ************************************ */
168 __lcall_instr = RELOCATED(.)
169         .byte 0x9a
170         .word 0x0000, 0x0000
171         /* ************************************ */
172
173         /* If we got here, we are just about done.
174          * Need to get back to protected mode.
175          */
176         movl    %cr0, %eax
177         orl     $PE, %eax
178         movl    %eax, %cr0
179
180         /* Now that we are in protected mode
181          * jump to a 32 bit code segment.
182          */
183         data32  ljmp    $0x10, $RELOCATED(1f)
184 1:
185         .code32
186         mov     $0x18, %ax
187         mov     %ax, %ds
188         mov     %ax, %es
189         mov     %ax, %fs
190         mov     %ax, %gs
191         mov     %ax, %ss
192
193         /* restore proper idt */
194         lidt    idtarg
195
196         /* and exit */
197         movl    __stack, %esp
198         popal
199
200         // TODO return AX from OPROM call
201         ret
202
203         .globl __realmode_interrupt
204 __realmode_interrupt = RELOCATED(.)
205         /* save all registers to the stack */
206         pushal
207         /* save the stack */
208         movl    %esp, __stack
209         movl    %esp, %ebp
210
211         /* This function is called with regparm=0 and we have
212          * to skip the 32 byte from pushal. Hence start at 36.
213          */
214
215         /* prepare interrupt calling code */
216         movl    36(%ebp), %eax
217         movb    %al, __intXX_instr + 1 /* intno */
218
219         /* initial register values */
220         movl    40(%ebp), %eax
221         movl    %eax, __registers +  0 /* eax */
222         movl    44(%ebp), %eax
223         movl    %eax, __registers +  4 /* ebx */
224         movl    48(%ebp), %eax
225         movl    %eax, __registers +  8 /* ecx */
226         movl    52(%ebp), %eax
227         movl    %eax, __registers + 12 /* edx */
228         movl    56(%ebp), %eax
229         movl    %eax, __registers + 16 /* esi */
230         movl    60(%ebp), %eax
231         movl    %eax, __registers + 20 /* edi */
232
233         /*  This configures CS properly for real mode. */
234         ljmp    $0x28, $RELOCATED(1f)
235 1:
236         .code16 /* 16 bit code from here on... */
237
238         /* Load the segment registers w/ properly configured segment
239          * descriptors.  They will retain these configurations (limits,
240          * writability, etc.) once protected mode is turned off.
241          */
242         mov     $0x30, %ax
243         mov     %ax, %ds
244         mov     %ax, %es
245         mov     %ax, %fs
246         mov     %ax, %gs
247         mov     %ax, %ss
248
249         /* Turn off protected mode */
250         movl    %cr0, %eax
251         andl    $~PE, %eax
252         movl    %eax, %cr0
253
254         /* Now really going into real mode */
255         data32 ljmp     $0, $RELOCATED(1f)
256 1:
257
258         /* put the stack at the end of page zero.  That way we can easily 
259          * share it between real mode and protected mode, because %esp and
260          * %ss:%sp point to the same memory.
261          */
262         /* setup a stack */
263         mov     $0x0, %ax
264         mov     %ax, %ss
265         movl    $0x1000, %eax
266         movl    %eax, %esp
267
268         /* Load 16-bit intXX IDT */
269         xor     %ax, %ax
270         mov     %ax, %ds
271         lidt    __realmode_idt
272
273         /* initialize registers for intXX call */
274         movl    __registers +  0, %eax
275         movl    __registers +  4, %ebx
276         movl    __registers +  8, %ecx
277         movl    __registers + 12, %edx
278         movl    __registers + 16, %esi
279         movl    __registers + 20, %edi
280
281         /* Set all segments to 0x0000 */
282         push    %ax
283         xor     %ax, %ax
284         mov     %ax, %ds
285         mov     %ax, %es
286         mov     %ax, %fs
287         mov     %ax, %gs
288         pop     %ax
289
290 __intXX_instr = RELOCATED(.)
291         .byte 0xcd, 0x00 /* This becomes intXX */
292
293         /* Ok, the job is done, now go back to protected mode coreboot */
294         movl    %cr0, %eax
295         orl     $PE, %eax
296         movl    %eax, %cr0
297
298         /* Now that we are in protected mode jump to a 32-bit code segment. */
299         data32  ljmp    $0x10, $RELOCATED(1f)
300 1:
301         .code32
302         mov     $0x18, %ax
303         mov     %ax, %ds
304         mov     %ax, %es
305         mov     %ax, %fs
306         mov     %ax, %gs
307         mov     %ax, %ss
308
309         /* restore coreboot's 32-bit IDT */
310         lidt    idtarg
311
312         /* Exit */
313         movl    __stack, %esp
314         popal
315         ret
316
317 /* This is the 16-bit interrupt entry point called by the IDT stub code.
318  *
319  * Before this code code is called, %eax is pushed to the stack, and the
320  * interrupt number is loaded into %al. On return this function cleans up
321  * for its caller.
322  */
323         .code16
324 __interrupt_handler_16bit = RELOCATED(.)
325         push    %ds
326         push    %es
327         push    %fs
328         push    %gs
329
330         /* Clean up the interrupt number. We could have done this in the stub,
331          * but it would have cost 2 more bytes per stub entry.
332          */
333         andl    $0xff, %eax
334         pushl   %eax            /* ... and make it the first parameter */
335
336         /* Switch to protected mode */
337         movl    %cr0, %eax
338         orl     $PE, %eax
339         movl    %eax, %cr0
340
341         /* ... and jump to a 32 bit code segment. */
342         data32 ljmp    $0x10, $RELOCATED(1f)
343 1:
344         .code32
345         mov     $0x18, %ax
346         mov     %ax, %ds
347         mov     %ax, %es
348         mov     %ax, %fs
349         mov     %ax, %gs
350         mov     %ax, %ss
351
352         lidt    idtarg
353
354         /* Call the C interrupt handler */
355         movl    $interrupt_handler, %eax
356         call    *%eax
357
358         /* Now return to real mode ... */
359         ljmp    $0x28, $RELOCATED(1f)
360 1:
361         .code16
362         /* Load the segment registers with properly configured segment
363          * descriptors.  They will retain these configurations (limits,
364          * writability, etc.) once protected mode is turned off.
365          */
366         mov     $0x30, %ax
367         mov     %ax, %ds
368         mov     %ax, %es
369         mov     %ax, %fs
370         mov     %ax, %gs
371         mov     %ax, %ss
372
373         /* Disable Protected Mode */
374         movl    %cr0, %eax
375         andl    $~PE, %eax
376         movl    %eax, %cr0
377
378         /* Now really going into real mode */
379         ljmp $0,  $RELOCATED(1f)
380 1:
381         /* Restore real-mode stack segment */
382         mov     $0x0, %ax
383         mov     %ax, %ss
384
385         /* Restore 16 bit IDT */
386         xor     %ax, %ax
387         mov     %ax, %ds
388         lidt    __realmode_idt
389
390         /* Restore all registers, including those
391          * manipulated by the C handler
392          */
393         popl    %eax
394         pop     %gs
395         pop     %fs
396         pop     %es
397         pop     %ds
398         popal
399         iret
400
401         .globl __realmode_code_size
402 __realmode_code_size = (. - __realmode_code)
403
404         .code32