Hi all,

I've failed to create a new task:

the l4_task_new in my code return 0 (L4_NIL_ID) which is indicate either the task was create as inactive or didn't create at all.

here is my code:

#include <l4/sys/types.h>
#include <l4/sys/syscalls.h>
#include <l4/sys/ipc.h>

#include <stdio.h>

#define STACK_SIZE    4096
char stack001[STACK_SIZE];
char stack002[STACK_SIZE];

#define SIGMA0_TID 0x04

static void new_task_fn(void)
{
    l4_threadid_t cur_id = l4_myself();
    enter_kdebug("new_task_fn\n");
    while(1)
    {       
        printf("Currently running in task id: %d, thread no: %d\n", cur_id.id.task, cur_id.id.lthread);
    }
}

static void page_handler(void)
{
   int res;
   
                 
    l4_threadid_t src_id;        // a faulting thread
    l4_umword_t rcv_dword0;     //    the faulting address
    l4_umword_t rcv_dword1;        //    the faulting eip
    l4_timeout_t timeout;
    l4_msgdope_t result;       
   
    enter_kdebug("Page handler handle faulting thread\n");
    timeout = L4_IPC_NEVER;
    res =    l4_ipc_wait(&src_id, L4_IPC_SHORT_MSG,
                            &rcv_dword0, &rcv_dword1,
                            timeout, &result);

    enter_kdebug("Page handler handle faulting thread\n");

    if (res) { //res == 0 ? SUCCESS : FAILED
        printf("Error occurs in l4_ipc_wait: %d\n", res);
        enter_kdebug("FAILED in l4_ipc_wait: %d\n");
    }   

    l4_umword_t fault_addr = rcv_dword0 &~ 0x00000003; //rcv_dword0 & 0xFFFFFFFC
                                                                        // mask lower bits for (write/grant) flags?
    l4_umword_t rw_addr    = fault_addr | 0x00000002;        // mask the 2 nd lower bits to set write flags

    while(1) {

        l4_snd_fpage_t fpage;
       
        fpage.snd_base = rw_addr; // do wee need to left shift the address???
       
        //we can also use the function l4_fpage(address, size, write, grant)
        fpage.fpage.fp.grant = 0;
        fpage.fpage.fp.write = 1;
        fpage.fpage.fp.page = rw_addr;
        fpage.fpage.fp.size = 12; //TODO: search the constant

        res = l4_ipc_reply_and_wait(src_id,                    //dest_id
                                            L4_IPC_SHORT_FPAGE,    //send descriptor
                                            fpage.snd_base,        //snd_dword0
                                            fpage.fpage.fpage,    //snd_dword1 - might change later to .fp
                                            &src_id,                    //src_id
                                            L4_IPC_SHORT_MSG,        //rcv descriptor
                                            &rcv_dword0,            //rcv_dword0
                                            &rcv_dword1,            //rcv_dword1
                                            L4_IPC_NEVER,            //timeout
                                            &result);                //result

        if(res) {//res == 0 ? SUCCESS : FAILED
            printf("FAILED in reply wait: %d\n", res);
            enter_kdebug("FAILED in reply wait\n");
        }
    }       
}

int main(void)
{
    // 1st thread
    l4_threadid_t first_tid;
    l4_umword_t first_eip, first_esp;
    l4_threadid_t preempter, pager_tid;
    l4_umword_t old_eflags, old_eip, old_esp;

    first_tid = l4_myself();   
    first_eip = first_esp = 0xFFFFFFFF;
    preempter = pager_tid = L4_INVALID_ID;

    l4_thread_ex_regs(first_tid, first_eip, first_esp,
                            &preempter, &pager_tid,
                            &old_eflags, &old_eip, &old_esp);
   
    // 2nd thread
    l4_threadid_t sec_tid;
    l4_umword_t sec_eip, sec_esp;
    l4_threadid_t sec_pager_tid;

    sec_tid = first_tid;
    sec_tid.id.lthread += 1;
    sec_eip = (l4_umword_t)&page_handler;
    sec_esp = (l4_umword_t)&stack001[STACK_SIZE-1];
    sec_pager_tid = pager_tid;
   
    printf("sec_tid, task no: %d, threadid: %d\n eip: %lx esp: %lx\n",
            sec_tid.id.task, sec_tid.id.lthread, sec_eip, sec_esp);

    l4_thread_ex_regs(sec_tid, sec_eip, sec_esp,
                            &preempter, &sec_pager_tid,
                            &old_eflags, &old_eip, &old_esp);

    // new task
    l4_taskid_t new_task;
    l4_umword_t mcp;             //max controlled priority, min[creator_mcp, specified_mcp]
    l4_umword_t new_esp;        //initial stack pointer for thread000 if new task - active
    l4_umword_t new_eip;        //entry point for thread000 if new task - active
    l4_threadid_t new_task_pgr; //L4_NIL_ID -  inactive task.
   
   
    /* l4_task_new - create/delete task
     *    create inactive/active task
     * active task - create a new address space with 128 threads and thread000 is started
     * inactive task - occupies no resources, no address space and no threads
     * task can be deleted by its chief or higher-level chief
     */
    new_task = first_tid;
    new_task.id.task += 1;
    mcp = (l4_umword_t)255;
    new_esp = (l4_umword_t)&stack002[STACK_SIZE-1];
    new_eip = (l4_umword_t)&new_task_fn;
    new_task_pgr = sec_tid;

    //printf("new task, task no: %d, threadid: %d\n", new_task.id.task, new_task.id.lthread);
    printf("new pager, task no: %d, threadid: %d\n eip: %lx esp: %lx\n",
            new_task_pgr.id.task, new_task_pgr.id.lthread, new_eip, new_esp);

    new_task = l4_task_new(new_task, mcp, new_esp, new_eip, new_task_pgr);

    printf("new task, task no: %d, threadid: %d\n", new_task.id.task, new_task.id.lthread);

    l4_threadid_t nil_tid = L4_NIL_ID;
    if (new_task.id.task == nil_tid.id.task)
    {
        enter_kdebug("Failed to create new task\n");
    }

    //continue running thread0
    int i = 0;
    while(1)
    {   
        if (i%10 == 0) {
            l4_thread_switch(new_task);
            enter_kdebug("i/10\n");
        }
        printf("Continue running thread, task id: %d, thread no: %d\n", first_tid.id.task, first_tid.id.lthread);
        i++;
    }

    return 0;
}


any idea?



Never miss a thing. Make Yahoo your homepage.