Towards adding sys_clone()

Stopped working on self_spawn() - going to finish clone() syscall first.
Arch-specific clone() library call that does ipc() and cloned child setup.
- Need to finish thread_create() that satisfy clone() necessities. i.e. setting up its stack.
  Question: Does the pager (and thus the microkernel) have to explicitly set SP_USR?
  Once the call is known to be successful, the library could set it.
This commit is contained in:
Bahadir Balban
2008-09-11 16:56:41 +03:00
parent fc51512438
commit af03975dc1
6 changed files with 230 additions and 35 deletions

View File

@@ -46,6 +46,7 @@
#define L4_IPC_TAG_STAT 23
#define L4_IPC_TAG_FSTAT 24
#define L4_IPC_TAG_FSYNC 25 /* Pager notifies vfs of file close */
#define L4_IPC_TAG_CLONE 26 /* Pager notifies vfs of file close */
/* Tags for ipc between fs0 and mm0 */

View File

@@ -39,6 +39,48 @@ BEGIN_PROC(l4_kread)
*/
END_PROC(l4_kread)
/*
* For clone() we need special assembler handling
* Same signature as ipc(): @r0 = to, @r1 = from
*
* NOTE: MR_RETURN register is hardcoded here.
* It must be updated if MR_RETURN offset is changed!
*/
BEGIN_PROC(clone_asm)
stmfd sp!, {r4-r8,lr} @ Save context.
utcb_address r12 @ Get utcb address.
ldmia r12!, {r3-r8} @ Load 6 Message registers from utcb. MR0-MR5
ldr r12, =__l4_ipc
mov lr, pc
ldr pc, [r12] @ Perform the ipc()
/*
* At this moment:
* - MR_RETURN tells us whether we are parent or child (or have failed).
* - Child has new SP set, with |func_ptr|arg1|{End of stack}SP<-| on stack.
* - Child needs exit logic when its function is finished.
*/
cmp r0, #0 @ Check ipc success
blt ipc_failed
cmp r2, #0 @ Check ipc return register MR_RETURN.
blt clone_failed @ Ipc was ok but clone() failed.
bgt parent_return @ It has child pid, goto parent return.
child:
ldr r0, [sp, #-4]! @ Load child's first argument.
mov lr, pc @ Save return address
ldr pc, [sp, #-4]! @ Load function pointer from stack
child_exit:
b child_exit @ We infinitely loop for now.
@ Return with normal ipc return sequence
parent_return:
clone_failed:
ipc_failed:
utcb_address r12 @ Get utcb
stmia r12, {r3-r8} @ Store mrs.
ldmfd sp!, {r4-r8,pc} @ Return restoring pc and context.
END_PROC(clone_asm)
/*
* Inter-process communication. Loads message registers as arguments before the call,
* and stores them as results after the call. @r0 = to, @r1 = from.

View File

@@ -47,3 +47,39 @@ int fork(void)
return ret;
}
extern int arch_clone(int, int);
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
{
/* Set up the child stack */
unsigned int *stack = child_stack;
int ret;
/* First word of new stack is arg */
stack[-1] = (unsigned long)arg;
/* Second word of new stack is function address */
stack[-2] = (unsigned long)fn;
/* Write the tag */
l4_set_tag(L4_IPC_TAG_CLONE);
/* Write the args as in usual ipc */
write_mr(L4SYS_ARG0, flags);
write_mr(L4SYS_ARG1, (unsigned long)child_stack);
/* Perform an ipc but with different return logic. See implementation. */
if ((ret = arch_clone(PAGER_TID, PAGER_TID)) < 0) {
printf("%s: L4 IPC Error: %d.\n", __FUNCTION__, ret);
return ret;
}
if ((ret = l4_get_retval()) < 0) {
printf("%s: CLONE Error: %d.\n", __FUNCTION__, ret);
return ret;
}
return ret;
}

View File

@@ -150,11 +150,16 @@ void handle_requests(void)
}
}
#if 0
/*
* Executes the given function in a new thread in the current
* address space but on a brand new stack.
*/
int self_spawn(void)
{
struct task_ids ids;
struct tcb *self, *self_child;
// void *stack;
unsigned long stack, stack_size;
BUG_ON(!(self = find_task(self_tid())));
@@ -166,6 +171,10 @@ int self_spawn(void)
self_child = task_create(self, &ids, THREAD_CREATE_SAMESPC,
TCB_SHARED_VM | TCB_SHARED_FILES);
if (IS_ERR(self_child = tcb_alloc_init(TCB_SHARED_VM
| TCB_SHARED_FILES)))
BUG();
/*
* Create a new utcb. Every pager thread will
* need its own utcb to answer calls.
@@ -176,24 +185,44 @@ int self_spawn(void)
task_map_prefault_utcb(self_child, self_child);
/*
* TODO: Set up a child stack by mmapping an anonymous
* region of mmap's choice. TODO: Time to add MAP_GROWSDOWN ???
* Set up a child stack by mmapping an anonymous region.
*/
if (do_mmap(0, 0, self, 0,
VM_READ | VM_WRITE | VMA_ANONYMOUS | VMA_PRIVATE, 1) < 0)
stack_size = self->stack_end - self->stack_start;
if (IS_ERR(stack = do_mmap(0, 0, self, 0,
VM_READ | VM_WRITE | VMA_ANONYMOUS
| VMA_PRIVATE | VMA_GROWSDOWN,
__pfn(stack_size)))) {
printf("%s: Error spawning %s, Error code: %d\n",
__FUNCTION__, __TASKNAME__, (int)stack);
BUG();
}
/* TODO: Notify vfs ??? */
/* Modify stack marker of child tcb */
self_child->stack_end = stack;
self_child->stack_start = stack - stack_size;
/* Prefault child stack */
for (int i = 0; i < __pfn(stack_size); i++)
prefault_page(self_child,
self_child->stack_start + __pfn_to_addr(i),
VM_READ | VM_WRITE);
/* Copy current stack to child */
memcpy((void *)self_child->stack_start,
(void *)self->stack_start, stack_size);
/* TODO: Modify registers ???, it depends on what state is copied in C0 */
/* TODO: Notify vfs ??? */
task_add_global(self_child);
if (l4_thread_control(THREAD_CREATE | THREAD_CREATE_SAMESPC, ids)
l4_thread_control(THREAD_RUN, &ids);
return 0;
}
#endif
void main(void)
{

View File

@@ -63,8 +63,11 @@ int do_fork(struct tcb *parent)
* Create a new L4 thread with parent's page tables
* kernel stack and kernel-side tcb copied
*/
child = task_create(parent, &ids, THREAD_CREATE_COPYSPC,
TCB_NO_SHARING);
if (IS_ERR(child = task_create(parent, &ids, THREAD_CREATE_COPYSPC,
TCB_NO_SHARING))) {
l4_ipc_return((int)child);
return 0;
}
/* Create new utcb for child since it can't use its parent's */
child->utcb = utcb_vaddr_new();
@@ -110,3 +113,66 @@ int sys_fork(l4id_t sender)
return do_fork(parent);
}
int sys_clone(l4id_t sender, void *child_stack, unsigned int flags)
{
struct task_ids ids;
struct vm_file *utcb_shm;
struct tcb *parent, *child;
unsigned long stack, stack_size;
BUG_ON(!(parent = find_task(sender)));
ids.tid = TASK_ID_INVALID;
ids.spid = parent->spid;
ids.tgid = parent->tgid;
if (IS_ERR(child = task_create(parent, &ids, THREAD_CREATE_SAMESPC,
TCB_SHARED_VM | TCB_SHARED_FILES))) {
l4_ipc_return((int)child);
return 0;
}
/* Allocate a unique utcb address for child */
child->utcb = utcb_vaddr_new();
/*
* Create the utcb shared memory segment
* available for child to shmat()
*/
if (IS_ERR(utcb_shm = shm_new((key_t)child->utcb,
__pfn(DEFAULT_UTCB_SIZE)))) {
l4_ipc_return((int)utcb_shm);
return 0;
}
/* Map and prefault child's utcb to vfs task */
task_map_prefault_utcb(find_task(VFS_TID), child);
/* Set up child stack marks with given stack argument */
child->stack_end = (unsigned long)child_stack;
child->stack_start = 0;
/* We can now notify vfs about forked process */
vfs_notify_fork(child, parent);
/* Add child to global task list */
task_add_global(child);
printf("%s/%s: Starting forked child.\n", __TASKNAME__, __FUNCTION__);
/* Start forked child. */
l4_thread_control(THREAD_RUN, &ids);
/* Return back to parent */
l4_ipc_return(child->tid);
return 0;
}