Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / i386 / boot / biosboot / asm.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:34:13  rpd
27  * $FreeBSD: src/sys/i386/boot/biosboot/asm.S,v 1.13 1999/08/28 00:43:11 peter Exp $
28  */
29
30
31 /*
32   Copyright 1988, 1989, 1990, 1991, 1992 
33    by Intel Corporation, Santa Clara, California.
34
35                 All Rights Reserved
36
37 Permission to use, copy, modify, and distribute this software and
38 its documentation for any purpose and without fee is hereby
39 granted, provided that the above copyright notice appears in all
40 copies and that both the copyright notice and this permission notice
41 appear in supporting documentation, and that the name of Intel
42 not be used in advertising or publicity pertaining to distribution
43 of the software without specific, written prior permission.
44
45 INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
46 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
47 IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
48 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
49 LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
50 NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
51 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 */
53
54         .file "asm.s"
55
56 #include "asm.h"
57
58
59 CR0_PE_ON       =       0x1
60 CR0_PE_OFF      =       0xfffffffe
61
62         .text
63
64 /*
65  *
66  * real_to_prot()
67  *      transfer from real mode to protected mode.
68  */
69
70 ENTRY(real_to_prot)
71         /* guarantee that interrupt is disabled when in prot mode */
72         cli
73
74         /* load the gdtr */
75         addr32
76         data32
77         lgdt    EXT(Gdtr)
78
79         /* set the PE bit of CR0 */
80         mov     %cr0, %eax
81
82         data32
83         or      $CR0_PE_ON, %eax
84         mov     %eax, %cr0 
85
86         /*
87          * make intrasegment jump to flush the processor pipeline and
88          * reload CS register
89          */
90         data32
91         ljmp    $0x18, $xprot
92 xprot:
93
94         /*
95          * we are in USE32 mode now
96          * set up the protected mode segment registers : DS, SS, ES, FS
97          */
98         movw    $0x20, %ax      /* data segment */
99         mov     %ax, %ds        /* gas would waste a prefix byte for movw */
100         mov     %ax, %ss
101         mov     %ax, %es
102         movw    $0x10, %ax      /* flat segment */
103         mov     %ax, %fs
104
105 #ifdef BDE_DEBUGGER
106         /* load idtr so we can debug */
107         lidt    EXT(Idtr_prot)
108 #endif
109
110         ret
111
112 /*
113  *
114  * prot_to_real()
115  *      transfer from protected mode to real mode
116  * 
117  */
118
119 ENTRY(prot_to_real)
120
121         /* Prepare %ax while we're still in a mode that gas understands. */
122         movw    $0x30, %ax
123
124         /* Change to use16 mode. */
125         ljmp    $0x28, $x16
126 x16:
127
128         mov     %ax, %ds
129         mov     %ax, %ss
130         mov     %ax, %es
131         mov     %ax, %fs
132
133         /* clear the PE bit of CR0 */
134         mov     %cr0, %eax
135         data32
136         and     $CR0_PE_OFF, %eax
137         mov     %eax, %cr0
138
139         /*
140          * make intersegment jmp to flush the processor pipeline
141          * and reload CS register
142          */
143         data32
144         ljmp    $BOOTSEG, $xreal
145 xreal:
146
147         /*
148          * we are in real mode now
149          * set up the real mode segment registers : DS, SS, ES, FS
150          */
151         mov     %cs, %ax
152         mov     %ax, %ds
153         mov     %ax, %ss
154         mov     %ax, %es
155         mov     %ax, %fs
156
157 #ifdef BDE_DEBUGGER
158         /* load idtr so we can debug */
159         addr32
160         data32
161         lidt    EXT(Idtr_real)
162 #endif
163
164         data32
165         ret
166
167 /*
168  * startprog(phyaddr)
169  *      start the program on protected mode where phyaddr is the entry point
170  *
171  * XXX This whole mess should go away and we should run the boot code in
172  * flat 32 bit mode with it linked -T BOOTSEG.  See the netboot code for
173  * how this is done.
174  */
175
176 ENTRY(startprog)
177         push    %ebp
178         mov     %esp, %ebp
179         movl    %esp, %eax              /* Use eax as the old stack pointer */
180
181         /* convert the current stack to a 32 bit flat model */
182         movw    $0x10, %bx
183         mov     %bx, %ss
184         addl    $(BOOTSEG<<4),%esp
185         
186         /* copy the arguments from the old stack to the new stack */
187         pushl   0x14(%eax)              /* &bootinfo */
188         pushl   $0                      /* was &nfsdiskless */
189         pushl   $0                      /* was esym */
190         pushl   $0                      /* was cyloffset */
191         pushl   0x10(%eax)              /* bootdev */
192         pushl   0x0C(%eax)              /* howto */
193         movl    $(ourreturn),%ebx
194         addl    $(BOOTSEG<<4),%ebx      /* Fix it up for flat segments */
195         pushl   %ebx                    /* our return address */
196         
197         /* push on our entry address */
198         pushl   $0x08                   /* segment selector */
199         pushl   0x08(%eax)              /* kernel entry address */
200
201         /* convert over the other data segs */
202         movw    $0x10, %bx
203         mov     %bx, %ds
204         mov     %bx, %es
205
206         /* convert the PC (and code seg) */
207         lret
208 ourreturn:
209         /* For now there is not much we can do, just lock in a loop */
210         jmp     ourreturn
211
212 /*
213  * pcpy(src, dst, cnt)
214  *      where src is a virtual address and dst is a physical address
215  */
216
217 ENTRY(pcpy)
218         push    %ebp
219         mov     %esp, %ebp
220         push    %es
221         push    %esi
222         push    %edi
223         push    %ecx
224
225         cld
226
227         /* set %es to point at the flat segment */
228         movw    $0x10, %ax
229         mov     %ax, %es
230
231         mov     0x8(%ebp), %esi         /* source */
232         mov     0xc(%ebp), %edi         /* destination */
233         mov     0x10(%ebp), %ecx        /* count */
234
235         rep
236         movsb
237
238         pop     %ecx
239         pop     %edi
240         pop     %esi
241         pop     %es
242         pop     %ebp
243
244         ret