;kernel.asm
;Author: Josh Holtrop
;Modified: 10/30/03

%include "bootdef.inc"

%define	GDT 0x100000
%define IDT 0x102000

[global start]
[extern _isr]
[extern _k_init]

bits 32

;This is where the kernel begins execution
start:
	cli				;if they weren't already off
	mov	edi, GDT
	mov	esi, gdt
	mov	ecx, gdt_end-gdt
copy_gdt:
	lodsb
	stosb
	loop	copy_gdt

	mov	edi, IDT		;destination
	mov	esi, isr_0		;address of isr0
	mov	edx, isr_1-isr_0	;distance between isr labels
	mov	ecx, 50			;number of isrlabels
fill_idt:
	mov	ebx, esi
	mov	ax, si
	stosw				;0 offset 15:0
	mov	ax, KERNEL_CODE
	stosw				;2 selector 15:0
	mov	ax, 0x8E00
	stosw				;4 [P][DPL][0][TYPE][0][0][0][0][0][0][0][0]
	shr	esi, 16
	mov	ax, si
	stosw				;6 offset 31:16
	mov	esi, ebx
	add	esi, edx
	loop	fill_idt
	mov	word	[IDT+0x30*8+4], 0xEE00	;interrupt 0x30 has user priviledges

	lgdt	[gdtr]			;load gdt
	jmp	KERNEL_CODE:newgdtcontinue
newgdtcontinue:
	mov	ax, KERNEL_DATA
	mov	es, ax
	mov	ds, ax
	mov	gs, ax
	mov	fs, ax
	mov	ss, ax
	mov	esp, 0x1ffffc		;stack just under 2mb, moves downward
	lidt	[idtr]			;load idt

	call	_k_init
haltit:
	hlt				;halt processor when k_init is done
	jmp	haltit			;shouldn't get here...

%include "gdt.inc"
%include "idt.inc"


