From b2f4235f394d141bee3f71b3b5ec8b3d8947b876 Mon Sep 17 00:00:00 2001 From: AceVest Date: Thu, 5 Jun 2014 00:14:53 +0800 Subject: [PATCH] add lock into memory system --- drivers/ide.c | 4 ++-- include/irq.h | 23 +++++++++++++++-------- include/system.h | 9 ++++++++- include/task.h | 6 +++++- kernel/fork.c | 2 +- kernel/init.c | 26 ++++++++++++++++++++------ kernel/irq.c | 12 ++++++++++++ kernel/sched.c | 26 ++++++++++++++++++-------- mm/buddy.c | 7 +++++++ mm/slub.c | 31 ++++++++++++++++++++++++++++++- setup/setup.c | 2 +- setup/system.c | 5 +++-- 12 files changed, 122 insertions(+), 31 deletions(-) diff --git a/drivers/ide.c b/drivers/ide.c index edaa0bc..2d3bca3 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -146,7 +146,7 @@ void ide_irq() insl(REG_DATA(0), buf, 512>>2); u16_t *s = (u16_t *) (buf+510); - printk("insw %04x\n", *s); + printk("hard disk data %04x\n", *s); } @@ -218,7 +218,7 @@ void ide_read_identify() void ide_init() { - ide_read_identify(); + //ide_read_identify(); init_pci_controller(PCI_VENDORID_INTEL, 0x2829); init_pci_controller(PCI_VENDORID_INTEL, 0x7010); } diff --git a/include/irq.h b/include/irq.h index 48573e9..d135684 100644 --- a/include/irq.h +++ b/include/irq.h @@ -56,12 +56,19 @@ int request_irq(unsigned int irq, const char *devname, void *dev_id); -static inline int enable_irq(unsigned int irq) -{ - return irq_desc[irq].chip->enable(irq); -} -static inline int disable_irq(unsigned int irq) -{ - return irq_desc[irq].chip->disable(irq); -} +int open_irq(unsigned int irq); +int close_irq(unsigned int irq); + +#define enable_irq() asm("sti") +#define disable_irq() asm("cli") + +#define irq_save(x) __asm__ __volatile__("pushfl; popl %0; cli":"=g"(x)::"memory") + +#define irq_restore(x) do { \ + typecheck(unsigned long, x); \ + __asm__ __volatile__("pushl %0; popfl"::"g"(x):"memory", "cc"); \ +} while(0) + + + #endif //_IRQ_H diff --git a/include/system.h b/include/system.h index b595f55..aa90148 100644 --- a/include/system.h +++ b/include/system.h @@ -39,13 +39,20 @@ #define PT_REGS_ESP 60 #define PT_REGS_SS 64 -#ifndef ASM +#ifndef ASM #include "types.h" #include "printk.h" #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) +#define typecheck(type, x) \ +({ type __dummy; \ + typeof(x) __dummy2; \ + (void)(&__dummy == &__dummy2); \ + 1; \ +}) + void *kmalloc(size_t size, gfp_t gfpflags); void kfree(void *addr); diff --git a/include/task.h b/include/task.h index 6a20e8f..a59e4bd 100644 --- a/include/task.h +++ b/include/task.h @@ -48,6 +48,8 @@ typedef union task_union unsigned long esp; unsigned long eip; + unsigned long weight; + pid_t pid; pid_t ppid; unsigned int state; @@ -79,7 +81,9 @@ static inline task_union *get_current() #define current get_current() -#define ROOT_TSK_PID (0) +#define ROOT_TSK_PID (7) + +#define TASK_INIT_WEIGHT 10 extern ListHead tsk_list; diff --git a/kernel/fork.c b/kernel/fork.c index 61bc292..d593a60 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -88,7 +88,7 @@ int do_fork(pt_regs_t *regs, unsigned long flags) printk("tsk %08x child_regs esp %08x esp0 %08x\n", tsk, tsk->esp, tsk->esp0); tsk->state = TASK_RUNNING; - + tsk->weight= TASK_INIT_WEIGHT; INIT_LIST_HEAD(&tsk->list); // TODO Lock diff --git a/kernel/init.c b/kernel/init.c index a9dbbde..eca9520 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -21,12 +21,14 @@ Desc gdt[NGDT]; char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); +static unsigned int eid = 0; void init_task_entry() { printk("hahahha %s\n", __func__); + unsigned int id = eid++; while(1) { - printk("i"); + printk("%d", id); asm("sti;hlt;"); } } @@ -39,12 +41,24 @@ void root_task_entry() asm("sti;hlt;"); } #endif - pt_regs_t regs; - memset((void*)®s, 0, sizeof(regs)); - regs.edx = (unsigned long) init_task_entry; - int pid = do_fork(®s, FORK_KRNL); + { + pt_regs_t regs; + memset((void*)®s, 0, sizeof(regs)); + regs.edx = (unsigned long) init_task_entry; + int pid = do_fork(®s, FORK_KRNL); + printk("a pid is %d\n", pid); + } + + + { + pt_regs_t regs; + memset((void*)®s, 0, sizeof(regs)); + regs.edx = (unsigned long) init_task_entry; + int pid = do_fork(®s, FORK_KRNL); + printk("b pid is %d\n", pid); + } + - printk("pid is %d\n", pid); while(1) { printk("r"); diff --git a/kernel/irq.c b/kernel/irq.c index 43c63d0..fbee97f 100644 --- a/kernel/irq.c +++ b/kernel/irq.c @@ -110,3 +110,15 @@ int request_irq( unsigned int irq, //printk("irq: %d action:%x\n", irq, p); return 0; } + + + +int open_irq(unsigned int irq) +{ + return irq_desc[irq].chip->enable(irq); +} + +int close_irq(unsigned int irq) +{ + return irq_desc[irq].chip->disable(irq); +} diff --git a/kernel/sched.c b/kernel/sched.c index 0ac9746..ddf5c40 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -23,9 +23,9 @@ task_union root_task __attribute__((__aligned__(PAGE_SIZE))); pid_t get_next_pid() { - static pid_t g_pid = ROOT_TSK_PID; + static pid_t g_pid = ROOT_TSK_PID; - pid_t pid = g_pid; + pid_t pid = g_pid++; return pid; } @@ -60,6 +60,7 @@ void init_root_tsk() root_task.pid = get_next_pid(); root_task.ppid = 0; root_task.state = TASK_RUNNING; + root_task.weight = TASK_INIT_WEIGHT; INIT_LIST_HEAD(&root_task.list); for(i=0; ilist)) + unsigned int max_weight = 0; + + list_for_each(pos, &root_task.list) { p = list_entry(pos, task_union, list); - if(p->state == TASK_RUNNING) + if(p->state != TASK_RUNNING) + continue; + + if(p->weight > max_weight) { + max_weight = p->weight; sel = p; - last_sel = sel; - break; + } + else if(p->weight == 0) + { + p->weight = TASK_INIT_WEIGHT; } } + sel->weight--; + task_union *prev = current; task_union *next = sel; @@ -168,7 +178,7 @@ unsigned long schedule() void debug_sched() { - task_union *p = list_entry(root_task.list.next, task_union, list); + task_union *p = list_entry(current->list.next, task_union, list); p->state = (p->state == TASK_RUNNING) ? TASK_INTERRUPTIBLE : TASK_RUNNING; } diff --git a/mm/buddy.c b/mm/buddy.c index 8567ba8..53b1b1c 100644 --- a/mm/buddy.c +++ b/mm/buddy.c @@ -7,6 +7,7 @@ * ------------------------------------------------------------------------ */ #include +#include #include struct buddy_system @@ -93,7 +94,10 @@ unsigned long alloc_pages(unsigned int gfp_mask, unsigned int order) // gfp_mask // ... + unsigned long flags; + irq_save(flags); page_t *page = __alloc_pages(order); + irq_restore(flags); return (unsigned long) page2va(page); } @@ -143,7 +147,10 @@ void free_pages(unsigned long addr) page_t *page = va2page(addr); + unsigned long flags; + irq_save(flags); __free_pages(page, page->order); + irq_restore(flags); } void dump_buddy_system() diff --git a/mm/slub.c b/mm/slub.c index 5221ad7..ddd0c06 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -9,6 +9,7 @@ #include #include +#include list_head_t slub_caches = LIST_HEAD_INIT(slub_caches); @@ -174,6 +175,9 @@ static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) if(cache == 0) return 0; + unsigned long flags; + irq_save(flags); + if(cache->page == 0 || cache->page->freelist == 0) { cache->page = 0; @@ -186,6 +190,8 @@ static void *slub_alloc(kmem_cache_t *cache, gfp_t gfpflags) cache->page->inuse++; } + irq_restore(flags); + return object; } @@ -211,6 +217,9 @@ static void __slub_free(kmem_cache_t *cache, page_t *page, void *addr) static void slub_free(kmem_cache_t *cache, page_t *page, void *addr) { + unsigned long flags; + irq_save(flags); + void **object = addr; page->inuse--; @@ -224,6 +233,8 @@ static void slub_free(kmem_cache_t *cache, page_t *page, void *addr) { __slub_free(cache, page, addr); } + + irq_restore(flags); } void *kmem_cache_alloc(kmem_cache_t *cache, gfp_t gfpflags) @@ -245,6 +256,9 @@ void *kmalloc(size_t size, gfp_t gfpflags) unsigned int i; kmem_cache_t *cache = 0; + unsigned long flags; + irq_save(flags); + for(i=0; icache; slub_free(cache, page, addr); + + irq_restore(flags); } kmem_cache_t *kmem_cache_create(const char *name, size_t size, size_t align) { + kmem_cache_t *cache = kmalloc(sizeof(kmem_cache_t), 0); if(cache == 0) return 0; + unsigned long flags; + irq_save(flags); + kmem_cache_init(cache, name, size, align); list_add(&(cache->list), &slub_caches); + irq_restore(flags); + return cache; } diff --git a/setup/setup.c b/setup/setup.c index 4fff3bd..a8a27e1 100644 --- a/setup/setup.c +++ b/setup/setup.c @@ -34,7 +34,7 @@ extern void setup_ext2(); extern void reboot(); -#define HZ 100 +#define HZ 10 #define CLOCK_TICK_RATE 1193180 #define LATCH ((CLOCK_TICK_RATE + HZ/2) / HZ) diff --git a/setup/system.c b/setup/system.c index 77540f3..6b27976 100644 --- a/setup/system.c +++ b/setup/system.c @@ -124,8 +124,9 @@ void setup_irqs() } for(i=0; i<16; i++) - enable_irq(i); - asm("sti"); + open_irq(i); + + enable_irq(); } void set_tss() -- 2.44.0