Page 1 of 1

initial stack mystery.. grub? ld?

Posted: Tue Mar 30, 2010 4:10 pm
by W1Z4RDZ
hello,

i've been programming my os for a couple weeks now, and as i was trying to figure some things out to try some context switching, i noticed my ESP register for my kernel wasn't where i expected.. it's just under the 64kb mark.

my linker file states the base of the executable is 1mb and i've never had any reason to doubt that number until now. i assumed (you know what they say...) everything was loading from grub relative to that address. i've dumped the memory and found code there, so that's great, but ESP is definitely placed just under 64kb.

the stack is declared in the inital asm code after space has been reserved (and 4kb aligned) and referred to by a label after the reserved space in the bss section.

i feel like ESP should initially be at 1mb+64kb because i linked it at 1mb. i've modified my code to move esp 1mb higher (where my kernel was reserving space for it (because i assumed)), but i feel like i'm not going about things the right way. there's a lot of reserved memory 'down there' and it makes me nervous to just let the stack grow 'down there.'

what don't i understand here? is there a way i can force everything to happen/link at 1mb? or should i keep doing what i'm doing and bump it up 1mb higher than 'normal' after boot?

if you need more info, just let me know.

ps: if this has already been answered on this forum somewhere, please tell me how you found it.

Re: initial stack mystery.. grub? ld?

Posted: Tue Mar 30, 2010 4:26 pm
by qw
Hi w1z4rdz,
Please post the asm code and your linker script, so we can have a look at them.

Roel

Re: initial stack mystery.. grub? ld?

Posted: Tue Mar 30, 2010 4:32 pm
by W1Z4RDZ
my kernel code started with bran's kernel dev tutorial.. doubt i modified link.ld, and i'm sure i've changed up start.asm

link.ld:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
start.asm (abridged):

Code: Select all

[BITS 32]

    
global start

start:
    mov esp, _sys_stack
    ;add esp, 0x100000
    jmp stublet

ALIGN 4
mboot:
    MULTIBOOT_PAGE_ALIGN	equ 1<<0
    MULTIBOOT_MEMORY_INFO	equ 1<<1
    MULTIBOOT_AOUT_KLUDGE	equ 1<<16
    MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
    EXTERN code, bss, end

    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM
    
    dd mboot
    dd code
    dd bss
    dd end
    dd start

stublet:
    extern _kernel_main
    push eax
    push ebx
    call _kernel_main
    jmp $
    
;;;;;; for length's sake i've omitted all the irrelevant stuff

SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
global _sys_stack
_sys_stack:

Re: initial stack mystery.. grub? ld?

Posted: Wed Mar 31, 2010 1:23 pm
by W1Z4RDZ
hmm, now esp is reading correctly. i have no clue what i did to break or fix it. i really thought it was just the initial setup i misunderstood, but now i get it again. i think, now, somewhere along the lines code execution is jumping to a section of data which is really... messy. i guess i'm going to have to unwind my os backwards in time a bit and do more prodding.

now.. how do i get rid of this useless thread? haha :-k O:)

Re: initial stack mystery.. grub? ld?

Posted: Wed Mar 31, 2010 1:55 pm
by qw
Must be my good karma.