mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-13 11:06:46 +00:00
删除了一些信息的打印
This commit is contained in:
parent
98e62e1e19
commit
49eaf6ce95
@ -26,18 +26,17 @@
|
|||||||
|
|
||||||
// 定义类型的缩写
|
// 定义类型的缩写
|
||||||
typedef unsigned long ul;
|
typedef unsigned long ul;
|
||||||
typedef unsigned long long ull;
|
typedef unsigned long long int ull;
|
||||||
typedef long long ll;
|
typedef long long int ll;
|
||||||
|
|
||||||
#define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
|
#define ABS(x) ((x) > 0 ? (x) : -(x)) // 绝对值
|
||||||
|
|
||||||
// 四舍五入成整数
|
// 四舍五入成整数
|
||||||
ul round(double x)
|
ul round(double x)
|
||||||
{
|
{
|
||||||
return (ul)(x+0.5);
|
return (ul)(x + 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//链表数据结构
|
//链表数据结构
|
||||||
struct List
|
struct List
|
||||||
{
|
{
|
||||||
@ -112,11 +111,9 @@ static inline int strlen(char *s)
|
|||||||
return __res;
|
return __res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void *memset(void *dst, unsigned char C, ul Count)
|
void *memset(void *dst, unsigned char C, ul Count)
|
||||||
{
|
{
|
||||||
|
|
||||||
int d0, d1;
|
int d0, d1;
|
||||||
unsigned long tmp = C * 0x0101010101010101UL;
|
unsigned long tmp = C * 0x0101010101010101UL;
|
||||||
__asm__ __volatile__("cld \n\t"
|
__asm__ __volatile__("cld \n\t"
|
||||||
@ -140,7 +137,7 @@ void *memset(void *dst, unsigned char C, ul Count)
|
|||||||
void *memset_c(void *dst, unsigned char c, ul n)
|
void *memset_c(void *dst, unsigned char c, ul n)
|
||||||
{
|
{
|
||||||
unsigned char *s = (unsigned char *)dst;
|
unsigned char *s = (unsigned char *)dst;
|
||||||
for(int i=0;i<n;++i)
|
for (int i = 0; i < n; ++i)
|
||||||
s[i] = c;
|
s[i] = c;
|
||||||
return dst;
|
return dst;
|
||||||
}
|
}
|
@ -316,7 +316,7 @@ static int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
case 'X':
|
case 'X':
|
||||||
//flags |= SPECIAL;
|
//flags |= SPECIAL;
|
||||||
if (qualifier == 'l')
|
if (qualifier == 'l')
|
||||||
str = write_num(str, va_arg(args, long long), 16, field_width, precision, flags);
|
str = write_num(str, va_arg(args, ull), 16, field_width, precision, flags);
|
||||||
else
|
else
|
||||||
str = write_num(str, va_arg(args, int), 16, field_width, precision, flags);
|
str = write_num(str, va_arg(args, int), 16, field_width, precision, flags);
|
||||||
break;
|
break;
|
||||||
@ -379,7 +379,7 @@ static int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
return str - buf;
|
return str - buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *write_num(char *str, long long num, int base, int field_width, int precision, int flags)
|
static char *write_num(char *str, ull num, int base, int field_width, int precision, int flags)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @brief 将数字按照指定的要求转换成对应的字符串
|
* @brief 将数字按照指定的要求转换成对应的字符串
|
||||||
|
@ -92,7 +92,7 @@ static int vsprintf(char *buf, const char *fmt, va_list args);
|
|||||||
* @param precision 精度
|
* @param precision 精度
|
||||||
* @param flags 标志位
|
* @param flags 标志位
|
||||||
*/
|
*/
|
||||||
static char* write_num(char *str, long long num, int base, int field_width, int precision, int flags);
|
static char* write_num(char *str, ull num, int base, int field_width, int precision, int flags);
|
||||||
|
|
||||||
|
|
||||||
static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags);
|
static char *write_float_point_num(char *str, double num, int field_width, int precision, int flags);
|
||||||
|
@ -66,7 +66,7 @@ void test_printk()
|
|||||||
void test_mm()
|
void test_mm()
|
||||||
{
|
{
|
||||||
kinfo("Testing memory management unit...");
|
kinfo("Testing memory management unit...");
|
||||||
printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *memory_management_struct.bmp + 1);
|
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1));
|
||||||
kinfo("Try to allocate 64 memory pages.");
|
kinfo("Try to allocate 64 memory pages.");
|
||||||
struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
|
struct Page *page = alloc_pages(ZONE_NORMAL, 64, PAGE_PGT_MAPPED | PAGE_ACTIVE | PAGE_KERNEL);
|
||||||
for (int i = 0; i <= 65; ++i)
|
for (int i = 0; i <= 65; ++i)
|
||||||
@ -76,7 +76,8 @@ void test_mm()
|
|||||||
if (((i + 1) % 2) == 0)
|
if (((i + 1) % 2) == 0)
|
||||||
printk("\n");
|
printk("\n");
|
||||||
}
|
}
|
||||||
printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *memory_management_struct.bmp, *memory_management_struct.bmp + 1);
|
|
||||||
|
//printk("bmp[0]:%#018x\tbmp[1]%#018lx\n", *(memory_management_struct.bmp), *(memory_management_struct.bmp + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void init()
|
void init()
|
||||||
@ -96,7 +97,7 @@ void init()
|
|||||||
|
|
||||||
//asm volatile(" fxsave %0 " ::"m"(fxsave_region));
|
//asm volatile(" fxsave %0 " ::"m"(fxsave_region));
|
||||||
// 初始化内存管理单元
|
// 初始化内存管理单元
|
||||||
printk("[ DragonOS ] Initializing memory manage unit...\n");
|
|
||||||
mm_init();
|
mm_init();
|
||||||
}
|
}
|
||||||
//操作系统内核从这里开始执行
|
//操作系统内核从这里开始执行
|
||||||
|
@ -1,20 +1,18 @@
|
|||||||
#include "mm.h"
|
#include "mm.h"
|
||||||
#include "../common/printk.h"
|
#include "../common/printk.h"
|
||||||
|
#include "../common/kprint.h"
|
||||||
|
|
||||||
ul Total_Memory = 0;
|
ul Total_Memory = 0;
|
||||||
ul total_2M_pages = 0;
|
ul total_2M_pages = 0;
|
||||||
|
|
||||||
void mm_init()
|
void mm_init()
|
||||||
{
|
{
|
||||||
printk("[ INFO ] Initializing memory management unit...\n");
|
kinfo("Initializing memory management unit...");
|
||||||
// 实模式下获取到的信息的起始地址,转换为ARDS指针
|
// 实模式下获取到的信息的起始地址,转换为ARDS指针
|
||||||
struct ARDS *ards_ptr = (struct ARDS *)0xffff800000007e00;
|
struct ARDS *ards_ptr = (struct ARDS *)0xffff800000007e00;
|
||||||
|
|
||||||
for (int i = 0; i < 32; ++i)
|
for (int i = 0; i < 32; ++i)
|
||||||
{
|
{
|
||||||
//printk("Addr = %#18lx\tLength = %#18lx\tType = %#10lx\n",
|
|
||||||
// ards_ptr->BaseAddr, ards_ptr->Length, ards_ptr->type);
|
|
||||||
|
|
||||||
//可用的内存
|
//可用的内存
|
||||||
if (ards_ptr->type == 1)
|
if (ards_ptr->type == 1)
|
||||||
Total_Memory += ards_ptr->Length;
|
Total_Memory += ards_ptr->Length;
|
||||||
@ -51,7 +49,7 @@ void mm_init()
|
|||||||
|
|
||||||
total_2M_pages += ((addr_end - addr_start) >> PAGE_2M_SHIFT);
|
total_2M_pages += ((addr_end - addr_start) >> PAGE_2M_SHIFT);
|
||||||
}
|
}
|
||||||
printk("[ INFO ] Total amounts of 2M pages : %ld.\n", total_2M_pages);
|
kinfo("Total amounts of 2M pages : %ld.", total_2M_pages);
|
||||||
|
|
||||||
// 设置内核程序不同部分的起止地址
|
// 设置内核程序不同部分的起止地址
|
||||||
memory_management_struct.kernel_code_start = (ul)&_text;
|
memory_management_struct.kernel_code_start = (ul)&_text;
|
||||||
@ -147,20 +145,21 @@ void mm_init()
|
|||||||
// 计算zone结构体的总长度(按照64位对齐)
|
// 计算zone结构体的总长度(按照64位对齐)
|
||||||
memory_management_struct.zones_struct_len = (memory_management_struct.count_zones * sizeof(struct Zone) + sizeof(ul) - 1) & (~(sizeof(ul) - 1));
|
memory_management_struct.zones_struct_len = (memory_management_struct.count_zones * sizeof(struct Zone) + sizeof(ul) - 1) & (~(sizeof(ul) - 1));
|
||||||
|
|
||||||
|
/*
|
||||||
printk_color(ORANGE, BLACK, "bmp:%#18lx, bmp_len:%#18lx, bits_size:%#18lx\n", memory_management_struct.bmp, memory_management_struct.bmp_len, memory_management_struct.bits_size);
|
printk_color(ORANGE, BLACK, "bmp:%#18lx, bmp_len:%#18lx, bits_size:%#18lx\n", memory_management_struct.bmp, memory_management_struct.bmp_len, memory_management_struct.bits_size);
|
||||||
|
|
||||||
printk_color(ORANGE, BLACK, "pages_struct:%#18lx, count_pages:%#18lx, pages_struct_len:%#18lx\n", memory_management_struct.pages_struct, memory_management_struct.count_pages, memory_management_struct.pages_struct_len);
|
printk_color(ORANGE, BLACK, "pages_struct:%#18lx, count_pages:%#18lx, pages_struct_len:%#18lx\n", memory_management_struct.pages_struct, memory_management_struct.count_pages, memory_management_struct.pages_struct_len);
|
||||||
|
|
||||||
printk_color(ORANGE, BLACK, "zones_struct:%#18lx, count_zones:%#18lx, zones_struct_len:%#18lx\n", memory_management_struct.zones_struct, memory_management_struct.count_zones, memory_management_struct.zones_struct_len);
|
printk_color(ORANGE, BLACK, "zones_struct:%#18lx, count_zones:%#18lx, zones_struct_len:%#18lx\n", memory_management_struct.zones_struct, memory_management_struct.count_zones, memory_management_struct.zones_struct_len);
|
||||||
|
*/
|
||||||
ZONE_DMA_INDEX = 0; //need rewrite in the future
|
ZONE_DMA_INDEX = 0; //need rewrite in the future
|
||||||
ZONE_NORMAL_INDEX = 0; //need rewrite in the future
|
ZONE_NORMAL_INDEX = 0; //need rewrite in the future
|
||||||
|
|
||||||
for (int i = 0; i < memory_management_struct.count_zones; ++i) //need rewrite in the future
|
for (int i = 0; i < memory_management_struct.count_zones; ++i) //need rewrite in the future
|
||||||
{
|
{
|
||||||
struct Zone *z = memory_management_struct.zones_struct + i;
|
struct Zone *z = memory_management_struct.zones_struct + i;
|
||||||
printk_color(ORANGE, BLACK, "zone_addr_start:%#18lx, zone_addr_end:%#18lx, zone_length:%#18lx, pages_group:%#18lx, count_pages:%#18lx\n",
|
//printk_color(ORANGE, BLACK, "zone_addr_start:%#18lx, zone_addr_end:%#18lx, zone_length:%#18lx, pages_group:%#18lx, count_pages:%#18lx\n",
|
||||||
z->zone_addr_start, z->zone_addr_end, z->zone_length, z->pages_group, z->count_pages);
|
// z->zone_addr_start, z->zone_addr_end, z->zone_length, z->pages_group, z->count_pages);
|
||||||
|
|
||||||
// 1GB以上的内存空间不做映射
|
// 1GB以上的内存空间不做映射
|
||||||
if (z->zone_addr_start == 0x100000000)
|
if (z->zone_addr_start == 0x100000000)
|
||||||
@ -169,8 +168,8 @@ void mm_init()
|
|||||||
// 设置内存页管理结构的地址,预留了一段空间,防止内存越界。
|
// 设置内存页管理结构的地址,预留了一段空间,防止内存越界。
|
||||||
memory_management_struct.end_of_struct = (ul)((ul)memory_management_struct.zones_struct + memory_management_struct.zones_struct_len + sizeof(long) * 32) & (~(sizeof(long) - 1));
|
memory_management_struct.end_of_struct = (ul)((ul)memory_management_struct.zones_struct + memory_management_struct.zones_struct_len + sizeof(long) * 32) & (~(sizeof(long) - 1));
|
||||||
|
|
||||||
printk_color(ORANGE, BLACK, "code_start:%#18lx, code_end:%#18lx, data_end:%#18lx, kernel_end:%#18lx, end_of_struct:%#18lx\n",
|
//printk_color(ORANGE, BLACK, "code_start:%#18lx, code_end:%#18lx, data_end:%#18lx, kernel_end:%#18lx, end_of_struct:%#18lx\n",
|
||||||
memory_management_struct.kernel_code_start, memory_management_struct.kernel_code_end, memory_management_struct.kernel_data_end, memory_management_struct.kernel_end, memory_management_struct.end_of_struct);
|
// memory_management_struct.kernel_code_start, memory_management_struct.kernel_code_end, memory_management_struct.kernel_data_end, memory_management_struct.kernel_end, memory_management_struct.end_of_struct);
|
||||||
|
|
||||||
// 初始化内存管理单元结构所占的物理页的结构体
|
// 初始化内存管理单元结构所占的物理页的结构体
|
||||||
|
|
||||||
@ -183,9 +182,11 @@ void mm_init()
|
|||||||
|
|
||||||
ul *cr3 = get_CR3();
|
ul *cr3 = get_CR3();
|
||||||
|
|
||||||
|
/*
|
||||||
printk_color(INDIGO, BLACK, "cr3:\t%#018lx\n", cr3);
|
printk_color(INDIGO, BLACK, "cr3:\t%#018lx\n", cr3);
|
||||||
printk_color(INDIGO, BLACK, "*cr3:\t%#018lx\n", *(phys_2_virt(cr3)) & (~0xff));
|
printk_color(INDIGO, BLACK, "*cr3:\t%#018lx\n", *(phys_2_virt(cr3)) & (~0xff));
|
||||||
printk_color(INDIGO, BLACK, "**cr3:\t%#018lx\n", *phys_2_virt(*(phys_2_virt(cr3)) & (~0xff)) & (~0xff));
|
printk_color(INDIGO, BLACK, "**cr3:\t%#018lx\n", *phys_2_virt(*(phys_2_virt(cr3)) & (~0xff)) & (~0xff));
|
||||||
|
*/
|
||||||
|
|
||||||
// 消除一致性页表映射,将页目录(PML4E)的前10项清空
|
// 消除一致性页表映射,将页目录(PML4E)的前10项清空
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
@ -193,7 +194,7 @@ void mm_init()
|
|||||||
|
|
||||||
flush_tlb();
|
flush_tlb();
|
||||||
|
|
||||||
printk("[ INFO ] Memory management unit initialized.\n");
|
kinfo("Memory management unit initialize complete!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -211,8 +212,8 @@ unsigned long page_init(struct Page *page, ul flags)
|
|||||||
if (!page->attr)
|
if (!page->attr)
|
||||||
{
|
{
|
||||||
// 将bmp对应的标志位置位
|
// 将bmp对应的标志位置位
|
||||||
*(memory_management_struct.bmp + ((page->addr_phys >> PAGE_2M_SHIFT) >> 6)) |= (1UL << ((page->addr_phys >> PAGE_2M_SHIFT) % 64));
|
|
||||||
|
|
||||||
|
*(memory_management_struct.bmp + ((page->addr_phys >> PAGE_2M_SHIFT) >> 6)) |= 1UL << (page->addr_phys >> PAGE_2M_SHIFT) % 64;
|
||||||
page->attr = flags;
|
page->attr = flags;
|
||||||
++(page->ref_counts);
|
++(page->ref_counts);
|
||||||
++(page->zone->count_pages_using);
|
++(page->zone->count_pages_using);
|
||||||
@ -229,7 +230,9 @@ unsigned long page_init(struct Page *page, ul flags)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 将bmp对应的标志位置位
|
// 将bmp对应的标志位置位
|
||||||
*(memory_management_struct.bmp + ((page->addr_phys >> PAGE_2M_SHIFT) >> 6)) |= (1UL << ((page->addr_phys >> PAGE_2M_SHIFT) % 64));
|
//*(memory_management_struct.bmp + ((page->addr_phys >> PAGE_2M_SHIFT) >> 6)) |= (1UL << ((page->addr_phys >> PAGE_2M_SHIFT) % 64));
|
||||||
|
*(memory_management_struct.bmp + ((page->addr_phys >> PAGE_2M_SHIFT) >> 6)) |= 1UL << (page->addr_phys >> PAGE_2M_SHIFT) % 64;
|
||||||
|
|
||||||
page->attr |= flags;
|
page->attr |= flags;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -263,9 +266,7 @@ struct Page *alloc_pages(unsigned int zone_select, int num, ul flags)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
printk("[ ");
|
kwarn("In alloc_pages: param: zone_select incorrect.");
|
||||||
printk_color(YELLOW, BLACK, "WARN");
|
|
||||||
printk(" ] In alloc_pages: param: zone_select incorrect.\n");
|
|
||||||
// 返回空
|
// 返回空
|
||||||
return NULL;
|
return NULL;
|
||||||
break;
|
break;
|
||||||
@ -292,20 +293,19 @@ struct Page *alloc_pages(unsigned int zone_select, int num, ul flags)
|
|||||||
|
|
||||||
ul shift = j % 64;
|
ul shift = j % 64;
|
||||||
|
|
||||||
for (int k = shift; k < 64 - shift; ++k)
|
for (ul k = shift; k < 64 - shift; ++k)
|
||||||
{
|
{
|
||||||
// 寻找连续num个空页
|
// 寻找连续num个空页
|
||||||
if (!(((*p >> k) | (*(p + 1) << (64 - k))) & (num == 64 ? 0xffffffffffffffffUL : ((1 << num) - 1))))
|
if (!(((*p >> k) | (*(p + 1) << (64 - k))) & (num == 64 ? 0xffffffffffffffffUL : ((1UL << num) - 1))))
|
||||||
{
|
{
|
||||||
ul start_page_num = j + k - shift; // 计算得到要开始获取的内存页的页号(书上的公式有问题,这个是改过之后的版本)
|
ul start_page_num = j + k - 1; // 计算得到要开始获取的内存页的页号(书上的公式有问题,这个是改过之后的版本)
|
||||||
for(int l=0;l<num;++l)
|
for (ul l = 0; l < num; ++l)
|
||||||
{
|
{
|
||||||
struct Page* x = memory_management_struct.pages_struct+start_page_num+l;
|
struct Page *x = memory_management_struct.pages_struct + start_page_num + l;
|
||||||
|
|
||||||
page_init(x, flags);
|
page_init(x, flags);
|
||||||
}
|
}
|
||||||
// 成功分配了页面,返回第一个页面的指针
|
// 成功分配了页面,返回第一个页面的指针
|
||||||
return (struct Page*)(memory_management_struct.pages_struct+start_page_num);
|
return (struct Page *)(memory_management_struct.pages_struct + start_page_num);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user