当前位置: 首页 > news >正文

第二章:模块的编译与运行-9 Platform Dependency

In continuation of the previous text: 第二章:模块的编译与运行-8 Version Dependency, let's GO ahead .

Platform Dependency

Each computer platform has its peculiarities, and kernel designers are free to exploit all the peculiarities to achieve better performance in the target object file.

每个计算机平台都有其特性,内核设计者可以充分利用这些特性来提升目标目标文件的性能。

Unlike application developers, who must link their code with precompiled libraries and stick to conventions on parameter passing, kernel developers can dedicate some processor registers to specific roles, and they have done so. Moreover, kernel code can be optimized for a specific processor in a CPU family to get the best from the target platform: unlike applications that are often distributed in binary format, a custom compilation of the kernel can be optimized for a specific computer set.

与应用程序开发者不同(他们必须将代码与预编译库链接,并严格遵循参数传递约定),内核开发者可以将某些处理器寄存器专门用于特定角色,而且他们确实这么做了。此外,内核代码可以针对特定 CPU 系列中的处理器进行优化,以充分发挥目标平台的性能:与通常以二进制形式分发的应用程序不同,内核的定制编译可以针对特定计算机进行优化。

For example, the IA32 (x86) architecture has been subdivided into several different processor types. The old 80386 processor is still supported (for now), even though its instruction set is, by modern standards, quite limited. The more modern processors in this architecture have introduced a number of new capabilities, including faster instructions for entering the kernel, interprocessor locking, copying data, etc. Newer processors can also, when operated in the correct mode, employ 36-bit (orlarger) physical addresses, allowing them to address more than 4 GB of physical memory. Other processor families have seen similar improvements. The kernel,
depending on various configuration options, can be built to make use of these additional features.

例如,IA32(x86)架构已细分为多种不同的处理器类型。老旧的 80386 处理器目前仍受支持,尽管以现代标准来看,其指令集相当有限。该架构中更现代的处理器引入了许多新功能,包括更快的内核进入指令、处理器间锁定、数据复制等。较新的处理器在正确模式下运行时,还可以使用 36 位(或更大)的物理地址,使其能够寻址超过 4GB 的物理内存。其他处理器系列也有类似的改进。内核可以根据各种配置选项,构建为利用这些额外功能的版本。

Clearly, if a module is to work with a given kernel, it must be built with the same understanding of the target processor as that kernel was. Once again, the vermagic.o object comes in to play. When a module is loaded, the kernel checks the processor-specific configuration options for the module and makes sure they match the running kernel. If the module was compiled with different options, it is not loaded.

显然,若模块要与特定内核协同工作,其构建时对目标处理器的理解必须与该内核一致。这里,vermagic.o目标文件再次发挥作用。加载模块时,内核会检查模块的处理器特定配置选项,确保其与运行中的内核匹配。如果模块是用不同的选项编译的,则不会被加载。

If you are planning to write a driver for general distribution, you may well be wondering just how you can possibly support all these different variations. The best answer, of course, is to release your driver under a GPL-compatible license and contribute it to the mainline kernel. Failing that, distributing your driver in source form and a set of scripts to compile it on the user’s system may be the best answer. Some vendors have released tools to make this task easier. If you must distribute your driver in binary form, you need to look at the different kernels provided by your target distributions, and provide a version of the module for each. Be sure to take into account any errata kernels that may have been released since the distribution was produced. Then, there are licensing issues to be considered, as we discussed in the section “License Terms” in Chapter 1. As a general rule, distributing things in source form is an easier way to make your way in the world.

如果您计划编写一个供广泛分发的驱动程序,可能会想知道如何才能支持所有这些不同的变体。当然,最佳答案是采用与 GPL 兼容的许可证发布您的驱动程序,并将其贡献到主线内核中。如果做不到这一点,以源代码形式分发驱动程序并提供一套在用户系统上编译它的脚本可能是最佳选择。一些供应商已经发布了工具来简化这项任务。如果必须以二进制形式分发驱动程序,则需要考虑目标发行版提供的不同内核,并为每个内核提供相应的模块版本。务必考虑发行版发布后可能推出的任何勘误内核。此外,还有我们在第 1 章 “许可条款” 部分讨论过的许可问题。一般来说,以源代码形式分发是更简单的方式。

补充说明:

  1. 处理器特定优化的具体体现
    内核通过配置选项(如CONFIG_MK8针对 AMD K8 处理器,CONFIG_X86_64针对 64 位 x86 处理器)启用特定于 CPU 的优化。例如,针对支持 SSE 指令集的处理器,内核可能使用更高效的内存复制函数,而对旧处理器则回退到通用实现。模块若要正确调用这些优化后的函数,必须与内核使用相同的处理器配置选项编译。

  2. vermagic.o的处理器兼容性校验
    该文件不仅包含内核版本信息,还记录了处理器相关的配置(如CONFIG_CPU_FAMILYCONFIG_X86_L1_CACHE_SHIFT等)。加载时,内核会对比模块与自身的处理器配置哈希值,若不匹配则拒绝加载,避免因指令集不兼容导致的崩溃(如模块使用 SSE 指令但运行内核的 CPU 不支持)。

  3. 驱动分发的实践建议

    • 主线内核集成:这是最佳方案,驱动会随内核版本更新自动适配新处理器和接口,由内核社区共同维护兼容性。

    • 源代码分发:用户可根据自身内核配置和处理器类型编译,避免二进制兼容问题,但需提供清晰的编译指南。

    • 二进制分发:需为不同发行版(如 Ubuntu、Fedora)、不同内核版本和处理器架构(如 x86、ARM)预编译模块,维护成本高,且可能受限于内核许可条款。

理解平台特性与内核编译选项的关系,对于开发跨硬件兼容的驱动程序至关重要。

http://www.dtcms.com/a/470260.html

相关文章:

  • java多模块概念
  • 小企业网站维护什么东西互联网培训
  • 找人做网站做的很烂网站自助建设推广
  • uhttpd HTTPS 在嵌入式与 OpenWrt 上的实战部署与排查
  • 合肥网站建设正规公司抖音如何推广引流
  • [cpprestsdk] 构建HTTP消息 | http_headers.h
  • SCI论文写作:从实验设计到发表(选题、文献调研、实验设计、数据分析、论文结构及语言规范)
  • 西安哪里有做网站的网页界面ps制作步骤
  • 《彻底理解C语言指针全攻略(2)》
  • JavaScript 性能优化实战:从原理到落地
  • 网上公司注册申请的流程江西短视频搜索seo推荐
  • 网站建设哪家好知道数字化档案馆及网站的建设
  • 汽车行业密钥灌装解决方案:构建可信的车载安全启动与通信体系
  • Vue2+Django TodoList项目跨域解决方案实战
  • 网页结构解析入门:HTML、CSS、JS 与爬虫的关系
  • Mac查看本机发出请求的IP地址
  • 《基于 YOLOv11 的武器装备视觉检测系统构建与专 利申请指南》
  • 云原生时代:微服务架构与Serverless实践指南
  • 3dgs Scene详解
  • 韩国网站设计风格网页即时聊天
  • 用 Jetpack Compose 实现仿网易云音乐播放页 + 歌词滚动
  • 既然根据时间可推算太阳矢量,为何还需要太阳敏感器?
  • 做娱乐新闻的网站有哪些网站建设教材
  • ORACLE数据库字符集
  • 本机做网站服务上传到凡科手机网站建设开发
  • 谷歌和IBM:量子计算新时代即将到来
  • 做那种事免费网站WordPress网站动漫你在
  • ROS 点云配准与姿态估计
  • 活动预告|海天瑞声与您相约 NCMMSC 2025
  • Java入门级教程22——Socket编程