Initial import from FreeBSD RELENG_4:
[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  */
29
30 #include        "asm.h"
31
32 /* Conventional GDT indexes. */
33 #define BOOT_CS_INDEX           3
34 #define BOOT_CS16_INDEX         5
35 #define BOOT_DS_INDEX           4
36
37 #ifdef BDE_DEBUGGER
38 #define DB_CS_INDEX             14
39 #define DB_CS16_INDEX           15
40 #define DB_DS_INDEX             16
41 #define GDT_INDEX               17
42 #endif
43
44 /* Vector numbers. */
45 #define BREAKPOINT_VECTOR       3
46 #define DEBUG_VECTOR            1
47
48 /*
49  * boot2() -- second stage boot
50  * SP points to default string if found
51  */
52
53 ENTRY(boot2)
54         data32
55         subl    %eax, %eax
56         mov     %cs, %ax
57         mov     %ax, %ds
58         mov     %ax, %es
59         data32
60         shll    $4, %eax
61
62         /* fix up GDT entries for bootstrap */
63 #define FIXUP(gdt_index) \
64         addr32; \
65         movl    %eax, EXT(Gdt)+(8*gdt_index)+2; /* actually movw %ax */ \
66         addr32; \
67         movb    %bl, EXT(Gdt)+(8*gdt_index)+4
68
69         data32
70         shld    $16, %eax, %ebx
71         FIXUP(BOOT_CS_INDEX)
72         FIXUP(BOOT_CS16_INDEX)
73         FIXUP(BOOT_DS_INDEX)
74
75         /* fix up GDT pointer */
76         data32
77         movl    %eax, %ecx
78         data32
79         addl    $ EXT(Gdt), %eax
80         addr32
81         data32
82         movl    %eax, EXT(Gdtr)+2
83
84 #ifdef BDE_DEBUGGER
85         /* fix up GDT entry for GDT */
86         data32
87         shld    $16, %eax, %ebx
88         FIXUP(GDT_INDEX)
89
90         /* fix up IDT pointer */
91         data32
92         addl    $ EXT(Idt), %ecx
93         addr32
94         data32
95         movl    %ecx, EXT(Idtr_prot)+2
96
97         /* %es = vector table segment for a while */
98         push    %es
99         data32
100         subl    %eax, %eax
101         mov     %ax, %es
102
103         /* fix up GDT entries for bdb */
104         data32
105         movl    $4*DEBUG_VECTOR, %esi
106         addr32
107         movl    %es: 2(%esi), %eax      /* actually movw to %ax */
108         data32
109         shll    $4, %eax
110         data32
111         shld    $16, %eax, %ebx
112         FIXUP(DB_CS_INDEX)
113         FIXUP(DB_CS16_INDEX)
114         FIXUP(DB_DS_INDEX)
115
116         /* Fetch entry points of bdb's protected mode trap handlers.  These
117          * are stored at 2 before the corresponding entry points for real mode.
118          */
119         data32
120         subl    %ebx, %ebx
121         addr32
122         movl    %es: (%esi), %ebx       /* actually movw to %bx */
123         data32
124         subl    %ecx, %ecx
125         addr32
126         movl    %es: 4*(BREAKPOINT_VECTOR-DEBUG_VECTOR)(%esi), %ecx
127                                         /* actually movw to %cx */
128
129         /* %es = bdb segment for a while */
130         data32
131         shrl    $4, %eax
132         mov     %ax, %es
133
134         /* fix up IDT entries for bdb */
135         data32
136         subl    $2, %ebx                /* calculate EA to check it */
137         jb      1f                      /* give up if it would trap */
138         addr32
139         movl    %es: (%ebx), %eax       /* actually movw to %ax */
140         addr32
141         movl    %eax, EXT(Idt)+8*DEBUG_VECTOR   /* actually movw %ax */
142 1:
143         data32
144         subl    $2, %ecx
145         jb      1f
146         addr32
147         movl    %es: (%ecx), %eax       /* actually movw to %ax */
148         addr32
149         movl    %eax, EXT(Idt)+8*BREAKPOINT_VECTOR      /* actually movw %ax */
150 1:
151
152         /* finished with groping in real mode segments */
153         pop     %es
154 #endif /* BDE_DEBUGGER */
155
156         /* change to protected mode */
157         data32
158         call    EXT(real_to_prot)
159
160         /* clear the bss */
161         movl    $ EXT(edata), %edi      /* no EXT(_edata) - krufty ld */
162         movl    $ EXT(end), %ecx        /* or EXT(_end) */
163         subl    %edi, %ecx
164         subb    %al, %al
165         rep
166         stosb
167
168 #ifdef NAMEBLOCK
169         movl    %esp, EXT(dflt_name)
170 #endif
171
172         movzbl  %dl, %edx       /* discard head (%dh) and random high bits */
173         pushl   %edx
174         call    EXT(boot)
175 oops:
176         hlt
177         jmp     oops