2

I read that when an OS call is executed, the process only undergoes a mode switch as opposed to a context switch. From my understanding, this will elevate the privilege of the process and allow it to access the kernel code which is mapped into it's virtual address space. (This will require a security ring change using a Trap gate I believe). However, if this OS call is going to perform IO, it might need to use the page cache. How does this happen without a context switch? Or is the page cache also mapped to every process's virtual address space?

I could be wrong in some of my descriptions above. Correct me if I am. I am trying to piece this together. Also, I am more interested in the Linux kernel.

4

2 回答 2

1

一个简化的解释:

当您更改为内核模式时,页面映射不会更改。但是,内核自己的内存空间变得可访问(由于环更改)。在内核模式下,仍然可以访问进程的用户空间内存。因此,对于标准 I/O 调用,无需执行任何操作 - 可以直接访问用户空间。但是,在许多情况下,这不是一个好主意,因为传递的指针可能指向未映射的内存,或已分页内存,或在调用中途消失。因此通常使用copy_to_usercopy_from_user

可能有一些系统调用改变内存映射。例如,fork()创建页面映射的 CoW 副本。exec和朋友将页面重新映射到磁盘上的可执行文件。然而,这些是例外。

此外,系统调用可能会进行上下文切换。例如sleep()几乎可以保证。但是,这不是为了访问调用程序的内存。

于 2015-02-02T22:51:10.070 回答
1

例如,Linux/x86 通常对用户空间/内核空间使用 3Gb/1Gb 拆分。这意味着内核可以直接访问近 1 Gb 的物理内存。

要访问更多内存,将使用高内存(即:反弹缓冲区)。

当然,4Gb/4Gb 拆分(可以在内核配置中配置)需要上下文切换。

于 2015-02-03T21:22:59.607 回答