Problem creating new task

Ashford Nichols <[email protected]> Thu, 28 Jul 2011 12:29:20 -0700
Newsgroups gmane.comp.micro-kernel.l4.l4ka.general
Message-ID <CAPT4yvoCjbx7JwCM43DM16NC6gaFC89K4XH2xJaz8Ej847cifA@mail.gmail.com>
--00504502ad377df07d04a9262e11
Content-Type: text/plain; charset=ISO-8859-1

I have looked at the example code that came with the kernel and have derived
this piece of code in an attempt to create a new task:

#include <l4io.h>
#include <l4/thread.h>
#include <l4/ipc.h>
#include <l4/kip.h>
#include <l4/schedule.h>

#define MEM_START (16*1024*1024)
#define PAGE_SIZE 4096

L4_ThreadId_t thread1;
L4_Time_t one_sec;
char* stack_start = (char*)MEM_START;
unsigned int increment=1;

char* get_page(void){
char *a_pointer = (char*)stack_start;
stack_start += PAGE_SIZE;
for(int i=0; i<PAGE_SIZE; i++){
a_pointer[i] = 0;
}
return a_pointer;
}

L4_ThreadId_t create_thread(void(*func)(void)){
L4_ThreadId_t me, me_local, tid;
L4_Word_t base_no, result, utcb_new, utcb_base;
void *kip;
kip = L4_KernelInterface();
me = L4_Myself();
me_local = L4_MyLocalId();
base_no = L4_ThreadNo(me);
tid = L4_GlobalId(base_no+increment, 1);
utcb_base = me_local.raw;
utcb_base &= ~(L4_UtcbAreaSize(kip) -1);
utcb_new = utcb_base + (L4_UtcbSize(kip) * increment);
//code to create new task
L4_Fpage_t kip_area = L4_FpageLog2((L4_Word_t)kip, L4_KipAreaSizeLog2(kip));
L4_Fpage_t utcb_area = L4_FpageLog2(utcb_base, L4_UtcbAreaSizeLog2(kip));
result = L4_ThreadControl(tid, tid, me, L4_nilthread, (void*)-1);
if(!result) printf("the thread was not created\nerror [%d]\n",
(int)L4_ErrorCode());
result = L4_SpaceControl(tid, 0, kip_area, utcb_area, L4_nilthread, 0);
if(!result) printf("the address space was not setup\nerror [%d]\n",
(int)L4_ErrorCode());
result = L4_ThreadControl(tid, tid, me, me, (void*)utcb_new);
if(!result) printf("the thread was not activated\nerror [%d]\n",
(int)L4_ErrorCode());
if(func!=0){
L4_Msg_t msg;
L4_MsgTag_t tag;
L4_Word_t ip = (L4_Word_t)func;
L4_Word_t sp = (L4_Word_t)(get_page()+PAGE_SIZE);
L4_Clear(&msg);
L4_Append(&msg, ip);
L4_Append(&msg, sp);
L4_Load(&msg);
tag = L4_Send(tid, one_sec);
if(L4_IpcFailed(tag)) printf("@ the thread was not started\nerror [%d]\n",
(int)L4_ErrorCode());
}
increment++;
return tid;
}

void t1(void){
printf("& borg1\n");
L4_Sleep(L4_Never);
}

int main(){
printf("the application has been started\n");
one_sec = L4_TimePeriod(1000000);
root = L4_Myself();
thread1 = create_thread(t1);
L4_Sleep(L4_TimePeriod(3000000));
return 0;
}

when this code is run, nothing is output to the screen and upon inspection
of the MRs the thread experiences a page-fault, which I suspect is not
suppose to happen since it does not appear to happen in the example code. Am
I doing something wrong?
-- 
Ashford

--00504502ad377df07d04a9262e11
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>I have looked at the example code that came with the kernel and have d=
erived this piece of code in an attempt to create a new task:</div><div><br=
></div><div>#include &lt;l4io.h&gt;</div><div>#include &lt;l4/thread.h&gt;<=
/div>
<div>#include &lt;l4/ipc.h&gt;</div><div>#include &lt;l4/kip.h&gt;</div><di=
v>#include &lt;l4/schedule.h&gt;</div><div><br></div><div>#define MEM_START=
 (16*1024*1024)</div><div>#define PAGE_SIZE 4096</div><div><br></div><div>
L4_ThreadId_t thread1;</div><div>L4_Time_t one_sec;</div><div>char* stack_s=
tart =3D (char*)MEM_START;</div><div>unsigned int increment=3D1;</div><div>=
<br></div><div>char* get_page(void){</div><div><span class=3D"Apple-tab-spa=
n" style=3D"white-space:pre">	</span>char *a_pointer =3D (char*)stack_start=
;</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>stack=
_start +=3D PAGE_SIZE;</div><div><span class=3D"Apple-tab-span" style=3D"wh=
ite-space:pre">	</span>for(int i=3D0; i&lt;PAGE_SIZE; i++){</div><div><span=
 class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>a_pointer[i] =
=3D 0;</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>}</di=
v><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>ret=
urn a_pointer;</div><div>}</div><div><br></div><div>L4_ThreadId_t create_th=
read(void(*func)(void)){</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>L4_Th=
readId_t me, me_local, tid;</div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">	</span>L4_Word_t base_no, result, utcb_new, utcb_base=
;</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>void =
*kip;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	<=
/span>kip =3D L4_KernelInterface();</div><div><span class=3D"Apple-tab-span=
" style=3D"white-space:pre">	</span>me =3D L4_Myself();</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>me_lo=
cal =3D L4_MyLocalId();</div><div><span class=3D"Apple-tab-span" style=3D"w=
hite-space:pre">	</span>base_no =3D L4_ThreadNo(me);</div><div><span class=
=3D"Apple-tab-span" style=3D"white-space:pre">	</span>tid =3D L4_GlobalId(b=
ase_no+increment, 1);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>utcb_=
base =3D me_local.raw;</div><div><span class=3D"Apple-tab-span" style=3D"wh=
ite-space:pre">	</span>utcb_base &amp;=3D ~(L4_UtcbAreaSize(kip) -1);</div>=
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>utcb_=
new =3D utcb_base + (L4_UtcbSize(kip) * increment);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>//cod=
e to create new task</div><div><span class=3D"Apple-tab-span" style=3D"whit=
e-space:pre">	</span>L4_Fpage_t kip_area =3D L4_FpageLog2((L4_Word_t)kip, L=
4_KipAreaSizeLog2(kip));</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>L4_Fp=
age_t utcb_area =3D L4_FpageLog2(utcb_base, L4_UtcbAreaSizeLog2(kip));</div=
><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>resu=
lt =3D L4_ThreadControl(tid, tid, me, L4_nilthread, (void*)-1);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>if(!r=
esult) printf(&quot;the thread was not created\nerror [%d]\n&quot;, (int)L4=
_ErrorCode());</div><div><span class=3D"Apple-tab-span" style=3D"white-spac=
e:pre">	</span>result =3D L4_SpaceControl(tid, 0, kip_area, utcb_area, L4_n=
ilthread, 0);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>if(!r=
esult) printf(&quot;the address space was not setup\nerror [%d]\n&quot;, (i=
nt)L4_ErrorCode());</div><div><span class=3D"Apple-tab-span" style=3D"white=
-space:pre">	</span>result =3D L4_ThreadControl(tid, tid, me, me, (void*)ut=
cb_new);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>if(!r=
esult) printf(&quot;the thread was not activated\nerror [%d]\n&quot;, (int)=
L4_ErrorCode());</div><div><span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre">	</span>if(func!=3D0){</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>L4_M=
sg_t msg;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre=
">		</span>L4_MsgTag_t tag;</div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">		</span>L4_Word_t ip =3D (L4_Word_t)func;</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>L4_W=
ord_t sp =3D (L4_Word_t)(get_page()+PAGE_SIZE);</div><div><span class=3D"Ap=
ple-tab-span" style=3D"white-space:pre">		</span>L4_Clear(&amp;msg);</div><=
div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>L4_Ap=
pend(&amp;msg, ip);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>L4_A=
ppend(&amp;msg, sp);</div><div><span class=3D"Apple-tab-span" style=3D"whit=
e-space:pre">		</span>L4_Load(&amp;msg);</div><div><span class=3D"Apple-tab=
-span" style=3D"white-space:pre">		</span>tag =3D L4_Send(tid, one_sec);</d=
iv>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">		</span>if(L=
4_IpcFailed(tag)) printf(&quot;@ the thread was not started\nerror [%d]\n&q=
uot;, (int)L4_ErrorCode());</div><div><span class=3D"Apple-tab-span" style=
=3D"white-space:pre">	</span>}</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>incre=
ment++;</div><div><span class=3D"Apple-tab-span" style=3D"white-space:pre">=
	</span>return tid;</div><div>}</div><div><br></div><div>void t1(void){</di=
v><div>
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>printf(&qu=
ot;&amp; borg1\n&quot;);</div><div><span class=3D"Apple-tab-span" style=3D"=
white-space:pre">	</span>L4_Sleep(L4_Never);</div><div>}</div><div><br></di=
v><div>
int main(){</div><div><span class=3D"Apple-tab-span" style=3D"white-space:p=
re">	</span>printf(&quot;the application has been started\n&quot;);</div><d=
iv><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>one_sec=
 =3D L4_TimePeriod(1000000);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>root =
=3D L4_Myself();</div><div><span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre">	</span>thread1 =3D create_thread(t1);</div><div><span class=3D"Ap=
ple-tab-span" style=3D"white-space:pre">	</span>L4_Sleep(L4_TimePeriod(3000=
000));</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>retur=
n 0;</div><div>}</div><div><span class=3D"Apple-tab-span" style=3D"white-sp=
ace:pre">	</span></div><div><br></div>when this code is run, nothing is out=
put to the screen and upon inspection of the MRs the thread experiences a p=
age-fault, which I suspect is not suppose to happen since it does not appea=
r to happen in the example code. Am I doing something wrong?<br>
-- <br>Ashford<br>

--00504502ad377df07d04a9262e11--