From: acevest Date: Sun, 22 Jun 2014 07:35:14 +0000 (+0800) Subject: add semaphore X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=3058aec2fd181cd96b307ca584a075e76e246a28;p=kernel.git add semaphore --- diff --git a/.gitignore b/.gitignore index 8953fc4..27fa5fc 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ *.BIN *.map *.diff +*.swp bin/hw bin/sh a.* diff --git a/drivers/ahci.c b/drivers/ahci.c deleted file mode 100644 index fd112c9..0000000 --- a/drivers/ahci.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * ------------------------------------------------------------------------ - * File Name: ahci.c - * Author: Zhao Yanbai - * Sat May 24 20:05:49 2014 - * Description: none - * ------------------------------------------------------------------------ - */ - -#include -#include -#include -#include -#include -#include -#include - - -ahci_init() -{ - - - -} diff --git a/drivers/ide.c b/drivers/ide.c index 2d3bca3..f5556df 100644 --- a/drivers/ide.c +++ b/drivers/ide.c @@ -15,6 +15,7 @@ #include #include #include +#include unsigned int HD_CHL0_CMD_BASE = 0x1F0; unsigned int HD_CHL1_CMD_BASE = 0x170; @@ -101,6 +102,8 @@ void ide_status() printk(" ide status %02x pci status %02x\n", idest, pcist); } + + void ide_debug() { u32 device; @@ -116,6 +119,13 @@ void ide_debug() printk("ide_debug\n"); } +DECLARE_MUTEX(mutex); +void debug_sem() +{ + down(&mutex); + ide_debug(); +} + void init_pci_controller(unsigned int vendor, unsigned int device) { pci_device_t *pci = pci_find_device(vendor, device); @@ -147,10 +157,11 @@ void ide_irq() insl(REG_DATA(0), buf, 512>>2); u16_t *s = (u16_t *) (buf+510); printk("hard disk data %04x\n", *s); + up(&mutex); } -void print_ide_identify(const char *buf) +void print_ide_identify(const char *buf) { char *p; short *ident; diff --git a/include/atomic.h b/include/atomic.h new file mode 100644 index 0000000..f23f7f3 --- /dev/null +++ b/include/atomic.h @@ -0,0 +1,16 @@ +/* + * ------------------------------------------------------------------------ + * File Name: atomic.h + * Author: Zhao Yanbai + * Sat Jun 21 18:37:21 2014 + * Description: none + * ------------------------------------------------------------------------ + */ + +#pragma once + +#define atomic_inc(x) __sync_add_and_fetch((x),1) +#define atomic_dec(x) __sync_sub_and_fetch((x),1) +#define atomic_add(x,y) __sync_add_and_fetch((x),(y)) +#define atomic_sub(x,y) __sync_sub_and_fetch((x),(y)) + diff --git a/include/fs.h b/include/fs.h index 0afecd1..b55d307 100644 --- a/include/fs.h +++ b/include/fs.h @@ -10,7 +10,7 @@ *-------------------------------------------------------------------------- */ -#ifndef _FS_H +#ifndef _FS_H #define _FS_H #include diff --git a/include/list.h b/include/list.h index 0e6145d..f57d998 100644 --- a/include/list.h +++ b/include/list.h @@ -37,6 +37,10 @@ do{ \ #define list_entry(ptr, type, member) \ ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) +#define list_first_entry(ptr, type, member) \ + list_entry((ptr)->next, type, member) + + #define list_for_each(pos, head) \ for(pos = (head)->next; pos != (head); pos = pos->next) @@ -85,3 +89,4 @@ static inline int list_empty(list_head_t *head) { return head->next == head; } + diff --git a/include/sched.h b/include/sched.h index 4073ba2..b6d104a 100644 --- a/include/sched.h +++ b/include/sched.h @@ -14,30 +14,17 @@ *-------------------------------------------------------------------------- */ -#ifndef _SCHED_H -#define _SCHED_H +#pragma once #include -#define NR_TASKS 3 -//task_union * tTasks[NR_TASKS]; -//void add_task(); -//void SetupTasks(); -//void test_taskA(); -//void test_taskB(); -//unsigned long schedule(pt_regs_t * regs); -unsigned long schedule(); +#include -pid_t get_next_pid(); -void init_tsk_cr3(task_union *); +#define FORK_USER 0 +#define FORK_KRNL 1 +unsigned long schedule(); inline void wake_up(wait_queue_t * wq); inline void sleep_on(wait_queue_t * wq); -#define TASK_CNT 64 - extern task_union root_task; - -#define FORK_KRNL 1 - -#endif //_SCHED_H diff --git a/include/semaphore.h b/include/semaphore.h new file mode 100644 index 0000000..50d76be --- /dev/null +++ b/include/semaphore.h @@ -0,0 +1,32 @@ +/* + * ------------------------------------------------------------------------ + * File Name: semaphore.h + * Author: Zhao Yanbai + * Sun Jun 22 13:53:18 2014 + * Description: none + * ------------------------------------------------------------------------ + */ + +#pragma once + +#include +#include +#include + +typedef struct semaphore +{ + volatile unsigned int cnt; + list_head_t wait_list; +} semaphore_t; + +#define SEMAPHORE_INIT(name, n) \ +{ \ + .cnt = (n), \ + .wait_list = LIST_HEAD_INIT((name).wait_list) \ +} + +#define DECLARE_MUTEX(name) \ + semaphore_t name = SEMAPHORE_INIT(name, 1) + +void down(semaphore_t *s); +void up(semaphore_t *s); diff --git a/include/task.h b/include/task.h index a59e4bd..0ab883a 100644 --- a/include/task.h +++ b/include/task.h @@ -23,15 +23,15 @@ #include #include #include -#include #include enum { TASK_UNUSED, TASK_RUNNING, - TASK_UNINTERRUPTIBLE, - TASK_INTERRUPTIBLE, + TASK_WAIT, + //TASK_UNINTERRUPTIBLE, + //TASK_INTERRUPTIBLE, TASK_EXITING }; @@ -41,7 +41,6 @@ typedef union task_union { unsigned long preempt_cnt; - unsigned long esp0; /* kernel stack */ /* for context switch */ @@ -60,8 +59,6 @@ typedef union task_union list_head_t list; - wait_queue_t wait; - pFile fps[NR_OPENS]; }; @@ -71,7 +68,6 @@ typedef union task_union task_union *alloc_task_union(); - static inline task_union *get_current() { task_union *tsk; @@ -85,9 +81,6 @@ static inline task_union *get_current() #define TASK_INIT_WEIGHT 10 -extern ListHead tsk_list; - -#define add_tsk2list(tsk) list_add_tail((&(tsk)->list), &tsk_list) #define get_tsk_from_list(p) list_entry((p), Task, list) #define del_tsk_from_list(tsk) list_del((&tsk->list)) #endif diff --git a/include/wait.h b/include/wait.h index a7b32cd..199bd3c 100644 --- a/include/wait.h +++ b/include/wait.h @@ -13,13 +13,37 @@ #pragma once #include +#include +#include typedef struct { - list_head_t wait; - + list_head_t task_list; } wait_queue_head_t; -typedef list_head_t wait_queue_t; +typedef struct +{ + task_union *task; + list_head_t task_list; +} wait_queue_t; + +#define WAIT_QUEUE_HEAD_INITIALIZER(name) \ +{ \ + .task_list = LIST_HEAD_INIT((name).task_list) \ +} + +#define DECLARE_WAIT_QUEUE_HEAD(name) \ + wait_queue_head_t name = WAIT_QUEUE_HEAD_INITIALIZER(name) + +#define WAIT_QUEUE_INITIALIZER(name) \ +{ \ + .task = tsk, \ + .task_list = LIST_HEAD_INIT((name).task_list) \ +} + +#define DECLARE_WAIT_QUEUE(name) \ + wait_queue_t name = WAIT_QUEUE_INITIALIZER(name) + void init_wait_queue(wait_queue_head_t * wqh); + diff --git a/kernel/fork.c b/kernel/fork.c index d593a60..fb40c6b 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -19,6 +19,7 @@ int sysc_fork(pt_regs_t regs) extern void ret_from_fork_user(); extern void ret_from_fork_krnl(); +extern pid_t get_next_pid(); int do_fork(pt_regs_t *regs, unsigned long flags) { diff --git a/kernel/init.c b/kernel/init.c index eca9520..7711f45 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -21,13 +21,22 @@ Desc gdt[NGDT]; char __initdata kernel_init_stack[KRNL_INIT_STACK_SIZE] __attribute__ ((__aligned__(PAGE_SIZE))); -static unsigned int eid = 0; +static unsigned int eid = 1; +void debug_sem(); void init_task_entry() { printk("hahahha %s\n", __func__); unsigned int id = eid++; + int i = 0; while(1) { + i++; + if(i == id*100) + { + printk("---READ---%d\n", id); + debug_sem(); + printk("---END----%d\n", id); + } printk("%d", id); asm("sti;hlt;"); } diff --git a/kernel/sched.c b/kernel/sched.c index ddf5c40..1e61176 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -91,15 +91,6 @@ task_union *alloc_task_union() } -task_union *get_unused_task_pcb() -{ - unsigned int i; - for(i=0; ilist.next, task_union, list); - p->state = (p->state == TASK_RUNNING) ? TASK_INTERRUPTIBLE : TASK_RUNNING; + p->state = (p->state == TASK_RUNNING) ? TASK_WAIT: TASK_RUNNING; } diff --git a/kernel/semaphore.c b/kernel/semaphore.c new file mode 100644 index 0000000..8be4d05 --- /dev/null +++ b/kernel/semaphore.c @@ -0,0 +1,90 @@ +/* + * ------------------------------------------------------------------------ + * File Name: semaphore.c + * Author: Zhao Yanbai + * Sun Jun 22 13:57:18 2014 + * Description: none + * ------------------------------------------------------------------------ + */ +#include +#include + +typedef struct semaphore_waiter +{ + list_head_t list; + task_union *task; + int up; +} semaphore_waiter_t; + +#define SEMAPHORE_WAITER_INITIALIZER(name, task) \ +{ \ + .list = LIST_HEAD_INIT((name).list), \ + .task = task, \ + .up = 0 \ +} + +#define DECLARE_SEMAPHORE_WAITER(name, task) \ + semaphore_waiter_t name = SEMAPHORE_WAITER_INITIALIZER(name, task) + + +void __down(semaphore_t *s) +{ + task_union *task = current; + DECLARE_SEMAPHORE_WAITER(waiter, task); + list_add_tail(&waiter.list, &s->wait_list); + + while(true) + { + task->state = TASK_WAIT; + + enable_irq(); + schedule(); + disable_irq(); + + if(waiter.up) + break; + } +} + +void down(semaphore_t *s) +{ + unsigned long iflags; + + irq_save(iflags); + + if(likely(s->cnt>0)) + { + s->cnt --; + } + else + { + __down(s); + } + + irq_restore(iflags); +} + +void __up(semaphore_t *s) +{ + semaphore_waiter_t *waiter = list_first_entry(&s->wait_list, semaphore_waiter_t, list); + list_del(&waiter->list); + waiter->up = 1; +} + + +void up(semaphore_t *s) +{ + unsigned long iflags; + + irq_save(iflags); + if(likely(list_empty(&s->wait_list))) + { + s->cnt ++; + } + else + { + __up(s); + } + + irq_restore(iflags); +} diff --git a/kernel/test_task.c b/kernel/test_task.c deleted file mode 100644 index 1a15a16..0000000 --- a/kernel/test_task.c +++ /dev/null @@ -1,132 +0,0 @@ -/* - *-------------------------------------------------------------------------- - * File Name: test_task.c - * - * Author: Zhao Yanbai [zhaoyanbai@126.com] - * Tue Feb 2 20:18:05 2010 - * - * Description: none - * - *-------------------------------------------------------------------------- - */ - - -#include -#include -#include -#include -task_union * tTasks[NR_TASKS]; - -#if 0 -void SetuptTasks() -{ - int i; - - - for(i=0; ipid = get_next_pid(); - tsk->ppid = 0; - init_tsk_cr3(tsk); - - pt_regs_t * r; - r = &tsk->regs; - memset((void *)r, 0, sizeof(pt_regs_t)); - r->ds = r->es = r->fs = r->gs = SELECTOR_USER_DS; - r->eip = (unsigned long)fun; - r->cs = SELECTOR_USER_CS; - r->eflags = 0x282; - r->esp = (unsigned long)tsk; - r->ss = SELECTOR_USER_SS; - - add_tsk2list(tsk); -#endif -} - -#if 0 -void add_task(void *fun) -{ - assert(fun != NULL); - task_union * tsk = NULL; - int i=0; - for(i=0; iregs;//(pt_regs_t *)(TASK_SIZE + (unsigned long)tsk); - //printk("Add Tsk: tsk:%08x r:%08x ", tsk, r); - //r--; - //printk("r:%08x sizeof regs:%x ", r, sizeof(pt_regs_t)); - - memset((void *)r, 0, sizeof(pt_regs_t)); - //printk("USER CS: %x\n", SELECTOR_USER_CS); - //printk("USER DS: %x\n", SELECTOR_USER_DS); - r->ds = r->es = r->fs = r->gs = SELECTOR_USER_DS; - r->eip = (unsigned long)fun; - r->cs = SELECTOR_USER_CS; - r->eflags = 0x282; - r->esp = (unsigned long)tsk; - r->ss = SELECTOR_USER_SS; - - - tTasks[i] = tsk; -} -#endif - -void delay(unsigned int d) -{ - unsigned int i; - int n = 10000; - for(i=0; i -void init_wait_queue(wait_queue_head_t * wqh) +void init_wait_queue(wait_queue_head_t *wqh) { - INIT_LIST_HEAD(&wqh->wait); + INIT_LIST_HEAD(&wqh->task_list); } -void add_wait_queue(wait_queue_head_t * wqh, wait_queue_t * wq) +void add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) { - list_add_tail(wq, &wqh->wait); + unsigned long iflags; + irq_save(iflags); + list_add_tail(&wq->task_list, &wqh->task_list); + irq_restore(iflags); } -void del_wait_queue(wait_queue_head_t * wqh, wait_queue_t * old) +void del_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) { - //list_del_init(); + unsigned long iflags; + irq_save(iflags); + list_del(&wq->task_list); + irq_restore(iflags); } + diff --git a/setup/setup.c b/setup/setup.c index 146cd08..8c26766 100644 --- a/setup/setup.c +++ b/setup/setup.c @@ -82,7 +82,6 @@ void setup_kernel() void ide_init(); ide_init(); - ahci_init(); printk("%s\n", version);