Strange trouble while copying kernel to 0x100000

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Strange trouble while copying kernel to 0x100000

Post by Combuster »

Tip: Copy-pasting code is not a good learning exercise. If you still need to do it, at least do it correctly.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
whellcome
Posts: 10
Joined: Thu Jun 30, 2016 12:04 pm

Re: Strange trouble while copying kernel to 0x100000

Post 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?
Last edited by whellcome on Fri Jul 01, 2016 8:23 am, edited 1 time in total.
whellcome
Posts: 10
Joined: Thu Jun 30, 2016 12:04 pm

Re: Strange trouble while copying kernel to 0x100000

Post by whellcome »

And I forgot the far jump
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Strange trouble while copying kernel to 0x100000

Post 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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Strange trouble while copying kernel to 0x100000

Post 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.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
whellcome
Posts: 10
Joined: Thu Jun 30, 2016 12:04 pm

Re: Strange trouble while copying kernel to 0x100000

Post 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.
Post Reply