Switching from user mode to kernel mode

前端 未结 4 1270
南笙
南笙 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 03:47

    There are usually as set of instructions that are not really to switch to kernel mode in s general way, but to request system services. So these switch to kernel mode, but only in the context of calling some piece of functionality which was set up by the operation system for the purpose of being called by user code.

    In most modern systems, even this is hidden by an API layer that implements a specific function part of which may be doing an operating system call as above.

    But in general it is true that user code cannot do the equivalent of saying "from this point on, I want to be running in kernel mode".

    0 讨论(0)
  • 2020-12-24 04:05

    That is a typo introduced in the 8th edition and kept in the 9th. In the seventh edition, page 19, it says instead:

    "The instruction to switch to user mode is an example of a privileged instruction."

    Which clearly makes a lot more sense.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 04:08

    In user land, you request privileged operations via system calls to the kernel, which performs the switch to kernel mode as necessary. The user is using an API, the kernel is performing the privileged operations.

    0 讨论(0)
提交回复
热议问题