目录

Linux Memory Management

Memory Address

Need for Virtual Addressing

Address Translation

Address Translation in Intel x86

MMU

Segmentation and Paging in Linux

Segments and their usage

Types of Segment Descriptors

Segmentation in Linux

Data structures for segmentation

Paging

Physical Address Extension (PAE)

Caching in Hardware

Translation Lookaside Buffers (TLB)

Process page tables


Linux Memory Management

Saurabh Purnaye

Posted on:March 1, 2018

Categories:Kernel, Linux, Memory

Memory Address

For x86 Architecture there are 3 types of addresses:

  • Logical Address:

    • This address is address of an instruction or data in machine language
    • This address consist of a segment and an offset i.e. distance from segment start address.
  • Linear address or Virtual address:
    • This address is a binary number in virtual memory that enables a process to use a location in main memory independently of other processes and to use more space than actually exists in primary storage by temporarily relegating some contents to a hard disk or internal flash drive.
  • Physical Address:
    • Address of the memory cells in RAM of the computer.

Need for Virtual Addressing

  • The main memory (RAM) available for a computer is limited.
  • Many processes use a common code in libraries.
  • Using Virtual addressing, a CPU and Kernel gives an impression to a process that the memory is unlimited.

Address Translation

  • Since 2 out of 3 address are virtual mentioned above, there is a need for address translation from Logical to Linear and Linear to Physical address.
  • For this reason each CPU contains a hardware names as Memory Management Unit (MMU).
  • Segmentation Unit: converts the Logical address to Linear.
  • Paging Unit: converts Linear address to Physical.
  • The address translation from linear address is done using two translation tables
    • Page Directory
    • Page Table

Image Credit: https://manybutfinite.com/post/memory-translation-and-segmentation/

Address Translation in Intel x86

Linear-Address Translation to a 4-KByte Page using IA-32e Paging

Formats of CR3 and Paging-Structure Entries with PAE Paging

MMU

Image Credit: By Mdjango, Andrew S. Tanenbaum (Own work) [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons

Segmentation and Paging in Linux

  • Both segmentation and Paging is redundent and hence Linux use segmentation in limited way.
  • Segmentation can assign a different linear address space to each process
  • Paging can map the same linear address space into different physical address spaces.

Segments and their usage

  • Each segment is described by 8-byte Segment Descriptor.
  • These descriptors are defined in Global Descriptor Table (GDT) or in the Local Descriptor Table(LDT).

Image credit: By John Källén (jkl at commons) (Own work) [Public domain], via Wikimedia Commons

Types of Segment Descriptors

  • Code Segment Descriptor
  • Data Segment Descriptor
  • Task State Segment Descriptor

Segmentation in Linux

image credit https://manybutfinite.com/post/anatomy-of-a-program-in-memory/

Data structures for segmentation

  • GDT Global Descriptor Table

    • There is one GDT per CPU in Linux.
    • There are 18 descriptors in each GDT for various purposes as follows:
      • Descriptors for Kernel code and User code.
      • Task State Segment (TSS)
      • default Local Descriptor Table (LDT)
      • Thread-Local Storage (TLS) segments
      • Advanced Power Management (APM )
      • Plug and Play (PnP ) BIOS services
      • Special segment used for handling exceptions.

Image Credit: Lars H. Rohwedder (User:RokerHRO – selfmade work

Paging


  • paging unit translates linear addresses into physical ones
  • For efficiency the linear addresses are divided in fixed length intervals called as pages. These continuous linear addresses within a page are mapped into continuous physical addresses.
  • Page frames: main memory is divided into fixed lenght page frames. Each page frame contains a page. Page is just block of data in memory or disk.
  • page tables: The data structures that map linear to physical addresses.
  • Page Size: 4 KB
  • Huge Pages: 2 MB and 1 GB.
  • Page address (32 bit) = Directory (10 bits) + Page Table (10 bits) + Offset(12 bit)

Physical Address Extension (PAE)

  • In order to use more than 4 GB memory Intel started using 36 pins for address translation effectively supporting more addresses.

Caching in Hardware

  • There are atleast 3 levels of caches supported in modern microprocessor.
  • The caches work on principle of spatial locality and principle of temporal locality.
  • Cache is devided into cache line generally of 64 bytes.
  • Most of caches are N-way associative.
  • Cache unit resides between the paging unit and the main memory.
  • It includes both a hardware cache memory and a cache controller.
  • Each cache line has a tag and some flags that stores the status of cache line.
  • CPUs first look for address in cache before looking into main memory.
  • Flushing cache is done using write Back mechanism which is more efficient. Only cache entry is updated by CPU and the main memory is updated eventually.

Translation Lookaside Buffers (TLB)

  • This is kind of a cache used for storing recently converted addresses between linear to physical.
  • The Address Translation unit first looks in TLB for physical address for given linear address if not found, the hardware goes through page tables to find the page.

Process page tables

  • Linux stores pages using 4 levels
  • Page Global Directory
  • Page Upper Directory
  • Page Middle Directory
  • Page Table
  • Each process has its own Page Global Directory and its own set of Page Tables.
  • Linear addresses from 0x00000000 to 0xbfffffff can be addressed when the process runs in either User or Kernel Mode.
  • Linear addresses from 0xc0000000 to 0xffffffff can be addressed only when the process runs in Kernel Mode.

内存管理:Linux Memory Management:MMU、段、分页、PAE、Cache、TLB相关推荐

  1. Linux内存管理Linux Memory Management Notes

    Linux 内存基础 地址类型 linux内核中有许多种不同的地址类型 用户虚拟地址 用户空间看到的常规地址,通过页表可以将虚拟地址和物理地址映射起来 物理地址 用在cpu和内存之间的地址叫做物理地址 ...

  2. HALCON:内存管理(Memory Management)

    HALCON:内存管理(Memory Management)

  3. 异构内存管理 Heterogeneous Memory Management (HMM)

    https://www.kernel.org/doc/html/latest/vm/hmm.html 目录 异构内存管理 (HMM) 使用特定于设备的内存分配器的问题 I/O 总线.设备内存特性 共享 ...

  4. 理解自动内存管理(Automatic Memory Management)

    内存管理 当一个对象,字符串或者数组被创建时,和它所需大小相符的存储空间会从一个中央内存池中被分配出来,这个内存池我们一般称之为堆(heap). 原理可以类比于C中的malloc()函数,只不过我们无 ...

  5. Oracle PGA内存管理 PGA Memory Management

    一.简介         Program Global Area(PGA)是一个和服务器进程关联的包含数据和控制信息的私有内存区域. 对于一个复杂的查询来说,需要在PAG中的SQL工作区(work a ...

  6. linux 进程 转存储,Linux memory management——(进程虚存空间的管理)(转)

    Linux memory management--(进程虚存空间的管理)(转)[@more@]Linux memory management--(进程虚存空间的管理) 1.内核空间和用户空间 进程运行 ...

  7. 如何展开Linux Memory Management学习?

    Linux的进程和内存是两座大山,没有翻过这两座大山对于内核的理解始终是不完整的. 关于Linux内存管理,在开始之前做些准备工作. 首先bing到了Quora的<How can one rea ...

  8. 内存管理工具Memory Analyzer的使用

    转载出自于内存管理工具Memory Analyzer的使用 安装 关于Eclipse Memory Analyzer的安装,网上有很多教程,我选择的是直接通过Eclipse插件的形式安装最新的Memo ...

  9. 详细剖析linux的内存管理方式(分段式、分页式、段页式),以及进程状态的具体关系

    进程状态之间的关系: 1.分段式内存管理: 分段:进程地址空间按照逻辑关系将自身划分为若干个段,每个段都有自己的段名,从0开始编址. 内存分配规则:以段为单位进行分配,每个段在内存中占据连续空间,但各 ...

最新文章

  1. GoCD 19.2.0 发布,ThoughtWorks 的持续集成引擎
  2. Android性能优化常见问题,附架构师必备技术详解
  3. 小米手机能用上鸿蒙吗,鸿蒙系统小米手机能用吗?鸿蒙系统支持第三方手机!
  4. linux 应用层时间和随机数,zigbee cc2530时间
  5. uva 10883——Supermean
  6. conda虚拟环境中安装ipython
  7. python 隐马尔科夫_机器学习算法之——隐马尔可夫(Hidden Markov ModelsHMM)原理及Python实现...
  8. php中会话技术,php session会话技术详解
  9. iOS AFN监听网络状态
  10. [超详细] Apache网页优化:网页压缩与网页缓存超详细
  11. Windows 8 下使用 ScrollViewer 替代 GridView
  12. MyBatis自学(5):延迟加载
  13. 寻找被黑金毁掉的黑客精神
  14. 移动边缘计算(MEC)
  15. WebSSH2和docker化
  16. PM_10 十大管理之项目沟通管理(七)和干系人管理(八)
  17. Focal-UNet
  18. The Secant Method(正割法、弦截法) 附C语言代码
  19. 网络安全工程师的学习路线,超级简单,收藏这一篇就够了
  20. 旧手机怎么当文件服务器,用旧手机做云存储服务器

热门文章

  1. 服务状态已停止_虾米音乐今日宣布关停,新平台”音螺“相关商标已注册
  2. mysql锁申请步骤_大话MySQL锁
  3. 【Cli下在vue.config.js配置configureWebpack/resolve/alias之无效 - 】
  4. git 无法提交空目录
  5. Intellij 中的git操作 转!
  6. javascript 之 className属性
  7. 用例子来区分4种文法
  8. load control template file /_controltemplates/taxonomypicker.ascx failed
  9. java多线程nullpointerexception_温故而知新!越是基础越容易被忽略,java最全基础知识,附赠资料...
  10. java jdbc 传参_java – 将参数传递给JDBC PreparedStatement