OSDev.org
https://forum.osdev.org/

Please help me solve the problem with fork
https://forum.osdev.org/viewtopic.php?f=13&t=33802
Page 1 of 1

Author:  trashman [ Wed Jul 31, 2019 6:33 am ]
Post subject:  Please help me solve the problem with fork

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.

Author:  Octocontrabass [ Wed Jul 31, 2019 6:40 am ]
Post subject:  Re: Please help me solve the problem with fork

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?

Author:  trashman [ Wed Jul 31, 2019 6:47 am ]
Post subject:  Re: Please help me solve the problem with fork

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:
   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;
   }

Author:  trashman [ Wed Jul 31, 2019 8:27 am ]
Post subject:  Re: Please help me solve the problem with fork

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:
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));
}

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/