Kernel binary end address

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
albeva
Member
Member
Posts: 42
Joined: Thu Aug 21, 2008 8:31 pm

Kernel binary end address

Post by albeva »

I am having a bit of trouble with my mm code. And the culprit is "end" that is defined by the ld script. It does seem to point to about the right address however when I use end + 4 byte as a base for my memory management it crashes.

I have followed kernel tutorials here : http://www.osdever.net/bkerndev/index.php and tutorial on memory paging/allocation here: http://www.jamesmolloy.co.uk/tutorial_h ... aging.html I have a good grasp of what is going on. But getting confused as to where *is* the real safe end of the kernel binary

And the only fix I found was by just adding roughly 1MB to it. then all works OK. I don't understand what is wrong with it.

Code: Select all

// end is defined in the linker script.
extern uint32 end asm("end");
uint32 placement_address = (uint32)&end + 0x100000; // anything less and will start crashing
Here is my linker script

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
	.text phys : AT(phys)
	{
		code = .;
		*(.text)
		*(.rodata)
		. = ALIGN(4096);
	}
	.data : AT(phys + (data - code))
	{
		data = .;
		start_ctors = .;
			*(.ctor*)
		end_ctors = .;
		start_dtors = .;
			*(.dtor*)
		end_dtors = .;
		*(.data)
		. = ALIGN(4096);
	}
	.bss : AT(phys + (bss - code))
	{
		bss = .;
		*(.bss)
		. = ALIGN(4096);
	}
	end = .;
}
In my start.asm file I allocate some space for the stack:

Code: Select all

    ; set up a stack
    ; first set a temporary and then call kmalloc
    ; to get more
    mov esp, end        ; place a temporary cache at the end
    add esp, 0x100      ; allow 256 bytes

    push 0              ; no physical address
    push 0              ; no align
    push 0x20000        ; allocate 128kb of stack
    extern _kmalloc_int
    call _kmalloc_int
    mov esp, eax
    add esp, 0x1FFFF    ; stack grows down!
   ... run kmain after that
PS yes when I set placement_address I do take into account that there is a temporary stack at "end". it's just that nothing less than 1MB seems to work.
User avatar
AndrewAPrice
Member
Member
Posts: 2308
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Kernel binary end address

Post by AndrewAPrice »

You could approximate.
e.g. I do BASE_ADDRESS + 1024k, which is enough to store my kernel plus any grub modules.
My OS is Perception.
albeva
Member
Member
Posts: 42
Joined: Thu Aug 21, 2008 8:31 pm

Re: Kernel binary end address

Post by albeva »

Thanks. I thought just as much. I think wiki should mention this small detail somewhere.
Post Reply