From 3c0e77a35690dc10c03e31b82244f3f5848cbed0 Mon Sep 17 00:00:00 2001 From: rain Date: Mon, 18 Jul 2022 23:57:05 +0000 Subject: [PATCH 1/2] add C. --- ...47\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" diff --git "a/C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" "b/C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" new file mode 100644 index 0000000..e69de29 -- Gitee From abaf11940aae5ab9d00640bcd2c7dc516692577d Mon Sep 17 00:00:00 2001 From: rain Date: Mon, 18 Jul 2022 23:59:36 +0000 Subject: [PATCH 2/2] =?UTF-8?q?add=20=E6=97=A0=E6=95=8C=E7=A6=BB=E8=B0=B1?= =?UTF-8?q?=E4=BD=8D=E8=BF=90=E7=AE=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...61\344\275\215\350\277\220\347\256\227.md" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git "a/C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" "b/C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" index e69de29..dc78eec 100644 --- "a/C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" +++ "b/C/\346\227\240\346\225\214\347\246\273\350\260\261\344\275\215\350\277\220\347\256\227.md" @@ -0,0 +1,77 @@ +某科学的玩具操作系统中的一串用来映射到虚拟内存的代码: +```c + +/* map page s (virtual) to d... okay, what am i doing? */ + +void *__mapto(void *s, void *d, int attr) { +#if CONFIG_64BIT + __archptr_t *lvl1, *lvl2, *lvl3, *p; + __archptr_t dest_addr; + + dest_addr = (__archptr_t) d; + + lvl1 = (__archptr_t *) LVL1_PT[(dest_addr >> 39) & 4095]; + + if (!lvl1) { + if (!(lvl1 = early_kmalloc(4096, 4096))) + panic("mapto: Out of memory\n"); + + LVL1_PT[(dest_addr >> 39) & 4095] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR; + memset(lvl1, 0, 4096); + } + + p = (__archptr_t *) ((__archptr_t) lvl1 & ~0xfffu); + lvl2 = (__archptr_t *) p[(dest_addr >> 30) & 4095]; + + if (!lvl2) { + if (!(lvl2 = early_kmalloc(4096, 4096))) + panic("mapto: Out of memory\n"); + + p[(dest_addr >> 30) & 4095] = (__archptr_t) lvl2 | DEFAULT_PAGE_ATTR; + memset(lvl2, 0, 4096); + } + + lvl3 = (__archptr_t *) ((__archptr_t) lvl2 & ~0xfffu); + + if (s) + lvl3[(dest_addr >> 21) & 4095] = (__archptr_t) s | attr; + + else + lvl3[(dest_addr >> 21) & 4095] &= ~0xfffu | attr; + + return d; +#else + + __archptr_t dest_addr = (__archptr_t) d; + __archptr_t *lvl1; + __archptr_t *lvl2; + + dest_addr &= ~PAGE_MASK; + + /* 10 lvl1 10 lvl2 12 offset */ + lvl1 = (__archptr_t *) LVL1_PT[dest_addr >> 22]; + + if (!lvl1) { + if (!(lvl1 = early_kmalloc(4096, 4096))) + panic("mapto: Out of memory\n"); + + LVL1_PT[dest_addr >> 22] = (__archptr_t) lvl1 | DEFAULT_PAGE_ATTR; + memset(lvl1, 0, 4096); + } + + lvl2 = (__archptr_t *) ((__archptr_t) lvl1 & ~0xffu); + + if (!s) + /* clear attributes and then set */ + + lvl2[(dest_addr >> 12) & 1023] &= ~0xff | attr; + + else + lvl2[(dest_addr >> 12) & 1023] = (__archptr_t) s | attr; + + + return (void *) dest_addr; +#endif +} + +``` \ No newline at end of file -- Gitee