Merge from vendor branch GCC:
[dragonfly.git] / sys / i386 / boot / biosboot / boot2.S
1 /*
2  * Mach Operating System
3  * Copyright (c) 1992, 1991 Carnegie Mellon University
4  * All Rights Reserved.
5  * 
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  * 
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  * 
16  * Carnegie Mellon requests users of this software to return to
17  * 
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  * 
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  *
26  *      from: Mach, Revision 2.2  92/04/04  11:35:26  rpd
27  * $FreeBSD: src/sys/i386/boot/biosboot/boot2.S,v 1.12 1999/08/28 00:43:13 peter Exp $
28  * $DragonFly: src/sys/i386/boot/biosboot/Attic/boot2.S,v 1.2 2003/06/17 04:28:34 dillon Exp $
29  */
30
31 #include        "asm.h"
32
33 /* Conventional GDT indexes. */
34 #define BOOT_CS_INDEX           3
35 #define BOOT_CS16_INDEX         5
36 #define BOOT_DS_INDEX           4
37
38 #ifdef BDE_DEBUGGER
39 #define DB_CS_INDEX             14
40 #define DB_CS16_INDEX           15
41 #define DB_DS_INDEX             16
42 #define GDT_INDEX               17
43 #endif
44
45 /* Vector numbers. */
46 #define BREAKPOINT_VECTOR       3
47 #define DEBUG_VECTOR            1
48
49 /*
50  * boot2() -- second stage boot
51  * SP points to default string if found
52  */
53
54 ENTRY(boot2)
55         data32
56         subl    %eax, %eax
57         mov     %cs, %ax
58         mov     %ax, %ds
59         mov     %ax, %es
60         data32
61         shll    $4, %eax
62
63         /* fix up GDT entries for bootstrap */
64 #define FIXUP(gdt_index) \
65         addr32; \
66         movl    %eax, EXT(Gdt)+(8*gdt_index)+2; /* actually movw %ax */ \
67         addr32; \
68         movb    %bl, EXT(Gdt)+(8*gdt_index)+4
69
70         data32
71         shld    $16, %eax, %ebx
72         FIXUP(BOOT_CS_INDEX)
73         FIXUP(BOOT_CS16_INDEX)
74         FIXUP(BOOT_DS_INDEX)
75
76         /* fix up GDT pointer */
77         data32
78         movl    %eax, %ecx
79         data32
80         addl    $ EXT(Gdt), %eax
81         addr32
82         data32
83         movl    %eax, EXT(Gdtr)+2
84
85 #ifdef BDE_DEBUGGER
86         /* fix up GDT entry for GDT */
87         data32
88         shld    $16, %eax, %ebx
89         FIXUP(GDT_INDEX)
90
91         /* fix up IDT pointer */
92         data32
93         addl    $ EXT(Idt), %ecx
94         addr32
95         data32
96         movl    %ecx, EXT(Idtr_prot)+2
97
98         /* %es = vector table segment for a while */
99         push    %es
100         data32
101         subl    %eax, %eax
102         mov     %ax, %es
103
104         /* fix up GDT entries for bdb */
105         data32
106         movl    $4*DEBUG_VECTOR, %esi
107         addr32
108         movl    %es: 2(%esi), %eax      /* actually movw to %ax */
109         data32
110         shll    $4, %eax
111         data32
112         shld    $16, %eax, %ebx
113         FIXUP(DB_CS_INDEX)
114         FIXUP(DB_CS16_INDEX)
115         FIXUP(DB_DS_INDEX)
116
117         /* Fetch entry points of bdb's protected mode trap handlers.  These
118          * are stored at 2 before the corresponding entry points for real mode.
119          */
120         data32
121         subl    %ebx, %ebx
122         addr32
123         movl    %es: (%esi), %ebx       /* actually movw to %bx */
124         data32
125         subl    %ecx, %ecx
126         addr32
127         movl    %es: 4*(BREAKPOINT_VECTOR-DEBUG_VECTOR)(%esi), %ecx
128                                         /* actually movw to %cx */
129
130         /* %es = bdb segment for a while */
131         data32
132         shrl    $4, %eax
133         mov     %ax, %es
134
135         /* fix up IDT entries for bdb */
136         data32
137         subl    $2, %ebx                /* calculate EA to check it */
138         jb      1f                      /* give up if it would trap */
139         addr32
140         movl    %es: (%ebx), %eax       /* actually movw to %ax */
141         addr32
142         movl    %eax, EXT(Idt)+8*DEBUG_VECTOR   /* actually movw %ax */
143 1:
144         data32
145         subl    $2, %ecx
146         jb      1f
147         addr32
148         movl    %es: (%ecx), %eax       /* actually movw to %ax */
149         addr32
150         movl    %eax, EXT(Idt)+8*BREAKPOINT_VECTOR      /* actually movw %ax */
151 1:
152
153         /* finished with groping in real mode segments */
154         pop     %es
155 #endif /* BDE_DEBUGGER */
156
157         /* change to protected mode */
158         data32
159         call    EXT(real_to_prot)
160
161         /* clear the bss */
162         movl    $ EXT(edata), %edi      /* no EXT(_edata) - krufty ld */
163         movl    $ EXT(end), %ecx        /* or EXT(_end) */
164         subl    %edi, %ecx
165         subb    %al, %al
166         rep
167         stosb
168
169 #ifdef NAMEBLOCK
170         movl    %esp, EXT(dflt_name)
171 #endif
172
173         movzbl  %dl, %edx       /* discard head (%dh) and random high bits */
174         pushl   %edx
175         call    EXT(boot)
176 oops:
177         hlt
178         jmp     oops