Switching from user mode to kernel mode

前端 未结 4 1289
南笙
南笙 2020-12-24 03:06

In my operating systems class, I\'m asked whether switching from user to kernel mode is privileged. This is not OS-specific. At first I thought yes, but it seems like a big

4条回答
  •  自闭症患者
    2020-12-24 04:06

    In user mode you cannot just switch to kernel mode. Interaction between user and kernel is done via system-calls. Each system-call is providing one defined service. The user sends the service name (usually a number) and the required parameters. Here is a real world example how this is done. It's x86 AT&T style assembler.

    It moves the system-call name into the EAX register, the pointer to the parameters into the EBX register of the CPU and then emits the software interrupt number 42. The interrupt handling will do the switch to kernel mode. The interrupt number is looked up in the Interrupt Descriptor Table (IDT) and invokes the function that's registered there, the syscall handler. This handler executes in kernel mode. On return to the user mode the code will move the content of EAX into the variable ret.

    pok_ret_t pok_do_syscall (pok_syscall_id_t syscall_id, pok_syscall_args_t* args)
    {
      pok_ret_t ret;
      uint32_t  args_addr;
      uint32_t  id;
    
      args_addr = (uint32_t) args;
      id        = (uint32_t) syscall_id;
    
      asm volatile ( "movl %1,%%eax  \n\t"
                     "movl %2,%%ebx  \n\t"
                     "int $42        \n\t"
                     "movl %%eax, %0 \n\t"
                     :"=g"(ret)
                     :"g"(id), "g"(args_addr)
                     : "%eax" , "%ebx"
                   );
      return ret;
    }
    

    The OS Dev wiki is a good point to read more about this.

    So you don't just switch to the kernel, but you can ask the kernel to do something for you. And then the kernel is telling you if it was done or not.

提交回复
热议问题