diff --git a/kernel/common/kprint.h b/kernel/common/kprint.h index c781d183..62790801 100644 --- a/kernel/common/kprint.h +++ b/kernel/common/kprint.h @@ -72,7 +72,7 @@ { \ printk("[ "); \ printk_color(RED, BLACK, "BUG"); \ - printk(" ] "); \ + printk(" ] (%s:%d)\t", __FILE__, __LINE__); \ printk(__VA_ARGS__); \ printk("\n"); \ } while (0); diff --git a/kernel/driver/interrupt/apic/apic.c b/kernel/driver/interrupt/apic/apic.c index bde8e0f7..c32d5e55 100644 --- a/kernel/driver/interrupt/apic/apic.c +++ b/kernel/driver/interrupt/apic/apic.c @@ -494,6 +494,12 @@ void do_IRQ(struct pt_regs *rsp, ul number) // 检测是否有未处理的软中断 if (softirq_status != 0) do_softirq(); + // 检测当前进程是否持有自旋锁,若持有自旋锁,则不进行抢占式的进程调度 + if (current_pcb->preempt_count > 0) + return; + else if (current_pcb->preempt_count < 0) + kBUG("current_pcb->preempt_count<0! pid=%d", current_pcb->pid); // should not be here + // 检测当前进程是否可被调度 if (current_pcb->flags & PROC_NEED_SCHED) { diff --git a/kernel/exception/trap.c b/kernel/exception/trap.c index 5fe0c8f6..3cad74c5 100644 --- a/kernel/exception/trap.c +++ b/kernel/exception/trap.c @@ -169,7 +169,7 @@ void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code) void do_general_protection(struct pt_regs *regs, unsigned long error_code) { - hlt(); + kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\n", error_code, regs->rsp, regs->rip); if (error_code & 0x01) printk_color(RED, BLACK, "The exception occurred during delivery of an event external to the program,such as an interrupt or an earlier exception.\n"); @@ -197,7 +197,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long error_code) unsigned long cr2 = 0; __asm__ __volatile__("movq %%cr2, %0":"=r"(cr2)::"memory"); - hlt(); + kerror("do_page_fault(14),Error code :%#018lx,RSP:%#018lx,RIP:%#018lx\n",error_code , regs->rsp , regs->rip); if(!(error_code & 0x01)) diff --git a/kernel/main.c b/kernel/main.c index 47bfc690..51b3bd1d 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -53,97 +53,6 @@ void show_welcome() printk_color(0x00e0ebeb, 0x00e0ebeb, " \n\n"); } -// 测试内存管理单元 - -void test_mm() -{ - kinfo("Testing memory management unit..."); - struct Page *page = NULL; - page = alloc_pages(ZONE_NORMAL, 63, 0); - page = alloc_pages(ZONE_NORMAL, 63, 0); - - printk_color(ORANGE, BLACK, "4.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tmemory_management_struct.bmp+2:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1), *(memory_management_struct.bmp + 2), memory_management_struct.zones_struct->count_pages_using, memory_management_struct.zones_struct->count_pages_free); - - for (int i = 80; i <= 85; ++i) - { - printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\t", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys); - i++; - printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\n", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys); - } - - for (int i = 140; i <= 145; i++) - { - printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\t", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys); - i++; - printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\n", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys); - } - - free_pages(page, 1); - - printk_color(ORANGE, BLACK, "5.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tmemory_management_struct.bmp+2:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1), *(memory_management_struct.bmp + 2), memory_management_struct.zones_struct->count_pages_using, memory_management_struct.zones_struct->count_pages_free); - - for (int i = 75; i <= 85; i++) - { - printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\t", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys); - i++; - printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\n", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys); - } - - page = alloc_pages(ZONE_UNMAPPED_IN_PGT, 63, 0); - - printk_color(ORANGE, BLACK, "6.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *(memory_management_struct.bmp + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), *(memory_management_struct.bmp + 1 + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_using, (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_free); - - free_pages(page, 1); - - printk_color(ORANGE, BLACK, "7.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *(memory_management_struct.bmp + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), *(memory_management_struct.bmp + 1 + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_using, (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_free); - - test_slab(); - kinfo("Memory management module test completed!"); -} - -void test_slab() -{ - kinfo("Testing SLAB..."); - kinfo("Testing kmalloc()..."); - - for (int i = 1; i < 16; ++i) - { - printk_color(ORANGE, BLACK, "mem_obj_size: %ldbytes\t", kmalloc_cache_group[i].size); - printk_color(ORANGE, BLACK, "bmp(before): %#018lx\t", *kmalloc_cache_group[i].cache_pool_entry->bmp); - - ul *tmp = kmalloc(kmalloc_cache_group[i].size, 0); - if (tmp == NULL) - { - kBUG("Cannot kmalloc such a memory: %ld bytes", kmalloc_cache_group[i].size); - } - - printk_color(ORANGE, BLACK, "bmp(middle): %#018lx\t", *kmalloc_cache_group[i].cache_pool_entry->bmp); - - kfree(tmp); - - printk_color(ORANGE, BLACK, "bmp(after): %#018lx\n", *kmalloc_cache_group[i].cache_pool_entry->bmp); - } - - // 测试自动扩容 - void *ptrs[7]; - for (int i = 0; i < 7; ++i) - ptrs[i] = kmalloc(kmalloc_cache_group[15].size, 0); - - struct slab_obj *slab_obj_ptr = kmalloc_cache_group[15].cache_pool_entry; - int count = 0; - do - { - kdebug("bmp(%d): addr=%#018lx\t value=%#018lx", count, slab_obj_ptr->bmp, *slab_obj_ptr->bmp); - - slab_obj_ptr = container_of(list_next(&slab_obj_ptr->list), struct slab_obj, list); - ++count; - } while (slab_obj_ptr != kmalloc_cache_group[15].cache_pool_entry); - - for (int i = 0; i < 7; ++i) - kfree(ptrs[i]); - - kinfo("SLAB test completed!"); -} struct gdtr gdtp; struct idtr idtp; void reload_gdt() @@ -151,8 +60,6 @@ void reload_gdt() gdtp.size = bsp_gdt_size-1; gdtp.gdt_vaddr = (ul)phys_2_virt((ul)&GDT_Table); - //kdebug("gdtvaddr=%#018lx", p.gdt_vaddr); - //kdebug("gdt size=%d", p.size); asm volatile("lgdt (%0) \n\t" ::"r"(&gdtp) : "memory"); @@ -230,8 +137,11 @@ void system_initialize() HPET_init(); - - while(1); + show_welcome(); + while(1) + { + printk_color(ORANGE, BLACK, "Initial_proc\n"); + } } //操作系统内核从这里开始执行 diff --git a/kernel/mm/slab.c b/kernel/mm/slab.c index a6023097..4aa20194 100644 --- a/kernel/mm/slab.c +++ b/kernel/mm/slab.c @@ -1,5 +1,27 @@ #include "slab.h" + + +struct slab kmalloc_cache_group[16] = + { + {32, 0, 0, NULL, NULL, NULL, NULL}, + {64, 0, 0, NULL, NULL, NULL, NULL}, + {128, 0, 0, NULL, NULL, NULL, NULL}, + {256, 0, 0, NULL, NULL, NULL, NULL}, + {512, 0, 0, NULL, NULL, NULL, NULL}, + {1024, 0, 0, NULL, NULL, NULL, NULL}, // 1KB + {2048, 0, 0, NULL, NULL, NULL, NULL}, + {4096, 0, 0, NULL, NULL, NULL, NULL}, // 4KB + {8192, 0, 0, NULL, NULL, NULL, NULL}, + {16384, 0, 0, NULL, NULL, NULL, NULL}, + {32768, 0, 0, NULL, NULL, NULL, NULL}, + {65536, 0, 0, NULL, NULL, NULL, NULL}, + {131072, 0, 0, NULL, NULL, NULL, NULL}, // 128KB + {262144, 0, 0, NULL, NULL, NULL, NULL}, + {524288, 0, 0, NULL, NULL, NULL, NULL}, + {1048576, 0, 0, NULL, NULL, NULL, NULL}, // 1MB +}; + /** * @brief 创建一个内存池 * diff --git a/kernel/mm/slab.h b/kernel/mm/slab.h index 4aa4431f..053d024a 100644 --- a/kernel/mm/slab.h +++ b/kernel/mm/slab.h @@ -46,7 +46,7 @@ struct slab void *(*constructor)(void *vaddr, ul arg); void *(*destructor)(void *vaddr, ul arg); }; - +//extern struct slab kmalloc_cache_group[16]; /** * @brief 通用内存分配函数 * @@ -119,22 +119,4 @@ struct slab_obj * kmalloc_create_slab_obj(ul size); */ ul slab_init(); -struct slab kmalloc_cache_group[16] = - { - {32, 0, 0, NULL, NULL, NULL, NULL}, - {64, 0, 0, NULL, NULL, NULL, NULL}, - {128, 0, 0, NULL, NULL, NULL, NULL}, - {256, 0, 0, NULL, NULL, NULL, NULL}, - {512, 0, 0, NULL, NULL, NULL, NULL}, - {1024, 0, 0, NULL, NULL, NULL, NULL}, // 1KB - {2048, 0, 0, NULL, NULL, NULL, NULL}, - {4096, 0, 0, NULL, NULL, NULL, NULL}, // 4KB - {8192, 0, 0, NULL, NULL, NULL, NULL}, - {16384, 0, 0, NULL, NULL, NULL, NULL}, - {32768, 0, 0, NULL, NULL, NULL, NULL}, - {65536, 0, 0, NULL, NULL, NULL, NULL}, - {131072, 0, 0, NULL, NULL, NULL, NULL}, // 128KB - {262144, 0, 0, NULL, NULL, NULL, NULL}, - {524288, 0, 0, NULL, NULL, NULL, NULL}, - {1048576, 0, 0, NULL, NULL, NULL, NULL}, // 1MB -}; \ No newline at end of file +