Mapping higher-half kernel: checking my understanding

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

Post Reply
xixpsychoxix
Posts: 59
Joined: Tue Oct 13, 2009 8:49 pm

Mapping higher-half kernel: checking my understanding

Post by xixpsychoxix » Mon Jun 16, 2014 6:45 pm

Ok, so I am trying to understand the mapping of the kernel to the 3gb mark within the series. As I understand it, the boot loader sets up a temporary page directory, copies the kernel to 3gb (1mb physical), and jumps to the image. Here is the relevant code:

Code: Select all


%define IMAGE_RMODE_BASE 0x3000
%define IMAGE_PMODE_BASE 0xC0000000

bits 32

%include "Paging.inc"

BadImage db "*** FATAL: Invalid or corrupt kernel image. Halting system.", 0

Stage3:

	;-------------------------------;
	;   Set registers				;
	;-------------------------------;

	mov	ax, DATA_DESC		; set data segments to data selector (0x10)
	mov	ds, ax
	mov	ss, ax
	mov	es, ax
	mov	esp, 90000h		; stack begins from 90000h

	call	ClrScr32

	call	EnablePaging

CopyImage:
  	 mov	eax, dword [ImageSize]
  	 movzx	ebx, word [bpbBytesPerSector]
  	 mul	ebx
  	 mov	ebx, 4
  	 div	ebx
   	 cld
   	 mov    esi, IMAGE_RMODE_BASE
   	 mov	edi, IMAGE_PMODE_BASE
   	 mov	ecx, eax
   	 rep	movsd                   ; copy image to its protected mode address

TestImage:
  	  mov    ebx, [IMAGE_PMODE_BASE+60]
  	  add    ebx, IMAGE_PMODE_BASE    ; ebx now points to file sig (PE00)
  	  mov    esi, ebx
  	  mov    edi, ImageSig
  	  cmpsw
  	  je     EXECUTE
  	  mov	ebx, BadImage
  	  call	Puts32
  	  cli
  	  hlt
So, if I am understanding this correctly, if I would copy my kernel BEFORE paging was enabled, I would change IMAGE_PMODE_BASE to 1mb rather than 3gb:

Code: Select all

%define IMAGE_RMODE_BASE 0x3000
[b]%define IMAGE_PMODE_BASE 0x100000[/b]
bits 32

%include "Paging.inc"

BadImage db "*** FATAL: Invalid or corrupt kernel image. Halting system.", 0

Stage3:

	;-------------------------------;
	;   Set registers				;
	;-------------------------------;

	mov	ax, DATA_DESC		; set data segments to data selector (0x10)
	mov	ds, ax
	mov	ss, ax
	mov	es, ax
	mov	esp, 90000h		; stack begins from 90000h

	call	ClrScr32

CopyImage:
  	 mov	eax, dword [ImageSize]
  	 movzx	ebx, word [bpbBytesPerSector]
  	 mul	ebx
  	 mov	ebx, 4
  	 div	ebx
   	 cld
   	 mov    esi, IMAGE_RMODE_BASE
   	 mov	edi, IMAGE_PMODE_BASE
   	 mov	ecx, eax
   	 rep	movsd                   ; copy image to its protected mode address

TestImage:
  	  mov    ebx, [IMAGE_PMODE_BASE+60]
  	  add    ebx, IMAGE_PMODE_BASE    ; ebx now points to file sig (PE00)
  	  mov    esi, ebx
  	  mov    edi, ImageSig
  	  cmpsw

               [b]call EnablePaging[/b]
  	  je     EXECUTE
  	  mov	ebx, BadImage
  	  call	Puts32
  	  cli
  	  hlt
Additionally, I have a question. The kernel DISABLES paging to set up the physical memory manager. But now that the kernel has been built for address 0xc0000000 (3gb), how does this code not cause a triple-fault (any jmps-calls go to an incorrect address)? I think I have the answer, so if someone could check my guess and verify: The jumps/calls involved in setting up the physical memory manager are all relative jumps/calls, not fixed jumps/calls. But how can we ensure that this is the case? I think I am starting to majorly confuse myself, so if anyone can make sense out of my crazy thoughts please feel free to take a stab at it! Thanks :)

Post Reply