Please help me solve the problem with fork

Programming, for all ages and all languages.
Post Reply
trashman
Posts: 5
Joined: Wed Jul 31, 2019 6:18 am

Please help me solve the problem with fork

Post by trashman »

Good day to all.
I am learning from the tutorial by jamesmolloy (http://www.jamesmolloy.co.uk/tutorial_html/). Thank him for a good tutorial. I wrote my own heap to fix some errors. Fork works fine in kernel mode. But I see errors in user mode (sys_fork()). If the fork first returns 0, the child process will stop working, and the parent process and the rest will continue. Otherwise, everything works correctly. How can I find out where the error is?
Please help me find a bug.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Please help me solve the problem with fork

Post by Octocontrabass »

I see you wrote your own heap implementation to fix some bugs in the tutorial. Did you fix the other bugs in the tutorial too?
trashman
Posts: 5
Joined: Wed Jul 31, 2019 6:18 am

Re: Please help me solve the problem with fork

Post by trashman »

Octocontrabass wrote:I see you wrote your own heap implementation to fix some bugs in the tutorial. Did you fix the other bugs in the tutorial too?
Yes. I corrected the errors described there. But I didn’t know where to start rewriting multitasking. It also worked well in kernel mode. Then I got rid of the move_stack () function. According to my observations, I often get the right job, but the problem has not yet been solved.

Code: Select all

	if (current_task == parent_task)
	{
		uint32_t esp; asm volatile("mov %%esp, %0" : "=r"(esp));
		uint32_t ebp; asm volatile("mov %%ebp, %0" : "=r"(ebp));

		if (current_task->kernel_stack > new_task->kernel_stack) {
			new_task->esp = esp - (current_task->kernel_stack - new_task->kernel_stack);
			new_task->ebp = ebp - (current_task->kernel_stack - new_task->kernel_stack);
		} else {
			new_task->esp = esp + (new_task->kernel_stack - current_task->kernel_stack);
			new_task->ebp = ebp - (current_task->kernel_stack - new_task->kernel_stack);
		}
		memcpy((void *)(new_task->kernel_stack - 2048), (void *)(current_task->kernel_stack - 2048), 2048);

	//	new_task->esp				= esp;
	//	new_task->ebp				= ebp;
		new_task->eip				= eip;

		asm volatile("sti");

		return new_task->id;
	}
trashman
Posts: 5
Joined: Wed Jul 31, 2019 6:18 am

Re: Please help me solve the problem with fork

Post by trashman »

Image
Image
When system/init launched, the init goes into user mode. system/init calls the system call fork. Then either the child breaks down or works correctly. In 3, the fork is called again.

Code: Select all

void switch_to_user_mode(uintptr_t location)
{
	asm volatile("sti");

	set_kernel_stack(current_task->kernel_stack);

	asm volatile(		"mov $0x23, %%ax\n"
					"mov %%ax, %%ds\n"
					"mov %%ax, %%es\n"
					"mov %%ax, %%fs\n"
					"mov %%ax, %%gs\n"
					"mov %%esp, %%eax\n"
					"pushl $0x23\n"
					"pushl %%eax\n"
					"pushf\n"
					"popl %%eax\n"
					"orl  $0x200, %%eax\n"
					"pushl %%eax\n"
					"pushl $0x1B\n"
					"pushl %0\n"
					"iret\n"
					: : "m"(location));
}
Post Reply