summaryrefslogtreecommitdiff
path: root/src/arch/i386/lib/c_start.S
blob: ccd0127dae1b5562ac1f3a8f4f4ae84b08fd3844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <arch/asm.h>
#include <arch/intel.h>

	.section ".text"
	.code32
	.globl _start
_start:
	cli
	lgdt	%cs:gdtaddr
	ljmp	$0x10, $1f
1:	movl	$0x18, %eax
	movl	%eax, %ds
	movl	%eax, %es
	movl	%eax, %ss
	movl	%eax, %fs
	movl	%eax, %gs

	intel_chip_post_macro(0x13)		/* post 12 */

	/** clear stack */
	leal	_stack, %edi
	movl	$_estack, %ecx
	subl	%edi, %ecx
	xorl	%eax, %eax
	rep
	stosb

	/** clear bss */
	leal	_bss, %edi
	movl	$_ebss, %ecx
	subl	%edi, %ecx
	jz	.Lnobss
	xorl	%eax, %eax
	rep
	stosb
.Lnobss:

	/* set new stack */
	movl	$_estack, %esp

	/* Push the cpu index and struct cpu */
	pushl	$0
	pushl	$0

	/* push the boot_complete flag */
	pushl	%ebp

	/* Save the stack location */
	movl	%esp, %ebp

	/* Initialize the Interrupt Descriptor table */
	leal	_idt, %edi
	leal	vec0, %ebx
	movl	$(0x10 << 16), %eax	/* cs selector */

1:	movw	%bx, %ax
	movl	%ebx, %edx
	movw	$0x8E00, %dx		/* Interrupt gate - dpl=0, present */
	movl	%eax, 0(%edi)
	movl	%edx, 4(%edi)
	addl	$6, %ebx
	addl	$8, %edi
	cmpl	$_idt_end, %edi
	jne	1b

	/* Load the Interrupt descriptor table */
	lidt	idtarg

	/*
	 *	Now we are finished. Memory is up, data is copied and
	 *	bss is cleared.   Now we call the main routine and
	 *	let it do the rest.
	 */ 
	intel_chip_post_macro(0xfe)	/* post fe */

	/* Restore the stack location */
	movl	%ebp, %esp
	
	/* The boot_complete flag has already been pushed */
	call	hardwaremain
	/*NOTREACHED*/
.Lhlt:
	intel_chip_post_macro(0xee)	/* post fe */
	hlt
	jmp	.Lhlt
	
vec0:
	pushl	$0 /* error code */
	pushl	$0 /* vector */
	jmp int_hand
vec1:
	pushl	$0 /* error code */
	pushl	$1 /* vector */
	jmp int_hand
	
vec2:
	pushl	$0 /* error code */
	pushl	$2 /* vector */
	jmp int_hand
	
vec3:
	pushl	$0 /* error code */
	pushl	$3 /* vector */
	jmp	int_hand
	
vec4:
	pushl	$0 /* error code */
	pushl	$4 /* vector */
	jmp	int_hand
	
vec5:
	pushl	$0 /* error code */
	pushl	$5 /* vector */
	jmp	int_hand
	
vec6:
	pushl	$0 /* error code */
	pushl	$6 /* vector */
	jmp	int_hand
	
vec7:
	pushl	$0 /* error code */
	pushl	$7 /* vector */
	jmp	int_hand
	
vec8:
	/* error code */
	pushl	$8 /* vector */
	jmp	int_hand
	.word	0x9090
	
vec9:
	pushl	$0 /* error code */
	pushl	$9 /* vector */
	jmp int_hand
	
vec10:
	/* error code */
	pushl	$10 /* vector */
	jmp	int_hand
	.word	0x9090
	
vec11:
	/* error code */
	pushl	$11 /* vector */
	jmp	int_hand
	.word	0x9090
		
vec12:
	/* error code */
	pushl	$12 /* vector */
	jmp	int_hand
	.word	0x9090
	
vec13:
	/* error code */
	pushl	$13 /* vector */
	jmp	int_hand
	.word	0x9090
	
vec14:
	/* error code */
	pushl	$14 /* vector */
	jmp	int_hand
	.word	0x9090
	
vec15:
	pushl	$0 /* error code */
	pushl	$15 /* vector */
	jmp	int_hand
	
vec16:
	pushl	$0 /* error code */
	pushl	$16 /* vector */
	jmp	int_hand
	
vec17:
	/* error code */
	pushl	$17 /* vector */
	jmp	int_hand
	.word	0x9090
	
vec18:
	pushl	$0 /* error code */
	pushl	$18 /* vector */
	jmp	int_hand
	
vec19:
	pushl	$0 /* error code */
	pushl	$19 /* vector */
	jmp	int_hand
int_hand:
	/* At this point on the stack there is:
	 *  0(%esp) vector
	 *  4(%esp) error code
	 *  8(%esp) eip
	 * 12(%esp) cs
	 * 16(%esp) eflags
	 */
	pushl	%edi
	pushl	%esi
	pushl	%ebp
	/* Original stack pointer */
	leal	32(%esp), %ebp
	pushl	%ebp
	pushl	%ebx
	pushl	%edx
	pushl	%ecx
	pushl	%eax

	pushl	%esp	/* Pointer to structure on the stack */
	call	x86_exception
	pop	%eax	/* Drop the pointer */

	popl	%eax
	popl	%ecx
	popl	%edx
	popl	%ebx
	popl	%ebp	/* Ignore saved %esp value */
	popl	%ebp
	popl	%esi
	popl	%edi

	addl	$8, %esp /* pop of the vector and error code */

	iret

#if CONFIG_GDB_STUB == 1

	.globl gdb_stub_breakpoint
gdb_stub_breakpoint:
	popl	%eax	/* Return address */
	pushfl	
	pushl	%cs
	pushl	%eax	/* Return address */
	pushl	$0	/* No error code */
	pushl	$32	/* vector 32 is user defined */
	jmp	int_hand

#endif

	.globl gdt, gdt_end, gdt_limit, idtarg

gdt_limit = gdt_end - gdt - 1	/* compute the table limit */
gdtaddr:
	.word	gdt_limit
	.long	gdt		/* we know the offset */

	 .data

	/* This is the gdt for GCC part of LinuxBIOS.
	 * It is different from the gdt in ROMCC/ASM part of LinuxBIOS
	 * which is defined in entry32.inc */
gdt:
	/* selgdt 0, unused */
	.word	0x0000, 0x0000		/* dummy */
	.byte	0x00, 0x00, 0x00, 0x00

	/* selgdt 8, unused */
	.word	0x0000, 0x0000		/* dummy */
	.byte	0x00, 0x00, 0x00, 0x00

	/* selgdt 0x10, flat code segment */
	.word	0xffff, 0x0000		
	.byte	0x00, 0x9b, 0xcf, 0x00	

	/* selgdt 0x18, flat data segment */
	.word	0xffff, 0x0000		
	.byte	0x00, 0x93, 0xcf, 0x00	

	/* selgdt 0x20, unused */
	.word	0x0000, 0x0000		/* dummy */
	.byte	0x00, 0x00, 0x00, 0x00

#if defined(CONFIG_LEGACY_VGABIOS) && (CONFIG_LEGACY_VGABIOS == 1)
	/* from monty:
	 * 0x00009a00,0000ffffULL,   20h: 16-bit 64k code at 0x00000000
         * 0x00009200,0000ffffULL    28h: 16-bit 64k data at 0x00000000 */

	/* selgdt 0x28, 16-bit 64k code at 0x00000000 */
	.word 0xffff, 0x0000
	.byte 0x00, 0x9a, 0x00, 0x00

	/* selgdt 0x30, 16-bit 64k data at 0x00000000 */
	.word 0xffff, 0x0000
	.byte 0, 0x92, 0, 0
#endif /* defined(CONFIG_VGABIOS) && (CONFIG_VGABIOS == 1) */
gdt_end:

idtarg:
	.word	_idt_end - _idt - 1	/* limit */
	.long	_idt
	.word	0	
_idt:
	.fill	20, 8, 0	# idt is unitiailzed
_idt_end:

	.previous
.code32