Linux Device Drivers阅读笔记

Linux Device Drivers, 3rd Edition
2.3.1. User Space and Kernel Space

内核空间和用户空间

Unix transfers execution from user space to kernel space whenever an application issues a system call or is suspended 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 particular process.

 

这是很关键的段落:

Unix可以通过系统调用和硬件中断实现从用户空间到内核空间的切换。对于系统调用,内核代码运行于调用进程的上下文中,内核代码可以访问调用进程的数据。对于硬件中断,内核代码运行于中断上下文,独立于当前被打断的进程,所以此时不能访问被中断的当前进程空间的数据。

 

从用户空间切换到内核空间时要使用内核空间堆栈,linux内核空间堆栈<8k, 写内核程序要相当注意栈的使用。

 

当系统从内核空间返回或者从中断上下文中返回用户空间时,如果有重新调度需求,系统会进行重新调度,即用户抢占。

 

系统处于中断上下文或者进程持有自旋锁时不允许内核抢占。

 

内核抢占一般发生在,

    1 当系统在内核空间执行时,被某个中断打断,当从这个中断返回内核空间时。

    2 内核空间释放自旋锁时。

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.

 

内核空间和用户空间拷贝数据时必须用上面两个函数,因为用户空间对应的物理页面可能被交换出去。

 

 

关于多进程在I386下的实现原理可以参考:

1 杨季文老师编写的 80X86汇编语言程序设计

2 周明德老师编写的 保护模式下的80386程序设计

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值