startprog32.S revision 1.2 1 /* $NetBSD: startprog32.S,v 1.2 2017/02/24 12:24:25 nonaka Exp $ */
2 /* NetBSD: startprog.S,v 1.4 2016/12/04 08:21:08 maxv Exp */
3
4 /*
5 * Ported to boot 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
6 *
7 * Mach Operating System
8 * Copyright (c) 1992, 1991 Carnegie Mellon University
9 * All Rights Reserved.
10 *
11 * Permission to use, copy, modify and distribute this software and its
12 * documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation.
16 *
17 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
19 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 *
21 * Carnegie Mellon requests users of this software to return to
22 *
23 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
24 * School of Computer Science
25 * Carnegie Mellon University
26 * Pittsburgh PA 15213-3890
27 *
28 * any improvements or extensions that they make and grant Carnegie Mellon
29 * the rights to redistribute these changes.
30 */
31
32 /*
33 * Copyright 1988, 1989, 1990, 1991, 1992
34 * by Intel Corporation, Santa Clara, California.
35 *
36 * All Rights Reserved
37 *
38 * Permission to use, copy, modify, and distribute this software and
39 * its documentation for any purpose and without fee is hereby
40 * granted, provided that the above copyright notice appears in all
41 * copies and that both the copyright notice and this permission notice
42 * appear in supporting documentation, and that the name of Intel
43 * not be used in advertising or publicity pertaining to distribution
44 * of the software without specific, written prior permission.
45 *
46 * INTEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
47 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
48 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
49 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
50 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
51 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
52 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53 */
54
55 #include <machine/asm.h>
56 #include <machine/specialreg.h>
57
58 #define CODE_SEGMENT 0x08
59 #define DATA_SEGMENT 0x10
60
61 .align 16
62 .globl _C_LABEL(startprog32)
63 _C_LABEL(startprog32):
64 .quad 0
65
66 .globl _C_LABEL(startprog32_size)
67 _C_LABEL(startprog32_size):
68 .long startprog32_end - _C_LABEL(startprog32_start)
69
70 .text
71 .p2align 4,,15
72
73 /*
74 * startprog32(entry,argc,argv,stack,kern_start,kern_load,kern_size,loadaddr)
75 */
76 ENTRY(startprog32_start)
77 start:
78 pushl %ebp
79 movl %esp, %ebp
80
81 /*
82 * 8(%ebp): kernel entry address
83 * 12(%ebp): argc
84 * 16(%ebp): argv
85 * 20(%ebp): stack address
86 * 24(%ebp): kernel start address
87 * 28(%ebp): loaded kernel address
88 * 32(%ebp): loaded kernel size
89 * 36(%ebp): loaded start address
90 */
91
92 cli
93
94 movl 8(%ebp), %ebx /* %ebx: entry address */
95 movl 36(%ebp), %edx /* %edx: loaded start address */
96
97 /* Prepare a new stack */
98 movl 20(%ebp), %eax /* stack */
99 subl $4, %eax
100 movl %eax, %edi
101
102 /* Push some number of args onto the stack */
103 movl 12(%ebp), %ecx /* argc */
104 movl %ecx, %eax
105 decl %eax
106 shl $2, %eax
107 addl 16(%ebp), %eax /* ptr to last arg */
108 movl %eax, %esi
109
110 std /* backwards */
111 rep
112 movsl /* copy %ds:(%esi) -> %es:(%edi) */
113 cld
114 mov %edi, %esp /* set new stack pointer */
115
116 /* Copy kernel */
117 movl 24(%ebp), %edi /* dest */
118 movl 28(%ebp), %esi /* src */
119 movl 32(%ebp), %ecx /* size */
120 #if defined(NO_OVERLAP)
121 movl %ecx, %eax
122 #else
123 movl %edi, %eax
124 subl %esi, %eax
125 cmpl %ecx, %eax /* overlapping? */
126 movl %ecx, %eax
127 jb .Lbackwards
128 #endif
129 /* nope, copy forwards. */
130 shrl $2, %ecx /* copy by words */
131 rep
132 movsl
133 and $3, %eax /* any bytes left? */
134 jnz .Ltrailing
135 jmp .Lcopy_done
136
137 .Ltrailing:
138 cmp $2, %eax
139 jb 1f
140 movw (%esi), %ax
141 movw %ax, (%edi)
142 je .Lcopy_done
143 movb 2(%esi), %al
144 movb %al, 2(%edi)
145 jmp .Lcopy_done
146 1: movb (%esi), %al
147 movb %al, (%edi)
148 jmp .Lcopy_done
149
150 #if !defined(NO_OVERLAP)
151 .Lbackwards:
152 addl %ecx, %edi /* copy backwards. */
153 addl %ecx, %esi
154 and $3, %eax /* any fractional bytes? */
155 jnz .Lback_align
156 .Lback_aligned:
157 shrl $2, %ecx
158 subl $4, %esi
159 subl $4, %edi
160 std
161 rep
162 movsl
163 cld
164 jmp .Lcopy_done
165
166 .Lback_align:
167 sub %eax, %esi
168 sub %eax, %edi
169 cmp $2, %eax
170 jb 1f
171 je 2f
172 movb 2(%esi), %al
173 movb %al, 2(%edi)
174 2: movw (%esi), %ax
175 movw %ax, (%edi)
176 jmp .Lback_aligned
177 1: movb (%esi), %al
178 movb %al, (%edi)
179 jmp .Lback_aligned
180 #endif
181 /* End of copy kernel */
182 .Lcopy_done:
183 cld /* LynxOS depends on it */
184
185 /* Prepare jump address */
186 lea (start32a - start)(%edx), %eax
187 movl %eax, (start32r - start)(%edx)
188
189 /* Setup GDT */
190 lea (gdt - start)(%edx), %eax
191 movl %eax, (gdtrr - start)(%edx)
192 lgdt (gdtr - start)(%edx)
193
194 /* Jump to set %cs */
195 ljmp *(start32r - start)(%edx)
196
197 .align 4
198 start32a:
199 movl $DATA_SEGMENT, %eax
200 movw %ax, %ds
201 movw %ax, %es
202 movw %ax, %fs
203 movw %ax, %gs
204 movw %ax, %ss
205
206 /* Already set new stack pointer */
207 movl %esp, %ebp
208
209 /* Disable Paging in CR0 */
210 movl %cr0, %eax
211 andl $(~CR0_PG), %eax
212 movl %eax, %cr0
213
214 /* Disable PAE in CR4 */
215 movl %cr4, %eax
216 andl $(~CR4_PAE), %eax
217 movl %eax, %cr4
218
219 jmp start32b
220
221 .align 4
222 start32b:
223 xor %eax, %eax
224 movl %ebx, (start32r - start)(%edx)
225 ljmp *(start32r - start)(%edx)
226
227 .align 16
228 start32r:
229 .long 0
230 .long CODE_SEGMENT
231 .align 16
232 gdt:
233 .long 0, 0
234 .byte 0xff, 0xff, 0x00, 0x00, 0x00, 0x9f, 0xcf, 0x00
235 .byte 0xff, 0xff, 0x00, 0x00, 0x00, 0x93, 0xcf, 0x00
236 gdtr:
237 .word gdtr - gdt
238 gdtrr:
239 .quad
240 start32end:
241 /* Space for the stack */
242 .align 16
243 .space 8192
244 startprog32_end:
245