All pastes #20633 Raw Edit

MrCiircle

public text v1 · immutable
#20633 ·published 2005-08-21 14:39 UTC
rendered paste body
;entry symbol, required by ld, to make an executable		
		bits	32

		section .text		
		
; Some constants before we get started.
		PageDirPtr		equ	$200000
		PageTablePtr	equ	$201000
		FourK			equ $1000
				
;magic stuff to enable multi-boot compliance...
		section .text
		
		mb_magic:		dd 1BADB002h
		mb_flags:		dd 2
		mb_checksum:	dd 0-2-1BADB002h
		
global _start
_start:
;- Jump to start of loader...
		jmp	init

;- include the kernel binary.	
kernelbinary:
		incbin 'kernel.bin'
	
init:
		;- we need the size of the kernel bin div 4 in ecx
		;- so...
		mov eax, init 				;- get end of kernel bin
		mov ecx, kernelbinary 		;- get start of kernel bin
		sub eax, ecx				;- Sub start from end to get size in bytes
		shr eax, 2					;- divide size in bytes by for to get size in Dwords
		mov ecx, eax				;- We need this in ecx
	
		cld							;- not that I care what direction the copy goes in :-P
		lea  esi, [kernelbinary] 	;- load address of kernel binary (source)
		lea  edi, [100000h] 		;- load address of destination
		rep movsd;					;- do the move.
	
		;- Now we need to setup our paging system...
		;- Start by filling in the entries in our page table. 
		;- We are going to map <4M pysical to >3G virtual 		
		
		xor eax,eax					;- eax = 0 - loop counter
		xor ebx,ebx					;- ebx = 0 - mapping address used for first 1M
		mov ecx,PageTablePtr		;- ecx - points to PageTable entry
loop1:	
		mov edx,ebx					;- copy address into edx
		or	edx,3					;- edx or 3 - attribute set to supervisor level, read/write, present(011 binary)
		mov [ecx], edx				;- copy our entry in.
		add ebx, FourK				;- Add 4k to target address.				
		add ecx, 4					;- Add 4 bytes to our entry pointer
		inc eax						;- inc the loop counter			
		cmp eax,1023				;- have we reached the last entry in first 4M mapping?
		jle loop1					;- if not go back and do it all again.
		
		
		;- Now setup the page directory to point at the 3G aligned memory space
		;- Start by zero-ing out the page directory...		
		mov ecx, PageDirPtr			;- Point me at entry 0
		xor ebx, ebx				;- zero out the entry cos its gonna point to nil and be non-present
		or  ebx, 2					;- // attribute set to: supervisor level, read/write, not present(010 binary)
		mov eax,0					;- eax = 0 - loop counter			
loop2:			
		;- check to see if this itteration requires our page table as entry.
		cmp eax, 767;----------------------------------------------------------------
		je inserttable				;- if we are going to insert our table jump out and do it.
		
		;- insert the entry 
		Mov [ecx], ebx;				;- copy our entry in.
		
back:
		Add ecx, 4					;- move our entry pointer 4bytes up
		inc eax						;- inc the loop counter		
		cmp eax,1023				;- have we reached the last entry?
		jle loop2					;- if not go back and do it all again.	
		jmp startpaging				;- loop is finished - begin paging
		
inserttable:
		mov ebx, PageTablePtr		;- Get the page table pointer
		or  ebx, 03;-----------------------------------------------------------------
		mov [ecx], ebx				;- Insert the entry
		xor ebx, ebx				;- clear ebx
		or 	ebx, 2					;- restore attribute settings to not present
		jmp back					;- go back into page directory loop.
		
		;- Now enable paging 
startpaging:
		mov eax, $200000
		mov cr3, eax
		mov eax, cr0
		or eax, $8000000
		mov cr0, eax
		
		;- Finally we jump to 3G+1M (our kernel is linked here)
		jmp 0C0100001h