PageRenderTime 314ms CodeModel.GetById 135ms app.highlight 4ms RepoModel.GetById 173ms app.codeStats 0ms

/asm/boot/test/kernel.s

http://github.com/dennis-gemini/tests
Assembly | 164 lines | 142 code | 22 blank | 0 comment | 9 complexity | 260e517e9e54ea60d44b98ea0708147c MD5 | raw file
  1#
  2# type := [Pr Priv*2 1 Ex DC RW Ac]
  3#
  4# Pr   := {0: absent, 1: present}
  5#
  6# Priv := {0:ring 0 (highest), 1:ring 1, 2:ring 2, 3:ring 3(lowest)}
  7#
  8# Ex   := {1:exec, 0:data}
  9#
 10# DC   := direction bit for data {
 11#		0:grows up,
 12#		1:grows down (offset >= base)
 13#	  }
 14#	  confirming bit for code {
 15#		0: executed with cpl = Priv,
 16#		1: executed with cpl <= Priv,
 17#	  }
 18#
 19# RW   := readable for code selectors {
 20#		1: readable (write access is never allowed for code segment)
 21#	  }
 22#	  writable for data selectors {
 23#		1: writable (read access is always allowed for data segment)
 24#	  }
 25#
 26# Ac   := {0: initially set, not-accessed, 1: set by CPU if accessed}
 27
 28#
 29# flags := [Gr Sz 00 0000]
 30#
 31# Gr    := {0: 1B, 1: 4K}
 32#
 33# Sz    := {0: 16bit, 1: 32bit}
 34#
 35
 36.macro GDT_ENTRY base, limit, type, flags
 37	.byte ((\limit) >>  0) & 0xff
 38	.byte ((\limit) >>  8) & 0xff
 39	.byte ((\base ) >>  0) & 0xff
 40	.byte ((\base ) >>  8) & 0xff
 41	.byte ((\base ) >> 16) & 0xff
 42	.byte ((\type ) >>  0) & 0xff
 43	.byte ((\limit) >> 16) & 0x0f | ((\flags ) & 0xf0)
 44	.byte ((\base ) >> 24) & 0xff
 45.endm
 46
 47.text
 48.code16
 49start16:
 50	mov      %cs, %ax
 51	mov      %ax, %ds
 52	mov      %ax, %es
 53	mov      %ax, %ss
 54	mov      $top_of_stack, %ax
 55	mov      %ax, %sp
 56
 57	mov      $msg_loader_started, %si
 58	call     print
 59
 60	#########################################
 61	# Fast A20 gate
 62	inb      $0x92, %al
 63	or       $2, %al
 64	outb     %al, $0x92
 65
 66	# Use BIOS INT15, 2401
 67#	mov      $0x2401, %ax
 68#	int      $0x15
 69
 70	# Alternative way to turn on A20
 71#1:	inb      $0x64, %al
 72#	testb    $0x02, %al
 73#	jnz      1b
 74#
 75#	movb     $0xd1, %al
 76#	outb     %al, $0x64
 77#
 78#2:	inb      $0x64, %al
 79#	testb    $0x02, %al
 80#	jnz      2b
 81#
 82#	movb     $0xdf, %al
 83#	outb     %al, $0x60
 84
 85	#########################################
 86	# Load GDT and IDT
 87#	xor      %eax, %eax
 88#	mov      %ds, %ax
 89#	shl      $4, %eax
 90#	addl     $gdt_begin, %eax
 91#	mov      %eax, gdt_base
 92
 93	xorw     %ax, %ax
 94	movw     %ax, %ds
 95	movw     %ax, %es
 96	movw     %ax, %ss
 97
 98	cli
 99	cld
100	lidtw    idt_desc
101	lgdtw    gdt_desc
102
103	#########################################
104	# Enter protected mode
105	# for 386
106	mov      %cr0, %eax
107	or       $1, %eax
108	mov      %eax, %cr0
109
110	# for 286
111#	smsw     %ax
112#	or       $1, %ax
113#	lmsw     %ax
114
115	#########################################
116	# Align segment registers
117	ljmpl    $0x08, $start32
118	#########################################
119
120.code32
121start32:
122	mov      $0x10, %ax
123	mov      %ax, %ds
124	mov      %ax, %es
125	mov      %ax, %fs
126	mov      %ax, %gs
127	mov      %ax, %ss
128
129	movb     $'*', %al
130	movb     $0x0C, %ah
131#	movl     $0xb8000, %ebx
132#	movw     %ax, %es: (%ebx)
133
134	movl     $(80 * 25), %ecx
135	movl     $0xb8000, %edi
136	cld
137	repnz
138	stosw
139
1401:
141	nop
142	jmp      1b
143
144.code16
145msg_loader_started: .ascii "Hello, Real Mode!\r\n\0"
146stack: .space 1024
147top_of_stack:
148
149.balign 4
150gdt_begin:
151	GDT_ENTRY 0x00000000, 0x00000, 0x00, 0x00	#0x00 dummy segment
152	GDT_ENTRY 0x00000000, 0xfffff, 0x9a, 0xc0	#0x08 code segment
153	GDT_ENTRY 0x00000000, 0xfffff, 0x92, 0xc0	#0x10 data segment
154gdt_end:
155
156gdt_desc:
157gdt_len: .word (gdt_end - gdt_begin - 1)
158gdt_base:.long gdt_begin
159
160.balign 4
161idt_desc:
162	.word 0
163	.long 0
164