Linux Device Drivers, 3rd edition2.3.1. user space and kernel space
Kernel space and user space
UNIX transfers execution from user space to kernel space whenever an application issues a system call or is suincluded by a hardware interrupt. kernel code executing a system call is working in the context of a process-it operates on behalf of the calling process and is able to access data in the process's address space. code that handles interrupts, on the other hand, is asynchronous with respect to processes and is not related to any participant process.
This is a key section:
UNIX can switch from user space to kernel space through system calls and hardware interruptions. For system calls, the kernel code runs in the context of the calling process, and the kernel code can access the data of the calling process. For hardware interruptions, the kernel code runs in the interrupt context and is independent of the currently interrupted process. Therefore, data in the interrupted current process space cannot be accessed at this time.
When switching from a user space to a kernel space, the kernel space stack should be used. The Linux kernel space stack should be less than 8 K. To write the kernel program, pay attention to the use of the stack.
When the system returns the user space from the kernel space or from the interrupt context, if there is a need for rescheduling, the system will re-schedule, that is, the user preemptible.
Kernel preemption is not allowed when the system is in the interrupt context or when the process holds a spin lock.
Kernel preemption generally occurs in,
1. When the system is executed in the kernel space, it is interrupted and returned to the kernel space.
2. When the kernel space is released.
3.7. read and write
unsigned long copy_to_user(void _ _user *to, const void *from, unsigned long count);unsigned long copy_from_user(void *to, const void _ _user *from, unsigned long count);
Although these functions behave like normal memcpy functions, a little extra care must be used when accessing user space from kernel code. the user pages being addressed might not be currently present in memory, and the virtual memory subsystem can put the process to sleep while the page is being transferred into place. this happens, for example, when the page must be retrieved from swap space.
The preceding two functions must be used to copy data between the kernel space and the user space, because the physical pages corresponding to the user space may be swapped out.
For the implementation principle of multi-process under i386, refer:
1. 80x86 assembly language programming compiled by Yang Jiwen
2. The 80386 Program Design in the protection mode compiled by Zhou Mingde