After switching to higher half, kernel address is corrupted

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
stdcall
Member
Member
Posts: 78
Joined: Thu Mar 14, 2013 1:30 am

After switching to higher half, kernel address is corrupted

Post by stdcall »

Hi.
I recently enabled paging with higher half kernel.
Since then, accessing kernel start and end address variables I declared in the linker script returns wrong addresses.

This is my linker script

Code: Select all

  ENTRY(EntryPoint)
  OUTPUT_FORMAT(elf32-i386)
  
  ENTRY_BASE = 0x100000;
  CODE_VIRT  = 0xC0000000;
  
  SECTIONS {
     /* The kernel will live at 3GB + 1MB in the virtual
        address space, which will be mapped to 1MB in the
        physical address space. */
      
      . = ENTRY_BASE;
  
      .boot : {
      *(.multiboot)
_     *(.bootcode)
      *.(bootstack)
      }
  
      . += CODE_VIRT;
  
     .text : AT(ADDR(.text) - 0xC0000000) {
      kernel_start = .;
      *(.text)
      *(.rodata*)
     }
      kernel_end = .;
  
     .data ALIGN (0x1000) : AT(ADDR(.data) - 0xC0000000) {
         *(.data)
     }
  
     .bss : AT(ADDR(.bss) - 0xC0000000) {
         _sbss = .;
         *(COMMON)
         *(.bss)
         _ebss = .;
     }
  }
The correct start addresses should be: (according to readelf)

Code: Select all

[ 4] .text             PROGBITS        c0104820 005820 0020e8 00  AX  0   0 16
[ 5] .data             PROGBITS        c0107000 008000 002000 00  WA  0   0 4096
[ 6] .bss              NOBITS          c0109000 00a000 008030 00  WA  0   0 4096
[ 7] .debug_info       PROGBITS        00000000 00a000 0024ae 00      0   0  1
When I print the address I get wrong address:
Kernel start: 0x3fefb7e0, kernel end: 0x3fef96f8

The actual code to print it:

Code: Select all

extern uint32_t kernel_start;
extern uint32_t kernel_end;

printk("Kernel start: 0x%x, kernel end: 0x%x\r\n", (uint32_t) &kernel_start, (uint32_t) &kernel_end);
Before enabling paging, this code worked perfectly.
What am I missing ?
“Meaningless! Meaningless!”
says the Teacher.
“Utterly meaningless!
Everything is meaningless.” - Ecclesiastes 1, 2

Educational Purpose Operating System - EPOS
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: After switching to higher half, kernel address is corrup

Post by Octocontrabass »

You're missing a working printk function.
stdcall
Member
Member
Posts: 78
Joined: Thu Mar 14, 2013 1:30 am

Re: After switching to higher half, kernel address is corrup

Post by stdcall »

You're missing a working printk function.
Care to elaborate ?
“Meaningless! Meaningless!”
says the Teacher.
“Utterly meaningless!
Everything is meaningless.” - Ecclesiastes 1, 2

Educational Purpose Operating System - EPOS
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: After switching to higher half, kernel address is corrup

Post by kzinti »

Your printk function is obviously not working. The values you get make no sense. Not only are your start/end addresses not matching what we can see in your linker script, you have end < start!
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: After switching to higher half, kernel address is corrup

Post by Octocontrabass »

mellowcandle wrote:Care to elaborate ?
You've never used your printk function to print a number bigger than 0x7FFFFFFF, so you've never noticed that it's treating the input as signed when it should be unsigned.
stdcall
Member
Member
Posts: 78
Joined: Thu Mar 14, 2013 1:30 am

Re: After switching to higher half, kernel address is corrup

Post by stdcall »

Yes !!!

Turns out I have itoa and uitoa, and I called the wrong one for %x.

Thanks !!
“Meaningless! Meaningless!”
says the Teacher.
“Utterly meaningless!
Everything is meaningless.” - Ecclesiastes 1, 2

Educational Purpose Operating System - EPOS
Post Reply