免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5245 | 回复: 5
打印 上一主题 下一主题

[内存管理] 一个函数介绍逻辑地址,虚拟地址,线性地址和物理地址如何转换 [复制链接]

论坛徽章:
9
程序设计版块每日发帖之星
日期:2016-02-11 06:20:00程序设计版块每日发帖之星
日期:2016-02-14 06:20:00程序设计版块每日发帖之星
日期:2016-02-14 06:20:0015-16赛季CBA联赛之吉林
日期:2016-03-23 17:25:0015-16赛季CBA联赛之浙江
日期:2016-04-01 08:25:0615-16赛季CBA联赛之山西
日期:2016-04-01 10:09:1915-16赛季CBA联赛之广夏
日期:2016-06-03 15:58:212016科比退役纪念章
日期:2016-07-28 17:42:5215-16赛季CBA联赛之广东
日期:2017-02-20 23:32:43
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-01-19 15:08 |只看该作者 |倒序浏览
本帖最后由 Buddy_Zhang1 于 2018-01-19 15:44 编辑

下面函数用于介绍 逻辑地址,虚拟地址,线性地址和物理地址的转换过程:
github 地址:https://github.com/BiscuitOS/BiscuitOS
原理来自 Intel X86 官方手册 -- 系统编程卷 -- Segmentation and Paging Section

  1. /*
  2. * Logical Address Mechanism on MMU
  3. *
  4. * (C) 2018.01 BiscuitOS <buddy.zhang@aliyun.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/head.h>
  12. #include <linux/sched.h>

  13. #include <test/debug.h>

  14. static char var[50] = "BiscuitOS";

  15. /*
  16. * Logical address convent to physical address
  17. */
  18. static void logic_to_physic(void)
  19. {
  20.     struct logic_addr la;
  21.     unsigned long virtual, linear, physic;
  22.     unsigned long base, limit;
  23.     unsigned long cs, ds, cr3, cr4;
  24.     unsigned char cpl, dpl;
  25.     struct desc_struct *desc;
  26.     unsigned long PDE, PTE;

  27.     /* Obtain specific segment selector */
  28.     __asm__ ("movl %%cs, %0\n\r"
  29.              "movl %%ds, %1"
  30.              : "=r" (cs), "=r" (ds));

  31.     /* Establish a logical address */
  32.     la.offset = (unsigned long)&var;
  33.     la.sel    = ds;

  34.     /* Obtain virtual address */
  35.     virtual = la.offset;

  36.     /* violation COW */
  37.     var[48] = 'A';

  38.     /* Uses the offset in the segment selector to locate the segment
  39.      * descriptor for the segment in the GDT or LDT and reads it into
  40.      * the processor. (This step is needed only when a new segment selector
  41.      * is loaded into a segment register.) */
  42.     if ((la.sel >> 2) & 0x1)
  43.         desc = &current->ldt[la.sel >> 3];
  44.     else
  45.         desc = &gdt[la.sel >> 3];
  46.     /* Examines the segment descriptor to check the access rights and range
  47.      * of the segment to insure that the segment is accessible and that
  48.      * offset is within the limit of the segment */
  49.     base  = get_base(*desc);
  50.     limit = get_limit(*desc) * 4096;
  51.     if (la.offset > limit)
  52.         panic("Out of segment\n");
  53.     cpl = cs & 0x3;
  54.     dpl = desc->b >> 13 & 0x3;
  55.     if (cpl > dpl)
  56.         panic("Trigger #GP");

  57.     /* Obtain linear address on flat-protect model */
  58.     linear = base + la.offset;
  59.    
  60.     /* Obtain physical address of pgdir from CR3 register, the contents of
  61.      * 'cr3' is point to the physical of pg_dir, so refers it as a pointer. */
  62.     __asm__ ("movl %%cr3, %0" : "=r" (cr3));

  63.     /* A 4-KByte naturally aligned page directory is located at the
  64.      * physical address specified in bits 31:12 of CR3. A page directory
  65.      * comprises 1024 32-bit entries (PDEs). A PDE is selected using
  66.      * the physical address defined as follow:
  67.      *
  68.      * -- Bits 39:32 are all 0
  69.      * -- Bits 31:12 are from CR3.
  70.      * -- Bits 11:2  are bits 31:22 of the linear address
  71.      * -- Bits  1:0  are 0.
  72.      *
  73.      * Becasue a PDE is identified using bits 31:22 of the linear address,
  74.      * it control access to a 4-Mbytes region of the linear-address space.
  75.      * Use of the PDE depends on CR4.PSE and PDE's PS flag (bit 7)*/
  76.     PDE =  ((unsigned char)(((cr3 >> 12) & 0xFFFFF) << 12) +
  77.            (((linear >> 22) & 0x3FF) << 2)) & 0xFFFFFFFC;
  78.     /* Another way to compute PDE:
  79.      * PDE = (unsigned long *)cr3[linear >> 22] */

  80.     __asm__ ("movl %%cr4, %0" : "=r" (cr4));
  81.     if (((cr4 >> 4) & 0x1) && ((PDE >> 4) & 0x1)) {
  82.         /* If CR4.PSE = 1 and the PDE's PS flag is 1, the PDE maps a 4-MByte
  83.          * page. The final physical address is computed as follows:
  84.          *
  85.          * -- Bits 39:32 are bits 20:13 of the PDE.
  86.          * -- Bits 31:22 are bits 31:22 of the PDE.
  87.          * -- Bits 21:0  are from the original linear address.
  88.          */
  89.         PTE = 0x0; /* No complete on BiscuitOS */
  90.     } else if (!((cr4 >> 4) & 0x1) && !((PDE >> 4) & 0x1)) {
  91.         /* If CR4.PSE = 0 or the PDE's PS flag is 0, a 4-KByte naturally
  92.          * aligned page table is located at the physical address specified
  93.          * in bits 31:12 of the PDE. A page table comprises 1024 32-bit
  94.          * entries (PTEs). A PTE is selected using the physical address
  95.          * defined as follows:
  96.          *
  97.          * -- Bits 39:32 are all 0
  98.          * -- Bits 31:12 are from the PDE.
  99.          * -- Bits 11:2  are bits 21:12 of the linear address.
  100.          * -- Bits  1:0  are 0.
  101.          */
  102.         PTE = (unsigned long)((unsigned char *)(
  103.               ((*(unsigned long *)PDE >> 12) & 0xFFFFF) << 12) +
  104.               (((linear >> 12) & 0x3FF) << 2)) & 0xFFFFFFFC;
  105.         /* Anther way to compute PTE:
  106.          * PTE = (unsigned long *)PDE[(linear >> 12) & 0x3FF] */
  107.     }
  108.     /* Because a PTE is identified using bits 31:12 of the linear address,
  109.      * every PTE maps a 4-KByte page. The final physical address is
  110.      * computed as follows:
  111.      *
  112.      * -- Bits 39:32 are all 0
  113.      * -- Bits 31:12 are from the PTE.
  114.      * -- Bits 11:0  are from the original linear address.
  115.      */
  116.     physic = (unsigned long)(unsigned char *)(
  117.              ((*(unsigned long *)PTE >> 12) & 0xFFFFF) << 12) +
  118.              (linear & 0xFFF);

  119.     printk("Logical Address: %#x:%#x\n", la.sel, la.offset);
  120.     printk("Virtual Address: %#x\n", virtual);
  121.     printk("Linear  Address: %#x\n", linear);
  122.     printk("Physic  Address: %#x\n", physic);
  123.     printk("Original:  %s\n", la.offset);
  124.     printk("Translate: %s\n", physic);
  125. }
复制代码

评分

参与人数 1可用积分 +8 收起 理由
Godbach + 8 赞一个!

查看全部评分

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
2 [报告]
发表于 2018-01-23 17:12 |只看该作者
赞,学习了!

43.    /* violation COW */
44.    var[48] = 'A';

这两行的目的是什么?

论坛徽章:
9
程序设计版块每日发帖之星
日期:2016-02-11 06:20:00程序设计版块每日发帖之星
日期:2016-02-14 06:20:00程序设计版块每日发帖之星
日期:2016-02-14 06:20:0015-16赛季CBA联赛之吉林
日期:2016-03-23 17:25:0015-16赛季CBA联赛之浙江
日期:2016-04-01 08:25:0615-16赛季CBA联赛之山西
日期:2016-04-01 10:09:1915-16赛季CBA联赛之广夏
日期:2016-06-03 15:58:212016科比退役纪念章
日期:2016-07-28 17:42:5215-16赛季CBA联赛之广东
日期:2017-02-20 23:32:43
3 [报告]
发表于 2018-01-25 16:37 |只看该作者
回复 2# nswcfd

我的测试代码运行的 x86 环境是配置成回写的,也就是写时复制,对指定地址进行一次写操作防止缺页发生,我当时加这行代码的时候是这么想的,也是在看 Intel 手册上有说过这种情况。

论坛徽章:
13
15-16赛季CBA联赛之八一
日期:2016-07-08 21:00:1415-16赛季CBA联赛之同曦
日期:2017-02-15 14:26:1515-16赛季CBA联赛之佛山
日期:2017-02-20 14:19:2615-16赛季CBA联赛之青岛
日期:2017-05-07 16:49:1115-16赛季CBA联赛之广夏
日期:2017-07-30 09:13:1215-16赛季CBA联赛之广东
日期:2018-07-05 22:34:3615-16赛季CBA联赛之江苏
日期:2018-09-03 12:10:2115-16赛季CBA联赛之上海
日期:2018-09-25 03:49:2215-16赛季CBA联赛之广东
日期:2018-09-25 04:09:12
4 [报告]
发表于 2018-01-26 08:54 |只看该作者
nice, zan !

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
5 [报告]
发表于 2018-02-02 21:06 |只看该作者
回复 3# Buddy_Zhang1

多谢,了解了。

论坛徽章:
2
2016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之广夏
日期:2016-12-07 08:32:11
6 [报告]
发表于 2018-03-23 16:21 |只看该作者
原来我也是这么认为的,不过现在我认为线性地址和虚拟地址是一个东西,前者是intel术语,后者是内核术语,不必区分。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP