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.
 
<sourcesyntaxhighlight lang="asm">
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 quadworduint64_t boundary
</syntaxhighlight>
</source>
 
==kernel.c==
This is not exactly your average int main(). Most notably, you do not have any library stuff available. Welcome to kernel land.
 
<source lang="c">
void _main( void* mbd, unsigned int magic )
{
//write your kernel here
}
</source>
 
==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.
 
<sourcesyntaxhighlight lang="c">
<pre>
ENTRY(loader)
OUTPUT_FORMAT(elf32-i386)
Line 143 ⟶ 138:
}
}
</syntaxhighlight>
</pre>
 
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., and Ifif you attempt to do the translation, you may get your execution when you don't want it or you may get "entry point isn't in a segment" error.
 
==Troubleshootingkernel.c==
 
;Grub <tt><nowiki>Error 7: Loading below 1MB is not supported</nowiki></tt>
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:
:Many older versions of GRUB ignore the 'physical address hint' of the [[ELF]] sections. Try to make sure you are using at least GRUB v 0.94.
 
<syntaxhighlight lang="c">
;Grub <tt>Error 28: Selected item cannot fit into memory</tt>
terminal_buffer = (uint16_t*) 0xB8000;
: Make sure your linker script is correct. See the example above. GRUB is attempting to load your kernel at the ''physical'' address of 0xC0000000, instead of 0x100000. The AT commands in the linker script fix this.
</syntaxhighlight>
 
to
;It doesn't work with BOCHS
:Some prebuilt versions of Bochs do not support 4MB pages. You may need to build your own with the following options:
<pre>
--enable-4meg-pages support 4Megabyte pages extensions
--enable-pae support Physical Address Extensions
</pre>
 
<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>
: For example, in your loader.s:
 
<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==
;I got a page fault (#PF) when accessing my GRUB multibootMultiboot info structure
: The address passed by loader.s is physical, you have to make it virtual to add your virtual base to it. For example, in your loader.s:
<sourcesyntaxhighlight lang="asm">
add ebx, KERNEL_VIRTUAL_BASE ; make the address virtual
push ebx ; push it on the stack for _main()
</syntaxhighlight>
</source>
: Don't forget to make all addresses pointing to memory locations in the multibootMultiboot info structure also virtual.
 
: 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]]