mirror of
https://github.com/drasko/codezero.git
synced 2026-02-01 04:33:13 +01:00
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:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user