Higher Half x86 Bare Bones (Backup): Difference between revisions
[unchecked revision] | [unchecked revision] |
Content deleted Content added
add maintenance template |
|||
(19 intermediate revisions by 10 users not shown) | |||
Line 1:
{{Disputed|Talk:Higher_Half_x86_Bare_Bones}}
{{FirstPerson}}
{{TutorialExplain}}
{{BeginnersWarning}}
{{You}}
{{Rating|2}}
Here is some sample code for a kernel that is loaded by GRUB and is mapped in the upper half of memory. In this case, the kernel is loaded at 1MB in the physical address space (0x00100000), but is mapped at 3GB + 1MB in the virtual address space (0xC0100000). It is recommended that you have a firm grasp of the contents within the [[Bare bones|Bare bones tutorial]] and [[Paging]] before attempting this.
==loader.asm==
This piece of code is taking over control from the Multiboot bootloader. It sets up a page directory with page table entries that identity map the first 4MB, and also map the first 4MB to virtual 3GB. After setting up paging, it unmaps the identity mapping so that the kernel is entirely in the higher half and jumps into the kernel proper.
<
global _loader ; Make entry point visible to linker.
extern _main ; _main is defined elsewhere
Line 101 ⟶ 106:
align 32
stack:
resb STACKSIZE ; reserve 16k stack on a
</syntaxhighlight>
<source lang="c">▼
==linker.ld==
This is a little trickier than it was for the [[Bare bones|C kernel tutorial]], since you need to distinguish between virtual addresses (which will be in the higher half) and load addresses, which GRUB needs to decide where to put your kernel.
ENTRY(loader)
OUTPUT_FORMAT(elf32-i386)
Line 143 ⟶ 138:
}
}
</syntaxhighlight>
Note that we use loader (and not _loader) as our entry point. This is due to the fact that _loader's address is approximately 0xC0100000, if we try to set our eip to that address it will not find our loader function.
Also note our entry point is not being converted to physical address. GRUB does this conversion when calculating starting value of EIP
==
Using the [[Bare bones#Writing_a_kernel_in_C|kernel.c code]] from the original bare bones tutorial will work fine, with one small change. On the fourth line in terminal_initialize(), change:
<syntaxhighlight lang="c">
terminal_buffer = (uint16_t*) 0xB8000;
</syntaxhighlight>
to
<syntaxhighlight lang="c">
;I got a page fault (#PF) when accessing my GRUB multiboot info structure▼
terminal_buffer = (uint16_t*) 0xC00B8000;
: The address passed by loader.s is physical, you have to make it virtual to add your virtual base to it.▼
</syntaxhighlight>
<source lang="asm">▼
This accomodates for the kernel's new offset into higher-half space. Any direct memory access by the kernel at this point should take place with respect to this offset where necessary.
==Troubleshooting==
▲
add ebx, KERNEL_VIRTUAL_BASE ; make the address virtual
push ebx ; push it on the stack for _main()
</syntaxhighlight>
▲: Don't forget to make all addresses pointing to memory locations in the multiboot info structure also virtual.
==See Also==
===Articles===
*[[Bare bones|Bare bones]]
*An [[User:Glauxosdever/Higher_Half_x86_Bare_Bones|alternate]] higher half Bare Bones.
[[Category:Bare bones tutorials|Higher Half bare bones]]
|