|  | @@ -1,6 +1,8 @@
 | 
	
		
			
				|  |  |  #include "slab.h"
 | 
	
		
			
				|  |  |  #include <common/compiler.h>
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +#pragma GCC push_options
 | 
	
		
			
				|  |  | +#pragma GCC optimize("O0")
 | 
	
		
			
				|  |  |  struct slab kmalloc_cache_group[16] =
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          {32, 0, 0, NULL, NULL, NULL, NULL},
 | 
	
	
		
			
				|  | @@ -348,9 +350,9 @@ ul slab_init()
 | 
	
		
			
				|  |  |      kinfo("Initializing SLAB...");
 | 
	
		
			
				|  |  |      // 将slab的内存池空间放置在mms的后方
 | 
	
		
			
				|  |  |      ul tmp_addr = memory_management_struct.end_of_struct;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      for (int i = 0; i < 16; ++i)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        io_mfence();
 | 
	
		
			
				|  |  |          spin_init(&kmalloc_cache_group[i].lock);
 | 
	
		
			
				|  |  |          // 将slab内存池对象的空间放置在mms的后面,并且预留4个unsigned long 的空间以防止内存越界
 | 
	
		
			
				|  |  |          kmalloc_cache_group[i].cache_pool_entry = (struct slab_obj *)memory_management_struct.end_of_struct;
 | 
	
	
		
			
				|  | @@ -370,7 +372,7 @@ ul slab_init()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // bmp后方预留4个unsigned long的空间防止内存越界,且按照8byte进行对齐
 | 
	
		
			
				|  |  |          memory_management_struct.end_of_struct = (ul)(memory_management_struct.end_of_struct + kmalloc_cache_group[i].cache_pool_entry->bmp_len + (sizeof(ul) << 2)) & (~(sizeof(ul) - 1));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        io_mfence();
 | 
	
		
			
				|  |  |          // @todo:此处可优化,直接把所有位设置为0,然后再对部分不存在对应的内存对象的位设置为1
 | 
	
		
			
				|  |  |          memset(kmalloc_cache_group[i].cache_pool_entry->bmp, 0xff, kmalloc_cache_group[i].cache_pool_entry->bmp_len);
 | 
	
		
			
				|  |  |          for (int j = 0; j < kmalloc_cache_group[i].cache_pool_entry->bmp_count; ++j)
 | 
	
	
		
			
				|  | @@ -378,6 +380,7 @@ ul slab_init()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          kmalloc_cache_group[i].count_total_using = 0;
 | 
	
		
			
				|  |  |          kmalloc_cache_group[i].count_total_free = kmalloc_cache_group[i].cache_pool_entry->count_free;
 | 
	
		
			
				|  |  | +        io_mfence();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      struct Page *page = NULL;
 | 
	
	
		
			
				|  | @@ -388,13 +391,17 @@ ul slab_init()
 | 
	
		
			
				|  |  |      ul page_num = 0;
 | 
	
		
			
				|  |  |      for (int i = PAGE_2M_ALIGN(virt_2_phys(tmp_addr)) >> PAGE_2M_SHIFT; i <= tmp_page_mms_end; ++i)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          page = memory_management_struct.pages_struct + i;
 | 
	
		
			
				|  |  |          page_num = page->addr_phys >> PAGE_2M_SHIFT;
 | 
	
		
			
				|  |  |          *(memory_management_struct.bmp + (page_num >> 6)) |= (1UL << (page_num % 64));
 | 
	
		
			
				|  |  |          ++page->zone->count_pages_using;
 | 
	
		
			
				|  |  | +        io_mfence();
 | 
	
		
			
				|  |  |          --page->zone->count_pages_free;
 | 
	
		
			
				|  |  |          page_init(page, PAGE_KERNEL_INIT | PAGE_KERNEL | PAGE_PGT_MAPPED);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +    io_mfence();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      // 为slab内存池对象分配内存空间
 | 
	
		
			
				|  |  |      ul *virt = NULL;
 | 
	
	
		
			
				|  | @@ -406,8 +413,11 @@ ul slab_init()
 | 
	
		
			
				|  |  |          page = Virt_To_2M_Page(virt);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          page_num = page->addr_phys >> PAGE_2M_SHIFT;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          *(memory_management_struct.bmp + (page_num >> 6)) |= (1UL << (page_num % 64));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          ++page->zone->count_pages_using;
 | 
	
		
			
				|  |  | +        io_mfence();    // 该位置必须加一个mfence,否则O3优化运行时会报错
 | 
	
		
			
				|  |  |          --page->zone->count_pages_free;
 | 
	
		
			
				|  |  |          page_init(page, PAGE_PGT_MAPPED | PAGE_KERNEL | PAGE_KERNEL_INIT);
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -696,4 +706,5 @@ unsigned long kfree(void *address)
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      kBUG("kfree(): Can't free memory.");
 | 
	
		
			
				|  |  |      return ECANNOT_FREE_MEM;
 | 
	
		
			
				|  |  | -}
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +#pragma GCC pop_options
 |