Linux内核中的copy_from_user如何在内部工作? [英] How does copy_from_user from the Linux kernel work internally?

查看:77
本文介绍了Linux内核中的copy_from_user如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

copy_from_user()函数在内部如何工作?考虑到内核确实具有访问用户内存空间的特权,它是否使用任何缓冲区还是完成了任何内存映射?

How exactly does the copy_from_user() function work internally? Does it use any buffers or is there any memory mapping done, considering the fact that kernel does have the privilege to access the user memory space?

推荐答案

copy_from_user()的实现高度依赖于体系结构.

The implementation of copy_from_user() is highly dependent on the architecture.

在x86和x86-64上,它只是直接从用户空间地址进行读取并写入内核空间地址,同时如果已配置,则暂时禁用SMAP(超级用户模式访问阻止).它的棘手部分是将copy_from_user()代码放在一个特殊的区域中,以便页面错误处理程序可以识别其中何时发生错误.发生在copy_from_user()中的内存保护故障不会像其他任何进程上下文代码触发那样杀死进程,也不会像在中断上下文中发生那样使内核陷入恐慌-它只是简单地恢复执行返回-EFAULT到调用者的代码路径.

On x86 and x86-64, it simply does a direct read from the userspace address and write to the kernelspace address, while temporarily disabling SMAP (Supervisor Mode Access Prevention) if it is configured. The tricky part of it is that the copy_from_user() code is placed into a special region so that the page fault handler can recognise when a fault occurs within it. A memory protection fault that occurs in copy_from_user() doesn't kill the process like it would if it is triggered by any other process-context code, or panic the kernel like it would if it occured in interrupt context - it simply resumes execution in a code path which returns -EFAULT to the caller.

这篇关于Linux内核中的copy_from_user如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆