免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: Buddy_Zhang1
打印 上一主题 下一主题

[内核入门] Welcome to BiscuitOS [复制链接]

论坛徽章:
154
2022北京冬奥会纪念版徽章
日期:2015-08-07 17:10:5720周年集字徽章-年
日期:2022-10-26 16:44:2015-16赛季CBA联赛之深圳
日期:2022-11-02 14:02:4515-16赛季CBA联赛之八一
日期:2022-11-28 12:07:4820周年集字徽章-20	
日期:2023-07-19 08:49:4515-16赛季CBA联赛之八一
日期:2023-11-04 19:23:5115-16赛季CBA联赛之广夏
日期:2023-12-13 18:09:34
11 [报告]
发表于 2017-12-18 12:45 |只看该作者
是不是又来造轮子了,,,才4个人就可以了

论坛徽章:
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
12 [报告]
发表于 2017-12-19 10:34 |只看该作者
回复 11# shang2010

人不在数量,在于质量

论坛徽章:
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
13 [报告]
发表于 2018-01-19 15:06 |只看该作者
BiscuitOS 内核(linux0.11) 关于逻辑地址,虚拟地址,线性地址和物理地址的转换过程:

  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. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP