File: start32.S

package info (click to toggle)
mtd 20050122-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,244 kB
  • ctags: 9,869
  • sloc: ansic: 97,013; asm: 1,055; sh: 558; makefile: 356; cpp: 68
file content (585 lines) | stat: -rw-r--r-- 12,719 bytes parent folder | download | duplicates (2)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#define KERN_CODE_SEG	0x08
#define KERN_DATA_SEG	0x10
#define REAL_MODE_SEG	0x18
#define REAL_MODE_DSEG	0x20
#define CR0_PE		1

/*
 * NOTE: if you write a subroutine that is called from C code (gcc/egcs),
 * then you only have to take care of %ebx, %esi, %edi and %ebp.  These
 * registers must not be altered under any circumstance.  All other registers
 * may be clobbered without any negative side effects.  If you don't follow
 * this rule then you'll run into strange effects that only occur on some
 * gcc versions (because the register allocator may use different registers).
 *
 * All the data32 prefixes for the ljmp instructions are necessary, because
 * the assembler emits code with a relocation address of 0.  This means that
 * all destinations are initially negative, which the assembler doesn't grok,
 * because for some reason negative numbers don't fit into 16 bits. The addr32
 * prefixes are there for the same reasons, because otherwise the memory
 * references are only 16 bit wide.  Theoretically they are all superfluous.
 * One last note about prefixes: the data32 prefixes on all call _real_to_prot
 * instructions could be removed if the _real_to_prot function is changed to
 * deal correctly with 16 bit return addresses.  I tried it, but failed.
 */

/**************************************************************************
START - Where all the fun begins....
**************************************************************************/
/* this must be the first thing in the file because we enter from the top */
	.global	_start
_start:
	.code16
	movw	%ss,%bx
	movw	%sp,%cx
	data32	call	_real_to_prot

	.code32
	movw	%bx,initss
	movw	%cx,initsp
	
#ifdef SERIAL_CONSOLE
	call	initserial
#endif
	call	main
	/* fall through */

	.globl	exit
exit:
	movw	initss,%bx
	movw	initsp,%cx
	call	_prot_to_real
	.code16
/*	we reset sp to the location just before entering main
	instead of relying on the return from main because exit
	could have been called from anywhere */
	movw	%bx,%ss
	movw	%cx,%sp
	lret
	.code32

initss:
	.word	0
initsp:
	.word	0

/**************************************************************************
SLOWDOWNIO - Short delay for I/O port accesses
**************************************************************************/
	.globl	slowdownio
slowdownio:
	jmp	1f
1:	jmp	2f
2:	ret

/**************************************************************************
CURRTICKS - Get Time
**************************************************************************/
	.globl	currticks
currticks:
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	xorl	%edx,%edx
	call	_prot_to_real
	.code16
	xorw	%ax,%ax
	int	$0x1a
	data32	call	_real_to_prot
	.code32
	xorl	%eax,%eax
	shll	$16,%ecx
	movl	%edx,%eax
	orl	%ecx,%eax
	popl	%edi
	popl	%esi
	popl	%ebx
	ret

#ifdef SERIAL_CONSOLE
/**************************************************************************
INITSERIAL - Initialize serial port. Stolen from lilo
**************************************************************************/
	.globl	initserial
initserial:
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	xorl	%ecx,%ecx
	movb	$COMPORT,%cl
	movb	$COMPARM,%ch	/* 0xe3 == 9600N8, 0xa3 == 2400N8 */
	call	_prot_to_real
	.code16
	xorw	%ax,%ax
	xorw	%dx,%dx
	movb	%cl,%dl
	xchgb	%ch,%al
	int	$0x14		/* initializes port. Base address of port is stored at 0x400 + offset */
	data32	call	_real_to_prot
	.code32
	shll	%ecx
	addl	$0x400,%ecx
	movzwl	(%ecx),%ebx
	movl	%ebx,slbase
	popl	%edi
	popl	%esi
	popl	%ebx
	ret	

slbase:	
	.long	0x0

/**************************************************************************
SERDISP - Print a character on a serial line. Stolen from lilo.
**************************************************************************/
serdisp:
	pushl	%eax		/* wait for space in the send buffer */
	addl	$5,%edx
1:
	inb	%dx,%al
	testb	$0x20,%al	/* ready to send ? */
	jz	1b		/* no -> wait */
	subl	$5,%edx		/* send the character */
	popl	%eax
	outb	%al,%dx
	ret
#endif	/* SERIAL_CONSOLE */

/**************************************************************************
PUTCHAR - Print a character
**************************************************************************/
	.globl	putchar
putchar:
	pushl	%ebp
	movl	%esp,%ebp
	pushl	%ebx
	pushl	%esi
	pushl	%edi
#ifdef SERIAL_CONSOLE
	movl	slbase,%edx
	orl	%edx,%edx
	jz	1f
	movb	8(%ebp),%al
	call	serdisp
1:	
#endif
#ifdef	ANSIESC
	movzbl	8(%ebp),%ecx
	pushl	%ecx
	call	handleansi
	addl	$4,%esp
#else
	movb	8(%ebp),%cl
	call	_prot_to_real
	.code16
	movl	$1,%ebx
	movb	$0x0e,%ah
	movb	%cl,%al
	int	$0x10
	data32 call	_real_to_prot
	.code32
#endif
	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%ebp
	ret

/**************************************************************************
INT10 - Call Interrupt 0x10
**************************************************************************/
#ifdef	ANSIESC
	.globl	_int10
_int10:
	push	%ebp
	mov	%esp,%ebp
	push	%ebx
	push	%esi
	push	%edi
	movw	8(%ebp),%si
	movw	10(%ebp),%bx
	movw	12(%ebp),%cx
	movw	14(%ebp),%dx
	call	_prot_to_real
	.code16
	movw	%si,%ax
	int	$0x10
	movw	%ax,%si
	data32	call	_real_to_prot
	.code32
	movw	%si,int10ret
	movw	%bx,int10ret+2
	movw	%cx,int10ret+4
	movw	%dx,int10ret+6
	pop	%edi
	pop	%esi
	pop	%ebx
	pop	%ebp
	ret

	.globl	int10ret
int10ret:
	.word	0,0,0,0
#endif

/**************************************************************************
GETCHAR - Get a character
**************************************************************************/
	.globl	getchar
getchar:
	pushl	%ebx
	pushl	%esi
	pushl	%edi

#ifdef SERIAL_CONSOLE
	movl	slbase,%edx
	orl	%edx,%edx
	jz	3f		/* no serial port available */
1:	
	addl	$5,%edx
2:	
	inb	%dx,%al
	testb	$1,%al
	jz	2b
	subl	$5,%edx
	inb	%dx,%al
	andl	$0x7f,%eax
	je	1b
	popl	%edi
	popl	%esi
	popl	%ebx
	ret
3:	
#endif
	call	_prot_to_real
	.code16
	movb	$0x0,%ah
	int	$0x16
	movb	%al,%bl
	data32 	call	_real_to_prot
	.code32
	xor	%eax,%eax
	movzbl	%bl,%eax

	popl	%edi
	popl	%esi
	popl	%ebx
	ret

/**************************************************************************
ISKEY - Check for keyboard interrupt
**************************************************************************/
	.globl	iskey
iskey:
	pushl	%ebx
	pushl	%esi
	pushl	%edi
#ifdef SERIAL_CONSOLE
	movl	slbase,%edx
	orl	%edx,%edx
	jz	1f		/* no serial port, we loose */
	addl	$5,%edx
	inb	%dx,%al
	andl	$0x01,%eax
	popl	%edi
	popl	%esi
	popl	%ebx
	ret
1:
#endif
	call	_prot_to_real
	.code16
	xorw	%bx,%bx
	movb	$0x1,%ah
	int	$0x16
	jz	2f
	movb	%al,%bl
2:
	data32	call	_real_to_prot
	.code32
	movzbl	%bl,%eax
	popl	%edi
	popl	%esi
	popl	%ebx
	ret

#ifndef SERIAL_CONSOLE
/**************************************************************************
GETSHIFT - Get keyboard shift state
**************************************************************************/
	.globl	getshift
getshift:
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	call	_prot_to_real
	.code16
	movb	$2,%ah
	int	$0x16
	andb	$0xdf,%al
	movw	%ax,%bx
	data32	call	_real_to_prot
	.code32
	movzbl	%bl,%eax
	popl	%edi
	popl	%esi
	popl	%ebx
	ret
#endif

/**************************************************************************
MEMSIZE - Determine size of extended memory
**************************************************************************/
	.globl	memsize
memsize:
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	call	_prot_to_real
	.code16
	movw	$0x8800,%ax
	int	$0x15
	movw	%ax,%bx
	data32	call	_real_to_prot
	.code32
	movzwl	%bx,%eax
	popl	%edi
	popl	%esi
	popl	%ebx
	ret

/**************************************************************************
DISK_INIT - Initialize the disk system
**************************************************************************/
#ifdef FLOPPY
	.globl	disk_init
disk_init:
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	call	_prot_to_real
	.code16
	xorw	%ax,%ax
	movb	$0x80,%dl
	int	$0x13
	data32	call	_real_to_prot
	.code32
	popl	%edi
	popl	%esi
	popl	%ebx
	ret
#endif

/**************************************************************************
DISK_READ - Read a sector from disk
**************************************************************************/
#ifdef FLOPPY
	.globl	disk_read
disk_read:
	pushl	%ebp
	movl	%esp,%ebp
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	movb	8(%ebp),%dl	/* drive number    */
	movb	16(%ebp),%dh	/* head number     */
	movb	12(%ebp),%ch	/* cylinder number */
	movb	13(%ebp),%cl	/* cylinder number */
	shl	$6,%cl
	orb	20(%ebp),%cl	/* sector number   */
	movw	26(%ebp),%si
	rorw	$4,%esi
	movw	24(%ebp),%bx	/* buffer          */
	call	_prot_to_real
	.code16
	movw	$0x0201,%ax
	movw	%si,%es
	int	$0x13
	jc	1f
	xorw	%ax,%ax
1:
	movw	%ax,%bx
	data32	call	_real_to_prot
	.code32
	movzwl	%bx,%eax
	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%ebp
	ret
#endif

/**************************************************************************
XSTART - Transfer control to the kernel just loaded
**************************************************************************/
	.globl	xstart
xstart:
	pushl	%ebp
	movl	%esp,%ebp
	pushl	%ebx
	pushl	%esi
	pushl	%edi
	movl	8(%ebp),%eax
	movl	%eax,execaddr
	movl	12(%ebp),%ebx
	movl	16(%ebp),%ecx	/* bootp record (32bit pointer) */
	addl	$28,%ecx	/* ip, udp header */
	shll	$12,%ecx
	shrw	$12,%cx
	call	_prot_to_real
	.code16
	pushl	%ecx		/* bootp record */
	pushl	%ebx		/* file header */
	movl	$((RELOC<<12)+(1f-RELOC)),%eax
	pushl	%eax
	addr32 ljmp	execaddr-RELOC
1:
	addw	$8,%sp		/* XXX or is this 10 in case of a 16bit "ret" */
	data32	call	_real_to_prot
	.code32
	popl	%edi
	popl	%esi
	popl	%ebx
	popl	%ebp
	ret

execaddr:
	.long	0

/**************************************************************************
SETJMP - Save stack context for non-local goto
**************************************************************************/
	.globl	setjmp
setjmp:
	movl	4(%esp),%ecx
	movl	0(%esp),%edx
	movl	%edx,0(%ecx)
	movl	%ebx,4(%ecx)
	movl	%esp,8(%ecx)
	movl	%ebp,12(%ecx)
	movl	%esi,16(%ecx)
	movl	%edi,20(%ecx)
	movl	%eax,24(%ecx)
	movl	$0,%eax
	ret

/**************************************************************************
SETJMP - Non-local jump to a saved stack context
**************************************************************************/
	.globl	longjmp
longjmp:
	movl	4(%esp),%edx
	movl	8(%esp),%eax
	movl	0(%edx),%ecx
	movl	4(%edx),%ebx
	movl	8(%edx),%esp
	movl	12(%edx),%ebp
	movl	16(%edx),%esi
	movl	20(%edx),%edi
	cmpl	$0,%eax
	jne	1f
	movl	$1,%eax
1:	movl	%ecx,0(%esp)
	ret

/**************************************************************************
_REAL_TO_PROT - Go from REAL mode to Protected Mode
**************************************************************************/
	.globl	_real_to_prot
_real_to_prot:
	.code16
	cli
	cs
	addr32 lgdt	gdtarg-RELOC
	movl	%cr0,%eax
	orl	$CR0_PE,%eax
	movl	%eax,%cr0		/* turn on protected mode */

	/* flush prefetch queue, and reload %cs:%eip */
	data32	ljmp	$KERN_CODE_SEG,$1f
1:
	.code32
	/* reload other segment registers */
	movl	$KERN_DATA_SEG,%eax
	movw	%ax,%ds
	movw	%ax,%es
	movw	%ax,%ss
	addl	$RELOC,%esp		/* Fix up stack pointer */
	xorl	%eax,%eax
	movw	%ax,%fs
	movw	%ax,%gs
	popl	%eax			/* Fix up return address */
	addl	$RELOC,%eax
	pushl	%eax
	ret

/**************************************************************************
_PROT_TO_REAL - Go from Protected Mode to REAL Mode
**************************************************************************/
	.globl	_prot_to_real
_prot_to_real:
	.code32
	popl	%eax
	subl	$RELOC,%eax		/* Adjust return address */
	pushl	%eax
	subl	$RELOC,%esp		/* Adjust stack pointer */
	ljmp	$REAL_MODE_SEG,$1f-RELOC	/* jump to a 16 bit segment */
1:
	.code16
	movw	$REAL_MODE_DSEG,%ax
	movw	%ax,%ds
	movw	%ax,%ss
	movw	%ax,%es
	movw	%ax,%fs
	movw	%ax,%gs

	/* clear the PE bit of CR0 */
	movl	%cr0,%eax
	andl	$0!CR0_PE,%eax
	movl	%eax,%cr0

	/* make intersegment jmp to flush the processor pipeline
	 * and reload %cs:%eip (to clear upper 16 bits of %eip).
	 */
	data32 ljmp	$(RELOC)>>4,$2f-RELOC
2:
	/* we are in real mode now
	 * set up the real mode segment registers : %ds, $ss, %es
	 */
	movw	%cs,%ax
	movw	%ax,%ds
	movw	%ax,%es
	movw	%ax,%ss
	sti
	data32	ret		/* There is a 32 bit return address on the stack */
	.code32

/**************************************************************************
GLOBAL DESCRIPTOR TABLE
**************************************************************************/
	.align	4
_gdt:
gdtarg:
	.word	0x27			/* limit */
	.long	_gdt			/* addr */
	.byte	0,0

_pmcs:
	/* 32 bit protected mode code segment */
	.word	0xffff,0
	.byte	0,0x9f,0xcf,0

_pmds:
	/* 32 bit protected mode data segment */
	.word	0xffff,0
	.byte	0,0x93,0xcf,0

	/* 16 bit real mode code segment */
	.word	0xffff,(RELOC&0xffff)
	.byte	(RELOC>>16),0x9b,0x00,(RELOC>>24)

	/* 16 bit real mode data segment */
	.word	0xffff,(RELOC&0xffff)
	.byte	(RELOC>>16),0x93,0x00,(RELOC>>24)

	.align	4