OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Feb 23, 2018 12:43 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Problem passing NULL to function with structure as parameter
PostPosted: Sat Dec 09, 2017 1:27 am 
Offline
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 21
Location: Petaling Jaya, Malaysia
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:
void _kmain(uint32_t magic, uint32_t mbi_addr) {
    init_term(NULL);
    ...

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


I'm performing a check first thing in init_term:
Code:
    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:
#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:
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:
static term_api_t _api;


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


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

matching the _api.getcolor.

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


Top
 Profile  
 
 Post subject: Re: Problem passing NULL to function with structure as param
PostPosted: Sat Dec 09, 2017 4:52 am 
Offline
User avatar

Joined: Wed Nov 07, 2012 2:40 am
Posts: 21
Location: Petaling Jaya, Malaysia
: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:
(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:
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group