Problem passing NULL to function with structure as parameter

Programming, for all ages and all languages.
Post Reply
User avatar
tongko
Member
Member
Posts: 26
Joined: Wed Nov 07, 2012 2:40 am
Location: Petaling Jaya, Malaysia

Problem passing NULL to function with structure as parameter

Post by tongko »

Hi All,
I don't even know the subject is describing my problem correctly, as I'm noob in C. Here's the story, been scratching my head for days.

My kernel are loaded at 0xC0100000. Function init_term is at 0xC0403CA7. Kernel stack bottom: 0xC0406014, top 0xC0506014

Code: Select all

void _kmain(uint32_t magic, uint32_t mbi_addr) {
    init_term(NULL);
    ...
Signature of init_term as such.

Code: Select all

void init_term(term_api_t *api);
I'm performing a check first thing in init_term:

Code: Select all

    if (api) {    // <== triple fault when executing this line.
        // load term functions by api.
    }
I've tried "if (api != NULL)" not working, too.
When run debugging, it seems like [api] is not poiting to the kernel stack space. It is at 0x80000011, which non of the page are mapped to...

I just don't understand why this is happening, I'm expecting [api] should be at kernel stack top - 20 byte, because:
@loader
push mbi_addr <= - 4
push magic <= - 4
call _kmain <= - 4 (return address)
@_kmain
push 0 <= - 4
call init_term <= - 4 (return address)

but, as mention, it is 0x80000011, not 0xC0506028.

I don't know what happen.

EDIT: [NULL] is define as

Code: Select all

#define NULL ((void *) 0)
EDIT 2: Please point out if more information are needed.

EDIT 3: I'm thinking maybe I should forget about the parameter, for now, and go on. So I remove the parameter and then I'm stuck again:
I'm trying to assign a function to a function pointer, which is a member of a struct:

Code: Select all

typedef struct {
	const term_color_t *(*get_color)(void);
	void (*set_color)(const term_color_t *color);
	const cursor_info_t *(*get_cursor_info)(void);
	void (*set_cursor_info)(const cursor_info_t *info);
	void (*set_cursor_point)(const cursor_point_t *point);
	void (*putc)(char c);
	void (*putc_at)(char c, const cursor_point_t *point);
	void (*puts)(const char *s);
	void (*clear)(void);
} term_api_t;
In my term.c source file, I declare the variable:

Code: Select all

static term_api_t _api;
The variable [_api] is at 0xC0406000. Back to where I remove the parameter, first line of code is:

Code: Select all

    _api.get_color = get_color;
The get_color function is at 0xC04037E0 and the signature is:

Code: Select all

static const term_color_t *get_color(void)
matching the _api.getcolor.

Then I the debugger break automatically giving me the stack trace:

Code: Select all

??@0x0000e05b (Unknown Source:0)
0x0000e05b? non of the code is at this address, how can it end up there?
Continue execution will cause a TP and qemu reset.
User avatar
tongko
Member
Member
Posts: 26
Joined: Wed Nov 07, 2012 2:40 am
Location: Petaling Jaya, Malaysia

Re: Problem passing NULL to function with structure as param

Post by tongko »

:oops:
Ended up, I think the error is due to the screwed paging, I screwed up the paging setup, I guess.

After compile and link, I did an objdump -D kernel.elf then I can see that the addressing of all function are perfectly laid.

Then in GDB, I do this:

Code: Select all

(gdb) disassemble init_term
gdb shows a bunch of asm code that even noobs like me can tell something is wrong. The instruction list out are all

Code: Select all

 mov 0x08(%eax), al 
all the way down until reaches the function end address. This can only explain one thing, When CPU translate the linear address which suppose to represent init_term function, instead of pointing to the right physical address, it point the nowhere.

I'll take a look at my paging setup test again hoping to find the bug and get this solve.
Post Reply