Improve smp detection code.
[seabios.git] / src / romlayout.S
1 // Rom layout and bios assembler to C interface.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "config.h"
9
10 #define PROTECTED_MODE_CS (2 << 3) // 0x10
11 #define PROTECTED_MODE_DS (3 << 3) // 0x18
12 #define REAL_MODE_CS      (4 << 3) // 0x20
13 #define REAL_MODE_DS      (5 << 3) // 0x28
14
15
16 /****************************************************************
17  * Include of 16bit C code
18  ****************************************************************/
19
20         .code16gcc
21 .include "out/blob.16.s"
22
23
24 /****************************************************************
25  * Entry macros
26  ****************************************************************/
27
28         // Call a C function - this does the minimal work necessary to
29         // call into C.  It sets up %ds, backs up %es, and backs up
30         // those registers that are call clobbered by the C compiler.
31         .macro ENTRY cfunc
32         cld
33         pushl %eax              // Save registers clobbered by C code
34         pushl %ecx
35         pushl %edx
36         pushw %es
37         pushw %ds
38         movw %ss, %ax           // Move %ss to %ds
39         movw %ax, %ds
40         pushl %esp              // Backup %esp, then clear high bits
41         movzwl %sp, %esp
42         calll \cfunc
43         popl %esp               // Restore %esp (including high bits)
44         popw %ds                // Restore registers saved above
45         popw %es
46         popl %edx
47         popl %ecx
48         popl %eax
49         .endm
50
51         // Call a C function with current register list as an
52         // argument.  This backs up the registers and sets %eax
53         // to point to the backup.  On return, the registers are
54         // restored from the structure.
55         .macro ENTRY_ARG cfunc
56         cld
57         pushl %eax              // Save registers (matches struct bregs)
58         pushl %ecx
59         pushl %edx
60         pushl %ebx
61         pushl %esi
62         pushl %edi
63         pushw %es
64         pushw %ds
65         movw %ss, %ax           // Move %ss to %ds
66         movw %ax, %ds
67         movl %esp, %ebx         // Backup %esp, then zero high bits
68         movzwl %sp, %esp
69         movl %esp, %eax         // First arg is pointer to struct bregs
70         calll \cfunc
71         movl %ebx, %esp         // Restore %esp (including high bits)
72         popw %ds                // Restore registers (from struct bregs)
73         popw %es
74         popl %edi
75         popl %esi
76         popl %ebx
77         popl %edx
78         popl %ecx
79         popl %eax
80         .endm
81
82         // As above, but don't mangle %esp
83         .macro ENTRY_ARG_ESP cfunc
84         cld
85         pushl %eax              // Save registers (matches struct bregs)
86         pushl %ecx
87         pushl %edx
88         pushl %ebx
89         pushl %esi
90         pushl %edi
91         pushw %es
92         pushw %ds
93         movw %ss, %ax           // Move %ss to %ds
94         movw %ax, %ds
95         movl %esp, %eax         // First arg is pointer to struct bregs
96         calll \cfunc
97         popw %ds                // Restore registers (from struct bregs)
98         popw %es
99         popl %edi
100         popl %esi
101         popl %ebx
102         popl %edx
103         popl %ecx
104         popl %eax
105         .endm
106
107         // Macro to reset the 16bit stack
108         // Clobbers %ax
109         .macro RESET_STACK
110         xorw %ax, %ax
111         movw %ax, %ss
112         movl $ BUILD_STACK_ADDR , %esp
113         cld
114         .endm
115
116         // Specify a location in the fixed part of bios area.
117         .macro ORG addr
118         .section .text.fixed.addr
119         .org \addr - BUILD_START_FIXED
120         .endm
121
122
123 /****************************************************************
124  * POST handler
125  ****************************************************************/
126
127         ORG 0xe05b
128 post16:
129         // init the stack pointer
130         RESET_STACK
131
132         pushl $_code32__start
133
134         // Fall through to transition32 function below
135
136
137 /****************************************************************
138  * Call trampolines
139  ****************************************************************/
140
141 // Place CPU into 32bit mode from 16bit mode.
142 // Clobbers: %eax, flags, stack registers, cr0, idt/gdt
143 transition32:
144         // Disable irqs
145         cli
146
147         // enable a20
148         inb $0x92, %al
149         orb $0x02, %al
150         outb %al, $0x92
151
152         // Set segment descriptors
153         lidt %cs:pmode_IDT_info
154         lgdt %cs:rombios32_gdt_48
155
156         // set PE bit in CR0
157         movl  %cr0, %eax
158         orb   $0x01, %al
159         movl  %eax, %cr0
160
161         // start protected mode code
162         ljmpl $PROTECTED_MODE_CS, $(BUILD_BIOS_ADDR + 1f)
163
164         .code32
165 1:
166         // init data segments
167         movl $PROTECTED_MODE_DS, %eax
168         movw %ax, %ds
169         movw %ax, %es
170         movw %ax, %ss
171         xorl %eax, %eax
172         movw %ax, %fs
173         movw %ax, %gs
174
175         retl
176
177 // Call a 16bit function from 32bit mode.
178 // %eax = address of struct bregs
179 // Clobbers: all gp registers, flags, stack registers, cr0, idt/gdt
180         .global __call16_from32
181 __call16_from32:
182         pushl %eax
183
184         // Jump to 16bit mode
185         ljmpw $REAL_MODE_CS, $1f
186
187         .code16gcc
188 1:
189         // restore data segment limits to 0xffff
190         movw $REAL_MODE_DS, %ax
191         movw %ax, %ds
192         movw %ax, %es
193         movw %ax, %ss
194         movw %ax, %fs
195         movw %ax, %gs
196
197         // reset PE bit in CR0
198         movl %cr0, %eax
199         andb $0xfe, %al
200         movl %eax, %cr0
201
202         // far jump to flush CPU queue after transition to real mode
203         ljmpw $SEG_BIOS, $2f
204
205 2:
206         // restore IDT to normal real-mode defaults
207         lidt %cs:rmode_IDT_info
208
209         // Clear segment registers
210         xorw %ax, %ax
211         movw %ax, %fs
212         movw %ax, %gs
213         movw %ax, %es
214         movw %ax, %ds
215         movw %ax, %ss  // Assume stack is in segment 0
216
217         popl %eax
218
219         // Set __call16 return address to be transition32
220         pushl $transition32
221
222         // Fall through to __call16
223
224
225 // Call a 16bit function from 16bit mode with a specified cpu register state
226 // %eax = address of struct bregs
227 // Clobbers: all gp registers, es
228         .global __call16
229 __call16:
230         // Save eax
231         pushl %eax
232
233         // Setup for iretw call
234         pushw $SEG_BIOS
235         pushw $1f               // return point
236         pushw 0x20(%eax)        // flags
237         pushl 0x1c(%eax)        // CS:IP
238
239         // Load calling registers.
240         movl 0x04(%eax), %edi
241         movl 0x08(%eax), %esi
242         movl 0x0c(%eax), %ebx
243         movl 0x10(%eax), %edx
244         movl 0x14(%eax), %ecx
245         movw 0x02(%eax), %es    // XXX - should load %ds too
246         movl 0x18(%eax), %eax
247
248         // Invoke call
249         iretw                   // XXX - just do a lcalll
250 1:
251         // Store flags, eax, ecx
252         pushfw
253         pushl %eax
254         movl 0x06(%esp), %eax
255         movl %ecx, %ss:0x14(%eax)       // Save %ecx
256         movw %ss, %cx
257         movw %cx, %ds                   // Restore %ds == %ss
258         popl %ecx
259         movl %ecx, 0x18(%eax)           // Save %eax
260         popw %cx
261         movw %cx, 0x20(%eax)            // Save flags
262
263         // Store remaining registers
264         movw %es, 0x02(%eax)
265         movl %edi, 0x04(%eax)
266         movl %esi, 0x08(%eax)
267         movl %ebx, 0x0c(%eax)
268         movl %edx, 0x10(%eax)
269
270         // Remove %eax
271         popl %eax
272
273         cld
274
275         retl
276
277
278 // APM trampolines
279         .global apm16protected_entry
280 apm16protected_entry:
281         pushfw          // save flags
282         pushl %eax      // dummy
283         ENTRY_ARG handle_1553
284         addw $4, %sp    // pop dummy
285         popfw           // restore flags
286         lretw
287
288         .code32
289         .global apm32protected_entry
290 apm32protected_entry:
291         pushfw
292         pushw %cs       // Setup for long jump to 16bit mode
293         pushw $1f
294         addw $8, 2(%esp)
295         ljmpw *(%esp)
296         .code16gcc
297 1:
298         ENTRY_ARG_ESP handle_1553
299
300         movw $2f,(%esp) // Setup for long jump back to 32bit mode
301         subw $8, 2(%esp)
302         ljmpw *(%esp)
303         .code32
304 2:
305         addl $4, %esp   // pop call address
306         popfw
307         lretl
308
309 // 32bit elf entry point
310         .global post32
311 post32:
312         cli
313         cld
314         lidtl (BUILD_BIOS_ADDR + pmode_IDT_info)
315         lgdtl (BUILD_BIOS_ADDR + rombios32_gdt_48)
316         movl $BUILD_STACK_ADDR, %esp
317         ljmpl $PROTECTED_MODE_CS, $_code32__start
318
319         .code16gcc
320
321 // Shutdown a CPU.  We want this in the 0xf000 section to ensure that
322 // the code wont be overwritten with something else.  (Should
323 // something spurious wake up the CPU, we want to be sure that the hlt
324 // insn will still be present and will shutdown the CPU.)
325         .global permanent_halt
326 permanent_halt:
327         cli
328 1:      hlt
329         jmp 1b
330
331
332 /****************************************************************
333  * GDT and IDT tables
334  ****************************************************************/
335
336 // Protected mode IDT descriptor
337 //
338 // I just make the limit 0, so the machine will shutdown
339 // if an exception occurs during protected mode memory
340 // transfers.
341 //
342 // Set base to f0000 to correspond to beginning of BIOS,
343 // in case I actually define an IDT later
344 // Set limit to 0
345 pmode_IDT_info:
346         .word 0x0000  // limit 15:00
347         .long 0xf0000 // base 16:47
348
349 // Real mode IDT descriptor
350 //
351 // Set to typical real-mode values.
352 // base  = 000000
353 // limit =   03ff
354 rmode_IDT_info:
355         .word 0x03ff  // limit 15:00
356         .long 0       // base 16:47
357
358 rombios32_gdt_48:
359         .word 0x30
360         .word rombios32_gdt
361         .word 0x000f
362
363         .balign 8
364 rombios32_gdt:
365         .word 0, 0, 0, 0
366         .word 0, 0, 0, 0
367         // 32 bit flat code segment (PROTECTED_MODE_CS)
368         .word 0xffff, 0, 0x9b00, 0x00cf
369         // 32 bit flat data segment (PROTECTED_MODE_DS)
370         .word 0xffff, 0, 0x9300, 0x00cf
371         // 16 bit code segment base=0xf0000 limit=0xffff (REAL_MODE_CS)
372         .word 0xffff, 0, 0x9b0f, 0x0000
373         // 16 bit data segment base=0x0 limit=0xffff (REAL_MODE_DS)
374         .word 0xffff, 0, 0x9300, 0x0000
375
376 // We need a copy of this string in the 0xf000 segment, but we are not
377 // actually a PnP BIOS, so make sure it is *not* aligned, so OSes will
378 // not see it if they scan.
379         .global pnp_string
380         .balign 2
381         .byte 0
382 pnp_string:
383         .ascii "$PnP"
384
385
386 /****************************************************************
387  * Interrupt entry points
388  ****************************************************************/
389
390         // Define an entry point for an interrupt (no args passed).
391         .macro IRQ_ENTRY num
392         .global entry_\num
393         entry_\num :
394         cli         // In case something far-calls instead of using "int"
395         ENTRY handle_\num
396         iretw
397         .endm
398
399         // Define an entry point for an interrupt (can read/modify args).
400         .macro IRQ_ENTRY_ARG num
401         .global entry_\num
402         entry_\num :
403         cli         // In case something far-calls instead of using "int"
404         ENTRY_ARG handle_\num
405         iretw
406         .endm
407
408         ORG 0xe2c3
409         IRQ_ENTRY nmi
410
411         IRQ_ENTRY_ARG 13
412         IRQ_ENTRY_ARG 12
413         IRQ_ENTRY_ARG 11
414         IRQ_ENTRY 76
415         IRQ_ENTRY 1c
416         IRQ_ENTRY 70
417
418         ORG 0xe3fe
419         jmp entry_13
420
421         ORG 0xe401
422         // XXX - Fixed Disk Parameter Table
423
424         ORG 0xe6f2
425         jmp entry_19
426
427         ORG 0xe6f5
428 .include "out/cbt.proc.16.s"
429         .text
430
431         ORG 0xe729
432         // XXX - Baud Rate Generator Table
433
434         ORG 0xe739
435         IRQ_ENTRY_ARG 14
436
437         IRQ_ENTRY 74
438         IRQ_ENTRY 75
439
440         // int 18/19 are special - they reset the stack and do not return.
441         .global entry_19
442 entry_19:
443         RESET_STACK
444         pushl $_code32_handle_19
445         jmp transition32
446
447         .global entry_18
448 entry_18:
449         RESET_STACK
450         pushl $_code32_handle_18
451         jmp transition32
452
453         // IRQ trampolines
454         .macro IRQ_TRAMPOLINE num
455         .global irq_trampoline_0x\num
456         irq_trampoline_0x\num :
457         int $0x\num
458         lretw
459         .endm
460
461         IRQ_TRAMPOLINE 02
462         IRQ_TRAMPOLINE 10
463         IRQ_TRAMPOLINE 13
464         IRQ_TRAMPOLINE 15
465         IRQ_TRAMPOLINE 16
466         IRQ_TRAMPOLINE 18
467         IRQ_TRAMPOLINE 19
468         IRQ_TRAMPOLINE 1c
469         IRQ_TRAMPOLINE 4a
470
471         ORG 0xe82e
472         IRQ_ENTRY_ARG 16
473
474 entry_hwirq:
475         ENTRY handle_hwirq
476
477         ORG 0xe987
478         IRQ_ENTRY 09
479
480         ORG 0xec59
481         IRQ_ENTRY_ARG 40
482
483         ORG 0xef57
484         IRQ_ENTRY 0e
485
486         ORG 0xefc7
487 .include "out/floppy_dbt.proc.16.s"
488         .text
489
490         ORG 0xefd2
491         IRQ_ENTRY_ARG 17
492
493         ORG 0xf045
494         // XXX int 10
495         iretw
496
497         ORG 0xf065
498         IRQ_ENTRY_ARG 10
499
500         ORG 0xf0a4
501         // XXX int 1D
502         iretw
503
504         .global freespace2_start, freespace2_end
505 freespace2_start:
506
507         ORG 0xf841
508 freespace2_end:
509         jmp entry_12
510
511         ORG 0xf84d
512         jmp entry_11
513
514         ORG 0xf859
515         IRQ_ENTRY_ARG 15
516
517         ORG 0xfa6e
518 .include "out/font.proc.16.s"
519         .text
520
521         ORG 0xfe6e
522         IRQ_ENTRY_ARG 1a
523
524         ORG 0xfea5
525         IRQ_ENTRY 08
526
527         ORG 0xfef3
528         // XXX - Initial Interrupt Vector Offsets Loaded by POST
529
530         ORG 0xff00
531         // XXX - BIOS_COPYRIGHT_STRING
532         .ascii "(c) 2002 MandrakeSoft S.A. Written by Kevin Lawton & the Bochs team."
533
534         ORG 0xff53
535         .global dummy_iret_handler
536 dummy_iret_handler:
537         iretw
538
539         ORG 0xff54
540         IRQ_ENTRY_ARG 05
541
542         ORG 0xfff0 // Power-up Entry Point
543         ljmpw $SEG_BIOS, $post16
544
545         ORG 0xfff5
546         // BIOS build date
547         .ascii "06/23/99"
548
549         ORG 0xfffe
550         .byte CONFIG_MODEL_ID
551
552         .global bios_checksum
553 bios_checksum:
554         .byte 0x00
555
556         .end