I'm currently trying to get multitasking with usermode applications working. The task-switching and fork code follow the general idears of JamesM's tutorials, with the exception that the kernel stacks (tss.esp0) are always mapped at the same address in userspace. So when forking the current processes stack gets copied (haven't employed copy-on-write yet). In my kernel's main i do something like that:
Code: Select all
switch_to_usermode();
int ret = syscall_fork(); <-- this works
if(!ret) {
syscall_execvp("/boot/test2"); <-- inside there it doesn't
}
Code: Select all
int fork() {
asm volatile("cli");
task_t *parent_task = (task_t*) current_task;
UINT32 physical;
UINT32 esp, ebp, eip;
task_t *new_task = (task_t*) kvmalloc(sizeof(task_t));
clone_directory(FALSE, &physical);
new_task->pid = get_next_pid();
new_task->esp = new_task->ebp = 0;
new_task->eip = 0;
new_task->directory_physical = physical;
new_task->kernel_stack = parent_task->kernel_stack;
new_task->next = 0;
...
copy_open_files(new_task, parent_task);
BOCHS_BREAKPOINT;
task_t *tmpt = ready_queue;
while(tmpt->next) tmpt = tmpt->next;
tmpt->next = new_task;t <-- exactly here: Illegal opcode exception (for the parent_task)
eip = read_eip();
if(current_task == parent_task) {
asm volatile("mov %%esp, %0" : "=r" (esp));
asm volatile("mov %%ebp, %0" : "=r" (ebp));
new_task->esp = esp;
new_task->ebp = ebp;
new_task->eip = eip;
return new_task->pid;
} else {
return 0;
}
}
Code: Select all
task_t *tmpt = ready_queue;
while(tmpt->next) tmpt = tmpt->next;
tmpt->next = new_task;t
Code: Select all
if(parent_task->pid != 0) {
task_t *tmpt = ready_queue;
while(tmpt->next) tmpt = tmpt->next;
tmpt->next = new_task;
} else {
task_t *tmpt = ready_queue;
while(tmpt->next) tmpt = tmpt->next;
tmpt->next = new_task;
}
So my question is: How does the if construct affect the stack? What does that tell me about my problem? What the f***?
gdb disassembly and test2 code are http://pastebin.com/T06VRuzP.
Sorry if my question is noobish, i'm still learning . And sorry for typos+grammar, english is not my native language.