Page 2 of 2

Re: Strange trouble while copying kernel to 0x100000

Posted: Fri Jul 01, 2016 7:31 am
by Combuster
Tip: Copy-pasting code is not a good learning exercise. If you still need to do it, at least do it correctly.

Re: Strange trouble while copying kernel to 0x100000

Posted: Fri Jul 01, 2016 8:17 am
by whellcome
But enabling A20,loading a gdt_descriptor,
and then setting the last bit of cr0 will do it
Or there's something i don't understand?

Re: Strange trouble while copying kernel to 0x100000

Posted: Fri Jul 01, 2016 8:23 am
by whellcome
And I forgot the far jump

Re: Strange trouble while copying kernel to 0x100000

Posted: Fri Jul 01, 2016 9:38 am
by Combuster
The real order depends on what you need to do, but is not particularly fixed. Some typical conditions would be:
1 You need A20 before using memory above 1MB.
2 PE in CR0 determines if segments are loaded from the GDT or treated as value * 16
3 You need to tell the CPU where the GDT is using the LGDT instruction before using it.
4 You need a GDT entry to change the size of a data segment
5 You need a GDT entry to run 32-bit code.
6 If PE and interrupts are enabled together, you must have an IDT and GDT.

For instance, you need a GDT, with contents, and the PE bit set to use them, but you can technically do any of those steps in any order. However if you do 2 before 3/4/5 you're restricted from changing segments until you complete them. Similarly, running just above 1M only requires step 1. Running 32-bit code above 1M requires 1,2,3 and 5.

Altogether you're quite free to do what you want. Because everybody tends to use the same sort of tutorials, you might call the following order a "tradition": 5,4,1,3,2

Re: Strange trouble while copying kernel to 0x100000

Posted: Fri Jul 01, 2016 11:08 am
by onlyonemac
  • Don't try to access memory above 0x100000 until you're in protected mode with A20 enabled.
  • Don't assume that just because the CPU is fetching instructions from above 0x100000 that it really is fetching instructions from that address. A20 is, from a logical point of view, outside the CPU, so if A20 is not enabled and the CPU attempts to fetch an instruction from (e.g.) 0x100002 it will actually get the instruction that's at 0x000002, even though it appears that the instruction came from 0x100002.

Re: Strange trouble while copying kernel to 0x100000

Posted: Sat Jul 02, 2016 10:09 am
by whellcome
What I copied:
All things can be considered as is.
For example how to set the PE bit of cr0 register.
But I tried to optimize and find the best way to do this kind of things.
I'm also thinking I could be a little messy and confused when I wrote.
Anyway now all it's working...
Now I have to understand how to compile C/C++ code correctly.