Compiler flags for 64-bit higher half kernel.

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.
Post Reply
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Compiler flags for 64-bit higher half kernel.

Post by torshie »

Since addesses in 64-bit higher half kernel are 64-bit, we need special compiler flags to handle the "relocation truncated to fit: R_X86_64_32S against symbol xxx" problem. Currently I know two flags: one is "-mcmodel=large", the other one is "-fPIC".

When compiled with flag "-mcmodel=large", all function calls are translated to 2 instructions:

Code: Select all

ffffff8000200051:       48 b8 10 00 20 00 80    movabs $0xffffff8000200010,%rax
ffffff8000200058:       ff ff ff 
ffffff800020005b:       ff d0                   callq  *%rax
Because 64-bit CPU has IP-relative addessing support, when compiled with flag "-fPIC", all function calls are translated to 1 instruction:

Code: Select all

ffffff8000200071:       e8 d1 ff ff ff          callq  200047 <_start-0xffffff7fffffffb9>
My question is which flag will get better performance? Probably "-fPIC" beats "-mcmodel=large" in function calls. How about other situations, for example, memory copy, jump, etc. Anyone knows the flag used by linux/freebsd?

Thanks
torshie
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Compiler flags for 64-bit higher half kernel.

Post by gerryg400 »

Linux lives in the top 2GB of RAM and is compiled with the -mcmodel=kernel flag.

function calls look like

Code: Select all

ffffffff8000001f:   e8 28 20 00 00     callq ffffffff8000204c  <kinit>
If a trainstation is where trains stop, what is a workstation ?
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Re: Compiler flags for 64-bit higher half kernel.

Post by torshie »

Great, seems my kernel lives in the wrong place, lol :mrgreen:
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Compiler flags for 64-bit higher half kernel.

Post by gerryg400 »

The top 2 GB seems to work okay. You can still use 64bit pointers to access all of memory. I moved my kernel up there a little while ago.

The main issue I had was that I now need to build 2 copies of libc, one -mcmodel=kernel and one -mcmodel=small (I think).
If a trainstation is where trains stop, what is a workstation ?
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Re: Compiler flags for 64-bit higher half kernel.

Post by torshie »

I use cmake to build my kernel, so build an extra libc is very easy. In fact, I already have built two libc, one for the loader(32-bit), one for my kernel(64-bit).
Post Reply