Problem passing NULL to function with structure as parameter
Page 1 of 1

Author:  tongko [ Sat Dec 09, 2017 1:27 am ]
Post subject:  Problem passing NULL to function with structure as parameter

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
void _kmain(uint32_t magic, uint32_t mbi_addr) {

Signature of init_term as such.
void init_term(term_api_t *api);

I'm performing a check first thing in init_term:
    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:
push mbi_addr <= - 4
push magic <= - 4
call _kmain <= - 4 (return address)
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
#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:

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:
static term_api_t _api;

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

The get_color function is at 0xC04037E0 and the signature is:
static const term_color_t *get_color(void)

matching the _api.getcolor.

Then I the debugger break automatically giving me the stack trace:
??@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.

Author:  tongko [ Sat Dec 09, 2017 4:52 am ]
Post subject:  Re: Problem passing NULL to function with structure as param

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:
(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

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.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group